skbuff.c 98 KB

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