connect.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641
  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. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  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 < pdu_length) {
  397. cFYI(1, ("requested %d bytes but only got %d bytes",
  398. pdu_length, 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->hostname);
  688. kfree(server);
  689. if (length > 0)
  690. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  691. GFP_KERNEL);
  692. return 0;
  693. }
  694. /* extract the host portion of the UNC string */
  695. static char *
  696. extract_hostname(const char *unc)
  697. {
  698. const char *src;
  699. char *dst, *delim;
  700. unsigned int len;
  701. /* skip double chars at beginning of string */
  702. /* BB: check validity of these bytes? */
  703. src = unc + 2;
  704. /* delimiter between hostname and sharename is always '\\' now */
  705. delim = strchr(src, '\\');
  706. if (!delim)
  707. return ERR_PTR(-EINVAL);
  708. len = delim - src;
  709. dst = kmalloc((len + 1), GFP_KERNEL);
  710. if (dst == NULL)
  711. return ERR_PTR(-ENOMEM);
  712. memcpy(dst, src, len);
  713. dst[len] = '\0';
  714. return dst;
  715. }
  716. static int
  717. cifs_parse_mount_options(char *options, const char *devname,
  718. struct smb_vol *vol)
  719. {
  720. char *value;
  721. char *data;
  722. unsigned int temp_len, i, j;
  723. char separator[2];
  724. separator[0] = ',';
  725. separator[1] = 0;
  726. if (Local_System_Name[0] != 0)
  727. memcpy(vol->source_rfc1001_name, Local_System_Name, 15);
  728. else {
  729. char *nodename = utsname()->nodename;
  730. int n = strnlen(nodename, 15);
  731. memset(vol->source_rfc1001_name, 0x20, 15);
  732. for (i = 0; i < n; i++) {
  733. /* does not have to be perfect mapping since field is
  734. informational, only used for servers that do not support
  735. port 445 and it can be overridden at mount time */
  736. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  737. }
  738. }
  739. vol->source_rfc1001_name[15] = 0;
  740. /* null target name indicates to use *SMBSERVR default called name
  741. if we end up sending RFC1001 session initialize */
  742. vol->target_rfc1001_name[0] = 0;
  743. vol->linux_uid = current->uid; /* current->euid instead? */
  744. vol->linux_gid = current->gid;
  745. vol->dir_mode = S_IRWXUGO;
  746. /* 2767 perms indicate mandatory locking support */
  747. vol->file_mode = (S_IRWXUGO | S_ISGID) & (~S_IXGRP);
  748. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  749. vol->rw = TRUE;
  750. /* default is always to request posix paths. */
  751. vol->posix_paths = 1;
  752. if (!options)
  753. return 1;
  754. if (strncmp(options, "sep=", 4) == 0) {
  755. if (options[4] != 0) {
  756. separator[0] = options[4];
  757. options += 5;
  758. } else {
  759. cFYI(1, ("Null separator not allowed"));
  760. }
  761. }
  762. while ((data = strsep(&options, separator)) != NULL) {
  763. if (!*data)
  764. continue;
  765. if ((value = strchr(data, '=')) != NULL)
  766. *value++ = '\0';
  767. /* Have to parse this before we parse for "user" */
  768. if (strnicmp(data, "user_xattr", 10) == 0) {
  769. vol->no_xattr = 0;
  770. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  771. vol->no_xattr = 1;
  772. } else if (strnicmp(data, "user", 4) == 0) {
  773. if (!value) {
  774. printk(KERN_WARNING
  775. "CIFS: invalid or missing username\n");
  776. return 1; /* needs_arg; */
  777. } else if (!*value) {
  778. /* null user, ie anonymous, authentication */
  779. vol->nullauth = 1;
  780. }
  781. if (strnlen(value, 200) < 200) {
  782. vol->username = value;
  783. } else {
  784. printk(KERN_WARNING "CIFS: username too long\n");
  785. return 1;
  786. }
  787. } else if (strnicmp(data, "pass", 4) == 0) {
  788. if (!value) {
  789. vol->password = NULL;
  790. continue;
  791. } else if (value[0] == 0) {
  792. /* check if string begins with double comma
  793. since that would mean the password really
  794. does start with a comma, and would not
  795. indicate an empty string */
  796. if (value[1] != separator[0]) {
  797. vol->password = NULL;
  798. continue;
  799. }
  800. }
  801. temp_len = strlen(value);
  802. /* removed password length check, NTLM passwords
  803. can be arbitrarily long */
  804. /* if comma in password, the string will be
  805. prematurely null terminated. Commas in password are
  806. specified across the cifs mount interface by a double
  807. comma ie ,, and a comma used as in other cases ie ','
  808. as a parameter delimiter/separator is single and due
  809. to the strsep above is temporarily zeroed. */
  810. /* NB: password legally can have multiple commas and
  811. the only illegal character in a password is null */
  812. if ((value[temp_len] == 0) &&
  813. (value[temp_len+1] == separator[0])) {
  814. /* reinsert comma */
  815. value[temp_len] = separator[0];
  816. temp_len += 2; /* move after second comma */
  817. while (value[temp_len] != 0) {
  818. if (value[temp_len] == separator[0]) {
  819. if (value[temp_len+1] ==
  820. separator[0]) {
  821. /* skip second comma */
  822. temp_len++;
  823. } else {
  824. /* single comma indicating start
  825. of next parm */
  826. break;
  827. }
  828. }
  829. temp_len++;
  830. }
  831. if (value[temp_len] == 0) {
  832. options = NULL;
  833. } else {
  834. value[temp_len] = 0;
  835. /* point option to start of next parm */
  836. options = value + temp_len + 1;
  837. }
  838. /* go from value to value + temp_len condensing
  839. double commas to singles. Note that this ends up
  840. allocating a few bytes too many, which is ok */
  841. vol->password = kzalloc(temp_len, GFP_KERNEL);
  842. if (vol->password == NULL) {
  843. printk(KERN_WARNING "CIFS: no memory "
  844. "for password\n");
  845. return 1;
  846. }
  847. for (i = 0, j = 0; i < temp_len; i++, j++) {
  848. vol->password[j] = value[i];
  849. if (value[i] == separator[0]
  850. && value[i+1] == separator[0]) {
  851. /* skip second comma */
  852. i++;
  853. }
  854. }
  855. vol->password[j] = 0;
  856. } else {
  857. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  858. if (vol->password == NULL) {
  859. printk(KERN_WARNING "CIFS: no memory "
  860. "for password\n");
  861. return 1;
  862. }
  863. strcpy(vol->password, value);
  864. }
  865. } else if (strnicmp(data, "ip", 2) == 0) {
  866. if (!value || !*value) {
  867. vol->UNCip = NULL;
  868. } else if (strnlen(value, 35) < 35) {
  869. vol->UNCip = value;
  870. } else {
  871. printk(KERN_WARNING "CIFS: ip address "
  872. "too long\n");
  873. return 1;
  874. }
  875. } else if (strnicmp(data, "sec", 3) == 0) {
  876. if (!value || !*value) {
  877. cERROR(1, ("no security value specified"));
  878. continue;
  879. } else if (strnicmp(value, "krb5i", 5) == 0) {
  880. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  881. CIFSSEC_MUST_SIGN;
  882. } else if (strnicmp(value, "krb5p", 5) == 0) {
  883. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  884. CIFSSEC_MAY_KRB5; */
  885. cERROR(1, ("Krb5 cifs privacy not supported"));
  886. return 1;
  887. } else if (strnicmp(value, "krb5", 4) == 0) {
  888. vol->secFlg |= CIFSSEC_MAY_KRB5;
  889. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  890. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  891. CIFSSEC_MUST_SIGN;
  892. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  893. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  894. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  895. vol->secFlg |= CIFSSEC_MAY_NTLM |
  896. CIFSSEC_MUST_SIGN;
  897. } else if (strnicmp(value, "ntlm", 4) == 0) {
  898. /* ntlm is default so can be turned off too */
  899. vol->secFlg |= CIFSSEC_MAY_NTLM;
  900. } else if (strnicmp(value, "nontlm", 6) == 0) {
  901. /* BB is there a better way to do this? */
  902. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  903. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  904. } else if (strnicmp(value, "lanman", 6) == 0) {
  905. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  906. #endif
  907. } else if (strnicmp(value, "none", 4) == 0) {
  908. vol->nullauth = 1;
  909. } else {
  910. cERROR(1, ("bad security option: %s", value));
  911. return 1;
  912. }
  913. } else if ((strnicmp(data, "unc", 3) == 0)
  914. || (strnicmp(data, "target", 6) == 0)
  915. || (strnicmp(data, "path", 4) == 0)) {
  916. if (!value || !*value) {
  917. printk(KERN_WARNING "CIFS: invalid path to "
  918. "network resource\n");
  919. return 1; /* needs_arg; */
  920. }
  921. if ((temp_len = strnlen(value, 300)) < 300) {
  922. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  923. if (vol->UNC == NULL)
  924. return 1;
  925. strcpy(vol->UNC, value);
  926. if (strncmp(vol->UNC, "//", 2) == 0) {
  927. vol->UNC[0] = '\\';
  928. vol->UNC[1] = '\\';
  929. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  930. printk(KERN_WARNING
  931. "CIFS: UNC Path does not begin "
  932. "with // or \\\\ \n");
  933. return 1;
  934. }
  935. } else {
  936. printk(KERN_WARNING "CIFS: UNC name too long\n");
  937. return 1;
  938. }
  939. } else if ((strnicmp(data, "domain", 3) == 0)
  940. || (strnicmp(data, "workgroup", 5) == 0)) {
  941. if (!value || !*value) {
  942. printk(KERN_WARNING "CIFS: invalid domain name\n");
  943. return 1; /* needs_arg; */
  944. }
  945. /* BB are there cases in which a comma can be valid in
  946. a domain name and need special handling? */
  947. if (strnlen(value, 256) < 256) {
  948. vol->domainname = value;
  949. cFYI(1, ("Domain name set"));
  950. } else {
  951. printk(KERN_WARNING "CIFS: domain name too "
  952. "long\n");
  953. return 1;
  954. }
  955. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  956. if (!value || !*value) {
  957. printk(KERN_WARNING
  958. "CIFS: invalid path prefix\n");
  959. return 1; /* needs_argument */
  960. }
  961. if ((temp_len = strnlen(value, 1024)) < 1024) {
  962. if (value[0] != '/')
  963. temp_len++; /* missing leading slash */
  964. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  965. if (vol->prepath == NULL)
  966. return 1;
  967. if (value[0] != '/') {
  968. vol->prepath[0] = '/';
  969. strcpy(vol->prepath+1, value);
  970. } else
  971. strcpy(vol->prepath, value);
  972. cFYI(1, ("prefix path %s", vol->prepath));
  973. } else {
  974. printk(KERN_WARNING "CIFS: prefix too long\n");
  975. return 1;
  976. }
  977. } else if (strnicmp(data, "iocharset", 9) == 0) {
  978. if (!value || !*value) {
  979. printk(KERN_WARNING "CIFS: invalid iocharset "
  980. "specified\n");
  981. return 1; /* needs_arg; */
  982. }
  983. if (strnlen(value, 65) < 65) {
  984. if (strnicmp(value, "default", 7))
  985. vol->iocharset = value;
  986. /* if iocharset not set then load_nls_default
  987. is used by caller */
  988. cFYI(1, ("iocharset set to %s", value));
  989. } else {
  990. printk(KERN_WARNING "CIFS: iocharset name "
  991. "too long.\n");
  992. return 1;
  993. }
  994. } else if (strnicmp(data, "uid", 3) == 0) {
  995. if (value && *value) {
  996. vol->linux_uid =
  997. simple_strtoul(value, &value, 0);
  998. vol->override_uid = 1;
  999. }
  1000. } else if (strnicmp(data, "gid", 3) == 0) {
  1001. if (value && *value) {
  1002. vol->linux_gid =
  1003. simple_strtoul(value, &value, 0);
  1004. vol->override_gid = 1;
  1005. }
  1006. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1007. if (value && *value) {
  1008. vol->file_mode =
  1009. simple_strtoul(value, &value, 0);
  1010. }
  1011. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1012. if (value && *value) {
  1013. vol->dir_mode =
  1014. simple_strtoul(value, &value, 0);
  1015. }
  1016. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1017. if (value && *value) {
  1018. vol->dir_mode =
  1019. simple_strtoul(value, &value, 0);
  1020. }
  1021. } else if (strnicmp(data, "port", 4) == 0) {
  1022. if (value && *value) {
  1023. vol->port =
  1024. simple_strtoul(value, &value, 0);
  1025. }
  1026. } else if (strnicmp(data, "rsize", 5) == 0) {
  1027. if (value && *value) {
  1028. vol->rsize =
  1029. simple_strtoul(value, &value, 0);
  1030. }
  1031. } else if (strnicmp(data, "wsize", 5) == 0) {
  1032. if (value && *value) {
  1033. vol->wsize =
  1034. simple_strtoul(value, &value, 0);
  1035. }
  1036. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1037. if (value && *value) {
  1038. vol->sockopt =
  1039. simple_strtoul(value, &value, 0);
  1040. }
  1041. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1042. if (!value || !*value || (*value == ' ')) {
  1043. cFYI(1, ("invalid (empty) netbiosname"));
  1044. } else {
  1045. memset(vol->source_rfc1001_name, 0x20, 15);
  1046. for (i = 0; i < 15; i++) {
  1047. /* BB are there cases in which a comma can be
  1048. valid in this workstation netbios name (and need
  1049. special handling)? */
  1050. /* We do not uppercase netbiosname for user */
  1051. if (value[i] == 0)
  1052. break;
  1053. else
  1054. vol->source_rfc1001_name[i] =
  1055. value[i];
  1056. }
  1057. /* The string has 16th byte zero still from
  1058. set at top of the function */
  1059. if ((i == 15) && (value[i] != 0))
  1060. printk(KERN_WARNING "CIFS: netbiosname"
  1061. " longer than 15 truncated.\n");
  1062. }
  1063. } else if (strnicmp(data, "servern", 7) == 0) {
  1064. /* servernetbiosname specified override *SMBSERVER */
  1065. if (!value || !*value || (*value == ' ')) {
  1066. cFYI(1, ("empty server netbiosname specified"));
  1067. } else {
  1068. /* last byte, type, is 0x20 for servr type */
  1069. memset(vol->target_rfc1001_name, 0x20, 16);
  1070. for (i = 0; i < 15; i++) {
  1071. /* BB are there cases in which a comma can be
  1072. valid in this workstation netbios name
  1073. (and need special handling)? */
  1074. /* user or mount helper must uppercase
  1075. the netbiosname */
  1076. if (value[i] == 0)
  1077. break;
  1078. else
  1079. vol->target_rfc1001_name[i] =
  1080. value[i];
  1081. }
  1082. /* The string has 16th byte zero still from
  1083. set at top of the function */
  1084. if ((i == 15) && (value[i] != 0))
  1085. printk(KERN_WARNING "CIFS: server net"
  1086. "biosname longer than 15 truncated.\n");
  1087. }
  1088. } else if (strnicmp(data, "credentials", 4) == 0) {
  1089. /* ignore */
  1090. } else if (strnicmp(data, "version", 3) == 0) {
  1091. /* ignore */
  1092. } else if (strnicmp(data, "guest", 5) == 0) {
  1093. /* ignore */
  1094. } else if (strnicmp(data, "rw", 2) == 0) {
  1095. vol->rw = TRUE;
  1096. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1097. (strnicmp(data, "nosuid", 6) == 0) ||
  1098. (strnicmp(data, "exec", 4) == 0) ||
  1099. (strnicmp(data, "noexec", 6) == 0) ||
  1100. (strnicmp(data, "nodev", 5) == 0) ||
  1101. (strnicmp(data, "noauto", 6) == 0) ||
  1102. (strnicmp(data, "dev", 3) == 0)) {
  1103. /* The mount tool or mount.cifs helper (if present)
  1104. uses these opts to set flags, and the flags are read
  1105. by the kernel vfs layer before we get here (ie
  1106. before read super) so there is no point trying to
  1107. parse these options again and set anything and it
  1108. is ok to just ignore them */
  1109. continue;
  1110. } else if (strnicmp(data, "ro", 2) == 0) {
  1111. vol->rw = FALSE;
  1112. } else if (strnicmp(data, "hard", 4) == 0) {
  1113. vol->retry = 1;
  1114. } else if (strnicmp(data, "soft", 4) == 0) {
  1115. vol->retry = 0;
  1116. } else if (strnicmp(data, "perm", 4) == 0) {
  1117. vol->noperm = 0;
  1118. } else if (strnicmp(data, "noperm", 6) == 0) {
  1119. vol->noperm = 1;
  1120. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1121. vol->remap = 1;
  1122. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1123. vol->remap = 0;
  1124. } else if (strnicmp(data, "sfu", 3) == 0) {
  1125. vol->sfu_emul = 1;
  1126. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1127. vol->sfu_emul = 0;
  1128. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1129. vol->posix_paths = 1;
  1130. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1131. vol->posix_paths = 0;
  1132. } else if (strnicmp(data, "nounix", 6) == 0) {
  1133. vol->no_linux_ext = 1;
  1134. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1135. vol->no_linux_ext = 1;
  1136. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1137. (strnicmp(data, "ignorecase", 10) == 0)) {
  1138. vol->nocase = 1;
  1139. } else if (strnicmp(data, "brl", 3) == 0) {
  1140. vol->nobrl = 0;
  1141. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1142. (strnicmp(data, "nolock", 6) == 0)) {
  1143. vol->nobrl = 1;
  1144. /* turn off mandatory locking in mode
  1145. if remote locking is turned off since the
  1146. local vfs will do advisory */
  1147. if (vol->file_mode ==
  1148. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1149. vol->file_mode = S_IALLUGO;
  1150. } else if (strnicmp(data, "setuids", 7) == 0) {
  1151. vol->setuids = 1;
  1152. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1153. vol->setuids = 0;
  1154. } else if (strnicmp(data, "nohard", 6) == 0) {
  1155. vol->retry = 0;
  1156. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1157. vol->retry = 1;
  1158. } else if (strnicmp(data, "nointr", 6) == 0) {
  1159. vol->intr = 0;
  1160. } else if (strnicmp(data, "intr", 4) == 0) {
  1161. vol->intr = 1;
  1162. } else if (strnicmp(data, "serverino", 7) == 0) {
  1163. vol->server_ino = 1;
  1164. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1165. vol->server_ino = 0;
  1166. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1167. vol->cifs_acl = 1;
  1168. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1169. vol->cifs_acl = 0;
  1170. } else if (strnicmp(data, "acl", 3) == 0) {
  1171. vol->no_psx_acl = 0;
  1172. } else if (strnicmp(data, "noacl", 5) == 0) {
  1173. vol->no_psx_acl = 1;
  1174. } else if (strnicmp(data, "sign", 4) == 0) {
  1175. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1176. /* } else if (strnicmp(data, "seal",4) == 0) {
  1177. vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1178. } else if (strnicmp(data, "direct", 6) == 0) {
  1179. vol->direct_io = 1;
  1180. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1181. vol->direct_io = 1;
  1182. } else if (strnicmp(data, "in6_addr", 8) == 0) {
  1183. if (!value || !*value) {
  1184. vol->in6_addr = NULL;
  1185. } else if (strnlen(value, 49) == 48) {
  1186. vol->in6_addr = value;
  1187. } else {
  1188. printk(KERN_WARNING "CIFS: ip v6 address not "
  1189. "48 characters long\n");
  1190. return 1;
  1191. }
  1192. } else if (strnicmp(data, "noac", 4) == 0) {
  1193. printk(KERN_WARNING "CIFS: Mount option noac not "
  1194. "supported. Instead set "
  1195. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1196. } else
  1197. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1198. data);
  1199. }
  1200. if (vol->UNC == NULL) {
  1201. if (devname == NULL) {
  1202. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1203. "target\n");
  1204. return 1;
  1205. }
  1206. if ((temp_len = strnlen(devname, 300)) < 300) {
  1207. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1208. if (vol->UNC == NULL)
  1209. return 1;
  1210. strcpy(vol->UNC, devname);
  1211. if (strncmp(vol->UNC, "//", 2) == 0) {
  1212. vol->UNC[0] = '\\';
  1213. vol->UNC[1] = '\\';
  1214. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1215. printk(KERN_WARNING "CIFS: UNC Path does not "
  1216. "begin with // or \\\\ \n");
  1217. return 1;
  1218. }
  1219. } else {
  1220. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1221. return 1;
  1222. }
  1223. }
  1224. if (vol->UNCip == NULL)
  1225. vol->UNCip = &vol->UNC[2];
  1226. return 0;
  1227. }
  1228. static struct cifsSesInfo *
  1229. cifs_find_tcp_session(struct in_addr *target_ip_addr,
  1230. struct in6_addr *target_ip6_addr,
  1231. char *userName, struct TCP_Server_Info **psrvTcp)
  1232. {
  1233. struct list_head *tmp;
  1234. struct cifsSesInfo *ses;
  1235. *psrvTcp = NULL;
  1236. read_lock(&GlobalSMBSeslock);
  1237. list_for_each(tmp, &GlobalSMBSessionList) {
  1238. ses = list_entry(tmp, struct cifsSesInfo, cifsSessionList);
  1239. if (ses->server) {
  1240. if ((target_ip_addr &&
  1241. (ses->server->addr.sockAddr.sin_addr.s_addr
  1242. == target_ip_addr->s_addr)) || (target_ip6_addr
  1243. && memcmp(&ses->server->addr.sockAddr6.sin6_addr,
  1244. target_ip6_addr, sizeof(*target_ip6_addr)))) {
  1245. /* BB lock server and tcp session and increment
  1246. use count here?? */
  1247. /* found a match on the TCP session */
  1248. *psrvTcp = ses->server;
  1249. /* BB check if reconnection needed */
  1250. if (strncmp
  1251. (ses->userName, userName,
  1252. MAX_USERNAME_SIZE) == 0){
  1253. read_unlock(&GlobalSMBSeslock);
  1254. /* Found exact match on both TCP and
  1255. SMB sessions */
  1256. return ses;
  1257. }
  1258. }
  1259. }
  1260. /* else tcp and smb sessions need reconnection */
  1261. }
  1262. read_unlock(&GlobalSMBSeslock);
  1263. return NULL;
  1264. }
  1265. static struct cifsTconInfo *
  1266. find_unc(__be32 new_target_ip_addr, char *uncName, char *userName)
  1267. {
  1268. struct list_head *tmp;
  1269. struct cifsTconInfo *tcon;
  1270. read_lock(&GlobalSMBSeslock);
  1271. list_for_each(tmp, &GlobalTreeConnectionList) {
  1272. cFYI(1, ("Next tcon"));
  1273. tcon = list_entry(tmp, struct cifsTconInfo, cifsConnectionList);
  1274. if (tcon->ses) {
  1275. if (tcon->ses->server) {
  1276. cFYI(1,
  1277. ("old ip addr: %x == new ip %x ?",
  1278. tcon->ses->server->addr.sockAddr.sin_addr.
  1279. s_addr, new_target_ip_addr));
  1280. if (tcon->ses->server->addr.sockAddr.sin_addr.
  1281. s_addr == new_target_ip_addr) {
  1282. /* BB lock tcon, server and tcp session and increment use count here? */
  1283. /* found a match on the TCP session */
  1284. /* BB check if reconnection needed */
  1285. cFYI(1,
  1286. ("IP match, old UNC: %s new: %s",
  1287. tcon->treeName, uncName));
  1288. if (strncmp
  1289. (tcon->treeName, uncName,
  1290. MAX_TREE_SIZE) == 0) {
  1291. cFYI(1,
  1292. ("and old usr: %s new: %s",
  1293. tcon->treeName, uncName));
  1294. if (strncmp
  1295. (tcon->ses->userName,
  1296. userName,
  1297. MAX_USERNAME_SIZE) == 0) {
  1298. read_unlock(&GlobalSMBSeslock);
  1299. /* matched smb session
  1300. (user name */
  1301. return tcon;
  1302. }
  1303. }
  1304. }
  1305. }
  1306. }
  1307. }
  1308. read_unlock(&GlobalSMBSeslock);
  1309. return NULL;
  1310. }
  1311. int
  1312. connect_to_dfs_path(int xid, struct cifsSesInfo *pSesInfo,
  1313. const char *old_path, const struct nls_table *nls_codepage,
  1314. int remap)
  1315. {
  1316. unsigned char *referrals = NULL;
  1317. unsigned int num_referrals;
  1318. int rc = 0;
  1319. rc = get_dfs_path(xid, pSesInfo, old_path, nls_codepage,
  1320. &num_referrals, &referrals, remap);
  1321. /* BB Add in code to: if valid refrl, if not ip address contact
  1322. the helper that resolves tcp names, mount to it, try to
  1323. tcon to it unmount it if fail */
  1324. kfree(referrals);
  1325. return rc;
  1326. }
  1327. int
  1328. get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path,
  1329. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  1330. unsigned char **preferrals, int remap)
  1331. {
  1332. char *temp_unc;
  1333. int rc = 0;
  1334. *pnum_referrals = 0;
  1335. if (pSesInfo->ipc_tid == 0) {
  1336. temp_unc = kmalloc(2 /* for slashes */ +
  1337. strnlen(pSesInfo->serverName,
  1338. SERVER_NAME_LEN_WITH_NULL * 2)
  1339. + 1 + 4 /* slash IPC$ */ + 2,
  1340. GFP_KERNEL);
  1341. if (temp_unc == NULL)
  1342. return -ENOMEM;
  1343. temp_unc[0] = '\\';
  1344. temp_unc[1] = '\\';
  1345. strcpy(temp_unc + 2, pSesInfo->serverName);
  1346. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  1347. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  1348. cFYI(1,
  1349. ("CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid));
  1350. kfree(temp_unc);
  1351. }
  1352. if (rc == 0)
  1353. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  1354. pnum_referrals, nls_codepage, remap);
  1355. return rc;
  1356. }
  1357. /* See RFC1001 section 14 on representation of Netbios names */
  1358. static void rfc1002mangle(char *target, char *source, unsigned int length)
  1359. {
  1360. unsigned int i, j;
  1361. for (i = 0, j = 0; i < (length); i++) {
  1362. /* mask a nibble at a time and encode */
  1363. target[j] = 'A' + (0x0F & (source[i] >> 4));
  1364. target[j+1] = 'A' + (0x0F & source[i]);
  1365. j += 2;
  1366. }
  1367. }
  1368. static int
  1369. ipv4_connect(struct sockaddr_in *psin_server, struct socket **csocket,
  1370. char *netbios_name, char *target_name)
  1371. {
  1372. int rc = 0;
  1373. int connected = 0;
  1374. __be16 orig_port = 0;
  1375. if (*csocket == NULL) {
  1376. rc = sock_create_kern(PF_INET, SOCK_STREAM,
  1377. IPPROTO_TCP, csocket);
  1378. if (rc < 0) {
  1379. cERROR(1, ("Error %d creating socket", rc));
  1380. *csocket = NULL;
  1381. return rc;
  1382. } else {
  1383. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1384. cFYI(1, ("Socket created"));
  1385. (*csocket)->sk->sk_allocation = GFP_NOFS;
  1386. }
  1387. }
  1388. psin_server->sin_family = AF_INET;
  1389. if (psin_server->sin_port) { /* user overrode default port */
  1390. rc = (*csocket)->ops->connect(*csocket,
  1391. (struct sockaddr *) psin_server,
  1392. sizeof(struct sockaddr_in), 0);
  1393. if (rc >= 0)
  1394. connected = 1;
  1395. }
  1396. if (!connected) {
  1397. /* save original port so we can retry user specified port
  1398. later if fall back ports fail this time */
  1399. orig_port = psin_server->sin_port;
  1400. /* do not retry on the same port we just failed on */
  1401. if (psin_server->sin_port != htons(CIFS_PORT)) {
  1402. psin_server->sin_port = htons(CIFS_PORT);
  1403. rc = (*csocket)->ops->connect(*csocket,
  1404. (struct sockaddr *) psin_server,
  1405. sizeof(struct sockaddr_in), 0);
  1406. if (rc >= 0)
  1407. connected = 1;
  1408. }
  1409. }
  1410. if (!connected) {
  1411. psin_server->sin_port = htons(RFC1001_PORT);
  1412. rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
  1413. psin_server,
  1414. sizeof(struct sockaddr_in), 0);
  1415. if (rc >= 0)
  1416. connected = 1;
  1417. }
  1418. /* give up here - unless we want to retry on different
  1419. protocol families some day */
  1420. if (!connected) {
  1421. if (orig_port)
  1422. psin_server->sin_port = orig_port;
  1423. cFYI(1, ("Error %d connecting to server via ipv4", rc));
  1424. sock_release(*csocket);
  1425. *csocket = NULL;
  1426. return rc;
  1427. }
  1428. /* Eventually check for other socket options to change from
  1429. the default. sock_setsockopt not used because it expects
  1430. user space buffer */
  1431. cFYI(1, ("sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  1432. (*csocket)->sk->sk_sndbuf,
  1433. (*csocket)->sk->sk_rcvbuf, (*csocket)->sk->sk_rcvtimeo));
  1434. (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
  1435. /* make the bufsizes depend on wsize/rsize and max requests */
  1436. if ((*csocket)->sk->sk_sndbuf < (200 * 1024))
  1437. (*csocket)->sk->sk_sndbuf = 200 * 1024;
  1438. if ((*csocket)->sk->sk_rcvbuf < (140 * 1024))
  1439. (*csocket)->sk->sk_rcvbuf = 140 * 1024;
  1440. /* send RFC1001 sessinit */
  1441. if (psin_server->sin_port == htons(RFC1001_PORT)) {
  1442. /* some servers require RFC1001 sessinit before sending
  1443. negprot - BB check reconnection in case where second
  1444. sessinit is sent but no second negprot */
  1445. struct rfc1002_session_packet *ses_init_buf;
  1446. struct smb_hdr *smb_buf;
  1447. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  1448. GFP_KERNEL);
  1449. if (ses_init_buf) {
  1450. ses_init_buf->trailer.session_req.called_len = 32;
  1451. if (target_name && (target_name[0] != 0)) {
  1452. rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
  1453. target_name, 16);
  1454. } else {
  1455. rfc1002mangle(ses_init_buf->trailer.session_req.called_name,
  1456. DEFAULT_CIFS_CALLED_NAME, 16);
  1457. }
  1458. ses_init_buf->trailer.session_req.calling_len = 32;
  1459. /* calling name ends in null (byte 16) from old smb
  1460. convention. */
  1461. if (netbios_name && (netbios_name[0] != 0)) {
  1462. rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
  1463. netbios_name, 16);
  1464. } else {
  1465. rfc1002mangle(ses_init_buf->trailer.session_req.calling_name,
  1466. "LINUX_CIFS_CLNT", 16);
  1467. }
  1468. ses_init_buf->trailer.session_req.scope1 = 0;
  1469. ses_init_buf->trailer.session_req.scope2 = 0;
  1470. smb_buf = (struct smb_hdr *)ses_init_buf;
  1471. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  1472. smb_buf->smb_buf_length = 0x81000044;
  1473. rc = smb_send(*csocket, smb_buf, 0x44,
  1474. (struct sockaddr *)psin_server);
  1475. kfree(ses_init_buf);
  1476. msleep(1); /* RFC1001 layer in at least one server
  1477. requires very short break before negprot
  1478. presumably because not expecting negprot
  1479. to follow so fast. This is a simple
  1480. solution that works without
  1481. complicating the code and causes no
  1482. significant slowing down on mount
  1483. for everyone else */
  1484. }
  1485. /* else the negprot may still work without this
  1486. even though malloc failed */
  1487. }
  1488. return rc;
  1489. }
  1490. static int
  1491. ipv6_connect(struct sockaddr_in6 *psin_server, struct socket **csocket)
  1492. {
  1493. int rc = 0;
  1494. int connected = 0;
  1495. __be16 orig_port = 0;
  1496. if (*csocket == NULL) {
  1497. rc = sock_create_kern(PF_INET6, SOCK_STREAM,
  1498. IPPROTO_TCP, csocket);
  1499. if (rc < 0) {
  1500. cERROR(1, ("Error %d creating ipv6 socket", rc));
  1501. *csocket = NULL;
  1502. return rc;
  1503. } else {
  1504. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1505. cFYI(1, ("ipv6 Socket created"));
  1506. (*csocket)->sk->sk_allocation = GFP_NOFS;
  1507. }
  1508. }
  1509. psin_server->sin6_family = AF_INET6;
  1510. if (psin_server->sin6_port) { /* user overrode default port */
  1511. rc = (*csocket)->ops->connect(*csocket,
  1512. (struct sockaddr *) psin_server,
  1513. sizeof(struct sockaddr_in6), 0);
  1514. if (rc >= 0)
  1515. connected = 1;
  1516. }
  1517. if (!connected) {
  1518. /* save original port so we can retry user specified port
  1519. later if fall back ports fail this time */
  1520. orig_port = psin_server->sin6_port;
  1521. /* do not retry on the same port we just failed on */
  1522. if (psin_server->sin6_port != htons(CIFS_PORT)) {
  1523. psin_server->sin6_port = htons(CIFS_PORT);
  1524. rc = (*csocket)->ops->connect(*csocket,
  1525. (struct sockaddr *) psin_server,
  1526. sizeof(struct sockaddr_in6), 0);
  1527. if (rc >= 0)
  1528. connected = 1;
  1529. }
  1530. }
  1531. if (!connected) {
  1532. psin_server->sin6_port = htons(RFC1001_PORT);
  1533. rc = (*csocket)->ops->connect(*csocket, (struct sockaddr *)
  1534. psin_server, sizeof(struct sockaddr_in6), 0);
  1535. if (rc >= 0)
  1536. connected = 1;
  1537. }
  1538. /* give up here - unless we want to retry on different
  1539. protocol families some day */
  1540. if (!connected) {
  1541. if (orig_port)
  1542. psin_server->sin6_port = orig_port;
  1543. cFYI(1, ("Error %d connecting to server via ipv6", rc));
  1544. sock_release(*csocket);
  1545. *csocket = NULL;
  1546. return rc;
  1547. }
  1548. /* Eventually check for other socket options to change from
  1549. the default. sock_setsockopt not used because it expects
  1550. user space buffer */
  1551. (*csocket)->sk->sk_rcvtimeo = 7 * HZ;
  1552. return rc;
  1553. }
  1554. void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
  1555. struct super_block *sb, struct smb_vol *vol_info)
  1556. {
  1557. /* if we are reconnecting then should we check to see if
  1558. * any requested capabilities changed locally e.g. via
  1559. * remount but we can not do much about it here
  1560. * if they have (even if we could detect it by the following)
  1561. * Perhaps we could add a backpointer to array of sb from tcon
  1562. * or if we change to make all sb to same share the same
  1563. * sb as NFS - then we only have one backpointer to sb.
  1564. * What if we wanted to mount the server share twice once with
  1565. * and once without posixacls or posix paths? */
  1566. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  1567. if (vol_info && vol_info->no_linux_ext) {
  1568. tcon->fsUnixInfo.Capability = 0;
  1569. tcon->unix_ext = 0; /* Unix Extensions disabled */
  1570. cFYI(1, ("Linux protocol extensions disabled"));
  1571. return;
  1572. } else if (vol_info)
  1573. tcon->unix_ext = 1; /* Unix Extensions supported */
  1574. if (tcon->unix_ext == 0) {
  1575. cFYI(1, ("Unix extensions disabled so not set on reconnect"));
  1576. return;
  1577. }
  1578. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  1579. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  1580. /* check for reconnect case in which we do not
  1581. want to change the mount behavior if we can avoid it */
  1582. if (vol_info == NULL) {
  1583. /* turn off POSIX ACL and PATHNAMES if not set
  1584. originally at mount time */
  1585. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  1586. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  1587. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0)
  1588. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  1589. }
  1590. cap &= CIFS_UNIX_CAP_MASK;
  1591. if (vol_info && vol_info->no_psx_acl)
  1592. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  1593. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  1594. cFYI(1, ("negotiated posix acl support"));
  1595. if (sb)
  1596. sb->s_flags |= MS_POSIXACL;
  1597. }
  1598. if (vol_info && vol_info->posix_paths == 0)
  1599. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  1600. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  1601. cFYI(1, ("negotiate posix pathnames"));
  1602. if (sb)
  1603. CIFS_SB(sb)->mnt_cifs_flags |=
  1604. CIFS_MOUNT_POSIX_PATHS;
  1605. }
  1606. /* We might be setting the path sep back to a different
  1607. form if we are reconnecting and the server switched its
  1608. posix path capability for this share */
  1609. if (sb && (CIFS_SB(sb)->prepathlen > 0))
  1610. CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
  1611. if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
  1612. if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
  1613. CIFS_SB(sb)->rsize = 127 * 1024;
  1614. #ifdef CONFIG_CIFS_DEBUG2
  1615. cFYI(1, ("larger reads not supported by srv"));
  1616. #endif
  1617. }
  1618. }
  1619. cFYI(1, ("Negotiate caps 0x%x", (int)cap));
  1620. #ifdef CONFIG_CIFS_DEBUG2
  1621. if (cap & CIFS_UNIX_FCNTL_CAP)
  1622. cFYI(1, ("FCNTL cap"));
  1623. if (cap & CIFS_UNIX_EXTATTR_CAP)
  1624. cFYI(1, ("EXTATTR cap"));
  1625. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  1626. cFYI(1, ("POSIX path cap"));
  1627. if (cap & CIFS_UNIX_XATTR_CAP)
  1628. cFYI(1, ("XATTR cap"));
  1629. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  1630. cFYI(1, ("POSIX ACL cap"));
  1631. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  1632. cFYI(1, ("very large read cap"));
  1633. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  1634. cFYI(1, ("very large write cap"));
  1635. #endif /* CIFS_DEBUG2 */
  1636. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  1637. if (vol_info == NULL) {
  1638. cFYI(1, ("resetting capabilities failed"));
  1639. } else
  1640. cERROR(1, ("Negotiating Unix capabilities "
  1641. "with the server failed. Consider "
  1642. "mounting with the Unix Extensions\n"
  1643. "disabled, if problems are found, "
  1644. "by specifying the nounix mount "
  1645. "option."));
  1646. }
  1647. }
  1648. }
  1649. int
  1650. cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
  1651. char *mount_data, const char *devname)
  1652. {
  1653. int rc = 0;
  1654. int xid;
  1655. int address_type = AF_INET;
  1656. struct socket *csocket = NULL;
  1657. struct sockaddr_in sin_server;
  1658. struct sockaddr_in6 sin_server6;
  1659. struct smb_vol volume_info;
  1660. struct cifsSesInfo *pSesInfo = NULL;
  1661. struct cifsSesInfo *existingCifsSes = NULL;
  1662. struct cifsTconInfo *tcon = NULL;
  1663. struct TCP_Server_Info *srvTcp = NULL;
  1664. xid = GetXid();
  1665. /* cFYI(1, ("Entering cifs_mount. Xid: %d with: %s", xid, mount_data)); */
  1666. memset(&volume_info, 0, sizeof(struct smb_vol));
  1667. if (cifs_parse_mount_options(mount_data, devname, &volume_info)) {
  1668. rc = -EINVAL;
  1669. goto out;
  1670. }
  1671. if (volume_info.nullauth) {
  1672. cFYI(1, ("null user"));
  1673. volume_info.username = "";
  1674. } else if (volume_info.username) {
  1675. /* BB fixme parse for domain name here */
  1676. cFYI(1, ("Username: %s", volume_info.username));
  1677. } else {
  1678. cifserror("No username specified");
  1679. /* In userspace mount helper we can get user name from alternate
  1680. locations such as env variables and files on disk */
  1681. rc = -EINVAL;
  1682. goto out;
  1683. }
  1684. if (volume_info.UNCip && volume_info.UNC) {
  1685. rc = cifs_inet_pton(AF_INET, volume_info.UNCip,
  1686. &sin_server.sin_addr.s_addr);
  1687. if (rc <= 0) {
  1688. /* not ipv4 address, try ipv6 */
  1689. rc = cifs_inet_pton(AF_INET6, volume_info.UNCip,
  1690. &sin_server6.sin6_addr.in6_u);
  1691. if (rc > 0)
  1692. address_type = AF_INET6;
  1693. } else {
  1694. address_type = AF_INET;
  1695. }
  1696. if (rc <= 0) {
  1697. /* we failed translating address */
  1698. rc = -EINVAL;
  1699. goto out;
  1700. }
  1701. cFYI(1, ("UNC: %s ip: %s", volume_info.UNC, volume_info.UNCip));
  1702. /* success */
  1703. rc = 0;
  1704. } else if (volume_info.UNCip) {
  1705. /* BB using ip addr as server name to connect to the
  1706. DFS root below */
  1707. cERROR(1, ("Connecting to DFS root not implemented yet"));
  1708. rc = -EINVAL;
  1709. goto out;
  1710. } else /* which servers DFS root would we conect to */ {
  1711. cERROR(1,
  1712. ("CIFS mount error: No UNC path (e.g. -o "
  1713. "unc=//192.168.1.100/public) specified"));
  1714. rc = -EINVAL;
  1715. goto out;
  1716. }
  1717. /* this is needed for ASCII cp to Unicode converts */
  1718. if (volume_info.iocharset == NULL) {
  1719. cifs_sb->local_nls = load_nls_default();
  1720. /* load_nls_default can not return null */
  1721. } else {
  1722. cifs_sb->local_nls = load_nls(volume_info.iocharset);
  1723. if (cifs_sb->local_nls == NULL) {
  1724. cERROR(1, ("CIFS mount error: iocharset %s not found",
  1725. volume_info.iocharset));
  1726. rc = -ELIBACC;
  1727. goto out;
  1728. }
  1729. }
  1730. if (address_type == AF_INET)
  1731. existingCifsSes = cifs_find_tcp_session(&sin_server.sin_addr,
  1732. NULL /* no ipv6 addr */,
  1733. volume_info.username, &srvTcp);
  1734. else if (address_type == AF_INET6) {
  1735. cFYI(1, ("looking for ipv6 address"));
  1736. existingCifsSes = cifs_find_tcp_session(NULL /* no ipv4 addr */,
  1737. &sin_server6.sin6_addr,
  1738. volume_info.username, &srvTcp);
  1739. } else {
  1740. rc = -EINVAL;
  1741. goto out;
  1742. }
  1743. if (srvTcp) {
  1744. cFYI(1, ("Existing tcp session with server found"));
  1745. } else { /* create socket */
  1746. if (volume_info.port)
  1747. sin_server.sin_port = htons(volume_info.port);
  1748. else
  1749. sin_server.sin_port = 0;
  1750. if (address_type == AF_INET6) {
  1751. cFYI(1, ("attempting ipv6 connect"));
  1752. /* BB should we allow ipv6 on port 139? */
  1753. /* other OS never observed in Wild doing 139 with v6 */
  1754. rc = ipv6_connect(&sin_server6, &csocket);
  1755. } else
  1756. rc = ipv4_connect(&sin_server, &csocket,
  1757. volume_info.source_rfc1001_name,
  1758. volume_info.target_rfc1001_name);
  1759. if (rc < 0) {
  1760. cERROR(1, ("Error connecting to IPv4 socket. "
  1761. "Aborting operation"));
  1762. if (csocket != NULL)
  1763. sock_release(csocket);
  1764. goto out;
  1765. }
  1766. srvTcp = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1767. if (!srvTcp) {
  1768. rc = -ENOMEM;
  1769. sock_release(csocket);
  1770. goto out;
  1771. } else {
  1772. memcpy(&srvTcp->addr.sockAddr, &sin_server,
  1773. sizeof(struct sockaddr_in));
  1774. atomic_set(&srvTcp->inFlight, 0);
  1775. /* BB Add code for ipv6 case too */
  1776. srvTcp->ssocket = csocket;
  1777. srvTcp->protocolType = IPV4;
  1778. srvTcp->hostname = extract_hostname(volume_info.UNC);
  1779. if (IS_ERR(srvTcp->hostname)) {
  1780. rc = PTR_ERR(srvTcp->hostname);
  1781. sock_release(csocket);
  1782. goto out;
  1783. }
  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(srvTcp->hostname);
  1799. goto out;
  1800. }
  1801. wait_for_completion(&cifsd_complete);
  1802. rc = 0;
  1803. memcpy(srvTcp->workstation_RFC1001_name,
  1804. volume_info.source_rfc1001_name, 16);
  1805. memcpy(srvTcp->server_RFC1001_name,
  1806. volume_info.target_rfc1001_name, 16);
  1807. srvTcp->sequence_number = 0;
  1808. }
  1809. }
  1810. if (existingCifsSes) {
  1811. pSesInfo = existingCifsSes;
  1812. cFYI(1, ("Existing smb sess found"));
  1813. } else if (!rc) {
  1814. cFYI(1, ("Existing smb sess not found"));
  1815. pSesInfo = sesInfoAlloc();
  1816. if (pSesInfo == NULL)
  1817. rc = -ENOMEM;
  1818. else {
  1819. pSesInfo->server = srvTcp;
  1820. sprintf(pSesInfo->serverName, "%u.%u.%u.%u",
  1821. NIPQUAD(sin_server.sin_addr.s_addr));
  1822. }
  1823. if (!rc) {
  1824. /* volume_info.password freed at unmount */
  1825. if (volume_info.password) {
  1826. pSesInfo->password = volume_info.password;
  1827. /* set to NULL to prevent freeing on exit */
  1828. volume_info.password = NULL;
  1829. }
  1830. if (volume_info.username)
  1831. strncpy(pSesInfo->userName,
  1832. volume_info.username,
  1833. MAX_USERNAME_SIZE);
  1834. if (volume_info.domainname) {
  1835. int len = strlen(volume_info.domainname);
  1836. pSesInfo->domainName =
  1837. kmalloc(len + 1, GFP_KERNEL);
  1838. if (pSesInfo->domainName)
  1839. strcpy(pSesInfo->domainName,
  1840. volume_info.domainname);
  1841. }
  1842. pSesInfo->linux_uid = volume_info.linux_uid;
  1843. pSesInfo->overrideSecFlg = volume_info.secFlg;
  1844. down(&pSesInfo->sesSem);
  1845. /* BB FIXME need to pass vol->secFlgs BB */
  1846. rc = cifs_setup_session(xid, pSesInfo,
  1847. cifs_sb->local_nls);
  1848. up(&pSesInfo->sesSem);
  1849. if (!rc)
  1850. atomic_inc(&srvTcp->socketUseCount);
  1851. }
  1852. }
  1853. /* search for existing tcon to this server share */
  1854. if (!rc) {
  1855. if (volume_info.rsize > CIFSMaxBufSize) {
  1856. cERROR(1, ("rsize %d too large, using MaxBufSize",
  1857. volume_info.rsize));
  1858. cifs_sb->rsize = CIFSMaxBufSize;
  1859. } else if ((volume_info.rsize) &&
  1860. (volume_info.rsize <= CIFSMaxBufSize))
  1861. cifs_sb->rsize = volume_info.rsize;
  1862. else /* default */
  1863. cifs_sb->rsize = CIFSMaxBufSize;
  1864. if (volume_info.wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
  1865. cERROR(1, ("wsize %d too large, using 4096 instead",
  1866. volume_info.wsize));
  1867. cifs_sb->wsize = 4096;
  1868. } else if (volume_info.wsize)
  1869. cifs_sb->wsize = volume_info.wsize;
  1870. else
  1871. cifs_sb->wsize =
  1872. min_t(const int, PAGEVEC_SIZE * PAGE_CACHE_SIZE,
  1873. 127*1024);
  1874. /* old default of CIFSMaxBufSize was too small now
  1875. that SMB Write2 can send multiple pages in kvec.
  1876. RFC1001 does not describe what happens when frame
  1877. bigger than 128K is sent so use that as max in
  1878. conjunction with 52K kvec constraint on arch with 4K
  1879. page size */
  1880. if (cifs_sb->rsize < 2048) {
  1881. cifs_sb->rsize = 2048;
  1882. /* Windows ME may prefer this */
  1883. cFYI(1, ("readsize set to minimum: 2048"));
  1884. }
  1885. /* calculate prepath */
  1886. cifs_sb->prepath = volume_info.prepath;
  1887. if (cifs_sb->prepath) {
  1888. cifs_sb->prepathlen = strlen(cifs_sb->prepath);
  1889. cifs_sb->prepath[0] = CIFS_DIR_SEP(cifs_sb);
  1890. volume_info.prepath = NULL;
  1891. } else
  1892. cifs_sb->prepathlen = 0;
  1893. cifs_sb->mnt_uid = volume_info.linux_uid;
  1894. cifs_sb->mnt_gid = volume_info.linux_gid;
  1895. cifs_sb->mnt_file_mode = volume_info.file_mode;
  1896. cifs_sb->mnt_dir_mode = volume_info.dir_mode;
  1897. cFYI(1, ("file mode: 0x%x dir mode: 0x%x",
  1898. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode));
  1899. if (volume_info.noperm)
  1900. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  1901. if (volume_info.setuids)
  1902. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  1903. if (volume_info.server_ino)
  1904. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  1905. if (volume_info.remap)
  1906. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  1907. if (volume_info.no_xattr)
  1908. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  1909. if (volume_info.sfu_emul)
  1910. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  1911. if (volume_info.nobrl)
  1912. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  1913. if (volume_info.cifs_acl)
  1914. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  1915. if (volume_info.override_uid)
  1916. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  1917. if (volume_info.override_gid)
  1918. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  1919. if (volume_info.direct_io) {
  1920. cFYI(1, ("mounting share using direct i/o"));
  1921. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  1922. }
  1923. tcon =
  1924. find_unc(sin_server.sin_addr.s_addr, volume_info.UNC,
  1925. volume_info.username);
  1926. if (tcon) {
  1927. cFYI(1, ("Found match on UNC path"));
  1928. /* we can have only one retry value for a connection
  1929. to a share so for resources mounted more than once
  1930. to the same server share the last value passed in
  1931. for the retry flag is used */
  1932. tcon->retry = volume_info.retry;
  1933. tcon->nocase = volume_info.nocase;
  1934. } else {
  1935. tcon = tconInfoAlloc();
  1936. if (tcon == NULL)
  1937. rc = -ENOMEM;
  1938. else {
  1939. /* check for null share name ie connecting to
  1940. * dfs root */
  1941. /* BB check if this works for exactly length
  1942. * three strings */
  1943. if ((strchr(volume_info.UNC + 3, '\\') == NULL)
  1944. && (strchr(volume_info.UNC + 3, '/') ==
  1945. NULL)) {
  1946. rc = connect_to_dfs_path(xid, pSesInfo,
  1947. "", cifs_sb->local_nls,
  1948. cifs_sb->mnt_cifs_flags &
  1949. CIFS_MOUNT_MAP_SPECIAL_CHR);
  1950. rc = -ENODEV;
  1951. goto out;
  1952. } else {
  1953. /* BB Do we need to wrap sesSem around
  1954. * this TCon call and Unix SetFS as
  1955. * we do on SessSetup and reconnect? */
  1956. rc = CIFSTCon(xid, pSesInfo,
  1957. volume_info.UNC,
  1958. tcon, cifs_sb->local_nls);
  1959. cFYI(1, ("CIFS Tcon rc = %d", rc));
  1960. }
  1961. if (!rc) {
  1962. atomic_inc(&pSesInfo->inUse);
  1963. tcon->retry = volume_info.retry;
  1964. tcon->nocase = volume_info.nocase;
  1965. }
  1966. }
  1967. }
  1968. }
  1969. if (pSesInfo) {
  1970. if (pSesInfo->capabilities & CAP_LARGE_FILES) {
  1971. sb->s_maxbytes = (u64) 1 << 63;
  1972. } else
  1973. sb->s_maxbytes = (u64) 1 << 31; /* 2 GB */
  1974. }
  1975. /* BB FIXME fix time_gran to be larger for LANMAN sessions */
  1976. sb->s_time_gran = 100;
  1977. /* on error free sesinfo and tcon struct if needed */
  1978. if (rc) {
  1979. /* if session setup failed, use count is zero but
  1980. we still need to free cifsd thread */
  1981. if (atomic_read(&srvTcp->socketUseCount) == 0) {
  1982. spin_lock(&GlobalMid_Lock);
  1983. srvTcp->tcpStatus = CifsExiting;
  1984. spin_unlock(&GlobalMid_Lock);
  1985. if (srvTcp->tsk) {
  1986. struct task_struct *tsk;
  1987. /* If we could verify that kthread_stop would
  1988. always wake up processes blocked in
  1989. tcp in recv_mesg then we could remove the
  1990. send_sig call */
  1991. force_sig(SIGKILL, srvTcp->tsk);
  1992. tsk = srvTcp->tsk;
  1993. if (tsk)
  1994. kthread_stop(tsk);
  1995. }
  1996. }
  1997. /* If find_unc succeeded then rc == 0 so we can not end */
  1998. if (tcon) /* up accidently freeing someone elses tcon struct */
  1999. tconInfoFree(tcon);
  2000. if (existingCifsSes == NULL) {
  2001. if (pSesInfo) {
  2002. if ((pSesInfo->server) &&
  2003. (pSesInfo->status == CifsGood)) {
  2004. int temp_rc;
  2005. temp_rc = CIFSSMBLogoff(xid, pSesInfo);
  2006. /* if the socketUseCount is now zero */
  2007. if ((temp_rc == -ESHUTDOWN) &&
  2008. (pSesInfo->server) &&
  2009. (pSesInfo->server->tsk)) {
  2010. struct task_struct *tsk;
  2011. force_sig(SIGKILL,
  2012. pSesInfo->server->tsk);
  2013. tsk = pSesInfo->server->tsk;
  2014. if (tsk)
  2015. kthread_stop(tsk);
  2016. }
  2017. } else {
  2018. cFYI(1, ("No session or bad tcon"));
  2019. if ((pSesInfo->server) &&
  2020. (pSesInfo->server->tsk)) {
  2021. struct task_struct *tsk;
  2022. force_sig(SIGKILL,
  2023. pSesInfo->server->tsk);
  2024. tsk = pSesInfo->server->tsk;
  2025. if (tsk)
  2026. kthread_stop(tsk);
  2027. }
  2028. }
  2029. sesInfoFree(pSesInfo);
  2030. /* pSesInfo = NULL; */
  2031. }
  2032. }
  2033. } else {
  2034. atomic_inc(&tcon->useCount);
  2035. cifs_sb->tcon = tcon;
  2036. tcon->ses = pSesInfo;
  2037. /* do not care if following two calls succeed - informational */
  2038. if (!tcon->ipc) {
  2039. CIFSSMBQFSDeviceInfo(xid, tcon);
  2040. CIFSSMBQFSAttributeInfo(xid, tcon);
  2041. }
  2042. /* tell server which Unix caps we support */
  2043. if (tcon->ses->capabilities & CAP_UNIX)
  2044. /* reset of caps checks mount to see if unix extensions
  2045. disabled for just this mount */
  2046. reset_cifs_unix_caps(xid, tcon, sb, &volume_info);
  2047. else
  2048. tcon->unix_ext = 0; /* server does not support them */
  2049. if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
  2050. cifs_sb->rsize = 1024 * 127;
  2051. #ifdef CONFIG_CIFS_DEBUG2
  2052. cFYI(1, ("no very large read support, rsize now 127K"));
  2053. #endif
  2054. }
  2055. if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
  2056. cifs_sb->wsize = min(cifs_sb->wsize,
  2057. (tcon->ses->server->maxBuf -
  2058. MAX_CIFS_HDR_SIZE));
  2059. if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
  2060. cifs_sb->rsize = min(cifs_sb->rsize,
  2061. (tcon->ses->server->maxBuf -
  2062. MAX_CIFS_HDR_SIZE));
  2063. }
  2064. /* volume_info.password is freed above when existing session found
  2065. (in which case it is not needed anymore) but when new sesion is created
  2066. the password ptr is put in the new session structure (in which case the
  2067. password will be freed at unmount time) */
  2068. out:
  2069. /* zero out password before freeing */
  2070. if (volume_info.password != NULL) {
  2071. memset(volume_info.password, 0, strlen(volume_info.password));
  2072. kfree(volume_info.password);
  2073. }
  2074. kfree(volume_info.UNC);
  2075. kfree(volume_info.prepath);
  2076. FreeXid(xid);
  2077. return rc;
  2078. }
  2079. static int
  2080. CIFSSessSetup(unsigned int xid, struct cifsSesInfo *ses,
  2081. char session_key[CIFS_SESS_KEY_SIZE],
  2082. const struct nls_table *nls_codepage)
  2083. {
  2084. struct smb_hdr *smb_buffer;
  2085. struct smb_hdr *smb_buffer_response;
  2086. SESSION_SETUP_ANDX *pSMB;
  2087. SESSION_SETUP_ANDX *pSMBr;
  2088. char *bcc_ptr;
  2089. char *user;
  2090. char *domain;
  2091. int rc = 0;
  2092. int remaining_words = 0;
  2093. int bytes_returned = 0;
  2094. int len;
  2095. __u32 capabilities;
  2096. __u16 count;
  2097. cFYI(1, ("In sesssetup"));
  2098. if (ses == NULL)
  2099. return -EINVAL;
  2100. user = ses->userName;
  2101. domain = ses->domainName;
  2102. smb_buffer = cifs_buf_get();
  2103. if (smb_buffer == NULL) {
  2104. return -ENOMEM;
  2105. }
  2106. smb_buffer_response = smb_buffer;
  2107. pSMBr = pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
  2108. /* send SMBsessionSetup here */
  2109. header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
  2110. NULL /* no tCon exists yet */ , 13 /* wct */ );
  2111. smb_buffer->Mid = GetNextMid(ses->server);
  2112. pSMB->req_no_secext.AndXCommand = 0xFF;
  2113. pSMB->req_no_secext.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
  2114. pSMB->req_no_secext.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  2115. if (ses->server->secMode &
  2116. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2117. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2118. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  2119. CAP_LARGE_WRITE_X | CAP_LARGE_READ_X;
  2120. if (ses->capabilities & CAP_UNICODE) {
  2121. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2122. capabilities |= CAP_UNICODE;
  2123. }
  2124. if (ses->capabilities & CAP_STATUS32) {
  2125. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2126. capabilities |= CAP_STATUS32;
  2127. }
  2128. if (ses->capabilities & CAP_DFS) {
  2129. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2130. capabilities |= CAP_DFS;
  2131. }
  2132. pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities);
  2133. pSMB->req_no_secext.CaseInsensitivePasswordLength =
  2134. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2135. pSMB->req_no_secext.CaseSensitivePasswordLength =
  2136. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2137. bcc_ptr = pByteArea(smb_buffer);
  2138. memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
  2139. bcc_ptr += CIFS_SESS_KEY_SIZE;
  2140. memcpy(bcc_ptr, (char *) session_key, CIFS_SESS_KEY_SIZE);
  2141. bcc_ptr += CIFS_SESS_KEY_SIZE;
  2142. if (ses->capabilities & CAP_UNICODE) {
  2143. if ((long) bcc_ptr % 2) { /* must be word aligned for Unicode */
  2144. *bcc_ptr = 0;
  2145. bcc_ptr++;
  2146. }
  2147. if (user == NULL)
  2148. bytes_returned = 0; /* skip null user */
  2149. else
  2150. bytes_returned =
  2151. cifs_strtoUCS((__le16 *) bcc_ptr, user, 100,
  2152. nls_codepage);
  2153. /* convert number of 16 bit words to bytes */
  2154. bcc_ptr += 2 * bytes_returned;
  2155. bcc_ptr += 2; /* trailing null */
  2156. if (domain == NULL)
  2157. bytes_returned =
  2158. cifs_strtoUCS((__le16 *) bcc_ptr,
  2159. "CIFS_LINUX_DOM", 32, nls_codepage);
  2160. else
  2161. bytes_returned =
  2162. cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
  2163. nls_codepage);
  2164. bcc_ptr += 2 * bytes_returned;
  2165. bcc_ptr += 2;
  2166. bytes_returned =
  2167. cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
  2168. 32, nls_codepage);
  2169. bcc_ptr += 2 * bytes_returned;
  2170. bytes_returned =
  2171. cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release,
  2172. 32, nls_codepage);
  2173. bcc_ptr += 2 * bytes_returned;
  2174. bcc_ptr += 2;
  2175. bytes_returned =
  2176. cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  2177. 64, nls_codepage);
  2178. bcc_ptr += 2 * bytes_returned;
  2179. bcc_ptr += 2;
  2180. } else {
  2181. if (user != NULL) {
  2182. strncpy(bcc_ptr, user, 200);
  2183. bcc_ptr += strnlen(user, 200);
  2184. }
  2185. *bcc_ptr = 0;
  2186. bcc_ptr++;
  2187. if (domain == NULL) {
  2188. strcpy(bcc_ptr, "CIFS_LINUX_DOM");
  2189. bcc_ptr += strlen("CIFS_LINUX_DOM") + 1;
  2190. } else {
  2191. strncpy(bcc_ptr, domain, 64);
  2192. bcc_ptr += strnlen(domain, 64);
  2193. *bcc_ptr = 0;
  2194. bcc_ptr++;
  2195. }
  2196. strcpy(bcc_ptr, "Linux version ");
  2197. bcc_ptr += strlen("Linux version ");
  2198. strcpy(bcc_ptr, utsname()->release);
  2199. bcc_ptr += strlen(utsname()->release) + 1;
  2200. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  2201. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  2202. }
  2203. count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
  2204. smb_buffer->smb_buf_length += count;
  2205. pSMB->req_no_secext.ByteCount = cpu_to_le16(count);
  2206. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
  2207. &bytes_returned, CIFS_LONG_OP);
  2208. if (rc) {
  2209. /* rc = map_smb_to_linux_error(smb_buffer_response); now done in SendReceive */
  2210. } else if ((smb_buffer_response->WordCount == 3)
  2211. || (smb_buffer_response->WordCount == 4)) {
  2212. __u16 action = le16_to_cpu(pSMBr->resp.Action);
  2213. __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
  2214. if (action & GUEST_LOGIN)
  2215. cFYI(1, (" Guest login")); /* BB mark SesInfo struct? */
  2216. ses->Suid = smb_buffer_response->Uid; /* UID left in wire format
  2217. (little endian) */
  2218. cFYI(1, ("UID = %d ", ses->Suid));
  2219. /* response can have either 3 or 4 word count - Samba sends 3 */
  2220. bcc_ptr = pByteArea(smb_buffer_response);
  2221. if ((pSMBr->resp.hdr.WordCount == 3)
  2222. || ((pSMBr->resp.hdr.WordCount == 4)
  2223. && (blob_len < pSMBr->resp.ByteCount))) {
  2224. if (pSMBr->resp.hdr.WordCount == 4)
  2225. bcc_ptr += blob_len;
  2226. if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
  2227. if ((long) (bcc_ptr) % 2) {
  2228. remaining_words =
  2229. (BCC(smb_buffer_response) - 1) / 2;
  2230. /* Unicode strings must be word
  2231. aligned */
  2232. bcc_ptr++;
  2233. } else {
  2234. remaining_words =
  2235. BCC(smb_buffer_response) / 2;
  2236. }
  2237. len =
  2238. UniStrnlen((wchar_t *) bcc_ptr,
  2239. remaining_words - 1);
  2240. /* We look for obvious messed up bcc or strings in response so we do not go off
  2241. the end since (at least) WIN2K and Windows XP have a major bug in not null
  2242. terminating last Unicode string in response */
  2243. if (ses->serverOS)
  2244. kfree(ses->serverOS);
  2245. ses->serverOS = kzalloc(2 * (len + 1),
  2246. GFP_KERNEL);
  2247. if (ses->serverOS == NULL)
  2248. goto sesssetup_nomem;
  2249. cifs_strfromUCS_le(ses->serverOS,
  2250. (__le16 *)bcc_ptr,
  2251. len, nls_codepage);
  2252. bcc_ptr += 2 * (len + 1);
  2253. remaining_words -= len + 1;
  2254. ses->serverOS[2 * len] = 0;
  2255. ses->serverOS[1 + (2 * len)] = 0;
  2256. if (remaining_words > 0) {
  2257. len = UniStrnlen((wchar_t *)bcc_ptr,
  2258. remaining_words-1);
  2259. kfree(ses->serverNOS);
  2260. ses->serverNOS = kzalloc(2 * (len + 1),
  2261. GFP_KERNEL);
  2262. if (ses->serverNOS == NULL)
  2263. goto sesssetup_nomem;
  2264. cifs_strfromUCS_le(ses->serverNOS,
  2265. (__le16 *)bcc_ptr,
  2266. len, nls_codepage);
  2267. bcc_ptr += 2 * (len + 1);
  2268. ses->serverNOS[2 * len] = 0;
  2269. ses->serverNOS[1 + (2 * len)] = 0;
  2270. if (strncmp(ses->serverNOS,
  2271. "NT LAN Manager 4", 16) == 0) {
  2272. cFYI(1, ("NT4 server"));
  2273. ses->flags |= CIFS_SES_NT4;
  2274. }
  2275. remaining_words -= len + 1;
  2276. if (remaining_words > 0) {
  2277. len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
  2278. /* last string is not always null terminated
  2279. (for e.g. for Windows XP & 2000) */
  2280. if (ses->serverDomain)
  2281. kfree(ses->serverDomain);
  2282. ses->serverDomain =
  2283. kzalloc(2*(len+1),
  2284. GFP_KERNEL);
  2285. if (ses->serverDomain == NULL)
  2286. goto sesssetup_nomem;
  2287. cifs_strfromUCS_le(ses->serverDomain,
  2288. (__le16 *)bcc_ptr,
  2289. len, nls_codepage);
  2290. bcc_ptr += 2 * (len + 1);
  2291. ses->serverDomain[2*len] = 0;
  2292. ses->serverDomain[1+(2*len)] = 0;
  2293. } else { /* else no more room so create
  2294. dummy domain string */
  2295. if (ses->serverDomain)
  2296. kfree(ses->serverDomain);
  2297. ses->serverDomain =
  2298. kzalloc(2, GFP_KERNEL);
  2299. }
  2300. } else { /* no room so create dummy domain
  2301. and NOS string */
  2302. /* if these kcallocs fail not much we
  2303. can do, but better to not fail the
  2304. sesssetup itself */
  2305. kfree(ses->serverDomain);
  2306. ses->serverDomain =
  2307. kzalloc(2, GFP_KERNEL);
  2308. kfree(ses->serverNOS);
  2309. ses->serverNOS =
  2310. kzalloc(2, GFP_KERNEL);
  2311. }
  2312. } else { /* ASCII */
  2313. len = strnlen(bcc_ptr, 1024);
  2314. if (((long) bcc_ptr + len) - (long)
  2315. pByteArea(smb_buffer_response)
  2316. <= BCC(smb_buffer_response)) {
  2317. kfree(ses->serverOS);
  2318. ses->serverOS = kzalloc(len + 1,
  2319. GFP_KERNEL);
  2320. if (ses->serverOS == NULL)
  2321. goto sesssetup_nomem;
  2322. strncpy(ses->serverOS, bcc_ptr, len);
  2323. bcc_ptr += len;
  2324. /* null terminate the string */
  2325. bcc_ptr[0] = 0;
  2326. bcc_ptr++;
  2327. len = strnlen(bcc_ptr, 1024);
  2328. kfree(ses->serverNOS);
  2329. ses->serverNOS = kzalloc(len + 1,
  2330. GFP_KERNEL);
  2331. if (ses->serverNOS == NULL)
  2332. goto sesssetup_nomem;
  2333. strncpy(ses->serverNOS, bcc_ptr, len);
  2334. bcc_ptr += len;
  2335. bcc_ptr[0] = 0;
  2336. bcc_ptr++;
  2337. len = strnlen(bcc_ptr, 1024);
  2338. if (ses->serverDomain)
  2339. kfree(ses->serverDomain);
  2340. ses->serverDomain = kzalloc(len + 1,
  2341. GFP_KERNEL);
  2342. if (ses->serverDomain == NULL)
  2343. goto sesssetup_nomem;
  2344. strncpy(ses->serverDomain, bcc_ptr,
  2345. len);
  2346. bcc_ptr += len;
  2347. bcc_ptr[0] = 0;
  2348. bcc_ptr++;
  2349. } else
  2350. cFYI(1,
  2351. ("Variable field of length %d "
  2352. "extends beyond end of smb ",
  2353. len));
  2354. }
  2355. } else {
  2356. cERROR(1,
  2357. (" Security Blob Length extends beyond "
  2358. "end of SMB"));
  2359. }
  2360. } else {
  2361. cERROR(1,
  2362. (" Invalid Word count %d: ",
  2363. smb_buffer_response->WordCount));
  2364. rc = -EIO;
  2365. }
  2366. sesssetup_nomem: /* do not return an error on nomem for the info strings,
  2367. since that could make reconnection harder, and
  2368. reconnection might be needed to free memory */
  2369. cifs_buf_release(smb_buffer);
  2370. return rc;
  2371. }
  2372. static int
  2373. CIFSNTLMSSPNegotiateSessSetup(unsigned int xid,
  2374. struct cifsSesInfo *ses, int *pNTLMv2_flag,
  2375. const struct nls_table *nls_codepage)
  2376. {
  2377. struct smb_hdr *smb_buffer;
  2378. struct smb_hdr *smb_buffer_response;
  2379. SESSION_SETUP_ANDX *pSMB;
  2380. SESSION_SETUP_ANDX *pSMBr;
  2381. char *bcc_ptr;
  2382. char *domain;
  2383. int rc = 0;
  2384. int remaining_words = 0;
  2385. int bytes_returned = 0;
  2386. int len;
  2387. int SecurityBlobLength = sizeof(NEGOTIATE_MESSAGE);
  2388. PNEGOTIATE_MESSAGE SecurityBlob;
  2389. PCHALLENGE_MESSAGE SecurityBlob2;
  2390. __u32 negotiate_flags, capabilities;
  2391. __u16 count;
  2392. cFYI(1, ("In NTLMSSP sesssetup (negotiate)"));
  2393. if (ses == NULL)
  2394. return -EINVAL;
  2395. domain = ses->domainName;
  2396. *pNTLMv2_flag = FALSE;
  2397. smb_buffer = cifs_buf_get();
  2398. if (smb_buffer == NULL) {
  2399. return -ENOMEM;
  2400. }
  2401. smb_buffer_response = smb_buffer;
  2402. pSMB = (SESSION_SETUP_ANDX *) smb_buffer;
  2403. pSMBr = (SESSION_SETUP_ANDX *) smb_buffer_response;
  2404. /* send SMBsessionSetup here */
  2405. header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
  2406. NULL /* no tCon exists yet */ , 12 /* wct */ );
  2407. smb_buffer->Mid = GetNextMid(ses->server);
  2408. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  2409. pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
  2410. pSMB->req.AndXCommand = 0xFF;
  2411. pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
  2412. pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  2413. if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2414. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2415. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  2416. CAP_EXTENDED_SECURITY;
  2417. if (ses->capabilities & CAP_UNICODE) {
  2418. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2419. capabilities |= CAP_UNICODE;
  2420. }
  2421. if (ses->capabilities & CAP_STATUS32) {
  2422. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2423. capabilities |= CAP_STATUS32;
  2424. }
  2425. if (ses->capabilities & CAP_DFS) {
  2426. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2427. capabilities |= CAP_DFS;
  2428. }
  2429. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  2430. bcc_ptr = (char *) &pSMB->req.SecurityBlob;
  2431. SecurityBlob = (PNEGOTIATE_MESSAGE) bcc_ptr;
  2432. strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
  2433. SecurityBlob->MessageType = NtLmNegotiate;
  2434. negotiate_flags =
  2435. NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_OEM |
  2436. NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_NTLM |
  2437. NTLMSSP_NEGOTIATE_56 |
  2438. /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN | */ NTLMSSP_NEGOTIATE_128;
  2439. if (sign_CIFS_PDUs)
  2440. negotiate_flags |= NTLMSSP_NEGOTIATE_SIGN;
  2441. /* if (ntlmv2_support)
  2442. negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;*/
  2443. /* setup pointers to domain name and workstation name */
  2444. bcc_ptr += SecurityBlobLength;
  2445. SecurityBlob->WorkstationName.Buffer = 0;
  2446. SecurityBlob->WorkstationName.Length = 0;
  2447. SecurityBlob->WorkstationName.MaximumLength = 0;
  2448. /* Domain not sent on first Sesssetup in NTLMSSP, instead it is sent
  2449. along with username on auth request (ie the response to challenge) */
  2450. SecurityBlob->DomainName.Buffer = 0;
  2451. SecurityBlob->DomainName.Length = 0;
  2452. SecurityBlob->DomainName.MaximumLength = 0;
  2453. if (ses->capabilities & CAP_UNICODE) {
  2454. if ((long) bcc_ptr % 2) {
  2455. *bcc_ptr = 0;
  2456. bcc_ptr++;
  2457. }
  2458. bytes_returned =
  2459. cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
  2460. 32, nls_codepage);
  2461. bcc_ptr += 2 * bytes_returned;
  2462. bytes_returned =
  2463. cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
  2464. nls_codepage);
  2465. bcc_ptr += 2 * bytes_returned;
  2466. bcc_ptr += 2; /* null terminate Linux version */
  2467. bytes_returned =
  2468. cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  2469. 64, nls_codepage);
  2470. bcc_ptr += 2 * bytes_returned;
  2471. *(bcc_ptr + 1) = 0;
  2472. *(bcc_ptr + 2) = 0;
  2473. bcc_ptr += 2; /* null terminate network opsys string */
  2474. *(bcc_ptr + 1) = 0;
  2475. *(bcc_ptr + 2) = 0;
  2476. bcc_ptr += 2; /* null domain */
  2477. } else { /* ASCII */
  2478. strcpy(bcc_ptr, "Linux version ");
  2479. bcc_ptr += strlen("Linux version ");
  2480. strcpy(bcc_ptr, utsname()->release);
  2481. bcc_ptr += strlen(utsname()->release) + 1;
  2482. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  2483. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  2484. bcc_ptr++; /* empty domain field */
  2485. *bcc_ptr = 0;
  2486. }
  2487. SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
  2488. pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
  2489. count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
  2490. smb_buffer->smb_buf_length += count;
  2491. pSMB->req.ByteCount = cpu_to_le16(count);
  2492. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
  2493. &bytes_returned, CIFS_LONG_OP);
  2494. if (smb_buffer_response->Status.CifsError ==
  2495. cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED))
  2496. rc = 0;
  2497. if (rc) {
  2498. /* rc = map_smb_to_linux_error(smb_buffer_response); *//* done in SendReceive now */
  2499. } else if ((smb_buffer_response->WordCount == 3)
  2500. || (smb_buffer_response->WordCount == 4)) {
  2501. __u16 action = le16_to_cpu(pSMBr->resp.Action);
  2502. __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
  2503. if (action & GUEST_LOGIN)
  2504. cFYI(1, (" Guest login"));
  2505. /* Do we want to set anything in SesInfo struct when guest login? */
  2506. bcc_ptr = pByteArea(smb_buffer_response);
  2507. /* response can have either 3 or 4 word count - Samba sends 3 */
  2508. SecurityBlob2 = (PCHALLENGE_MESSAGE) bcc_ptr;
  2509. if (SecurityBlob2->MessageType != NtLmChallenge) {
  2510. cFYI(1,
  2511. ("Unexpected NTLMSSP message type received %d",
  2512. SecurityBlob2->MessageType));
  2513. } else if (ses) {
  2514. ses->Suid = smb_buffer_response->Uid; /* UID left in le format */
  2515. cFYI(1, ("UID = %d", ses->Suid));
  2516. if ((pSMBr->resp.hdr.WordCount == 3)
  2517. || ((pSMBr->resp.hdr.WordCount == 4)
  2518. && (blob_len <
  2519. pSMBr->resp.ByteCount))) {
  2520. if (pSMBr->resp.hdr.WordCount == 4) {
  2521. bcc_ptr += blob_len;
  2522. cFYI(1, ("Security Blob Length %d",
  2523. blob_len));
  2524. }
  2525. cFYI(1, ("NTLMSSP Challenge rcvd"));
  2526. memcpy(ses->server->cryptKey,
  2527. SecurityBlob2->Challenge,
  2528. CIFS_CRYPTO_KEY_SIZE);
  2529. if (SecurityBlob2->NegotiateFlags &
  2530. cpu_to_le32(NTLMSSP_NEGOTIATE_NTLMV2))
  2531. *pNTLMv2_flag = TRUE;
  2532. if ((SecurityBlob2->NegotiateFlags &
  2533. cpu_to_le32(NTLMSSP_NEGOTIATE_ALWAYS_SIGN))
  2534. || (sign_CIFS_PDUs > 1))
  2535. ses->server->secMode |=
  2536. SECMODE_SIGN_REQUIRED;
  2537. if ((SecurityBlob2->NegotiateFlags &
  2538. cpu_to_le32(NTLMSSP_NEGOTIATE_SIGN)) && (sign_CIFS_PDUs))
  2539. ses->server->secMode |=
  2540. SECMODE_SIGN_ENABLED;
  2541. if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
  2542. if ((long) (bcc_ptr) % 2) {
  2543. remaining_words =
  2544. (BCC(smb_buffer_response)
  2545. - 1) / 2;
  2546. /* Must word align unicode strings */
  2547. bcc_ptr++;
  2548. } else {
  2549. remaining_words =
  2550. BCC
  2551. (smb_buffer_response) / 2;
  2552. }
  2553. len =
  2554. UniStrnlen((wchar_t *) bcc_ptr,
  2555. remaining_words - 1);
  2556. /* We look for obvious messed up bcc or strings in response so we do not go off
  2557. the end since (at least) WIN2K and Windows XP have a major bug in not null
  2558. terminating last Unicode string in response */
  2559. if (ses->serverOS)
  2560. kfree(ses->serverOS);
  2561. ses->serverOS =
  2562. kzalloc(2 * (len + 1), GFP_KERNEL);
  2563. cifs_strfromUCS_le(ses->serverOS,
  2564. (__le16 *)
  2565. bcc_ptr, len,
  2566. nls_codepage);
  2567. bcc_ptr += 2 * (len + 1);
  2568. remaining_words -= len + 1;
  2569. ses->serverOS[2 * len] = 0;
  2570. ses->serverOS[1 + (2 * len)] = 0;
  2571. if (remaining_words > 0) {
  2572. len = UniStrnlen((wchar_t *)
  2573. bcc_ptr,
  2574. remaining_words
  2575. - 1);
  2576. kfree(ses->serverNOS);
  2577. ses->serverNOS =
  2578. kzalloc(2 * (len + 1),
  2579. GFP_KERNEL);
  2580. cifs_strfromUCS_le(ses->
  2581. serverNOS,
  2582. (__le16 *)
  2583. bcc_ptr,
  2584. len,
  2585. nls_codepage);
  2586. bcc_ptr += 2 * (len + 1);
  2587. ses->serverNOS[2 * len] = 0;
  2588. ses->serverNOS[1 +
  2589. (2 * len)] = 0;
  2590. remaining_words -= len + 1;
  2591. if (remaining_words > 0) {
  2592. len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
  2593. /* last string not always null terminated
  2594. (for e.g. for Windows XP & 2000) */
  2595. kfree(ses->serverDomain);
  2596. ses->serverDomain =
  2597. kzalloc(2 *
  2598. (len +
  2599. 1),
  2600. GFP_KERNEL);
  2601. cifs_strfromUCS_le
  2602. (ses->serverDomain,
  2603. (__le16 *)bcc_ptr,
  2604. len, nls_codepage);
  2605. bcc_ptr +=
  2606. 2 * (len + 1);
  2607. ses->serverDomain[2*len]
  2608. = 0;
  2609. ses->serverDomain
  2610. [1 + (2 * len)]
  2611. = 0;
  2612. } /* else no more room so create dummy domain string */
  2613. else {
  2614. kfree(ses->serverDomain);
  2615. ses->serverDomain =
  2616. kzalloc(2,
  2617. GFP_KERNEL);
  2618. }
  2619. } else { /* no room so create dummy domain and NOS string */
  2620. kfree(ses->serverDomain);
  2621. ses->serverDomain =
  2622. kzalloc(2, GFP_KERNEL);
  2623. kfree(ses->serverNOS);
  2624. ses->serverNOS =
  2625. kzalloc(2, GFP_KERNEL);
  2626. }
  2627. } else { /* ASCII */
  2628. len = strnlen(bcc_ptr, 1024);
  2629. if (((long) bcc_ptr + len) - (long)
  2630. pByteArea(smb_buffer_response)
  2631. <= BCC(smb_buffer_response)) {
  2632. if (ses->serverOS)
  2633. kfree(ses->serverOS);
  2634. ses->serverOS =
  2635. kzalloc(len + 1,
  2636. GFP_KERNEL);
  2637. strncpy(ses->serverOS,
  2638. bcc_ptr, len);
  2639. bcc_ptr += len;
  2640. bcc_ptr[0] = 0; /* null terminate string */
  2641. bcc_ptr++;
  2642. len = strnlen(bcc_ptr, 1024);
  2643. kfree(ses->serverNOS);
  2644. ses->serverNOS =
  2645. kzalloc(len + 1,
  2646. GFP_KERNEL);
  2647. strncpy(ses->serverNOS, bcc_ptr, len);
  2648. bcc_ptr += len;
  2649. bcc_ptr[0] = 0;
  2650. bcc_ptr++;
  2651. len = strnlen(bcc_ptr, 1024);
  2652. kfree(ses->serverDomain);
  2653. ses->serverDomain =
  2654. kzalloc(len + 1,
  2655. GFP_KERNEL);
  2656. strncpy(ses->serverDomain,
  2657. bcc_ptr, len);
  2658. bcc_ptr += len;
  2659. bcc_ptr[0] = 0;
  2660. bcc_ptr++;
  2661. } else
  2662. cFYI(1,
  2663. ("field of length %d "
  2664. "extends beyond end of smb",
  2665. len));
  2666. }
  2667. } else {
  2668. cERROR(1, ("Security Blob Length extends beyond"
  2669. " end of SMB"));
  2670. }
  2671. } else {
  2672. cERROR(1, ("No session structure passed in."));
  2673. }
  2674. } else {
  2675. cERROR(1,
  2676. (" Invalid Word count %d:",
  2677. smb_buffer_response->WordCount));
  2678. rc = -EIO;
  2679. }
  2680. cifs_buf_release(smb_buffer);
  2681. return rc;
  2682. }
  2683. static int
  2684. CIFSNTLMSSPAuthSessSetup(unsigned int xid, struct cifsSesInfo *ses,
  2685. char *ntlm_session_key, int ntlmv2_flag,
  2686. const struct nls_table *nls_codepage)
  2687. {
  2688. struct smb_hdr *smb_buffer;
  2689. struct smb_hdr *smb_buffer_response;
  2690. SESSION_SETUP_ANDX *pSMB;
  2691. SESSION_SETUP_ANDX *pSMBr;
  2692. char *bcc_ptr;
  2693. char *user;
  2694. char *domain;
  2695. int rc = 0;
  2696. int remaining_words = 0;
  2697. int bytes_returned = 0;
  2698. int len;
  2699. int SecurityBlobLength = sizeof(AUTHENTICATE_MESSAGE);
  2700. PAUTHENTICATE_MESSAGE SecurityBlob;
  2701. __u32 negotiate_flags, capabilities;
  2702. __u16 count;
  2703. cFYI(1, ("In NTLMSSPSessSetup (Authenticate)"));
  2704. if (ses == NULL)
  2705. return -EINVAL;
  2706. user = ses->userName;
  2707. domain = ses->domainName;
  2708. smb_buffer = cifs_buf_get();
  2709. if (smb_buffer == NULL) {
  2710. return -ENOMEM;
  2711. }
  2712. smb_buffer_response = smb_buffer;
  2713. pSMB = (SESSION_SETUP_ANDX *)smb_buffer;
  2714. pSMBr = (SESSION_SETUP_ANDX *)smb_buffer_response;
  2715. /* send SMBsessionSetup here */
  2716. header_assemble(smb_buffer, SMB_COM_SESSION_SETUP_ANDX,
  2717. NULL /* no tCon exists yet */ , 12 /* wct */ );
  2718. smb_buffer->Mid = GetNextMid(ses->server);
  2719. pSMB->req.hdr.Flags |= (SMBFLG_CASELESS | SMBFLG_CANONICAL_PATH_FORMAT);
  2720. pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC;
  2721. pSMB->req.AndXCommand = 0xFF;
  2722. pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf);
  2723. pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq);
  2724. pSMB->req.hdr.Uid = ses->Suid;
  2725. if (ses->server->secMode & (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2726. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2727. capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS |
  2728. CAP_EXTENDED_SECURITY;
  2729. if (ses->capabilities & CAP_UNICODE) {
  2730. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2731. capabilities |= CAP_UNICODE;
  2732. }
  2733. if (ses->capabilities & CAP_STATUS32) {
  2734. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2735. capabilities |= CAP_STATUS32;
  2736. }
  2737. if (ses->capabilities & CAP_DFS) {
  2738. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2739. capabilities |= CAP_DFS;
  2740. }
  2741. pSMB->req.Capabilities = cpu_to_le32(capabilities);
  2742. bcc_ptr = (char *)&pSMB->req.SecurityBlob;
  2743. SecurityBlob = (PAUTHENTICATE_MESSAGE)bcc_ptr;
  2744. strncpy(SecurityBlob->Signature, NTLMSSP_SIGNATURE, 8);
  2745. SecurityBlob->MessageType = NtLmAuthenticate;
  2746. bcc_ptr += SecurityBlobLength;
  2747. negotiate_flags = NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_REQUEST_TARGET |
  2748. NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_TARGET_INFO |
  2749. 0x80000000 | NTLMSSP_NEGOTIATE_128;
  2750. if (sign_CIFS_PDUs)
  2751. negotiate_flags |= /* NTLMSSP_NEGOTIATE_ALWAYS_SIGN |*/ NTLMSSP_NEGOTIATE_SIGN;
  2752. if (ntlmv2_flag)
  2753. negotiate_flags |= NTLMSSP_NEGOTIATE_NTLMV2;
  2754. /* setup pointers to domain name and workstation name */
  2755. SecurityBlob->WorkstationName.Buffer = 0;
  2756. SecurityBlob->WorkstationName.Length = 0;
  2757. SecurityBlob->WorkstationName.MaximumLength = 0;
  2758. SecurityBlob->SessionKey.Length = 0;
  2759. SecurityBlob->SessionKey.MaximumLength = 0;
  2760. SecurityBlob->SessionKey.Buffer = 0;
  2761. SecurityBlob->LmChallengeResponse.Length = 0;
  2762. SecurityBlob->LmChallengeResponse.MaximumLength = 0;
  2763. SecurityBlob->LmChallengeResponse.Buffer = 0;
  2764. SecurityBlob->NtChallengeResponse.Length =
  2765. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2766. SecurityBlob->NtChallengeResponse.MaximumLength =
  2767. cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2768. memcpy(bcc_ptr, ntlm_session_key, CIFS_SESS_KEY_SIZE);
  2769. SecurityBlob->NtChallengeResponse.Buffer =
  2770. cpu_to_le32(SecurityBlobLength);
  2771. SecurityBlobLength += CIFS_SESS_KEY_SIZE;
  2772. bcc_ptr += CIFS_SESS_KEY_SIZE;
  2773. if (ses->capabilities & CAP_UNICODE) {
  2774. if (domain == NULL) {
  2775. SecurityBlob->DomainName.Buffer = 0;
  2776. SecurityBlob->DomainName.Length = 0;
  2777. SecurityBlob->DomainName.MaximumLength = 0;
  2778. } else {
  2779. __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, domain, 64,
  2780. nls_codepage);
  2781. ln *= 2;
  2782. SecurityBlob->DomainName.MaximumLength =
  2783. cpu_to_le16(ln);
  2784. SecurityBlob->DomainName.Buffer =
  2785. cpu_to_le32(SecurityBlobLength);
  2786. bcc_ptr += ln;
  2787. SecurityBlobLength += ln;
  2788. SecurityBlob->DomainName.Length = cpu_to_le16(ln);
  2789. }
  2790. if (user == NULL) {
  2791. SecurityBlob->UserName.Buffer = 0;
  2792. SecurityBlob->UserName.Length = 0;
  2793. SecurityBlob->UserName.MaximumLength = 0;
  2794. } else {
  2795. __u16 ln = cifs_strtoUCS((__le16 *) bcc_ptr, user, 64,
  2796. nls_codepage);
  2797. ln *= 2;
  2798. SecurityBlob->UserName.MaximumLength =
  2799. cpu_to_le16(ln);
  2800. SecurityBlob->UserName.Buffer =
  2801. cpu_to_le32(SecurityBlobLength);
  2802. bcc_ptr += ln;
  2803. SecurityBlobLength += ln;
  2804. SecurityBlob->UserName.Length = cpu_to_le16(ln);
  2805. }
  2806. /* SecurityBlob->WorkstationName.Length =
  2807. cifs_strtoUCS((__le16 *) bcc_ptr, "AMACHINE",64, nls_codepage);
  2808. SecurityBlob->WorkstationName.Length *= 2;
  2809. SecurityBlob->WorkstationName.MaximumLength =
  2810. cpu_to_le16(SecurityBlob->WorkstationName.Length);
  2811. SecurityBlob->WorkstationName.Buffer =
  2812. cpu_to_le32(SecurityBlobLength);
  2813. bcc_ptr += SecurityBlob->WorkstationName.Length;
  2814. SecurityBlobLength += SecurityBlob->WorkstationName.Length;
  2815. SecurityBlob->WorkstationName.Length =
  2816. cpu_to_le16(SecurityBlob->WorkstationName.Length); */
  2817. if ((long) bcc_ptr % 2) {
  2818. *bcc_ptr = 0;
  2819. bcc_ptr++;
  2820. }
  2821. bytes_returned =
  2822. cifs_strtoUCS((__le16 *) bcc_ptr, "Linux version ",
  2823. 32, nls_codepage);
  2824. bcc_ptr += 2 * bytes_returned;
  2825. bytes_returned =
  2826. cifs_strtoUCS((__le16 *) bcc_ptr, utsname()->release, 32,
  2827. nls_codepage);
  2828. bcc_ptr += 2 * bytes_returned;
  2829. bcc_ptr += 2; /* null term version string */
  2830. bytes_returned =
  2831. cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS,
  2832. 64, nls_codepage);
  2833. bcc_ptr += 2 * bytes_returned;
  2834. *(bcc_ptr + 1) = 0;
  2835. *(bcc_ptr + 2) = 0;
  2836. bcc_ptr += 2; /* null terminate network opsys string */
  2837. *(bcc_ptr + 1) = 0;
  2838. *(bcc_ptr + 2) = 0;
  2839. bcc_ptr += 2; /* null domain */
  2840. } else { /* ASCII */
  2841. if (domain == NULL) {
  2842. SecurityBlob->DomainName.Buffer = 0;
  2843. SecurityBlob->DomainName.Length = 0;
  2844. SecurityBlob->DomainName.MaximumLength = 0;
  2845. } else {
  2846. __u16 ln;
  2847. negotiate_flags |= NTLMSSP_NEGOTIATE_DOMAIN_SUPPLIED;
  2848. strncpy(bcc_ptr, domain, 63);
  2849. ln = strnlen(domain, 64);
  2850. SecurityBlob->DomainName.MaximumLength =
  2851. cpu_to_le16(ln);
  2852. SecurityBlob->DomainName.Buffer =
  2853. cpu_to_le32(SecurityBlobLength);
  2854. bcc_ptr += ln;
  2855. SecurityBlobLength += ln;
  2856. SecurityBlob->DomainName.Length = cpu_to_le16(ln);
  2857. }
  2858. if (user == NULL) {
  2859. SecurityBlob->UserName.Buffer = 0;
  2860. SecurityBlob->UserName.Length = 0;
  2861. SecurityBlob->UserName.MaximumLength = 0;
  2862. } else {
  2863. __u16 ln;
  2864. strncpy(bcc_ptr, user, 63);
  2865. ln = strnlen(user, 64);
  2866. SecurityBlob->UserName.MaximumLength = cpu_to_le16(ln);
  2867. SecurityBlob->UserName.Buffer =
  2868. cpu_to_le32(SecurityBlobLength);
  2869. bcc_ptr += ln;
  2870. SecurityBlobLength += ln;
  2871. SecurityBlob->UserName.Length = cpu_to_le16(ln);
  2872. }
  2873. /* BB fill in our workstation name if known BB */
  2874. strcpy(bcc_ptr, "Linux version ");
  2875. bcc_ptr += strlen("Linux version ");
  2876. strcpy(bcc_ptr, utsname()->release);
  2877. bcc_ptr += strlen(utsname()->release) + 1;
  2878. strcpy(bcc_ptr, CIFS_NETWORK_OPSYS);
  2879. bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1;
  2880. bcc_ptr++; /* null domain */
  2881. *bcc_ptr = 0;
  2882. }
  2883. SecurityBlob->NegotiateFlags = cpu_to_le32(negotiate_flags);
  2884. pSMB->req.SecurityBlobLength = cpu_to_le16(SecurityBlobLength);
  2885. count = (long) bcc_ptr - (long) pByteArea(smb_buffer);
  2886. smb_buffer->smb_buf_length += count;
  2887. pSMB->req.ByteCount = cpu_to_le16(count);
  2888. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response,
  2889. &bytes_returned, CIFS_LONG_OP);
  2890. if (rc) {
  2891. /* rc = map_smb_to_linux_error(smb_buffer_response) done in SendReceive now */
  2892. } else if ((smb_buffer_response->WordCount == 3) ||
  2893. (smb_buffer_response->WordCount == 4)) {
  2894. __u16 action = le16_to_cpu(pSMBr->resp.Action);
  2895. __u16 blob_len = le16_to_cpu(pSMBr->resp.SecurityBlobLength);
  2896. if (action & GUEST_LOGIN)
  2897. cFYI(1, (" Guest login")); /* BB Should we set anything
  2898. in SesInfo struct ? */
  2899. /* if (SecurityBlob2->MessageType != NtLm??) {
  2900. cFYI("Unexpected message type on auth response is %d"));
  2901. } */
  2902. if (ses) {
  2903. cFYI(1,
  2904. ("Check challenge UID %d vs auth response UID %d",
  2905. ses->Suid, smb_buffer_response->Uid));
  2906. /* UID left in wire format */
  2907. ses->Suid = smb_buffer_response->Uid;
  2908. bcc_ptr = pByteArea(smb_buffer_response);
  2909. /* response can have either 3 or 4 word count - Samba sends 3 */
  2910. if ((pSMBr->resp.hdr.WordCount == 3)
  2911. || ((pSMBr->resp.hdr.WordCount == 4)
  2912. && (blob_len <
  2913. pSMBr->resp.ByteCount))) {
  2914. if (pSMBr->resp.hdr.WordCount == 4) {
  2915. bcc_ptr +=
  2916. blob_len;
  2917. cFYI(1,
  2918. ("Security Blob Length %d ",
  2919. blob_len));
  2920. }
  2921. cFYI(1,
  2922. ("NTLMSSP response to Authenticate "));
  2923. if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
  2924. if ((long) (bcc_ptr) % 2) {
  2925. remaining_words =
  2926. (BCC(smb_buffer_response)
  2927. - 1) / 2;
  2928. bcc_ptr++; /* Unicode strings must be word aligned */
  2929. } else {
  2930. remaining_words = BCC(smb_buffer_response) / 2;
  2931. }
  2932. len = UniStrnlen((wchar_t *) bcc_ptr,
  2933. remaining_words - 1);
  2934. /* We look for obvious messed up bcc or strings in response so we do not go off
  2935. the end since (at least) WIN2K and Windows XP have a major bug in not null
  2936. terminating last Unicode string in response */
  2937. if (ses->serverOS)
  2938. kfree(ses->serverOS);
  2939. ses->serverOS =
  2940. kzalloc(2 * (len + 1), GFP_KERNEL);
  2941. cifs_strfromUCS_le(ses->serverOS,
  2942. (__le16 *)
  2943. bcc_ptr, len,
  2944. nls_codepage);
  2945. bcc_ptr += 2 * (len + 1);
  2946. remaining_words -= len + 1;
  2947. ses->serverOS[2 * len] = 0;
  2948. ses->serverOS[1 + (2 * len)] = 0;
  2949. if (remaining_words > 0) {
  2950. len = UniStrnlen((wchar_t *)
  2951. bcc_ptr,
  2952. remaining_words
  2953. - 1);
  2954. kfree(ses->serverNOS);
  2955. ses->serverNOS =
  2956. kzalloc(2 * (len + 1),
  2957. GFP_KERNEL);
  2958. cifs_strfromUCS_le(ses->
  2959. serverNOS,
  2960. (__le16 *)
  2961. bcc_ptr,
  2962. len,
  2963. nls_codepage);
  2964. bcc_ptr += 2 * (len + 1);
  2965. ses->serverNOS[2 * len] = 0;
  2966. ses->serverNOS[1+(2*len)] = 0;
  2967. remaining_words -= len + 1;
  2968. if (remaining_words > 0) {
  2969. len = UniStrnlen((wchar_t *) bcc_ptr, remaining_words);
  2970. /* last string not always null terminated (e.g. for Windows XP & 2000) */
  2971. if (ses->serverDomain)
  2972. kfree(ses->serverDomain);
  2973. ses->serverDomain =
  2974. kzalloc(2 *
  2975. (len +
  2976. 1),
  2977. GFP_KERNEL);
  2978. cifs_strfromUCS_le
  2979. (ses->
  2980. serverDomain,
  2981. (__le16 *)
  2982. bcc_ptr, len,
  2983. nls_codepage);
  2984. bcc_ptr +=
  2985. 2 * (len + 1);
  2986. ses->
  2987. serverDomain[2
  2988. * len]
  2989. = 0;
  2990. ses->
  2991. serverDomain[1
  2992. +
  2993. (2
  2994. *
  2995. len)]
  2996. = 0;
  2997. } /* else no more room so create dummy domain string */
  2998. else {
  2999. if (ses->serverDomain)
  3000. kfree(ses->serverDomain);
  3001. ses->serverDomain = kzalloc(2,GFP_KERNEL);
  3002. }
  3003. } else { /* no room so create dummy domain and NOS string */
  3004. if (ses->serverDomain)
  3005. kfree(ses->serverDomain);
  3006. ses->serverDomain = kzalloc(2, GFP_KERNEL);
  3007. kfree(ses->serverNOS);
  3008. ses->serverNOS = kzalloc(2, GFP_KERNEL);
  3009. }
  3010. } else { /* ASCII */
  3011. len = strnlen(bcc_ptr, 1024);
  3012. if (((long) bcc_ptr + len) -
  3013. (long) pByteArea(smb_buffer_response)
  3014. <= BCC(smb_buffer_response)) {
  3015. if (ses->serverOS)
  3016. kfree(ses->serverOS);
  3017. ses->serverOS = kzalloc(len + 1, GFP_KERNEL);
  3018. strncpy(ses->serverOS,bcc_ptr, len);
  3019. bcc_ptr += len;
  3020. bcc_ptr[0] = 0; /* null terminate the string */
  3021. bcc_ptr++;
  3022. len = strnlen(bcc_ptr, 1024);
  3023. kfree(ses->serverNOS);
  3024. ses->serverNOS = kzalloc(len+1,
  3025. GFP_KERNEL);
  3026. strncpy(ses->serverNOS,
  3027. bcc_ptr, len);
  3028. bcc_ptr += len;
  3029. bcc_ptr[0] = 0;
  3030. bcc_ptr++;
  3031. len = strnlen(bcc_ptr, 1024);
  3032. if (ses->serverDomain)
  3033. kfree(ses->serverDomain);
  3034. ses->serverDomain =
  3035. kzalloc(len+1,
  3036. GFP_KERNEL);
  3037. strncpy(ses->serverDomain,
  3038. bcc_ptr, len);
  3039. bcc_ptr += len;
  3040. bcc_ptr[0] = 0;
  3041. bcc_ptr++;
  3042. } else
  3043. cFYI(1, ("field of length %d "
  3044. "extends beyond end of smb ",
  3045. len));
  3046. }
  3047. } else {
  3048. cERROR(1, ("Security Blob extends beyond end "
  3049. "of SMB"));
  3050. }
  3051. } else {
  3052. cERROR(1, ("No session structure passed in."));
  3053. }
  3054. } else {
  3055. cERROR(1, ("Invalid Word count %d: ",
  3056. smb_buffer_response->WordCount));
  3057. rc = -EIO;
  3058. }
  3059. cifs_buf_release(smb_buffer);
  3060. return rc;
  3061. }
  3062. int
  3063. CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
  3064. const char *tree, struct cifsTconInfo *tcon,
  3065. const struct nls_table *nls_codepage)
  3066. {
  3067. struct smb_hdr *smb_buffer;
  3068. struct smb_hdr *smb_buffer_response;
  3069. TCONX_REQ *pSMB;
  3070. TCONX_RSP *pSMBr;
  3071. unsigned char *bcc_ptr;
  3072. int rc = 0;
  3073. int length;
  3074. __u16 count;
  3075. if (ses == NULL)
  3076. return -EIO;
  3077. smb_buffer = cifs_buf_get();
  3078. if (smb_buffer == NULL) {
  3079. return -ENOMEM;
  3080. }
  3081. smb_buffer_response = smb_buffer;
  3082. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  3083. NULL /*no tid */ , 4 /*wct */ );
  3084. smb_buffer->Mid = GetNextMid(ses->server);
  3085. smb_buffer->Uid = ses->Suid;
  3086. pSMB = (TCONX_REQ *) smb_buffer;
  3087. pSMBr = (TCONX_RSP *) smb_buffer_response;
  3088. pSMB->AndXCommand = 0xFF;
  3089. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  3090. bcc_ptr = &pSMB->Password[0];
  3091. if ((ses->server->secMode) & SECMODE_USER) {
  3092. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  3093. *bcc_ptr = 0; /* password is null byte */
  3094. bcc_ptr++; /* skip password */
  3095. /* already aligned so no need to do it below */
  3096. } else {
  3097. pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
  3098. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  3099. specified as required (when that support is added to
  3100. the vfs in the future) as only NTLM or the much
  3101. weaker LANMAN (which we do not send by default) is accepted
  3102. by Samba (not sure whether other servers allow
  3103. NTLMv2 password here) */
  3104. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  3105. if ((extended_security & CIFSSEC_MAY_LANMAN) &&
  3106. (ses->server->secType == LANMAN))
  3107. calc_lanman_hash(ses, bcc_ptr);
  3108. else
  3109. #endif /* CIFS_WEAK_PW_HASH */
  3110. SMBNTencrypt(ses->password,
  3111. ses->server->cryptKey,
  3112. bcc_ptr);
  3113. bcc_ptr += CIFS_SESS_KEY_SIZE;
  3114. if (ses->capabilities & CAP_UNICODE) {
  3115. /* must align unicode strings */
  3116. *bcc_ptr = 0; /* null byte password */
  3117. bcc_ptr++;
  3118. }
  3119. }
  3120. if (ses->server->secMode &
  3121. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  3122. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  3123. if (ses->capabilities & CAP_STATUS32) {
  3124. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  3125. }
  3126. if (ses->capabilities & CAP_DFS) {
  3127. smb_buffer->Flags2 |= SMBFLG2_DFS;
  3128. }
  3129. if (ses->capabilities & CAP_UNICODE) {
  3130. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  3131. length =
  3132. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  3133. 6 /* max utf8 char length in bytes */ *
  3134. (/* server len*/ + 256 /* share len */), nls_codepage);
  3135. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  3136. bcc_ptr += 2; /* skip trailing null */
  3137. } else { /* ASCII */
  3138. strcpy(bcc_ptr, tree);
  3139. bcc_ptr += strlen(tree) + 1;
  3140. }
  3141. strcpy(bcc_ptr, "?????");
  3142. bcc_ptr += strlen("?????");
  3143. bcc_ptr += 1;
  3144. count = bcc_ptr - &pSMB->Password[0];
  3145. pSMB->hdr.smb_buf_length += count;
  3146. pSMB->ByteCount = cpu_to_le16(count);
  3147. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  3148. CIFS_STD_OP);
  3149. /* if (rc) rc = map_smb_to_linux_error(smb_buffer_response); */
  3150. /* above now done in SendReceive */
  3151. if ((rc == 0) && (tcon != NULL)) {
  3152. tcon->tidStatus = CifsGood;
  3153. tcon->tid = smb_buffer_response->Tid;
  3154. bcc_ptr = pByteArea(smb_buffer_response);
  3155. length = strnlen(bcc_ptr, BCC(smb_buffer_response) - 2);
  3156. /* skip service field (NB: this field is always ASCII) */
  3157. if (length == 3) {
  3158. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  3159. (bcc_ptr[2] == 'C')) {
  3160. cFYI(1, ("IPC connection"));
  3161. tcon->ipc = 1;
  3162. }
  3163. } else if (length == 2) {
  3164. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  3165. /* the most common case */
  3166. cFYI(1, ("disk share connection"));
  3167. }
  3168. }
  3169. bcc_ptr += length + 1;
  3170. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  3171. if (smb_buffer->Flags2 & SMBFLG2_UNICODE) {
  3172. length = UniStrnlen((wchar_t *) bcc_ptr, 512);
  3173. if ((bcc_ptr + (2 * length)) -
  3174. pByteArea(smb_buffer_response) <=
  3175. BCC(smb_buffer_response)) {
  3176. kfree(tcon->nativeFileSystem);
  3177. tcon->nativeFileSystem =
  3178. kzalloc(length + 2, GFP_KERNEL);
  3179. if (tcon->nativeFileSystem)
  3180. cifs_strfromUCS_le(
  3181. tcon->nativeFileSystem,
  3182. (__le16 *) bcc_ptr,
  3183. length, nls_codepage);
  3184. bcc_ptr += 2 * length;
  3185. bcc_ptr[0] = 0; /* null terminate the string */
  3186. bcc_ptr[1] = 0;
  3187. bcc_ptr += 2;
  3188. }
  3189. /* else do not bother copying these information fields*/
  3190. } else {
  3191. length = strnlen(bcc_ptr, 1024);
  3192. if ((bcc_ptr + length) -
  3193. pByteArea(smb_buffer_response) <=
  3194. BCC(smb_buffer_response)) {
  3195. kfree(tcon->nativeFileSystem);
  3196. tcon->nativeFileSystem =
  3197. kzalloc(length + 1, GFP_KERNEL);
  3198. if (tcon->nativeFileSystem)
  3199. strncpy(tcon->nativeFileSystem, bcc_ptr,
  3200. length);
  3201. }
  3202. /* else do not bother copying these information fields*/
  3203. }
  3204. if ((smb_buffer_response->WordCount == 3) ||
  3205. (smb_buffer_response->WordCount == 7))
  3206. /* field is in same location */
  3207. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  3208. else
  3209. tcon->Flags = 0;
  3210. cFYI(1, ("Tcon flags: 0x%x ", tcon->Flags));
  3211. } else if ((rc == 0) && tcon == NULL) {
  3212. /* all we need to save for IPC$ connection */
  3213. ses->ipc_tid = smb_buffer_response->Tid;
  3214. }
  3215. cifs_buf_release(smb_buffer);
  3216. return rc;
  3217. }
  3218. int
  3219. cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
  3220. {
  3221. int rc = 0;
  3222. int xid;
  3223. struct cifsSesInfo *ses = NULL;
  3224. struct task_struct *cifsd_task;
  3225. char *tmp;
  3226. xid = GetXid();
  3227. if (cifs_sb->tcon) {
  3228. ses = cifs_sb->tcon->ses; /* save ptr to ses before delete tcon!*/
  3229. rc = CIFSSMBTDis(xid, cifs_sb->tcon);
  3230. if (rc == -EBUSY) {
  3231. FreeXid(xid);
  3232. return 0;
  3233. }
  3234. tconInfoFree(cifs_sb->tcon);
  3235. if ((ses) && (ses->server)) {
  3236. /* save off task so we do not refer to ses later */
  3237. cifsd_task = ses->server->tsk;
  3238. cFYI(1, ("About to do SMBLogoff "));
  3239. rc = CIFSSMBLogoff(xid, ses);
  3240. if (rc == -EBUSY) {
  3241. FreeXid(xid);
  3242. return 0;
  3243. } else if (rc == -ESHUTDOWN) {
  3244. cFYI(1, ("Waking up socket by sending signal"));
  3245. if (cifsd_task) {
  3246. force_sig(SIGKILL, cifsd_task);
  3247. kthread_stop(cifsd_task);
  3248. }
  3249. rc = 0;
  3250. } /* else - we have an smb session
  3251. left on this socket do not kill cifsd */
  3252. } else
  3253. cFYI(1, ("No session or bad tcon"));
  3254. }
  3255. cifs_sb->tcon = NULL;
  3256. tmp = cifs_sb->prepath;
  3257. cifs_sb->prepathlen = 0;
  3258. cifs_sb->prepath = NULL;
  3259. kfree(tmp);
  3260. if (ses)
  3261. schedule_timeout_interruptible(msecs_to_jiffies(500));
  3262. if (ses)
  3263. sesInfoFree(ses);
  3264. FreeXid(xid);
  3265. return rc; /* BB check if we should always return zero here */
  3266. }
  3267. int cifs_setup_session(unsigned int xid, struct cifsSesInfo *pSesInfo,
  3268. struct nls_table *nls_info)
  3269. {
  3270. int rc = 0;
  3271. char ntlm_session_key[CIFS_SESS_KEY_SIZE];
  3272. int ntlmv2_flag = FALSE;
  3273. int first_time = 0;
  3274. /* what if server changes its buffer size after dropping the session? */
  3275. if (pSesInfo->server->maxBuf == 0) /* no need to send on reconnect */ {
  3276. rc = CIFSSMBNegotiate(xid, pSesInfo);
  3277. if (rc == -EAGAIN) /* retry only once on 1st time connection */ {
  3278. rc = CIFSSMBNegotiate(xid, pSesInfo);
  3279. if (rc == -EAGAIN)
  3280. rc = -EHOSTDOWN;
  3281. }
  3282. if (rc == 0) {
  3283. spin_lock(&GlobalMid_Lock);
  3284. if (pSesInfo->server->tcpStatus != CifsExiting)
  3285. pSesInfo->server->tcpStatus = CifsGood;
  3286. else
  3287. rc = -EHOSTDOWN;
  3288. spin_unlock(&GlobalMid_Lock);
  3289. }
  3290. first_time = 1;
  3291. }
  3292. if (!rc) {
  3293. pSesInfo->flags = 0;
  3294. pSesInfo->capabilities = pSesInfo->server->capabilities;
  3295. if (linuxExtEnabled == 0)
  3296. pSesInfo->capabilities &= (~CAP_UNIX);
  3297. /* pSesInfo->sequence_number = 0;*/
  3298. cFYI(1,
  3299. ("Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  3300. pSesInfo->server->secMode,
  3301. pSesInfo->server->capabilities,
  3302. pSesInfo->server->timeAdj));
  3303. if (experimEnabled < 2)
  3304. rc = CIFS_SessSetup(xid, pSesInfo,
  3305. first_time, nls_info);
  3306. else if (extended_security
  3307. && (pSesInfo->capabilities
  3308. & CAP_EXTENDED_SECURITY)
  3309. && (pSesInfo->server->secType == NTLMSSP)) {
  3310. rc = -EOPNOTSUPP;
  3311. } else if (extended_security
  3312. && (pSesInfo->capabilities & CAP_EXTENDED_SECURITY)
  3313. && (pSesInfo->server->secType == RawNTLMSSP)) {
  3314. cFYI(1, ("NTLMSSP sesssetup"));
  3315. rc = CIFSNTLMSSPNegotiateSessSetup(xid,
  3316. pSesInfo,
  3317. &ntlmv2_flag,
  3318. nls_info);
  3319. if (!rc) {
  3320. if (ntlmv2_flag) {
  3321. char *v2_response;
  3322. cFYI(1, ("more secure NTLM ver2 hash"));
  3323. if (CalcNTLMv2_partial_mac_key(pSesInfo,
  3324. nls_info)) {
  3325. rc = -ENOMEM;
  3326. goto ss_err_exit;
  3327. } else
  3328. v2_response = kmalloc(16 + 64 /* blob */, GFP_KERNEL);
  3329. if (v2_response) {
  3330. CalcNTLMv2_response(pSesInfo,
  3331. v2_response);
  3332. /* if (first_time)
  3333. cifs_calculate_ntlmv2_mac_key(
  3334. pSesInfo->server->mac_signing_key,
  3335. response, ntlm_session_key,*/
  3336. kfree(v2_response);
  3337. /* BB Put dummy sig in SessSetup PDU? */
  3338. } else {
  3339. rc = -ENOMEM;
  3340. goto ss_err_exit;
  3341. }
  3342. } else {
  3343. SMBNTencrypt(pSesInfo->password,
  3344. pSesInfo->server->cryptKey,
  3345. ntlm_session_key);
  3346. if (first_time)
  3347. cifs_calculate_mac_key(
  3348. &pSesInfo->server->mac_signing_key,
  3349. ntlm_session_key,
  3350. pSesInfo->password);
  3351. }
  3352. /* for better security the weaker lanman hash not sent
  3353. in AuthSessSetup so we no longer calculate it */
  3354. rc = CIFSNTLMSSPAuthSessSetup(xid,
  3355. pSesInfo,
  3356. ntlm_session_key,
  3357. ntlmv2_flag,
  3358. nls_info);
  3359. }
  3360. } else { /* old style NTLM 0.12 session setup */
  3361. SMBNTencrypt(pSesInfo->password,
  3362. pSesInfo->server->cryptKey,
  3363. ntlm_session_key);
  3364. if (first_time)
  3365. cifs_calculate_mac_key(
  3366. &pSesInfo->server->mac_signing_key,
  3367. ntlm_session_key, pSesInfo->password);
  3368. rc = CIFSSessSetup(xid, pSesInfo,
  3369. ntlm_session_key, nls_info);
  3370. }
  3371. if (rc) {
  3372. cERROR(1, ("Send error in SessSetup = %d", rc));
  3373. } else {
  3374. cFYI(1, ("CIFS Session Established successfully"));
  3375. pSesInfo->status = CifsGood;
  3376. }
  3377. }
  3378. ss_err_exit:
  3379. return rc;
  3380. }