connect.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2009
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/list.h>
  25. #include <linux/wait.h>
  26. #include <linux/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/ctype.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mempool.h>
  31. #include <linux/delay.h>
  32. #include <linux/completion.h>
  33. #include <linux/kthread.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/freezer.h>
  36. #include <linux/namei.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/processor.h>
  39. #include <linux/inet.h>
  40. #include <net/ipv6.h>
  41. #include "cifspdu.h"
  42. #include "cifsglob.h"
  43. #include "cifsproto.h"
  44. #include "cifs_unicode.h"
  45. #include "cifs_debug.h"
  46. #include "cifs_fs_sb.h"
  47. #include "ntlmssp.h"
  48. #include "nterr.h"
  49. #include "rfc1002pdu.h"
  50. #include "fscache.h"
  51. #define CIFS_PORT 445
  52. #define RFC1001_PORT 139
  53. /* SMB echo "timeout" -- FIXME: tunable? */
  54. #define SMB_ECHO_INTERVAL (60 * HZ)
  55. extern mempool_t *cifs_req_poolp;
  56. /* FIXME: should these be tunable? */
  57. #define TLINK_ERROR_EXPIRE (1 * HZ)
  58. #define TLINK_IDLE_EXPIRE (600 * HZ)
  59. static int ip_connect(struct TCP_Server_Info *server);
  60. static int generic_ip_connect(struct TCP_Server_Info *server);
  61. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  62. static void cifs_prune_tlinks(struct work_struct *work);
  63. /*
  64. * cifs tcp session reconnection
  65. *
  66. * mark tcp session as reconnecting so temporarily locked
  67. * mark all smb sessions as reconnecting for tcp session
  68. * reconnect tcp session
  69. * wake up waiters on reconnection? - (not needed currently)
  70. */
  71. static int
  72. cifs_reconnect(struct TCP_Server_Info *server)
  73. {
  74. int rc = 0;
  75. struct list_head *tmp, *tmp2;
  76. struct cifs_ses *ses;
  77. struct cifs_tcon *tcon;
  78. struct mid_q_entry *mid_entry;
  79. struct list_head retry_list;
  80. spin_lock(&GlobalMid_Lock);
  81. if (server->tcpStatus == CifsExiting) {
  82. /* the demux thread will exit normally
  83. next time through the loop */
  84. spin_unlock(&GlobalMid_Lock);
  85. return rc;
  86. } else
  87. server->tcpStatus = CifsNeedReconnect;
  88. spin_unlock(&GlobalMid_Lock);
  89. server->maxBuf = 0;
  90. cFYI(1, "Reconnecting tcp session");
  91. /* before reconnecting the tcp session, mark the smb session (uid)
  92. and the tid bad so they are not used until reconnected */
  93. cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
  94. spin_lock(&cifs_tcp_ses_lock);
  95. list_for_each(tmp, &server->smb_ses_list) {
  96. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  97. ses->need_reconnect = true;
  98. ses->ipc_tid = 0;
  99. list_for_each(tmp2, &ses->tcon_list) {
  100. tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
  101. tcon->need_reconnect = true;
  102. }
  103. }
  104. spin_unlock(&cifs_tcp_ses_lock);
  105. /* do not want to be sending data on a socket we are freeing */
  106. cFYI(1, "%s: tearing down socket", __func__);
  107. mutex_lock(&server->srv_mutex);
  108. if (server->ssocket) {
  109. cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
  110. server->ssocket->flags);
  111. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  112. cFYI(1, "Post shutdown state: 0x%x Flags: 0x%lx",
  113. server->ssocket->state,
  114. server->ssocket->flags);
  115. sock_release(server->ssocket);
  116. server->ssocket = NULL;
  117. }
  118. server->sequence_number = 0;
  119. server->session_estab = false;
  120. kfree(server->session_key.response);
  121. server->session_key.response = NULL;
  122. server->session_key.len = 0;
  123. server->lstrp = jiffies;
  124. mutex_unlock(&server->srv_mutex);
  125. /* mark submitted MIDs for retry and issue callback */
  126. INIT_LIST_HEAD(&retry_list);
  127. cFYI(1, "%s: moving mids to private list", __func__);
  128. spin_lock(&GlobalMid_Lock);
  129. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  130. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  131. if (mid_entry->midState == MID_REQUEST_SUBMITTED)
  132. mid_entry->midState = MID_RETRY_NEEDED;
  133. list_move(&mid_entry->qhead, &retry_list);
  134. }
  135. spin_unlock(&GlobalMid_Lock);
  136. cFYI(1, "%s: issuing mid callbacks", __func__);
  137. list_for_each_safe(tmp, tmp2, &retry_list) {
  138. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  139. list_del_init(&mid_entry->qhead);
  140. mid_entry->callback(mid_entry);
  141. }
  142. while (server->tcpStatus == CifsNeedReconnect) {
  143. try_to_freeze();
  144. /* we should try only the port we connected to before */
  145. rc = generic_ip_connect(server);
  146. if (rc) {
  147. cFYI(1, "reconnect error %d", rc);
  148. msleep(3000);
  149. } else {
  150. atomic_inc(&tcpSesReconnectCount);
  151. spin_lock(&GlobalMid_Lock);
  152. if (server->tcpStatus != CifsExiting)
  153. server->tcpStatus = CifsNeedNegotiate;
  154. spin_unlock(&GlobalMid_Lock);
  155. }
  156. }
  157. return rc;
  158. }
  159. /*
  160. return codes:
  161. 0 not a transact2, or all data present
  162. >0 transact2 with that much data missing
  163. -EINVAL = invalid transact2
  164. */
  165. static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
  166. {
  167. struct smb_t2_rsp *pSMBt;
  168. int remaining;
  169. __u16 total_data_size, data_in_this_rsp;
  170. if (pSMB->Command != SMB_COM_TRANSACTION2)
  171. return 0;
  172. /* check for plausible wct, bcc and t2 data and parm sizes */
  173. /* check for parm and data offset going beyond end of smb */
  174. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  175. cFYI(1, "invalid transact2 word count");
  176. return -EINVAL;
  177. }
  178. pSMBt = (struct smb_t2_rsp *)pSMB;
  179. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  180. data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  181. if (total_data_size == data_in_this_rsp)
  182. return 0;
  183. else if (total_data_size < data_in_this_rsp) {
  184. cFYI(1, "total data %d smaller than data in frame %d",
  185. total_data_size, data_in_this_rsp);
  186. return -EINVAL;
  187. }
  188. remaining = total_data_size - data_in_this_rsp;
  189. cFYI(1, "missing %d bytes from transact2, check next response",
  190. remaining);
  191. if (total_data_size > maxBufSize) {
  192. cERROR(1, "TotalDataSize %d is over maximum buffer %d",
  193. total_data_size, maxBufSize);
  194. return -EINVAL;
  195. }
  196. return remaining;
  197. }
  198. static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
  199. {
  200. struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
  201. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
  202. char *data_area_of_target;
  203. char *data_area_of_buf2;
  204. int remaining;
  205. unsigned int byte_count, total_in_buf;
  206. __u16 total_data_size, total_in_buf2;
  207. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  208. if (total_data_size !=
  209. get_unaligned_le16(&pSMB2->t2_rsp.TotalDataCount))
  210. cFYI(1, "total data size of primary and secondary t2 differ");
  211. total_in_buf = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  212. remaining = total_data_size - total_in_buf;
  213. if (remaining < 0)
  214. return -EPROTO;
  215. if (remaining == 0) /* nothing to do, ignore */
  216. return 0;
  217. total_in_buf2 = get_unaligned_le16(&pSMB2->t2_rsp.DataCount);
  218. if (remaining < total_in_buf2) {
  219. cFYI(1, "transact2 2nd response contains too much data");
  220. }
  221. /* find end of first SMB data area */
  222. data_area_of_target = (char *)&pSMBt->hdr.Protocol +
  223. get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
  224. /* validate target area */
  225. data_area_of_buf2 = (char *)&pSMB2->hdr.Protocol +
  226. get_unaligned_le16(&pSMB2->t2_rsp.DataOffset);
  227. data_area_of_target += total_in_buf;
  228. /* copy second buffer into end of first buffer */
  229. total_in_buf += total_in_buf2;
  230. /* is the result too big for the field? */
  231. if (total_in_buf > USHRT_MAX)
  232. return -EPROTO;
  233. put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
  234. /* fix up the BCC */
  235. byte_count = get_bcc(pTargetSMB);
  236. byte_count += total_in_buf2;
  237. /* is the result too big for the field? */
  238. if (byte_count > USHRT_MAX)
  239. return -EPROTO;
  240. put_bcc(byte_count, pTargetSMB);
  241. byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
  242. byte_count += total_in_buf2;
  243. /* don't allow buffer to overflow */
  244. if (byte_count > CIFSMaxBufSize)
  245. return -ENOBUFS;
  246. pTargetSMB->smb_buf_length = cpu_to_be32(byte_count);
  247. memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
  248. if (remaining == total_in_buf2) {
  249. cFYI(1, "found the last secondary response");
  250. return 0; /* we are done */
  251. } else /* more responses to go */
  252. return 1;
  253. }
  254. static void
  255. cifs_echo_request(struct work_struct *work)
  256. {
  257. int rc;
  258. struct TCP_Server_Info *server = container_of(work,
  259. struct TCP_Server_Info, echo.work);
  260. /*
  261. * We cannot send an echo until the NEGOTIATE_PROTOCOL request is
  262. * done, which is indicated by maxBuf != 0. Also, no need to ping if
  263. * we got a response recently
  264. */
  265. if (server->maxBuf == 0 ||
  266. time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
  267. goto requeue_echo;
  268. rc = CIFSSMBEcho(server);
  269. if (rc)
  270. cFYI(1, "Unable to send echo request to server: %s",
  271. server->hostname);
  272. requeue_echo:
  273. queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
  274. }
  275. static int
  276. cifs_demultiplex_thread(struct TCP_Server_Info *server)
  277. {
  278. int length;
  279. unsigned int pdu_length, total_read;
  280. struct smb_hdr *smb_buffer = NULL;
  281. struct smb_hdr *bigbuf = NULL;
  282. struct smb_hdr *smallbuf = NULL;
  283. struct msghdr smb_msg;
  284. struct kvec iov;
  285. struct socket *csocket = server->ssocket;
  286. struct list_head *tmp, *tmp2;
  287. struct task_struct *task_to_wake = NULL;
  288. struct mid_q_entry *mid_entry;
  289. char temp;
  290. bool isLargeBuf = false;
  291. bool isMultiRsp;
  292. int reconnect;
  293. current->flags |= PF_MEMALLOC;
  294. cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
  295. length = atomic_inc_return(&tcpSesAllocCount);
  296. if (length > 1)
  297. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  298. GFP_KERNEL);
  299. set_freezable();
  300. while (server->tcpStatus != CifsExiting) {
  301. if (try_to_freeze())
  302. continue;
  303. if (bigbuf == NULL) {
  304. bigbuf = cifs_buf_get();
  305. if (!bigbuf) {
  306. cERROR(1, "No memory for large SMB response");
  307. msleep(3000);
  308. /* retry will check if exiting */
  309. continue;
  310. }
  311. } else if (isLargeBuf) {
  312. /* we are reusing a dirty large buf, clear its start */
  313. memset(bigbuf, 0, sizeof(struct smb_hdr));
  314. }
  315. if (smallbuf == NULL) {
  316. smallbuf = cifs_small_buf_get();
  317. if (!smallbuf) {
  318. cERROR(1, "No memory for SMB response");
  319. msleep(1000);
  320. /* retry will check if exiting */
  321. continue;
  322. }
  323. /* beginning of smb buffer is cleared in our buf_get */
  324. } else /* if existing small buf clear beginning */
  325. memset(smallbuf, 0, sizeof(struct smb_hdr));
  326. isLargeBuf = false;
  327. isMultiRsp = false;
  328. smb_buffer = smallbuf;
  329. iov.iov_base = smb_buffer;
  330. iov.iov_len = 4;
  331. smb_msg.msg_control = NULL;
  332. smb_msg.msg_controllen = 0;
  333. pdu_length = 4; /* enough to get RFC1001 header */
  334. incomplete_rcv:
  335. if (echo_retries > 0 && server->tcpStatus == CifsGood &&
  336. time_after(jiffies, server->lstrp +
  337. (echo_retries * SMB_ECHO_INTERVAL))) {
  338. cERROR(1, "Server %s has not responded in %d seconds. "
  339. "Reconnecting...", server->hostname,
  340. (echo_retries * SMB_ECHO_INTERVAL / HZ));
  341. cifs_reconnect(server);
  342. csocket = server->ssocket;
  343. wake_up(&server->response_q);
  344. continue;
  345. }
  346. length =
  347. kernel_recvmsg(csocket, &smb_msg,
  348. &iov, 1, pdu_length, 0 /* BB other flags? */);
  349. if (server->tcpStatus == CifsExiting) {
  350. break;
  351. } else if (server->tcpStatus == CifsNeedReconnect) {
  352. cFYI(1, "Reconnect after server stopped responding");
  353. cifs_reconnect(server);
  354. cFYI(1, "call to reconnect done");
  355. csocket = server->ssocket;
  356. continue;
  357. } else if (length == -ERESTARTSYS ||
  358. length == -EAGAIN ||
  359. length == -EINTR) {
  360. msleep(1); /* minimum sleep to prevent looping
  361. allowing socket to clear and app threads to set
  362. tcpStatus CifsNeedReconnect if server hung */
  363. if (pdu_length < 4) {
  364. iov.iov_base = (4 - pdu_length) +
  365. (char *)smb_buffer;
  366. iov.iov_len = pdu_length;
  367. smb_msg.msg_control = NULL;
  368. smb_msg.msg_controllen = 0;
  369. goto incomplete_rcv;
  370. } else
  371. continue;
  372. } else if (length <= 0) {
  373. cFYI(1, "Reconnect after unexpected peek error %d",
  374. length);
  375. cifs_reconnect(server);
  376. csocket = server->ssocket;
  377. wake_up(&server->response_q);
  378. continue;
  379. } else if (length < pdu_length) {
  380. cFYI(1, "requested %d bytes but only got %d bytes",
  381. pdu_length, length);
  382. pdu_length -= length;
  383. msleep(1);
  384. goto incomplete_rcv;
  385. }
  386. /* The right amount was read from socket - 4 bytes */
  387. /* so we can now interpret the length field */
  388. /* the first byte big endian of the length field,
  389. is actually not part of the length but the type
  390. with the most common, zero, as regular data */
  391. temp = *((char *) smb_buffer);
  392. /* Note that FC 1001 length is big endian on the wire,
  393. but we convert it here so it is always manipulated
  394. as host byte order */
  395. pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
  396. cFYI(1, "rfc1002 length 0x%x", pdu_length+4);
  397. if (temp == (char) RFC1002_SESSION_KEEP_ALIVE) {
  398. continue;
  399. } else if (temp == (char)RFC1002_POSITIVE_SESSION_RESPONSE) {
  400. cFYI(1, "Good RFC 1002 session rsp");
  401. continue;
  402. } else if (temp == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) {
  403. /* we get this from Windows 98 instead of
  404. an error on SMB negprot response */
  405. cFYI(1, "Negative RFC1002 Session Response Error 0x%x)",
  406. pdu_length);
  407. /* give server a second to clean up */
  408. msleep(1000);
  409. /* always try 445 first on reconnect since we get NACK
  410. * on some if we ever connected to port 139 (the NACK
  411. * is since we do not begin with RFC1001 session
  412. * initialize frame)
  413. */
  414. cifs_set_port((struct sockaddr *)
  415. &server->dstaddr, CIFS_PORT);
  416. cifs_reconnect(server);
  417. csocket = server->ssocket;
  418. wake_up(&server->response_q);
  419. continue;
  420. } else if (temp != (char) 0) {
  421. cERROR(1, "Unknown RFC 1002 frame");
  422. cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
  423. length);
  424. cifs_reconnect(server);
  425. csocket = server->ssocket;
  426. continue;
  427. }
  428. /* else we have an SMB response */
  429. if ((pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) ||
  430. (pdu_length < sizeof(struct smb_hdr) - 1 - 4)) {
  431. cERROR(1, "Invalid size SMB length %d pdu_length %d",
  432. length, pdu_length+4);
  433. cifs_reconnect(server);
  434. csocket = server->ssocket;
  435. wake_up(&server->response_q);
  436. continue;
  437. }
  438. /* else length ok */
  439. reconnect = 0;
  440. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  441. isLargeBuf = true;
  442. memcpy(bigbuf, smallbuf, 4);
  443. smb_buffer = bigbuf;
  444. }
  445. length = 0;
  446. iov.iov_base = 4 + (char *)smb_buffer;
  447. iov.iov_len = pdu_length;
  448. for (total_read = 0; total_read < pdu_length;
  449. total_read += length) {
  450. length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
  451. pdu_length - total_read, 0);
  452. if (server->tcpStatus == CifsExiting) {
  453. /* then will exit */
  454. reconnect = 2;
  455. break;
  456. } else if (server->tcpStatus == CifsNeedReconnect) {
  457. cifs_reconnect(server);
  458. csocket = server->ssocket;
  459. /* Reconnect wakes up rspns q */
  460. /* Now we will reread sock */
  461. reconnect = 1;
  462. break;
  463. } else if (length == -ERESTARTSYS ||
  464. length == -EAGAIN ||
  465. length == -EINTR) {
  466. msleep(1); /* minimum sleep to prevent looping,
  467. allowing socket to clear and app
  468. threads to set tcpStatus
  469. CifsNeedReconnect if server hung*/
  470. length = 0;
  471. continue;
  472. } else if (length <= 0) {
  473. cERROR(1, "Received no data, expecting %d",
  474. pdu_length - total_read);
  475. cifs_reconnect(server);
  476. csocket = server->ssocket;
  477. reconnect = 1;
  478. break;
  479. }
  480. }
  481. if (reconnect == 2)
  482. break;
  483. else if (reconnect == 1)
  484. continue;
  485. total_read += 4; /* account for rfc1002 hdr */
  486. dump_smb(smb_buffer, total_read);
  487. /*
  488. * We know that we received enough to get to the MID as we
  489. * checked the pdu_length earlier. Now check to see
  490. * if the rest of the header is OK. We borrow the length
  491. * var for the rest of the loop to avoid a new stack var.
  492. *
  493. * 48 bytes is enough to display the header and a little bit
  494. * into the payload for debugging purposes.
  495. */
  496. length = checkSMB(smb_buffer, smb_buffer->Mid, total_read);
  497. if (length != 0)
  498. cifs_dump_mem("Bad SMB: ", smb_buffer,
  499. min_t(unsigned int, total_read, 48));
  500. mid_entry = NULL;
  501. server->lstrp = jiffies;
  502. spin_lock(&GlobalMid_Lock);
  503. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  504. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  505. if (mid_entry->mid != smb_buffer->Mid ||
  506. mid_entry->midState != MID_REQUEST_SUBMITTED ||
  507. mid_entry->command != smb_buffer->Command) {
  508. mid_entry = NULL;
  509. continue;
  510. }
  511. if (length == 0 &&
  512. check2ndT2(smb_buffer, server->maxBuf) > 0) {
  513. /* We have a multipart transact2 resp */
  514. isMultiRsp = true;
  515. if (mid_entry->resp_buf) {
  516. /* merge response - fix up 1st*/
  517. length = coalesce_t2(smb_buffer,
  518. mid_entry->resp_buf);
  519. if (length > 0) {
  520. length = 0;
  521. mid_entry->multiRsp = true;
  522. break;
  523. } else {
  524. /* all parts received or
  525. * packet is malformed
  526. */
  527. mid_entry->multiEnd = true;
  528. goto multi_t2_fnd;
  529. }
  530. } else {
  531. if (!isLargeBuf) {
  532. /*
  533. * FIXME: switch to already
  534. * allocated largebuf?
  535. */
  536. cERROR(1, "1st trans2 resp "
  537. "needs bigbuf");
  538. } else {
  539. /* Have first buffer */
  540. mid_entry->resp_buf =
  541. smb_buffer;
  542. mid_entry->largeBuf = true;
  543. bigbuf = NULL;
  544. }
  545. }
  546. break;
  547. }
  548. mid_entry->resp_buf = smb_buffer;
  549. mid_entry->largeBuf = isLargeBuf;
  550. multi_t2_fnd:
  551. if (length == 0)
  552. mid_entry->midState = MID_RESPONSE_RECEIVED;
  553. else
  554. mid_entry->midState = MID_RESPONSE_MALFORMED;
  555. #ifdef CONFIG_CIFS_STATS2
  556. mid_entry->when_received = jiffies;
  557. #endif
  558. list_del_init(&mid_entry->qhead);
  559. break;
  560. }
  561. spin_unlock(&GlobalMid_Lock);
  562. if (mid_entry != NULL) {
  563. mid_entry->callback(mid_entry);
  564. /* Was previous buf put in mpx struct for multi-rsp? */
  565. if (!isMultiRsp) {
  566. /* smb buffer will be freed by user thread */
  567. if (isLargeBuf)
  568. bigbuf = NULL;
  569. else
  570. smallbuf = NULL;
  571. }
  572. } else if (length != 0) {
  573. /* response sanity checks failed */
  574. continue;
  575. } else if (!is_valid_oplock_break(smb_buffer, server) &&
  576. !isMultiRsp) {
  577. cERROR(1, "No task to wake, unknown frame received! "
  578. "NumMids %d", atomic_read(&midCount));
  579. cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
  580. sizeof(struct smb_hdr));
  581. #ifdef CONFIG_CIFS_DEBUG2
  582. cifs_dump_detail(smb_buffer);
  583. cifs_dump_mids(server);
  584. #endif /* CIFS_DEBUG2 */
  585. }
  586. } /* end while !EXITING */
  587. /* take it off the list, if it's not already */
  588. spin_lock(&cifs_tcp_ses_lock);
  589. list_del_init(&server->tcp_ses_list);
  590. spin_unlock(&cifs_tcp_ses_lock);
  591. spin_lock(&GlobalMid_Lock);
  592. server->tcpStatus = CifsExiting;
  593. spin_unlock(&GlobalMid_Lock);
  594. wake_up_all(&server->response_q);
  595. /* check if we have blocked requests that need to free */
  596. /* Note that cifs_max_pending is normally 50, but
  597. can be set at module install time to as little as two */
  598. spin_lock(&GlobalMid_Lock);
  599. if (atomic_read(&server->inFlight) >= cifs_max_pending)
  600. atomic_set(&server->inFlight, cifs_max_pending - 1);
  601. /* We do not want to set the max_pending too low or we
  602. could end up with the counter going negative */
  603. spin_unlock(&GlobalMid_Lock);
  604. /* Although there should not be any requests blocked on
  605. this queue it can not hurt to be paranoid and try to wake up requests
  606. that may haven been blocked when more than 50 at time were on the wire
  607. to the same server - they now will see the session is in exit state
  608. and get out of SendReceive. */
  609. wake_up_all(&server->request_q);
  610. /* give those requests time to exit */
  611. msleep(125);
  612. if (server->ssocket) {
  613. sock_release(csocket);
  614. server->ssocket = NULL;
  615. }
  616. /* buffer usually freed in free_mid - need to free it here on exit */
  617. cifs_buf_release(bigbuf);
  618. if (smallbuf) /* no sense logging a debug message if NULL */
  619. cifs_small_buf_release(smallbuf);
  620. if (!list_empty(&server->pending_mid_q)) {
  621. struct list_head dispose_list;
  622. INIT_LIST_HEAD(&dispose_list);
  623. spin_lock(&GlobalMid_Lock);
  624. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  625. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  626. cFYI(1, "Clearing mid 0x%x", mid_entry->mid);
  627. mid_entry->midState = MID_SHUTDOWN;
  628. list_move(&mid_entry->qhead, &dispose_list);
  629. }
  630. spin_unlock(&GlobalMid_Lock);
  631. /* now walk dispose list and issue callbacks */
  632. list_for_each_safe(tmp, tmp2, &dispose_list) {
  633. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  634. cFYI(1, "Callback mid 0x%x", mid_entry->mid);
  635. list_del_init(&mid_entry->qhead);
  636. mid_entry->callback(mid_entry);
  637. }
  638. /* 1/8th of sec is more than enough time for them to exit */
  639. msleep(125);
  640. }
  641. if (!list_empty(&server->pending_mid_q)) {
  642. /* mpx threads have not exited yet give them
  643. at least the smb send timeout time for long ops */
  644. /* due to delays on oplock break requests, we need
  645. to wait at least 45 seconds before giving up
  646. on a request getting a response and going ahead
  647. and killing cifsd */
  648. cFYI(1, "Wait for exit from demultiplex thread");
  649. msleep(46000);
  650. /* if threads still have not exited they are probably never
  651. coming home not much else we can do but free the memory */
  652. }
  653. kfree(server->hostname);
  654. task_to_wake = xchg(&server->tsk, NULL);
  655. kfree(server);
  656. length = atomic_dec_return(&tcpSesAllocCount);
  657. if (length > 0)
  658. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  659. GFP_KERNEL);
  660. /* if server->tsk was NULL then wait for a signal before exiting */
  661. if (!task_to_wake) {
  662. set_current_state(TASK_INTERRUPTIBLE);
  663. while (!signal_pending(current)) {
  664. schedule();
  665. set_current_state(TASK_INTERRUPTIBLE);
  666. }
  667. set_current_state(TASK_RUNNING);
  668. }
  669. module_put_and_exit(0);
  670. }
  671. /* extract the host portion of the UNC string */
  672. static char *
  673. extract_hostname(const char *unc)
  674. {
  675. const char *src;
  676. char *dst, *delim;
  677. unsigned int len;
  678. /* skip double chars at beginning of string */
  679. /* BB: check validity of these bytes? */
  680. src = unc + 2;
  681. /* delimiter between hostname and sharename is always '\\' now */
  682. delim = strchr(src, '\\');
  683. if (!delim)
  684. return ERR_PTR(-EINVAL);
  685. len = delim - src;
  686. dst = kmalloc((len + 1), GFP_KERNEL);
  687. if (dst == NULL)
  688. return ERR_PTR(-ENOMEM);
  689. memcpy(dst, src, len);
  690. dst[len] = '\0';
  691. return dst;
  692. }
  693. static int
  694. cifs_parse_mount_options(const char *mountdata, const char *devname,
  695. struct smb_vol *vol)
  696. {
  697. char *value, *data, *end;
  698. char *mountdata_copy, *options;
  699. unsigned int temp_len, i, j;
  700. char separator[2];
  701. short int override_uid = -1;
  702. short int override_gid = -1;
  703. bool uid_specified = false;
  704. bool gid_specified = false;
  705. char *nodename = utsname()->nodename;
  706. separator[0] = ',';
  707. separator[1] = 0;
  708. /*
  709. * does not have to be perfect mapping since field is
  710. * informational, only used for servers that do not support
  711. * port 445 and it can be overridden at mount time
  712. */
  713. memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
  714. for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
  715. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  716. vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
  717. /* null target name indicates to use *SMBSERVR default called name
  718. if we end up sending RFC1001 session initialize */
  719. vol->target_rfc1001_name[0] = 0;
  720. vol->cred_uid = current_uid();
  721. vol->linux_uid = current_uid();
  722. vol->linux_gid = current_gid();
  723. /* default to only allowing write access to owner of the mount */
  724. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  725. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  726. /* default is always to request posix paths. */
  727. vol->posix_paths = 1;
  728. /* default to using server inode numbers where available */
  729. vol->server_ino = 1;
  730. vol->actimeo = CIFS_DEF_ACTIMEO;
  731. if (!mountdata)
  732. goto cifs_parse_mount_err;
  733. mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
  734. if (!mountdata_copy)
  735. goto cifs_parse_mount_err;
  736. options = mountdata_copy;
  737. end = options + strlen(options);
  738. if (strncmp(options, "sep=", 4) == 0) {
  739. if (options[4] != 0) {
  740. separator[0] = options[4];
  741. options += 5;
  742. } else {
  743. cFYI(1, "Null separator not allowed");
  744. }
  745. }
  746. while ((data = strsep(&options, separator)) != NULL) {
  747. if (!*data)
  748. continue;
  749. if ((value = strchr(data, '=')) != NULL)
  750. *value++ = '\0';
  751. /* Have to parse this before we parse for "user" */
  752. if (strnicmp(data, "user_xattr", 10) == 0) {
  753. vol->no_xattr = 0;
  754. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  755. vol->no_xattr = 1;
  756. } else if (strnicmp(data, "user", 4) == 0) {
  757. if (!value) {
  758. printk(KERN_WARNING
  759. "CIFS: invalid or missing username\n");
  760. goto cifs_parse_mount_err;
  761. } else if (!*value) {
  762. /* null user, ie anonymous, authentication */
  763. vol->nullauth = 1;
  764. }
  765. if (strnlen(value, MAX_USERNAME_SIZE) <
  766. MAX_USERNAME_SIZE) {
  767. vol->username = kstrdup(value, GFP_KERNEL);
  768. if (!vol->username) {
  769. printk(KERN_WARNING "CIFS: no memory "
  770. "for username\n");
  771. goto cifs_parse_mount_err;
  772. }
  773. } else {
  774. printk(KERN_WARNING "CIFS: username too long\n");
  775. goto cifs_parse_mount_err;
  776. }
  777. } else if (strnicmp(data, "pass", 4) == 0) {
  778. if (!value) {
  779. vol->password = NULL;
  780. continue;
  781. } else if (value[0] == 0) {
  782. /* check if string begins with double comma
  783. since that would mean the password really
  784. does start with a comma, and would not
  785. indicate an empty string */
  786. if (value[1] != separator[0]) {
  787. vol->password = NULL;
  788. continue;
  789. }
  790. }
  791. temp_len = strlen(value);
  792. /* removed password length check, NTLM passwords
  793. can be arbitrarily long */
  794. /* if comma in password, the string will be
  795. prematurely null terminated. Commas in password are
  796. specified across the cifs mount interface by a double
  797. comma ie ,, and a comma used as in other cases ie ','
  798. as a parameter delimiter/separator is single and due
  799. to the strsep above is temporarily zeroed. */
  800. /* NB: password legally can have multiple commas and
  801. the only illegal character in a password is null */
  802. if ((value[temp_len] == 0) &&
  803. (value + temp_len < end) &&
  804. (value[temp_len+1] == separator[0])) {
  805. /* reinsert comma */
  806. value[temp_len] = separator[0];
  807. temp_len += 2; /* move after second comma */
  808. while (value[temp_len] != 0) {
  809. if (value[temp_len] == separator[0]) {
  810. if (value[temp_len+1] ==
  811. separator[0]) {
  812. /* skip second comma */
  813. temp_len++;
  814. } else {
  815. /* single comma indicating start
  816. of next parm */
  817. break;
  818. }
  819. }
  820. temp_len++;
  821. }
  822. if (value[temp_len] == 0) {
  823. options = NULL;
  824. } else {
  825. value[temp_len] = 0;
  826. /* point option to start of next parm */
  827. options = value + temp_len + 1;
  828. }
  829. /* go from value to value + temp_len condensing
  830. double commas to singles. Note that this ends up
  831. allocating a few bytes too many, which is ok */
  832. vol->password = kzalloc(temp_len, GFP_KERNEL);
  833. if (vol->password == NULL) {
  834. printk(KERN_WARNING "CIFS: no memory "
  835. "for password\n");
  836. goto cifs_parse_mount_err;
  837. }
  838. for (i = 0, j = 0; i < temp_len; i++, j++) {
  839. vol->password[j] = value[i];
  840. if (value[i] == separator[0]
  841. && value[i+1] == separator[0]) {
  842. /* skip second comma */
  843. i++;
  844. }
  845. }
  846. vol->password[j] = 0;
  847. } else {
  848. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  849. if (vol->password == NULL) {
  850. printk(KERN_WARNING "CIFS: no memory "
  851. "for password\n");
  852. goto cifs_parse_mount_err;
  853. }
  854. strcpy(vol->password, value);
  855. }
  856. } else if (!strnicmp(data, "ip", 2) ||
  857. !strnicmp(data, "addr", 4)) {
  858. if (!value || !*value) {
  859. vol->UNCip = NULL;
  860. } else if (strnlen(value, INET6_ADDRSTRLEN) <
  861. INET6_ADDRSTRLEN) {
  862. vol->UNCip = kstrdup(value, GFP_KERNEL);
  863. if (!vol->UNCip) {
  864. printk(KERN_WARNING "CIFS: no memory "
  865. "for UNC IP\n");
  866. goto cifs_parse_mount_err;
  867. }
  868. } else {
  869. printk(KERN_WARNING "CIFS: ip address "
  870. "too long\n");
  871. goto cifs_parse_mount_err;
  872. }
  873. } else if (strnicmp(data, "sec", 3) == 0) {
  874. if (!value || !*value) {
  875. cERROR(1, "no security value specified");
  876. continue;
  877. } else if (strnicmp(value, "krb5i", 5) == 0) {
  878. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  879. CIFSSEC_MUST_SIGN;
  880. } else if (strnicmp(value, "krb5p", 5) == 0) {
  881. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  882. CIFSSEC_MAY_KRB5; */
  883. cERROR(1, "Krb5 cifs privacy not supported");
  884. goto cifs_parse_mount_err;
  885. } else if (strnicmp(value, "krb5", 4) == 0) {
  886. vol->secFlg |= CIFSSEC_MAY_KRB5;
  887. } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
  888. vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
  889. CIFSSEC_MUST_SIGN;
  890. } else if (strnicmp(value, "ntlmssp", 7) == 0) {
  891. vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
  892. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  893. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  894. CIFSSEC_MUST_SIGN;
  895. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  896. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  897. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  898. vol->secFlg |= CIFSSEC_MAY_NTLM |
  899. CIFSSEC_MUST_SIGN;
  900. } else if (strnicmp(value, "ntlm", 4) == 0) {
  901. /* ntlm is default so can be turned off too */
  902. vol->secFlg |= CIFSSEC_MAY_NTLM;
  903. } else if (strnicmp(value, "nontlm", 6) == 0) {
  904. /* BB is there a better way to do this? */
  905. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  906. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  907. } else if (strnicmp(value, "lanman", 6) == 0) {
  908. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  909. #endif
  910. } else if (strnicmp(value, "none", 4) == 0) {
  911. vol->nullauth = 1;
  912. } else {
  913. cERROR(1, "bad security option: %s", value);
  914. goto cifs_parse_mount_err;
  915. }
  916. } else if (strnicmp(data, "vers", 3) == 0) {
  917. if (!value || !*value) {
  918. cERROR(1, "no protocol version specified"
  919. " after vers= mount option");
  920. } else if ((strnicmp(value, "cifs", 4) == 0) ||
  921. (strnicmp(value, "1", 1) == 0)) {
  922. /* this is the default */
  923. continue;
  924. }
  925. } else if ((strnicmp(data, "unc", 3) == 0)
  926. || (strnicmp(data, "target", 6) == 0)
  927. || (strnicmp(data, "path", 4) == 0)) {
  928. if (!value || !*value) {
  929. printk(KERN_WARNING "CIFS: invalid path to "
  930. "network resource\n");
  931. goto cifs_parse_mount_err;
  932. }
  933. if ((temp_len = strnlen(value, 300)) < 300) {
  934. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  935. if (vol->UNC == NULL)
  936. goto cifs_parse_mount_err;
  937. strcpy(vol->UNC, value);
  938. if (strncmp(vol->UNC, "//", 2) == 0) {
  939. vol->UNC[0] = '\\';
  940. vol->UNC[1] = '\\';
  941. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  942. printk(KERN_WARNING
  943. "CIFS: UNC Path does not begin "
  944. "with // or \\\\ \n");
  945. goto cifs_parse_mount_err;
  946. }
  947. } else {
  948. printk(KERN_WARNING "CIFS: UNC name too long\n");
  949. goto cifs_parse_mount_err;
  950. }
  951. } else if ((strnicmp(data, "domain", 3) == 0)
  952. || (strnicmp(data, "workgroup", 5) == 0)) {
  953. if (!value || !*value) {
  954. printk(KERN_WARNING "CIFS: invalid domain name\n");
  955. goto cifs_parse_mount_err;
  956. }
  957. /* BB are there cases in which a comma can be valid in
  958. a domain name and need special handling? */
  959. if (strnlen(value, 256) < 256) {
  960. vol->domainname = kstrdup(value, GFP_KERNEL);
  961. if (!vol->domainname) {
  962. printk(KERN_WARNING "CIFS: no memory "
  963. "for domainname\n");
  964. goto cifs_parse_mount_err;
  965. }
  966. cFYI(1, "Domain name set");
  967. } else {
  968. printk(KERN_WARNING "CIFS: domain name too "
  969. "long\n");
  970. goto cifs_parse_mount_err;
  971. }
  972. } else if (strnicmp(data, "srcaddr", 7) == 0) {
  973. vol->srcaddr.ss_family = AF_UNSPEC;
  974. if (!value || !*value) {
  975. printk(KERN_WARNING "CIFS: srcaddr value"
  976. " not specified.\n");
  977. goto cifs_parse_mount_err;
  978. }
  979. i = cifs_convert_address((struct sockaddr *)&vol->srcaddr,
  980. value, strlen(value));
  981. if (i == 0) {
  982. printk(KERN_WARNING "CIFS: Could not parse"
  983. " srcaddr: %s\n",
  984. value);
  985. goto cifs_parse_mount_err;
  986. }
  987. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  988. if (!value || !*value) {
  989. printk(KERN_WARNING
  990. "CIFS: invalid path prefix\n");
  991. goto cifs_parse_mount_err;
  992. }
  993. if ((temp_len = strnlen(value, 1024)) < 1024) {
  994. if (value[0] != '/')
  995. temp_len++; /* missing leading slash */
  996. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  997. if (vol->prepath == NULL)
  998. goto cifs_parse_mount_err;
  999. if (value[0] != '/') {
  1000. vol->prepath[0] = '/';
  1001. strcpy(vol->prepath+1, value);
  1002. } else
  1003. strcpy(vol->prepath, value);
  1004. cFYI(1, "prefix path %s", vol->prepath);
  1005. } else {
  1006. printk(KERN_WARNING "CIFS: prefix too long\n");
  1007. goto cifs_parse_mount_err;
  1008. }
  1009. } else if (strnicmp(data, "iocharset", 9) == 0) {
  1010. if (!value || !*value) {
  1011. printk(KERN_WARNING "CIFS: invalid iocharset "
  1012. "specified\n");
  1013. goto cifs_parse_mount_err;
  1014. }
  1015. if (strnlen(value, 65) < 65) {
  1016. if (strnicmp(value, "default", 7)) {
  1017. vol->iocharset = kstrdup(value,
  1018. GFP_KERNEL);
  1019. if (!vol->iocharset) {
  1020. printk(KERN_WARNING "CIFS: no "
  1021. "memory for"
  1022. "charset\n");
  1023. goto cifs_parse_mount_err;
  1024. }
  1025. }
  1026. /* if iocharset not set then load_nls_default
  1027. is used by caller */
  1028. cFYI(1, "iocharset set to %s", value);
  1029. } else {
  1030. printk(KERN_WARNING "CIFS: iocharset name "
  1031. "too long.\n");
  1032. goto cifs_parse_mount_err;
  1033. }
  1034. } else if (!strnicmp(data, "uid", 3) && value && *value) {
  1035. vol->linux_uid = simple_strtoul(value, &value, 0);
  1036. uid_specified = true;
  1037. } else if (!strnicmp(data, "cruid", 5) && value && *value) {
  1038. vol->cred_uid = simple_strtoul(value, &value, 0);
  1039. } else if (!strnicmp(data, "forceuid", 8)) {
  1040. override_uid = 1;
  1041. } else if (!strnicmp(data, "noforceuid", 10)) {
  1042. override_uid = 0;
  1043. } else if (!strnicmp(data, "gid", 3) && value && *value) {
  1044. vol->linux_gid = simple_strtoul(value, &value, 0);
  1045. gid_specified = true;
  1046. } else if (!strnicmp(data, "forcegid", 8)) {
  1047. override_gid = 1;
  1048. } else if (!strnicmp(data, "noforcegid", 10)) {
  1049. override_gid = 0;
  1050. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1051. if (value && *value) {
  1052. vol->file_mode =
  1053. simple_strtoul(value, &value, 0);
  1054. }
  1055. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1056. if (value && *value) {
  1057. vol->dir_mode =
  1058. simple_strtoul(value, &value, 0);
  1059. }
  1060. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1061. if (value && *value) {
  1062. vol->dir_mode =
  1063. simple_strtoul(value, &value, 0);
  1064. }
  1065. } else if (strnicmp(data, "port", 4) == 0) {
  1066. if (value && *value) {
  1067. vol->port =
  1068. simple_strtoul(value, &value, 0);
  1069. }
  1070. } else if (strnicmp(data, "rsize", 5) == 0) {
  1071. if (value && *value) {
  1072. vol->rsize =
  1073. simple_strtoul(value, &value, 0);
  1074. }
  1075. } else if (strnicmp(data, "wsize", 5) == 0) {
  1076. if (value && *value) {
  1077. vol->wsize =
  1078. simple_strtoul(value, &value, 0);
  1079. }
  1080. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1081. if (!value || !*value) {
  1082. cERROR(1, "no socket option specified");
  1083. continue;
  1084. } else if (strnicmp(value, "TCP_NODELAY", 11) == 0) {
  1085. vol->sockopt_tcp_nodelay = 1;
  1086. }
  1087. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1088. if (!value || !*value || (*value == ' ')) {
  1089. cFYI(1, "invalid (empty) netbiosname");
  1090. } else {
  1091. memset(vol->source_rfc1001_name, 0x20,
  1092. RFC1001_NAME_LEN);
  1093. /*
  1094. * FIXME: are there cases in which a comma can
  1095. * be valid in workstation netbios name (and
  1096. * need special handling)?
  1097. */
  1098. for (i = 0; i < RFC1001_NAME_LEN; i++) {
  1099. /* don't ucase netbiosname for user */
  1100. if (value[i] == 0)
  1101. break;
  1102. vol->source_rfc1001_name[i] = value[i];
  1103. }
  1104. /* The string has 16th byte zero still from
  1105. set at top of the function */
  1106. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1107. printk(KERN_WARNING "CIFS: netbiosname"
  1108. " longer than 15 truncated.\n");
  1109. }
  1110. } else if (strnicmp(data, "servern", 7) == 0) {
  1111. /* servernetbiosname specified override *SMBSERVER */
  1112. if (!value || !*value || (*value == ' ')) {
  1113. cFYI(1, "empty server netbiosname specified");
  1114. } else {
  1115. /* last byte, type, is 0x20 for servr type */
  1116. memset(vol->target_rfc1001_name, 0x20,
  1117. RFC1001_NAME_LEN_WITH_NULL);
  1118. for (i = 0; i < 15; i++) {
  1119. /* BB are there cases in which a comma can be
  1120. valid in this workstation netbios name
  1121. (and need special handling)? */
  1122. /* user or mount helper must uppercase
  1123. the netbiosname */
  1124. if (value[i] == 0)
  1125. break;
  1126. else
  1127. vol->target_rfc1001_name[i] =
  1128. value[i];
  1129. }
  1130. /* The string has 16th byte zero still from
  1131. set at top of the function */
  1132. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1133. printk(KERN_WARNING "CIFS: server net"
  1134. "biosname longer than 15 truncated.\n");
  1135. }
  1136. } else if (strnicmp(data, "actimeo", 7) == 0) {
  1137. if (value && *value) {
  1138. vol->actimeo = HZ * simple_strtoul(value,
  1139. &value, 0);
  1140. if (vol->actimeo > CIFS_MAX_ACTIMEO) {
  1141. cERROR(1, "CIFS: attribute cache"
  1142. "timeout too large");
  1143. goto cifs_parse_mount_err;
  1144. }
  1145. }
  1146. } else if (strnicmp(data, "credentials", 4) == 0) {
  1147. /* ignore */
  1148. } else if (strnicmp(data, "version", 3) == 0) {
  1149. /* ignore */
  1150. } else if (strnicmp(data, "guest", 5) == 0) {
  1151. /* ignore */
  1152. } else if (strnicmp(data, "rw", 2) == 0) {
  1153. /* ignore */
  1154. } else if (strnicmp(data, "ro", 2) == 0) {
  1155. /* ignore */
  1156. } else if (strnicmp(data, "noblocksend", 11) == 0) {
  1157. vol->noblocksnd = 1;
  1158. } else if (strnicmp(data, "noautotune", 10) == 0) {
  1159. vol->noautotune = 1;
  1160. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1161. (strnicmp(data, "nosuid", 6) == 0) ||
  1162. (strnicmp(data, "exec", 4) == 0) ||
  1163. (strnicmp(data, "noexec", 6) == 0) ||
  1164. (strnicmp(data, "nodev", 5) == 0) ||
  1165. (strnicmp(data, "noauto", 6) == 0) ||
  1166. (strnicmp(data, "dev", 3) == 0)) {
  1167. /* The mount tool or mount.cifs helper (if present)
  1168. uses these opts to set flags, and the flags are read
  1169. by the kernel vfs layer before we get here (ie
  1170. before read super) so there is no point trying to
  1171. parse these options again and set anything and it
  1172. is ok to just ignore them */
  1173. continue;
  1174. } else if (strnicmp(data, "hard", 4) == 0) {
  1175. vol->retry = 1;
  1176. } else if (strnicmp(data, "soft", 4) == 0) {
  1177. vol->retry = 0;
  1178. } else if (strnicmp(data, "perm", 4) == 0) {
  1179. vol->noperm = 0;
  1180. } else if (strnicmp(data, "noperm", 6) == 0) {
  1181. vol->noperm = 1;
  1182. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1183. vol->remap = 1;
  1184. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1185. vol->remap = 0;
  1186. } else if (strnicmp(data, "sfu", 3) == 0) {
  1187. vol->sfu_emul = 1;
  1188. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1189. vol->sfu_emul = 0;
  1190. } else if (strnicmp(data, "nodfs", 5) == 0) {
  1191. vol->nodfs = 1;
  1192. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1193. vol->posix_paths = 1;
  1194. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1195. vol->posix_paths = 0;
  1196. } else if (strnicmp(data, "nounix", 6) == 0) {
  1197. vol->no_linux_ext = 1;
  1198. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1199. vol->no_linux_ext = 1;
  1200. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1201. (strnicmp(data, "ignorecase", 10) == 0)) {
  1202. vol->nocase = 1;
  1203. } else if (strnicmp(data, "mand", 4) == 0) {
  1204. /* ignore */
  1205. } else if (strnicmp(data, "nomand", 6) == 0) {
  1206. /* ignore */
  1207. } else if (strnicmp(data, "_netdev", 7) == 0) {
  1208. /* ignore */
  1209. } else if (strnicmp(data, "brl", 3) == 0) {
  1210. vol->nobrl = 0;
  1211. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1212. (strnicmp(data, "nolock", 6) == 0)) {
  1213. vol->nobrl = 1;
  1214. /* turn off mandatory locking in mode
  1215. if remote locking is turned off since the
  1216. local vfs will do advisory */
  1217. if (vol->file_mode ==
  1218. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1219. vol->file_mode = S_IALLUGO;
  1220. } else if (strnicmp(data, "forcemandatorylock", 9) == 0) {
  1221. /* will take the shorter form "forcemand" as well */
  1222. /* This mount option will force use of mandatory
  1223. (DOS/Windows style) byte range locks, instead of
  1224. using posix advisory byte range locks, even if the
  1225. Unix extensions are available and posix locks would
  1226. be supported otherwise. If Unix extensions are not
  1227. negotiated this has no effect since mandatory locks
  1228. would be used (mandatory locks is all that those
  1229. those servers support) */
  1230. vol->mand_lock = 1;
  1231. } else if (strnicmp(data, "setuids", 7) == 0) {
  1232. vol->setuids = 1;
  1233. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1234. vol->setuids = 0;
  1235. } else if (strnicmp(data, "dynperm", 7) == 0) {
  1236. vol->dynperm = true;
  1237. } else if (strnicmp(data, "nodynperm", 9) == 0) {
  1238. vol->dynperm = false;
  1239. } else if (strnicmp(data, "nohard", 6) == 0) {
  1240. vol->retry = 0;
  1241. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1242. vol->retry = 1;
  1243. } else if (strnicmp(data, "nointr", 6) == 0) {
  1244. vol->intr = 0;
  1245. } else if (strnicmp(data, "intr", 4) == 0) {
  1246. vol->intr = 1;
  1247. } else if (strnicmp(data, "nostrictsync", 12) == 0) {
  1248. vol->nostrictsync = 1;
  1249. } else if (strnicmp(data, "strictsync", 10) == 0) {
  1250. vol->nostrictsync = 0;
  1251. } else if (strnicmp(data, "serverino", 7) == 0) {
  1252. vol->server_ino = 1;
  1253. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1254. vol->server_ino = 0;
  1255. } else if (strnicmp(data, "rwpidforward", 4) == 0) {
  1256. vol->rwpidforward = 1;
  1257. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1258. vol->cifs_acl = 1;
  1259. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1260. vol->cifs_acl = 0;
  1261. } else if (strnicmp(data, "acl", 3) == 0) {
  1262. vol->no_psx_acl = 0;
  1263. } else if (strnicmp(data, "noacl", 5) == 0) {
  1264. vol->no_psx_acl = 1;
  1265. } else if (strnicmp(data, "locallease", 6) == 0) {
  1266. vol->local_lease = 1;
  1267. } else if (strnicmp(data, "sign", 4) == 0) {
  1268. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1269. } else if (strnicmp(data, "seal", 4) == 0) {
  1270. /* we do not do the following in secFlags because seal
  1271. is a per tree connection (mount) not a per socket
  1272. or per-smb connection option in the protocol */
  1273. /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1274. vol->seal = 1;
  1275. } else if (strnicmp(data, "direct", 6) == 0) {
  1276. vol->direct_io = 1;
  1277. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1278. vol->direct_io = 1;
  1279. } else if (strnicmp(data, "strictcache", 11) == 0) {
  1280. vol->strict_io = 1;
  1281. } else if (strnicmp(data, "noac", 4) == 0) {
  1282. printk(KERN_WARNING "CIFS: Mount option noac not "
  1283. "supported. Instead set "
  1284. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1285. } else if (strnicmp(data, "fsc", 3) == 0) {
  1286. #ifndef CONFIG_CIFS_FSCACHE
  1287. cERROR(1, "FS-Cache support needs CONFIG_CIFS_FSCACHE"
  1288. "kernel config option set");
  1289. goto cifs_parse_mount_err;
  1290. #endif
  1291. vol->fsc = true;
  1292. } else if (strnicmp(data, "mfsymlinks", 10) == 0) {
  1293. vol->mfsymlinks = true;
  1294. } else if (strnicmp(data, "multiuser", 8) == 0) {
  1295. vol->multiuser = true;
  1296. } else
  1297. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1298. data);
  1299. }
  1300. if (vol->UNC == NULL) {
  1301. if (devname == NULL) {
  1302. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1303. "target\n");
  1304. goto cifs_parse_mount_err;
  1305. }
  1306. if ((temp_len = strnlen(devname, 300)) < 300) {
  1307. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1308. if (vol->UNC == NULL)
  1309. goto cifs_parse_mount_err;
  1310. strcpy(vol->UNC, devname);
  1311. if (strncmp(vol->UNC, "//", 2) == 0) {
  1312. vol->UNC[0] = '\\';
  1313. vol->UNC[1] = '\\';
  1314. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1315. printk(KERN_WARNING "CIFS: UNC Path does not "
  1316. "begin with // or \\\\ \n");
  1317. goto cifs_parse_mount_err;
  1318. }
  1319. value = strpbrk(vol->UNC+2, "/\\");
  1320. if (value)
  1321. *value = '\\';
  1322. } else {
  1323. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1324. goto cifs_parse_mount_err;
  1325. }
  1326. }
  1327. if (vol->multiuser && !(vol->secFlg & CIFSSEC_MAY_KRB5)) {
  1328. cERROR(1, "Multiuser mounts currently require krb5 "
  1329. "authentication!");
  1330. goto cifs_parse_mount_err;
  1331. }
  1332. if (vol->UNCip == NULL)
  1333. vol->UNCip = &vol->UNC[2];
  1334. if (uid_specified)
  1335. vol->override_uid = override_uid;
  1336. else if (override_uid == 1)
  1337. printk(KERN_NOTICE "CIFS: ignoring forceuid mount option "
  1338. "specified with no uid= option.\n");
  1339. if (gid_specified)
  1340. vol->override_gid = override_gid;
  1341. else if (override_gid == 1)
  1342. printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
  1343. "specified with no gid= option.\n");
  1344. kfree(mountdata_copy);
  1345. return 0;
  1346. cifs_parse_mount_err:
  1347. kfree(mountdata_copy);
  1348. return 1;
  1349. }
  1350. /** Returns true if srcaddr isn't specified and rhs isn't
  1351. * specified, or if srcaddr is specified and
  1352. * matches the IP address of the rhs argument.
  1353. */
  1354. static bool
  1355. srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1356. {
  1357. switch (srcaddr->sa_family) {
  1358. case AF_UNSPEC:
  1359. return (rhs->sa_family == AF_UNSPEC);
  1360. case AF_INET: {
  1361. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1362. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1363. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  1364. }
  1365. case AF_INET6: {
  1366. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1367. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)&rhs;
  1368. return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
  1369. }
  1370. default:
  1371. WARN_ON(1);
  1372. return false; /* don't expect to be here */
  1373. }
  1374. }
  1375. /*
  1376. * If no port is specified in addr structure, we try to match with 445 port
  1377. * and if it fails - with 139 ports. It should be called only if address
  1378. * families of server and addr are equal.
  1379. */
  1380. static bool
  1381. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  1382. {
  1383. __be16 port, *sport;
  1384. switch (addr->sa_family) {
  1385. case AF_INET:
  1386. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  1387. port = ((struct sockaddr_in *) addr)->sin_port;
  1388. break;
  1389. case AF_INET6:
  1390. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  1391. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  1392. break;
  1393. default:
  1394. WARN_ON(1);
  1395. return false;
  1396. }
  1397. if (!port) {
  1398. port = htons(CIFS_PORT);
  1399. if (port == *sport)
  1400. return true;
  1401. port = htons(RFC1001_PORT);
  1402. }
  1403. return port == *sport;
  1404. }
  1405. static bool
  1406. match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
  1407. struct sockaddr *srcaddr)
  1408. {
  1409. switch (addr->sa_family) {
  1410. case AF_INET: {
  1411. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  1412. struct sockaddr_in *srv_addr4 =
  1413. (struct sockaddr_in *)&server->dstaddr;
  1414. if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
  1415. return false;
  1416. break;
  1417. }
  1418. case AF_INET6: {
  1419. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  1420. struct sockaddr_in6 *srv_addr6 =
  1421. (struct sockaddr_in6 *)&server->dstaddr;
  1422. if (!ipv6_addr_equal(&addr6->sin6_addr,
  1423. &srv_addr6->sin6_addr))
  1424. return false;
  1425. if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
  1426. return false;
  1427. break;
  1428. }
  1429. default:
  1430. WARN_ON(1);
  1431. return false; /* don't expect to be here */
  1432. }
  1433. if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
  1434. return false;
  1435. return true;
  1436. }
  1437. static bool
  1438. match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
  1439. {
  1440. unsigned int secFlags;
  1441. if (vol->secFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  1442. secFlags = vol->secFlg;
  1443. else
  1444. secFlags = global_secflags | vol->secFlg;
  1445. switch (server->secType) {
  1446. case LANMAN:
  1447. if (!(secFlags & (CIFSSEC_MAY_LANMAN|CIFSSEC_MAY_PLNTXT)))
  1448. return false;
  1449. break;
  1450. case NTLMv2:
  1451. if (!(secFlags & CIFSSEC_MAY_NTLMV2))
  1452. return false;
  1453. break;
  1454. case NTLM:
  1455. if (!(secFlags & CIFSSEC_MAY_NTLM))
  1456. return false;
  1457. break;
  1458. case Kerberos:
  1459. if (!(secFlags & CIFSSEC_MAY_KRB5))
  1460. return false;
  1461. break;
  1462. case RawNTLMSSP:
  1463. if (!(secFlags & CIFSSEC_MAY_NTLMSSP))
  1464. return false;
  1465. break;
  1466. default:
  1467. /* shouldn't happen */
  1468. return false;
  1469. }
  1470. /* now check if signing mode is acceptable */
  1471. if ((secFlags & CIFSSEC_MAY_SIGN) == 0 &&
  1472. (server->sec_mode & SECMODE_SIGN_REQUIRED))
  1473. return false;
  1474. else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) &&
  1475. (server->sec_mode &
  1476. (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0)
  1477. return false;
  1478. return true;
  1479. }
  1480. static int match_server(struct TCP_Server_Info *server, struct sockaddr *addr,
  1481. struct smb_vol *vol)
  1482. {
  1483. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  1484. return 0;
  1485. if (!match_address(server, addr,
  1486. (struct sockaddr *)&vol->srcaddr))
  1487. return 0;
  1488. if (!match_port(server, addr))
  1489. return 0;
  1490. if (!match_security(server, vol))
  1491. return 0;
  1492. return 1;
  1493. }
  1494. static struct TCP_Server_Info *
  1495. cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
  1496. {
  1497. struct TCP_Server_Info *server;
  1498. spin_lock(&cifs_tcp_ses_lock);
  1499. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  1500. if (!match_server(server, addr, vol))
  1501. continue;
  1502. ++server->srv_count;
  1503. spin_unlock(&cifs_tcp_ses_lock);
  1504. cFYI(1, "Existing tcp session with server found");
  1505. return server;
  1506. }
  1507. spin_unlock(&cifs_tcp_ses_lock);
  1508. return NULL;
  1509. }
  1510. static void
  1511. cifs_put_tcp_session(struct TCP_Server_Info *server)
  1512. {
  1513. struct task_struct *task;
  1514. spin_lock(&cifs_tcp_ses_lock);
  1515. if (--server->srv_count > 0) {
  1516. spin_unlock(&cifs_tcp_ses_lock);
  1517. return;
  1518. }
  1519. put_net(cifs_net_ns(server));
  1520. list_del_init(&server->tcp_ses_list);
  1521. spin_unlock(&cifs_tcp_ses_lock);
  1522. cancel_delayed_work_sync(&server->echo);
  1523. spin_lock(&GlobalMid_Lock);
  1524. server->tcpStatus = CifsExiting;
  1525. spin_unlock(&GlobalMid_Lock);
  1526. cifs_crypto_shash_release(server);
  1527. cifs_fscache_release_client_cookie(server);
  1528. kfree(server->session_key.response);
  1529. server->session_key.response = NULL;
  1530. server->session_key.len = 0;
  1531. task = xchg(&server->tsk, NULL);
  1532. if (task)
  1533. force_sig(SIGKILL, task);
  1534. }
  1535. static struct TCP_Server_Info *
  1536. cifs_get_tcp_session(struct smb_vol *volume_info)
  1537. {
  1538. struct TCP_Server_Info *tcp_ses = NULL;
  1539. struct sockaddr_storage addr;
  1540. struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
  1541. struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
  1542. int rc;
  1543. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1544. cFYI(1, "UNC: %s ip: %s", volume_info->UNC, volume_info->UNCip);
  1545. if (volume_info->UNCip && volume_info->UNC) {
  1546. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  1547. volume_info->UNCip,
  1548. strlen(volume_info->UNCip),
  1549. volume_info->port);
  1550. if (!rc) {
  1551. /* we failed translating address */
  1552. rc = -EINVAL;
  1553. goto out_err;
  1554. }
  1555. } else if (volume_info->UNCip) {
  1556. /* BB using ip addr as tcp_ses name to connect to the
  1557. DFS root below */
  1558. cERROR(1, "Connecting to DFS root not implemented yet");
  1559. rc = -EINVAL;
  1560. goto out_err;
  1561. } else /* which tcp_sess DFS root would we conect to */ {
  1562. cERROR(1, "CIFS mount error: No UNC path (e.g. -o "
  1563. "unc=//192.168.1.100/public) specified");
  1564. rc = -EINVAL;
  1565. goto out_err;
  1566. }
  1567. /* see if we already have a matching tcp_ses */
  1568. tcp_ses = cifs_find_tcp_session((struct sockaddr *)&addr, volume_info);
  1569. if (tcp_ses)
  1570. return tcp_ses;
  1571. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1572. if (!tcp_ses) {
  1573. rc = -ENOMEM;
  1574. goto out_err;
  1575. }
  1576. rc = cifs_crypto_shash_allocate(tcp_ses);
  1577. if (rc) {
  1578. cERROR(1, "could not setup hash structures rc %d", rc);
  1579. goto out_err;
  1580. }
  1581. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  1582. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  1583. if (IS_ERR(tcp_ses->hostname)) {
  1584. rc = PTR_ERR(tcp_ses->hostname);
  1585. goto out_err_crypto_release;
  1586. }
  1587. tcp_ses->noblocksnd = volume_info->noblocksnd;
  1588. tcp_ses->noautotune = volume_info->noautotune;
  1589. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  1590. atomic_set(&tcp_ses->inFlight, 0);
  1591. init_waitqueue_head(&tcp_ses->response_q);
  1592. init_waitqueue_head(&tcp_ses->request_q);
  1593. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1594. mutex_init(&tcp_ses->srv_mutex);
  1595. memcpy(tcp_ses->workstation_RFC1001_name,
  1596. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1597. memcpy(tcp_ses->server_RFC1001_name,
  1598. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1599. tcp_ses->session_estab = false;
  1600. tcp_ses->sequence_number = 0;
  1601. tcp_ses->lstrp = jiffies;
  1602. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1603. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1604. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  1605. /*
  1606. * at this point we are the only ones with the pointer
  1607. * to the struct since the kernel thread not created yet
  1608. * no need to spinlock this init of tcpStatus or srv_count
  1609. */
  1610. tcp_ses->tcpStatus = CifsNew;
  1611. memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
  1612. sizeof(tcp_ses->srcaddr));
  1613. ++tcp_ses->srv_count;
  1614. if (addr.ss_family == AF_INET6) {
  1615. cFYI(1, "attempting ipv6 connect");
  1616. /* BB should we allow ipv6 on port 139? */
  1617. /* other OS never observed in Wild doing 139 with v6 */
  1618. memcpy(&tcp_ses->dstaddr, sin_server6,
  1619. sizeof(struct sockaddr_in6));
  1620. } else
  1621. memcpy(&tcp_ses->dstaddr, sin_server,
  1622. sizeof(struct sockaddr_in));
  1623. rc = ip_connect(tcp_ses);
  1624. if (rc < 0) {
  1625. cERROR(1, "Error connecting to socket. Aborting operation");
  1626. goto out_err_crypto_release;
  1627. }
  1628. /*
  1629. * since we're in a cifs function already, we know that
  1630. * this will succeed. No need for try_module_get().
  1631. */
  1632. __module_get(THIS_MODULE);
  1633. tcp_ses->tsk = kthread_run((void *)(void *)cifs_demultiplex_thread,
  1634. tcp_ses, "cifsd");
  1635. if (IS_ERR(tcp_ses->tsk)) {
  1636. rc = PTR_ERR(tcp_ses->tsk);
  1637. cERROR(1, "error %d create cifsd thread", rc);
  1638. module_put(THIS_MODULE);
  1639. goto out_err_crypto_release;
  1640. }
  1641. tcp_ses->tcpStatus = CifsNeedNegotiate;
  1642. /* thread spawned, put it on the list */
  1643. spin_lock(&cifs_tcp_ses_lock);
  1644. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1645. spin_unlock(&cifs_tcp_ses_lock);
  1646. cifs_fscache_get_client_cookie(tcp_ses);
  1647. /* queue echo request delayed work */
  1648. queue_delayed_work(system_nrt_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
  1649. return tcp_ses;
  1650. out_err_crypto_release:
  1651. cifs_crypto_shash_release(tcp_ses);
  1652. put_net(cifs_net_ns(tcp_ses));
  1653. out_err:
  1654. if (tcp_ses) {
  1655. if (!IS_ERR(tcp_ses->hostname))
  1656. kfree(tcp_ses->hostname);
  1657. if (tcp_ses->ssocket)
  1658. sock_release(tcp_ses->ssocket);
  1659. kfree(tcp_ses);
  1660. }
  1661. return ERR_PTR(rc);
  1662. }
  1663. static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
  1664. {
  1665. switch (ses->server->secType) {
  1666. case Kerberos:
  1667. if (vol->cred_uid != ses->cred_uid)
  1668. return 0;
  1669. break;
  1670. default:
  1671. /* anything else takes username/password */
  1672. if (ses->user_name == NULL)
  1673. return 0;
  1674. if (strncmp(ses->user_name, vol->username,
  1675. MAX_USERNAME_SIZE))
  1676. return 0;
  1677. if (strlen(vol->username) != 0 &&
  1678. ses->password != NULL &&
  1679. strncmp(ses->password,
  1680. vol->password ? vol->password : "",
  1681. MAX_PASSWORD_SIZE))
  1682. return 0;
  1683. }
  1684. return 1;
  1685. }
  1686. static struct cifs_ses *
  1687. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
  1688. {
  1689. struct cifs_ses *ses;
  1690. spin_lock(&cifs_tcp_ses_lock);
  1691. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1692. if (!match_session(ses, vol))
  1693. continue;
  1694. ++ses->ses_count;
  1695. spin_unlock(&cifs_tcp_ses_lock);
  1696. return ses;
  1697. }
  1698. spin_unlock(&cifs_tcp_ses_lock);
  1699. return NULL;
  1700. }
  1701. static void
  1702. cifs_put_smb_ses(struct cifs_ses *ses)
  1703. {
  1704. int xid;
  1705. struct TCP_Server_Info *server = ses->server;
  1706. cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count);
  1707. spin_lock(&cifs_tcp_ses_lock);
  1708. if (--ses->ses_count > 0) {
  1709. spin_unlock(&cifs_tcp_ses_lock);
  1710. return;
  1711. }
  1712. list_del_init(&ses->smb_ses_list);
  1713. spin_unlock(&cifs_tcp_ses_lock);
  1714. if (ses->status == CifsGood) {
  1715. xid = GetXid();
  1716. CIFSSMBLogoff(xid, ses);
  1717. _FreeXid(xid);
  1718. }
  1719. sesInfoFree(ses);
  1720. cifs_put_tcp_session(server);
  1721. }
  1722. static bool warned_on_ntlm; /* globals init to false automatically */
  1723. static struct cifs_ses *
  1724. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  1725. {
  1726. int rc = -ENOMEM, xid;
  1727. struct cifs_ses *ses;
  1728. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  1729. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  1730. xid = GetXid();
  1731. ses = cifs_find_smb_ses(server, volume_info);
  1732. if (ses) {
  1733. cFYI(1, "Existing smb sess found (status=%d)", ses->status);
  1734. mutex_lock(&ses->session_mutex);
  1735. rc = cifs_negotiate_protocol(xid, ses);
  1736. if (rc) {
  1737. mutex_unlock(&ses->session_mutex);
  1738. /* problem -- put our ses reference */
  1739. cifs_put_smb_ses(ses);
  1740. FreeXid(xid);
  1741. return ERR_PTR(rc);
  1742. }
  1743. if (ses->need_reconnect) {
  1744. cFYI(1, "Session needs reconnect");
  1745. rc = cifs_setup_session(xid, ses,
  1746. volume_info->local_nls);
  1747. if (rc) {
  1748. mutex_unlock(&ses->session_mutex);
  1749. /* problem -- put our reference */
  1750. cifs_put_smb_ses(ses);
  1751. FreeXid(xid);
  1752. return ERR_PTR(rc);
  1753. }
  1754. }
  1755. mutex_unlock(&ses->session_mutex);
  1756. /* existing SMB ses has a server reference already */
  1757. cifs_put_tcp_session(server);
  1758. FreeXid(xid);
  1759. return ses;
  1760. }
  1761. cFYI(1, "Existing smb sess not found");
  1762. ses = sesInfoAlloc();
  1763. if (ses == NULL)
  1764. goto get_ses_fail;
  1765. /* new SMB session uses our server ref */
  1766. ses->server = server;
  1767. if (server->dstaddr.ss_family == AF_INET6)
  1768. sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
  1769. else
  1770. sprintf(ses->serverName, "%pI4", &addr->sin_addr);
  1771. if (volume_info->username) {
  1772. ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
  1773. if (!ses->user_name)
  1774. goto get_ses_fail;
  1775. }
  1776. /* volume_info->password freed at unmount */
  1777. if (volume_info->password) {
  1778. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  1779. if (!ses->password)
  1780. goto get_ses_fail;
  1781. }
  1782. if (volume_info->domainname) {
  1783. ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
  1784. if (!ses->domainName)
  1785. goto get_ses_fail;
  1786. }
  1787. ses->cred_uid = volume_info->cred_uid;
  1788. ses->linux_uid = volume_info->linux_uid;
  1789. /* ntlmv2 is much stronger than ntlm security, and has been broadly
  1790. supported for many years, time to update default security mechanism */
  1791. if ((volume_info->secFlg == 0) && warned_on_ntlm == false) {
  1792. warned_on_ntlm = true;
  1793. cERROR(1, "default security mechanism requested. The default "
  1794. "security mechanism will be upgraded from ntlm to "
  1795. "ntlmv2 in kernel release 2.6.41");
  1796. }
  1797. ses->overrideSecFlg = volume_info->secFlg;
  1798. mutex_lock(&ses->session_mutex);
  1799. rc = cifs_negotiate_protocol(xid, ses);
  1800. if (!rc)
  1801. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  1802. mutex_unlock(&ses->session_mutex);
  1803. if (rc)
  1804. goto get_ses_fail;
  1805. /* success, put it on the list */
  1806. spin_lock(&cifs_tcp_ses_lock);
  1807. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  1808. spin_unlock(&cifs_tcp_ses_lock);
  1809. FreeXid(xid);
  1810. return ses;
  1811. get_ses_fail:
  1812. sesInfoFree(ses);
  1813. FreeXid(xid);
  1814. return ERR_PTR(rc);
  1815. }
  1816. static int match_tcon(struct cifs_tcon *tcon, const char *unc)
  1817. {
  1818. if (tcon->tidStatus == CifsExiting)
  1819. return 0;
  1820. if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
  1821. return 0;
  1822. return 1;
  1823. }
  1824. static struct cifs_tcon *
  1825. cifs_find_tcon(struct cifs_ses *ses, const char *unc)
  1826. {
  1827. struct list_head *tmp;
  1828. struct cifs_tcon *tcon;
  1829. spin_lock(&cifs_tcp_ses_lock);
  1830. list_for_each(tmp, &ses->tcon_list) {
  1831. tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
  1832. if (!match_tcon(tcon, unc))
  1833. continue;
  1834. ++tcon->tc_count;
  1835. spin_unlock(&cifs_tcp_ses_lock);
  1836. return tcon;
  1837. }
  1838. spin_unlock(&cifs_tcp_ses_lock);
  1839. return NULL;
  1840. }
  1841. static void
  1842. cifs_put_tcon(struct cifs_tcon *tcon)
  1843. {
  1844. int xid;
  1845. struct cifs_ses *ses = tcon->ses;
  1846. cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  1847. spin_lock(&cifs_tcp_ses_lock);
  1848. if (--tcon->tc_count > 0) {
  1849. spin_unlock(&cifs_tcp_ses_lock);
  1850. return;
  1851. }
  1852. list_del_init(&tcon->tcon_list);
  1853. spin_unlock(&cifs_tcp_ses_lock);
  1854. xid = GetXid();
  1855. CIFSSMBTDis(xid, tcon);
  1856. _FreeXid(xid);
  1857. cifs_fscache_release_super_cookie(tcon);
  1858. tconInfoFree(tcon);
  1859. cifs_put_smb_ses(ses);
  1860. }
  1861. static struct cifs_tcon *
  1862. cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  1863. {
  1864. int rc, xid;
  1865. struct cifs_tcon *tcon;
  1866. tcon = cifs_find_tcon(ses, volume_info->UNC);
  1867. if (tcon) {
  1868. cFYI(1, "Found match on UNC path");
  1869. /* existing tcon already has a reference */
  1870. cifs_put_smb_ses(ses);
  1871. if (tcon->seal != volume_info->seal)
  1872. cERROR(1, "transport encryption setting "
  1873. "conflicts with existing tid");
  1874. return tcon;
  1875. }
  1876. tcon = tconInfoAlloc();
  1877. if (tcon == NULL) {
  1878. rc = -ENOMEM;
  1879. goto out_fail;
  1880. }
  1881. tcon->ses = ses;
  1882. if (volume_info->password) {
  1883. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  1884. if (!tcon->password) {
  1885. rc = -ENOMEM;
  1886. goto out_fail;
  1887. }
  1888. }
  1889. if (strchr(volume_info->UNC + 3, '\\') == NULL
  1890. && strchr(volume_info->UNC + 3, '/') == NULL) {
  1891. cERROR(1, "Missing share name");
  1892. rc = -ENODEV;
  1893. goto out_fail;
  1894. }
  1895. /* BB Do we need to wrap session_mutex around
  1896. * this TCon call and Unix SetFS as
  1897. * we do on SessSetup and reconnect? */
  1898. xid = GetXid();
  1899. rc = CIFSTCon(xid, ses, volume_info->UNC, tcon, volume_info->local_nls);
  1900. FreeXid(xid);
  1901. cFYI(1, "CIFS Tcon rc = %d", rc);
  1902. if (rc)
  1903. goto out_fail;
  1904. if (volume_info->nodfs) {
  1905. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  1906. cFYI(1, "DFS disabled (%d)", tcon->Flags);
  1907. }
  1908. tcon->seal = volume_info->seal;
  1909. /* we can have only one retry value for a connection
  1910. to a share so for resources mounted more than once
  1911. to the same server share the last value passed in
  1912. for the retry flag is used */
  1913. tcon->retry = volume_info->retry;
  1914. tcon->nocase = volume_info->nocase;
  1915. tcon->local_lease = volume_info->local_lease;
  1916. spin_lock(&cifs_tcp_ses_lock);
  1917. list_add(&tcon->tcon_list, &ses->tcon_list);
  1918. spin_unlock(&cifs_tcp_ses_lock);
  1919. cifs_fscache_get_super_cookie(tcon);
  1920. return tcon;
  1921. out_fail:
  1922. tconInfoFree(tcon);
  1923. return ERR_PTR(rc);
  1924. }
  1925. void
  1926. cifs_put_tlink(struct tcon_link *tlink)
  1927. {
  1928. if (!tlink || IS_ERR(tlink))
  1929. return;
  1930. if (!atomic_dec_and_test(&tlink->tl_count) ||
  1931. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  1932. tlink->tl_time = jiffies;
  1933. return;
  1934. }
  1935. if (!IS_ERR(tlink_tcon(tlink)))
  1936. cifs_put_tcon(tlink_tcon(tlink));
  1937. kfree(tlink);
  1938. return;
  1939. }
  1940. static inline struct tcon_link *
  1941. cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb);
  1942. static int
  1943. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  1944. {
  1945. struct cifs_sb_info *old = CIFS_SB(sb);
  1946. struct cifs_sb_info *new = mnt_data->cifs_sb;
  1947. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  1948. return 0;
  1949. if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
  1950. (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
  1951. return 0;
  1952. if (old->rsize != new->rsize)
  1953. return 0;
  1954. /*
  1955. * We want to share sb only if we don't specify wsize or specified wsize
  1956. * is greater or equal than existing one.
  1957. */
  1958. if (new->wsize && new->wsize < old->wsize)
  1959. return 0;
  1960. if (old->mnt_uid != new->mnt_uid || old->mnt_gid != new->mnt_gid)
  1961. return 0;
  1962. if (old->mnt_file_mode != new->mnt_file_mode ||
  1963. old->mnt_dir_mode != new->mnt_dir_mode)
  1964. return 0;
  1965. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  1966. return 0;
  1967. if (old->actimeo != new->actimeo)
  1968. return 0;
  1969. return 1;
  1970. }
  1971. int
  1972. cifs_match_super(struct super_block *sb, void *data)
  1973. {
  1974. struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
  1975. struct smb_vol *volume_info;
  1976. struct cifs_sb_info *cifs_sb;
  1977. struct TCP_Server_Info *tcp_srv;
  1978. struct cifs_ses *ses;
  1979. struct cifs_tcon *tcon;
  1980. struct tcon_link *tlink;
  1981. struct sockaddr_storage addr;
  1982. int rc = 0;
  1983. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1984. spin_lock(&cifs_tcp_ses_lock);
  1985. cifs_sb = CIFS_SB(sb);
  1986. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  1987. if (IS_ERR(tlink)) {
  1988. spin_unlock(&cifs_tcp_ses_lock);
  1989. return rc;
  1990. }
  1991. tcon = tlink_tcon(tlink);
  1992. ses = tcon->ses;
  1993. tcp_srv = ses->server;
  1994. volume_info = mnt_data->vol;
  1995. if (!volume_info->UNCip || !volume_info->UNC)
  1996. goto out;
  1997. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  1998. volume_info->UNCip,
  1999. strlen(volume_info->UNCip),
  2000. volume_info->port);
  2001. if (!rc)
  2002. goto out;
  2003. if (!match_server(tcp_srv, (struct sockaddr *)&addr, volume_info) ||
  2004. !match_session(ses, volume_info) ||
  2005. !match_tcon(tcon, volume_info->UNC)) {
  2006. rc = 0;
  2007. goto out;
  2008. }
  2009. rc = compare_mount_options(sb, mnt_data);
  2010. out:
  2011. cifs_put_tlink(tlink);
  2012. spin_unlock(&cifs_tcp_ses_lock);
  2013. return rc;
  2014. }
  2015. int
  2016. get_dfs_path(int xid, struct cifs_ses *pSesInfo, const char *old_path,
  2017. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  2018. struct dfs_info3_param **preferrals, int remap)
  2019. {
  2020. char *temp_unc;
  2021. int rc = 0;
  2022. *pnum_referrals = 0;
  2023. *preferrals = NULL;
  2024. if (pSesInfo->ipc_tid == 0) {
  2025. temp_unc = kmalloc(2 /* for slashes */ +
  2026. strnlen(pSesInfo->serverName,
  2027. SERVER_NAME_LEN_WITH_NULL * 2)
  2028. + 1 + 4 /* slash IPC$ */ + 2,
  2029. GFP_KERNEL);
  2030. if (temp_unc == NULL)
  2031. return -ENOMEM;
  2032. temp_unc[0] = '\\';
  2033. temp_unc[1] = '\\';
  2034. strcpy(temp_unc + 2, pSesInfo->serverName);
  2035. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  2036. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  2037. cFYI(1, "CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid);
  2038. kfree(temp_unc);
  2039. }
  2040. if (rc == 0)
  2041. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  2042. pnum_referrals, nls_codepage, remap);
  2043. /* BB map targetUNCs to dfs_info3 structures, here or
  2044. in CIFSGetDFSRefer BB */
  2045. return rc;
  2046. }
  2047. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2048. static struct lock_class_key cifs_key[2];
  2049. static struct lock_class_key cifs_slock_key[2];
  2050. static inline void
  2051. cifs_reclassify_socket4(struct socket *sock)
  2052. {
  2053. struct sock *sk = sock->sk;
  2054. BUG_ON(sock_owned_by_user(sk));
  2055. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  2056. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  2057. }
  2058. static inline void
  2059. cifs_reclassify_socket6(struct socket *sock)
  2060. {
  2061. struct sock *sk = sock->sk;
  2062. BUG_ON(sock_owned_by_user(sk));
  2063. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  2064. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  2065. }
  2066. #else
  2067. static inline void
  2068. cifs_reclassify_socket4(struct socket *sock)
  2069. {
  2070. }
  2071. static inline void
  2072. cifs_reclassify_socket6(struct socket *sock)
  2073. {
  2074. }
  2075. #endif
  2076. /* See RFC1001 section 14 on representation of Netbios names */
  2077. static void rfc1002mangle(char *target, char *source, unsigned int length)
  2078. {
  2079. unsigned int i, j;
  2080. for (i = 0, j = 0; i < (length); i++) {
  2081. /* mask a nibble at a time and encode */
  2082. target[j] = 'A' + (0x0F & (source[i] >> 4));
  2083. target[j+1] = 'A' + (0x0F & source[i]);
  2084. j += 2;
  2085. }
  2086. }
  2087. static int
  2088. bind_socket(struct TCP_Server_Info *server)
  2089. {
  2090. int rc = 0;
  2091. if (server->srcaddr.ss_family != AF_UNSPEC) {
  2092. /* Bind to the specified local IP address */
  2093. struct socket *socket = server->ssocket;
  2094. rc = socket->ops->bind(socket,
  2095. (struct sockaddr *) &server->srcaddr,
  2096. sizeof(server->srcaddr));
  2097. if (rc < 0) {
  2098. struct sockaddr_in *saddr4;
  2099. struct sockaddr_in6 *saddr6;
  2100. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  2101. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  2102. if (saddr6->sin6_family == AF_INET6)
  2103. cERROR(1, "cifs: "
  2104. "Failed to bind to: %pI6c, error: %d\n",
  2105. &saddr6->sin6_addr, rc);
  2106. else
  2107. cERROR(1, "cifs: "
  2108. "Failed to bind to: %pI4, error: %d\n",
  2109. &saddr4->sin_addr.s_addr, rc);
  2110. }
  2111. }
  2112. return rc;
  2113. }
  2114. static int
  2115. ip_rfc1001_connect(struct TCP_Server_Info *server)
  2116. {
  2117. int rc = 0;
  2118. /*
  2119. * some servers require RFC1001 sessinit before sending
  2120. * negprot - BB check reconnection in case where second
  2121. * sessinit is sent but no second negprot
  2122. */
  2123. struct rfc1002_session_packet *ses_init_buf;
  2124. struct smb_hdr *smb_buf;
  2125. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  2126. GFP_KERNEL);
  2127. if (ses_init_buf) {
  2128. ses_init_buf->trailer.session_req.called_len = 32;
  2129. if (server->server_RFC1001_name &&
  2130. server->server_RFC1001_name[0] != 0)
  2131. rfc1002mangle(ses_init_buf->trailer.
  2132. session_req.called_name,
  2133. server->server_RFC1001_name,
  2134. RFC1001_NAME_LEN_WITH_NULL);
  2135. else
  2136. rfc1002mangle(ses_init_buf->trailer.
  2137. session_req.called_name,
  2138. DEFAULT_CIFS_CALLED_NAME,
  2139. RFC1001_NAME_LEN_WITH_NULL);
  2140. ses_init_buf->trailer.session_req.calling_len = 32;
  2141. /*
  2142. * calling name ends in null (byte 16) from old smb
  2143. * convention.
  2144. */
  2145. if (server->workstation_RFC1001_name &&
  2146. server->workstation_RFC1001_name[0] != 0)
  2147. rfc1002mangle(ses_init_buf->trailer.
  2148. session_req.calling_name,
  2149. server->workstation_RFC1001_name,
  2150. RFC1001_NAME_LEN_WITH_NULL);
  2151. else
  2152. rfc1002mangle(ses_init_buf->trailer.
  2153. session_req.calling_name,
  2154. "LINUX_CIFS_CLNT",
  2155. RFC1001_NAME_LEN_WITH_NULL);
  2156. ses_init_buf->trailer.session_req.scope1 = 0;
  2157. ses_init_buf->trailer.session_req.scope2 = 0;
  2158. smb_buf = (struct smb_hdr *)ses_init_buf;
  2159. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  2160. smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
  2161. rc = smb_send(server, smb_buf, 0x44);
  2162. kfree(ses_init_buf);
  2163. /*
  2164. * RFC1001 layer in at least one server
  2165. * requires very short break before negprot
  2166. * presumably because not expecting negprot
  2167. * to follow so fast. This is a simple
  2168. * solution that works without
  2169. * complicating the code and causes no
  2170. * significant slowing down on mount
  2171. * for everyone else
  2172. */
  2173. usleep_range(1000, 2000);
  2174. }
  2175. /*
  2176. * else the negprot may still work without this
  2177. * even though malloc failed
  2178. */
  2179. return rc;
  2180. }
  2181. static int
  2182. generic_ip_connect(struct TCP_Server_Info *server)
  2183. {
  2184. int rc = 0;
  2185. __be16 sport;
  2186. int slen, sfamily;
  2187. struct socket *socket = server->ssocket;
  2188. struct sockaddr *saddr;
  2189. saddr = (struct sockaddr *) &server->dstaddr;
  2190. if (server->dstaddr.ss_family == AF_INET6) {
  2191. sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
  2192. slen = sizeof(struct sockaddr_in6);
  2193. sfamily = AF_INET6;
  2194. } else {
  2195. sport = ((struct sockaddr_in *) saddr)->sin_port;
  2196. slen = sizeof(struct sockaddr_in);
  2197. sfamily = AF_INET;
  2198. }
  2199. if (socket == NULL) {
  2200. rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
  2201. IPPROTO_TCP, &socket, 1);
  2202. if (rc < 0) {
  2203. cERROR(1, "Error %d creating socket", rc);
  2204. server->ssocket = NULL;
  2205. return rc;
  2206. }
  2207. /* BB other socket options to set KEEPALIVE, NODELAY? */
  2208. cFYI(1, "Socket created");
  2209. server->ssocket = socket;
  2210. socket->sk->sk_allocation = GFP_NOFS;
  2211. if (sfamily == AF_INET6)
  2212. cifs_reclassify_socket6(socket);
  2213. else
  2214. cifs_reclassify_socket4(socket);
  2215. }
  2216. rc = bind_socket(server);
  2217. if (rc < 0)
  2218. return rc;
  2219. rc = socket->ops->connect(socket, saddr, slen, 0);
  2220. if (rc < 0) {
  2221. cFYI(1, "Error %d connecting to server", rc);
  2222. sock_release(socket);
  2223. server->ssocket = NULL;
  2224. return rc;
  2225. }
  2226. /*
  2227. * Eventually check for other socket options to change from
  2228. * the default. sock_setsockopt not used because it expects
  2229. * user space buffer
  2230. */
  2231. socket->sk->sk_rcvtimeo = 7 * HZ;
  2232. socket->sk->sk_sndtimeo = 5 * HZ;
  2233. /* make the bufsizes depend on wsize/rsize and max requests */
  2234. if (server->noautotune) {
  2235. if (socket->sk->sk_sndbuf < (200 * 1024))
  2236. socket->sk->sk_sndbuf = 200 * 1024;
  2237. if (socket->sk->sk_rcvbuf < (140 * 1024))
  2238. socket->sk->sk_rcvbuf = 140 * 1024;
  2239. }
  2240. if (server->tcp_nodelay) {
  2241. int val = 1;
  2242. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  2243. (char *)&val, sizeof(val));
  2244. if (rc)
  2245. cFYI(1, "set TCP_NODELAY socket option error %d", rc);
  2246. }
  2247. cFYI(1, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  2248. socket->sk->sk_sndbuf,
  2249. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  2250. if (sport == htons(RFC1001_PORT))
  2251. rc = ip_rfc1001_connect(server);
  2252. return rc;
  2253. }
  2254. static int
  2255. ip_connect(struct TCP_Server_Info *server)
  2256. {
  2257. __be16 *sport;
  2258. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2259. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2260. if (server->dstaddr.ss_family == AF_INET6)
  2261. sport = &addr6->sin6_port;
  2262. else
  2263. sport = &addr->sin_port;
  2264. if (*sport == 0) {
  2265. int rc;
  2266. /* try with 445 port at first */
  2267. *sport = htons(CIFS_PORT);
  2268. rc = generic_ip_connect(server);
  2269. if (rc >= 0)
  2270. return rc;
  2271. /* if it failed, try with 139 port */
  2272. *sport = htons(RFC1001_PORT);
  2273. }
  2274. return generic_ip_connect(server);
  2275. }
  2276. void reset_cifs_unix_caps(int xid, struct cifs_tcon *tcon,
  2277. struct super_block *sb, struct smb_vol *vol_info)
  2278. {
  2279. /* if we are reconnecting then should we check to see if
  2280. * any requested capabilities changed locally e.g. via
  2281. * remount but we can not do much about it here
  2282. * if they have (even if we could detect it by the following)
  2283. * Perhaps we could add a backpointer to array of sb from tcon
  2284. * or if we change to make all sb to same share the same
  2285. * sb as NFS - then we only have one backpointer to sb.
  2286. * What if we wanted to mount the server share twice once with
  2287. * and once without posixacls or posix paths? */
  2288. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2289. if (vol_info && vol_info->no_linux_ext) {
  2290. tcon->fsUnixInfo.Capability = 0;
  2291. tcon->unix_ext = 0; /* Unix Extensions disabled */
  2292. cFYI(1, "Linux protocol extensions disabled");
  2293. return;
  2294. } else if (vol_info)
  2295. tcon->unix_ext = 1; /* Unix Extensions supported */
  2296. if (tcon->unix_ext == 0) {
  2297. cFYI(1, "Unix extensions disabled so not set on reconnect");
  2298. return;
  2299. }
  2300. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  2301. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2302. cFYI(1, "unix caps which server supports %lld", cap);
  2303. /* check for reconnect case in which we do not
  2304. want to change the mount behavior if we can avoid it */
  2305. if (vol_info == NULL) {
  2306. /* turn off POSIX ACL and PATHNAMES if not set
  2307. originally at mount time */
  2308. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  2309. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2310. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2311. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2312. cERROR(1, "POSIXPATH support change");
  2313. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2314. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2315. cERROR(1, "possible reconnect error");
  2316. cERROR(1, "server disabled POSIX path support");
  2317. }
  2318. }
  2319. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2320. cERROR(1, "per-share encryption not supported yet");
  2321. cap &= CIFS_UNIX_CAP_MASK;
  2322. if (vol_info && vol_info->no_psx_acl)
  2323. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2324. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  2325. cFYI(1, "negotiated posix acl support");
  2326. if (sb)
  2327. sb->s_flags |= MS_POSIXACL;
  2328. }
  2329. if (vol_info && vol_info->posix_paths == 0)
  2330. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2331. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  2332. cFYI(1, "negotiate posix pathnames");
  2333. if (sb)
  2334. CIFS_SB(sb)->mnt_cifs_flags |=
  2335. CIFS_MOUNT_POSIX_PATHS;
  2336. }
  2337. if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
  2338. if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
  2339. CIFS_SB(sb)->rsize = 127 * 1024;
  2340. cFYI(DBG2, "larger reads not supported by srv");
  2341. }
  2342. }
  2343. cFYI(1, "Negotiate caps 0x%x", (int)cap);
  2344. #ifdef CONFIG_CIFS_DEBUG2
  2345. if (cap & CIFS_UNIX_FCNTL_CAP)
  2346. cFYI(1, "FCNTL cap");
  2347. if (cap & CIFS_UNIX_EXTATTR_CAP)
  2348. cFYI(1, "EXTATTR cap");
  2349. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2350. cFYI(1, "POSIX path cap");
  2351. if (cap & CIFS_UNIX_XATTR_CAP)
  2352. cFYI(1, "XATTR cap");
  2353. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  2354. cFYI(1, "POSIX ACL cap");
  2355. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  2356. cFYI(1, "very large read cap");
  2357. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  2358. cFYI(1, "very large write cap");
  2359. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  2360. cFYI(1, "transport encryption cap");
  2361. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2362. cFYI(1, "mandatory transport encryption cap");
  2363. #endif /* CIFS_DEBUG2 */
  2364. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  2365. if (vol_info == NULL) {
  2366. cFYI(1, "resetting capabilities failed");
  2367. } else
  2368. cERROR(1, "Negotiating Unix capabilities "
  2369. "with the server failed. Consider "
  2370. "mounting with the Unix Extensions\n"
  2371. "disabled, if problems are found, "
  2372. "by specifying the nounix mount "
  2373. "option.");
  2374. }
  2375. }
  2376. }
  2377. void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
  2378. struct cifs_sb_info *cifs_sb)
  2379. {
  2380. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  2381. if (pvolume_info->rsize > CIFSMaxBufSize) {
  2382. cERROR(1, "rsize %d too large, using MaxBufSize",
  2383. pvolume_info->rsize);
  2384. cifs_sb->rsize = CIFSMaxBufSize;
  2385. } else if ((pvolume_info->rsize) &&
  2386. (pvolume_info->rsize <= CIFSMaxBufSize))
  2387. cifs_sb->rsize = pvolume_info->rsize;
  2388. else /* default */
  2389. cifs_sb->rsize = CIFSMaxBufSize;
  2390. if (cifs_sb->rsize < 2048) {
  2391. cifs_sb->rsize = 2048;
  2392. /* Windows ME may prefer this */
  2393. cFYI(1, "readsize set to minimum: 2048");
  2394. }
  2395. /*
  2396. * Temporarily set wsize for matching superblock. If we end up using
  2397. * new sb then cifs_negotiate_wsize will later negotiate it downward
  2398. * if needed.
  2399. */
  2400. cifs_sb->wsize = pvolume_info->wsize;
  2401. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  2402. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  2403. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  2404. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  2405. cFYI(1, "file mode: 0x%x dir mode: 0x%x",
  2406. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  2407. cifs_sb->actimeo = pvolume_info->actimeo;
  2408. cifs_sb->local_nls = pvolume_info->local_nls;
  2409. if (pvolume_info->noperm)
  2410. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  2411. if (pvolume_info->setuids)
  2412. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  2413. if (pvolume_info->server_ino)
  2414. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  2415. if (pvolume_info->remap)
  2416. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  2417. if (pvolume_info->no_xattr)
  2418. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  2419. if (pvolume_info->sfu_emul)
  2420. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  2421. if (pvolume_info->nobrl)
  2422. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  2423. if (pvolume_info->nostrictsync)
  2424. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  2425. if (pvolume_info->mand_lock)
  2426. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  2427. if (pvolume_info->rwpidforward)
  2428. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
  2429. if (pvolume_info->cifs_acl)
  2430. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  2431. if (pvolume_info->override_uid)
  2432. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  2433. if (pvolume_info->override_gid)
  2434. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  2435. if (pvolume_info->dynperm)
  2436. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  2437. if (pvolume_info->fsc)
  2438. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
  2439. if (pvolume_info->multiuser)
  2440. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
  2441. CIFS_MOUNT_NO_PERM);
  2442. if (pvolume_info->strict_io)
  2443. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
  2444. if (pvolume_info->direct_io) {
  2445. cFYI(1, "mounting share using direct i/o");
  2446. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  2447. }
  2448. if (pvolume_info->mfsymlinks) {
  2449. if (pvolume_info->sfu_emul) {
  2450. cERROR(1, "mount option mfsymlinks ignored if sfu "
  2451. "mount option is used");
  2452. } else {
  2453. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
  2454. }
  2455. }
  2456. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  2457. cERROR(1, "mount option dynperm ignored if cifsacl "
  2458. "mount option supported");
  2459. }
  2460. /*
  2461. * When the server supports very large writes via POSIX extensions, we can
  2462. * allow up to 2^24 - PAGE_CACHE_SIZE.
  2463. *
  2464. * Note that this might make for "interesting" allocation problems during
  2465. * writeback however (as we have to allocate an array of pointers for the
  2466. * pages). A 16M write means ~32kb page array with PAGE_CACHE_SIZE == 4096.
  2467. */
  2468. #define CIFS_MAX_WSIZE ((1<<24) - PAGE_CACHE_SIZE)
  2469. /*
  2470. * When the server doesn't allow large posix writes, default to a wsize of
  2471. * 128k - PAGE_CACHE_SIZE -- one page less than the largest frame size
  2472. * described in RFC1001. This allows space for the header without going over
  2473. * that by default.
  2474. */
  2475. #define CIFS_MAX_RFC1001_WSIZE (128 * 1024 - PAGE_CACHE_SIZE)
  2476. /*
  2477. * The default wsize is 1M. find_get_pages seems to return a maximum of 256
  2478. * pages in a single call. With PAGE_CACHE_SIZE == 4k, this means we can fill
  2479. * a single wsize request with a single call.
  2480. */
  2481. #define CIFS_DEFAULT_WSIZE (1024 * 1024)
  2482. static unsigned int
  2483. cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
  2484. {
  2485. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2486. struct TCP_Server_Info *server = tcon->ses->server;
  2487. unsigned int wsize = pvolume_info->wsize ? pvolume_info->wsize :
  2488. CIFS_DEFAULT_WSIZE;
  2489. /* can server support 24-bit write sizes? (via UNIX extensions) */
  2490. if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  2491. wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1001_WSIZE);
  2492. /* no CAP_LARGE_WRITE_X? Limit it to 16 bits */
  2493. if (!(server->capabilities & CAP_LARGE_WRITE_X))
  2494. wsize = min_t(unsigned int, wsize, USHRT_MAX);
  2495. /* hard limit of CIFS_MAX_WSIZE */
  2496. wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
  2497. return wsize;
  2498. }
  2499. static int
  2500. is_path_accessible(int xid, struct cifs_tcon *tcon,
  2501. struct cifs_sb_info *cifs_sb, const char *full_path)
  2502. {
  2503. int rc;
  2504. FILE_ALL_INFO *pfile_info;
  2505. pfile_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  2506. if (pfile_info == NULL)
  2507. return -ENOMEM;
  2508. rc = CIFSSMBQPathInfo(xid, tcon, full_path, pfile_info,
  2509. 0 /* not legacy */, cifs_sb->local_nls,
  2510. cifs_sb->mnt_cifs_flags &
  2511. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2512. if (rc == -EOPNOTSUPP || rc == -EINVAL)
  2513. rc = SMBQueryInformation(xid, tcon, full_path, pfile_info,
  2514. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  2515. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2516. kfree(pfile_info);
  2517. return rc;
  2518. }
  2519. void
  2520. cifs_cleanup_volume_info(struct smb_vol **pvolume_info)
  2521. {
  2522. struct smb_vol *volume_info;
  2523. if (!pvolume_info || !*pvolume_info)
  2524. return;
  2525. volume_info = *pvolume_info;
  2526. kfree(volume_info->username);
  2527. kzfree(volume_info->password);
  2528. kfree(volume_info->UNC);
  2529. kfree(volume_info->UNCip);
  2530. kfree(volume_info->domainname);
  2531. kfree(volume_info->iocharset);
  2532. kfree(volume_info->prepath);
  2533. kfree(volume_info);
  2534. *pvolume_info = NULL;
  2535. return;
  2536. }
  2537. #ifdef CONFIG_CIFS_DFS_UPCALL
  2538. /* build_path_to_root returns full path to root when
  2539. * we do not have an exiting connection (tcon) */
  2540. static char *
  2541. build_unc_path_to_root(const struct smb_vol *volume_info,
  2542. const struct cifs_sb_info *cifs_sb)
  2543. {
  2544. char *full_path;
  2545. int unc_len = strnlen(volume_info->UNC, MAX_TREE_SIZE + 1);
  2546. full_path = kmalloc(unc_len + 1, GFP_KERNEL);
  2547. if (full_path == NULL)
  2548. return ERR_PTR(-ENOMEM);
  2549. strncpy(full_path, volume_info->UNC, unc_len);
  2550. full_path[unc_len] = 0; /* add trailing null */
  2551. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  2552. return full_path;
  2553. }
  2554. /*
  2555. * Perform a dfs referral query for a share and (optionally) prefix
  2556. *
  2557. * If a referral is found, cifs_sb->mountdata will be (re-)allocated
  2558. * to a string containing updated options for the submount. Otherwise it
  2559. * will be left untouched.
  2560. *
  2561. * Returns the rc from get_dfs_path to the caller, which can be used to
  2562. * determine whether there were referrals.
  2563. */
  2564. static int
  2565. expand_dfs_referral(int xid, struct cifs_ses *pSesInfo,
  2566. struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
  2567. int check_prefix)
  2568. {
  2569. int rc;
  2570. unsigned int num_referrals = 0;
  2571. struct dfs_info3_param *referrals = NULL;
  2572. char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
  2573. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  2574. if (IS_ERR(full_path))
  2575. return PTR_ERR(full_path);
  2576. /* For DFS paths, skip the first '\' of the UNC */
  2577. ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
  2578. rc = get_dfs_path(xid, pSesInfo , ref_path, cifs_sb->local_nls,
  2579. &num_referrals, &referrals,
  2580. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  2581. if (!rc && num_referrals > 0) {
  2582. char *fake_devname = NULL;
  2583. mdata = cifs_compose_mount_options(cifs_sb->mountdata,
  2584. full_path + 1, referrals,
  2585. &fake_devname);
  2586. free_dfs_info_array(referrals, num_referrals);
  2587. kfree(fake_devname);
  2588. if (cifs_sb->mountdata != NULL)
  2589. kfree(cifs_sb->mountdata);
  2590. if (IS_ERR(mdata)) {
  2591. rc = PTR_ERR(mdata);
  2592. mdata = NULL;
  2593. }
  2594. cifs_sb->mountdata = mdata;
  2595. }
  2596. kfree(full_path);
  2597. return rc;
  2598. }
  2599. #endif
  2600. int cifs_setup_volume_info(struct smb_vol **pvolume_info, char *mount_data,
  2601. const char *devname)
  2602. {
  2603. struct smb_vol *volume_info;
  2604. int rc = 0;
  2605. *pvolume_info = NULL;
  2606. volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
  2607. if (!volume_info) {
  2608. rc = -ENOMEM;
  2609. goto out;
  2610. }
  2611. if (cifs_parse_mount_options(mount_data, devname,
  2612. volume_info)) {
  2613. rc = -EINVAL;
  2614. goto out;
  2615. }
  2616. if (volume_info->nullauth) {
  2617. cFYI(1, "null user");
  2618. volume_info->username = "";
  2619. } else if (volume_info->username) {
  2620. /* BB fixme parse for domain name here */
  2621. cFYI(1, "Username: %s", volume_info->username);
  2622. } else {
  2623. cifserror("No username specified");
  2624. /* In userspace mount helper we can get user name from alternate
  2625. locations such as env variables and files on disk */
  2626. rc = -EINVAL;
  2627. goto out;
  2628. }
  2629. /* this is needed for ASCII cp to Unicode converts */
  2630. if (volume_info->iocharset == NULL) {
  2631. /* load_nls_default cannot return null */
  2632. volume_info->local_nls = load_nls_default();
  2633. } else {
  2634. volume_info->local_nls = load_nls(volume_info->iocharset);
  2635. if (volume_info->local_nls == NULL) {
  2636. cERROR(1, "CIFS mount error: iocharset %s not found",
  2637. volume_info->iocharset);
  2638. rc = -ELIBACC;
  2639. goto out;
  2640. }
  2641. }
  2642. *pvolume_info = volume_info;
  2643. return rc;
  2644. out:
  2645. cifs_cleanup_volume_info(&volume_info);
  2646. return rc;
  2647. }
  2648. int
  2649. cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
  2650. struct smb_vol *volume_info, const char *devname)
  2651. {
  2652. int rc = 0;
  2653. int xid;
  2654. struct cifs_ses *pSesInfo;
  2655. struct cifs_tcon *tcon;
  2656. struct TCP_Server_Info *srvTcp;
  2657. char *full_path;
  2658. struct tcon_link *tlink;
  2659. #ifdef CONFIG_CIFS_DFS_UPCALL
  2660. int referral_walks_count = 0;
  2661. try_mount_again:
  2662. /* cleanup activities if we're chasing a referral */
  2663. if (referral_walks_count) {
  2664. if (tcon)
  2665. cifs_put_tcon(tcon);
  2666. else if (pSesInfo)
  2667. cifs_put_smb_ses(pSesInfo);
  2668. cifs_cleanup_volume_info(&volume_info);
  2669. FreeXid(xid);
  2670. }
  2671. #endif
  2672. tcon = NULL;
  2673. pSesInfo = NULL;
  2674. srvTcp = NULL;
  2675. full_path = NULL;
  2676. tlink = NULL;
  2677. xid = GetXid();
  2678. /* get a reference to a tcp session */
  2679. srvTcp = cifs_get_tcp_session(volume_info);
  2680. if (IS_ERR(srvTcp)) {
  2681. rc = PTR_ERR(srvTcp);
  2682. goto out;
  2683. }
  2684. /* get a reference to a SMB session */
  2685. pSesInfo = cifs_get_smb_ses(srvTcp, volume_info);
  2686. if (IS_ERR(pSesInfo)) {
  2687. rc = PTR_ERR(pSesInfo);
  2688. pSesInfo = NULL;
  2689. goto mount_fail_check;
  2690. }
  2691. if (pSesInfo->capabilities & CAP_LARGE_FILES)
  2692. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2693. else
  2694. sb->s_maxbytes = MAX_NON_LFS;
  2695. /* BB FIXME fix time_gran to be larger for LANMAN sessions */
  2696. sb->s_time_gran = 100;
  2697. /* search for existing tcon to this server share */
  2698. tcon = cifs_get_tcon(pSesInfo, volume_info);
  2699. if (IS_ERR(tcon)) {
  2700. rc = PTR_ERR(tcon);
  2701. tcon = NULL;
  2702. goto remote_path_check;
  2703. }
  2704. /* tell server which Unix caps we support */
  2705. if (tcon->ses->capabilities & CAP_UNIX) {
  2706. /* reset of caps checks mount to see if unix extensions
  2707. disabled for just this mount */
  2708. reset_cifs_unix_caps(xid, tcon, sb, volume_info);
  2709. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  2710. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  2711. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
  2712. rc = -EACCES;
  2713. goto mount_fail_check;
  2714. }
  2715. } else
  2716. tcon->unix_ext = 0; /* server does not support them */
  2717. /* do not care if following two calls succeed - informational */
  2718. if (!tcon->ipc) {
  2719. CIFSSMBQFSDeviceInfo(xid, tcon);
  2720. CIFSSMBQFSAttributeInfo(xid, tcon);
  2721. }
  2722. if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
  2723. cifs_sb->rsize = 1024 * 127;
  2724. cFYI(DBG2, "no very large read support, rsize now 127K");
  2725. }
  2726. if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
  2727. cifs_sb->rsize = min(cifs_sb->rsize,
  2728. (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
  2729. cifs_sb->wsize = cifs_negotiate_wsize(tcon, volume_info);
  2730. remote_path_check:
  2731. #ifdef CONFIG_CIFS_DFS_UPCALL
  2732. /*
  2733. * Perform an unconditional check for whether there are DFS
  2734. * referrals for this path without prefix, to provide support
  2735. * for DFS referrals from w2k8 servers which don't seem to respond
  2736. * with PATH_NOT_COVERED to requests that include the prefix.
  2737. * Chase the referral if found, otherwise continue normally.
  2738. */
  2739. if (referral_walks_count == 0) {
  2740. int refrc = expand_dfs_referral(xid, pSesInfo, volume_info,
  2741. cifs_sb, false);
  2742. if (!refrc) {
  2743. referral_walks_count++;
  2744. goto try_mount_again;
  2745. }
  2746. }
  2747. #endif
  2748. /* check if a whole path is not remote */
  2749. if (!rc && tcon) {
  2750. /* build_path_to_root works only when we have a valid tcon */
  2751. full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
  2752. if (full_path == NULL) {
  2753. rc = -ENOMEM;
  2754. goto mount_fail_check;
  2755. }
  2756. rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
  2757. if (rc != 0 && rc != -EREMOTE) {
  2758. kfree(full_path);
  2759. goto mount_fail_check;
  2760. }
  2761. kfree(full_path);
  2762. }
  2763. /* get referral if needed */
  2764. if (rc == -EREMOTE) {
  2765. #ifdef CONFIG_CIFS_DFS_UPCALL
  2766. if (referral_walks_count > MAX_NESTED_LINKS) {
  2767. /*
  2768. * BB: when we implement proper loop detection,
  2769. * we will remove this check. But now we need it
  2770. * to prevent an indefinite loop if 'DFS tree' is
  2771. * misconfigured (i.e. has loops).
  2772. */
  2773. rc = -ELOOP;
  2774. goto mount_fail_check;
  2775. }
  2776. rc = expand_dfs_referral(xid, pSesInfo, volume_info, cifs_sb,
  2777. true);
  2778. if (!rc) {
  2779. referral_walks_count++;
  2780. goto try_mount_again;
  2781. }
  2782. goto mount_fail_check;
  2783. #else /* No DFS support, return error on mount */
  2784. rc = -EOPNOTSUPP;
  2785. #endif
  2786. }
  2787. if (rc)
  2788. goto mount_fail_check;
  2789. /* now, hang the tcon off of the superblock */
  2790. tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
  2791. if (tlink == NULL) {
  2792. rc = -ENOMEM;
  2793. goto mount_fail_check;
  2794. }
  2795. tlink->tl_uid = pSesInfo->linux_uid;
  2796. tlink->tl_tcon = tcon;
  2797. tlink->tl_time = jiffies;
  2798. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  2799. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  2800. cifs_sb->master_tlink = tlink;
  2801. spin_lock(&cifs_sb->tlink_tree_lock);
  2802. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  2803. spin_unlock(&cifs_sb->tlink_tree_lock);
  2804. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  2805. TLINK_IDLE_EXPIRE);
  2806. mount_fail_check:
  2807. /* on error free sesinfo and tcon struct if needed */
  2808. if (rc) {
  2809. /* If find_unc succeeded then rc == 0 so we can not end */
  2810. /* up accidentally freeing someone elses tcon struct */
  2811. if (tcon)
  2812. cifs_put_tcon(tcon);
  2813. else if (pSesInfo)
  2814. cifs_put_smb_ses(pSesInfo);
  2815. else
  2816. cifs_put_tcp_session(srvTcp);
  2817. goto out;
  2818. }
  2819. /* volume_info->password is freed above when existing session found
  2820. (in which case it is not needed anymore) but when new sesion is created
  2821. the password ptr is put in the new session structure (in which case the
  2822. password will be freed at unmount time) */
  2823. out:
  2824. /* zero out password before freeing */
  2825. FreeXid(xid);
  2826. return rc;
  2827. }
  2828. int
  2829. CIFSTCon(unsigned int xid, struct cifs_ses *ses,
  2830. const char *tree, struct cifs_tcon *tcon,
  2831. const struct nls_table *nls_codepage)
  2832. {
  2833. struct smb_hdr *smb_buffer;
  2834. struct smb_hdr *smb_buffer_response;
  2835. TCONX_REQ *pSMB;
  2836. TCONX_RSP *pSMBr;
  2837. unsigned char *bcc_ptr;
  2838. int rc = 0;
  2839. int length;
  2840. __u16 bytes_left, count;
  2841. if (ses == NULL)
  2842. return -EIO;
  2843. smb_buffer = cifs_buf_get();
  2844. if (smb_buffer == NULL)
  2845. return -ENOMEM;
  2846. smb_buffer_response = smb_buffer;
  2847. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  2848. NULL /*no tid */ , 4 /*wct */ );
  2849. smb_buffer->Mid = GetNextMid(ses->server);
  2850. smb_buffer->Uid = ses->Suid;
  2851. pSMB = (TCONX_REQ *) smb_buffer;
  2852. pSMBr = (TCONX_RSP *) smb_buffer_response;
  2853. pSMB->AndXCommand = 0xFF;
  2854. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  2855. bcc_ptr = &pSMB->Password[0];
  2856. if ((ses->server->sec_mode) & SECMODE_USER) {
  2857. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  2858. *bcc_ptr = 0; /* password is null byte */
  2859. bcc_ptr++; /* skip password */
  2860. /* already aligned so no need to do it below */
  2861. } else {
  2862. pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  2863. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  2864. specified as required (when that support is added to
  2865. the vfs in the future) as only NTLM or the much
  2866. weaker LANMAN (which we do not send by default) is accepted
  2867. by Samba (not sure whether other servers allow
  2868. NTLMv2 password here) */
  2869. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  2870. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  2871. (ses->server->secType == LANMAN))
  2872. calc_lanman_hash(tcon->password, ses->server->cryptkey,
  2873. ses->server->sec_mode &
  2874. SECMODE_PW_ENCRYPT ? true : false,
  2875. bcc_ptr);
  2876. else
  2877. #endif /* CIFS_WEAK_PW_HASH */
  2878. rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
  2879. bcc_ptr);
  2880. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  2881. if (ses->capabilities & CAP_UNICODE) {
  2882. /* must align unicode strings */
  2883. *bcc_ptr = 0; /* null byte password */
  2884. bcc_ptr++;
  2885. }
  2886. }
  2887. if (ses->server->sec_mode &
  2888. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2889. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2890. if (ses->capabilities & CAP_STATUS32) {
  2891. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2892. }
  2893. if (ses->capabilities & CAP_DFS) {
  2894. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2895. }
  2896. if (ses->capabilities & CAP_UNICODE) {
  2897. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2898. length =
  2899. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  2900. 6 /* max utf8 char length in bytes */ *
  2901. (/* server len*/ + 256 /* share len */), nls_codepage);
  2902. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  2903. bcc_ptr += 2; /* skip trailing null */
  2904. } else { /* ASCII */
  2905. strcpy(bcc_ptr, tree);
  2906. bcc_ptr += strlen(tree) + 1;
  2907. }
  2908. strcpy(bcc_ptr, "?????");
  2909. bcc_ptr += strlen("?????");
  2910. bcc_ptr += 1;
  2911. count = bcc_ptr - &pSMB->Password[0];
  2912. pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
  2913. pSMB->hdr.smb_buf_length) + count);
  2914. pSMB->ByteCount = cpu_to_le16(count);
  2915. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  2916. 0);
  2917. /* above now done in SendReceive */
  2918. if ((rc == 0) && (tcon != NULL)) {
  2919. bool is_unicode;
  2920. tcon->tidStatus = CifsGood;
  2921. tcon->need_reconnect = false;
  2922. tcon->tid = smb_buffer_response->Tid;
  2923. bcc_ptr = pByteArea(smb_buffer_response);
  2924. bytes_left = get_bcc(smb_buffer_response);
  2925. length = strnlen(bcc_ptr, bytes_left - 2);
  2926. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  2927. is_unicode = true;
  2928. else
  2929. is_unicode = false;
  2930. /* skip service field (NB: this field is always ASCII) */
  2931. if (length == 3) {
  2932. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  2933. (bcc_ptr[2] == 'C')) {
  2934. cFYI(1, "IPC connection");
  2935. tcon->ipc = 1;
  2936. }
  2937. } else if (length == 2) {
  2938. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  2939. /* the most common case */
  2940. cFYI(1, "disk share connection");
  2941. }
  2942. }
  2943. bcc_ptr += length + 1;
  2944. bytes_left -= (length + 1);
  2945. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  2946. /* mostly informational -- no need to fail on error here */
  2947. kfree(tcon->nativeFileSystem);
  2948. tcon->nativeFileSystem = cifs_strndup_from_ucs(bcc_ptr,
  2949. bytes_left, is_unicode,
  2950. nls_codepage);
  2951. cFYI(1, "nativeFileSystem=%s", tcon->nativeFileSystem);
  2952. if ((smb_buffer_response->WordCount == 3) ||
  2953. (smb_buffer_response->WordCount == 7))
  2954. /* field is in same location */
  2955. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  2956. else
  2957. tcon->Flags = 0;
  2958. cFYI(1, "Tcon flags: 0x%x ", tcon->Flags);
  2959. } else if ((rc == 0) && tcon == NULL) {
  2960. /* all we need to save for IPC$ connection */
  2961. ses->ipc_tid = smb_buffer_response->Tid;
  2962. }
  2963. cifs_buf_release(smb_buffer);
  2964. return rc;
  2965. }
  2966. int
  2967. cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
  2968. {
  2969. struct rb_root *root = &cifs_sb->tlink_tree;
  2970. struct rb_node *node;
  2971. struct tcon_link *tlink;
  2972. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  2973. spin_lock(&cifs_sb->tlink_tree_lock);
  2974. while ((node = rb_first(root))) {
  2975. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  2976. cifs_get_tlink(tlink);
  2977. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  2978. rb_erase(node, root);
  2979. spin_unlock(&cifs_sb->tlink_tree_lock);
  2980. cifs_put_tlink(tlink);
  2981. spin_lock(&cifs_sb->tlink_tree_lock);
  2982. }
  2983. spin_unlock(&cifs_sb->tlink_tree_lock);
  2984. return 0;
  2985. }
  2986. int cifs_negotiate_protocol(unsigned int xid, struct cifs_ses *ses)
  2987. {
  2988. int rc = 0;
  2989. struct TCP_Server_Info *server = ses->server;
  2990. /* only send once per connect */
  2991. if (server->maxBuf != 0)
  2992. return 0;
  2993. rc = CIFSSMBNegotiate(xid, ses);
  2994. if (rc == -EAGAIN) {
  2995. /* retry only once on 1st time connection */
  2996. rc = CIFSSMBNegotiate(xid, ses);
  2997. if (rc == -EAGAIN)
  2998. rc = -EHOSTDOWN;
  2999. }
  3000. if (rc == 0) {
  3001. spin_lock(&GlobalMid_Lock);
  3002. if (server->tcpStatus != CifsExiting)
  3003. server->tcpStatus = CifsGood;
  3004. else
  3005. rc = -EHOSTDOWN;
  3006. spin_unlock(&GlobalMid_Lock);
  3007. }
  3008. return rc;
  3009. }
  3010. int cifs_setup_session(unsigned int xid, struct cifs_ses *ses,
  3011. struct nls_table *nls_info)
  3012. {
  3013. int rc = 0;
  3014. struct TCP_Server_Info *server = ses->server;
  3015. ses->flags = 0;
  3016. ses->capabilities = server->capabilities;
  3017. if (linuxExtEnabled == 0)
  3018. ses->capabilities &= (~CAP_UNIX);
  3019. cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  3020. server->sec_mode, server->capabilities, server->timeAdj);
  3021. rc = CIFS_SessSetup(xid, ses, nls_info);
  3022. if (rc) {
  3023. cERROR(1, "Send error in SessSetup = %d", rc);
  3024. } else {
  3025. mutex_lock(&ses->server->srv_mutex);
  3026. if (!server->session_estab) {
  3027. server->session_key.response = ses->auth_key.response;
  3028. server->session_key.len = ses->auth_key.len;
  3029. server->sequence_number = 0x2;
  3030. server->session_estab = true;
  3031. ses->auth_key.response = NULL;
  3032. }
  3033. mutex_unlock(&server->srv_mutex);
  3034. cFYI(1, "CIFS Session Established successfully");
  3035. spin_lock(&GlobalMid_Lock);
  3036. ses->status = CifsGood;
  3037. ses->need_reconnect = false;
  3038. spin_unlock(&GlobalMid_Lock);
  3039. }
  3040. kfree(ses->auth_key.response);
  3041. ses->auth_key.response = NULL;
  3042. ses->auth_key.len = 0;
  3043. kfree(ses->ntlmssp);
  3044. ses->ntlmssp = NULL;
  3045. return rc;
  3046. }
  3047. static struct cifs_tcon *
  3048. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, uid_t fsuid)
  3049. {
  3050. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  3051. struct cifs_ses *ses;
  3052. struct cifs_tcon *tcon = NULL;
  3053. struct smb_vol *vol_info;
  3054. char username[28]; /* big enough for "krb50x" + hex of ULONG_MAX 6+16 */
  3055. /* We used to have this as MAX_USERNAME which is */
  3056. /* way too big now (256 instead of 32) */
  3057. vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
  3058. if (vol_info == NULL) {
  3059. tcon = ERR_PTR(-ENOMEM);
  3060. goto out;
  3061. }
  3062. snprintf(username, MAX_USERNAME_SIZE, "krb50x%x", fsuid);
  3063. vol_info->username = username;
  3064. vol_info->local_nls = cifs_sb->local_nls;
  3065. vol_info->linux_uid = fsuid;
  3066. vol_info->cred_uid = fsuid;
  3067. vol_info->UNC = master_tcon->treeName;
  3068. vol_info->retry = master_tcon->retry;
  3069. vol_info->nocase = master_tcon->nocase;
  3070. vol_info->local_lease = master_tcon->local_lease;
  3071. vol_info->no_linux_ext = !master_tcon->unix_ext;
  3072. /* FIXME: allow for other secFlg settings */
  3073. vol_info->secFlg = CIFSSEC_MUST_KRB5;
  3074. /* get a reference for the same TCP session */
  3075. spin_lock(&cifs_tcp_ses_lock);
  3076. ++master_tcon->ses->server->srv_count;
  3077. spin_unlock(&cifs_tcp_ses_lock);
  3078. ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
  3079. if (IS_ERR(ses)) {
  3080. tcon = (struct cifs_tcon *)ses;
  3081. cifs_put_tcp_session(master_tcon->ses->server);
  3082. goto out;
  3083. }
  3084. tcon = cifs_get_tcon(ses, vol_info);
  3085. if (IS_ERR(tcon)) {
  3086. cifs_put_smb_ses(ses);
  3087. goto out;
  3088. }
  3089. if (ses->capabilities & CAP_UNIX)
  3090. reset_cifs_unix_caps(0, tcon, NULL, vol_info);
  3091. out:
  3092. kfree(vol_info);
  3093. return tcon;
  3094. }
  3095. static inline struct tcon_link *
  3096. cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
  3097. {
  3098. return cifs_sb->master_tlink;
  3099. }
  3100. struct cifs_tcon *
  3101. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  3102. {
  3103. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  3104. }
  3105. static int
  3106. cifs_sb_tcon_pending_wait(void *unused)
  3107. {
  3108. schedule();
  3109. return signal_pending(current) ? -ERESTARTSYS : 0;
  3110. }
  3111. /* find and return a tlink with given uid */
  3112. static struct tcon_link *
  3113. tlink_rb_search(struct rb_root *root, uid_t uid)
  3114. {
  3115. struct rb_node *node = root->rb_node;
  3116. struct tcon_link *tlink;
  3117. while (node) {
  3118. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3119. if (tlink->tl_uid > uid)
  3120. node = node->rb_left;
  3121. else if (tlink->tl_uid < uid)
  3122. node = node->rb_right;
  3123. else
  3124. return tlink;
  3125. }
  3126. return NULL;
  3127. }
  3128. /* insert a tcon_link into the tree */
  3129. static void
  3130. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  3131. {
  3132. struct rb_node **new = &(root->rb_node), *parent = NULL;
  3133. struct tcon_link *tlink;
  3134. while (*new) {
  3135. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  3136. parent = *new;
  3137. if (tlink->tl_uid > new_tlink->tl_uid)
  3138. new = &((*new)->rb_left);
  3139. else
  3140. new = &((*new)->rb_right);
  3141. }
  3142. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  3143. rb_insert_color(&new_tlink->tl_rbnode, root);
  3144. }
  3145. /*
  3146. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  3147. * current task.
  3148. *
  3149. * If the superblock doesn't refer to a multiuser mount, then just return
  3150. * the master tcon for the mount.
  3151. *
  3152. * First, search the rbtree for an existing tcon for this fsuid. If one
  3153. * exists, then check to see if it's pending construction. If it is then wait
  3154. * for construction to complete. Once it's no longer pending, check to see if
  3155. * it failed and either return an error or retry construction, depending on
  3156. * the timeout.
  3157. *
  3158. * If one doesn't exist then insert a new tcon_link struct into the tree and
  3159. * try to construct a new one.
  3160. */
  3161. struct tcon_link *
  3162. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  3163. {
  3164. int ret;
  3165. uid_t fsuid = current_fsuid();
  3166. struct tcon_link *tlink, *newtlink;
  3167. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  3168. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  3169. spin_lock(&cifs_sb->tlink_tree_lock);
  3170. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3171. if (tlink)
  3172. cifs_get_tlink(tlink);
  3173. spin_unlock(&cifs_sb->tlink_tree_lock);
  3174. if (tlink == NULL) {
  3175. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3176. if (newtlink == NULL)
  3177. return ERR_PTR(-ENOMEM);
  3178. newtlink->tl_uid = fsuid;
  3179. newtlink->tl_tcon = ERR_PTR(-EACCES);
  3180. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  3181. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  3182. cifs_get_tlink(newtlink);
  3183. spin_lock(&cifs_sb->tlink_tree_lock);
  3184. /* was one inserted after previous search? */
  3185. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3186. if (tlink) {
  3187. cifs_get_tlink(tlink);
  3188. spin_unlock(&cifs_sb->tlink_tree_lock);
  3189. kfree(newtlink);
  3190. goto wait_for_construction;
  3191. }
  3192. tlink = newtlink;
  3193. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3194. spin_unlock(&cifs_sb->tlink_tree_lock);
  3195. } else {
  3196. wait_for_construction:
  3197. ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  3198. cifs_sb_tcon_pending_wait,
  3199. TASK_INTERRUPTIBLE);
  3200. if (ret) {
  3201. cifs_put_tlink(tlink);
  3202. return ERR_PTR(ret);
  3203. }
  3204. /* if it's good, return it */
  3205. if (!IS_ERR(tlink->tl_tcon))
  3206. return tlink;
  3207. /* return error if we tried this already recently */
  3208. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  3209. cifs_put_tlink(tlink);
  3210. return ERR_PTR(-EACCES);
  3211. }
  3212. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  3213. goto wait_for_construction;
  3214. }
  3215. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  3216. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  3217. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  3218. if (IS_ERR(tlink->tl_tcon)) {
  3219. cifs_put_tlink(tlink);
  3220. return ERR_PTR(-EACCES);
  3221. }
  3222. return tlink;
  3223. }
  3224. /*
  3225. * periodic workqueue job that scans tcon_tree for a superblock and closes
  3226. * out tcons.
  3227. */
  3228. static void
  3229. cifs_prune_tlinks(struct work_struct *work)
  3230. {
  3231. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  3232. prune_tlinks.work);
  3233. struct rb_root *root = &cifs_sb->tlink_tree;
  3234. struct rb_node *node = rb_first(root);
  3235. struct rb_node *tmp;
  3236. struct tcon_link *tlink;
  3237. /*
  3238. * Because we drop the spinlock in the loop in order to put the tlink
  3239. * it's not guarded against removal of links from the tree. The only
  3240. * places that remove entries from the tree are this function and
  3241. * umounts. Because this function is non-reentrant and is canceled
  3242. * before umount can proceed, this is safe.
  3243. */
  3244. spin_lock(&cifs_sb->tlink_tree_lock);
  3245. node = rb_first(root);
  3246. while (node != NULL) {
  3247. tmp = node;
  3248. node = rb_next(tmp);
  3249. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  3250. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  3251. atomic_read(&tlink->tl_count) != 0 ||
  3252. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  3253. continue;
  3254. cifs_get_tlink(tlink);
  3255. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3256. rb_erase(tmp, root);
  3257. spin_unlock(&cifs_sb->tlink_tree_lock);
  3258. cifs_put_tlink(tlink);
  3259. spin_lock(&cifs_sb->tlink_tree_lock);
  3260. }
  3261. spin_unlock(&cifs_sb->tlink_tree_lock);
  3262. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  3263. TLINK_IDLE_EXPIRE);
  3264. }