connect.c 105 KB

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