skbuff.c 111 KB

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