connect.c 143 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2011
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/sched/mm.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/list.h>
  27. #include <linux/wait.h>
  28. #include <linux/slab.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/ctype.h>
  31. #include <linux/utsname.h>
  32. #include <linux/mempool.h>
  33. #include <linux/delay.h>
  34. #include <linux/completion.h>
  35. #include <linux/kthread.h>
  36. #include <linux/pagevec.h>
  37. #include <linux/freezer.h>
  38. #include <linux/namei.h>
  39. #include <linux/uuid.h>
  40. #include <linux/uaccess.h>
  41. #include <asm/processor.h>
  42. #include <linux/inet.h>
  43. #include <linux/module.h>
  44. #include <keys/user-type.h>
  45. #include <net/ipv6.h>
  46. #include <linux/parser.h>
  47. #include <linux/bvec.h>
  48. #include "cifspdu.h"
  49. #include "cifsglob.h"
  50. #include "cifsproto.h"
  51. #include "cifs_unicode.h"
  52. #include "cifs_debug.h"
  53. #include "cifs_fs_sb.h"
  54. #include "ntlmssp.h"
  55. #include "nterr.h"
  56. #include "rfc1002pdu.h"
  57. #include "fscache.h"
  58. #include "smb2proto.h"
  59. #include "smbdirect.h"
  60. #include "dns_resolve.h"
  61. #ifdef CONFIG_CIFS_DFS_UPCALL
  62. #include "dfs_cache.h"
  63. #endif
  64. #include "fs_context.h"
  65. extern mempool_t *cifs_req_poolp;
  66. extern bool disable_legacy_dialects;
  67. /* FIXME: should these be tunable? */
  68. #define TLINK_ERROR_EXPIRE (1 * HZ)
  69. #define TLINK_IDLE_EXPIRE (600 * HZ)
  70. /* Drop the connection to not overload the server */
  71. #define NUM_STATUS_IO_TIMEOUT 5
  72. enum {
  73. /* Mount options that take no arguments */
  74. Opt_user_xattr, Opt_nouser_xattr,
  75. Opt_forceuid, Opt_noforceuid,
  76. Opt_forcegid, Opt_noforcegid,
  77. Opt_noblocksend, Opt_noautotune, Opt_nolease,
  78. Opt_hard, Opt_soft, Opt_perm, Opt_noperm, Opt_nodelete,
  79. Opt_mapposix, Opt_nomapposix,
  80. Opt_mapchars, Opt_nomapchars, Opt_sfu,
  81. Opt_nosfu, Opt_nodfs, Opt_posixpaths,
  82. Opt_noposixpaths, Opt_nounix, Opt_unix,
  83. Opt_nocase,
  84. Opt_brl, Opt_nobrl,
  85. Opt_handlecache, Opt_nohandlecache,
  86. Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
  87. Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
  88. Opt_nohard, Opt_nosoft,
  89. Opt_nointr, Opt_intr,
  90. Opt_nostrictsync, Opt_strictsync,
  91. Opt_serverino, Opt_noserverino,
  92. Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
  93. Opt_acl, Opt_noacl, Opt_locallease,
  94. Opt_sign, Opt_ignore_signature, Opt_seal, Opt_noac,
  95. Opt_fsc, Opt_mfsymlinks,
  96. Opt_multiuser, Opt_sloppy, Opt_nosharesock,
  97. Opt_persistent, Opt_nopersistent,
  98. Opt_resilient, Opt_noresilient,
  99. Opt_domainauto, Opt_rdma, Opt_modesid, Opt_rootfs,
  100. Opt_multichannel, Opt_nomultichannel,
  101. Opt_compress,
  102. /* Mount options which take numeric value */
  103. Opt_backupuid, Opt_backupgid, Opt_uid,
  104. Opt_cruid, Opt_gid, Opt_file_mode,
  105. Opt_dirmode, Opt_port,
  106. Opt_min_enc_offload,
  107. Opt_blocksize, Opt_rsize, Opt_wsize, Opt_actimeo,
  108. Opt_echo_interval, Opt_max_credits, Opt_handletimeout,
  109. Opt_snapshot, Opt_max_channels,
  110. /* Mount options which take string value */
  111. Opt_user, Opt_pass, Opt_ip,
  112. Opt_domain, Opt_srcaddr, Opt_iocharset,
  113. Opt_netbiosname, Opt_servern,
  114. Opt_ver, Opt_vers, Opt_sec, Opt_cache,
  115. /* Mount options to be ignored */
  116. Opt_ignore,
  117. /* Options which could be blank */
  118. Opt_blank_pass,
  119. Opt_blank_user,
  120. Opt_blank_ip,
  121. Opt_err
  122. };
  123. static const match_table_t cifs_mount_option_tokens = {
  124. { Opt_user_xattr, "user_xattr" },
  125. { Opt_nouser_xattr, "nouser_xattr" },
  126. { Opt_forceuid, "forceuid" },
  127. { Opt_noforceuid, "noforceuid" },
  128. { Opt_forcegid, "forcegid" },
  129. { Opt_noforcegid, "noforcegid" },
  130. { Opt_noblocksend, "noblocksend" },
  131. { Opt_noautotune, "noautotune" },
  132. { Opt_nolease, "nolease" },
  133. { Opt_hard, "hard" },
  134. { Opt_soft, "soft" },
  135. { Opt_perm, "perm" },
  136. { Opt_noperm, "noperm" },
  137. { Opt_nodelete, "nodelete" },
  138. { Opt_mapchars, "mapchars" }, /* SFU style */
  139. { Opt_nomapchars, "nomapchars" },
  140. { Opt_mapposix, "mapposix" }, /* SFM style */
  141. { Opt_nomapposix, "nomapposix" },
  142. { Opt_sfu, "sfu" },
  143. { Opt_nosfu, "nosfu" },
  144. { Opt_nodfs, "nodfs" },
  145. { Opt_posixpaths, "posixpaths" },
  146. { Opt_noposixpaths, "noposixpaths" },
  147. { Opt_nounix, "nounix" },
  148. { Opt_nounix, "nolinux" },
  149. { Opt_nounix, "noposix" },
  150. { Opt_unix, "unix" },
  151. { Opt_unix, "linux" },
  152. { Opt_unix, "posix" },
  153. { Opt_nocase, "nocase" },
  154. { Opt_nocase, "ignorecase" },
  155. { Opt_brl, "brl" },
  156. { Opt_nobrl, "nobrl" },
  157. { Opt_handlecache, "handlecache" },
  158. { Opt_nohandlecache, "nohandlecache" },
  159. { Opt_nobrl, "nolock" },
  160. { Opt_forcemandatorylock, "forcemandatorylock" },
  161. { Opt_forcemandatorylock, "forcemand" },
  162. { Opt_setuids, "setuids" },
  163. { Opt_nosetuids, "nosetuids" },
  164. { Opt_setuidfromacl, "idsfromsid" },
  165. { Opt_dynperm, "dynperm" },
  166. { Opt_nodynperm, "nodynperm" },
  167. { Opt_nohard, "nohard" },
  168. { Opt_nosoft, "nosoft" },
  169. { Opt_nointr, "nointr" },
  170. { Opt_intr, "intr" },
  171. { Opt_nostrictsync, "nostrictsync" },
  172. { Opt_strictsync, "strictsync" },
  173. { Opt_serverino, "serverino" },
  174. { Opt_noserverino, "noserverino" },
  175. { Opt_rwpidforward, "rwpidforward" },
  176. { Opt_modesid, "modefromsid" },
  177. { Opt_cifsacl, "cifsacl" },
  178. { Opt_nocifsacl, "nocifsacl" },
  179. { Opt_acl, "acl" },
  180. { Opt_noacl, "noacl" },
  181. { Opt_locallease, "locallease" },
  182. { Opt_sign, "sign" },
  183. { Opt_ignore_signature, "signloosely" },
  184. { Opt_seal, "seal" },
  185. { Opt_noac, "noac" },
  186. { Opt_fsc, "fsc" },
  187. { Opt_mfsymlinks, "mfsymlinks" },
  188. { Opt_multiuser, "multiuser" },
  189. { Opt_sloppy, "sloppy" },
  190. { Opt_nosharesock, "nosharesock" },
  191. { Opt_persistent, "persistenthandles"},
  192. { Opt_nopersistent, "nopersistenthandles"},
  193. { Opt_resilient, "resilienthandles"},
  194. { Opt_noresilient, "noresilienthandles"},
  195. { Opt_domainauto, "domainauto"},
  196. { Opt_rdma, "rdma"},
  197. { Opt_multichannel, "multichannel" },
  198. { Opt_nomultichannel, "nomultichannel" },
  199. { Opt_backupuid, "backupuid=%s" },
  200. { Opt_backupgid, "backupgid=%s" },
  201. { Opt_uid, "uid=%s" },
  202. { Opt_cruid, "cruid=%s" },
  203. { Opt_gid, "gid=%s" },
  204. { Opt_file_mode, "file_mode=%s" },
  205. { Opt_dirmode, "dirmode=%s" },
  206. { Opt_dirmode, "dir_mode=%s" },
  207. { Opt_port, "port=%s" },
  208. { Opt_min_enc_offload, "esize=%s" },
  209. { Opt_blocksize, "bsize=%s" },
  210. { Opt_rsize, "rsize=%s" },
  211. { Opt_wsize, "wsize=%s" },
  212. { Opt_actimeo, "actimeo=%s" },
  213. { Opt_handletimeout, "handletimeout=%s" },
  214. { Opt_echo_interval, "echo_interval=%s" },
  215. { Opt_max_credits, "max_credits=%s" },
  216. { Opt_snapshot, "snapshot=%s" },
  217. { Opt_max_channels, "max_channels=%s" },
  218. { Opt_compress, "compress=%s" },
  219. { Opt_blank_user, "user=" },
  220. { Opt_blank_user, "username=" },
  221. { Opt_user, "user=%s" },
  222. { Opt_user, "username=%s" },
  223. { Opt_blank_pass, "pass=" },
  224. { Opt_blank_pass, "password=" },
  225. { Opt_pass, "pass=%s" },
  226. { Opt_pass, "password=%s" },
  227. { Opt_blank_ip, "ip=" },
  228. { Opt_blank_ip, "addr=" },
  229. { Opt_ip, "ip=%s" },
  230. { Opt_ip, "addr=%s" },
  231. { Opt_ignore, "unc=%s" },
  232. { Opt_ignore, "target=%s" },
  233. { Opt_ignore, "path=%s" },
  234. { Opt_domain, "dom=%s" },
  235. { Opt_domain, "domain=%s" },
  236. { Opt_domain, "workgroup=%s" },
  237. { Opt_srcaddr, "srcaddr=%s" },
  238. { Opt_ignore, "prefixpath=%s" },
  239. { Opt_iocharset, "iocharset=%s" },
  240. { Opt_netbiosname, "netbiosname=%s" },
  241. { Opt_servern, "servern=%s" },
  242. { Opt_ver, "ver=%s" },
  243. { Opt_vers, "vers=%s" },
  244. { Opt_sec, "sec=%s" },
  245. { Opt_cache, "cache=%s" },
  246. { Opt_ignore, "cred" },
  247. { Opt_ignore, "credentials" },
  248. { Opt_ignore, "cred=%s" },
  249. { Opt_ignore, "credentials=%s" },
  250. { Opt_ignore, "guest" },
  251. { Opt_ignore, "rw" },
  252. { Opt_ignore, "ro" },
  253. { Opt_ignore, "suid" },
  254. { Opt_ignore, "nosuid" },
  255. { Opt_ignore, "exec" },
  256. { Opt_ignore, "noexec" },
  257. { Opt_ignore, "nodev" },
  258. { Opt_ignore, "noauto" },
  259. { Opt_ignore, "dev" },
  260. { Opt_ignore, "mand" },
  261. { Opt_ignore, "nomand" },
  262. { Opt_ignore, "relatime" },
  263. { Opt_ignore, "_netdev" },
  264. { Opt_rootfs, "rootfs" },
  265. { Opt_err, NULL }
  266. };
  267. static int ip_connect(struct TCP_Server_Info *server);
  268. static int generic_ip_connect(struct TCP_Server_Info *server);
  269. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  270. static void cifs_prune_tlinks(struct work_struct *work);
  271. static char *extract_hostname(const char *unc);
  272. /*
  273. * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
  274. * get their ip addresses changed at some point.
  275. *
  276. * This should be called with server->srv_mutex held.
  277. */
  278. #ifdef CONFIG_CIFS_DFS_UPCALL
  279. static int reconn_set_ipaddr(struct TCP_Server_Info *server)
  280. {
  281. int rc;
  282. int len;
  283. char *unc, *ipaddr = NULL;
  284. if (!server->hostname)
  285. return -EINVAL;
  286. len = strlen(server->hostname) + 3;
  287. unc = kmalloc(len, GFP_KERNEL);
  288. if (!unc) {
  289. cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
  290. return -ENOMEM;
  291. }
  292. scnprintf(unc, len, "\\\\%s", server->hostname);
  293. rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
  294. kfree(unc);
  295. if (rc < 0) {
  296. cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
  297. __func__, server->hostname, rc);
  298. return rc;
  299. }
  300. spin_lock(&cifs_tcp_ses_lock);
  301. rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
  302. strlen(ipaddr));
  303. spin_unlock(&cifs_tcp_ses_lock);
  304. kfree(ipaddr);
  305. return !rc ? -1 : 0;
  306. }
  307. #else
  308. static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
  309. {
  310. return 0;
  311. }
  312. #endif
  313. #ifdef CONFIG_CIFS_DFS_UPCALL
  314. /* These functions must be called with server->srv_mutex held */
  315. static void reconn_set_next_dfs_target(struct TCP_Server_Info *server,
  316. struct cifs_sb_info *cifs_sb,
  317. struct dfs_cache_tgt_list *tgt_list,
  318. struct dfs_cache_tgt_iterator **tgt_it)
  319. {
  320. const char *name;
  321. if (!cifs_sb || !cifs_sb->origin_fullpath)
  322. return;
  323. if (!*tgt_it) {
  324. *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
  325. } else {
  326. *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
  327. if (!*tgt_it)
  328. *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
  329. }
  330. cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
  331. name = dfs_cache_get_tgt_name(*tgt_it);
  332. kfree(server->hostname);
  333. server->hostname = extract_hostname(name);
  334. if (IS_ERR(server->hostname)) {
  335. cifs_dbg(FYI,
  336. "%s: failed to extract hostname from target: %ld\n",
  337. __func__, PTR_ERR(server->hostname));
  338. }
  339. }
  340. static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
  341. struct dfs_cache_tgt_list *tl)
  342. {
  343. if (!cifs_sb->origin_fullpath)
  344. return -EOPNOTSUPP;
  345. return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
  346. }
  347. #endif
  348. /*
  349. * cifs tcp session reconnection
  350. *
  351. * mark tcp session as reconnecting so temporarily locked
  352. * mark all smb sessions as reconnecting for tcp session
  353. * reconnect tcp session
  354. * wake up waiters on reconnection? - (not needed currently)
  355. */
  356. int
  357. cifs_reconnect(struct TCP_Server_Info *server)
  358. {
  359. int rc = 0;
  360. struct list_head *tmp, *tmp2;
  361. struct cifs_ses *ses;
  362. struct cifs_tcon *tcon;
  363. struct mid_q_entry *mid_entry;
  364. struct list_head retry_list;
  365. #ifdef CONFIG_CIFS_DFS_UPCALL
  366. struct super_block *sb = NULL;
  367. struct cifs_sb_info *cifs_sb = NULL;
  368. struct dfs_cache_tgt_list tgt_list = {0};
  369. struct dfs_cache_tgt_iterator *tgt_it = NULL;
  370. #endif
  371. spin_lock(&GlobalMid_Lock);
  372. server->nr_targets = 1;
  373. #ifdef CONFIG_CIFS_DFS_UPCALL
  374. spin_unlock(&GlobalMid_Lock);
  375. sb = cifs_get_tcp_super(server);
  376. if (IS_ERR(sb)) {
  377. rc = PTR_ERR(sb);
  378. cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
  379. __func__, rc);
  380. sb = NULL;
  381. } else {
  382. cifs_sb = CIFS_SB(sb);
  383. rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list);
  384. if (rc) {
  385. cifs_sb = NULL;
  386. if (rc != -EOPNOTSUPP) {
  387. cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
  388. __func__);
  389. }
  390. } else {
  391. server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
  392. }
  393. }
  394. cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
  395. server->nr_targets);
  396. spin_lock(&GlobalMid_Lock);
  397. #endif
  398. if (server->tcpStatus == CifsExiting) {
  399. /* the demux thread will exit normally
  400. next time through the loop */
  401. spin_unlock(&GlobalMid_Lock);
  402. #ifdef CONFIG_CIFS_DFS_UPCALL
  403. dfs_cache_free_tgts(&tgt_list);
  404. cifs_put_tcp_super(sb);
  405. #endif
  406. wake_up(&server->response_q);
  407. return rc;
  408. } else
  409. server->tcpStatus = CifsNeedReconnect;
  410. spin_unlock(&GlobalMid_Lock);
  411. server->maxBuf = 0;
  412. server->max_read = 0;
  413. cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
  414. trace_smb3_reconnect(server->CurrentMid, server->hostname);
  415. /* before reconnecting the tcp session, mark the smb session (uid)
  416. and the tid bad so they are not used until reconnected */
  417. cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
  418. __func__);
  419. spin_lock(&cifs_tcp_ses_lock);
  420. list_for_each(tmp, &server->smb_ses_list) {
  421. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  422. ses->need_reconnect = true;
  423. list_for_each(tmp2, &ses->tcon_list) {
  424. tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
  425. tcon->need_reconnect = true;
  426. }
  427. if (ses->tcon_ipc)
  428. ses->tcon_ipc->need_reconnect = true;
  429. }
  430. spin_unlock(&cifs_tcp_ses_lock);
  431. /* do not want to be sending data on a socket we are freeing */
  432. cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
  433. mutex_lock(&server->srv_mutex);
  434. if (server->ssocket) {
  435. cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
  436. server->ssocket->state, server->ssocket->flags);
  437. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  438. cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
  439. server->ssocket->state, server->ssocket->flags);
  440. sock_release(server->ssocket);
  441. server->ssocket = NULL;
  442. }
  443. server->sequence_number = 0;
  444. server->session_estab = false;
  445. kfree(server->session_key.response);
  446. server->session_key.response = NULL;
  447. server->session_key.len = 0;
  448. server->lstrp = jiffies;
  449. /* mark submitted MIDs for retry and issue callback */
  450. INIT_LIST_HEAD(&retry_list);
  451. cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
  452. spin_lock(&GlobalMid_Lock);
  453. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  454. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  455. kref_get(&mid_entry->refcount);
  456. if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
  457. mid_entry->mid_state = MID_RETRY_NEEDED;
  458. list_move(&mid_entry->qhead, &retry_list);
  459. mid_entry->mid_flags |= MID_DELETED;
  460. }
  461. spin_unlock(&GlobalMid_Lock);
  462. mutex_unlock(&server->srv_mutex);
  463. cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
  464. list_for_each_safe(tmp, tmp2, &retry_list) {
  465. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  466. list_del_init(&mid_entry->qhead);
  467. mid_entry->callback(mid_entry);
  468. cifs_mid_q_entry_release(mid_entry);
  469. }
  470. if (cifs_rdma_enabled(server)) {
  471. mutex_lock(&server->srv_mutex);
  472. smbd_destroy(server);
  473. mutex_unlock(&server->srv_mutex);
  474. }
  475. do {
  476. try_to_freeze();
  477. mutex_lock(&server->srv_mutex);
  478. #ifdef CONFIG_CIFS_DFS_UPCALL
  479. /*
  480. * Set up next DFS target server (if any) for reconnect. If DFS
  481. * feature is disabled, then we will retry last server we
  482. * connected to before.
  483. */
  484. reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it);
  485. #endif
  486. rc = reconn_set_ipaddr(server);
  487. if (rc) {
  488. cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
  489. __func__, rc);
  490. }
  491. if (cifs_rdma_enabled(server))
  492. rc = smbd_reconnect(server);
  493. else
  494. rc = generic_ip_connect(server);
  495. if (rc) {
  496. cifs_dbg(FYI, "reconnect error %d\n", rc);
  497. mutex_unlock(&server->srv_mutex);
  498. msleep(3000);
  499. } else {
  500. atomic_inc(&tcpSesReconnectCount);
  501. set_credits(server, 1);
  502. spin_lock(&GlobalMid_Lock);
  503. if (server->tcpStatus != CifsExiting)
  504. server->tcpStatus = CifsNeedNegotiate;
  505. spin_unlock(&GlobalMid_Lock);
  506. mutex_unlock(&server->srv_mutex);
  507. }
  508. } while (server->tcpStatus == CifsNeedReconnect);
  509. #ifdef CONFIG_CIFS_DFS_UPCALL
  510. if (tgt_it) {
  511. rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
  512. tgt_it);
  513. if (rc) {
  514. cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
  515. __func__, rc);
  516. }
  517. rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
  518. if (rc) {
  519. cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
  520. __func__, rc);
  521. }
  522. dfs_cache_free_tgts(&tgt_list);
  523. }
  524. cifs_put_tcp_super(sb);
  525. #endif
  526. if (server->tcpStatus == CifsNeedNegotiate)
  527. mod_delayed_work(cifsiod_wq, &server->echo, 0);
  528. wake_up(&server->response_q);
  529. return rc;
  530. }
  531. static void
  532. cifs_echo_request(struct work_struct *work)
  533. {
  534. int rc;
  535. struct TCP_Server_Info *server = container_of(work,
  536. struct TCP_Server_Info, echo.work);
  537. unsigned long echo_interval;
  538. /*
  539. * If we need to renegotiate, set echo interval to zero to
  540. * immediately call echo service where we can renegotiate.
  541. */
  542. if (server->tcpStatus == CifsNeedNegotiate)
  543. echo_interval = 0;
  544. else
  545. echo_interval = server->echo_interval;
  546. /*
  547. * We cannot send an echo if it is disabled.
  548. * Also, no need to ping if we got a response recently.
  549. */
  550. if (server->tcpStatus == CifsNeedReconnect ||
  551. server->tcpStatus == CifsExiting ||
  552. server->tcpStatus == CifsNew ||
  553. (server->ops->can_echo && !server->ops->can_echo(server)) ||
  554. time_before(jiffies, server->lstrp + echo_interval - HZ))
  555. goto requeue_echo;
  556. rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
  557. if (rc)
  558. cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
  559. server->hostname);
  560. requeue_echo:
  561. queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
  562. }
  563. static bool
  564. allocate_buffers(struct TCP_Server_Info *server)
  565. {
  566. if (!server->bigbuf) {
  567. server->bigbuf = (char *)cifs_buf_get();
  568. if (!server->bigbuf) {
  569. cifs_server_dbg(VFS, "No memory for large SMB response\n");
  570. msleep(3000);
  571. /* retry will check if exiting */
  572. return false;
  573. }
  574. } else if (server->large_buf) {
  575. /* we are reusing a dirty large buf, clear its start */
  576. memset(server->bigbuf, 0, HEADER_SIZE(server));
  577. }
  578. if (!server->smallbuf) {
  579. server->smallbuf = (char *)cifs_small_buf_get();
  580. if (!server->smallbuf) {
  581. cifs_server_dbg(VFS, "No memory for SMB response\n");
  582. msleep(1000);
  583. /* retry will check if exiting */
  584. return false;
  585. }
  586. /* beginning of smb buffer is cleared in our buf_get */
  587. } else {
  588. /* if existing small buf clear beginning */
  589. memset(server->smallbuf, 0, HEADER_SIZE(server));
  590. }
  591. return true;
  592. }
  593. static bool
  594. server_unresponsive(struct TCP_Server_Info *server)
  595. {
  596. /*
  597. * We need to wait 3 echo intervals to make sure we handle such
  598. * situations right:
  599. * 1s client sends a normal SMB request
  600. * 2s client gets a response
  601. * 30s echo workqueue job pops, and decides we got a response recently
  602. * and don't need to send another
  603. * ...
  604. * 65s kernel_recvmsg times out, and we see that we haven't gotten
  605. * a response in >60s.
  606. */
  607. if ((server->tcpStatus == CifsGood ||
  608. server->tcpStatus == CifsNeedNegotiate) &&
  609. (!server->ops->can_echo || server->ops->can_echo(server)) &&
  610. time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
  611. cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
  612. (3 * server->echo_interval) / HZ);
  613. cifs_reconnect(server);
  614. return true;
  615. }
  616. return false;
  617. }
  618. static inline bool
  619. zero_credits(struct TCP_Server_Info *server)
  620. {
  621. int val;
  622. spin_lock(&server->req_lock);
  623. val = server->credits + server->echo_credits + server->oplock_credits;
  624. if (server->in_flight == 0 && val == 0) {
  625. spin_unlock(&server->req_lock);
  626. return true;
  627. }
  628. spin_unlock(&server->req_lock);
  629. return false;
  630. }
  631. static int
  632. cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
  633. {
  634. int length = 0;
  635. int total_read;
  636. smb_msg->msg_control = NULL;
  637. smb_msg->msg_controllen = 0;
  638. for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
  639. try_to_freeze();
  640. /* reconnect if no credits and no requests in flight */
  641. if (zero_credits(server)) {
  642. cifs_reconnect(server);
  643. return -ECONNABORTED;
  644. }
  645. if (server_unresponsive(server))
  646. return -ECONNABORTED;
  647. if (cifs_rdma_enabled(server) && server->smbd_conn)
  648. length = smbd_recv(server->smbd_conn, smb_msg);
  649. else
  650. length = sock_recvmsg(server->ssocket, smb_msg, 0);
  651. if (server->tcpStatus == CifsExiting)
  652. return -ESHUTDOWN;
  653. if (server->tcpStatus == CifsNeedReconnect) {
  654. cifs_reconnect(server);
  655. return -ECONNABORTED;
  656. }
  657. if (length == -ERESTARTSYS ||
  658. length == -EAGAIN ||
  659. length == -EINTR) {
  660. /*
  661. * Minimum sleep to prevent looping, allowing socket
  662. * to clear and app threads to set tcpStatus
  663. * CifsNeedReconnect if server hung.
  664. */
  665. usleep_range(1000, 2000);
  666. length = 0;
  667. continue;
  668. }
  669. if (length <= 0) {
  670. cifs_dbg(FYI, "Received no data or error: %d\n", length);
  671. cifs_reconnect(server);
  672. return -ECONNABORTED;
  673. }
  674. }
  675. return total_read;
  676. }
  677. int
  678. cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
  679. unsigned int to_read)
  680. {
  681. struct msghdr smb_msg;
  682. struct kvec iov = {.iov_base = buf, .iov_len = to_read};
  683. iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
  684. return cifs_readv_from_socket(server, &smb_msg);
  685. }
  686. int
  687. cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
  688. unsigned int page_offset, unsigned int to_read)
  689. {
  690. struct msghdr smb_msg;
  691. struct bio_vec bv = {
  692. .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
  693. iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
  694. return cifs_readv_from_socket(server, &smb_msg);
  695. }
  696. static bool
  697. is_smb_response(struct TCP_Server_Info *server, unsigned char type)
  698. {
  699. /*
  700. * The first byte big endian of the length field,
  701. * is actually not part of the length but the type
  702. * with the most common, zero, as regular data.
  703. */
  704. switch (type) {
  705. case RFC1002_SESSION_MESSAGE:
  706. /* Regular SMB response */
  707. return true;
  708. case RFC1002_SESSION_KEEP_ALIVE:
  709. cifs_dbg(FYI, "RFC 1002 session keep alive\n");
  710. break;
  711. case RFC1002_POSITIVE_SESSION_RESPONSE:
  712. cifs_dbg(FYI, "RFC 1002 positive session response\n");
  713. break;
  714. case RFC1002_NEGATIVE_SESSION_RESPONSE:
  715. /*
  716. * We get this from Windows 98 instead of an error on
  717. * SMB negprot response.
  718. */
  719. cifs_dbg(FYI, "RFC 1002 negative session response\n");
  720. /* give server a second to clean up */
  721. msleep(1000);
  722. /*
  723. * Always try 445 first on reconnect since we get NACK
  724. * on some if we ever connected to port 139 (the NACK
  725. * is since we do not begin with RFC1001 session
  726. * initialize frame).
  727. */
  728. cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
  729. cifs_reconnect(server);
  730. break;
  731. default:
  732. cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
  733. cifs_reconnect(server);
  734. }
  735. return false;
  736. }
  737. void
  738. dequeue_mid(struct mid_q_entry *mid, bool malformed)
  739. {
  740. #ifdef CONFIG_CIFS_STATS2
  741. mid->when_received = jiffies;
  742. #endif
  743. spin_lock(&GlobalMid_Lock);
  744. if (!malformed)
  745. mid->mid_state = MID_RESPONSE_RECEIVED;
  746. else
  747. mid->mid_state = MID_RESPONSE_MALFORMED;
  748. /*
  749. * Trying to handle/dequeue a mid after the send_recv()
  750. * function has finished processing it is a bug.
  751. */
  752. if (mid->mid_flags & MID_DELETED)
  753. pr_warn_once("trying to dequeue a deleted mid\n");
  754. else {
  755. list_del_init(&mid->qhead);
  756. mid->mid_flags |= MID_DELETED;
  757. }
  758. spin_unlock(&GlobalMid_Lock);
  759. }
  760. static unsigned int
  761. smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
  762. {
  763. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
  764. /*
  765. * SMB1 does not use credits.
  766. */
  767. if (server->vals->header_preamble_size)
  768. return 0;
  769. return le16_to_cpu(shdr->CreditRequest);
  770. }
  771. static void
  772. handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  773. char *buf, int malformed)
  774. {
  775. if (server->ops->check_trans2 &&
  776. server->ops->check_trans2(mid, server, buf, malformed))
  777. return;
  778. mid->credits_received = smb2_get_credits_from_hdr(buf, server);
  779. mid->resp_buf = buf;
  780. mid->large_buf = server->large_buf;
  781. /* Was previous buf put in mpx struct for multi-rsp? */
  782. if (!mid->multiRsp) {
  783. /* smb buffer will be freed by user thread */
  784. if (server->large_buf)
  785. server->bigbuf = NULL;
  786. else
  787. server->smallbuf = NULL;
  788. }
  789. dequeue_mid(mid, malformed);
  790. }
  791. static void clean_demultiplex_info(struct TCP_Server_Info *server)
  792. {
  793. int length;
  794. /* take it off the list, if it's not already */
  795. spin_lock(&cifs_tcp_ses_lock);
  796. list_del_init(&server->tcp_ses_list);
  797. spin_unlock(&cifs_tcp_ses_lock);
  798. cancel_delayed_work_sync(&server->echo);
  799. spin_lock(&GlobalMid_Lock);
  800. server->tcpStatus = CifsExiting;
  801. spin_unlock(&GlobalMid_Lock);
  802. wake_up_all(&server->response_q);
  803. /* check if we have blocked requests that need to free */
  804. spin_lock(&server->req_lock);
  805. if (server->credits <= 0)
  806. server->credits = 1;
  807. spin_unlock(&server->req_lock);
  808. /*
  809. * Although there should not be any requests blocked on this queue it
  810. * can not hurt to be paranoid and try to wake up requests that may
  811. * haven been blocked when more than 50 at time were on the wire to the
  812. * same server - they now will see the session is in exit state and get
  813. * out of SendReceive.
  814. */
  815. wake_up_all(&server->request_q);
  816. /* give those requests time to exit */
  817. msleep(125);
  818. if (cifs_rdma_enabled(server))
  819. smbd_destroy(server);
  820. if (server->ssocket) {
  821. sock_release(server->ssocket);
  822. server->ssocket = NULL;
  823. }
  824. if (!list_empty(&server->pending_mid_q)) {
  825. struct list_head dispose_list;
  826. struct mid_q_entry *mid_entry;
  827. struct list_head *tmp, *tmp2;
  828. INIT_LIST_HEAD(&dispose_list);
  829. spin_lock(&GlobalMid_Lock);
  830. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  831. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  832. cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
  833. kref_get(&mid_entry->refcount);
  834. mid_entry->mid_state = MID_SHUTDOWN;
  835. list_move(&mid_entry->qhead, &dispose_list);
  836. mid_entry->mid_flags |= MID_DELETED;
  837. }
  838. spin_unlock(&GlobalMid_Lock);
  839. /* now walk dispose list and issue callbacks */
  840. list_for_each_safe(tmp, tmp2, &dispose_list) {
  841. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  842. cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
  843. list_del_init(&mid_entry->qhead);
  844. mid_entry->callback(mid_entry);
  845. cifs_mid_q_entry_release(mid_entry);
  846. }
  847. /* 1/8th of sec is more than enough time for them to exit */
  848. msleep(125);
  849. }
  850. if (!list_empty(&server->pending_mid_q)) {
  851. /*
  852. * mpx threads have not exited yet give them at least the smb
  853. * send timeout time for long ops.
  854. *
  855. * Due to delays on oplock break requests, we need to wait at
  856. * least 45 seconds before giving up on a request getting a
  857. * response and going ahead and killing cifsd.
  858. */
  859. cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
  860. msleep(46000);
  861. /*
  862. * If threads still have not exited they are probably never
  863. * coming home not much else we can do but free the memory.
  864. */
  865. }
  866. kfree(server->hostname);
  867. kfree(server);
  868. length = atomic_dec_return(&tcpSesAllocCount);
  869. if (length > 0)
  870. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  871. }
  872. static int
  873. standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  874. {
  875. int length;
  876. char *buf = server->smallbuf;
  877. unsigned int pdu_length = server->pdu_size;
  878. /* make sure this will fit in a large buffer */
  879. if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
  880. server->vals->header_preamble_size) {
  881. cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
  882. cifs_reconnect(server);
  883. return -ECONNABORTED;
  884. }
  885. /* switch to large buffer if too big for a small one */
  886. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  887. server->large_buf = true;
  888. memcpy(server->bigbuf, buf, server->total_read);
  889. buf = server->bigbuf;
  890. }
  891. /* now read the rest */
  892. length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
  893. pdu_length - HEADER_SIZE(server) + 1
  894. + server->vals->header_preamble_size);
  895. if (length < 0)
  896. return length;
  897. server->total_read += length;
  898. dump_smb(buf, server->total_read);
  899. return cifs_handle_standard(server, mid);
  900. }
  901. int
  902. cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  903. {
  904. char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
  905. int length;
  906. /*
  907. * We know that we received enough to get to the MID as we
  908. * checked the pdu_length earlier. Now check to see
  909. * if the rest of the header is OK. We borrow the length
  910. * var for the rest of the loop to avoid a new stack var.
  911. *
  912. * 48 bytes is enough to display the header and a little bit
  913. * into the payload for debugging purposes.
  914. */
  915. length = server->ops->check_message(buf, server->total_read, server);
  916. if (length != 0)
  917. cifs_dump_mem("Bad SMB: ", buf,
  918. min_t(unsigned int, server->total_read, 48));
  919. if (server->ops->is_session_expired &&
  920. server->ops->is_session_expired(buf)) {
  921. cifs_reconnect(server);
  922. return -1;
  923. }
  924. if (server->ops->is_status_pending &&
  925. server->ops->is_status_pending(buf, server))
  926. return -1;
  927. if (!mid)
  928. return length;
  929. handle_mid(mid, server, buf, length);
  930. return 0;
  931. }
  932. static void
  933. smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
  934. {
  935. struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
  936. /*
  937. * SMB1 does not use credits.
  938. */
  939. if (server->vals->header_preamble_size)
  940. return;
  941. if (shdr->CreditRequest) {
  942. spin_lock(&server->req_lock);
  943. server->credits += le16_to_cpu(shdr->CreditRequest);
  944. spin_unlock(&server->req_lock);
  945. wake_up(&server->request_q);
  946. }
  947. }
  948. static int
  949. cifs_demultiplex_thread(void *p)
  950. {
  951. int i, num_mids, length;
  952. struct TCP_Server_Info *server = p;
  953. unsigned int pdu_length;
  954. unsigned int next_offset;
  955. char *buf = NULL;
  956. struct task_struct *task_to_wake = NULL;
  957. struct mid_q_entry *mids[MAX_COMPOUND];
  958. char *bufs[MAX_COMPOUND];
  959. unsigned int noreclaim_flag, num_io_timeout = 0;
  960. noreclaim_flag = memalloc_noreclaim_save();
  961. cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
  962. length = atomic_inc_return(&tcpSesAllocCount);
  963. if (length > 1)
  964. mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
  965. set_freezable();
  966. allow_kernel_signal(SIGKILL);
  967. while (server->tcpStatus != CifsExiting) {
  968. if (try_to_freeze())
  969. continue;
  970. if (!allocate_buffers(server))
  971. continue;
  972. server->large_buf = false;
  973. buf = server->smallbuf;
  974. pdu_length = 4; /* enough to get RFC1001 header */
  975. length = cifs_read_from_socket(server, buf, pdu_length);
  976. if (length < 0)
  977. continue;
  978. if (server->vals->header_preamble_size == 0)
  979. server->total_read = 0;
  980. else
  981. server->total_read = length;
  982. /*
  983. * The right amount was read from socket - 4 bytes,
  984. * so we can now interpret the length field.
  985. */
  986. pdu_length = get_rfc1002_length(buf);
  987. cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
  988. if (!is_smb_response(server, buf[0]))
  989. continue;
  990. next_pdu:
  991. server->pdu_size = pdu_length;
  992. /* make sure we have enough to get to the MID */
  993. if (server->pdu_size < HEADER_SIZE(server) - 1 -
  994. server->vals->header_preamble_size) {
  995. cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
  996. server->pdu_size);
  997. cifs_reconnect(server);
  998. continue;
  999. }
  1000. /* read down to the MID */
  1001. length = cifs_read_from_socket(server,
  1002. buf + server->vals->header_preamble_size,
  1003. HEADER_SIZE(server) - 1
  1004. - server->vals->header_preamble_size);
  1005. if (length < 0)
  1006. continue;
  1007. server->total_read += length;
  1008. if (server->ops->next_header) {
  1009. next_offset = server->ops->next_header(buf);
  1010. if (next_offset)
  1011. server->pdu_size = next_offset;
  1012. }
  1013. memset(mids, 0, sizeof(mids));
  1014. memset(bufs, 0, sizeof(bufs));
  1015. num_mids = 0;
  1016. if (server->ops->is_transform_hdr &&
  1017. server->ops->receive_transform &&
  1018. server->ops->is_transform_hdr(buf)) {
  1019. length = server->ops->receive_transform(server,
  1020. mids,
  1021. bufs,
  1022. &num_mids);
  1023. } else {
  1024. mids[0] = server->ops->find_mid(server, buf);
  1025. bufs[0] = buf;
  1026. num_mids = 1;
  1027. if (!mids[0] || !mids[0]->receive)
  1028. length = standard_receive3(server, mids[0]);
  1029. else
  1030. length = mids[0]->receive(server, mids[0]);
  1031. }
  1032. if (length < 0) {
  1033. for (i = 0; i < num_mids; i++)
  1034. if (mids[i])
  1035. cifs_mid_q_entry_release(mids[i]);
  1036. continue;
  1037. }
  1038. if (server->ops->is_status_io_timeout &&
  1039. server->ops->is_status_io_timeout(buf)) {
  1040. num_io_timeout++;
  1041. if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
  1042. cifs_reconnect(server);
  1043. num_io_timeout = 0;
  1044. continue;
  1045. }
  1046. }
  1047. server->lstrp = jiffies;
  1048. for (i = 0; i < num_mids; i++) {
  1049. if (mids[i] != NULL) {
  1050. mids[i]->resp_buf_size = server->pdu_size;
  1051. if (!mids[i]->multiRsp || mids[i]->multiEnd)
  1052. mids[i]->callback(mids[i]);
  1053. cifs_mid_q_entry_release(mids[i]);
  1054. } else if (server->ops->is_oplock_break &&
  1055. server->ops->is_oplock_break(bufs[i],
  1056. server)) {
  1057. smb2_add_credits_from_hdr(bufs[i], server);
  1058. cifs_dbg(FYI, "Received oplock break\n");
  1059. } else {
  1060. cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
  1061. atomic_read(&midCount));
  1062. cifs_dump_mem("Received Data is: ", bufs[i],
  1063. HEADER_SIZE(server));
  1064. smb2_add_credits_from_hdr(bufs[i], server);
  1065. #ifdef CONFIG_CIFS_DEBUG2
  1066. if (server->ops->dump_detail)
  1067. server->ops->dump_detail(bufs[i],
  1068. server);
  1069. cifs_dump_mids(server);
  1070. #endif /* CIFS_DEBUG2 */
  1071. }
  1072. }
  1073. if (pdu_length > server->pdu_size) {
  1074. if (!allocate_buffers(server))
  1075. continue;
  1076. pdu_length -= server->pdu_size;
  1077. server->total_read = 0;
  1078. server->large_buf = false;
  1079. buf = server->smallbuf;
  1080. goto next_pdu;
  1081. }
  1082. } /* end while !EXITING */
  1083. /* buffer usually freed in free_mid - need to free it here on exit */
  1084. cifs_buf_release(server->bigbuf);
  1085. if (server->smallbuf) /* no sense logging a debug message if NULL */
  1086. cifs_small_buf_release(server->smallbuf);
  1087. task_to_wake = xchg(&server->tsk, NULL);
  1088. clean_demultiplex_info(server);
  1089. /* if server->tsk was NULL then wait for a signal before exiting */
  1090. if (!task_to_wake) {
  1091. set_current_state(TASK_INTERRUPTIBLE);
  1092. while (!signal_pending(current)) {
  1093. schedule();
  1094. set_current_state(TASK_INTERRUPTIBLE);
  1095. }
  1096. set_current_state(TASK_RUNNING);
  1097. }
  1098. memalloc_noreclaim_restore(noreclaim_flag);
  1099. module_put_and_exit(0);
  1100. }
  1101. /* extract the host portion of the UNC string */
  1102. static char *
  1103. extract_hostname(const char *unc)
  1104. {
  1105. const char *src;
  1106. char *dst, *delim;
  1107. unsigned int len;
  1108. /* skip double chars at beginning of string */
  1109. /* BB: check validity of these bytes? */
  1110. if (strlen(unc) < 3)
  1111. return ERR_PTR(-EINVAL);
  1112. for (src = unc; *src && *src == '\\'; src++)
  1113. ;
  1114. if (!*src)
  1115. return ERR_PTR(-EINVAL);
  1116. /* delimiter between hostname and sharename is always '\\' now */
  1117. delim = strchr(src, '\\');
  1118. if (!delim)
  1119. return ERR_PTR(-EINVAL);
  1120. len = delim - src;
  1121. dst = kmalloc((len + 1), GFP_KERNEL);
  1122. if (dst == NULL)
  1123. return ERR_PTR(-ENOMEM);
  1124. memcpy(dst, src, len);
  1125. dst[len] = '\0';
  1126. return dst;
  1127. }
  1128. static int get_option_ul(substring_t args[], unsigned long *option)
  1129. {
  1130. int rc;
  1131. char *string;
  1132. string = match_strdup(args);
  1133. if (string == NULL)
  1134. return -ENOMEM;
  1135. rc = kstrtoul(string, 0, option);
  1136. kfree(string);
  1137. return rc;
  1138. }
  1139. static int get_option_uid(substring_t args[], kuid_t *result)
  1140. {
  1141. unsigned long value;
  1142. kuid_t uid;
  1143. int rc;
  1144. rc = get_option_ul(args, &value);
  1145. if (rc)
  1146. return rc;
  1147. uid = make_kuid(current_user_ns(), value);
  1148. if (!uid_valid(uid))
  1149. return -EINVAL;
  1150. *result = uid;
  1151. return 0;
  1152. }
  1153. static int get_option_gid(substring_t args[], kgid_t *result)
  1154. {
  1155. unsigned long value;
  1156. kgid_t gid;
  1157. int rc;
  1158. rc = get_option_ul(args, &value);
  1159. if (rc)
  1160. return rc;
  1161. gid = make_kgid(current_user_ns(), value);
  1162. if (!gid_valid(gid))
  1163. return -EINVAL;
  1164. *result = gid;
  1165. return 0;
  1166. }
  1167. /*
  1168. * Parse a devname into substrings and populate the vol->UNC and vol->prepath
  1169. * fields with the result. Returns 0 on success and an error otherwise.
  1170. */
  1171. static int
  1172. cifs_parse_devname(const char *devname, struct smb_vol *vol)
  1173. {
  1174. char *pos;
  1175. const char *delims = "/\\";
  1176. size_t len;
  1177. if (unlikely(!devname || !*devname)) {
  1178. cifs_dbg(VFS, "Device name not specified\n");
  1179. return -EINVAL;
  1180. }
  1181. /* make sure we have a valid UNC double delimiter prefix */
  1182. len = strspn(devname, delims);
  1183. if (len != 2)
  1184. return -EINVAL;
  1185. /* find delimiter between host and sharename */
  1186. pos = strpbrk(devname + 2, delims);
  1187. if (!pos)
  1188. return -EINVAL;
  1189. /* skip past delimiter */
  1190. ++pos;
  1191. /* now go until next delimiter or end of string */
  1192. len = strcspn(pos, delims);
  1193. /* move "pos" up to delimiter or NULL */
  1194. pos += len;
  1195. vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
  1196. if (!vol->UNC)
  1197. return -ENOMEM;
  1198. convert_delimiter(vol->UNC, '\\');
  1199. /* skip any delimiter */
  1200. if (*pos == '/' || *pos == '\\')
  1201. pos++;
  1202. /* If pos is NULL then no prepath */
  1203. if (!*pos)
  1204. return 0;
  1205. vol->prepath = kstrdup(pos, GFP_KERNEL);
  1206. if (!vol->prepath)
  1207. return -ENOMEM;
  1208. return 0;
  1209. }
  1210. static int
  1211. cifs_parse_mount_options(const char *mountdata, const char *devname,
  1212. struct smb_vol *vol, bool is_smb3)
  1213. {
  1214. char *data, *end;
  1215. char *mountdata_copy = NULL, *options;
  1216. unsigned int temp_len, i, j;
  1217. char separator[2];
  1218. short int override_uid = -1;
  1219. short int override_gid = -1;
  1220. bool uid_specified = false;
  1221. bool gid_specified = false;
  1222. bool sloppy = false;
  1223. char *invalid = NULL;
  1224. char *nodename = utsname()->nodename;
  1225. char *string = NULL;
  1226. char *tmp_end, *value;
  1227. char delim;
  1228. bool got_ip = false;
  1229. bool got_version = false;
  1230. unsigned short port = 0;
  1231. struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
  1232. separator[0] = ',';
  1233. separator[1] = 0;
  1234. delim = separator[0];
  1235. /* ensure we always start with zeroed-out smb_vol */
  1236. memset(vol, 0, sizeof(*vol));
  1237. /*
  1238. * does not have to be perfect mapping since field is
  1239. * informational, only used for servers that do not support
  1240. * port 445 and it can be overridden at mount time
  1241. */
  1242. memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
  1243. for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
  1244. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  1245. vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
  1246. /* null target name indicates to use *SMBSERVR default called name
  1247. if we end up sending RFC1001 session initialize */
  1248. vol->target_rfc1001_name[0] = 0;
  1249. vol->cred_uid = current_uid();
  1250. vol->linux_uid = current_uid();
  1251. vol->linux_gid = current_gid();
  1252. vol->bsize = 1024 * 1024; /* can improve cp performance significantly */
  1253. /*
  1254. * default to SFM style remapping of seven reserved characters
  1255. * unless user overrides it or we negotiate CIFS POSIX where
  1256. * it is unnecessary. Can not simultaneously use more than one mapping
  1257. * since then readdir could list files that open could not open
  1258. */
  1259. vol->remap = true;
  1260. /* default to only allowing write access to owner of the mount */
  1261. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  1262. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  1263. /* default is always to request posix paths. */
  1264. vol->posix_paths = 1;
  1265. /* default to using server inode numbers where available */
  1266. vol->server_ino = 1;
  1267. /* default is to use strict cifs caching semantics */
  1268. vol->strict_io = true;
  1269. vol->actimeo = CIFS_DEF_ACTIMEO;
  1270. /* Most clients set timeout to 0, allows server to use its default */
  1271. vol->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
  1272. /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
  1273. vol->ops = &smb30_operations;
  1274. vol->vals = &smbdefault_values;
  1275. vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
  1276. /* default to no multichannel (single server connection) */
  1277. vol->multichannel = false;
  1278. vol->max_channels = 1;
  1279. if (!mountdata)
  1280. goto cifs_parse_mount_err;
  1281. mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
  1282. if (!mountdata_copy)
  1283. goto cifs_parse_mount_err;
  1284. options = mountdata_copy;
  1285. end = options + strlen(options);
  1286. if (strncmp(options, "sep=", 4) == 0) {
  1287. if (options[4] != 0) {
  1288. separator[0] = options[4];
  1289. options += 5;
  1290. } else {
  1291. cifs_dbg(FYI, "Null separator not allowed\n");
  1292. }
  1293. }
  1294. vol->backupuid_specified = false; /* no backup intent for a user */
  1295. vol->backupgid_specified = false; /* no backup intent for a group */
  1296. switch (cifs_parse_devname(devname, vol)) {
  1297. case 0:
  1298. break;
  1299. case -ENOMEM:
  1300. cifs_dbg(VFS, "Unable to allocate memory for devname\n");
  1301. goto cifs_parse_mount_err;
  1302. case -EINVAL:
  1303. cifs_dbg(VFS, "Malformed UNC in devname\n");
  1304. goto cifs_parse_mount_err;
  1305. default:
  1306. cifs_dbg(VFS, "Unknown error parsing devname\n");
  1307. goto cifs_parse_mount_err;
  1308. }
  1309. while ((data = strsep(&options, separator)) != NULL) {
  1310. substring_t args[MAX_OPT_ARGS];
  1311. unsigned long option;
  1312. int token;
  1313. if (!*data)
  1314. continue;
  1315. token = match_token(data, cifs_mount_option_tokens, args);
  1316. switch (token) {
  1317. /* Ingnore the following */
  1318. case Opt_ignore:
  1319. break;
  1320. /* Boolean values */
  1321. case Opt_user_xattr:
  1322. vol->no_xattr = 0;
  1323. break;
  1324. case Opt_nouser_xattr:
  1325. vol->no_xattr = 1;
  1326. break;
  1327. case Opt_forceuid:
  1328. override_uid = 1;
  1329. break;
  1330. case Opt_noforceuid:
  1331. override_uid = 0;
  1332. break;
  1333. case Opt_forcegid:
  1334. override_gid = 1;
  1335. break;
  1336. case Opt_noforcegid:
  1337. override_gid = 0;
  1338. break;
  1339. case Opt_noblocksend:
  1340. vol->noblocksnd = 1;
  1341. break;
  1342. case Opt_noautotune:
  1343. vol->noautotune = 1;
  1344. break;
  1345. case Opt_nolease:
  1346. vol->no_lease = 1;
  1347. break;
  1348. case Opt_hard:
  1349. vol->retry = 1;
  1350. break;
  1351. case Opt_soft:
  1352. vol->retry = 0;
  1353. break;
  1354. case Opt_perm:
  1355. vol->noperm = 0;
  1356. break;
  1357. case Opt_noperm:
  1358. vol->noperm = 1;
  1359. break;
  1360. case Opt_nodelete:
  1361. vol->nodelete = 1;
  1362. break;
  1363. case Opt_mapchars:
  1364. vol->sfu_remap = true;
  1365. vol->remap = false; /* disable SFM mapping */
  1366. break;
  1367. case Opt_nomapchars:
  1368. vol->sfu_remap = false;
  1369. break;
  1370. case Opt_mapposix:
  1371. vol->remap = true;
  1372. vol->sfu_remap = false; /* disable SFU mapping */
  1373. break;
  1374. case Opt_nomapposix:
  1375. vol->remap = false;
  1376. break;
  1377. case Opt_sfu:
  1378. vol->sfu_emul = 1;
  1379. break;
  1380. case Opt_nosfu:
  1381. vol->sfu_emul = 0;
  1382. break;
  1383. case Opt_nodfs:
  1384. vol->nodfs = 1;
  1385. break;
  1386. case Opt_rootfs:
  1387. #ifdef CONFIG_CIFS_ROOT
  1388. vol->rootfs = true;
  1389. #endif
  1390. break;
  1391. case Opt_posixpaths:
  1392. vol->posix_paths = 1;
  1393. break;
  1394. case Opt_noposixpaths:
  1395. vol->posix_paths = 0;
  1396. break;
  1397. case Opt_nounix:
  1398. if (vol->linux_ext)
  1399. cifs_dbg(VFS,
  1400. "conflicting unix mount options\n");
  1401. vol->no_linux_ext = 1;
  1402. break;
  1403. case Opt_unix:
  1404. if (vol->no_linux_ext)
  1405. cifs_dbg(VFS,
  1406. "conflicting unix mount options\n");
  1407. vol->linux_ext = 1;
  1408. break;
  1409. case Opt_nocase:
  1410. vol->nocase = 1;
  1411. break;
  1412. case Opt_brl:
  1413. vol->nobrl = 0;
  1414. break;
  1415. case Opt_nobrl:
  1416. vol->nobrl = 1;
  1417. /*
  1418. * turn off mandatory locking in mode
  1419. * if remote locking is turned off since the
  1420. * local vfs will do advisory
  1421. */
  1422. if (vol->file_mode ==
  1423. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1424. vol->file_mode = S_IALLUGO;
  1425. break;
  1426. case Opt_nohandlecache:
  1427. vol->nohandlecache = 1;
  1428. break;
  1429. case Opt_handlecache:
  1430. vol->nohandlecache = 0;
  1431. break;
  1432. case Opt_forcemandatorylock:
  1433. vol->mand_lock = 1;
  1434. break;
  1435. case Opt_setuids:
  1436. vol->setuids = 1;
  1437. break;
  1438. case Opt_nosetuids:
  1439. vol->setuids = 0;
  1440. break;
  1441. case Opt_setuidfromacl:
  1442. vol->setuidfromacl = 1;
  1443. break;
  1444. case Opt_dynperm:
  1445. vol->dynperm = true;
  1446. break;
  1447. case Opt_nodynperm:
  1448. vol->dynperm = false;
  1449. break;
  1450. case Opt_nohard:
  1451. vol->retry = 0;
  1452. break;
  1453. case Opt_nosoft:
  1454. vol->retry = 1;
  1455. break;
  1456. case Opt_nointr:
  1457. vol->intr = 0;
  1458. break;
  1459. case Opt_intr:
  1460. vol->intr = 1;
  1461. break;
  1462. case Opt_nostrictsync:
  1463. vol->nostrictsync = 1;
  1464. break;
  1465. case Opt_strictsync:
  1466. vol->nostrictsync = 0;
  1467. break;
  1468. case Opt_serverino:
  1469. vol->server_ino = 1;
  1470. break;
  1471. case Opt_noserverino:
  1472. vol->server_ino = 0;
  1473. break;
  1474. case Opt_rwpidforward:
  1475. vol->rwpidforward = 1;
  1476. break;
  1477. case Opt_modesid:
  1478. vol->mode_ace = 1;
  1479. break;
  1480. case Opt_cifsacl:
  1481. vol->cifs_acl = 1;
  1482. break;
  1483. case Opt_nocifsacl:
  1484. vol->cifs_acl = 0;
  1485. break;
  1486. case Opt_acl:
  1487. vol->no_psx_acl = 0;
  1488. break;
  1489. case Opt_noacl:
  1490. vol->no_psx_acl = 1;
  1491. break;
  1492. case Opt_locallease:
  1493. vol->local_lease = 1;
  1494. break;
  1495. case Opt_sign:
  1496. vol->sign = true;
  1497. break;
  1498. case Opt_ignore_signature:
  1499. vol->sign = true;
  1500. vol->ignore_signature = true;
  1501. break;
  1502. case Opt_seal:
  1503. /* we do not do the following in secFlags because seal
  1504. * is a per tree connection (mount) not a per socket
  1505. * or per-smb connection option in the protocol
  1506. * vol->secFlg |= CIFSSEC_MUST_SEAL;
  1507. */
  1508. vol->seal = 1;
  1509. break;
  1510. case Opt_noac:
  1511. pr_warn("Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
  1512. break;
  1513. case Opt_fsc:
  1514. #ifndef CONFIG_CIFS_FSCACHE
  1515. cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
  1516. goto cifs_parse_mount_err;
  1517. #endif
  1518. vol->fsc = true;
  1519. break;
  1520. case Opt_mfsymlinks:
  1521. vol->mfsymlinks = true;
  1522. break;
  1523. case Opt_multiuser:
  1524. vol->multiuser = true;
  1525. break;
  1526. case Opt_sloppy:
  1527. sloppy = true;
  1528. break;
  1529. case Opt_nosharesock:
  1530. vol->nosharesock = true;
  1531. break;
  1532. case Opt_nopersistent:
  1533. vol->nopersistent = true;
  1534. if (vol->persistent) {
  1535. cifs_dbg(VFS,
  1536. "persistenthandles mount options conflict\n");
  1537. goto cifs_parse_mount_err;
  1538. }
  1539. break;
  1540. case Opt_persistent:
  1541. vol->persistent = true;
  1542. if ((vol->nopersistent) || (vol->resilient)) {
  1543. cifs_dbg(VFS,
  1544. "persistenthandles mount options conflict\n");
  1545. goto cifs_parse_mount_err;
  1546. }
  1547. break;
  1548. case Opt_resilient:
  1549. vol->resilient = true;
  1550. if (vol->persistent) {
  1551. cifs_dbg(VFS,
  1552. "persistenthandles mount options conflict\n");
  1553. goto cifs_parse_mount_err;
  1554. }
  1555. break;
  1556. case Opt_noresilient:
  1557. vol->resilient = false; /* already the default */
  1558. break;
  1559. case Opt_domainauto:
  1560. vol->domainauto = true;
  1561. break;
  1562. case Opt_rdma:
  1563. vol->rdma = true;
  1564. break;
  1565. case Opt_multichannel:
  1566. vol->multichannel = true;
  1567. /* if number of channels not specified, default to 2 */
  1568. if (vol->max_channels < 2)
  1569. vol->max_channels = 2;
  1570. break;
  1571. case Opt_nomultichannel:
  1572. vol->multichannel = false;
  1573. vol->max_channels = 1;
  1574. break;
  1575. case Opt_compress:
  1576. vol->compression = UNKNOWN_TYPE;
  1577. cifs_dbg(VFS,
  1578. "SMB3 compression support is experimental\n");
  1579. break;
  1580. /* Numeric Values */
  1581. case Opt_backupuid:
  1582. if (get_option_uid(args, &vol->backupuid)) {
  1583. cifs_dbg(VFS, "%s: Invalid backupuid value\n",
  1584. __func__);
  1585. goto cifs_parse_mount_err;
  1586. }
  1587. vol->backupuid_specified = true;
  1588. break;
  1589. case Opt_backupgid:
  1590. if (get_option_gid(args, &vol->backupgid)) {
  1591. cifs_dbg(VFS, "%s: Invalid backupgid value\n",
  1592. __func__);
  1593. goto cifs_parse_mount_err;
  1594. }
  1595. vol->backupgid_specified = true;
  1596. break;
  1597. case Opt_uid:
  1598. if (get_option_uid(args, &vol->linux_uid)) {
  1599. cifs_dbg(VFS, "%s: Invalid uid value\n",
  1600. __func__);
  1601. goto cifs_parse_mount_err;
  1602. }
  1603. uid_specified = true;
  1604. break;
  1605. case Opt_cruid:
  1606. if (get_option_uid(args, &vol->cred_uid)) {
  1607. cifs_dbg(VFS, "%s: Invalid cruid value\n",
  1608. __func__);
  1609. goto cifs_parse_mount_err;
  1610. }
  1611. break;
  1612. case Opt_gid:
  1613. if (get_option_gid(args, &vol->linux_gid)) {
  1614. cifs_dbg(VFS, "%s: Invalid gid value\n",
  1615. __func__);
  1616. goto cifs_parse_mount_err;
  1617. }
  1618. gid_specified = true;
  1619. break;
  1620. case Opt_file_mode:
  1621. if (get_option_ul(args, &option)) {
  1622. cifs_dbg(VFS, "%s: Invalid file_mode value\n",
  1623. __func__);
  1624. goto cifs_parse_mount_err;
  1625. }
  1626. vol->file_mode = option;
  1627. break;
  1628. case Opt_dirmode:
  1629. if (get_option_ul(args, &option)) {
  1630. cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
  1631. __func__);
  1632. goto cifs_parse_mount_err;
  1633. }
  1634. vol->dir_mode = option;
  1635. break;
  1636. case Opt_port:
  1637. if (get_option_ul(args, &option) ||
  1638. option > USHRT_MAX) {
  1639. cifs_dbg(VFS, "%s: Invalid port value\n",
  1640. __func__);
  1641. goto cifs_parse_mount_err;
  1642. }
  1643. port = (unsigned short)option;
  1644. break;
  1645. case Opt_min_enc_offload:
  1646. if (get_option_ul(args, &option)) {
  1647. cifs_dbg(VFS, "Invalid minimum encrypted read offload size (esize)\n");
  1648. goto cifs_parse_mount_err;
  1649. }
  1650. vol->min_offload = option;
  1651. break;
  1652. case Opt_blocksize:
  1653. if (get_option_ul(args, &option)) {
  1654. cifs_dbg(VFS, "%s: Invalid blocksize value\n",
  1655. __func__);
  1656. goto cifs_parse_mount_err;
  1657. }
  1658. /*
  1659. * inode blocksize realistically should never need to be
  1660. * less than 16K or greater than 16M and default is 1MB.
  1661. * Note that small inode block sizes (e.g. 64K) can lead
  1662. * to very poor performance of common tools like cp and scp
  1663. */
  1664. if ((option < CIFS_MAX_MSGSIZE) ||
  1665. (option > (4 * SMB3_DEFAULT_IOSIZE))) {
  1666. cifs_dbg(VFS, "%s: Invalid blocksize\n",
  1667. __func__);
  1668. goto cifs_parse_mount_err;
  1669. }
  1670. vol->bsize = option;
  1671. break;
  1672. case Opt_rsize:
  1673. if (get_option_ul(args, &option)) {
  1674. cifs_dbg(VFS, "%s: Invalid rsize value\n",
  1675. __func__);
  1676. goto cifs_parse_mount_err;
  1677. }
  1678. vol->rsize = option;
  1679. break;
  1680. case Opt_wsize:
  1681. if (get_option_ul(args, &option)) {
  1682. cifs_dbg(VFS, "%s: Invalid wsize value\n",
  1683. __func__);
  1684. goto cifs_parse_mount_err;
  1685. }
  1686. vol->wsize = option;
  1687. break;
  1688. case Opt_actimeo:
  1689. if (get_option_ul(args, &option)) {
  1690. cifs_dbg(VFS, "%s: Invalid actimeo value\n",
  1691. __func__);
  1692. goto cifs_parse_mount_err;
  1693. }
  1694. vol->actimeo = HZ * option;
  1695. if (vol->actimeo > CIFS_MAX_ACTIMEO) {
  1696. cifs_dbg(VFS, "attribute cache timeout too large\n");
  1697. goto cifs_parse_mount_err;
  1698. }
  1699. break;
  1700. case Opt_handletimeout:
  1701. if (get_option_ul(args, &option)) {
  1702. cifs_dbg(VFS, "%s: Invalid handletimeout value\n",
  1703. __func__);
  1704. goto cifs_parse_mount_err;
  1705. }
  1706. vol->handle_timeout = option;
  1707. if (vol->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
  1708. cifs_dbg(VFS, "Invalid handle cache timeout, longer than 16 minutes\n");
  1709. goto cifs_parse_mount_err;
  1710. }
  1711. break;
  1712. case Opt_echo_interval:
  1713. if (get_option_ul(args, &option)) {
  1714. cifs_dbg(VFS, "%s: Invalid echo interval value\n",
  1715. __func__);
  1716. goto cifs_parse_mount_err;
  1717. }
  1718. vol->echo_interval = option;
  1719. break;
  1720. case Opt_snapshot:
  1721. if (get_option_ul(args, &option)) {
  1722. cifs_dbg(VFS, "%s: Invalid snapshot time\n",
  1723. __func__);
  1724. goto cifs_parse_mount_err;
  1725. }
  1726. vol->snapshot_time = option;
  1727. break;
  1728. case Opt_max_credits:
  1729. if (get_option_ul(args, &option) || (option < 20) ||
  1730. (option > 60000)) {
  1731. cifs_dbg(VFS, "%s: Invalid max_credits value\n",
  1732. __func__);
  1733. goto cifs_parse_mount_err;
  1734. }
  1735. vol->max_credits = option;
  1736. break;
  1737. case Opt_max_channels:
  1738. if (get_option_ul(args, &option) || option < 1 ||
  1739. option > CIFS_MAX_CHANNELS) {
  1740. cifs_dbg(VFS, "%s: Invalid max_channels value, needs to be 1-%d\n",
  1741. __func__, CIFS_MAX_CHANNELS);
  1742. goto cifs_parse_mount_err;
  1743. }
  1744. vol->max_channels = option;
  1745. break;
  1746. /* String Arguments */
  1747. case Opt_blank_user:
  1748. /* null user, ie. anonymous authentication */
  1749. vol->nullauth = 1;
  1750. vol->username = NULL;
  1751. break;
  1752. case Opt_user:
  1753. string = match_strdup(args);
  1754. if (string == NULL)
  1755. goto out_nomem;
  1756. if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
  1757. CIFS_MAX_USERNAME_LEN) {
  1758. pr_warn("username too long\n");
  1759. goto cifs_parse_mount_err;
  1760. }
  1761. kfree(vol->username);
  1762. vol->username = kstrdup(string, GFP_KERNEL);
  1763. if (!vol->username)
  1764. goto cifs_parse_mount_err;
  1765. break;
  1766. case Opt_blank_pass:
  1767. /* passwords have to be handled differently
  1768. * to allow the character used for deliminator
  1769. * to be passed within them
  1770. */
  1771. /*
  1772. * Check if this is a case where the password
  1773. * starts with a delimiter
  1774. */
  1775. tmp_end = strchr(data, '=');
  1776. tmp_end++;
  1777. if (!(tmp_end < end && tmp_end[1] == delim)) {
  1778. /* No it is not. Set the password to NULL */
  1779. kfree_sensitive(vol->password);
  1780. vol->password = NULL;
  1781. break;
  1782. }
  1783. fallthrough; /* to Opt_pass below */
  1784. case Opt_pass:
  1785. /* Obtain the value string */
  1786. value = strchr(data, '=');
  1787. value++;
  1788. /* Set tmp_end to end of the string */
  1789. tmp_end = (char *) value + strlen(value);
  1790. /* Check if following character is the deliminator
  1791. * If yes, we have encountered a double deliminator
  1792. * reset the NULL character to the deliminator
  1793. */
  1794. if (tmp_end < end && tmp_end[1] == delim) {
  1795. tmp_end[0] = delim;
  1796. /* Keep iterating until we get to a single
  1797. * deliminator OR the end
  1798. */
  1799. while ((tmp_end = strchr(tmp_end, delim))
  1800. != NULL && (tmp_end[1] == delim)) {
  1801. tmp_end = (char *) &tmp_end[2];
  1802. }
  1803. /* Reset var options to point to next element */
  1804. if (tmp_end) {
  1805. tmp_end[0] = '\0';
  1806. options = (char *) &tmp_end[1];
  1807. } else
  1808. /* Reached the end of the mount option
  1809. * string */
  1810. options = end;
  1811. }
  1812. kfree_sensitive(vol->password);
  1813. /* Now build new password string */
  1814. temp_len = strlen(value);
  1815. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  1816. if (vol->password == NULL) {
  1817. pr_warn("no memory for password\n");
  1818. goto cifs_parse_mount_err;
  1819. }
  1820. for (i = 0, j = 0; i < temp_len; i++, j++) {
  1821. vol->password[j] = value[i];
  1822. if ((value[i] == delim) &&
  1823. value[i+1] == delim)
  1824. /* skip the second deliminator */
  1825. i++;
  1826. }
  1827. vol->password[j] = '\0';
  1828. break;
  1829. case Opt_blank_ip:
  1830. /* FIXME: should this be an error instead? */
  1831. got_ip = false;
  1832. break;
  1833. case Opt_ip:
  1834. string = match_strdup(args);
  1835. if (string == NULL)
  1836. goto out_nomem;
  1837. if (!cifs_convert_address(dstaddr, string,
  1838. strlen(string))) {
  1839. pr_err("bad ip= option (%s)\n", string);
  1840. goto cifs_parse_mount_err;
  1841. }
  1842. got_ip = true;
  1843. break;
  1844. case Opt_domain:
  1845. string = match_strdup(args);
  1846. if (string == NULL)
  1847. goto out_nomem;
  1848. if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
  1849. == CIFS_MAX_DOMAINNAME_LEN) {
  1850. pr_warn("domain name too long\n");
  1851. goto cifs_parse_mount_err;
  1852. }
  1853. kfree(vol->domainname);
  1854. vol->domainname = kstrdup(string, GFP_KERNEL);
  1855. if (!vol->domainname) {
  1856. pr_warn("no memory for domainname\n");
  1857. goto cifs_parse_mount_err;
  1858. }
  1859. cifs_dbg(FYI, "Domain name set\n");
  1860. break;
  1861. case Opt_srcaddr:
  1862. string = match_strdup(args);
  1863. if (string == NULL)
  1864. goto out_nomem;
  1865. if (!cifs_convert_address(
  1866. (struct sockaddr *)&vol->srcaddr,
  1867. string, strlen(string))) {
  1868. pr_warn("Could not parse srcaddr: %s\n",
  1869. string);
  1870. goto cifs_parse_mount_err;
  1871. }
  1872. break;
  1873. case Opt_iocharset:
  1874. string = match_strdup(args);
  1875. if (string == NULL)
  1876. goto out_nomem;
  1877. if (strnlen(string, 1024) >= 65) {
  1878. pr_warn("iocharset name too long\n");
  1879. goto cifs_parse_mount_err;
  1880. }
  1881. if (strncasecmp(string, "default", 7) != 0) {
  1882. kfree(vol->iocharset);
  1883. vol->iocharset = kstrdup(string,
  1884. GFP_KERNEL);
  1885. if (!vol->iocharset) {
  1886. pr_warn("no memory for charset\n");
  1887. goto cifs_parse_mount_err;
  1888. }
  1889. }
  1890. /* if iocharset not set then load_nls_default
  1891. * is used by caller
  1892. */
  1893. cifs_dbg(FYI, "iocharset set to %s\n", string);
  1894. break;
  1895. case Opt_netbiosname:
  1896. string = match_strdup(args);
  1897. if (string == NULL)
  1898. goto out_nomem;
  1899. memset(vol->source_rfc1001_name, 0x20,
  1900. RFC1001_NAME_LEN);
  1901. /*
  1902. * FIXME: are there cases in which a comma can
  1903. * be valid in workstation netbios name (and
  1904. * need special handling)?
  1905. */
  1906. for (i = 0; i < RFC1001_NAME_LEN; i++) {
  1907. /* don't ucase netbiosname for user */
  1908. if (string[i] == 0)
  1909. break;
  1910. vol->source_rfc1001_name[i] = string[i];
  1911. }
  1912. /* The string has 16th byte zero still from
  1913. * set at top of the function
  1914. */
  1915. if (i == RFC1001_NAME_LEN && string[i] != 0)
  1916. pr_warn("netbiosname longer than 15 truncated\n");
  1917. break;
  1918. case Opt_servern:
  1919. /* servernetbiosname specified override *SMBSERVER */
  1920. string = match_strdup(args);
  1921. if (string == NULL)
  1922. goto out_nomem;
  1923. /* last byte, type, is 0x20 for servr type */
  1924. memset(vol->target_rfc1001_name, 0x20,
  1925. RFC1001_NAME_LEN_WITH_NULL);
  1926. /* BB are there cases in which a comma can be
  1927. valid in this workstation netbios name
  1928. (and need special handling)? */
  1929. /* user or mount helper must uppercase the
  1930. netbios name */
  1931. for (i = 0; i < 15; i++) {
  1932. if (string[i] == 0)
  1933. break;
  1934. vol->target_rfc1001_name[i] = string[i];
  1935. }
  1936. /* The string has 16th byte zero still from
  1937. set at top of the function */
  1938. if (i == RFC1001_NAME_LEN && string[i] != 0)
  1939. pr_warn("server netbiosname longer than 15 truncated\n");
  1940. break;
  1941. case Opt_ver:
  1942. /* version of mount userspace tools, not dialect */
  1943. string = match_strdup(args);
  1944. if (string == NULL)
  1945. goto out_nomem;
  1946. /* If interface changes in mount.cifs bump to new ver */
  1947. if (strncasecmp(string, "1", 1) == 0) {
  1948. if (strlen(string) > 1) {
  1949. pr_warn("Bad mount helper ver=%s. Did you want SMB1 (CIFS) dialect and mean to type vers=1.0 instead?\n",
  1950. string);
  1951. goto cifs_parse_mount_err;
  1952. }
  1953. /* This is the default */
  1954. break;
  1955. }
  1956. /* For all other value, error */
  1957. pr_warn("Invalid mount helper version specified\n");
  1958. goto cifs_parse_mount_err;
  1959. case Opt_vers:
  1960. /* protocol version (dialect) */
  1961. string = match_strdup(args);
  1962. if (string == NULL)
  1963. goto out_nomem;
  1964. if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
  1965. goto cifs_parse_mount_err;
  1966. got_version = true;
  1967. break;
  1968. case Opt_sec:
  1969. string = match_strdup(args);
  1970. if (string == NULL)
  1971. goto out_nomem;
  1972. if (cifs_parse_security_flavors(string, vol) != 0)
  1973. goto cifs_parse_mount_err;
  1974. break;
  1975. case Opt_cache:
  1976. string = match_strdup(args);
  1977. if (string == NULL)
  1978. goto out_nomem;
  1979. if (cifs_parse_cache_flavor(string, vol) != 0)
  1980. goto cifs_parse_mount_err;
  1981. break;
  1982. default:
  1983. /*
  1984. * An option we don't recognize. Save it off for later
  1985. * if we haven't already found one
  1986. */
  1987. if (!invalid)
  1988. invalid = data;
  1989. break;
  1990. }
  1991. /* Free up any allocated string */
  1992. kfree(string);
  1993. string = NULL;
  1994. }
  1995. if (!sloppy && invalid) {
  1996. pr_err("Unknown mount option \"%s\"\n", invalid);
  1997. goto cifs_parse_mount_err;
  1998. }
  1999. if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
  2000. cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
  2001. goto cifs_parse_mount_err;
  2002. }
  2003. #ifndef CONFIG_KEYS
  2004. /* Muliuser mounts require CONFIG_KEYS support */
  2005. if (vol->multiuser) {
  2006. cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
  2007. goto cifs_parse_mount_err;
  2008. }
  2009. #endif
  2010. if (!vol->UNC) {
  2011. cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
  2012. goto cifs_parse_mount_err;
  2013. }
  2014. /* make sure UNC has a share name */
  2015. if (!strchr(vol->UNC + 3, '\\')) {
  2016. cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
  2017. goto cifs_parse_mount_err;
  2018. }
  2019. if (!got_ip) {
  2020. int len;
  2021. const char *slash;
  2022. /* No ip= option specified? Try to get it from UNC */
  2023. /* Use the address part of the UNC. */
  2024. slash = strchr(&vol->UNC[2], '\\');
  2025. len = slash - &vol->UNC[2];
  2026. if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
  2027. pr_err("Unable to determine destination address\n");
  2028. goto cifs_parse_mount_err;
  2029. }
  2030. }
  2031. /* set the port that we got earlier */
  2032. cifs_set_port(dstaddr, port);
  2033. if (uid_specified)
  2034. vol->override_uid = override_uid;
  2035. else if (override_uid == 1)
  2036. pr_notice("ignoring forceuid mount option specified with no uid= option\n");
  2037. if (gid_specified)
  2038. vol->override_gid = override_gid;
  2039. else if (override_gid == 1)
  2040. pr_notice("ignoring forcegid mount option specified with no gid= option\n");
  2041. if (got_version == false)
  2042. pr_warn_once("No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3.1.1), from CIFS (SMB1). To use the less secure SMB1 dialect to access old servers which do not support SMB3.1.1 (or even SMB3 or SMB2.1) specify vers=1.0 on mount.\n");
  2043. kfree(mountdata_copy);
  2044. return 0;
  2045. out_nomem:
  2046. pr_warn("Could not allocate temporary buffer\n");
  2047. cifs_parse_mount_err:
  2048. kfree(string);
  2049. kfree(mountdata_copy);
  2050. return 1;
  2051. }
  2052. /** Returns true if srcaddr isn't specified and rhs isn't
  2053. * specified, or if srcaddr is specified and
  2054. * matches the IP address of the rhs argument.
  2055. */
  2056. bool
  2057. cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
  2058. {
  2059. switch (srcaddr->sa_family) {
  2060. case AF_UNSPEC:
  2061. return (rhs->sa_family == AF_UNSPEC);
  2062. case AF_INET: {
  2063. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  2064. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  2065. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  2066. }
  2067. case AF_INET6: {
  2068. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  2069. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
  2070. return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
  2071. }
  2072. default:
  2073. WARN_ON(1);
  2074. return false; /* don't expect to be here */
  2075. }
  2076. }
  2077. /*
  2078. * If no port is specified in addr structure, we try to match with 445 port
  2079. * and if it fails - with 139 ports. It should be called only if address
  2080. * families of server and addr are equal.
  2081. */
  2082. static bool
  2083. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  2084. {
  2085. __be16 port, *sport;
  2086. /* SMBDirect manages its own ports, don't match it here */
  2087. if (server->rdma)
  2088. return true;
  2089. switch (addr->sa_family) {
  2090. case AF_INET:
  2091. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  2092. port = ((struct sockaddr_in *) addr)->sin_port;
  2093. break;
  2094. case AF_INET6:
  2095. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  2096. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  2097. break;
  2098. default:
  2099. WARN_ON(1);
  2100. return false;
  2101. }
  2102. if (!port) {
  2103. port = htons(CIFS_PORT);
  2104. if (port == *sport)
  2105. return true;
  2106. port = htons(RFC1001_PORT);
  2107. }
  2108. return port == *sport;
  2109. }
  2110. static bool
  2111. match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
  2112. struct sockaddr *srcaddr)
  2113. {
  2114. switch (addr->sa_family) {
  2115. case AF_INET: {
  2116. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  2117. struct sockaddr_in *srv_addr4 =
  2118. (struct sockaddr_in *)&server->dstaddr;
  2119. if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
  2120. return false;
  2121. break;
  2122. }
  2123. case AF_INET6: {
  2124. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  2125. struct sockaddr_in6 *srv_addr6 =
  2126. (struct sockaddr_in6 *)&server->dstaddr;
  2127. if (!ipv6_addr_equal(&addr6->sin6_addr,
  2128. &srv_addr6->sin6_addr))
  2129. return false;
  2130. if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
  2131. return false;
  2132. break;
  2133. }
  2134. default:
  2135. WARN_ON(1);
  2136. return false; /* don't expect to be here */
  2137. }
  2138. if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
  2139. return false;
  2140. return true;
  2141. }
  2142. static bool
  2143. match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
  2144. {
  2145. /*
  2146. * The select_sectype function should either return the vol->sectype
  2147. * that was specified, or "Unspecified" if that sectype was not
  2148. * compatible with the given NEGOTIATE request.
  2149. */
  2150. if (server->ops->select_sectype(server, vol->sectype)
  2151. == Unspecified)
  2152. return false;
  2153. /*
  2154. * Now check if signing mode is acceptable. No need to check
  2155. * global_secflags at this point since if MUST_SIGN is set then
  2156. * the server->sign had better be too.
  2157. */
  2158. if (vol->sign && !server->sign)
  2159. return false;
  2160. return true;
  2161. }
  2162. static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
  2163. {
  2164. struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
  2165. if (vol->nosharesock)
  2166. return 0;
  2167. /* If multidialect negotiation see if existing sessions match one */
  2168. if (strcmp(vol->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
  2169. if (server->vals->protocol_id < SMB30_PROT_ID)
  2170. return 0;
  2171. } else if (strcmp(vol->vals->version_string,
  2172. SMBDEFAULT_VERSION_STRING) == 0) {
  2173. if (server->vals->protocol_id < SMB21_PROT_ID)
  2174. return 0;
  2175. } else if ((server->vals != vol->vals) || (server->ops != vol->ops))
  2176. return 0;
  2177. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  2178. return 0;
  2179. if (!match_address(server, addr,
  2180. (struct sockaddr *)&vol->srcaddr))
  2181. return 0;
  2182. if (!match_port(server, addr))
  2183. return 0;
  2184. if (!match_security(server, vol))
  2185. return 0;
  2186. if (server->echo_interval != vol->echo_interval * HZ)
  2187. return 0;
  2188. if (server->rdma != vol->rdma)
  2189. return 0;
  2190. if (server->ignore_signature != vol->ignore_signature)
  2191. return 0;
  2192. if (server->min_offload != vol->min_offload)
  2193. return 0;
  2194. return 1;
  2195. }
  2196. struct TCP_Server_Info *
  2197. cifs_find_tcp_session(struct smb_vol *vol)
  2198. {
  2199. struct TCP_Server_Info *server;
  2200. spin_lock(&cifs_tcp_ses_lock);
  2201. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  2202. /*
  2203. * Skip ses channels since they're only handled in lower layers
  2204. * (e.g. cifs_send_recv).
  2205. */
  2206. if (server->is_channel || !match_server(server, vol))
  2207. continue;
  2208. ++server->srv_count;
  2209. spin_unlock(&cifs_tcp_ses_lock);
  2210. cifs_dbg(FYI, "Existing tcp session with server found\n");
  2211. return server;
  2212. }
  2213. spin_unlock(&cifs_tcp_ses_lock);
  2214. return NULL;
  2215. }
  2216. void
  2217. cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
  2218. {
  2219. struct task_struct *task;
  2220. spin_lock(&cifs_tcp_ses_lock);
  2221. if (--server->srv_count > 0) {
  2222. spin_unlock(&cifs_tcp_ses_lock);
  2223. return;
  2224. }
  2225. put_net(cifs_net_ns(server));
  2226. list_del_init(&server->tcp_ses_list);
  2227. spin_unlock(&cifs_tcp_ses_lock);
  2228. cancel_delayed_work_sync(&server->echo);
  2229. if (from_reconnect)
  2230. /*
  2231. * Avoid deadlock here: reconnect work calls
  2232. * cifs_put_tcp_session() at its end. Need to be sure
  2233. * that reconnect work does nothing with server pointer after
  2234. * that step.
  2235. */
  2236. cancel_delayed_work(&server->reconnect);
  2237. else
  2238. cancel_delayed_work_sync(&server->reconnect);
  2239. spin_lock(&GlobalMid_Lock);
  2240. server->tcpStatus = CifsExiting;
  2241. spin_unlock(&GlobalMid_Lock);
  2242. cifs_crypto_secmech_release(server);
  2243. cifs_fscache_release_client_cookie(server);
  2244. kfree(server->session_key.response);
  2245. server->session_key.response = NULL;
  2246. server->session_key.len = 0;
  2247. task = xchg(&server->tsk, NULL);
  2248. if (task)
  2249. send_sig(SIGKILL, task, 1);
  2250. }
  2251. struct TCP_Server_Info *
  2252. cifs_get_tcp_session(struct smb_vol *volume_info)
  2253. {
  2254. struct TCP_Server_Info *tcp_ses = NULL;
  2255. int rc;
  2256. cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
  2257. /* see if we already have a matching tcp_ses */
  2258. tcp_ses = cifs_find_tcp_session(volume_info);
  2259. if (tcp_ses)
  2260. return tcp_ses;
  2261. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  2262. if (!tcp_ses) {
  2263. rc = -ENOMEM;
  2264. goto out_err;
  2265. }
  2266. tcp_ses->ops = volume_info->ops;
  2267. tcp_ses->vals = volume_info->vals;
  2268. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  2269. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  2270. if (IS_ERR(tcp_ses->hostname)) {
  2271. rc = PTR_ERR(tcp_ses->hostname);
  2272. goto out_err_crypto_release;
  2273. }
  2274. tcp_ses->noblockcnt = volume_info->rootfs;
  2275. tcp_ses->noblocksnd = volume_info->noblocksnd || volume_info->rootfs;
  2276. tcp_ses->noautotune = volume_info->noautotune;
  2277. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  2278. tcp_ses->rdma = volume_info->rdma;
  2279. tcp_ses->in_flight = 0;
  2280. tcp_ses->max_in_flight = 0;
  2281. tcp_ses->credits = 1;
  2282. init_waitqueue_head(&tcp_ses->response_q);
  2283. init_waitqueue_head(&tcp_ses->request_q);
  2284. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  2285. mutex_init(&tcp_ses->srv_mutex);
  2286. memcpy(tcp_ses->workstation_RFC1001_name,
  2287. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  2288. memcpy(tcp_ses->server_RFC1001_name,
  2289. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  2290. tcp_ses->session_estab = false;
  2291. tcp_ses->sequence_number = 0;
  2292. tcp_ses->reconnect_instance = 1;
  2293. tcp_ses->lstrp = jiffies;
  2294. tcp_ses->compress_algorithm = cpu_to_le16(volume_info->compression);
  2295. spin_lock_init(&tcp_ses->req_lock);
  2296. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  2297. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  2298. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  2299. INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
  2300. mutex_init(&tcp_ses->reconnect_mutex);
  2301. memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
  2302. sizeof(tcp_ses->srcaddr));
  2303. memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
  2304. sizeof(tcp_ses->dstaddr));
  2305. if (volume_info->use_client_guid)
  2306. memcpy(tcp_ses->client_guid, volume_info->client_guid,
  2307. SMB2_CLIENT_GUID_SIZE);
  2308. else
  2309. generate_random_uuid(tcp_ses->client_guid);
  2310. /*
  2311. * at this point we are the only ones with the pointer
  2312. * to the struct since the kernel thread not created yet
  2313. * no need to spinlock this init of tcpStatus or srv_count
  2314. */
  2315. tcp_ses->tcpStatus = CifsNew;
  2316. ++tcp_ses->srv_count;
  2317. if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
  2318. volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
  2319. tcp_ses->echo_interval = volume_info->echo_interval * HZ;
  2320. else
  2321. tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
  2322. if (tcp_ses->rdma) {
  2323. #ifndef CONFIG_CIFS_SMB_DIRECT
  2324. cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
  2325. rc = -ENOENT;
  2326. goto out_err_crypto_release;
  2327. #endif
  2328. tcp_ses->smbd_conn = smbd_get_connection(
  2329. tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
  2330. if (tcp_ses->smbd_conn) {
  2331. cifs_dbg(VFS, "RDMA transport established\n");
  2332. rc = 0;
  2333. goto smbd_connected;
  2334. } else {
  2335. rc = -ENOENT;
  2336. goto out_err_crypto_release;
  2337. }
  2338. }
  2339. rc = ip_connect(tcp_ses);
  2340. if (rc < 0) {
  2341. cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
  2342. goto out_err_crypto_release;
  2343. }
  2344. smbd_connected:
  2345. /*
  2346. * since we're in a cifs function already, we know that
  2347. * this will succeed. No need for try_module_get().
  2348. */
  2349. __module_get(THIS_MODULE);
  2350. tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
  2351. tcp_ses, "cifsd");
  2352. if (IS_ERR(tcp_ses->tsk)) {
  2353. rc = PTR_ERR(tcp_ses->tsk);
  2354. cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
  2355. module_put(THIS_MODULE);
  2356. goto out_err_crypto_release;
  2357. }
  2358. tcp_ses->min_offload = volume_info->min_offload;
  2359. tcp_ses->tcpStatus = CifsNeedNegotiate;
  2360. if ((volume_info->max_credits < 20) || (volume_info->max_credits > 60000))
  2361. tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
  2362. else
  2363. tcp_ses->max_credits = volume_info->max_credits;
  2364. tcp_ses->nr_targets = 1;
  2365. tcp_ses->ignore_signature = volume_info->ignore_signature;
  2366. /* thread spawned, put it on the list */
  2367. spin_lock(&cifs_tcp_ses_lock);
  2368. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  2369. spin_unlock(&cifs_tcp_ses_lock);
  2370. cifs_fscache_get_client_cookie(tcp_ses);
  2371. /* queue echo request delayed work */
  2372. queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
  2373. return tcp_ses;
  2374. out_err_crypto_release:
  2375. cifs_crypto_secmech_release(tcp_ses);
  2376. put_net(cifs_net_ns(tcp_ses));
  2377. out_err:
  2378. if (tcp_ses) {
  2379. if (!IS_ERR(tcp_ses->hostname))
  2380. kfree(tcp_ses->hostname);
  2381. if (tcp_ses->ssocket)
  2382. sock_release(tcp_ses->ssocket);
  2383. kfree(tcp_ses);
  2384. }
  2385. return ERR_PTR(rc);
  2386. }
  2387. static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
  2388. {
  2389. if (vol->sectype != Unspecified &&
  2390. vol->sectype != ses->sectype)
  2391. return 0;
  2392. /*
  2393. * If an existing session is limited to less channels than
  2394. * requested, it should not be reused
  2395. */
  2396. if (ses->chan_max < vol->max_channels)
  2397. return 0;
  2398. switch (ses->sectype) {
  2399. case Kerberos:
  2400. if (!uid_eq(vol->cred_uid, ses->cred_uid))
  2401. return 0;
  2402. break;
  2403. default:
  2404. /* NULL username means anonymous session */
  2405. if (ses->user_name == NULL) {
  2406. if (!vol->nullauth)
  2407. return 0;
  2408. break;
  2409. }
  2410. /* anything else takes username/password */
  2411. if (strncmp(ses->user_name,
  2412. vol->username ? vol->username : "",
  2413. CIFS_MAX_USERNAME_LEN))
  2414. return 0;
  2415. if ((vol->username && strlen(vol->username) != 0) &&
  2416. ses->password != NULL &&
  2417. strncmp(ses->password,
  2418. vol->password ? vol->password : "",
  2419. CIFS_MAX_PASSWORD_LEN))
  2420. return 0;
  2421. }
  2422. return 1;
  2423. }
  2424. /**
  2425. * cifs_setup_ipc - helper to setup the IPC tcon for the session
  2426. *
  2427. * A new IPC connection is made and stored in the session
  2428. * tcon_ipc. The IPC tcon has the same lifetime as the session.
  2429. */
  2430. static int
  2431. cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
  2432. {
  2433. int rc = 0, xid;
  2434. struct cifs_tcon *tcon;
  2435. struct nls_table *nls_codepage;
  2436. char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
  2437. bool seal = false;
  2438. struct TCP_Server_Info *server = ses->server;
  2439. /*
  2440. * If the mount request that resulted in the creation of the
  2441. * session requires encryption, force IPC to be encrypted too.
  2442. */
  2443. if (volume_info->seal) {
  2444. if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
  2445. seal = true;
  2446. else {
  2447. cifs_server_dbg(VFS,
  2448. "IPC: server doesn't support encryption\n");
  2449. return -EOPNOTSUPP;
  2450. }
  2451. }
  2452. tcon = tconInfoAlloc();
  2453. if (tcon == NULL)
  2454. return -ENOMEM;
  2455. scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
  2456. /* cannot fail */
  2457. nls_codepage = load_nls_default();
  2458. xid = get_xid();
  2459. tcon->ses = ses;
  2460. tcon->ipc = true;
  2461. tcon->seal = seal;
  2462. rc = server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
  2463. free_xid(xid);
  2464. if (rc) {
  2465. cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
  2466. tconInfoFree(tcon);
  2467. goto out;
  2468. }
  2469. cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
  2470. ses->tcon_ipc = tcon;
  2471. out:
  2472. unload_nls(nls_codepage);
  2473. return rc;
  2474. }
  2475. /**
  2476. * cifs_free_ipc - helper to release the session IPC tcon
  2477. *
  2478. * Needs to be called everytime a session is destroyed
  2479. */
  2480. static int
  2481. cifs_free_ipc(struct cifs_ses *ses)
  2482. {
  2483. int rc = 0, xid;
  2484. struct cifs_tcon *tcon = ses->tcon_ipc;
  2485. if (tcon == NULL)
  2486. return 0;
  2487. if (ses->server->ops->tree_disconnect) {
  2488. xid = get_xid();
  2489. rc = ses->server->ops->tree_disconnect(xid, tcon);
  2490. free_xid(xid);
  2491. }
  2492. if (rc)
  2493. cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
  2494. tconInfoFree(tcon);
  2495. ses->tcon_ipc = NULL;
  2496. return rc;
  2497. }
  2498. static struct cifs_ses *
  2499. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
  2500. {
  2501. struct cifs_ses *ses;
  2502. spin_lock(&cifs_tcp_ses_lock);
  2503. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  2504. if (ses->status == CifsExiting)
  2505. continue;
  2506. if (!match_session(ses, vol))
  2507. continue;
  2508. ++ses->ses_count;
  2509. spin_unlock(&cifs_tcp_ses_lock);
  2510. return ses;
  2511. }
  2512. spin_unlock(&cifs_tcp_ses_lock);
  2513. return NULL;
  2514. }
  2515. void cifs_put_smb_ses(struct cifs_ses *ses)
  2516. {
  2517. unsigned int rc, xid;
  2518. struct TCP_Server_Info *server = ses->server;
  2519. cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
  2520. spin_lock(&cifs_tcp_ses_lock);
  2521. if (ses->status == CifsExiting) {
  2522. spin_unlock(&cifs_tcp_ses_lock);
  2523. return;
  2524. }
  2525. if (--ses->ses_count > 0) {
  2526. spin_unlock(&cifs_tcp_ses_lock);
  2527. return;
  2528. }
  2529. spin_unlock(&cifs_tcp_ses_lock);
  2530. spin_lock(&GlobalMid_Lock);
  2531. if (ses->status == CifsGood)
  2532. ses->status = CifsExiting;
  2533. spin_unlock(&GlobalMid_Lock);
  2534. cifs_free_ipc(ses);
  2535. if (ses->status == CifsExiting && server->ops->logoff) {
  2536. xid = get_xid();
  2537. rc = server->ops->logoff(xid, ses);
  2538. if (rc)
  2539. cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
  2540. __func__, rc);
  2541. _free_xid(xid);
  2542. }
  2543. spin_lock(&cifs_tcp_ses_lock);
  2544. list_del_init(&ses->smb_ses_list);
  2545. spin_unlock(&cifs_tcp_ses_lock);
  2546. /* close any extra channels */
  2547. if (ses->chan_count > 1) {
  2548. int i;
  2549. for (i = 1; i < ses->chan_count; i++)
  2550. cifs_put_tcp_session(ses->chans[i].server, 0);
  2551. }
  2552. sesInfoFree(ses);
  2553. cifs_put_tcp_session(server, 0);
  2554. }
  2555. #ifdef CONFIG_KEYS
  2556. /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
  2557. #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
  2558. /* Populate username and pw fields from keyring if possible */
  2559. static int
  2560. cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
  2561. {
  2562. int rc = 0;
  2563. int is_domain = 0;
  2564. const char *delim, *payload;
  2565. char *desc;
  2566. ssize_t len;
  2567. struct key *key;
  2568. struct TCP_Server_Info *server = ses->server;
  2569. struct sockaddr_in *sa;
  2570. struct sockaddr_in6 *sa6;
  2571. const struct user_key_payload *upayload;
  2572. desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
  2573. if (!desc)
  2574. return -ENOMEM;
  2575. /* try to find an address key first */
  2576. switch (server->dstaddr.ss_family) {
  2577. case AF_INET:
  2578. sa = (struct sockaddr_in *)&server->dstaddr;
  2579. sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
  2580. break;
  2581. case AF_INET6:
  2582. sa6 = (struct sockaddr_in6 *)&server->dstaddr;
  2583. sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
  2584. break;
  2585. default:
  2586. cifs_dbg(FYI, "Bad ss_family (%hu)\n",
  2587. server->dstaddr.ss_family);
  2588. rc = -EINVAL;
  2589. goto out_err;
  2590. }
  2591. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  2592. key = request_key(&key_type_logon, desc, "");
  2593. if (IS_ERR(key)) {
  2594. if (!ses->domainName) {
  2595. cifs_dbg(FYI, "domainName is NULL\n");
  2596. rc = PTR_ERR(key);
  2597. goto out_err;
  2598. }
  2599. /* didn't work, try to find a domain key */
  2600. sprintf(desc, "cifs:d:%s", ses->domainName);
  2601. cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
  2602. key = request_key(&key_type_logon, desc, "");
  2603. if (IS_ERR(key)) {
  2604. rc = PTR_ERR(key);
  2605. goto out_err;
  2606. }
  2607. is_domain = 1;
  2608. }
  2609. down_read(&key->sem);
  2610. upayload = user_key_payload_locked(key);
  2611. if (IS_ERR_OR_NULL(upayload)) {
  2612. rc = upayload ? PTR_ERR(upayload) : -EINVAL;
  2613. goto out_key_put;
  2614. }
  2615. /* find first : in payload */
  2616. payload = upayload->data;
  2617. delim = strnchr(payload, upayload->datalen, ':');
  2618. cifs_dbg(FYI, "payload=%s\n", payload);
  2619. if (!delim) {
  2620. cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
  2621. upayload->datalen);
  2622. rc = -EINVAL;
  2623. goto out_key_put;
  2624. }
  2625. len = delim - payload;
  2626. if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
  2627. cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
  2628. len);
  2629. rc = -EINVAL;
  2630. goto out_key_put;
  2631. }
  2632. vol->username = kstrndup(payload, len, GFP_KERNEL);
  2633. if (!vol->username) {
  2634. cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
  2635. len);
  2636. rc = -ENOMEM;
  2637. goto out_key_put;
  2638. }
  2639. cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
  2640. len = key->datalen - (len + 1);
  2641. if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
  2642. cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
  2643. rc = -EINVAL;
  2644. kfree(vol->username);
  2645. vol->username = NULL;
  2646. goto out_key_put;
  2647. }
  2648. ++delim;
  2649. vol->password = kstrndup(delim, len, GFP_KERNEL);
  2650. if (!vol->password) {
  2651. cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
  2652. len);
  2653. rc = -ENOMEM;
  2654. kfree(vol->username);
  2655. vol->username = NULL;
  2656. goto out_key_put;
  2657. }
  2658. /*
  2659. * If we have a domain key then we must set the domainName in the
  2660. * for the request.
  2661. */
  2662. if (is_domain && ses->domainName) {
  2663. vol->domainname = kstrndup(ses->domainName,
  2664. strlen(ses->domainName),
  2665. GFP_KERNEL);
  2666. if (!vol->domainname) {
  2667. cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
  2668. len);
  2669. rc = -ENOMEM;
  2670. kfree(vol->username);
  2671. vol->username = NULL;
  2672. kfree_sensitive(vol->password);
  2673. vol->password = NULL;
  2674. goto out_key_put;
  2675. }
  2676. }
  2677. out_key_put:
  2678. up_read(&key->sem);
  2679. key_put(key);
  2680. out_err:
  2681. kfree(desc);
  2682. cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
  2683. return rc;
  2684. }
  2685. #else /* ! CONFIG_KEYS */
  2686. static inline int
  2687. cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
  2688. struct cifs_ses *ses __attribute__((unused)))
  2689. {
  2690. return -ENOSYS;
  2691. }
  2692. #endif /* CONFIG_KEYS */
  2693. /**
  2694. * cifs_get_smb_ses - get a session matching @volume_info data from @server
  2695. *
  2696. * This function assumes it is being called from cifs_mount() where we
  2697. * already got a server reference (server refcount +1). See
  2698. * cifs_get_tcon() for refcount explanations.
  2699. */
  2700. struct cifs_ses *
  2701. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  2702. {
  2703. int rc = -ENOMEM;
  2704. unsigned int xid;
  2705. struct cifs_ses *ses;
  2706. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2707. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2708. xid = get_xid();
  2709. ses = cifs_find_smb_ses(server, volume_info);
  2710. if (ses) {
  2711. cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
  2712. ses->status);
  2713. mutex_lock(&ses->session_mutex);
  2714. rc = cifs_negotiate_protocol(xid, ses);
  2715. if (rc) {
  2716. mutex_unlock(&ses->session_mutex);
  2717. /* problem -- put our ses reference */
  2718. cifs_put_smb_ses(ses);
  2719. free_xid(xid);
  2720. return ERR_PTR(rc);
  2721. }
  2722. if (ses->need_reconnect) {
  2723. cifs_dbg(FYI, "Session needs reconnect\n");
  2724. rc = cifs_setup_session(xid, ses,
  2725. volume_info->local_nls);
  2726. if (rc) {
  2727. mutex_unlock(&ses->session_mutex);
  2728. /* problem -- put our reference */
  2729. cifs_put_smb_ses(ses);
  2730. free_xid(xid);
  2731. return ERR_PTR(rc);
  2732. }
  2733. }
  2734. mutex_unlock(&ses->session_mutex);
  2735. /* existing SMB ses has a server reference already */
  2736. cifs_put_tcp_session(server, 0);
  2737. free_xid(xid);
  2738. return ses;
  2739. }
  2740. cifs_dbg(FYI, "Existing smb sess not found\n");
  2741. ses = sesInfoAlloc();
  2742. if (ses == NULL)
  2743. goto get_ses_fail;
  2744. /* new SMB session uses our server ref */
  2745. ses->server = server;
  2746. if (server->dstaddr.ss_family == AF_INET6)
  2747. sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
  2748. else
  2749. sprintf(ses->serverName, "%pI4", &addr->sin_addr);
  2750. if (volume_info->username) {
  2751. ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
  2752. if (!ses->user_name)
  2753. goto get_ses_fail;
  2754. }
  2755. /* volume_info->password freed at unmount */
  2756. if (volume_info->password) {
  2757. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  2758. if (!ses->password)
  2759. goto get_ses_fail;
  2760. }
  2761. if (volume_info->domainname) {
  2762. ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
  2763. if (!ses->domainName)
  2764. goto get_ses_fail;
  2765. }
  2766. if (volume_info->domainauto)
  2767. ses->domainAuto = volume_info->domainauto;
  2768. ses->cred_uid = volume_info->cred_uid;
  2769. ses->linux_uid = volume_info->linux_uid;
  2770. ses->sectype = volume_info->sectype;
  2771. ses->sign = volume_info->sign;
  2772. mutex_lock(&ses->session_mutex);
  2773. /* add server as first channel */
  2774. ses->chans[0].server = server;
  2775. ses->chan_count = 1;
  2776. ses->chan_max = volume_info->multichannel ? volume_info->max_channels:1;
  2777. rc = cifs_negotiate_protocol(xid, ses);
  2778. if (!rc)
  2779. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  2780. /* each channel uses a different signing key */
  2781. memcpy(ses->chans[0].signkey, ses->smb3signingkey,
  2782. sizeof(ses->smb3signingkey));
  2783. mutex_unlock(&ses->session_mutex);
  2784. if (rc)
  2785. goto get_ses_fail;
  2786. /* success, put it on the list and add it as first channel */
  2787. spin_lock(&cifs_tcp_ses_lock);
  2788. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  2789. spin_unlock(&cifs_tcp_ses_lock);
  2790. free_xid(xid);
  2791. cifs_setup_ipc(ses, volume_info);
  2792. return ses;
  2793. get_ses_fail:
  2794. sesInfoFree(ses);
  2795. free_xid(xid);
  2796. return ERR_PTR(rc);
  2797. }
  2798. static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
  2799. {
  2800. if (tcon->tidStatus == CifsExiting)
  2801. return 0;
  2802. if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
  2803. return 0;
  2804. if (tcon->seal != volume_info->seal)
  2805. return 0;
  2806. if (tcon->snapshot_time != volume_info->snapshot_time)
  2807. return 0;
  2808. if (tcon->handle_timeout != volume_info->handle_timeout)
  2809. return 0;
  2810. if (tcon->no_lease != volume_info->no_lease)
  2811. return 0;
  2812. if (tcon->nodelete != volume_info->nodelete)
  2813. return 0;
  2814. return 1;
  2815. }
  2816. static struct cifs_tcon *
  2817. cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  2818. {
  2819. struct list_head *tmp;
  2820. struct cifs_tcon *tcon;
  2821. spin_lock(&cifs_tcp_ses_lock);
  2822. list_for_each(tmp, &ses->tcon_list) {
  2823. tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
  2824. #ifdef CONFIG_CIFS_DFS_UPCALL
  2825. if (tcon->dfs_path)
  2826. continue;
  2827. #endif
  2828. if (!match_tcon(tcon, volume_info))
  2829. continue;
  2830. ++tcon->tc_count;
  2831. spin_unlock(&cifs_tcp_ses_lock);
  2832. return tcon;
  2833. }
  2834. spin_unlock(&cifs_tcp_ses_lock);
  2835. return NULL;
  2836. }
  2837. void
  2838. cifs_put_tcon(struct cifs_tcon *tcon)
  2839. {
  2840. unsigned int xid;
  2841. struct cifs_ses *ses;
  2842. /*
  2843. * IPC tcon share the lifetime of their session and are
  2844. * destroyed in the session put function
  2845. */
  2846. if (tcon == NULL || tcon->ipc)
  2847. return;
  2848. ses = tcon->ses;
  2849. cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  2850. spin_lock(&cifs_tcp_ses_lock);
  2851. if (--tcon->tc_count > 0) {
  2852. spin_unlock(&cifs_tcp_ses_lock);
  2853. return;
  2854. }
  2855. list_del_init(&tcon->tcon_list);
  2856. spin_unlock(&cifs_tcp_ses_lock);
  2857. xid = get_xid();
  2858. if (ses->server->ops->tree_disconnect)
  2859. ses->server->ops->tree_disconnect(xid, tcon);
  2860. _free_xid(xid);
  2861. cifs_fscache_release_super_cookie(tcon);
  2862. tconInfoFree(tcon);
  2863. cifs_put_smb_ses(ses);
  2864. }
  2865. /**
  2866. * cifs_get_tcon - get a tcon matching @volume_info data from @ses
  2867. *
  2868. * - tcon refcount is the number of mount points using the tcon.
  2869. * - ses refcount is the number of tcon using the session.
  2870. *
  2871. * 1. This function assumes it is being called from cifs_mount() where
  2872. * we already got a session reference (ses refcount +1).
  2873. *
  2874. * 2. Since we're in the context of adding a mount point, the end
  2875. * result should be either:
  2876. *
  2877. * a) a new tcon already allocated with refcount=1 (1 mount point) and
  2878. * its session refcount incremented (1 new tcon). This +1 was
  2879. * already done in (1).
  2880. *
  2881. * b) an existing tcon with refcount+1 (add a mount point to it) and
  2882. * identical ses refcount (no new tcon). Because of (1) we need to
  2883. * decrement the ses refcount.
  2884. */
  2885. static struct cifs_tcon *
  2886. cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  2887. {
  2888. int rc, xid;
  2889. struct cifs_tcon *tcon;
  2890. tcon = cifs_find_tcon(ses, volume_info);
  2891. if (tcon) {
  2892. /*
  2893. * tcon has refcount already incremented but we need to
  2894. * decrement extra ses reference gotten by caller (case b)
  2895. */
  2896. cifs_dbg(FYI, "Found match on UNC path\n");
  2897. cifs_put_smb_ses(ses);
  2898. return tcon;
  2899. }
  2900. if (!ses->server->ops->tree_connect) {
  2901. rc = -ENOSYS;
  2902. goto out_fail;
  2903. }
  2904. tcon = tconInfoAlloc();
  2905. if (tcon == NULL) {
  2906. rc = -ENOMEM;
  2907. goto out_fail;
  2908. }
  2909. if (volume_info->snapshot_time) {
  2910. if (ses->server->vals->protocol_id == 0) {
  2911. cifs_dbg(VFS,
  2912. "Use SMB2 or later for snapshot mount option\n");
  2913. rc = -EOPNOTSUPP;
  2914. goto out_fail;
  2915. } else
  2916. tcon->snapshot_time = volume_info->snapshot_time;
  2917. }
  2918. if (volume_info->handle_timeout) {
  2919. if (ses->server->vals->protocol_id == 0) {
  2920. cifs_dbg(VFS,
  2921. "Use SMB2.1 or later for handle timeout option\n");
  2922. rc = -EOPNOTSUPP;
  2923. goto out_fail;
  2924. } else
  2925. tcon->handle_timeout = volume_info->handle_timeout;
  2926. }
  2927. tcon->ses = ses;
  2928. if (volume_info->password) {
  2929. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  2930. if (!tcon->password) {
  2931. rc = -ENOMEM;
  2932. goto out_fail;
  2933. }
  2934. }
  2935. if (volume_info->seal) {
  2936. if (ses->server->vals->protocol_id == 0) {
  2937. cifs_dbg(VFS,
  2938. "SMB3 or later required for encryption\n");
  2939. rc = -EOPNOTSUPP;
  2940. goto out_fail;
  2941. } else if (tcon->ses->server->capabilities &
  2942. SMB2_GLOBAL_CAP_ENCRYPTION)
  2943. tcon->seal = true;
  2944. else {
  2945. cifs_dbg(VFS, "Encryption is not supported on share\n");
  2946. rc = -EOPNOTSUPP;
  2947. goto out_fail;
  2948. }
  2949. }
  2950. if (volume_info->linux_ext) {
  2951. if (ses->server->posix_ext_supported) {
  2952. tcon->posix_extensions = true;
  2953. pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
  2954. } else {
  2955. cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
  2956. rc = -EOPNOTSUPP;
  2957. goto out_fail;
  2958. }
  2959. }
  2960. /*
  2961. * BB Do we need to wrap session_mutex around this TCon call and Unix
  2962. * SetFS as we do on SessSetup and reconnect?
  2963. */
  2964. xid = get_xid();
  2965. rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
  2966. volume_info->local_nls);
  2967. free_xid(xid);
  2968. cifs_dbg(FYI, "Tcon rc = %d\n", rc);
  2969. if (rc)
  2970. goto out_fail;
  2971. tcon->use_persistent = false;
  2972. /* check if SMB2 or later, CIFS does not support persistent handles */
  2973. if (volume_info->persistent) {
  2974. if (ses->server->vals->protocol_id == 0) {
  2975. cifs_dbg(VFS,
  2976. "SMB3 or later required for persistent handles\n");
  2977. rc = -EOPNOTSUPP;
  2978. goto out_fail;
  2979. } else if (ses->server->capabilities &
  2980. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2981. tcon->use_persistent = true;
  2982. else /* persistent handles requested but not supported */ {
  2983. cifs_dbg(VFS,
  2984. "Persistent handles not supported on share\n");
  2985. rc = -EOPNOTSUPP;
  2986. goto out_fail;
  2987. }
  2988. } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
  2989. && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
  2990. && (volume_info->nopersistent == false)) {
  2991. cifs_dbg(FYI, "enabling persistent handles\n");
  2992. tcon->use_persistent = true;
  2993. } else if (volume_info->resilient) {
  2994. if (ses->server->vals->protocol_id == 0) {
  2995. cifs_dbg(VFS,
  2996. "SMB2.1 or later required for resilient handles\n");
  2997. rc = -EOPNOTSUPP;
  2998. goto out_fail;
  2999. }
  3000. tcon->use_resilient = true;
  3001. }
  3002. /* If the user really knows what they are doing they can override */
  3003. if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
  3004. if (volume_info->cache_ro)
  3005. cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
  3006. else if (volume_info->cache_rw)
  3007. cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
  3008. }
  3009. if (volume_info->no_lease) {
  3010. if (ses->server->vals->protocol_id == 0) {
  3011. cifs_dbg(VFS,
  3012. "SMB2 or later required for nolease option\n");
  3013. rc = -EOPNOTSUPP;
  3014. goto out_fail;
  3015. } else
  3016. tcon->no_lease = volume_info->no_lease;
  3017. }
  3018. /*
  3019. * We can have only one retry value for a connection to a share so for
  3020. * resources mounted more than once to the same server share the last
  3021. * value passed in for the retry flag is used.
  3022. */
  3023. tcon->retry = volume_info->retry;
  3024. tcon->nocase = volume_info->nocase;
  3025. if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
  3026. tcon->nohandlecache = volume_info->nohandlecache;
  3027. else
  3028. tcon->nohandlecache = 1;
  3029. tcon->nodelete = volume_info->nodelete;
  3030. tcon->local_lease = volume_info->local_lease;
  3031. INIT_LIST_HEAD(&tcon->pending_opens);
  3032. spin_lock(&cifs_tcp_ses_lock);
  3033. list_add(&tcon->tcon_list, &ses->tcon_list);
  3034. spin_unlock(&cifs_tcp_ses_lock);
  3035. cifs_fscache_get_super_cookie(tcon);
  3036. return tcon;
  3037. out_fail:
  3038. tconInfoFree(tcon);
  3039. return ERR_PTR(rc);
  3040. }
  3041. void
  3042. cifs_put_tlink(struct tcon_link *tlink)
  3043. {
  3044. if (!tlink || IS_ERR(tlink))
  3045. return;
  3046. if (!atomic_dec_and_test(&tlink->tl_count) ||
  3047. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  3048. tlink->tl_time = jiffies;
  3049. return;
  3050. }
  3051. if (!IS_ERR(tlink_tcon(tlink)))
  3052. cifs_put_tcon(tlink_tcon(tlink));
  3053. kfree(tlink);
  3054. return;
  3055. }
  3056. static int
  3057. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  3058. {
  3059. struct cifs_sb_info *old = CIFS_SB(sb);
  3060. struct cifs_sb_info *new = mnt_data->cifs_sb;
  3061. unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
  3062. unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
  3063. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  3064. return 0;
  3065. if (old->mnt_cifs_serverino_autodisabled)
  3066. newflags &= ~CIFS_MOUNT_SERVER_INUM;
  3067. if (oldflags != newflags)
  3068. return 0;
  3069. /*
  3070. * We want to share sb only if we don't specify an r/wsize or
  3071. * specified r/wsize is greater than or equal to existing one.
  3072. */
  3073. if (new->wsize && new->wsize < old->wsize)
  3074. return 0;
  3075. if (new->rsize && new->rsize < old->rsize)
  3076. return 0;
  3077. if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
  3078. return 0;
  3079. if (old->mnt_file_mode != new->mnt_file_mode ||
  3080. old->mnt_dir_mode != new->mnt_dir_mode)
  3081. return 0;
  3082. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  3083. return 0;
  3084. if (old->actimeo != new->actimeo)
  3085. return 0;
  3086. return 1;
  3087. }
  3088. static int
  3089. match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  3090. {
  3091. struct cifs_sb_info *old = CIFS_SB(sb);
  3092. struct cifs_sb_info *new = mnt_data->cifs_sb;
  3093. bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
  3094. old->prepath;
  3095. bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
  3096. new->prepath;
  3097. if (old_set && new_set && !strcmp(new->prepath, old->prepath))
  3098. return 1;
  3099. else if (!old_set && !new_set)
  3100. return 1;
  3101. return 0;
  3102. }
  3103. int
  3104. cifs_match_super(struct super_block *sb, void *data)
  3105. {
  3106. struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
  3107. struct smb_vol *volume_info;
  3108. struct cifs_sb_info *cifs_sb;
  3109. struct TCP_Server_Info *tcp_srv;
  3110. struct cifs_ses *ses;
  3111. struct cifs_tcon *tcon;
  3112. struct tcon_link *tlink;
  3113. int rc = 0;
  3114. spin_lock(&cifs_tcp_ses_lock);
  3115. cifs_sb = CIFS_SB(sb);
  3116. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  3117. if (tlink == NULL) {
  3118. /* can not match superblock if tlink were ever null */
  3119. spin_unlock(&cifs_tcp_ses_lock);
  3120. return 0;
  3121. }
  3122. tcon = tlink_tcon(tlink);
  3123. ses = tcon->ses;
  3124. tcp_srv = ses->server;
  3125. volume_info = mnt_data->vol;
  3126. if (!match_server(tcp_srv, volume_info) ||
  3127. !match_session(ses, volume_info) ||
  3128. !match_tcon(tcon, volume_info) ||
  3129. !match_prepath(sb, mnt_data)) {
  3130. rc = 0;
  3131. goto out;
  3132. }
  3133. rc = compare_mount_options(sb, mnt_data);
  3134. out:
  3135. spin_unlock(&cifs_tcp_ses_lock);
  3136. cifs_put_tlink(tlink);
  3137. return rc;
  3138. }
  3139. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  3140. static struct lock_class_key cifs_key[2];
  3141. static struct lock_class_key cifs_slock_key[2];
  3142. static inline void
  3143. cifs_reclassify_socket4(struct socket *sock)
  3144. {
  3145. struct sock *sk = sock->sk;
  3146. BUG_ON(!sock_allow_reclassification(sk));
  3147. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  3148. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  3149. }
  3150. static inline void
  3151. cifs_reclassify_socket6(struct socket *sock)
  3152. {
  3153. struct sock *sk = sock->sk;
  3154. BUG_ON(!sock_allow_reclassification(sk));
  3155. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  3156. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  3157. }
  3158. #else
  3159. static inline void
  3160. cifs_reclassify_socket4(struct socket *sock)
  3161. {
  3162. }
  3163. static inline void
  3164. cifs_reclassify_socket6(struct socket *sock)
  3165. {
  3166. }
  3167. #endif
  3168. /* See RFC1001 section 14 on representation of Netbios names */
  3169. static void rfc1002mangle(char *target, char *source, unsigned int length)
  3170. {
  3171. unsigned int i, j;
  3172. for (i = 0, j = 0; i < (length); i++) {
  3173. /* mask a nibble at a time and encode */
  3174. target[j] = 'A' + (0x0F & (source[i] >> 4));
  3175. target[j+1] = 'A' + (0x0F & source[i]);
  3176. j += 2;
  3177. }
  3178. }
  3179. static int
  3180. bind_socket(struct TCP_Server_Info *server)
  3181. {
  3182. int rc = 0;
  3183. if (server->srcaddr.ss_family != AF_UNSPEC) {
  3184. /* Bind to the specified local IP address */
  3185. struct socket *socket = server->ssocket;
  3186. rc = socket->ops->bind(socket,
  3187. (struct sockaddr *) &server->srcaddr,
  3188. sizeof(server->srcaddr));
  3189. if (rc < 0) {
  3190. struct sockaddr_in *saddr4;
  3191. struct sockaddr_in6 *saddr6;
  3192. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  3193. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  3194. if (saddr6->sin6_family == AF_INET6)
  3195. cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
  3196. &saddr6->sin6_addr, rc);
  3197. else
  3198. cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
  3199. &saddr4->sin_addr.s_addr, rc);
  3200. }
  3201. }
  3202. return rc;
  3203. }
  3204. static int
  3205. ip_rfc1001_connect(struct TCP_Server_Info *server)
  3206. {
  3207. int rc = 0;
  3208. /*
  3209. * some servers require RFC1001 sessinit before sending
  3210. * negprot - BB check reconnection in case where second
  3211. * sessinit is sent but no second negprot
  3212. */
  3213. struct rfc1002_session_packet *ses_init_buf;
  3214. struct smb_hdr *smb_buf;
  3215. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  3216. GFP_KERNEL);
  3217. if (ses_init_buf) {
  3218. ses_init_buf->trailer.session_req.called_len = 32;
  3219. if (server->server_RFC1001_name[0] != 0)
  3220. rfc1002mangle(ses_init_buf->trailer.
  3221. session_req.called_name,
  3222. server->server_RFC1001_name,
  3223. RFC1001_NAME_LEN_WITH_NULL);
  3224. else
  3225. rfc1002mangle(ses_init_buf->trailer.
  3226. session_req.called_name,
  3227. DEFAULT_CIFS_CALLED_NAME,
  3228. RFC1001_NAME_LEN_WITH_NULL);
  3229. ses_init_buf->trailer.session_req.calling_len = 32;
  3230. /*
  3231. * calling name ends in null (byte 16) from old smb
  3232. * convention.
  3233. */
  3234. if (server->workstation_RFC1001_name[0] != 0)
  3235. rfc1002mangle(ses_init_buf->trailer.
  3236. session_req.calling_name,
  3237. server->workstation_RFC1001_name,
  3238. RFC1001_NAME_LEN_WITH_NULL);
  3239. else
  3240. rfc1002mangle(ses_init_buf->trailer.
  3241. session_req.calling_name,
  3242. "LINUX_CIFS_CLNT",
  3243. RFC1001_NAME_LEN_WITH_NULL);
  3244. ses_init_buf->trailer.session_req.scope1 = 0;
  3245. ses_init_buf->trailer.session_req.scope2 = 0;
  3246. smb_buf = (struct smb_hdr *)ses_init_buf;
  3247. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  3248. smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
  3249. rc = smb_send(server, smb_buf, 0x44);
  3250. kfree(ses_init_buf);
  3251. /*
  3252. * RFC1001 layer in at least one server
  3253. * requires very short break before negprot
  3254. * presumably because not expecting negprot
  3255. * to follow so fast. This is a simple
  3256. * solution that works without
  3257. * complicating the code and causes no
  3258. * significant slowing down on mount
  3259. * for everyone else
  3260. */
  3261. usleep_range(1000, 2000);
  3262. }
  3263. /*
  3264. * else the negprot may still work without this
  3265. * even though malloc failed
  3266. */
  3267. return rc;
  3268. }
  3269. static int
  3270. generic_ip_connect(struct TCP_Server_Info *server)
  3271. {
  3272. int rc = 0;
  3273. __be16 sport;
  3274. int slen, sfamily;
  3275. struct socket *socket = server->ssocket;
  3276. struct sockaddr *saddr;
  3277. saddr = (struct sockaddr *) &server->dstaddr;
  3278. if (server->dstaddr.ss_family == AF_INET6) {
  3279. struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
  3280. sport = ipv6->sin6_port;
  3281. slen = sizeof(struct sockaddr_in6);
  3282. sfamily = AF_INET6;
  3283. cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
  3284. ntohs(sport));
  3285. } else {
  3286. struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
  3287. sport = ipv4->sin_port;
  3288. slen = sizeof(struct sockaddr_in);
  3289. sfamily = AF_INET;
  3290. cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
  3291. ntohs(sport));
  3292. }
  3293. if (socket == NULL) {
  3294. rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
  3295. IPPROTO_TCP, &socket, 1);
  3296. if (rc < 0) {
  3297. cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
  3298. server->ssocket = NULL;
  3299. return rc;
  3300. }
  3301. /* BB other socket options to set KEEPALIVE, NODELAY? */
  3302. cifs_dbg(FYI, "Socket created\n");
  3303. server->ssocket = socket;
  3304. socket->sk->sk_allocation = GFP_NOFS;
  3305. if (sfamily == AF_INET6)
  3306. cifs_reclassify_socket6(socket);
  3307. else
  3308. cifs_reclassify_socket4(socket);
  3309. }
  3310. rc = bind_socket(server);
  3311. if (rc < 0)
  3312. return rc;
  3313. /*
  3314. * Eventually check for other socket options to change from
  3315. * the default. sock_setsockopt not used because it expects
  3316. * user space buffer
  3317. */
  3318. socket->sk->sk_rcvtimeo = 7 * HZ;
  3319. socket->sk->sk_sndtimeo = 5 * HZ;
  3320. /* make the bufsizes depend on wsize/rsize and max requests */
  3321. if (server->noautotune) {
  3322. if (socket->sk->sk_sndbuf < (200 * 1024))
  3323. socket->sk->sk_sndbuf = 200 * 1024;
  3324. if (socket->sk->sk_rcvbuf < (140 * 1024))
  3325. socket->sk->sk_rcvbuf = 140 * 1024;
  3326. }
  3327. if (server->tcp_nodelay)
  3328. tcp_sock_set_nodelay(socket->sk);
  3329. cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
  3330. socket->sk->sk_sndbuf,
  3331. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  3332. rc = socket->ops->connect(socket, saddr, slen,
  3333. server->noblockcnt ? O_NONBLOCK : 0);
  3334. /*
  3335. * When mounting SMB root file systems, we do not want to block in
  3336. * connect. Otherwise bail out and then let cifs_reconnect() perform
  3337. * reconnect failover - if possible.
  3338. */
  3339. if (server->noblockcnt && rc == -EINPROGRESS)
  3340. rc = 0;
  3341. if (rc < 0) {
  3342. cifs_dbg(FYI, "Error %d connecting to server\n", rc);
  3343. sock_release(socket);
  3344. server->ssocket = NULL;
  3345. return rc;
  3346. }
  3347. if (sport == htons(RFC1001_PORT))
  3348. rc = ip_rfc1001_connect(server);
  3349. return rc;
  3350. }
  3351. static int
  3352. ip_connect(struct TCP_Server_Info *server)
  3353. {
  3354. __be16 *sport;
  3355. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  3356. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  3357. if (server->dstaddr.ss_family == AF_INET6)
  3358. sport = &addr6->sin6_port;
  3359. else
  3360. sport = &addr->sin_port;
  3361. if (*sport == 0) {
  3362. int rc;
  3363. /* try with 445 port at first */
  3364. *sport = htons(CIFS_PORT);
  3365. rc = generic_ip_connect(server);
  3366. if (rc >= 0)
  3367. return rc;
  3368. /* if it failed, try with 139 port */
  3369. *sport = htons(RFC1001_PORT);
  3370. }
  3371. return generic_ip_connect(server);
  3372. }
  3373. void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
  3374. struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
  3375. {
  3376. /* if we are reconnecting then should we check to see if
  3377. * any requested capabilities changed locally e.g. via
  3378. * remount but we can not do much about it here
  3379. * if they have (even if we could detect it by the following)
  3380. * Perhaps we could add a backpointer to array of sb from tcon
  3381. * or if we change to make all sb to same share the same
  3382. * sb as NFS - then we only have one backpointer to sb.
  3383. * What if we wanted to mount the server share twice once with
  3384. * and once without posixacls or posix paths? */
  3385. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  3386. if (vol_info && vol_info->no_linux_ext) {
  3387. tcon->fsUnixInfo.Capability = 0;
  3388. tcon->unix_ext = 0; /* Unix Extensions disabled */
  3389. cifs_dbg(FYI, "Linux protocol extensions disabled\n");
  3390. return;
  3391. } else if (vol_info)
  3392. tcon->unix_ext = 1; /* Unix Extensions supported */
  3393. if (tcon->unix_ext == 0) {
  3394. cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
  3395. return;
  3396. }
  3397. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  3398. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  3399. cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
  3400. /* check for reconnect case in which we do not
  3401. want to change the mount behavior if we can avoid it */
  3402. if (vol_info == NULL) {
  3403. /* turn off POSIX ACL and PATHNAMES if not set
  3404. originally at mount time */
  3405. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  3406. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  3407. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  3408. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  3409. cifs_dbg(VFS, "POSIXPATH support change\n");
  3410. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  3411. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  3412. cifs_dbg(VFS, "possible reconnect error\n");
  3413. cifs_dbg(VFS, "server disabled POSIX path support\n");
  3414. }
  3415. }
  3416. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  3417. cifs_dbg(VFS, "per-share encryption not supported yet\n");
  3418. cap &= CIFS_UNIX_CAP_MASK;
  3419. if (vol_info && vol_info->no_psx_acl)
  3420. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  3421. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  3422. cifs_dbg(FYI, "negotiated posix acl support\n");
  3423. if (cifs_sb)
  3424. cifs_sb->mnt_cifs_flags |=
  3425. CIFS_MOUNT_POSIXACL;
  3426. }
  3427. if (vol_info && vol_info->posix_paths == 0)
  3428. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  3429. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  3430. cifs_dbg(FYI, "negotiate posix pathnames\n");
  3431. if (cifs_sb)
  3432. cifs_sb->mnt_cifs_flags |=
  3433. CIFS_MOUNT_POSIX_PATHS;
  3434. }
  3435. cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
  3436. #ifdef CONFIG_CIFS_DEBUG2
  3437. if (cap & CIFS_UNIX_FCNTL_CAP)
  3438. cifs_dbg(FYI, "FCNTL cap\n");
  3439. if (cap & CIFS_UNIX_EXTATTR_CAP)
  3440. cifs_dbg(FYI, "EXTATTR cap\n");
  3441. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  3442. cifs_dbg(FYI, "POSIX path cap\n");
  3443. if (cap & CIFS_UNIX_XATTR_CAP)
  3444. cifs_dbg(FYI, "XATTR cap\n");
  3445. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  3446. cifs_dbg(FYI, "POSIX ACL cap\n");
  3447. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  3448. cifs_dbg(FYI, "very large read cap\n");
  3449. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  3450. cifs_dbg(FYI, "very large write cap\n");
  3451. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  3452. cifs_dbg(FYI, "transport encryption cap\n");
  3453. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  3454. cifs_dbg(FYI, "mandatory transport encryption cap\n");
  3455. #endif /* CIFS_DEBUG2 */
  3456. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  3457. if (vol_info == NULL) {
  3458. cifs_dbg(FYI, "resetting capabilities failed\n");
  3459. } else
  3460. 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");
  3461. }
  3462. }
  3463. }
  3464. int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
  3465. struct cifs_sb_info *cifs_sb)
  3466. {
  3467. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  3468. spin_lock_init(&cifs_sb->tlink_tree_lock);
  3469. cifs_sb->tlink_tree = RB_ROOT;
  3470. cifs_sb->bsize = pvolume_info->bsize;
  3471. /*
  3472. * Temporarily set r/wsize for matching superblock. If we end up using
  3473. * new sb then client will later negotiate it downward if needed.
  3474. */
  3475. cifs_sb->rsize = pvolume_info->rsize;
  3476. cifs_sb->wsize = pvolume_info->wsize;
  3477. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  3478. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  3479. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  3480. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  3481. cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
  3482. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  3483. cifs_sb->actimeo = pvolume_info->actimeo;
  3484. cifs_sb->local_nls = pvolume_info->local_nls;
  3485. if (pvolume_info->nodfs)
  3486. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
  3487. if (pvolume_info->noperm)
  3488. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  3489. if (pvolume_info->setuids)
  3490. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  3491. if (pvolume_info->setuidfromacl)
  3492. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
  3493. if (pvolume_info->server_ino)
  3494. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  3495. if (pvolume_info->remap)
  3496. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
  3497. if (pvolume_info->sfu_remap)
  3498. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  3499. if (pvolume_info->no_xattr)
  3500. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  3501. if (pvolume_info->sfu_emul)
  3502. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  3503. if (pvolume_info->nobrl)
  3504. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  3505. if (pvolume_info->nohandlecache)
  3506. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
  3507. if (pvolume_info->nostrictsync)
  3508. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  3509. if (pvolume_info->mand_lock)
  3510. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  3511. if (pvolume_info->rwpidforward)
  3512. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
  3513. if (pvolume_info->mode_ace)
  3514. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
  3515. if (pvolume_info->cifs_acl)
  3516. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  3517. if (pvolume_info->backupuid_specified) {
  3518. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
  3519. cifs_sb->mnt_backupuid = pvolume_info->backupuid;
  3520. }
  3521. if (pvolume_info->backupgid_specified) {
  3522. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
  3523. cifs_sb->mnt_backupgid = pvolume_info->backupgid;
  3524. }
  3525. if (pvolume_info->override_uid)
  3526. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  3527. if (pvolume_info->override_gid)
  3528. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  3529. if (pvolume_info->dynperm)
  3530. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  3531. if (pvolume_info->fsc)
  3532. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
  3533. if (pvolume_info->multiuser)
  3534. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
  3535. CIFS_MOUNT_NO_PERM);
  3536. if (pvolume_info->strict_io)
  3537. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
  3538. if (pvolume_info->direct_io) {
  3539. cifs_dbg(FYI, "mounting share using direct i/o\n");
  3540. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  3541. }
  3542. if (pvolume_info->cache_ro) {
  3543. cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
  3544. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
  3545. } else if (pvolume_info->cache_rw) {
  3546. cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
  3547. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
  3548. CIFS_MOUNT_RW_CACHE);
  3549. }
  3550. if (pvolume_info->mfsymlinks) {
  3551. if (pvolume_info->sfu_emul) {
  3552. /*
  3553. * Our SFU ("Services for Unix" emulation does not allow
  3554. * creating symlinks but does allow reading existing SFU
  3555. * symlinks (it does allow both creating and reading SFU
  3556. * style mknod and FIFOs though). When "mfsymlinks" and
  3557. * "sfu" are both enabled at the same time, it allows
  3558. * reading both types of symlinks, but will only create
  3559. * them with mfsymlinks format. This allows better
  3560. * Apple compatibility (probably better for Samba too)
  3561. * while still recognizing old Windows style symlinks.
  3562. */
  3563. cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
  3564. }
  3565. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
  3566. }
  3567. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  3568. cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
  3569. if (pvolume_info->prepath) {
  3570. cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
  3571. if (cifs_sb->prepath == NULL)
  3572. return -ENOMEM;
  3573. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  3574. }
  3575. return 0;
  3576. }
  3577. void
  3578. cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
  3579. {
  3580. kfree(volume_info->username);
  3581. kfree_sensitive(volume_info->password);
  3582. kfree(volume_info->UNC);
  3583. kfree(volume_info->domainname);
  3584. kfree(volume_info->iocharset);
  3585. kfree(volume_info->prepath);
  3586. }
  3587. void
  3588. cifs_cleanup_volume_info(struct smb_vol *volume_info)
  3589. {
  3590. if (!volume_info)
  3591. return;
  3592. cifs_cleanup_volume_info_contents(volume_info);
  3593. kfree(volume_info);
  3594. }
  3595. /* Release all succeed connections */
  3596. static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
  3597. unsigned int xid,
  3598. struct TCP_Server_Info *server,
  3599. struct cifs_ses *ses, struct cifs_tcon *tcon)
  3600. {
  3601. int rc = 0;
  3602. if (tcon)
  3603. cifs_put_tcon(tcon);
  3604. else if (ses)
  3605. cifs_put_smb_ses(ses);
  3606. else if (server)
  3607. cifs_put_tcp_session(server, 0);
  3608. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
  3609. free_xid(xid);
  3610. }
  3611. /* Get connections for tcp, ses and tcon */
  3612. static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
  3613. unsigned int *xid,
  3614. struct TCP_Server_Info **nserver,
  3615. struct cifs_ses **nses, struct cifs_tcon **ntcon)
  3616. {
  3617. int rc = 0;
  3618. struct TCP_Server_Info *server;
  3619. struct cifs_ses *ses;
  3620. struct cifs_tcon *tcon;
  3621. *nserver = NULL;
  3622. *nses = NULL;
  3623. *ntcon = NULL;
  3624. *xid = get_xid();
  3625. /* get a reference to a tcp session */
  3626. server = cifs_get_tcp_session(vol);
  3627. if (IS_ERR(server)) {
  3628. rc = PTR_ERR(server);
  3629. return rc;
  3630. }
  3631. *nserver = server;
  3632. /* get a reference to a SMB session */
  3633. ses = cifs_get_smb_ses(server, vol);
  3634. if (IS_ERR(ses)) {
  3635. rc = PTR_ERR(ses);
  3636. return rc;
  3637. }
  3638. *nses = ses;
  3639. if ((vol->persistent == true) && (!(ses->server->capabilities &
  3640. SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
  3641. cifs_server_dbg(VFS, "persistent handles not supported by server\n");
  3642. return -EOPNOTSUPP;
  3643. }
  3644. /* search for existing tcon to this server share */
  3645. tcon = cifs_get_tcon(ses, vol);
  3646. if (IS_ERR(tcon)) {
  3647. rc = PTR_ERR(tcon);
  3648. return rc;
  3649. }
  3650. *ntcon = tcon;
  3651. /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
  3652. if (tcon->posix_extensions)
  3653. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
  3654. /* tell server which Unix caps we support */
  3655. if (cap_unix(tcon->ses)) {
  3656. /*
  3657. * reset of caps checks mount to see if unix extensions disabled
  3658. * for just this mount.
  3659. */
  3660. reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
  3661. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  3662. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  3663. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
  3664. return -EACCES;
  3665. } else
  3666. tcon->unix_ext = 0; /* server does not support them */
  3667. /* do not care if a following call succeed - informational */
  3668. if (!tcon->pipe && server->ops->qfs_tcon) {
  3669. server->ops->qfs_tcon(*xid, tcon, cifs_sb);
  3670. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
  3671. if (tcon->fsDevInfo.DeviceCharacteristics &
  3672. cpu_to_le32(FILE_READ_ONLY_DEVICE))
  3673. cifs_dbg(VFS, "mounted to read only share\n");
  3674. else if ((cifs_sb->mnt_cifs_flags &
  3675. CIFS_MOUNT_RW_CACHE) == 0)
  3676. cifs_dbg(VFS, "read only mount of RW share\n");
  3677. /* no need to log a RW mount of a typical RW share */
  3678. }
  3679. }
  3680. cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
  3681. cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
  3682. return 0;
  3683. }
  3684. static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
  3685. struct cifs_tcon *tcon)
  3686. {
  3687. struct tcon_link *tlink;
  3688. /* hang the tcon off of the superblock */
  3689. tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3690. if (tlink == NULL)
  3691. return -ENOMEM;
  3692. tlink->tl_uid = ses->linux_uid;
  3693. tlink->tl_tcon = tcon;
  3694. tlink->tl_time = jiffies;
  3695. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  3696. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3697. cifs_sb->master_tlink = tlink;
  3698. spin_lock(&cifs_sb->tlink_tree_lock);
  3699. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3700. spin_unlock(&cifs_sb->tlink_tree_lock);
  3701. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  3702. TLINK_IDLE_EXPIRE);
  3703. return 0;
  3704. }
  3705. #ifdef CONFIG_CIFS_DFS_UPCALL
  3706. /*
  3707. * cifs_build_path_to_root returns full path to root when we do not have an
  3708. * exiting connection (tcon)
  3709. */
  3710. static char *
  3711. build_unc_path_to_root(const struct smb_vol *vol,
  3712. const struct cifs_sb_info *cifs_sb, bool useppath)
  3713. {
  3714. char *full_path, *pos;
  3715. unsigned int pplen = useppath && vol->prepath ?
  3716. strlen(vol->prepath) + 1 : 0;
  3717. unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
  3718. if (unc_len > MAX_TREE_SIZE)
  3719. return ERR_PTR(-EINVAL);
  3720. full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
  3721. if (full_path == NULL)
  3722. return ERR_PTR(-ENOMEM);
  3723. memcpy(full_path, vol->UNC, unc_len);
  3724. pos = full_path + unc_len;
  3725. if (pplen) {
  3726. *pos = CIFS_DIR_SEP(cifs_sb);
  3727. memcpy(pos + 1, vol->prepath, pplen);
  3728. pos += pplen;
  3729. }
  3730. *pos = '\0'; /* add trailing null */
  3731. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  3732. cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
  3733. return full_path;
  3734. }
  3735. /**
  3736. * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
  3737. *
  3738. *
  3739. * If a referral is found, cifs_sb->mountdata will be (re-)allocated
  3740. * to a string containing updated options for the submount. Otherwise it
  3741. * will be left untouched.
  3742. *
  3743. * Returns the rc from get_dfs_path to the caller, which can be used to
  3744. * determine whether there were referrals.
  3745. */
  3746. static int
  3747. expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
  3748. struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
  3749. char *ref_path)
  3750. {
  3751. int rc;
  3752. struct dfs_info3_param referral = {0};
  3753. char *full_path = NULL, *mdata = NULL;
  3754. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
  3755. return -EREMOTE;
  3756. full_path = build_unc_path_to_root(volume_info, cifs_sb, true);
  3757. if (IS_ERR(full_path))
  3758. return PTR_ERR(full_path);
  3759. rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
  3760. ref_path, &referral, NULL);
  3761. if (!rc) {
  3762. char *fake_devname = NULL;
  3763. mdata = cifs_compose_mount_options(cifs_sb->mountdata,
  3764. full_path + 1, &referral,
  3765. &fake_devname);
  3766. free_dfs_info_param(&referral);
  3767. if (IS_ERR(mdata)) {
  3768. rc = PTR_ERR(mdata);
  3769. mdata = NULL;
  3770. } else {
  3771. cifs_cleanup_volume_info_contents(volume_info);
  3772. rc = cifs_setup_volume_info(volume_info, mdata,
  3773. fake_devname, false);
  3774. }
  3775. kfree(fake_devname);
  3776. kfree(cifs_sb->mountdata);
  3777. cifs_sb->mountdata = mdata;
  3778. }
  3779. kfree(full_path);
  3780. return rc;
  3781. }
  3782. static inline int get_next_dfs_tgt(const char *path,
  3783. struct dfs_cache_tgt_list *tgt_list,
  3784. struct dfs_cache_tgt_iterator **tgt_it)
  3785. {
  3786. if (!*tgt_it)
  3787. *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
  3788. else
  3789. *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
  3790. return !*tgt_it ? -EHOSTDOWN : 0;
  3791. }
  3792. static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
  3793. struct smb_vol *fake_vol, struct smb_vol *vol)
  3794. {
  3795. const char *tgt = dfs_cache_get_tgt_name(tgt_it);
  3796. int len = strlen(tgt) + 2;
  3797. char *new_unc;
  3798. new_unc = kmalloc(len, GFP_KERNEL);
  3799. if (!new_unc)
  3800. return -ENOMEM;
  3801. scnprintf(new_unc, len, "\\%s", tgt);
  3802. kfree(vol->UNC);
  3803. vol->UNC = new_unc;
  3804. if (fake_vol->prepath) {
  3805. kfree(vol->prepath);
  3806. vol->prepath = fake_vol->prepath;
  3807. fake_vol->prepath = NULL;
  3808. }
  3809. memcpy(&vol->dstaddr, &fake_vol->dstaddr, sizeof(vol->dstaddr));
  3810. return 0;
  3811. }
  3812. static int setup_dfs_tgt_conn(const char *path, const char *full_path,
  3813. const struct dfs_cache_tgt_iterator *tgt_it,
  3814. struct cifs_sb_info *cifs_sb, struct smb_vol *vol, unsigned int *xid,
  3815. struct TCP_Server_Info **server, struct cifs_ses **ses,
  3816. struct cifs_tcon **tcon)
  3817. {
  3818. int rc;
  3819. struct dfs_info3_param ref = {0};
  3820. char *mdata = NULL, *fake_devname = NULL;
  3821. struct smb_vol fake_vol = {NULL};
  3822. cifs_dbg(FYI, "%s: dfs path: %s\n", __func__, path);
  3823. rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
  3824. if (rc)
  3825. return rc;
  3826. mdata = cifs_compose_mount_options(cifs_sb->mountdata, full_path + 1, &ref, &fake_devname);
  3827. free_dfs_info_param(&ref);
  3828. if (IS_ERR(mdata)) {
  3829. rc = PTR_ERR(mdata);
  3830. mdata = NULL;
  3831. } else {
  3832. cifs_dbg(FYI, "%s: fake_devname: %s\n", __func__, fake_devname);
  3833. rc = cifs_setup_volume_info(&fake_vol, mdata, fake_devname,
  3834. false);
  3835. }
  3836. kfree(mdata);
  3837. kfree(fake_devname);
  3838. if (!rc) {
  3839. /*
  3840. * We use a 'fake_vol' here because we need pass it down to the
  3841. * mount_{get,put} functions to test connection against new DFS
  3842. * targets.
  3843. */
  3844. mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
  3845. rc = mount_get_conns(&fake_vol, cifs_sb, xid, server, ses,
  3846. tcon);
  3847. if (!rc || (*server && *ses)) {
  3848. /*
  3849. * We were able to connect to new target server.
  3850. * Update current volume info with new target server.
  3851. */
  3852. rc = update_vol_info(tgt_it, &fake_vol, vol);
  3853. }
  3854. }
  3855. cifs_cleanup_volume_info_contents(&fake_vol);
  3856. return rc;
  3857. }
  3858. static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb,
  3859. struct smb_vol *vol, struct cifs_ses *root_ses, unsigned int *xid,
  3860. struct TCP_Server_Info **server, struct cifs_ses **ses,
  3861. struct cifs_tcon **tcon)
  3862. {
  3863. int rc;
  3864. struct dfs_cache_tgt_list tgt_list;
  3865. struct dfs_cache_tgt_iterator *tgt_it = NULL;
  3866. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
  3867. return -EOPNOTSUPP;
  3868. rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
  3869. if (rc)
  3870. return rc;
  3871. for (;;) {
  3872. /* Get next DFS target server - if any */
  3873. rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
  3874. if (rc)
  3875. break;
  3876. /* Connect to next DFS target */
  3877. rc = setup_dfs_tgt_conn(path, full_path, tgt_it, cifs_sb, vol, xid, server, ses,
  3878. tcon);
  3879. if (!rc || (*server && *ses))
  3880. break;
  3881. }
  3882. if (!rc) {
  3883. /*
  3884. * Update DFS target hint in DFS referral cache with the target
  3885. * server we successfully reconnected to.
  3886. */
  3887. rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses,
  3888. cifs_sb->local_nls,
  3889. cifs_remap(cifs_sb), path,
  3890. tgt_it);
  3891. }
  3892. dfs_cache_free_tgts(&tgt_list);
  3893. return rc;
  3894. }
  3895. #endif
  3896. int
  3897. cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  3898. const char *devname, bool is_smb3)
  3899. {
  3900. int rc = 0;
  3901. if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
  3902. return -EINVAL;
  3903. if (volume_info->nullauth) {
  3904. cifs_dbg(FYI, "Anonymous login\n");
  3905. kfree(volume_info->username);
  3906. volume_info->username = NULL;
  3907. } else if (volume_info->username) {
  3908. /* BB fixme parse for domain name here */
  3909. cifs_dbg(FYI, "Username: %s\n", volume_info->username);
  3910. } else {
  3911. cifs_dbg(VFS, "No username specified\n");
  3912. /* In userspace mount helper we can get user name from alternate
  3913. locations such as env variables and files on disk */
  3914. return -EINVAL;
  3915. }
  3916. /* this is needed for ASCII cp to Unicode converts */
  3917. if (volume_info->iocharset == NULL) {
  3918. /* load_nls_default cannot return null */
  3919. volume_info->local_nls = load_nls_default();
  3920. } else {
  3921. volume_info->local_nls = load_nls(volume_info->iocharset);
  3922. if (volume_info->local_nls == NULL) {
  3923. cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
  3924. volume_info->iocharset);
  3925. return -ELIBACC;
  3926. }
  3927. }
  3928. return rc;
  3929. }
  3930. struct smb_vol *
  3931. cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
  3932. {
  3933. int rc;
  3934. struct smb_vol *volume_info;
  3935. volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
  3936. if (!volume_info)
  3937. return ERR_PTR(-ENOMEM);
  3938. rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
  3939. if (rc) {
  3940. cifs_cleanup_volume_info(volume_info);
  3941. volume_info = ERR_PTR(rc);
  3942. }
  3943. return volume_info;
  3944. }
  3945. static int
  3946. cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
  3947. unsigned int xid,
  3948. struct cifs_tcon *tcon,
  3949. struct cifs_sb_info *cifs_sb,
  3950. char *full_path,
  3951. int added_treename)
  3952. {
  3953. int rc;
  3954. char *s;
  3955. char sep, tmp;
  3956. int skip = added_treename ? 1 : 0;
  3957. sep = CIFS_DIR_SEP(cifs_sb);
  3958. s = full_path;
  3959. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
  3960. while (rc == 0) {
  3961. /* skip separators */
  3962. while (*s == sep)
  3963. s++;
  3964. if (!*s)
  3965. break;
  3966. /* next separator */
  3967. while (*s && *s != sep)
  3968. s++;
  3969. /*
  3970. * if the treename is added, we then have to skip the first
  3971. * part within the separators
  3972. */
  3973. if (skip) {
  3974. skip = 0;
  3975. continue;
  3976. }
  3977. /*
  3978. * temporarily null-terminate the path at the end of
  3979. * the current component
  3980. */
  3981. tmp = *s;
  3982. *s = 0;
  3983. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  3984. full_path);
  3985. *s = tmp;
  3986. }
  3987. return rc;
  3988. }
  3989. /*
  3990. * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
  3991. * otherwise 0.
  3992. */
  3993. static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
  3994. const unsigned int xid,
  3995. struct TCP_Server_Info *server,
  3996. struct cifs_tcon *tcon)
  3997. {
  3998. int rc;
  3999. char *full_path;
  4000. if (!server->ops->is_path_accessible)
  4001. return -EOPNOTSUPP;
  4002. /*
  4003. * cifs_build_path_to_root works only when we have a valid tcon
  4004. */
  4005. full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
  4006. tcon->Flags & SMB_SHARE_IS_IN_DFS);
  4007. if (full_path == NULL)
  4008. return -ENOMEM;
  4009. cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
  4010. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
  4011. full_path);
  4012. if (rc != 0 && rc != -EREMOTE) {
  4013. kfree(full_path);
  4014. return rc;
  4015. }
  4016. if (rc != -EREMOTE) {
  4017. rc = cifs_are_all_path_components_accessible(server, xid, tcon,
  4018. cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
  4019. if (rc != 0) {
  4020. cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
  4021. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  4022. rc = 0;
  4023. }
  4024. }
  4025. kfree(full_path);
  4026. return rc;
  4027. }
  4028. #ifdef CONFIG_CIFS_DFS_UPCALL
  4029. static void set_root_ses(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
  4030. struct cifs_ses **root_ses)
  4031. {
  4032. if (ses) {
  4033. spin_lock(&cifs_tcp_ses_lock);
  4034. ses->ses_count++;
  4035. if (ses->tcon_ipc)
  4036. ses->tcon_ipc->remap = cifs_remap(cifs_sb);
  4037. spin_unlock(&cifs_tcp_ses_lock);
  4038. }
  4039. *root_ses = ses;
  4040. }
  4041. static void put_root_ses(struct cifs_ses *ses)
  4042. {
  4043. if (ses)
  4044. cifs_put_smb_ses(ses);
  4045. }
  4046. /* Check if a path component is remote and then update @dfs_path accordingly */
  4047. static int check_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
  4048. const unsigned int xid, struct TCP_Server_Info *server,
  4049. struct cifs_tcon *tcon, char **dfs_path)
  4050. {
  4051. char *path, *s;
  4052. char sep = CIFS_DIR_SEP(cifs_sb), tmp;
  4053. char *npath;
  4054. int rc = 0;
  4055. int added_treename = tcon->Flags & SMB_SHARE_IS_IN_DFS;
  4056. int skip = added_treename;
  4057. path = cifs_build_path_to_root(vol, cifs_sb, tcon, added_treename);
  4058. if (!path)
  4059. return -ENOMEM;
  4060. /*
  4061. * Walk through the path components in @path and check if they're accessible. In case any of
  4062. * the components is -EREMOTE, then update @dfs_path with the next DFS referral request path
  4063. * (NOT including the remaining components).
  4064. */
  4065. s = path;
  4066. do {
  4067. /* skip separators */
  4068. while (*s && *s == sep)
  4069. s++;
  4070. if (!*s)
  4071. break;
  4072. /* next separator */
  4073. while (*s && *s != sep)
  4074. s++;
  4075. /*
  4076. * if the treename is added, we then have to skip the first
  4077. * part within the separators
  4078. */
  4079. if (skip) {
  4080. skip = 0;
  4081. continue;
  4082. }
  4083. tmp = *s;
  4084. *s = 0;
  4085. rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, path);
  4086. if (rc && rc == -EREMOTE) {
  4087. struct smb_vol v = {NULL};
  4088. /* if @path contains a tree name, skip it in the prefix path */
  4089. if (added_treename) {
  4090. rc = cifs_parse_devname(path, &v);
  4091. if (rc)
  4092. break;
  4093. rc = -EREMOTE;
  4094. npath = build_unc_path_to_root(&v, cifs_sb, true);
  4095. cifs_cleanup_volume_info_contents(&v);
  4096. } else {
  4097. v.UNC = vol->UNC;
  4098. v.prepath = path + 1;
  4099. npath = build_unc_path_to_root(&v, cifs_sb, true);
  4100. }
  4101. if (IS_ERR(npath)) {
  4102. rc = PTR_ERR(npath);
  4103. break;
  4104. }
  4105. kfree(*dfs_path);
  4106. *dfs_path = npath;
  4107. }
  4108. *s = tmp;
  4109. } while (rc == 0);
  4110. kfree(path);
  4111. return rc;
  4112. }
  4113. int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
  4114. {
  4115. int rc = 0;
  4116. unsigned int xid;
  4117. struct TCP_Server_Info *server = NULL;
  4118. struct cifs_ses *ses = NULL, *root_ses = NULL;
  4119. struct cifs_tcon *tcon = NULL;
  4120. int count = 0;
  4121. char *ref_path = NULL, *full_path = NULL;
  4122. char *oldmnt = NULL;
  4123. char *mntdata = NULL;
  4124. rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
  4125. /*
  4126. * Unconditionally try to get an DFS referral (even cached) to determine whether it is an
  4127. * DFS mount.
  4128. *
  4129. * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
  4130. * to respond with PATH_NOT_COVERED to requests that include the prefix.
  4131. */
  4132. if (dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), vol->UNC + 1, NULL,
  4133. NULL)) {
  4134. /* No DFS referral was returned. Looks like a regular share. */
  4135. if (rc)
  4136. goto error;
  4137. /* Check if it is fully accessible and then mount it */
  4138. rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
  4139. if (!rc)
  4140. goto out;
  4141. if (rc != -EREMOTE)
  4142. goto error;
  4143. }
  4144. /* Save mount options */
  4145. mntdata = kstrndup(cifs_sb->mountdata, strlen(cifs_sb->mountdata), GFP_KERNEL);
  4146. if (!mntdata) {
  4147. rc = -ENOMEM;
  4148. goto error;
  4149. }
  4150. /* Get path of DFS root */
  4151. ref_path = build_unc_path_to_root(vol, cifs_sb, false);
  4152. if (IS_ERR(ref_path)) {
  4153. rc = PTR_ERR(ref_path);
  4154. ref_path = NULL;
  4155. goto error;
  4156. }
  4157. set_root_ses(cifs_sb, ses, &root_ses);
  4158. do {
  4159. /* Save full path of last DFS path we used to resolve final target server */
  4160. kfree(full_path);
  4161. full_path = build_unc_path_to_root(vol, cifs_sb, !!count);
  4162. if (IS_ERR(full_path)) {
  4163. rc = PTR_ERR(full_path);
  4164. full_path = NULL;
  4165. break;
  4166. }
  4167. /* Chase referral */
  4168. oldmnt = cifs_sb->mountdata;
  4169. rc = expand_dfs_referral(xid, root_ses, vol, cifs_sb, ref_path + 1);
  4170. if (rc)
  4171. break;
  4172. /* Connect to new DFS target only if we were redirected */
  4173. if (oldmnt != cifs_sb->mountdata) {
  4174. mount_put_conns(cifs_sb, xid, server, ses, tcon);
  4175. rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
  4176. }
  4177. if (rc && !server && !ses) {
  4178. /* Failed to connect. Try to connect to other targets in the referral. */
  4179. rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, vol, root_ses, &xid,
  4180. &server, &ses, &tcon);
  4181. }
  4182. if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses)
  4183. break;
  4184. if (!tcon)
  4185. continue;
  4186. /* Make sure that requests go through new root servers */
  4187. if (is_tcon_dfs(tcon)) {
  4188. put_root_ses(root_ses);
  4189. set_root_ses(cifs_sb, ses, &root_ses);
  4190. }
  4191. /* Check for remaining path components and then continue chasing them (-EREMOTE) */
  4192. rc = check_dfs_prepath(cifs_sb, vol, xid, server, tcon, &ref_path);
  4193. /* Prevent recursion on broken link referrals */
  4194. if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS)
  4195. rc = -ELOOP;
  4196. } while (rc == -EREMOTE);
  4197. if (rc)
  4198. goto error;
  4199. put_root_ses(root_ses);
  4200. root_ses = NULL;
  4201. kfree(ref_path);
  4202. ref_path = NULL;
  4203. /*
  4204. * Store DFS full path in both superblock and tree connect structures.
  4205. *
  4206. * For DFS root mounts, the prefix path (cifs_sb->prepath) is preserved during reconnect so
  4207. * only the root path is set in cifs_sb->origin_fullpath and tcon->dfs_path. And for DFS
  4208. * links, the prefix path is included in both and may be changed during reconnect. See
  4209. * cifs_tree_connect().
  4210. */
  4211. cifs_sb->origin_fullpath = kstrndup(full_path, strlen(full_path), GFP_KERNEL);
  4212. if (!cifs_sb->origin_fullpath) {
  4213. rc = -ENOMEM;
  4214. goto error;
  4215. }
  4216. spin_lock(&cifs_tcp_ses_lock);
  4217. tcon->dfs_path = full_path;
  4218. full_path = NULL;
  4219. tcon->remap = cifs_remap(cifs_sb);
  4220. spin_unlock(&cifs_tcp_ses_lock);
  4221. /* Add original volume information for DFS cache to be used when refreshing referrals */
  4222. rc = dfs_cache_add_vol(mntdata, vol, cifs_sb->origin_fullpath);
  4223. if (rc)
  4224. goto error;
  4225. /*
  4226. * After reconnecting to a different server, unique ids won't
  4227. * match anymore, so we disable serverino. This prevents
  4228. * dentry revalidation to think the dentry are stale (ESTALE).
  4229. */
  4230. cifs_autodisable_serverino(cifs_sb);
  4231. /*
  4232. * Force the use of prefix path to support failover on DFS paths that
  4233. * resolve to targets that have different prefix paths.
  4234. */
  4235. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
  4236. kfree(cifs_sb->prepath);
  4237. cifs_sb->prepath = vol->prepath;
  4238. vol->prepath = NULL;
  4239. out:
  4240. free_xid(xid);
  4241. cifs_try_adding_channels(ses);
  4242. return mount_setup_tlink(cifs_sb, ses, tcon);
  4243. error:
  4244. kfree(ref_path);
  4245. kfree(full_path);
  4246. kfree(mntdata);
  4247. kfree(cifs_sb->origin_fullpath);
  4248. put_root_ses(root_ses);
  4249. mount_put_conns(cifs_sb, xid, server, ses, tcon);
  4250. return rc;
  4251. }
  4252. #else
  4253. int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
  4254. {
  4255. int rc = 0;
  4256. unsigned int xid;
  4257. struct cifs_ses *ses;
  4258. struct cifs_tcon *tcon;
  4259. struct TCP_Server_Info *server;
  4260. rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
  4261. if (rc)
  4262. goto error;
  4263. if (tcon) {
  4264. rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
  4265. if (rc == -EREMOTE)
  4266. rc = -EOPNOTSUPP;
  4267. if (rc)
  4268. goto error;
  4269. }
  4270. free_xid(xid);
  4271. return mount_setup_tlink(cifs_sb, ses, tcon);
  4272. error:
  4273. mount_put_conns(cifs_sb, xid, server, ses, tcon);
  4274. return rc;
  4275. }
  4276. #endif
  4277. /*
  4278. * Issue a TREE_CONNECT request.
  4279. */
  4280. int
  4281. CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
  4282. const char *tree, struct cifs_tcon *tcon,
  4283. const struct nls_table *nls_codepage)
  4284. {
  4285. struct smb_hdr *smb_buffer;
  4286. struct smb_hdr *smb_buffer_response;
  4287. TCONX_REQ *pSMB;
  4288. TCONX_RSP *pSMBr;
  4289. unsigned char *bcc_ptr;
  4290. int rc = 0;
  4291. int length;
  4292. __u16 bytes_left, count;
  4293. if (ses == NULL)
  4294. return -EIO;
  4295. smb_buffer = cifs_buf_get();
  4296. if (smb_buffer == NULL)
  4297. return -ENOMEM;
  4298. smb_buffer_response = smb_buffer;
  4299. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  4300. NULL /*no tid */ , 4 /*wct */ );
  4301. smb_buffer->Mid = get_next_mid(ses->server);
  4302. smb_buffer->Uid = ses->Suid;
  4303. pSMB = (TCONX_REQ *) smb_buffer;
  4304. pSMBr = (TCONX_RSP *) smb_buffer_response;
  4305. pSMB->AndXCommand = 0xFF;
  4306. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  4307. bcc_ptr = &pSMB->Password[0];
  4308. if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
  4309. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  4310. *bcc_ptr = 0; /* password is null byte */
  4311. bcc_ptr++; /* skip password */
  4312. /* already aligned so no need to do it below */
  4313. } else {
  4314. pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  4315. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  4316. specified as required (when that support is added to
  4317. the vfs in the future) as only NTLM or the much
  4318. weaker LANMAN (which we do not send by default) is accepted
  4319. by Samba (not sure whether other servers allow
  4320. NTLMv2 password here) */
  4321. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  4322. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  4323. (ses->sectype == LANMAN))
  4324. calc_lanman_hash(tcon->password, ses->server->cryptkey,
  4325. ses->server->sec_mode &
  4326. SECMODE_PW_ENCRYPT ? true : false,
  4327. bcc_ptr);
  4328. else
  4329. #endif /* CIFS_WEAK_PW_HASH */
  4330. rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
  4331. bcc_ptr, nls_codepage);
  4332. if (rc) {
  4333. cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
  4334. __func__, rc);
  4335. cifs_buf_release(smb_buffer);
  4336. return rc;
  4337. }
  4338. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  4339. if (ses->capabilities & CAP_UNICODE) {
  4340. /* must align unicode strings */
  4341. *bcc_ptr = 0; /* null byte password */
  4342. bcc_ptr++;
  4343. }
  4344. }
  4345. if (ses->server->sign)
  4346. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  4347. if (ses->capabilities & CAP_STATUS32) {
  4348. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  4349. }
  4350. if (ses->capabilities & CAP_DFS) {
  4351. smb_buffer->Flags2 |= SMBFLG2_DFS;
  4352. }
  4353. if (ses->capabilities & CAP_UNICODE) {
  4354. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  4355. length =
  4356. cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
  4357. 6 /* max utf8 char length in bytes */ *
  4358. (/* server len*/ + 256 /* share len */), nls_codepage);
  4359. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  4360. bcc_ptr += 2; /* skip trailing null */
  4361. } else { /* ASCII */
  4362. strcpy(bcc_ptr, tree);
  4363. bcc_ptr += strlen(tree) + 1;
  4364. }
  4365. strcpy(bcc_ptr, "?????");
  4366. bcc_ptr += strlen("?????");
  4367. bcc_ptr += 1;
  4368. count = bcc_ptr - &pSMB->Password[0];
  4369. be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
  4370. pSMB->ByteCount = cpu_to_le16(count);
  4371. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  4372. 0);
  4373. /* above now done in SendReceive */
  4374. if (rc == 0) {
  4375. bool is_unicode;
  4376. tcon->tidStatus = CifsGood;
  4377. tcon->need_reconnect = false;
  4378. tcon->tid = smb_buffer_response->Tid;
  4379. bcc_ptr = pByteArea(smb_buffer_response);
  4380. bytes_left = get_bcc(smb_buffer_response);
  4381. length = strnlen(bcc_ptr, bytes_left - 2);
  4382. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  4383. is_unicode = true;
  4384. else
  4385. is_unicode = false;
  4386. /* skip service field (NB: this field is always ASCII) */
  4387. if (length == 3) {
  4388. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  4389. (bcc_ptr[2] == 'C')) {
  4390. cifs_dbg(FYI, "IPC connection\n");
  4391. tcon->ipc = true;
  4392. tcon->pipe = true;
  4393. }
  4394. } else if (length == 2) {
  4395. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  4396. /* the most common case */
  4397. cifs_dbg(FYI, "disk share connection\n");
  4398. }
  4399. }
  4400. bcc_ptr += length + 1;
  4401. bytes_left -= (length + 1);
  4402. strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
  4403. /* mostly informational -- no need to fail on error here */
  4404. kfree(tcon->nativeFileSystem);
  4405. tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
  4406. bytes_left, is_unicode,
  4407. nls_codepage);
  4408. cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
  4409. if ((smb_buffer_response->WordCount == 3) ||
  4410. (smb_buffer_response->WordCount == 7))
  4411. /* field is in same location */
  4412. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  4413. else
  4414. tcon->Flags = 0;
  4415. cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
  4416. }
  4417. cifs_buf_release(smb_buffer);
  4418. return rc;
  4419. }
  4420. static void delayed_free(struct rcu_head *p)
  4421. {
  4422. struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
  4423. unload_nls(sbi->local_nls);
  4424. kfree(sbi);
  4425. }
  4426. void
  4427. cifs_umount(struct cifs_sb_info *cifs_sb)
  4428. {
  4429. struct rb_root *root = &cifs_sb->tlink_tree;
  4430. struct rb_node *node;
  4431. struct tcon_link *tlink;
  4432. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  4433. spin_lock(&cifs_sb->tlink_tree_lock);
  4434. while ((node = rb_first(root))) {
  4435. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  4436. cifs_get_tlink(tlink);
  4437. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  4438. rb_erase(node, root);
  4439. spin_unlock(&cifs_sb->tlink_tree_lock);
  4440. cifs_put_tlink(tlink);
  4441. spin_lock(&cifs_sb->tlink_tree_lock);
  4442. }
  4443. spin_unlock(&cifs_sb->tlink_tree_lock);
  4444. kfree(cifs_sb->mountdata);
  4445. kfree(cifs_sb->prepath);
  4446. #ifdef CONFIG_CIFS_DFS_UPCALL
  4447. dfs_cache_del_vol(cifs_sb->origin_fullpath);
  4448. kfree(cifs_sb->origin_fullpath);
  4449. #endif
  4450. call_rcu(&cifs_sb->rcu, delayed_free);
  4451. }
  4452. int
  4453. cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
  4454. {
  4455. int rc = 0;
  4456. struct TCP_Server_Info *server = cifs_ses_server(ses);
  4457. if (!server->ops->need_neg || !server->ops->negotiate)
  4458. return -ENOSYS;
  4459. /* only send once per connect */
  4460. if (!server->ops->need_neg(server))
  4461. return 0;
  4462. rc = server->ops->negotiate(xid, ses);
  4463. if (rc == 0) {
  4464. spin_lock(&GlobalMid_Lock);
  4465. if (server->tcpStatus == CifsNeedNegotiate)
  4466. server->tcpStatus = CifsGood;
  4467. else
  4468. rc = -EHOSTDOWN;
  4469. spin_unlock(&GlobalMid_Lock);
  4470. }
  4471. return rc;
  4472. }
  4473. int
  4474. cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
  4475. struct nls_table *nls_info)
  4476. {
  4477. int rc = -ENOSYS;
  4478. struct TCP_Server_Info *server = cifs_ses_server(ses);
  4479. if (!ses->binding) {
  4480. ses->capabilities = server->capabilities;
  4481. if (linuxExtEnabled == 0)
  4482. ses->capabilities &= (~server->vals->cap_unix);
  4483. if (ses->auth_key.response) {
  4484. cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
  4485. ses->auth_key.response);
  4486. kfree(ses->auth_key.response);
  4487. ses->auth_key.response = NULL;
  4488. ses->auth_key.len = 0;
  4489. }
  4490. }
  4491. cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
  4492. server->sec_mode, server->capabilities, server->timeAdj);
  4493. if (server->ops->sess_setup)
  4494. rc = server->ops->sess_setup(xid, ses, nls_info);
  4495. if (rc)
  4496. cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
  4497. return rc;
  4498. }
  4499. static int
  4500. cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
  4501. {
  4502. vol->sectype = ses->sectype;
  4503. /* krb5 is special, since we don't need username or pw */
  4504. if (vol->sectype == Kerberos)
  4505. return 0;
  4506. return cifs_set_cifscreds(vol, ses);
  4507. }
  4508. static struct cifs_tcon *
  4509. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
  4510. {
  4511. int rc;
  4512. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  4513. struct cifs_ses *ses;
  4514. struct cifs_tcon *tcon = NULL;
  4515. struct smb_vol *vol_info;
  4516. vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
  4517. if (vol_info == NULL)
  4518. return ERR_PTR(-ENOMEM);
  4519. vol_info->local_nls = cifs_sb->local_nls;
  4520. vol_info->linux_uid = fsuid;
  4521. vol_info->cred_uid = fsuid;
  4522. vol_info->UNC = master_tcon->treeName;
  4523. vol_info->retry = master_tcon->retry;
  4524. vol_info->nocase = master_tcon->nocase;
  4525. vol_info->nohandlecache = master_tcon->nohandlecache;
  4526. vol_info->local_lease = master_tcon->local_lease;
  4527. vol_info->no_lease = master_tcon->no_lease;
  4528. vol_info->resilient = master_tcon->use_resilient;
  4529. vol_info->persistent = master_tcon->use_persistent;
  4530. vol_info->handle_timeout = master_tcon->handle_timeout;
  4531. vol_info->no_linux_ext = !master_tcon->unix_ext;
  4532. vol_info->linux_ext = master_tcon->posix_extensions;
  4533. vol_info->sectype = master_tcon->ses->sectype;
  4534. vol_info->sign = master_tcon->ses->sign;
  4535. vol_info->seal = master_tcon->seal;
  4536. rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
  4537. if (rc) {
  4538. tcon = ERR_PTR(rc);
  4539. goto out;
  4540. }
  4541. /* get a reference for the same TCP session */
  4542. spin_lock(&cifs_tcp_ses_lock);
  4543. ++master_tcon->ses->server->srv_count;
  4544. spin_unlock(&cifs_tcp_ses_lock);
  4545. ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
  4546. if (IS_ERR(ses)) {
  4547. tcon = (struct cifs_tcon *)ses;
  4548. cifs_put_tcp_session(master_tcon->ses->server, 0);
  4549. goto out;
  4550. }
  4551. tcon = cifs_get_tcon(ses, vol_info);
  4552. if (IS_ERR(tcon)) {
  4553. cifs_put_smb_ses(ses);
  4554. goto out;
  4555. }
  4556. if (cap_unix(ses))
  4557. reset_cifs_unix_caps(0, tcon, NULL, vol_info);
  4558. out:
  4559. kfree(vol_info->username);
  4560. kfree_sensitive(vol_info->password);
  4561. kfree(vol_info);
  4562. return tcon;
  4563. }
  4564. struct cifs_tcon *
  4565. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  4566. {
  4567. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  4568. }
  4569. /* find and return a tlink with given uid */
  4570. static struct tcon_link *
  4571. tlink_rb_search(struct rb_root *root, kuid_t uid)
  4572. {
  4573. struct rb_node *node = root->rb_node;
  4574. struct tcon_link *tlink;
  4575. while (node) {
  4576. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  4577. if (uid_gt(tlink->tl_uid, uid))
  4578. node = node->rb_left;
  4579. else if (uid_lt(tlink->tl_uid, uid))
  4580. node = node->rb_right;
  4581. else
  4582. return tlink;
  4583. }
  4584. return NULL;
  4585. }
  4586. /* insert a tcon_link into the tree */
  4587. static void
  4588. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  4589. {
  4590. struct rb_node **new = &(root->rb_node), *parent = NULL;
  4591. struct tcon_link *tlink;
  4592. while (*new) {
  4593. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  4594. parent = *new;
  4595. if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
  4596. new = &((*new)->rb_left);
  4597. else
  4598. new = &((*new)->rb_right);
  4599. }
  4600. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  4601. rb_insert_color(&new_tlink->tl_rbnode, root);
  4602. }
  4603. /*
  4604. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  4605. * current task.
  4606. *
  4607. * If the superblock doesn't refer to a multiuser mount, then just return
  4608. * the master tcon for the mount.
  4609. *
  4610. * First, search the rbtree for an existing tcon for this fsuid. If one
  4611. * exists, then check to see if it's pending construction. If it is then wait
  4612. * for construction to complete. Once it's no longer pending, check to see if
  4613. * it failed and either return an error or retry construction, depending on
  4614. * the timeout.
  4615. *
  4616. * If one doesn't exist then insert a new tcon_link struct into the tree and
  4617. * try to construct a new one.
  4618. */
  4619. struct tcon_link *
  4620. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  4621. {
  4622. int ret;
  4623. kuid_t fsuid = current_fsuid();
  4624. struct tcon_link *tlink, *newtlink;
  4625. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  4626. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  4627. spin_lock(&cifs_sb->tlink_tree_lock);
  4628. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  4629. if (tlink)
  4630. cifs_get_tlink(tlink);
  4631. spin_unlock(&cifs_sb->tlink_tree_lock);
  4632. if (tlink == NULL) {
  4633. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  4634. if (newtlink == NULL)
  4635. return ERR_PTR(-ENOMEM);
  4636. newtlink->tl_uid = fsuid;
  4637. newtlink->tl_tcon = ERR_PTR(-EACCES);
  4638. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  4639. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  4640. cifs_get_tlink(newtlink);
  4641. spin_lock(&cifs_sb->tlink_tree_lock);
  4642. /* was one inserted after previous search? */
  4643. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  4644. if (tlink) {
  4645. cifs_get_tlink(tlink);
  4646. spin_unlock(&cifs_sb->tlink_tree_lock);
  4647. kfree(newtlink);
  4648. goto wait_for_construction;
  4649. }
  4650. tlink = newtlink;
  4651. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  4652. spin_unlock(&cifs_sb->tlink_tree_lock);
  4653. } else {
  4654. wait_for_construction:
  4655. ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  4656. TASK_INTERRUPTIBLE);
  4657. if (ret) {
  4658. cifs_put_tlink(tlink);
  4659. return ERR_PTR(-ERESTARTSYS);
  4660. }
  4661. /* if it's good, return it */
  4662. if (!IS_ERR(tlink->tl_tcon))
  4663. return tlink;
  4664. /* return error if we tried this already recently */
  4665. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  4666. cifs_put_tlink(tlink);
  4667. return ERR_PTR(-EACCES);
  4668. }
  4669. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  4670. goto wait_for_construction;
  4671. }
  4672. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  4673. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  4674. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  4675. if (IS_ERR(tlink->tl_tcon)) {
  4676. cifs_put_tlink(tlink);
  4677. return ERR_PTR(-EACCES);
  4678. }
  4679. return tlink;
  4680. }
  4681. /*
  4682. * periodic workqueue job that scans tcon_tree for a superblock and closes
  4683. * out tcons.
  4684. */
  4685. static void
  4686. cifs_prune_tlinks(struct work_struct *work)
  4687. {
  4688. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  4689. prune_tlinks.work);
  4690. struct rb_root *root = &cifs_sb->tlink_tree;
  4691. struct rb_node *node;
  4692. struct rb_node *tmp;
  4693. struct tcon_link *tlink;
  4694. /*
  4695. * Because we drop the spinlock in the loop in order to put the tlink
  4696. * it's not guarded against removal of links from the tree. The only
  4697. * places that remove entries from the tree are this function and
  4698. * umounts. Because this function is non-reentrant and is canceled
  4699. * before umount can proceed, this is safe.
  4700. */
  4701. spin_lock(&cifs_sb->tlink_tree_lock);
  4702. node = rb_first(root);
  4703. while (node != NULL) {
  4704. tmp = node;
  4705. node = rb_next(tmp);
  4706. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  4707. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  4708. atomic_read(&tlink->tl_count) != 0 ||
  4709. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  4710. continue;
  4711. cifs_get_tlink(tlink);
  4712. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  4713. rb_erase(tmp, root);
  4714. spin_unlock(&cifs_sb->tlink_tree_lock);
  4715. cifs_put_tlink(tlink);
  4716. spin_lock(&cifs_sb->tlink_tree_lock);
  4717. }
  4718. spin_unlock(&cifs_sb->tlink_tree_lock);
  4719. queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
  4720. TLINK_IDLE_EXPIRE);
  4721. }
  4722. #ifdef CONFIG_CIFS_DFS_UPCALL
  4723. int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
  4724. {
  4725. int rc;
  4726. struct TCP_Server_Info *server = tcon->ses->server;
  4727. const struct smb_version_operations *ops = server->ops;
  4728. struct dfs_cache_tgt_list tl;
  4729. struct dfs_cache_tgt_iterator *it = NULL;
  4730. char *tree;
  4731. const char *tcp_host;
  4732. size_t tcp_host_len;
  4733. const char *dfs_host;
  4734. size_t dfs_host_len;
  4735. char *share = NULL, *prefix = NULL;
  4736. struct dfs_info3_param ref = {0};
  4737. bool isroot;
  4738. tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
  4739. if (!tree)
  4740. return -ENOMEM;
  4741. /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
  4742. if (!tcon->dfs_path || dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl)) {
  4743. if (tcon->ipc) {
  4744. scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
  4745. rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
  4746. } else {
  4747. rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
  4748. }
  4749. goto out;
  4750. }
  4751. isroot = ref.server_type == DFS_TYPE_ROOT;
  4752. free_dfs_info_param(&ref);
  4753. extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
  4754. for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) {
  4755. bool target_match;
  4756. kfree(share);
  4757. kfree(prefix);
  4758. share = NULL;
  4759. prefix = NULL;
  4760. rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix);
  4761. if (rc) {
  4762. cifs_dbg(VFS, "%s: failed to parse target share %d\n",
  4763. __func__, rc);
  4764. continue;
  4765. }
  4766. extract_unc_hostname(share, &dfs_host, &dfs_host_len);
  4767. if (dfs_host_len != tcp_host_len
  4768. || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
  4769. cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
  4770. dfs_host, (int)tcp_host_len, tcp_host);
  4771. rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match);
  4772. if (rc) {
  4773. cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
  4774. break;
  4775. }
  4776. if (!target_match) {
  4777. cifs_dbg(FYI, "%s: skipping target\n", __func__);
  4778. continue;
  4779. }
  4780. }
  4781. if (tcon->ipc) {
  4782. scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share);
  4783. rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
  4784. } else {
  4785. scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
  4786. rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
  4787. /* Only handle prefix paths of DFS link targets */
  4788. if (!rc && !isroot) {
  4789. rc = update_super_prepath(tcon, prefix);
  4790. break;
  4791. }
  4792. }
  4793. if (rc == -EREMOTE)
  4794. break;
  4795. }
  4796. kfree(share);
  4797. kfree(prefix);
  4798. if (!rc) {
  4799. if (it)
  4800. rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it);
  4801. else
  4802. rc = -ENOENT;
  4803. }
  4804. dfs_cache_free_tgts(&tl);
  4805. out:
  4806. kfree(tree);
  4807. return rc;
  4808. }
  4809. #else
  4810. int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
  4811. {
  4812. const struct smb_version_operations *ops = tcon->ses->server->ops;
  4813. return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
  4814. }
  4815. #endif