connect.c 98 KB

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