connect.c 114 KB

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