connect.c 106 KB

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