connect.c 92 KB

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