connect.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2009
  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/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/ctype.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mempool.h>
  31. #include <linux/delay.h>
  32. #include <linux/completion.h>
  33. #include <linux/kthread.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/freezer.h>
  36. #include <linux/namei.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/processor.h>
  39. #include <linux/inet.h>
  40. #include <net/ipv6.h>
  41. #include "cifspdu.h"
  42. #include "cifsglob.h"
  43. #include "cifsproto.h"
  44. #include "cifs_unicode.h"
  45. #include "cifs_debug.h"
  46. #include "cifs_fs_sb.h"
  47. #include "ntlmssp.h"
  48. #include "nterr.h"
  49. #include "rfc1002pdu.h"
  50. #include "cn_cifs.h"
  51. #define CIFS_PORT 445
  52. #define RFC1001_PORT 139
  53. extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8,
  54. unsigned char *p24);
  55. extern mempool_t *cifs_req_poolp;
  56. struct smb_vol {
  57. char *username;
  58. char *password;
  59. char *domainname;
  60. char *UNC;
  61. char *UNCip;
  62. char *iocharset; /* local code page for mapping to and from Unicode */
  63. char source_rfc1001_name[16]; /* netbios name of client */
  64. char target_rfc1001_name[16]; /* netbios name of server for Win9x/ME */
  65. uid_t linux_uid;
  66. gid_t linux_gid;
  67. mode_t file_mode;
  68. mode_t dir_mode;
  69. unsigned secFlg;
  70. bool retry:1;
  71. bool intr:1;
  72. bool setuids:1;
  73. bool override_uid:1;
  74. bool override_gid:1;
  75. bool dynperm:1;
  76. bool noperm:1;
  77. bool no_psx_acl:1; /* set if posix acl support should be disabled */
  78. bool cifs_acl:1;
  79. bool no_xattr:1; /* set if xattr (EA) support should be disabled*/
  80. bool server_ino:1; /* use inode numbers from server ie UniqueId */
  81. bool direct_io:1;
  82. bool remap:1; /* set to remap seven reserved chars in filenames */
  83. bool posix_paths:1; /* unset to not ask for posix pathnames. */
  84. bool no_linux_ext:1;
  85. bool sfu_emul:1;
  86. bool nullauth:1; /* attempt to authenticate with null user */
  87. bool nocase:1; /* request case insensitive filenames */
  88. bool nobrl:1; /* disable sending byte range locks to srv */
  89. bool mand_lock:1; /* send mandatory not posix byte range lock reqs */
  90. bool seal:1; /* request transport encryption on share */
  91. bool nodfs:1; /* Do not request DFS, even if available */
  92. bool local_lease:1; /* check leases only on local system, not remote */
  93. bool noblocksnd:1;
  94. bool noautotune:1;
  95. bool nostrictsync:1; /* do not force expensive SMBflush on every sync */
  96. unsigned int rsize;
  97. unsigned int wsize;
  98. bool sockopt_tcp_nodelay:1;
  99. unsigned short int port;
  100. char *prepath;
  101. struct nls_table *local_nls;
  102. };
  103. static int ipv4_connect(struct TCP_Server_Info *server);
  104. static int ipv6_connect(struct TCP_Server_Info *server);
  105. /*
  106. * cifs tcp session reconnection
  107. *
  108. * mark tcp session as reconnecting so temporarily locked
  109. * mark all smb sessions as reconnecting for tcp session
  110. * reconnect tcp session
  111. * wake up waiters on reconnection? - (not needed currently)
  112. */
  113. static int
  114. cifs_reconnect(struct TCP_Server_Info *server)
  115. {
  116. int rc = 0;
  117. struct list_head *tmp, *tmp2;
  118. struct cifsSesInfo *ses;
  119. struct cifsTconInfo *tcon;
  120. struct mid_q_entry *mid_entry;
  121. spin_lock(&GlobalMid_Lock);
  122. if (server->tcpStatus == CifsExiting) {
  123. /* the demux thread will exit normally
  124. next time through the loop */
  125. spin_unlock(&GlobalMid_Lock);
  126. return rc;
  127. } else
  128. server->tcpStatus = CifsNeedReconnect;
  129. spin_unlock(&GlobalMid_Lock);
  130. server->maxBuf = 0;
  131. cFYI(1, "Reconnecting tcp session");
  132. /* before reconnecting the tcp session, mark the smb session (uid)
  133. and the tid bad so they are not used until reconnected */
  134. read_lock(&cifs_tcp_ses_lock);
  135. list_for_each(tmp, &server->smb_ses_list) {
  136. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  137. ses->need_reconnect = true;
  138. ses->ipc_tid = 0;
  139. list_for_each(tmp2, &ses->tcon_list) {
  140. tcon = list_entry(tmp2, struct cifsTconInfo, tcon_list);
  141. tcon->need_reconnect = true;
  142. }
  143. }
  144. read_unlock(&cifs_tcp_ses_lock);
  145. /* do not want to be sending data on a socket we are freeing */
  146. mutex_lock(&server->srv_mutex);
  147. if (server->ssocket) {
  148. cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
  149. server->ssocket->flags);
  150. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  151. cFYI(1, "Post shutdown state: 0x%x Flags: 0x%lx",
  152. server->ssocket->state,
  153. server->ssocket->flags);
  154. sock_release(server->ssocket);
  155. server->ssocket = NULL;
  156. }
  157. spin_lock(&GlobalMid_Lock);
  158. list_for_each(tmp, &server->pending_mid_q) {
  159. mid_entry = list_entry(tmp, struct
  160. mid_q_entry,
  161. qhead);
  162. if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
  163. /* Mark other intransit requests as needing
  164. retry so we do not immediately mark the
  165. session bad again (ie after we reconnect
  166. below) as they timeout too */
  167. mid_entry->midState = MID_RETRY_NEEDED;
  168. }
  169. }
  170. spin_unlock(&GlobalMid_Lock);
  171. mutex_unlock(&server->srv_mutex);
  172. while ((server->tcpStatus != CifsExiting) &&
  173. (server->tcpStatus != CifsGood)) {
  174. try_to_freeze();
  175. if (server->addr.sockAddr6.sin6_family == AF_INET6)
  176. rc = ipv6_connect(server);
  177. else
  178. rc = ipv4_connect(server);
  179. if (rc) {
  180. cFYI(1, "reconnect error %d", rc);
  181. msleep(3000);
  182. } else {
  183. atomic_inc(&tcpSesReconnectCount);
  184. spin_lock(&GlobalMid_Lock);
  185. if (server->tcpStatus != CifsExiting)
  186. server->tcpStatus = CifsGood;
  187. server->sequence_number = 0;
  188. spin_unlock(&GlobalMid_Lock);
  189. /* atomic_set(&server->inFlight,0);*/
  190. wake_up(&server->response_q);
  191. }
  192. }
  193. return rc;
  194. }
  195. /*
  196. return codes:
  197. 0 not a transact2, or all data present
  198. >0 transact2 with that much data missing
  199. -EINVAL = invalid transact2
  200. */
  201. static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
  202. {
  203. struct smb_t2_rsp *pSMBt;
  204. int total_data_size;
  205. int data_in_this_rsp;
  206. int remaining;
  207. if (pSMB->Command != SMB_COM_TRANSACTION2)
  208. return 0;
  209. /* check for plausible wct, bcc and t2 data and parm sizes */
  210. /* check for parm and data offset going beyond end of smb */
  211. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  212. cFYI(1, "invalid transact2 word count");
  213. return -EINVAL;
  214. }
  215. pSMBt = (struct smb_t2_rsp *)pSMB;
  216. total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
  217. data_in_this_rsp = le16_to_cpu(pSMBt->t2_rsp.DataCount);
  218. remaining = total_data_size - data_in_this_rsp;
  219. if (remaining == 0)
  220. return 0;
  221. else if (remaining < 0) {
  222. cFYI(1, "total data %d smaller than data in frame %d",
  223. total_data_size, data_in_this_rsp);
  224. return -EINVAL;
  225. } else {
  226. cFYI(1, "missing %d bytes from transact2, check next response",
  227. remaining);
  228. if (total_data_size > maxBufSize) {
  229. cERROR(1, "TotalDataSize %d is over maximum buffer %d",
  230. total_data_size, maxBufSize);
  231. return -EINVAL;
  232. }
  233. return remaining;
  234. }
  235. }
  236. static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
  237. {
  238. struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
  239. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
  240. int total_data_size;
  241. int total_in_buf;
  242. int remaining;
  243. int total_in_buf2;
  244. char *data_area_of_target;
  245. char *data_area_of_buf2;
  246. __u16 byte_count;
  247. total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount);
  248. if (total_data_size != le16_to_cpu(pSMB2->t2_rsp.TotalDataCount)) {
  249. cFYI(1, "total data size of primary and secondary t2 differ");
  250. }
  251. total_in_buf = le16_to_cpu(pSMBt->t2_rsp.DataCount);
  252. remaining = total_data_size - total_in_buf;
  253. if (remaining < 0)
  254. return -EINVAL;
  255. if (remaining == 0) /* nothing to do, ignore */
  256. return 0;
  257. total_in_buf2 = le16_to_cpu(pSMB2->t2_rsp.DataCount);
  258. if (remaining < total_in_buf2) {
  259. cFYI(1, "transact2 2nd response contains too much data");
  260. }
  261. /* find end of first SMB data area */
  262. data_area_of_target = (char *)&pSMBt->hdr.Protocol +
  263. le16_to_cpu(pSMBt->t2_rsp.DataOffset);
  264. /* validate target area */
  265. data_area_of_buf2 = (char *) &pSMB2->hdr.Protocol +
  266. le16_to_cpu(pSMB2->t2_rsp.DataOffset);
  267. data_area_of_target += total_in_buf;
  268. /* copy second buffer into end of first buffer */
  269. memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
  270. total_in_buf += total_in_buf2;
  271. pSMBt->t2_rsp.DataCount = cpu_to_le16(total_in_buf);
  272. byte_count = le16_to_cpu(BCC_LE(pTargetSMB));
  273. byte_count += total_in_buf2;
  274. BCC_LE(pTargetSMB) = cpu_to_le16(byte_count);
  275. byte_count = pTargetSMB->smb_buf_length;
  276. byte_count += total_in_buf2;
  277. /* BB also add check that we are not beyond maximum buffer size */
  278. pTargetSMB->smb_buf_length = byte_count;
  279. if (remaining == total_in_buf2) {
  280. cFYI(1, "found the last secondary response");
  281. return 0; /* we are done */
  282. } else /* more responses to go */
  283. return 1;
  284. }
  285. static int
  286. cifs_demultiplex_thread(struct TCP_Server_Info *server)
  287. {
  288. int length;
  289. unsigned int pdu_length, total_read;
  290. struct smb_hdr *smb_buffer = NULL;
  291. struct smb_hdr *bigbuf = NULL;
  292. struct smb_hdr *smallbuf = NULL;
  293. struct msghdr smb_msg;
  294. struct kvec iov;
  295. struct socket *csocket = server->ssocket;
  296. struct list_head *tmp;
  297. struct cifsSesInfo *ses;
  298. struct task_struct *task_to_wake = NULL;
  299. struct mid_q_entry *mid_entry;
  300. char temp;
  301. bool isLargeBuf = false;
  302. bool isMultiRsp;
  303. int reconnect;
  304. current->flags |= PF_MEMALLOC;
  305. cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
  306. length = atomic_inc_return(&tcpSesAllocCount);
  307. if (length > 1)
  308. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  309. GFP_KERNEL);
  310. set_freezable();
  311. while (server->tcpStatus != CifsExiting) {
  312. if (try_to_freeze())
  313. continue;
  314. if (bigbuf == NULL) {
  315. bigbuf = cifs_buf_get();
  316. if (!bigbuf) {
  317. cERROR(1, "No memory for large SMB response");
  318. msleep(3000);
  319. /* retry will check if exiting */
  320. continue;
  321. }
  322. } else if (isLargeBuf) {
  323. /* we are reusing a dirty large buf, clear its start */
  324. memset(bigbuf, 0, sizeof(struct smb_hdr));
  325. }
  326. if (smallbuf == NULL) {
  327. smallbuf = cifs_small_buf_get();
  328. if (!smallbuf) {
  329. cERROR(1, "No memory for SMB response");
  330. msleep(1000);
  331. /* retry will check if exiting */
  332. continue;
  333. }
  334. /* beginning of smb buffer is cleared in our buf_get */
  335. } else /* if existing small buf clear beginning */
  336. memset(smallbuf, 0, sizeof(struct smb_hdr));
  337. isLargeBuf = false;
  338. isMultiRsp = false;
  339. smb_buffer = smallbuf;
  340. iov.iov_base = smb_buffer;
  341. iov.iov_len = 4;
  342. smb_msg.msg_control = NULL;
  343. smb_msg.msg_controllen = 0;
  344. pdu_length = 4; /* enough to get RFC1001 header */
  345. incomplete_rcv:
  346. length =
  347. kernel_recvmsg(csocket, &smb_msg,
  348. &iov, 1, pdu_length, 0 /* BB other flags? */);
  349. if (server->tcpStatus == CifsExiting) {
  350. break;
  351. } else if (server->tcpStatus == CifsNeedReconnect) {
  352. cFYI(1, "Reconnect after server stopped responding");
  353. cifs_reconnect(server);
  354. cFYI(1, "call to reconnect done");
  355. csocket = server->ssocket;
  356. continue;
  357. } else if ((length == -ERESTARTSYS) || (length == -EAGAIN)) {
  358. msleep(1); /* minimum sleep to prevent looping
  359. allowing socket to clear and app threads to set
  360. tcpStatus CifsNeedReconnect if server hung */
  361. if (pdu_length < 4) {
  362. iov.iov_base = (4 - pdu_length) +
  363. (char *)smb_buffer;
  364. iov.iov_len = pdu_length;
  365. smb_msg.msg_control = NULL;
  366. smb_msg.msg_controllen = 0;
  367. goto incomplete_rcv;
  368. } else
  369. continue;
  370. } else if (length <= 0) {
  371. if (server->tcpStatus == CifsNew) {
  372. cFYI(1, "tcp session abend after SMBnegprot");
  373. /* some servers kill the TCP session rather than
  374. returning an SMB negprot error, in which
  375. case reconnecting here is not going to help,
  376. and so simply return error to mount */
  377. break;
  378. }
  379. if (!try_to_freeze() && (length == -EINTR)) {
  380. cFYI(1, "cifsd thread killed");
  381. break;
  382. }
  383. cFYI(1, "Reconnect after unexpected peek error %d",
  384. length);
  385. cifs_reconnect(server);
  386. csocket = server->ssocket;
  387. wake_up(&server->response_q);
  388. continue;
  389. } else if (length < pdu_length) {
  390. cFYI(1, "requested %d bytes but only got %d bytes",
  391. pdu_length, length);
  392. pdu_length -= length;
  393. msleep(1);
  394. goto incomplete_rcv;
  395. }
  396. /* The right amount was read from socket - 4 bytes */
  397. /* so we can now interpret the length field */
  398. /* the first byte big endian of the length field,
  399. is actually not part of the length but the type
  400. with the most common, zero, as regular data */
  401. temp = *((char *) smb_buffer);
  402. /* Note that FC 1001 length is big endian on the wire,
  403. but we convert it here so it is always manipulated
  404. as host byte order */
  405. pdu_length = be32_to_cpu((__force __be32)smb_buffer->smb_buf_length);
  406. smb_buffer->smb_buf_length = pdu_length;
  407. cFYI(1, "rfc1002 length 0x%x", pdu_length+4);
  408. if (temp == (char) RFC1002_SESSION_KEEP_ALIVE) {
  409. continue;
  410. } else if (temp == (char)RFC1002_POSITIVE_SESSION_RESPONSE) {
  411. cFYI(1, "Good RFC 1002 session rsp");
  412. continue;
  413. } else if (temp == (char)RFC1002_NEGATIVE_SESSION_RESPONSE) {
  414. /* we get this from Windows 98 instead of
  415. an error on SMB negprot response */
  416. cFYI(1, "Negative RFC1002 Session Response Error 0x%x)",
  417. pdu_length);
  418. if (server->tcpStatus == CifsNew) {
  419. /* if nack on negprot (rather than
  420. ret of smb negprot error) reconnecting
  421. not going to help, ret error to mount */
  422. break;
  423. } else {
  424. /* give server a second to
  425. clean up before reconnect attempt */
  426. msleep(1000);
  427. /* always try 445 first on reconnect
  428. since we get NACK on some if we ever
  429. connected to port 139 (the NACK is
  430. since we do not begin with RFC1001
  431. session initialize frame) */
  432. server->addr.sockAddr.sin_port =
  433. htons(CIFS_PORT);
  434. cifs_reconnect(server);
  435. csocket = server->ssocket;
  436. wake_up(&server->response_q);
  437. continue;
  438. }
  439. } else if (temp != (char) 0) {
  440. cERROR(1, "Unknown RFC 1002 frame");
  441. cifs_dump_mem(" Received Data: ", (char *)smb_buffer,
  442. length);
  443. cifs_reconnect(server);
  444. csocket = server->ssocket;
  445. continue;
  446. }
  447. /* else we have an SMB response */
  448. if ((pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) ||
  449. (pdu_length < sizeof(struct smb_hdr) - 1 - 4)) {
  450. cERROR(1, "Invalid size SMB length %d pdu_length %d",
  451. length, pdu_length+4);
  452. cifs_reconnect(server);
  453. csocket = server->ssocket;
  454. wake_up(&server->response_q);
  455. continue;
  456. }
  457. /* else length ok */
  458. reconnect = 0;
  459. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  460. isLargeBuf = true;
  461. memcpy(bigbuf, smallbuf, 4);
  462. smb_buffer = bigbuf;
  463. }
  464. length = 0;
  465. iov.iov_base = 4 + (char *)smb_buffer;
  466. iov.iov_len = pdu_length;
  467. for (total_read = 0; total_read < pdu_length;
  468. total_read += length) {
  469. length = kernel_recvmsg(csocket, &smb_msg, &iov, 1,
  470. pdu_length - total_read, 0);
  471. if ((server->tcpStatus == CifsExiting) ||
  472. (length == -EINTR)) {
  473. /* then will exit */
  474. reconnect = 2;
  475. break;
  476. } else if (server->tcpStatus == CifsNeedReconnect) {
  477. cifs_reconnect(server);
  478. csocket = server->ssocket;
  479. /* Reconnect wakes up rspns q */
  480. /* Now we will reread sock */
  481. reconnect = 1;
  482. break;
  483. } else if ((length == -ERESTARTSYS) ||
  484. (length == -EAGAIN)) {
  485. msleep(1); /* minimum sleep to prevent looping,
  486. allowing socket to clear and app
  487. threads to set tcpStatus
  488. CifsNeedReconnect if server hung*/
  489. length = 0;
  490. continue;
  491. } else if (length <= 0) {
  492. cERROR(1, "Received no data, expecting %d",
  493. pdu_length - total_read);
  494. cifs_reconnect(server);
  495. csocket = server->ssocket;
  496. reconnect = 1;
  497. break;
  498. }
  499. }
  500. if (reconnect == 2)
  501. break;
  502. else if (reconnect == 1)
  503. continue;
  504. length += 4; /* account for rfc1002 hdr */
  505. dump_smb(smb_buffer, length);
  506. if (checkSMB(smb_buffer, smb_buffer->Mid, total_read+4)) {
  507. cifs_dump_mem("Bad SMB: ", smb_buffer, 48);
  508. continue;
  509. }
  510. task_to_wake = NULL;
  511. spin_lock(&GlobalMid_Lock);
  512. list_for_each(tmp, &server->pending_mid_q) {
  513. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  514. if ((mid_entry->mid == smb_buffer->Mid) &&
  515. (mid_entry->midState == MID_REQUEST_SUBMITTED) &&
  516. (mid_entry->command == smb_buffer->Command)) {
  517. if (check2ndT2(smb_buffer,server->maxBuf) > 0) {
  518. /* We have a multipart transact2 resp */
  519. isMultiRsp = true;
  520. if (mid_entry->resp_buf) {
  521. /* merge response - fix up 1st*/
  522. if (coalesce_t2(smb_buffer,
  523. mid_entry->resp_buf)) {
  524. mid_entry->multiRsp =
  525. true;
  526. break;
  527. } else {
  528. /* all parts received */
  529. mid_entry->multiEnd =
  530. true;
  531. goto multi_t2_fnd;
  532. }
  533. } else {
  534. if (!isLargeBuf) {
  535. cERROR(1, "1st trans2 resp needs bigbuf");
  536. /* BB maybe we can fix this up, switch
  537. to already allocated large buffer? */
  538. } else {
  539. /* Have first buffer */
  540. mid_entry->resp_buf =
  541. smb_buffer;
  542. mid_entry->largeBuf =
  543. true;
  544. bigbuf = NULL;
  545. }
  546. }
  547. break;
  548. }
  549. mid_entry->resp_buf = smb_buffer;
  550. mid_entry->largeBuf = isLargeBuf;
  551. multi_t2_fnd:
  552. task_to_wake = mid_entry->tsk;
  553. mid_entry->midState = MID_RESPONSE_RECEIVED;
  554. #ifdef CONFIG_CIFS_STATS2
  555. mid_entry->when_received = jiffies;
  556. #endif
  557. /* so we do not time out requests to server
  558. which is still responding (since server could
  559. be busy but not dead) */
  560. server->lstrp = jiffies;
  561. break;
  562. }
  563. }
  564. spin_unlock(&GlobalMid_Lock);
  565. if (task_to_wake) {
  566. /* Was previous buf put in mpx struct for multi-rsp? */
  567. if (!isMultiRsp) {
  568. /* smb buffer will be freed by user thread */
  569. if (isLargeBuf)
  570. bigbuf = NULL;
  571. else
  572. smallbuf = NULL;
  573. }
  574. wake_up_process(task_to_wake);
  575. } else if (!is_valid_oplock_break(smb_buffer, server) &&
  576. !isMultiRsp) {
  577. cERROR(1, "No task to wake, unknown frame received! "
  578. "NumMids %d", midCount.counter);
  579. cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
  580. sizeof(struct smb_hdr));
  581. #ifdef CONFIG_CIFS_DEBUG2
  582. cifs_dump_detail(smb_buffer);
  583. cifs_dump_mids(server);
  584. #endif /* CIFS_DEBUG2 */
  585. }
  586. } /* end while !EXITING */
  587. /* take it off the list, if it's not already */
  588. write_lock(&cifs_tcp_ses_lock);
  589. list_del_init(&server->tcp_ses_list);
  590. write_unlock(&cifs_tcp_ses_lock);
  591. spin_lock(&GlobalMid_Lock);
  592. server->tcpStatus = CifsExiting;
  593. spin_unlock(&GlobalMid_Lock);
  594. wake_up_all(&server->response_q);
  595. /* check if we have blocked requests that need to free */
  596. /* Note that cifs_max_pending is normally 50, but
  597. can be set at module install time to as little as two */
  598. spin_lock(&GlobalMid_Lock);
  599. if (atomic_read(&server->inFlight) >= cifs_max_pending)
  600. atomic_set(&server->inFlight, cifs_max_pending - 1);
  601. /* We do not want to set the max_pending too low or we
  602. could end up with the counter going negative */
  603. spin_unlock(&GlobalMid_Lock);
  604. /* Although there should not be any requests blocked on
  605. this queue it can not hurt to be paranoid and try to wake up requests
  606. that may haven been blocked when more than 50 at time were on the wire
  607. to the same server - they now will see the session is in exit state
  608. and get out of SendReceive. */
  609. wake_up_all(&server->request_q);
  610. /* give those requests time to exit */
  611. msleep(125);
  612. if (server->ssocket) {
  613. sock_release(csocket);
  614. server->ssocket = NULL;
  615. }
  616. /* buffer usuallly freed in free_mid - need to free it here on exit */
  617. cifs_buf_release(bigbuf);
  618. if (smallbuf) /* no sense logging a debug message if NULL */
  619. cifs_small_buf_release(smallbuf);
  620. /*
  621. * BB: we shouldn't have to do any of this. It shouldn't be
  622. * possible to exit from the thread with active SMB sessions
  623. */
  624. read_lock(&cifs_tcp_ses_lock);
  625. if (list_empty(&server->pending_mid_q)) {
  626. /* loop through server session structures attached to this and
  627. mark them dead */
  628. list_for_each(tmp, &server->smb_ses_list) {
  629. ses = list_entry(tmp, struct cifsSesInfo,
  630. smb_ses_list);
  631. ses->status = CifsExiting;
  632. ses->server = NULL;
  633. }
  634. read_unlock(&cifs_tcp_ses_lock);
  635. } else {
  636. /* although we can not zero the server struct pointer yet,
  637. since there are active requests which may depnd on them,
  638. mark the corresponding SMB sessions as exiting too */
  639. list_for_each(tmp, &server->smb_ses_list) {
  640. ses = list_entry(tmp, struct cifsSesInfo,
  641. smb_ses_list);
  642. ses->status = CifsExiting;
  643. }
  644. spin_lock(&GlobalMid_Lock);
  645. list_for_each(tmp, &server->pending_mid_q) {
  646. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  647. if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
  648. cFYI(1, "Clearing Mid 0x%x - waking up ",
  649. mid_entry->mid);
  650. task_to_wake = mid_entry->tsk;
  651. if (task_to_wake)
  652. wake_up_process(task_to_wake);
  653. }
  654. }
  655. spin_unlock(&GlobalMid_Lock);
  656. read_unlock(&cifs_tcp_ses_lock);
  657. /* 1/8th of sec is more than enough time for them to exit */
  658. msleep(125);
  659. }
  660. if (!list_empty(&server->pending_mid_q)) {
  661. /* mpx threads have not exited yet give them
  662. at least the smb send timeout time for long ops */
  663. /* due to delays on oplock break requests, we need
  664. to wait at least 45 seconds before giving up
  665. on a request getting a response and going ahead
  666. and killing cifsd */
  667. cFYI(1, "Wait for exit from demultiplex thread");
  668. msleep(46000);
  669. /* if threads still have not exited they are probably never
  670. coming home not much else we can do but free the memory */
  671. }
  672. /* last chance to mark ses pointers invalid
  673. if there are any pointing to this (e.g
  674. if a crazy root user tried to kill cifsd
  675. kernel thread explicitly this might happen) */
  676. /* BB: This shouldn't be necessary, see above */
  677. read_lock(&cifs_tcp_ses_lock);
  678. list_for_each(tmp, &server->smb_ses_list) {
  679. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  680. ses->server = NULL;
  681. }
  682. read_unlock(&cifs_tcp_ses_lock);
  683. kfree(server->hostname);
  684. task_to_wake = xchg(&server->tsk, NULL);
  685. kfree(server);
  686. length = atomic_dec_return(&tcpSesAllocCount);
  687. if (length > 0)
  688. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  689. GFP_KERNEL);
  690. /* if server->tsk was NULL then wait for a signal before exiting */
  691. if (!task_to_wake) {
  692. set_current_state(TASK_INTERRUPTIBLE);
  693. while (!signal_pending(current)) {
  694. schedule();
  695. set_current_state(TASK_INTERRUPTIBLE);
  696. }
  697. set_current_state(TASK_RUNNING);
  698. }
  699. module_put_and_exit(0);
  700. }
  701. /* extract the host portion of the UNC string */
  702. static char *
  703. extract_hostname(const char *unc)
  704. {
  705. const char *src;
  706. char *dst, *delim;
  707. unsigned int len;
  708. /* skip double chars at beginning of string */
  709. /* BB: check validity of these bytes? */
  710. src = unc + 2;
  711. /* delimiter between hostname and sharename is always '\\' now */
  712. delim = strchr(src, '\\');
  713. if (!delim)
  714. return ERR_PTR(-EINVAL);
  715. len = delim - src;
  716. dst = kmalloc((len + 1), GFP_KERNEL);
  717. if (dst == NULL)
  718. return ERR_PTR(-ENOMEM);
  719. memcpy(dst, src, len);
  720. dst[len] = '\0';
  721. return dst;
  722. }
  723. static int
  724. cifs_parse_mount_options(char *options, const char *devname,
  725. struct smb_vol *vol)
  726. {
  727. char *value;
  728. char *data;
  729. unsigned int temp_len, i, j;
  730. char separator[2];
  731. short int override_uid = -1;
  732. short int override_gid = -1;
  733. bool uid_specified = false;
  734. bool gid_specified = false;
  735. separator[0] = ',';
  736. separator[1] = 0;
  737. if (Local_System_Name[0] != 0)
  738. memcpy(vol->source_rfc1001_name, Local_System_Name, 15);
  739. else {
  740. char *nodename = utsname()->nodename;
  741. int n = strnlen(nodename, 15);
  742. memset(vol->source_rfc1001_name, 0x20, 15);
  743. for (i = 0; i < n; i++) {
  744. /* does not have to be perfect mapping since field is
  745. informational, only used for servers that do not support
  746. port 445 and it can be overridden at mount time */
  747. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  748. }
  749. }
  750. vol->source_rfc1001_name[15] = 0;
  751. /* null target name indicates to use *SMBSERVR default called name
  752. if we end up sending RFC1001 session initialize */
  753. vol->target_rfc1001_name[0] = 0;
  754. vol->linux_uid = current_uid(); /* use current_euid() instead? */
  755. vol->linux_gid = current_gid();
  756. /* default to only allowing write access to owner of the mount */
  757. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  758. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  759. /* default is always to request posix paths. */
  760. vol->posix_paths = 1;
  761. /* default to using server inode numbers where available */
  762. vol->server_ino = 1;
  763. if (!options)
  764. return 1;
  765. if (strncmp(options, "sep=", 4) == 0) {
  766. if (options[4] != 0) {
  767. separator[0] = options[4];
  768. options += 5;
  769. } else {
  770. cFYI(1, "Null separator not allowed");
  771. }
  772. }
  773. while ((data = strsep(&options, separator)) != NULL) {
  774. if (!*data)
  775. continue;
  776. if ((value = strchr(data, '=')) != NULL)
  777. *value++ = '\0';
  778. /* Have to parse this before we parse for "user" */
  779. if (strnicmp(data, "user_xattr", 10) == 0) {
  780. vol->no_xattr = 0;
  781. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  782. vol->no_xattr = 1;
  783. } else if (strnicmp(data, "user", 4) == 0) {
  784. if (!value) {
  785. printk(KERN_WARNING
  786. "CIFS: invalid or missing username\n");
  787. return 1; /* needs_arg; */
  788. } else if (!*value) {
  789. /* null user, ie anonymous, authentication */
  790. vol->nullauth = 1;
  791. }
  792. if (strnlen(value, 200) < 200) {
  793. vol->username = value;
  794. } else {
  795. printk(KERN_WARNING "CIFS: username too long\n");
  796. return 1;
  797. }
  798. } else if (strnicmp(data, "pass", 4) == 0) {
  799. if (!value) {
  800. vol->password = NULL;
  801. continue;
  802. } else if (value[0] == 0) {
  803. /* check if string begins with double comma
  804. since that would mean the password really
  805. does start with a comma, and would not
  806. indicate an empty string */
  807. if (value[1] != separator[0]) {
  808. vol->password = NULL;
  809. continue;
  810. }
  811. }
  812. temp_len = strlen(value);
  813. /* removed password length check, NTLM passwords
  814. can be arbitrarily long */
  815. /* if comma in password, the string will be
  816. prematurely null terminated. Commas in password are
  817. specified across the cifs mount interface by a double
  818. comma ie ,, and a comma used as in other cases ie ','
  819. as a parameter delimiter/separator is single and due
  820. to the strsep above is temporarily zeroed. */
  821. /* NB: password legally can have multiple commas and
  822. the only illegal character in a password is null */
  823. if ((value[temp_len] == 0) &&
  824. (value[temp_len+1] == separator[0])) {
  825. /* reinsert comma */
  826. value[temp_len] = separator[0];
  827. temp_len += 2; /* move after second comma */
  828. while (value[temp_len] != 0) {
  829. if (value[temp_len] == separator[0]) {
  830. if (value[temp_len+1] ==
  831. separator[0]) {
  832. /* skip second comma */
  833. temp_len++;
  834. } else {
  835. /* single comma indicating start
  836. of next parm */
  837. break;
  838. }
  839. }
  840. temp_len++;
  841. }
  842. if (value[temp_len] == 0) {
  843. options = NULL;
  844. } else {
  845. value[temp_len] = 0;
  846. /* point option to start of next parm */
  847. options = value + temp_len + 1;
  848. }
  849. /* go from value to value + temp_len condensing
  850. double commas to singles. Note that this ends up
  851. allocating a few bytes too many, which is ok */
  852. vol->password = kzalloc(temp_len, GFP_KERNEL);
  853. if (vol->password == NULL) {
  854. printk(KERN_WARNING "CIFS: no memory "
  855. "for password\n");
  856. return 1;
  857. }
  858. for (i = 0, j = 0; i < temp_len; i++, j++) {
  859. vol->password[j] = value[i];
  860. if (value[i] == separator[0]
  861. && value[i+1] == separator[0]) {
  862. /* skip second comma */
  863. i++;
  864. }
  865. }
  866. vol->password[j] = 0;
  867. } else {
  868. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  869. if (vol->password == NULL) {
  870. printk(KERN_WARNING "CIFS: no memory "
  871. "for password\n");
  872. return 1;
  873. }
  874. strcpy(vol->password, value);
  875. }
  876. } else if (!strnicmp(data, "ip", 2) ||
  877. !strnicmp(data, "addr", 4)) {
  878. if (!value || !*value) {
  879. vol->UNCip = NULL;
  880. } else if (strnlen(value, INET6_ADDRSTRLEN) <
  881. INET6_ADDRSTRLEN) {
  882. vol->UNCip = value;
  883. } else {
  884. printk(KERN_WARNING "CIFS: ip address "
  885. "too long\n");
  886. return 1;
  887. }
  888. } else if (strnicmp(data, "sec", 3) == 0) {
  889. if (!value || !*value) {
  890. cERROR(1, "no security value specified");
  891. continue;
  892. } else if (strnicmp(value, "krb5i", 5) == 0) {
  893. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  894. CIFSSEC_MUST_SIGN;
  895. } else if (strnicmp(value, "krb5p", 5) == 0) {
  896. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  897. CIFSSEC_MAY_KRB5; */
  898. cERROR(1, "Krb5 cifs privacy not supported");
  899. return 1;
  900. } else if (strnicmp(value, "krb5", 4) == 0) {
  901. vol->secFlg |= CIFSSEC_MAY_KRB5;
  902. #ifdef CONFIG_CIFS_EXPERIMENTAL
  903. } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
  904. vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
  905. CIFSSEC_MUST_SIGN;
  906. } else if (strnicmp(value, "ntlmssp", 7) == 0) {
  907. vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
  908. #endif
  909. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  910. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  911. CIFSSEC_MUST_SIGN;
  912. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  913. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  914. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  915. vol->secFlg |= CIFSSEC_MAY_NTLM |
  916. CIFSSEC_MUST_SIGN;
  917. } else if (strnicmp(value, "ntlm", 4) == 0) {
  918. /* ntlm is default so can be turned off too */
  919. vol->secFlg |= CIFSSEC_MAY_NTLM;
  920. } else if (strnicmp(value, "nontlm", 6) == 0) {
  921. /* BB is there a better way to do this? */
  922. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  923. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  924. } else if (strnicmp(value, "lanman", 6) == 0) {
  925. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  926. #endif
  927. } else if (strnicmp(value, "none", 4) == 0) {
  928. vol->nullauth = 1;
  929. } else {
  930. cERROR(1, "bad security option: %s", value);
  931. return 1;
  932. }
  933. } else if ((strnicmp(data, "unc", 3) == 0)
  934. || (strnicmp(data, "target", 6) == 0)
  935. || (strnicmp(data, "path", 4) == 0)) {
  936. if (!value || !*value) {
  937. printk(KERN_WARNING "CIFS: invalid path to "
  938. "network resource\n");
  939. return 1; /* needs_arg; */
  940. }
  941. if ((temp_len = strnlen(value, 300)) < 300) {
  942. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  943. if (vol->UNC == NULL)
  944. return 1;
  945. strcpy(vol->UNC, value);
  946. if (strncmp(vol->UNC, "//", 2) == 0) {
  947. vol->UNC[0] = '\\';
  948. vol->UNC[1] = '\\';
  949. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  950. printk(KERN_WARNING
  951. "CIFS: UNC Path does not begin "
  952. "with // or \\\\ \n");
  953. return 1;
  954. }
  955. } else {
  956. printk(KERN_WARNING "CIFS: UNC name too long\n");
  957. return 1;
  958. }
  959. } else if ((strnicmp(data, "domain", 3) == 0)
  960. || (strnicmp(data, "workgroup", 5) == 0)) {
  961. if (!value || !*value) {
  962. printk(KERN_WARNING "CIFS: invalid domain name\n");
  963. return 1; /* needs_arg; */
  964. }
  965. /* BB are there cases in which a comma can be valid in
  966. a domain name and need special handling? */
  967. if (strnlen(value, 256) < 256) {
  968. vol->domainname = value;
  969. cFYI(1, "Domain name set");
  970. } else {
  971. printk(KERN_WARNING "CIFS: domain name too "
  972. "long\n");
  973. return 1;
  974. }
  975. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  976. if (!value || !*value) {
  977. printk(KERN_WARNING
  978. "CIFS: invalid path prefix\n");
  979. return 1; /* needs_argument */
  980. }
  981. if ((temp_len = strnlen(value, 1024)) < 1024) {
  982. if (value[0] != '/')
  983. temp_len++; /* missing leading slash */
  984. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  985. if (vol->prepath == NULL)
  986. return 1;
  987. if (value[0] != '/') {
  988. vol->prepath[0] = '/';
  989. strcpy(vol->prepath+1, value);
  990. } else
  991. strcpy(vol->prepath, value);
  992. cFYI(1, "prefix path %s", vol->prepath);
  993. } else {
  994. printk(KERN_WARNING "CIFS: prefix too long\n");
  995. return 1;
  996. }
  997. } else if (strnicmp(data, "iocharset", 9) == 0) {
  998. if (!value || !*value) {
  999. printk(KERN_WARNING "CIFS: invalid iocharset "
  1000. "specified\n");
  1001. return 1; /* needs_arg; */
  1002. }
  1003. if (strnlen(value, 65) < 65) {
  1004. if (strnicmp(value, "default", 7))
  1005. vol->iocharset = value;
  1006. /* if iocharset not set then load_nls_default
  1007. is used by caller */
  1008. cFYI(1, "iocharset set to %s", value);
  1009. } else {
  1010. printk(KERN_WARNING "CIFS: iocharset name "
  1011. "too long.\n");
  1012. return 1;
  1013. }
  1014. } else if (!strnicmp(data, "uid", 3) && value && *value) {
  1015. vol->linux_uid = simple_strtoul(value, &value, 0);
  1016. uid_specified = true;
  1017. } else if (!strnicmp(data, "forceuid", 8)) {
  1018. override_uid = 1;
  1019. } else if (!strnicmp(data, "noforceuid", 10)) {
  1020. override_uid = 0;
  1021. } else if (!strnicmp(data, "gid", 3) && value && *value) {
  1022. vol->linux_gid = simple_strtoul(value, &value, 0);
  1023. gid_specified = true;
  1024. } else if (!strnicmp(data, "forcegid", 8)) {
  1025. override_gid = 1;
  1026. } else if (!strnicmp(data, "noforcegid", 10)) {
  1027. override_gid = 0;
  1028. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1029. if (value && *value) {
  1030. vol->file_mode =
  1031. simple_strtoul(value, &value, 0);
  1032. }
  1033. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1034. if (value && *value) {
  1035. vol->dir_mode =
  1036. simple_strtoul(value, &value, 0);
  1037. }
  1038. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1039. if (value && *value) {
  1040. vol->dir_mode =
  1041. simple_strtoul(value, &value, 0);
  1042. }
  1043. } else if (strnicmp(data, "port", 4) == 0) {
  1044. if (value && *value) {
  1045. vol->port =
  1046. simple_strtoul(value, &value, 0);
  1047. }
  1048. } else if (strnicmp(data, "rsize", 5) == 0) {
  1049. if (value && *value) {
  1050. vol->rsize =
  1051. simple_strtoul(value, &value, 0);
  1052. }
  1053. } else if (strnicmp(data, "wsize", 5) == 0) {
  1054. if (value && *value) {
  1055. vol->wsize =
  1056. simple_strtoul(value, &value, 0);
  1057. }
  1058. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1059. if (!value || !*value) {
  1060. cERROR(1, "no socket option specified");
  1061. continue;
  1062. } else if (strnicmp(value, "TCP_NODELAY", 11) == 0) {
  1063. vol->sockopt_tcp_nodelay = 1;
  1064. }
  1065. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1066. if (!value || !*value || (*value == ' ')) {
  1067. cFYI(1, "invalid (empty) netbiosname");
  1068. } else {
  1069. memset(vol->source_rfc1001_name, 0x20, 15);
  1070. for (i = 0; i < 15; i++) {
  1071. /* BB are there cases in which a comma can be
  1072. valid in this workstation netbios name (and need
  1073. special handling)? */
  1074. /* We do not uppercase netbiosname for user */
  1075. if (value[i] == 0)
  1076. break;
  1077. else
  1078. vol->source_rfc1001_name[i] =
  1079. value[i];
  1080. }
  1081. /* The string has 16th byte zero still from
  1082. set at top of the function */
  1083. if ((i == 15) && (value[i] != 0))
  1084. printk(KERN_WARNING "CIFS: netbiosname"
  1085. " longer than 15 truncated.\n");
  1086. }
  1087. } else if (strnicmp(data, "servern", 7) == 0) {
  1088. /* servernetbiosname specified override *SMBSERVER */
  1089. if (!value || !*value || (*value == ' ')) {
  1090. cFYI(1, "empty server netbiosname specified");
  1091. } else {
  1092. /* last byte, type, is 0x20 for servr type */
  1093. memset(vol->target_rfc1001_name, 0x20, 16);
  1094. for (i = 0; i < 15; i++) {
  1095. /* BB are there cases in which a comma can be
  1096. valid in this workstation netbios name
  1097. (and need special handling)? */
  1098. /* user or mount helper must uppercase
  1099. the netbiosname */
  1100. if (value[i] == 0)
  1101. break;
  1102. else
  1103. vol->target_rfc1001_name[i] =
  1104. value[i];
  1105. }
  1106. /* The string has 16th byte zero still from
  1107. set at top of the function */
  1108. if ((i == 15) && (value[i] != 0))
  1109. printk(KERN_WARNING "CIFS: server net"
  1110. "biosname longer than 15 truncated.\n");
  1111. }
  1112. } else if (strnicmp(data, "credentials", 4) == 0) {
  1113. /* ignore */
  1114. } else if (strnicmp(data, "version", 3) == 0) {
  1115. /* ignore */
  1116. } else if (strnicmp(data, "guest", 5) == 0) {
  1117. /* ignore */
  1118. } else if (strnicmp(data, "rw", 2) == 0) {
  1119. /* ignore */
  1120. } else if (strnicmp(data, "ro", 2) == 0) {
  1121. /* ignore */
  1122. } else if (strnicmp(data, "noblocksend", 11) == 0) {
  1123. vol->noblocksnd = 1;
  1124. } else if (strnicmp(data, "noautotune", 10) == 0) {
  1125. vol->noautotune = 1;
  1126. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1127. (strnicmp(data, "nosuid", 6) == 0) ||
  1128. (strnicmp(data, "exec", 4) == 0) ||
  1129. (strnicmp(data, "noexec", 6) == 0) ||
  1130. (strnicmp(data, "nodev", 5) == 0) ||
  1131. (strnicmp(data, "noauto", 6) == 0) ||
  1132. (strnicmp(data, "dev", 3) == 0)) {
  1133. /* The mount tool or mount.cifs helper (if present)
  1134. uses these opts to set flags, and the flags are read
  1135. by the kernel vfs layer before we get here (ie
  1136. before read super) so there is no point trying to
  1137. parse these options again and set anything and it
  1138. is ok to just ignore them */
  1139. continue;
  1140. } else if (strnicmp(data, "hard", 4) == 0) {
  1141. vol->retry = 1;
  1142. } else if (strnicmp(data, "soft", 4) == 0) {
  1143. vol->retry = 0;
  1144. } else if (strnicmp(data, "perm", 4) == 0) {
  1145. vol->noperm = 0;
  1146. } else if (strnicmp(data, "noperm", 6) == 0) {
  1147. vol->noperm = 1;
  1148. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1149. vol->remap = 1;
  1150. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1151. vol->remap = 0;
  1152. } else if (strnicmp(data, "sfu", 3) == 0) {
  1153. vol->sfu_emul = 1;
  1154. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1155. vol->sfu_emul = 0;
  1156. } else if (strnicmp(data, "nodfs", 5) == 0) {
  1157. vol->nodfs = 1;
  1158. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1159. vol->posix_paths = 1;
  1160. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1161. vol->posix_paths = 0;
  1162. } else if (strnicmp(data, "nounix", 6) == 0) {
  1163. vol->no_linux_ext = 1;
  1164. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1165. vol->no_linux_ext = 1;
  1166. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1167. (strnicmp(data, "ignorecase", 10) == 0)) {
  1168. vol->nocase = 1;
  1169. } else if (strnicmp(data, "brl", 3) == 0) {
  1170. vol->nobrl = 0;
  1171. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1172. (strnicmp(data, "nolock", 6) == 0)) {
  1173. vol->nobrl = 1;
  1174. /* turn off mandatory locking in mode
  1175. if remote locking is turned off since the
  1176. local vfs will do advisory */
  1177. if (vol->file_mode ==
  1178. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1179. vol->file_mode = S_IALLUGO;
  1180. } else if (strnicmp(data, "forcemandatorylock", 9) == 0) {
  1181. /* will take the shorter form "forcemand" as well */
  1182. /* This mount option will force use of mandatory
  1183. (DOS/Windows style) byte range locks, instead of
  1184. using posix advisory byte range locks, even if the
  1185. Unix extensions are available and posix locks would
  1186. be supported otherwise. If Unix extensions are not
  1187. negotiated this has no effect since mandatory locks
  1188. would be used (mandatory locks is all that those
  1189. those servers support) */
  1190. vol->mand_lock = 1;
  1191. } else if (strnicmp(data, "setuids", 7) == 0) {
  1192. vol->setuids = 1;
  1193. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1194. vol->setuids = 0;
  1195. } else if (strnicmp(data, "dynperm", 7) == 0) {
  1196. vol->dynperm = true;
  1197. } else if (strnicmp(data, "nodynperm", 9) == 0) {
  1198. vol->dynperm = false;
  1199. } else if (strnicmp(data, "nohard", 6) == 0) {
  1200. vol->retry = 0;
  1201. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1202. vol->retry = 1;
  1203. } else if (strnicmp(data, "nointr", 6) == 0) {
  1204. vol->intr = 0;
  1205. } else if (strnicmp(data, "intr", 4) == 0) {
  1206. vol->intr = 1;
  1207. } else if (strnicmp(data, "nostrictsync", 12) == 0) {
  1208. vol->nostrictsync = 1;
  1209. } else if (strnicmp(data, "strictsync", 10) == 0) {
  1210. vol->nostrictsync = 0;
  1211. } else if (strnicmp(data, "serverino", 7) == 0) {
  1212. vol->server_ino = 1;
  1213. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1214. vol->server_ino = 0;
  1215. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1216. vol->cifs_acl = 1;
  1217. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1218. vol->cifs_acl = 0;
  1219. } else if (strnicmp(data, "acl", 3) == 0) {
  1220. vol->no_psx_acl = 0;
  1221. } else if (strnicmp(data, "noacl", 5) == 0) {
  1222. vol->no_psx_acl = 1;
  1223. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1224. } else if (strnicmp(data, "locallease", 6) == 0) {
  1225. vol->local_lease = 1;
  1226. #endif
  1227. } else if (strnicmp(data, "sign", 4) == 0) {
  1228. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1229. } else if (strnicmp(data, "seal", 4) == 0) {
  1230. /* we do not do the following in secFlags because seal
  1231. is a per tree connection (mount) not a per socket
  1232. or per-smb connection option in the protocol */
  1233. /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1234. vol->seal = 1;
  1235. } else if (strnicmp(data, "direct", 6) == 0) {
  1236. vol->direct_io = 1;
  1237. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1238. vol->direct_io = 1;
  1239. } else if (strnicmp(data, "noac", 4) == 0) {
  1240. printk(KERN_WARNING "CIFS: Mount option noac not "
  1241. "supported. Instead set "
  1242. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1243. } else
  1244. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1245. data);
  1246. }
  1247. if (vol->UNC == NULL) {
  1248. if (devname == NULL) {
  1249. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1250. "target\n");
  1251. return 1;
  1252. }
  1253. if ((temp_len = strnlen(devname, 300)) < 300) {
  1254. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1255. if (vol->UNC == NULL)
  1256. return 1;
  1257. strcpy(vol->UNC, devname);
  1258. if (strncmp(vol->UNC, "//", 2) == 0) {
  1259. vol->UNC[0] = '\\';
  1260. vol->UNC[1] = '\\';
  1261. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1262. printk(KERN_WARNING "CIFS: UNC Path does not "
  1263. "begin with // or \\\\ \n");
  1264. return 1;
  1265. }
  1266. value = strpbrk(vol->UNC+2, "/\\");
  1267. if (value)
  1268. *value = '\\';
  1269. } else {
  1270. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1271. return 1;
  1272. }
  1273. }
  1274. if (vol->UNCip == NULL)
  1275. vol->UNCip = &vol->UNC[2];
  1276. if (uid_specified)
  1277. vol->override_uid = override_uid;
  1278. else if (override_uid == 1)
  1279. printk(KERN_NOTICE "CIFS: ignoring forceuid mount option "
  1280. "specified with no uid= option.\n");
  1281. if (gid_specified)
  1282. vol->override_gid = override_gid;
  1283. else if (override_gid == 1)
  1284. printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
  1285. "specified with no gid= option.\n");
  1286. return 0;
  1287. }
  1288. static struct TCP_Server_Info *
  1289. cifs_find_tcp_session(struct sockaddr_storage *addr, unsigned short int port)
  1290. {
  1291. struct list_head *tmp;
  1292. struct TCP_Server_Info *server;
  1293. struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
  1294. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
  1295. write_lock(&cifs_tcp_ses_lock);
  1296. list_for_each(tmp, &cifs_tcp_ses_list) {
  1297. server = list_entry(tmp, struct TCP_Server_Info,
  1298. tcp_ses_list);
  1299. /*
  1300. * the demux thread can exit on its own while still in CifsNew
  1301. * so don't accept any sockets in that state. Since the
  1302. * tcpStatus never changes back to CifsNew it's safe to check
  1303. * for this without a lock.
  1304. */
  1305. if (server->tcpStatus == CifsNew)
  1306. continue;
  1307. switch (addr->ss_family) {
  1308. case AF_INET:
  1309. if (addr4->sin_addr.s_addr ==
  1310. server->addr.sockAddr.sin_addr.s_addr) {
  1311. addr4->sin_port = htons(port);
  1312. /* user overrode default port? */
  1313. if (addr4->sin_port) {
  1314. if (addr4->sin_port !=
  1315. server->addr.sockAddr.sin_port)
  1316. continue;
  1317. }
  1318. break;
  1319. } else
  1320. continue;
  1321. case AF_INET6:
  1322. if (ipv6_addr_equal(&addr6->sin6_addr,
  1323. &server->addr.sockAddr6.sin6_addr) &&
  1324. (addr6->sin6_scope_id ==
  1325. server->addr.sockAddr6.sin6_scope_id)) {
  1326. addr6->sin6_port = htons(port);
  1327. /* user overrode default port? */
  1328. if (addr6->sin6_port) {
  1329. if (addr6->sin6_port !=
  1330. server->addr.sockAddr6.sin6_port)
  1331. continue;
  1332. }
  1333. break;
  1334. } else
  1335. continue;
  1336. }
  1337. ++server->srv_count;
  1338. write_unlock(&cifs_tcp_ses_lock);
  1339. cFYI(1, "Existing tcp session with server found");
  1340. return server;
  1341. }
  1342. write_unlock(&cifs_tcp_ses_lock);
  1343. return NULL;
  1344. }
  1345. static void
  1346. cifs_put_tcp_session(struct TCP_Server_Info *server)
  1347. {
  1348. struct task_struct *task;
  1349. write_lock(&cifs_tcp_ses_lock);
  1350. if (--server->srv_count > 0) {
  1351. write_unlock(&cifs_tcp_ses_lock);
  1352. return;
  1353. }
  1354. list_del_init(&server->tcp_ses_list);
  1355. write_unlock(&cifs_tcp_ses_lock);
  1356. spin_lock(&GlobalMid_Lock);
  1357. server->tcpStatus = CifsExiting;
  1358. spin_unlock(&GlobalMid_Lock);
  1359. task = xchg(&server->tsk, NULL);
  1360. if (task)
  1361. force_sig(SIGKILL, task);
  1362. }
  1363. static struct TCP_Server_Info *
  1364. cifs_get_tcp_session(struct smb_vol *volume_info)
  1365. {
  1366. struct TCP_Server_Info *tcp_ses = NULL;
  1367. struct sockaddr_storage addr;
  1368. struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
  1369. struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
  1370. int rc;
  1371. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1372. cFYI(1, "UNC: %s ip: %s", volume_info->UNC, volume_info->UNCip);
  1373. if (volume_info->UNCip && volume_info->UNC) {
  1374. rc = cifs_convert_address(volume_info->UNCip, &addr);
  1375. if (!rc) {
  1376. /* we failed translating address */
  1377. rc = -EINVAL;
  1378. goto out_err;
  1379. }
  1380. } else if (volume_info->UNCip) {
  1381. /* BB using ip addr as tcp_ses name to connect to the
  1382. DFS root below */
  1383. cERROR(1, "Connecting to DFS root not implemented yet");
  1384. rc = -EINVAL;
  1385. goto out_err;
  1386. } else /* which tcp_sess DFS root would we conect to */ {
  1387. cERROR(1, "CIFS mount error: No UNC path (e.g. -o "
  1388. "unc=//192.168.1.100/public) specified");
  1389. rc = -EINVAL;
  1390. goto out_err;
  1391. }
  1392. /* see if we already have a matching tcp_ses */
  1393. tcp_ses = cifs_find_tcp_session(&addr, volume_info->port);
  1394. if (tcp_ses)
  1395. return tcp_ses;
  1396. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1397. if (!tcp_ses) {
  1398. rc = -ENOMEM;
  1399. goto out_err;
  1400. }
  1401. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  1402. if (IS_ERR(tcp_ses->hostname)) {
  1403. rc = PTR_ERR(tcp_ses->hostname);
  1404. goto out_err;
  1405. }
  1406. tcp_ses->noblocksnd = volume_info->noblocksnd;
  1407. tcp_ses->noautotune = volume_info->noautotune;
  1408. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  1409. atomic_set(&tcp_ses->inFlight, 0);
  1410. init_waitqueue_head(&tcp_ses->response_q);
  1411. init_waitqueue_head(&tcp_ses->request_q);
  1412. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1413. mutex_init(&tcp_ses->srv_mutex);
  1414. memcpy(tcp_ses->workstation_RFC1001_name,
  1415. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1416. memcpy(tcp_ses->server_RFC1001_name,
  1417. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1418. tcp_ses->sequence_number = 0;
  1419. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1420. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1421. /*
  1422. * at this point we are the only ones with the pointer
  1423. * to the struct since the kernel thread not created yet
  1424. * no need to spinlock this init of tcpStatus or srv_count
  1425. */
  1426. tcp_ses->tcpStatus = CifsNew;
  1427. ++tcp_ses->srv_count;
  1428. if (addr.ss_family == AF_INET6) {
  1429. cFYI(1, "attempting ipv6 connect");
  1430. /* BB should we allow ipv6 on port 139? */
  1431. /* other OS never observed in Wild doing 139 with v6 */
  1432. sin_server6->sin6_port = htons(volume_info->port);
  1433. memcpy(&tcp_ses->addr.sockAddr6, sin_server6,
  1434. sizeof(struct sockaddr_in6));
  1435. rc = ipv6_connect(tcp_ses);
  1436. } else {
  1437. sin_server->sin_port = htons(volume_info->port);
  1438. memcpy(&tcp_ses->addr.sockAddr, sin_server,
  1439. sizeof(struct sockaddr_in));
  1440. rc = ipv4_connect(tcp_ses);
  1441. }
  1442. if (rc < 0) {
  1443. cERROR(1, "Error connecting to socket. Aborting operation");
  1444. goto out_err;
  1445. }
  1446. /*
  1447. * since we're in a cifs function already, we know that
  1448. * this will succeed. No need for try_module_get().
  1449. */
  1450. __module_get(THIS_MODULE);
  1451. tcp_ses->tsk = kthread_run((void *)(void *)cifs_demultiplex_thread,
  1452. tcp_ses, "cifsd");
  1453. if (IS_ERR(tcp_ses->tsk)) {
  1454. rc = PTR_ERR(tcp_ses->tsk);
  1455. cERROR(1, "error %d create cifsd thread", rc);
  1456. module_put(THIS_MODULE);
  1457. goto out_err;
  1458. }
  1459. /* thread spawned, put it on the list */
  1460. write_lock(&cifs_tcp_ses_lock);
  1461. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1462. write_unlock(&cifs_tcp_ses_lock);
  1463. return tcp_ses;
  1464. out_err:
  1465. if (tcp_ses) {
  1466. if (!IS_ERR(tcp_ses->hostname))
  1467. kfree(tcp_ses->hostname);
  1468. if (tcp_ses->ssocket)
  1469. sock_release(tcp_ses->ssocket);
  1470. kfree(tcp_ses);
  1471. }
  1472. return ERR_PTR(rc);
  1473. }
  1474. static struct cifsSesInfo *
  1475. cifs_find_smb_ses(struct TCP_Server_Info *server, char *username)
  1476. {
  1477. struct list_head *tmp;
  1478. struct cifsSesInfo *ses;
  1479. write_lock(&cifs_tcp_ses_lock);
  1480. list_for_each(tmp, &server->smb_ses_list) {
  1481. ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
  1482. if (strncmp(ses->userName, username, MAX_USERNAME_SIZE))
  1483. continue;
  1484. ++ses->ses_count;
  1485. write_unlock(&cifs_tcp_ses_lock);
  1486. return ses;
  1487. }
  1488. write_unlock(&cifs_tcp_ses_lock);
  1489. return NULL;
  1490. }
  1491. static void
  1492. cifs_put_smb_ses(struct cifsSesInfo *ses)
  1493. {
  1494. int xid;
  1495. struct TCP_Server_Info *server = ses->server;
  1496. cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count);
  1497. write_lock(&cifs_tcp_ses_lock);
  1498. if (--ses->ses_count > 0) {
  1499. write_unlock(&cifs_tcp_ses_lock);
  1500. return;
  1501. }
  1502. list_del_init(&ses->smb_ses_list);
  1503. write_unlock(&cifs_tcp_ses_lock);
  1504. if (ses->status == CifsGood) {
  1505. xid = GetXid();
  1506. CIFSSMBLogoff(xid, ses);
  1507. _FreeXid(xid);
  1508. }
  1509. sesInfoFree(ses);
  1510. cifs_put_tcp_session(server);
  1511. }
  1512. static struct cifsSesInfo *
  1513. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  1514. {
  1515. int rc = -ENOMEM, xid;
  1516. struct cifsSesInfo *ses;
  1517. xid = GetXid();
  1518. ses = cifs_find_smb_ses(server, volume_info->username);
  1519. if (ses) {
  1520. cFYI(1, "Existing smb sess found (status=%d)", ses->status);
  1521. /* existing SMB ses has a server reference already */
  1522. cifs_put_tcp_session(server);
  1523. mutex_lock(&ses->session_mutex);
  1524. rc = cifs_negotiate_protocol(xid, ses);
  1525. if (rc) {
  1526. mutex_unlock(&ses->session_mutex);
  1527. /* problem -- put our ses reference */
  1528. cifs_put_smb_ses(ses);
  1529. FreeXid(xid);
  1530. return ERR_PTR(rc);
  1531. }
  1532. if (ses->need_reconnect) {
  1533. cFYI(1, "Session needs reconnect");
  1534. rc = cifs_setup_session(xid, ses,
  1535. volume_info->local_nls);
  1536. if (rc) {
  1537. mutex_unlock(&ses->session_mutex);
  1538. /* problem -- put our reference */
  1539. cifs_put_smb_ses(ses);
  1540. FreeXid(xid);
  1541. return ERR_PTR(rc);
  1542. }
  1543. }
  1544. mutex_unlock(&ses->session_mutex);
  1545. FreeXid(xid);
  1546. return ses;
  1547. }
  1548. cFYI(1, "Existing smb sess not found");
  1549. ses = sesInfoAlloc();
  1550. if (ses == NULL)
  1551. goto get_ses_fail;
  1552. /* new SMB session uses our server ref */
  1553. ses->server = server;
  1554. if (server->addr.sockAddr6.sin6_family == AF_INET6)
  1555. sprintf(ses->serverName, "%pI6",
  1556. &server->addr.sockAddr6.sin6_addr);
  1557. else
  1558. sprintf(ses->serverName, "%pI4",
  1559. &server->addr.sockAddr.sin_addr.s_addr);
  1560. if (volume_info->username)
  1561. strncpy(ses->userName, volume_info->username,
  1562. MAX_USERNAME_SIZE);
  1563. /* volume_info->password freed at unmount */
  1564. if (volume_info->password) {
  1565. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  1566. if (!ses->password)
  1567. goto get_ses_fail;
  1568. }
  1569. if (volume_info->domainname) {
  1570. int len = strlen(volume_info->domainname);
  1571. ses->domainName = kmalloc(len + 1, GFP_KERNEL);
  1572. if (ses->domainName)
  1573. strcpy(ses->domainName, volume_info->domainname);
  1574. }
  1575. ses->linux_uid = volume_info->linux_uid;
  1576. ses->overrideSecFlg = volume_info->secFlg;
  1577. mutex_lock(&ses->session_mutex);
  1578. rc = cifs_negotiate_protocol(xid, ses);
  1579. if (!rc)
  1580. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  1581. mutex_unlock(&ses->session_mutex);
  1582. if (rc)
  1583. goto get_ses_fail;
  1584. /* success, put it on the list */
  1585. write_lock(&cifs_tcp_ses_lock);
  1586. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  1587. write_unlock(&cifs_tcp_ses_lock);
  1588. FreeXid(xid);
  1589. return ses;
  1590. get_ses_fail:
  1591. sesInfoFree(ses);
  1592. FreeXid(xid);
  1593. return ERR_PTR(rc);
  1594. }
  1595. static struct cifsTconInfo *
  1596. cifs_find_tcon(struct cifsSesInfo *ses, const char *unc)
  1597. {
  1598. struct list_head *tmp;
  1599. struct cifsTconInfo *tcon;
  1600. write_lock(&cifs_tcp_ses_lock);
  1601. list_for_each(tmp, &ses->tcon_list) {
  1602. tcon = list_entry(tmp, struct cifsTconInfo, tcon_list);
  1603. if (tcon->tidStatus == CifsExiting)
  1604. continue;
  1605. if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
  1606. continue;
  1607. ++tcon->tc_count;
  1608. write_unlock(&cifs_tcp_ses_lock);
  1609. return tcon;
  1610. }
  1611. write_unlock(&cifs_tcp_ses_lock);
  1612. return NULL;
  1613. }
  1614. static void
  1615. cifs_put_tcon(struct cifsTconInfo *tcon)
  1616. {
  1617. int xid;
  1618. struct cifsSesInfo *ses = tcon->ses;
  1619. cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  1620. write_lock(&cifs_tcp_ses_lock);
  1621. if (--tcon->tc_count > 0) {
  1622. write_unlock(&cifs_tcp_ses_lock);
  1623. return;
  1624. }
  1625. list_del_init(&tcon->tcon_list);
  1626. write_unlock(&cifs_tcp_ses_lock);
  1627. xid = GetXid();
  1628. CIFSSMBTDis(xid, tcon);
  1629. _FreeXid(xid);
  1630. tconInfoFree(tcon);
  1631. cifs_put_smb_ses(ses);
  1632. }
  1633. static struct cifsTconInfo *
  1634. cifs_get_tcon(struct cifsSesInfo *ses, struct smb_vol *volume_info)
  1635. {
  1636. int rc, xid;
  1637. struct cifsTconInfo *tcon;
  1638. tcon = cifs_find_tcon(ses, volume_info->UNC);
  1639. if (tcon) {
  1640. cFYI(1, "Found match on UNC path");
  1641. /* existing tcon already has a reference */
  1642. cifs_put_smb_ses(ses);
  1643. if (tcon->seal != volume_info->seal)
  1644. cERROR(1, "transport encryption setting "
  1645. "conflicts with existing tid");
  1646. return tcon;
  1647. }
  1648. tcon = tconInfoAlloc();
  1649. if (tcon == NULL) {
  1650. rc = -ENOMEM;
  1651. goto out_fail;
  1652. }
  1653. tcon->ses = ses;
  1654. if (volume_info->password) {
  1655. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  1656. if (!tcon->password) {
  1657. rc = -ENOMEM;
  1658. goto out_fail;
  1659. }
  1660. }
  1661. if (strchr(volume_info->UNC + 3, '\\') == NULL
  1662. && strchr(volume_info->UNC + 3, '/') == NULL) {
  1663. cERROR(1, "Missing share name");
  1664. rc = -ENODEV;
  1665. goto out_fail;
  1666. }
  1667. /* BB Do we need to wrap session_mutex around
  1668. * this TCon call and Unix SetFS as
  1669. * we do on SessSetup and reconnect? */
  1670. xid = GetXid();
  1671. rc = CIFSTCon(xid, ses, volume_info->UNC, tcon, volume_info->local_nls);
  1672. FreeXid(xid);
  1673. cFYI(1, "CIFS Tcon rc = %d", rc);
  1674. if (rc)
  1675. goto out_fail;
  1676. if (volume_info->nodfs) {
  1677. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  1678. cFYI(1, "DFS disabled (%d)", tcon->Flags);
  1679. }
  1680. tcon->seal = volume_info->seal;
  1681. /* we can have only one retry value for a connection
  1682. to a share so for resources mounted more than once
  1683. to the same server share the last value passed in
  1684. for the retry flag is used */
  1685. tcon->retry = volume_info->retry;
  1686. tcon->nocase = volume_info->nocase;
  1687. tcon->local_lease = volume_info->local_lease;
  1688. write_lock(&cifs_tcp_ses_lock);
  1689. list_add(&tcon->tcon_list, &ses->tcon_list);
  1690. write_unlock(&cifs_tcp_ses_lock);
  1691. return tcon;
  1692. out_fail:
  1693. tconInfoFree(tcon);
  1694. return ERR_PTR(rc);
  1695. }
  1696. int
  1697. get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path,
  1698. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  1699. struct dfs_info3_param **preferrals, int remap)
  1700. {
  1701. char *temp_unc;
  1702. int rc = 0;
  1703. *pnum_referrals = 0;
  1704. *preferrals = NULL;
  1705. if (pSesInfo->ipc_tid == 0) {
  1706. temp_unc = kmalloc(2 /* for slashes */ +
  1707. strnlen(pSesInfo->serverName,
  1708. SERVER_NAME_LEN_WITH_NULL * 2)
  1709. + 1 + 4 /* slash IPC$ */ + 2,
  1710. GFP_KERNEL);
  1711. if (temp_unc == NULL)
  1712. return -ENOMEM;
  1713. temp_unc[0] = '\\';
  1714. temp_unc[1] = '\\';
  1715. strcpy(temp_unc + 2, pSesInfo->serverName);
  1716. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  1717. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  1718. cFYI(1, "CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid);
  1719. kfree(temp_unc);
  1720. }
  1721. if (rc == 0)
  1722. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  1723. pnum_referrals, nls_codepage, remap);
  1724. /* BB map targetUNCs to dfs_info3 structures, here or
  1725. in CIFSGetDFSRefer BB */
  1726. return rc;
  1727. }
  1728. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1729. static struct lock_class_key cifs_key[2];
  1730. static struct lock_class_key cifs_slock_key[2];
  1731. static inline void
  1732. cifs_reclassify_socket4(struct socket *sock)
  1733. {
  1734. struct sock *sk = sock->sk;
  1735. BUG_ON(sock_owned_by_user(sk));
  1736. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  1737. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  1738. }
  1739. static inline void
  1740. cifs_reclassify_socket6(struct socket *sock)
  1741. {
  1742. struct sock *sk = sock->sk;
  1743. BUG_ON(sock_owned_by_user(sk));
  1744. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  1745. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  1746. }
  1747. #else
  1748. static inline void
  1749. cifs_reclassify_socket4(struct socket *sock)
  1750. {
  1751. }
  1752. static inline void
  1753. cifs_reclassify_socket6(struct socket *sock)
  1754. {
  1755. }
  1756. #endif
  1757. /* See RFC1001 section 14 on representation of Netbios names */
  1758. static void rfc1002mangle(char *target, char *source, unsigned int length)
  1759. {
  1760. unsigned int i, j;
  1761. for (i = 0, j = 0; i < (length); i++) {
  1762. /* mask a nibble at a time and encode */
  1763. target[j] = 'A' + (0x0F & (source[i] >> 4));
  1764. target[j+1] = 'A' + (0x0F & source[i]);
  1765. j += 2;
  1766. }
  1767. }
  1768. static int
  1769. ipv4_connect(struct TCP_Server_Info *server)
  1770. {
  1771. int rc = 0;
  1772. int val;
  1773. bool connected = false;
  1774. __be16 orig_port = 0;
  1775. struct socket *socket = server->ssocket;
  1776. if (socket == NULL) {
  1777. rc = sock_create_kern(PF_INET, SOCK_STREAM,
  1778. IPPROTO_TCP, &socket);
  1779. if (rc < 0) {
  1780. cERROR(1, "Error %d creating socket", rc);
  1781. return rc;
  1782. }
  1783. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1784. cFYI(1, "Socket created");
  1785. server->ssocket = socket;
  1786. socket->sk->sk_allocation = GFP_NOFS;
  1787. cifs_reclassify_socket4(socket);
  1788. }
  1789. /* user overrode default port */
  1790. if (server->addr.sockAddr.sin_port) {
  1791. rc = socket->ops->connect(socket, (struct sockaddr *)
  1792. &server->addr.sockAddr,
  1793. sizeof(struct sockaddr_in), 0);
  1794. if (rc >= 0)
  1795. connected = true;
  1796. }
  1797. if (!connected) {
  1798. /* save original port so we can retry user specified port
  1799. later if fall back ports fail this time */
  1800. orig_port = server->addr.sockAddr.sin_port;
  1801. /* do not retry on the same port we just failed on */
  1802. if (server->addr.sockAddr.sin_port != htons(CIFS_PORT)) {
  1803. server->addr.sockAddr.sin_port = htons(CIFS_PORT);
  1804. rc = socket->ops->connect(socket,
  1805. (struct sockaddr *)
  1806. &server->addr.sockAddr,
  1807. sizeof(struct sockaddr_in), 0);
  1808. if (rc >= 0)
  1809. connected = true;
  1810. }
  1811. }
  1812. if (!connected) {
  1813. server->addr.sockAddr.sin_port = htons(RFC1001_PORT);
  1814. rc = socket->ops->connect(socket, (struct sockaddr *)
  1815. &server->addr.sockAddr,
  1816. sizeof(struct sockaddr_in), 0);
  1817. if (rc >= 0)
  1818. connected = true;
  1819. }
  1820. /* give up here - unless we want to retry on different
  1821. protocol families some day */
  1822. if (!connected) {
  1823. if (orig_port)
  1824. server->addr.sockAddr.sin_port = orig_port;
  1825. cFYI(1, "Error %d connecting to server via ipv4", rc);
  1826. sock_release(socket);
  1827. server->ssocket = NULL;
  1828. return rc;
  1829. }
  1830. /*
  1831. * Eventually check for other socket options to change from
  1832. * the default. sock_setsockopt not used because it expects
  1833. * user space buffer
  1834. */
  1835. socket->sk->sk_rcvtimeo = 7 * HZ;
  1836. socket->sk->sk_sndtimeo = 5 * HZ;
  1837. /* make the bufsizes depend on wsize/rsize and max requests */
  1838. if (server->noautotune) {
  1839. if (socket->sk->sk_sndbuf < (200 * 1024))
  1840. socket->sk->sk_sndbuf = 200 * 1024;
  1841. if (socket->sk->sk_rcvbuf < (140 * 1024))
  1842. socket->sk->sk_rcvbuf = 140 * 1024;
  1843. }
  1844. if (server->tcp_nodelay) {
  1845. val = 1;
  1846. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  1847. (char *)&val, sizeof(val));
  1848. if (rc)
  1849. cFYI(1, "set TCP_NODELAY socket option error %d", rc);
  1850. }
  1851. cFYI(1, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  1852. socket->sk->sk_sndbuf,
  1853. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  1854. /* send RFC1001 sessinit */
  1855. if (server->addr.sockAddr.sin_port == htons(RFC1001_PORT)) {
  1856. /* some servers require RFC1001 sessinit before sending
  1857. negprot - BB check reconnection in case where second
  1858. sessinit is sent but no second negprot */
  1859. struct rfc1002_session_packet *ses_init_buf;
  1860. struct smb_hdr *smb_buf;
  1861. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  1862. GFP_KERNEL);
  1863. if (ses_init_buf) {
  1864. ses_init_buf->trailer.session_req.called_len = 32;
  1865. if (server->server_RFC1001_name &&
  1866. server->server_RFC1001_name[0] != 0)
  1867. rfc1002mangle(ses_init_buf->trailer.
  1868. session_req.called_name,
  1869. server->server_RFC1001_name,
  1870. RFC1001_NAME_LEN_WITH_NULL);
  1871. else
  1872. rfc1002mangle(ses_init_buf->trailer.
  1873. session_req.called_name,
  1874. DEFAULT_CIFS_CALLED_NAME,
  1875. RFC1001_NAME_LEN_WITH_NULL);
  1876. ses_init_buf->trailer.session_req.calling_len = 32;
  1877. /* calling name ends in null (byte 16) from old smb
  1878. convention. */
  1879. if (server->workstation_RFC1001_name &&
  1880. server->workstation_RFC1001_name[0] != 0)
  1881. rfc1002mangle(ses_init_buf->trailer.
  1882. session_req.calling_name,
  1883. server->workstation_RFC1001_name,
  1884. RFC1001_NAME_LEN_WITH_NULL);
  1885. else
  1886. rfc1002mangle(ses_init_buf->trailer.
  1887. session_req.calling_name,
  1888. "LINUX_CIFS_CLNT",
  1889. RFC1001_NAME_LEN_WITH_NULL);
  1890. ses_init_buf->trailer.session_req.scope1 = 0;
  1891. ses_init_buf->trailer.session_req.scope2 = 0;
  1892. smb_buf = (struct smb_hdr *)ses_init_buf;
  1893. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  1894. smb_buf->smb_buf_length = 0x81000044;
  1895. rc = smb_send(server, smb_buf, 0x44);
  1896. kfree(ses_init_buf);
  1897. msleep(1); /* RFC1001 layer in at least one server
  1898. requires very short break before negprot
  1899. presumably because not expecting negprot
  1900. to follow so fast. This is a simple
  1901. solution that works without
  1902. complicating the code and causes no
  1903. significant slowing down on mount
  1904. for everyone else */
  1905. }
  1906. /* else the negprot may still work without this
  1907. even though malloc failed */
  1908. }
  1909. return rc;
  1910. }
  1911. static int
  1912. ipv6_connect(struct TCP_Server_Info *server)
  1913. {
  1914. int rc = 0;
  1915. int val;
  1916. bool connected = false;
  1917. __be16 orig_port = 0;
  1918. struct socket *socket = server->ssocket;
  1919. if (socket == NULL) {
  1920. rc = sock_create_kern(PF_INET6, SOCK_STREAM,
  1921. IPPROTO_TCP, &socket);
  1922. if (rc < 0) {
  1923. cERROR(1, "Error %d creating ipv6 socket", rc);
  1924. socket = NULL;
  1925. return rc;
  1926. }
  1927. /* BB other socket options to set KEEPALIVE, NODELAY? */
  1928. cFYI(1, "ipv6 Socket created");
  1929. server->ssocket = socket;
  1930. socket->sk->sk_allocation = GFP_NOFS;
  1931. cifs_reclassify_socket6(socket);
  1932. }
  1933. /* user overrode default port */
  1934. if (server->addr.sockAddr6.sin6_port) {
  1935. rc = socket->ops->connect(socket,
  1936. (struct sockaddr *) &server->addr.sockAddr6,
  1937. sizeof(struct sockaddr_in6), 0);
  1938. if (rc >= 0)
  1939. connected = true;
  1940. }
  1941. if (!connected) {
  1942. /* save original port so we can retry user specified port
  1943. later if fall back ports fail this time */
  1944. orig_port = server->addr.sockAddr6.sin6_port;
  1945. /* do not retry on the same port we just failed on */
  1946. if (server->addr.sockAddr6.sin6_port != htons(CIFS_PORT)) {
  1947. server->addr.sockAddr6.sin6_port = htons(CIFS_PORT);
  1948. rc = socket->ops->connect(socket, (struct sockaddr *)
  1949. &server->addr.sockAddr6,
  1950. sizeof(struct sockaddr_in6), 0);
  1951. if (rc >= 0)
  1952. connected = true;
  1953. }
  1954. }
  1955. if (!connected) {
  1956. server->addr.sockAddr6.sin6_port = htons(RFC1001_PORT);
  1957. rc = socket->ops->connect(socket, (struct sockaddr *)
  1958. &server->addr.sockAddr6,
  1959. sizeof(struct sockaddr_in6), 0);
  1960. if (rc >= 0)
  1961. connected = true;
  1962. }
  1963. /* give up here - unless we want to retry on different
  1964. protocol families some day */
  1965. if (!connected) {
  1966. if (orig_port)
  1967. server->addr.sockAddr6.sin6_port = orig_port;
  1968. cFYI(1, "Error %d connecting to server via ipv6", rc);
  1969. sock_release(socket);
  1970. server->ssocket = NULL;
  1971. return rc;
  1972. }
  1973. /*
  1974. * Eventually check for other socket options to change from
  1975. * the default. sock_setsockopt not used because it expects
  1976. * user space buffer
  1977. */
  1978. socket->sk->sk_rcvtimeo = 7 * HZ;
  1979. socket->sk->sk_sndtimeo = 5 * HZ;
  1980. if (server->tcp_nodelay) {
  1981. val = 1;
  1982. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  1983. (char *)&val, sizeof(val));
  1984. if (rc)
  1985. cFYI(1, "set TCP_NODELAY socket option error %d", rc);
  1986. }
  1987. server->ssocket = socket;
  1988. return rc;
  1989. }
  1990. void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
  1991. struct super_block *sb, struct smb_vol *vol_info)
  1992. {
  1993. /* if we are reconnecting then should we check to see if
  1994. * any requested capabilities changed locally e.g. via
  1995. * remount but we can not do much about it here
  1996. * if they have (even if we could detect it by the following)
  1997. * Perhaps we could add a backpointer to array of sb from tcon
  1998. * or if we change to make all sb to same share the same
  1999. * sb as NFS - then we only have one backpointer to sb.
  2000. * What if we wanted to mount the server share twice once with
  2001. * and once without posixacls or posix paths? */
  2002. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2003. if (vol_info && vol_info->no_linux_ext) {
  2004. tcon->fsUnixInfo.Capability = 0;
  2005. tcon->unix_ext = 0; /* Unix Extensions disabled */
  2006. cFYI(1, "Linux protocol extensions disabled");
  2007. return;
  2008. } else if (vol_info)
  2009. tcon->unix_ext = 1; /* Unix Extensions supported */
  2010. if (tcon->unix_ext == 0) {
  2011. cFYI(1, "Unix extensions disabled so not set on reconnect");
  2012. return;
  2013. }
  2014. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  2015. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2016. /* check for reconnect case in which we do not
  2017. want to change the mount behavior if we can avoid it */
  2018. if (vol_info == NULL) {
  2019. /* turn off POSIX ACL and PATHNAMES if not set
  2020. originally at mount time */
  2021. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  2022. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2023. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2024. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2025. cERROR(1, "POSIXPATH support change");
  2026. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2027. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2028. cERROR(1, "possible reconnect error");
  2029. cERROR(1, "server disabled POSIX path support");
  2030. }
  2031. }
  2032. cap &= CIFS_UNIX_CAP_MASK;
  2033. if (vol_info && vol_info->no_psx_acl)
  2034. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2035. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  2036. cFYI(1, "negotiated posix acl support");
  2037. if (sb)
  2038. sb->s_flags |= MS_POSIXACL;
  2039. }
  2040. if (vol_info && vol_info->posix_paths == 0)
  2041. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2042. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  2043. cFYI(1, "negotiate posix pathnames");
  2044. if (sb)
  2045. CIFS_SB(sb)->mnt_cifs_flags |=
  2046. CIFS_MOUNT_POSIX_PATHS;
  2047. }
  2048. /* We might be setting the path sep back to a different
  2049. form if we are reconnecting and the server switched its
  2050. posix path capability for this share */
  2051. if (sb && (CIFS_SB(sb)->prepathlen > 0))
  2052. CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
  2053. if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
  2054. if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
  2055. CIFS_SB(sb)->rsize = 127 * 1024;
  2056. cFYI(DBG2, "larger reads not supported by srv");
  2057. }
  2058. }
  2059. cFYI(1, "Negotiate caps 0x%x", (int)cap);
  2060. #ifdef CONFIG_CIFS_DEBUG2
  2061. if (cap & CIFS_UNIX_FCNTL_CAP)
  2062. cFYI(1, "FCNTL cap");
  2063. if (cap & CIFS_UNIX_EXTATTR_CAP)
  2064. cFYI(1, "EXTATTR cap");
  2065. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2066. cFYI(1, "POSIX path cap");
  2067. if (cap & CIFS_UNIX_XATTR_CAP)
  2068. cFYI(1, "XATTR cap");
  2069. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  2070. cFYI(1, "POSIX ACL cap");
  2071. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  2072. cFYI(1, "very large read cap");
  2073. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  2074. cFYI(1, "very large write cap");
  2075. #endif /* CIFS_DEBUG2 */
  2076. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  2077. if (vol_info == NULL) {
  2078. cFYI(1, "resetting capabilities failed");
  2079. } else
  2080. cERROR(1, "Negotiating Unix capabilities "
  2081. "with the server failed. Consider "
  2082. "mounting with the Unix Extensions\n"
  2083. "disabled, if problems are found, "
  2084. "by specifying the nounix mount "
  2085. "option.");
  2086. }
  2087. }
  2088. }
  2089. static void
  2090. convert_delimiter(char *path, char delim)
  2091. {
  2092. int i;
  2093. char old_delim;
  2094. if (path == NULL)
  2095. return;
  2096. if (delim == '/')
  2097. old_delim = '\\';
  2098. else
  2099. old_delim = '/';
  2100. for (i = 0; path[i] != '\0'; i++) {
  2101. if (path[i] == old_delim)
  2102. path[i] = delim;
  2103. }
  2104. }
  2105. static void setup_cifs_sb(struct smb_vol *pvolume_info,
  2106. struct cifs_sb_info *cifs_sb)
  2107. {
  2108. if (pvolume_info->rsize > CIFSMaxBufSize) {
  2109. cERROR(1, "rsize %d too large, using MaxBufSize",
  2110. pvolume_info->rsize);
  2111. cifs_sb->rsize = CIFSMaxBufSize;
  2112. } else if ((pvolume_info->rsize) &&
  2113. (pvolume_info->rsize <= CIFSMaxBufSize))
  2114. cifs_sb->rsize = pvolume_info->rsize;
  2115. else /* default */
  2116. cifs_sb->rsize = CIFSMaxBufSize;
  2117. if (pvolume_info->wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
  2118. cERROR(1, "wsize %d too large, using 4096 instead",
  2119. pvolume_info->wsize);
  2120. cifs_sb->wsize = 4096;
  2121. } else if (pvolume_info->wsize)
  2122. cifs_sb->wsize = pvolume_info->wsize;
  2123. else
  2124. cifs_sb->wsize = min_t(const int,
  2125. PAGEVEC_SIZE * PAGE_CACHE_SIZE,
  2126. 127*1024);
  2127. /* old default of CIFSMaxBufSize was too small now
  2128. that SMB Write2 can send multiple pages in kvec.
  2129. RFC1001 does not describe what happens when frame
  2130. bigger than 128K is sent so use that as max in
  2131. conjunction with 52K kvec constraint on arch with 4K
  2132. page size */
  2133. if (cifs_sb->rsize < 2048) {
  2134. cifs_sb->rsize = 2048;
  2135. /* Windows ME may prefer this */
  2136. cFYI(1, "readsize set to minimum: 2048");
  2137. }
  2138. /* calculate prepath */
  2139. cifs_sb->prepath = pvolume_info->prepath;
  2140. if (cifs_sb->prepath) {
  2141. cifs_sb->prepathlen = strlen(cifs_sb->prepath);
  2142. /* we can not convert the / to \ in the path
  2143. separators in the prefixpath yet because we do not
  2144. know (until reset_cifs_unix_caps is called later)
  2145. whether POSIX PATH CAP is available. We normalize
  2146. the / to \ after reset_cifs_unix_caps is called */
  2147. pvolume_info->prepath = NULL;
  2148. } else
  2149. cifs_sb->prepathlen = 0;
  2150. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  2151. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  2152. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  2153. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  2154. cFYI(1, "file mode: 0x%x dir mode: 0x%x",
  2155. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  2156. if (pvolume_info->noperm)
  2157. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  2158. if (pvolume_info->setuids)
  2159. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  2160. if (pvolume_info->server_ino)
  2161. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  2162. if (pvolume_info->remap)
  2163. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  2164. if (pvolume_info->no_xattr)
  2165. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  2166. if (pvolume_info->sfu_emul)
  2167. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  2168. if (pvolume_info->nobrl)
  2169. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  2170. if (pvolume_info->nostrictsync)
  2171. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  2172. if (pvolume_info->mand_lock)
  2173. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  2174. if (pvolume_info->cifs_acl)
  2175. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  2176. if (pvolume_info->override_uid)
  2177. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  2178. if (pvolume_info->override_gid)
  2179. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  2180. if (pvolume_info->dynperm)
  2181. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  2182. if (pvolume_info->direct_io) {
  2183. cFYI(1, "mounting share using direct i/o");
  2184. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  2185. }
  2186. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  2187. cERROR(1, "mount option dynperm ignored if cifsacl "
  2188. "mount option supported");
  2189. }
  2190. static int
  2191. is_path_accessible(int xid, struct cifsTconInfo *tcon,
  2192. struct cifs_sb_info *cifs_sb, const char *full_path)
  2193. {
  2194. int rc;
  2195. FILE_ALL_INFO *pfile_info;
  2196. pfile_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  2197. if (pfile_info == NULL)
  2198. return -ENOMEM;
  2199. rc = CIFSSMBQPathInfo(xid, tcon, full_path, pfile_info,
  2200. 0 /* not legacy */, cifs_sb->local_nls,
  2201. cifs_sb->mnt_cifs_flags &
  2202. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2203. kfree(pfile_info);
  2204. return rc;
  2205. }
  2206. static void
  2207. cleanup_volume_info(struct smb_vol **pvolume_info)
  2208. {
  2209. struct smb_vol *volume_info;
  2210. if (!pvolume_info || !*pvolume_info)
  2211. return;
  2212. volume_info = *pvolume_info;
  2213. kzfree(volume_info->password);
  2214. kfree(volume_info->UNC);
  2215. kfree(volume_info->prepath);
  2216. kfree(volume_info);
  2217. *pvolume_info = NULL;
  2218. return;
  2219. }
  2220. #ifdef CONFIG_CIFS_DFS_UPCALL
  2221. /* build_path_to_root returns full path to root when
  2222. * we do not have an exiting connection (tcon) */
  2223. static char *
  2224. build_unc_path_to_root(const struct smb_vol *volume_info,
  2225. const struct cifs_sb_info *cifs_sb)
  2226. {
  2227. char *full_path;
  2228. int unc_len = strnlen(volume_info->UNC, MAX_TREE_SIZE + 1);
  2229. full_path = kmalloc(unc_len + cifs_sb->prepathlen + 1, GFP_KERNEL);
  2230. if (full_path == NULL)
  2231. return ERR_PTR(-ENOMEM);
  2232. strncpy(full_path, volume_info->UNC, unc_len);
  2233. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  2234. int i;
  2235. for (i = 0; i < unc_len; i++) {
  2236. if (full_path[i] == '\\')
  2237. full_path[i] = '/';
  2238. }
  2239. }
  2240. if (cifs_sb->prepathlen)
  2241. strncpy(full_path + unc_len, cifs_sb->prepath,
  2242. cifs_sb->prepathlen);
  2243. full_path[unc_len + cifs_sb->prepathlen] = 0; /* add trailing null */
  2244. return full_path;
  2245. }
  2246. #endif
  2247. int
  2248. cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb,
  2249. char *mount_data_global, const char *devname)
  2250. {
  2251. int rc;
  2252. int xid;
  2253. struct smb_vol *volume_info;
  2254. struct cifsSesInfo *pSesInfo;
  2255. struct cifsTconInfo *tcon;
  2256. struct TCP_Server_Info *srvTcp;
  2257. char *full_path;
  2258. char *mount_data = mount_data_global;
  2259. #ifdef CONFIG_CIFS_DFS_UPCALL
  2260. struct dfs_info3_param *referrals = NULL;
  2261. unsigned int num_referrals = 0;
  2262. int referral_walks_count = 0;
  2263. try_mount_again:
  2264. #endif
  2265. rc = 0;
  2266. tcon = NULL;
  2267. pSesInfo = NULL;
  2268. srvTcp = NULL;
  2269. full_path = NULL;
  2270. xid = GetXid();
  2271. volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
  2272. if (!volume_info) {
  2273. rc = -ENOMEM;
  2274. goto out;
  2275. }
  2276. if (cifs_parse_mount_options(mount_data, devname, volume_info)) {
  2277. rc = -EINVAL;
  2278. goto out;
  2279. }
  2280. if (volume_info->nullauth) {
  2281. cFYI(1, "null user");
  2282. volume_info->username = "";
  2283. } else if (volume_info->username) {
  2284. /* BB fixme parse for domain name here */
  2285. cFYI(1, "Username: %s", volume_info->username);
  2286. } else {
  2287. cifserror("No username specified");
  2288. /* In userspace mount helper we can get user name from alternate
  2289. locations such as env variables and files on disk */
  2290. rc = -EINVAL;
  2291. goto out;
  2292. }
  2293. /* this is needed for ASCII cp to Unicode converts */
  2294. if (volume_info->iocharset == NULL) {
  2295. /* load_nls_default cannot return null */
  2296. volume_info->local_nls = load_nls_default();
  2297. } else {
  2298. volume_info->local_nls = load_nls(volume_info->iocharset);
  2299. if (volume_info->local_nls == NULL) {
  2300. cERROR(1, "CIFS mount error: iocharset %s not found",
  2301. volume_info->iocharset);
  2302. rc = -ELIBACC;
  2303. goto out;
  2304. }
  2305. }
  2306. cifs_sb->local_nls = volume_info->local_nls;
  2307. /* get a reference to a tcp session */
  2308. srvTcp = cifs_get_tcp_session(volume_info);
  2309. if (IS_ERR(srvTcp)) {
  2310. rc = PTR_ERR(srvTcp);
  2311. goto out;
  2312. }
  2313. /* get a reference to a SMB session */
  2314. pSesInfo = cifs_get_smb_ses(srvTcp, volume_info);
  2315. if (IS_ERR(pSesInfo)) {
  2316. rc = PTR_ERR(pSesInfo);
  2317. pSesInfo = NULL;
  2318. goto mount_fail_check;
  2319. }
  2320. setup_cifs_sb(volume_info, cifs_sb);
  2321. if (pSesInfo->capabilities & CAP_LARGE_FILES)
  2322. sb->s_maxbytes = MAX_LFS_FILESIZE;
  2323. else
  2324. sb->s_maxbytes = MAX_NON_LFS;
  2325. /* BB FIXME fix time_gran to be larger for LANMAN sessions */
  2326. sb->s_time_gran = 100;
  2327. /* search for existing tcon to this server share */
  2328. tcon = cifs_get_tcon(pSesInfo, volume_info);
  2329. if (IS_ERR(tcon)) {
  2330. rc = PTR_ERR(tcon);
  2331. tcon = NULL;
  2332. goto remote_path_check;
  2333. }
  2334. cifs_sb->tcon = tcon;
  2335. /* do not care if following two calls succeed - informational */
  2336. if (!tcon->ipc) {
  2337. CIFSSMBQFSDeviceInfo(xid, tcon);
  2338. CIFSSMBQFSAttributeInfo(xid, tcon);
  2339. }
  2340. /* tell server which Unix caps we support */
  2341. if (tcon->ses->capabilities & CAP_UNIX)
  2342. /* reset of caps checks mount to see if unix extensions
  2343. disabled for just this mount */
  2344. reset_cifs_unix_caps(xid, tcon, sb, volume_info);
  2345. else
  2346. tcon->unix_ext = 0; /* server does not support them */
  2347. /* convert forward to back slashes in prepath here if needed */
  2348. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
  2349. convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
  2350. if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
  2351. cifs_sb->rsize = 1024 * 127;
  2352. cFYI(DBG2, "no very large read support, rsize now 127K");
  2353. }
  2354. if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
  2355. cifs_sb->wsize = min(cifs_sb->wsize,
  2356. (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
  2357. if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
  2358. cifs_sb->rsize = min(cifs_sb->rsize,
  2359. (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
  2360. remote_path_check:
  2361. /* check if a whole path (including prepath) is not remote */
  2362. if (!rc && cifs_sb->prepathlen && tcon) {
  2363. /* build_path_to_root works only when we have a valid tcon */
  2364. full_path = cifs_build_path_to_root(cifs_sb);
  2365. if (full_path == NULL) {
  2366. rc = -ENOMEM;
  2367. goto mount_fail_check;
  2368. }
  2369. rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
  2370. if (rc != -EREMOTE) {
  2371. kfree(full_path);
  2372. goto mount_fail_check;
  2373. }
  2374. kfree(full_path);
  2375. }
  2376. /* get referral if needed */
  2377. if (rc == -EREMOTE) {
  2378. #ifdef CONFIG_CIFS_DFS_UPCALL
  2379. if (referral_walks_count > MAX_NESTED_LINKS) {
  2380. /*
  2381. * BB: when we implement proper loop detection,
  2382. * we will remove this check. But now we need it
  2383. * to prevent an indefinite loop if 'DFS tree' is
  2384. * misconfigured (i.e. has loops).
  2385. */
  2386. rc = -ELOOP;
  2387. goto mount_fail_check;
  2388. }
  2389. /* convert forward to back slashes in prepath here if needed */
  2390. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
  2391. convert_delimiter(cifs_sb->prepath,
  2392. CIFS_DIR_SEP(cifs_sb));
  2393. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  2394. if (IS_ERR(full_path)) {
  2395. rc = PTR_ERR(full_path);
  2396. goto mount_fail_check;
  2397. }
  2398. cFYI(1, "Getting referral for: %s", full_path);
  2399. rc = get_dfs_path(xid, pSesInfo , full_path + 1,
  2400. cifs_sb->local_nls, &num_referrals, &referrals,
  2401. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  2402. if (!rc && num_referrals > 0) {
  2403. char *fake_devname = NULL;
  2404. if (mount_data != mount_data_global)
  2405. kfree(mount_data);
  2406. mount_data = cifs_compose_mount_options(
  2407. cifs_sb->mountdata, full_path + 1,
  2408. referrals, &fake_devname);
  2409. free_dfs_info_array(referrals, num_referrals);
  2410. kfree(fake_devname);
  2411. kfree(full_path);
  2412. if (IS_ERR(mount_data)) {
  2413. rc = PTR_ERR(mount_data);
  2414. mount_data = NULL;
  2415. goto mount_fail_check;
  2416. }
  2417. if (tcon)
  2418. cifs_put_tcon(tcon);
  2419. else if (pSesInfo)
  2420. cifs_put_smb_ses(pSesInfo);
  2421. cleanup_volume_info(&volume_info);
  2422. referral_walks_count++;
  2423. FreeXid(xid);
  2424. goto try_mount_again;
  2425. }
  2426. #else /* No DFS support, return error on mount */
  2427. rc = -EOPNOTSUPP;
  2428. #endif
  2429. }
  2430. mount_fail_check:
  2431. /* on error free sesinfo and tcon struct if needed */
  2432. if (rc) {
  2433. if (mount_data != mount_data_global)
  2434. kfree(mount_data);
  2435. /* If find_unc succeeded then rc == 0 so we can not end */
  2436. /* up accidently freeing someone elses tcon struct */
  2437. if (tcon)
  2438. cifs_put_tcon(tcon);
  2439. else if (pSesInfo)
  2440. cifs_put_smb_ses(pSesInfo);
  2441. else
  2442. cifs_put_tcp_session(srvTcp);
  2443. goto out;
  2444. }
  2445. /* volume_info->password is freed above when existing session found
  2446. (in which case it is not needed anymore) but when new sesion is created
  2447. the password ptr is put in the new session structure (in which case the
  2448. password will be freed at unmount time) */
  2449. out:
  2450. /* zero out password before freeing */
  2451. cleanup_volume_info(&volume_info);
  2452. FreeXid(xid);
  2453. return rc;
  2454. }
  2455. int
  2456. CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
  2457. const char *tree, struct cifsTconInfo *tcon,
  2458. const struct nls_table *nls_codepage)
  2459. {
  2460. struct smb_hdr *smb_buffer;
  2461. struct smb_hdr *smb_buffer_response;
  2462. TCONX_REQ *pSMB;
  2463. TCONX_RSP *pSMBr;
  2464. unsigned char *bcc_ptr;
  2465. int rc = 0;
  2466. int length, bytes_left;
  2467. __u16 count;
  2468. if (ses == NULL)
  2469. return -EIO;
  2470. smb_buffer = cifs_buf_get();
  2471. if (smb_buffer == NULL)
  2472. return -ENOMEM;
  2473. smb_buffer_response = smb_buffer;
  2474. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  2475. NULL /*no tid */ , 4 /*wct */ );
  2476. smb_buffer->Mid = GetNextMid(ses->server);
  2477. smb_buffer->Uid = ses->Suid;
  2478. pSMB = (TCONX_REQ *) smb_buffer;
  2479. pSMBr = (TCONX_RSP *) smb_buffer_response;
  2480. pSMB->AndXCommand = 0xFF;
  2481. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  2482. bcc_ptr = &pSMB->Password[0];
  2483. if ((ses->server->secMode) & SECMODE_USER) {
  2484. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  2485. *bcc_ptr = 0; /* password is null byte */
  2486. bcc_ptr++; /* skip password */
  2487. /* already aligned so no need to do it below */
  2488. } else {
  2489. pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE);
  2490. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  2491. specified as required (when that support is added to
  2492. the vfs in the future) as only NTLM or the much
  2493. weaker LANMAN (which we do not send by default) is accepted
  2494. by Samba (not sure whether other servers allow
  2495. NTLMv2 password here) */
  2496. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  2497. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  2498. (ses->server->secType == LANMAN))
  2499. calc_lanman_hash(tcon->password, ses->server->cryptKey,
  2500. ses->server->secMode &
  2501. SECMODE_PW_ENCRYPT ? true : false,
  2502. bcc_ptr);
  2503. else
  2504. #endif /* CIFS_WEAK_PW_HASH */
  2505. SMBNTencrypt(tcon->password, ses->server->cryptKey,
  2506. bcc_ptr);
  2507. bcc_ptr += CIFS_SESS_KEY_SIZE;
  2508. if (ses->capabilities & CAP_UNICODE) {
  2509. /* must align unicode strings */
  2510. *bcc_ptr = 0; /* null byte password */
  2511. bcc_ptr++;
  2512. }
  2513. }
  2514. if (ses->server->secMode &
  2515. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  2516. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  2517. if (ses->capabilities & CAP_STATUS32) {
  2518. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  2519. }
  2520. if (ses->capabilities & CAP_DFS) {
  2521. smb_buffer->Flags2 |= SMBFLG2_DFS;
  2522. }
  2523. if (ses->capabilities & CAP_UNICODE) {
  2524. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  2525. length =
  2526. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  2527. 6 /* max utf8 char length in bytes */ *
  2528. (/* server len*/ + 256 /* share len */), nls_codepage);
  2529. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  2530. bcc_ptr += 2; /* skip trailing null */
  2531. } else { /* ASCII */
  2532. strcpy(bcc_ptr, tree);
  2533. bcc_ptr += strlen(tree) + 1;
  2534. }
  2535. strcpy(bcc_ptr, "?????");
  2536. bcc_ptr += strlen("?????");
  2537. bcc_ptr += 1;
  2538. count = bcc_ptr - &pSMB->Password[0];
  2539. pSMB->hdr.smb_buf_length += count;
  2540. pSMB->ByteCount = cpu_to_le16(count);
  2541. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  2542. CIFS_STD_OP);
  2543. /* above now done in SendReceive */
  2544. if ((rc == 0) && (tcon != NULL)) {
  2545. bool is_unicode;
  2546. tcon->tidStatus = CifsGood;
  2547. tcon->need_reconnect = false;
  2548. tcon->tid = smb_buffer_response->Tid;
  2549. bcc_ptr = pByteArea(smb_buffer_response);
  2550. bytes_left = BCC(smb_buffer_response);
  2551. length = strnlen(bcc_ptr, bytes_left - 2);
  2552. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  2553. is_unicode = true;
  2554. else
  2555. is_unicode = false;
  2556. /* skip service field (NB: this field is always ASCII) */
  2557. if (length == 3) {
  2558. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  2559. (bcc_ptr[2] == 'C')) {
  2560. cFYI(1, "IPC connection");
  2561. tcon->ipc = 1;
  2562. }
  2563. } else if (length == 2) {
  2564. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  2565. /* the most common case */
  2566. cFYI(1, "disk share connection");
  2567. }
  2568. }
  2569. bcc_ptr += length + 1;
  2570. bytes_left -= (length + 1);
  2571. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  2572. /* mostly informational -- no need to fail on error here */
  2573. kfree(tcon->nativeFileSystem);
  2574. tcon->nativeFileSystem = cifs_strndup_from_ucs(bcc_ptr,
  2575. bytes_left, is_unicode,
  2576. nls_codepage);
  2577. cFYI(1, "nativeFileSystem=%s", tcon->nativeFileSystem);
  2578. if ((smb_buffer_response->WordCount == 3) ||
  2579. (smb_buffer_response->WordCount == 7))
  2580. /* field is in same location */
  2581. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  2582. else
  2583. tcon->Flags = 0;
  2584. cFYI(1, "Tcon flags: 0x%x ", tcon->Flags);
  2585. } else if ((rc == 0) && tcon == NULL) {
  2586. /* all we need to save for IPC$ connection */
  2587. ses->ipc_tid = smb_buffer_response->Tid;
  2588. }
  2589. cifs_buf_release(smb_buffer);
  2590. return rc;
  2591. }
  2592. int
  2593. cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb)
  2594. {
  2595. int rc = 0;
  2596. char *tmp;
  2597. if (cifs_sb->tcon)
  2598. cifs_put_tcon(cifs_sb->tcon);
  2599. cifs_sb->tcon = NULL;
  2600. tmp = cifs_sb->prepath;
  2601. cifs_sb->prepathlen = 0;
  2602. cifs_sb->prepath = NULL;
  2603. kfree(tmp);
  2604. return rc;
  2605. }
  2606. int cifs_negotiate_protocol(unsigned int xid, struct cifsSesInfo *ses)
  2607. {
  2608. int rc = 0;
  2609. struct TCP_Server_Info *server = ses->server;
  2610. /* only send once per connect */
  2611. if (server->maxBuf != 0)
  2612. return 0;
  2613. rc = CIFSSMBNegotiate(xid, ses);
  2614. if (rc == -EAGAIN) {
  2615. /* retry only once on 1st time connection */
  2616. rc = CIFSSMBNegotiate(xid, ses);
  2617. if (rc == -EAGAIN)
  2618. rc = -EHOSTDOWN;
  2619. }
  2620. if (rc == 0) {
  2621. spin_lock(&GlobalMid_Lock);
  2622. if (server->tcpStatus != CifsExiting)
  2623. server->tcpStatus = CifsGood;
  2624. else
  2625. rc = -EHOSTDOWN;
  2626. spin_unlock(&GlobalMid_Lock);
  2627. }
  2628. return rc;
  2629. }
  2630. int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses,
  2631. struct nls_table *nls_info)
  2632. {
  2633. int rc = 0;
  2634. struct TCP_Server_Info *server = ses->server;
  2635. ses->flags = 0;
  2636. ses->capabilities = server->capabilities;
  2637. if (linuxExtEnabled == 0)
  2638. ses->capabilities &= (~CAP_UNIX);
  2639. cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  2640. server->secMode, server->capabilities, server->timeAdj);
  2641. rc = CIFS_SessSetup(xid, ses, nls_info);
  2642. if (rc) {
  2643. cERROR(1, "Send error in SessSetup = %d", rc);
  2644. } else {
  2645. cFYI(1, "CIFS Session Established successfully");
  2646. spin_lock(&GlobalMid_Lock);
  2647. ses->status = CifsGood;
  2648. ses->need_reconnect = false;
  2649. spin_unlock(&GlobalMid_Lock);
  2650. }
  2651. return rc;
  2652. }