connect.c 98 KB

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