skbuff.c 102 KB

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