skbuff.c 107 KB

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