skbuff.c 113 KB

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