connect.c 107 KB

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