skbuff.c 89 KB

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