connect.c 111 KB

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