skbuff.c 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583
  1. /*
  2. * Routines having to do with the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
  5. * Florian La Roche <rzsfl@rz.uni-sb.de>
  6. *
  7. * Fixes:
  8. * Alan Cox : Fixed the worst of the load
  9. * balancer bugs.
  10. * Dave Platt : Interrupt stacking fix.
  11. * Richard Kooijman : Timestamp fixes.
  12. * Alan Cox : Changed buffer format.
  13. * Alan Cox : destructor hook for AF_UNIX etc.
  14. * Linus Torvalds : Better skb_clone.
  15. * Alan Cox : Added skb_copy.
  16. * Alan Cox : Added all the changed routines Linus
  17. * only put in the headers
  18. * Ray VanTassle : Fixed --skb->lock in free
  19. * Alan Cox : skb_copy copy arp field
  20. * Andi Kleen : slabified it.
  21. * Robert Olsson : Removed skb_head_pool
  22. *
  23. * NOTE:
  24. * The __skb_ routines should be called with interrupts
  25. * disabled, or you better be *real* sure that the operation is atomic
  26. * with respect to whatever list is being frobbed (e.g. via lock_sock()
  27. * or via disabling bottom half handlers, etc).
  28. *
  29. * This program is free software; you can redistribute it and/or
  30. * modify it under the terms of the GNU General Public License
  31. * as published by the Free Software Foundation; either version
  32. * 2 of the License, or (at your option) any later version.
  33. */
  34. /*
  35. * The functions in this file will not compile correctly with gcc 2.4.x
  36. */
  37. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/mm.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/in.h>
  44. #include <linux/inet.h>
  45. #include <linux/slab.h>
  46. #include <linux/tcp.h>
  47. #include <linux/udp.h>
  48. #include <linux/sctp.h>
  49. #include <linux/netdevice.h>
  50. #ifdef CONFIG_NET_CLS_ACT
  51. #include <net/pkt_sched.h>
  52. #endif
  53. #include <linux/string.h>
  54. #include <linux/skbuff.h>
  55. #include <linux/splice.h>
  56. #include <linux/cache.h>
  57. #include <linux/rtnetlink.h>
  58. #include <linux/init.h>
  59. #include <linux/scatterlist.h>
  60. #include <linux/errqueue.h>
  61. #include <linux/prefetch.h>
  62. #include <linux/if_vlan.h>
  63. #include <net/protocol.h>
  64. #include <net/dst.h>
  65. #include <net/sock.h>
  66. #include <net/checksum.h>
  67. #include <net/ip6_checksum.h>
  68. #include <net/xfrm.h>
  69. #include <linux/uaccess.h>
  70. #include <trace/events/skb.h>
  71. #include <linux/highmem.h>
  72. #include <linux/capability.h>
  73. #include <linux/user_namespace.h>
  74. struct kmem_cache *skbuff_head_cache __ro_after_init;
  75. static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
  76. int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
  77. EXPORT_SYMBOL(sysctl_max_skb_frags);
  78. /**
  79. * skb_panic - private function for out-of-line support
  80. * @skb: buffer
  81. * @sz: size
  82. * @addr: address
  83. * @msg: skb_over_panic or skb_under_panic
  84. *
  85. * Out-of-line support for skb_put() and skb_push().
  86. * Called via the wrapper skb_over_panic() or skb_under_panic().
  87. * Keep out of line to prevent kernel bloat.
  88. * __builtin_return_address is not used because it is not always reliable.
  89. */
  90. static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
  91. const char msg[])
  92. {
  93. pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
  94. msg, addr, skb->len, sz, skb->head, skb->data,
  95. (unsigned long)skb->tail, (unsigned long)skb->end,
  96. skb->dev ? skb->dev->name : "<NULL>");
  97. BUG();
  98. }
  99. static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  100. {
  101. skb_panic(skb, sz, addr, __func__);
  102. }
  103. static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  104. {
  105. skb_panic(skb, sz, addr, __func__);
  106. }
  107. /*
  108. * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
  109. * the caller if emergency pfmemalloc reserves are being used. If it is and
  110. * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
  111. * may be used. Otherwise, the packet data may be discarded until enough
  112. * memory is free
  113. */
  114. #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
  115. __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
  116. static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
  117. unsigned long ip, bool *pfmemalloc)
  118. {
  119. void *obj;
  120. bool ret_pfmemalloc = false;
  121. /*
  122. * Try a regular allocation, when that fails and we're not entitled
  123. * to the reserves, fail.
  124. */
  125. obj = kmalloc_node_track_caller(size,
  126. flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
  127. node);
  128. if (obj || !(gfp_pfmemalloc_allowed(flags)))
  129. goto out;
  130. /* Try again but now we are using pfmemalloc reserves */
  131. ret_pfmemalloc = true;
  132. obj = kmalloc_node_track_caller(size, flags, node);
  133. out:
  134. if (pfmemalloc)
  135. *pfmemalloc = ret_pfmemalloc;
  136. return obj;
  137. }
  138. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  139. * 'private' fields and also do memory statistics to find all the
  140. * [BEEP] leaks.
  141. *
  142. */
  143. /**
  144. * __alloc_skb - allocate a network buffer
  145. * @size: size to allocate
  146. * @gfp_mask: allocation mask
  147. * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
  148. * instead of head cache and allocate a cloned (child) skb.
  149. * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
  150. * allocations in case the data is required for writeback
  151. * @node: numa node to allocate memory on
  152. *
  153. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  154. * tail room of at least size bytes. The object has a reference count
  155. * of one. The return is the buffer. On a failure the return is %NULL.
  156. *
  157. * Buffers may only be allocated from interrupts using a @gfp_mask of
  158. * %GFP_ATOMIC.
  159. */
  160. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  161. int flags, int node)
  162. {
  163. struct kmem_cache *cache;
  164. struct skb_shared_info *shinfo;
  165. struct sk_buff *skb;
  166. u8 *data;
  167. bool pfmemalloc;
  168. cache = (flags & SKB_ALLOC_FCLONE)
  169. ? skbuff_fclone_cache : skbuff_head_cache;
  170. if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
  171. gfp_mask |= __GFP_MEMALLOC;
  172. /* Get the HEAD */
  173. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  174. if (!skb)
  175. goto out;
  176. prefetchw(skb);
  177. /* We do our best to align skb_shared_info on a separate cache
  178. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  179. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  180. * Both skb->head and skb_shared_info are cache line aligned.
  181. */
  182. size = SKB_DATA_ALIGN(size);
  183. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  184. data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
  185. if (!data)
  186. goto nodata;
  187. /* kmalloc(size) might give us more room than requested.
  188. * Put skb_shared_info exactly at the end of allocated zone,
  189. * to allow max possible filling before reallocation.
  190. */
  191. size = SKB_WITH_OVERHEAD(ksize(data));
  192. prefetchw(data + size);
  193. /*
  194. * Only clear those fields we need to clear, not those that we will
  195. * actually initialise below. Hence, don't put any more fields after
  196. * the tail pointer in struct sk_buff!
  197. */
  198. memset(skb, 0, offsetof(struct sk_buff, tail));
  199. /* Account for allocated memory : skb + skb->head */
  200. skb->truesize = SKB_TRUESIZE(size);
  201. skb->pfmemalloc = pfmemalloc;
  202. refcount_set(&skb->users, 1);
  203. skb->head = data;
  204. skb->data = data;
  205. skb_reset_tail_pointer(skb);
  206. skb->end = skb->tail + size;
  207. skb->mac_header = (typeof(skb->mac_header))~0U;
  208. skb->transport_header = (typeof(skb->transport_header))~0U;
  209. /* make sure we initialize shinfo sequentially */
  210. shinfo = skb_shinfo(skb);
  211. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  212. atomic_set(&shinfo->dataref, 1);
  213. if (flags & SKB_ALLOC_FCLONE) {
  214. struct sk_buff_fclones *fclones;
  215. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  216. skb->fclone = SKB_FCLONE_ORIG;
  217. refcount_set(&fclones->fclone_ref, 1);
  218. fclones->skb2.fclone = SKB_FCLONE_CLONE;
  219. }
  220. out:
  221. return skb;
  222. nodata:
  223. kmem_cache_free(cache, skb);
  224. skb = NULL;
  225. goto out;
  226. }
  227. EXPORT_SYMBOL(__alloc_skb);
  228. /**
  229. * __build_skb - build a network buffer
  230. * @data: data buffer provided by caller
  231. * @frag_size: size of data, or 0 if head was kmalloced
  232. *
  233. * Allocate a new &sk_buff. Caller provides space holding head and
  234. * skb_shared_info. @data must have been allocated by kmalloc() only if
  235. * @frag_size is 0, otherwise data should come from the page allocator
  236. * or vmalloc()
  237. * The return is the new skb buffer.
  238. * On a failure the return is %NULL, and @data is not freed.
  239. * Notes :
  240. * Before IO, driver allocates only data buffer where NIC put incoming frame
  241. * Driver should add room at head (NET_SKB_PAD) and
  242. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  243. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  244. * before giving packet to stack.
  245. * RX rings only contains data buffers, not full skbs.
  246. */
  247. struct sk_buff *__build_skb(void *data, unsigned int frag_size)
  248. {
  249. struct skb_shared_info *shinfo;
  250. struct sk_buff *skb;
  251. unsigned int size = frag_size ? : ksize(data);
  252. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  253. if (!skb)
  254. return NULL;
  255. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  256. memset(skb, 0, offsetof(struct sk_buff, tail));
  257. skb->truesize = SKB_TRUESIZE(size);
  258. refcount_set(&skb->users, 1);
  259. skb->head = data;
  260. skb->data = data;
  261. skb_reset_tail_pointer(skb);
  262. skb->end = skb->tail + size;
  263. skb->mac_header = (typeof(skb->mac_header))~0U;
  264. skb->transport_header = (typeof(skb->transport_header))~0U;
  265. /* make sure we initialize shinfo sequentially */
  266. shinfo = skb_shinfo(skb);
  267. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  268. atomic_set(&shinfo->dataref, 1);
  269. return skb;
  270. }
  271. /* build_skb() is wrapper over __build_skb(), that specifically
  272. * takes care of skb->head and skb->pfmemalloc
  273. * This means that if @frag_size is not zero, then @data must be backed
  274. * by a page fragment, not kmalloc() or vmalloc()
  275. */
  276. struct sk_buff *build_skb(void *data, unsigned int frag_size)
  277. {
  278. struct sk_buff *skb = __build_skb(data, frag_size);
  279. if (skb && frag_size) {
  280. skb->head_frag = 1;
  281. if (page_is_pfmemalloc(virt_to_head_page(data)))
  282. skb->pfmemalloc = 1;
  283. }
  284. return skb;
  285. }
  286. EXPORT_SYMBOL(build_skb);
  287. #define NAPI_SKB_CACHE_SIZE 64
  288. struct napi_alloc_cache {
  289. struct page_frag_cache page;
  290. unsigned int skb_count;
  291. void *skb_cache[NAPI_SKB_CACHE_SIZE];
  292. };
  293. static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
  294. static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
  295. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  296. {
  297. struct page_frag_cache *nc;
  298. unsigned long flags;
  299. void *data;
  300. local_irq_save(flags);
  301. nc = this_cpu_ptr(&netdev_alloc_cache);
  302. data = page_frag_alloc(nc, fragsz, gfp_mask);
  303. local_irq_restore(flags);
  304. return data;
  305. }
  306. /**
  307. * netdev_alloc_frag - allocate a page fragment
  308. * @fragsz: fragment size
  309. *
  310. * Allocates a frag from a page for receive buffer.
  311. * Uses GFP_ATOMIC allocations.
  312. */
  313. void *netdev_alloc_frag(unsigned int fragsz)
  314. {
  315. return __netdev_alloc_frag(fragsz, GFP_ATOMIC);
  316. }
  317. EXPORT_SYMBOL(netdev_alloc_frag);
  318. static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  319. {
  320. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  321. return page_frag_alloc(&nc->page, fragsz, gfp_mask);
  322. }
  323. void *napi_alloc_frag(unsigned int fragsz)
  324. {
  325. return __napi_alloc_frag(fragsz, GFP_ATOMIC);
  326. }
  327. EXPORT_SYMBOL(napi_alloc_frag);
  328. /**
  329. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  330. * @dev: network device to receive on
  331. * @len: length to allocate
  332. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  333. *
  334. * Allocate a new &sk_buff and assign it a usage count of one. The
  335. * buffer has NET_SKB_PAD headroom built in. Users should allocate
  336. * the headroom they think they need without accounting for the
  337. * built in space. The built in space is used for optimisations.
  338. *
  339. * %NULL is returned if there is no free memory.
  340. */
  341. struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
  342. gfp_t gfp_mask)
  343. {
  344. struct page_frag_cache *nc;
  345. unsigned long flags;
  346. struct sk_buff *skb;
  347. bool pfmemalloc;
  348. void *data;
  349. len += NET_SKB_PAD;
  350. if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
  351. (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
  352. skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
  353. if (!skb)
  354. goto skb_fail;
  355. goto skb_success;
  356. }
  357. len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  358. len = SKB_DATA_ALIGN(len);
  359. if (sk_memalloc_socks())
  360. gfp_mask |= __GFP_MEMALLOC;
  361. local_irq_save(flags);
  362. nc = this_cpu_ptr(&netdev_alloc_cache);
  363. data = page_frag_alloc(nc, len, gfp_mask);
  364. pfmemalloc = nc->pfmemalloc;
  365. local_irq_restore(flags);
  366. if (unlikely(!data))
  367. return NULL;
  368. skb = __build_skb(data, len);
  369. if (unlikely(!skb)) {
  370. skb_free_frag(data);
  371. return NULL;
  372. }
  373. /* use OR instead of assignment to avoid clearing of bits in mask */
  374. if (pfmemalloc)
  375. skb->pfmemalloc = 1;
  376. skb->head_frag = 1;
  377. skb_success:
  378. skb_reserve(skb, NET_SKB_PAD);
  379. skb->dev = dev;
  380. skb_fail:
  381. return skb;
  382. }
  383. EXPORT_SYMBOL(__netdev_alloc_skb);
  384. /**
  385. * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
  386. * @napi: napi instance this buffer was allocated for
  387. * @len: length to allocate
  388. * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
  389. *
  390. * Allocate a new sk_buff for use in NAPI receive. This buffer will
  391. * attempt to allocate the head from a special reserved region used
  392. * only for NAPI Rx allocation. By doing this we can save several
  393. * CPU cycles by avoiding having to disable and re-enable IRQs.
  394. *
  395. * %NULL is returned if there is no free memory.
  396. */
  397. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
  398. gfp_t gfp_mask)
  399. {
  400. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  401. struct sk_buff *skb;
  402. void *data;
  403. len += NET_SKB_PAD + NET_IP_ALIGN;
  404. if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
  405. (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
  406. skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
  407. if (!skb)
  408. goto skb_fail;
  409. goto skb_success;
  410. }
  411. len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  412. len = SKB_DATA_ALIGN(len);
  413. if (sk_memalloc_socks())
  414. gfp_mask |= __GFP_MEMALLOC;
  415. data = page_frag_alloc(&nc->page, len, gfp_mask);
  416. if (unlikely(!data))
  417. return NULL;
  418. skb = __build_skb(data, len);
  419. if (unlikely(!skb)) {
  420. skb_free_frag(data);
  421. return NULL;
  422. }
  423. /* use OR instead of assignment to avoid clearing of bits in mask */
  424. if (nc->page.pfmemalloc)
  425. skb->pfmemalloc = 1;
  426. skb->head_frag = 1;
  427. skb_success:
  428. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  429. skb->dev = napi->dev;
  430. skb_fail:
  431. return skb;
  432. }
  433. EXPORT_SYMBOL(__napi_alloc_skb);
  434. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  435. int size, unsigned int truesize)
  436. {
  437. skb_fill_page_desc(skb, i, page, off, size);
  438. skb->len += size;
  439. skb->data_len += size;
  440. skb->truesize += truesize;
  441. }
  442. EXPORT_SYMBOL(skb_add_rx_frag);
  443. void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
  444. unsigned int truesize)
  445. {
  446. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  447. skb_frag_size_add(frag, size);
  448. skb->len += size;
  449. skb->data_len += size;
  450. skb->truesize += truesize;
  451. }
  452. EXPORT_SYMBOL(skb_coalesce_rx_frag);
  453. static void skb_drop_list(struct sk_buff **listp)
  454. {
  455. kfree_skb_list(*listp);
  456. *listp = NULL;
  457. }
  458. static inline void skb_drop_fraglist(struct sk_buff *skb)
  459. {
  460. skb_drop_list(&skb_shinfo(skb)->frag_list);
  461. }
  462. static void skb_clone_fraglist(struct sk_buff *skb)
  463. {
  464. struct sk_buff *list;
  465. skb_walk_frags(skb, list)
  466. skb_get(list);
  467. }
  468. static void skb_free_head(struct sk_buff *skb)
  469. {
  470. unsigned char *head = skb->head;
  471. if (skb->head_frag)
  472. skb_free_frag(head);
  473. else
  474. kfree(head);
  475. }
  476. static void skb_release_data(struct sk_buff *skb)
  477. {
  478. struct skb_shared_info *shinfo = skb_shinfo(skb);
  479. int i;
  480. if (skb->cloned &&
  481. atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  482. &shinfo->dataref))
  483. return;
  484. for (i = 0; i < shinfo->nr_frags; i++)
  485. __skb_frag_unref(&shinfo->frags[i]);
  486. if (shinfo->frag_list)
  487. kfree_skb_list(shinfo->frag_list);
  488. skb_zcopy_clear(skb, true);
  489. skb_free_head(skb);
  490. }
  491. /*
  492. * Free an skbuff by memory without cleaning the state.
  493. */
  494. static void kfree_skbmem(struct sk_buff *skb)
  495. {
  496. struct sk_buff_fclones *fclones;
  497. switch (skb->fclone) {
  498. case SKB_FCLONE_UNAVAILABLE:
  499. kmem_cache_free(skbuff_head_cache, skb);
  500. return;
  501. case SKB_FCLONE_ORIG:
  502. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  503. /* We usually free the clone (TX completion) before original skb
  504. * This test would have no chance to be true for the clone,
  505. * while here, branch prediction will be good.
  506. */
  507. if (refcount_read(&fclones->fclone_ref) == 1)
  508. goto fastpath;
  509. break;
  510. default: /* SKB_FCLONE_CLONE */
  511. fclones = container_of(skb, struct sk_buff_fclones, skb2);
  512. break;
  513. }
  514. if (!refcount_dec_and_test(&fclones->fclone_ref))
  515. return;
  516. fastpath:
  517. kmem_cache_free(skbuff_fclone_cache, fclones);
  518. }
  519. void skb_release_head_state(struct sk_buff *skb)
  520. {
  521. skb_dst_drop(skb);
  522. secpath_reset(skb);
  523. if (skb->destructor) {
  524. WARN_ON(in_irq());
  525. skb->destructor(skb);
  526. }
  527. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  528. nf_conntrack_put(skb_nfct(skb));
  529. #endif
  530. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  531. nf_bridge_put(skb->nf_bridge);
  532. #endif
  533. }
  534. /* Free everything but the sk_buff shell. */
  535. static void skb_release_all(struct sk_buff *skb)
  536. {
  537. skb_release_head_state(skb);
  538. if (likely(skb->head))
  539. skb_release_data(skb);
  540. }
  541. /**
  542. * __kfree_skb - private function
  543. * @skb: buffer
  544. *
  545. * Free an sk_buff. Release anything attached to the buffer.
  546. * Clean the state. This is an internal helper function. Users should
  547. * always call kfree_skb
  548. */
  549. void __kfree_skb(struct sk_buff *skb)
  550. {
  551. skb_release_all(skb);
  552. kfree_skbmem(skb);
  553. }
  554. EXPORT_SYMBOL(__kfree_skb);
  555. /**
  556. * kfree_skb - free an sk_buff
  557. * @skb: buffer to free
  558. *
  559. * Drop a reference to the buffer and free it if the usage count has
  560. * hit zero.
  561. */
  562. void kfree_skb(struct sk_buff *skb)
  563. {
  564. if (!skb_unref(skb))
  565. return;
  566. trace_kfree_skb(skb, __builtin_return_address(0));
  567. __kfree_skb(skb);
  568. }
  569. EXPORT_SYMBOL(kfree_skb);
  570. void kfree_skb_list(struct sk_buff *segs)
  571. {
  572. while (segs) {
  573. struct sk_buff *next = segs->next;
  574. kfree_skb(segs);
  575. segs = next;
  576. }
  577. }
  578. EXPORT_SYMBOL(kfree_skb_list);
  579. /**
  580. * skb_tx_error - report an sk_buff xmit error
  581. * @skb: buffer that triggered an error
  582. *
  583. * Report xmit error if a device callback is tracking this skb.
  584. * skb must be freed afterwards.
  585. */
  586. void skb_tx_error(struct sk_buff *skb)
  587. {
  588. skb_zcopy_clear(skb, true);
  589. }
  590. EXPORT_SYMBOL(skb_tx_error);
  591. /**
  592. * consume_skb - free an skbuff
  593. * @skb: buffer to free
  594. *
  595. * Drop a ref to the buffer and free it if the usage count has hit zero
  596. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  597. * is being dropped after a failure and notes that
  598. */
  599. void consume_skb(struct sk_buff *skb)
  600. {
  601. if (!skb_unref(skb))
  602. return;
  603. trace_consume_skb(skb);
  604. __kfree_skb(skb);
  605. }
  606. EXPORT_SYMBOL(consume_skb);
  607. /**
  608. * consume_stateless_skb - free an skbuff, assuming it is stateless
  609. * @skb: buffer to free
  610. *
  611. * Alike consume_skb(), but this variant assumes that this is the last
  612. * skb reference and all the head states have been already dropped
  613. */
  614. void __consume_stateless_skb(struct sk_buff *skb)
  615. {
  616. trace_consume_skb(skb);
  617. skb_release_data(skb);
  618. kfree_skbmem(skb);
  619. }
  620. void __kfree_skb_flush(void)
  621. {
  622. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  623. /* flush skb_cache if containing objects */
  624. if (nc->skb_count) {
  625. kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
  626. nc->skb_cache);
  627. nc->skb_count = 0;
  628. }
  629. }
  630. static inline void _kfree_skb_defer(struct sk_buff *skb)
  631. {
  632. struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
  633. /* drop skb->head and call any destructors for packet */
  634. skb_release_all(skb);
  635. /* record skb to CPU local list */
  636. nc->skb_cache[nc->skb_count++] = skb;
  637. #ifdef CONFIG_SLUB
  638. /* SLUB writes into objects when freeing */
  639. prefetchw(skb);
  640. #endif
  641. /* flush skb_cache if it is filled */
  642. if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
  643. kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
  644. nc->skb_cache);
  645. nc->skb_count = 0;
  646. }
  647. }
  648. void __kfree_skb_defer(struct sk_buff *skb)
  649. {
  650. _kfree_skb_defer(skb);
  651. }
  652. void napi_consume_skb(struct sk_buff *skb, int budget)
  653. {
  654. if (unlikely(!skb))
  655. return;
  656. /* Zero budget indicate non-NAPI context called us, like netpoll */
  657. if (unlikely(!budget)) {
  658. dev_consume_skb_any(skb);
  659. return;
  660. }
  661. if (!skb_unref(skb))
  662. return;
  663. /* if reaching here SKB is ready to free */
  664. trace_consume_skb(skb);
  665. /* if SKB is a clone, don't handle this case */
  666. if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
  667. __kfree_skb(skb);
  668. return;
  669. }
  670. _kfree_skb_defer(skb);
  671. }
  672. EXPORT_SYMBOL(napi_consume_skb);
  673. /* Make sure a field is enclosed inside headers_start/headers_end section */
  674. #define CHECK_SKB_FIELD(field) \
  675. BUILD_BUG_ON(offsetof(struct sk_buff, field) < \
  676. offsetof(struct sk_buff, headers_start)); \
  677. BUILD_BUG_ON(offsetof(struct sk_buff, field) > \
  678. offsetof(struct sk_buff, headers_end)); \
  679. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  680. {
  681. new->tstamp = old->tstamp;
  682. /* We do not copy old->sk */
  683. new->dev = old->dev;
  684. memcpy(new->cb, old->cb, sizeof(old->cb));
  685. skb_dst_copy(new, old);
  686. #ifdef CONFIG_XFRM
  687. new->sp = secpath_get(old->sp);
  688. #endif
  689. __nf_copy(new, old, false);
  690. /* Note : this field could be in headers_start/headers_end section
  691. * It is not yet because we do not want to have a 16 bit hole
  692. */
  693. new->queue_mapping = old->queue_mapping;
  694. memcpy(&new->headers_start, &old->headers_start,
  695. offsetof(struct sk_buff, headers_end) -
  696. offsetof(struct sk_buff, headers_start));
  697. CHECK_SKB_FIELD(protocol);
  698. CHECK_SKB_FIELD(csum);
  699. CHECK_SKB_FIELD(hash);
  700. CHECK_SKB_FIELD(priority);
  701. CHECK_SKB_FIELD(skb_iif);
  702. CHECK_SKB_FIELD(vlan_proto);
  703. CHECK_SKB_FIELD(vlan_tci);
  704. CHECK_SKB_FIELD(transport_header);
  705. CHECK_SKB_FIELD(network_header);
  706. CHECK_SKB_FIELD(mac_header);
  707. CHECK_SKB_FIELD(inner_protocol);
  708. CHECK_SKB_FIELD(inner_transport_header);
  709. CHECK_SKB_FIELD(inner_network_header);
  710. CHECK_SKB_FIELD(inner_mac_header);
  711. CHECK_SKB_FIELD(mark);
  712. #ifdef CONFIG_NETWORK_SECMARK
  713. CHECK_SKB_FIELD(secmark);
  714. #endif
  715. #ifdef CONFIG_NET_RX_BUSY_POLL
  716. CHECK_SKB_FIELD(napi_id);
  717. #endif
  718. #ifdef CONFIG_XPS
  719. CHECK_SKB_FIELD(sender_cpu);
  720. #endif
  721. #ifdef CONFIG_NET_SCHED
  722. CHECK_SKB_FIELD(tc_index);
  723. #endif
  724. }
  725. /*
  726. * You should not add any new code to this function. Add it to
  727. * __copy_skb_header above instead.
  728. */
  729. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  730. {
  731. #define C(x) n->x = skb->x
  732. n->next = n->prev = NULL;
  733. n->sk = NULL;
  734. __copy_skb_header(n, skb);
  735. C(len);
  736. C(data_len);
  737. C(mac_len);
  738. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  739. n->cloned = 1;
  740. n->nohdr = 0;
  741. n->peeked = 0;
  742. C(pfmemalloc);
  743. n->destructor = NULL;
  744. C(tail);
  745. C(end);
  746. C(head);
  747. C(head_frag);
  748. C(data);
  749. C(truesize);
  750. refcount_set(&n->users, 1);
  751. atomic_inc(&(skb_shinfo(skb)->dataref));
  752. skb->cloned = 1;
  753. return n;
  754. #undef C
  755. }
  756. /**
  757. * skb_morph - morph one skb into another
  758. * @dst: the skb to receive the contents
  759. * @src: the skb to supply the contents
  760. *
  761. * This is identical to skb_clone except that the target skb is
  762. * supplied by the user.
  763. *
  764. * The target skb is returned upon exit.
  765. */
  766. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  767. {
  768. skb_release_all(dst);
  769. return __skb_clone(dst, src);
  770. }
  771. EXPORT_SYMBOL_GPL(skb_morph);
  772. int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
  773. {
  774. unsigned long max_pg, num_pg, new_pg, old_pg;
  775. struct user_struct *user;
  776. if (capable(CAP_IPC_LOCK) || !size)
  777. return 0;
  778. num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */
  779. max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
  780. user = mmp->user ? : current_user();
  781. do {
  782. old_pg = atomic_long_read(&user->locked_vm);
  783. new_pg = old_pg + num_pg;
  784. if (new_pg > max_pg)
  785. return -ENOBUFS;
  786. } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) !=
  787. old_pg);
  788. if (!mmp->user) {
  789. mmp->user = get_uid(user);
  790. mmp->num_pg = num_pg;
  791. } else {
  792. mmp->num_pg += num_pg;
  793. }
  794. return 0;
  795. }
  796. EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
  797. void mm_unaccount_pinned_pages(struct mmpin *mmp)
  798. {
  799. if (mmp->user) {
  800. atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
  801. free_uid(mmp->user);
  802. }
  803. }
  804. EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
  805. struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size)
  806. {
  807. struct ubuf_info *uarg;
  808. struct sk_buff *skb;
  809. WARN_ON_ONCE(!in_task());
  810. skb = sock_omalloc(sk, 0, GFP_KERNEL);
  811. if (!skb)
  812. return NULL;
  813. BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
  814. uarg = (void *)skb->cb;
  815. uarg->mmp.user = NULL;
  816. if (mm_account_pinned_pages(&uarg->mmp, size)) {
  817. kfree_skb(skb);
  818. return NULL;
  819. }
  820. uarg->callback = sock_zerocopy_callback;
  821. uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
  822. uarg->len = 1;
  823. uarg->bytelen = size;
  824. uarg->zerocopy = 1;
  825. refcount_set(&uarg->refcnt, 1);
  826. sock_hold(sk);
  827. return uarg;
  828. }
  829. EXPORT_SYMBOL_GPL(sock_zerocopy_alloc);
  830. static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg)
  831. {
  832. return container_of((void *)uarg, struct sk_buff, cb);
  833. }
  834. struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
  835. struct ubuf_info *uarg)
  836. {
  837. if (uarg) {
  838. const u32 byte_limit = 1 << 19; /* limit to a few TSO */
  839. u32 bytelen, next;
  840. /* realloc only when socket is locked (TCP, UDP cork),
  841. * so uarg->len and sk_zckey access is serialized
  842. */
  843. if (!sock_owned_by_user(sk)) {
  844. WARN_ON_ONCE(1);
  845. return NULL;
  846. }
  847. bytelen = uarg->bytelen + size;
  848. if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) {
  849. /* TCP can create new skb to attach new uarg */
  850. if (sk->sk_type == SOCK_STREAM)
  851. goto new_alloc;
  852. return NULL;
  853. }
  854. next = (u32)atomic_read(&sk->sk_zckey);
  855. if ((u32)(uarg->id + uarg->len) == next) {
  856. if (mm_account_pinned_pages(&uarg->mmp, size))
  857. return NULL;
  858. uarg->len++;
  859. uarg->bytelen = bytelen;
  860. atomic_set(&sk->sk_zckey, ++next);
  861. sock_zerocopy_get(uarg);
  862. return uarg;
  863. }
  864. }
  865. new_alloc:
  866. return sock_zerocopy_alloc(sk, size);
  867. }
  868. EXPORT_SYMBOL_GPL(sock_zerocopy_realloc);
  869. static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
  870. {
  871. struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
  872. u32 old_lo, old_hi;
  873. u64 sum_len;
  874. old_lo = serr->ee.ee_info;
  875. old_hi = serr->ee.ee_data;
  876. sum_len = old_hi - old_lo + 1ULL + len;
  877. if (sum_len >= (1ULL << 32))
  878. return false;
  879. if (lo != old_hi + 1)
  880. return false;
  881. serr->ee.ee_data += len;
  882. return true;
  883. }
  884. void sock_zerocopy_callback(struct ubuf_info *uarg, bool success)
  885. {
  886. struct sk_buff *tail, *skb = skb_from_uarg(uarg);
  887. struct sock_exterr_skb *serr;
  888. struct sock *sk = skb->sk;
  889. struct sk_buff_head *q;
  890. unsigned long flags;
  891. u32 lo, hi;
  892. u16 len;
  893. mm_unaccount_pinned_pages(&uarg->mmp);
  894. /* if !len, there was only 1 call, and it was aborted
  895. * so do not queue a completion notification
  896. */
  897. if (!uarg->len || sock_flag(sk, SOCK_DEAD))
  898. goto release;
  899. len = uarg->len;
  900. lo = uarg->id;
  901. hi = uarg->id + len - 1;
  902. serr = SKB_EXT_ERR(skb);
  903. memset(serr, 0, sizeof(*serr));
  904. serr->ee.ee_errno = 0;
  905. serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
  906. serr->ee.ee_data = hi;
  907. serr->ee.ee_info = lo;
  908. if (!success)
  909. serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
  910. q = &sk->sk_error_queue;
  911. spin_lock_irqsave(&q->lock, flags);
  912. tail = skb_peek_tail(q);
  913. if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
  914. !skb_zerocopy_notify_extend(tail, lo, len)) {
  915. __skb_queue_tail(q, skb);
  916. skb = NULL;
  917. }
  918. spin_unlock_irqrestore(&q->lock, flags);
  919. sk->sk_error_report(sk);
  920. release:
  921. consume_skb(skb);
  922. sock_put(sk);
  923. }
  924. EXPORT_SYMBOL_GPL(sock_zerocopy_callback);
  925. void sock_zerocopy_put(struct ubuf_info *uarg)
  926. {
  927. if (uarg && refcount_dec_and_test(&uarg->refcnt)) {
  928. if (uarg->callback)
  929. uarg->callback(uarg, uarg->zerocopy);
  930. else
  931. consume_skb(skb_from_uarg(uarg));
  932. }
  933. }
  934. EXPORT_SYMBOL_GPL(sock_zerocopy_put);
  935. void sock_zerocopy_put_abort(struct ubuf_info *uarg)
  936. {
  937. if (uarg) {
  938. struct sock *sk = skb_from_uarg(uarg)->sk;
  939. atomic_dec(&sk->sk_zckey);
  940. uarg->len--;
  941. sock_zerocopy_put(uarg);
  942. }
  943. }
  944. EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort);
  945. extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb,
  946. struct iov_iter *from, size_t length);
  947. int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
  948. struct msghdr *msg, int len,
  949. struct ubuf_info *uarg)
  950. {
  951. struct ubuf_info *orig_uarg = skb_zcopy(skb);
  952. struct iov_iter orig_iter = msg->msg_iter;
  953. int err, orig_len = skb->len;
  954. /* An skb can only point to one uarg. This edge case happens when
  955. * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
  956. */
  957. if (orig_uarg && uarg != orig_uarg)
  958. return -EEXIST;
  959. err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len);
  960. if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
  961. struct sock *save_sk = skb->sk;
  962. /* Streams do not free skb on error. Reset to prev state. */
  963. msg->msg_iter = orig_iter;
  964. skb->sk = sk;
  965. ___pskb_trim(skb, orig_len);
  966. skb->sk = save_sk;
  967. return err;
  968. }
  969. skb_zcopy_set(skb, uarg);
  970. return skb->len - orig_len;
  971. }
  972. EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
  973. static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
  974. gfp_t gfp_mask)
  975. {
  976. if (skb_zcopy(orig)) {
  977. if (skb_zcopy(nskb)) {
  978. /* !gfp_mask callers are verified to !skb_zcopy(nskb) */
  979. if (!gfp_mask) {
  980. WARN_ON_ONCE(1);
  981. return -ENOMEM;
  982. }
  983. if (skb_uarg(nskb) == skb_uarg(orig))
  984. return 0;
  985. if (skb_copy_ubufs(nskb, GFP_ATOMIC))
  986. return -EIO;
  987. }
  988. skb_zcopy_set(nskb, skb_uarg(orig));
  989. }
  990. return 0;
  991. }
  992. /**
  993. * skb_copy_ubufs - copy userspace skb frags buffers to kernel
  994. * @skb: the skb to modify
  995. * @gfp_mask: allocation priority
  996. *
  997. * This must be called on SKBTX_DEV_ZEROCOPY skb.
  998. * It will copy all frags into kernel and drop the reference
  999. * to userspace pages.
  1000. *
  1001. * If this function is called from an interrupt gfp_mask() must be
  1002. * %GFP_ATOMIC.
  1003. *
  1004. * Returns 0 on success or a negative error code on failure
  1005. * to allocate kernel memory to copy to.
  1006. */
  1007. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  1008. {
  1009. int num_frags = skb_shinfo(skb)->nr_frags;
  1010. struct page *page, *head = NULL;
  1011. int i, new_frags;
  1012. u32 d_off;
  1013. if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
  1014. return -EINVAL;
  1015. if (!num_frags)
  1016. goto release;
  1017. new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1018. for (i = 0; i < new_frags; i++) {
  1019. page = alloc_page(gfp_mask);
  1020. if (!page) {
  1021. while (head) {
  1022. struct page *next = (struct page *)page_private(head);
  1023. put_page(head);
  1024. head = next;
  1025. }
  1026. return -ENOMEM;
  1027. }
  1028. set_page_private(page, (unsigned long)head);
  1029. head = page;
  1030. }
  1031. page = head;
  1032. d_off = 0;
  1033. for (i = 0; i < num_frags; i++) {
  1034. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1035. u32 p_off, p_len, copied;
  1036. struct page *p;
  1037. u8 *vaddr;
  1038. skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f),
  1039. p, p_off, p_len, copied) {
  1040. u32 copy, done = 0;
  1041. vaddr = kmap_atomic(p);
  1042. while (done < p_len) {
  1043. if (d_off == PAGE_SIZE) {
  1044. d_off = 0;
  1045. page = (struct page *)page_private(page);
  1046. }
  1047. copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
  1048. memcpy(page_address(page) + d_off,
  1049. vaddr + p_off + done, copy);
  1050. done += copy;
  1051. d_off += copy;
  1052. }
  1053. kunmap_atomic(vaddr);
  1054. }
  1055. }
  1056. /* skb frags release userspace buffers */
  1057. for (i = 0; i < num_frags; i++)
  1058. skb_frag_unref(skb, i);
  1059. /* skb frags point to kernel buffers */
  1060. for (i = 0; i < new_frags - 1; i++) {
  1061. __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
  1062. head = (struct page *)page_private(head);
  1063. }
  1064. __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
  1065. skb_shinfo(skb)->nr_frags = new_frags;
  1066. release:
  1067. skb_zcopy_clear(skb, false);
  1068. return 0;
  1069. }
  1070. EXPORT_SYMBOL_GPL(skb_copy_ubufs);
  1071. /**
  1072. * skb_clone - duplicate an sk_buff
  1073. * @skb: buffer to clone
  1074. * @gfp_mask: allocation priority
  1075. *
  1076. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  1077. * copies share the same packet data but not structure. The new
  1078. * buffer has a reference count of 1. If the allocation fails the
  1079. * function returns %NULL otherwise the new buffer is returned.
  1080. *
  1081. * If this function is called from an interrupt gfp_mask() must be
  1082. * %GFP_ATOMIC.
  1083. */
  1084. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  1085. {
  1086. struct sk_buff_fclones *fclones = container_of(skb,
  1087. struct sk_buff_fclones,
  1088. skb1);
  1089. struct sk_buff *n;
  1090. if (skb_orphan_frags(skb, gfp_mask))
  1091. return NULL;
  1092. if (skb->fclone == SKB_FCLONE_ORIG &&
  1093. refcount_read(&fclones->fclone_ref) == 1) {
  1094. n = &fclones->skb2;
  1095. refcount_set(&fclones->fclone_ref, 2);
  1096. } else {
  1097. if (skb_pfmemalloc(skb))
  1098. gfp_mask |= __GFP_MEMALLOC;
  1099. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  1100. if (!n)
  1101. return NULL;
  1102. n->fclone = SKB_FCLONE_UNAVAILABLE;
  1103. }
  1104. return __skb_clone(n, skb);
  1105. }
  1106. EXPORT_SYMBOL(skb_clone);
  1107. void skb_headers_offset_update(struct sk_buff *skb, int off)
  1108. {
  1109. /* Only adjust this if it actually is csum_start rather than csum */
  1110. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1111. skb->csum_start += off;
  1112. /* {transport,network,mac}_header and tail are relative to skb->head */
  1113. skb->transport_header += off;
  1114. skb->network_header += off;
  1115. if (skb_mac_header_was_set(skb))
  1116. skb->mac_header += off;
  1117. skb->inner_transport_header += off;
  1118. skb->inner_network_header += off;
  1119. skb->inner_mac_header += off;
  1120. }
  1121. EXPORT_SYMBOL(skb_headers_offset_update);
  1122. void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
  1123. {
  1124. __copy_skb_header(new, old);
  1125. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  1126. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  1127. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  1128. }
  1129. EXPORT_SYMBOL(skb_copy_header);
  1130. static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
  1131. {
  1132. if (skb_pfmemalloc(skb))
  1133. return SKB_ALLOC_RX;
  1134. return 0;
  1135. }
  1136. /**
  1137. * skb_copy - create private copy of an sk_buff
  1138. * @skb: buffer to copy
  1139. * @gfp_mask: allocation priority
  1140. *
  1141. * Make a copy of both an &sk_buff and its data. This is used when the
  1142. * caller wishes to modify the data and needs a private copy of the
  1143. * data to alter. Returns %NULL on failure or the pointer to the buffer
  1144. * on success. The returned buffer has a reference count of 1.
  1145. *
  1146. * As by-product this function converts non-linear &sk_buff to linear
  1147. * one, so that &sk_buff becomes completely private and caller is allowed
  1148. * to modify all the data of returned buffer. This means that this
  1149. * function is not recommended for use in circumstances when only
  1150. * header is going to be modified. Use pskb_copy() instead.
  1151. */
  1152. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  1153. {
  1154. int headerlen = skb_headroom(skb);
  1155. unsigned int size = skb_end_offset(skb) + skb->data_len;
  1156. struct sk_buff *n = __alloc_skb(size, gfp_mask,
  1157. skb_alloc_rx_flag(skb), NUMA_NO_NODE);
  1158. if (!n)
  1159. return NULL;
  1160. /* Set the data pointer */
  1161. skb_reserve(n, headerlen);
  1162. /* Set the tail pointer and length */
  1163. skb_put(n, skb->len);
  1164. BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
  1165. skb_copy_header(n, skb);
  1166. return n;
  1167. }
  1168. EXPORT_SYMBOL(skb_copy);
  1169. /**
  1170. * __pskb_copy_fclone - create copy of an sk_buff with private head.
  1171. * @skb: buffer to copy
  1172. * @headroom: headroom of new skb
  1173. * @gfp_mask: allocation priority
  1174. * @fclone: if true allocate the copy of the skb from the fclone
  1175. * cache instead of the head cache; it is recommended to set this
  1176. * to true for the cases where the copy will likely be cloned
  1177. *
  1178. * Make a copy of both an &sk_buff and part of its data, located
  1179. * in header. Fragmented data remain shared. This is used when
  1180. * the caller wishes to modify only header of &sk_buff and needs
  1181. * private copy of the header to alter. Returns %NULL on failure
  1182. * or the pointer to the buffer on success.
  1183. * The returned buffer has a reference count of 1.
  1184. */
  1185. struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
  1186. gfp_t gfp_mask, bool fclone)
  1187. {
  1188. unsigned int size = skb_headlen(skb) + headroom;
  1189. int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
  1190. struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
  1191. if (!n)
  1192. goto out;
  1193. /* Set the data pointer */
  1194. skb_reserve(n, headroom);
  1195. /* Set the tail pointer and length */
  1196. skb_put(n, skb_headlen(skb));
  1197. /* Copy the bytes */
  1198. skb_copy_from_linear_data(skb, n->data, n->len);
  1199. n->truesize += skb->data_len;
  1200. n->data_len = skb->data_len;
  1201. n->len = skb->len;
  1202. if (skb_shinfo(skb)->nr_frags) {
  1203. int i;
  1204. if (skb_orphan_frags(skb, gfp_mask) ||
  1205. skb_zerocopy_clone(n, skb, gfp_mask)) {
  1206. kfree_skb(n);
  1207. n = NULL;
  1208. goto out;
  1209. }
  1210. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1211. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  1212. skb_frag_ref(skb, i);
  1213. }
  1214. skb_shinfo(n)->nr_frags = i;
  1215. }
  1216. if (skb_has_frag_list(skb)) {
  1217. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  1218. skb_clone_fraglist(n);
  1219. }
  1220. skb_copy_header(n, skb);
  1221. out:
  1222. return n;
  1223. }
  1224. EXPORT_SYMBOL(__pskb_copy_fclone);
  1225. /**
  1226. * pskb_expand_head - reallocate header of &sk_buff
  1227. * @skb: buffer to reallocate
  1228. * @nhead: room to add at head
  1229. * @ntail: room to add at tail
  1230. * @gfp_mask: allocation priority
  1231. *
  1232. * Expands (or creates identical copy, if @nhead and @ntail are zero)
  1233. * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
  1234. * reference count of 1. Returns zero in the case of success or error,
  1235. * if expansion failed. In the last case, &sk_buff is not changed.
  1236. *
  1237. * All the pointers pointing into skb header may change and must be
  1238. * reloaded after call to this function.
  1239. */
  1240. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  1241. gfp_t gfp_mask)
  1242. {
  1243. int i, osize = skb_end_offset(skb);
  1244. int size = osize + nhead + ntail;
  1245. long off;
  1246. u8 *data;
  1247. BUG_ON(nhead < 0);
  1248. BUG_ON(skb_shared(skb));
  1249. size = SKB_DATA_ALIGN(size);
  1250. if (skb_pfmemalloc(skb))
  1251. gfp_mask |= __GFP_MEMALLOC;
  1252. data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  1253. gfp_mask, NUMA_NO_NODE, NULL);
  1254. if (!data)
  1255. goto nodata;
  1256. size = SKB_WITH_OVERHEAD(ksize(data));
  1257. /* Copy only real data... and, alas, header. This should be
  1258. * optimized for the cases when header is void.
  1259. */
  1260. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  1261. memcpy((struct skb_shared_info *)(data + size),
  1262. skb_shinfo(skb),
  1263. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  1264. /*
  1265. * if shinfo is shared we must drop the old head gracefully, but if it
  1266. * is not we can just drop the old head and let the existing refcount
  1267. * be since all we did is relocate the values
  1268. */
  1269. if (skb_cloned(skb)) {
  1270. if (skb_orphan_frags(skb, gfp_mask))
  1271. goto nofrags;
  1272. if (skb_zcopy(skb))
  1273. refcount_inc(&skb_uarg(skb)->refcnt);
  1274. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1275. skb_frag_ref(skb, i);
  1276. if (skb_has_frag_list(skb))
  1277. skb_clone_fraglist(skb);
  1278. skb_release_data(skb);
  1279. } else {
  1280. skb_free_head(skb);
  1281. }
  1282. off = (data + nhead) - skb->head;
  1283. skb->head = data;
  1284. skb->head_frag = 0;
  1285. skb->data += off;
  1286. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1287. skb->end = size;
  1288. off = nhead;
  1289. #else
  1290. skb->end = skb->head + size;
  1291. #endif
  1292. skb->tail += off;
  1293. skb_headers_offset_update(skb, nhead);
  1294. skb->cloned = 0;
  1295. skb->hdr_len = 0;
  1296. skb->nohdr = 0;
  1297. atomic_set(&skb_shinfo(skb)->dataref, 1);
  1298. skb_metadata_clear(skb);
  1299. /* It is not generally safe to change skb->truesize.
  1300. * For the moment, we really care of rx path, or
  1301. * when skb is orphaned (not attached to a socket).
  1302. */
  1303. if (!skb->sk || skb->destructor == sock_edemux)
  1304. skb->truesize += size - osize;
  1305. return 0;
  1306. nofrags:
  1307. kfree(data);
  1308. nodata:
  1309. return -ENOMEM;
  1310. }
  1311. EXPORT_SYMBOL(pskb_expand_head);
  1312. /* Make private copy of skb with writable head and some headroom */
  1313. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  1314. {
  1315. struct sk_buff *skb2;
  1316. int delta = headroom - skb_headroom(skb);
  1317. if (delta <= 0)
  1318. skb2 = pskb_copy(skb, GFP_ATOMIC);
  1319. else {
  1320. skb2 = skb_clone(skb, GFP_ATOMIC);
  1321. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  1322. GFP_ATOMIC)) {
  1323. kfree_skb(skb2);
  1324. skb2 = NULL;
  1325. }
  1326. }
  1327. return skb2;
  1328. }
  1329. EXPORT_SYMBOL(skb_realloc_headroom);
  1330. /**
  1331. * skb_copy_expand - copy and expand sk_buff
  1332. * @skb: buffer to copy
  1333. * @newheadroom: new free bytes at head
  1334. * @newtailroom: new free bytes at tail
  1335. * @gfp_mask: allocation priority
  1336. *
  1337. * Make a copy of both an &sk_buff and its data and while doing so
  1338. * allocate additional space.
  1339. *
  1340. * This is used when the caller wishes to modify the data and needs a
  1341. * private copy of the data to alter as well as more space for new fields.
  1342. * Returns %NULL on failure or the pointer to the buffer
  1343. * on success. The returned buffer has a reference count of 1.
  1344. *
  1345. * You must pass %GFP_ATOMIC as the allocation priority if this function
  1346. * is called from an interrupt.
  1347. */
  1348. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  1349. int newheadroom, int newtailroom,
  1350. gfp_t gfp_mask)
  1351. {
  1352. /*
  1353. * Allocate the copy buffer
  1354. */
  1355. struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
  1356. gfp_mask, skb_alloc_rx_flag(skb),
  1357. NUMA_NO_NODE);
  1358. int oldheadroom = skb_headroom(skb);
  1359. int head_copy_len, head_copy_off;
  1360. if (!n)
  1361. return NULL;
  1362. skb_reserve(n, newheadroom);
  1363. /* Set the tail pointer and length */
  1364. skb_put(n, skb->len);
  1365. head_copy_len = oldheadroom;
  1366. head_copy_off = 0;
  1367. if (newheadroom <= head_copy_len)
  1368. head_copy_len = newheadroom;
  1369. else
  1370. head_copy_off = newheadroom - head_copy_len;
  1371. /* Copy the linear header and data. */
  1372. BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  1373. skb->len + head_copy_len));
  1374. skb_copy_header(n, skb);
  1375. skb_headers_offset_update(n, newheadroom - oldheadroom);
  1376. return n;
  1377. }
  1378. EXPORT_SYMBOL(skb_copy_expand);
  1379. /**
  1380. * __skb_pad - zero pad the tail of an skb
  1381. * @skb: buffer to pad
  1382. * @pad: space to pad
  1383. * @free_on_error: free buffer on error
  1384. *
  1385. * Ensure that a buffer is followed by a padding area that is zero
  1386. * filled. Used by network drivers which may DMA or transfer data
  1387. * beyond the buffer end onto the wire.
  1388. *
  1389. * May return error in out of memory cases. The skb is freed on error
  1390. * if @free_on_error is true.
  1391. */
  1392. int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
  1393. {
  1394. int err;
  1395. int ntail;
  1396. /* If the skbuff is non linear tailroom is always zero.. */
  1397. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  1398. memset(skb->data+skb->len, 0, pad);
  1399. return 0;
  1400. }
  1401. ntail = skb->data_len + pad - (skb->end - skb->tail);
  1402. if (likely(skb_cloned(skb) || ntail > 0)) {
  1403. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  1404. if (unlikely(err))
  1405. goto free_skb;
  1406. }
  1407. /* FIXME: The use of this function with non-linear skb's really needs
  1408. * to be audited.
  1409. */
  1410. err = skb_linearize(skb);
  1411. if (unlikely(err))
  1412. goto free_skb;
  1413. memset(skb->data + skb->len, 0, pad);
  1414. return 0;
  1415. free_skb:
  1416. if (free_on_error)
  1417. kfree_skb(skb);
  1418. return err;
  1419. }
  1420. EXPORT_SYMBOL(__skb_pad);
  1421. /**
  1422. * pskb_put - add data to the tail of a potentially fragmented buffer
  1423. * @skb: start of the buffer to use
  1424. * @tail: tail fragment of the buffer to use
  1425. * @len: amount of data to add
  1426. *
  1427. * This function extends the used data area of the potentially
  1428. * fragmented buffer. @tail must be the last fragment of @skb -- or
  1429. * @skb itself. If this would exceed the total buffer size the kernel
  1430. * will panic. A pointer to the first byte of the extra data is
  1431. * returned.
  1432. */
  1433. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
  1434. {
  1435. if (tail != skb) {
  1436. skb->data_len += len;
  1437. skb->len += len;
  1438. }
  1439. return skb_put(tail, len);
  1440. }
  1441. EXPORT_SYMBOL_GPL(pskb_put);
  1442. /**
  1443. * skb_put - add data to a buffer
  1444. * @skb: buffer to use
  1445. * @len: amount of data to add
  1446. *
  1447. * This function extends the used data area of the buffer. If this would
  1448. * exceed the total buffer size the kernel will panic. A pointer to the
  1449. * first byte of the extra data is returned.
  1450. */
  1451. void *skb_put(struct sk_buff *skb, unsigned int len)
  1452. {
  1453. void *tmp = skb_tail_pointer(skb);
  1454. SKB_LINEAR_ASSERT(skb);
  1455. skb->tail += len;
  1456. skb->len += len;
  1457. if (unlikely(skb->tail > skb->end))
  1458. skb_over_panic(skb, len, __builtin_return_address(0));
  1459. return tmp;
  1460. }
  1461. EXPORT_SYMBOL(skb_put);
  1462. /**
  1463. * skb_push - add data to the start of a buffer
  1464. * @skb: buffer to use
  1465. * @len: amount of data to add
  1466. *
  1467. * This function extends the used data area of the buffer at the buffer
  1468. * start. If this would exceed the total buffer headroom the kernel will
  1469. * panic. A pointer to the first byte of the extra data is returned.
  1470. */
  1471. void *skb_push(struct sk_buff *skb, unsigned int len)
  1472. {
  1473. skb->data -= len;
  1474. skb->len += len;
  1475. if (unlikely(skb->data < skb->head))
  1476. skb_under_panic(skb, len, __builtin_return_address(0));
  1477. return skb->data;
  1478. }
  1479. EXPORT_SYMBOL(skb_push);
  1480. /**
  1481. * skb_pull - remove data from the start of a buffer
  1482. * @skb: buffer to use
  1483. * @len: amount of data to remove
  1484. *
  1485. * This function removes data from the start of a buffer, returning
  1486. * the memory to the headroom. A pointer to the next data in the buffer
  1487. * is returned. Once the data has been pulled future pushes will overwrite
  1488. * the old data.
  1489. */
  1490. void *skb_pull(struct sk_buff *skb, unsigned int len)
  1491. {
  1492. return skb_pull_inline(skb, len);
  1493. }
  1494. EXPORT_SYMBOL(skb_pull);
  1495. /**
  1496. * skb_trim - remove end from a buffer
  1497. * @skb: buffer to alter
  1498. * @len: new length
  1499. *
  1500. * Cut the length of a buffer down by removing data from the tail. If
  1501. * the buffer is already under the length specified it is not modified.
  1502. * The skb must be linear.
  1503. */
  1504. void skb_trim(struct sk_buff *skb, unsigned int len)
  1505. {
  1506. if (skb->len > len)
  1507. __skb_trim(skb, len);
  1508. }
  1509. EXPORT_SYMBOL(skb_trim);
  1510. /* Trims skb to length len. It can change skb pointers.
  1511. */
  1512. int ___pskb_trim(struct sk_buff *skb, unsigned int len)
  1513. {
  1514. struct sk_buff **fragp;
  1515. struct sk_buff *frag;
  1516. int offset = skb_headlen(skb);
  1517. int nfrags = skb_shinfo(skb)->nr_frags;
  1518. int i;
  1519. int err;
  1520. if (skb_cloned(skb) &&
  1521. unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
  1522. return err;
  1523. i = 0;
  1524. if (offset >= len)
  1525. goto drop_pages;
  1526. for (; i < nfrags; i++) {
  1527. int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1528. if (end < len) {
  1529. offset = end;
  1530. continue;
  1531. }
  1532. skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
  1533. drop_pages:
  1534. skb_shinfo(skb)->nr_frags = i;
  1535. for (; i < nfrags; i++)
  1536. skb_frag_unref(skb, i);
  1537. if (skb_has_frag_list(skb))
  1538. skb_drop_fraglist(skb);
  1539. goto done;
  1540. }
  1541. for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
  1542. fragp = &frag->next) {
  1543. int end = offset + frag->len;
  1544. if (skb_shared(frag)) {
  1545. struct sk_buff *nfrag;
  1546. nfrag = skb_clone(frag, GFP_ATOMIC);
  1547. if (unlikely(!nfrag))
  1548. return -ENOMEM;
  1549. nfrag->next = frag->next;
  1550. consume_skb(frag);
  1551. frag = nfrag;
  1552. *fragp = frag;
  1553. }
  1554. if (end < len) {
  1555. offset = end;
  1556. continue;
  1557. }
  1558. if (end > len &&
  1559. unlikely((err = pskb_trim(frag, len - offset))))
  1560. return err;
  1561. if (frag->next)
  1562. skb_drop_list(&frag->next);
  1563. break;
  1564. }
  1565. done:
  1566. if (len > skb_headlen(skb)) {
  1567. skb->data_len -= skb->len - len;
  1568. skb->len = len;
  1569. } else {
  1570. skb->len = len;
  1571. skb->data_len = 0;
  1572. skb_set_tail_pointer(skb, len);
  1573. }
  1574. if (!skb->sk || skb->destructor == sock_edemux)
  1575. skb_condense(skb);
  1576. return 0;
  1577. }
  1578. EXPORT_SYMBOL(___pskb_trim);
  1579. /* Note : use pskb_trim_rcsum() instead of calling this directly
  1580. */
  1581. int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
  1582. {
  1583. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1584. int delta = skb->len - len;
  1585. skb->csum = csum_block_sub(skb->csum,
  1586. skb_checksum(skb, len, delta, 0),
  1587. len);
  1588. }
  1589. return __pskb_trim(skb, len);
  1590. }
  1591. EXPORT_SYMBOL(pskb_trim_rcsum_slow);
  1592. /**
  1593. * __pskb_pull_tail - advance tail of skb header
  1594. * @skb: buffer to reallocate
  1595. * @delta: number of bytes to advance tail
  1596. *
  1597. * The function makes a sense only on a fragmented &sk_buff,
  1598. * it expands header moving its tail forward and copying necessary
  1599. * data from fragmented part.
  1600. *
  1601. * &sk_buff MUST have reference count of 1.
  1602. *
  1603. * Returns %NULL (and &sk_buff does not change) if pull failed
  1604. * or value of new tail of skb in the case of success.
  1605. *
  1606. * All the pointers pointing into skb header may change and must be
  1607. * reloaded after call to this function.
  1608. */
  1609. /* Moves tail of skb head forward, copying data from fragmented part,
  1610. * when it is necessary.
  1611. * 1. It may fail due to malloc failure.
  1612. * 2. It may change skb pointers.
  1613. *
  1614. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1615. */
  1616. void *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1617. {
  1618. /* If skb has not enough free space at tail, get new one
  1619. * plus 128 bytes for future expansions. If we have enough
  1620. * room at tail, reallocate without expansion only if skb is cloned.
  1621. */
  1622. int i, k, eat = (skb->tail + delta) - skb->end;
  1623. if (eat > 0 || skb_cloned(skb)) {
  1624. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1625. GFP_ATOMIC))
  1626. return NULL;
  1627. }
  1628. BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
  1629. skb_tail_pointer(skb), delta));
  1630. /* Optimization: no fragments, no reasons to preestimate
  1631. * size of pulled pages. Superb.
  1632. */
  1633. if (!skb_has_frag_list(skb))
  1634. goto pull_pages;
  1635. /* Estimate size of pulled pages. */
  1636. eat = delta;
  1637. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1638. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1639. if (size >= eat)
  1640. goto pull_pages;
  1641. eat -= size;
  1642. }
  1643. /* If we need update frag list, we are in troubles.
  1644. * Certainly, it is possible to add an offset to skb data,
  1645. * but taking into account that pulling is expected to
  1646. * be very rare operation, it is worth to fight against
  1647. * further bloating skb head and crucify ourselves here instead.
  1648. * Pure masohism, indeed. 8)8)
  1649. */
  1650. if (eat) {
  1651. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1652. struct sk_buff *clone = NULL;
  1653. struct sk_buff *insp = NULL;
  1654. do {
  1655. BUG_ON(!list);
  1656. if (list->len <= eat) {
  1657. /* Eaten as whole. */
  1658. eat -= list->len;
  1659. list = list->next;
  1660. insp = list;
  1661. } else {
  1662. /* Eaten partially. */
  1663. if (skb_shared(list)) {
  1664. /* Sucks! We need to fork list. :-( */
  1665. clone = skb_clone(list, GFP_ATOMIC);
  1666. if (!clone)
  1667. return NULL;
  1668. insp = list->next;
  1669. list = clone;
  1670. } else {
  1671. /* This may be pulled without
  1672. * problems. */
  1673. insp = list;
  1674. }
  1675. if (!pskb_pull(list, eat)) {
  1676. kfree_skb(clone);
  1677. return NULL;
  1678. }
  1679. break;
  1680. }
  1681. } while (eat);
  1682. /* Free pulled out fragments. */
  1683. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1684. skb_shinfo(skb)->frag_list = list->next;
  1685. kfree_skb(list);
  1686. }
  1687. /* And insert new clone at head. */
  1688. if (clone) {
  1689. clone->next = list;
  1690. skb_shinfo(skb)->frag_list = clone;
  1691. }
  1692. }
  1693. /* Success! Now we may commit changes to skb data. */
  1694. pull_pages:
  1695. eat = delta;
  1696. k = 0;
  1697. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1698. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1699. if (size <= eat) {
  1700. skb_frag_unref(skb, i);
  1701. eat -= size;
  1702. } else {
  1703. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1704. if (eat) {
  1705. skb_shinfo(skb)->frags[k].page_offset += eat;
  1706. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  1707. if (!i)
  1708. goto end;
  1709. eat = 0;
  1710. }
  1711. k++;
  1712. }
  1713. }
  1714. skb_shinfo(skb)->nr_frags = k;
  1715. end:
  1716. skb->tail += delta;
  1717. skb->data_len -= delta;
  1718. if (!skb->data_len)
  1719. skb_zcopy_clear(skb, false);
  1720. return skb_tail_pointer(skb);
  1721. }
  1722. EXPORT_SYMBOL(__pskb_pull_tail);
  1723. /**
  1724. * skb_copy_bits - copy bits from skb to kernel buffer
  1725. * @skb: source skb
  1726. * @offset: offset in source
  1727. * @to: destination buffer
  1728. * @len: number of bytes to copy
  1729. *
  1730. * Copy the specified number of bytes from the source skb to the
  1731. * destination buffer.
  1732. *
  1733. * CAUTION ! :
  1734. * If its prototype is ever changed,
  1735. * check arch/{*}/net/{*}.S files,
  1736. * since it is called from BPF assembly code.
  1737. */
  1738. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1739. {
  1740. int start = skb_headlen(skb);
  1741. struct sk_buff *frag_iter;
  1742. int i, copy;
  1743. if (offset > (int)skb->len - len)
  1744. goto fault;
  1745. /* Copy header. */
  1746. if ((copy = start - offset) > 0) {
  1747. if (copy > len)
  1748. copy = len;
  1749. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1750. if ((len -= copy) == 0)
  1751. return 0;
  1752. offset += copy;
  1753. to += copy;
  1754. }
  1755. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1756. int end;
  1757. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1758. WARN_ON(start > offset + len);
  1759. end = start + skb_frag_size(f);
  1760. if ((copy = end - offset) > 0) {
  1761. u32 p_off, p_len, copied;
  1762. struct page *p;
  1763. u8 *vaddr;
  1764. if (copy > len)
  1765. copy = len;
  1766. skb_frag_foreach_page(f,
  1767. f->page_offset + offset - start,
  1768. copy, p, p_off, p_len, copied) {
  1769. vaddr = kmap_atomic(p);
  1770. memcpy(to + copied, vaddr + p_off, p_len);
  1771. kunmap_atomic(vaddr);
  1772. }
  1773. if ((len -= copy) == 0)
  1774. return 0;
  1775. offset += copy;
  1776. to += copy;
  1777. }
  1778. start = end;
  1779. }
  1780. skb_walk_frags(skb, frag_iter) {
  1781. int end;
  1782. WARN_ON(start > offset + len);
  1783. end = start + frag_iter->len;
  1784. if ((copy = end - offset) > 0) {
  1785. if (copy > len)
  1786. copy = len;
  1787. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1788. goto fault;
  1789. if ((len -= copy) == 0)
  1790. return 0;
  1791. offset += copy;
  1792. to += copy;
  1793. }
  1794. start = end;
  1795. }
  1796. if (!len)
  1797. return 0;
  1798. fault:
  1799. return -EFAULT;
  1800. }
  1801. EXPORT_SYMBOL(skb_copy_bits);
  1802. /*
  1803. * Callback from splice_to_pipe(), if we need to release some pages
  1804. * at the end of the spd in case we error'ed out in filling the pipe.
  1805. */
  1806. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1807. {
  1808. put_page(spd->pages[i]);
  1809. }
  1810. static struct page *linear_to_page(struct page *page, unsigned int *len,
  1811. unsigned int *offset,
  1812. struct sock *sk)
  1813. {
  1814. struct page_frag *pfrag = sk_page_frag(sk);
  1815. if (!sk_page_frag_refill(sk, pfrag))
  1816. return NULL;
  1817. *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
  1818. memcpy(page_address(pfrag->page) + pfrag->offset,
  1819. page_address(page) + *offset, *len);
  1820. *offset = pfrag->offset;
  1821. pfrag->offset += *len;
  1822. return pfrag->page;
  1823. }
  1824. static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
  1825. struct page *page,
  1826. unsigned int offset)
  1827. {
  1828. return spd->nr_pages &&
  1829. spd->pages[spd->nr_pages - 1] == page &&
  1830. (spd->partial[spd->nr_pages - 1].offset +
  1831. spd->partial[spd->nr_pages - 1].len == offset);
  1832. }
  1833. /*
  1834. * Fill page/offset/length into spd, if it can hold more pages.
  1835. */
  1836. static bool spd_fill_page(struct splice_pipe_desc *spd,
  1837. struct pipe_inode_info *pipe, struct page *page,
  1838. unsigned int *len, unsigned int offset,
  1839. bool linear,
  1840. struct sock *sk)
  1841. {
  1842. if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
  1843. return true;
  1844. if (linear) {
  1845. page = linear_to_page(page, len, &offset, sk);
  1846. if (!page)
  1847. return true;
  1848. }
  1849. if (spd_can_coalesce(spd, page, offset)) {
  1850. spd->partial[spd->nr_pages - 1].len += *len;
  1851. return false;
  1852. }
  1853. get_page(page);
  1854. spd->pages[spd->nr_pages] = page;
  1855. spd->partial[spd->nr_pages].len = *len;
  1856. spd->partial[spd->nr_pages].offset = offset;
  1857. spd->nr_pages++;
  1858. return false;
  1859. }
  1860. static bool __splice_segment(struct page *page, unsigned int poff,
  1861. unsigned int plen, unsigned int *off,
  1862. unsigned int *len,
  1863. struct splice_pipe_desc *spd, bool linear,
  1864. struct sock *sk,
  1865. struct pipe_inode_info *pipe)
  1866. {
  1867. if (!*len)
  1868. return true;
  1869. /* skip this segment if already processed */
  1870. if (*off >= plen) {
  1871. *off -= plen;
  1872. return false;
  1873. }
  1874. /* ignore any bits we already processed */
  1875. poff += *off;
  1876. plen -= *off;
  1877. *off = 0;
  1878. do {
  1879. unsigned int flen = min(*len, plen);
  1880. if (spd_fill_page(spd, pipe, page, &flen, poff,
  1881. linear, sk))
  1882. return true;
  1883. poff += flen;
  1884. plen -= flen;
  1885. *len -= flen;
  1886. } while (*len && plen);
  1887. return false;
  1888. }
  1889. /*
  1890. * Map linear and fragment data from the skb to spd. It reports true if the
  1891. * pipe is full or if we already spliced the requested length.
  1892. */
  1893. static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1894. unsigned int *offset, unsigned int *len,
  1895. struct splice_pipe_desc *spd, struct sock *sk)
  1896. {
  1897. int seg;
  1898. struct sk_buff *iter;
  1899. /* map the linear part :
  1900. * If skb->head_frag is set, this 'linear' part is backed by a
  1901. * fragment, and if the head is not shared with any clones then
  1902. * we can avoid a copy since we own the head portion of this page.
  1903. */
  1904. if (__splice_segment(virt_to_page(skb->data),
  1905. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1906. skb_headlen(skb),
  1907. offset, len, spd,
  1908. skb_head_is_locked(skb),
  1909. sk, pipe))
  1910. return true;
  1911. /*
  1912. * then map the fragments
  1913. */
  1914. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1915. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1916. if (__splice_segment(skb_frag_page(f),
  1917. f->page_offset, skb_frag_size(f),
  1918. offset, len, spd, false, sk, pipe))
  1919. return true;
  1920. }
  1921. skb_walk_frags(skb, iter) {
  1922. if (*offset >= iter->len) {
  1923. *offset -= iter->len;
  1924. continue;
  1925. }
  1926. /* __skb_splice_bits() only fails if the output has no room
  1927. * left, so no point in going over the frag_list for the error
  1928. * case.
  1929. */
  1930. if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
  1931. return true;
  1932. }
  1933. return false;
  1934. }
  1935. /*
  1936. * Map data from the skb to a pipe. Should handle both the linear part,
  1937. * the fragments, and the frag list.
  1938. */
  1939. int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
  1940. struct pipe_inode_info *pipe, unsigned int tlen,
  1941. unsigned int flags)
  1942. {
  1943. struct partial_page partial[MAX_SKB_FRAGS];
  1944. struct page *pages[MAX_SKB_FRAGS];
  1945. struct splice_pipe_desc spd = {
  1946. .pages = pages,
  1947. .partial = partial,
  1948. .nr_pages_max = MAX_SKB_FRAGS,
  1949. .ops = &nosteal_pipe_buf_ops,
  1950. .spd_release = sock_spd_release,
  1951. };
  1952. int ret = 0;
  1953. __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
  1954. if (spd.nr_pages)
  1955. ret = splice_to_pipe(pipe, &spd);
  1956. return ret;
  1957. }
  1958. EXPORT_SYMBOL_GPL(skb_splice_bits);
  1959. /* Send skb data on a socket. Socket must be locked. */
  1960. int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
  1961. int len)
  1962. {
  1963. unsigned int orig_len = len;
  1964. struct sk_buff *head = skb;
  1965. unsigned short fragidx;
  1966. int slen, ret;
  1967. do_frag_list:
  1968. /* Deal with head data */
  1969. while (offset < skb_headlen(skb) && len) {
  1970. struct kvec kv;
  1971. struct msghdr msg;
  1972. slen = min_t(int, len, skb_headlen(skb) - offset);
  1973. kv.iov_base = skb->data + offset;
  1974. kv.iov_len = slen;
  1975. memset(&msg, 0, sizeof(msg));
  1976. ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
  1977. if (ret <= 0)
  1978. goto error;
  1979. offset += ret;
  1980. len -= ret;
  1981. }
  1982. /* All the data was skb head? */
  1983. if (!len)
  1984. goto out;
  1985. /* Make offset relative to start of frags */
  1986. offset -= skb_headlen(skb);
  1987. /* Find where we are in frag list */
  1988. for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  1989. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  1990. if (offset < frag->size)
  1991. break;
  1992. offset -= frag->size;
  1993. }
  1994. for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  1995. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  1996. slen = min_t(size_t, len, frag->size - offset);
  1997. while (slen) {
  1998. ret = kernel_sendpage_locked(sk, frag->page.p,
  1999. frag->page_offset + offset,
  2000. slen, MSG_DONTWAIT);
  2001. if (ret <= 0)
  2002. goto error;
  2003. len -= ret;
  2004. offset += ret;
  2005. slen -= ret;
  2006. }
  2007. offset = 0;
  2008. }
  2009. if (len) {
  2010. /* Process any frag lists */
  2011. if (skb == head) {
  2012. if (skb_has_frag_list(skb)) {
  2013. skb = skb_shinfo(skb)->frag_list;
  2014. goto do_frag_list;
  2015. }
  2016. } else if (skb->next) {
  2017. skb = skb->next;
  2018. goto do_frag_list;
  2019. }
  2020. }
  2021. out:
  2022. return orig_len - len;
  2023. error:
  2024. return orig_len == len ? ret : orig_len - len;
  2025. }
  2026. EXPORT_SYMBOL_GPL(skb_send_sock_locked);
  2027. /* Send skb data on a socket. */
  2028. int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
  2029. {
  2030. int ret = 0;
  2031. lock_sock(sk);
  2032. ret = skb_send_sock_locked(sk, skb, offset, len);
  2033. release_sock(sk);
  2034. return ret;
  2035. }
  2036. EXPORT_SYMBOL_GPL(skb_send_sock);
  2037. /**
  2038. * skb_store_bits - store bits from kernel buffer to skb
  2039. * @skb: destination buffer
  2040. * @offset: offset in destination
  2041. * @from: source buffer
  2042. * @len: number of bytes to copy
  2043. *
  2044. * Copy the specified number of bytes from the source buffer to the
  2045. * destination skb. This function handles all the messy bits of
  2046. * traversing fragment lists and such.
  2047. */
  2048. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  2049. {
  2050. int start = skb_headlen(skb);
  2051. struct sk_buff *frag_iter;
  2052. int i, copy;
  2053. if (offset > (int)skb->len - len)
  2054. goto fault;
  2055. if ((copy = start - offset) > 0) {
  2056. if (copy > len)
  2057. copy = len;
  2058. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  2059. if ((len -= copy) == 0)
  2060. return 0;
  2061. offset += copy;
  2062. from += copy;
  2063. }
  2064. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2065. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2066. int end;
  2067. WARN_ON(start > offset + len);
  2068. end = start + skb_frag_size(frag);
  2069. if ((copy = end - offset) > 0) {
  2070. u32 p_off, p_len, copied;
  2071. struct page *p;
  2072. u8 *vaddr;
  2073. if (copy > len)
  2074. copy = len;
  2075. skb_frag_foreach_page(frag,
  2076. frag->page_offset + offset - start,
  2077. copy, p, p_off, p_len, copied) {
  2078. vaddr = kmap_atomic(p);
  2079. memcpy(vaddr + p_off, from + copied, p_len);
  2080. kunmap_atomic(vaddr);
  2081. }
  2082. if ((len -= copy) == 0)
  2083. return 0;
  2084. offset += copy;
  2085. from += copy;
  2086. }
  2087. start = end;
  2088. }
  2089. skb_walk_frags(skb, frag_iter) {
  2090. int end;
  2091. WARN_ON(start > offset + len);
  2092. end = start + frag_iter->len;
  2093. if ((copy = end - offset) > 0) {
  2094. if (copy > len)
  2095. copy = len;
  2096. if (skb_store_bits(frag_iter, offset - start,
  2097. from, copy))
  2098. goto fault;
  2099. if ((len -= copy) == 0)
  2100. return 0;
  2101. offset += copy;
  2102. from += copy;
  2103. }
  2104. start = end;
  2105. }
  2106. if (!len)
  2107. return 0;
  2108. fault:
  2109. return -EFAULT;
  2110. }
  2111. EXPORT_SYMBOL(skb_store_bits);
  2112. /* Checksum skb data. */
  2113. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  2114. __wsum csum, const struct skb_checksum_ops *ops)
  2115. {
  2116. int start = skb_headlen(skb);
  2117. int i, copy = start - offset;
  2118. struct sk_buff *frag_iter;
  2119. int pos = 0;
  2120. /* Checksum header. */
  2121. if (copy > 0) {
  2122. if (copy > len)
  2123. copy = len;
  2124. csum = ops->update(skb->data + offset, copy, csum);
  2125. if ((len -= copy) == 0)
  2126. return csum;
  2127. offset += copy;
  2128. pos = copy;
  2129. }
  2130. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2131. int end;
  2132. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2133. WARN_ON(start > offset + len);
  2134. end = start + skb_frag_size(frag);
  2135. if ((copy = end - offset) > 0) {
  2136. u32 p_off, p_len, copied;
  2137. struct page *p;
  2138. __wsum csum2;
  2139. u8 *vaddr;
  2140. if (copy > len)
  2141. copy = len;
  2142. skb_frag_foreach_page(frag,
  2143. frag->page_offset + offset - start,
  2144. copy, p, p_off, p_len, copied) {
  2145. vaddr = kmap_atomic(p);
  2146. csum2 = ops->update(vaddr + p_off, p_len, 0);
  2147. kunmap_atomic(vaddr);
  2148. csum = ops->combine(csum, csum2, pos, p_len);
  2149. pos += p_len;
  2150. }
  2151. if (!(len -= copy))
  2152. return csum;
  2153. offset += copy;
  2154. }
  2155. start = end;
  2156. }
  2157. skb_walk_frags(skb, frag_iter) {
  2158. int end;
  2159. WARN_ON(start > offset + len);
  2160. end = start + frag_iter->len;
  2161. if ((copy = end - offset) > 0) {
  2162. __wsum csum2;
  2163. if (copy > len)
  2164. copy = len;
  2165. csum2 = __skb_checksum(frag_iter, offset - start,
  2166. copy, 0, ops);
  2167. csum = ops->combine(csum, csum2, pos, copy);
  2168. if ((len -= copy) == 0)
  2169. return csum;
  2170. offset += copy;
  2171. pos += copy;
  2172. }
  2173. start = end;
  2174. }
  2175. BUG_ON(len);
  2176. return csum;
  2177. }
  2178. EXPORT_SYMBOL(__skb_checksum);
  2179. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  2180. int len, __wsum csum)
  2181. {
  2182. const struct skb_checksum_ops ops = {
  2183. .update = csum_partial_ext,
  2184. .combine = csum_block_add_ext,
  2185. };
  2186. return __skb_checksum(skb, offset, len, csum, &ops);
  2187. }
  2188. EXPORT_SYMBOL(skb_checksum);
  2189. /* Both of above in one bottle. */
  2190. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  2191. u8 *to, int len, __wsum csum)
  2192. {
  2193. int start = skb_headlen(skb);
  2194. int i, copy = start - offset;
  2195. struct sk_buff *frag_iter;
  2196. int pos = 0;
  2197. /* Copy header. */
  2198. if (copy > 0) {
  2199. if (copy > len)
  2200. copy = len;
  2201. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  2202. copy, csum);
  2203. if ((len -= copy) == 0)
  2204. return csum;
  2205. offset += copy;
  2206. to += copy;
  2207. pos = copy;
  2208. }
  2209. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2210. int end;
  2211. WARN_ON(start > offset + len);
  2212. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2213. if ((copy = end - offset) > 0) {
  2214. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2215. u32 p_off, p_len, copied;
  2216. struct page *p;
  2217. __wsum csum2;
  2218. u8 *vaddr;
  2219. if (copy > len)
  2220. copy = len;
  2221. skb_frag_foreach_page(frag,
  2222. frag->page_offset + offset - start,
  2223. copy, p, p_off, p_len, copied) {
  2224. vaddr = kmap_atomic(p);
  2225. csum2 = csum_partial_copy_nocheck(vaddr + p_off,
  2226. to + copied,
  2227. p_len, 0);
  2228. kunmap_atomic(vaddr);
  2229. csum = csum_block_add(csum, csum2, pos);
  2230. pos += p_len;
  2231. }
  2232. if (!(len -= copy))
  2233. return csum;
  2234. offset += copy;
  2235. to += copy;
  2236. }
  2237. start = end;
  2238. }
  2239. skb_walk_frags(skb, frag_iter) {
  2240. __wsum csum2;
  2241. int end;
  2242. WARN_ON(start > offset + len);
  2243. end = start + frag_iter->len;
  2244. if ((copy = end - offset) > 0) {
  2245. if (copy > len)
  2246. copy = len;
  2247. csum2 = skb_copy_and_csum_bits(frag_iter,
  2248. offset - start,
  2249. to, copy, 0);
  2250. csum = csum_block_add(csum, csum2, pos);
  2251. if ((len -= copy) == 0)
  2252. return csum;
  2253. offset += copy;
  2254. to += copy;
  2255. pos += copy;
  2256. }
  2257. start = end;
  2258. }
  2259. BUG_ON(len);
  2260. return csum;
  2261. }
  2262. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  2263. static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
  2264. {
  2265. net_warn_ratelimited(
  2266. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  2267. __func__);
  2268. return 0;
  2269. }
  2270. static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
  2271. int offset, int len)
  2272. {
  2273. net_warn_ratelimited(
  2274. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  2275. __func__);
  2276. return 0;
  2277. }
  2278. static const struct skb_checksum_ops default_crc32c_ops = {
  2279. .update = warn_crc32c_csum_update,
  2280. .combine = warn_crc32c_csum_combine,
  2281. };
  2282. const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
  2283. &default_crc32c_ops;
  2284. EXPORT_SYMBOL(crc32c_csum_stub);
  2285. /**
  2286. * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
  2287. * @from: source buffer
  2288. *
  2289. * Calculates the amount of linear headroom needed in the 'to' skb passed
  2290. * into skb_zerocopy().
  2291. */
  2292. unsigned int
  2293. skb_zerocopy_headlen(const struct sk_buff *from)
  2294. {
  2295. unsigned int hlen = 0;
  2296. if (!from->head_frag ||
  2297. skb_headlen(from) < L1_CACHE_BYTES ||
  2298. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  2299. hlen = skb_headlen(from);
  2300. if (skb_has_frag_list(from))
  2301. hlen = from->len;
  2302. return hlen;
  2303. }
  2304. EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
  2305. /**
  2306. * skb_zerocopy - Zero copy skb to skb
  2307. * @to: destination buffer
  2308. * @from: source buffer
  2309. * @len: number of bytes to copy from source buffer
  2310. * @hlen: size of linear headroom in destination buffer
  2311. *
  2312. * Copies up to `len` bytes from `from` to `to` by creating references
  2313. * to the frags in the source buffer.
  2314. *
  2315. * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
  2316. * headroom in the `to` buffer.
  2317. *
  2318. * Return value:
  2319. * 0: everything is OK
  2320. * -ENOMEM: couldn't orphan frags of @from due to lack of memory
  2321. * -EFAULT: skb_copy_bits() found some problem with skb geometry
  2322. */
  2323. int
  2324. skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
  2325. {
  2326. int i, j = 0;
  2327. int plen = 0; /* length of skb->head fragment */
  2328. int ret;
  2329. struct page *page;
  2330. unsigned int offset;
  2331. BUG_ON(!from->head_frag && !hlen);
  2332. /* dont bother with small payloads */
  2333. if (len <= skb_tailroom(to))
  2334. return skb_copy_bits(from, 0, skb_put(to, len), len);
  2335. if (hlen) {
  2336. ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
  2337. if (unlikely(ret))
  2338. return ret;
  2339. len -= hlen;
  2340. } else {
  2341. plen = min_t(int, skb_headlen(from), len);
  2342. if (plen) {
  2343. page = virt_to_head_page(from->head);
  2344. offset = from->data - (unsigned char *)page_address(page);
  2345. __skb_fill_page_desc(to, 0, page, offset, plen);
  2346. get_page(page);
  2347. j = 1;
  2348. len -= plen;
  2349. }
  2350. }
  2351. to->truesize += len + plen;
  2352. to->len += len + plen;
  2353. to->data_len += len + plen;
  2354. if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
  2355. skb_tx_error(from);
  2356. return -ENOMEM;
  2357. }
  2358. skb_zerocopy_clone(to, from, GFP_ATOMIC);
  2359. for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
  2360. if (!len)
  2361. break;
  2362. skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
  2363. skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
  2364. len -= skb_shinfo(to)->frags[j].size;
  2365. skb_frag_ref(to, j);
  2366. j++;
  2367. }
  2368. skb_shinfo(to)->nr_frags = j;
  2369. return 0;
  2370. }
  2371. EXPORT_SYMBOL_GPL(skb_zerocopy);
  2372. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  2373. {
  2374. __wsum csum;
  2375. long csstart;
  2376. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2377. csstart = skb_checksum_start_offset(skb);
  2378. else
  2379. csstart = skb_headlen(skb);
  2380. BUG_ON(csstart > skb_headlen(skb));
  2381. skb_copy_from_linear_data(skb, to, csstart);
  2382. csum = 0;
  2383. if (csstart != skb->len)
  2384. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  2385. skb->len - csstart, 0);
  2386. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2387. long csstuff = csstart + skb->csum_offset;
  2388. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  2389. }
  2390. }
  2391. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  2392. /**
  2393. * skb_dequeue - remove from the head of the queue
  2394. * @list: list to dequeue from
  2395. *
  2396. * Remove the head of the list. The list lock is taken so the function
  2397. * may be used safely with other locking list functions. The head item is
  2398. * returned or %NULL if the list is empty.
  2399. */
  2400. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  2401. {
  2402. unsigned long flags;
  2403. struct sk_buff *result;
  2404. spin_lock_irqsave(&list->lock, flags);
  2405. result = __skb_dequeue(list);
  2406. spin_unlock_irqrestore(&list->lock, flags);
  2407. return result;
  2408. }
  2409. EXPORT_SYMBOL(skb_dequeue);
  2410. /**
  2411. * skb_dequeue_tail - remove from the tail of the queue
  2412. * @list: list to dequeue from
  2413. *
  2414. * Remove the tail of the list. The list lock is taken so the function
  2415. * may be used safely with other locking list functions. The tail item is
  2416. * returned or %NULL if the list is empty.
  2417. */
  2418. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  2419. {
  2420. unsigned long flags;
  2421. struct sk_buff *result;
  2422. spin_lock_irqsave(&list->lock, flags);
  2423. result = __skb_dequeue_tail(list);
  2424. spin_unlock_irqrestore(&list->lock, flags);
  2425. return result;
  2426. }
  2427. EXPORT_SYMBOL(skb_dequeue_tail);
  2428. /**
  2429. * skb_queue_purge - empty a list
  2430. * @list: list to empty
  2431. *
  2432. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  2433. * the list and one reference dropped. This function takes the list
  2434. * lock and is atomic with respect to other list locking functions.
  2435. */
  2436. void skb_queue_purge(struct sk_buff_head *list)
  2437. {
  2438. struct sk_buff *skb;
  2439. while ((skb = skb_dequeue(list)) != NULL)
  2440. kfree_skb(skb);
  2441. }
  2442. EXPORT_SYMBOL(skb_queue_purge);
  2443. /**
  2444. * skb_rbtree_purge - empty a skb rbtree
  2445. * @root: root of the rbtree to empty
  2446. * Return value: the sum of truesizes of all purged skbs.
  2447. *
  2448. * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
  2449. * the list and one reference dropped. This function does not take
  2450. * any lock. Synchronization should be handled by the caller (e.g., TCP
  2451. * out-of-order queue is protected by the socket lock).
  2452. */
  2453. unsigned int skb_rbtree_purge(struct rb_root *root)
  2454. {
  2455. struct rb_node *p = rb_first(root);
  2456. unsigned int sum = 0;
  2457. while (p) {
  2458. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  2459. p = rb_next(p);
  2460. rb_erase(&skb->rbnode, root);
  2461. sum += skb->truesize;
  2462. kfree_skb(skb);
  2463. }
  2464. return sum;
  2465. }
  2466. /**
  2467. * skb_queue_head - queue a buffer at the list head
  2468. * @list: list to use
  2469. * @newsk: buffer to queue
  2470. *
  2471. * Queue a buffer at the start of the list. This function takes the
  2472. * list lock and can be used safely with other locking &sk_buff functions
  2473. * safely.
  2474. *
  2475. * A buffer cannot be placed on two lists at the same time.
  2476. */
  2477. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  2478. {
  2479. unsigned long flags;
  2480. spin_lock_irqsave(&list->lock, flags);
  2481. __skb_queue_head(list, newsk);
  2482. spin_unlock_irqrestore(&list->lock, flags);
  2483. }
  2484. EXPORT_SYMBOL(skb_queue_head);
  2485. /**
  2486. * skb_queue_tail - queue a buffer at the list tail
  2487. * @list: list to use
  2488. * @newsk: buffer to queue
  2489. *
  2490. * Queue a buffer at the tail of the list. This function takes the
  2491. * list lock and can be used safely with other locking &sk_buff functions
  2492. * safely.
  2493. *
  2494. * A buffer cannot be placed on two lists at the same time.
  2495. */
  2496. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  2497. {
  2498. unsigned long flags;
  2499. spin_lock_irqsave(&list->lock, flags);
  2500. __skb_queue_tail(list, newsk);
  2501. spin_unlock_irqrestore(&list->lock, flags);
  2502. }
  2503. EXPORT_SYMBOL(skb_queue_tail);
  2504. /**
  2505. * skb_unlink - remove a buffer from a list
  2506. * @skb: buffer to remove
  2507. * @list: list to use
  2508. *
  2509. * Remove a packet from a list. The list locks are taken and this
  2510. * function is atomic with respect to other list locked calls
  2511. *
  2512. * You must know what list the SKB is on.
  2513. */
  2514. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  2515. {
  2516. unsigned long flags;
  2517. spin_lock_irqsave(&list->lock, flags);
  2518. __skb_unlink(skb, list);
  2519. spin_unlock_irqrestore(&list->lock, flags);
  2520. }
  2521. EXPORT_SYMBOL(skb_unlink);
  2522. /**
  2523. * skb_append - append a buffer
  2524. * @old: buffer to insert after
  2525. * @newsk: buffer to insert
  2526. * @list: list to use
  2527. *
  2528. * Place a packet after a given packet in a list. The list locks are taken
  2529. * and this function is atomic with respect to other list locked calls.
  2530. * A buffer cannot be placed on two lists at the same time.
  2531. */
  2532. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2533. {
  2534. unsigned long flags;
  2535. spin_lock_irqsave(&list->lock, flags);
  2536. __skb_queue_after(list, old, newsk);
  2537. spin_unlock_irqrestore(&list->lock, flags);
  2538. }
  2539. EXPORT_SYMBOL(skb_append);
  2540. /**
  2541. * skb_insert - insert a buffer
  2542. * @old: buffer to insert before
  2543. * @newsk: buffer to insert
  2544. * @list: list to use
  2545. *
  2546. * Place a packet before a given packet in a list. The list locks are
  2547. * taken and this function is atomic with respect to other list locked
  2548. * calls.
  2549. *
  2550. * A buffer cannot be placed on two lists at the same time.
  2551. */
  2552. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2553. {
  2554. unsigned long flags;
  2555. spin_lock_irqsave(&list->lock, flags);
  2556. __skb_insert(newsk, old->prev, old, list);
  2557. spin_unlock_irqrestore(&list->lock, flags);
  2558. }
  2559. EXPORT_SYMBOL(skb_insert);
  2560. static inline void skb_split_inside_header(struct sk_buff *skb,
  2561. struct sk_buff* skb1,
  2562. const u32 len, const int pos)
  2563. {
  2564. int i;
  2565. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  2566. pos - len);
  2567. /* And move data appendix as is. */
  2568. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  2569. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  2570. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  2571. skb_shinfo(skb)->nr_frags = 0;
  2572. skb1->data_len = skb->data_len;
  2573. skb1->len += skb1->data_len;
  2574. skb->data_len = 0;
  2575. skb->len = len;
  2576. skb_set_tail_pointer(skb, len);
  2577. }
  2578. static inline void skb_split_no_header(struct sk_buff *skb,
  2579. struct sk_buff* skb1,
  2580. const u32 len, int pos)
  2581. {
  2582. int i, k = 0;
  2583. const int nfrags = skb_shinfo(skb)->nr_frags;
  2584. skb_shinfo(skb)->nr_frags = 0;
  2585. skb1->len = skb1->data_len = skb->len - len;
  2586. skb->len = len;
  2587. skb->data_len = len - pos;
  2588. for (i = 0; i < nfrags; i++) {
  2589. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2590. if (pos + size > len) {
  2591. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  2592. if (pos < len) {
  2593. /* Split frag.
  2594. * We have two variants in this case:
  2595. * 1. Move all the frag to the second
  2596. * part, if it is possible. F.e.
  2597. * this approach is mandatory for TUX,
  2598. * where splitting is expensive.
  2599. * 2. Split is accurately. We make this.
  2600. */
  2601. skb_frag_ref(skb, i);
  2602. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  2603. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  2604. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  2605. skb_shinfo(skb)->nr_frags++;
  2606. }
  2607. k++;
  2608. } else
  2609. skb_shinfo(skb)->nr_frags++;
  2610. pos += size;
  2611. }
  2612. skb_shinfo(skb1)->nr_frags = k;
  2613. }
  2614. /**
  2615. * skb_split - Split fragmented skb to two parts at length len.
  2616. * @skb: the buffer to split
  2617. * @skb1: the buffer to receive the second part
  2618. * @len: new length for skb
  2619. */
  2620. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2621. {
  2622. int pos = skb_headlen(skb);
  2623. skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
  2624. SKBTX_SHARED_FRAG;
  2625. skb_zerocopy_clone(skb1, skb, 0);
  2626. if (len < pos) /* Split line is inside header. */
  2627. skb_split_inside_header(skb, skb1, len, pos);
  2628. else /* Second chunk has no header, nothing to copy. */
  2629. skb_split_no_header(skb, skb1, len, pos);
  2630. }
  2631. EXPORT_SYMBOL(skb_split);
  2632. /* Shifting from/to a cloned skb is a no-go.
  2633. *
  2634. * Caller cannot keep skb_shinfo related pointers past calling here!
  2635. */
  2636. static int skb_prepare_for_shift(struct sk_buff *skb)
  2637. {
  2638. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2639. }
  2640. /**
  2641. * skb_shift - Shifts paged data partially from skb to another
  2642. * @tgt: buffer into which tail data gets added
  2643. * @skb: buffer from which the paged data comes from
  2644. * @shiftlen: shift up to this many bytes
  2645. *
  2646. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2647. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2648. * It's up to caller to free skb if everything was shifted.
  2649. *
  2650. * If @tgt runs out of frags, the whole operation is aborted.
  2651. *
  2652. * Skb cannot include anything else but paged data while tgt is allowed
  2653. * to have non-paged data as well.
  2654. *
  2655. * TODO: full sized shift could be optimized but that would need
  2656. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2657. */
  2658. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2659. {
  2660. int from, to, merge, todo;
  2661. struct skb_frag_struct *fragfrom, *fragto;
  2662. BUG_ON(shiftlen > skb->len);
  2663. if (skb_headlen(skb))
  2664. return 0;
  2665. if (skb_zcopy(tgt) || skb_zcopy(skb))
  2666. return 0;
  2667. todo = shiftlen;
  2668. from = 0;
  2669. to = skb_shinfo(tgt)->nr_frags;
  2670. fragfrom = &skb_shinfo(skb)->frags[from];
  2671. /* Actual merge is delayed until the point when we know we can
  2672. * commit all, so that we don't have to undo partial changes
  2673. */
  2674. if (!to ||
  2675. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2676. fragfrom->page_offset)) {
  2677. merge = -1;
  2678. } else {
  2679. merge = to - 1;
  2680. todo -= skb_frag_size(fragfrom);
  2681. if (todo < 0) {
  2682. if (skb_prepare_for_shift(skb) ||
  2683. skb_prepare_for_shift(tgt))
  2684. return 0;
  2685. /* All previous frag pointers might be stale! */
  2686. fragfrom = &skb_shinfo(skb)->frags[from];
  2687. fragto = &skb_shinfo(tgt)->frags[merge];
  2688. skb_frag_size_add(fragto, shiftlen);
  2689. skb_frag_size_sub(fragfrom, shiftlen);
  2690. fragfrom->page_offset += shiftlen;
  2691. goto onlymerged;
  2692. }
  2693. from++;
  2694. }
  2695. /* Skip full, not-fitting skb to avoid expensive operations */
  2696. if ((shiftlen == skb->len) &&
  2697. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2698. return 0;
  2699. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2700. return 0;
  2701. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2702. if (to == MAX_SKB_FRAGS)
  2703. return 0;
  2704. fragfrom = &skb_shinfo(skb)->frags[from];
  2705. fragto = &skb_shinfo(tgt)->frags[to];
  2706. if (todo >= skb_frag_size(fragfrom)) {
  2707. *fragto = *fragfrom;
  2708. todo -= skb_frag_size(fragfrom);
  2709. from++;
  2710. to++;
  2711. } else {
  2712. __skb_frag_ref(fragfrom);
  2713. fragto->page = fragfrom->page;
  2714. fragto->page_offset = fragfrom->page_offset;
  2715. skb_frag_size_set(fragto, todo);
  2716. fragfrom->page_offset += todo;
  2717. skb_frag_size_sub(fragfrom, todo);
  2718. todo = 0;
  2719. to++;
  2720. break;
  2721. }
  2722. }
  2723. /* Ready to "commit" this state change to tgt */
  2724. skb_shinfo(tgt)->nr_frags = to;
  2725. if (merge >= 0) {
  2726. fragfrom = &skb_shinfo(skb)->frags[0];
  2727. fragto = &skb_shinfo(tgt)->frags[merge];
  2728. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2729. __skb_frag_unref(fragfrom);
  2730. }
  2731. /* Reposition in the original skb */
  2732. to = 0;
  2733. while (from < skb_shinfo(skb)->nr_frags)
  2734. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2735. skb_shinfo(skb)->nr_frags = to;
  2736. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2737. onlymerged:
  2738. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2739. * the other hand might need it if it needs to be resent
  2740. */
  2741. tgt->ip_summed = CHECKSUM_PARTIAL;
  2742. skb->ip_summed = CHECKSUM_PARTIAL;
  2743. /* Yak, is it really working this way? Some helper please? */
  2744. skb->len -= shiftlen;
  2745. skb->data_len -= shiftlen;
  2746. skb->truesize -= shiftlen;
  2747. tgt->len += shiftlen;
  2748. tgt->data_len += shiftlen;
  2749. tgt->truesize += shiftlen;
  2750. return shiftlen;
  2751. }
  2752. /**
  2753. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2754. * @skb: the buffer to read
  2755. * @from: lower offset of data to be read
  2756. * @to: upper offset of data to be read
  2757. * @st: state variable
  2758. *
  2759. * Initializes the specified state variable. Must be called before
  2760. * invoking skb_seq_read() for the first time.
  2761. */
  2762. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2763. unsigned int to, struct skb_seq_state *st)
  2764. {
  2765. st->lower_offset = from;
  2766. st->upper_offset = to;
  2767. st->root_skb = st->cur_skb = skb;
  2768. st->frag_idx = st->stepped_offset = 0;
  2769. st->frag_data = NULL;
  2770. }
  2771. EXPORT_SYMBOL(skb_prepare_seq_read);
  2772. /**
  2773. * skb_seq_read - Sequentially read skb data
  2774. * @consumed: number of bytes consumed by the caller so far
  2775. * @data: destination pointer for data to be returned
  2776. * @st: state variable
  2777. *
  2778. * Reads a block of skb data at @consumed relative to the
  2779. * lower offset specified to skb_prepare_seq_read(). Assigns
  2780. * the head of the data block to @data and returns the length
  2781. * of the block or 0 if the end of the skb data or the upper
  2782. * offset has been reached.
  2783. *
  2784. * The caller is not required to consume all of the data
  2785. * returned, i.e. @consumed is typically set to the number
  2786. * of bytes already consumed and the next call to
  2787. * skb_seq_read() will return the remaining part of the block.
  2788. *
  2789. * Note 1: The size of each block of data returned can be arbitrary,
  2790. * this limitation is the cost for zerocopy sequential
  2791. * reads of potentially non linear data.
  2792. *
  2793. * Note 2: Fragment lists within fragments are not implemented
  2794. * at the moment, state->root_skb could be replaced with
  2795. * a stack for this purpose.
  2796. */
  2797. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2798. struct skb_seq_state *st)
  2799. {
  2800. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2801. skb_frag_t *frag;
  2802. if (unlikely(abs_offset >= st->upper_offset)) {
  2803. if (st->frag_data) {
  2804. kunmap_atomic(st->frag_data);
  2805. st->frag_data = NULL;
  2806. }
  2807. return 0;
  2808. }
  2809. next_skb:
  2810. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2811. if (abs_offset < block_limit && !st->frag_data) {
  2812. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2813. return block_limit - abs_offset;
  2814. }
  2815. if (st->frag_idx == 0 && !st->frag_data)
  2816. st->stepped_offset += skb_headlen(st->cur_skb);
  2817. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2818. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2819. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2820. if (abs_offset < block_limit) {
  2821. if (!st->frag_data)
  2822. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2823. *data = (u8 *) st->frag_data + frag->page_offset +
  2824. (abs_offset - st->stepped_offset);
  2825. return block_limit - abs_offset;
  2826. }
  2827. if (st->frag_data) {
  2828. kunmap_atomic(st->frag_data);
  2829. st->frag_data = NULL;
  2830. }
  2831. st->frag_idx++;
  2832. st->stepped_offset += skb_frag_size(frag);
  2833. }
  2834. if (st->frag_data) {
  2835. kunmap_atomic(st->frag_data);
  2836. st->frag_data = NULL;
  2837. }
  2838. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2839. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2840. st->frag_idx = 0;
  2841. goto next_skb;
  2842. } else if (st->cur_skb->next) {
  2843. st->cur_skb = st->cur_skb->next;
  2844. st->frag_idx = 0;
  2845. goto next_skb;
  2846. }
  2847. return 0;
  2848. }
  2849. EXPORT_SYMBOL(skb_seq_read);
  2850. /**
  2851. * skb_abort_seq_read - Abort a sequential read of skb data
  2852. * @st: state variable
  2853. *
  2854. * Must be called if skb_seq_read() was not called until it
  2855. * returned 0.
  2856. */
  2857. void skb_abort_seq_read(struct skb_seq_state *st)
  2858. {
  2859. if (st->frag_data)
  2860. kunmap_atomic(st->frag_data);
  2861. }
  2862. EXPORT_SYMBOL(skb_abort_seq_read);
  2863. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2864. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2865. struct ts_config *conf,
  2866. struct ts_state *state)
  2867. {
  2868. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2869. }
  2870. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2871. {
  2872. skb_abort_seq_read(TS_SKB_CB(state));
  2873. }
  2874. /**
  2875. * skb_find_text - Find a text pattern in skb data
  2876. * @skb: the buffer to look in
  2877. * @from: search offset
  2878. * @to: search limit
  2879. * @config: textsearch configuration
  2880. *
  2881. * Finds a pattern in the skb data according to the specified
  2882. * textsearch configuration. Use textsearch_next() to retrieve
  2883. * subsequent occurrences of the pattern. Returns the offset
  2884. * to the first occurrence or UINT_MAX if no match was found.
  2885. */
  2886. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2887. unsigned int to, struct ts_config *config)
  2888. {
  2889. struct ts_state state;
  2890. unsigned int ret;
  2891. config->get_next_block = skb_ts_get_next_block;
  2892. config->finish = skb_ts_finish;
  2893. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
  2894. ret = textsearch_find(config, &state);
  2895. return (ret <= to - from ? ret : UINT_MAX);
  2896. }
  2897. EXPORT_SYMBOL(skb_find_text);
  2898. /**
  2899. * skb_append_datato_frags - append the user data to a skb
  2900. * @sk: sock structure
  2901. * @skb: skb structure to be appended with user data.
  2902. * @getfrag: call back function to be used for getting the user data
  2903. * @from: pointer to user message iov
  2904. * @length: length of the iov message
  2905. *
  2906. * Description: This procedure append the user data in the fragment part
  2907. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2908. */
  2909. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2910. int (*getfrag)(void *from, char *to, int offset,
  2911. int len, int odd, struct sk_buff *skb),
  2912. void *from, int length)
  2913. {
  2914. int frg_cnt = skb_shinfo(skb)->nr_frags;
  2915. int copy;
  2916. int offset = 0;
  2917. int ret;
  2918. struct page_frag *pfrag = &current->task_frag;
  2919. do {
  2920. /* Return error if we don't have space for new frag */
  2921. if (frg_cnt >= MAX_SKB_FRAGS)
  2922. return -EMSGSIZE;
  2923. if (!sk_page_frag_refill(sk, pfrag))
  2924. return -ENOMEM;
  2925. /* copy the user data to page */
  2926. copy = min_t(int, length, pfrag->size - pfrag->offset);
  2927. ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
  2928. offset, copy, 0, skb);
  2929. if (ret < 0)
  2930. return -EFAULT;
  2931. /* copy was successful so update the size parameters */
  2932. skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
  2933. copy);
  2934. frg_cnt++;
  2935. pfrag->offset += copy;
  2936. get_page(pfrag->page);
  2937. skb->truesize += copy;
  2938. refcount_add(copy, &sk->sk_wmem_alloc);
  2939. skb->len += copy;
  2940. skb->data_len += copy;
  2941. offset += copy;
  2942. length -= copy;
  2943. } while (length > 0);
  2944. return 0;
  2945. }
  2946. EXPORT_SYMBOL(skb_append_datato_frags);
  2947. int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
  2948. int offset, size_t size)
  2949. {
  2950. int i = skb_shinfo(skb)->nr_frags;
  2951. if (skb_can_coalesce(skb, i, page, offset)) {
  2952. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
  2953. } else if (i < MAX_SKB_FRAGS) {
  2954. get_page(page);
  2955. skb_fill_page_desc(skb, i, page, offset, size);
  2956. } else {
  2957. return -EMSGSIZE;
  2958. }
  2959. return 0;
  2960. }
  2961. EXPORT_SYMBOL_GPL(skb_append_pagefrags);
  2962. /**
  2963. * skb_pull_rcsum - pull skb and update receive checksum
  2964. * @skb: buffer to update
  2965. * @len: length of data pulled
  2966. *
  2967. * This function performs an skb_pull on the packet and updates
  2968. * the CHECKSUM_COMPLETE checksum. It should be used on
  2969. * receive path processing instead of skb_pull unless you know
  2970. * that the checksum difference is zero (e.g., a valid IP header)
  2971. * or you are setting ip_summed to CHECKSUM_NONE.
  2972. */
  2973. void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2974. {
  2975. unsigned char *data = skb->data;
  2976. BUG_ON(len > skb->len);
  2977. __skb_pull(skb, len);
  2978. skb_postpull_rcsum(skb, data, len);
  2979. return skb->data;
  2980. }
  2981. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2982. static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
  2983. {
  2984. skb_frag_t head_frag;
  2985. struct page *page;
  2986. page = virt_to_head_page(frag_skb->head);
  2987. head_frag.page.p = page;
  2988. head_frag.page_offset = frag_skb->data -
  2989. (unsigned char *)page_address(page);
  2990. head_frag.size = skb_headlen(frag_skb);
  2991. return head_frag;
  2992. }
  2993. /**
  2994. * skb_segment - Perform protocol segmentation on skb.
  2995. * @head_skb: buffer to segment
  2996. * @features: features for the output path (see dev->features)
  2997. *
  2998. * This function performs segmentation on the given skb. It returns
  2999. * a pointer to the first in a list of new skbs for the segments.
  3000. * In case of error it returns ERR_PTR(err).
  3001. */
  3002. struct sk_buff *skb_segment(struct sk_buff *head_skb,
  3003. netdev_features_t features)
  3004. {
  3005. struct sk_buff *segs = NULL;
  3006. struct sk_buff *tail = NULL;
  3007. struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
  3008. skb_frag_t *frag = skb_shinfo(head_skb)->frags;
  3009. unsigned int mss = skb_shinfo(head_skb)->gso_size;
  3010. unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
  3011. struct sk_buff *frag_skb = head_skb;
  3012. unsigned int offset = doffset;
  3013. unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
  3014. unsigned int partial_segs = 0;
  3015. unsigned int headroom;
  3016. unsigned int len = head_skb->len;
  3017. __be16 proto;
  3018. bool csum, sg;
  3019. int nfrags = skb_shinfo(head_skb)->nr_frags;
  3020. int err = -ENOMEM;
  3021. int i = 0;
  3022. int pos;
  3023. int dummy;
  3024. __skb_push(head_skb, doffset);
  3025. proto = skb_network_protocol(head_skb, &dummy);
  3026. if (unlikely(!proto))
  3027. return ERR_PTR(-EINVAL);
  3028. sg = !!(features & NETIF_F_SG);
  3029. csum = !!can_checksum_protocol(features, proto);
  3030. if (sg && csum && (mss != GSO_BY_FRAGS)) {
  3031. if (!(features & NETIF_F_GSO_PARTIAL)) {
  3032. struct sk_buff *iter;
  3033. unsigned int frag_len;
  3034. if (!list_skb ||
  3035. !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
  3036. goto normal;
  3037. /* If we get here then all the required
  3038. * GSO features except frag_list are supported.
  3039. * Try to split the SKB to multiple GSO SKBs
  3040. * with no frag_list.
  3041. * Currently we can do that only when the buffers don't
  3042. * have a linear part and all the buffers except
  3043. * the last are of the same length.
  3044. */
  3045. frag_len = list_skb->len;
  3046. skb_walk_frags(head_skb, iter) {
  3047. if (frag_len != iter->len && iter->next)
  3048. goto normal;
  3049. if (skb_headlen(iter) && !iter->head_frag)
  3050. goto normal;
  3051. len -= iter->len;
  3052. }
  3053. if (len != frag_len)
  3054. goto normal;
  3055. }
  3056. /* GSO partial only requires that we trim off any excess that
  3057. * doesn't fit into an MSS sized block, so take care of that
  3058. * now.
  3059. */
  3060. partial_segs = len / mss;
  3061. if (partial_segs > 1)
  3062. mss *= partial_segs;
  3063. else
  3064. partial_segs = 0;
  3065. }
  3066. normal:
  3067. headroom = skb_headroom(head_skb);
  3068. pos = skb_headlen(head_skb);
  3069. do {
  3070. struct sk_buff *nskb;
  3071. skb_frag_t *nskb_frag;
  3072. int hsize;
  3073. int size;
  3074. if (unlikely(mss == GSO_BY_FRAGS)) {
  3075. len = list_skb->len;
  3076. } else {
  3077. len = head_skb->len - offset;
  3078. if (len > mss)
  3079. len = mss;
  3080. }
  3081. hsize = skb_headlen(head_skb) - offset;
  3082. if (hsize < 0)
  3083. hsize = 0;
  3084. if (hsize > len || !sg)
  3085. hsize = len;
  3086. if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
  3087. (skb_headlen(list_skb) == len || sg)) {
  3088. BUG_ON(skb_headlen(list_skb) > len);
  3089. i = 0;
  3090. nfrags = skb_shinfo(list_skb)->nr_frags;
  3091. frag = skb_shinfo(list_skb)->frags;
  3092. frag_skb = list_skb;
  3093. pos += skb_headlen(list_skb);
  3094. while (pos < offset + len) {
  3095. BUG_ON(i >= nfrags);
  3096. size = skb_frag_size(frag);
  3097. if (pos + size > offset + len)
  3098. break;
  3099. i++;
  3100. pos += size;
  3101. frag++;
  3102. }
  3103. nskb = skb_clone(list_skb, GFP_ATOMIC);
  3104. list_skb = list_skb->next;
  3105. if (unlikely(!nskb))
  3106. goto err;
  3107. if (unlikely(pskb_trim(nskb, len))) {
  3108. kfree_skb(nskb);
  3109. goto err;
  3110. }
  3111. hsize = skb_end_offset(nskb);
  3112. if (skb_cow_head(nskb, doffset + headroom)) {
  3113. kfree_skb(nskb);
  3114. goto err;
  3115. }
  3116. nskb->truesize += skb_end_offset(nskb) - hsize;
  3117. skb_release_head_state(nskb);
  3118. __skb_push(nskb, doffset);
  3119. } else {
  3120. nskb = __alloc_skb(hsize + doffset + headroom,
  3121. GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
  3122. NUMA_NO_NODE);
  3123. if (unlikely(!nskb))
  3124. goto err;
  3125. skb_reserve(nskb, headroom);
  3126. __skb_put(nskb, doffset);
  3127. }
  3128. if (segs)
  3129. tail->next = nskb;
  3130. else
  3131. segs = nskb;
  3132. tail = nskb;
  3133. __copy_skb_header(nskb, head_skb);
  3134. skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
  3135. skb_reset_mac_len(nskb);
  3136. skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
  3137. nskb->data - tnl_hlen,
  3138. doffset + tnl_hlen);
  3139. if (nskb->len == len + doffset)
  3140. goto perform_csum_check;
  3141. if (!sg) {
  3142. if (!nskb->remcsum_offload)
  3143. nskb->ip_summed = CHECKSUM_NONE;
  3144. SKB_GSO_CB(nskb)->csum =
  3145. skb_copy_and_csum_bits(head_skb, offset,
  3146. skb_put(nskb, len),
  3147. len, 0);
  3148. SKB_GSO_CB(nskb)->csum_start =
  3149. skb_headroom(nskb) + doffset;
  3150. continue;
  3151. }
  3152. nskb_frag = skb_shinfo(nskb)->frags;
  3153. skb_copy_from_linear_data_offset(head_skb, offset,
  3154. skb_put(nskb, hsize), hsize);
  3155. skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
  3156. SKBTX_SHARED_FRAG;
  3157. if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
  3158. skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
  3159. goto err;
  3160. while (pos < offset + len) {
  3161. if (i >= nfrags) {
  3162. i = 0;
  3163. nfrags = skb_shinfo(list_skb)->nr_frags;
  3164. frag = skb_shinfo(list_skb)->frags;
  3165. frag_skb = list_skb;
  3166. if (!skb_headlen(list_skb)) {
  3167. BUG_ON(!nfrags);
  3168. } else {
  3169. BUG_ON(!list_skb->head_frag);
  3170. /* to make room for head_frag. */
  3171. i--;
  3172. frag--;
  3173. }
  3174. if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
  3175. skb_zerocopy_clone(nskb, frag_skb,
  3176. GFP_ATOMIC))
  3177. goto err;
  3178. list_skb = list_skb->next;
  3179. }
  3180. if (unlikely(skb_shinfo(nskb)->nr_frags >=
  3181. MAX_SKB_FRAGS)) {
  3182. net_warn_ratelimited(
  3183. "skb_segment: too many frags: %u %u\n",
  3184. pos, mss);
  3185. err = -EINVAL;
  3186. goto err;
  3187. }
  3188. *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
  3189. __skb_frag_ref(nskb_frag);
  3190. size = skb_frag_size(nskb_frag);
  3191. if (pos < offset) {
  3192. nskb_frag->page_offset += offset - pos;
  3193. skb_frag_size_sub(nskb_frag, offset - pos);
  3194. }
  3195. skb_shinfo(nskb)->nr_frags++;
  3196. if (pos + size <= offset + len) {
  3197. i++;
  3198. frag++;
  3199. pos += size;
  3200. } else {
  3201. skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
  3202. goto skip_fraglist;
  3203. }
  3204. nskb_frag++;
  3205. }
  3206. skip_fraglist:
  3207. nskb->data_len = len - hsize;
  3208. nskb->len += nskb->data_len;
  3209. nskb->truesize += nskb->data_len;
  3210. perform_csum_check:
  3211. if (!csum) {
  3212. if (skb_has_shared_frag(nskb) &&
  3213. __skb_linearize(nskb))
  3214. goto err;
  3215. if (!nskb->remcsum_offload)
  3216. nskb->ip_summed = CHECKSUM_NONE;
  3217. SKB_GSO_CB(nskb)->csum =
  3218. skb_checksum(nskb, doffset,
  3219. nskb->len - doffset, 0);
  3220. SKB_GSO_CB(nskb)->csum_start =
  3221. skb_headroom(nskb) + doffset;
  3222. }
  3223. } while ((offset += len) < head_skb->len);
  3224. /* Some callers want to get the end of the list.
  3225. * Put it in segs->prev to avoid walking the list.
  3226. * (see validate_xmit_skb_list() for example)
  3227. */
  3228. segs->prev = tail;
  3229. if (partial_segs) {
  3230. struct sk_buff *iter;
  3231. int type = skb_shinfo(head_skb)->gso_type;
  3232. unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
  3233. /* Update type to add partial and then remove dodgy if set */
  3234. type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
  3235. type &= ~SKB_GSO_DODGY;
  3236. /* Update GSO info and prepare to start updating headers on
  3237. * our way back down the stack of protocols.
  3238. */
  3239. for (iter = segs; iter; iter = iter->next) {
  3240. skb_shinfo(iter)->gso_size = gso_size;
  3241. skb_shinfo(iter)->gso_segs = partial_segs;
  3242. skb_shinfo(iter)->gso_type = type;
  3243. SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
  3244. }
  3245. if (tail->len - doffset <= gso_size)
  3246. skb_shinfo(tail)->gso_size = 0;
  3247. else if (tail != segs)
  3248. skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
  3249. }
  3250. /* Following permits correct backpressure, for protocols
  3251. * using skb_set_owner_w().
  3252. * Idea is to tranfert ownership from head_skb to last segment.
  3253. */
  3254. if (head_skb->destructor == sock_wfree) {
  3255. swap(tail->truesize, head_skb->truesize);
  3256. swap(tail->destructor, head_skb->destructor);
  3257. swap(tail->sk, head_skb->sk);
  3258. }
  3259. return segs;
  3260. err:
  3261. kfree_skb_list(segs);
  3262. return ERR_PTR(err);
  3263. }
  3264. EXPORT_SYMBOL_GPL(skb_segment);
  3265. int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
  3266. {
  3267. struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
  3268. unsigned int offset = skb_gro_offset(skb);
  3269. unsigned int headlen = skb_headlen(skb);
  3270. unsigned int len = skb_gro_len(skb);
  3271. unsigned int delta_truesize;
  3272. struct sk_buff *lp;
  3273. if (unlikely(p->len + len >= 65536))
  3274. return -E2BIG;
  3275. lp = NAPI_GRO_CB(p)->last;
  3276. pinfo = skb_shinfo(lp);
  3277. if (headlen <= offset) {
  3278. skb_frag_t *frag;
  3279. skb_frag_t *frag2;
  3280. int i = skbinfo->nr_frags;
  3281. int nr_frags = pinfo->nr_frags + i;
  3282. if (nr_frags > MAX_SKB_FRAGS)
  3283. goto merge;
  3284. offset -= headlen;
  3285. pinfo->nr_frags = nr_frags;
  3286. skbinfo->nr_frags = 0;
  3287. frag = pinfo->frags + nr_frags;
  3288. frag2 = skbinfo->frags + i;
  3289. do {
  3290. *--frag = *--frag2;
  3291. } while (--i);
  3292. frag->page_offset += offset;
  3293. skb_frag_size_sub(frag, offset);
  3294. /* all fragments truesize : remove (head size + sk_buff) */
  3295. delta_truesize = skb->truesize -
  3296. SKB_TRUESIZE(skb_end_offset(skb));
  3297. skb->truesize -= skb->data_len;
  3298. skb->len -= skb->data_len;
  3299. skb->data_len = 0;
  3300. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  3301. goto done;
  3302. } else if (skb->head_frag) {
  3303. int nr_frags = pinfo->nr_frags;
  3304. skb_frag_t *frag = pinfo->frags + nr_frags;
  3305. struct page *page = virt_to_head_page(skb->head);
  3306. unsigned int first_size = headlen - offset;
  3307. unsigned int first_offset;
  3308. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  3309. goto merge;
  3310. first_offset = skb->data -
  3311. (unsigned char *)page_address(page) +
  3312. offset;
  3313. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  3314. frag->page.p = page;
  3315. frag->page_offset = first_offset;
  3316. skb_frag_size_set(frag, first_size);
  3317. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  3318. /* We dont need to clear skbinfo->nr_frags here */
  3319. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  3320. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  3321. goto done;
  3322. }
  3323. merge:
  3324. delta_truesize = skb->truesize;
  3325. if (offset > headlen) {
  3326. unsigned int eat = offset - headlen;
  3327. skbinfo->frags[0].page_offset += eat;
  3328. skb_frag_size_sub(&skbinfo->frags[0], eat);
  3329. skb->data_len -= eat;
  3330. skb->len -= eat;
  3331. offset = headlen;
  3332. }
  3333. __skb_pull(skb, offset);
  3334. if (NAPI_GRO_CB(p)->last == p)
  3335. skb_shinfo(p)->frag_list = skb;
  3336. else
  3337. NAPI_GRO_CB(p)->last->next = skb;
  3338. NAPI_GRO_CB(p)->last = skb;
  3339. __skb_header_release(skb);
  3340. lp = p;
  3341. done:
  3342. NAPI_GRO_CB(p)->count++;
  3343. p->data_len += len;
  3344. p->truesize += delta_truesize;
  3345. p->len += len;
  3346. if (lp != p) {
  3347. lp->data_len += len;
  3348. lp->truesize += delta_truesize;
  3349. lp->len += len;
  3350. }
  3351. NAPI_GRO_CB(skb)->same_flow = 1;
  3352. return 0;
  3353. }
  3354. EXPORT_SYMBOL_GPL(skb_gro_receive);
  3355. void __init skb_init(void)
  3356. {
  3357. skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
  3358. sizeof(struct sk_buff),
  3359. 0,
  3360. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  3361. offsetof(struct sk_buff, cb),
  3362. sizeof_field(struct sk_buff, cb),
  3363. NULL);
  3364. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  3365. sizeof(struct sk_buff_fclones),
  3366. 0,
  3367. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  3368. NULL);
  3369. }
  3370. static int
  3371. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
  3372. unsigned int recursion_level)
  3373. {
  3374. int start = skb_headlen(skb);
  3375. int i, copy = start - offset;
  3376. struct sk_buff *frag_iter;
  3377. int elt = 0;
  3378. if (unlikely(recursion_level >= 24))
  3379. return -EMSGSIZE;
  3380. if (copy > 0) {
  3381. if (copy > len)
  3382. copy = len;
  3383. sg_set_buf(sg, skb->data + offset, copy);
  3384. elt++;
  3385. if ((len -= copy) == 0)
  3386. return elt;
  3387. offset += copy;
  3388. }
  3389. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3390. int end;
  3391. WARN_ON(start > offset + len);
  3392. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  3393. if ((copy = end - offset) > 0) {
  3394. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  3395. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  3396. return -EMSGSIZE;
  3397. if (copy > len)
  3398. copy = len;
  3399. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  3400. frag->page_offset+offset-start);
  3401. elt++;
  3402. if (!(len -= copy))
  3403. return elt;
  3404. offset += copy;
  3405. }
  3406. start = end;
  3407. }
  3408. skb_walk_frags(skb, frag_iter) {
  3409. int end, ret;
  3410. WARN_ON(start > offset + len);
  3411. end = start + frag_iter->len;
  3412. if ((copy = end - offset) > 0) {
  3413. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  3414. return -EMSGSIZE;
  3415. if (copy > len)
  3416. copy = len;
  3417. ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  3418. copy, recursion_level + 1);
  3419. if (unlikely(ret < 0))
  3420. return ret;
  3421. elt += ret;
  3422. if ((len -= copy) == 0)
  3423. return elt;
  3424. offset += copy;
  3425. }
  3426. start = end;
  3427. }
  3428. BUG_ON(len);
  3429. return elt;
  3430. }
  3431. /**
  3432. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  3433. * @skb: Socket buffer containing the buffers to be mapped
  3434. * @sg: The scatter-gather list to map into
  3435. * @offset: The offset into the buffer's contents to start mapping
  3436. * @len: Length of buffer space to be mapped
  3437. *
  3438. * Fill the specified scatter-gather list with mappings/pointers into a
  3439. * region of the buffer space attached to a socket buffer. Returns either
  3440. * the number of scatterlist items used, or -EMSGSIZE if the contents
  3441. * could not fit.
  3442. */
  3443. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  3444. {
  3445. int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
  3446. if (nsg <= 0)
  3447. return nsg;
  3448. sg_mark_end(&sg[nsg - 1]);
  3449. return nsg;
  3450. }
  3451. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  3452. /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
  3453. * sglist without mark the sg which contain last skb data as the end.
  3454. * So the caller can mannipulate sg list as will when padding new data after
  3455. * the first call without calling sg_unmark_end to expend sg list.
  3456. *
  3457. * Scenario to use skb_to_sgvec_nomark:
  3458. * 1. sg_init_table
  3459. * 2. skb_to_sgvec_nomark(payload1)
  3460. * 3. skb_to_sgvec_nomark(payload2)
  3461. *
  3462. * This is equivalent to:
  3463. * 1. sg_init_table
  3464. * 2. skb_to_sgvec(payload1)
  3465. * 3. sg_unmark_end
  3466. * 4. skb_to_sgvec(payload2)
  3467. *
  3468. * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
  3469. * is more preferable.
  3470. */
  3471. int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  3472. int offset, int len)
  3473. {
  3474. return __skb_to_sgvec(skb, sg, offset, len, 0);
  3475. }
  3476. EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
  3477. /**
  3478. * skb_cow_data - Check that a socket buffer's data buffers are writable
  3479. * @skb: The socket buffer to check.
  3480. * @tailbits: Amount of trailing space to be added
  3481. * @trailer: Returned pointer to the skb where the @tailbits space begins
  3482. *
  3483. * Make sure that the data buffers attached to a socket buffer are
  3484. * writable. If they are not, private copies are made of the data buffers
  3485. * and the socket buffer is set to use these instead.
  3486. *
  3487. * If @tailbits is given, make sure that there is space to write @tailbits
  3488. * bytes of data beyond current end of socket buffer. @trailer will be
  3489. * set to point to the skb in which this space begins.
  3490. *
  3491. * The number of scatterlist elements required to completely map the
  3492. * COW'd and extended socket buffer will be returned.
  3493. */
  3494. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  3495. {
  3496. int copyflag;
  3497. int elt;
  3498. struct sk_buff *skb1, **skb_p;
  3499. /* If skb is cloned or its head is paged, reallocate
  3500. * head pulling out all the pages (pages are considered not writable
  3501. * at the moment even if they are anonymous).
  3502. */
  3503. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  3504. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  3505. return -ENOMEM;
  3506. /* Easy case. Most of packets will go this way. */
  3507. if (!skb_has_frag_list(skb)) {
  3508. /* A little of trouble, not enough of space for trailer.
  3509. * This should not happen, when stack is tuned to generate
  3510. * good frames. OK, on miss we reallocate and reserve even more
  3511. * space, 128 bytes is fair. */
  3512. if (skb_tailroom(skb) < tailbits &&
  3513. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  3514. return -ENOMEM;
  3515. /* Voila! */
  3516. *trailer = skb;
  3517. return 1;
  3518. }
  3519. /* Misery. We are in troubles, going to mincer fragments... */
  3520. elt = 1;
  3521. skb_p = &skb_shinfo(skb)->frag_list;
  3522. copyflag = 0;
  3523. while ((skb1 = *skb_p) != NULL) {
  3524. int ntail = 0;
  3525. /* The fragment is partially pulled by someone,
  3526. * this can happen on input. Copy it and everything
  3527. * after it. */
  3528. if (skb_shared(skb1))
  3529. copyflag = 1;
  3530. /* If the skb is the last, worry about trailer. */
  3531. if (skb1->next == NULL && tailbits) {
  3532. if (skb_shinfo(skb1)->nr_frags ||
  3533. skb_has_frag_list(skb1) ||
  3534. skb_tailroom(skb1) < tailbits)
  3535. ntail = tailbits + 128;
  3536. }
  3537. if (copyflag ||
  3538. skb_cloned(skb1) ||
  3539. ntail ||
  3540. skb_shinfo(skb1)->nr_frags ||
  3541. skb_has_frag_list(skb1)) {
  3542. struct sk_buff *skb2;
  3543. /* Fuck, we are miserable poor guys... */
  3544. if (ntail == 0)
  3545. skb2 = skb_copy(skb1, GFP_ATOMIC);
  3546. else
  3547. skb2 = skb_copy_expand(skb1,
  3548. skb_headroom(skb1),
  3549. ntail,
  3550. GFP_ATOMIC);
  3551. if (unlikely(skb2 == NULL))
  3552. return -ENOMEM;
  3553. if (skb1->sk)
  3554. skb_set_owner_w(skb2, skb1->sk);
  3555. /* Looking around. Are we still alive?
  3556. * OK, link new skb, drop old one */
  3557. skb2->next = skb1->next;
  3558. *skb_p = skb2;
  3559. kfree_skb(skb1);
  3560. skb1 = skb2;
  3561. }
  3562. elt++;
  3563. *trailer = skb1;
  3564. skb_p = &skb1->next;
  3565. }
  3566. return elt;
  3567. }
  3568. EXPORT_SYMBOL_GPL(skb_cow_data);
  3569. static void sock_rmem_free(struct sk_buff *skb)
  3570. {
  3571. struct sock *sk = skb->sk;
  3572. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  3573. }
  3574. static void skb_set_err_queue(struct sk_buff *skb)
  3575. {
  3576. /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
  3577. * So, it is safe to (mis)use it to mark skbs on the error queue.
  3578. */
  3579. skb->pkt_type = PACKET_OUTGOING;
  3580. BUILD_BUG_ON(PACKET_OUTGOING == 0);
  3581. }
  3582. /*
  3583. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  3584. */
  3585. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  3586. {
  3587. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  3588. (unsigned int)sk->sk_rcvbuf)
  3589. return -ENOMEM;
  3590. skb_orphan(skb);
  3591. skb->sk = sk;
  3592. skb->destructor = sock_rmem_free;
  3593. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  3594. skb_set_err_queue(skb);
  3595. /* before exiting rcu section, make sure dst is refcounted */
  3596. skb_dst_force(skb);
  3597. skb_queue_tail(&sk->sk_error_queue, skb);
  3598. if (!sock_flag(sk, SOCK_DEAD))
  3599. sk->sk_error_report(sk);
  3600. return 0;
  3601. }
  3602. EXPORT_SYMBOL(sock_queue_err_skb);
  3603. static bool is_icmp_err_skb(const struct sk_buff *skb)
  3604. {
  3605. return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  3606. SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
  3607. }
  3608. struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
  3609. {
  3610. struct sk_buff_head *q = &sk->sk_error_queue;
  3611. struct sk_buff *skb, *skb_next = NULL;
  3612. bool icmp_next = false;
  3613. unsigned long flags;
  3614. spin_lock_irqsave(&q->lock, flags);
  3615. skb = __skb_dequeue(q);
  3616. if (skb && (skb_next = skb_peek(q))) {
  3617. icmp_next = is_icmp_err_skb(skb_next);
  3618. if (icmp_next)
  3619. sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
  3620. }
  3621. spin_unlock_irqrestore(&q->lock, flags);
  3622. if (is_icmp_err_skb(skb) && !icmp_next)
  3623. sk->sk_err = 0;
  3624. if (skb_next)
  3625. sk->sk_error_report(sk);
  3626. return skb;
  3627. }
  3628. EXPORT_SYMBOL(sock_dequeue_err_skb);
  3629. /**
  3630. * skb_clone_sk - create clone of skb, and take reference to socket
  3631. * @skb: the skb to clone
  3632. *
  3633. * This function creates a clone of a buffer that holds a reference on
  3634. * sk_refcnt. Buffers created via this function are meant to be
  3635. * returned using sock_queue_err_skb, or free via kfree_skb.
  3636. *
  3637. * When passing buffers allocated with this function to sock_queue_err_skb
  3638. * it is necessary to wrap the call with sock_hold/sock_put in order to
  3639. * prevent the socket from being released prior to being enqueued on
  3640. * the sk_error_queue.
  3641. */
  3642. struct sk_buff *skb_clone_sk(struct sk_buff *skb)
  3643. {
  3644. struct sock *sk = skb->sk;
  3645. struct sk_buff *clone;
  3646. if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
  3647. return NULL;
  3648. clone = skb_clone(skb, GFP_ATOMIC);
  3649. if (!clone) {
  3650. sock_put(sk);
  3651. return NULL;
  3652. }
  3653. clone->sk = sk;
  3654. clone->destructor = sock_efree;
  3655. return clone;
  3656. }
  3657. EXPORT_SYMBOL(skb_clone_sk);
  3658. static void __skb_complete_tx_timestamp(struct sk_buff *skb,
  3659. struct sock *sk,
  3660. int tstype,
  3661. bool opt_stats)
  3662. {
  3663. struct sock_exterr_skb *serr;
  3664. int err;
  3665. BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
  3666. serr = SKB_EXT_ERR(skb);
  3667. memset(serr, 0, sizeof(*serr));
  3668. serr->ee.ee_errno = ENOMSG;
  3669. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  3670. serr->ee.ee_info = tstype;
  3671. serr->opt_stats = opt_stats;
  3672. serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
  3673. if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
  3674. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  3675. if (sk->sk_protocol == IPPROTO_TCP &&
  3676. sk->sk_type == SOCK_STREAM)
  3677. serr->ee.ee_data -= sk->sk_tskey;
  3678. }
  3679. err = sock_queue_err_skb(sk, skb);
  3680. if (err)
  3681. kfree_skb(skb);
  3682. }
  3683. static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
  3684. {
  3685. bool ret;
  3686. if (likely(sysctl_tstamp_allow_data || tsonly))
  3687. return true;
  3688. read_lock_bh(&sk->sk_callback_lock);
  3689. ret = sk->sk_socket && sk->sk_socket->file &&
  3690. file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
  3691. read_unlock_bh(&sk->sk_callback_lock);
  3692. return ret;
  3693. }
  3694. void skb_complete_tx_timestamp(struct sk_buff *skb,
  3695. struct skb_shared_hwtstamps *hwtstamps)
  3696. {
  3697. struct sock *sk = skb->sk;
  3698. if (!skb_may_tx_timestamp(sk, false))
  3699. goto err;
  3700. /* Take a reference to prevent skb_orphan() from freeing the socket,
  3701. * but only if the socket refcount is not zero.
  3702. */
  3703. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  3704. *skb_hwtstamps(skb) = *hwtstamps;
  3705. __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
  3706. sock_put(sk);
  3707. return;
  3708. }
  3709. err:
  3710. kfree_skb(skb);
  3711. }
  3712. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  3713. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  3714. struct skb_shared_hwtstamps *hwtstamps,
  3715. struct sock *sk, int tstype)
  3716. {
  3717. struct sk_buff *skb;
  3718. bool tsonly, opt_stats = false;
  3719. if (!sk)
  3720. return;
  3721. if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
  3722. skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
  3723. return;
  3724. tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
  3725. if (!skb_may_tx_timestamp(sk, tsonly))
  3726. return;
  3727. if (tsonly) {
  3728. #ifdef CONFIG_INET
  3729. if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
  3730. sk->sk_protocol == IPPROTO_TCP &&
  3731. sk->sk_type == SOCK_STREAM) {
  3732. skb = tcp_get_timestamping_opt_stats(sk);
  3733. opt_stats = true;
  3734. } else
  3735. #endif
  3736. skb = alloc_skb(0, GFP_ATOMIC);
  3737. } else {
  3738. skb = skb_clone(orig_skb, GFP_ATOMIC);
  3739. }
  3740. if (!skb)
  3741. return;
  3742. if (tsonly) {
  3743. skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
  3744. SKBTX_ANY_TSTAMP;
  3745. skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
  3746. }
  3747. if (hwtstamps)
  3748. *skb_hwtstamps(skb) = *hwtstamps;
  3749. else
  3750. skb->tstamp = ktime_get_real();
  3751. __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
  3752. }
  3753. EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
  3754. void skb_tstamp_tx(struct sk_buff *orig_skb,
  3755. struct skb_shared_hwtstamps *hwtstamps)
  3756. {
  3757. return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
  3758. SCM_TSTAMP_SND);
  3759. }
  3760. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  3761. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  3762. {
  3763. struct sock *sk = skb->sk;
  3764. struct sock_exterr_skb *serr;
  3765. int err = 1;
  3766. skb->wifi_acked_valid = 1;
  3767. skb->wifi_acked = acked;
  3768. serr = SKB_EXT_ERR(skb);
  3769. memset(serr, 0, sizeof(*serr));
  3770. serr->ee.ee_errno = ENOMSG;
  3771. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  3772. /* Take a reference to prevent skb_orphan() from freeing the socket,
  3773. * but only if the socket refcount is not zero.
  3774. */
  3775. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  3776. err = sock_queue_err_skb(sk, skb);
  3777. sock_put(sk);
  3778. }
  3779. if (err)
  3780. kfree_skb(skb);
  3781. }
  3782. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  3783. /**
  3784. * skb_partial_csum_set - set up and verify partial csum values for packet
  3785. * @skb: the skb to set
  3786. * @start: the number of bytes after skb->data to start checksumming.
  3787. * @off: the offset from start to place the checksum.
  3788. *
  3789. * For untrusted partially-checksummed packets, we need to make sure the values
  3790. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  3791. *
  3792. * This function checks and sets those values and skb->ip_summed: if this
  3793. * returns false you should drop the packet.
  3794. */
  3795. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  3796. {
  3797. u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
  3798. u32 csum_start = skb_headroom(skb) + (u32)start;
  3799. if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
  3800. net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
  3801. start, off, skb_headroom(skb), skb_headlen(skb));
  3802. return false;
  3803. }
  3804. skb->ip_summed = CHECKSUM_PARTIAL;
  3805. skb->csum_start = csum_start;
  3806. skb->csum_offset = off;
  3807. skb_set_transport_header(skb, start);
  3808. return true;
  3809. }
  3810. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  3811. static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  3812. unsigned int max)
  3813. {
  3814. if (skb_headlen(skb) >= len)
  3815. return 0;
  3816. /* If we need to pullup then pullup to the max, so we
  3817. * won't need to do it again.
  3818. */
  3819. if (max > skb->len)
  3820. max = skb->len;
  3821. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  3822. return -ENOMEM;
  3823. if (skb_headlen(skb) < len)
  3824. return -EPROTO;
  3825. return 0;
  3826. }
  3827. #define MAX_TCP_HDR_LEN (15 * 4)
  3828. static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
  3829. typeof(IPPROTO_IP) proto,
  3830. unsigned int off)
  3831. {
  3832. switch (proto) {
  3833. int err;
  3834. case IPPROTO_TCP:
  3835. err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
  3836. off + MAX_TCP_HDR_LEN);
  3837. if (!err && !skb_partial_csum_set(skb, off,
  3838. offsetof(struct tcphdr,
  3839. check)))
  3840. err = -EPROTO;
  3841. return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
  3842. case IPPROTO_UDP:
  3843. err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
  3844. off + sizeof(struct udphdr));
  3845. if (!err && !skb_partial_csum_set(skb, off,
  3846. offsetof(struct udphdr,
  3847. check)))
  3848. err = -EPROTO;
  3849. return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
  3850. }
  3851. return ERR_PTR(-EPROTO);
  3852. }
  3853. /* This value should be large enough to cover a tagged ethernet header plus
  3854. * maximally sized IP and TCP or UDP headers.
  3855. */
  3856. #define MAX_IP_HDR_LEN 128
  3857. static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
  3858. {
  3859. unsigned int off;
  3860. bool fragment;
  3861. __sum16 *csum;
  3862. int err;
  3863. fragment = false;
  3864. err = skb_maybe_pull_tail(skb,
  3865. sizeof(struct iphdr),
  3866. MAX_IP_HDR_LEN);
  3867. if (err < 0)
  3868. goto out;
  3869. if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
  3870. fragment = true;
  3871. off = ip_hdrlen(skb);
  3872. err = -EPROTO;
  3873. if (fragment)
  3874. goto out;
  3875. csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
  3876. if (IS_ERR(csum))
  3877. return PTR_ERR(csum);
  3878. if (recalculate)
  3879. *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  3880. ip_hdr(skb)->daddr,
  3881. skb->len - off,
  3882. ip_hdr(skb)->protocol, 0);
  3883. err = 0;
  3884. out:
  3885. return err;
  3886. }
  3887. /* This value should be large enough to cover a tagged ethernet header plus
  3888. * an IPv6 header, all options, and a maximal TCP or UDP header.
  3889. */
  3890. #define MAX_IPV6_HDR_LEN 256
  3891. #define OPT_HDR(type, skb, off) \
  3892. (type *)(skb_network_header(skb) + (off))
  3893. static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
  3894. {
  3895. int err;
  3896. u8 nexthdr;
  3897. unsigned int off;
  3898. unsigned int len;
  3899. bool fragment;
  3900. bool done;
  3901. __sum16 *csum;
  3902. fragment = false;
  3903. done = false;
  3904. off = sizeof(struct ipv6hdr);
  3905. err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  3906. if (err < 0)
  3907. goto out;
  3908. nexthdr = ipv6_hdr(skb)->nexthdr;
  3909. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  3910. while (off <= len && !done) {
  3911. switch (nexthdr) {
  3912. case IPPROTO_DSTOPTS:
  3913. case IPPROTO_HOPOPTS:
  3914. case IPPROTO_ROUTING: {
  3915. struct ipv6_opt_hdr *hp;
  3916. err = skb_maybe_pull_tail(skb,
  3917. off +
  3918. sizeof(struct ipv6_opt_hdr),
  3919. MAX_IPV6_HDR_LEN);
  3920. if (err < 0)
  3921. goto out;
  3922. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  3923. nexthdr = hp->nexthdr;
  3924. off += ipv6_optlen(hp);
  3925. break;
  3926. }
  3927. case IPPROTO_AH: {
  3928. struct ip_auth_hdr *hp;
  3929. err = skb_maybe_pull_tail(skb,
  3930. off +
  3931. sizeof(struct ip_auth_hdr),
  3932. MAX_IPV6_HDR_LEN);
  3933. if (err < 0)
  3934. goto out;
  3935. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  3936. nexthdr = hp->nexthdr;
  3937. off += ipv6_authlen(hp);
  3938. break;
  3939. }
  3940. case IPPROTO_FRAGMENT: {
  3941. struct frag_hdr *hp;
  3942. err = skb_maybe_pull_tail(skb,
  3943. off +
  3944. sizeof(struct frag_hdr),
  3945. MAX_IPV6_HDR_LEN);
  3946. if (err < 0)
  3947. goto out;
  3948. hp = OPT_HDR(struct frag_hdr, skb, off);
  3949. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  3950. fragment = true;
  3951. nexthdr = hp->nexthdr;
  3952. off += sizeof(struct frag_hdr);
  3953. break;
  3954. }
  3955. default:
  3956. done = true;
  3957. break;
  3958. }
  3959. }
  3960. err = -EPROTO;
  3961. if (!done || fragment)
  3962. goto out;
  3963. csum = skb_checksum_setup_ip(skb, nexthdr, off);
  3964. if (IS_ERR(csum))
  3965. return PTR_ERR(csum);
  3966. if (recalculate)
  3967. *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  3968. &ipv6_hdr(skb)->daddr,
  3969. skb->len - off, nexthdr, 0);
  3970. err = 0;
  3971. out:
  3972. return err;
  3973. }
  3974. /**
  3975. * skb_checksum_setup - set up partial checksum offset
  3976. * @skb: the skb to set up
  3977. * @recalculate: if true the pseudo-header checksum will be recalculated
  3978. */
  3979. int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
  3980. {
  3981. int err;
  3982. switch (skb->protocol) {
  3983. case htons(ETH_P_IP):
  3984. err = skb_checksum_setup_ipv4(skb, recalculate);
  3985. break;
  3986. case htons(ETH_P_IPV6):
  3987. err = skb_checksum_setup_ipv6(skb, recalculate);
  3988. break;
  3989. default:
  3990. err = -EPROTO;
  3991. break;
  3992. }
  3993. return err;
  3994. }
  3995. EXPORT_SYMBOL(skb_checksum_setup);
  3996. /**
  3997. * skb_checksum_maybe_trim - maybe trims the given skb
  3998. * @skb: the skb to check
  3999. * @transport_len: the data length beyond the network header
  4000. *
  4001. * Checks whether the given skb has data beyond the given transport length.
  4002. * If so, returns a cloned skb trimmed to this transport length.
  4003. * Otherwise returns the provided skb. Returns NULL in error cases
  4004. * (e.g. transport_len exceeds skb length or out-of-memory).
  4005. *
  4006. * Caller needs to set the skb transport header and free any returned skb if it
  4007. * differs from the provided skb.
  4008. */
  4009. static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
  4010. unsigned int transport_len)
  4011. {
  4012. struct sk_buff *skb_chk;
  4013. unsigned int len = skb_transport_offset(skb) + transport_len;
  4014. int ret;
  4015. if (skb->len < len)
  4016. return NULL;
  4017. else if (skb->len == len)
  4018. return skb;
  4019. skb_chk = skb_clone(skb, GFP_ATOMIC);
  4020. if (!skb_chk)
  4021. return NULL;
  4022. ret = pskb_trim_rcsum(skb_chk, len);
  4023. if (ret) {
  4024. kfree_skb(skb_chk);
  4025. return NULL;
  4026. }
  4027. return skb_chk;
  4028. }
  4029. /**
  4030. * skb_checksum_trimmed - validate checksum of an skb
  4031. * @skb: the skb to check
  4032. * @transport_len: the data length beyond the network header
  4033. * @skb_chkf: checksum function to use
  4034. *
  4035. * Applies the given checksum function skb_chkf to the provided skb.
  4036. * Returns a checked and maybe trimmed skb. Returns NULL on error.
  4037. *
  4038. * If the skb has data beyond the given transport length, then a
  4039. * trimmed & cloned skb is checked and returned.
  4040. *
  4041. * Caller needs to set the skb transport header and free any returned skb if it
  4042. * differs from the provided skb.
  4043. */
  4044. struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
  4045. unsigned int transport_len,
  4046. __sum16(*skb_chkf)(struct sk_buff *skb))
  4047. {
  4048. struct sk_buff *skb_chk;
  4049. unsigned int offset = skb_transport_offset(skb);
  4050. __sum16 ret;
  4051. skb_chk = skb_checksum_maybe_trim(skb, transport_len);
  4052. if (!skb_chk)
  4053. goto err;
  4054. if (!pskb_may_pull(skb_chk, offset))
  4055. goto err;
  4056. skb_pull_rcsum(skb_chk, offset);
  4057. ret = skb_chkf(skb_chk);
  4058. skb_push_rcsum(skb_chk, offset);
  4059. if (ret)
  4060. goto err;
  4061. return skb_chk;
  4062. err:
  4063. if (skb_chk && skb_chk != skb)
  4064. kfree_skb(skb_chk);
  4065. return NULL;
  4066. }
  4067. EXPORT_SYMBOL(skb_checksum_trimmed);
  4068. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  4069. {
  4070. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  4071. skb->dev->name);
  4072. }
  4073. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  4074. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  4075. {
  4076. if (head_stolen) {
  4077. skb_release_head_state(skb);
  4078. kmem_cache_free(skbuff_head_cache, skb);
  4079. } else {
  4080. __kfree_skb(skb);
  4081. }
  4082. }
  4083. EXPORT_SYMBOL(kfree_skb_partial);
  4084. /**
  4085. * skb_try_coalesce - try to merge skb to prior one
  4086. * @to: prior buffer
  4087. * @from: buffer to add
  4088. * @fragstolen: pointer to boolean
  4089. * @delta_truesize: how much more was allocated than was requested
  4090. */
  4091. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  4092. bool *fragstolen, int *delta_truesize)
  4093. {
  4094. struct skb_shared_info *to_shinfo, *from_shinfo;
  4095. int i, delta, len = from->len;
  4096. *fragstolen = false;
  4097. if (skb_cloned(to))
  4098. return false;
  4099. if (len <= skb_tailroom(to)) {
  4100. if (len)
  4101. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  4102. *delta_truesize = 0;
  4103. return true;
  4104. }
  4105. to_shinfo = skb_shinfo(to);
  4106. from_shinfo = skb_shinfo(from);
  4107. if (to_shinfo->frag_list || from_shinfo->frag_list)
  4108. return false;
  4109. if (skb_zcopy(to) || skb_zcopy(from))
  4110. return false;
  4111. if (skb_headlen(from) != 0) {
  4112. struct page *page;
  4113. unsigned int offset;
  4114. if (to_shinfo->nr_frags +
  4115. from_shinfo->nr_frags >= MAX_SKB_FRAGS)
  4116. return false;
  4117. if (skb_head_is_locked(from))
  4118. return false;
  4119. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  4120. page = virt_to_head_page(from->head);
  4121. offset = from->data - (unsigned char *)page_address(page);
  4122. skb_fill_page_desc(to, to_shinfo->nr_frags,
  4123. page, offset, skb_headlen(from));
  4124. *fragstolen = true;
  4125. } else {
  4126. if (to_shinfo->nr_frags +
  4127. from_shinfo->nr_frags > MAX_SKB_FRAGS)
  4128. return false;
  4129. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  4130. }
  4131. WARN_ON_ONCE(delta < len);
  4132. memcpy(to_shinfo->frags + to_shinfo->nr_frags,
  4133. from_shinfo->frags,
  4134. from_shinfo->nr_frags * sizeof(skb_frag_t));
  4135. to_shinfo->nr_frags += from_shinfo->nr_frags;
  4136. if (!skb_cloned(from))
  4137. from_shinfo->nr_frags = 0;
  4138. /* if the skb is not cloned this does nothing
  4139. * since we set nr_frags to 0.
  4140. */
  4141. for (i = 0; i < from_shinfo->nr_frags; i++)
  4142. __skb_frag_ref(&from_shinfo->frags[i]);
  4143. to->truesize += delta;
  4144. to->len += len;
  4145. to->data_len += len;
  4146. *delta_truesize = delta;
  4147. return true;
  4148. }
  4149. EXPORT_SYMBOL(skb_try_coalesce);
  4150. /**
  4151. * skb_scrub_packet - scrub an skb
  4152. *
  4153. * @skb: buffer to clean
  4154. * @xnet: packet is crossing netns
  4155. *
  4156. * skb_scrub_packet can be used after encapsulating or decapsulting a packet
  4157. * into/from a tunnel. Some information have to be cleared during these
  4158. * operations.
  4159. * skb_scrub_packet can also be used to clean a skb before injecting it in
  4160. * another namespace (@xnet == true). We have to clear all information in the
  4161. * skb that could impact namespace isolation.
  4162. */
  4163. void skb_scrub_packet(struct sk_buff *skb, bool xnet)
  4164. {
  4165. skb->pkt_type = PACKET_HOST;
  4166. skb->skb_iif = 0;
  4167. skb->ignore_df = 0;
  4168. skb_dst_drop(skb);
  4169. secpath_reset(skb);
  4170. nf_reset(skb);
  4171. nf_reset_trace(skb);
  4172. #ifdef CONFIG_NET_SWITCHDEV
  4173. skb->offload_fwd_mark = 0;
  4174. skb->offload_mr_fwd_mark = 0;
  4175. #endif
  4176. if (!xnet)
  4177. return;
  4178. ipvs_reset(skb);
  4179. skb->mark = 0;
  4180. skb->tstamp = 0;
  4181. }
  4182. EXPORT_SYMBOL_GPL(skb_scrub_packet);
  4183. /**
  4184. * skb_gso_transport_seglen - Return length of individual segments of a gso packet
  4185. *
  4186. * @skb: GSO skb
  4187. *
  4188. * skb_gso_transport_seglen is used to determine the real size of the
  4189. * individual segments, including Layer4 headers (TCP/UDP).
  4190. *
  4191. * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
  4192. */
  4193. static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
  4194. {
  4195. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  4196. unsigned int thlen = 0;
  4197. if (skb->encapsulation) {
  4198. thlen = skb_inner_transport_header(skb) -
  4199. skb_transport_header(skb);
  4200. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  4201. thlen += inner_tcp_hdrlen(skb);
  4202. } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  4203. thlen = tcp_hdrlen(skb);
  4204. } else if (unlikely(skb_is_gso_sctp(skb))) {
  4205. thlen = sizeof(struct sctphdr);
  4206. } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
  4207. thlen = sizeof(struct udphdr);
  4208. }
  4209. /* UFO sets gso_size to the size of the fragmentation
  4210. * payload, i.e. the size of the L4 (UDP) header is already
  4211. * accounted for.
  4212. */
  4213. return thlen + shinfo->gso_size;
  4214. }
  4215. /**
  4216. * skb_gso_network_seglen - Return length of individual segments of a gso packet
  4217. *
  4218. * @skb: GSO skb
  4219. *
  4220. * skb_gso_network_seglen is used to determine the real size of the
  4221. * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
  4222. *
  4223. * The MAC/L2 header is not accounted for.
  4224. */
  4225. static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
  4226. {
  4227. unsigned int hdr_len = skb_transport_header(skb) -
  4228. skb_network_header(skb);
  4229. return hdr_len + skb_gso_transport_seglen(skb);
  4230. }
  4231. /**
  4232. * skb_gso_mac_seglen - Return length of individual segments of a gso packet
  4233. *
  4234. * @skb: GSO skb
  4235. *
  4236. * skb_gso_mac_seglen is used to determine the real size of the
  4237. * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
  4238. * headers (TCP/UDP).
  4239. */
  4240. static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
  4241. {
  4242. unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  4243. return hdr_len + skb_gso_transport_seglen(skb);
  4244. }
  4245. /**
  4246. * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
  4247. *
  4248. * There are a couple of instances where we have a GSO skb, and we
  4249. * want to determine what size it would be after it is segmented.
  4250. *
  4251. * We might want to check:
  4252. * - L3+L4+payload size (e.g. IP forwarding)
  4253. * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
  4254. *
  4255. * This is a helper to do that correctly considering GSO_BY_FRAGS.
  4256. *
  4257. * @seg_len: The segmented length (from skb_gso_*_seglen). In the
  4258. * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
  4259. *
  4260. * @max_len: The maximum permissible length.
  4261. *
  4262. * Returns true if the segmented length <= max length.
  4263. */
  4264. static inline bool skb_gso_size_check(const struct sk_buff *skb,
  4265. unsigned int seg_len,
  4266. unsigned int max_len) {
  4267. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  4268. const struct sk_buff *iter;
  4269. if (shinfo->gso_size != GSO_BY_FRAGS)
  4270. return seg_len <= max_len;
  4271. /* Undo this so we can re-use header sizes */
  4272. seg_len -= GSO_BY_FRAGS;
  4273. skb_walk_frags(skb, iter) {
  4274. if (seg_len + skb_headlen(iter) > max_len)
  4275. return false;
  4276. }
  4277. return true;
  4278. }
  4279. /**
  4280. * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
  4281. *
  4282. * @skb: GSO skb
  4283. * @mtu: MTU to validate against
  4284. *
  4285. * skb_gso_validate_network_len validates if a given skb will fit a
  4286. * wanted MTU once split. It considers L3 headers, L4 headers, and the
  4287. * payload.
  4288. */
  4289. bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
  4290. {
  4291. return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
  4292. }
  4293. EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
  4294. /**
  4295. * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
  4296. *
  4297. * @skb: GSO skb
  4298. * @len: length to validate against
  4299. *
  4300. * skb_gso_validate_mac_len validates if a given skb will fit a wanted
  4301. * length once split, including L2, L3 and L4 headers and the payload.
  4302. */
  4303. bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
  4304. {
  4305. return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
  4306. }
  4307. EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
  4308. static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
  4309. {
  4310. int mac_len;
  4311. if (skb_cow(skb, skb_headroom(skb)) < 0) {
  4312. kfree_skb(skb);
  4313. return NULL;
  4314. }
  4315. mac_len = skb->data - skb_mac_header(skb);
  4316. if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
  4317. memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
  4318. mac_len - VLAN_HLEN - ETH_TLEN);
  4319. }
  4320. skb->mac_header += VLAN_HLEN;
  4321. return skb;
  4322. }
  4323. struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
  4324. {
  4325. struct vlan_hdr *vhdr;
  4326. u16 vlan_tci;
  4327. if (unlikely(skb_vlan_tag_present(skb))) {
  4328. /* vlan_tci is already set-up so leave this for another time */
  4329. return skb;
  4330. }
  4331. skb = skb_share_check(skb, GFP_ATOMIC);
  4332. if (unlikely(!skb))
  4333. goto err_free;
  4334. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  4335. goto err_free;
  4336. vhdr = (struct vlan_hdr *)skb->data;
  4337. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  4338. __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
  4339. skb_pull_rcsum(skb, VLAN_HLEN);
  4340. vlan_set_encap_proto(skb, vhdr);
  4341. skb = skb_reorder_vlan_header(skb);
  4342. if (unlikely(!skb))
  4343. goto err_free;
  4344. skb_reset_network_header(skb);
  4345. skb_reset_transport_header(skb);
  4346. skb_reset_mac_len(skb);
  4347. return skb;
  4348. err_free:
  4349. kfree_skb(skb);
  4350. return NULL;
  4351. }
  4352. EXPORT_SYMBOL(skb_vlan_untag);
  4353. int skb_ensure_writable(struct sk_buff *skb, int write_len)
  4354. {
  4355. if (!pskb_may_pull(skb, write_len))
  4356. return -ENOMEM;
  4357. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  4358. return 0;
  4359. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  4360. }
  4361. EXPORT_SYMBOL(skb_ensure_writable);
  4362. /* remove VLAN header from packet and update csum accordingly.
  4363. * expects a non skb_vlan_tag_present skb with a vlan tag payload
  4364. */
  4365. int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
  4366. {
  4367. struct vlan_hdr *vhdr;
  4368. int offset = skb->data - skb_mac_header(skb);
  4369. int err;
  4370. if (WARN_ONCE(offset,
  4371. "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
  4372. offset)) {
  4373. return -EINVAL;
  4374. }
  4375. err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
  4376. if (unlikely(err))
  4377. return err;
  4378. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  4379. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  4380. *vlan_tci = ntohs(vhdr->h_vlan_TCI);
  4381. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  4382. __skb_pull(skb, VLAN_HLEN);
  4383. vlan_set_encap_proto(skb, vhdr);
  4384. skb->mac_header += VLAN_HLEN;
  4385. if (skb_network_offset(skb) < ETH_HLEN)
  4386. skb_set_network_header(skb, ETH_HLEN);
  4387. skb_reset_mac_len(skb);
  4388. return err;
  4389. }
  4390. EXPORT_SYMBOL(__skb_vlan_pop);
  4391. /* Pop a vlan tag either from hwaccel or from payload.
  4392. * Expects skb->data at mac header.
  4393. */
  4394. int skb_vlan_pop(struct sk_buff *skb)
  4395. {
  4396. u16 vlan_tci;
  4397. __be16 vlan_proto;
  4398. int err;
  4399. if (likely(skb_vlan_tag_present(skb))) {
  4400. skb->vlan_tci = 0;
  4401. } else {
  4402. if (unlikely(!eth_type_vlan(skb->protocol)))
  4403. return 0;
  4404. err = __skb_vlan_pop(skb, &vlan_tci);
  4405. if (err)
  4406. return err;
  4407. }
  4408. /* move next vlan tag to hw accel tag */
  4409. if (likely(!eth_type_vlan(skb->protocol)))
  4410. return 0;
  4411. vlan_proto = skb->protocol;
  4412. err = __skb_vlan_pop(skb, &vlan_tci);
  4413. if (unlikely(err))
  4414. return err;
  4415. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  4416. return 0;
  4417. }
  4418. EXPORT_SYMBOL(skb_vlan_pop);
  4419. /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
  4420. * Expects skb->data at mac header.
  4421. */
  4422. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  4423. {
  4424. if (skb_vlan_tag_present(skb)) {
  4425. int offset = skb->data - skb_mac_header(skb);
  4426. int err;
  4427. if (WARN_ONCE(offset,
  4428. "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
  4429. offset)) {
  4430. return -EINVAL;
  4431. }
  4432. err = __vlan_insert_tag(skb, skb->vlan_proto,
  4433. skb_vlan_tag_get(skb));
  4434. if (err)
  4435. return err;
  4436. skb->protocol = skb->vlan_proto;
  4437. skb->mac_len += VLAN_HLEN;
  4438. skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  4439. }
  4440. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  4441. return 0;
  4442. }
  4443. EXPORT_SYMBOL(skb_vlan_push);
  4444. /**
  4445. * alloc_skb_with_frags - allocate skb with page frags
  4446. *
  4447. * @header_len: size of linear part
  4448. * @data_len: needed length in frags
  4449. * @max_page_order: max page order desired.
  4450. * @errcode: pointer to error code if any
  4451. * @gfp_mask: allocation mask
  4452. *
  4453. * This can be used to allocate a paged skb, given a maximal order for frags.
  4454. */
  4455. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  4456. unsigned long data_len,
  4457. int max_page_order,
  4458. int *errcode,
  4459. gfp_t gfp_mask)
  4460. {
  4461. int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  4462. unsigned long chunk;
  4463. struct sk_buff *skb;
  4464. struct page *page;
  4465. gfp_t gfp_head;
  4466. int i;
  4467. *errcode = -EMSGSIZE;
  4468. /* Note this test could be relaxed, if we succeed to allocate
  4469. * high order pages...
  4470. */
  4471. if (npages > MAX_SKB_FRAGS)
  4472. return NULL;
  4473. gfp_head = gfp_mask;
  4474. if (gfp_head & __GFP_DIRECT_RECLAIM)
  4475. gfp_head |= __GFP_RETRY_MAYFAIL;
  4476. *errcode = -ENOBUFS;
  4477. skb = alloc_skb(header_len, gfp_head);
  4478. if (!skb)
  4479. return NULL;
  4480. skb->truesize += npages << PAGE_SHIFT;
  4481. for (i = 0; npages > 0; i++) {
  4482. int order = max_page_order;
  4483. while (order) {
  4484. if (npages >= 1 << order) {
  4485. page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
  4486. __GFP_COMP |
  4487. __GFP_NOWARN,
  4488. order);
  4489. if (page)
  4490. goto fill_page;
  4491. /* Do not retry other high order allocations */
  4492. order = 1;
  4493. max_page_order = 0;
  4494. }
  4495. order--;
  4496. }
  4497. page = alloc_page(gfp_mask);
  4498. if (!page)
  4499. goto failure;
  4500. fill_page:
  4501. chunk = min_t(unsigned long, data_len,
  4502. PAGE_SIZE << order);
  4503. skb_fill_page_desc(skb, i, page, 0, chunk);
  4504. data_len -= chunk;
  4505. npages -= 1 << order;
  4506. }
  4507. return skb;
  4508. failure:
  4509. kfree_skb(skb);
  4510. return NULL;
  4511. }
  4512. EXPORT_SYMBOL(alloc_skb_with_frags);
  4513. /* carve out the first off bytes from skb when off < headlen */
  4514. static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
  4515. const int headlen, gfp_t gfp_mask)
  4516. {
  4517. int i;
  4518. int size = skb_end_offset(skb);
  4519. int new_hlen = headlen - off;
  4520. u8 *data;
  4521. size = SKB_DATA_ALIGN(size);
  4522. if (skb_pfmemalloc(skb))
  4523. gfp_mask |= __GFP_MEMALLOC;
  4524. data = kmalloc_reserve(size +
  4525. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  4526. gfp_mask, NUMA_NO_NODE, NULL);
  4527. if (!data)
  4528. return -ENOMEM;
  4529. size = SKB_WITH_OVERHEAD(ksize(data));
  4530. /* Copy real data, and all frags */
  4531. skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
  4532. skb->len -= off;
  4533. memcpy((struct skb_shared_info *)(data + size),
  4534. skb_shinfo(skb),
  4535. offsetof(struct skb_shared_info,
  4536. frags[skb_shinfo(skb)->nr_frags]));
  4537. if (skb_cloned(skb)) {
  4538. /* drop the old head gracefully */
  4539. if (skb_orphan_frags(skb, gfp_mask)) {
  4540. kfree(data);
  4541. return -ENOMEM;
  4542. }
  4543. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  4544. skb_frag_ref(skb, i);
  4545. if (skb_has_frag_list(skb))
  4546. skb_clone_fraglist(skb);
  4547. skb_release_data(skb);
  4548. } else {
  4549. /* we can reuse existing recount- all we did was
  4550. * relocate values
  4551. */
  4552. skb_free_head(skb);
  4553. }
  4554. skb->head = data;
  4555. skb->data = data;
  4556. skb->head_frag = 0;
  4557. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  4558. skb->end = size;
  4559. #else
  4560. skb->end = skb->head + size;
  4561. #endif
  4562. skb_set_tail_pointer(skb, skb_headlen(skb));
  4563. skb_headers_offset_update(skb, 0);
  4564. skb->cloned = 0;
  4565. skb->hdr_len = 0;
  4566. skb->nohdr = 0;
  4567. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4568. return 0;
  4569. }
  4570. static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
  4571. /* carve out the first eat bytes from skb's frag_list. May recurse into
  4572. * pskb_carve()
  4573. */
  4574. static int pskb_carve_frag_list(struct sk_buff *skb,
  4575. struct skb_shared_info *shinfo, int eat,
  4576. gfp_t gfp_mask)
  4577. {
  4578. struct sk_buff *list = shinfo->frag_list;
  4579. struct sk_buff *clone = NULL;
  4580. struct sk_buff *insp = NULL;
  4581. do {
  4582. if (!list) {
  4583. pr_err("Not enough bytes to eat. Want %d\n", eat);
  4584. return -EFAULT;
  4585. }
  4586. if (list->len <= eat) {
  4587. /* Eaten as whole. */
  4588. eat -= list->len;
  4589. list = list->next;
  4590. insp = list;
  4591. } else {
  4592. /* Eaten partially. */
  4593. if (skb_shared(list)) {
  4594. clone = skb_clone(list, gfp_mask);
  4595. if (!clone)
  4596. return -ENOMEM;
  4597. insp = list->next;
  4598. list = clone;
  4599. } else {
  4600. /* This may be pulled without problems. */
  4601. insp = list;
  4602. }
  4603. if (pskb_carve(list, eat, gfp_mask) < 0) {
  4604. kfree_skb(clone);
  4605. return -ENOMEM;
  4606. }
  4607. break;
  4608. }
  4609. } while (eat);
  4610. /* Free pulled out fragments. */
  4611. while ((list = shinfo->frag_list) != insp) {
  4612. shinfo->frag_list = list->next;
  4613. kfree_skb(list);
  4614. }
  4615. /* And insert new clone at head. */
  4616. if (clone) {
  4617. clone->next = list;
  4618. shinfo->frag_list = clone;
  4619. }
  4620. return 0;
  4621. }
  4622. /* carve off first len bytes from skb. Split line (off) is in the
  4623. * non-linear part of skb
  4624. */
  4625. static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
  4626. int pos, gfp_t gfp_mask)
  4627. {
  4628. int i, k = 0;
  4629. int size = skb_end_offset(skb);
  4630. u8 *data;
  4631. const int nfrags = skb_shinfo(skb)->nr_frags;
  4632. struct skb_shared_info *shinfo;
  4633. size = SKB_DATA_ALIGN(size);
  4634. if (skb_pfmemalloc(skb))
  4635. gfp_mask |= __GFP_MEMALLOC;
  4636. data = kmalloc_reserve(size +
  4637. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  4638. gfp_mask, NUMA_NO_NODE, NULL);
  4639. if (!data)
  4640. return -ENOMEM;
  4641. size = SKB_WITH_OVERHEAD(ksize(data));
  4642. memcpy((struct skb_shared_info *)(data + size),
  4643. skb_shinfo(skb), offsetof(struct skb_shared_info,
  4644. frags[skb_shinfo(skb)->nr_frags]));
  4645. if (skb_orphan_frags(skb, gfp_mask)) {
  4646. kfree(data);
  4647. return -ENOMEM;
  4648. }
  4649. shinfo = (struct skb_shared_info *)(data + size);
  4650. for (i = 0; i < nfrags; i++) {
  4651. int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  4652. if (pos + fsize > off) {
  4653. shinfo->frags[k] = skb_shinfo(skb)->frags[i];
  4654. if (pos < off) {
  4655. /* Split frag.
  4656. * We have two variants in this case:
  4657. * 1. Move all the frag to the second
  4658. * part, if it is possible. F.e.
  4659. * this approach is mandatory for TUX,
  4660. * where splitting is expensive.
  4661. * 2. Split is accurately. We make this.
  4662. */
  4663. shinfo->frags[0].page_offset += off - pos;
  4664. skb_frag_size_sub(&shinfo->frags[0], off - pos);
  4665. }
  4666. skb_frag_ref(skb, i);
  4667. k++;
  4668. }
  4669. pos += fsize;
  4670. }
  4671. shinfo->nr_frags = k;
  4672. if (skb_has_frag_list(skb))
  4673. skb_clone_fraglist(skb);
  4674. if (k == 0) {
  4675. /* split line is in frag list */
  4676. pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
  4677. }
  4678. skb_release_data(skb);
  4679. skb->head = data;
  4680. skb->head_frag = 0;
  4681. skb->data = data;
  4682. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  4683. skb->end = size;
  4684. #else
  4685. skb->end = skb->head + size;
  4686. #endif
  4687. skb_reset_tail_pointer(skb);
  4688. skb_headers_offset_update(skb, 0);
  4689. skb->cloned = 0;
  4690. skb->hdr_len = 0;
  4691. skb->nohdr = 0;
  4692. skb->len -= off;
  4693. skb->data_len = skb->len;
  4694. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4695. return 0;
  4696. }
  4697. /* remove len bytes from the beginning of the skb */
  4698. static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
  4699. {
  4700. int headlen = skb_headlen(skb);
  4701. if (len < headlen)
  4702. return pskb_carve_inside_header(skb, len, headlen, gfp);
  4703. else
  4704. return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
  4705. }
  4706. /* Extract to_copy bytes starting at off from skb, and return this in
  4707. * a new skb
  4708. */
  4709. struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
  4710. int to_copy, gfp_t gfp)
  4711. {
  4712. struct sk_buff *clone = skb_clone(skb, gfp);
  4713. if (!clone)
  4714. return NULL;
  4715. if (pskb_carve(clone, off, gfp) < 0 ||
  4716. pskb_trim(clone, to_copy)) {
  4717. kfree_skb(clone);
  4718. return NULL;
  4719. }
  4720. return clone;
  4721. }
  4722. EXPORT_SYMBOL(pskb_extract);
  4723. /**
  4724. * skb_condense - try to get rid of fragments/frag_list if possible
  4725. * @skb: buffer
  4726. *
  4727. * Can be used to save memory before skb is added to a busy queue.
  4728. * If packet has bytes in frags and enough tail room in skb->head,
  4729. * pull all of them, so that we can free the frags right now and adjust
  4730. * truesize.
  4731. * Notes:
  4732. * We do not reallocate skb->head thus can not fail.
  4733. * Caller must re-evaluate skb->truesize if needed.
  4734. */
  4735. void skb_condense(struct sk_buff *skb)
  4736. {
  4737. if (skb->data_len) {
  4738. if (skb->data_len > skb->end - skb->tail ||
  4739. skb_cloned(skb))
  4740. return;
  4741. /* Nice, we can free page frag(s) right now */
  4742. __pskb_pull_tail(skb, skb->data_len);
  4743. }
  4744. /* At this point, skb->truesize might be over estimated,
  4745. * because skb had a fragment, and fragments do not tell
  4746. * their truesize.
  4747. * When we pulled its content into skb->head, fragment
  4748. * was freed, but __pskb_pull_tail() could not possibly
  4749. * adjust skb->truesize, not knowing the frag truesize.
  4750. */
  4751. skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
  4752. }