skbuff.c 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines having to do with the 'struct sk_buff' memory handlers.
  4. *
  5. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  6. * Florian La Roche <rzsfl@rz.uni-sb.de>
  7. *
  8. * Fixes:
  9. * Alan Cox : Fixed the worst of the load
  10. * balancer bugs.
  11. * Dave Platt : Interrupt stacking fix.
  12. * Richard Kooijman : Timestamp fixes.
  13. * Alan Cox : Changed buffer format.
  14. * Alan Cox : destructor hook for AF_UNIX etc.
  15. * Linus Torvalds : Better skb_clone.
  16. * Alan Cox : Added skb_copy.
  17. * Alan Cox : Added all the changed routines Linus
  18. * only put in the headers
  19. * Ray VanTassle : Fixed --skb->lock in free
  20. * Alan Cox : skb_copy copy arp field
  21. * Andi Kleen : slabified it.
  22. * Robert Olsson : Removed skb_head_pool
  23. *
  24. * NOTE:
  25. * The __skb_ routines should be called with interrupts
  26. * disabled, or you better be *real* sure that the operation is atomic
  27. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  28. * or via disabling bottom half handlers, etc).
  29. */
  30. /*
  31. * The functions in this file will not compile correctly with gcc 2.4.x
  32. */
  33. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  34. #include <linux/module.h>
  35. #include <linux/types.h>
  36. #include <linux/kernel.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/in.h>
  40. #include <linux/inet.h>
  41. #include <linux/slab.h>
  42. #include <linux/tcp.h>
  43. #include <linux/udp.h>
  44. #include <linux/sctp.h>
  45. #include <linux/netdevice.h>
  46. #ifdef CONFIG_NET_CLS_ACT
  47. #include <net/pkt_sched.h>
  48. #endif
  49. #include <linux/string.h>
  50. #include <linux/skbuff.h>
  51. #include <linux/skbuff_ref.h>
  52. #include <linux/splice.h>
  53. #include <linux/cache.h>
  54. #include <linux/rtnetlink.h>
  55. #include <linux/init.h>
  56. #include <linux/scatterlist.h>
  57. #include <linux/errqueue.h>
  58. #include <linux/prefetch.h>
  59. #include <linux/bitfield.h>
  60. #include <linux/if_vlan.h>
  61. #include <linux/mpls.h>
  62. #include <linux/kcov.h>
  63. #include <linux/iov_iter.h>
  64. #include <net/protocol.h>
  65. #include <net/dst.h>
  66. #include <net/sock.h>
  67. #include <net/checksum.h>
  68. #include <net/gro.h>
  69. #include <net/gso.h>
  70. #include <net/hotdata.h>
  71. #include <net/ip6_checksum.h>
  72. #include <net/xfrm.h>
  73. #include <net/mpls.h>
  74. #include <net/mptcp.h>
  75. #include <net/mctp.h>
  76. #include <net/page_pool/helpers.h>
  77. #include <net/dropreason.h>
  78. #include <linux/uaccess.h>
  79. #include <trace/events/skb.h>
  80. #include <linux/highmem.h>
  81. #include <linux/capability.h>
  82. #include <linux/user_namespace.h>
  83. #include <linux/indirect_call_wrapper.h>
  84. #include <linux/textsearch.h>
  85. #include "dev.h"
  86. #include "netmem_priv.h"
  87. #include "sock_destructor.h"
  88. #ifdef CONFIG_SKB_EXTENSIONS
  89. static struct kmem_cache *skbuff_ext_cache __ro_after_init;
  90. #endif
  91. #define GRO_MAX_HEAD_PAD (GRO_MAX_HEAD + NET_SKB_PAD + NET_IP_ALIGN)
  92. #define SKB_SMALL_HEAD_SIZE SKB_HEAD_ALIGN(max(MAX_TCP_HEADER, \
  93. GRO_MAX_HEAD_PAD))
  94. /* We want SKB_SMALL_HEAD_CACHE_SIZE to not be a power of two.
  95. * This should ensure that SKB_SMALL_HEAD_HEADROOM is a unique
  96. * size, and we can differentiate heads from skb_small_head_cache
  97. * vs system slabs by looking at their size (skb_end_offset()).
  98. */
  99. #define SKB_SMALL_HEAD_CACHE_SIZE \
  100. (is_power_of_2(SKB_SMALL_HEAD_SIZE) ? \
  101. (SKB_SMALL_HEAD_SIZE + L1_CACHE_BYTES) : \
  102. SKB_SMALL_HEAD_SIZE)
  103. #define SKB_SMALL_HEAD_HEADROOM \
  104. SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)
  105. /* kcm_write_msgs() relies on casting paged frags to bio_vec to use
  106. * iov_iter_bvec(). These static asserts ensure the cast is valid is long as the
  107. * netmem is a page.
  108. */
  109. static_assert(offsetof(struct bio_vec, bv_page) ==
  110. offsetof(skb_frag_t, netmem));
  111. static_assert(sizeof_field(struct bio_vec, bv_page) ==
  112. sizeof_field(skb_frag_t, netmem));
  113. static_assert(offsetof(struct bio_vec, bv_len) == offsetof(skb_frag_t, len));
  114. static_assert(sizeof_field(struct bio_vec, bv_len) ==
  115. sizeof_field(skb_frag_t, len));
  116. static_assert(offsetof(struct bio_vec, bv_offset) ==
  117. offsetof(skb_frag_t, offset));
  118. static_assert(sizeof_field(struct bio_vec, bv_offset) ==
  119. sizeof_field(skb_frag_t, offset));
  120. #undef FN
  121. #define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
  122. static const char * const drop_reasons[] = {
  123. [SKB_CONSUMED] = "CONSUMED",
  124. DEFINE_DROP_REASON(FN, FN)
  125. };
  126. static const struct drop_reason_list drop_reasons_core = {
  127. .reasons = drop_reasons,
  128. .n_reasons = ARRAY_SIZE(drop_reasons),
  129. };
  130. const struct drop_reason_list __rcu *
  131. drop_reasons_by_subsys[SKB_DROP_REASON_SUBSYS_NUM] = {
  132. [SKB_DROP_REASON_SUBSYS_CORE] = RCU_INITIALIZER(&drop_reasons_core),
  133. };
  134. EXPORT_SYMBOL(drop_reasons_by_subsys);
  135. /**
  136. * drop_reasons_register_subsys - register another drop reason subsystem
  137. * @subsys: the subsystem to register, must not be the core
  138. * @list: the list of drop reasons within the subsystem, must point to
  139. * a statically initialized list
  140. */
  141. void drop_reasons_register_subsys(enum skb_drop_reason_subsys subsys,
  142. const struct drop_reason_list *list)
  143. {
  144. if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
  145. subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
  146. "invalid subsystem %d\n", subsys))
  147. return;
  148. /* must point to statically allocated memory, so INIT is OK */
  149. RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], list);
  150. }
  151. EXPORT_SYMBOL_GPL(drop_reasons_register_subsys);
  152. /**
  153. * drop_reasons_unregister_subsys - unregister a drop reason subsystem
  154. * @subsys: the subsystem to remove, must not be the core
  155. *
  156. * Note: This will synchronize_rcu() to ensure no users when it returns.
  157. */
  158. void drop_reasons_unregister_subsys(enum skb_drop_reason_subsys subsys)
  159. {
  160. if (WARN(subsys <= SKB_DROP_REASON_SUBSYS_CORE ||
  161. subsys >= ARRAY_SIZE(drop_reasons_by_subsys),
  162. "invalid subsystem %d\n", subsys))
  163. return;
  164. RCU_INIT_POINTER(drop_reasons_by_subsys[subsys], NULL);
  165. synchronize_rcu();
  166. }
  167. EXPORT_SYMBOL_GPL(drop_reasons_unregister_subsys);
  168. /**
  169. * skb_panic - private function for out-of-line support
  170. * @skb: buffer
  171. * @sz: size
  172. * @addr: address
  173. * @msg: skb_over_panic or skb_under_panic
  174. *
  175. * Out-of-line support for skb_put() and skb_push().
  176. * Called via the wrapper skb_over_panic() or skb_under_panic().
  177. * Keep out of line to prevent kernel bloat.
  178. * __builtin_return_address is not used because it is not always reliable.
  179. */
  180. static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
  181. const char msg[])
  182. {
  183. pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
  184. msg, addr, skb->len, sz, skb->head, skb->data,
  185. (unsigned long)skb->tail, (unsigned long)skb->end,
  186. skb->dev ? skb->dev->name : "<NULL>");
  187. BUG();
  188. }
  189. static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  190. {
  191. skb_panic(skb, sz, addr, __func__);
  192. }
  193. static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  194. {
  195. skb_panic(skb, sz, addr, __func__);
  196. }
  197. #define NAPI_SKB_CACHE_SIZE 64
  198. #define NAPI_SKB_CACHE_BULK 16
  199. #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2)
  200. #if PAGE_SIZE == SZ_4K
  201. #define NAPI_HAS_SMALL_PAGE_FRAG 1
  202. #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc)
  203. /* specialized page frag allocator using a single order 0 page
  204. * and slicing it into 1K sized fragment. Constrained to systems
  205. * with a very limited amount of 1K fragments fitting a single
  206. * page - to avoid excessive truesize underestimation
  207. */
  208. struct page_frag_1k {
  209. void *va;
  210. u16 offset;
  211. bool pfmemalloc;
  212. };
  213. static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
  214. {
  215. struct page *page;
  216. int offset;
  217. offset = nc->offset - SZ_1K;
  218. if (likely(offset >= 0))
  219. goto use_frag;
  220. page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
  221. if (!page)
  222. return NULL;
  223. nc->va = page_address(page);
  224. nc->pfmemalloc = page_is_pfmemalloc(page);
  225. offset = PAGE_SIZE - SZ_1K;
  226. page_ref_add(page, offset / SZ_1K);
  227. use_frag:
  228. nc->offset = offset;
  229. return nc->va + offset;
  230. }
  231. #else
  232. /* the small page is actually unused in this build; add dummy helpers
  233. * to please the compiler and avoid later preprocessor's conditionals
  234. */
  235. #define NAPI_HAS_SMALL_PAGE_FRAG 0
  236. #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false
  237. struct page_frag_1k {
  238. };
  239. static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
  240. {
  241. return NULL;
  242. }
  243. #endif
  244. struct napi_alloc_cache {
  245. local_lock_t bh_lock;
  246. struct page_frag_cache page;
  247. struct page_frag_1k page_small;
  248. unsigned int skb_count;
  249. void *skb_cache[NAPI_SKB_CACHE_SIZE];
  250. };
  251. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  252. static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache) = {
  253. .bh_lock = INIT_LOCAL_LOCK(bh_lock),
  254. };
  255. /* Double check that napi_get_frags() allocates skbs with
  256. * skb->head being backed by slab, not a page fragment.
  257. * This is to make sure bug fixed in 3226b158e67c
  258. * ("net: avoid 32 x truesize under-estimation for tiny skbs")
  259. * does not accidentally come back.
  260. */
  261. void napi_get_frags_check(struct napi_struct *napi)
  262. {
  263. struct sk_buff *skb;
  264. local_bh_disable();
  265. skb = napi_get_frags(napi);
  266. WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
  267. napi_free_frags(napi);
  268. local_bh_enable();
  269. }
  270. void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
  271. {
  272. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  273. void *data;
  274. fragsz = SKB_DATA_ALIGN(fragsz);
  275. local_lock_nested_bh(&napi_alloc_cache.bh_lock);
  276. data = __page_frag_alloc_align(&nc->page, fragsz,
  277. GFP_ATOMIC | __GFP_NOWARN, align_mask);
  278. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  279. return data;
  280. }
  281. EXPORT_SYMBOL(__napi_alloc_frag_align);
  282. void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
  283. {
  284. void *data;
  285. if (in_hardirq() || irqs_disabled()) {
  286. struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
  287. fragsz = SKB_DATA_ALIGN(fragsz);
  288. data = __page_frag_alloc_align(nc, fragsz,
  289. GFP_ATOMIC | __GFP_NOWARN,
  290. align_mask);
  291. } else {
  292. local_bh_disable();
  293. data = __napi_alloc_frag_align(fragsz, align_mask);
  294. local_bh_enable();
  295. }
  296. return data;
  297. }
  298. EXPORT_SYMBOL(__netdev_alloc_frag_align);
  299. static struct sk_buff *napi_skb_cache_get(void)
  300. {
  301. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  302. struct sk_buff *skb;
  303. local_lock_nested_bh(&napi_alloc_cache.bh_lock);
  304. if (unlikely(!nc->skb_count)) {
  305. nc->skb_count = kmem_cache_alloc_bulk(net_hotdata.skbuff_cache,
  306. GFP_ATOMIC | __GFP_NOWARN,
  307. NAPI_SKB_CACHE_BULK,
  308. nc->skb_cache);
  309. if (unlikely(!nc->skb_count)) {
  310. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  311. return NULL;
  312. }
  313. }
  314. skb = nc->skb_cache[--nc->skb_count];
  315. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  316. kasan_mempool_unpoison_object(skb, kmem_cache_size(net_hotdata.skbuff_cache));
  317. return skb;
  318. }
  319. static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
  320. unsigned int size)
  321. {
  322. struct skb_shared_info *shinfo;
  323. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  324. /* Assumes caller memset cleared SKB */
  325. skb->truesize = SKB_TRUESIZE(size);
  326. refcount_set(&skb->users, 1);
  327. skb->head = data;
  328. skb->data = data;
  329. skb_reset_tail_pointer(skb);
  330. skb_set_end_offset(skb, size);
  331. skb->mac_header = (typeof(skb->mac_header))~0U;
  332. skb->transport_header = (typeof(skb->transport_header))~0U;
  333. skb->alloc_cpu = raw_smp_processor_id();
  334. /* make sure we initialize shinfo sequentially */
  335. shinfo = skb_shinfo(skb);
  336. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  337. atomic_set(&shinfo->dataref, 1);
  338. skb_set_kcov_handle(skb, kcov_common_handle());
  339. }
  340. static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
  341. unsigned int *size)
  342. {
  343. void *resized;
  344. /* Must find the allocation size (and grow it to match). */
  345. *size = ksize(data);
  346. /* krealloc() will immediately return "data" when
  347. * "ksize(data)" is requested: it is the existing upper
  348. * bounds. As a result, GFP_ATOMIC will be ignored. Note
  349. * that this "new" pointer needs to be passed back to the
  350. * caller for use so the __alloc_size hinting will be
  351. * tracked correctly.
  352. */
  353. resized = krealloc(data, *size, GFP_ATOMIC);
  354. WARN_ON_ONCE(resized != data);
  355. return resized;
  356. }
  357. /* build_skb() variant which can operate on slab buffers.
  358. * Note that this should be used sparingly as slab buffers
  359. * cannot be combined efficiently by GRO!
  360. */
  361. struct sk_buff *slab_build_skb(void *data)
  362. {
  363. struct sk_buff *skb;
  364. unsigned int size;
  365. skb = kmem_cache_alloc(net_hotdata.skbuff_cache,
  366. GFP_ATOMIC | __GFP_NOWARN);
  367. if (unlikely(!skb))
  368. return NULL;
  369. memset(skb, 0, offsetof(struct sk_buff, tail));
  370. data = __slab_build_skb(skb, data, &size);
  371. __finalize_skb_around(skb, data, size);
  372. return skb;
  373. }
  374. EXPORT_SYMBOL(slab_build_skb);
  375. /* Caller must provide SKB that is memset cleared */
  376. static void __build_skb_around(struct sk_buff *skb, void *data,
  377. unsigned int frag_size)
  378. {
  379. unsigned int size = frag_size;
  380. /* frag_size == 0 is considered deprecated now. Callers
  381. * using slab buffer should use slab_build_skb() instead.
  382. */
  383. if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
  384. data = __slab_build_skb(skb, data, &size);
  385. __finalize_skb_around(skb, data, size);
  386. }
  387. /**
  388. * __build_skb - build a network buffer
  389. * @data: data buffer provided by caller
  390. * @frag_size: size of data (must not be 0)
  391. *
  392. * Allocate a new &sk_buff. Caller provides space holding head and
  393. * skb_shared_info. @data must have been allocated from the page
  394. * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
  395. * allocation is deprecated, and callers should use slab_build_skb()
  396. * instead.)
  397. * The return is the new skb buffer.
  398. * On a failure the return is %NULL, and @data is not freed.
  399. * Notes :
  400. * Before IO, driver allocates only data buffer where NIC put incoming frame
  401. * Driver should add room at head (NET_SKB_PAD) and
  402. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  403. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  404. * before giving packet to stack.
  405. * RX rings only contains data buffers, not full skbs.
  406. */
  407. struct sk_buff *__build_skb(void *data, unsigned int frag_size)
  408. {
  409. struct sk_buff *skb;
  410. skb = kmem_cache_alloc(net_hotdata.skbuff_cache,
  411. GFP_ATOMIC | __GFP_NOWARN);
  412. if (unlikely(!skb))
  413. return NULL;
  414. memset(skb, 0, offsetof(struct sk_buff, tail));
  415. __build_skb_around(skb, data, frag_size);
  416. return skb;
  417. }
  418. /* build_skb() is wrapper over __build_skb(), that specifically
  419. * takes care of skb->head and skb->pfmemalloc
  420. */
  421. struct sk_buff *build_skb(void *data, unsigned int frag_size)
  422. {
  423. struct sk_buff *skb = __build_skb(data, frag_size);
  424. if (likely(skb && frag_size)) {
  425. skb->head_frag = 1;
  426. skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
  427. }
  428. return skb;
  429. }
  430. EXPORT_SYMBOL(build_skb);
  431. /**
  432. * build_skb_around - build a network buffer around provided skb
  433. * @skb: sk_buff provide by caller, must be memset cleared
  434. * @data: data buffer provided by caller
  435. * @frag_size: size of data
  436. */
  437. struct sk_buff *build_skb_around(struct sk_buff *skb,
  438. void *data, unsigned int frag_size)
  439. {
  440. if (unlikely(!skb))
  441. return NULL;
  442. __build_skb_around(skb, data, frag_size);
  443. if (frag_size) {
  444. skb->head_frag = 1;
  445. skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
  446. }
  447. return skb;
  448. }
  449. EXPORT_SYMBOL(build_skb_around);
  450. /**
  451. * __napi_build_skb - build a network buffer
  452. * @data: data buffer provided by caller
  453. * @frag_size: size of data
  454. *
  455. * Version of __build_skb() that uses NAPI percpu caches to obtain
  456. * skbuff_head instead of inplace allocation.
  457. *
  458. * Returns a new &sk_buff on success, %NULL on allocation failure.
  459. */
  460. static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
  461. {
  462. struct sk_buff *skb;
  463. skb = napi_skb_cache_get();
  464. if (unlikely(!skb))
  465. return NULL;
  466. memset(skb, 0, offsetof(struct sk_buff, tail));
  467. __build_skb_around(skb, data, frag_size);
  468. return skb;
  469. }
  470. /**
  471. * napi_build_skb - build a network buffer
  472. * @data: data buffer provided by caller
  473. * @frag_size: size of data
  474. *
  475. * Version of __napi_build_skb() that takes care of skb->head_frag
  476. * and skb->pfmemalloc when the data is a page or page fragment.
  477. *
  478. * Returns a new &sk_buff on success, %NULL on allocation failure.
  479. */
  480. struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
  481. {
  482. struct sk_buff *skb = __napi_build_skb(data, frag_size);
  483. if (likely(skb) && frag_size) {
  484. skb->head_frag = 1;
  485. skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
  486. }
  487. return skb;
  488. }
  489. EXPORT_SYMBOL(napi_build_skb);
  490. /*
  491. * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
  492. * the caller if emergency pfmemalloc reserves are being used. If it is and
  493. * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
  494. * may be used. Otherwise, the packet data may be discarded until enough
  495. * memory is free
  496. */
  497. static void *kmalloc_reserve(unsigned int *size, gfp_t flags, int node,
  498. bool *pfmemalloc)
  499. {
  500. bool ret_pfmemalloc = false;
  501. size_t obj_size;
  502. void *obj;
  503. obj_size = SKB_HEAD_ALIGN(*size);
  504. if (obj_size <= SKB_SMALL_HEAD_CACHE_SIZE &&
  505. !(flags & KMALLOC_NOT_NORMAL_BITS)) {
  506. obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache,
  507. flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
  508. node);
  509. *size = SKB_SMALL_HEAD_CACHE_SIZE;
  510. if (obj || !(gfp_pfmemalloc_allowed(flags)))
  511. goto out;
  512. /* Try again but now we are using pfmemalloc reserves */
  513. ret_pfmemalloc = true;
  514. obj = kmem_cache_alloc_node(net_hotdata.skb_small_head_cache, flags, node);
  515. goto out;
  516. }
  517. obj_size = kmalloc_size_roundup(obj_size);
  518. /* The following cast might truncate high-order bits of obj_size, this
  519. * is harmless because kmalloc(obj_size >= 2^32) will fail anyway.
  520. */
  521. *size = (unsigned int)obj_size;
  522. /*
  523. * Try a regular allocation, when that fails and we're not entitled
  524. * to the reserves, fail.
  525. */
  526. obj = kmalloc_node_track_caller(obj_size,
  527. flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
  528. node);
  529. if (obj || !(gfp_pfmemalloc_allowed(flags)))
  530. goto out;
  531. /* Try again but now we are using pfmemalloc reserves */
  532. ret_pfmemalloc = true;
  533. obj = kmalloc_node_track_caller(obj_size, flags, node);
  534. out:
  535. if (pfmemalloc)
  536. *pfmemalloc = ret_pfmemalloc;
  537. return obj;
  538. }
  539. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  540. * 'private' fields and also do memory statistics to find all the
  541. * [BEEP] leaks.
  542. *
  543. */
  544. /**
  545. * __alloc_skb - allocate a network buffer
  546. * @size: size to allocate
  547. * @gfp_mask: allocation mask
  548. * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
  549. * instead of head cache and allocate a cloned (child) skb.
  550. * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
  551. * allocations in case the data is required for writeback
  552. * @node: numa node to allocate memory on
  553. *
  554. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  555. * tail room of at least size bytes. The object has a reference count
  556. * of one. The return is the buffer. On a failure the return is %NULL.
  557. *
  558. * Buffers may only be allocated from interrupts using a @gfp_mask of
  559. * %GFP_ATOMIC.
  560. */
  561. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  562. int flags, int node)
  563. {
  564. struct kmem_cache *cache;
  565. struct sk_buff *skb;
  566. bool pfmemalloc;
  567. u8 *data;
  568. cache = (flags & SKB_ALLOC_FCLONE)
  569. ? net_hotdata.skbuff_fclone_cache : net_hotdata.skbuff_cache;
  570. if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
  571. gfp_mask |= __GFP_MEMALLOC;
  572. /* Get the HEAD */
  573. if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
  574. likely(node == NUMA_NO_NODE || node == numa_mem_id()))
  575. skb = napi_skb_cache_get();
  576. else
  577. skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
  578. if (unlikely(!skb))
  579. return NULL;
  580. prefetchw(skb);
  581. /* We do our best to align skb_shared_info on a separate cache
  582. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  583. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  584. * Both skb->head and skb_shared_info are cache line aligned.
  585. */
  586. data = kmalloc_reserve(&size, gfp_mask, node, &pfmemalloc);
  587. if (unlikely(!data))
  588. goto nodata;
  589. /* kmalloc_size_roundup() might give us more room than requested.
  590. * Put skb_shared_info exactly at the end of allocated zone,
  591. * to allow max possible filling before reallocation.
  592. */
  593. prefetchw(data + SKB_WITH_OVERHEAD(size));
  594. /*
  595. * Only clear those fields we need to clear, not those that we will
  596. * actually initialise below. Hence, don't put any more fields after
  597. * the tail pointer in struct sk_buff!
  598. */
  599. memset(skb, 0, offsetof(struct sk_buff, tail));
  600. __build_skb_around(skb, data, size);
  601. skb->pfmemalloc = pfmemalloc;
  602. if (flags & SKB_ALLOC_FCLONE) {
  603. struct sk_buff_fclones *fclones;
  604. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  605. skb->fclone = SKB_FCLONE_ORIG;
  606. refcount_set(&fclones->fclone_ref, 1);
  607. }
  608. return skb;
  609. nodata:
  610. kmem_cache_free(cache, skb);
  611. return NULL;
  612. }
  613. EXPORT_SYMBOL(__alloc_skb);
  614. /**
  615. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  616. * @dev: network device to receive on
  617. * @len: length to allocate
  618. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  619. *
  620. * Allocate a new &sk_buff and assign it a usage count of one. The
  621. * buffer has NET_SKB_PAD headroom built in. Users should allocate
  622. * the headroom they think they need without accounting for the
  623. * built in space. The built in space is used for optimisations.
  624. *
  625. * %NULL is returned if there is no free memory.
  626. */
  627. struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  628. gfp_t gfp_mask)
  629. {
  630. struct page_frag_cache *nc;
  631. struct sk_buff *skb;
  632. bool pfmemalloc;
  633. void *data;
  634. len += NET_SKB_PAD;
  635. /* If requested length is either too small or too big,
  636. * we use kmalloc() for skb->head allocation.
  637. */
  638. if (len <= SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE) ||
  639. len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
  640. (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
  641. skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
  642. if (!skb)
  643. goto skb_fail;
  644. goto skb_success;
  645. }
  646. len = SKB_HEAD_ALIGN(len);
  647. if (sk_memalloc_socks())
  648. gfp_mask |= __GFP_MEMALLOC;
  649. if (in_hardirq() || irqs_disabled()) {
  650. nc = this_cpu_ptr(&netdev_alloc_cache);
  651. data = page_frag_alloc(nc, len, gfp_mask);
  652. pfmemalloc = nc->pfmemalloc;
  653. } else {
  654. local_bh_disable();
  655. local_lock_nested_bh(&napi_alloc_cache.bh_lock);
  656. nc = this_cpu_ptr(&napi_alloc_cache.page);
  657. data = page_frag_alloc(nc, len, gfp_mask);
  658. pfmemalloc = nc->pfmemalloc;
  659. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  660. local_bh_enable();
  661. }
  662. if (unlikely(!data))
  663. return NULL;
  664. skb = __build_skb(data, len);
  665. if (unlikely(!skb)) {
  666. skb_free_frag(data);
  667. return NULL;
  668. }
  669. if (pfmemalloc)
  670. skb->pfmemalloc = 1;
  671. skb->head_frag = 1;
  672. skb_success:
  673. skb_reserve(skb, NET_SKB_PAD);
  674. skb->dev = dev;
  675. skb_fail:
  676. return skb;
  677. }
  678. EXPORT_SYMBOL(__netdev_alloc_skb);
  679. /**
  680. * napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
  681. * @napi: napi instance this buffer was allocated for
  682. * @len: length to allocate
  683. *
  684. * Allocate a new sk_buff for use in NAPI receive. This buffer will
  685. * attempt to allocate the head from a special reserved region used
  686. * only for NAPI Rx allocation. By doing this we can save several
  687. * CPU cycles by avoiding having to disable and re-enable IRQs.
  688. *
  689. * %NULL is returned if there is no free memory.
  690. */
  691. struct sk_buff *napi_alloc_skb(struct napi_struct *napi, unsigned int len)
  692. {
  693. gfp_t gfp_mask = GFP_ATOMIC | __GFP_NOWARN;
  694. struct napi_alloc_cache *nc;
  695. struct sk_buff *skb;
  696. bool pfmemalloc;
  697. void *data;
  698. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  699. len += NET_SKB_PAD + NET_IP_ALIGN;
  700. /* If requested length is either too small or too big,
  701. * we use kmalloc() for skb->head allocation.
  702. * When the small frag allocator is available, prefer it over kmalloc
  703. * for small fragments
  704. */
  705. if ((!NAPI_HAS_SMALL_PAGE_FRAG &&
  706. len <= SKB_WITH_OVERHEAD(SKB_SMALL_HEAD_CACHE_SIZE)) ||
  707. len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
  708. (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
  709. skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
  710. NUMA_NO_NODE);
  711. if (!skb)
  712. goto skb_fail;
  713. goto skb_success;
  714. }
  715. if (sk_memalloc_socks())
  716. gfp_mask |= __GFP_MEMALLOC;
  717. local_lock_nested_bh(&napi_alloc_cache.bh_lock);
  718. nc = this_cpu_ptr(&napi_alloc_cache);
  719. if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
  720. /* we are artificially inflating the allocation size, but
  721. * that is not as bad as it may look like, as:
  722. * - 'len' less than GRO_MAX_HEAD makes little sense
  723. * - On most systems, larger 'len' values lead to fragment
  724. * size above 512 bytes
  725. * - kmalloc would use the kmalloc-1k slab for such values
  726. * - Builds with smaller GRO_MAX_HEAD will very likely do
  727. * little networking, as that implies no WiFi and no
  728. * tunnels support, and 32 bits arches.
  729. */
  730. len = SZ_1K;
  731. data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
  732. pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
  733. } else {
  734. len = SKB_HEAD_ALIGN(len);
  735. data = page_frag_alloc(&nc->page, len, gfp_mask);
  736. pfmemalloc = nc->page.pfmemalloc;
  737. }
  738. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  739. if (unlikely(!data))
  740. return NULL;
  741. skb = __napi_build_skb(data, len);
  742. if (unlikely(!skb)) {
  743. skb_free_frag(data);
  744. return NULL;
  745. }
  746. if (pfmemalloc)
  747. skb->pfmemalloc = 1;
  748. skb->head_frag = 1;
  749. skb_success:
  750. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  751. skb->dev = napi->dev;
  752. skb_fail:
  753. return skb;
  754. }
  755. EXPORT_SYMBOL(napi_alloc_skb);
  756. void skb_add_rx_frag_netmem(struct sk_buff *skb, int i, netmem_ref netmem,
  757. int off, int size, unsigned int truesize)
  758. {
  759. DEBUG_NET_WARN_ON_ONCE(size > truesize);
  760. skb_fill_netmem_desc(skb, i, netmem, off, size);
  761. skb->len += size;
  762. skb->data_len += size;
  763. skb->truesize += truesize;
  764. }
  765. EXPORT_SYMBOL(skb_add_rx_frag_netmem);
  766. void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
  767. unsigned int truesize)
  768. {
  769. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  770. DEBUG_NET_WARN_ON_ONCE(size > truesize);
  771. skb_frag_size_add(frag, size);
  772. skb->len += size;
  773. skb->data_len += size;
  774. skb->truesize += truesize;
  775. }
  776. EXPORT_SYMBOL(skb_coalesce_rx_frag);
  777. static void skb_drop_list(struct sk_buff **listp)
  778. {
  779. kfree_skb_list(*listp);
  780. *listp = NULL;
  781. }
  782. static inline void skb_drop_fraglist(struct sk_buff *skb)
  783. {
  784. skb_drop_list(&skb_shinfo(skb)->frag_list);
  785. }
  786. static void skb_clone_fraglist(struct sk_buff *skb)
  787. {
  788. struct sk_buff *list;
  789. skb_walk_frags(skb, list)
  790. skb_get(list);
  791. }
  792. int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
  793. unsigned int headroom)
  794. {
  795. #if IS_ENABLED(CONFIG_PAGE_POOL)
  796. u32 size, truesize, len, max_head_size, off;
  797. struct sk_buff *skb = *pskb, *nskb;
  798. int err, i, head_off;
  799. void *data;
  800. /* XDP does not support fraglist so we need to linearize
  801. * the skb.
  802. */
  803. if (skb_has_frag_list(skb))
  804. return -EOPNOTSUPP;
  805. max_head_size = SKB_WITH_OVERHEAD(PAGE_SIZE - headroom);
  806. if (skb->len > max_head_size + MAX_SKB_FRAGS * PAGE_SIZE)
  807. return -ENOMEM;
  808. size = min_t(u32, skb->len, max_head_size);
  809. truesize = SKB_HEAD_ALIGN(size) + headroom;
  810. data = page_pool_dev_alloc_va(pool, &truesize);
  811. if (!data)
  812. return -ENOMEM;
  813. nskb = napi_build_skb(data, truesize);
  814. if (!nskb) {
  815. page_pool_free_va(pool, data, true);
  816. return -ENOMEM;
  817. }
  818. skb_reserve(nskb, headroom);
  819. skb_copy_header(nskb, skb);
  820. skb_mark_for_recycle(nskb);
  821. err = skb_copy_bits(skb, 0, nskb->data, size);
  822. if (err) {
  823. consume_skb(nskb);
  824. return err;
  825. }
  826. skb_put(nskb, size);
  827. head_off = skb_headroom(nskb) - skb_headroom(skb);
  828. skb_headers_offset_update(nskb, head_off);
  829. off = size;
  830. len = skb->len - off;
  831. for (i = 0; i < MAX_SKB_FRAGS && off < skb->len; i++) {
  832. struct page *page;
  833. u32 page_off;
  834. size = min_t(u32, len, PAGE_SIZE);
  835. truesize = size;
  836. page = page_pool_dev_alloc(pool, &page_off, &truesize);
  837. if (!page) {
  838. consume_skb(nskb);
  839. return -ENOMEM;
  840. }
  841. skb_add_rx_frag(nskb, i, page, page_off, size, truesize);
  842. err = skb_copy_bits(skb, off, page_address(page) + page_off,
  843. size);
  844. if (err) {
  845. consume_skb(nskb);
  846. return err;
  847. }
  848. len -= size;
  849. off += size;
  850. }
  851. consume_skb(skb);
  852. *pskb = nskb;
  853. return 0;
  854. #else
  855. return -EOPNOTSUPP;
  856. #endif
  857. }
  858. EXPORT_SYMBOL(skb_pp_cow_data);
  859. int skb_cow_data_for_xdp(struct page_pool *pool, struct sk_buff **pskb,
  860. struct bpf_prog *prog)
  861. {
  862. if (!prog->aux->xdp_has_frags)
  863. return -EINVAL;
  864. return skb_pp_cow_data(pool, pskb, XDP_PACKET_HEADROOM);
  865. }
  866. EXPORT_SYMBOL(skb_cow_data_for_xdp);
  867. #if IS_ENABLED(CONFIG_PAGE_POOL)
  868. bool napi_pp_put_page(netmem_ref netmem)
  869. {
  870. netmem = netmem_compound_head(netmem);
  871. if (unlikely(!netmem_is_pp(netmem)))
  872. return false;
  873. page_pool_put_full_netmem(netmem_get_pp(netmem), netmem, false);
  874. return true;
  875. }
  876. EXPORT_SYMBOL(napi_pp_put_page);
  877. #endif
  878. static bool skb_pp_recycle(struct sk_buff *skb, void *data)
  879. {
  880. if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
  881. return false;
  882. return napi_pp_put_page(page_to_netmem(virt_to_page(data)));
  883. }
  884. /**
  885. * skb_pp_frag_ref() - Increase fragment references of a page pool aware skb
  886. * @skb: page pool aware skb
  887. *
  888. * Increase the fragment reference count (pp_ref_count) of a skb. This is
  889. * intended to gain fragment references only for page pool aware skbs,
  890. * i.e. when skb->pp_recycle is true, and not for fragments in a
  891. * non-pp-recycling skb. It has a fallback to increase references on normal
  892. * pages, as page pool aware skbs may also have normal page fragments.
  893. */
  894. static int skb_pp_frag_ref(struct sk_buff *skb)
  895. {
  896. struct skb_shared_info *shinfo;
  897. netmem_ref head_netmem;
  898. int i;
  899. if (!skb->pp_recycle)
  900. return -EINVAL;
  901. shinfo = skb_shinfo(skb);
  902. for (i = 0; i < shinfo->nr_frags; i++) {
  903. head_netmem = netmem_compound_head(shinfo->frags[i].netmem);
  904. if (likely(netmem_is_pp(head_netmem)))
  905. page_pool_ref_netmem(head_netmem);
  906. else
  907. page_ref_inc(netmem_to_page(head_netmem));
  908. }
  909. return 0;
  910. }
  911. static void skb_kfree_head(void *head, unsigned int end_offset)
  912. {
  913. if (end_offset == SKB_SMALL_HEAD_HEADROOM)
  914. kmem_cache_free(net_hotdata.skb_small_head_cache, head);
  915. else
  916. kfree(head);
  917. }
  918. static void skb_free_head(struct sk_buff *skb)
  919. {
  920. unsigned char *head = skb->head;
  921. if (skb->head_frag) {
  922. if (skb_pp_recycle(skb, head))
  923. return;
  924. skb_free_frag(head);
  925. } else {
  926. skb_kfree_head(head, skb_end_offset(skb));
  927. }
  928. }
  929. static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason)
  930. {
  931. struct skb_shared_info *shinfo = skb_shinfo(skb);
  932. int i;
  933. if (!skb_data_unref(skb, shinfo))
  934. goto exit;
  935. if (skb_zcopy(skb)) {
  936. bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
  937. skb_zcopy_clear(skb, true);
  938. if (skip_unref)
  939. goto free_head;
  940. }
  941. for (i = 0; i < shinfo->nr_frags; i++)
  942. __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
  943. free_head:
  944. if (shinfo->frag_list)
  945. kfree_skb_list_reason(shinfo->frag_list, reason);
  946. skb_free_head(skb);
  947. exit:
  948. /* When we clone an SKB we copy the reycling bit. The pp_recycle
  949. * bit is only set on the head though, so in order to avoid races
  950. * while trying to recycle fragments on __skb_frag_unref() we need
  951. * to make one SKB responsible for triggering the recycle path.
  952. * So disable the recycling bit if an SKB is cloned and we have
  953. * additional references to the fragmented part of the SKB.
  954. * Eventually the last SKB will have the recycling bit set and it's
  955. * dataref set to 0, which will trigger the recycling
  956. */
  957. skb->pp_recycle = 0;
  958. }
  959. /*
  960. * Free an skbuff by memory without cleaning the state.
  961. */
  962. static void kfree_skbmem(struct sk_buff *skb)
  963. {
  964. struct sk_buff_fclones *fclones;
  965. switch (skb->fclone) {
  966. case SKB_FCLONE_UNAVAILABLE:
  967. kmem_cache_free(net_hotdata.skbuff_cache, skb);
  968. return;
  969. case SKB_FCLONE_ORIG:
  970. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  971. /* We usually free the clone (TX completion) before original skb
  972. * This test would have no chance to be true for the clone,
  973. * while here, branch prediction will be good.
  974. */
  975. if (refcount_read(&fclones->fclone_ref) == 1)
  976. goto fastpath;
  977. break;
  978. default: /* SKB_FCLONE_CLONE */
  979. fclones = container_of(skb, struct sk_buff_fclones, skb2);
  980. break;
  981. }
  982. if (!refcount_dec_and_test(&fclones->fclone_ref))
  983. return;
  984. fastpath:
  985. kmem_cache_free(net_hotdata.skbuff_fclone_cache, fclones);
  986. }
  987. void skb_release_head_state(struct sk_buff *skb)
  988. {
  989. skb_dst_drop(skb);
  990. if (skb->destructor) {
  991. DEBUG_NET_WARN_ON_ONCE(in_hardirq());
  992. skb->destructor(skb);
  993. }
  994. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  995. nf_conntrack_put(skb_nfct(skb));
  996. #endif
  997. skb_ext_put(skb);
  998. }
  999. /* Free everything but the sk_buff shell. */
  1000. static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason)
  1001. {
  1002. skb_release_head_state(skb);
  1003. if (likely(skb->head))
  1004. skb_release_data(skb, reason);
  1005. }
  1006. /**
  1007. * __kfree_skb - private function
  1008. * @skb: buffer
  1009. *
  1010. * Free an sk_buff. Release anything attached to the buffer.
  1011. * Clean the state. This is an internal helper function. Users should
  1012. * always call kfree_skb
  1013. */
  1014. void __kfree_skb(struct sk_buff *skb)
  1015. {
  1016. skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED);
  1017. kfree_skbmem(skb);
  1018. }
  1019. EXPORT_SYMBOL(__kfree_skb);
  1020. static __always_inline
  1021. bool __sk_skb_reason_drop(struct sock *sk, struct sk_buff *skb,
  1022. enum skb_drop_reason reason)
  1023. {
  1024. if (unlikely(!skb_unref(skb)))
  1025. return false;
  1026. DEBUG_NET_WARN_ON_ONCE(reason == SKB_NOT_DROPPED_YET ||
  1027. u32_get_bits(reason,
  1028. SKB_DROP_REASON_SUBSYS_MASK) >=
  1029. SKB_DROP_REASON_SUBSYS_NUM);
  1030. if (reason == SKB_CONSUMED)
  1031. trace_consume_skb(skb, __builtin_return_address(0));
  1032. else
  1033. trace_kfree_skb(skb, __builtin_return_address(0), reason, sk);
  1034. return true;
  1035. }
  1036. /**
  1037. * sk_skb_reason_drop - free an sk_buff with special reason
  1038. * @sk: the socket to receive @skb, or NULL if not applicable
  1039. * @skb: buffer to free
  1040. * @reason: reason why this skb is dropped
  1041. *
  1042. * Drop a reference to the buffer and free it if the usage count has hit
  1043. * zero. Meanwhile, pass the receiving socket and drop reason to
  1044. * 'kfree_skb' tracepoint.
  1045. */
  1046. void __fix_address
  1047. sk_skb_reason_drop(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
  1048. {
  1049. if (__sk_skb_reason_drop(sk, skb, reason))
  1050. __kfree_skb(skb);
  1051. }
  1052. EXPORT_SYMBOL(sk_skb_reason_drop);
  1053. #define KFREE_SKB_BULK_SIZE 16
  1054. struct skb_free_array {
  1055. unsigned int skb_count;
  1056. void *skb_array[KFREE_SKB_BULK_SIZE];
  1057. };
  1058. static void kfree_skb_add_bulk(struct sk_buff *skb,
  1059. struct skb_free_array *sa,
  1060. enum skb_drop_reason reason)
  1061. {
  1062. /* if SKB is a clone, don't handle this case */
  1063. if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
  1064. __kfree_skb(skb);
  1065. return;
  1066. }
  1067. skb_release_all(skb, reason);
  1068. sa->skb_array[sa->skb_count++] = skb;
  1069. if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) {
  1070. kmem_cache_free_bulk(net_hotdata.skbuff_cache, KFREE_SKB_BULK_SIZE,
  1071. sa->skb_array);
  1072. sa->skb_count = 0;
  1073. }
  1074. }
  1075. void __fix_address
  1076. kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason)
  1077. {
  1078. struct skb_free_array sa;
  1079. sa.skb_count = 0;
  1080. while (segs) {
  1081. struct sk_buff *next = segs->next;
  1082. if (__sk_skb_reason_drop(NULL, segs, reason)) {
  1083. skb_poison_list(segs);
  1084. kfree_skb_add_bulk(segs, &sa, reason);
  1085. }
  1086. segs = next;
  1087. }
  1088. if (sa.skb_count)
  1089. kmem_cache_free_bulk(net_hotdata.skbuff_cache, sa.skb_count, sa.skb_array);
  1090. }
  1091. EXPORT_SYMBOL(kfree_skb_list_reason);
  1092. /* Dump skb information and contents.
  1093. *
  1094. * Must only be called from net_ratelimit()-ed paths.
  1095. *
  1096. * Dumps whole packets if full_pkt, only headers otherwise.
  1097. */
  1098. void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
  1099. {
  1100. struct skb_shared_info *sh = skb_shinfo(skb);
  1101. struct net_device *dev = skb->dev;
  1102. struct sock *sk = skb->sk;
  1103. struct sk_buff *list_skb;
  1104. bool has_mac, has_trans;
  1105. int headroom, tailroom;
  1106. int i, len, seg_len;
  1107. if (full_pkt)
  1108. len = skb->len;
  1109. else
  1110. len = min_t(int, skb->len, MAX_HEADER + 128);
  1111. headroom = skb_headroom(skb);
  1112. tailroom = skb_tailroom(skb);
  1113. has_mac = skb_mac_header_was_set(skb);
  1114. has_trans = skb_transport_header_was_set(skb);
  1115. printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
  1116. "mac=(%d,%d) mac_len=%u net=(%d,%d) trans=%d\n"
  1117. "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
  1118. "csum(0x%x start=%u offset=%u ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
  1119. "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n"
  1120. "priority=0x%x mark=0x%x alloc_cpu=%u vlan_all=0x%x\n"
  1121. "encapsulation=%d inner(proto=0x%04x, mac=%u, net=%u, trans=%u)\n",
  1122. level, skb->len, headroom, skb_headlen(skb), tailroom,
  1123. has_mac ? skb->mac_header : -1,
  1124. has_mac ? skb_mac_header_len(skb) : -1,
  1125. skb->mac_len,
  1126. skb->network_header,
  1127. has_trans ? skb_network_header_len(skb) : -1,
  1128. has_trans ? skb->transport_header : -1,
  1129. sh->tx_flags, sh->nr_frags,
  1130. sh->gso_size, sh->gso_type, sh->gso_segs,
  1131. skb->csum, skb->csum_start, skb->csum_offset, skb->ip_summed,
  1132. skb->csum_complete_sw, skb->csum_valid, skb->csum_level,
  1133. skb->hash, skb->sw_hash, skb->l4_hash,
  1134. ntohs(skb->protocol), skb->pkt_type, skb->skb_iif,
  1135. skb->priority, skb->mark, skb->alloc_cpu, skb->vlan_all,
  1136. skb->encapsulation, skb->inner_protocol, skb->inner_mac_header,
  1137. skb->inner_network_header, skb->inner_transport_header);
  1138. if (dev)
  1139. printk("%sdev name=%s feat=%pNF\n",
  1140. level, dev->name, &dev->features);
  1141. if (sk)
  1142. printk("%ssk family=%hu type=%u proto=%u\n",
  1143. level, sk->sk_family, sk->sk_type, sk->sk_protocol);
  1144. if (full_pkt && headroom)
  1145. print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
  1146. 16, 1, skb->head, headroom, false);
  1147. seg_len = min_t(int, skb_headlen(skb), len);
  1148. if (seg_len)
  1149. print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET,
  1150. 16, 1, skb->data, seg_len, false);
  1151. len -= seg_len;
  1152. if (full_pkt && tailroom)
  1153. print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
  1154. 16, 1, skb_tail_pointer(skb), tailroom, false);
  1155. for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
  1156. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1157. u32 p_off, p_len, copied;
  1158. struct page *p;
  1159. u8 *vaddr;
  1160. if (skb_frag_is_net_iov(frag)) {
  1161. printk("%sskb frag %d: not readable\n", level, i);
  1162. len -= skb_frag_size(frag);
  1163. if (!len)
  1164. break;
  1165. continue;
  1166. }
  1167. skb_frag_foreach_page(frag, skb_frag_off(frag),
  1168. skb_frag_size(frag), p, p_off, p_len,
  1169. copied) {
  1170. seg_len = min_t(int, p_len, len);
  1171. vaddr = kmap_atomic(p);
  1172. print_hex_dump(level, "skb frag: ",
  1173. DUMP_PREFIX_OFFSET,
  1174. 16, 1, vaddr + p_off, seg_len, false);
  1175. kunmap_atomic(vaddr);
  1176. len -= seg_len;
  1177. if (!len)
  1178. break;
  1179. }
  1180. }
  1181. if (full_pkt && skb_has_frag_list(skb)) {
  1182. printk("skb fraglist:\n");
  1183. skb_walk_frags(skb, list_skb)
  1184. skb_dump(level, list_skb, true);
  1185. }
  1186. }
  1187. EXPORT_SYMBOL(skb_dump);
  1188. /**
  1189. * skb_tx_error - report an sk_buff xmit error
  1190. * @skb: buffer that triggered an error
  1191. *
  1192. * Report xmit error if a device callback is tracking this skb.
  1193. * skb must be freed afterwards.
  1194. */
  1195. void skb_tx_error(struct sk_buff *skb)
  1196. {
  1197. if (skb) {
  1198. skb_zcopy_downgrade_managed(skb);
  1199. skb_zcopy_clear(skb, true);
  1200. }
  1201. }
  1202. EXPORT_SYMBOL(skb_tx_error);
  1203. #ifdef CONFIG_TRACEPOINTS
  1204. /**
  1205. * consume_skb - free an skbuff
  1206. * @skb: buffer to free
  1207. *
  1208. * Drop a ref to the buffer and free it if the usage count has hit zero
  1209. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  1210. * is being dropped after a failure and notes that
  1211. */
  1212. void consume_skb(struct sk_buff *skb)
  1213. {
  1214. if (!skb_unref(skb))
  1215. return;
  1216. trace_consume_skb(skb, __builtin_return_address(0));
  1217. __kfree_skb(skb);
  1218. }
  1219. EXPORT_SYMBOL(consume_skb);
  1220. #endif
  1221. /**
  1222. * __consume_stateless_skb - free an skbuff, assuming it is stateless
  1223. * @skb: buffer to free
  1224. *
  1225. * Alike consume_skb(), but this variant assumes that this is the last
  1226. * skb reference and all the head states have been already dropped
  1227. */
  1228. void __consume_stateless_skb(struct sk_buff *skb)
  1229. {
  1230. trace_consume_skb(skb, __builtin_return_address(0));
  1231. skb_release_data(skb, SKB_CONSUMED);
  1232. kfree_skbmem(skb);
  1233. }
  1234. static void napi_skb_cache_put(struct sk_buff *skb)
  1235. {
  1236. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  1237. u32 i;
  1238. if (!kasan_mempool_poison_object(skb))
  1239. return;
  1240. local_lock_nested_bh(&napi_alloc_cache.bh_lock);
  1241. nc->skb_cache[nc->skb_count++] = skb;
  1242. if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
  1243. for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
  1244. kasan_mempool_unpoison_object(nc->skb_cache[i],
  1245. kmem_cache_size(net_hotdata.skbuff_cache));
  1246. kmem_cache_free_bulk(net_hotdata.skbuff_cache, NAPI_SKB_CACHE_HALF,
  1247. nc->skb_cache + NAPI_SKB_CACHE_HALF);
  1248. nc->skb_count = NAPI_SKB_CACHE_HALF;
  1249. }
  1250. local_unlock_nested_bh(&napi_alloc_cache.bh_lock);
  1251. }
  1252. void __napi_kfree_skb(struct sk_buff *skb, enum skb_drop_reason reason)
  1253. {
  1254. skb_release_all(skb, reason);
  1255. napi_skb_cache_put(skb);
  1256. }
  1257. void napi_skb_free_stolen_head(struct sk_buff *skb)
  1258. {
  1259. if (unlikely(skb->slow_gro)) {
  1260. nf_reset_ct(skb);
  1261. skb_dst_drop(skb);
  1262. skb_ext_put(skb);
  1263. skb_orphan(skb);
  1264. skb->slow_gro = 0;
  1265. }
  1266. napi_skb_cache_put(skb);
  1267. }
  1268. void napi_consume_skb(struct sk_buff *skb, int budget)
  1269. {
  1270. /* Zero budget indicate non-NAPI context called us, like netpoll */
  1271. if (unlikely(!budget)) {
  1272. dev_consume_skb_any(skb);
  1273. return;
  1274. }
  1275. DEBUG_NET_WARN_ON_ONCE(!in_softirq());
  1276. if (!skb_unref(skb))
  1277. return;
  1278. /* if reaching here SKB is ready to free */
  1279. trace_consume_skb(skb, __builtin_return_address(0));
  1280. /* if SKB is a clone, don't handle this case */
  1281. if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
  1282. __kfree_skb(skb);
  1283. return;
  1284. }
  1285. skb_release_all(skb, SKB_CONSUMED);
  1286. napi_skb_cache_put(skb);
  1287. }
  1288. EXPORT_SYMBOL(napi_consume_skb);
  1289. /* Make sure a field is contained by headers group */
  1290. #define CHECK_SKB_FIELD(field) \
  1291. BUILD_BUG_ON(offsetof(struct sk_buff, field) != \
  1292. offsetof(struct sk_buff, headers.field)); \
  1293. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  1294. {
  1295. new->tstamp = old->tstamp;
  1296. /* We do not copy old->sk */
  1297. new->dev = old->dev;
  1298. memcpy(new->cb, old->cb, sizeof(old->cb));
  1299. skb_dst_copy(new, old);
  1300. __skb_ext_copy(new, old);
  1301. __nf_copy(new, old, false);
  1302. /* Note : this field could be in the headers group.
  1303. * It is not yet because we do not want to have a 16 bit hole
  1304. */
  1305. new->queue_mapping = old->queue_mapping;
  1306. memcpy(&new->headers, &old->headers, sizeof(new->headers));
  1307. CHECK_SKB_FIELD(protocol);
  1308. CHECK_SKB_FIELD(csum);
  1309. CHECK_SKB_FIELD(hash);
  1310. CHECK_SKB_FIELD(priority);
  1311. CHECK_SKB_FIELD(skb_iif);
  1312. CHECK_SKB_FIELD(vlan_proto);
  1313. CHECK_SKB_FIELD(vlan_tci);
  1314. CHECK_SKB_FIELD(transport_header);
  1315. CHECK_SKB_FIELD(network_header);
  1316. CHECK_SKB_FIELD(mac_header);
  1317. CHECK_SKB_FIELD(inner_protocol);
  1318. CHECK_SKB_FIELD(inner_transport_header);
  1319. CHECK_SKB_FIELD(inner_network_header);
  1320. CHECK_SKB_FIELD(inner_mac_header);
  1321. CHECK_SKB_FIELD(mark);
  1322. #ifdef CONFIG_NETWORK_SECMARK
  1323. CHECK_SKB_FIELD(secmark);
  1324. #endif
  1325. #ifdef CONFIG_NET_RX_BUSY_POLL
  1326. CHECK_SKB_FIELD(napi_id);
  1327. #endif
  1328. CHECK_SKB_FIELD(alloc_cpu);
  1329. #ifdef CONFIG_XPS
  1330. CHECK_SKB_FIELD(sender_cpu);
  1331. #endif
  1332. #ifdef CONFIG_NET_SCHED
  1333. CHECK_SKB_FIELD(tc_index);
  1334. #endif
  1335. }
  1336. /*
  1337. * You should not add any new code to this function. Add it to
  1338. * __copy_skb_header above instead.
  1339. */
  1340. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  1341. {
  1342. #define C(x) n->x = skb->x
  1343. n->next = n->prev = NULL;
  1344. n->sk = NULL;
  1345. __copy_skb_header(n, skb);
  1346. C(len);
  1347. C(data_len);
  1348. C(mac_len);
  1349. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  1350. n->cloned = 1;
  1351. n->nohdr = 0;
  1352. n->peeked = 0;
  1353. C(pfmemalloc);
  1354. C(pp_recycle);
  1355. n->destructor = NULL;
  1356. C(tail);
  1357. C(end);
  1358. C(head);
  1359. C(head_frag);
  1360. C(data);
  1361. C(truesize);
  1362. refcount_set(&n->users, 1);
  1363. atomic_inc(&(skb_shinfo(skb)->dataref));
  1364. skb->cloned = 1;
  1365. return n;
  1366. #undef C
  1367. }
  1368. /**
  1369. * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
  1370. * @first: first sk_buff of the msg
  1371. */
  1372. struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
  1373. {
  1374. struct sk_buff *n;
  1375. n = alloc_skb(0, GFP_ATOMIC);
  1376. if (!n)
  1377. return NULL;
  1378. n->len = first->len;
  1379. n->data_len = first->len;
  1380. n->truesize = first->truesize;
  1381. skb_shinfo(n)->frag_list = first;
  1382. __copy_skb_header(n, first);
  1383. n->destructor = NULL;
  1384. return n;
  1385. }
  1386. EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
  1387. /**
  1388. * skb_morph - morph one skb into another
  1389. * @dst: the skb to receive the contents
  1390. * @src: the skb to supply the contents
  1391. *
  1392. * This is identical to skb_clone except that the target skb is
  1393. * supplied by the user.
  1394. *
  1395. * The target skb is returned upon exit.
  1396. */
  1397. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  1398. {
  1399. skb_release_all(dst, SKB_CONSUMED);
  1400. return __skb_clone(dst, src);
  1401. }
  1402. EXPORT_SYMBOL_GPL(skb_morph);
  1403. int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
  1404. {
  1405. unsigned long max_pg, num_pg, new_pg, old_pg, rlim;
  1406. struct user_struct *user;
  1407. if (capable(CAP_IPC_LOCK) || !size)
  1408. return 0;
  1409. rlim = rlimit(RLIMIT_MEMLOCK);
  1410. if (rlim == RLIM_INFINITY)
  1411. return 0;
  1412. num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
  1413. max_pg = rlim >> PAGE_SHIFT;
  1414. user = mmp->user ? : current_user();
  1415. old_pg = atomic_long_read(&user->locked_vm);
  1416. do {
  1417. new_pg = old_pg + num_pg;
  1418. if (new_pg > max_pg)
  1419. return -ENOBUFS;
  1420. } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg));
  1421. if (!mmp->user) {
  1422. mmp->user = get_uid(user);
  1423. mmp->num_pg = num_pg;
  1424. } else {
  1425. mmp->num_pg += num_pg;
  1426. }
  1427. return 0;
  1428. }
  1429. EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
  1430. void mm_unaccount_pinned_pages(struct mmpin *mmp)
  1431. {
  1432. if (mmp->user) {
  1433. atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
  1434. free_uid(mmp->user);
  1435. }
  1436. }
  1437. EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
  1438. static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
  1439. {
  1440. struct ubuf_info_msgzc *uarg;
  1441. struct sk_buff *skb;
  1442. WARN_ON_ONCE(!in_task());
  1443. skb = sock_omalloc(sk, 0, GFP_KERNEL);
  1444. if (!skb)
  1445. return NULL;
  1446. BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
  1447. uarg = (void *)skb->cb;
  1448. uarg->mmp.user = NULL;
  1449. if (mm_account_pinned_pages(&uarg->mmp, size)) {
  1450. kfree_skb(skb);
  1451. return NULL;
  1452. }
  1453. uarg->ubuf.ops = &msg_zerocopy_ubuf_ops;
  1454. uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
  1455. uarg->len = 1;
  1456. uarg->bytelen = size;
  1457. uarg->zerocopy = 1;
  1458. uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
  1459. refcount_set(&uarg->ubuf.refcnt, 1);
  1460. sock_hold(sk);
  1461. return &uarg->ubuf;
  1462. }
  1463. static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg)
  1464. {
  1465. return container_of((void *)uarg, struct sk_buff, cb);
  1466. }
  1467. struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
  1468. struct ubuf_info *uarg)
  1469. {
  1470. if (uarg) {
  1471. struct ubuf_info_msgzc *uarg_zc;
  1472. const u32 byte_limit = 1 << 19; /* limit to a few TSO */
  1473. u32 bytelen, next;
  1474. /* there might be non MSG_ZEROCOPY users */
  1475. if (uarg->ops != &msg_zerocopy_ubuf_ops)
  1476. return NULL;
  1477. /* realloc only when socket is locked (TCP, UDP cork),
  1478. * so uarg->len and sk_zckey access is serialized
  1479. */
  1480. if (!sock_owned_by_user(sk)) {
  1481. WARN_ON_ONCE(1);
  1482. return NULL;
  1483. }
  1484. uarg_zc = uarg_to_msgzc(uarg);
  1485. bytelen = uarg_zc->bytelen + size;
  1486. if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) {
  1487. /* TCP can create new skb to attach new uarg */
  1488. if (sk->sk_type == SOCK_STREAM)
  1489. goto new_alloc;
  1490. return NULL;
  1491. }
  1492. next = (u32)atomic_read(&sk->sk_zckey);
  1493. if ((u32)(uarg_zc->id + uarg_zc->len) == next) {
  1494. if (mm_account_pinned_pages(&uarg_zc->mmp, size))
  1495. return NULL;
  1496. uarg_zc->len++;
  1497. uarg_zc->bytelen = bytelen;
  1498. atomic_set(&sk->sk_zckey, ++next);
  1499. /* no extra ref when appending to datagram (MSG_MORE) */
  1500. if (sk->sk_type == SOCK_STREAM)
  1501. net_zcopy_get(uarg);
  1502. return uarg;
  1503. }
  1504. }
  1505. new_alloc:
  1506. return msg_zerocopy_alloc(sk, size);
  1507. }
  1508. EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
  1509. static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
  1510. {
  1511. struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
  1512. u32 old_lo, old_hi;
  1513. u64 sum_len;
  1514. old_lo = serr->ee.ee_info;
  1515. old_hi = serr->ee.ee_data;
  1516. sum_len = old_hi - old_lo + 1ULL + len;
  1517. if (sum_len >= (1ULL << 32))
  1518. return false;
  1519. if (lo != old_hi + 1)
  1520. return false;
  1521. serr->ee.ee_data += len;
  1522. return true;
  1523. }
  1524. static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg)
  1525. {
  1526. struct sk_buff *tail, *skb = skb_from_uarg(uarg);
  1527. struct sock_exterr_skb *serr;
  1528. struct sock *sk = skb->sk;
  1529. struct sk_buff_head *q;
  1530. unsigned long flags;
  1531. bool is_zerocopy;
  1532. u32 lo, hi;
  1533. u16 len;
  1534. mm_unaccount_pinned_pages(&uarg->mmp);
  1535. /* if !len, there was only 1 call, and it was aborted
  1536. * so do not queue a completion notification
  1537. */
  1538. if (!uarg->len || sock_flag(sk, SOCK_DEAD))
  1539. goto release;
  1540. len = uarg->len;
  1541. lo = uarg->id;
  1542. hi = uarg->id + len - 1;
  1543. is_zerocopy = uarg->zerocopy;
  1544. serr = SKB_EXT_ERR(skb);
  1545. memset(serr, 0, sizeof(*serr));
  1546. serr->ee.ee_errno = 0;
  1547. serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
  1548. serr->ee.ee_data = hi;
  1549. serr->ee.ee_info = lo;
  1550. if (!is_zerocopy)
  1551. serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
  1552. q = &sk->sk_error_queue;
  1553. spin_lock_irqsave(&q->lock, flags);
  1554. tail = skb_peek_tail(q);
  1555. if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
  1556. !skb_zerocopy_notify_extend(tail, lo, len)) {
  1557. __skb_queue_tail(q, skb);
  1558. skb = NULL;
  1559. }
  1560. spin_unlock_irqrestore(&q->lock, flags);
  1561. sk_error_report(sk);
  1562. release:
  1563. consume_skb(skb);
  1564. sock_put(sk);
  1565. }
  1566. static void msg_zerocopy_complete(struct sk_buff *skb, struct ubuf_info *uarg,
  1567. bool success)
  1568. {
  1569. struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg);
  1570. uarg_zc->zerocopy = uarg_zc->zerocopy & success;
  1571. if (refcount_dec_and_test(&uarg->refcnt))
  1572. __msg_zerocopy_callback(uarg_zc);
  1573. }
  1574. void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
  1575. {
  1576. struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk;
  1577. atomic_dec(&sk->sk_zckey);
  1578. uarg_to_msgzc(uarg)->len--;
  1579. if (have_uref)
  1580. msg_zerocopy_complete(NULL, uarg, true);
  1581. }
  1582. EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
  1583. const struct ubuf_info_ops msg_zerocopy_ubuf_ops = {
  1584. .complete = msg_zerocopy_complete,
  1585. };
  1586. EXPORT_SYMBOL_GPL(msg_zerocopy_ubuf_ops);
  1587. int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
  1588. struct msghdr *msg, int len,
  1589. struct ubuf_info *uarg)
  1590. {
  1591. int err, orig_len = skb->len;
  1592. if (uarg->ops->link_skb) {
  1593. err = uarg->ops->link_skb(skb, uarg);
  1594. if (err)
  1595. return err;
  1596. } else {
  1597. struct ubuf_info *orig_uarg = skb_zcopy(skb);
  1598. /* An skb can only point to one uarg. This edge case happens
  1599. * when TCP appends to an skb, but zerocopy_realloc triggered
  1600. * a new alloc.
  1601. */
  1602. if (orig_uarg && uarg != orig_uarg)
  1603. return -EEXIST;
  1604. }
  1605. err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len);
  1606. if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
  1607. struct sock *save_sk = skb->sk;
  1608. /* Streams do not free skb on error. Reset to prev state. */
  1609. iov_iter_revert(&msg->msg_iter, skb->len - orig_len);
  1610. skb->sk = sk;
  1611. ___pskb_trim(skb, orig_len);
  1612. skb->sk = save_sk;
  1613. return err;
  1614. }
  1615. skb_zcopy_set(skb, uarg, NULL);
  1616. return skb->len - orig_len;
  1617. }
  1618. EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
  1619. void __skb_zcopy_downgrade_managed(struct sk_buff *skb)
  1620. {
  1621. int i;
  1622. skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS;
  1623. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1624. skb_frag_ref(skb, i);
  1625. }
  1626. EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed);
  1627. static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
  1628. gfp_t gfp_mask)
  1629. {
  1630. if (skb_zcopy(orig)) {
  1631. if (skb_zcopy(nskb)) {
  1632. /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
  1633. if (!gfp_mask) {
  1634. WARN_ON_ONCE(1);
  1635. return -ENOMEM;
  1636. }
  1637. if (skb_uarg(nskb) == skb_uarg(orig))
  1638. return 0;
  1639. if (skb_copy_ubufs(nskb, GFP_ATOMIC))
  1640. return -EIO;
  1641. }
  1642. skb_zcopy_set(nskb, skb_uarg(orig), NULL);
  1643. }
  1644. return 0;
  1645. }
  1646. /**
  1647. * skb_copy_ubufs - copy userspace skb frags buffers to kernel
  1648. * @skb: the skb to modify
  1649. * @gfp_mask: allocation priority
  1650. *
  1651. * This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
  1652. * It will copy all frags into kernel and drop the reference
  1653. * to userspace pages.
  1654. *
  1655. * If this function is called from an interrupt gfp_mask() must be
  1656. * %GFP_ATOMIC.
  1657. *
  1658. * Returns 0 on success or a negative error code on failure
  1659. * to allocate kernel memory to copy to.
  1660. */
  1661. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  1662. {
  1663. int num_frags = skb_shinfo(skb)->nr_frags;
  1664. struct page *page, *head = NULL;
  1665. int i, order, psize, new_frags;
  1666. u32 d_off;
  1667. if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
  1668. return -EINVAL;
  1669. if (!skb_frags_readable(skb))
  1670. return -EFAULT;
  1671. if (!num_frags)
  1672. goto release;
  1673. /* We might have to allocate high order pages, so compute what minimum
  1674. * page order is needed.
  1675. */
  1676. order = 0;
  1677. while ((PAGE_SIZE << order) * MAX_SKB_FRAGS < __skb_pagelen(skb))
  1678. order++;
  1679. psize = (PAGE_SIZE << order);
  1680. new_frags = (__skb_pagelen(skb) + psize - 1) >> (PAGE_SHIFT + order);
  1681. for (i = 0; i < new_frags; i++) {
  1682. page = alloc_pages(gfp_mask | __GFP_COMP, order);
  1683. if (!page) {
  1684. while (head) {
  1685. struct page *next = (struct page *)page_private(head);
  1686. put_page(head);
  1687. head = next;
  1688. }
  1689. return -ENOMEM;
  1690. }
  1691. set_page_private(page, (unsigned long)head);
  1692. head = page;
  1693. }
  1694. page = head;
  1695. d_off = 0;
  1696. for (i = 0; i < num_frags; i++) {
  1697. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1698. u32 p_off, p_len, copied;
  1699. struct page *p;
  1700. u8 *vaddr;
  1701. skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
  1702. p, p_off, p_len, copied) {
  1703. u32 copy, done = 0;
  1704. vaddr = kmap_atomic(p);
  1705. while (done < p_len) {
  1706. if (d_off == psize) {
  1707. d_off = 0;
  1708. page = (struct page *)page_private(page);
  1709. }
  1710. copy = min_t(u32, psize - d_off, p_len - done);
  1711. memcpy(page_address(page) + d_off,
  1712. vaddr + p_off + done, copy);
  1713. done += copy;
  1714. d_off += copy;
  1715. }
  1716. kunmap_atomic(vaddr);
  1717. }
  1718. }
  1719. /* skb frags release userspace buffers */
  1720. for (i = 0; i < num_frags; i++)
  1721. skb_frag_unref(skb, i);
  1722. /* skb frags point to kernel buffers */
  1723. for (i = 0; i < new_frags - 1; i++) {
  1724. __skb_fill_netmem_desc(skb, i, page_to_netmem(head), 0, psize);
  1725. head = (struct page *)page_private(head);
  1726. }
  1727. __skb_fill_netmem_desc(skb, new_frags - 1, page_to_netmem(head), 0,
  1728. d_off);
  1729. skb_shinfo(skb)->nr_frags = new_frags;
  1730. release:
  1731. skb_zcopy_clear(skb, false);
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL_GPL(skb_copy_ubufs);
  1735. /**
  1736. * skb_clone - duplicate an sk_buff
  1737. * @skb: buffer to clone
  1738. * @gfp_mask: allocation priority
  1739. *
  1740. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  1741. * copies share the same packet data but not structure. The new
  1742. * buffer has a reference count of 1. If the allocation fails the
  1743. * function returns %NULL otherwise the new buffer is returned.
  1744. *
  1745. * If this function is called from an interrupt gfp_mask() must be
  1746. * %GFP_ATOMIC.
  1747. */
  1748. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  1749. {
  1750. struct sk_buff_fclones *fclones = container_of(skb,
  1751. struct sk_buff_fclones,
  1752. skb1);
  1753. struct sk_buff *n;
  1754. if (skb_orphan_frags(skb, gfp_mask))
  1755. return NULL;
  1756. if (skb->fclone == SKB_FCLONE_ORIG &&
  1757. refcount_read(&fclones->fclone_ref) == 1) {
  1758. n = &fclones->skb2;
  1759. refcount_set(&fclones->fclone_ref, 2);
  1760. n->fclone = SKB_FCLONE_CLONE;
  1761. } else {
  1762. if (skb_pfmemalloc(skb))
  1763. gfp_mask |= __GFP_MEMALLOC;
  1764. n = kmem_cache_alloc(net_hotdata.skbuff_cache, gfp_mask);
  1765. if (!n)
  1766. return NULL;
  1767. n->fclone = SKB_FCLONE_UNAVAILABLE;
  1768. }
  1769. return __skb_clone(n, skb);
  1770. }
  1771. EXPORT_SYMBOL(skb_clone);
  1772. void skb_headers_offset_update(struct sk_buff *skb, int off)
  1773. {
  1774. /* Only adjust this if it actually is csum_start rather than csum */
  1775. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1776. skb->csum_start += off;
  1777. /* {transport,network,mac}_header and tail are relative to skb->head */
  1778. skb->transport_header += off;
  1779. skb->network_header += off;
  1780. if (skb_mac_header_was_set(skb))
  1781. skb->mac_header += off;
  1782. skb->inner_transport_header += off;
  1783. skb->inner_network_header += off;
  1784. skb->inner_mac_header += off;
  1785. }
  1786. EXPORT_SYMBOL(skb_headers_offset_update);
  1787. void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
  1788. {
  1789. __copy_skb_header(new, old);
  1790. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  1791. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  1792. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  1793. }
  1794. EXPORT_SYMBOL(skb_copy_header);
  1795. static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
  1796. {
  1797. if (skb_pfmemalloc(skb))
  1798. return SKB_ALLOC_RX;
  1799. return 0;
  1800. }
  1801. /**
  1802. * skb_copy - create private copy of an sk_buff
  1803. * @skb: buffer to copy
  1804. * @gfp_mask: allocation priority
  1805. *
  1806. * Make a copy of both an &sk_buff and its data. This is used when the
  1807. * caller wishes to modify the data and needs a private copy of the
  1808. * data to alter. Returns %NULL on failure or the pointer to the buffer
  1809. * on success. The returned buffer has a reference count of 1.
  1810. *
  1811. * As by-product this function converts non-linear &sk_buff to linear
  1812. * one, so that &sk_buff becomes completely private and caller is allowed
  1813. * to modify all the data of returned buffer. This means that this
  1814. * function is not recommended for use in circumstances when only
  1815. * header is going to be modified. Use pskb_copy() instead.
  1816. */
  1817. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  1818. {
  1819. struct sk_buff *n;
  1820. unsigned int size;
  1821. int headerlen;
  1822. if (!skb_frags_readable(skb))
  1823. return NULL;
  1824. if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
  1825. return NULL;
  1826. headerlen = skb_headroom(skb);
  1827. size = skb_end_offset(skb) + skb->data_len;
  1828. n = __alloc_skb(size, gfp_mask,
  1829. skb_alloc_rx_flag(skb), NUMA_NO_NODE);
  1830. if (!n)
  1831. return NULL;
  1832. /* Set the data pointer */
  1833. skb_reserve(n, headerlen);
  1834. /* Set the tail pointer and length */
  1835. skb_put(n, skb->len);
  1836. BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
  1837. skb_copy_header(n, skb);
  1838. return n;
  1839. }
  1840. EXPORT_SYMBOL(skb_copy);
  1841. /**
  1842. * __pskb_copy_fclone - create copy of an sk_buff with private head.
  1843. * @skb: buffer to copy
  1844. * @headroom: headroom of new skb
  1845. * @gfp_mask: allocation priority
  1846. * @fclone: if true allocate the copy of the skb from the fclone
  1847. * cache instead of the head cache; it is recommended to set this
  1848. * to true for the cases where the copy will likely be cloned
  1849. *
  1850. * Make a copy of both an &sk_buff and part of its data, located
  1851. * in header. Fragmented data remain shared. This is used when
  1852. * the caller wishes to modify only header of &sk_buff and needs
  1853. * private copy of the header to alter. Returns %NULL on failure
  1854. * or the pointer to the buffer on success.
  1855. * The returned buffer has a reference count of 1.
  1856. */
  1857. struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
  1858. gfp_t gfp_mask, bool fclone)
  1859. {
  1860. unsigned int size = skb_headlen(skb) + headroom;
  1861. int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
  1862. struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
  1863. if (!n)
  1864. goto out;
  1865. /* Set the data pointer */
  1866. skb_reserve(n, headroom);
  1867. /* Set the tail pointer and length */
  1868. skb_put(n, skb_headlen(skb));
  1869. /* Copy the bytes */
  1870. skb_copy_from_linear_data(skb, n->data, n->len);
  1871. n->truesize += skb->data_len;
  1872. n->data_len = skb->data_len;
  1873. n->len = skb->len;
  1874. if (skb_shinfo(skb)->nr_frags) {
  1875. int i;
  1876. if (skb_orphan_frags(skb, gfp_mask) ||
  1877. skb_zerocopy_clone(n, skb, gfp_mask)) {
  1878. kfree_skb(n);
  1879. n = NULL;
  1880. goto out;
  1881. }
  1882. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1883. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  1884. skb_frag_ref(skb, i);
  1885. }
  1886. skb_shinfo(n)->nr_frags = i;
  1887. }
  1888. if (skb_has_frag_list(skb)) {
  1889. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  1890. skb_clone_fraglist(n);
  1891. }
  1892. skb_copy_header(n, skb);
  1893. out:
  1894. return n;
  1895. }
  1896. EXPORT_SYMBOL(__pskb_copy_fclone);
  1897. /**
  1898. * pskb_expand_head - reallocate header of &sk_buff
  1899. * @skb: buffer to reallocate
  1900. * @nhead: room to add at head
  1901. * @ntail: room to add at tail
  1902. * @gfp_mask: allocation priority
  1903. *
  1904. * Expands (or creates identical copy, if @nhead and @ntail are zero)
  1905. * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
  1906. * reference count of 1. Returns zero in the case of success or error,
  1907. * if expansion failed. In the last case, &sk_buff is not changed.
  1908. *
  1909. * All the pointers pointing into skb header may change and must be
  1910. * reloaded after call to this function.
  1911. */
  1912. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  1913. gfp_t gfp_mask)
  1914. {
  1915. unsigned int osize = skb_end_offset(skb);
  1916. unsigned int size = osize + nhead + ntail;
  1917. long off;
  1918. u8 *data;
  1919. int i;
  1920. BUG_ON(nhead < 0);
  1921. BUG_ON(skb_shared(skb));
  1922. skb_zcopy_downgrade_managed(skb);
  1923. if (skb_pfmemalloc(skb))
  1924. gfp_mask |= __GFP_MEMALLOC;
  1925. data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
  1926. if (!data)
  1927. goto nodata;
  1928. size = SKB_WITH_OVERHEAD(size);
  1929. /* Copy only real data... and, alas, header. This should be
  1930. * optimized for the cases when header is void.
  1931. */
  1932. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  1933. memcpy((struct skb_shared_info *)(data + size),
  1934. skb_shinfo(skb),
  1935. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  1936. /*
  1937. * if shinfo is shared we must drop the old head gracefully, but if it
  1938. * is not we can just drop the old head and let the existing refcount
  1939. * be since all we did is relocate the values
  1940. */
  1941. if (skb_cloned(skb)) {
  1942. if (skb_orphan_frags(skb, gfp_mask))
  1943. goto nofrags;
  1944. if (skb_zcopy(skb))
  1945. refcount_inc(&skb_uarg(skb)->refcnt);
  1946. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1947. skb_frag_ref(skb, i);
  1948. if (skb_has_frag_list(skb))
  1949. skb_clone_fraglist(skb);
  1950. skb_release_data(skb, SKB_CONSUMED);
  1951. } else {
  1952. skb_free_head(skb);
  1953. }
  1954. off = (data + nhead) - skb->head;
  1955. skb->head = data;
  1956. skb->head_frag = 0;
  1957. skb->data += off;
  1958. skb_set_end_offset(skb, size);
  1959. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1960. off = nhead;
  1961. #endif
  1962. skb->tail += off;
  1963. skb_headers_offset_update(skb, nhead);
  1964. skb->cloned = 0;
  1965. skb->hdr_len = 0;
  1966. skb->nohdr = 0;
  1967. atomic_set(&skb_shinfo(skb)->dataref, 1);
  1968. skb_metadata_clear(skb);
  1969. /* It is not generally safe to change skb->truesize.
  1970. * For the moment, we really care of rx path, or
  1971. * when skb is orphaned (not attached to a socket).
  1972. */
  1973. if (!skb->sk || skb->destructor == sock_edemux)
  1974. skb->truesize += size - osize;
  1975. return 0;
  1976. nofrags:
  1977. skb_kfree_head(data, size);
  1978. nodata:
  1979. return -ENOMEM;
  1980. }
  1981. EXPORT_SYMBOL(pskb_expand_head);
  1982. /* Make private copy of skb with writable head and some headroom */
  1983. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  1984. {
  1985. struct sk_buff *skb2;
  1986. int delta = headroom - skb_headroom(skb);
  1987. if (delta <= 0)
  1988. skb2 = pskb_copy(skb, GFP_ATOMIC);
  1989. else {
  1990. skb2 = skb_clone(skb, GFP_ATOMIC);
  1991. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  1992. GFP_ATOMIC)) {
  1993. kfree_skb(skb2);
  1994. skb2 = NULL;
  1995. }
  1996. }
  1997. return skb2;
  1998. }
  1999. EXPORT_SYMBOL(skb_realloc_headroom);
  2000. /* Note: We plan to rework this in linux-6.4 */
  2001. int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
  2002. {
  2003. unsigned int saved_end_offset, saved_truesize;
  2004. struct skb_shared_info *shinfo;
  2005. int res;
  2006. saved_end_offset = skb_end_offset(skb);
  2007. saved_truesize = skb->truesize;
  2008. res = pskb_expand_head(skb, 0, 0, pri);
  2009. if (res)
  2010. return res;
  2011. skb->truesize = saved_truesize;
  2012. if (likely(skb_end_offset(skb) == saved_end_offset))
  2013. return 0;
  2014. /* We can not change skb->end if the original or new value
  2015. * is SKB_SMALL_HEAD_HEADROOM, as it might break skb_kfree_head().
  2016. */
  2017. if (saved_end_offset == SKB_SMALL_HEAD_HEADROOM ||
  2018. skb_end_offset(skb) == SKB_SMALL_HEAD_HEADROOM) {
  2019. /* We think this path should not be taken.
  2020. * Add a temporary trace to warn us just in case.
  2021. */
  2022. pr_err_once("__skb_unclone_keeptruesize() skb_end_offset() %u -> %u\n",
  2023. saved_end_offset, skb_end_offset(skb));
  2024. WARN_ON_ONCE(1);
  2025. return 0;
  2026. }
  2027. shinfo = skb_shinfo(skb);
  2028. /* We are about to change back skb->end,
  2029. * we need to move skb_shinfo() to its new location.
  2030. */
  2031. memmove(skb->head + saved_end_offset,
  2032. shinfo,
  2033. offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
  2034. skb_set_end_offset(skb, saved_end_offset);
  2035. return 0;
  2036. }
  2037. /**
  2038. * skb_expand_head - reallocate header of &sk_buff
  2039. * @skb: buffer to reallocate
  2040. * @headroom: needed headroom
  2041. *
  2042. * Unlike skb_realloc_headroom, this one does not allocate a new skb
  2043. * if possible; copies skb->sk to new skb as needed
  2044. * and frees original skb in case of failures.
  2045. *
  2046. * It expect increased headroom and generates warning otherwise.
  2047. */
  2048. struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
  2049. {
  2050. int delta = headroom - skb_headroom(skb);
  2051. int osize = skb_end_offset(skb);
  2052. struct sock *sk = skb->sk;
  2053. if (WARN_ONCE(delta <= 0,
  2054. "%s is expecting an increase in the headroom", __func__))
  2055. return skb;
  2056. delta = SKB_DATA_ALIGN(delta);
  2057. /* pskb_expand_head() might crash, if skb is shared. */
  2058. if (skb_shared(skb) || !is_skb_wmem(skb)) {
  2059. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  2060. if (unlikely(!nskb))
  2061. goto fail;
  2062. if (sk)
  2063. skb_set_owner_w(nskb, sk);
  2064. consume_skb(skb);
  2065. skb = nskb;
  2066. }
  2067. if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
  2068. goto fail;
  2069. if (sk && is_skb_wmem(skb)) {
  2070. delta = skb_end_offset(skb) - osize;
  2071. refcount_add(delta, &sk->sk_wmem_alloc);
  2072. skb->truesize += delta;
  2073. }
  2074. return skb;
  2075. fail:
  2076. kfree_skb(skb);
  2077. return NULL;
  2078. }
  2079. EXPORT_SYMBOL(skb_expand_head);
  2080. /**
  2081. * skb_copy_expand - copy and expand sk_buff
  2082. * @skb: buffer to copy
  2083. * @newheadroom: new free bytes at head
  2084. * @newtailroom: new free bytes at tail
  2085. * @gfp_mask: allocation priority
  2086. *
  2087. * Make a copy of both an &sk_buff and its data and while doing so
  2088. * allocate additional space.
  2089. *
  2090. * This is used when the caller wishes to modify the data and needs a
  2091. * private copy of the data to alter as well as more space for new fields.
  2092. * Returns %NULL on failure or the pointer to the buffer
  2093. * on success. The returned buffer has a reference count of 1.
  2094. *
  2095. * You must pass %GFP_ATOMIC as the allocation priority if this function
  2096. * is called from an interrupt.
  2097. */
  2098. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  2099. int newheadroom, int newtailroom,
  2100. gfp_t gfp_mask)
  2101. {
  2102. /*
  2103. * Allocate the copy buffer
  2104. */
  2105. int head_copy_len, head_copy_off;
  2106. struct sk_buff *n;
  2107. int oldheadroom;
  2108. if (!skb_frags_readable(skb))
  2109. return NULL;
  2110. if (WARN_ON_ONCE(skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST))
  2111. return NULL;
  2112. oldheadroom = skb_headroom(skb);
  2113. n = __alloc_skb(newheadroom + skb->len + newtailroom,
  2114. gfp_mask, skb_alloc_rx_flag(skb),
  2115. NUMA_NO_NODE);
  2116. if (!n)
  2117. return NULL;
  2118. skb_reserve(n, newheadroom);
  2119. /* Set the tail pointer and length */
  2120. skb_put(n, skb->len);
  2121. head_copy_len = oldheadroom;
  2122. head_copy_off = 0;
  2123. if (newheadroom <= head_copy_len)
  2124. head_copy_len = newheadroom;
  2125. else
  2126. head_copy_off = newheadroom - head_copy_len;
  2127. /* Copy the linear header and data. */
  2128. BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  2129. skb->len + head_copy_len));
  2130. skb_copy_header(n, skb);
  2131. skb_headers_offset_update(n, newheadroom - oldheadroom);
  2132. return n;
  2133. }
  2134. EXPORT_SYMBOL(skb_copy_expand);
  2135. /**
  2136. * __skb_pad - zero pad the tail of an skb
  2137. * @skb: buffer to pad
  2138. * @pad: space to pad
  2139. * @free_on_error: free buffer on error
  2140. *
  2141. * Ensure that a buffer is followed by a padding area that is zero
  2142. * filled. Used by network drivers which may DMA or transfer data
  2143. * beyond the buffer end onto the wire.
  2144. *
  2145. * May return error in out of memory cases. The skb is freed on error
  2146. * if @free_on_error is true.
  2147. */
  2148. int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
  2149. {
  2150. int err;
  2151. int ntail;
  2152. /* If the skbuff is non linear tailroom is always zero.. */
  2153. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  2154. memset(skb->data+skb->len, 0, pad);
  2155. return 0;
  2156. }
  2157. ntail = skb->data_len + pad - (skb->end - skb->tail);
  2158. if (likely(skb_cloned(skb) || ntail > 0)) {
  2159. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  2160. if (unlikely(err))
  2161. goto free_skb;
  2162. }
  2163. /* FIXME: The use of this function with non-linear skb's really needs
  2164. * to be audited.
  2165. */
  2166. err = skb_linearize(skb);
  2167. if (unlikely(err))
  2168. goto free_skb;
  2169. memset(skb->data + skb->len, 0, pad);
  2170. return 0;
  2171. free_skb:
  2172. if (free_on_error)
  2173. kfree_skb(skb);
  2174. return err;
  2175. }
  2176. EXPORT_SYMBOL(__skb_pad);
  2177. /**
  2178. * pskb_put - add data to the tail of a potentially fragmented buffer
  2179. * @skb: start of the buffer to use
  2180. * @tail: tail fragment of the buffer to use
  2181. * @len: amount of data to add
  2182. *
  2183. * This function extends the used data area of the potentially
  2184. * fragmented buffer. @tail must be the last fragment of @skb -- or
  2185. * @skb itself. If this would exceed the total buffer size the kernel
  2186. * will panic. A pointer to the first byte of the extra data is
  2187. * returned.
  2188. */
  2189. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  2190. {
  2191. if (tail != skb) {
  2192. skb->data_len += len;
  2193. skb->len += len;
  2194. }
  2195. return skb_put(tail, len);
  2196. }
  2197. EXPORT_SYMBOL_GPL(pskb_put);
  2198. /**
  2199. * skb_put - add data to a buffer
  2200. * @skb: buffer to use
  2201. * @len: amount of data to add
  2202. *
  2203. * This function extends the used data area of the buffer. If this would
  2204. * exceed the total buffer size the kernel will panic. A pointer to the
  2205. * first byte of the extra data is returned.
  2206. */
  2207. void *skb_put(struct sk_buff *skb, unsigned int len)
  2208. {
  2209. void *tmp = skb_tail_pointer(skb);
  2210. SKB_LINEAR_ASSERT(skb);
  2211. skb->tail += len;
  2212. skb->len += len;
  2213. if (unlikely(skb->tail > skb->end))
  2214. skb_over_panic(skb, len, __builtin_return_address(0));
  2215. return tmp;
  2216. }
  2217. EXPORT_SYMBOL(skb_put);
  2218. /**
  2219. * skb_push - add data to the start of a buffer
  2220. * @skb: buffer to use
  2221. * @len: amount of data to add
  2222. *
  2223. * This function extends the used data area of the buffer at the buffer
  2224. * start. If this would exceed the total buffer headroom the kernel will
  2225. * panic. A pointer to the first byte of the extra data is returned.
  2226. */
  2227. void *skb_push(struct sk_buff *skb, unsigned int len)
  2228. {
  2229. skb->data -= len;
  2230. skb->len += len;
  2231. if (unlikely(skb->data < skb->head))
  2232. skb_under_panic(skb, len, __builtin_return_address(0));
  2233. return skb->data;
  2234. }
  2235. EXPORT_SYMBOL(skb_push);
  2236. /**
  2237. * skb_pull - remove data from the start of a buffer
  2238. * @skb: buffer to use
  2239. * @len: amount of data to remove
  2240. *
  2241. * This function removes data from the start of a buffer, returning
  2242. * the memory to the headroom. A pointer to the next data in the buffer
  2243. * is returned. Once the data has been pulled future pushes will overwrite
  2244. * the old data.
  2245. */
  2246. void *skb_pull(struct sk_buff *skb, unsigned int len)
  2247. {
  2248. return skb_pull_inline(skb, len);
  2249. }
  2250. EXPORT_SYMBOL(skb_pull);
  2251. /**
  2252. * skb_pull_data - remove data from the start of a buffer returning its
  2253. * original position.
  2254. * @skb: buffer to use
  2255. * @len: amount of data to remove
  2256. *
  2257. * This function removes data from the start of a buffer, returning
  2258. * the memory to the headroom. A pointer to the original data in the buffer
  2259. * is returned after checking if there is enough data to pull. Once the
  2260. * data has been pulled future pushes will overwrite the old data.
  2261. */
  2262. void *skb_pull_data(struct sk_buff *skb, size_t len)
  2263. {
  2264. void *data = skb->data;
  2265. if (skb->len < len)
  2266. return NULL;
  2267. skb_pull(skb, len);
  2268. return data;
  2269. }
  2270. EXPORT_SYMBOL(skb_pull_data);
  2271. /**
  2272. * skb_trim - remove end from a buffer
  2273. * @skb: buffer to alter
  2274. * @len: new length
  2275. *
  2276. * Cut the length of a buffer down by removing data from the tail. If
  2277. * the buffer is already under the length specified it is not modified.
  2278. * The skb must be linear.
  2279. */
  2280. void skb_trim(struct sk_buff *skb, unsigned int len)
  2281. {
  2282. if (skb->len > len)
  2283. __skb_trim(skb, len);
  2284. }
  2285. EXPORT_SYMBOL(skb_trim);
  2286. /* Trims skb to length len. It can change skb pointers.
  2287. */
  2288. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  2289. {
  2290. struct sk_buff **fragp;
  2291. struct sk_buff *frag;
  2292. int offset = skb_headlen(skb);
  2293. int nfrags = skb_shinfo(skb)->nr_frags;
  2294. int i;
  2295. int err;
  2296. if (skb_cloned(skb) &&
  2297. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  2298. return err;
  2299. i = 0;
  2300. if (offset >= len)
  2301. goto drop_pages;
  2302. for (; i < nfrags; i++) {
  2303. int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2304. if (end < len) {
  2305. offset = end;
  2306. continue;
  2307. }
  2308. skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
  2309. drop_pages:
  2310. skb_shinfo(skb)->nr_frags = i;
  2311. for (; i < nfrags; i++)
  2312. skb_frag_unref(skb, i);
  2313. if (skb_has_frag_list(skb))
  2314. skb_drop_fraglist(skb);
  2315. goto done;
  2316. }
  2317. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  2318. fragp = &frag->next) {
  2319. int end = offset + frag->len;
  2320. if (skb_shared(frag)) {
  2321. struct sk_buff *nfrag;
  2322. nfrag = skb_clone(frag, GFP_ATOMIC);
  2323. if (unlikely(!nfrag))
  2324. return -ENOMEM;
  2325. nfrag->next = frag->next;
  2326. consume_skb(frag);
  2327. frag = nfrag;
  2328. *fragp = frag;
  2329. }
  2330. if (end < len) {
  2331. offset = end;
  2332. continue;
  2333. }
  2334. if (end > len &&
  2335. unlikely((err = pskb_trim(frag, len - offset))))
  2336. return err;
  2337. if (frag->next)
  2338. skb_drop_list(&frag->next);
  2339. break;
  2340. }
  2341. done:
  2342. if (len > skb_headlen(skb)) {
  2343. skb->data_len -= skb->len - len;
  2344. skb->len = len;
  2345. } else {
  2346. skb->len = len;
  2347. skb->data_len = 0;
  2348. skb_set_tail_pointer(skb, len);
  2349. }
  2350. if (!skb->sk || skb->destructor == sock_edemux)
  2351. skb_condense(skb);
  2352. return 0;
  2353. }
  2354. EXPORT_SYMBOL(___pskb_trim);
  2355. /* Note : use pskb_trim_rcsum() instead of calling this directly
  2356. */
  2357. int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
  2358. {
  2359. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  2360. int delta = skb->len - len;
  2361. skb->csum = csum_block_sub(skb->csum,
  2362. skb_checksum(skb, len, delta, 0),
  2363. len);
  2364. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2365. int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
  2366. int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
  2367. if (offset + sizeof(__sum16) > hdlen)
  2368. return -EINVAL;
  2369. }
  2370. return __pskb_trim(skb, len);
  2371. }
  2372. EXPORT_SYMBOL(pskb_trim_rcsum_slow);
  2373. /**
  2374. * __pskb_pull_tail - advance tail of skb header
  2375. * @skb: buffer to reallocate
  2376. * @delta: number of bytes to advance tail
  2377. *
  2378. * The function makes a sense only on a fragmented &sk_buff,
  2379. * it expands header moving its tail forward and copying necessary
  2380. * data from fragmented part.
  2381. *
  2382. * &sk_buff MUST have reference count of 1.
  2383. *
  2384. * Returns %NULL (and &sk_buff does not change) if pull failed
  2385. * or value of new tail of skb in the case of success.
  2386. *
  2387. * All the pointers pointing into skb header may change and must be
  2388. * reloaded after call to this function.
  2389. */
  2390. /* Moves tail of skb head forward, copying data from fragmented part,
  2391. * when it is necessary.
  2392. * 1. It may fail due to malloc failure.
  2393. * 2. It may change skb pointers.
  2394. *
  2395. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  2396. */
  2397. void *__pskb_pull_tail(struct sk_buff *skb, int delta)
  2398. {
  2399. /* If skb has not enough free space at tail, get new one
  2400. * plus 128 bytes for future expansions. If we have enough
  2401. * room at tail, reallocate without expansion only if skb is cloned.
  2402. */
  2403. int i, k, eat = (skb->tail + delta) - skb->end;
  2404. if (!skb_frags_readable(skb))
  2405. return NULL;
  2406. if (eat > 0 || skb_cloned(skb)) {
  2407. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  2408. GFP_ATOMIC))
  2409. return NULL;
  2410. }
  2411. BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
  2412. skb_tail_pointer(skb), delta));
  2413. /* Optimization: no fragments, no reasons to preestimate
  2414. * size of pulled pages. Superb.
  2415. */
  2416. if (!skb_has_frag_list(skb))
  2417. goto pull_pages;
  2418. /* Estimate size of pulled pages. */
  2419. eat = delta;
  2420. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2421. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2422. if (size >= eat)
  2423. goto pull_pages;
  2424. eat -= size;
  2425. }
  2426. /* If we need update frag list, we are in troubles.
  2427. * Certainly, it is possible to add an offset to skb data,
  2428. * but taking into account that pulling is expected to
  2429. * be very rare operation, it is worth to fight against
  2430. * further bloating skb head and crucify ourselves here instead.
  2431. * Pure masohism, indeed. 8)8)
  2432. */
  2433. if (eat) {
  2434. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  2435. struct sk_buff *clone = NULL;
  2436. struct sk_buff *insp = NULL;
  2437. do {
  2438. if (list->len <= eat) {
  2439. /* Eaten as whole. */
  2440. eat -= list->len;
  2441. list = list->next;
  2442. insp = list;
  2443. } else {
  2444. /* Eaten partially. */
  2445. if (skb_is_gso(skb) && !list->head_frag &&
  2446. skb_headlen(list))
  2447. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  2448. if (skb_shared(list)) {
  2449. /* Sucks! We need to fork list. :-( */
  2450. clone = skb_clone(list, GFP_ATOMIC);
  2451. if (!clone)
  2452. return NULL;
  2453. insp = list->next;
  2454. list = clone;
  2455. } else {
  2456. /* This may be pulled without
  2457. * problems. */
  2458. insp = list;
  2459. }
  2460. if (!pskb_pull(list, eat)) {
  2461. kfree_skb(clone);
  2462. return NULL;
  2463. }
  2464. break;
  2465. }
  2466. } while (eat);
  2467. /* Free pulled out fragments. */
  2468. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  2469. skb_shinfo(skb)->frag_list = list->next;
  2470. consume_skb(list);
  2471. }
  2472. /* And insert new clone at head. */
  2473. if (clone) {
  2474. clone->next = list;
  2475. skb_shinfo(skb)->frag_list = clone;
  2476. }
  2477. }
  2478. /* Success! Now we may commit changes to skb data. */
  2479. pull_pages:
  2480. eat = delta;
  2481. k = 0;
  2482. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2483. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2484. if (size <= eat) {
  2485. skb_frag_unref(skb, i);
  2486. eat -= size;
  2487. } else {
  2488. skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
  2489. *frag = skb_shinfo(skb)->frags[i];
  2490. if (eat) {
  2491. skb_frag_off_add(frag, eat);
  2492. skb_frag_size_sub(frag, eat);
  2493. if (!i)
  2494. goto end;
  2495. eat = 0;
  2496. }
  2497. k++;
  2498. }
  2499. }
  2500. skb_shinfo(skb)->nr_frags = k;
  2501. end:
  2502. skb->tail += delta;
  2503. skb->data_len -= delta;
  2504. if (!skb->data_len)
  2505. skb_zcopy_clear(skb, false);
  2506. return skb_tail_pointer(skb);
  2507. }
  2508. EXPORT_SYMBOL(__pskb_pull_tail);
  2509. /**
  2510. * skb_copy_bits - copy bits from skb to kernel buffer
  2511. * @skb: source skb
  2512. * @offset: offset in source
  2513. * @to: destination buffer
  2514. * @len: number of bytes to copy
  2515. *
  2516. * Copy the specified number of bytes from the source skb to the
  2517. * destination buffer.
  2518. *
  2519. * CAUTION ! :
  2520. * If its prototype is ever changed,
  2521. * check arch/{*}/net/{*}.S files,
  2522. * since it is called from BPF assembly code.
  2523. */
  2524. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  2525. {
  2526. int start = skb_headlen(skb);
  2527. struct sk_buff *frag_iter;
  2528. int i, copy;
  2529. if (offset > (int)skb->len - len)
  2530. goto fault;
  2531. /* Copy header. */
  2532. if ((copy = start - offset) > 0) {
  2533. if (copy > len)
  2534. copy = len;
  2535. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  2536. if ((len -= copy) == 0)
  2537. return 0;
  2538. offset += copy;
  2539. to += copy;
  2540. }
  2541. if (!skb_frags_readable(skb))
  2542. goto fault;
  2543. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2544. int end;
  2545. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  2546. WARN_ON(start > offset + len);
  2547. end = start + skb_frag_size(f);
  2548. if ((copy = end - offset) > 0) {
  2549. u32 p_off, p_len, copied;
  2550. struct page *p;
  2551. u8 *vaddr;
  2552. if (copy > len)
  2553. copy = len;
  2554. skb_frag_foreach_page(f,
  2555. skb_frag_off(f) + offset - start,
  2556. copy, p, p_off, p_len, copied) {
  2557. vaddr = kmap_atomic(p);
  2558. memcpy(to + copied, vaddr + p_off, p_len);
  2559. kunmap_atomic(vaddr);
  2560. }
  2561. if ((len -= copy) == 0)
  2562. return 0;
  2563. offset += copy;
  2564. to += copy;
  2565. }
  2566. start = end;
  2567. }
  2568. skb_walk_frags(skb, frag_iter) {
  2569. int end;
  2570. WARN_ON(start > offset + len);
  2571. end = start + frag_iter->len;
  2572. if ((copy = end - offset) > 0) {
  2573. if (copy > len)
  2574. copy = len;
  2575. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  2576. goto fault;
  2577. if ((len -= copy) == 0)
  2578. return 0;
  2579. offset += copy;
  2580. to += copy;
  2581. }
  2582. start = end;
  2583. }
  2584. if (!len)
  2585. return 0;
  2586. fault:
  2587. return -EFAULT;
  2588. }
  2589. EXPORT_SYMBOL(skb_copy_bits);
  2590. /*
  2591. * Callback from splice_to_pipe(), if we need to release some pages
  2592. * at the end of the spd in case we error'ed out in filling the pipe.
  2593. */
  2594. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  2595. {
  2596. put_page(spd->pages[i]);
  2597. }
  2598. static struct page *linear_to_page(struct page *page, unsigned int *len,
  2599. unsigned int *offset,
  2600. struct sock *sk)
  2601. {
  2602. struct page_frag *pfrag = sk_page_frag(sk);
  2603. if (!sk_page_frag_refill(sk, pfrag))
  2604. return NULL;
  2605. *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
  2606. memcpy(page_address(pfrag->page) + pfrag->offset,
  2607. page_address(page) + *offset, *len);
  2608. *offset = pfrag->offset;
  2609. pfrag->offset += *len;
  2610. return pfrag->page;
  2611. }
  2612. static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
  2613. struct page *page,
  2614. unsigned int offset)
  2615. {
  2616. return spd->nr_pages &&
  2617. spd->pages[spd->nr_pages - 1] == page &&
  2618. (spd->partial[spd->nr_pages - 1].offset +
  2619. spd->partial[spd->nr_pages - 1].len == offset);
  2620. }
  2621. /*
  2622. * Fill page/offset/length into spd, if it can hold more pages.
  2623. */
  2624. static bool spd_fill_page(struct splice_pipe_desc *spd,
  2625. struct pipe_inode_info *pipe, struct page *page,
  2626. unsigned int *len, unsigned int offset,
  2627. bool linear,
  2628. struct sock *sk)
  2629. {
  2630. if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
  2631. return true;
  2632. if (linear) {
  2633. page = linear_to_page(page, len, &offset, sk);
  2634. if (!page)
  2635. return true;
  2636. }
  2637. if (spd_can_coalesce(spd, page, offset)) {
  2638. spd->partial[spd->nr_pages - 1].len += *len;
  2639. return false;
  2640. }
  2641. get_page(page);
  2642. spd->pages[spd->nr_pages] = page;
  2643. spd->partial[spd->nr_pages].len = *len;
  2644. spd->partial[spd->nr_pages].offset = offset;
  2645. spd->nr_pages++;
  2646. return false;
  2647. }
  2648. static bool __splice_segment(struct page *page, unsigned int poff,
  2649. unsigned int plen, unsigned int *off,
  2650. unsigned int *len,
  2651. struct splice_pipe_desc *spd, bool linear,
  2652. struct sock *sk,
  2653. struct pipe_inode_info *pipe)
  2654. {
  2655. if (!*len)
  2656. return true;
  2657. /* skip this segment if already processed */
  2658. if (*off >= plen) {
  2659. *off -= plen;
  2660. return false;
  2661. }
  2662. /* ignore any bits we already processed */
  2663. poff += *off;
  2664. plen -= *off;
  2665. *off = 0;
  2666. do {
  2667. unsigned int flen = min(*len, plen);
  2668. if (spd_fill_page(spd, pipe, page, &flen, poff,
  2669. linear, sk))
  2670. return true;
  2671. poff += flen;
  2672. plen -= flen;
  2673. *len -= flen;
  2674. } while (*len && plen);
  2675. return false;
  2676. }
  2677. /*
  2678. * Map linear and fragment data from the skb to spd. It reports true if the
  2679. * pipe is full or if we already spliced the requested length.
  2680. */
  2681. static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  2682. unsigned int *offset, unsigned int *len,
  2683. struct splice_pipe_desc *spd, struct sock *sk)
  2684. {
  2685. int seg;
  2686. struct sk_buff *iter;
  2687. /* map the linear part :
  2688. * If skb->head_frag is set, this 'linear' part is backed by a
  2689. * fragment, and if the head is not shared with any clones then
  2690. * we can avoid a copy since we own the head portion of this page.
  2691. */
  2692. if (__splice_segment(virt_to_page(skb->data),
  2693. (unsigned long) skb->data & (PAGE_SIZE - 1),
  2694. skb_headlen(skb),
  2695. offset, len, spd,
  2696. skb_head_is_locked(skb),
  2697. sk, pipe))
  2698. return true;
  2699. /*
  2700. * then map the fragments
  2701. */
  2702. if (!skb_frags_readable(skb))
  2703. return false;
  2704. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  2705. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  2706. if (WARN_ON_ONCE(!skb_frag_page(f)))
  2707. return false;
  2708. if (__splice_segment(skb_frag_page(f),
  2709. skb_frag_off(f), skb_frag_size(f),
  2710. offset, len, spd, false, sk, pipe))
  2711. return true;
  2712. }
  2713. skb_walk_frags(skb, iter) {
  2714. if (*offset >= iter->len) {
  2715. *offset -= iter->len;
  2716. continue;
  2717. }
  2718. /* __skb_splice_bits() only fails if the output has no room
  2719. * left, so no point in going over the frag_list for the error
  2720. * case.
  2721. */
  2722. if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
  2723. return true;
  2724. }
  2725. return false;
  2726. }
  2727. /*
  2728. * Map data from the skb to a pipe. Should handle both the linear part,
  2729. * the fragments, and the frag list.
  2730. */
  2731. int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
  2732. struct pipe_inode_info *pipe, unsigned int tlen,
  2733. unsigned int flags)
  2734. {
  2735. struct partial_page partial[MAX_SKB_FRAGS];
  2736. struct page *pages[MAX_SKB_FRAGS];
  2737. struct splice_pipe_desc spd = {
  2738. .pages = pages,
  2739. .partial = partial,
  2740. .nr_pages_max = MAX_SKB_FRAGS,
  2741. .ops = &nosteal_pipe_buf_ops,
  2742. .spd_release = sock_spd_release,
  2743. };
  2744. int ret = 0;
  2745. __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
  2746. if (spd.nr_pages)
  2747. ret = splice_to_pipe(pipe, &spd);
  2748. return ret;
  2749. }
  2750. EXPORT_SYMBOL_GPL(skb_splice_bits);
  2751. static int sendmsg_locked(struct sock *sk, struct msghdr *msg)
  2752. {
  2753. struct socket *sock = sk->sk_socket;
  2754. size_t size = msg_data_left(msg);
  2755. if (!sock)
  2756. return -EINVAL;
  2757. if (!sock->ops->sendmsg_locked)
  2758. return sock_no_sendmsg_locked(sk, msg, size);
  2759. return sock->ops->sendmsg_locked(sk, msg, size);
  2760. }
  2761. static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg)
  2762. {
  2763. struct socket *sock = sk->sk_socket;
  2764. if (!sock)
  2765. return -EINVAL;
  2766. return sock_sendmsg(sock, msg);
  2767. }
  2768. typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg);
  2769. static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
  2770. int len, sendmsg_func sendmsg)
  2771. {
  2772. unsigned int orig_len = len;
  2773. struct sk_buff *head = skb;
  2774. unsigned short fragidx;
  2775. int slen, ret;
  2776. do_frag_list:
  2777. /* Deal with head data */
  2778. while (offset < skb_headlen(skb) && len) {
  2779. struct kvec kv;
  2780. struct msghdr msg;
  2781. slen = min_t(int, len, skb_headlen(skb) - offset);
  2782. kv.iov_base = skb->data + offset;
  2783. kv.iov_len = slen;
  2784. memset(&msg, 0, sizeof(msg));
  2785. msg.msg_flags = MSG_DONTWAIT;
  2786. iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, &kv, 1, slen);
  2787. ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
  2788. sendmsg_unlocked, sk, &msg);
  2789. if (ret <= 0)
  2790. goto error;
  2791. offset += ret;
  2792. len -= ret;
  2793. }
  2794. /* All the data was skb head? */
  2795. if (!len)
  2796. goto out;
  2797. /* Make offset relative to start of frags */
  2798. offset -= skb_headlen(skb);
  2799. /* Find where we are in frag list */
  2800. for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  2801. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  2802. if (offset < skb_frag_size(frag))
  2803. break;
  2804. offset -= skb_frag_size(frag);
  2805. }
  2806. for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  2807. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  2808. slen = min_t(size_t, len, skb_frag_size(frag) - offset);
  2809. while (slen) {
  2810. struct bio_vec bvec;
  2811. struct msghdr msg = {
  2812. .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT,
  2813. };
  2814. bvec_set_page(&bvec, skb_frag_page(frag), slen,
  2815. skb_frag_off(frag) + offset);
  2816. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
  2817. slen);
  2818. ret = INDIRECT_CALL_2(sendmsg, sendmsg_locked,
  2819. sendmsg_unlocked, sk, &msg);
  2820. if (ret <= 0)
  2821. goto error;
  2822. len -= ret;
  2823. offset += ret;
  2824. slen -= ret;
  2825. }
  2826. offset = 0;
  2827. }
  2828. if (len) {
  2829. /* Process any frag lists */
  2830. if (skb == head) {
  2831. if (skb_has_frag_list(skb)) {
  2832. skb = skb_shinfo(skb)->frag_list;
  2833. goto do_frag_list;
  2834. }
  2835. } else if (skb->next) {
  2836. skb = skb->next;
  2837. goto do_frag_list;
  2838. }
  2839. }
  2840. out:
  2841. return orig_len - len;
  2842. error:
  2843. return orig_len == len ? ret : orig_len - len;
  2844. }
  2845. /* Send skb data on a socket. Socket must be locked. */
  2846. int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
  2847. int len)
  2848. {
  2849. return __skb_send_sock(sk, skb, offset, len, sendmsg_locked);
  2850. }
  2851. EXPORT_SYMBOL_GPL(skb_send_sock_locked);
  2852. /* Send skb data on a socket. Socket must be unlocked. */
  2853. int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
  2854. {
  2855. return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked);
  2856. }
  2857. /**
  2858. * skb_store_bits - store bits from kernel buffer to skb
  2859. * @skb: destination buffer
  2860. * @offset: offset in destination
  2861. * @from: source buffer
  2862. * @len: number of bytes to copy
  2863. *
  2864. * Copy the specified number of bytes from the source buffer to the
  2865. * destination skb. This function handles all the messy bits of
  2866. * traversing fragment lists and such.
  2867. */
  2868. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  2869. {
  2870. int start = skb_headlen(skb);
  2871. struct sk_buff *frag_iter;
  2872. int i, copy;
  2873. if (offset > (int)skb->len - len)
  2874. goto fault;
  2875. if ((copy = start - offset) > 0) {
  2876. if (copy > len)
  2877. copy = len;
  2878. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  2879. if ((len -= copy) == 0)
  2880. return 0;
  2881. offset += copy;
  2882. from += copy;
  2883. }
  2884. if (!skb_frags_readable(skb))
  2885. goto fault;
  2886. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2887. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2888. int end;
  2889. WARN_ON(start > offset + len);
  2890. end = start + skb_frag_size(frag);
  2891. if ((copy = end - offset) > 0) {
  2892. u32 p_off, p_len, copied;
  2893. struct page *p;
  2894. u8 *vaddr;
  2895. if (copy > len)
  2896. copy = len;
  2897. skb_frag_foreach_page(frag,
  2898. skb_frag_off(frag) + offset - start,
  2899. copy, p, p_off, p_len, copied) {
  2900. vaddr = kmap_atomic(p);
  2901. memcpy(vaddr + p_off, from + copied, p_len);
  2902. kunmap_atomic(vaddr);
  2903. }
  2904. if ((len -= copy) == 0)
  2905. return 0;
  2906. offset += copy;
  2907. from += copy;
  2908. }
  2909. start = end;
  2910. }
  2911. skb_walk_frags(skb, frag_iter) {
  2912. int end;
  2913. WARN_ON(start > offset + len);
  2914. end = start + frag_iter->len;
  2915. if ((copy = end - offset) > 0) {
  2916. if (copy > len)
  2917. copy = len;
  2918. if (skb_store_bits(frag_iter, offset - start,
  2919. from, copy))
  2920. goto fault;
  2921. if ((len -= copy) == 0)
  2922. return 0;
  2923. offset += copy;
  2924. from += copy;
  2925. }
  2926. start = end;
  2927. }
  2928. if (!len)
  2929. return 0;
  2930. fault:
  2931. return -EFAULT;
  2932. }
  2933. EXPORT_SYMBOL(skb_store_bits);
  2934. /* Checksum skb data. */
  2935. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  2936. __wsum csum, const struct skb_checksum_ops *ops)
  2937. {
  2938. int start = skb_headlen(skb);
  2939. int i, copy = start - offset;
  2940. struct sk_buff *frag_iter;
  2941. int pos = 0;
  2942. /* Checksum header. */
  2943. if (copy > 0) {
  2944. if (copy > len)
  2945. copy = len;
  2946. csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
  2947. skb->data + offset, copy, csum);
  2948. if ((len -= copy) == 0)
  2949. return csum;
  2950. offset += copy;
  2951. pos = copy;
  2952. }
  2953. if (WARN_ON_ONCE(!skb_frags_readable(skb)))
  2954. return 0;
  2955. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2956. int end;
  2957. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2958. WARN_ON(start > offset + len);
  2959. end = start + skb_frag_size(frag);
  2960. if ((copy = end - offset) > 0) {
  2961. u32 p_off, p_len, copied;
  2962. struct page *p;
  2963. __wsum csum2;
  2964. u8 *vaddr;
  2965. if (copy > len)
  2966. copy = len;
  2967. skb_frag_foreach_page(frag,
  2968. skb_frag_off(frag) + offset - start,
  2969. copy, p, p_off, p_len, copied) {
  2970. vaddr = kmap_atomic(p);
  2971. csum2 = INDIRECT_CALL_1(ops->update,
  2972. csum_partial_ext,
  2973. vaddr + p_off, p_len, 0);
  2974. kunmap_atomic(vaddr);
  2975. csum = INDIRECT_CALL_1(ops->combine,
  2976. csum_block_add_ext, csum,
  2977. csum2, pos, p_len);
  2978. pos += p_len;
  2979. }
  2980. if (!(len -= copy))
  2981. return csum;
  2982. offset += copy;
  2983. }
  2984. start = end;
  2985. }
  2986. skb_walk_frags(skb, frag_iter) {
  2987. int end;
  2988. WARN_ON(start > offset + len);
  2989. end = start + frag_iter->len;
  2990. if ((copy = end - offset) > 0) {
  2991. __wsum csum2;
  2992. if (copy > len)
  2993. copy = len;
  2994. csum2 = __skb_checksum(frag_iter, offset - start,
  2995. copy, 0, ops);
  2996. csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
  2997. csum, csum2, pos, copy);
  2998. if ((len -= copy) == 0)
  2999. return csum;
  3000. offset += copy;
  3001. pos += copy;
  3002. }
  3003. start = end;
  3004. }
  3005. BUG_ON(len);
  3006. return csum;
  3007. }
  3008. EXPORT_SYMBOL(__skb_checksum);
  3009. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  3010. int len, __wsum csum)
  3011. {
  3012. const struct skb_checksum_ops ops = {
  3013. .update = csum_partial_ext,
  3014. .combine = csum_block_add_ext,
  3015. };
  3016. return __skb_checksum(skb, offset, len, csum, &ops);
  3017. }
  3018. EXPORT_SYMBOL(skb_checksum);
  3019. /* Both of above in one bottle. */
  3020. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  3021. u8 *to, int len)
  3022. {
  3023. int start = skb_headlen(skb);
  3024. int i, copy = start - offset;
  3025. struct sk_buff *frag_iter;
  3026. int pos = 0;
  3027. __wsum csum = 0;
  3028. /* Copy header. */
  3029. if (copy > 0) {
  3030. if (copy > len)
  3031. copy = len;
  3032. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  3033. copy);
  3034. if ((len -= copy) == 0)
  3035. return csum;
  3036. offset += copy;
  3037. to += copy;
  3038. pos = copy;
  3039. }
  3040. if (!skb_frags_readable(skb))
  3041. return 0;
  3042. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3043. int end;
  3044. WARN_ON(start > offset + len);
  3045. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  3046. if ((copy = end - offset) > 0) {
  3047. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  3048. u32 p_off, p_len, copied;
  3049. struct page *p;
  3050. __wsum csum2;
  3051. u8 *vaddr;
  3052. if (copy > len)
  3053. copy = len;
  3054. skb_frag_foreach_page(frag,
  3055. skb_frag_off(frag) + offset - start,
  3056. copy, p, p_off, p_len, copied) {
  3057. vaddr = kmap_atomic(p);
  3058. csum2 = csum_partial_copy_nocheck(vaddr + p_off,
  3059. to + copied,
  3060. p_len);
  3061. kunmap_atomic(vaddr);
  3062. csum = csum_block_add(csum, csum2, pos);
  3063. pos += p_len;
  3064. }
  3065. if (!(len -= copy))
  3066. return csum;
  3067. offset += copy;
  3068. to += copy;
  3069. }
  3070. start = end;
  3071. }
  3072. skb_walk_frags(skb, frag_iter) {
  3073. __wsum csum2;
  3074. int end;
  3075. WARN_ON(start > offset + len);
  3076. end = start + frag_iter->len;
  3077. if ((copy = end - offset) > 0) {
  3078. if (copy > len)
  3079. copy = len;
  3080. csum2 = skb_copy_and_csum_bits(frag_iter,
  3081. offset - start,
  3082. to, copy);
  3083. csum = csum_block_add(csum, csum2, pos);
  3084. if ((len -= copy) == 0)
  3085. return csum;
  3086. offset += copy;
  3087. to += copy;
  3088. pos += copy;
  3089. }
  3090. start = end;
  3091. }
  3092. BUG_ON(len);
  3093. return csum;
  3094. }
  3095. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  3096. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
  3097. {
  3098. __sum16 sum;
  3099. sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
  3100. /* See comments in __skb_checksum_complete(). */
  3101. if (likely(!sum)) {
  3102. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  3103. !skb->csum_complete_sw)
  3104. netdev_rx_csum_fault(skb->dev, skb);
  3105. }
  3106. if (!skb_shared(skb))
  3107. skb->csum_valid = !sum;
  3108. return sum;
  3109. }
  3110. EXPORT_SYMBOL(__skb_checksum_complete_head);
  3111. /* This function assumes skb->csum already holds pseudo header's checksum,
  3112. * which has been changed from the hardware checksum, for example, by
  3113. * __skb_checksum_validate_complete(). And, the original skb->csum must
  3114. * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
  3115. *
  3116. * It returns non-zero if the recomputed checksum is still invalid, otherwise
  3117. * zero. The new checksum is stored back into skb->csum unless the skb is
  3118. * shared.
  3119. */
  3120. __sum16 __skb_checksum_complete(struct sk_buff *skb)
  3121. {
  3122. __wsum csum;
  3123. __sum16 sum;
  3124. csum = skb_checksum(skb, 0, skb->len, 0);
  3125. sum = csum_fold(csum_add(skb->csum, csum));
  3126. /* This check is inverted, because we already knew the hardware
  3127. * checksum is invalid before calling this function. So, if the
  3128. * re-computed checksum is valid instead, then we have a mismatch
  3129. * between the original skb->csum and skb_checksum(). This means either
  3130. * the original hardware checksum is incorrect or we screw up skb->csum
  3131. * when moving skb->data around.
  3132. */
  3133. if (likely(!sum)) {
  3134. if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
  3135. !skb->csum_complete_sw)
  3136. netdev_rx_csum_fault(skb->dev, skb);
  3137. }
  3138. if (!skb_shared(skb)) {
  3139. /* Save full packet checksum */
  3140. skb->csum = csum;
  3141. skb->ip_summed = CHECKSUM_COMPLETE;
  3142. skb->csum_complete_sw = 1;
  3143. skb->csum_valid = !sum;
  3144. }
  3145. return sum;
  3146. }
  3147. EXPORT_SYMBOL(__skb_checksum_complete);
  3148. static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
  3149. {
  3150. net_warn_ratelimited(
  3151. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  3152. __func__);
  3153. return 0;
  3154. }
  3155. static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
  3156. int offset, int len)
  3157. {
  3158. net_warn_ratelimited(
  3159. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  3160. __func__);
  3161. return 0;
  3162. }
  3163. static const struct skb_checksum_ops default_crc32c_ops = {
  3164. .update = warn_crc32c_csum_update,
  3165. .combine = warn_crc32c_csum_combine,
  3166. };
  3167. const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
  3168. &default_crc32c_ops;
  3169. EXPORT_SYMBOL(crc32c_csum_stub);
  3170. /**
  3171. * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
  3172. * @from: source buffer
  3173. *
  3174. * Calculates the amount of linear headroom needed in the 'to' skb passed
  3175. * into skb_zerocopy().
  3176. */
  3177. unsigned int
  3178. skb_zerocopy_headlen(const struct sk_buff *from)
  3179. {
  3180. unsigned int hlen = 0;
  3181. if (!from->head_frag ||
  3182. skb_headlen(from) < L1_CACHE_BYTES ||
  3183. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
  3184. hlen = skb_headlen(from);
  3185. if (!hlen)
  3186. hlen = from->len;
  3187. }
  3188. if (skb_has_frag_list(from))
  3189. hlen = from->len;
  3190. return hlen;
  3191. }
  3192. EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
  3193. /**
  3194. * skb_zerocopy - Zero copy skb to skb
  3195. * @to: destination buffer
  3196. * @from: source buffer
  3197. * @len: number of bytes to copy from source buffer
  3198. * @hlen: size of linear headroom in destination buffer
  3199. *
  3200. * Copies up to `len` bytes from `from` to `to` by creating references
  3201. * to the frags in the source buffer.
  3202. *
  3203. * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
  3204. * headroom in the `to` buffer.
  3205. *
  3206. * Return value:
  3207. * 0: everything is OK
  3208. * -ENOMEM: couldn't orphan frags of @from due to lack of memory
  3209. * -EFAULT: skb_copy_bits() found some problem with skb geometry
  3210. */
  3211. int
  3212. skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
  3213. {
  3214. int i, j = 0;
  3215. int plen = 0; /* length of skb->head fragment */
  3216. int ret;
  3217. struct page *page;
  3218. unsigned int offset;
  3219. BUG_ON(!from->head_frag && !hlen);
  3220. /* dont bother with small payloads */
  3221. if (len <= skb_tailroom(to))
  3222. return skb_copy_bits(from, 0, skb_put(to, len), len);
  3223. if (hlen) {
  3224. ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
  3225. if (unlikely(ret))
  3226. return ret;
  3227. len -= hlen;
  3228. } else {
  3229. plen = min_t(int, skb_headlen(from), len);
  3230. if (plen) {
  3231. page = virt_to_head_page(from->head);
  3232. offset = from->data - (unsigned char *)page_address(page);
  3233. __skb_fill_netmem_desc(to, 0, page_to_netmem(page),
  3234. offset, plen);
  3235. get_page(page);
  3236. j = 1;
  3237. len -= plen;
  3238. }
  3239. }
  3240. skb_len_add(to, len + plen);
  3241. if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
  3242. skb_tx_error(from);
  3243. return -ENOMEM;
  3244. }
  3245. skb_zerocopy_clone(to, from, GFP_ATOMIC);
  3246. for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
  3247. int size;
  3248. if (!len)
  3249. break;
  3250. skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
  3251. size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
  3252. len);
  3253. skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
  3254. len -= size;
  3255. skb_frag_ref(to, j);
  3256. j++;
  3257. }
  3258. skb_shinfo(to)->nr_frags = j;
  3259. return 0;
  3260. }
  3261. EXPORT_SYMBOL_GPL(skb_zerocopy);
  3262. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  3263. {
  3264. __wsum csum;
  3265. long csstart;
  3266. if (skb->ip_summed == CHECKSUM_PARTIAL)
  3267. csstart = skb_checksum_start_offset(skb);
  3268. else
  3269. csstart = skb_headlen(skb);
  3270. BUG_ON(csstart > skb_headlen(skb));
  3271. skb_copy_from_linear_data(skb, to, csstart);
  3272. csum = 0;
  3273. if (csstart != skb->len)
  3274. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  3275. skb->len - csstart);
  3276. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3277. long csstuff = csstart + skb->csum_offset;
  3278. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  3279. }
  3280. }
  3281. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  3282. /**
  3283. * skb_dequeue - remove from the head of the queue
  3284. * @list: list to dequeue from
  3285. *
  3286. * Remove the head of the list. The list lock is taken so the function
  3287. * may be used safely with other locking list functions. The head item is
  3288. * returned or %NULL if the list is empty.
  3289. */
  3290. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  3291. {
  3292. unsigned long flags;
  3293. struct sk_buff *result;
  3294. spin_lock_irqsave(&list->lock, flags);
  3295. result = __skb_dequeue(list);
  3296. spin_unlock_irqrestore(&list->lock, flags);
  3297. return result;
  3298. }
  3299. EXPORT_SYMBOL(skb_dequeue);
  3300. /**
  3301. * skb_dequeue_tail - remove from the tail of the queue
  3302. * @list: list to dequeue from
  3303. *
  3304. * Remove the tail of the list. The list lock is taken so the function
  3305. * may be used safely with other locking list functions. The tail item is
  3306. * returned or %NULL if the list is empty.
  3307. */
  3308. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  3309. {
  3310. unsigned long flags;
  3311. struct sk_buff *result;
  3312. spin_lock_irqsave(&list->lock, flags);
  3313. result = __skb_dequeue_tail(list);
  3314. spin_unlock_irqrestore(&list->lock, flags);
  3315. return result;
  3316. }
  3317. EXPORT_SYMBOL(skb_dequeue_tail);
  3318. /**
  3319. * skb_queue_purge_reason - empty a list
  3320. * @list: list to empty
  3321. * @reason: drop reason
  3322. *
  3323. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  3324. * the list and one reference dropped. This function takes the list
  3325. * lock and is atomic with respect to other list locking functions.
  3326. */
  3327. void skb_queue_purge_reason(struct sk_buff_head *list,
  3328. enum skb_drop_reason reason)
  3329. {
  3330. struct sk_buff_head tmp;
  3331. unsigned long flags;
  3332. if (skb_queue_empty_lockless(list))
  3333. return;
  3334. __skb_queue_head_init(&tmp);
  3335. spin_lock_irqsave(&list->lock, flags);
  3336. skb_queue_splice_init(list, &tmp);
  3337. spin_unlock_irqrestore(&list->lock, flags);
  3338. __skb_queue_purge_reason(&tmp, reason);
  3339. }
  3340. EXPORT_SYMBOL(skb_queue_purge_reason);
  3341. /**
  3342. * skb_rbtree_purge - empty a skb rbtree
  3343. * @root: root of the rbtree to empty
  3344. * Return value: the sum of truesizes of all purged skbs.
  3345. *
  3346. * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
  3347. * the list and one reference dropped. This function does not take
  3348. * any lock. Synchronization should be handled by the caller (e.g., TCP
  3349. * out-of-order queue is protected by the socket lock).
  3350. */
  3351. unsigned int skb_rbtree_purge(struct rb_root *root)
  3352. {
  3353. struct rb_node *p = rb_first(root);
  3354. unsigned int sum = 0;
  3355. while (p) {
  3356. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  3357. p = rb_next(p);
  3358. rb_erase(&skb->rbnode, root);
  3359. sum += skb->truesize;
  3360. kfree_skb(skb);
  3361. }
  3362. return sum;
  3363. }
  3364. void skb_errqueue_purge(struct sk_buff_head *list)
  3365. {
  3366. struct sk_buff *skb, *next;
  3367. struct sk_buff_head kill;
  3368. unsigned long flags;
  3369. __skb_queue_head_init(&kill);
  3370. spin_lock_irqsave(&list->lock, flags);
  3371. skb_queue_walk_safe(list, skb, next) {
  3372. if (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ZEROCOPY ||
  3373. SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_TIMESTAMPING)
  3374. continue;
  3375. __skb_unlink(skb, list);
  3376. __skb_queue_tail(&kill, skb);
  3377. }
  3378. spin_unlock_irqrestore(&list->lock, flags);
  3379. __skb_queue_purge(&kill);
  3380. }
  3381. EXPORT_SYMBOL(skb_errqueue_purge);
  3382. /**
  3383. * skb_queue_head - queue a buffer at the list head
  3384. * @list: list to use
  3385. * @newsk: buffer to queue
  3386. *
  3387. * Queue a buffer at the start of the list. This function takes the
  3388. * list lock and can be used safely with other locking &sk_buff functions
  3389. * safely.
  3390. *
  3391. * A buffer cannot be placed on two lists at the same time.
  3392. */
  3393. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  3394. {
  3395. unsigned long flags;
  3396. spin_lock_irqsave(&list->lock, flags);
  3397. __skb_queue_head(list, newsk);
  3398. spin_unlock_irqrestore(&list->lock, flags);
  3399. }
  3400. EXPORT_SYMBOL(skb_queue_head);
  3401. /**
  3402. * skb_queue_tail - queue a buffer at the list tail
  3403. * @list: list to use
  3404. * @newsk: buffer to queue
  3405. *
  3406. * Queue a buffer at the tail of the list. This function takes the
  3407. * list lock and can be used safely with other locking &sk_buff functions
  3408. * safely.
  3409. *
  3410. * A buffer cannot be placed on two lists at the same time.
  3411. */
  3412. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  3413. {
  3414. unsigned long flags;
  3415. spin_lock_irqsave(&list->lock, flags);
  3416. __skb_queue_tail(list, newsk);
  3417. spin_unlock_irqrestore(&list->lock, flags);
  3418. }
  3419. EXPORT_SYMBOL(skb_queue_tail);
  3420. /**
  3421. * skb_unlink - remove a buffer from a list
  3422. * @skb: buffer to remove
  3423. * @list: list to use
  3424. *
  3425. * Remove a packet from a list. The list locks are taken and this
  3426. * function is atomic with respect to other list locked calls
  3427. *
  3428. * You must know what list the SKB is on.
  3429. */
  3430. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  3431. {
  3432. unsigned long flags;
  3433. spin_lock_irqsave(&list->lock, flags);
  3434. __skb_unlink(skb, list);
  3435. spin_unlock_irqrestore(&list->lock, flags);
  3436. }
  3437. EXPORT_SYMBOL(skb_unlink);
  3438. /**
  3439. * skb_append - append a buffer
  3440. * @old: buffer to insert after
  3441. * @newsk: buffer to insert
  3442. * @list: list to use
  3443. *
  3444. * Place a packet after a given packet in a list. The list locks are taken
  3445. * and this function is atomic with respect to other list locked calls.
  3446. * A buffer cannot be placed on two lists at the same time.
  3447. */
  3448. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  3449. {
  3450. unsigned long flags;
  3451. spin_lock_irqsave(&list->lock, flags);
  3452. __skb_queue_after(list, old, newsk);
  3453. spin_unlock_irqrestore(&list->lock, flags);
  3454. }
  3455. EXPORT_SYMBOL(skb_append);
  3456. static inline void skb_split_inside_header(struct sk_buff *skb,
  3457. struct sk_buff* skb1,
  3458. const u32 len, const int pos)
  3459. {
  3460. int i;
  3461. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  3462. pos - len);
  3463. /* And move data appendix as is. */
  3464. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  3465. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  3466. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  3467. skb1->unreadable = skb->unreadable;
  3468. skb_shinfo(skb)->nr_frags = 0;
  3469. skb1->data_len = skb->data_len;
  3470. skb1->len += skb1->data_len;
  3471. skb->data_len = 0;
  3472. skb->len = len;
  3473. skb_set_tail_pointer(skb, len);
  3474. }
  3475. static inline void skb_split_no_header(struct sk_buff *skb,
  3476. struct sk_buff* skb1,
  3477. const u32 len, int pos)
  3478. {
  3479. int i, k = 0;
  3480. const int nfrags = skb_shinfo(skb)->nr_frags;
  3481. skb_shinfo(skb)->nr_frags = 0;
  3482. skb1->len = skb1->data_len = skb->len - len;
  3483. skb->len = len;
  3484. skb->data_len = len - pos;
  3485. for (i = 0; i < nfrags; i++) {
  3486. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  3487. if (pos + size > len) {
  3488. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  3489. if (pos < len) {
  3490. /* Split frag.
  3491. * We have two variants in this case:
  3492. * 1. Move all the frag to the second
  3493. * part, if it is possible. F.e.
  3494. * this approach is mandatory for TUX,
  3495. * where splitting is expensive.
  3496. * 2. Split is accurately. We make this.
  3497. */
  3498. skb_frag_ref(skb, i);
  3499. skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
  3500. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  3501. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  3502. skb_shinfo(skb)->nr_frags++;
  3503. }
  3504. k++;
  3505. } else
  3506. skb_shinfo(skb)->nr_frags++;
  3507. pos += size;
  3508. }
  3509. skb_shinfo(skb1)->nr_frags = k;
  3510. skb1->unreadable = skb->unreadable;
  3511. }
  3512. /**
  3513. * skb_split - Split fragmented skb to two parts at length len.
  3514. * @skb: the buffer to split
  3515. * @skb1: the buffer to receive the second part
  3516. * @len: new length for skb
  3517. */
  3518. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  3519. {
  3520. int pos = skb_headlen(skb);
  3521. const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY;
  3522. skb_zcopy_downgrade_managed(skb);
  3523. skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags;
  3524. skb_zerocopy_clone(skb1, skb, 0);
  3525. if (len < pos) /* Split line is inside header. */
  3526. skb_split_inside_header(skb, skb1, len, pos);
  3527. else /* Second chunk has no header, nothing to copy. */
  3528. skb_split_no_header(skb, skb1, len, pos);
  3529. }
  3530. EXPORT_SYMBOL(skb_split);
  3531. /* Shifting from/to a cloned skb is a no-go.
  3532. *
  3533. * Caller cannot keep skb_shinfo related pointers past calling here!
  3534. */
  3535. static int skb_prepare_for_shift(struct sk_buff *skb)
  3536. {
  3537. return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
  3538. }
  3539. /**
  3540. * skb_shift - Shifts paged data partially from skb to another
  3541. * @tgt: buffer into which tail data gets added
  3542. * @skb: buffer from which the paged data comes from
  3543. * @shiftlen: shift up to this many bytes
  3544. *
  3545. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  3546. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  3547. * It's up to caller to free skb if everything was shifted.
  3548. *
  3549. * If @tgt runs out of frags, the whole operation is aborted.
  3550. *
  3551. * Skb cannot include anything else but paged data while tgt is allowed
  3552. * to have non-paged data as well.
  3553. *
  3554. * TODO: full sized shift could be optimized but that would need
  3555. * specialized skb free'er to handle frags without up-to-date nr_frags.
  3556. */
  3557. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  3558. {
  3559. int from, to, merge, todo;
  3560. skb_frag_t *fragfrom, *fragto;
  3561. BUG_ON(shiftlen > skb->len);
  3562. if (skb_headlen(skb))
  3563. return 0;
  3564. if (skb_zcopy(tgt) || skb_zcopy(skb))
  3565. return 0;
  3566. DEBUG_NET_WARN_ON_ONCE(tgt->pp_recycle != skb->pp_recycle);
  3567. DEBUG_NET_WARN_ON_ONCE(skb_cmp_decrypted(tgt, skb));
  3568. todo = shiftlen;
  3569. from = 0;
  3570. to = skb_shinfo(tgt)->nr_frags;
  3571. fragfrom = &skb_shinfo(skb)->frags[from];
  3572. /* Actual merge is delayed until the point when we know we can
  3573. * commit all, so that we don't have to undo partial changes
  3574. */
  3575. if (!skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  3576. skb_frag_off(fragfrom))) {
  3577. merge = -1;
  3578. } else {
  3579. merge = to - 1;
  3580. todo -= skb_frag_size(fragfrom);
  3581. if (todo < 0) {
  3582. if (skb_prepare_for_shift(skb) ||
  3583. skb_prepare_for_shift(tgt))
  3584. return 0;
  3585. /* All previous frag pointers might be stale! */
  3586. fragfrom = &skb_shinfo(skb)->frags[from];
  3587. fragto = &skb_shinfo(tgt)->frags[merge];
  3588. skb_frag_size_add(fragto, shiftlen);
  3589. skb_frag_size_sub(fragfrom, shiftlen);
  3590. skb_frag_off_add(fragfrom, shiftlen);
  3591. goto onlymerged;
  3592. }
  3593. from++;
  3594. }
  3595. /* Skip full, not-fitting skb to avoid expensive operations */
  3596. if ((shiftlen == skb->len) &&
  3597. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  3598. return 0;
  3599. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  3600. return 0;
  3601. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  3602. if (to == MAX_SKB_FRAGS)
  3603. return 0;
  3604. fragfrom = &skb_shinfo(skb)->frags[from];
  3605. fragto = &skb_shinfo(tgt)->frags[to];
  3606. if (todo >= skb_frag_size(fragfrom)) {
  3607. *fragto = *fragfrom;
  3608. todo -= skb_frag_size(fragfrom);
  3609. from++;
  3610. to++;
  3611. } else {
  3612. __skb_frag_ref(fragfrom);
  3613. skb_frag_page_copy(fragto, fragfrom);
  3614. skb_frag_off_copy(fragto, fragfrom);
  3615. skb_frag_size_set(fragto, todo);
  3616. skb_frag_off_add(fragfrom, todo);
  3617. skb_frag_size_sub(fragfrom, todo);
  3618. todo = 0;
  3619. to++;
  3620. break;
  3621. }
  3622. }
  3623. /* Ready to "commit" this state change to tgt */
  3624. skb_shinfo(tgt)->nr_frags = to;
  3625. if (merge >= 0) {
  3626. fragfrom = &skb_shinfo(skb)->frags[0];
  3627. fragto = &skb_shinfo(tgt)->frags[merge];
  3628. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  3629. __skb_frag_unref(fragfrom, skb->pp_recycle);
  3630. }
  3631. /* Reposition in the original skb */
  3632. to = 0;
  3633. while (from < skb_shinfo(skb)->nr_frags)
  3634. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  3635. skb_shinfo(skb)->nr_frags = to;
  3636. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  3637. onlymerged:
  3638. /* Most likely the tgt won't ever need its checksum anymore, skb on
  3639. * the other hand might need it if it needs to be resent
  3640. */
  3641. tgt->ip_summed = CHECKSUM_PARTIAL;
  3642. skb->ip_summed = CHECKSUM_PARTIAL;
  3643. skb_len_add(skb, -shiftlen);
  3644. skb_len_add(tgt, shiftlen);
  3645. return shiftlen;
  3646. }
  3647. /**
  3648. * skb_prepare_seq_read - Prepare a sequential read of skb data
  3649. * @skb: the buffer to read
  3650. * @from: lower offset of data to be read
  3651. * @to: upper offset of data to be read
  3652. * @st: state variable
  3653. *
  3654. * Initializes the specified state variable. Must be called before
  3655. * invoking skb_seq_read() for the first time.
  3656. */
  3657. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  3658. unsigned int to, struct skb_seq_state *st)
  3659. {
  3660. st->lower_offset = from;
  3661. st->upper_offset = to;
  3662. st->root_skb = st->cur_skb = skb;
  3663. st->frag_idx = st->stepped_offset = 0;
  3664. st->frag_data = NULL;
  3665. st->frag_off = 0;
  3666. }
  3667. EXPORT_SYMBOL(skb_prepare_seq_read);
  3668. /**
  3669. * skb_seq_read - Sequentially read skb data
  3670. * @consumed: number of bytes consumed by the caller so far
  3671. * @data: destination pointer for data to be returned
  3672. * @st: state variable
  3673. *
  3674. * Reads a block of skb data at @consumed relative to the
  3675. * lower offset specified to skb_prepare_seq_read(). Assigns
  3676. * the head of the data block to @data and returns the length
  3677. * of the block or 0 if the end of the skb data or the upper
  3678. * offset has been reached.
  3679. *
  3680. * The caller is not required to consume all of the data
  3681. * returned, i.e. @consumed is typically set to the number
  3682. * of bytes already consumed and the next call to
  3683. * skb_seq_read() will return the remaining part of the block.
  3684. *
  3685. * Note 1: The size of each block of data returned can be arbitrary,
  3686. * this limitation is the cost for zerocopy sequential
  3687. * reads of potentially non linear data.
  3688. *
  3689. * Note 2: Fragment lists within fragments are not implemented
  3690. * at the moment, state->root_skb could be replaced with
  3691. * a stack for this purpose.
  3692. */
  3693. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  3694. struct skb_seq_state *st)
  3695. {
  3696. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  3697. skb_frag_t *frag;
  3698. if (unlikely(abs_offset >= st->upper_offset)) {
  3699. if (st->frag_data) {
  3700. kunmap_atomic(st->frag_data);
  3701. st->frag_data = NULL;
  3702. }
  3703. return 0;
  3704. }
  3705. next_skb:
  3706. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  3707. if (abs_offset < block_limit && !st->frag_data) {
  3708. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  3709. return block_limit - abs_offset;
  3710. }
  3711. if (!skb_frags_readable(st->cur_skb))
  3712. return 0;
  3713. if (st->frag_idx == 0 && !st->frag_data)
  3714. st->stepped_offset += skb_headlen(st->cur_skb);
  3715. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  3716. unsigned int pg_idx, pg_off, pg_sz;
  3717. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  3718. pg_idx = 0;
  3719. pg_off = skb_frag_off(frag);
  3720. pg_sz = skb_frag_size(frag);
  3721. if (skb_frag_must_loop(skb_frag_page(frag))) {
  3722. pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
  3723. pg_off = offset_in_page(pg_off + st->frag_off);
  3724. pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
  3725. PAGE_SIZE - pg_off);
  3726. }
  3727. block_limit = pg_sz + st->stepped_offset;
  3728. if (abs_offset < block_limit) {
  3729. if (!st->frag_data)
  3730. st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
  3731. *data = (u8 *)st->frag_data + pg_off +
  3732. (abs_offset - st->stepped_offset);
  3733. return block_limit - abs_offset;
  3734. }
  3735. if (st->frag_data) {
  3736. kunmap_atomic(st->frag_data);
  3737. st->frag_data = NULL;
  3738. }
  3739. st->stepped_offset += pg_sz;
  3740. st->frag_off += pg_sz;
  3741. if (st->frag_off == skb_frag_size(frag)) {
  3742. st->frag_off = 0;
  3743. st->frag_idx++;
  3744. }
  3745. }
  3746. if (st->frag_data) {
  3747. kunmap_atomic(st->frag_data);
  3748. st->frag_data = NULL;
  3749. }
  3750. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  3751. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  3752. st->frag_idx = 0;
  3753. goto next_skb;
  3754. } else if (st->cur_skb->next) {
  3755. st->cur_skb = st->cur_skb->next;
  3756. st->frag_idx = 0;
  3757. goto next_skb;
  3758. }
  3759. return 0;
  3760. }
  3761. EXPORT_SYMBOL(skb_seq_read);
  3762. /**
  3763. * skb_abort_seq_read - Abort a sequential read of skb data
  3764. * @st: state variable
  3765. *
  3766. * Must be called if skb_seq_read() was not called until it
  3767. * returned 0.
  3768. */
  3769. void skb_abort_seq_read(struct skb_seq_state *st)
  3770. {
  3771. if (st->frag_data)
  3772. kunmap_atomic(st->frag_data);
  3773. }
  3774. EXPORT_SYMBOL(skb_abort_seq_read);
  3775. /**
  3776. * skb_copy_seq_read() - copy from a skb_seq_state to a buffer
  3777. * @st: source skb_seq_state
  3778. * @offset: offset in source
  3779. * @to: destination buffer
  3780. * @len: number of bytes to copy
  3781. *
  3782. * Copy @len bytes from @offset bytes into the source @st to the destination
  3783. * buffer @to. `offset` should increase (or be unchanged) with each subsequent
  3784. * call to this function. If offset needs to decrease from the previous use `st`
  3785. * should be reset first.
  3786. *
  3787. * Return: 0 on success or -EINVAL if the copy ended early
  3788. */
  3789. int skb_copy_seq_read(struct skb_seq_state *st, int offset, void *to, int len)
  3790. {
  3791. const u8 *data;
  3792. u32 sqlen;
  3793. for (;;) {
  3794. sqlen = skb_seq_read(offset, &data, st);
  3795. if (sqlen == 0)
  3796. return -EINVAL;
  3797. if (sqlen >= len) {
  3798. memcpy(to, data, len);
  3799. return 0;
  3800. }
  3801. memcpy(to, data, sqlen);
  3802. to += sqlen;
  3803. offset += sqlen;
  3804. len -= sqlen;
  3805. }
  3806. }
  3807. EXPORT_SYMBOL(skb_copy_seq_read);
  3808. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  3809. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  3810. struct ts_config *conf,
  3811. struct ts_state *state)
  3812. {
  3813. return skb_seq_read(offset, text, TS_SKB_CB(state));
  3814. }
  3815. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  3816. {
  3817. skb_abort_seq_read(TS_SKB_CB(state));
  3818. }
  3819. /**
  3820. * skb_find_text - Find a text pattern in skb data
  3821. * @skb: the buffer to look in
  3822. * @from: search offset
  3823. * @to: search limit
  3824. * @config: textsearch configuration
  3825. *
  3826. * Finds a pattern in the skb data according to the specified
  3827. * textsearch configuration. Use textsearch_next() to retrieve
  3828. * subsequent occurrences of the pattern. Returns the offset
  3829. * to the first occurrence or UINT_MAX if no match was found.
  3830. */
  3831. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  3832. unsigned int to, struct ts_config *config)
  3833. {
  3834. unsigned int patlen = config->ops->get_pattern_len(config);
  3835. struct ts_state state;
  3836. unsigned int ret;
  3837. BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
  3838. config->get_next_block = skb_ts_get_next_block;
  3839. config->finish = skb_ts_finish;
  3840. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
  3841. ret = textsearch_find(config, &state);
  3842. return (ret + patlen <= to - from ? ret : UINT_MAX);
  3843. }
  3844. EXPORT_SYMBOL(skb_find_text);
  3845. int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
  3846. int offset, size_t size, size_t max_frags)
  3847. {
  3848. int i = skb_shinfo(skb)->nr_frags;
  3849. if (skb_can_coalesce(skb, i, page, offset)) {
  3850. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
  3851. } else if (i < max_frags) {
  3852. skb_zcopy_downgrade_managed(skb);
  3853. get_page(page);
  3854. skb_fill_page_desc_noacc(skb, i, page, offset, size);
  3855. } else {
  3856. return -EMSGSIZE;
  3857. }
  3858. return 0;
  3859. }
  3860. EXPORT_SYMBOL_GPL(skb_append_pagefrags);
  3861. /**
  3862. * skb_pull_rcsum - pull skb and update receive checksum
  3863. * @skb: buffer to update
  3864. * @len: length of data pulled
  3865. *
  3866. * This function performs an skb_pull on the packet and updates
  3867. * the CHECKSUM_COMPLETE checksum. It should be used on
  3868. * receive path processing instead of skb_pull unless you know
  3869. * that the checksum difference is zero (e.g., a valid IP header)
  3870. * or you are setting ip_summed to CHECKSUM_NONE.
  3871. */
  3872. void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  3873. {
  3874. unsigned char *data = skb->data;
  3875. BUG_ON(len > skb->len);
  3876. __skb_pull(skb, len);
  3877. skb_postpull_rcsum(skb, data, len);
  3878. return skb->data;
  3879. }
  3880. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  3881. static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
  3882. {
  3883. skb_frag_t head_frag;
  3884. struct page *page;
  3885. page = virt_to_head_page(frag_skb->head);
  3886. skb_frag_fill_page_desc(&head_frag, page, frag_skb->data -
  3887. (unsigned char *)page_address(page),
  3888. skb_headlen(frag_skb));
  3889. return head_frag;
  3890. }
  3891. struct sk_buff *skb_segment_list(struct sk_buff *skb,
  3892. netdev_features_t features,
  3893. unsigned int offset)
  3894. {
  3895. struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
  3896. unsigned int tnl_hlen = skb_tnl_header_len(skb);
  3897. unsigned int delta_truesize = 0;
  3898. unsigned int delta_len = 0;
  3899. struct sk_buff *tail = NULL;
  3900. struct sk_buff *nskb, *tmp;
  3901. int len_diff, err;
  3902. skb_push(skb, -skb_network_offset(skb) + offset);
  3903. /* Ensure the head is writeable before touching the shared info */
  3904. err = skb_unclone(skb, GFP_ATOMIC);
  3905. if (err)
  3906. goto err_linearize;
  3907. skb_shinfo(skb)->frag_list = NULL;
  3908. while (list_skb) {
  3909. nskb = list_skb;
  3910. list_skb = list_skb->next;
  3911. err = 0;
  3912. delta_truesize += nskb->truesize;
  3913. if (skb_shared(nskb)) {
  3914. tmp = skb_clone(nskb, GFP_ATOMIC);
  3915. if (tmp) {
  3916. consume_skb(nskb);
  3917. nskb = tmp;
  3918. err = skb_unclone(nskb, GFP_ATOMIC);
  3919. } else {
  3920. err = -ENOMEM;
  3921. }
  3922. }
  3923. if (!tail)
  3924. skb->next = nskb;
  3925. else
  3926. tail->next = nskb;
  3927. if (unlikely(err)) {
  3928. nskb->next = list_skb;
  3929. goto err_linearize;
  3930. }
  3931. tail = nskb;
  3932. delta_len += nskb->len;
  3933. skb_push(nskb, -skb_network_offset(nskb) + offset);
  3934. skb_release_head_state(nskb);
  3935. len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
  3936. __copy_skb_header(nskb, skb);
  3937. skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
  3938. nskb->transport_header += len_diff;
  3939. skb_copy_from_linear_data_offset(skb, -tnl_hlen,
  3940. nskb->data - tnl_hlen,
  3941. offset + tnl_hlen);
  3942. if (skb_needs_linearize(nskb, features) &&
  3943. __skb_linearize(nskb))
  3944. goto err_linearize;
  3945. }
  3946. skb->truesize = skb->truesize - delta_truesize;
  3947. skb->data_len = skb->data_len - delta_len;
  3948. skb->len = skb->len - delta_len;
  3949. skb_gso_reset(skb);
  3950. skb->prev = tail;
  3951. if (skb_needs_linearize(skb, features) &&
  3952. __skb_linearize(skb))
  3953. goto err_linearize;
  3954. skb_get(skb);
  3955. return skb;
  3956. err_linearize:
  3957. kfree_skb_list(skb->next);
  3958. skb->next = NULL;
  3959. return ERR_PTR(-ENOMEM);
  3960. }
  3961. EXPORT_SYMBOL_GPL(skb_segment_list);
  3962. /**
  3963. * skb_segment - Perform protocol segmentation on skb.
  3964. * @head_skb: buffer to segment
  3965. * @features: features for the output path (see dev->features)
  3966. *
  3967. * This function performs segmentation on the given skb. It returns
  3968. * a pointer to the first in a list of new skbs for the segments.
  3969. * In case of error it returns ERR_PTR(err).
  3970. */
  3971. struct sk_buff *skb_segment(struct sk_buff *head_skb,
  3972. netdev_features_t features)
  3973. {
  3974. struct sk_buff *segs = NULL;
  3975. struct sk_buff *tail = NULL;
  3976. struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
  3977. unsigned int mss = skb_shinfo(head_skb)->gso_size;
  3978. unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
  3979. unsigned int offset = doffset;
  3980. unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
  3981. unsigned int partial_segs = 0;
  3982. unsigned int headroom;
  3983. unsigned int len = head_skb->len;
  3984. struct sk_buff *frag_skb;
  3985. skb_frag_t *frag;
  3986. __be16 proto;
  3987. bool csum, sg;
  3988. int err = -ENOMEM;
  3989. int i = 0;
  3990. int nfrags, pos;
  3991. if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
  3992. mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
  3993. struct sk_buff *check_skb;
  3994. for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
  3995. if (skb_headlen(check_skb) && !check_skb->head_frag) {
  3996. /* gso_size is untrusted, and we have a frag_list with
  3997. * a linear non head_frag item.
  3998. *
  3999. * If head_skb's headlen does not fit requested gso_size,
  4000. * it means that the frag_list members do NOT terminate
  4001. * on exact gso_size boundaries. Hence we cannot perform
  4002. * skb_frag_t page sharing. Therefore we must fallback to
  4003. * copying the frag_list skbs; we do so by disabling SG.
  4004. */
  4005. features &= ~NETIF_F_SG;
  4006. break;
  4007. }
  4008. }
  4009. }
  4010. __skb_push(head_skb, doffset);
  4011. proto = skb_network_protocol(head_skb, NULL);
  4012. if (unlikely(!proto))
  4013. return ERR_PTR(-EINVAL);
  4014. sg = !!(features & NETIF_F_SG);
  4015. csum = !!can_checksum_protocol(features, proto);
  4016. if (sg && csum && (mss != GSO_BY_FRAGS)) {
  4017. if (!(features & NETIF_F_GSO_PARTIAL)) {
  4018. struct sk_buff *iter;
  4019. unsigned int frag_len;
  4020. if (!list_skb ||
  4021. !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
  4022. goto normal;
  4023. /* If we get here then all the required
  4024. * GSO features except frag_list are supported.
  4025. * Try to split the SKB to multiple GSO SKBs
  4026. * with no frag_list.
  4027. * Currently we can do that only when the buffers don't
  4028. * have a linear part and all the buffers except
  4029. * the last are of the same length.
  4030. */
  4031. frag_len = list_skb->len;
  4032. skb_walk_frags(head_skb, iter) {
  4033. if (frag_len != iter->len && iter->next)
  4034. goto normal;
  4035. if (skb_headlen(iter) && !iter->head_frag)
  4036. goto normal;
  4037. len -= iter->len;
  4038. }
  4039. if (len != frag_len)
  4040. goto normal;
  4041. }
  4042. /* GSO partial only requires that we trim off any excess that
  4043. * doesn't fit into an MSS sized block, so take care of that
  4044. * now.
  4045. * Cap len to not accidentally hit GSO_BY_FRAGS.
  4046. */
  4047. partial_segs = min(len, GSO_BY_FRAGS - 1) / mss;
  4048. if (partial_segs > 1)
  4049. mss *= partial_segs;
  4050. else
  4051. partial_segs = 0;
  4052. }
  4053. normal:
  4054. headroom = skb_headroom(head_skb);
  4055. pos = skb_headlen(head_skb);
  4056. if (skb_orphan_frags(head_skb, GFP_ATOMIC))
  4057. return ERR_PTR(-ENOMEM);
  4058. nfrags = skb_shinfo(head_skb)->nr_frags;
  4059. frag = skb_shinfo(head_skb)->frags;
  4060. frag_skb = head_skb;
  4061. do {
  4062. struct sk_buff *nskb;
  4063. skb_frag_t *nskb_frag;
  4064. int hsize;
  4065. int size;
  4066. if (unlikely(mss == GSO_BY_FRAGS)) {
  4067. len = list_skb->len;
  4068. } else {
  4069. len = head_skb->len - offset;
  4070. if (len > mss)
  4071. len = mss;
  4072. }
  4073. hsize = skb_headlen(head_skb) - offset;
  4074. if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
  4075. (skb_headlen(list_skb) == len || sg)) {
  4076. BUG_ON(skb_headlen(list_skb) > len);
  4077. nskb = skb_clone(list_skb, GFP_ATOMIC);
  4078. if (unlikely(!nskb))
  4079. goto err;
  4080. i = 0;
  4081. nfrags = skb_shinfo(list_skb)->nr_frags;
  4082. frag = skb_shinfo(list_skb)->frags;
  4083. frag_skb = list_skb;
  4084. pos += skb_headlen(list_skb);
  4085. while (pos < offset + len) {
  4086. BUG_ON(i >= nfrags);
  4087. size = skb_frag_size(frag);
  4088. if (pos + size > offset + len)
  4089. break;
  4090. i++;
  4091. pos += size;
  4092. frag++;
  4093. }
  4094. list_skb = list_skb->next;
  4095. if (unlikely(pskb_trim(nskb, len))) {
  4096. kfree_skb(nskb);
  4097. goto err;
  4098. }
  4099. hsize = skb_end_offset(nskb);
  4100. if (skb_cow_head(nskb, doffset + headroom)) {
  4101. kfree_skb(nskb);
  4102. goto err;
  4103. }
  4104. nskb->truesize += skb_end_offset(nskb) - hsize;
  4105. skb_release_head_state(nskb);
  4106. __skb_push(nskb, doffset);
  4107. } else {
  4108. if (hsize < 0)
  4109. hsize = 0;
  4110. if (hsize > len || !sg)
  4111. hsize = len;
  4112. nskb = __alloc_skb(hsize + doffset + headroom,
  4113. GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
  4114. NUMA_NO_NODE);
  4115. if (unlikely(!nskb))
  4116. goto err;
  4117. skb_reserve(nskb, headroom);
  4118. __skb_put(nskb, doffset);
  4119. }
  4120. if (segs)
  4121. tail->next = nskb;
  4122. else
  4123. segs = nskb;
  4124. tail = nskb;
  4125. __copy_skb_header(nskb, head_skb);
  4126. skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
  4127. skb_reset_mac_len(nskb);
  4128. skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
  4129. nskb->data - tnl_hlen,
  4130. doffset + tnl_hlen);
  4131. if (nskb->len == len + doffset)
  4132. goto perform_csum_check;
  4133. if (!sg) {
  4134. if (!csum) {
  4135. if (!nskb->remcsum_offload)
  4136. nskb->ip_summed = CHECKSUM_NONE;
  4137. SKB_GSO_CB(nskb)->csum =
  4138. skb_copy_and_csum_bits(head_skb, offset,
  4139. skb_put(nskb,
  4140. len),
  4141. len);
  4142. SKB_GSO_CB(nskb)->csum_start =
  4143. skb_headroom(nskb) + doffset;
  4144. } else {
  4145. if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
  4146. goto err;
  4147. }
  4148. continue;
  4149. }
  4150. nskb_frag = skb_shinfo(nskb)->frags;
  4151. skb_copy_from_linear_data_offset(head_skb, offset,
  4152. skb_put(nskb, hsize), hsize);
  4153. skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
  4154. SKBFL_SHARED_FRAG;
  4155. if (skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
  4156. goto err;
  4157. while (pos < offset + len) {
  4158. if (i >= nfrags) {
  4159. if (skb_orphan_frags(list_skb, GFP_ATOMIC) ||
  4160. skb_zerocopy_clone(nskb, list_skb,
  4161. GFP_ATOMIC))
  4162. goto err;
  4163. i = 0;
  4164. nfrags = skb_shinfo(list_skb)->nr_frags;
  4165. frag = skb_shinfo(list_skb)->frags;
  4166. frag_skb = list_skb;
  4167. if (!skb_headlen(list_skb)) {
  4168. BUG_ON(!nfrags);
  4169. } else {
  4170. BUG_ON(!list_skb->head_frag);
  4171. /* to make room for head_frag. */
  4172. i--;
  4173. frag--;
  4174. }
  4175. list_skb = list_skb->next;
  4176. }
  4177. if (unlikely(skb_shinfo(nskb)->nr_frags >=
  4178. MAX_SKB_FRAGS)) {
  4179. net_warn_ratelimited(
  4180. "skb_segment: too many frags: %u %u\n",
  4181. pos, mss);
  4182. err = -EINVAL;
  4183. goto err;
  4184. }
  4185. *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
  4186. __skb_frag_ref(nskb_frag);
  4187. size = skb_frag_size(nskb_frag);
  4188. if (pos < offset) {
  4189. skb_frag_off_add(nskb_frag, offset - pos);
  4190. skb_frag_size_sub(nskb_frag, offset - pos);
  4191. }
  4192. skb_shinfo(nskb)->nr_frags++;
  4193. if (pos + size <= offset + len) {
  4194. i++;
  4195. frag++;
  4196. pos += size;
  4197. } else {
  4198. skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
  4199. goto skip_fraglist;
  4200. }
  4201. nskb_frag++;
  4202. }
  4203. skip_fraglist:
  4204. nskb->data_len = len - hsize;
  4205. nskb->len += nskb->data_len;
  4206. nskb->truesize += nskb->data_len;
  4207. perform_csum_check:
  4208. if (!csum) {
  4209. if (skb_has_shared_frag(nskb) &&
  4210. __skb_linearize(nskb))
  4211. goto err;
  4212. if (!nskb->remcsum_offload)
  4213. nskb->ip_summed = CHECKSUM_NONE;
  4214. SKB_GSO_CB(nskb)->csum =
  4215. skb_checksum(nskb, doffset,
  4216. nskb->len - doffset, 0);
  4217. SKB_GSO_CB(nskb)->csum_start =
  4218. skb_headroom(nskb) + doffset;
  4219. }
  4220. } while ((offset += len) < head_skb->len);
  4221. /* Some callers want to get the end of the list.
  4222. * Put it in segs->prev to avoid walking the list.
  4223. * (see validate_xmit_skb_list() for example)
  4224. */
  4225. segs->prev = tail;
  4226. if (partial_segs) {
  4227. struct sk_buff *iter;
  4228. int type = skb_shinfo(head_skb)->gso_type;
  4229. unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
  4230. /* Update type to add partial and then remove dodgy if set */
  4231. type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
  4232. type &= ~SKB_GSO_DODGY;
  4233. /* Update GSO info and prepare to start updating headers on
  4234. * our way back down the stack of protocols.
  4235. */
  4236. for (iter = segs; iter; iter = iter->next) {
  4237. skb_shinfo(iter)->gso_size = gso_size;
  4238. skb_shinfo(iter)->gso_segs = partial_segs;
  4239. skb_shinfo(iter)->gso_type = type;
  4240. SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
  4241. }
  4242. if (tail->len - doffset <= gso_size)
  4243. skb_shinfo(tail)->gso_size = 0;
  4244. else if (tail != segs)
  4245. skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
  4246. }
  4247. /* Following permits correct backpressure, for protocols
  4248. * using skb_set_owner_w().
  4249. * Idea is to tranfert ownership from head_skb to last segment.
  4250. */
  4251. if (head_skb->destructor == sock_wfree) {
  4252. swap(tail->truesize, head_skb->truesize);
  4253. swap(tail->destructor, head_skb->destructor);
  4254. swap(tail->sk, head_skb->sk);
  4255. }
  4256. return segs;
  4257. err:
  4258. kfree_skb_list(segs);
  4259. return ERR_PTR(err);
  4260. }
  4261. EXPORT_SYMBOL_GPL(skb_segment);
  4262. #ifdef CONFIG_SKB_EXTENSIONS
  4263. #define SKB_EXT_ALIGN_VALUE 8
  4264. #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
  4265. static const u8 skb_ext_type_len[] = {
  4266. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  4267. [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
  4268. #endif
  4269. #ifdef CONFIG_XFRM
  4270. [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
  4271. #endif
  4272. #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
  4273. [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
  4274. #endif
  4275. #if IS_ENABLED(CONFIG_MPTCP)
  4276. [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
  4277. #endif
  4278. #if IS_ENABLED(CONFIG_MCTP_FLOWS)
  4279. [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
  4280. #endif
  4281. };
  4282. static __always_inline unsigned int skb_ext_total_length(void)
  4283. {
  4284. unsigned int l = SKB_EXT_CHUNKSIZEOF(struct skb_ext);
  4285. int i;
  4286. for (i = 0; i < ARRAY_SIZE(skb_ext_type_len); i++)
  4287. l += skb_ext_type_len[i];
  4288. return l;
  4289. }
  4290. static void skb_extensions_init(void)
  4291. {
  4292. BUILD_BUG_ON(SKB_EXT_NUM >= 8);
  4293. #if !IS_ENABLED(CONFIG_KCOV_INSTRUMENT_ALL)
  4294. BUILD_BUG_ON(skb_ext_total_length() > 255);
  4295. #endif
  4296. skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
  4297. SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
  4298. 0,
  4299. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  4300. NULL);
  4301. }
  4302. #else
  4303. static void skb_extensions_init(void) {}
  4304. #endif
  4305. /* The SKB kmem_cache slab is critical for network performance. Never
  4306. * merge/alias the slab with similar sized objects. This avoids fragmentation
  4307. * that hurts performance of kmem_cache_{alloc,free}_bulk APIs.
  4308. */
  4309. #ifndef CONFIG_SLUB_TINY
  4310. #define FLAG_SKB_NO_MERGE SLAB_NO_MERGE
  4311. #else /* CONFIG_SLUB_TINY - simple loop in kmem_cache_alloc_bulk */
  4312. #define FLAG_SKB_NO_MERGE 0
  4313. #endif
  4314. void __init skb_init(void)
  4315. {
  4316. net_hotdata.skbuff_cache = kmem_cache_create_usercopy("skbuff_head_cache",
  4317. sizeof(struct sk_buff),
  4318. 0,
  4319. SLAB_HWCACHE_ALIGN|SLAB_PANIC|
  4320. FLAG_SKB_NO_MERGE,
  4321. offsetof(struct sk_buff, cb),
  4322. sizeof_field(struct sk_buff, cb),
  4323. NULL);
  4324. net_hotdata.skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  4325. sizeof(struct sk_buff_fclones),
  4326. 0,
  4327. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  4328. NULL);
  4329. /* usercopy should only access first SKB_SMALL_HEAD_HEADROOM bytes.
  4330. * struct skb_shared_info is located at the end of skb->head,
  4331. * and should not be copied to/from user.
  4332. */
  4333. net_hotdata.skb_small_head_cache = kmem_cache_create_usercopy("skbuff_small_head",
  4334. SKB_SMALL_HEAD_CACHE_SIZE,
  4335. 0,
  4336. SLAB_HWCACHE_ALIGN | SLAB_PANIC,
  4337. 0,
  4338. SKB_SMALL_HEAD_HEADROOM,
  4339. NULL);
  4340. skb_extensions_init();
  4341. }
  4342. static int
  4343. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
  4344. unsigned int recursion_level)
  4345. {
  4346. int start = skb_headlen(skb);
  4347. int i, copy = start - offset;
  4348. struct sk_buff *frag_iter;
  4349. int elt = 0;
  4350. if (unlikely(recursion_level >= 24))
  4351. return -EMSGSIZE;
  4352. if (copy > 0) {
  4353. if (copy > len)
  4354. copy = len;
  4355. sg_set_buf(sg, skb->data + offset, copy);
  4356. elt++;
  4357. if ((len -= copy) == 0)
  4358. return elt;
  4359. offset += copy;
  4360. }
  4361. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  4362. int end;
  4363. WARN_ON(start > offset + len);
  4364. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  4365. if ((copy = end - offset) > 0) {
  4366. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4367. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  4368. return -EMSGSIZE;
  4369. if (copy > len)
  4370. copy = len;
  4371. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  4372. skb_frag_off(frag) + offset - start);
  4373. elt++;
  4374. if (!(len -= copy))
  4375. return elt;
  4376. offset += copy;
  4377. }
  4378. start = end;
  4379. }
  4380. skb_walk_frags(skb, frag_iter) {
  4381. int end, ret;
  4382. WARN_ON(start > offset + len);
  4383. end = start + frag_iter->len;
  4384. if ((copy = end - offset) > 0) {
  4385. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  4386. return -EMSGSIZE;
  4387. if (copy > len)
  4388. copy = len;
  4389. ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  4390. copy, recursion_level + 1);
  4391. if (unlikely(ret < 0))
  4392. return ret;
  4393. elt += ret;
  4394. if ((len -= copy) == 0)
  4395. return elt;
  4396. offset += copy;
  4397. }
  4398. start = end;
  4399. }
  4400. BUG_ON(len);
  4401. return elt;
  4402. }
  4403. /**
  4404. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  4405. * @skb: Socket buffer containing the buffers to be mapped
  4406. * @sg: The scatter-gather list to map into
  4407. * @offset: The offset into the buffer's contents to start mapping
  4408. * @len: Length of buffer space to be mapped
  4409. *
  4410. * Fill the specified scatter-gather list with mappings/pointers into a
  4411. * region of the buffer space attached to a socket buffer. Returns either
  4412. * the number of scatterlist items used, or -EMSGSIZE if the contents
  4413. * could not fit.
  4414. */
  4415. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  4416. {
  4417. int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
  4418. if (nsg <= 0)
  4419. return nsg;
  4420. sg_mark_end(&sg[nsg - 1]);
  4421. return nsg;
  4422. }
  4423. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  4424. /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
  4425. * sglist without mark the sg which contain last skb data as the end.
  4426. * So the caller can mannipulate sg list as will when padding new data after
  4427. * the first call without calling sg_unmark_end to expend sg list.
  4428. *
  4429. * Scenario to use skb_to_sgvec_nomark:
  4430. * 1. sg_init_table
  4431. * 2. skb_to_sgvec_nomark(payload1)
  4432. * 3. skb_to_sgvec_nomark(payload2)
  4433. *
  4434. * This is equivalent to:
  4435. * 1. sg_init_table
  4436. * 2. skb_to_sgvec(payload1)
  4437. * 3. sg_unmark_end
  4438. * 4. skb_to_sgvec(payload2)
  4439. *
  4440. * When mapping multiple payload conditionally, skb_to_sgvec_nomark
  4441. * is more preferable.
  4442. */
  4443. int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  4444. int offset, int len)
  4445. {
  4446. return __skb_to_sgvec(skb, sg, offset, len, 0);
  4447. }
  4448. EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
  4449. /**
  4450. * skb_cow_data - Check that a socket buffer's data buffers are writable
  4451. * @skb: The socket buffer to check.
  4452. * @tailbits: Amount of trailing space to be added
  4453. * @trailer: Returned pointer to the skb where the @tailbits space begins
  4454. *
  4455. * Make sure that the data buffers attached to a socket buffer are
  4456. * writable. If they are not, private copies are made of the data buffers
  4457. * and the socket buffer is set to use these instead.
  4458. *
  4459. * If @tailbits is given, make sure that there is space to write @tailbits
  4460. * bytes of data beyond current end of socket buffer. @trailer will be
  4461. * set to point to the skb in which this space begins.
  4462. *
  4463. * The number of scatterlist elements required to completely map the
  4464. * COW'd and extended socket buffer will be returned.
  4465. */
  4466. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  4467. {
  4468. int copyflag;
  4469. int elt;
  4470. struct sk_buff *skb1, **skb_p;
  4471. /* If skb is cloned or its head is paged, reallocate
  4472. * head pulling out all the pages (pages are considered not writable
  4473. * at the moment even if they are anonymous).
  4474. */
  4475. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  4476. !__pskb_pull_tail(skb, __skb_pagelen(skb)))
  4477. return -ENOMEM;
  4478. /* Easy case. Most of packets will go this way. */
  4479. if (!skb_has_frag_list(skb)) {
  4480. /* A little of trouble, not enough of space for trailer.
  4481. * This should not happen, when stack is tuned to generate
  4482. * good frames. OK, on miss we reallocate and reserve even more
  4483. * space, 128 bytes is fair. */
  4484. if (skb_tailroom(skb) < tailbits &&
  4485. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  4486. return -ENOMEM;
  4487. /* Voila! */
  4488. *trailer = skb;
  4489. return 1;
  4490. }
  4491. /* Misery. We are in troubles, going to mincer fragments... */
  4492. elt = 1;
  4493. skb_p = &skb_shinfo(skb)->frag_list;
  4494. copyflag = 0;
  4495. while ((skb1 = *skb_p) != NULL) {
  4496. int ntail = 0;
  4497. /* The fragment is partially pulled by someone,
  4498. * this can happen on input. Copy it and everything
  4499. * after it. */
  4500. if (skb_shared(skb1))
  4501. copyflag = 1;
  4502. /* If the skb is the last, worry about trailer. */
  4503. if (skb1->next == NULL && tailbits) {
  4504. if (skb_shinfo(skb1)->nr_frags ||
  4505. skb_has_frag_list(skb1) ||
  4506. skb_tailroom(skb1) < tailbits)
  4507. ntail = tailbits + 128;
  4508. }
  4509. if (copyflag ||
  4510. skb_cloned(skb1) ||
  4511. ntail ||
  4512. skb_shinfo(skb1)->nr_frags ||
  4513. skb_has_frag_list(skb1)) {
  4514. struct sk_buff *skb2;
  4515. /* Fuck, we are miserable poor guys... */
  4516. if (ntail == 0)
  4517. skb2 = skb_copy(skb1, GFP_ATOMIC);
  4518. else
  4519. skb2 = skb_copy_expand(skb1,
  4520. skb_headroom(skb1),
  4521. ntail,
  4522. GFP_ATOMIC);
  4523. if (unlikely(skb2 == NULL))
  4524. return -ENOMEM;
  4525. if (skb1->sk)
  4526. skb_set_owner_w(skb2, skb1->sk);
  4527. /* Looking around. Are we still alive?
  4528. * OK, link new skb, drop old one */
  4529. skb2->next = skb1->next;
  4530. *skb_p = skb2;
  4531. kfree_skb(skb1);
  4532. skb1 = skb2;
  4533. }
  4534. elt++;
  4535. *trailer = skb1;
  4536. skb_p = &skb1->next;
  4537. }
  4538. return elt;
  4539. }
  4540. EXPORT_SYMBOL_GPL(skb_cow_data);
  4541. static void sock_rmem_free(struct sk_buff *skb)
  4542. {
  4543. struct sock *sk = skb->sk;
  4544. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  4545. }
  4546. static void skb_set_err_queue(struct sk_buff *skb)
  4547. {
  4548. /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
  4549. * So, it is safe to (mis)use it to mark skbs on the error queue.
  4550. */
  4551. skb->pkt_type = PACKET_OUTGOING;
  4552. BUILD_BUG_ON(PACKET_OUTGOING == 0);
  4553. }
  4554. /*
  4555. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  4556. */
  4557. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  4558. {
  4559. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  4560. (unsigned int)READ_ONCE(sk->sk_rcvbuf))
  4561. return -ENOMEM;
  4562. skb_orphan(skb);
  4563. skb->sk = sk;
  4564. skb->destructor = sock_rmem_free;
  4565. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  4566. skb_set_err_queue(skb);
  4567. /* before exiting rcu section, make sure dst is refcounted */
  4568. skb_dst_force(skb);
  4569. skb_queue_tail(&sk->sk_error_queue, skb);
  4570. if (!sock_flag(sk, SOCK_DEAD))
  4571. sk_error_report(sk);
  4572. return 0;
  4573. }
  4574. EXPORT_SYMBOL(sock_queue_err_skb);
  4575. static bool is_icmp_err_skb(const struct sk_buff *skb)
  4576. {
  4577. return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  4578. SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
  4579. }
  4580. struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
  4581. {
  4582. struct sk_buff_head *q = &sk->sk_error_queue;
  4583. struct sk_buff *skb, *skb_next = NULL;
  4584. bool icmp_next = false;
  4585. unsigned long flags;
  4586. if (skb_queue_empty_lockless(q))
  4587. return NULL;
  4588. spin_lock_irqsave(&q->lock, flags);
  4589. skb = __skb_dequeue(q);
  4590. if (skb && (skb_next = skb_peek(q))) {
  4591. icmp_next = is_icmp_err_skb(skb_next);
  4592. if (icmp_next)
  4593. sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
  4594. }
  4595. spin_unlock_irqrestore(&q->lock, flags);
  4596. if (is_icmp_err_skb(skb) && !icmp_next)
  4597. sk->sk_err = 0;
  4598. if (skb_next)
  4599. sk_error_report(sk);
  4600. return skb;
  4601. }
  4602. EXPORT_SYMBOL(sock_dequeue_err_skb);
  4603. /**
  4604. * skb_clone_sk - create clone of skb, and take reference to socket
  4605. * @skb: the skb to clone
  4606. *
  4607. * This function creates a clone of a buffer that holds a reference on
  4608. * sk_refcnt. Buffers created via this function are meant to be
  4609. * returned using sock_queue_err_skb, or free via kfree_skb.
  4610. *
  4611. * When passing buffers allocated with this function to sock_queue_err_skb
  4612. * it is necessary to wrap the call with sock_hold/sock_put in order to
  4613. * prevent the socket from being released prior to being enqueued on
  4614. * the sk_error_queue.
  4615. */
  4616. struct sk_buff *skb_clone_sk(struct sk_buff *skb)
  4617. {
  4618. struct sock *sk = skb->sk;
  4619. struct sk_buff *clone;
  4620. if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
  4621. return NULL;
  4622. clone = skb_clone(skb, GFP_ATOMIC);
  4623. if (!clone) {
  4624. sock_put(sk);
  4625. return NULL;
  4626. }
  4627. clone->sk = sk;
  4628. clone->destructor = sock_efree;
  4629. return clone;
  4630. }
  4631. EXPORT_SYMBOL(skb_clone_sk);
  4632. static void __skb_complete_tx_timestamp(struct sk_buff *skb,
  4633. struct sock *sk,
  4634. int tstype,
  4635. bool opt_stats)
  4636. {
  4637. struct sock_exterr_skb *serr;
  4638. int err;
  4639. BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
  4640. serr = SKB_EXT_ERR(skb);
  4641. memset(serr, 0, sizeof(*serr));
  4642. serr->ee.ee_errno = ENOMSG;
  4643. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  4644. serr->ee.ee_info = tstype;
  4645. serr->opt_stats = opt_stats;
  4646. serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
  4647. if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_ID) {
  4648. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  4649. if (sk_is_tcp(sk))
  4650. serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
  4651. }
  4652. err = sock_queue_err_skb(sk, skb);
  4653. if (err)
  4654. kfree_skb(skb);
  4655. }
  4656. static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
  4657. {
  4658. bool ret;
  4659. if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
  4660. return true;
  4661. read_lock_bh(&sk->sk_callback_lock);
  4662. ret = sk->sk_socket && sk->sk_socket->file &&
  4663. file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
  4664. read_unlock_bh(&sk->sk_callback_lock);
  4665. return ret;
  4666. }
  4667. void skb_complete_tx_timestamp(struct sk_buff *skb,
  4668. struct skb_shared_hwtstamps *hwtstamps)
  4669. {
  4670. struct sock *sk = skb->sk;
  4671. if (!skb_may_tx_timestamp(sk, false))
  4672. goto err;
  4673. /* Take a reference to prevent skb_orphan() from freeing the socket,
  4674. * but only if the socket refcount is not zero.
  4675. */
  4676. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  4677. *skb_hwtstamps(skb) = *hwtstamps;
  4678. __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
  4679. sock_put(sk);
  4680. return;
  4681. }
  4682. err:
  4683. kfree_skb(skb);
  4684. }
  4685. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  4686. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  4687. const struct sk_buff *ack_skb,
  4688. struct skb_shared_hwtstamps *hwtstamps,
  4689. struct sock *sk, int tstype)
  4690. {
  4691. struct sk_buff *skb;
  4692. bool tsonly, opt_stats = false;
  4693. u32 tsflags;
  4694. if (!sk)
  4695. return;
  4696. tsflags = READ_ONCE(sk->sk_tsflags);
  4697. if (!hwtstamps && !(tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
  4698. skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
  4699. return;
  4700. tsonly = tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
  4701. if (!skb_may_tx_timestamp(sk, tsonly))
  4702. return;
  4703. if (tsonly) {
  4704. #ifdef CONFIG_INET
  4705. if ((tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
  4706. sk_is_tcp(sk)) {
  4707. skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
  4708. ack_skb);
  4709. opt_stats = true;
  4710. } else
  4711. #endif
  4712. skb = alloc_skb(0, GFP_ATOMIC);
  4713. } else {
  4714. skb = skb_clone(orig_skb, GFP_ATOMIC);
  4715. if (skb_orphan_frags_rx(skb, GFP_ATOMIC)) {
  4716. kfree_skb(skb);
  4717. return;
  4718. }
  4719. }
  4720. if (!skb)
  4721. return;
  4722. if (tsonly) {
  4723. skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
  4724. SKBTX_ANY_TSTAMP;
  4725. skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
  4726. }
  4727. if (hwtstamps)
  4728. *skb_hwtstamps(skb) = *hwtstamps;
  4729. else
  4730. __net_timestamp(skb);
  4731. __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
  4732. }
  4733. EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
  4734. void skb_tstamp_tx(struct sk_buff *orig_skb,
  4735. struct skb_shared_hwtstamps *hwtstamps)
  4736. {
  4737. return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
  4738. SCM_TSTAMP_SND);
  4739. }
  4740. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  4741. #ifdef CONFIG_WIRELESS
  4742. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  4743. {
  4744. struct sock *sk = skb->sk;
  4745. struct sock_exterr_skb *serr;
  4746. int err = 1;
  4747. skb->wifi_acked_valid = 1;
  4748. skb->wifi_acked = acked;
  4749. serr = SKB_EXT_ERR(skb);
  4750. memset(serr, 0, sizeof(*serr));
  4751. serr->ee.ee_errno = ENOMSG;
  4752. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  4753. /* Take a reference to prevent skb_orphan() from freeing the socket,
  4754. * but only if the socket refcount is not zero.
  4755. */
  4756. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  4757. err = sock_queue_err_skb(sk, skb);
  4758. sock_put(sk);
  4759. }
  4760. if (err)
  4761. kfree_skb(skb);
  4762. }
  4763. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  4764. #endif /* CONFIG_WIRELESS */
  4765. /**
  4766. * skb_partial_csum_set - set up and verify partial csum values for packet
  4767. * @skb: the skb to set
  4768. * @start: the number of bytes after skb->data to start checksumming.
  4769. * @off: the offset from start to place the checksum.
  4770. *
  4771. * For untrusted partially-checksummed packets, we need to make sure the values
  4772. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  4773. *
  4774. * This function checks and sets those values and skb->ip_summed: if this
  4775. * returns false you should drop the packet.
  4776. */
  4777. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  4778. {
  4779. u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
  4780. u32 csum_start = skb_headroom(skb) + (u32)start;
  4781. if (unlikely(csum_start >= U16_MAX || csum_end > skb_headlen(skb))) {
  4782. net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
  4783. start, off, skb_headroom(skb), skb_headlen(skb));
  4784. return false;
  4785. }
  4786. skb->ip_summed = CHECKSUM_PARTIAL;
  4787. skb->csum_start = csum_start;
  4788. skb->csum_offset = off;
  4789. skb->transport_header = csum_start;
  4790. return true;
  4791. }
  4792. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  4793. static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  4794. unsigned int max)
  4795. {
  4796. if (skb_headlen(skb) >= len)
  4797. return 0;
  4798. /* If we need to pullup then pullup to the max, so we
  4799. * won't need to do it again.
  4800. */
  4801. if (max > skb->len)
  4802. max = skb->len;
  4803. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  4804. return -ENOMEM;
  4805. if (skb_headlen(skb) < len)
  4806. return -EPROTO;
  4807. return 0;
  4808. }
  4809. #define MAX_TCP_HDR_LEN (15 * 4)
  4810. static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
  4811. typeof(IPPROTO_IP) proto,
  4812. unsigned int off)
  4813. {
  4814. int err;
  4815. switch (proto) {
  4816. case IPPROTO_TCP:
  4817. err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
  4818. off + MAX_TCP_HDR_LEN);
  4819. if (!err && !skb_partial_csum_set(skb, off,
  4820. offsetof(struct tcphdr,
  4821. check)))
  4822. err = -EPROTO;
  4823. return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
  4824. case IPPROTO_UDP:
  4825. err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
  4826. off + sizeof(struct udphdr));
  4827. if (!err && !skb_partial_csum_set(skb, off,
  4828. offsetof(struct udphdr,
  4829. check)))
  4830. err = -EPROTO;
  4831. return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
  4832. }
  4833. return ERR_PTR(-EPROTO);
  4834. }
  4835. /* This value should be large enough to cover a tagged ethernet header plus
  4836. * maximally sized IP and TCP or UDP headers.
  4837. */
  4838. #define MAX_IP_HDR_LEN 128
  4839. static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
  4840. {
  4841. unsigned int off;
  4842. bool fragment;
  4843. __sum16 *csum;
  4844. int err;
  4845. fragment = false;
  4846. err = skb_maybe_pull_tail(skb,
  4847. sizeof(struct iphdr),
  4848. MAX_IP_HDR_LEN);
  4849. if (err < 0)
  4850. goto out;
  4851. if (ip_is_fragment(ip_hdr(skb)))
  4852. fragment = true;
  4853. off = ip_hdrlen(skb);
  4854. err = -EPROTO;
  4855. if (fragment)
  4856. goto out;
  4857. csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
  4858. if (IS_ERR(csum))
  4859. return PTR_ERR(csum);
  4860. if (recalculate)
  4861. *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  4862. ip_hdr(skb)->daddr,
  4863. skb->len - off,
  4864. ip_hdr(skb)->protocol, 0);
  4865. err = 0;
  4866. out:
  4867. return err;
  4868. }
  4869. /* This value should be large enough to cover a tagged ethernet header plus
  4870. * an IPv6 header, all options, and a maximal TCP or UDP header.
  4871. */
  4872. #define MAX_IPV6_HDR_LEN 256
  4873. #define OPT_HDR(type, skb, off) \
  4874. (type *)(skb_network_header(skb) + (off))
  4875. static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
  4876. {
  4877. int err;
  4878. u8 nexthdr;
  4879. unsigned int off;
  4880. unsigned int len;
  4881. bool fragment;
  4882. bool done;
  4883. __sum16 *csum;
  4884. fragment = false;
  4885. done = false;
  4886. off = sizeof(struct ipv6hdr);
  4887. err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  4888. if (err < 0)
  4889. goto out;
  4890. nexthdr = ipv6_hdr(skb)->nexthdr;
  4891. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  4892. while (off <= len && !done) {
  4893. switch (nexthdr) {
  4894. case IPPROTO_DSTOPTS:
  4895. case IPPROTO_HOPOPTS:
  4896. case IPPROTO_ROUTING: {
  4897. struct ipv6_opt_hdr *hp;
  4898. err = skb_maybe_pull_tail(skb,
  4899. off +
  4900. sizeof(struct ipv6_opt_hdr),
  4901. MAX_IPV6_HDR_LEN);
  4902. if (err < 0)
  4903. goto out;
  4904. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  4905. nexthdr = hp->nexthdr;
  4906. off += ipv6_optlen(hp);
  4907. break;
  4908. }
  4909. case IPPROTO_AH: {
  4910. struct ip_auth_hdr *hp;
  4911. err = skb_maybe_pull_tail(skb,
  4912. off +
  4913. sizeof(struct ip_auth_hdr),
  4914. MAX_IPV6_HDR_LEN);
  4915. if (err < 0)
  4916. goto out;
  4917. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  4918. nexthdr = hp->nexthdr;
  4919. off += ipv6_authlen(hp);
  4920. break;
  4921. }
  4922. case IPPROTO_FRAGMENT: {
  4923. struct frag_hdr *hp;
  4924. err = skb_maybe_pull_tail(skb,
  4925. off +
  4926. sizeof(struct frag_hdr),
  4927. MAX_IPV6_HDR_LEN);
  4928. if (err < 0)
  4929. goto out;
  4930. hp = OPT_HDR(struct frag_hdr, skb, off);
  4931. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  4932. fragment = true;
  4933. nexthdr = hp->nexthdr;
  4934. off += sizeof(struct frag_hdr);
  4935. break;
  4936. }
  4937. default:
  4938. done = true;
  4939. break;
  4940. }
  4941. }
  4942. err = -EPROTO;
  4943. if (!done || fragment)
  4944. goto out;
  4945. csum = skb_checksum_setup_ip(skb, nexthdr, off);
  4946. if (IS_ERR(csum))
  4947. return PTR_ERR(csum);
  4948. if (recalculate)
  4949. *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  4950. &ipv6_hdr(skb)->daddr,
  4951. skb->len - off, nexthdr, 0);
  4952. err = 0;
  4953. out:
  4954. return err;
  4955. }
  4956. /**
  4957. * skb_checksum_setup - set up partial checksum offset
  4958. * @skb: the skb to set up
  4959. * @recalculate: if true the pseudo-header checksum will be recalculated
  4960. */
  4961. int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
  4962. {
  4963. int err;
  4964. switch (skb->protocol) {
  4965. case htons(ETH_P_IP):
  4966. err = skb_checksum_setup_ipv4(skb, recalculate);
  4967. break;
  4968. case htons(ETH_P_IPV6):
  4969. err = skb_checksum_setup_ipv6(skb, recalculate);
  4970. break;
  4971. default:
  4972. err = -EPROTO;
  4973. break;
  4974. }
  4975. return err;
  4976. }
  4977. EXPORT_SYMBOL(skb_checksum_setup);
  4978. /**
  4979. * skb_checksum_maybe_trim - maybe trims the given skb
  4980. * @skb: the skb to check
  4981. * @transport_len: the data length beyond the network header
  4982. *
  4983. * Checks whether the given skb has data beyond the given transport length.
  4984. * If so, returns a cloned skb trimmed to this transport length.
  4985. * Otherwise returns the provided skb. Returns NULL in error cases
  4986. * (e.g. transport_len exceeds skb length or out-of-memory).
  4987. *
  4988. * Caller needs to set the skb transport header and free any returned skb if it
  4989. * differs from the provided skb.
  4990. */
  4991. static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
  4992. unsigned int transport_len)
  4993. {
  4994. struct sk_buff *skb_chk;
  4995. unsigned int len = skb_transport_offset(skb) + transport_len;
  4996. int ret;
  4997. if (skb->len < len)
  4998. return NULL;
  4999. else if (skb->len == len)
  5000. return skb;
  5001. skb_chk = skb_clone(skb, GFP_ATOMIC);
  5002. if (!skb_chk)
  5003. return NULL;
  5004. ret = pskb_trim_rcsum(skb_chk, len);
  5005. if (ret) {
  5006. kfree_skb(skb_chk);
  5007. return NULL;
  5008. }
  5009. return skb_chk;
  5010. }
  5011. /**
  5012. * skb_checksum_trimmed - validate checksum of an skb
  5013. * @skb: the skb to check
  5014. * @transport_len: the data length beyond the network header
  5015. * @skb_chkf: checksum function to use
  5016. *
  5017. * Applies the given checksum function skb_chkf to the provided skb.
  5018. * Returns a checked and maybe trimmed skb. Returns NULL on error.
  5019. *
  5020. * If the skb has data beyond the given transport length, then a
  5021. * trimmed & cloned skb is checked and returned.
  5022. *
  5023. * Caller needs to set the skb transport header and free any returned skb if it
  5024. * differs from the provided skb.
  5025. */
  5026. struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
  5027. unsigned int transport_len,
  5028. __sum16(*skb_chkf)(struct sk_buff *skb))
  5029. {
  5030. struct sk_buff *skb_chk;
  5031. unsigned int offset = skb_transport_offset(skb);
  5032. __sum16 ret;
  5033. skb_chk = skb_checksum_maybe_trim(skb, transport_len);
  5034. if (!skb_chk)
  5035. goto err;
  5036. if (!pskb_may_pull(skb_chk, offset))
  5037. goto err;
  5038. skb_pull_rcsum(skb_chk, offset);
  5039. ret = skb_chkf(skb_chk);
  5040. skb_push_rcsum(skb_chk, offset);
  5041. if (ret)
  5042. goto err;
  5043. return skb_chk;
  5044. err:
  5045. if (skb_chk && skb_chk != skb)
  5046. kfree_skb(skb_chk);
  5047. return NULL;
  5048. }
  5049. EXPORT_SYMBOL(skb_checksum_trimmed);
  5050. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  5051. {
  5052. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  5053. skb->dev->name);
  5054. }
  5055. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  5056. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  5057. {
  5058. if (head_stolen) {
  5059. skb_release_head_state(skb);
  5060. kmem_cache_free(net_hotdata.skbuff_cache, skb);
  5061. } else {
  5062. __kfree_skb(skb);
  5063. }
  5064. }
  5065. EXPORT_SYMBOL(kfree_skb_partial);
  5066. /**
  5067. * skb_try_coalesce - try to merge skb to prior one
  5068. * @to: prior buffer
  5069. * @from: buffer to add
  5070. * @fragstolen: pointer to boolean
  5071. * @delta_truesize: how much more was allocated than was requested
  5072. */
  5073. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  5074. bool *fragstolen, int *delta_truesize)
  5075. {
  5076. struct skb_shared_info *to_shinfo, *from_shinfo;
  5077. int i, delta, len = from->len;
  5078. *fragstolen = false;
  5079. if (skb_cloned(to))
  5080. return false;
  5081. /* In general, avoid mixing page_pool and non-page_pool allocated
  5082. * pages within the same SKB. In theory we could take full
  5083. * references if @from is cloned and !@to->pp_recycle but its
  5084. * tricky (due to potential race with the clone disappearing) and
  5085. * rare, so not worth dealing with.
  5086. */
  5087. if (to->pp_recycle != from->pp_recycle)
  5088. return false;
  5089. if (skb_frags_readable(from) != skb_frags_readable(to))
  5090. return false;
  5091. if (len <= skb_tailroom(to) && skb_frags_readable(from)) {
  5092. if (len)
  5093. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  5094. *delta_truesize = 0;
  5095. return true;
  5096. }
  5097. to_shinfo = skb_shinfo(to);
  5098. from_shinfo = skb_shinfo(from);
  5099. if (to_shinfo->frag_list || from_shinfo->frag_list)
  5100. return false;
  5101. if (skb_zcopy(to) || skb_zcopy(from))
  5102. return false;
  5103. if (skb_headlen(from) != 0) {
  5104. struct page *page;
  5105. unsigned int offset;
  5106. if (to_shinfo->nr_frags +
  5107. from_shinfo->nr_frags >= MAX_SKB_FRAGS)
  5108. return false;
  5109. if (skb_head_is_locked(from))
  5110. return false;
  5111. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  5112. page = virt_to_head_page(from->head);
  5113. offset = from->data - (unsigned char *)page_address(page);
  5114. skb_fill_page_desc(to, to_shinfo->nr_frags,
  5115. page, offset, skb_headlen(from));
  5116. *fragstolen = true;
  5117. } else {
  5118. if (to_shinfo->nr_frags +
  5119. from_shinfo->nr_frags > MAX_SKB_FRAGS)
  5120. return false;
  5121. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  5122. }
  5123. WARN_ON_ONCE(delta < len);
  5124. memcpy(to_shinfo->frags + to_shinfo->nr_frags,
  5125. from_shinfo->frags,
  5126. from_shinfo->nr_frags * sizeof(skb_frag_t));
  5127. to_shinfo->nr_frags += from_shinfo->nr_frags;
  5128. if (!skb_cloned(from))
  5129. from_shinfo->nr_frags = 0;
  5130. /* if the skb is not cloned this does nothing
  5131. * since we set nr_frags to 0.
  5132. */
  5133. if (skb_pp_frag_ref(from)) {
  5134. for (i = 0; i < from_shinfo->nr_frags; i++)
  5135. __skb_frag_ref(&from_shinfo->frags[i]);
  5136. }
  5137. to->truesize += delta;
  5138. to->len += len;
  5139. to->data_len += len;
  5140. *delta_truesize = delta;
  5141. return true;
  5142. }
  5143. EXPORT_SYMBOL(skb_try_coalesce);
  5144. /**
  5145. * skb_scrub_packet - scrub an skb
  5146. *
  5147. * @skb: buffer to clean
  5148. * @xnet: packet is crossing netns
  5149. *
  5150. * skb_scrub_packet can be used after encapsulating or decapsulating a packet
  5151. * into/from a tunnel. Some information have to be cleared during these
  5152. * operations.
  5153. * skb_scrub_packet can also be used to clean a skb before injecting it in
  5154. * another namespace (@xnet == true). We have to clear all information in the
  5155. * skb that could impact namespace isolation.
  5156. */
  5157. void skb_scrub_packet(struct sk_buff *skb, bool xnet)
  5158. {
  5159. skb->pkt_type = PACKET_HOST;
  5160. skb->skb_iif = 0;
  5161. skb->ignore_df = 0;
  5162. skb_dst_drop(skb);
  5163. skb_ext_reset(skb);
  5164. nf_reset_ct(skb);
  5165. nf_reset_trace(skb);
  5166. #ifdef CONFIG_NET_SWITCHDEV
  5167. skb->offload_fwd_mark = 0;
  5168. skb->offload_l3_fwd_mark = 0;
  5169. #endif
  5170. ipvs_reset(skb);
  5171. if (!xnet)
  5172. return;
  5173. skb->mark = 0;
  5174. skb_clear_tstamp(skb);
  5175. }
  5176. EXPORT_SYMBOL_GPL(skb_scrub_packet);
  5177. static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
  5178. {
  5179. int mac_len, meta_len;
  5180. void *meta;
  5181. if (skb_cow(skb, skb_headroom(skb)) < 0) {
  5182. kfree_skb(skb);
  5183. return NULL;
  5184. }
  5185. mac_len = skb->data - skb_mac_header(skb);
  5186. if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
  5187. memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
  5188. mac_len - VLAN_HLEN - ETH_TLEN);
  5189. }
  5190. meta_len = skb_metadata_len(skb);
  5191. if (meta_len) {
  5192. meta = skb_metadata_end(skb) - meta_len;
  5193. memmove(meta + VLAN_HLEN, meta, meta_len);
  5194. }
  5195. skb->mac_header += VLAN_HLEN;
  5196. return skb;
  5197. }
  5198. struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
  5199. {
  5200. struct vlan_hdr *vhdr;
  5201. u16 vlan_tci;
  5202. if (unlikely(skb_vlan_tag_present(skb))) {
  5203. /* vlan_tci is already set-up so leave this for another time */
  5204. return skb;
  5205. }
  5206. skb = skb_share_check(skb, GFP_ATOMIC);
  5207. if (unlikely(!skb))
  5208. goto err_free;
  5209. /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
  5210. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
  5211. goto err_free;
  5212. vhdr = (struct vlan_hdr *)skb->data;
  5213. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  5214. __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
  5215. skb_pull_rcsum(skb, VLAN_HLEN);
  5216. vlan_set_encap_proto(skb, vhdr);
  5217. skb = skb_reorder_vlan_header(skb);
  5218. if (unlikely(!skb))
  5219. goto err_free;
  5220. skb_reset_network_header(skb);
  5221. if (!skb_transport_header_was_set(skb))
  5222. skb_reset_transport_header(skb);
  5223. skb_reset_mac_len(skb);
  5224. return skb;
  5225. err_free:
  5226. kfree_skb(skb);
  5227. return NULL;
  5228. }
  5229. EXPORT_SYMBOL(skb_vlan_untag);
  5230. int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
  5231. {
  5232. if (!pskb_may_pull(skb, write_len))
  5233. return -ENOMEM;
  5234. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  5235. return 0;
  5236. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  5237. }
  5238. EXPORT_SYMBOL(skb_ensure_writable);
  5239. int skb_ensure_writable_head_tail(struct sk_buff *skb, struct net_device *dev)
  5240. {
  5241. int needed_headroom = dev->needed_headroom;
  5242. int needed_tailroom = dev->needed_tailroom;
  5243. /* For tail taggers, we need to pad short frames ourselves, to ensure
  5244. * that the tail tag does not fail at its role of being at the end of
  5245. * the packet, once the conduit interface pads the frame. Account for
  5246. * that pad length here, and pad later.
  5247. */
  5248. if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
  5249. needed_tailroom += ETH_ZLEN - skb->len;
  5250. /* skb_headroom() returns unsigned int... */
  5251. needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
  5252. needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
  5253. if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
  5254. /* No reallocation needed, yay! */
  5255. return 0;
  5256. return pskb_expand_head(skb, needed_headroom, needed_tailroom,
  5257. GFP_ATOMIC);
  5258. }
  5259. EXPORT_SYMBOL(skb_ensure_writable_head_tail);
  5260. /* remove VLAN header from packet and update csum accordingly.
  5261. * expects a non skb_vlan_tag_present skb with a vlan tag payload
  5262. */
  5263. int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
  5264. {
  5265. int offset = skb->data - skb_mac_header(skb);
  5266. int err;
  5267. if (WARN_ONCE(offset,
  5268. "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
  5269. offset)) {
  5270. return -EINVAL;
  5271. }
  5272. err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
  5273. if (unlikely(err))
  5274. return err;
  5275. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  5276. vlan_remove_tag(skb, vlan_tci);
  5277. skb->mac_header += VLAN_HLEN;
  5278. if (skb_network_offset(skb) < ETH_HLEN)
  5279. skb_set_network_header(skb, ETH_HLEN);
  5280. skb_reset_mac_len(skb);
  5281. return err;
  5282. }
  5283. EXPORT_SYMBOL(__skb_vlan_pop);
  5284. /* Pop a vlan tag either from hwaccel or from payload.
  5285. * Expects skb->data at mac header.
  5286. */
  5287. int skb_vlan_pop(struct sk_buff *skb)
  5288. {
  5289. u16 vlan_tci;
  5290. __be16 vlan_proto;
  5291. int err;
  5292. if (likely(skb_vlan_tag_present(skb))) {
  5293. __vlan_hwaccel_clear_tag(skb);
  5294. } else {
  5295. if (unlikely(!eth_type_vlan(skb->protocol)))
  5296. return 0;
  5297. err = __skb_vlan_pop(skb, &vlan_tci);
  5298. if (err)
  5299. return err;
  5300. }
  5301. /* move next vlan tag to hw accel tag */
  5302. if (likely(!eth_type_vlan(skb->protocol)))
  5303. return 0;
  5304. vlan_proto = skb->protocol;
  5305. err = __skb_vlan_pop(skb, &vlan_tci);
  5306. if (unlikely(err))
  5307. return err;
  5308. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  5309. return 0;
  5310. }
  5311. EXPORT_SYMBOL(skb_vlan_pop);
  5312. /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
  5313. * Expects skb->data at mac header.
  5314. */
  5315. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  5316. {
  5317. if (skb_vlan_tag_present(skb)) {
  5318. int offset = skb->data - skb_mac_header(skb);
  5319. int err;
  5320. if (WARN_ONCE(offset,
  5321. "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
  5322. offset)) {
  5323. return -EINVAL;
  5324. }
  5325. err = __vlan_insert_tag(skb, skb->vlan_proto,
  5326. skb_vlan_tag_get(skb));
  5327. if (err)
  5328. return err;
  5329. skb->protocol = skb->vlan_proto;
  5330. skb->network_header -= VLAN_HLEN;
  5331. skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  5332. }
  5333. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  5334. return 0;
  5335. }
  5336. EXPORT_SYMBOL(skb_vlan_push);
  5337. /**
  5338. * skb_eth_pop() - Drop the Ethernet header at the head of a packet
  5339. *
  5340. * @skb: Socket buffer to modify
  5341. *
  5342. * Drop the Ethernet header of @skb.
  5343. *
  5344. * Expects that skb->data points to the mac header and that no VLAN tags are
  5345. * present.
  5346. *
  5347. * Returns 0 on success, -errno otherwise.
  5348. */
  5349. int skb_eth_pop(struct sk_buff *skb)
  5350. {
  5351. if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
  5352. skb_network_offset(skb) < ETH_HLEN)
  5353. return -EPROTO;
  5354. skb_pull_rcsum(skb, ETH_HLEN);
  5355. skb_reset_mac_header(skb);
  5356. skb_reset_mac_len(skb);
  5357. return 0;
  5358. }
  5359. EXPORT_SYMBOL(skb_eth_pop);
  5360. /**
  5361. * skb_eth_push() - Add a new Ethernet header at the head of a packet
  5362. *
  5363. * @skb: Socket buffer to modify
  5364. * @dst: Destination MAC address of the new header
  5365. * @src: Source MAC address of the new header
  5366. *
  5367. * Prepend @skb with a new Ethernet header.
  5368. *
  5369. * Expects that skb->data points to the mac header, which must be empty.
  5370. *
  5371. * Returns 0 on success, -errno otherwise.
  5372. */
  5373. int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
  5374. const unsigned char *src)
  5375. {
  5376. struct ethhdr *eth;
  5377. int err;
  5378. if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
  5379. return -EPROTO;
  5380. err = skb_cow_head(skb, sizeof(*eth));
  5381. if (err < 0)
  5382. return err;
  5383. skb_push(skb, sizeof(*eth));
  5384. skb_reset_mac_header(skb);
  5385. skb_reset_mac_len(skb);
  5386. eth = eth_hdr(skb);
  5387. ether_addr_copy(eth->h_dest, dst);
  5388. ether_addr_copy(eth->h_source, src);
  5389. eth->h_proto = skb->protocol;
  5390. skb_postpush_rcsum(skb, eth, sizeof(*eth));
  5391. return 0;
  5392. }
  5393. EXPORT_SYMBOL(skb_eth_push);
  5394. /* Update the ethertype of hdr and the skb csum value if required. */
  5395. static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
  5396. __be16 ethertype)
  5397. {
  5398. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  5399. __be16 diff[] = { ~hdr->h_proto, ethertype };
  5400. skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
  5401. }
  5402. hdr->h_proto = ethertype;
  5403. }
  5404. /**
  5405. * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
  5406. * the packet
  5407. *
  5408. * @skb: buffer
  5409. * @mpls_lse: MPLS label stack entry to push
  5410. * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
  5411. * @mac_len: length of the MAC header
  5412. * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
  5413. * ethernet
  5414. *
  5415. * Expects skb->data at mac header.
  5416. *
  5417. * Returns 0 on success, -errno otherwise.
  5418. */
  5419. int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
  5420. int mac_len, bool ethernet)
  5421. {
  5422. struct mpls_shim_hdr *lse;
  5423. int err;
  5424. if (unlikely(!eth_p_mpls(mpls_proto)))
  5425. return -EINVAL;
  5426. /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
  5427. if (skb->encapsulation)
  5428. return -EINVAL;
  5429. err = skb_cow_head(skb, MPLS_HLEN);
  5430. if (unlikely(err))
  5431. return err;
  5432. if (!skb->inner_protocol) {
  5433. skb_set_inner_network_header(skb, skb_network_offset(skb));
  5434. skb_set_inner_protocol(skb, skb->protocol);
  5435. }
  5436. skb_push(skb, MPLS_HLEN);
  5437. memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
  5438. mac_len);
  5439. skb_reset_mac_header(skb);
  5440. skb_set_network_header(skb, mac_len);
  5441. skb_reset_mac_len(skb);
  5442. lse = mpls_hdr(skb);
  5443. lse->label_stack_entry = mpls_lse;
  5444. skb_postpush_rcsum(skb, lse, MPLS_HLEN);
  5445. if (ethernet && mac_len >= ETH_HLEN)
  5446. skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
  5447. skb->protocol = mpls_proto;
  5448. return 0;
  5449. }
  5450. EXPORT_SYMBOL_GPL(skb_mpls_push);
  5451. /**
  5452. * skb_mpls_pop() - pop the outermost MPLS header
  5453. *
  5454. * @skb: buffer
  5455. * @next_proto: ethertype of header after popped MPLS header
  5456. * @mac_len: length of the MAC header
  5457. * @ethernet: flag to indicate if the packet is ethernet
  5458. *
  5459. * Expects skb->data at mac header.
  5460. *
  5461. * Returns 0 on success, -errno otherwise.
  5462. */
  5463. int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
  5464. bool ethernet)
  5465. {
  5466. int err;
  5467. if (unlikely(!eth_p_mpls(skb->protocol)))
  5468. return 0;
  5469. err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
  5470. if (unlikely(err))
  5471. return err;
  5472. skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
  5473. memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
  5474. mac_len);
  5475. __skb_pull(skb, MPLS_HLEN);
  5476. skb_reset_mac_header(skb);
  5477. skb_set_network_header(skb, mac_len);
  5478. if (ethernet && mac_len >= ETH_HLEN) {
  5479. struct ethhdr *hdr;
  5480. /* use mpls_hdr() to get ethertype to account for VLANs. */
  5481. hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
  5482. skb_mod_eth_type(skb, hdr, next_proto);
  5483. }
  5484. skb->protocol = next_proto;
  5485. return 0;
  5486. }
  5487. EXPORT_SYMBOL_GPL(skb_mpls_pop);
  5488. /**
  5489. * skb_mpls_update_lse() - modify outermost MPLS header and update csum
  5490. *
  5491. * @skb: buffer
  5492. * @mpls_lse: new MPLS label stack entry to update to
  5493. *
  5494. * Expects skb->data at mac header.
  5495. *
  5496. * Returns 0 on success, -errno otherwise.
  5497. */
  5498. int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
  5499. {
  5500. int err;
  5501. if (unlikely(!eth_p_mpls(skb->protocol)))
  5502. return -EINVAL;
  5503. err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
  5504. if (unlikely(err))
  5505. return err;
  5506. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  5507. __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
  5508. skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
  5509. }
  5510. mpls_hdr(skb)->label_stack_entry = mpls_lse;
  5511. return 0;
  5512. }
  5513. EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
  5514. /**
  5515. * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
  5516. *
  5517. * @skb: buffer
  5518. *
  5519. * Expects skb->data at mac header.
  5520. *
  5521. * Returns 0 on success, -errno otherwise.
  5522. */
  5523. int skb_mpls_dec_ttl(struct sk_buff *skb)
  5524. {
  5525. u32 lse;
  5526. u8 ttl;
  5527. if (unlikely(!eth_p_mpls(skb->protocol)))
  5528. return -EINVAL;
  5529. if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
  5530. return -ENOMEM;
  5531. lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
  5532. ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
  5533. if (!--ttl)
  5534. return -EINVAL;
  5535. lse &= ~MPLS_LS_TTL_MASK;
  5536. lse |= ttl << MPLS_LS_TTL_SHIFT;
  5537. return skb_mpls_update_lse(skb, cpu_to_be32(lse));
  5538. }
  5539. EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
  5540. /**
  5541. * alloc_skb_with_frags - allocate skb with page frags
  5542. *
  5543. * @header_len: size of linear part
  5544. * @data_len: needed length in frags
  5545. * @order: max page order desired.
  5546. * @errcode: pointer to error code if any
  5547. * @gfp_mask: allocation mask
  5548. *
  5549. * This can be used to allocate a paged skb, given a maximal order for frags.
  5550. */
  5551. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  5552. unsigned long data_len,
  5553. int order,
  5554. int *errcode,
  5555. gfp_t gfp_mask)
  5556. {
  5557. unsigned long chunk;
  5558. struct sk_buff *skb;
  5559. struct page *page;
  5560. int nr_frags = 0;
  5561. *errcode = -EMSGSIZE;
  5562. if (unlikely(data_len > MAX_SKB_FRAGS * (PAGE_SIZE << order)))
  5563. return NULL;
  5564. *errcode = -ENOBUFS;
  5565. skb = alloc_skb(header_len, gfp_mask);
  5566. if (!skb)
  5567. return NULL;
  5568. while (data_len) {
  5569. if (nr_frags == MAX_SKB_FRAGS)
  5570. goto failure;
  5571. while (order && PAGE_ALIGN(data_len) < (PAGE_SIZE << order))
  5572. order--;
  5573. if (order) {
  5574. page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
  5575. __GFP_COMP |
  5576. __GFP_NOWARN,
  5577. order);
  5578. if (!page) {
  5579. order--;
  5580. continue;
  5581. }
  5582. } else {
  5583. page = alloc_page(gfp_mask);
  5584. if (!page)
  5585. goto failure;
  5586. }
  5587. chunk = min_t(unsigned long, data_len,
  5588. PAGE_SIZE << order);
  5589. skb_fill_page_desc(skb, nr_frags, page, 0, chunk);
  5590. nr_frags++;
  5591. skb->truesize += (PAGE_SIZE << order);
  5592. data_len -= chunk;
  5593. }
  5594. return skb;
  5595. failure:
  5596. kfree_skb(skb);
  5597. return NULL;
  5598. }
  5599. EXPORT_SYMBOL(alloc_skb_with_frags);
  5600. /* carve out the first off bytes from skb when off < headlen */
  5601. static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
  5602. const int headlen, gfp_t gfp_mask)
  5603. {
  5604. int i;
  5605. unsigned int size = skb_end_offset(skb);
  5606. int new_hlen = headlen - off;
  5607. u8 *data;
  5608. if (skb_pfmemalloc(skb))
  5609. gfp_mask |= __GFP_MEMALLOC;
  5610. data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
  5611. if (!data)
  5612. return -ENOMEM;
  5613. size = SKB_WITH_OVERHEAD(size);
  5614. /* Copy real data, and all frags */
  5615. skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
  5616. skb->len -= off;
  5617. memcpy((struct skb_shared_info *)(data + size),
  5618. skb_shinfo(skb),
  5619. offsetof(struct skb_shared_info,
  5620. frags[skb_shinfo(skb)->nr_frags]));
  5621. if (skb_cloned(skb)) {
  5622. /* drop the old head gracefully */
  5623. if (skb_orphan_frags(skb, gfp_mask)) {
  5624. skb_kfree_head(data, size);
  5625. return -ENOMEM;
  5626. }
  5627. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  5628. skb_frag_ref(skb, i);
  5629. if (skb_has_frag_list(skb))
  5630. skb_clone_fraglist(skb);
  5631. skb_release_data(skb, SKB_CONSUMED);
  5632. } else {
  5633. /* we can reuse existing recount- all we did was
  5634. * relocate values
  5635. */
  5636. skb_free_head(skb);
  5637. }
  5638. skb->head = data;
  5639. skb->data = data;
  5640. skb->head_frag = 0;
  5641. skb_set_end_offset(skb, size);
  5642. skb_set_tail_pointer(skb, skb_headlen(skb));
  5643. skb_headers_offset_update(skb, 0);
  5644. skb->cloned = 0;
  5645. skb->hdr_len = 0;
  5646. skb->nohdr = 0;
  5647. atomic_set(&skb_shinfo(skb)->dataref, 1);
  5648. return 0;
  5649. }
  5650. static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
  5651. /* carve out the first eat bytes from skb's frag_list. May recurse into
  5652. * pskb_carve()
  5653. */
  5654. static int pskb_carve_frag_list(struct sk_buff *skb,
  5655. struct skb_shared_info *shinfo, int eat,
  5656. gfp_t gfp_mask)
  5657. {
  5658. struct sk_buff *list = shinfo->frag_list;
  5659. struct sk_buff *clone = NULL;
  5660. struct sk_buff *insp = NULL;
  5661. do {
  5662. if (!list) {
  5663. pr_err("Not enough bytes to eat. Want %d\n", eat);
  5664. return -EFAULT;
  5665. }
  5666. if (list->len <= eat) {
  5667. /* Eaten as whole. */
  5668. eat -= list->len;
  5669. list = list->next;
  5670. insp = list;
  5671. } else {
  5672. /* Eaten partially. */
  5673. if (skb_shared(list)) {
  5674. clone = skb_clone(list, gfp_mask);
  5675. if (!clone)
  5676. return -ENOMEM;
  5677. insp = list->next;
  5678. list = clone;
  5679. } else {
  5680. /* This may be pulled without problems. */
  5681. insp = list;
  5682. }
  5683. if (pskb_carve(list, eat, gfp_mask) < 0) {
  5684. kfree_skb(clone);
  5685. return -ENOMEM;
  5686. }
  5687. break;
  5688. }
  5689. } while (eat);
  5690. /* Free pulled out fragments. */
  5691. while ((list = shinfo->frag_list) != insp) {
  5692. shinfo->frag_list = list->next;
  5693. consume_skb(list);
  5694. }
  5695. /* And insert new clone at head. */
  5696. if (clone) {
  5697. clone->next = list;
  5698. shinfo->frag_list = clone;
  5699. }
  5700. return 0;
  5701. }
  5702. /* carve off first len bytes from skb. Split line (off) is in the
  5703. * non-linear part of skb
  5704. */
  5705. static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
  5706. int pos, gfp_t gfp_mask)
  5707. {
  5708. int i, k = 0;
  5709. unsigned int size = skb_end_offset(skb);
  5710. u8 *data;
  5711. const int nfrags = skb_shinfo(skb)->nr_frags;
  5712. struct skb_shared_info *shinfo;
  5713. if (skb_pfmemalloc(skb))
  5714. gfp_mask |= __GFP_MEMALLOC;
  5715. data = kmalloc_reserve(&size, gfp_mask, NUMA_NO_NODE, NULL);
  5716. if (!data)
  5717. return -ENOMEM;
  5718. size = SKB_WITH_OVERHEAD(size);
  5719. memcpy((struct skb_shared_info *)(data + size),
  5720. skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
  5721. if (skb_orphan_frags(skb, gfp_mask)) {
  5722. skb_kfree_head(data, size);
  5723. return -ENOMEM;
  5724. }
  5725. shinfo = (struct skb_shared_info *)(data + size);
  5726. for (i = 0; i < nfrags; i++) {
  5727. int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  5728. if (pos + fsize > off) {
  5729. shinfo->frags[k] = skb_shinfo(skb)->frags[i];
  5730. if (pos < off) {
  5731. /* Split frag.
  5732. * We have two variants in this case:
  5733. * 1. Move all the frag to the second
  5734. * part, if it is possible. F.e.
  5735. * this approach is mandatory for TUX,
  5736. * where splitting is expensive.
  5737. * 2. Split is accurately. We make this.
  5738. */
  5739. skb_frag_off_add(&shinfo->frags[0], off - pos);
  5740. skb_frag_size_sub(&shinfo->frags[0], off - pos);
  5741. }
  5742. skb_frag_ref(skb, i);
  5743. k++;
  5744. }
  5745. pos += fsize;
  5746. }
  5747. shinfo->nr_frags = k;
  5748. if (skb_has_frag_list(skb))
  5749. skb_clone_fraglist(skb);
  5750. /* split line is in frag list */
  5751. if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
  5752. /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
  5753. if (skb_has_frag_list(skb))
  5754. kfree_skb_list(skb_shinfo(skb)->frag_list);
  5755. skb_kfree_head(data, size);
  5756. return -ENOMEM;
  5757. }
  5758. skb_release_data(skb, SKB_CONSUMED);
  5759. skb->head = data;
  5760. skb->head_frag = 0;
  5761. skb->data = data;
  5762. skb_set_end_offset(skb, size);
  5763. skb_reset_tail_pointer(skb);
  5764. skb_headers_offset_update(skb, 0);
  5765. skb->cloned = 0;
  5766. skb->hdr_len = 0;
  5767. skb->nohdr = 0;
  5768. skb->len -= off;
  5769. skb->data_len = skb->len;
  5770. atomic_set(&skb_shinfo(skb)->dataref, 1);
  5771. return 0;
  5772. }
  5773. /* remove len bytes from the beginning of the skb */
  5774. static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
  5775. {
  5776. int headlen = skb_headlen(skb);
  5777. if (len < headlen)
  5778. return pskb_carve_inside_header(skb, len, headlen, gfp);
  5779. else
  5780. return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
  5781. }
  5782. /* Extract to_copy bytes starting at off from skb, and return this in
  5783. * a new skb
  5784. */
  5785. struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
  5786. int to_copy, gfp_t gfp)
  5787. {
  5788. struct sk_buff *clone = skb_clone(skb, gfp);
  5789. if (!clone)
  5790. return NULL;
  5791. if (pskb_carve(clone, off, gfp) < 0 ||
  5792. pskb_trim(clone, to_copy)) {
  5793. kfree_skb(clone);
  5794. return NULL;
  5795. }
  5796. return clone;
  5797. }
  5798. EXPORT_SYMBOL(pskb_extract);
  5799. /**
  5800. * skb_condense - try to get rid of fragments/frag_list if possible
  5801. * @skb: buffer
  5802. *
  5803. * Can be used to save memory before skb is added to a busy queue.
  5804. * If packet has bytes in frags and enough tail room in skb->head,
  5805. * pull all of them, so that we can free the frags right now and adjust
  5806. * truesize.
  5807. * Notes:
  5808. * We do not reallocate skb->head thus can not fail.
  5809. * Caller must re-evaluate skb->truesize if needed.
  5810. */
  5811. void skb_condense(struct sk_buff *skb)
  5812. {
  5813. if (skb->data_len) {
  5814. if (skb->data_len > skb->end - skb->tail ||
  5815. skb_cloned(skb) || !skb_frags_readable(skb))
  5816. return;
  5817. /* Nice, we can free page frag(s) right now */
  5818. __pskb_pull_tail(skb, skb->data_len);
  5819. }
  5820. /* At this point, skb->truesize might be over estimated,
  5821. * because skb had a fragment, and fragments do not tell
  5822. * their truesize.
  5823. * When we pulled its content into skb->head, fragment
  5824. * was freed, but __pskb_pull_tail() could not possibly
  5825. * adjust skb->truesize, not knowing the frag truesize.
  5826. */
  5827. skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
  5828. }
  5829. EXPORT_SYMBOL(skb_condense);
  5830. #ifdef CONFIG_SKB_EXTENSIONS
  5831. static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
  5832. {
  5833. return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
  5834. }
  5835. /**
  5836. * __skb_ext_alloc - allocate a new skb extensions storage
  5837. *
  5838. * @flags: See kmalloc().
  5839. *
  5840. * Returns the newly allocated pointer. The pointer can later attached to a
  5841. * skb via __skb_ext_set().
  5842. * Note: caller must handle the skb_ext as an opaque data.
  5843. */
  5844. struct skb_ext *__skb_ext_alloc(gfp_t flags)
  5845. {
  5846. struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
  5847. if (new) {
  5848. memset(new->offset, 0, sizeof(new->offset));
  5849. refcount_set(&new->refcnt, 1);
  5850. }
  5851. return new;
  5852. }
  5853. static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
  5854. unsigned int old_active)
  5855. {
  5856. struct skb_ext *new;
  5857. if (refcount_read(&old->refcnt) == 1)
  5858. return old;
  5859. new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
  5860. if (!new)
  5861. return NULL;
  5862. memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
  5863. refcount_set(&new->refcnt, 1);
  5864. #ifdef CONFIG_XFRM
  5865. if (old_active & (1 << SKB_EXT_SEC_PATH)) {
  5866. struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
  5867. unsigned int i;
  5868. for (i = 0; i < sp->len; i++)
  5869. xfrm_state_hold(sp->xvec[i]);
  5870. }
  5871. #endif
  5872. #ifdef CONFIG_MCTP_FLOWS
  5873. if (old_active & (1 << SKB_EXT_MCTP)) {
  5874. struct mctp_flow *flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
  5875. if (flow->key)
  5876. refcount_inc(&flow->key->refs);
  5877. }
  5878. #endif
  5879. __skb_ext_put(old);
  5880. return new;
  5881. }
  5882. /**
  5883. * __skb_ext_set - attach the specified extension storage to this skb
  5884. * @skb: buffer
  5885. * @id: extension id
  5886. * @ext: extension storage previously allocated via __skb_ext_alloc()
  5887. *
  5888. * Existing extensions, if any, are cleared.
  5889. *
  5890. * Returns the pointer to the extension.
  5891. */
  5892. void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
  5893. struct skb_ext *ext)
  5894. {
  5895. unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
  5896. skb_ext_put(skb);
  5897. newlen = newoff + skb_ext_type_len[id];
  5898. ext->chunks = newlen;
  5899. ext->offset[id] = newoff;
  5900. skb->extensions = ext;
  5901. skb->active_extensions = 1 << id;
  5902. return skb_ext_get_ptr(ext, id);
  5903. }
  5904. /**
  5905. * skb_ext_add - allocate space for given extension, COW if needed
  5906. * @skb: buffer
  5907. * @id: extension to allocate space for
  5908. *
  5909. * Allocates enough space for the given extension.
  5910. * If the extension is already present, a pointer to that extension
  5911. * is returned.
  5912. *
  5913. * If the skb was cloned, COW applies and the returned memory can be
  5914. * modified without changing the extension space of clones buffers.
  5915. *
  5916. * Returns pointer to the extension or NULL on allocation failure.
  5917. */
  5918. void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
  5919. {
  5920. struct skb_ext *new, *old = NULL;
  5921. unsigned int newlen, newoff;
  5922. if (skb->active_extensions) {
  5923. old = skb->extensions;
  5924. new = skb_ext_maybe_cow(old, skb->active_extensions);
  5925. if (!new)
  5926. return NULL;
  5927. if (__skb_ext_exist(new, id))
  5928. goto set_active;
  5929. newoff = new->chunks;
  5930. } else {
  5931. newoff = SKB_EXT_CHUNKSIZEOF(*new);
  5932. new = __skb_ext_alloc(GFP_ATOMIC);
  5933. if (!new)
  5934. return NULL;
  5935. }
  5936. newlen = newoff + skb_ext_type_len[id];
  5937. new->chunks = newlen;
  5938. new->offset[id] = newoff;
  5939. set_active:
  5940. skb->slow_gro = 1;
  5941. skb->extensions = new;
  5942. skb->active_extensions |= 1 << id;
  5943. return skb_ext_get_ptr(new, id);
  5944. }
  5945. EXPORT_SYMBOL(skb_ext_add);
  5946. #ifdef CONFIG_XFRM
  5947. static void skb_ext_put_sp(struct sec_path *sp)
  5948. {
  5949. unsigned int i;
  5950. for (i = 0; i < sp->len; i++)
  5951. xfrm_state_put(sp->xvec[i]);
  5952. }
  5953. #endif
  5954. #ifdef CONFIG_MCTP_FLOWS
  5955. static void skb_ext_put_mctp(struct mctp_flow *flow)
  5956. {
  5957. if (flow->key)
  5958. mctp_key_unref(flow->key);
  5959. }
  5960. #endif
  5961. void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
  5962. {
  5963. struct skb_ext *ext = skb->extensions;
  5964. skb->active_extensions &= ~(1 << id);
  5965. if (skb->active_extensions == 0) {
  5966. skb->extensions = NULL;
  5967. __skb_ext_put(ext);
  5968. #ifdef CONFIG_XFRM
  5969. } else if (id == SKB_EXT_SEC_PATH &&
  5970. refcount_read(&ext->refcnt) == 1) {
  5971. struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
  5972. skb_ext_put_sp(sp);
  5973. sp->len = 0;
  5974. #endif
  5975. }
  5976. }
  5977. EXPORT_SYMBOL(__skb_ext_del);
  5978. void __skb_ext_put(struct skb_ext *ext)
  5979. {
  5980. /* If this is last clone, nothing can increment
  5981. * it after check passes. Avoids one atomic op.
  5982. */
  5983. if (refcount_read(&ext->refcnt) == 1)
  5984. goto free_now;
  5985. if (!refcount_dec_and_test(&ext->refcnt))
  5986. return;
  5987. free_now:
  5988. #ifdef CONFIG_XFRM
  5989. if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
  5990. skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
  5991. #endif
  5992. #ifdef CONFIG_MCTP_FLOWS
  5993. if (__skb_ext_exist(ext, SKB_EXT_MCTP))
  5994. skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
  5995. #endif
  5996. kmem_cache_free(skbuff_ext_cache, ext);
  5997. }
  5998. EXPORT_SYMBOL(__skb_ext_put);
  5999. #endif /* CONFIG_SKB_EXTENSIONS */
  6000. static void kfree_skb_napi_cache(struct sk_buff *skb)
  6001. {
  6002. /* if SKB is a clone, don't handle this case */
  6003. if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
  6004. __kfree_skb(skb);
  6005. return;
  6006. }
  6007. local_bh_disable();
  6008. __napi_kfree_skb(skb, SKB_CONSUMED);
  6009. local_bh_enable();
  6010. }
  6011. /**
  6012. * skb_attempt_defer_free - queue skb for remote freeing
  6013. * @skb: buffer
  6014. *
  6015. * Put @skb in a per-cpu list, using the cpu which
  6016. * allocated the skb/pages to reduce false sharing
  6017. * and memory zone spinlock contention.
  6018. */
  6019. void skb_attempt_defer_free(struct sk_buff *skb)
  6020. {
  6021. int cpu = skb->alloc_cpu;
  6022. struct softnet_data *sd;
  6023. unsigned int defer_max;
  6024. bool kick;
  6025. if (cpu == raw_smp_processor_id() ||
  6026. WARN_ON_ONCE(cpu >= nr_cpu_ids) ||
  6027. !cpu_online(cpu)) {
  6028. nodefer: kfree_skb_napi_cache(skb);
  6029. return;
  6030. }
  6031. DEBUG_NET_WARN_ON_ONCE(skb_dst(skb));
  6032. DEBUG_NET_WARN_ON_ONCE(skb->destructor);
  6033. sd = &per_cpu(softnet_data, cpu);
  6034. defer_max = READ_ONCE(net_hotdata.sysctl_skb_defer_max);
  6035. if (READ_ONCE(sd->defer_count) >= defer_max)
  6036. goto nodefer;
  6037. spin_lock_bh(&sd->defer_lock);
  6038. /* Send an IPI every time queue reaches half capacity. */
  6039. kick = sd->defer_count == (defer_max >> 1);
  6040. /* Paired with the READ_ONCE() few lines above */
  6041. WRITE_ONCE(sd->defer_count, sd->defer_count + 1);
  6042. skb->next = sd->defer_list;
  6043. /* Paired with READ_ONCE() in skb_defer_free_flush() */
  6044. WRITE_ONCE(sd->defer_list, skb);
  6045. spin_unlock_bh(&sd->defer_lock);
  6046. /* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU
  6047. * if we are unlucky enough (this seems very unlikely).
  6048. */
  6049. if (unlikely(kick))
  6050. kick_defer_list_purge(sd, cpu);
  6051. }
  6052. static void skb_splice_csum_page(struct sk_buff *skb, struct page *page,
  6053. size_t offset, size_t len)
  6054. {
  6055. const char *kaddr;
  6056. __wsum csum;
  6057. kaddr = kmap_local_page(page);
  6058. csum = csum_partial(kaddr + offset, len, 0);
  6059. kunmap_local(kaddr);
  6060. skb->csum = csum_block_add(skb->csum, csum, skb->len);
  6061. }
  6062. /**
  6063. * skb_splice_from_iter - Splice (or copy) pages to skbuff
  6064. * @skb: The buffer to add pages to
  6065. * @iter: Iterator representing the pages to be added
  6066. * @maxsize: Maximum amount of pages to be added
  6067. * @gfp: Allocation flags
  6068. *
  6069. * This is a common helper function for supporting MSG_SPLICE_PAGES. It
  6070. * extracts pages from an iterator and adds them to the socket buffer if
  6071. * possible, copying them to fragments if not possible (such as if they're slab
  6072. * pages).
  6073. *
  6074. * Returns the amount of data spliced/copied or -EMSGSIZE if there's
  6075. * insufficient space in the buffer to transfer anything.
  6076. */
  6077. ssize_t skb_splice_from_iter(struct sk_buff *skb, struct iov_iter *iter,
  6078. ssize_t maxsize, gfp_t gfp)
  6079. {
  6080. size_t frag_limit = READ_ONCE(net_hotdata.sysctl_max_skb_frags);
  6081. struct page *pages[8], **ppages = pages;
  6082. ssize_t spliced = 0, ret = 0;
  6083. unsigned int i;
  6084. while (iter->count > 0) {
  6085. ssize_t space, nr, len;
  6086. size_t off;
  6087. ret = -EMSGSIZE;
  6088. space = frag_limit - skb_shinfo(skb)->nr_frags;
  6089. if (space < 0)
  6090. break;
  6091. /* We might be able to coalesce without increasing nr_frags */
  6092. nr = clamp_t(size_t, space, 1, ARRAY_SIZE(pages));
  6093. len = iov_iter_extract_pages(iter, &ppages, maxsize, nr, 0, &off);
  6094. if (len <= 0) {
  6095. ret = len ?: -EIO;
  6096. break;
  6097. }
  6098. i = 0;
  6099. do {
  6100. struct page *page = pages[i++];
  6101. size_t part = min_t(size_t, PAGE_SIZE - off, len);
  6102. ret = -EIO;
  6103. if (WARN_ON_ONCE(!sendpage_ok(page)))
  6104. goto out;
  6105. ret = skb_append_pagefrags(skb, page, off, part,
  6106. frag_limit);
  6107. if (ret < 0) {
  6108. iov_iter_revert(iter, len);
  6109. goto out;
  6110. }
  6111. if (skb->ip_summed == CHECKSUM_NONE)
  6112. skb_splice_csum_page(skb, page, off, part);
  6113. off = 0;
  6114. spliced += part;
  6115. maxsize -= part;
  6116. len -= part;
  6117. } while (len > 0);
  6118. if (maxsize <= 0)
  6119. break;
  6120. }
  6121. out:
  6122. skb_len_add(skb, spliced);
  6123. return spliced ?: ret;
  6124. }
  6125. EXPORT_SYMBOL(skb_splice_from_iter);
  6126. static __always_inline
  6127. size_t memcpy_from_iter_csum(void *iter_from, size_t progress,
  6128. size_t len, void *to, void *priv2)
  6129. {
  6130. __wsum *csum = priv2;
  6131. __wsum next = csum_partial_copy_nocheck(iter_from, to + progress, len);
  6132. *csum = csum_block_add(*csum, next, progress);
  6133. return 0;
  6134. }
  6135. static __always_inline
  6136. size_t copy_from_user_iter_csum(void __user *iter_from, size_t progress,
  6137. size_t len, void *to, void *priv2)
  6138. {
  6139. __wsum next, *csum = priv2;
  6140. next = csum_and_copy_from_user(iter_from, to + progress, len);
  6141. *csum = csum_block_add(*csum, next, progress);
  6142. return next ? 0 : len;
  6143. }
  6144. bool csum_and_copy_from_iter_full(void *addr, size_t bytes,
  6145. __wsum *csum, struct iov_iter *i)
  6146. {
  6147. size_t copied;
  6148. if (WARN_ON_ONCE(!i->data_source))
  6149. return false;
  6150. copied = iterate_and_advance2(i, bytes, addr, csum,
  6151. copy_from_user_iter_csum,
  6152. memcpy_from_iter_csum);
  6153. if (likely(copied == bytes))
  6154. return true;
  6155. iov_iter_revert(i, copied);
  6156. return false;
  6157. }
  6158. EXPORT_SYMBOL(csum_and_copy_from_iter_full);