skbuff.c 111 KB

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