connect.c 113 KB

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