connect.c 113 KB

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