connect.c 106 KB

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