connect.c 103 KB

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