connect.c 107 KB

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