connect.c 111 KB

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