smb2pdu.c 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. *
  4. * Copyright (C) International Business Machines Corp., 2009, 2013
  5. * Etersoft, 2012
  6. * Author(s): Steve French ([email protected])
  7. * Pavel Shilovsky ([email protected]) 2012
  8. *
  9. * Contains the routines for constructing the SMB2 PDUs themselves
  10. *
  11. */
  12. /* SMB2 PDU handling routines here - except for leftovers (eg session setup) */
  13. /* Note that there are handle based routines which must be */
  14. /* treated slightly differently for reconnection purposes since we never */
  15. /* want to reuse a stale file handle and only the caller knows the file info */
  16. #include <linux/fs.h>
  17. #include <linux/kernel.h>
  18. #include <linux/vfs.h>
  19. #include <linux/task_io_accounting_ops.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/uuid.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/xattr.h>
  24. #include "cifsglob.h"
  25. #include "cifsacl.h"
  26. #include "cifsproto.h"
  27. #include "smb2proto.h"
  28. #include "cifs_unicode.h"
  29. #include "cifs_debug.h"
  30. #include "ntlmssp.h"
  31. #include "smb2status.h"
  32. #include "smb2glob.h"
  33. #include "cifspdu.h"
  34. #include "cifs_spnego.h"
  35. #include "smbdirect.h"
  36. #include "trace.h"
  37. #ifdef CONFIG_CIFS_DFS_UPCALL
  38. #include "dfs_cache.h"
  39. #endif
  40. #include "cached_dir.h"
  41. /*
  42. * The following table defines the expected "StructureSize" of SMB2 requests
  43. * in order by SMB2 command. This is similar to "wct" in SMB/CIFS requests.
  44. *
  45. * Note that commands are defined in smb2pdu.h in le16 but the array below is
  46. * indexed by command in host byte order.
  47. */
  48. static const int smb2_req_struct_sizes[NUMBER_OF_SMB2_COMMANDS] = {
  49. /* SMB2_NEGOTIATE */ 36,
  50. /* SMB2_SESSION_SETUP */ 25,
  51. /* SMB2_LOGOFF */ 4,
  52. /* SMB2_TREE_CONNECT */ 9,
  53. /* SMB2_TREE_DISCONNECT */ 4,
  54. /* SMB2_CREATE */ 57,
  55. /* SMB2_CLOSE */ 24,
  56. /* SMB2_FLUSH */ 24,
  57. /* SMB2_READ */ 49,
  58. /* SMB2_WRITE */ 49,
  59. /* SMB2_LOCK */ 48,
  60. /* SMB2_IOCTL */ 57,
  61. /* SMB2_CANCEL */ 4,
  62. /* SMB2_ECHO */ 4,
  63. /* SMB2_QUERY_DIRECTORY */ 33,
  64. /* SMB2_CHANGE_NOTIFY */ 32,
  65. /* SMB2_QUERY_INFO */ 41,
  66. /* SMB2_SET_INFO */ 33,
  67. /* SMB2_OPLOCK_BREAK */ 24 /* BB this is 36 for LEASE_BREAK variant */
  68. };
  69. int smb3_encryption_required(const struct cifs_tcon *tcon)
  70. {
  71. if (!tcon || !tcon->ses)
  72. return 0;
  73. if ((tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
  74. (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
  75. return 1;
  76. if (tcon->seal &&
  77. (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
  78. return 1;
  79. return 0;
  80. }
  81. static void
  82. smb2_hdr_assemble(struct smb2_hdr *shdr, __le16 smb2_cmd,
  83. const struct cifs_tcon *tcon,
  84. struct TCP_Server_Info *server)
  85. {
  86. struct smb3_hdr_req *smb3_hdr;
  87. shdr->ProtocolId = SMB2_PROTO_NUMBER;
  88. shdr->StructureSize = cpu_to_le16(64);
  89. shdr->Command = smb2_cmd;
  90. if (server->dialect >= SMB30_PROT_ID) {
  91. /* After reconnect SMB3 must set ChannelSequence on subsequent reqs */
  92. smb3_hdr = (struct smb3_hdr_req *)shdr;
  93. /* if primary channel is not set yet, use default channel for chan sequence num */
  94. if (CIFS_SERVER_IS_CHAN(server))
  95. smb3_hdr->ChannelSequence =
  96. cpu_to_le16(server->primary_server->channel_sequence_num);
  97. else
  98. smb3_hdr->ChannelSequence = cpu_to_le16(server->channel_sequence_num);
  99. }
  100. if (server) {
  101. spin_lock(&server->req_lock);
  102. /* Request up to 10 credits but don't go over the limit. */
  103. if (server->credits >= server->max_credits)
  104. shdr->CreditRequest = cpu_to_le16(0);
  105. else
  106. shdr->CreditRequest = cpu_to_le16(
  107. min_t(int, server->max_credits -
  108. server->credits, 10));
  109. spin_unlock(&server->req_lock);
  110. } else {
  111. shdr->CreditRequest = cpu_to_le16(2);
  112. }
  113. shdr->Id.SyncId.ProcessId = cpu_to_le32((__u16)current->tgid);
  114. if (!tcon)
  115. goto out;
  116. /* GLOBAL_CAP_LARGE_MTU will only be set if dialect > SMB2.02 */
  117. /* See sections 2.2.4 and 3.2.4.1.5 of MS-SMB2 */
  118. if (server && (server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
  119. shdr->CreditCharge = cpu_to_le16(1);
  120. /* else CreditCharge MBZ */
  121. shdr->Id.SyncId.TreeId = cpu_to_le32(tcon->tid);
  122. /* Uid is not converted */
  123. if (tcon->ses)
  124. shdr->SessionId = cpu_to_le64(tcon->ses->Suid);
  125. /*
  126. * If we would set SMB2_FLAGS_DFS_OPERATIONS on open we also would have
  127. * to pass the path on the Open SMB prefixed by \\server\share.
  128. * Not sure when we would need to do the augmented path (if ever) and
  129. * setting this flag breaks the SMB2 open operation since it is
  130. * illegal to send an empty path name (without \\server\share prefix)
  131. * when the DFS flag is set in the SMB open header. We could
  132. * consider setting the flag on all operations other than open
  133. * but it is safer to net set it for now.
  134. */
  135. /* if (tcon->share_flags & SHI1005_FLAGS_DFS)
  136. shdr->Flags |= SMB2_FLAGS_DFS_OPERATIONS; */
  137. if (server && server->sign && !smb3_encryption_required(tcon))
  138. shdr->Flags |= SMB2_FLAGS_SIGNED;
  139. out:
  140. return;
  141. }
  142. static int
  143. smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon,
  144. struct TCP_Server_Info *server)
  145. {
  146. int rc = 0;
  147. struct nls_table *nls_codepage = NULL;
  148. struct cifs_ses *ses;
  149. /*
  150. * SMB2s NegProt, SessSetup, Logoff do not have tcon yet so
  151. * check for tcp and smb session status done differently
  152. * for those three - in the calling routine.
  153. */
  154. if (tcon == NULL)
  155. return 0;
  156. /*
  157. * Need to also skip SMB2_IOCTL because it is used for checking nested dfs links in
  158. * cifs_tree_connect().
  159. */
  160. if (smb2_command == SMB2_TREE_CONNECT || smb2_command == SMB2_IOCTL)
  161. return 0;
  162. spin_lock(&tcon->tc_lock);
  163. if (tcon->status == TID_EXITING) {
  164. /*
  165. * only tree disconnect allowed when disconnecting ...
  166. */
  167. if (smb2_command != SMB2_TREE_DISCONNECT) {
  168. spin_unlock(&tcon->tc_lock);
  169. cifs_dbg(FYI, "can not send cmd %d while umounting\n",
  170. smb2_command);
  171. return -ENODEV;
  172. }
  173. }
  174. spin_unlock(&tcon->tc_lock);
  175. ses = tcon->ses;
  176. if (!ses)
  177. return -EIO;
  178. spin_lock(&ses->ses_lock);
  179. if (ses->ses_status == SES_EXITING) {
  180. spin_unlock(&ses->ses_lock);
  181. return -EIO;
  182. }
  183. spin_unlock(&ses->ses_lock);
  184. if (!ses->server || !server)
  185. return -EIO;
  186. spin_lock(&server->srv_lock);
  187. if (server->tcpStatus == CifsNeedReconnect) {
  188. /*
  189. * Return to caller for TREE_DISCONNECT and LOGOFF and CLOSE
  190. * here since they are implicitly done when session drops.
  191. */
  192. switch (smb2_command) {
  193. /*
  194. * BB Should we keep oplock break and add flush to exceptions?
  195. */
  196. case SMB2_TREE_DISCONNECT:
  197. case SMB2_CANCEL:
  198. case SMB2_CLOSE:
  199. case SMB2_OPLOCK_BREAK:
  200. spin_unlock(&server->srv_lock);
  201. return -EAGAIN;
  202. }
  203. }
  204. spin_unlock(&server->srv_lock);
  205. again:
  206. rc = cifs_wait_for_server_reconnect(server, tcon->retry);
  207. if (rc)
  208. return rc;
  209. spin_lock(&ses->chan_lock);
  210. if (!cifs_chan_needs_reconnect(ses, server) && !tcon->need_reconnect) {
  211. spin_unlock(&ses->chan_lock);
  212. return 0;
  213. }
  214. spin_unlock(&ses->chan_lock);
  215. cifs_dbg(FYI, "sess reconnect mask: 0x%lx, tcon reconnect: %d",
  216. tcon->ses->chans_need_reconnect,
  217. tcon->need_reconnect);
  218. mutex_lock(&ses->session_mutex);
  219. /*
  220. * Recheck after acquire mutex. If another thread is negotiating
  221. * and the server never sends an answer the socket will be closed
  222. * and tcpStatus set to reconnect.
  223. */
  224. spin_lock(&server->srv_lock);
  225. if (server->tcpStatus == CifsNeedReconnect) {
  226. spin_unlock(&server->srv_lock);
  227. mutex_unlock(&ses->session_mutex);
  228. if (tcon->retry)
  229. goto again;
  230. rc = -EHOSTDOWN;
  231. goto out;
  232. }
  233. spin_unlock(&server->srv_lock);
  234. nls_codepage = load_nls_default();
  235. /*
  236. * need to prevent multiple threads trying to simultaneously
  237. * reconnect the same SMB session
  238. */
  239. spin_lock(&ses->ses_lock);
  240. spin_lock(&ses->chan_lock);
  241. if (!cifs_chan_needs_reconnect(ses, server) &&
  242. ses->ses_status == SES_GOOD) {
  243. spin_unlock(&ses->chan_lock);
  244. spin_unlock(&ses->ses_lock);
  245. /* this means that we only need to tree connect */
  246. if (tcon->need_reconnect)
  247. goto skip_sess_setup;
  248. mutex_unlock(&ses->session_mutex);
  249. goto out;
  250. }
  251. spin_unlock(&ses->chan_lock);
  252. spin_unlock(&ses->ses_lock);
  253. rc = cifs_negotiate_protocol(0, ses, server);
  254. if (!rc) {
  255. rc = cifs_setup_session(0, ses, server, nls_codepage);
  256. if ((rc == -EACCES) && !tcon->retry) {
  257. mutex_unlock(&ses->session_mutex);
  258. rc = -EHOSTDOWN;
  259. goto failed;
  260. } else if (rc) {
  261. mutex_unlock(&ses->session_mutex);
  262. goto out;
  263. }
  264. } else {
  265. mutex_unlock(&ses->session_mutex);
  266. goto out;
  267. }
  268. skip_sess_setup:
  269. if (!tcon->need_reconnect) {
  270. mutex_unlock(&ses->session_mutex);
  271. goto out;
  272. }
  273. cifs_mark_open_files_invalid(tcon);
  274. if (tcon->use_persistent)
  275. tcon->need_reopen_files = true;
  276. rc = cifs_tree_connect(0, tcon, nls_codepage);
  277. mutex_unlock(&ses->session_mutex);
  278. cifs_dbg(FYI, "reconnect tcon rc = %d\n", rc);
  279. if (rc) {
  280. /* If sess reconnected but tcon didn't, something strange ... */
  281. cifs_dbg(VFS, "reconnect tcon failed rc = %d\n", rc);
  282. goto out;
  283. }
  284. if (smb2_command != SMB2_INTERNAL_CMD)
  285. mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
  286. atomic_inc(&tconInfoReconnectCount);
  287. out:
  288. /*
  289. * Check if handle based operation so we know whether we can continue
  290. * or not without returning to caller to reset file handle.
  291. */
  292. /*
  293. * BB Is flush done by server on drop of tcp session? Should we special
  294. * case it and skip above?
  295. */
  296. switch (smb2_command) {
  297. case SMB2_FLUSH:
  298. case SMB2_READ:
  299. case SMB2_WRITE:
  300. case SMB2_LOCK:
  301. case SMB2_IOCTL:
  302. case SMB2_QUERY_DIRECTORY:
  303. case SMB2_CHANGE_NOTIFY:
  304. case SMB2_QUERY_INFO:
  305. case SMB2_SET_INFO:
  306. rc = -EAGAIN;
  307. }
  308. failed:
  309. unload_nls(nls_codepage);
  310. return rc;
  311. }
  312. static void
  313. fill_small_buf(__le16 smb2_command, struct cifs_tcon *tcon,
  314. struct TCP_Server_Info *server,
  315. void *buf,
  316. unsigned int *total_len)
  317. {
  318. struct smb2_pdu *spdu = buf;
  319. /* lookup word count ie StructureSize from table */
  320. __u16 parmsize = smb2_req_struct_sizes[le16_to_cpu(smb2_command)];
  321. /*
  322. * smaller than SMALL_BUFFER_SIZE but bigger than fixed area of
  323. * largest operations (Create)
  324. */
  325. memset(buf, 0, 256);
  326. smb2_hdr_assemble(&spdu->hdr, smb2_command, tcon, server);
  327. spdu->StructureSize2 = cpu_to_le16(parmsize);
  328. *total_len = parmsize + sizeof(struct smb2_hdr);
  329. }
  330. /*
  331. * Allocate and return pointer to an SMB request hdr, and set basic
  332. * SMB information in the SMB header. If the return code is zero, this
  333. * function must have filled in request_buf pointer.
  334. */
  335. static int __smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
  336. struct TCP_Server_Info *server,
  337. void **request_buf, unsigned int *total_len)
  338. {
  339. /* BB eventually switch this to SMB2 specific small buf size */
  340. if (smb2_command == SMB2_SET_INFO)
  341. *request_buf = cifs_buf_get();
  342. else
  343. *request_buf = cifs_small_buf_get();
  344. if (*request_buf == NULL) {
  345. /* BB should we add a retry in here if not a writepage? */
  346. return -ENOMEM;
  347. }
  348. fill_small_buf(smb2_command, tcon, server,
  349. (struct smb2_hdr *)(*request_buf),
  350. total_len);
  351. if (tcon != NULL) {
  352. uint16_t com_code = le16_to_cpu(smb2_command);
  353. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_sent[com_code]);
  354. cifs_stats_inc(&tcon->num_smbs_sent);
  355. }
  356. return 0;
  357. }
  358. static int smb2_plain_req_init(__le16 smb2_command, struct cifs_tcon *tcon,
  359. struct TCP_Server_Info *server,
  360. void **request_buf, unsigned int *total_len)
  361. {
  362. int rc;
  363. rc = smb2_reconnect(smb2_command, tcon, server);
  364. if (rc)
  365. return rc;
  366. return __smb2_plain_req_init(smb2_command, tcon, server, request_buf,
  367. total_len);
  368. }
  369. static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,
  370. struct TCP_Server_Info *server,
  371. void **request_buf, unsigned int *total_len)
  372. {
  373. /* Skip reconnect only for FSCTL_VALIDATE_NEGOTIATE_INFO IOCTLs */
  374. if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO) {
  375. return __smb2_plain_req_init(SMB2_IOCTL, tcon, server,
  376. request_buf, total_len);
  377. }
  378. return smb2_plain_req_init(SMB2_IOCTL, tcon, server,
  379. request_buf, total_len);
  380. }
  381. /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */
  382. static void
  383. build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)
  384. {
  385. pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
  386. pneg_ctxt->DataLength = cpu_to_le16(38);
  387. pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
  388. pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
  389. get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
  390. pneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;
  391. }
  392. static void
  393. build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)
  394. {
  395. pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
  396. pneg_ctxt->DataLength =
  397. cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
  398. - sizeof(struct smb2_neg_context));
  399. pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(3);
  400. pneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;
  401. pneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;
  402. pneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;
  403. }
  404. static unsigned int
  405. build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)
  406. {
  407. unsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);
  408. unsigned short num_algs = 1; /* number of signing algorithms sent */
  409. pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
  410. /*
  411. * Context Data length must be rounded to multiple of 8 for some servers
  412. */
  413. pneg_ctxt->DataLength = cpu_to_le16(ALIGN(sizeof(struct smb2_signing_capabilities) -
  414. sizeof(struct smb2_neg_context) +
  415. (num_algs * sizeof(u16)), 8));
  416. pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);
  417. pneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);
  418. ctxt_len += sizeof(__le16) * num_algs;
  419. ctxt_len = ALIGN(ctxt_len, 8);
  420. return ctxt_len;
  421. /* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */
  422. }
  423. static void
  424. build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)
  425. {
  426. pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
  427. if (require_gcm_256) {
  428. pneg_ctxt->DataLength = cpu_to_le16(4); /* Cipher Count + 1 cipher */
  429. pneg_ctxt->CipherCount = cpu_to_le16(1);
  430. pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES256_GCM;
  431. } else if (enable_gcm_256) {
  432. pneg_ctxt->DataLength = cpu_to_le16(8); /* Cipher Count + 3 ciphers */
  433. pneg_ctxt->CipherCount = cpu_to_le16(3);
  434. pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
  435. pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES256_GCM;
  436. pneg_ctxt->Ciphers[2] = SMB2_ENCRYPTION_AES128_CCM;
  437. } else {
  438. pneg_ctxt->DataLength = cpu_to_le16(6); /* Cipher Count + 2 ciphers */
  439. pneg_ctxt->CipherCount = cpu_to_le16(2);
  440. pneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;
  441. pneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;
  442. }
  443. }
  444. static unsigned int
  445. build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)
  446. {
  447. struct nls_table *cp = load_nls_default();
  448. pneg_ctxt->ContextType = SMB2_NETNAME_NEGOTIATE_CONTEXT_ID;
  449. /* copy up to max of first 100 bytes of server name to NetName field */
  450. pneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));
  451. /* context size is DataLength + minimal smb2_neg_context */
  452. return ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);
  453. }
  454. static void
  455. build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
  456. {
  457. pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
  458. pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
  459. /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
  460. pneg_ctxt->Name[0] = 0x93;
  461. pneg_ctxt->Name[1] = 0xAD;
  462. pneg_ctxt->Name[2] = 0x25;
  463. pneg_ctxt->Name[3] = 0x50;
  464. pneg_ctxt->Name[4] = 0x9C;
  465. pneg_ctxt->Name[5] = 0xB4;
  466. pneg_ctxt->Name[6] = 0x11;
  467. pneg_ctxt->Name[7] = 0xE7;
  468. pneg_ctxt->Name[8] = 0xB4;
  469. pneg_ctxt->Name[9] = 0x23;
  470. pneg_ctxt->Name[10] = 0x83;
  471. pneg_ctxt->Name[11] = 0xDE;
  472. pneg_ctxt->Name[12] = 0x96;
  473. pneg_ctxt->Name[13] = 0x8B;
  474. pneg_ctxt->Name[14] = 0xCD;
  475. pneg_ctxt->Name[15] = 0x7C;
  476. }
  477. static void
  478. assemble_neg_contexts(struct smb2_negotiate_req *req,
  479. struct TCP_Server_Info *server, unsigned int *total_len)
  480. {
  481. unsigned int ctxt_len, neg_context_count;
  482. struct TCP_Server_Info *pserver;
  483. char *pneg_ctxt;
  484. char *hostname;
  485. if (*total_len > 200) {
  486. /* In case length corrupted don't want to overrun smb buffer */
  487. cifs_server_dbg(VFS, "Bad frame length assembling neg contexts\n");
  488. return;
  489. }
  490. /*
  491. * round up total_len of fixed part of SMB3 negotiate request to 8
  492. * byte boundary before adding negotiate contexts
  493. */
  494. *total_len = ALIGN(*total_len, 8);
  495. pneg_ctxt = (*total_len) + (char *)req;
  496. req->NegotiateContextOffset = cpu_to_le32(*total_len);
  497. build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);
  498. ctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);
  499. *total_len += ctxt_len;
  500. pneg_ctxt += ctxt_len;
  501. build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);
  502. ctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);
  503. *total_len += ctxt_len;
  504. pneg_ctxt += ctxt_len;
  505. /*
  506. * secondary channels don't have the hostname field populated
  507. * use the hostname field in the primary channel instead
  508. */
  509. pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
  510. cifs_server_lock(pserver);
  511. hostname = pserver->hostname;
  512. if (hostname && (hostname[0] != 0)) {
  513. ctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,
  514. hostname);
  515. *total_len += ctxt_len;
  516. pneg_ctxt += ctxt_len;
  517. neg_context_count = 3;
  518. } else
  519. neg_context_count = 2;
  520. cifs_server_unlock(pserver);
  521. build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
  522. *total_len += sizeof(struct smb2_posix_neg_context);
  523. pneg_ctxt += sizeof(struct smb2_posix_neg_context);
  524. neg_context_count++;
  525. if (server->compress_algorithm) {
  526. build_compression_ctxt((struct smb2_compression_capabilities_context *)
  527. pneg_ctxt);
  528. ctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);
  529. *total_len += ctxt_len;
  530. pneg_ctxt += ctxt_len;
  531. neg_context_count++;
  532. }
  533. if (enable_negotiate_signing) {
  534. ctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)
  535. pneg_ctxt);
  536. *total_len += ctxt_len;
  537. pneg_ctxt += ctxt_len;
  538. neg_context_count++;
  539. }
  540. /* check for and add transport_capabilities and signing capabilities */
  541. req->NegotiateContextCount = cpu_to_le16(neg_context_count);
  542. }
  543. /* If invalid preauth context warn but use what we requested, SHA-512 */
  544. static void decode_preauth_context(struct smb2_preauth_neg_context *ctxt)
  545. {
  546. unsigned int len = le16_to_cpu(ctxt->DataLength);
  547. /*
  548. * Caller checked that DataLength remains within SMB boundary. We still
  549. * need to confirm that one HashAlgorithms member is accounted for.
  550. */
  551. if (len < MIN_PREAUTH_CTXT_DATA_LEN) {
  552. pr_warn_once("server sent bad preauth context\n");
  553. return;
  554. } else if (len < MIN_PREAUTH_CTXT_DATA_LEN + le16_to_cpu(ctxt->SaltLength)) {
  555. pr_warn_once("server sent invalid SaltLength\n");
  556. return;
  557. }
  558. if (le16_to_cpu(ctxt->HashAlgorithmCount) != 1)
  559. pr_warn_once("Invalid SMB3 hash algorithm count\n");
  560. if (ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512)
  561. pr_warn_once("unknown SMB3 hash algorithm\n");
  562. }
  563. static void decode_compress_ctx(struct TCP_Server_Info *server,
  564. struct smb2_compression_capabilities_context *ctxt)
  565. {
  566. unsigned int len = le16_to_cpu(ctxt->DataLength);
  567. /*
  568. * Caller checked that DataLength remains within SMB boundary. We still
  569. * need to confirm that one CompressionAlgorithms member is accounted
  570. * for.
  571. */
  572. if (len < 10) {
  573. pr_warn_once("server sent bad compression cntxt\n");
  574. return;
  575. }
  576. if (le16_to_cpu(ctxt->CompressionAlgorithmCount) != 1) {
  577. pr_warn_once("Invalid SMB3 compress algorithm count\n");
  578. return;
  579. }
  580. if (le16_to_cpu(ctxt->CompressionAlgorithms[0]) > 3) {
  581. pr_warn_once("unknown compression algorithm\n");
  582. return;
  583. }
  584. server->compress_algorithm = ctxt->CompressionAlgorithms[0];
  585. }
  586. static int decode_encrypt_ctx(struct TCP_Server_Info *server,
  587. struct smb2_encryption_neg_context *ctxt)
  588. {
  589. unsigned int len = le16_to_cpu(ctxt->DataLength);
  590. cifs_dbg(FYI, "decode SMB3.11 encryption neg context of len %d\n", len);
  591. /*
  592. * Caller checked that DataLength remains within SMB boundary. We still
  593. * need to confirm that one Cipher flexible array member is accounted
  594. * for.
  595. */
  596. if (len < MIN_ENCRYPT_CTXT_DATA_LEN) {
  597. pr_warn_once("server sent bad crypto ctxt len\n");
  598. return -EINVAL;
  599. }
  600. if (le16_to_cpu(ctxt->CipherCount) != 1) {
  601. pr_warn_once("Invalid SMB3.11 cipher count\n");
  602. return -EINVAL;
  603. }
  604. cifs_dbg(FYI, "SMB311 cipher type:%d\n", le16_to_cpu(ctxt->Ciphers[0]));
  605. if (require_gcm_256) {
  606. if (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM) {
  607. cifs_dbg(VFS, "Server does not support requested encryption type (AES256 GCM)\n");
  608. return -EOPNOTSUPP;
  609. }
  610. } else if (ctxt->Ciphers[0] == 0) {
  611. /*
  612. * e.g. if server only supported AES256_CCM (very unlikely)
  613. * or server supported no encryption types or had all disabled.
  614. * Since GLOBAL_CAP_ENCRYPTION will be not set, in the case
  615. * in which mount requested encryption ("seal") checks later
  616. * on during tree connection will return proper rc, but if
  617. * seal not requested by client, since server is allowed to
  618. * return 0 to indicate no supported cipher, we can't fail here
  619. */
  620. server->cipher_type = 0;
  621. server->capabilities &= ~SMB2_GLOBAL_CAP_ENCRYPTION;
  622. pr_warn_once("Server does not support requested encryption types\n");
  623. return 0;
  624. } else if ((ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_CCM) &&
  625. (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES128_GCM) &&
  626. (ctxt->Ciphers[0] != SMB2_ENCRYPTION_AES256_GCM)) {
  627. /* server returned a cipher we didn't ask for */
  628. pr_warn_once("Invalid SMB3.11 cipher returned\n");
  629. return -EINVAL;
  630. }
  631. server->cipher_type = ctxt->Ciphers[0];
  632. server->capabilities |= SMB2_GLOBAL_CAP_ENCRYPTION;
  633. return 0;
  634. }
  635. static void decode_signing_ctx(struct TCP_Server_Info *server,
  636. struct smb2_signing_capabilities *pctxt)
  637. {
  638. unsigned int len = le16_to_cpu(pctxt->DataLength);
  639. /*
  640. * Caller checked that DataLength remains within SMB boundary. We still
  641. * need to confirm that one SigningAlgorithms flexible array member is
  642. * accounted for.
  643. */
  644. if ((len < 4) || (len > 16)) {
  645. pr_warn_once("server sent bad signing negcontext\n");
  646. return;
  647. }
  648. if (le16_to_cpu(pctxt->SigningAlgorithmCount) != 1) {
  649. pr_warn_once("Invalid signing algorithm count\n");
  650. return;
  651. }
  652. if (le16_to_cpu(pctxt->SigningAlgorithms[0]) > 2) {
  653. pr_warn_once("unknown signing algorithm\n");
  654. return;
  655. }
  656. server->signing_negotiated = true;
  657. server->signing_algorithm = le16_to_cpu(pctxt->SigningAlgorithms[0]);
  658. cifs_dbg(FYI, "signing algorithm %d chosen\n",
  659. server->signing_algorithm);
  660. }
  661. static int smb311_decode_neg_context(struct smb2_negotiate_rsp *rsp,
  662. struct TCP_Server_Info *server,
  663. unsigned int len_of_smb)
  664. {
  665. struct smb2_neg_context *pctx;
  666. unsigned int offset = le32_to_cpu(rsp->NegotiateContextOffset);
  667. unsigned int ctxt_cnt = le16_to_cpu(rsp->NegotiateContextCount);
  668. unsigned int len_of_ctxts, i;
  669. int rc = 0;
  670. cifs_dbg(FYI, "decoding %d negotiate contexts\n", ctxt_cnt);
  671. if (len_of_smb <= offset) {
  672. cifs_server_dbg(VFS, "Invalid response: negotiate context offset\n");
  673. return -EINVAL;
  674. }
  675. len_of_ctxts = len_of_smb - offset;
  676. for (i = 0; i < ctxt_cnt; i++) {
  677. int clen;
  678. /* check that offset is not beyond end of SMB */
  679. if (len_of_ctxts < sizeof(struct smb2_neg_context))
  680. break;
  681. pctx = (struct smb2_neg_context *)(offset + (char *)rsp);
  682. clen = sizeof(struct smb2_neg_context)
  683. + le16_to_cpu(pctx->DataLength);
  684. /*
  685. * 2.2.4 SMB2 NEGOTIATE Response
  686. * Subsequent negotiate contexts MUST appear at the first 8-byte
  687. * aligned offset following the previous negotiate context.
  688. */
  689. if (i + 1 != ctxt_cnt)
  690. clen = ALIGN(clen, 8);
  691. if (clen > len_of_ctxts)
  692. break;
  693. if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES)
  694. decode_preauth_context(
  695. (struct smb2_preauth_neg_context *)pctx);
  696. else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES)
  697. rc = decode_encrypt_ctx(server,
  698. (struct smb2_encryption_neg_context *)pctx);
  699. else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES)
  700. decode_compress_ctx(server,
  701. (struct smb2_compression_capabilities_context *)pctx);
  702. else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE)
  703. server->posix_ext_supported = true;
  704. else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES)
  705. decode_signing_ctx(server,
  706. (struct smb2_signing_capabilities *)pctx);
  707. else
  708. cifs_server_dbg(VFS, "unknown negcontext of type %d ignored\n",
  709. le16_to_cpu(pctx->ContextType));
  710. if (rc)
  711. break;
  712. offset += clen;
  713. len_of_ctxts -= clen;
  714. }
  715. return rc;
  716. }
  717. static struct create_posix *
  718. create_posix_buf(umode_t mode)
  719. {
  720. struct create_posix *buf;
  721. buf = kzalloc(sizeof(struct create_posix),
  722. GFP_KERNEL);
  723. if (!buf)
  724. return NULL;
  725. buf->ccontext.DataOffset =
  726. cpu_to_le16(offsetof(struct create_posix, Mode));
  727. buf->ccontext.DataLength = cpu_to_le32(4);
  728. buf->ccontext.NameOffset =
  729. cpu_to_le16(offsetof(struct create_posix, Name));
  730. buf->ccontext.NameLength = cpu_to_le16(16);
  731. /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
  732. buf->Name[0] = 0x93;
  733. buf->Name[1] = 0xAD;
  734. buf->Name[2] = 0x25;
  735. buf->Name[3] = 0x50;
  736. buf->Name[4] = 0x9C;
  737. buf->Name[5] = 0xB4;
  738. buf->Name[6] = 0x11;
  739. buf->Name[7] = 0xE7;
  740. buf->Name[8] = 0xB4;
  741. buf->Name[9] = 0x23;
  742. buf->Name[10] = 0x83;
  743. buf->Name[11] = 0xDE;
  744. buf->Name[12] = 0x96;
  745. buf->Name[13] = 0x8B;
  746. buf->Name[14] = 0xCD;
  747. buf->Name[15] = 0x7C;
  748. buf->Mode = cpu_to_le32(mode);
  749. cifs_dbg(FYI, "mode on posix create 0%o\n", mode);
  750. return buf;
  751. }
  752. static int
  753. add_posix_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode)
  754. {
  755. struct smb2_create_req *req = iov[0].iov_base;
  756. unsigned int num = *num_iovec;
  757. iov[num].iov_base = create_posix_buf(mode);
  758. if (mode == ACL_NO_MODE)
  759. cifs_dbg(FYI, "Invalid mode\n");
  760. if (iov[num].iov_base == NULL)
  761. return -ENOMEM;
  762. iov[num].iov_len = sizeof(struct create_posix);
  763. if (!req->CreateContextsOffset)
  764. req->CreateContextsOffset = cpu_to_le32(
  765. sizeof(struct smb2_create_req) +
  766. iov[num - 1].iov_len);
  767. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_posix));
  768. *num_iovec = num + 1;
  769. return 0;
  770. }
  771. /*
  772. *
  773. * SMB2 Worker functions follow:
  774. *
  775. * The general structure of the worker functions is:
  776. * 1) Call smb2_init (assembles SMB2 header)
  777. * 2) Initialize SMB2 command specific fields in fixed length area of SMB
  778. * 3) Call smb_sendrcv2 (sends request on socket and waits for response)
  779. * 4) Decode SMB2 command specific fields in the fixed length area
  780. * 5) Decode variable length data area (if any for this SMB2 command type)
  781. * 6) Call free smb buffer
  782. * 7) return
  783. *
  784. */
  785. int
  786. SMB2_negotiate(const unsigned int xid,
  787. struct cifs_ses *ses,
  788. struct TCP_Server_Info *server)
  789. {
  790. struct smb_rqst rqst;
  791. struct smb2_negotiate_req *req;
  792. struct smb2_negotiate_rsp *rsp;
  793. struct kvec iov[1];
  794. struct kvec rsp_iov;
  795. int rc;
  796. int resp_buftype;
  797. int blob_offset, blob_length;
  798. char *security_blob;
  799. int flags = CIFS_NEG_OP;
  800. unsigned int total_len;
  801. cifs_dbg(FYI, "Negotiate protocol\n");
  802. if (!server) {
  803. WARN(1, "%s: server is NULL!\n", __func__);
  804. return -EIO;
  805. }
  806. rc = smb2_plain_req_init(SMB2_NEGOTIATE, NULL, server,
  807. (void **) &req, &total_len);
  808. if (rc)
  809. return rc;
  810. req->hdr.SessionId = 0;
  811. memset(server->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
  812. memset(ses->preauth_sha_hash, 0, SMB2_PREAUTH_HASH_SIZE);
  813. if (strcmp(server->vals->version_string,
  814. SMB3ANY_VERSION_STRING) == 0) {
  815. req->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
  816. req->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
  817. req->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
  818. req->DialectCount = cpu_to_le16(3);
  819. total_len += 6;
  820. } else if (strcmp(server->vals->version_string,
  821. SMBDEFAULT_VERSION_STRING) == 0) {
  822. req->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
  823. req->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
  824. req->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
  825. req->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
  826. req->DialectCount = cpu_to_le16(4);
  827. total_len += 8;
  828. } else {
  829. /* otherwise send specific dialect */
  830. req->Dialects[0] = cpu_to_le16(server->vals->protocol_id);
  831. req->DialectCount = cpu_to_le16(1);
  832. total_len += 2;
  833. }
  834. /* only one of SMB2 signing flags may be set in SMB2 request */
  835. if (ses->sign)
  836. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
  837. else if (global_secflags & CIFSSEC_MAY_SIGN)
  838. req->SecurityMode = cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
  839. else
  840. req->SecurityMode = 0;
  841. req->Capabilities = cpu_to_le32(server->vals->req_capabilities);
  842. if (ses->chan_max > 1)
  843. req->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
  844. /* ClientGUID must be zero for SMB2.02 dialect */
  845. if (server->vals->protocol_id == SMB20_PROT_ID)
  846. memset(req->ClientGUID, 0, SMB2_CLIENT_GUID_SIZE);
  847. else {
  848. memcpy(req->ClientGUID, server->client_guid,
  849. SMB2_CLIENT_GUID_SIZE);
  850. if ((server->vals->protocol_id == SMB311_PROT_ID) ||
  851. (strcmp(server->vals->version_string,
  852. SMB3ANY_VERSION_STRING) == 0) ||
  853. (strcmp(server->vals->version_string,
  854. SMBDEFAULT_VERSION_STRING) == 0))
  855. assemble_neg_contexts(req, server, &total_len);
  856. }
  857. iov[0].iov_base = (char *)req;
  858. iov[0].iov_len = total_len;
  859. memset(&rqst, 0, sizeof(struct smb_rqst));
  860. rqst.rq_iov = iov;
  861. rqst.rq_nvec = 1;
  862. rc = cifs_send_recv(xid, ses, server,
  863. &rqst, &resp_buftype, flags, &rsp_iov);
  864. cifs_small_buf_release(req);
  865. rsp = (struct smb2_negotiate_rsp *)rsp_iov.iov_base;
  866. /*
  867. * No tcon so can't do
  868. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  869. */
  870. if (rc == -EOPNOTSUPP) {
  871. cifs_server_dbg(VFS, "Dialect not supported by server. Consider specifying vers=1.0 or vers=2.0 on mount for accessing older servers\n");
  872. goto neg_exit;
  873. } else if (rc != 0)
  874. goto neg_exit;
  875. rc = -EIO;
  876. if (strcmp(server->vals->version_string,
  877. SMB3ANY_VERSION_STRING) == 0) {
  878. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
  879. cifs_server_dbg(VFS,
  880. "SMB2 dialect returned but not requested\n");
  881. goto neg_exit;
  882. } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
  883. cifs_server_dbg(VFS,
  884. "SMB2.1 dialect returned but not requested\n");
  885. goto neg_exit;
  886. } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
  887. /* ops set to 3.0 by default for default so update */
  888. server->ops = &smb311_operations;
  889. server->vals = &smb311_values;
  890. }
  891. } else if (strcmp(server->vals->version_string,
  892. SMBDEFAULT_VERSION_STRING) == 0) {
  893. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID)) {
  894. cifs_server_dbg(VFS,
  895. "SMB2 dialect returned but not requested\n");
  896. goto neg_exit;
  897. } else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID)) {
  898. /* ops set to 3.0 by default for default so update */
  899. server->ops = &smb21_operations;
  900. server->vals = &smb21_values;
  901. } else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
  902. server->ops = &smb311_operations;
  903. server->vals = &smb311_values;
  904. }
  905. } else if (le16_to_cpu(rsp->DialectRevision) !=
  906. server->vals->protocol_id) {
  907. /* if requested single dialect ensure returned dialect matched */
  908. cifs_server_dbg(VFS, "Invalid 0x%x dialect returned: not requested\n",
  909. le16_to_cpu(rsp->DialectRevision));
  910. goto neg_exit;
  911. }
  912. cifs_dbg(FYI, "mode 0x%x\n", rsp->SecurityMode);
  913. if (rsp->DialectRevision == cpu_to_le16(SMB20_PROT_ID))
  914. cifs_dbg(FYI, "negotiated smb2.0 dialect\n");
  915. else if (rsp->DialectRevision == cpu_to_le16(SMB21_PROT_ID))
  916. cifs_dbg(FYI, "negotiated smb2.1 dialect\n");
  917. else if (rsp->DialectRevision == cpu_to_le16(SMB30_PROT_ID))
  918. cifs_dbg(FYI, "negotiated smb3.0 dialect\n");
  919. else if (rsp->DialectRevision == cpu_to_le16(SMB302_PROT_ID))
  920. cifs_dbg(FYI, "negotiated smb3.02 dialect\n");
  921. else if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID))
  922. cifs_dbg(FYI, "negotiated smb3.1.1 dialect\n");
  923. else {
  924. cifs_server_dbg(VFS, "Invalid dialect returned by server 0x%x\n",
  925. le16_to_cpu(rsp->DialectRevision));
  926. goto neg_exit;
  927. }
  928. rc = 0;
  929. server->dialect = le16_to_cpu(rsp->DialectRevision);
  930. /*
  931. * Keep a copy of the hash after negprot. This hash will be
  932. * the starting hash value for all sessions made from this
  933. * server.
  934. */
  935. memcpy(server->preauth_sha_hash, ses->preauth_sha_hash,
  936. SMB2_PREAUTH_HASH_SIZE);
  937. /* SMB2 only has an extended negflavor */
  938. server->negflavor = CIFS_NEGFLAVOR_EXTENDED;
  939. /* set it to the maximum buffer size value we can send with 1 credit */
  940. server->maxBuf = min_t(unsigned int, le32_to_cpu(rsp->MaxTransactSize),
  941. SMB2_MAX_BUFFER_SIZE);
  942. server->max_read = le32_to_cpu(rsp->MaxReadSize);
  943. server->max_write = le32_to_cpu(rsp->MaxWriteSize);
  944. server->sec_mode = le16_to_cpu(rsp->SecurityMode);
  945. if ((server->sec_mode & SMB2_SEC_MODE_FLAGS_ALL) != server->sec_mode)
  946. cifs_dbg(FYI, "Server returned unexpected security mode 0x%x\n",
  947. server->sec_mode);
  948. server->capabilities = le32_to_cpu(rsp->Capabilities);
  949. /* Internal types */
  950. server->capabilities |= SMB2_NT_FIND | SMB2_LARGE_FILES;
  951. /*
  952. * SMB3.0 supports only 1 cipher and doesn't have a encryption neg context
  953. * Set the cipher type manually.
  954. */
  955. if (server->dialect == SMB30_PROT_ID && (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
  956. server->cipher_type = SMB2_ENCRYPTION_AES128_CCM;
  957. security_blob = smb2_get_data_area_len(&blob_offset, &blob_length,
  958. (struct smb2_hdr *)rsp);
  959. /*
  960. * See MS-SMB2 section 2.2.4: if no blob, client picks default which
  961. * for us will be
  962. * ses->sectype = RawNTLMSSP;
  963. * but for time being this is our only auth choice so doesn't matter.
  964. * We just found a server which sets blob length to zero expecting raw.
  965. */
  966. if (blob_length == 0) {
  967. cifs_dbg(FYI, "missing security blob on negprot\n");
  968. server->sec_ntlmssp = true;
  969. }
  970. rc = cifs_enable_signing(server, ses->sign);
  971. if (rc)
  972. goto neg_exit;
  973. if (blob_length) {
  974. rc = decode_negTokenInit(security_blob, blob_length, server);
  975. if (rc == 1)
  976. rc = 0;
  977. else if (rc == 0)
  978. rc = -EIO;
  979. }
  980. if (rsp->DialectRevision == cpu_to_le16(SMB311_PROT_ID)) {
  981. if (rsp->NegotiateContextCount)
  982. rc = smb311_decode_neg_context(rsp, server,
  983. rsp_iov.iov_len);
  984. else
  985. cifs_server_dbg(VFS, "Missing expected negotiate contexts\n");
  986. }
  987. neg_exit:
  988. free_rsp_buf(resp_buftype, rsp);
  989. return rc;
  990. }
  991. int smb3_validate_negotiate(const unsigned int xid, struct cifs_tcon *tcon)
  992. {
  993. int rc;
  994. struct validate_negotiate_info_req *pneg_inbuf;
  995. struct validate_negotiate_info_rsp *pneg_rsp = NULL;
  996. u32 rsplen;
  997. u32 inbuflen; /* max of 4 dialects */
  998. struct TCP_Server_Info *server = tcon->ses->server;
  999. cifs_dbg(FYI, "validate negotiate\n");
  1000. /* In SMB3.11 preauth integrity supersedes validate negotiate */
  1001. if (server->dialect == SMB311_PROT_ID)
  1002. return 0;
  1003. /*
  1004. * validation ioctl must be signed, so no point sending this if we
  1005. * can not sign it (ie are not known user). Even if signing is not
  1006. * required (enabled but not negotiated), in those cases we selectively
  1007. * sign just this, the first and only signed request on a connection.
  1008. * Having validation of negotiate info helps reduce attack vectors.
  1009. */
  1010. if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
  1011. return 0; /* validation requires signing */
  1012. if (tcon->ses->user_name == NULL) {
  1013. cifs_dbg(FYI, "Can't validate negotiate: null user mount\n");
  1014. return 0; /* validation requires signing */
  1015. }
  1016. if (tcon->ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
  1017. cifs_tcon_dbg(VFS, "Unexpected null user (anonymous) auth flag sent by server\n");
  1018. pneg_inbuf = kmalloc(sizeof(*pneg_inbuf), GFP_NOFS);
  1019. if (!pneg_inbuf)
  1020. return -ENOMEM;
  1021. pneg_inbuf->Capabilities =
  1022. cpu_to_le32(server->vals->req_capabilities);
  1023. if (tcon->ses->chan_max > 1)
  1024. pneg_inbuf->Capabilities |= cpu_to_le32(SMB2_GLOBAL_CAP_MULTI_CHANNEL);
  1025. memcpy(pneg_inbuf->Guid, server->client_guid,
  1026. SMB2_CLIENT_GUID_SIZE);
  1027. if (tcon->ses->sign)
  1028. pneg_inbuf->SecurityMode =
  1029. cpu_to_le16(SMB2_NEGOTIATE_SIGNING_REQUIRED);
  1030. else if (global_secflags & CIFSSEC_MAY_SIGN)
  1031. pneg_inbuf->SecurityMode =
  1032. cpu_to_le16(SMB2_NEGOTIATE_SIGNING_ENABLED);
  1033. else
  1034. pneg_inbuf->SecurityMode = 0;
  1035. if (strcmp(server->vals->version_string,
  1036. SMB3ANY_VERSION_STRING) == 0) {
  1037. pneg_inbuf->Dialects[0] = cpu_to_le16(SMB30_PROT_ID);
  1038. pneg_inbuf->Dialects[1] = cpu_to_le16(SMB302_PROT_ID);
  1039. pneg_inbuf->Dialects[2] = cpu_to_le16(SMB311_PROT_ID);
  1040. pneg_inbuf->DialectCount = cpu_to_le16(3);
  1041. /* SMB 2.1 not included so subtract one dialect from len */
  1042. inbuflen = sizeof(*pneg_inbuf) -
  1043. (sizeof(pneg_inbuf->Dialects[0]));
  1044. } else if (strcmp(server->vals->version_string,
  1045. SMBDEFAULT_VERSION_STRING) == 0) {
  1046. pneg_inbuf->Dialects[0] = cpu_to_le16(SMB21_PROT_ID);
  1047. pneg_inbuf->Dialects[1] = cpu_to_le16(SMB30_PROT_ID);
  1048. pneg_inbuf->Dialects[2] = cpu_to_le16(SMB302_PROT_ID);
  1049. pneg_inbuf->Dialects[3] = cpu_to_le16(SMB311_PROT_ID);
  1050. pneg_inbuf->DialectCount = cpu_to_le16(4);
  1051. /* structure is big enough for 4 dialects */
  1052. inbuflen = sizeof(*pneg_inbuf);
  1053. } else {
  1054. /* otherwise specific dialect was requested */
  1055. pneg_inbuf->Dialects[0] =
  1056. cpu_to_le16(server->vals->protocol_id);
  1057. pneg_inbuf->DialectCount = cpu_to_le16(1);
  1058. /* structure is big enough for 4 dialects, sending only 1 */
  1059. inbuflen = sizeof(*pneg_inbuf) -
  1060. sizeof(pneg_inbuf->Dialects[0]) * 3;
  1061. }
  1062. rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
  1063. FSCTL_VALIDATE_NEGOTIATE_INFO,
  1064. (char *)pneg_inbuf, inbuflen, CIFSMaxBufSize,
  1065. (char **)&pneg_rsp, &rsplen);
  1066. if (rc == -EOPNOTSUPP) {
  1067. /*
  1068. * Old Windows versions or Netapp SMB server can return
  1069. * not supported error. Client should accept it.
  1070. */
  1071. cifs_tcon_dbg(VFS, "Server does not support validate negotiate\n");
  1072. rc = 0;
  1073. goto out_free_inbuf;
  1074. } else if (rc != 0) {
  1075. cifs_tcon_dbg(VFS, "validate protocol negotiate failed: %d\n",
  1076. rc);
  1077. rc = -EIO;
  1078. goto out_free_inbuf;
  1079. }
  1080. rc = -EIO;
  1081. if (rsplen != sizeof(*pneg_rsp)) {
  1082. cifs_tcon_dbg(VFS, "Invalid protocol negotiate response size: %d\n",
  1083. rsplen);
  1084. /* relax check since Mac returns max bufsize allowed on ioctl */
  1085. if (rsplen > CIFSMaxBufSize || rsplen < sizeof(*pneg_rsp))
  1086. goto out_free_rsp;
  1087. }
  1088. /* check validate negotiate info response matches what we got earlier */
  1089. if (pneg_rsp->Dialect != cpu_to_le16(server->dialect))
  1090. goto vneg_out;
  1091. if (pneg_rsp->SecurityMode != cpu_to_le16(server->sec_mode))
  1092. goto vneg_out;
  1093. /* do not validate server guid because not saved at negprot time yet */
  1094. if ((le32_to_cpu(pneg_rsp->Capabilities) | SMB2_NT_FIND |
  1095. SMB2_LARGE_FILES) != server->capabilities)
  1096. goto vneg_out;
  1097. /* validate negotiate successful */
  1098. rc = 0;
  1099. cifs_dbg(FYI, "validate negotiate info successful\n");
  1100. goto out_free_rsp;
  1101. vneg_out:
  1102. cifs_tcon_dbg(VFS, "protocol revalidation - security settings mismatch\n");
  1103. out_free_rsp:
  1104. kfree(pneg_rsp);
  1105. out_free_inbuf:
  1106. kfree(pneg_inbuf);
  1107. return rc;
  1108. }
  1109. enum securityEnum
  1110. smb2_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested)
  1111. {
  1112. switch (requested) {
  1113. case Kerberos:
  1114. case RawNTLMSSP:
  1115. return requested;
  1116. case NTLMv2:
  1117. return RawNTLMSSP;
  1118. case Unspecified:
  1119. if (server->sec_ntlmssp &&
  1120. (global_secflags & CIFSSEC_MAY_NTLMSSP))
  1121. return RawNTLMSSP;
  1122. if ((server->sec_kerberos || server->sec_mskerberos) &&
  1123. (global_secflags & CIFSSEC_MAY_KRB5))
  1124. return Kerberos;
  1125. fallthrough;
  1126. default:
  1127. return Unspecified;
  1128. }
  1129. }
  1130. struct SMB2_sess_data {
  1131. unsigned int xid;
  1132. struct cifs_ses *ses;
  1133. struct TCP_Server_Info *server;
  1134. struct nls_table *nls_cp;
  1135. void (*func)(struct SMB2_sess_data *);
  1136. int result;
  1137. u64 previous_session;
  1138. /* we will send the SMB in three pieces:
  1139. * a fixed length beginning part, an optional
  1140. * SPNEGO blob (which can be zero length), and a
  1141. * last part which will include the strings
  1142. * and rest of bcc area. This allows us to avoid
  1143. * a large buffer 17K allocation
  1144. */
  1145. int buf0_type;
  1146. struct kvec iov[2];
  1147. };
  1148. static int
  1149. SMB2_sess_alloc_buffer(struct SMB2_sess_data *sess_data)
  1150. {
  1151. int rc;
  1152. struct cifs_ses *ses = sess_data->ses;
  1153. struct TCP_Server_Info *server = sess_data->server;
  1154. struct smb2_sess_setup_req *req;
  1155. unsigned int total_len;
  1156. bool is_binding = false;
  1157. rc = smb2_plain_req_init(SMB2_SESSION_SETUP, NULL, server,
  1158. (void **) &req,
  1159. &total_len);
  1160. if (rc)
  1161. return rc;
  1162. spin_lock(&ses->ses_lock);
  1163. is_binding = (ses->ses_status == SES_GOOD);
  1164. spin_unlock(&ses->ses_lock);
  1165. if (is_binding) {
  1166. req->hdr.SessionId = cpu_to_le64(ses->Suid);
  1167. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  1168. req->PreviousSessionId = 0;
  1169. req->Flags = SMB2_SESSION_REQ_FLAG_BINDING;
  1170. cifs_dbg(FYI, "Binding to sess id: %llx\n", ses->Suid);
  1171. } else {
  1172. /* First session, not a reauthenticate */
  1173. req->hdr.SessionId = 0;
  1174. /*
  1175. * if reconnect, we need to send previous sess id
  1176. * otherwise it is 0
  1177. */
  1178. req->PreviousSessionId = cpu_to_le64(sess_data->previous_session);
  1179. req->Flags = 0; /* MBZ */
  1180. cifs_dbg(FYI, "Fresh session. Previous: %llx\n",
  1181. sess_data->previous_session);
  1182. }
  1183. /* enough to enable echos and oplocks and one max size write */
  1184. if (server->credits >= server->max_credits)
  1185. req->hdr.CreditRequest = cpu_to_le16(0);
  1186. else
  1187. req->hdr.CreditRequest = cpu_to_le16(
  1188. min_t(int, server->max_credits -
  1189. server->credits, 130));
  1190. /* only one of SMB2 signing flags may be set in SMB2 request */
  1191. if (server->sign)
  1192. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_REQUIRED;
  1193. else if (global_secflags & CIFSSEC_MAY_SIGN) /* one flag unlike MUST_ */
  1194. req->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED;
  1195. else
  1196. req->SecurityMode = 0;
  1197. #ifdef CONFIG_CIFS_DFS_UPCALL
  1198. req->Capabilities = cpu_to_le32(SMB2_GLOBAL_CAP_DFS);
  1199. #else
  1200. req->Capabilities = 0;
  1201. #endif /* DFS_UPCALL */
  1202. req->Channel = 0; /* MBZ */
  1203. sess_data->iov[0].iov_base = (char *)req;
  1204. /* 1 for pad */
  1205. sess_data->iov[0].iov_len = total_len - 1;
  1206. /*
  1207. * This variable will be used to clear the buffer
  1208. * allocated above in case of any error in the calling function.
  1209. */
  1210. sess_data->buf0_type = CIFS_SMALL_BUFFER;
  1211. return 0;
  1212. }
  1213. static void
  1214. SMB2_sess_free_buffer(struct SMB2_sess_data *sess_data)
  1215. {
  1216. struct kvec *iov = sess_data->iov;
  1217. /* iov[1] is already freed by caller */
  1218. if (sess_data->buf0_type != CIFS_NO_BUFFER && iov[0].iov_base)
  1219. memzero_explicit(iov[0].iov_base, iov[0].iov_len);
  1220. free_rsp_buf(sess_data->buf0_type, iov[0].iov_base);
  1221. sess_data->buf0_type = CIFS_NO_BUFFER;
  1222. }
  1223. static int
  1224. SMB2_sess_sendreceive(struct SMB2_sess_data *sess_data)
  1225. {
  1226. int rc;
  1227. struct smb_rqst rqst;
  1228. struct smb2_sess_setup_req *req = sess_data->iov[0].iov_base;
  1229. struct kvec rsp_iov = { NULL, 0 };
  1230. /* Testing shows that buffer offset must be at location of Buffer[0] */
  1231. req->SecurityBufferOffset =
  1232. cpu_to_le16(sizeof(struct smb2_sess_setup_req) - 1 /* pad */);
  1233. req->SecurityBufferLength = cpu_to_le16(sess_data->iov[1].iov_len);
  1234. memset(&rqst, 0, sizeof(struct smb_rqst));
  1235. rqst.rq_iov = sess_data->iov;
  1236. rqst.rq_nvec = 2;
  1237. /* BB add code to build os and lm fields */
  1238. rc = cifs_send_recv(sess_data->xid, sess_data->ses,
  1239. sess_data->server,
  1240. &rqst,
  1241. &sess_data->buf0_type,
  1242. CIFS_LOG_ERROR | CIFS_SESS_OP, &rsp_iov);
  1243. cifs_small_buf_release(sess_data->iov[0].iov_base);
  1244. memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec));
  1245. return rc;
  1246. }
  1247. static int
  1248. SMB2_sess_establish_session(struct SMB2_sess_data *sess_data)
  1249. {
  1250. int rc = 0;
  1251. struct cifs_ses *ses = sess_data->ses;
  1252. struct TCP_Server_Info *server = sess_data->server;
  1253. cifs_server_lock(server);
  1254. if (server->ops->generate_signingkey) {
  1255. rc = server->ops->generate_signingkey(ses, server);
  1256. if (rc) {
  1257. cifs_dbg(FYI,
  1258. "SMB3 session key generation failed\n");
  1259. cifs_server_unlock(server);
  1260. return rc;
  1261. }
  1262. }
  1263. if (!server->session_estab) {
  1264. server->sequence_number = 0x2;
  1265. server->session_estab = true;
  1266. }
  1267. cifs_server_unlock(server);
  1268. cifs_dbg(FYI, "SMB2/3 session established successfully\n");
  1269. return rc;
  1270. }
  1271. #ifdef CONFIG_CIFS_UPCALL
  1272. static void
  1273. SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
  1274. {
  1275. int rc;
  1276. struct cifs_ses *ses = sess_data->ses;
  1277. struct TCP_Server_Info *server = sess_data->server;
  1278. struct cifs_spnego_msg *msg;
  1279. struct key *spnego_key = NULL;
  1280. struct smb2_sess_setup_rsp *rsp = NULL;
  1281. bool is_binding = false;
  1282. rc = SMB2_sess_alloc_buffer(sess_data);
  1283. if (rc)
  1284. goto out;
  1285. spnego_key = cifs_get_spnego_key(ses, server);
  1286. if (IS_ERR(spnego_key)) {
  1287. rc = PTR_ERR(spnego_key);
  1288. if (rc == -ENOKEY)
  1289. cifs_dbg(VFS, "Verify user has a krb5 ticket and keyutils is installed\n");
  1290. spnego_key = NULL;
  1291. goto out;
  1292. }
  1293. msg = spnego_key->payload.data[0];
  1294. /*
  1295. * check version field to make sure that cifs.upcall is
  1296. * sending us a response in an expected form
  1297. */
  1298. if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) {
  1299. cifs_dbg(VFS, "bad cifs.upcall version. Expected %d got %d\n",
  1300. CIFS_SPNEGO_UPCALL_VERSION, msg->version);
  1301. rc = -EKEYREJECTED;
  1302. goto out_put_spnego_key;
  1303. }
  1304. spin_lock(&ses->ses_lock);
  1305. is_binding = (ses->ses_status == SES_GOOD);
  1306. spin_unlock(&ses->ses_lock);
  1307. /* keep session key if binding */
  1308. if (!is_binding) {
  1309. kfree_sensitive(ses->auth_key.response);
  1310. ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len,
  1311. GFP_KERNEL);
  1312. if (!ses->auth_key.response) {
  1313. cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n",
  1314. msg->sesskey_len);
  1315. rc = -ENOMEM;
  1316. goto out_put_spnego_key;
  1317. }
  1318. ses->auth_key.len = msg->sesskey_len;
  1319. }
  1320. sess_data->iov[1].iov_base = msg->data + msg->sesskey_len;
  1321. sess_data->iov[1].iov_len = msg->secblob_len;
  1322. rc = SMB2_sess_sendreceive(sess_data);
  1323. if (rc)
  1324. goto out_put_spnego_key;
  1325. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  1326. /* keep session id and flags if binding */
  1327. if (!is_binding) {
  1328. ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
  1329. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  1330. }
  1331. rc = SMB2_sess_establish_session(sess_data);
  1332. out_put_spnego_key:
  1333. key_invalidate(spnego_key);
  1334. key_put(spnego_key);
  1335. if (rc) {
  1336. kfree_sensitive(ses->auth_key.response);
  1337. ses->auth_key.response = NULL;
  1338. ses->auth_key.len = 0;
  1339. }
  1340. out:
  1341. sess_data->result = rc;
  1342. sess_data->func = NULL;
  1343. SMB2_sess_free_buffer(sess_data);
  1344. }
  1345. #else
  1346. static void
  1347. SMB2_auth_kerberos(struct SMB2_sess_data *sess_data)
  1348. {
  1349. cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n");
  1350. sess_data->result = -EOPNOTSUPP;
  1351. sess_data->func = NULL;
  1352. }
  1353. #endif
  1354. static void
  1355. SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data);
  1356. static void
  1357. SMB2_sess_auth_rawntlmssp_negotiate(struct SMB2_sess_data *sess_data)
  1358. {
  1359. int rc;
  1360. struct cifs_ses *ses = sess_data->ses;
  1361. struct TCP_Server_Info *server = sess_data->server;
  1362. struct smb2_sess_setup_rsp *rsp = NULL;
  1363. unsigned char *ntlmssp_blob = NULL;
  1364. bool use_spnego = false; /* else use raw ntlmssp */
  1365. u16 blob_length = 0;
  1366. bool is_binding = false;
  1367. /*
  1368. * If memory allocation is successful, caller of this function
  1369. * frees it.
  1370. */
  1371. ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL);
  1372. if (!ses->ntlmssp) {
  1373. rc = -ENOMEM;
  1374. goto out_err;
  1375. }
  1376. ses->ntlmssp->sesskey_per_smbsess = true;
  1377. rc = SMB2_sess_alloc_buffer(sess_data);
  1378. if (rc)
  1379. goto out_err;
  1380. rc = build_ntlmssp_smb3_negotiate_blob(&ntlmssp_blob,
  1381. &blob_length, ses, server,
  1382. sess_data->nls_cp);
  1383. if (rc)
  1384. goto out;
  1385. if (use_spnego) {
  1386. /* BB eventually need to add this */
  1387. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  1388. rc = -EOPNOTSUPP;
  1389. goto out;
  1390. }
  1391. sess_data->iov[1].iov_base = ntlmssp_blob;
  1392. sess_data->iov[1].iov_len = blob_length;
  1393. rc = SMB2_sess_sendreceive(sess_data);
  1394. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  1395. /* If true, rc here is expected and not an error */
  1396. if (sess_data->buf0_type != CIFS_NO_BUFFER &&
  1397. rsp->hdr.Status == STATUS_MORE_PROCESSING_REQUIRED)
  1398. rc = 0;
  1399. if (rc)
  1400. goto out;
  1401. if (offsetof(struct smb2_sess_setup_rsp, Buffer) !=
  1402. le16_to_cpu(rsp->SecurityBufferOffset)) {
  1403. cifs_dbg(VFS, "Invalid security buffer offset %d\n",
  1404. le16_to_cpu(rsp->SecurityBufferOffset));
  1405. rc = -EIO;
  1406. goto out;
  1407. }
  1408. rc = decode_ntlmssp_challenge(rsp->Buffer,
  1409. le16_to_cpu(rsp->SecurityBufferLength), ses);
  1410. if (rc)
  1411. goto out;
  1412. cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n");
  1413. spin_lock(&ses->ses_lock);
  1414. is_binding = (ses->ses_status == SES_GOOD);
  1415. spin_unlock(&ses->ses_lock);
  1416. /* keep existing ses id and flags if binding */
  1417. if (!is_binding) {
  1418. ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
  1419. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  1420. }
  1421. out:
  1422. kfree_sensitive(ntlmssp_blob);
  1423. SMB2_sess_free_buffer(sess_data);
  1424. if (!rc) {
  1425. sess_data->result = 0;
  1426. sess_data->func = SMB2_sess_auth_rawntlmssp_authenticate;
  1427. return;
  1428. }
  1429. out_err:
  1430. kfree_sensitive(ses->ntlmssp);
  1431. ses->ntlmssp = NULL;
  1432. sess_data->result = rc;
  1433. sess_data->func = NULL;
  1434. }
  1435. static void
  1436. SMB2_sess_auth_rawntlmssp_authenticate(struct SMB2_sess_data *sess_data)
  1437. {
  1438. int rc;
  1439. struct cifs_ses *ses = sess_data->ses;
  1440. struct TCP_Server_Info *server = sess_data->server;
  1441. struct smb2_sess_setup_req *req;
  1442. struct smb2_sess_setup_rsp *rsp = NULL;
  1443. unsigned char *ntlmssp_blob = NULL;
  1444. bool use_spnego = false; /* else use raw ntlmssp */
  1445. u16 blob_length = 0;
  1446. bool is_binding = false;
  1447. rc = SMB2_sess_alloc_buffer(sess_data);
  1448. if (rc)
  1449. goto out;
  1450. req = (struct smb2_sess_setup_req *) sess_data->iov[0].iov_base;
  1451. req->hdr.SessionId = cpu_to_le64(ses->Suid);
  1452. rc = build_ntlmssp_auth_blob(&ntlmssp_blob, &blob_length,
  1453. ses, server,
  1454. sess_data->nls_cp);
  1455. if (rc) {
  1456. cifs_dbg(FYI, "build_ntlmssp_auth_blob failed %d\n", rc);
  1457. goto out;
  1458. }
  1459. if (use_spnego) {
  1460. /* BB eventually need to add this */
  1461. cifs_dbg(VFS, "spnego not supported for SMB2 yet\n");
  1462. rc = -EOPNOTSUPP;
  1463. goto out;
  1464. }
  1465. sess_data->iov[1].iov_base = ntlmssp_blob;
  1466. sess_data->iov[1].iov_len = blob_length;
  1467. rc = SMB2_sess_sendreceive(sess_data);
  1468. if (rc)
  1469. goto out;
  1470. rsp = (struct smb2_sess_setup_rsp *)sess_data->iov[0].iov_base;
  1471. spin_lock(&ses->ses_lock);
  1472. is_binding = (ses->ses_status == SES_GOOD);
  1473. spin_unlock(&ses->ses_lock);
  1474. /* keep existing ses id and flags if binding */
  1475. if (!is_binding) {
  1476. ses->Suid = le64_to_cpu(rsp->hdr.SessionId);
  1477. ses->session_flags = le16_to_cpu(rsp->SessionFlags);
  1478. }
  1479. rc = SMB2_sess_establish_session(sess_data);
  1480. #ifdef CONFIG_CIFS_DEBUG_DUMP_KEYS
  1481. if (ses->server->dialect < SMB30_PROT_ID) {
  1482. cifs_dbg(VFS, "%s: dumping generated SMB2 session keys\n", __func__);
  1483. /*
  1484. * The session id is opaque in terms of endianness, so we can't
  1485. * print it as a long long. we dump it as we got it on the wire
  1486. */
  1487. cifs_dbg(VFS, "Session Id %*ph\n", (int)sizeof(ses->Suid),
  1488. &ses->Suid);
  1489. cifs_dbg(VFS, "Session Key %*ph\n",
  1490. SMB2_NTLMV2_SESSKEY_SIZE, ses->auth_key.response);
  1491. cifs_dbg(VFS, "Signing Key %*ph\n",
  1492. SMB3_SIGN_KEY_SIZE, ses->auth_key.response);
  1493. }
  1494. #endif
  1495. out:
  1496. kfree_sensitive(ntlmssp_blob);
  1497. SMB2_sess_free_buffer(sess_data);
  1498. kfree_sensitive(ses->ntlmssp);
  1499. ses->ntlmssp = NULL;
  1500. sess_data->result = rc;
  1501. sess_data->func = NULL;
  1502. }
  1503. static int
  1504. SMB2_select_sec(struct SMB2_sess_data *sess_data)
  1505. {
  1506. int type;
  1507. struct cifs_ses *ses = sess_data->ses;
  1508. struct TCP_Server_Info *server = sess_data->server;
  1509. type = smb2_select_sectype(server, ses->sectype);
  1510. cifs_dbg(FYI, "sess setup type %d\n", type);
  1511. if (type == Unspecified) {
  1512. cifs_dbg(VFS, "Unable to select appropriate authentication method!\n");
  1513. return -EINVAL;
  1514. }
  1515. switch (type) {
  1516. case Kerberos:
  1517. sess_data->func = SMB2_auth_kerberos;
  1518. break;
  1519. case RawNTLMSSP:
  1520. sess_data->func = SMB2_sess_auth_rawntlmssp_negotiate;
  1521. break;
  1522. default:
  1523. cifs_dbg(VFS, "secType %d not supported!\n", type);
  1524. return -EOPNOTSUPP;
  1525. }
  1526. return 0;
  1527. }
  1528. int
  1529. SMB2_sess_setup(const unsigned int xid, struct cifs_ses *ses,
  1530. struct TCP_Server_Info *server,
  1531. const struct nls_table *nls_cp)
  1532. {
  1533. int rc = 0;
  1534. struct SMB2_sess_data *sess_data;
  1535. cifs_dbg(FYI, "Session Setup\n");
  1536. if (!server) {
  1537. WARN(1, "%s: server is NULL!\n", __func__);
  1538. return -EIO;
  1539. }
  1540. sess_data = kzalloc(sizeof(struct SMB2_sess_data), GFP_KERNEL);
  1541. if (!sess_data)
  1542. return -ENOMEM;
  1543. sess_data->xid = xid;
  1544. sess_data->ses = ses;
  1545. sess_data->server = server;
  1546. sess_data->buf0_type = CIFS_NO_BUFFER;
  1547. sess_data->nls_cp = (struct nls_table *) nls_cp;
  1548. sess_data->previous_session = ses->Suid;
  1549. rc = SMB2_select_sec(sess_data);
  1550. if (rc)
  1551. goto out;
  1552. /*
  1553. * Initialize the session hash with the server one.
  1554. */
  1555. memcpy(ses->preauth_sha_hash, server->preauth_sha_hash,
  1556. SMB2_PREAUTH_HASH_SIZE);
  1557. while (sess_data->func)
  1558. sess_data->func(sess_data);
  1559. if ((ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST) && (ses->sign))
  1560. cifs_server_dbg(VFS, "signing requested but authenticated as guest\n");
  1561. rc = sess_data->result;
  1562. out:
  1563. kfree_sensitive(sess_data);
  1564. return rc;
  1565. }
  1566. int
  1567. SMB2_logoff(const unsigned int xid, struct cifs_ses *ses)
  1568. {
  1569. struct smb_rqst rqst;
  1570. struct smb2_logoff_req *req; /* response is also trivial struct */
  1571. int rc = 0;
  1572. struct TCP_Server_Info *server;
  1573. int flags = 0;
  1574. unsigned int total_len;
  1575. struct kvec iov[1];
  1576. struct kvec rsp_iov;
  1577. int resp_buf_type;
  1578. cifs_dbg(FYI, "disconnect session %p\n", ses);
  1579. if (ses && (ses->server))
  1580. server = ses->server;
  1581. else
  1582. return -EIO;
  1583. /* no need to send SMB logoff if uid already closed due to reconnect */
  1584. spin_lock(&ses->chan_lock);
  1585. if (CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
  1586. spin_unlock(&ses->chan_lock);
  1587. goto smb2_session_already_dead;
  1588. }
  1589. spin_unlock(&ses->chan_lock);
  1590. rc = smb2_plain_req_init(SMB2_LOGOFF, NULL, ses->server,
  1591. (void **) &req, &total_len);
  1592. if (rc)
  1593. return rc;
  1594. /* since no tcon, smb2_init can not do this, so do here */
  1595. req->hdr.SessionId = cpu_to_le64(ses->Suid);
  1596. if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
  1597. flags |= CIFS_TRANSFORM_REQ;
  1598. else if (server->sign)
  1599. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  1600. flags |= CIFS_NO_RSP_BUF;
  1601. iov[0].iov_base = (char *)req;
  1602. iov[0].iov_len = total_len;
  1603. memset(&rqst, 0, sizeof(struct smb_rqst));
  1604. rqst.rq_iov = iov;
  1605. rqst.rq_nvec = 1;
  1606. rc = cifs_send_recv(xid, ses, ses->server,
  1607. &rqst, &resp_buf_type, flags, &rsp_iov);
  1608. cifs_small_buf_release(req);
  1609. /*
  1610. * No tcon so can't do
  1611. * cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_fail[SMB2...]);
  1612. */
  1613. smb2_session_already_dead:
  1614. return rc;
  1615. }
  1616. static inline void cifs_stats_fail_inc(struct cifs_tcon *tcon, uint16_t code)
  1617. {
  1618. cifs_stats_inc(&tcon->stats.smb2_stats.smb2_com_failed[code]);
  1619. }
  1620. #define MAX_SHARENAME_LENGTH (255 /* server */ + 80 /* share */ + 1 /* NULL */)
  1621. /* These are similar values to what Windows uses */
  1622. static inline void init_copy_chunk_defaults(struct cifs_tcon *tcon)
  1623. {
  1624. tcon->max_chunks = 256;
  1625. tcon->max_bytes_chunk = 1048576;
  1626. tcon->max_bytes_copy = 16777216;
  1627. }
  1628. int
  1629. SMB2_tcon(const unsigned int xid, struct cifs_ses *ses, const char *tree,
  1630. struct cifs_tcon *tcon, const struct nls_table *cp)
  1631. {
  1632. struct smb_rqst rqst;
  1633. struct smb2_tree_connect_req *req;
  1634. struct smb2_tree_connect_rsp *rsp = NULL;
  1635. struct kvec iov[2];
  1636. struct kvec rsp_iov = { NULL, 0 };
  1637. int rc = 0;
  1638. int resp_buftype;
  1639. int unc_path_len;
  1640. __le16 *unc_path = NULL;
  1641. int flags = 0;
  1642. unsigned int total_len;
  1643. struct TCP_Server_Info *server;
  1644. /* always use master channel */
  1645. server = ses->server;
  1646. cifs_dbg(FYI, "TCON\n");
  1647. if (!server || !tree)
  1648. return -EIO;
  1649. unc_path = kmalloc(MAX_SHARENAME_LENGTH * 2, GFP_KERNEL);
  1650. if (unc_path == NULL)
  1651. return -ENOMEM;
  1652. unc_path_len = cifs_strtoUTF16(unc_path, tree, strlen(tree), cp) + 1;
  1653. unc_path_len *= 2;
  1654. if (unc_path_len < 2) {
  1655. kfree(unc_path);
  1656. return -EINVAL;
  1657. }
  1658. /* SMB2 TREE_CONNECT request must be called with TreeId == 0 */
  1659. tcon->tid = 0;
  1660. atomic_set(&tcon->num_remote_opens, 0);
  1661. rc = smb2_plain_req_init(SMB2_TREE_CONNECT, tcon, server,
  1662. (void **) &req, &total_len);
  1663. if (rc) {
  1664. kfree(unc_path);
  1665. return rc;
  1666. }
  1667. if (smb3_encryption_required(tcon))
  1668. flags |= CIFS_TRANSFORM_REQ;
  1669. iov[0].iov_base = (char *)req;
  1670. /* 1 for pad */
  1671. iov[0].iov_len = total_len - 1;
  1672. /* Testing shows that buffer offset must be at location of Buffer[0] */
  1673. req->PathOffset = cpu_to_le16(sizeof(struct smb2_tree_connect_req)
  1674. - 1 /* pad */);
  1675. req->PathLength = cpu_to_le16(unc_path_len - 2);
  1676. iov[1].iov_base = unc_path;
  1677. iov[1].iov_len = unc_path_len;
  1678. /*
  1679. * 3.11 tcon req must be signed if not encrypted. See MS-SMB2 3.2.4.1.1
  1680. * unless it is guest or anonymous user. See MS-SMB2 3.2.5.3.1
  1681. * (Samba servers don't always set the flag so also check if null user)
  1682. */
  1683. if ((server->dialect == SMB311_PROT_ID) &&
  1684. !smb3_encryption_required(tcon) &&
  1685. !(ses->session_flags &
  1686. (SMB2_SESSION_FLAG_IS_GUEST|SMB2_SESSION_FLAG_IS_NULL)) &&
  1687. ((ses->user_name != NULL) || (ses->sectype == Kerberos)))
  1688. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  1689. memset(&rqst, 0, sizeof(struct smb_rqst));
  1690. rqst.rq_iov = iov;
  1691. rqst.rq_nvec = 2;
  1692. /* Need 64 for max size write so ask for more in case not there yet */
  1693. if (server->credits >= server->max_credits)
  1694. req->hdr.CreditRequest = cpu_to_le16(0);
  1695. else
  1696. req->hdr.CreditRequest = cpu_to_le16(
  1697. min_t(int, server->max_credits -
  1698. server->credits, 64));
  1699. rc = cifs_send_recv(xid, ses, server,
  1700. &rqst, &resp_buftype, flags, &rsp_iov);
  1701. cifs_small_buf_release(req);
  1702. rsp = (struct smb2_tree_connect_rsp *)rsp_iov.iov_base;
  1703. trace_smb3_tcon(xid, tcon->tid, ses->Suid, tree, rc);
  1704. if ((rc != 0) || (rsp == NULL)) {
  1705. cifs_stats_fail_inc(tcon, SMB2_TREE_CONNECT_HE);
  1706. tcon->need_reconnect = true;
  1707. goto tcon_error_exit;
  1708. }
  1709. switch (rsp->ShareType) {
  1710. case SMB2_SHARE_TYPE_DISK:
  1711. cifs_dbg(FYI, "connection to disk share\n");
  1712. break;
  1713. case SMB2_SHARE_TYPE_PIPE:
  1714. tcon->pipe = true;
  1715. cifs_dbg(FYI, "connection to pipe share\n");
  1716. break;
  1717. case SMB2_SHARE_TYPE_PRINT:
  1718. tcon->print = true;
  1719. cifs_dbg(FYI, "connection to printer\n");
  1720. break;
  1721. default:
  1722. cifs_server_dbg(VFS, "unknown share type %d\n", rsp->ShareType);
  1723. rc = -EOPNOTSUPP;
  1724. goto tcon_error_exit;
  1725. }
  1726. tcon->share_flags = le32_to_cpu(rsp->ShareFlags);
  1727. tcon->capabilities = rsp->Capabilities; /* we keep caps little endian */
  1728. tcon->maximal_access = le32_to_cpu(rsp->MaximalAccess);
  1729. tcon->tid = le32_to_cpu(rsp->hdr.Id.SyncId.TreeId);
  1730. strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
  1731. if ((rsp->Capabilities & SMB2_SHARE_CAP_DFS) &&
  1732. ((tcon->share_flags & SHI1005_FLAGS_DFS) == 0))
  1733. cifs_tcon_dbg(VFS, "DFS capability contradicts DFS flag\n");
  1734. if (tcon->seal &&
  1735. !(server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION))
  1736. cifs_tcon_dbg(VFS, "Encryption is requested but not supported\n");
  1737. init_copy_chunk_defaults(tcon);
  1738. if (server->ops->validate_negotiate)
  1739. rc = server->ops->validate_negotiate(xid, tcon);
  1740. tcon_exit:
  1741. free_rsp_buf(resp_buftype, rsp);
  1742. kfree(unc_path);
  1743. return rc;
  1744. tcon_error_exit:
  1745. if (rsp && rsp->hdr.Status == STATUS_BAD_NETWORK_NAME)
  1746. cifs_tcon_dbg(VFS, "BAD_NETWORK_NAME: %s\n", tree);
  1747. goto tcon_exit;
  1748. }
  1749. int
  1750. SMB2_tdis(const unsigned int xid, struct cifs_tcon *tcon)
  1751. {
  1752. struct smb_rqst rqst;
  1753. struct smb2_tree_disconnect_req *req; /* response is trivial */
  1754. int rc = 0;
  1755. struct cifs_ses *ses = tcon->ses;
  1756. int flags = 0;
  1757. unsigned int total_len;
  1758. struct kvec iov[1];
  1759. struct kvec rsp_iov;
  1760. int resp_buf_type;
  1761. cifs_dbg(FYI, "Tree Disconnect\n");
  1762. if (!ses || !(ses->server))
  1763. return -EIO;
  1764. trace_smb3_tdis_enter(xid, tcon->tid, ses->Suid, tcon->tree_name);
  1765. spin_lock(&ses->chan_lock);
  1766. if ((tcon->need_reconnect) ||
  1767. (CIFS_ALL_CHANS_NEED_RECONNECT(tcon->ses))) {
  1768. spin_unlock(&ses->chan_lock);
  1769. return 0;
  1770. }
  1771. spin_unlock(&ses->chan_lock);
  1772. invalidate_all_cached_dirs(tcon);
  1773. rc = smb2_plain_req_init(SMB2_TREE_DISCONNECT, tcon, ses->server,
  1774. (void **) &req,
  1775. &total_len);
  1776. if (rc)
  1777. return rc;
  1778. if (smb3_encryption_required(tcon))
  1779. flags |= CIFS_TRANSFORM_REQ;
  1780. flags |= CIFS_NO_RSP_BUF;
  1781. iov[0].iov_base = (char *)req;
  1782. iov[0].iov_len = total_len;
  1783. memset(&rqst, 0, sizeof(struct smb_rqst));
  1784. rqst.rq_iov = iov;
  1785. rqst.rq_nvec = 1;
  1786. rc = cifs_send_recv(xid, ses, ses->server,
  1787. &rqst, &resp_buf_type, flags, &rsp_iov);
  1788. cifs_small_buf_release(req);
  1789. if (rc) {
  1790. cifs_stats_fail_inc(tcon, SMB2_TREE_DISCONNECT_HE);
  1791. trace_smb3_tdis_err(xid, tcon->tid, ses->Suid, rc);
  1792. }
  1793. trace_smb3_tdis_done(xid, tcon->tid, ses->Suid);
  1794. return rc;
  1795. }
  1796. static struct create_durable *
  1797. create_durable_buf(void)
  1798. {
  1799. struct create_durable *buf;
  1800. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  1801. if (!buf)
  1802. return NULL;
  1803. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1804. (struct create_durable, Data));
  1805. buf->ccontext.DataLength = cpu_to_le32(16);
  1806. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1807. (struct create_durable, Name));
  1808. buf->ccontext.NameLength = cpu_to_le16(4);
  1809. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DHnQ" */
  1810. buf->Name[0] = 'D';
  1811. buf->Name[1] = 'H';
  1812. buf->Name[2] = 'n';
  1813. buf->Name[3] = 'Q';
  1814. return buf;
  1815. }
  1816. static struct create_durable *
  1817. create_reconnect_durable_buf(struct cifs_fid *fid)
  1818. {
  1819. struct create_durable *buf;
  1820. buf = kzalloc(sizeof(struct create_durable), GFP_KERNEL);
  1821. if (!buf)
  1822. return NULL;
  1823. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1824. (struct create_durable, Data));
  1825. buf->ccontext.DataLength = cpu_to_le32(16);
  1826. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1827. (struct create_durable, Name));
  1828. buf->ccontext.NameLength = cpu_to_le16(4);
  1829. buf->Data.Fid.PersistentFileId = fid->persistent_fid;
  1830. buf->Data.Fid.VolatileFileId = fid->volatile_fid;
  1831. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT is "DHnC" */
  1832. buf->Name[0] = 'D';
  1833. buf->Name[1] = 'H';
  1834. buf->Name[2] = 'n';
  1835. buf->Name[3] = 'C';
  1836. return buf;
  1837. }
  1838. static void
  1839. parse_query_id_ctxt(struct create_context *cc, struct smb2_file_all_info *buf)
  1840. {
  1841. struct create_on_disk_id *pdisk_id = (struct create_on_disk_id *)cc;
  1842. cifs_dbg(FYI, "parse query id context 0x%llx 0x%llx\n",
  1843. pdisk_id->DiskFileId, pdisk_id->VolumeId);
  1844. buf->IndexNumber = pdisk_id->DiskFileId;
  1845. }
  1846. static void
  1847. parse_posix_ctxt(struct create_context *cc, struct smb2_file_all_info *info,
  1848. struct create_posix_rsp *posix)
  1849. {
  1850. int sid_len;
  1851. u8 *beg = (u8 *)cc + le16_to_cpu(cc->DataOffset);
  1852. u8 *end = beg + le32_to_cpu(cc->DataLength);
  1853. u8 *sid;
  1854. memset(posix, 0, sizeof(*posix));
  1855. posix->nlink = le32_to_cpu(*(__le32 *)(beg + 0));
  1856. posix->reparse_tag = le32_to_cpu(*(__le32 *)(beg + 4));
  1857. posix->mode = le32_to_cpu(*(__le32 *)(beg + 8));
  1858. sid = beg + 12;
  1859. sid_len = posix_info_sid_size(sid, end);
  1860. if (sid_len < 0) {
  1861. cifs_dbg(VFS, "bad owner sid in posix create response\n");
  1862. return;
  1863. }
  1864. memcpy(&posix->owner, sid, sid_len);
  1865. sid = sid + sid_len;
  1866. sid_len = posix_info_sid_size(sid, end);
  1867. if (sid_len < 0) {
  1868. cifs_dbg(VFS, "bad group sid in posix create response\n");
  1869. return;
  1870. }
  1871. memcpy(&posix->group, sid, sid_len);
  1872. cifs_dbg(FYI, "nlink=%d mode=%o reparse_tag=%x\n",
  1873. posix->nlink, posix->mode, posix->reparse_tag);
  1874. }
  1875. void
  1876. smb2_parse_contexts(struct TCP_Server_Info *server,
  1877. struct smb2_create_rsp *rsp,
  1878. unsigned int *epoch, char *lease_key, __u8 *oplock,
  1879. struct smb2_file_all_info *buf,
  1880. struct create_posix_rsp *posix)
  1881. {
  1882. char *data_offset;
  1883. struct create_context *cc;
  1884. unsigned int next;
  1885. unsigned int remaining;
  1886. char *name;
  1887. static const char smb3_create_tag_posix[] = {
  1888. 0x93, 0xAD, 0x25, 0x50, 0x9C,
  1889. 0xB4, 0x11, 0xE7, 0xB4, 0x23, 0x83,
  1890. 0xDE, 0x96, 0x8B, 0xCD, 0x7C
  1891. };
  1892. *oplock = 0;
  1893. data_offset = (char *)rsp + le32_to_cpu(rsp->CreateContextsOffset);
  1894. remaining = le32_to_cpu(rsp->CreateContextsLength);
  1895. cc = (struct create_context *)data_offset;
  1896. /* Initialize inode number to 0 in case no valid data in qfid context */
  1897. if (buf)
  1898. buf->IndexNumber = 0;
  1899. while (remaining >= sizeof(struct create_context)) {
  1900. name = le16_to_cpu(cc->NameOffset) + (char *)cc;
  1901. if (le16_to_cpu(cc->NameLength) == 4 &&
  1902. strncmp(name, SMB2_CREATE_REQUEST_LEASE, 4) == 0)
  1903. *oplock = server->ops->parse_lease_buf(cc, epoch,
  1904. lease_key);
  1905. else if (buf && (le16_to_cpu(cc->NameLength) == 4) &&
  1906. strncmp(name, SMB2_CREATE_QUERY_ON_DISK_ID, 4) == 0)
  1907. parse_query_id_ctxt(cc, buf);
  1908. else if ((le16_to_cpu(cc->NameLength) == 16)) {
  1909. if (posix &&
  1910. memcmp(name, smb3_create_tag_posix, 16) == 0)
  1911. parse_posix_ctxt(cc, buf, posix);
  1912. }
  1913. /* else {
  1914. cifs_dbg(FYI, "Context not matched with len %d\n",
  1915. le16_to_cpu(cc->NameLength));
  1916. cifs_dump_mem("Cctxt name: ", name, 4);
  1917. } */
  1918. next = le32_to_cpu(cc->Next);
  1919. if (!next)
  1920. break;
  1921. remaining -= next;
  1922. cc = (struct create_context *)((char *)cc + next);
  1923. }
  1924. if (rsp->OplockLevel != SMB2_OPLOCK_LEVEL_LEASE)
  1925. *oplock = rsp->OplockLevel;
  1926. return;
  1927. }
  1928. static int
  1929. add_lease_context(struct TCP_Server_Info *server, struct kvec *iov,
  1930. unsigned int *num_iovec, u8 *lease_key, __u8 *oplock)
  1931. {
  1932. struct smb2_create_req *req = iov[0].iov_base;
  1933. unsigned int num = *num_iovec;
  1934. iov[num].iov_base = server->ops->create_lease_buf(lease_key, *oplock);
  1935. if (iov[num].iov_base == NULL)
  1936. return -ENOMEM;
  1937. iov[num].iov_len = server->vals->create_lease_size;
  1938. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
  1939. if (!req->CreateContextsOffset)
  1940. req->CreateContextsOffset = cpu_to_le32(
  1941. sizeof(struct smb2_create_req) +
  1942. iov[num - 1].iov_len);
  1943. le32_add_cpu(&req->CreateContextsLength,
  1944. server->vals->create_lease_size);
  1945. *num_iovec = num + 1;
  1946. return 0;
  1947. }
  1948. static struct create_durable_v2 *
  1949. create_durable_v2_buf(struct cifs_open_parms *oparms)
  1950. {
  1951. struct cifs_fid *pfid = oparms->fid;
  1952. struct create_durable_v2 *buf;
  1953. buf = kzalloc(sizeof(struct create_durable_v2), GFP_KERNEL);
  1954. if (!buf)
  1955. return NULL;
  1956. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  1957. (struct create_durable_v2, dcontext));
  1958. buf->ccontext.DataLength = cpu_to_le32(sizeof(struct durable_context_v2));
  1959. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  1960. (struct create_durable_v2, Name));
  1961. buf->ccontext.NameLength = cpu_to_le16(4);
  1962. /*
  1963. * NB: Handle timeout defaults to 0, which allows server to choose
  1964. * (most servers default to 120 seconds) and most clients default to 0.
  1965. * This can be overridden at mount ("handletimeout=") if the user wants
  1966. * a different persistent (or resilient) handle timeout for all opens
  1967. * opens on a particular SMB3 mount.
  1968. */
  1969. buf->dcontext.Timeout = cpu_to_le32(oparms->tcon->handle_timeout);
  1970. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  1971. generate_random_uuid(buf->dcontext.CreateGuid);
  1972. memcpy(pfid->create_guid, buf->dcontext.CreateGuid, 16);
  1973. /* SMB2_CREATE_DURABLE_HANDLE_REQUEST is "DH2Q" */
  1974. buf->Name[0] = 'D';
  1975. buf->Name[1] = 'H';
  1976. buf->Name[2] = '2';
  1977. buf->Name[3] = 'Q';
  1978. return buf;
  1979. }
  1980. static struct create_durable_handle_reconnect_v2 *
  1981. create_reconnect_durable_v2_buf(struct cifs_fid *fid)
  1982. {
  1983. struct create_durable_handle_reconnect_v2 *buf;
  1984. buf = kzalloc(sizeof(struct create_durable_handle_reconnect_v2),
  1985. GFP_KERNEL);
  1986. if (!buf)
  1987. return NULL;
  1988. buf->ccontext.DataOffset =
  1989. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1990. dcontext));
  1991. buf->ccontext.DataLength =
  1992. cpu_to_le32(sizeof(struct durable_reconnect_context_v2));
  1993. buf->ccontext.NameOffset =
  1994. cpu_to_le16(offsetof(struct create_durable_handle_reconnect_v2,
  1995. Name));
  1996. buf->ccontext.NameLength = cpu_to_le16(4);
  1997. buf->dcontext.Fid.PersistentFileId = fid->persistent_fid;
  1998. buf->dcontext.Fid.VolatileFileId = fid->volatile_fid;
  1999. buf->dcontext.Flags = cpu_to_le32(SMB2_DHANDLE_FLAG_PERSISTENT);
  2000. memcpy(buf->dcontext.CreateGuid, fid->create_guid, 16);
  2001. /* SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2 is "DH2C" */
  2002. buf->Name[0] = 'D';
  2003. buf->Name[1] = 'H';
  2004. buf->Name[2] = '2';
  2005. buf->Name[3] = 'C';
  2006. return buf;
  2007. }
  2008. static int
  2009. add_durable_v2_context(struct kvec *iov, unsigned int *num_iovec,
  2010. struct cifs_open_parms *oparms)
  2011. {
  2012. struct smb2_create_req *req = iov[0].iov_base;
  2013. unsigned int num = *num_iovec;
  2014. iov[num].iov_base = create_durable_v2_buf(oparms);
  2015. if (iov[num].iov_base == NULL)
  2016. return -ENOMEM;
  2017. iov[num].iov_len = sizeof(struct create_durable_v2);
  2018. if (!req->CreateContextsOffset)
  2019. req->CreateContextsOffset =
  2020. cpu_to_le32(sizeof(struct smb2_create_req) +
  2021. iov[1].iov_len);
  2022. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable_v2));
  2023. *num_iovec = num + 1;
  2024. return 0;
  2025. }
  2026. static int
  2027. add_durable_reconnect_v2_context(struct kvec *iov, unsigned int *num_iovec,
  2028. struct cifs_open_parms *oparms)
  2029. {
  2030. struct smb2_create_req *req = iov[0].iov_base;
  2031. unsigned int num = *num_iovec;
  2032. /* indicate that we don't need to relock the file */
  2033. oparms->reconnect = false;
  2034. iov[num].iov_base = create_reconnect_durable_v2_buf(oparms->fid);
  2035. if (iov[num].iov_base == NULL)
  2036. return -ENOMEM;
  2037. iov[num].iov_len = sizeof(struct create_durable_handle_reconnect_v2);
  2038. if (!req->CreateContextsOffset)
  2039. req->CreateContextsOffset =
  2040. cpu_to_le32(sizeof(struct smb2_create_req) +
  2041. iov[1].iov_len);
  2042. le32_add_cpu(&req->CreateContextsLength,
  2043. sizeof(struct create_durable_handle_reconnect_v2));
  2044. *num_iovec = num + 1;
  2045. return 0;
  2046. }
  2047. static int
  2048. add_durable_context(struct kvec *iov, unsigned int *num_iovec,
  2049. struct cifs_open_parms *oparms, bool use_persistent)
  2050. {
  2051. struct smb2_create_req *req = iov[0].iov_base;
  2052. unsigned int num = *num_iovec;
  2053. if (use_persistent) {
  2054. if (oparms->reconnect)
  2055. return add_durable_reconnect_v2_context(iov, num_iovec,
  2056. oparms);
  2057. else
  2058. return add_durable_v2_context(iov, num_iovec, oparms);
  2059. }
  2060. if (oparms->reconnect) {
  2061. iov[num].iov_base = create_reconnect_durable_buf(oparms->fid);
  2062. /* indicate that we don't need to relock the file */
  2063. oparms->reconnect = false;
  2064. } else
  2065. iov[num].iov_base = create_durable_buf();
  2066. if (iov[num].iov_base == NULL)
  2067. return -ENOMEM;
  2068. iov[num].iov_len = sizeof(struct create_durable);
  2069. if (!req->CreateContextsOffset)
  2070. req->CreateContextsOffset =
  2071. cpu_to_le32(sizeof(struct smb2_create_req) +
  2072. iov[1].iov_len);
  2073. le32_add_cpu(&req->CreateContextsLength, sizeof(struct create_durable));
  2074. *num_iovec = num + 1;
  2075. return 0;
  2076. }
  2077. /* See MS-SMB2 2.2.13.2.7 */
  2078. static struct crt_twarp_ctxt *
  2079. create_twarp_buf(__u64 timewarp)
  2080. {
  2081. struct crt_twarp_ctxt *buf;
  2082. buf = kzalloc(sizeof(struct crt_twarp_ctxt), GFP_KERNEL);
  2083. if (!buf)
  2084. return NULL;
  2085. buf->ccontext.DataOffset = cpu_to_le16(offsetof
  2086. (struct crt_twarp_ctxt, Timestamp));
  2087. buf->ccontext.DataLength = cpu_to_le32(8);
  2088. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  2089. (struct crt_twarp_ctxt, Name));
  2090. buf->ccontext.NameLength = cpu_to_le16(4);
  2091. /* SMB2_CREATE_TIMEWARP_TOKEN is "TWrp" */
  2092. buf->Name[0] = 'T';
  2093. buf->Name[1] = 'W';
  2094. buf->Name[2] = 'r';
  2095. buf->Name[3] = 'p';
  2096. buf->Timestamp = cpu_to_le64(timewarp);
  2097. return buf;
  2098. }
  2099. /* See MS-SMB2 2.2.13.2.7 */
  2100. static int
  2101. add_twarp_context(struct kvec *iov, unsigned int *num_iovec, __u64 timewarp)
  2102. {
  2103. struct smb2_create_req *req = iov[0].iov_base;
  2104. unsigned int num = *num_iovec;
  2105. iov[num].iov_base = create_twarp_buf(timewarp);
  2106. if (iov[num].iov_base == NULL)
  2107. return -ENOMEM;
  2108. iov[num].iov_len = sizeof(struct crt_twarp_ctxt);
  2109. if (!req->CreateContextsOffset)
  2110. req->CreateContextsOffset = cpu_to_le32(
  2111. sizeof(struct smb2_create_req) +
  2112. iov[num - 1].iov_len);
  2113. le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_twarp_ctxt));
  2114. *num_iovec = num + 1;
  2115. return 0;
  2116. }
  2117. /* See See http://technet.microsoft.com/en-us/library/hh509017(v=ws.10).aspx */
  2118. static void setup_owner_group_sids(char *buf)
  2119. {
  2120. struct owner_group_sids *sids = (struct owner_group_sids *)buf;
  2121. /* Populate the user ownership fields S-1-5-88-1 */
  2122. sids->owner.Revision = 1;
  2123. sids->owner.NumAuth = 3;
  2124. sids->owner.Authority[5] = 5;
  2125. sids->owner.SubAuthorities[0] = cpu_to_le32(88);
  2126. sids->owner.SubAuthorities[1] = cpu_to_le32(1);
  2127. sids->owner.SubAuthorities[2] = cpu_to_le32(current_fsuid().val);
  2128. /* Populate the group ownership fields S-1-5-88-2 */
  2129. sids->group.Revision = 1;
  2130. sids->group.NumAuth = 3;
  2131. sids->group.Authority[5] = 5;
  2132. sids->group.SubAuthorities[0] = cpu_to_le32(88);
  2133. sids->group.SubAuthorities[1] = cpu_to_le32(2);
  2134. sids->group.SubAuthorities[2] = cpu_to_le32(current_fsgid().val);
  2135. cifs_dbg(FYI, "owner S-1-5-88-1-%d, group S-1-5-88-2-%d\n", current_fsuid().val, current_fsgid().val);
  2136. }
  2137. /* See MS-SMB2 2.2.13.2.2 and MS-DTYP 2.4.6 */
  2138. static struct crt_sd_ctxt *
  2139. create_sd_buf(umode_t mode, bool set_owner, unsigned int *len)
  2140. {
  2141. struct crt_sd_ctxt *buf;
  2142. __u8 *ptr, *aclptr;
  2143. unsigned int acelen, acl_size, ace_count;
  2144. unsigned int owner_offset = 0;
  2145. unsigned int group_offset = 0;
  2146. struct smb3_acl acl = {};
  2147. *len = round_up(sizeof(struct crt_sd_ctxt) + (sizeof(struct cifs_ace) * 4), 8);
  2148. if (set_owner) {
  2149. /* sizeof(struct owner_group_sids) is already multiple of 8 so no need to round */
  2150. *len += sizeof(struct owner_group_sids);
  2151. }
  2152. buf = kzalloc(*len, GFP_KERNEL);
  2153. if (buf == NULL)
  2154. return buf;
  2155. ptr = (__u8 *)&buf[1];
  2156. if (set_owner) {
  2157. /* offset fields are from beginning of security descriptor not of create context */
  2158. owner_offset = ptr - (__u8 *)&buf->sd;
  2159. buf->sd.OffsetOwner = cpu_to_le32(owner_offset);
  2160. group_offset = owner_offset + offsetof(struct owner_group_sids, group);
  2161. buf->sd.OffsetGroup = cpu_to_le32(group_offset);
  2162. setup_owner_group_sids(ptr);
  2163. ptr += sizeof(struct owner_group_sids);
  2164. } else {
  2165. buf->sd.OffsetOwner = 0;
  2166. buf->sd.OffsetGroup = 0;
  2167. }
  2168. buf->ccontext.DataOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, sd));
  2169. buf->ccontext.NameOffset = cpu_to_le16(offsetof(struct crt_sd_ctxt, Name));
  2170. buf->ccontext.NameLength = cpu_to_le16(4);
  2171. /* SMB2_CREATE_SD_BUFFER_TOKEN is "SecD" */
  2172. buf->Name[0] = 'S';
  2173. buf->Name[1] = 'e';
  2174. buf->Name[2] = 'c';
  2175. buf->Name[3] = 'D';
  2176. buf->sd.Revision = 1; /* Must be one see MS-DTYP 2.4.6 */
  2177. /*
  2178. * ACL is "self relative" ie ACL is stored in contiguous block of memory
  2179. * and "DP" ie the DACL is present
  2180. */
  2181. buf->sd.Control = cpu_to_le16(ACL_CONTROL_SR | ACL_CONTROL_DP);
  2182. /* offset owner, group and Sbz1 and SACL are all zero */
  2183. buf->sd.OffsetDacl = cpu_to_le32(ptr - (__u8 *)&buf->sd);
  2184. /* Ship the ACL for now. we will copy it into buf later. */
  2185. aclptr = ptr;
  2186. ptr += sizeof(struct smb3_acl);
  2187. /* create one ACE to hold the mode embedded in reserved special SID */
  2188. acelen = setup_special_mode_ACE((struct cifs_ace *)ptr, (__u64)mode);
  2189. ptr += acelen;
  2190. acl_size = acelen + sizeof(struct smb3_acl);
  2191. ace_count = 1;
  2192. if (set_owner) {
  2193. /* we do not need to reallocate buffer to add the two more ACEs. plenty of space */
  2194. acelen = setup_special_user_owner_ACE((struct cifs_ace *)ptr);
  2195. ptr += acelen;
  2196. acl_size += acelen;
  2197. ace_count += 1;
  2198. }
  2199. /* and one more ACE to allow access for authenticated users */
  2200. acelen = setup_authusers_ACE((struct cifs_ace *)ptr);
  2201. ptr += acelen;
  2202. acl_size += acelen;
  2203. ace_count += 1;
  2204. acl.AclRevision = ACL_REVISION; /* See 2.4.4.1 of MS-DTYP */
  2205. acl.AclSize = cpu_to_le16(acl_size);
  2206. acl.AceCount = cpu_to_le16(ace_count);
  2207. /* acl.Sbz1 and Sbz2 MBZ so are not set here, but initialized above */
  2208. memcpy(aclptr, &acl, sizeof(struct smb3_acl));
  2209. buf->ccontext.DataLength = cpu_to_le32(ptr - (__u8 *)&buf->sd);
  2210. *len = round_up((unsigned int)(ptr - (__u8 *)buf), 8);
  2211. return buf;
  2212. }
  2213. static int
  2214. add_sd_context(struct kvec *iov, unsigned int *num_iovec, umode_t mode, bool set_owner)
  2215. {
  2216. struct smb2_create_req *req = iov[0].iov_base;
  2217. unsigned int num = *num_iovec;
  2218. unsigned int len = 0;
  2219. iov[num].iov_base = create_sd_buf(mode, set_owner, &len);
  2220. if (iov[num].iov_base == NULL)
  2221. return -ENOMEM;
  2222. iov[num].iov_len = len;
  2223. if (!req->CreateContextsOffset)
  2224. req->CreateContextsOffset = cpu_to_le32(
  2225. sizeof(struct smb2_create_req) +
  2226. iov[num - 1].iov_len);
  2227. le32_add_cpu(&req->CreateContextsLength, len);
  2228. *num_iovec = num + 1;
  2229. return 0;
  2230. }
  2231. static struct crt_query_id_ctxt *
  2232. create_query_id_buf(void)
  2233. {
  2234. struct crt_query_id_ctxt *buf;
  2235. buf = kzalloc(sizeof(struct crt_query_id_ctxt), GFP_KERNEL);
  2236. if (!buf)
  2237. return NULL;
  2238. buf->ccontext.DataOffset = cpu_to_le16(0);
  2239. buf->ccontext.DataLength = cpu_to_le32(0);
  2240. buf->ccontext.NameOffset = cpu_to_le16(offsetof
  2241. (struct crt_query_id_ctxt, Name));
  2242. buf->ccontext.NameLength = cpu_to_le16(4);
  2243. /* SMB2_CREATE_QUERY_ON_DISK_ID is "QFid" */
  2244. buf->Name[0] = 'Q';
  2245. buf->Name[1] = 'F';
  2246. buf->Name[2] = 'i';
  2247. buf->Name[3] = 'd';
  2248. return buf;
  2249. }
  2250. /* See MS-SMB2 2.2.13.2.9 */
  2251. static int
  2252. add_query_id_context(struct kvec *iov, unsigned int *num_iovec)
  2253. {
  2254. struct smb2_create_req *req = iov[0].iov_base;
  2255. unsigned int num = *num_iovec;
  2256. iov[num].iov_base = create_query_id_buf();
  2257. if (iov[num].iov_base == NULL)
  2258. return -ENOMEM;
  2259. iov[num].iov_len = sizeof(struct crt_query_id_ctxt);
  2260. if (!req->CreateContextsOffset)
  2261. req->CreateContextsOffset = cpu_to_le32(
  2262. sizeof(struct smb2_create_req) +
  2263. iov[num - 1].iov_len);
  2264. le32_add_cpu(&req->CreateContextsLength, sizeof(struct crt_query_id_ctxt));
  2265. *num_iovec = num + 1;
  2266. return 0;
  2267. }
  2268. static int
  2269. alloc_path_with_tree_prefix(__le16 **out_path, int *out_size, int *out_len,
  2270. const char *treename, const __le16 *path)
  2271. {
  2272. int treename_len, path_len;
  2273. struct nls_table *cp;
  2274. const __le16 sep[] = {cpu_to_le16('\\'), cpu_to_le16(0x0000)};
  2275. /*
  2276. * skip leading "\\"
  2277. */
  2278. treename_len = strlen(treename);
  2279. if (treename_len < 2 || !(treename[0] == '\\' && treename[1] == '\\'))
  2280. return -EINVAL;
  2281. treename += 2;
  2282. treename_len -= 2;
  2283. path_len = UniStrnlen((wchar_t *)path, PATH_MAX);
  2284. /* make room for one path separator only if @path isn't empty */
  2285. *out_len = treename_len + (path[0] ? 1 : 0) + path_len;
  2286. /*
  2287. * final path needs to be 8-byte aligned as specified in
  2288. * MS-SMB2 2.2.13 SMB2 CREATE Request.
  2289. */
  2290. *out_size = round_up(*out_len * sizeof(__le16), 8);
  2291. *out_path = kzalloc(*out_size + sizeof(__le16) /* null */, GFP_KERNEL);
  2292. if (!*out_path)
  2293. return -ENOMEM;
  2294. cp = load_nls_default();
  2295. cifs_strtoUTF16(*out_path, treename, treename_len, cp);
  2296. /* Do not append the separator if the path is empty */
  2297. if (path[0] != cpu_to_le16(0x0000)) {
  2298. UniStrcat(*out_path, sep);
  2299. UniStrcat(*out_path, path);
  2300. }
  2301. unload_nls(cp);
  2302. return 0;
  2303. }
  2304. int smb311_posix_mkdir(const unsigned int xid, struct inode *inode,
  2305. umode_t mode, struct cifs_tcon *tcon,
  2306. const char *full_path,
  2307. struct cifs_sb_info *cifs_sb)
  2308. {
  2309. struct smb_rqst rqst;
  2310. struct smb2_create_req *req;
  2311. struct smb2_create_rsp *rsp = NULL;
  2312. struct cifs_ses *ses = tcon->ses;
  2313. struct kvec iov[3]; /* make sure at least one for each open context */
  2314. struct kvec rsp_iov = {NULL, 0};
  2315. int resp_buftype;
  2316. int uni_path_len;
  2317. __le16 *copy_path = NULL;
  2318. int copy_size;
  2319. int rc = 0;
  2320. unsigned int n_iov = 2;
  2321. __u32 file_attributes = 0;
  2322. char *pc_buf = NULL;
  2323. int flags = 0;
  2324. unsigned int total_len;
  2325. __le16 *utf16_path = NULL;
  2326. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  2327. cifs_dbg(FYI, "mkdir\n");
  2328. /* resource #1: path allocation */
  2329. utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
  2330. if (!utf16_path)
  2331. return -ENOMEM;
  2332. if (!ses || !server) {
  2333. rc = -EIO;
  2334. goto err_free_path;
  2335. }
  2336. /* resource #2: request */
  2337. rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
  2338. (void **) &req, &total_len);
  2339. if (rc)
  2340. goto err_free_path;
  2341. if (smb3_encryption_required(tcon))
  2342. flags |= CIFS_TRANSFORM_REQ;
  2343. req->ImpersonationLevel = IL_IMPERSONATION;
  2344. req->DesiredAccess = cpu_to_le32(FILE_WRITE_ATTRIBUTES);
  2345. /* File attributes ignored on open (used in create though) */
  2346. req->FileAttributes = cpu_to_le32(file_attributes);
  2347. req->ShareAccess = FILE_SHARE_ALL_LE;
  2348. req->CreateDisposition = cpu_to_le32(FILE_CREATE);
  2349. req->CreateOptions = cpu_to_le32(CREATE_NOT_FILE);
  2350. iov[0].iov_base = (char *)req;
  2351. /* -1 since last byte is buf[0] which is sent below (path) */
  2352. iov[0].iov_len = total_len - 1;
  2353. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
  2354. /* [MS-SMB2] 2.2.13 NameOffset:
  2355. * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
  2356. * the SMB2 header, the file name includes a prefix that will
  2357. * be processed during DFS name normalization as specified in
  2358. * section 3.3.5.9. Otherwise, the file name is relative to
  2359. * the share that is identified by the TreeId in the SMB2
  2360. * header.
  2361. */
  2362. if (tcon->share_flags & SHI1005_FLAGS_DFS) {
  2363. int name_len;
  2364. req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
  2365. rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
  2366. &name_len,
  2367. tcon->tree_name, utf16_path);
  2368. if (rc)
  2369. goto err_free_req;
  2370. req->NameLength = cpu_to_le16(name_len * 2);
  2371. uni_path_len = copy_size;
  2372. /* free before overwriting resource */
  2373. kfree(utf16_path);
  2374. utf16_path = copy_path;
  2375. } else {
  2376. uni_path_len = (2 * UniStrnlen((wchar_t *)utf16_path, PATH_MAX)) + 2;
  2377. /* MUST set path len (NameLength) to 0 opening root of share */
  2378. req->NameLength = cpu_to_le16(uni_path_len - 2);
  2379. if (uni_path_len % 8 != 0) {
  2380. copy_size = roundup(uni_path_len, 8);
  2381. copy_path = kzalloc(copy_size, GFP_KERNEL);
  2382. if (!copy_path) {
  2383. rc = -ENOMEM;
  2384. goto err_free_req;
  2385. }
  2386. memcpy((char *)copy_path, (const char *)utf16_path,
  2387. uni_path_len);
  2388. uni_path_len = copy_size;
  2389. /* free before overwriting resource */
  2390. kfree(utf16_path);
  2391. utf16_path = copy_path;
  2392. }
  2393. }
  2394. iov[1].iov_len = uni_path_len;
  2395. iov[1].iov_base = utf16_path;
  2396. req->RequestedOplockLevel = SMB2_OPLOCK_LEVEL_NONE;
  2397. if (tcon->posix_extensions) {
  2398. /* resource #3: posix buf */
  2399. rc = add_posix_context(iov, &n_iov, mode);
  2400. if (rc)
  2401. goto err_free_req;
  2402. pc_buf = iov[n_iov-1].iov_base;
  2403. }
  2404. memset(&rqst, 0, sizeof(struct smb_rqst));
  2405. rqst.rq_iov = iov;
  2406. rqst.rq_nvec = n_iov;
  2407. /* no need to inc num_remote_opens because we close it just below */
  2408. trace_smb3_posix_mkdir_enter(xid, tcon->tid, ses->Suid, full_path, CREATE_NOT_FILE,
  2409. FILE_WRITE_ATTRIBUTES);
  2410. /* resource #4: response buffer */
  2411. rc = cifs_send_recv(xid, ses, server,
  2412. &rqst, &resp_buftype, flags, &rsp_iov);
  2413. if (rc) {
  2414. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  2415. trace_smb3_posix_mkdir_err(xid, tcon->tid, ses->Suid,
  2416. CREATE_NOT_FILE,
  2417. FILE_WRITE_ATTRIBUTES, rc);
  2418. goto err_free_rsp_buf;
  2419. }
  2420. /*
  2421. * Although unlikely to be possible for rsp to be null and rc not set,
  2422. * adding check below is slightly safer long term (and quiets Coverity
  2423. * warning)
  2424. */
  2425. rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
  2426. if (rsp == NULL) {
  2427. rc = -EIO;
  2428. kfree(pc_buf);
  2429. goto err_free_req;
  2430. }
  2431. trace_smb3_posix_mkdir_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
  2432. CREATE_NOT_FILE, FILE_WRITE_ATTRIBUTES);
  2433. SMB2_close(xid, tcon, rsp->PersistentFileId, rsp->VolatileFileId);
  2434. /* Eventually save off posix specific response info and timestaps */
  2435. err_free_rsp_buf:
  2436. free_rsp_buf(resp_buftype, rsp);
  2437. kfree(pc_buf);
  2438. err_free_req:
  2439. cifs_small_buf_release(req);
  2440. err_free_path:
  2441. kfree(utf16_path);
  2442. return rc;
  2443. }
  2444. int
  2445. SMB2_open_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  2446. struct smb_rqst *rqst, __u8 *oplock,
  2447. struct cifs_open_parms *oparms, __le16 *path)
  2448. {
  2449. struct smb2_create_req *req;
  2450. unsigned int n_iov = 2;
  2451. __u32 file_attributes = 0;
  2452. int copy_size;
  2453. int uni_path_len;
  2454. unsigned int total_len;
  2455. struct kvec *iov = rqst->rq_iov;
  2456. __le16 *copy_path;
  2457. int rc;
  2458. rc = smb2_plain_req_init(SMB2_CREATE, tcon, server,
  2459. (void **) &req, &total_len);
  2460. if (rc)
  2461. return rc;
  2462. iov[0].iov_base = (char *)req;
  2463. /* -1 since last byte is buf[0] which is sent below (path) */
  2464. iov[0].iov_len = total_len - 1;
  2465. if (oparms->create_options & CREATE_OPTION_READONLY)
  2466. file_attributes |= ATTR_READONLY;
  2467. if (oparms->create_options & CREATE_OPTION_SPECIAL)
  2468. file_attributes |= ATTR_SYSTEM;
  2469. req->ImpersonationLevel = IL_IMPERSONATION;
  2470. req->DesiredAccess = cpu_to_le32(oparms->desired_access);
  2471. /* File attributes ignored on open (used in create though) */
  2472. req->FileAttributes = cpu_to_le32(file_attributes);
  2473. req->ShareAccess = FILE_SHARE_ALL_LE;
  2474. req->CreateDisposition = cpu_to_le32(oparms->disposition);
  2475. req->CreateOptions = cpu_to_le32(oparms->create_options & CREATE_OPTIONS_MASK);
  2476. req->NameOffset = cpu_to_le16(sizeof(struct smb2_create_req));
  2477. /* [MS-SMB2] 2.2.13 NameOffset:
  2478. * If SMB2_FLAGS_DFS_OPERATIONS is set in the Flags field of
  2479. * the SMB2 header, the file name includes a prefix that will
  2480. * be processed during DFS name normalization as specified in
  2481. * section 3.3.5.9. Otherwise, the file name is relative to
  2482. * the share that is identified by the TreeId in the SMB2
  2483. * header.
  2484. */
  2485. if (tcon->share_flags & SHI1005_FLAGS_DFS) {
  2486. int name_len;
  2487. req->hdr.Flags |= SMB2_FLAGS_DFS_OPERATIONS;
  2488. rc = alloc_path_with_tree_prefix(&copy_path, &copy_size,
  2489. &name_len,
  2490. tcon->tree_name, path);
  2491. if (rc)
  2492. return rc;
  2493. req->NameLength = cpu_to_le16(name_len * 2);
  2494. uni_path_len = copy_size;
  2495. path = copy_path;
  2496. } else {
  2497. uni_path_len = (2 * UniStrnlen((wchar_t *)path, PATH_MAX)) + 2;
  2498. /* MUST set path len (NameLength) to 0 opening root of share */
  2499. req->NameLength = cpu_to_le16(uni_path_len - 2);
  2500. copy_size = round_up(uni_path_len, 8);
  2501. copy_path = kzalloc(copy_size, GFP_KERNEL);
  2502. if (!copy_path)
  2503. return -ENOMEM;
  2504. memcpy((char *)copy_path, (const char *)path,
  2505. uni_path_len);
  2506. uni_path_len = copy_size;
  2507. path = copy_path;
  2508. }
  2509. iov[1].iov_len = uni_path_len;
  2510. iov[1].iov_base = path;
  2511. if ((!server->oplocks) || (tcon->no_lease))
  2512. *oplock = SMB2_OPLOCK_LEVEL_NONE;
  2513. if (!(server->capabilities & SMB2_GLOBAL_CAP_LEASING) ||
  2514. *oplock == SMB2_OPLOCK_LEVEL_NONE)
  2515. req->RequestedOplockLevel = *oplock;
  2516. else if (!(server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) &&
  2517. (oparms->create_options & CREATE_NOT_FILE))
  2518. req->RequestedOplockLevel = *oplock; /* no srv lease support */
  2519. else {
  2520. rc = add_lease_context(server, iov, &n_iov,
  2521. oparms->fid->lease_key, oplock);
  2522. if (rc)
  2523. return rc;
  2524. }
  2525. if (*oplock == SMB2_OPLOCK_LEVEL_BATCH) {
  2526. /* need to set Next field of lease context if we request it */
  2527. if (server->capabilities & SMB2_GLOBAL_CAP_LEASING) {
  2528. struct create_context *ccontext =
  2529. (struct create_context *)iov[n_iov-1].iov_base;
  2530. ccontext->Next =
  2531. cpu_to_le32(server->vals->create_lease_size);
  2532. }
  2533. rc = add_durable_context(iov, &n_iov, oparms,
  2534. tcon->use_persistent);
  2535. if (rc)
  2536. return rc;
  2537. }
  2538. if (tcon->posix_extensions) {
  2539. if (n_iov > 2) {
  2540. struct create_context *ccontext =
  2541. (struct create_context *)iov[n_iov-1].iov_base;
  2542. ccontext->Next =
  2543. cpu_to_le32(iov[n_iov-1].iov_len);
  2544. }
  2545. rc = add_posix_context(iov, &n_iov, oparms->mode);
  2546. if (rc)
  2547. return rc;
  2548. }
  2549. if (tcon->snapshot_time) {
  2550. cifs_dbg(FYI, "adding snapshot context\n");
  2551. if (n_iov > 2) {
  2552. struct create_context *ccontext =
  2553. (struct create_context *)iov[n_iov-1].iov_base;
  2554. ccontext->Next =
  2555. cpu_to_le32(iov[n_iov-1].iov_len);
  2556. }
  2557. rc = add_twarp_context(iov, &n_iov, tcon->snapshot_time);
  2558. if (rc)
  2559. return rc;
  2560. }
  2561. if ((oparms->disposition != FILE_OPEN) && (oparms->cifs_sb)) {
  2562. bool set_mode;
  2563. bool set_owner;
  2564. if ((oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) &&
  2565. (oparms->mode != ACL_NO_MODE))
  2566. set_mode = true;
  2567. else {
  2568. set_mode = false;
  2569. oparms->mode = ACL_NO_MODE;
  2570. }
  2571. if (oparms->cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UID_FROM_ACL)
  2572. set_owner = true;
  2573. else
  2574. set_owner = false;
  2575. if (set_owner | set_mode) {
  2576. if (n_iov > 2) {
  2577. struct create_context *ccontext =
  2578. (struct create_context *)iov[n_iov-1].iov_base;
  2579. ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
  2580. }
  2581. cifs_dbg(FYI, "add sd with mode 0x%x\n", oparms->mode);
  2582. rc = add_sd_context(iov, &n_iov, oparms->mode, set_owner);
  2583. if (rc)
  2584. return rc;
  2585. }
  2586. }
  2587. if (n_iov > 2) {
  2588. struct create_context *ccontext =
  2589. (struct create_context *)iov[n_iov-1].iov_base;
  2590. ccontext->Next = cpu_to_le32(iov[n_iov-1].iov_len);
  2591. }
  2592. add_query_id_context(iov, &n_iov);
  2593. rqst->rq_nvec = n_iov;
  2594. return 0;
  2595. }
  2596. /* rq_iov[0] is the request and is released by cifs_small_buf_release().
  2597. * All other vectors are freed by kfree().
  2598. */
  2599. void
  2600. SMB2_open_free(struct smb_rqst *rqst)
  2601. {
  2602. int i;
  2603. if (rqst && rqst->rq_iov) {
  2604. cifs_small_buf_release(rqst->rq_iov[0].iov_base);
  2605. for (i = 1; i < rqst->rq_nvec; i++)
  2606. if (rqst->rq_iov[i].iov_base != smb2_padding)
  2607. kfree(rqst->rq_iov[i].iov_base);
  2608. }
  2609. }
  2610. int
  2611. SMB2_open(const unsigned int xid, struct cifs_open_parms *oparms, __le16 *path,
  2612. __u8 *oplock, struct smb2_file_all_info *buf,
  2613. struct create_posix_rsp *posix,
  2614. struct kvec *err_iov, int *buftype)
  2615. {
  2616. struct smb_rqst rqst;
  2617. struct smb2_create_rsp *rsp = NULL;
  2618. struct cifs_tcon *tcon = oparms->tcon;
  2619. struct cifs_ses *ses = tcon->ses;
  2620. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  2621. struct kvec iov[SMB2_CREATE_IOV_SIZE];
  2622. struct kvec rsp_iov = {NULL, 0};
  2623. int resp_buftype = CIFS_NO_BUFFER;
  2624. int rc = 0;
  2625. int flags = 0;
  2626. cifs_dbg(FYI, "create/open\n");
  2627. if (!ses || !server)
  2628. return -EIO;
  2629. if (smb3_encryption_required(tcon))
  2630. flags |= CIFS_TRANSFORM_REQ;
  2631. memset(&rqst, 0, sizeof(struct smb_rqst));
  2632. memset(&iov, 0, sizeof(iov));
  2633. rqst.rq_iov = iov;
  2634. rqst.rq_nvec = SMB2_CREATE_IOV_SIZE;
  2635. rc = SMB2_open_init(tcon, server,
  2636. &rqst, oplock, oparms, path);
  2637. if (rc)
  2638. goto creat_exit;
  2639. trace_smb3_open_enter(xid, tcon->tid, tcon->ses->Suid, oparms->path,
  2640. oparms->create_options, oparms->desired_access);
  2641. rc = cifs_send_recv(xid, ses, server,
  2642. &rqst, &resp_buftype, flags,
  2643. &rsp_iov);
  2644. rsp = (struct smb2_create_rsp *)rsp_iov.iov_base;
  2645. if (rc != 0) {
  2646. cifs_stats_fail_inc(tcon, SMB2_CREATE_HE);
  2647. if (err_iov && rsp) {
  2648. *err_iov = rsp_iov;
  2649. *buftype = resp_buftype;
  2650. resp_buftype = CIFS_NO_BUFFER;
  2651. rsp = NULL;
  2652. }
  2653. trace_smb3_open_err(xid, tcon->tid, ses->Suid,
  2654. oparms->create_options, oparms->desired_access, rc);
  2655. if (rc == -EREMCHG) {
  2656. pr_warn_once("server share %s deleted\n",
  2657. tcon->tree_name);
  2658. tcon->need_reconnect = true;
  2659. }
  2660. goto creat_exit;
  2661. } else if (rsp == NULL) /* unlikely to happen, but safer to check */
  2662. goto creat_exit;
  2663. else
  2664. trace_smb3_open_done(xid, rsp->PersistentFileId, tcon->tid, ses->Suid,
  2665. oparms->create_options, oparms->desired_access);
  2666. atomic_inc(&tcon->num_remote_opens);
  2667. oparms->fid->persistent_fid = rsp->PersistentFileId;
  2668. oparms->fid->volatile_fid = rsp->VolatileFileId;
  2669. oparms->fid->access = oparms->desired_access;
  2670. #ifdef CONFIG_CIFS_DEBUG2
  2671. oparms->fid->mid = le64_to_cpu(rsp->hdr.MessageId);
  2672. #endif /* CIFS_DEBUG2 */
  2673. if (buf) {
  2674. buf->CreationTime = rsp->CreationTime;
  2675. buf->LastAccessTime = rsp->LastAccessTime;
  2676. buf->LastWriteTime = rsp->LastWriteTime;
  2677. buf->ChangeTime = rsp->ChangeTime;
  2678. buf->AllocationSize = rsp->AllocationSize;
  2679. buf->EndOfFile = rsp->EndofFile;
  2680. buf->Attributes = rsp->FileAttributes;
  2681. buf->NumberOfLinks = cpu_to_le32(1);
  2682. buf->DeletePending = 0;
  2683. }
  2684. smb2_parse_contexts(server, rsp, &oparms->fid->epoch,
  2685. oparms->fid->lease_key, oplock, buf, posix);
  2686. creat_exit:
  2687. SMB2_open_free(&rqst);
  2688. free_rsp_buf(resp_buftype, rsp);
  2689. return rc;
  2690. }
  2691. int
  2692. SMB2_ioctl_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  2693. struct smb_rqst *rqst,
  2694. u64 persistent_fid, u64 volatile_fid, u32 opcode,
  2695. char *in_data, u32 indatalen,
  2696. __u32 max_response_size)
  2697. {
  2698. struct smb2_ioctl_req *req;
  2699. struct kvec *iov = rqst->rq_iov;
  2700. unsigned int total_len;
  2701. int rc;
  2702. char *in_data_buf;
  2703. rc = smb2_ioctl_req_init(opcode, tcon, server,
  2704. (void **) &req, &total_len);
  2705. if (rc)
  2706. return rc;
  2707. if (indatalen) {
  2708. /*
  2709. * indatalen is usually small at a couple of bytes max, so
  2710. * just allocate through generic pool
  2711. */
  2712. in_data_buf = kmemdup(in_data, indatalen, GFP_NOFS);
  2713. if (!in_data_buf) {
  2714. cifs_small_buf_release(req);
  2715. return -ENOMEM;
  2716. }
  2717. }
  2718. req->CtlCode = cpu_to_le32(opcode);
  2719. req->PersistentFileId = persistent_fid;
  2720. req->VolatileFileId = volatile_fid;
  2721. iov[0].iov_base = (char *)req;
  2722. /*
  2723. * If no input data, the size of ioctl struct in
  2724. * protocol spec still includes a 1 byte data buffer,
  2725. * but if input data passed to ioctl, we do not
  2726. * want to double count this, so we do not send
  2727. * the dummy one byte of data in iovec[0] if sending
  2728. * input data (in iovec[1]).
  2729. */
  2730. if (indatalen) {
  2731. req->InputCount = cpu_to_le32(indatalen);
  2732. /* do not set InputOffset if no input data */
  2733. req->InputOffset =
  2734. cpu_to_le32(offsetof(struct smb2_ioctl_req, Buffer));
  2735. rqst->rq_nvec = 2;
  2736. iov[0].iov_len = total_len - 1;
  2737. iov[1].iov_base = in_data_buf;
  2738. iov[1].iov_len = indatalen;
  2739. } else {
  2740. rqst->rq_nvec = 1;
  2741. iov[0].iov_len = total_len;
  2742. }
  2743. req->OutputOffset = 0;
  2744. req->OutputCount = 0; /* MBZ */
  2745. /*
  2746. * In most cases max_response_size is set to 16K (CIFSMaxBufSize)
  2747. * We Could increase default MaxOutputResponse, but that could require
  2748. * more credits. Windows typically sets this smaller, but for some
  2749. * ioctls it may be useful to allow server to send more. No point
  2750. * limiting what the server can send as long as fits in one credit
  2751. * We can not handle more than CIFS_MAX_BUF_SIZE yet but may want
  2752. * to increase this limit up in the future.
  2753. * Note that for snapshot queries that servers like Azure expect that
  2754. * the first query be minimal size (and just used to get the number/size
  2755. * of previous versions) so response size must be specified as EXACTLY
  2756. * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
  2757. * of eight bytes. Currently that is the only case where we set max
  2758. * response size smaller.
  2759. */
  2760. req->MaxOutputResponse = cpu_to_le32(max_response_size);
  2761. req->hdr.CreditCharge =
  2762. cpu_to_le16(DIV_ROUND_UP(max(indatalen, max_response_size),
  2763. SMB2_MAX_BUFFER_SIZE));
  2764. /* always an FSCTL (for now) */
  2765. req->Flags = cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL);
  2766. /* validate negotiate request must be signed - see MS-SMB2 3.2.5.5 */
  2767. if (opcode == FSCTL_VALIDATE_NEGOTIATE_INFO)
  2768. req->hdr.Flags |= SMB2_FLAGS_SIGNED;
  2769. return 0;
  2770. }
  2771. void
  2772. SMB2_ioctl_free(struct smb_rqst *rqst)
  2773. {
  2774. int i;
  2775. if (rqst && rqst->rq_iov) {
  2776. cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
  2777. for (i = 1; i < rqst->rq_nvec; i++)
  2778. if (rqst->rq_iov[i].iov_base != smb2_padding)
  2779. kfree(rqst->rq_iov[i].iov_base);
  2780. }
  2781. }
  2782. /*
  2783. * SMB2 IOCTL is used for both IOCTLs and FSCTLs
  2784. */
  2785. int
  2786. SMB2_ioctl(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  2787. u64 volatile_fid, u32 opcode, char *in_data, u32 indatalen,
  2788. u32 max_out_data_len, char **out_data,
  2789. u32 *plen /* returned data len */)
  2790. {
  2791. struct smb_rqst rqst;
  2792. struct smb2_ioctl_rsp *rsp = NULL;
  2793. struct cifs_ses *ses;
  2794. struct TCP_Server_Info *server;
  2795. struct kvec iov[SMB2_IOCTL_IOV_SIZE];
  2796. struct kvec rsp_iov = {NULL, 0};
  2797. int resp_buftype = CIFS_NO_BUFFER;
  2798. int rc = 0;
  2799. int flags = 0;
  2800. cifs_dbg(FYI, "SMB2 IOCTL\n");
  2801. if (out_data != NULL)
  2802. *out_data = NULL;
  2803. /* zero out returned data len, in case of error */
  2804. if (plen)
  2805. *plen = 0;
  2806. if (!tcon)
  2807. return -EIO;
  2808. ses = tcon->ses;
  2809. if (!ses)
  2810. return -EIO;
  2811. server = cifs_pick_channel(ses);
  2812. if (!server)
  2813. return -EIO;
  2814. if (smb3_encryption_required(tcon))
  2815. flags |= CIFS_TRANSFORM_REQ;
  2816. memset(&rqst, 0, sizeof(struct smb_rqst));
  2817. memset(&iov, 0, sizeof(iov));
  2818. rqst.rq_iov = iov;
  2819. rqst.rq_nvec = SMB2_IOCTL_IOV_SIZE;
  2820. rc = SMB2_ioctl_init(tcon, server,
  2821. &rqst, persistent_fid, volatile_fid, opcode,
  2822. in_data, indatalen, max_out_data_len);
  2823. if (rc)
  2824. goto ioctl_exit;
  2825. rc = cifs_send_recv(xid, ses, server,
  2826. &rqst, &resp_buftype, flags,
  2827. &rsp_iov);
  2828. rsp = (struct smb2_ioctl_rsp *)rsp_iov.iov_base;
  2829. if (rc != 0)
  2830. trace_smb3_fsctl_err(xid, persistent_fid, tcon->tid,
  2831. ses->Suid, 0, opcode, rc);
  2832. if ((rc != 0) && (rc != -EINVAL) && (rc != -E2BIG)) {
  2833. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  2834. goto ioctl_exit;
  2835. } else if (rc == -EINVAL) {
  2836. if ((opcode != FSCTL_SRV_COPYCHUNK_WRITE) &&
  2837. (opcode != FSCTL_SRV_COPYCHUNK)) {
  2838. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  2839. goto ioctl_exit;
  2840. }
  2841. } else if (rc == -E2BIG) {
  2842. if (opcode != FSCTL_QUERY_ALLOCATED_RANGES) {
  2843. cifs_stats_fail_inc(tcon, SMB2_IOCTL_HE);
  2844. goto ioctl_exit;
  2845. }
  2846. }
  2847. /* check if caller wants to look at return data or just return rc */
  2848. if ((plen == NULL) || (out_data == NULL))
  2849. goto ioctl_exit;
  2850. /*
  2851. * Although unlikely to be possible for rsp to be null and rc not set,
  2852. * adding check below is slightly safer long term (and quiets Coverity
  2853. * warning)
  2854. */
  2855. if (rsp == NULL) {
  2856. rc = -EIO;
  2857. goto ioctl_exit;
  2858. }
  2859. *plen = le32_to_cpu(rsp->OutputCount);
  2860. /* We check for obvious errors in the output buffer length and offset */
  2861. if (*plen == 0)
  2862. goto ioctl_exit; /* server returned no data */
  2863. else if (*plen > rsp_iov.iov_len || *plen > 0xFF00) {
  2864. cifs_tcon_dbg(VFS, "srv returned invalid ioctl length: %d\n", *plen);
  2865. *plen = 0;
  2866. rc = -EIO;
  2867. goto ioctl_exit;
  2868. }
  2869. if (rsp_iov.iov_len - *plen < le32_to_cpu(rsp->OutputOffset)) {
  2870. cifs_tcon_dbg(VFS, "Malformed ioctl resp: len %d offset %d\n", *plen,
  2871. le32_to_cpu(rsp->OutputOffset));
  2872. *plen = 0;
  2873. rc = -EIO;
  2874. goto ioctl_exit;
  2875. }
  2876. *out_data = kmemdup((char *)rsp + le32_to_cpu(rsp->OutputOffset),
  2877. *plen, GFP_KERNEL);
  2878. if (*out_data == NULL) {
  2879. rc = -ENOMEM;
  2880. goto ioctl_exit;
  2881. }
  2882. ioctl_exit:
  2883. SMB2_ioctl_free(&rqst);
  2884. free_rsp_buf(resp_buftype, rsp);
  2885. return rc;
  2886. }
  2887. /*
  2888. * Individual callers to ioctl worker function follow
  2889. */
  2890. int
  2891. SMB2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
  2892. u64 persistent_fid, u64 volatile_fid)
  2893. {
  2894. int rc;
  2895. struct compress_ioctl fsctl_input;
  2896. char *ret_data = NULL;
  2897. fsctl_input.CompressionState =
  2898. cpu_to_le16(COMPRESSION_FORMAT_DEFAULT);
  2899. rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
  2900. FSCTL_SET_COMPRESSION,
  2901. (char *)&fsctl_input /* data input */,
  2902. 2 /* in data len */, CIFSMaxBufSize /* max out data */,
  2903. &ret_data /* out data */, NULL);
  2904. cifs_dbg(FYI, "set compression rc %d\n", rc);
  2905. return rc;
  2906. }
  2907. int
  2908. SMB2_close_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  2909. struct smb_rqst *rqst,
  2910. u64 persistent_fid, u64 volatile_fid, bool query_attrs)
  2911. {
  2912. struct smb2_close_req *req;
  2913. struct kvec *iov = rqst->rq_iov;
  2914. unsigned int total_len;
  2915. int rc;
  2916. rc = smb2_plain_req_init(SMB2_CLOSE, tcon, server,
  2917. (void **) &req, &total_len);
  2918. if (rc)
  2919. return rc;
  2920. req->PersistentFileId = persistent_fid;
  2921. req->VolatileFileId = volatile_fid;
  2922. if (query_attrs)
  2923. req->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
  2924. else
  2925. req->Flags = 0;
  2926. iov[0].iov_base = (char *)req;
  2927. iov[0].iov_len = total_len;
  2928. return 0;
  2929. }
  2930. void
  2931. SMB2_close_free(struct smb_rqst *rqst)
  2932. {
  2933. if (rqst && rqst->rq_iov)
  2934. cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
  2935. }
  2936. int
  2937. __SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  2938. u64 persistent_fid, u64 volatile_fid,
  2939. struct smb2_file_network_open_info *pbuf)
  2940. {
  2941. struct smb_rqst rqst;
  2942. struct smb2_close_rsp *rsp = NULL;
  2943. struct cifs_ses *ses = tcon->ses;
  2944. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  2945. struct kvec iov[1];
  2946. struct kvec rsp_iov;
  2947. int resp_buftype = CIFS_NO_BUFFER;
  2948. int rc = 0;
  2949. int flags = 0;
  2950. bool query_attrs = false;
  2951. cifs_dbg(FYI, "Close\n");
  2952. if (!ses || !server)
  2953. return -EIO;
  2954. if (smb3_encryption_required(tcon))
  2955. flags |= CIFS_TRANSFORM_REQ;
  2956. memset(&rqst, 0, sizeof(struct smb_rqst));
  2957. memset(&iov, 0, sizeof(iov));
  2958. rqst.rq_iov = iov;
  2959. rqst.rq_nvec = 1;
  2960. /* check if need to ask server to return timestamps in close response */
  2961. if (pbuf)
  2962. query_attrs = true;
  2963. trace_smb3_close_enter(xid, persistent_fid, tcon->tid, ses->Suid);
  2964. rc = SMB2_close_init(tcon, server,
  2965. &rqst, persistent_fid, volatile_fid,
  2966. query_attrs);
  2967. if (rc)
  2968. goto close_exit;
  2969. rc = cifs_send_recv(xid, ses, server,
  2970. &rqst, &resp_buftype, flags, &rsp_iov);
  2971. rsp = (struct smb2_close_rsp *)rsp_iov.iov_base;
  2972. if (rc != 0) {
  2973. cifs_stats_fail_inc(tcon, SMB2_CLOSE_HE);
  2974. trace_smb3_close_err(xid, persistent_fid, tcon->tid, ses->Suid,
  2975. rc);
  2976. goto close_exit;
  2977. } else {
  2978. trace_smb3_close_done(xid, persistent_fid, tcon->tid,
  2979. ses->Suid);
  2980. /*
  2981. * Note that have to subtract 4 since struct network_open_info
  2982. * has a final 4 byte pad that close response does not have
  2983. */
  2984. if (pbuf)
  2985. memcpy(pbuf, (char *)&rsp->CreationTime, sizeof(*pbuf) - 4);
  2986. }
  2987. atomic_dec(&tcon->num_remote_opens);
  2988. close_exit:
  2989. SMB2_close_free(&rqst);
  2990. free_rsp_buf(resp_buftype, rsp);
  2991. /* retry close in a worker thread if this one is interrupted */
  2992. if (is_interrupt_error(rc)) {
  2993. int tmp_rc;
  2994. tmp_rc = smb2_handle_cancelled_close(tcon, persistent_fid,
  2995. volatile_fid);
  2996. if (tmp_rc)
  2997. cifs_dbg(VFS, "handle cancelled close fid 0x%llx returned error %d\n",
  2998. persistent_fid, tmp_rc);
  2999. }
  3000. return rc;
  3001. }
  3002. int
  3003. SMB2_close(const unsigned int xid, struct cifs_tcon *tcon,
  3004. u64 persistent_fid, u64 volatile_fid)
  3005. {
  3006. return __SMB2_close(xid, tcon, persistent_fid, volatile_fid, NULL);
  3007. }
  3008. int
  3009. smb2_validate_iov(unsigned int offset, unsigned int buffer_length,
  3010. struct kvec *iov, unsigned int min_buf_size)
  3011. {
  3012. unsigned int smb_len = iov->iov_len;
  3013. char *end_of_smb = smb_len + (char *)iov->iov_base;
  3014. char *begin_of_buf = offset + (char *)iov->iov_base;
  3015. char *end_of_buf = begin_of_buf + buffer_length;
  3016. if (buffer_length < min_buf_size) {
  3017. cifs_dbg(VFS, "buffer length %d smaller than minimum size %d\n",
  3018. buffer_length, min_buf_size);
  3019. return -EINVAL;
  3020. }
  3021. /* check if beyond RFC1001 maximum length */
  3022. if ((smb_len > 0x7FFFFF) || (buffer_length > 0x7FFFFF)) {
  3023. cifs_dbg(VFS, "buffer length %d or smb length %d too large\n",
  3024. buffer_length, smb_len);
  3025. return -EINVAL;
  3026. }
  3027. if ((begin_of_buf > end_of_smb) || (end_of_buf > end_of_smb)) {
  3028. cifs_dbg(VFS, "Invalid server response, bad offset to data\n");
  3029. return -EINVAL;
  3030. }
  3031. return 0;
  3032. }
  3033. /*
  3034. * If SMB buffer fields are valid, copy into temporary buffer to hold result.
  3035. * Caller must free buffer.
  3036. */
  3037. int
  3038. smb2_validate_and_copy_iov(unsigned int offset, unsigned int buffer_length,
  3039. struct kvec *iov, unsigned int minbufsize,
  3040. char *data)
  3041. {
  3042. char *begin_of_buf = offset + (char *)iov->iov_base;
  3043. int rc;
  3044. if (!data)
  3045. return -EINVAL;
  3046. rc = smb2_validate_iov(offset, buffer_length, iov, minbufsize);
  3047. if (rc)
  3048. return rc;
  3049. memcpy(data, begin_of_buf, minbufsize);
  3050. return 0;
  3051. }
  3052. int
  3053. SMB2_query_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  3054. struct smb_rqst *rqst,
  3055. u64 persistent_fid, u64 volatile_fid,
  3056. u8 info_class, u8 info_type, u32 additional_info,
  3057. size_t output_len, size_t input_len, void *input)
  3058. {
  3059. struct smb2_query_info_req *req;
  3060. struct kvec *iov = rqst->rq_iov;
  3061. unsigned int total_len;
  3062. int rc;
  3063. rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
  3064. (void **) &req, &total_len);
  3065. if (rc)
  3066. return rc;
  3067. req->InfoType = info_type;
  3068. req->FileInfoClass = info_class;
  3069. req->PersistentFileId = persistent_fid;
  3070. req->VolatileFileId = volatile_fid;
  3071. req->AdditionalInformation = cpu_to_le32(additional_info);
  3072. req->OutputBufferLength = cpu_to_le32(output_len);
  3073. if (input_len) {
  3074. req->InputBufferLength = cpu_to_le32(input_len);
  3075. /* total_len for smb query request never close to le16 max */
  3076. req->InputBufferOffset = cpu_to_le16(total_len - 1);
  3077. memcpy(req->Buffer, input, input_len);
  3078. }
  3079. iov[0].iov_base = (char *)req;
  3080. /* 1 for Buffer */
  3081. iov[0].iov_len = total_len - 1 + input_len;
  3082. return 0;
  3083. }
  3084. void
  3085. SMB2_query_info_free(struct smb_rqst *rqst)
  3086. {
  3087. if (rqst && rqst->rq_iov)
  3088. cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
  3089. }
  3090. static int
  3091. query_info(const unsigned int xid, struct cifs_tcon *tcon,
  3092. u64 persistent_fid, u64 volatile_fid, u8 info_class, u8 info_type,
  3093. u32 additional_info, size_t output_len, size_t min_len, void **data,
  3094. u32 *dlen)
  3095. {
  3096. struct smb_rqst rqst;
  3097. struct smb2_query_info_rsp *rsp = NULL;
  3098. struct kvec iov[1];
  3099. struct kvec rsp_iov;
  3100. int rc = 0;
  3101. int resp_buftype = CIFS_NO_BUFFER;
  3102. struct cifs_ses *ses = tcon->ses;
  3103. struct TCP_Server_Info *server;
  3104. int flags = 0;
  3105. bool allocated = false;
  3106. cifs_dbg(FYI, "Query Info\n");
  3107. if (!ses)
  3108. return -EIO;
  3109. server = cifs_pick_channel(ses);
  3110. if (!server)
  3111. return -EIO;
  3112. if (smb3_encryption_required(tcon))
  3113. flags |= CIFS_TRANSFORM_REQ;
  3114. memset(&rqst, 0, sizeof(struct smb_rqst));
  3115. memset(&iov, 0, sizeof(iov));
  3116. rqst.rq_iov = iov;
  3117. rqst.rq_nvec = 1;
  3118. rc = SMB2_query_info_init(tcon, server,
  3119. &rqst, persistent_fid, volatile_fid,
  3120. info_class, info_type, additional_info,
  3121. output_len, 0, NULL);
  3122. if (rc)
  3123. goto qinf_exit;
  3124. trace_smb3_query_info_enter(xid, persistent_fid, tcon->tid,
  3125. ses->Suid, info_class, (__u32)info_type);
  3126. rc = cifs_send_recv(xid, ses, server,
  3127. &rqst, &resp_buftype, flags, &rsp_iov);
  3128. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  3129. if (rc) {
  3130. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  3131. trace_smb3_query_info_err(xid, persistent_fid, tcon->tid,
  3132. ses->Suid, info_class, (__u32)info_type, rc);
  3133. goto qinf_exit;
  3134. }
  3135. trace_smb3_query_info_done(xid, persistent_fid, tcon->tid,
  3136. ses->Suid, info_class, (__u32)info_type);
  3137. if (dlen) {
  3138. *dlen = le32_to_cpu(rsp->OutputBufferLength);
  3139. if (!*data) {
  3140. *data = kmalloc(*dlen, GFP_KERNEL);
  3141. if (!*data) {
  3142. cifs_tcon_dbg(VFS,
  3143. "Error %d allocating memory for acl\n",
  3144. rc);
  3145. *dlen = 0;
  3146. rc = -ENOMEM;
  3147. goto qinf_exit;
  3148. }
  3149. allocated = true;
  3150. }
  3151. }
  3152. rc = smb2_validate_and_copy_iov(le16_to_cpu(rsp->OutputBufferOffset),
  3153. le32_to_cpu(rsp->OutputBufferLength),
  3154. &rsp_iov, dlen ? *dlen : min_len, *data);
  3155. if (rc && allocated) {
  3156. kfree(*data);
  3157. *data = NULL;
  3158. *dlen = 0;
  3159. }
  3160. qinf_exit:
  3161. SMB2_query_info_free(&rqst);
  3162. free_rsp_buf(resp_buftype, rsp);
  3163. return rc;
  3164. }
  3165. int SMB2_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  3166. u64 persistent_fid, u64 volatile_fid, struct smb2_file_all_info *data)
  3167. {
  3168. return query_info(xid, tcon, persistent_fid, volatile_fid,
  3169. FILE_ALL_INFORMATION, SMB2_O_INFO_FILE, 0,
  3170. sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
  3171. sizeof(struct smb2_file_all_info), (void **)&data,
  3172. NULL);
  3173. }
  3174. #if 0
  3175. /* currently unused, as now we are doing compounding instead (see smb311_posix_query_path_info) */
  3176. int
  3177. SMB311_posix_query_info(const unsigned int xid, struct cifs_tcon *tcon,
  3178. u64 persistent_fid, u64 volatile_fid, struct smb311_posix_qinfo *data, u32 *plen)
  3179. {
  3180. size_t output_len = sizeof(struct smb311_posix_qinfo *) +
  3181. (sizeof(struct cifs_sid) * 2) + (PATH_MAX * 2);
  3182. *plen = 0;
  3183. return query_info(xid, tcon, persistent_fid, volatile_fid,
  3184. SMB_FIND_FILE_POSIX_INFO, SMB2_O_INFO_FILE, 0,
  3185. output_len, sizeof(struct smb311_posix_qinfo), (void **)&data, plen);
  3186. /* Note caller must free "data" (passed in above). It may be allocated in query_info call */
  3187. }
  3188. #endif
  3189. int
  3190. SMB2_query_acl(const unsigned int xid, struct cifs_tcon *tcon,
  3191. u64 persistent_fid, u64 volatile_fid,
  3192. void **data, u32 *plen, u32 extra_info)
  3193. {
  3194. __u32 additional_info = OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
  3195. extra_info;
  3196. *plen = 0;
  3197. return query_info(xid, tcon, persistent_fid, volatile_fid,
  3198. 0, SMB2_O_INFO_SECURITY, additional_info,
  3199. SMB2_MAX_BUFFER_SIZE, MIN_SEC_DESC_LEN, data, plen);
  3200. }
  3201. int
  3202. SMB2_get_srv_num(const unsigned int xid, struct cifs_tcon *tcon,
  3203. u64 persistent_fid, u64 volatile_fid, __le64 *uniqueid)
  3204. {
  3205. return query_info(xid, tcon, persistent_fid, volatile_fid,
  3206. FILE_INTERNAL_INFORMATION, SMB2_O_INFO_FILE, 0,
  3207. sizeof(struct smb2_file_internal_info),
  3208. sizeof(struct smb2_file_internal_info),
  3209. (void **)&uniqueid, NULL);
  3210. }
  3211. /*
  3212. * CHANGE_NOTIFY Request is sent to get notifications on changes to a directory
  3213. * See MS-SMB2 2.2.35 and 2.2.36
  3214. */
  3215. static int
  3216. SMB2_notify_init(const unsigned int xid, struct smb_rqst *rqst,
  3217. struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  3218. u64 persistent_fid, u64 volatile_fid,
  3219. u32 completion_filter, bool watch_tree)
  3220. {
  3221. struct smb2_change_notify_req *req;
  3222. struct kvec *iov = rqst->rq_iov;
  3223. unsigned int total_len;
  3224. int rc;
  3225. rc = smb2_plain_req_init(SMB2_CHANGE_NOTIFY, tcon, server,
  3226. (void **) &req, &total_len);
  3227. if (rc)
  3228. return rc;
  3229. req->PersistentFileId = persistent_fid;
  3230. req->VolatileFileId = volatile_fid;
  3231. /* See note 354 of MS-SMB2, 64K max */
  3232. req->OutputBufferLength =
  3233. cpu_to_le32(SMB2_MAX_BUFFER_SIZE - MAX_SMB2_HDR_SIZE);
  3234. req->CompletionFilter = cpu_to_le32(completion_filter);
  3235. if (watch_tree)
  3236. req->Flags = cpu_to_le16(SMB2_WATCH_TREE);
  3237. else
  3238. req->Flags = 0;
  3239. iov[0].iov_base = (char *)req;
  3240. iov[0].iov_len = total_len;
  3241. return 0;
  3242. }
  3243. int
  3244. SMB2_change_notify(const unsigned int xid, struct cifs_tcon *tcon,
  3245. u64 persistent_fid, u64 volatile_fid, bool watch_tree,
  3246. u32 completion_filter, u32 max_out_data_len, char **out_data,
  3247. u32 *plen /* returned data len */)
  3248. {
  3249. struct cifs_ses *ses = tcon->ses;
  3250. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  3251. struct smb_rqst rqst;
  3252. struct smb2_change_notify_rsp *smb_rsp;
  3253. struct kvec iov[1];
  3254. struct kvec rsp_iov = {NULL, 0};
  3255. int resp_buftype = CIFS_NO_BUFFER;
  3256. int flags = 0;
  3257. int rc = 0;
  3258. cifs_dbg(FYI, "change notify\n");
  3259. if (!ses || !server)
  3260. return -EIO;
  3261. if (smb3_encryption_required(tcon))
  3262. flags |= CIFS_TRANSFORM_REQ;
  3263. memset(&rqst, 0, sizeof(struct smb_rqst));
  3264. memset(&iov, 0, sizeof(iov));
  3265. if (plen)
  3266. *plen = 0;
  3267. rqst.rq_iov = iov;
  3268. rqst.rq_nvec = 1;
  3269. rc = SMB2_notify_init(xid, &rqst, tcon, server,
  3270. persistent_fid, volatile_fid,
  3271. completion_filter, watch_tree);
  3272. if (rc)
  3273. goto cnotify_exit;
  3274. trace_smb3_notify_enter(xid, persistent_fid, tcon->tid, ses->Suid,
  3275. (u8)watch_tree, completion_filter);
  3276. rc = cifs_send_recv(xid, ses, server,
  3277. &rqst, &resp_buftype, flags, &rsp_iov);
  3278. if (rc != 0) {
  3279. cifs_stats_fail_inc(tcon, SMB2_CHANGE_NOTIFY_HE);
  3280. trace_smb3_notify_err(xid, persistent_fid, tcon->tid, ses->Suid,
  3281. (u8)watch_tree, completion_filter, rc);
  3282. } else {
  3283. trace_smb3_notify_done(xid, persistent_fid, tcon->tid,
  3284. ses->Suid, (u8)watch_tree, completion_filter);
  3285. /* validate that notify information is plausible */
  3286. if ((rsp_iov.iov_base == NULL) ||
  3287. (rsp_iov.iov_len < sizeof(struct smb2_change_notify_rsp)))
  3288. goto cnotify_exit;
  3289. smb_rsp = (struct smb2_change_notify_rsp *)rsp_iov.iov_base;
  3290. smb2_validate_iov(le16_to_cpu(smb_rsp->OutputBufferOffset),
  3291. le32_to_cpu(smb_rsp->OutputBufferLength), &rsp_iov,
  3292. sizeof(struct file_notify_information));
  3293. *out_data = kmemdup((char *)smb_rsp + le16_to_cpu(smb_rsp->OutputBufferOffset),
  3294. le32_to_cpu(smb_rsp->OutputBufferLength), GFP_KERNEL);
  3295. if (*out_data == NULL) {
  3296. rc = -ENOMEM;
  3297. goto cnotify_exit;
  3298. } else if (plen)
  3299. *plen = le32_to_cpu(smb_rsp->OutputBufferLength);
  3300. }
  3301. cnotify_exit:
  3302. if (rqst.rq_iov)
  3303. cifs_small_buf_release(rqst.rq_iov[0].iov_base); /* request */
  3304. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  3305. return rc;
  3306. }
  3307. /*
  3308. * This is a no-op for now. We're not really interested in the reply, but
  3309. * rather in the fact that the server sent one and that server->lstrp
  3310. * gets updated.
  3311. *
  3312. * FIXME: maybe we should consider checking that the reply matches request?
  3313. */
  3314. static void
  3315. smb2_echo_callback(struct mid_q_entry *mid)
  3316. {
  3317. struct TCP_Server_Info *server = mid->callback_data;
  3318. struct smb2_echo_rsp *rsp = (struct smb2_echo_rsp *)mid->resp_buf;
  3319. struct cifs_credits credits = { .value = 0, .instance = 0 };
  3320. if (mid->mid_state == MID_RESPONSE_RECEIVED
  3321. || mid->mid_state == MID_RESPONSE_MALFORMED) {
  3322. credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
  3323. credits.instance = server->reconnect_instance;
  3324. }
  3325. release_mid(mid);
  3326. add_credits(server, &credits, CIFS_ECHO_OP);
  3327. }
  3328. void smb2_reconnect_server(struct work_struct *work)
  3329. {
  3330. struct TCP_Server_Info *server = container_of(work,
  3331. struct TCP_Server_Info, reconnect.work);
  3332. struct TCP_Server_Info *pserver;
  3333. struct cifs_ses *ses, *ses2;
  3334. struct cifs_tcon *tcon, *tcon2;
  3335. struct list_head tmp_list, tmp_ses_list;
  3336. bool tcon_exist = false, ses_exist = false;
  3337. bool tcon_selected = false;
  3338. int rc;
  3339. bool resched = false;
  3340. /* If server is a channel, select the primary channel */
  3341. pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
  3342. /* Prevent simultaneous reconnects that can corrupt tcon->rlist list */
  3343. mutex_lock(&pserver->reconnect_mutex);
  3344. INIT_LIST_HEAD(&tmp_list);
  3345. INIT_LIST_HEAD(&tmp_ses_list);
  3346. cifs_dbg(FYI, "Reconnecting tcons and channels\n");
  3347. spin_lock(&cifs_tcp_ses_lock);
  3348. list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
  3349. spin_lock(&ses->ses_lock);
  3350. if (ses->ses_status == SES_EXITING) {
  3351. spin_unlock(&ses->ses_lock);
  3352. continue;
  3353. }
  3354. spin_unlock(&ses->ses_lock);
  3355. tcon_selected = false;
  3356. list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
  3357. if (tcon->need_reconnect || tcon->need_reopen_files) {
  3358. tcon->tc_count++;
  3359. list_add_tail(&tcon->rlist, &tmp_list);
  3360. tcon_selected = tcon_exist = true;
  3361. }
  3362. }
  3363. /*
  3364. * IPC has the same lifetime as its session and uses its
  3365. * refcount.
  3366. */
  3367. if (ses->tcon_ipc && ses->tcon_ipc->need_reconnect) {
  3368. list_add_tail(&ses->tcon_ipc->rlist, &tmp_list);
  3369. tcon_selected = tcon_exist = true;
  3370. ses->ses_count++;
  3371. }
  3372. /*
  3373. * handle the case where channel needs to reconnect
  3374. * binding session, but tcon is healthy (some other channel
  3375. * is active)
  3376. */
  3377. spin_lock(&ses->chan_lock);
  3378. if (!tcon_selected && cifs_chan_needs_reconnect(ses, server)) {
  3379. list_add_tail(&ses->rlist, &tmp_ses_list);
  3380. ses_exist = true;
  3381. ses->ses_count++;
  3382. }
  3383. spin_unlock(&ses->chan_lock);
  3384. }
  3385. /*
  3386. * Get the reference to server struct to be sure that the last call of
  3387. * cifs_put_tcon() in the loop below won't release the server pointer.
  3388. */
  3389. if (tcon_exist || ses_exist)
  3390. server->srv_count++;
  3391. spin_unlock(&cifs_tcp_ses_lock);
  3392. list_for_each_entry_safe(tcon, tcon2, &tmp_list, rlist) {
  3393. rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
  3394. if (!rc)
  3395. cifs_reopen_persistent_handles(tcon);
  3396. else
  3397. resched = true;
  3398. list_del_init(&tcon->rlist);
  3399. if (tcon->ipc)
  3400. cifs_put_smb_ses(tcon->ses);
  3401. else
  3402. cifs_put_tcon(tcon);
  3403. }
  3404. if (!ses_exist)
  3405. goto done;
  3406. /* allocate a dummy tcon struct used for reconnect */
  3407. tcon = tconInfoAlloc();
  3408. if (!tcon) {
  3409. resched = true;
  3410. list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
  3411. list_del_init(&ses->rlist);
  3412. cifs_put_smb_ses(ses);
  3413. }
  3414. goto done;
  3415. }
  3416. tcon->status = TID_GOOD;
  3417. tcon->retry = false;
  3418. tcon->need_reconnect = false;
  3419. /* now reconnect sessions for necessary channels */
  3420. list_for_each_entry_safe(ses, ses2, &tmp_ses_list, rlist) {
  3421. tcon->ses = ses;
  3422. rc = smb2_reconnect(SMB2_INTERNAL_CMD, tcon, server);
  3423. if (rc)
  3424. resched = true;
  3425. list_del_init(&ses->rlist);
  3426. cifs_put_smb_ses(ses);
  3427. }
  3428. tconInfoFree(tcon);
  3429. done:
  3430. cifs_dbg(FYI, "Reconnecting tcons and channels finished\n");
  3431. if (resched)
  3432. queue_delayed_work(cifsiod_wq, &server->reconnect, 2 * HZ);
  3433. mutex_unlock(&pserver->reconnect_mutex);
  3434. /* now we can safely release srv struct */
  3435. if (tcon_exist || ses_exist)
  3436. cifs_put_tcp_session(server, 1);
  3437. }
  3438. int
  3439. SMB2_echo(struct TCP_Server_Info *server)
  3440. {
  3441. struct smb2_echo_req *req;
  3442. int rc = 0;
  3443. struct kvec iov[1];
  3444. struct smb_rqst rqst = { .rq_iov = iov,
  3445. .rq_nvec = 1 };
  3446. unsigned int total_len;
  3447. cifs_dbg(FYI, "In echo request for conn_id %lld\n", server->conn_id);
  3448. spin_lock(&server->srv_lock);
  3449. if (server->ops->need_neg &&
  3450. server->ops->need_neg(server)) {
  3451. spin_unlock(&server->srv_lock);
  3452. /* No need to send echo on newly established connections */
  3453. mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
  3454. return rc;
  3455. }
  3456. spin_unlock(&server->srv_lock);
  3457. rc = smb2_plain_req_init(SMB2_ECHO, NULL, server,
  3458. (void **)&req, &total_len);
  3459. if (rc)
  3460. return rc;
  3461. req->hdr.CreditRequest = cpu_to_le16(1);
  3462. iov[0].iov_len = total_len;
  3463. iov[0].iov_base = (char *)req;
  3464. rc = cifs_call_async(server, &rqst, NULL, smb2_echo_callback, NULL,
  3465. server, CIFS_ECHO_OP, NULL);
  3466. if (rc)
  3467. cifs_dbg(FYI, "Echo request failed: %d\n", rc);
  3468. cifs_small_buf_release(req);
  3469. return rc;
  3470. }
  3471. void
  3472. SMB2_flush_free(struct smb_rqst *rqst)
  3473. {
  3474. if (rqst && rqst->rq_iov)
  3475. cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
  3476. }
  3477. int
  3478. SMB2_flush_init(const unsigned int xid, struct smb_rqst *rqst,
  3479. struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  3480. u64 persistent_fid, u64 volatile_fid)
  3481. {
  3482. struct smb2_flush_req *req;
  3483. struct kvec *iov = rqst->rq_iov;
  3484. unsigned int total_len;
  3485. int rc;
  3486. rc = smb2_plain_req_init(SMB2_FLUSH, tcon, server,
  3487. (void **) &req, &total_len);
  3488. if (rc)
  3489. return rc;
  3490. req->PersistentFileId = persistent_fid;
  3491. req->VolatileFileId = volatile_fid;
  3492. iov[0].iov_base = (char *)req;
  3493. iov[0].iov_len = total_len;
  3494. return 0;
  3495. }
  3496. int
  3497. SMB2_flush(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  3498. u64 volatile_fid)
  3499. {
  3500. struct cifs_ses *ses = tcon->ses;
  3501. struct smb_rqst rqst;
  3502. struct kvec iov[1];
  3503. struct kvec rsp_iov = {NULL, 0};
  3504. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  3505. int resp_buftype = CIFS_NO_BUFFER;
  3506. int flags = 0;
  3507. int rc = 0;
  3508. cifs_dbg(FYI, "flush\n");
  3509. if (!ses || !(ses->server))
  3510. return -EIO;
  3511. if (smb3_encryption_required(tcon))
  3512. flags |= CIFS_TRANSFORM_REQ;
  3513. memset(&rqst, 0, sizeof(struct smb_rqst));
  3514. memset(&iov, 0, sizeof(iov));
  3515. rqst.rq_iov = iov;
  3516. rqst.rq_nvec = 1;
  3517. rc = SMB2_flush_init(xid, &rqst, tcon, server,
  3518. persistent_fid, volatile_fid);
  3519. if (rc)
  3520. goto flush_exit;
  3521. trace_smb3_flush_enter(xid, persistent_fid, tcon->tid, ses->Suid);
  3522. rc = cifs_send_recv(xid, ses, server,
  3523. &rqst, &resp_buftype, flags, &rsp_iov);
  3524. if (rc != 0) {
  3525. cifs_stats_fail_inc(tcon, SMB2_FLUSH_HE);
  3526. trace_smb3_flush_err(xid, persistent_fid, tcon->tid, ses->Suid,
  3527. rc);
  3528. } else
  3529. trace_smb3_flush_done(xid, persistent_fid, tcon->tid,
  3530. ses->Suid);
  3531. flush_exit:
  3532. SMB2_flush_free(&rqst);
  3533. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  3534. return rc;
  3535. }
  3536. #ifdef CONFIG_CIFS_SMB_DIRECT
  3537. static inline bool smb3_use_rdma_offload(struct cifs_io_parms *io_parms)
  3538. {
  3539. struct TCP_Server_Info *server = io_parms->server;
  3540. struct cifs_tcon *tcon = io_parms->tcon;
  3541. /* we can only offload if we're connected */
  3542. if (!server || !tcon)
  3543. return false;
  3544. /* we can only offload on an rdma connection */
  3545. if (!server->rdma || !server->smbd_conn)
  3546. return false;
  3547. /* we don't support signed offload yet */
  3548. if (server->sign)
  3549. return false;
  3550. /* we don't support encrypted offload yet */
  3551. if (smb3_encryption_required(tcon))
  3552. return false;
  3553. /* offload also has its overhead, so only do it if desired */
  3554. if (io_parms->length < server->smbd_conn->rdma_readwrite_threshold)
  3555. return false;
  3556. return true;
  3557. }
  3558. #endif /* CONFIG_CIFS_SMB_DIRECT */
  3559. /*
  3560. * To form a chain of read requests, any read requests after the first should
  3561. * have the end_of_chain boolean set to true.
  3562. */
  3563. static int
  3564. smb2_new_read_req(void **buf, unsigned int *total_len,
  3565. struct cifs_io_parms *io_parms, struct cifs_readdata *rdata,
  3566. unsigned int remaining_bytes, int request_type)
  3567. {
  3568. int rc = -EACCES;
  3569. struct smb2_read_req *req = NULL;
  3570. struct smb2_hdr *shdr;
  3571. struct TCP_Server_Info *server = io_parms->server;
  3572. rc = smb2_plain_req_init(SMB2_READ, io_parms->tcon, server,
  3573. (void **) &req, total_len);
  3574. if (rc)
  3575. return rc;
  3576. if (server == NULL)
  3577. return -ECONNABORTED;
  3578. shdr = &req->hdr;
  3579. shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
  3580. req->PersistentFileId = io_parms->persistent_fid;
  3581. req->VolatileFileId = io_parms->volatile_fid;
  3582. req->ReadChannelInfoOffset = 0; /* reserved */
  3583. req->ReadChannelInfoLength = 0; /* reserved */
  3584. req->Channel = 0; /* reserved */
  3585. req->MinimumCount = 0;
  3586. req->Length = cpu_to_le32(io_parms->length);
  3587. req->Offset = cpu_to_le64(io_parms->offset);
  3588. trace_smb3_read_enter(0 /* xid */,
  3589. io_parms->persistent_fid,
  3590. io_parms->tcon->tid, io_parms->tcon->ses->Suid,
  3591. io_parms->offset, io_parms->length);
  3592. #ifdef CONFIG_CIFS_SMB_DIRECT
  3593. /*
  3594. * If we want to do a RDMA write, fill in and append
  3595. * smbd_buffer_descriptor_v1 to the end of read request
  3596. */
  3597. if (smb3_use_rdma_offload(io_parms)) {
  3598. struct smbd_buffer_descriptor_v1 *v1;
  3599. bool need_invalidate = server->dialect == SMB30_PROT_ID;
  3600. rdata->mr = smbd_register_mr(
  3601. server->smbd_conn, rdata->pages,
  3602. rdata->nr_pages, rdata->page_offset,
  3603. rdata->tailsz, true, need_invalidate);
  3604. if (!rdata->mr)
  3605. return -EAGAIN;
  3606. req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
  3607. if (need_invalidate)
  3608. req->Channel = SMB2_CHANNEL_RDMA_V1;
  3609. req->ReadChannelInfoOffset =
  3610. cpu_to_le16(offsetof(struct smb2_read_req, Buffer));
  3611. req->ReadChannelInfoLength =
  3612. cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
  3613. v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
  3614. v1->offset = cpu_to_le64(rdata->mr->mr->iova);
  3615. v1->token = cpu_to_le32(rdata->mr->mr->rkey);
  3616. v1->length = cpu_to_le32(rdata->mr->mr->length);
  3617. *total_len += sizeof(*v1) - 1;
  3618. }
  3619. #endif
  3620. if (request_type & CHAINED_REQUEST) {
  3621. if (!(request_type & END_OF_CHAIN)) {
  3622. /* next 8-byte aligned request */
  3623. *total_len = ALIGN(*total_len, 8);
  3624. shdr->NextCommand = cpu_to_le32(*total_len);
  3625. } else /* END_OF_CHAIN */
  3626. shdr->NextCommand = 0;
  3627. if (request_type & RELATED_REQUEST) {
  3628. shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
  3629. /*
  3630. * Related requests use info from previous read request
  3631. * in chain.
  3632. */
  3633. shdr->SessionId = cpu_to_le64(0xFFFFFFFFFFFFFFFF);
  3634. shdr->Id.SyncId.TreeId = cpu_to_le32(0xFFFFFFFF);
  3635. req->PersistentFileId = (u64)-1;
  3636. req->VolatileFileId = (u64)-1;
  3637. }
  3638. }
  3639. if (remaining_bytes > io_parms->length)
  3640. req->RemainingBytes = cpu_to_le32(remaining_bytes);
  3641. else
  3642. req->RemainingBytes = 0;
  3643. *buf = req;
  3644. return rc;
  3645. }
  3646. static void
  3647. smb2_readv_callback(struct mid_q_entry *mid)
  3648. {
  3649. struct cifs_readdata *rdata = mid->callback_data;
  3650. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  3651. struct TCP_Server_Info *server = rdata->server;
  3652. struct smb2_hdr *shdr =
  3653. (struct smb2_hdr *)rdata->iov[0].iov_base;
  3654. struct cifs_credits credits = { .value = 0, .instance = 0 };
  3655. struct smb_rqst rqst = { .rq_iov = &rdata->iov[1],
  3656. .rq_nvec = 1, };
  3657. if (rdata->got_bytes) {
  3658. rqst.rq_pages = rdata->pages;
  3659. rqst.rq_offset = rdata->page_offset;
  3660. rqst.rq_npages = rdata->nr_pages;
  3661. rqst.rq_pagesz = rdata->pagesz;
  3662. rqst.rq_tailsz = rdata->tailsz;
  3663. }
  3664. WARN_ONCE(rdata->server != mid->server,
  3665. "rdata server %p != mid server %p",
  3666. rdata->server, mid->server);
  3667. cifs_dbg(FYI, "%s: mid=%llu state=%d result=%d bytes=%u\n",
  3668. __func__, mid->mid, mid->mid_state, rdata->result,
  3669. rdata->bytes);
  3670. switch (mid->mid_state) {
  3671. case MID_RESPONSE_RECEIVED:
  3672. credits.value = le16_to_cpu(shdr->CreditRequest);
  3673. credits.instance = server->reconnect_instance;
  3674. /* result already set, check signature */
  3675. if (server->sign && !mid->decrypted) {
  3676. int rc;
  3677. rc = smb2_verify_signature(&rqst, server);
  3678. if (rc)
  3679. cifs_tcon_dbg(VFS, "SMB signature verification returned error = %d\n",
  3680. rc);
  3681. }
  3682. /* FIXME: should this be counted toward the initiating task? */
  3683. task_io_account_read(rdata->got_bytes);
  3684. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  3685. break;
  3686. case MID_REQUEST_SUBMITTED:
  3687. case MID_RETRY_NEEDED:
  3688. rdata->result = -EAGAIN;
  3689. if (server->sign && rdata->got_bytes)
  3690. /* reset bytes number since we can not check a sign */
  3691. rdata->got_bytes = 0;
  3692. /* FIXME: should this be counted toward the initiating task? */
  3693. task_io_account_read(rdata->got_bytes);
  3694. cifs_stats_bytes_read(tcon, rdata->got_bytes);
  3695. break;
  3696. case MID_RESPONSE_MALFORMED:
  3697. credits.value = le16_to_cpu(shdr->CreditRequest);
  3698. credits.instance = server->reconnect_instance;
  3699. fallthrough;
  3700. default:
  3701. rdata->result = -EIO;
  3702. }
  3703. #ifdef CONFIG_CIFS_SMB_DIRECT
  3704. /*
  3705. * If this rdata has a memmory registered, the MR can be freed
  3706. * MR needs to be freed as soon as I/O finishes to prevent deadlock
  3707. * because they have limited number and are used for future I/Os
  3708. */
  3709. if (rdata->mr) {
  3710. smbd_deregister_mr(rdata->mr);
  3711. rdata->mr = NULL;
  3712. }
  3713. #endif
  3714. if (rdata->result && rdata->result != -ENODATA) {
  3715. cifs_stats_fail_inc(tcon, SMB2_READ_HE);
  3716. trace_smb3_read_err(0 /* xid */,
  3717. rdata->cfile->fid.persistent_fid,
  3718. tcon->tid, tcon->ses->Suid, rdata->offset,
  3719. rdata->bytes, rdata->result);
  3720. } else
  3721. trace_smb3_read_done(0 /* xid */,
  3722. rdata->cfile->fid.persistent_fid,
  3723. tcon->tid, tcon->ses->Suid,
  3724. rdata->offset, rdata->got_bytes);
  3725. queue_work(cifsiod_wq, &rdata->work);
  3726. release_mid(mid);
  3727. add_credits(server, &credits, 0);
  3728. }
  3729. /* smb2_async_readv - send an async read, and set up mid to handle result */
  3730. int
  3731. smb2_async_readv(struct cifs_readdata *rdata)
  3732. {
  3733. int rc, flags = 0;
  3734. char *buf;
  3735. struct smb2_hdr *shdr;
  3736. struct cifs_io_parms io_parms;
  3737. struct smb_rqst rqst = { .rq_iov = rdata->iov,
  3738. .rq_nvec = 1 };
  3739. struct TCP_Server_Info *server;
  3740. struct cifs_tcon *tcon = tlink_tcon(rdata->cfile->tlink);
  3741. unsigned int total_len;
  3742. int credit_request;
  3743. cifs_dbg(FYI, "%s: offset=%llu bytes=%u\n",
  3744. __func__, rdata->offset, rdata->bytes);
  3745. if (!rdata->server)
  3746. rdata->server = cifs_pick_channel(tcon->ses);
  3747. io_parms.tcon = tlink_tcon(rdata->cfile->tlink);
  3748. io_parms.server = server = rdata->server;
  3749. io_parms.offset = rdata->offset;
  3750. io_parms.length = rdata->bytes;
  3751. io_parms.persistent_fid = rdata->cfile->fid.persistent_fid;
  3752. io_parms.volatile_fid = rdata->cfile->fid.volatile_fid;
  3753. io_parms.pid = rdata->pid;
  3754. rc = smb2_new_read_req(
  3755. (void **) &buf, &total_len, &io_parms, rdata, 0, 0);
  3756. if (rc)
  3757. return rc;
  3758. if (smb3_encryption_required(io_parms.tcon))
  3759. flags |= CIFS_TRANSFORM_REQ;
  3760. rdata->iov[0].iov_base = buf;
  3761. rdata->iov[0].iov_len = total_len;
  3762. shdr = (struct smb2_hdr *)buf;
  3763. if (rdata->credits.value > 0) {
  3764. shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(rdata->bytes,
  3765. SMB2_MAX_BUFFER_SIZE));
  3766. credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
  3767. if (server->credits >= server->max_credits)
  3768. shdr->CreditRequest = cpu_to_le16(0);
  3769. else
  3770. shdr->CreditRequest = cpu_to_le16(
  3771. min_t(int, server->max_credits -
  3772. server->credits, credit_request));
  3773. rc = adjust_credits(server, &rdata->credits, rdata->bytes);
  3774. if (rc)
  3775. goto async_readv_out;
  3776. flags |= CIFS_HAS_CREDITS;
  3777. }
  3778. kref_get(&rdata->refcount);
  3779. rc = cifs_call_async(server, &rqst,
  3780. cifs_readv_receive, smb2_readv_callback,
  3781. smb3_handle_read_data, rdata, flags,
  3782. &rdata->credits);
  3783. if (rc) {
  3784. kref_put(&rdata->refcount, cifs_readdata_release);
  3785. cifs_stats_fail_inc(io_parms.tcon, SMB2_READ_HE);
  3786. trace_smb3_read_err(0 /* xid */, io_parms.persistent_fid,
  3787. io_parms.tcon->tid,
  3788. io_parms.tcon->ses->Suid,
  3789. io_parms.offset, io_parms.length, rc);
  3790. }
  3791. async_readv_out:
  3792. cifs_small_buf_release(buf);
  3793. return rc;
  3794. }
  3795. int
  3796. SMB2_read(const unsigned int xid, struct cifs_io_parms *io_parms,
  3797. unsigned int *nbytes, char **buf, int *buf_type)
  3798. {
  3799. struct smb_rqst rqst;
  3800. int resp_buftype, rc;
  3801. struct smb2_read_req *req = NULL;
  3802. struct smb2_read_rsp *rsp = NULL;
  3803. struct kvec iov[1];
  3804. struct kvec rsp_iov;
  3805. unsigned int total_len;
  3806. int flags = CIFS_LOG_ERROR;
  3807. struct cifs_ses *ses = io_parms->tcon->ses;
  3808. if (!io_parms->server)
  3809. io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
  3810. *nbytes = 0;
  3811. rc = smb2_new_read_req((void **)&req, &total_len, io_parms, NULL, 0, 0);
  3812. if (rc)
  3813. return rc;
  3814. if (smb3_encryption_required(io_parms->tcon))
  3815. flags |= CIFS_TRANSFORM_REQ;
  3816. iov[0].iov_base = (char *)req;
  3817. iov[0].iov_len = total_len;
  3818. memset(&rqst, 0, sizeof(struct smb_rqst));
  3819. rqst.rq_iov = iov;
  3820. rqst.rq_nvec = 1;
  3821. rc = cifs_send_recv(xid, ses, io_parms->server,
  3822. &rqst, &resp_buftype, flags, &rsp_iov);
  3823. rsp = (struct smb2_read_rsp *)rsp_iov.iov_base;
  3824. if (rc) {
  3825. if (rc != -ENODATA) {
  3826. cifs_stats_fail_inc(io_parms->tcon, SMB2_READ_HE);
  3827. cifs_dbg(VFS, "Send error in read = %d\n", rc);
  3828. trace_smb3_read_err(xid,
  3829. req->PersistentFileId,
  3830. io_parms->tcon->tid, ses->Suid,
  3831. io_parms->offset, io_parms->length,
  3832. rc);
  3833. } else
  3834. trace_smb3_read_done(xid, req->PersistentFileId, io_parms->tcon->tid,
  3835. ses->Suid, io_parms->offset, 0);
  3836. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  3837. cifs_small_buf_release(req);
  3838. return rc == -ENODATA ? 0 : rc;
  3839. } else
  3840. trace_smb3_read_done(xid,
  3841. req->PersistentFileId,
  3842. io_parms->tcon->tid, ses->Suid,
  3843. io_parms->offset, io_parms->length);
  3844. cifs_small_buf_release(req);
  3845. *nbytes = le32_to_cpu(rsp->DataLength);
  3846. if ((*nbytes > CIFS_MAX_MSGSIZE) ||
  3847. (*nbytes > io_parms->length)) {
  3848. cifs_dbg(FYI, "bad length %d for count %d\n",
  3849. *nbytes, io_parms->length);
  3850. rc = -EIO;
  3851. *nbytes = 0;
  3852. }
  3853. if (*buf) {
  3854. memcpy(*buf, (char *)rsp + rsp->DataOffset, *nbytes);
  3855. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  3856. } else if (resp_buftype != CIFS_NO_BUFFER) {
  3857. *buf = rsp_iov.iov_base;
  3858. if (resp_buftype == CIFS_SMALL_BUFFER)
  3859. *buf_type = CIFS_SMALL_BUFFER;
  3860. else if (resp_buftype == CIFS_LARGE_BUFFER)
  3861. *buf_type = CIFS_LARGE_BUFFER;
  3862. }
  3863. return rc;
  3864. }
  3865. /*
  3866. * Check the mid_state and signature on received buffer (if any), and queue the
  3867. * workqueue completion task.
  3868. */
  3869. static void
  3870. smb2_writev_callback(struct mid_q_entry *mid)
  3871. {
  3872. struct cifs_writedata *wdata = mid->callback_data;
  3873. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  3874. struct TCP_Server_Info *server = wdata->server;
  3875. unsigned int written;
  3876. struct smb2_write_rsp *rsp = (struct smb2_write_rsp *)mid->resp_buf;
  3877. struct cifs_credits credits = { .value = 0, .instance = 0 };
  3878. WARN_ONCE(wdata->server != mid->server,
  3879. "wdata server %p != mid server %p",
  3880. wdata->server, mid->server);
  3881. switch (mid->mid_state) {
  3882. case MID_RESPONSE_RECEIVED:
  3883. credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
  3884. credits.instance = server->reconnect_instance;
  3885. wdata->result = smb2_check_receive(mid, server, 0);
  3886. if (wdata->result != 0)
  3887. break;
  3888. written = le32_to_cpu(rsp->DataLength);
  3889. /*
  3890. * Mask off high 16 bits when bytes written as returned
  3891. * by the server is greater than bytes requested by the
  3892. * client. OS/2 servers are known to set incorrect
  3893. * CountHigh values.
  3894. */
  3895. if (written > wdata->bytes)
  3896. written &= 0xFFFF;
  3897. if (written < wdata->bytes)
  3898. wdata->result = -ENOSPC;
  3899. else
  3900. wdata->bytes = written;
  3901. break;
  3902. case MID_REQUEST_SUBMITTED:
  3903. case MID_RETRY_NEEDED:
  3904. wdata->result = -EAGAIN;
  3905. break;
  3906. case MID_RESPONSE_MALFORMED:
  3907. credits.value = le16_to_cpu(rsp->hdr.CreditRequest);
  3908. credits.instance = server->reconnect_instance;
  3909. fallthrough;
  3910. default:
  3911. wdata->result = -EIO;
  3912. break;
  3913. }
  3914. #ifdef CONFIG_CIFS_SMB_DIRECT
  3915. /*
  3916. * If this wdata has a memory registered, the MR can be freed
  3917. * The number of MRs available is limited, it's important to recover
  3918. * used MR as soon as I/O is finished. Hold MR longer in the later
  3919. * I/O process can possibly result in I/O deadlock due to lack of MR
  3920. * to send request on I/O retry
  3921. */
  3922. if (wdata->mr) {
  3923. smbd_deregister_mr(wdata->mr);
  3924. wdata->mr = NULL;
  3925. }
  3926. #endif
  3927. if (wdata->result) {
  3928. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  3929. trace_smb3_write_err(0 /* no xid */,
  3930. wdata->cfile->fid.persistent_fid,
  3931. tcon->tid, tcon->ses->Suid, wdata->offset,
  3932. wdata->bytes, wdata->result);
  3933. if (wdata->result == -ENOSPC)
  3934. pr_warn_once("Out of space writing to %s\n",
  3935. tcon->tree_name);
  3936. } else
  3937. trace_smb3_write_done(0 /* no xid */,
  3938. wdata->cfile->fid.persistent_fid,
  3939. tcon->tid, tcon->ses->Suid,
  3940. wdata->offset, wdata->bytes);
  3941. queue_work(cifsiod_wq, &wdata->work);
  3942. release_mid(mid);
  3943. add_credits(server, &credits, 0);
  3944. }
  3945. /* smb2_async_writev - send an async write, and set up mid to handle result */
  3946. int
  3947. smb2_async_writev(struct cifs_writedata *wdata,
  3948. void (*release)(struct kref *kref))
  3949. {
  3950. int rc = -EACCES, flags = 0;
  3951. struct smb2_write_req *req = NULL;
  3952. struct smb2_hdr *shdr;
  3953. struct cifs_tcon *tcon = tlink_tcon(wdata->cfile->tlink);
  3954. struct TCP_Server_Info *server = wdata->server;
  3955. struct kvec iov[1];
  3956. struct smb_rqst rqst = { };
  3957. unsigned int total_len;
  3958. struct cifs_io_parms _io_parms;
  3959. struct cifs_io_parms *io_parms = NULL;
  3960. int credit_request;
  3961. if (!wdata->server)
  3962. server = wdata->server = cifs_pick_channel(tcon->ses);
  3963. /*
  3964. * in future we may get cifs_io_parms passed in from the caller,
  3965. * but for now we construct it here...
  3966. */
  3967. _io_parms = (struct cifs_io_parms) {
  3968. .tcon = tcon,
  3969. .server = server,
  3970. .offset = wdata->offset,
  3971. .length = wdata->bytes,
  3972. .persistent_fid = wdata->cfile->fid.persistent_fid,
  3973. .volatile_fid = wdata->cfile->fid.volatile_fid,
  3974. .pid = wdata->pid,
  3975. };
  3976. io_parms = &_io_parms;
  3977. rc = smb2_plain_req_init(SMB2_WRITE, tcon, server,
  3978. (void **) &req, &total_len);
  3979. if (rc)
  3980. return rc;
  3981. if (smb3_encryption_required(tcon))
  3982. flags |= CIFS_TRANSFORM_REQ;
  3983. shdr = (struct smb2_hdr *)req;
  3984. shdr->Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
  3985. req->PersistentFileId = io_parms->persistent_fid;
  3986. req->VolatileFileId = io_parms->volatile_fid;
  3987. req->WriteChannelInfoOffset = 0;
  3988. req->WriteChannelInfoLength = 0;
  3989. req->Channel = 0;
  3990. req->Offset = cpu_to_le64(io_parms->offset);
  3991. req->DataOffset = cpu_to_le16(
  3992. offsetof(struct smb2_write_req, Buffer));
  3993. req->RemainingBytes = 0;
  3994. trace_smb3_write_enter(0 /* xid */,
  3995. io_parms->persistent_fid,
  3996. io_parms->tcon->tid,
  3997. io_parms->tcon->ses->Suid,
  3998. io_parms->offset,
  3999. io_parms->length);
  4000. #ifdef CONFIG_CIFS_SMB_DIRECT
  4001. /*
  4002. * If we want to do a server RDMA read, fill in and append
  4003. * smbd_buffer_descriptor_v1 to the end of write request
  4004. */
  4005. if (smb3_use_rdma_offload(io_parms)) {
  4006. struct smbd_buffer_descriptor_v1 *v1;
  4007. bool need_invalidate = server->dialect == SMB30_PROT_ID;
  4008. wdata->mr = smbd_register_mr(
  4009. server->smbd_conn, wdata->pages,
  4010. wdata->nr_pages, wdata->page_offset,
  4011. wdata->tailsz, false, need_invalidate);
  4012. if (!wdata->mr) {
  4013. rc = -EAGAIN;
  4014. goto async_writev_out;
  4015. }
  4016. req->Length = 0;
  4017. req->DataOffset = 0;
  4018. if (wdata->nr_pages > 1)
  4019. req->RemainingBytes =
  4020. cpu_to_le32(
  4021. (wdata->nr_pages - 1) * wdata->pagesz -
  4022. wdata->page_offset + wdata->tailsz
  4023. );
  4024. else
  4025. req->RemainingBytes = cpu_to_le32(wdata->tailsz);
  4026. req->Channel = SMB2_CHANNEL_RDMA_V1_INVALIDATE;
  4027. if (need_invalidate)
  4028. req->Channel = SMB2_CHANNEL_RDMA_V1;
  4029. req->WriteChannelInfoOffset =
  4030. cpu_to_le16(offsetof(struct smb2_write_req, Buffer));
  4031. req->WriteChannelInfoLength =
  4032. cpu_to_le16(sizeof(struct smbd_buffer_descriptor_v1));
  4033. v1 = (struct smbd_buffer_descriptor_v1 *) &req->Buffer[0];
  4034. v1->offset = cpu_to_le64(wdata->mr->mr->iova);
  4035. v1->token = cpu_to_le32(wdata->mr->mr->rkey);
  4036. v1->length = cpu_to_le32(wdata->mr->mr->length);
  4037. }
  4038. #endif
  4039. iov[0].iov_len = total_len - 1;
  4040. iov[0].iov_base = (char *)req;
  4041. rqst.rq_iov = iov;
  4042. rqst.rq_nvec = 1;
  4043. rqst.rq_pages = wdata->pages;
  4044. rqst.rq_offset = wdata->page_offset;
  4045. rqst.rq_npages = wdata->nr_pages;
  4046. rqst.rq_pagesz = wdata->pagesz;
  4047. rqst.rq_tailsz = wdata->tailsz;
  4048. #ifdef CONFIG_CIFS_SMB_DIRECT
  4049. if (wdata->mr) {
  4050. iov[0].iov_len += sizeof(struct smbd_buffer_descriptor_v1);
  4051. rqst.rq_npages = 0;
  4052. }
  4053. #endif
  4054. cifs_dbg(FYI, "async write at %llu %u bytes\n",
  4055. io_parms->offset, io_parms->length);
  4056. #ifdef CONFIG_CIFS_SMB_DIRECT
  4057. /* For RDMA read, I/O size is in RemainingBytes not in Length */
  4058. if (!wdata->mr)
  4059. req->Length = cpu_to_le32(io_parms->length);
  4060. #else
  4061. req->Length = cpu_to_le32(io_parms->length);
  4062. #endif
  4063. if (wdata->credits.value > 0) {
  4064. shdr->CreditCharge = cpu_to_le16(DIV_ROUND_UP(wdata->bytes,
  4065. SMB2_MAX_BUFFER_SIZE));
  4066. credit_request = le16_to_cpu(shdr->CreditCharge) + 8;
  4067. if (server->credits >= server->max_credits)
  4068. shdr->CreditRequest = cpu_to_le16(0);
  4069. else
  4070. shdr->CreditRequest = cpu_to_le16(
  4071. min_t(int, server->max_credits -
  4072. server->credits, credit_request));
  4073. rc = adjust_credits(server, &wdata->credits, io_parms->length);
  4074. if (rc)
  4075. goto async_writev_out;
  4076. flags |= CIFS_HAS_CREDITS;
  4077. }
  4078. kref_get(&wdata->refcount);
  4079. rc = cifs_call_async(server, &rqst, NULL, smb2_writev_callback, NULL,
  4080. wdata, flags, &wdata->credits);
  4081. if (rc) {
  4082. trace_smb3_write_err(0 /* no xid */,
  4083. io_parms->persistent_fid,
  4084. io_parms->tcon->tid,
  4085. io_parms->tcon->ses->Suid,
  4086. io_parms->offset,
  4087. io_parms->length,
  4088. rc);
  4089. kref_put(&wdata->refcount, release);
  4090. cifs_stats_fail_inc(tcon, SMB2_WRITE_HE);
  4091. }
  4092. async_writev_out:
  4093. cifs_small_buf_release(req);
  4094. return rc;
  4095. }
  4096. /*
  4097. * SMB2_write function gets iov pointer to kvec array with n_vec as a length.
  4098. * The length field from io_parms must be at least 1 and indicates a number of
  4099. * elements with data to write that begins with position 1 in iov array. All
  4100. * data length is specified by count.
  4101. */
  4102. int
  4103. SMB2_write(const unsigned int xid, struct cifs_io_parms *io_parms,
  4104. unsigned int *nbytes, struct kvec *iov, int n_vec)
  4105. {
  4106. struct smb_rqst rqst;
  4107. int rc = 0;
  4108. struct smb2_write_req *req = NULL;
  4109. struct smb2_write_rsp *rsp = NULL;
  4110. int resp_buftype;
  4111. struct kvec rsp_iov;
  4112. int flags = 0;
  4113. unsigned int total_len;
  4114. struct TCP_Server_Info *server;
  4115. *nbytes = 0;
  4116. if (n_vec < 1)
  4117. return rc;
  4118. if (!io_parms->server)
  4119. io_parms->server = cifs_pick_channel(io_parms->tcon->ses);
  4120. server = io_parms->server;
  4121. if (server == NULL)
  4122. return -ECONNABORTED;
  4123. rc = smb2_plain_req_init(SMB2_WRITE, io_parms->tcon, server,
  4124. (void **) &req, &total_len);
  4125. if (rc)
  4126. return rc;
  4127. if (smb3_encryption_required(io_parms->tcon))
  4128. flags |= CIFS_TRANSFORM_REQ;
  4129. req->hdr.Id.SyncId.ProcessId = cpu_to_le32(io_parms->pid);
  4130. req->PersistentFileId = io_parms->persistent_fid;
  4131. req->VolatileFileId = io_parms->volatile_fid;
  4132. req->WriteChannelInfoOffset = 0;
  4133. req->WriteChannelInfoLength = 0;
  4134. req->Channel = 0;
  4135. req->Length = cpu_to_le32(io_parms->length);
  4136. req->Offset = cpu_to_le64(io_parms->offset);
  4137. req->DataOffset = cpu_to_le16(
  4138. offsetof(struct smb2_write_req, Buffer));
  4139. req->RemainingBytes = 0;
  4140. trace_smb3_write_enter(xid, io_parms->persistent_fid,
  4141. io_parms->tcon->tid, io_parms->tcon->ses->Suid,
  4142. io_parms->offset, io_parms->length);
  4143. iov[0].iov_base = (char *)req;
  4144. /* 1 for Buffer */
  4145. iov[0].iov_len = total_len - 1;
  4146. memset(&rqst, 0, sizeof(struct smb_rqst));
  4147. rqst.rq_iov = iov;
  4148. rqst.rq_nvec = n_vec + 1;
  4149. rc = cifs_send_recv(xid, io_parms->tcon->ses, server,
  4150. &rqst,
  4151. &resp_buftype, flags, &rsp_iov);
  4152. rsp = (struct smb2_write_rsp *)rsp_iov.iov_base;
  4153. if (rc) {
  4154. trace_smb3_write_err(xid,
  4155. req->PersistentFileId,
  4156. io_parms->tcon->tid,
  4157. io_parms->tcon->ses->Suid,
  4158. io_parms->offset, io_parms->length, rc);
  4159. cifs_stats_fail_inc(io_parms->tcon, SMB2_WRITE_HE);
  4160. cifs_dbg(VFS, "Send error in write = %d\n", rc);
  4161. } else {
  4162. *nbytes = le32_to_cpu(rsp->DataLength);
  4163. trace_smb3_write_done(xid,
  4164. req->PersistentFileId,
  4165. io_parms->tcon->tid,
  4166. io_parms->tcon->ses->Suid,
  4167. io_parms->offset, *nbytes);
  4168. }
  4169. cifs_small_buf_release(req);
  4170. free_rsp_buf(resp_buftype, rsp);
  4171. return rc;
  4172. }
  4173. int posix_info_sid_size(const void *beg, const void *end)
  4174. {
  4175. size_t subauth;
  4176. int total;
  4177. if (beg + 1 > end)
  4178. return -1;
  4179. subauth = *(u8 *)(beg+1);
  4180. if (subauth < 1 || subauth > 15)
  4181. return -1;
  4182. total = 1 + 1 + 6 + 4*subauth;
  4183. if (beg + total > end)
  4184. return -1;
  4185. return total;
  4186. }
  4187. int posix_info_parse(const void *beg, const void *end,
  4188. struct smb2_posix_info_parsed *out)
  4189. {
  4190. int total_len = 0;
  4191. int owner_len, group_len;
  4192. int name_len;
  4193. const void *owner_sid;
  4194. const void *group_sid;
  4195. const void *name;
  4196. /* if no end bound given, assume payload to be correct */
  4197. if (!end) {
  4198. const struct smb2_posix_info *p = beg;
  4199. end = beg + le32_to_cpu(p->NextEntryOffset);
  4200. /* last element will have a 0 offset, pick a sensible bound */
  4201. if (end == beg)
  4202. end += 0xFFFF;
  4203. }
  4204. /* check base buf */
  4205. if (beg + sizeof(struct smb2_posix_info) > end)
  4206. return -1;
  4207. total_len = sizeof(struct smb2_posix_info);
  4208. /* check owner sid */
  4209. owner_sid = beg + total_len;
  4210. owner_len = posix_info_sid_size(owner_sid, end);
  4211. if (owner_len < 0)
  4212. return -1;
  4213. total_len += owner_len;
  4214. /* check group sid */
  4215. group_sid = beg + total_len;
  4216. group_len = posix_info_sid_size(group_sid, end);
  4217. if (group_len < 0)
  4218. return -1;
  4219. total_len += group_len;
  4220. /* check name len */
  4221. if (beg + total_len + 4 > end)
  4222. return -1;
  4223. name_len = le32_to_cpu(*(__le32 *)(beg + total_len));
  4224. if (name_len < 1 || name_len > 0xFFFF)
  4225. return -1;
  4226. total_len += 4;
  4227. /* check name */
  4228. name = beg + total_len;
  4229. if (name + name_len > end)
  4230. return -1;
  4231. total_len += name_len;
  4232. if (out) {
  4233. out->base = beg;
  4234. out->size = total_len;
  4235. out->name_len = name_len;
  4236. out->name = name;
  4237. memcpy(&out->owner, owner_sid, owner_len);
  4238. memcpy(&out->group, group_sid, group_len);
  4239. }
  4240. return total_len;
  4241. }
  4242. static int posix_info_extra_size(const void *beg, const void *end)
  4243. {
  4244. int len = posix_info_parse(beg, end, NULL);
  4245. if (len < 0)
  4246. return -1;
  4247. return len - sizeof(struct smb2_posix_info);
  4248. }
  4249. static unsigned int
  4250. num_entries(int infotype, char *bufstart, char *end_of_buf, char **lastentry,
  4251. size_t size)
  4252. {
  4253. int len;
  4254. unsigned int entrycount = 0;
  4255. unsigned int next_offset = 0;
  4256. char *entryptr;
  4257. FILE_DIRECTORY_INFO *dir_info;
  4258. if (bufstart == NULL)
  4259. return 0;
  4260. entryptr = bufstart;
  4261. while (1) {
  4262. if (entryptr + next_offset < entryptr ||
  4263. entryptr + next_offset > end_of_buf ||
  4264. entryptr + next_offset + size > end_of_buf) {
  4265. cifs_dbg(VFS, "malformed search entry would overflow\n");
  4266. break;
  4267. }
  4268. entryptr = entryptr + next_offset;
  4269. dir_info = (FILE_DIRECTORY_INFO *)entryptr;
  4270. if (infotype == SMB_FIND_FILE_POSIX_INFO)
  4271. len = posix_info_extra_size(entryptr, end_of_buf);
  4272. else
  4273. len = le32_to_cpu(dir_info->FileNameLength);
  4274. if (len < 0 ||
  4275. entryptr + len < entryptr ||
  4276. entryptr + len > end_of_buf ||
  4277. entryptr + len + size > end_of_buf) {
  4278. cifs_dbg(VFS, "directory entry name would overflow frame end of buf %p\n",
  4279. end_of_buf);
  4280. break;
  4281. }
  4282. *lastentry = entryptr;
  4283. entrycount++;
  4284. next_offset = le32_to_cpu(dir_info->NextEntryOffset);
  4285. if (!next_offset)
  4286. break;
  4287. }
  4288. return entrycount;
  4289. }
  4290. /*
  4291. * Readdir/FindFirst
  4292. */
  4293. int SMB2_query_directory_init(const unsigned int xid,
  4294. struct cifs_tcon *tcon,
  4295. struct TCP_Server_Info *server,
  4296. struct smb_rqst *rqst,
  4297. u64 persistent_fid, u64 volatile_fid,
  4298. int index, int info_level)
  4299. {
  4300. struct smb2_query_directory_req *req;
  4301. unsigned char *bufptr;
  4302. __le16 asteriks = cpu_to_le16('*');
  4303. unsigned int output_size = CIFSMaxBufSize -
  4304. MAX_SMB2_CREATE_RESPONSE_SIZE -
  4305. MAX_SMB2_CLOSE_RESPONSE_SIZE;
  4306. unsigned int total_len;
  4307. struct kvec *iov = rqst->rq_iov;
  4308. int len, rc;
  4309. rc = smb2_plain_req_init(SMB2_QUERY_DIRECTORY, tcon, server,
  4310. (void **) &req, &total_len);
  4311. if (rc)
  4312. return rc;
  4313. switch (info_level) {
  4314. case SMB_FIND_FILE_DIRECTORY_INFO:
  4315. req->FileInformationClass = FILE_DIRECTORY_INFORMATION;
  4316. break;
  4317. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  4318. req->FileInformationClass = FILEID_FULL_DIRECTORY_INFORMATION;
  4319. break;
  4320. case SMB_FIND_FILE_POSIX_INFO:
  4321. req->FileInformationClass = SMB_FIND_FILE_POSIX_INFO;
  4322. break;
  4323. default:
  4324. cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
  4325. info_level);
  4326. return -EINVAL;
  4327. }
  4328. req->FileIndex = cpu_to_le32(index);
  4329. req->PersistentFileId = persistent_fid;
  4330. req->VolatileFileId = volatile_fid;
  4331. len = 0x2;
  4332. bufptr = req->Buffer;
  4333. memcpy(bufptr, &asteriks, len);
  4334. req->FileNameOffset =
  4335. cpu_to_le16(sizeof(struct smb2_query_directory_req) - 1);
  4336. req->FileNameLength = cpu_to_le16(len);
  4337. /*
  4338. * BB could be 30 bytes or so longer if we used SMB2 specific
  4339. * buffer lengths, but this is safe and close enough.
  4340. */
  4341. output_size = min_t(unsigned int, output_size, server->maxBuf);
  4342. output_size = min_t(unsigned int, output_size, 2 << 15);
  4343. req->OutputBufferLength = cpu_to_le32(output_size);
  4344. iov[0].iov_base = (char *)req;
  4345. /* 1 for Buffer */
  4346. iov[0].iov_len = total_len - 1;
  4347. iov[1].iov_base = (char *)(req->Buffer);
  4348. iov[1].iov_len = len;
  4349. trace_smb3_query_dir_enter(xid, persistent_fid, tcon->tid,
  4350. tcon->ses->Suid, index, output_size);
  4351. return 0;
  4352. }
  4353. void SMB2_query_directory_free(struct smb_rqst *rqst)
  4354. {
  4355. if (rqst && rqst->rq_iov) {
  4356. cifs_small_buf_release(rqst->rq_iov[0].iov_base); /* request */
  4357. }
  4358. }
  4359. int
  4360. smb2_parse_query_directory(struct cifs_tcon *tcon,
  4361. struct kvec *rsp_iov,
  4362. int resp_buftype,
  4363. struct cifs_search_info *srch_inf)
  4364. {
  4365. struct smb2_query_directory_rsp *rsp;
  4366. size_t info_buf_size;
  4367. char *end_of_smb;
  4368. int rc;
  4369. rsp = (struct smb2_query_directory_rsp *)rsp_iov->iov_base;
  4370. switch (srch_inf->info_level) {
  4371. case SMB_FIND_FILE_DIRECTORY_INFO:
  4372. info_buf_size = sizeof(FILE_DIRECTORY_INFO) - 1;
  4373. break;
  4374. case SMB_FIND_FILE_ID_FULL_DIR_INFO:
  4375. info_buf_size = sizeof(SEARCH_ID_FULL_DIR_INFO) - 1;
  4376. break;
  4377. case SMB_FIND_FILE_POSIX_INFO:
  4378. /* note that posix payload are variable size */
  4379. info_buf_size = sizeof(struct smb2_posix_info);
  4380. break;
  4381. default:
  4382. cifs_tcon_dbg(VFS, "info level %u isn't supported\n",
  4383. srch_inf->info_level);
  4384. return -EINVAL;
  4385. }
  4386. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  4387. le32_to_cpu(rsp->OutputBufferLength), rsp_iov,
  4388. info_buf_size);
  4389. if (rc) {
  4390. cifs_tcon_dbg(VFS, "bad info payload");
  4391. return rc;
  4392. }
  4393. srch_inf->unicode = true;
  4394. if (srch_inf->ntwrk_buf_start) {
  4395. if (srch_inf->smallBuf)
  4396. cifs_small_buf_release(srch_inf->ntwrk_buf_start);
  4397. else
  4398. cifs_buf_release(srch_inf->ntwrk_buf_start);
  4399. }
  4400. srch_inf->ntwrk_buf_start = (char *)rsp;
  4401. srch_inf->srch_entries_start = srch_inf->last_entry =
  4402. (char *)rsp + le16_to_cpu(rsp->OutputBufferOffset);
  4403. end_of_smb = rsp_iov->iov_len + (char *)rsp;
  4404. srch_inf->entries_in_buffer = num_entries(
  4405. srch_inf->info_level,
  4406. srch_inf->srch_entries_start,
  4407. end_of_smb,
  4408. &srch_inf->last_entry,
  4409. info_buf_size);
  4410. srch_inf->index_of_last_entry += srch_inf->entries_in_buffer;
  4411. cifs_dbg(FYI, "num entries %d last_index %lld srch start %p srch end %p\n",
  4412. srch_inf->entries_in_buffer, srch_inf->index_of_last_entry,
  4413. srch_inf->srch_entries_start, srch_inf->last_entry);
  4414. if (resp_buftype == CIFS_LARGE_BUFFER)
  4415. srch_inf->smallBuf = false;
  4416. else if (resp_buftype == CIFS_SMALL_BUFFER)
  4417. srch_inf->smallBuf = true;
  4418. else
  4419. cifs_tcon_dbg(VFS, "Invalid search buffer type\n");
  4420. return 0;
  4421. }
  4422. int
  4423. SMB2_query_directory(const unsigned int xid, struct cifs_tcon *tcon,
  4424. u64 persistent_fid, u64 volatile_fid, int index,
  4425. struct cifs_search_info *srch_inf)
  4426. {
  4427. struct smb_rqst rqst;
  4428. struct kvec iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
  4429. struct smb2_query_directory_rsp *rsp = NULL;
  4430. int resp_buftype = CIFS_NO_BUFFER;
  4431. struct kvec rsp_iov;
  4432. int rc = 0;
  4433. struct cifs_ses *ses = tcon->ses;
  4434. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4435. int flags = 0;
  4436. if (!ses || !(ses->server))
  4437. return -EIO;
  4438. if (smb3_encryption_required(tcon))
  4439. flags |= CIFS_TRANSFORM_REQ;
  4440. memset(&rqst, 0, sizeof(struct smb_rqst));
  4441. memset(&iov, 0, sizeof(iov));
  4442. rqst.rq_iov = iov;
  4443. rqst.rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
  4444. rc = SMB2_query_directory_init(xid, tcon, server,
  4445. &rqst, persistent_fid,
  4446. volatile_fid, index,
  4447. srch_inf->info_level);
  4448. if (rc)
  4449. goto qdir_exit;
  4450. rc = cifs_send_recv(xid, ses, server,
  4451. &rqst, &resp_buftype, flags, &rsp_iov);
  4452. rsp = (struct smb2_query_directory_rsp *)rsp_iov.iov_base;
  4453. if (rc) {
  4454. if (rc == -ENODATA &&
  4455. rsp->hdr.Status == STATUS_NO_MORE_FILES) {
  4456. trace_smb3_query_dir_done(xid, persistent_fid,
  4457. tcon->tid, tcon->ses->Suid, index, 0);
  4458. srch_inf->endOfSearch = true;
  4459. rc = 0;
  4460. } else {
  4461. trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
  4462. tcon->ses->Suid, index, 0, rc);
  4463. cifs_stats_fail_inc(tcon, SMB2_QUERY_DIRECTORY_HE);
  4464. }
  4465. goto qdir_exit;
  4466. }
  4467. rc = smb2_parse_query_directory(tcon, &rsp_iov, resp_buftype,
  4468. srch_inf);
  4469. if (rc) {
  4470. trace_smb3_query_dir_err(xid, persistent_fid, tcon->tid,
  4471. tcon->ses->Suid, index, 0, rc);
  4472. goto qdir_exit;
  4473. }
  4474. resp_buftype = CIFS_NO_BUFFER;
  4475. trace_smb3_query_dir_done(xid, persistent_fid, tcon->tid,
  4476. tcon->ses->Suid, index, srch_inf->entries_in_buffer);
  4477. qdir_exit:
  4478. SMB2_query_directory_free(&rqst);
  4479. free_rsp_buf(resp_buftype, rsp);
  4480. return rc;
  4481. }
  4482. int
  4483. SMB2_set_info_init(struct cifs_tcon *tcon, struct TCP_Server_Info *server,
  4484. struct smb_rqst *rqst,
  4485. u64 persistent_fid, u64 volatile_fid, u32 pid,
  4486. u8 info_class, u8 info_type, u32 additional_info,
  4487. void **data, unsigned int *size)
  4488. {
  4489. struct smb2_set_info_req *req;
  4490. struct kvec *iov = rqst->rq_iov;
  4491. unsigned int i, total_len;
  4492. int rc;
  4493. rc = smb2_plain_req_init(SMB2_SET_INFO, tcon, server,
  4494. (void **) &req, &total_len);
  4495. if (rc)
  4496. return rc;
  4497. req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
  4498. req->InfoType = info_type;
  4499. req->FileInfoClass = info_class;
  4500. req->PersistentFileId = persistent_fid;
  4501. req->VolatileFileId = volatile_fid;
  4502. req->AdditionalInformation = cpu_to_le32(additional_info);
  4503. req->BufferOffset =
  4504. cpu_to_le16(sizeof(struct smb2_set_info_req) - 1);
  4505. req->BufferLength = cpu_to_le32(*size);
  4506. memcpy(req->Buffer, *data, *size);
  4507. total_len += *size;
  4508. iov[0].iov_base = (char *)req;
  4509. /* 1 for Buffer */
  4510. iov[0].iov_len = total_len - 1;
  4511. for (i = 1; i < rqst->rq_nvec; i++) {
  4512. le32_add_cpu(&req->BufferLength, size[i]);
  4513. iov[i].iov_base = (char *)data[i];
  4514. iov[i].iov_len = size[i];
  4515. }
  4516. return 0;
  4517. }
  4518. void
  4519. SMB2_set_info_free(struct smb_rqst *rqst)
  4520. {
  4521. if (rqst && rqst->rq_iov)
  4522. cifs_buf_release(rqst->rq_iov[0].iov_base); /* request */
  4523. }
  4524. static int
  4525. send_set_info(const unsigned int xid, struct cifs_tcon *tcon,
  4526. u64 persistent_fid, u64 volatile_fid, u32 pid, u8 info_class,
  4527. u8 info_type, u32 additional_info, unsigned int num,
  4528. void **data, unsigned int *size)
  4529. {
  4530. struct smb_rqst rqst;
  4531. struct smb2_set_info_rsp *rsp = NULL;
  4532. struct kvec *iov;
  4533. struct kvec rsp_iov;
  4534. int rc = 0;
  4535. int resp_buftype;
  4536. struct cifs_ses *ses = tcon->ses;
  4537. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4538. int flags = 0;
  4539. if (!ses || !server)
  4540. return -EIO;
  4541. if (!num)
  4542. return -EINVAL;
  4543. if (smb3_encryption_required(tcon))
  4544. flags |= CIFS_TRANSFORM_REQ;
  4545. iov = kmalloc_array(num, sizeof(struct kvec), GFP_KERNEL);
  4546. if (!iov)
  4547. return -ENOMEM;
  4548. memset(&rqst, 0, sizeof(struct smb_rqst));
  4549. rqst.rq_iov = iov;
  4550. rqst.rq_nvec = num;
  4551. rc = SMB2_set_info_init(tcon, server,
  4552. &rqst, persistent_fid, volatile_fid, pid,
  4553. info_class, info_type, additional_info,
  4554. data, size);
  4555. if (rc) {
  4556. kfree(iov);
  4557. return rc;
  4558. }
  4559. rc = cifs_send_recv(xid, ses, server,
  4560. &rqst, &resp_buftype, flags,
  4561. &rsp_iov);
  4562. SMB2_set_info_free(&rqst);
  4563. rsp = (struct smb2_set_info_rsp *)rsp_iov.iov_base;
  4564. if (rc != 0) {
  4565. cifs_stats_fail_inc(tcon, SMB2_SET_INFO_HE);
  4566. trace_smb3_set_info_err(xid, persistent_fid, tcon->tid,
  4567. ses->Suid, info_class, (__u32)info_type, rc);
  4568. }
  4569. free_rsp_buf(resp_buftype, rsp);
  4570. kfree(iov);
  4571. return rc;
  4572. }
  4573. int
  4574. SMB2_set_eof(const unsigned int xid, struct cifs_tcon *tcon, u64 persistent_fid,
  4575. u64 volatile_fid, u32 pid, __le64 *eof)
  4576. {
  4577. struct smb2_file_eof_info info;
  4578. void *data;
  4579. unsigned int size;
  4580. info.EndOfFile = *eof;
  4581. data = &info;
  4582. size = sizeof(struct smb2_file_eof_info);
  4583. trace_smb3_set_eof(xid, persistent_fid, tcon->tid, tcon->ses->Suid, le64_to_cpu(*eof));
  4584. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  4585. pid, FILE_END_OF_FILE_INFORMATION, SMB2_O_INFO_FILE,
  4586. 0, 1, &data, &size);
  4587. }
  4588. int
  4589. SMB2_set_acl(const unsigned int xid, struct cifs_tcon *tcon,
  4590. u64 persistent_fid, u64 volatile_fid,
  4591. struct cifs_ntsd *pnntsd, int pacllen, int aclflag)
  4592. {
  4593. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  4594. current->tgid, 0, SMB2_O_INFO_SECURITY, aclflag,
  4595. 1, (void **)&pnntsd, &pacllen);
  4596. }
  4597. int
  4598. SMB2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
  4599. u64 persistent_fid, u64 volatile_fid,
  4600. struct smb2_file_full_ea_info *buf, int len)
  4601. {
  4602. return send_set_info(xid, tcon, persistent_fid, volatile_fid,
  4603. current->tgid, FILE_FULL_EA_INFORMATION, SMB2_O_INFO_FILE,
  4604. 0, 1, (void **)&buf, &len);
  4605. }
  4606. int
  4607. SMB2_oplock_break(const unsigned int xid, struct cifs_tcon *tcon,
  4608. const u64 persistent_fid, const u64 volatile_fid,
  4609. __u8 oplock_level)
  4610. {
  4611. struct smb_rqst rqst;
  4612. int rc;
  4613. struct smb2_oplock_break *req = NULL;
  4614. struct cifs_ses *ses = tcon->ses;
  4615. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4616. int flags = CIFS_OBREAK_OP;
  4617. unsigned int total_len;
  4618. struct kvec iov[1];
  4619. struct kvec rsp_iov;
  4620. int resp_buf_type;
  4621. cifs_dbg(FYI, "SMB2_oplock_break\n");
  4622. rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
  4623. (void **) &req, &total_len);
  4624. if (rc)
  4625. return rc;
  4626. if (smb3_encryption_required(tcon))
  4627. flags |= CIFS_TRANSFORM_REQ;
  4628. req->VolatileFid = volatile_fid;
  4629. req->PersistentFid = persistent_fid;
  4630. req->OplockLevel = oplock_level;
  4631. req->hdr.CreditRequest = cpu_to_le16(1);
  4632. flags |= CIFS_NO_RSP_BUF;
  4633. iov[0].iov_base = (char *)req;
  4634. iov[0].iov_len = total_len;
  4635. memset(&rqst, 0, sizeof(struct smb_rqst));
  4636. rqst.rq_iov = iov;
  4637. rqst.rq_nvec = 1;
  4638. rc = cifs_send_recv(xid, ses, server,
  4639. &rqst, &resp_buf_type, flags, &rsp_iov);
  4640. cifs_small_buf_release(req);
  4641. if (rc) {
  4642. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  4643. cifs_dbg(FYI, "Send error in Oplock Break = %d\n", rc);
  4644. }
  4645. return rc;
  4646. }
  4647. void
  4648. smb2_copy_fs_info_to_kstatfs(struct smb2_fs_full_size_info *pfs_inf,
  4649. struct kstatfs *kst)
  4650. {
  4651. kst->f_bsize = le32_to_cpu(pfs_inf->BytesPerSector) *
  4652. le32_to_cpu(pfs_inf->SectorsPerAllocationUnit);
  4653. kst->f_blocks = le64_to_cpu(pfs_inf->TotalAllocationUnits);
  4654. kst->f_bfree = kst->f_bavail =
  4655. le64_to_cpu(pfs_inf->CallerAvailableAllocationUnits);
  4656. return;
  4657. }
  4658. static void
  4659. copy_posix_fs_info_to_kstatfs(FILE_SYSTEM_POSIX_INFO *response_data,
  4660. struct kstatfs *kst)
  4661. {
  4662. kst->f_bsize = le32_to_cpu(response_data->BlockSize);
  4663. kst->f_blocks = le64_to_cpu(response_data->TotalBlocks);
  4664. kst->f_bfree = le64_to_cpu(response_data->BlocksAvail);
  4665. if (response_data->UserBlocksAvail == cpu_to_le64(-1))
  4666. kst->f_bavail = kst->f_bfree;
  4667. else
  4668. kst->f_bavail = le64_to_cpu(response_data->UserBlocksAvail);
  4669. if (response_data->TotalFileNodes != cpu_to_le64(-1))
  4670. kst->f_files = le64_to_cpu(response_data->TotalFileNodes);
  4671. if (response_data->FreeFileNodes != cpu_to_le64(-1))
  4672. kst->f_ffree = le64_to_cpu(response_data->FreeFileNodes);
  4673. return;
  4674. }
  4675. static int
  4676. build_qfs_info_req(struct kvec *iov, struct cifs_tcon *tcon,
  4677. struct TCP_Server_Info *server,
  4678. int level, int outbuf_len, u64 persistent_fid,
  4679. u64 volatile_fid)
  4680. {
  4681. int rc;
  4682. struct smb2_query_info_req *req;
  4683. unsigned int total_len;
  4684. cifs_dbg(FYI, "Query FSInfo level %d\n", level);
  4685. if ((tcon->ses == NULL) || server == NULL)
  4686. return -EIO;
  4687. rc = smb2_plain_req_init(SMB2_QUERY_INFO, tcon, server,
  4688. (void **) &req, &total_len);
  4689. if (rc)
  4690. return rc;
  4691. req->InfoType = SMB2_O_INFO_FILESYSTEM;
  4692. req->FileInfoClass = level;
  4693. req->PersistentFileId = persistent_fid;
  4694. req->VolatileFileId = volatile_fid;
  4695. /* 1 for pad */
  4696. req->InputBufferOffset =
  4697. cpu_to_le16(sizeof(struct smb2_query_info_req) - 1);
  4698. req->OutputBufferLength = cpu_to_le32(
  4699. outbuf_len + sizeof(struct smb2_query_info_rsp) - 1);
  4700. iov->iov_base = (char *)req;
  4701. iov->iov_len = total_len;
  4702. return 0;
  4703. }
  4704. int
  4705. SMB311_posix_qfs_info(const unsigned int xid, struct cifs_tcon *tcon,
  4706. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  4707. {
  4708. struct smb_rqst rqst;
  4709. struct smb2_query_info_rsp *rsp = NULL;
  4710. struct kvec iov;
  4711. struct kvec rsp_iov;
  4712. int rc = 0;
  4713. int resp_buftype;
  4714. struct cifs_ses *ses = tcon->ses;
  4715. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4716. FILE_SYSTEM_POSIX_INFO *info = NULL;
  4717. int flags = 0;
  4718. rc = build_qfs_info_req(&iov, tcon, server,
  4719. FS_POSIX_INFORMATION,
  4720. sizeof(FILE_SYSTEM_POSIX_INFO),
  4721. persistent_fid, volatile_fid);
  4722. if (rc)
  4723. return rc;
  4724. if (smb3_encryption_required(tcon))
  4725. flags |= CIFS_TRANSFORM_REQ;
  4726. memset(&rqst, 0, sizeof(struct smb_rqst));
  4727. rqst.rq_iov = &iov;
  4728. rqst.rq_nvec = 1;
  4729. rc = cifs_send_recv(xid, ses, server,
  4730. &rqst, &resp_buftype, flags, &rsp_iov);
  4731. cifs_small_buf_release(iov.iov_base);
  4732. if (rc) {
  4733. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  4734. goto posix_qfsinf_exit;
  4735. }
  4736. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  4737. info = (FILE_SYSTEM_POSIX_INFO *)(
  4738. le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
  4739. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  4740. le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
  4741. sizeof(FILE_SYSTEM_POSIX_INFO));
  4742. if (!rc)
  4743. copy_posix_fs_info_to_kstatfs(info, fsdata);
  4744. posix_qfsinf_exit:
  4745. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  4746. return rc;
  4747. }
  4748. int
  4749. SMB2_QFS_info(const unsigned int xid, struct cifs_tcon *tcon,
  4750. u64 persistent_fid, u64 volatile_fid, struct kstatfs *fsdata)
  4751. {
  4752. struct smb_rqst rqst;
  4753. struct smb2_query_info_rsp *rsp = NULL;
  4754. struct kvec iov;
  4755. struct kvec rsp_iov;
  4756. int rc = 0;
  4757. int resp_buftype;
  4758. struct cifs_ses *ses = tcon->ses;
  4759. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4760. struct smb2_fs_full_size_info *info = NULL;
  4761. int flags = 0;
  4762. rc = build_qfs_info_req(&iov, tcon, server,
  4763. FS_FULL_SIZE_INFORMATION,
  4764. sizeof(struct smb2_fs_full_size_info),
  4765. persistent_fid, volatile_fid);
  4766. if (rc)
  4767. return rc;
  4768. if (smb3_encryption_required(tcon))
  4769. flags |= CIFS_TRANSFORM_REQ;
  4770. memset(&rqst, 0, sizeof(struct smb_rqst));
  4771. rqst.rq_iov = &iov;
  4772. rqst.rq_nvec = 1;
  4773. rc = cifs_send_recv(xid, ses, server,
  4774. &rqst, &resp_buftype, flags, &rsp_iov);
  4775. cifs_small_buf_release(iov.iov_base);
  4776. if (rc) {
  4777. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  4778. goto qfsinf_exit;
  4779. }
  4780. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  4781. info = (struct smb2_fs_full_size_info *)(
  4782. le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
  4783. rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
  4784. le32_to_cpu(rsp->OutputBufferLength), &rsp_iov,
  4785. sizeof(struct smb2_fs_full_size_info));
  4786. if (!rc)
  4787. smb2_copy_fs_info_to_kstatfs(info, fsdata);
  4788. qfsinf_exit:
  4789. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  4790. return rc;
  4791. }
  4792. int
  4793. SMB2_QFS_attr(const unsigned int xid, struct cifs_tcon *tcon,
  4794. u64 persistent_fid, u64 volatile_fid, int level)
  4795. {
  4796. struct smb_rqst rqst;
  4797. struct smb2_query_info_rsp *rsp = NULL;
  4798. struct kvec iov;
  4799. struct kvec rsp_iov;
  4800. int rc = 0;
  4801. int resp_buftype, max_len, min_len;
  4802. struct cifs_ses *ses = tcon->ses;
  4803. struct TCP_Server_Info *server = cifs_pick_channel(ses);
  4804. unsigned int rsp_len, offset;
  4805. int flags = 0;
  4806. if (level == FS_DEVICE_INFORMATION) {
  4807. max_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  4808. min_len = sizeof(FILE_SYSTEM_DEVICE_INFO);
  4809. } else if (level == FS_ATTRIBUTE_INFORMATION) {
  4810. max_len = sizeof(FILE_SYSTEM_ATTRIBUTE_INFO);
  4811. min_len = MIN_FS_ATTR_INFO_SIZE;
  4812. } else if (level == FS_SECTOR_SIZE_INFORMATION) {
  4813. max_len = sizeof(struct smb3_fs_ss_info);
  4814. min_len = sizeof(struct smb3_fs_ss_info);
  4815. } else if (level == FS_VOLUME_INFORMATION) {
  4816. max_len = sizeof(struct smb3_fs_vol_info) + MAX_VOL_LABEL_LEN;
  4817. min_len = sizeof(struct smb3_fs_vol_info);
  4818. } else {
  4819. cifs_dbg(FYI, "Invalid qfsinfo level %d\n", level);
  4820. return -EINVAL;
  4821. }
  4822. rc = build_qfs_info_req(&iov, tcon, server,
  4823. level, max_len,
  4824. persistent_fid, volatile_fid);
  4825. if (rc)
  4826. return rc;
  4827. if (smb3_encryption_required(tcon))
  4828. flags |= CIFS_TRANSFORM_REQ;
  4829. memset(&rqst, 0, sizeof(struct smb_rqst));
  4830. rqst.rq_iov = &iov;
  4831. rqst.rq_nvec = 1;
  4832. rc = cifs_send_recv(xid, ses, server,
  4833. &rqst, &resp_buftype, flags, &rsp_iov);
  4834. cifs_small_buf_release(iov.iov_base);
  4835. if (rc) {
  4836. cifs_stats_fail_inc(tcon, SMB2_QUERY_INFO_HE);
  4837. goto qfsattr_exit;
  4838. }
  4839. rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
  4840. rsp_len = le32_to_cpu(rsp->OutputBufferLength);
  4841. offset = le16_to_cpu(rsp->OutputBufferOffset);
  4842. rc = smb2_validate_iov(offset, rsp_len, &rsp_iov, min_len);
  4843. if (rc)
  4844. goto qfsattr_exit;
  4845. if (level == FS_ATTRIBUTE_INFORMATION)
  4846. memcpy(&tcon->fsAttrInfo, offset
  4847. + (char *)rsp, min_t(unsigned int,
  4848. rsp_len, max_len));
  4849. else if (level == FS_DEVICE_INFORMATION)
  4850. memcpy(&tcon->fsDevInfo, offset
  4851. + (char *)rsp, sizeof(FILE_SYSTEM_DEVICE_INFO));
  4852. else if (level == FS_SECTOR_SIZE_INFORMATION) {
  4853. struct smb3_fs_ss_info *ss_info = (struct smb3_fs_ss_info *)
  4854. (offset + (char *)rsp);
  4855. tcon->ss_flags = le32_to_cpu(ss_info->Flags);
  4856. tcon->perf_sector_size =
  4857. le32_to_cpu(ss_info->PhysicalBytesPerSectorForPerf);
  4858. } else if (level == FS_VOLUME_INFORMATION) {
  4859. struct smb3_fs_vol_info *vol_info = (struct smb3_fs_vol_info *)
  4860. (offset + (char *)rsp);
  4861. tcon->vol_serial_number = vol_info->VolumeSerialNumber;
  4862. tcon->vol_create_time = vol_info->VolumeCreationTime;
  4863. }
  4864. qfsattr_exit:
  4865. free_rsp_buf(resp_buftype, rsp_iov.iov_base);
  4866. return rc;
  4867. }
  4868. int
  4869. smb2_lockv(const unsigned int xid, struct cifs_tcon *tcon,
  4870. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  4871. const __u32 num_lock, struct smb2_lock_element *buf)
  4872. {
  4873. struct smb_rqst rqst;
  4874. int rc = 0;
  4875. struct smb2_lock_req *req = NULL;
  4876. struct kvec iov[2];
  4877. struct kvec rsp_iov;
  4878. int resp_buf_type;
  4879. unsigned int count;
  4880. int flags = CIFS_NO_RSP_BUF;
  4881. unsigned int total_len;
  4882. struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
  4883. cifs_dbg(FYI, "smb2_lockv num lock %d\n", num_lock);
  4884. rc = smb2_plain_req_init(SMB2_LOCK, tcon, server,
  4885. (void **) &req, &total_len);
  4886. if (rc)
  4887. return rc;
  4888. if (smb3_encryption_required(tcon))
  4889. flags |= CIFS_TRANSFORM_REQ;
  4890. req->hdr.Id.SyncId.ProcessId = cpu_to_le32(pid);
  4891. req->LockCount = cpu_to_le16(num_lock);
  4892. req->PersistentFileId = persist_fid;
  4893. req->VolatileFileId = volatile_fid;
  4894. count = num_lock * sizeof(struct smb2_lock_element);
  4895. iov[0].iov_base = (char *)req;
  4896. iov[0].iov_len = total_len - sizeof(struct smb2_lock_element);
  4897. iov[1].iov_base = (char *)buf;
  4898. iov[1].iov_len = count;
  4899. cifs_stats_inc(&tcon->stats.cifs_stats.num_locks);
  4900. memset(&rqst, 0, sizeof(struct smb_rqst));
  4901. rqst.rq_iov = iov;
  4902. rqst.rq_nvec = 2;
  4903. rc = cifs_send_recv(xid, tcon->ses, server,
  4904. &rqst, &resp_buf_type, flags,
  4905. &rsp_iov);
  4906. cifs_small_buf_release(req);
  4907. if (rc) {
  4908. cifs_dbg(FYI, "Send error in smb2_lockv = %d\n", rc);
  4909. cifs_stats_fail_inc(tcon, SMB2_LOCK_HE);
  4910. trace_smb3_lock_err(xid, persist_fid, tcon->tid,
  4911. tcon->ses->Suid, rc);
  4912. }
  4913. return rc;
  4914. }
  4915. int
  4916. SMB2_lock(const unsigned int xid, struct cifs_tcon *tcon,
  4917. const __u64 persist_fid, const __u64 volatile_fid, const __u32 pid,
  4918. const __u64 length, const __u64 offset, const __u32 lock_flags,
  4919. const bool wait)
  4920. {
  4921. struct smb2_lock_element lock;
  4922. lock.Offset = cpu_to_le64(offset);
  4923. lock.Length = cpu_to_le64(length);
  4924. lock.Flags = cpu_to_le32(lock_flags);
  4925. if (!wait && lock_flags != SMB2_LOCKFLAG_UNLOCK)
  4926. lock.Flags |= cpu_to_le32(SMB2_LOCKFLAG_FAIL_IMMEDIATELY);
  4927. return smb2_lockv(xid, tcon, persist_fid, volatile_fid, pid, 1, &lock);
  4928. }
  4929. int
  4930. SMB2_lease_break(const unsigned int xid, struct cifs_tcon *tcon,
  4931. __u8 *lease_key, const __le32 lease_state)
  4932. {
  4933. struct smb_rqst rqst;
  4934. int rc;
  4935. struct smb2_lease_ack *req = NULL;
  4936. struct cifs_ses *ses = tcon->ses;
  4937. int flags = CIFS_OBREAK_OP;
  4938. unsigned int total_len;
  4939. struct kvec iov[1];
  4940. struct kvec rsp_iov;
  4941. int resp_buf_type;
  4942. __u64 *please_key_high;
  4943. __u64 *please_key_low;
  4944. struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
  4945. cifs_dbg(FYI, "SMB2_lease_break\n");
  4946. rc = smb2_plain_req_init(SMB2_OPLOCK_BREAK, tcon, server,
  4947. (void **) &req, &total_len);
  4948. if (rc)
  4949. return rc;
  4950. if (smb3_encryption_required(tcon))
  4951. flags |= CIFS_TRANSFORM_REQ;
  4952. req->hdr.CreditRequest = cpu_to_le16(1);
  4953. req->StructureSize = cpu_to_le16(36);
  4954. total_len += 12;
  4955. memcpy(req->LeaseKey, lease_key, 16);
  4956. req->LeaseState = lease_state;
  4957. flags |= CIFS_NO_RSP_BUF;
  4958. iov[0].iov_base = (char *)req;
  4959. iov[0].iov_len = total_len;
  4960. memset(&rqst, 0, sizeof(struct smb_rqst));
  4961. rqst.rq_iov = iov;
  4962. rqst.rq_nvec = 1;
  4963. rc = cifs_send_recv(xid, ses, server,
  4964. &rqst, &resp_buf_type, flags, &rsp_iov);
  4965. cifs_small_buf_release(req);
  4966. please_key_low = (__u64 *)lease_key;
  4967. please_key_high = (__u64 *)(lease_key+8);
  4968. if (rc) {
  4969. cifs_stats_fail_inc(tcon, SMB2_OPLOCK_BREAK_HE);
  4970. trace_smb3_lease_err(le32_to_cpu(lease_state), tcon->tid,
  4971. ses->Suid, *please_key_low, *please_key_high, rc);
  4972. cifs_dbg(FYI, "Send error in Lease Break = %d\n", rc);
  4973. } else
  4974. trace_smb3_lease_done(le32_to_cpu(lease_state), tcon->tid,
  4975. ses->Suid, *please_key_low, *please_key_high);
  4976. return rc;
  4977. }