skbuff.c 110 KB

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