skbuff.c 122 KB

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