skbuff.c 111 KB

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