connect.c 109 KB

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