skbuff.c 122 KB

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