skbuff.c 96 KB

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