skbuff.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522
  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. static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
  71. struct pipe_buffer *buf)
  72. {
  73. put_page(buf->page);
  74. }
  75. static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
  76. struct pipe_buffer *buf)
  77. {
  78. get_page(buf->page);
  79. }
  80. static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
  81. struct pipe_buffer *buf)
  82. {
  83. return 1;
  84. }
  85. /* Pipe buffer operations for a socket. */
  86. static const struct pipe_buf_operations sock_pipe_buf_ops = {
  87. .can_merge = 0,
  88. .map = generic_pipe_buf_map,
  89. .unmap = generic_pipe_buf_unmap,
  90. .confirm = generic_pipe_buf_confirm,
  91. .release = sock_pipe_buf_release,
  92. .steal = sock_pipe_buf_steal,
  93. .get = sock_pipe_buf_get,
  94. };
  95. /**
  96. * skb_panic - private function for out-of-line support
  97. * @skb: buffer
  98. * @sz: size
  99. * @addr: address
  100. * @msg: skb_over_panic or skb_under_panic
  101. *
  102. * Out-of-line support for skb_put() and skb_push().
  103. * Called via the wrapper skb_over_panic() or skb_under_panic().
  104. * Keep out of line to prevent kernel bloat.
  105. * __builtin_return_address is not used because it is not always reliable.
  106. */
  107. static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
  108. const char msg[])
  109. {
  110. pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
  111. msg, addr, skb->len, sz, skb->head, skb->data,
  112. (unsigned long)skb->tail, (unsigned long)skb->end,
  113. skb->dev ? skb->dev->name : "<NULL>");
  114. BUG();
  115. }
  116. static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  117. {
  118. skb_panic(skb, sz, addr, __func__);
  119. }
  120. static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
  121. {
  122. skb_panic(skb, sz, addr, __func__);
  123. }
  124. /*
  125. * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
  126. * the caller if emergency pfmemalloc reserves are being used. If it is and
  127. * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
  128. * may be used. Otherwise, the packet data may be discarded until enough
  129. * memory is free
  130. */
  131. #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
  132. __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
  133. static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
  134. unsigned long ip, bool *pfmemalloc)
  135. {
  136. void *obj;
  137. bool ret_pfmemalloc = false;
  138. /*
  139. * Try a regular allocation, when that fails and we're not entitled
  140. * to the reserves, fail.
  141. */
  142. obj = kmalloc_node_track_caller(size,
  143. flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
  144. node);
  145. if (obj || !(gfp_pfmemalloc_allowed(flags)))
  146. goto out;
  147. /* Try again but now we are using pfmemalloc reserves */
  148. ret_pfmemalloc = true;
  149. obj = kmalloc_node_track_caller(size, flags, node);
  150. out:
  151. if (pfmemalloc)
  152. *pfmemalloc = ret_pfmemalloc;
  153. return obj;
  154. }
  155. /* Allocate a new skbuff. We do this ourselves so we can fill in a few
  156. * 'private' fields and also do memory statistics to find all the
  157. * [BEEP] leaks.
  158. *
  159. */
  160. struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
  161. {
  162. struct sk_buff *skb;
  163. /* Get the HEAD */
  164. skb = kmem_cache_alloc_node(skbuff_head_cache,
  165. gfp_mask & ~__GFP_DMA, node);
  166. if (!skb)
  167. goto out;
  168. /*
  169. * Only clear those fields we need to clear, not those that we will
  170. * actually initialise below. Hence, don't put any more fields after
  171. * the tail pointer in struct sk_buff!
  172. */
  173. memset(skb, 0, offsetof(struct sk_buff, tail));
  174. skb->head = NULL;
  175. skb->truesize = sizeof(struct sk_buff);
  176. atomic_set(&skb->users, 1);
  177. skb->mac_header = (typeof(skb->mac_header))~0U;
  178. out:
  179. return skb;
  180. }
  181. /**
  182. * __alloc_skb - allocate a network buffer
  183. * @size: size to allocate
  184. * @gfp_mask: allocation mask
  185. * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
  186. * instead of head cache and allocate a cloned (child) skb.
  187. * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
  188. * allocations in case the data is required for writeback
  189. * @node: numa node to allocate memory on
  190. *
  191. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  192. * tail room of at least size bytes. The object has a reference count
  193. * of one. The return is the buffer. On a failure the return is %NULL.
  194. *
  195. * Buffers may only be allocated from interrupts using a @gfp_mask of
  196. * %GFP_ATOMIC.
  197. */
  198. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  199. int flags, int node)
  200. {
  201. struct kmem_cache *cache;
  202. struct skb_shared_info *shinfo;
  203. struct sk_buff *skb;
  204. u8 *data;
  205. bool pfmemalloc;
  206. cache = (flags & SKB_ALLOC_FCLONE)
  207. ? skbuff_fclone_cache : skbuff_head_cache;
  208. if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
  209. gfp_mask |= __GFP_MEMALLOC;
  210. /* Get the HEAD */
  211. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
  212. if (!skb)
  213. goto out;
  214. prefetchw(skb);
  215. /* We do our best to align skb_shared_info on a separate cache
  216. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  217. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  218. * Both skb->head and skb_shared_info are cache line aligned.
  219. */
  220. size = SKB_DATA_ALIGN(size);
  221. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  222. data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
  223. if (!data)
  224. goto nodata;
  225. /* kmalloc(size) might give us more room than requested.
  226. * Put skb_shared_info exactly at the end of allocated zone,
  227. * to allow max possible filling before reallocation.
  228. */
  229. size = SKB_WITH_OVERHEAD(ksize(data));
  230. prefetchw(data + size);
  231. /*
  232. * Only clear those fields we need to clear, not those that we will
  233. * actually initialise below. Hence, don't put any more fields after
  234. * the tail pointer in struct sk_buff!
  235. */
  236. memset(skb, 0, offsetof(struct sk_buff, tail));
  237. /* Account for allocated memory : skb + skb->head */
  238. skb->truesize = SKB_TRUESIZE(size);
  239. skb->pfmemalloc = pfmemalloc;
  240. atomic_set(&skb->users, 1);
  241. skb->head = data;
  242. skb->data = data;
  243. skb_reset_tail_pointer(skb);
  244. skb->end = skb->tail + size;
  245. skb->mac_header = (typeof(skb->mac_header))~0U;
  246. skb->transport_header = (typeof(skb->transport_header))~0U;
  247. /* make sure we initialize shinfo sequentially */
  248. shinfo = skb_shinfo(skb);
  249. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  250. atomic_set(&shinfo->dataref, 1);
  251. kmemcheck_annotate_variable(shinfo->destructor_arg);
  252. if (flags & SKB_ALLOC_FCLONE) {
  253. struct sk_buff *child = skb + 1;
  254. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  255. kmemcheck_annotate_bitfield(child, flags1);
  256. kmemcheck_annotate_bitfield(child, flags2);
  257. skb->fclone = SKB_FCLONE_ORIG;
  258. atomic_set(fclone_ref, 1);
  259. child->fclone = SKB_FCLONE_UNAVAILABLE;
  260. child->pfmemalloc = pfmemalloc;
  261. }
  262. out:
  263. return skb;
  264. nodata:
  265. kmem_cache_free(cache, skb);
  266. skb = NULL;
  267. goto out;
  268. }
  269. EXPORT_SYMBOL(__alloc_skb);
  270. /**
  271. * build_skb - build a network buffer
  272. * @data: data buffer provided by caller
  273. * @frag_size: size of fragment, or 0 if head was kmalloced
  274. *
  275. * Allocate a new &sk_buff. Caller provides space holding head and
  276. * skb_shared_info. @data must have been allocated by kmalloc()
  277. * The return is the new skb buffer.
  278. * On a failure the return is %NULL, and @data is not freed.
  279. * Notes :
  280. * Before IO, driver allocates only data buffer where NIC put incoming frame
  281. * Driver should add room at head (NET_SKB_PAD) and
  282. * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
  283. * After IO, driver calls build_skb(), to allocate sk_buff and populate it
  284. * before giving packet to stack.
  285. * RX rings only contains data buffers, not full skbs.
  286. */
  287. struct sk_buff *build_skb(void *data, unsigned int frag_size)
  288. {
  289. struct skb_shared_info *shinfo;
  290. struct sk_buff *skb;
  291. unsigned int size = frag_size ? : ksize(data);
  292. skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
  293. if (!skb)
  294. return NULL;
  295. size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  296. memset(skb, 0, offsetof(struct sk_buff, tail));
  297. skb->truesize = SKB_TRUESIZE(size);
  298. skb->head_frag = frag_size != 0;
  299. atomic_set(&skb->users, 1);
  300. skb->head = data;
  301. skb->data = data;
  302. skb_reset_tail_pointer(skb);
  303. skb->end = skb->tail + size;
  304. skb->mac_header = (typeof(skb->mac_header))~0U;
  305. skb->transport_header = (typeof(skb->transport_header))~0U;
  306. /* make sure we initialize shinfo sequentially */
  307. shinfo = skb_shinfo(skb);
  308. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  309. atomic_set(&shinfo->dataref, 1);
  310. kmemcheck_annotate_variable(shinfo->destructor_arg);
  311. return skb;
  312. }
  313. EXPORT_SYMBOL(build_skb);
  314. struct netdev_alloc_cache {
  315. struct page_frag frag;
  316. /* we maintain a pagecount bias, so that we dont dirty cache line
  317. * containing page->_count every time we allocate a fragment.
  318. */
  319. unsigned int pagecnt_bias;
  320. };
  321. static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
  322. static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
  323. {
  324. struct netdev_alloc_cache *nc;
  325. void *data = NULL;
  326. int order;
  327. unsigned long flags;
  328. local_irq_save(flags);
  329. nc = &__get_cpu_var(netdev_alloc_cache);
  330. if (unlikely(!nc->frag.page)) {
  331. refill:
  332. for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
  333. gfp_t gfp = gfp_mask;
  334. if (order)
  335. gfp |= __GFP_COMP | __GFP_NOWARN;
  336. nc->frag.page = alloc_pages(gfp, order);
  337. if (likely(nc->frag.page))
  338. break;
  339. if (--order < 0)
  340. goto end;
  341. }
  342. nc->frag.size = PAGE_SIZE << order;
  343. recycle:
  344. atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
  345. nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
  346. nc->frag.offset = 0;
  347. }
  348. if (nc->frag.offset + fragsz > nc->frag.size) {
  349. /* avoid unnecessary locked operations if possible */
  350. if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
  351. atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
  352. goto recycle;
  353. goto refill;
  354. }
  355. data = page_address(nc->frag.page) + nc->frag.offset;
  356. nc->frag.offset += fragsz;
  357. nc->pagecnt_bias--;
  358. end:
  359. local_irq_restore(flags);
  360. return data;
  361. }
  362. /**
  363. * netdev_alloc_frag - allocate a page fragment
  364. * @fragsz: fragment size
  365. *
  366. * Allocates a frag from a page for receive buffer.
  367. * Uses GFP_ATOMIC allocations.
  368. */
  369. void *netdev_alloc_frag(unsigned int fragsz)
  370. {
  371. return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
  372. }
  373. EXPORT_SYMBOL(netdev_alloc_frag);
  374. /**
  375. * __netdev_alloc_skb - allocate an skbuff for rx on a specific device
  376. * @dev: network device to receive on
  377. * @length: length to allocate
  378. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  379. *
  380. * Allocate a new &sk_buff and assign it a usage count of one. The
  381. * buffer has unspecified headroom built in. Users should allocate
  382. * the headroom they think they need without accounting for the
  383. * built in space. The built in space is used for optimisations.
  384. *
  385. * %NULL is returned if there is no free memory.
  386. */
  387. struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  388. unsigned int length, gfp_t gfp_mask)
  389. {
  390. struct sk_buff *skb = NULL;
  391. unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
  392. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  393. if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
  394. void *data;
  395. if (sk_memalloc_socks())
  396. gfp_mask |= __GFP_MEMALLOC;
  397. data = __netdev_alloc_frag(fragsz, gfp_mask);
  398. if (likely(data)) {
  399. skb = build_skb(data, fragsz);
  400. if (unlikely(!skb))
  401. put_page(virt_to_head_page(data));
  402. }
  403. } else {
  404. skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
  405. SKB_ALLOC_RX, NUMA_NO_NODE);
  406. }
  407. if (likely(skb)) {
  408. skb_reserve(skb, NET_SKB_PAD);
  409. skb->dev = dev;
  410. }
  411. return skb;
  412. }
  413. EXPORT_SYMBOL(__netdev_alloc_skb);
  414. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  415. int size, unsigned int truesize)
  416. {
  417. skb_fill_page_desc(skb, i, page, off, size);
  418. skb->len += size;
  419. skb->data_len += size;
  420. skb->truesize += truesize;
  421. }
  422. EXPORT_SYMBOL(skb_add_rx_frag);
  423. static void skb_drop_list(struct sk_buff **listp)
  424. {
  425. kfree_skb_list(*listp);
  426. *listp = NULL;
  427. }
  428. static inline void skb_drop_fraglist(struct sk_buff *skb)
  429. {
  430. skb_drop_list(&skb_shinfo(skb)->frag_list);
  431. }
  432. static void skb_clone_fraglist(struct sk_buff *skb)
  433. {
  434. struct sk_buff *list;
  435. skb_walk_frags(skb, list)
  436. skb_get(list);
  437. }
  438. static void skb_free_head(struct sk_buff *skb)
  439. {
  440. if (skb->head_frag)
  441. put_page(virt_to_head_page(skb->head));
  442. else
  443. kfree(skb->head);
  444. }
  445. static void skb_release_data(struct sk_buff *skb)
  446. {
  447. if (!skb->cloned ||
  448. !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
  449. &skb_shinfo(skb)->dataref)) {
  450. if (skb_shinfo(skb)->nr_frags) {
  451. int i;
  452. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  453. skb_frag_unref(skb, i);
  454. }
  455. /*
  456. * If skb buf is from userspace, we need to notify the caller
  457. * the lower device DMA has done;
  458. */
  459. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  460. struct ubuf_info *uarg;
  461. uarg = skb_shinfo(skb)->destructor_arg;
  462. if (uarg->callback)
  463. uarg->callback(uarg, true);
  464. }
  465. if (skb_has_frag_list(skb))
  466. skb_drop_fraglist(skb);
  467. skb_free_head(skb);
  468. }
  469. }
  470. /*
  471. * Free an skbuff by memory without cleaning the state.
  472. */
  473. static void kfree_skbmem(struct sk_buff *skb)
  474. {
  475. struct sk_buff *other;
  476. atomic_t *fclone_ref;
  477. switch (skb->fclone) {
  478. case SKB_FCLONE_UNAVAILABLE:
  479. kmem_cache_free(skbuff_head_cache, skb);
  480. break;
  481. case SKB_FCLONE_ORIG:
  482. fclone_ref = (atomic_t *) (skb + 2);
  483. if (atomic_dec_and_test(fclone_ref))
  484. kmem_cache_free(skbuff_fclone_cache, skb);
  485. break;
  486. case SKB_FCLONE_CLONE:
  487. fclone_ref = (atomic_t *) (skb + 1);
  488. other = skb - 1;
  489. /* The clone portion is available for
  490. * fast-cloning again.
  491. */
  492. skb->fclone = SKB_FCLONE_UNAVAILABLE;
  493. if (atomic_dec_and_test(fclone_ref))
  494. kmem_cache_free(skbuff_fclone_cache, other);
  495. break;
  496. }
  497. }
  498. static void skb_release_head_state(struct sk_buff *skb)
  499. {
  500. skb_dst_drop(skb);
  501. #ifdef CONFIG_XFRM
  502. secpath_put(skb->sp);
  503. #endif
  504. if (skb->destructor) {
  505. WARN_ON(in_irq());
  506. skb->destructor(skb);
  507. }
  508. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  509. nf_conntrack_put(skb->nfct);
  510. #endif
  511. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  512. nf_conntrack_put_reasm(skb->nfct_reasm);
  513. #endif
  514. #ifdef CONFIG_BRIDGE_NETFILTER
  515. nf_bridge_put(skb->nf_bridge);
  516. #endif
  517. /* XXX: IS this still necessary? - JHS */
  518. #ifdef CONFIG_NET_SCHED
  519. skb->tc_index = 0;
  520. #ifdef CONFIG_NET_CLS_ACT
  521. skb->tc_verd = 0;
  522. #endif
  523. #endif
  524. }
  525. /* Free everything but the sk_buff shell. */
  526. static void skb_release_all(struct sk_buff *skb)
  527. {
  528. skb_release_head_state(skb);
  529. if (likely(skb->head))
  530. skb_release_data(skb);
  531. }
  532. /**
  533. * __kfree_skb - private function
  534. * @skb: buffer
  535. *
  536. * Free an sk_buff. Release anything attached to the buffer.
  537. * Clean the state. This is an internal helper function. Users should
  538. * always call kfree_skb
  539. */
  540. void __kfree_skb(struct sk_buff *skb)
  541. {
  542. skb_release_all(skb);
  543. kfree_skbmem(skb);
  544. }
  545. EXPORT_SYMBOL(__kfree_skb);
  546. /**
  547. * kfree_skb - free an sk_buff
  548. * @skb: buffer to free
  549. *
  550. * Drop a reference to the buffer and free it if the usage count has
  551. * hit zero.
  552. */
  553. void kfree_skb(struct sk_buff *skb)
  554. {
  555. if (unlikely(!skb))
  556. return;
  557. if (likely(atomic_read(&skb->users) == 1))
  558. smp_rmb();
  559. else if (likely(!atomic_dec_and_test(&skb->users)))
  560. return;
  561. trace_kfree_skb(skb, __builtin_return_address(0));
  562. __kfree_skb(skb);
  563. }
  564. EXPORT_SYMBOL(kfree_skb);
  565. void kfree_skb_list(struct sk_buff *segs)
  566. {
  567. while (segs) {
  568. struct sk_buff *next = segs->next;
  569. kfree_skb(segs);
  570. segs = next;
  571. }
  572. }
  573. EXPORT_SYMBOL(kfree_skb_list);
  574. /**
  575. * skb_tx_error - report an sk_buff xmit error
  576. * @skb: buffer that triggered an error
  577. *
  578. * Report xmit error if a device callback is tracking this skb.
  579. * skb must be freed afterwards.
  580. */
  581. void skb_tx_error(struct sk_buff *skb)
  582. {
  583. if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
  584. struct ubuf_info *uarg;
  585. uarg = skb_shinfo(skb)->destructor_arg;
  586. if (uarg->callback)
  587. uarg->callback(uarg, false);
  588. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  589. }
  590. }
  591. EXPORT_SYMBOL(skb_tx_error);
  592. /**
  593. * consume_skb - free an skbuff
  594. * @skb: buffer to free
  595. *
  596. * Drop a ref to the buffer and free it if the usage count has hit zero
  597. * Functions identically to kfree_skb, but kfree_skb assumes that the frame
  598. * is being dropped after a failure and notes that
  599. */
  600. void consume_skb(struct sk_buff *skb)
  601. {
  602. if (unlikely(!skb))
  603. return;
  604. if (likely(atomic_read(&skb->users) == 1))
  605. smp_rmb();
  606. else if (likely(!atomic_dec_and_test(&skb->users)))
  607. return;
  608. trace_consume_skb(skb);
  609. __kfree_skb(skb);
  610. }
  611. EXPORT_SYMBOL(consume_skb);
  612. static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  613. {
  614. new->tstamp = old->tstamp;
  615. new->dev = old->dev;
  616. new->transport_header = old->transport_header;
  617. new->network_header = old->network_header;
  618. new->mac_header = old->mac_header;
  619. new->inner_protocol = old->inner_protocol;
  620. new->inner_transport_header = old->inner_transport_header;
  621. new->inner_network_header = old->inner_network_header;
  622. new->inner_mac_header = old->inner_mac_header;
  623. skb_dst_copy(new, old);
  624. new->rxhash = old->rxhash;
  625. new->ooo_okay = old->ooo_okay;
  626. new->l4_rxhash = old->l4_rxhash;
  627. new->no_fcs = old->no_fcs;
  628. new->encapsulation = old->encapsulation;
  629. #ifdef CONFIG_XFRM
  630. new->sp = secpath_get(old->sp);
  631. #endif
  632. memcpy(new->cb, old->cb, sizeof(old->cb));
  633. new->csum = old->csum;
  634. new->local_df = old->local_df;
  635. new->pkt_type = old->pkt_type;
  636. new->ip_summed = old->ip_summed;
  637. skb_copy_queue_mapping(new, old);
  638. new->priority = old->priority;
  639. #if IS_ENABLED(CONFIG_IP_VS)
  640. new->ipvs_property = old->ipvs_property;
  641. #endif
  642. new->pfmemalloc = old->pfmemalloc;
  643. new->protocol = old->protocol;
  644. new->mark = old->mark;
  645. new->skb_iif = old->skb_iif;
  646. __nf_copy(new, old);
  647. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  648. new->nf_trace = old->nf_trace;
  649. #endif
  650. #ifdef CONFIG_NET_SCHED
  651. new->tc_index = old->tc_index;
  652. #ifdef CONFIG_NET_CLS_ACT
  653. new->tc_verd = old->tc_verd;
  654. #endif
  655. #endif
  656. new->vlan_proto = old->vlan_proto;
  657. new->vlan_tci = old->vlan_tci;
  658. skb_copy_secmark(new, old);
  659. #ifdef CONFIG_NET_LL_RX_POLL
  660. new->napi_id = old->napi_id;
  661. #endif
  662. }
  663. /*
  664. * You should not add any new code to this function. Add it to
  665. * __copy_skb_header above instead.
  666. */
  667. static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
  668. {
  669. #define C(x) n->x = skb->x
  670. n->next = n->prev = NULL;
  671. n->sk = NULL;
  672. __copy_skb_header(n, skb);
  673. C(len);
  674. C(data_len);
  675. C(mac_len);
  676. n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
  677. n->cloned = 1;
  678. n->nohdr = 0;
  679. n->destructor = NULL;
  680. C(tail);
  681. C(end);
  682. C(head);
  683. C(head_frag);
  684. C(data);
  685. C(truesize);
  686. atomic_set(&n->users, 1);
  687. atomic_inc(&(skb_shinfo(skb)->dataref));
  688. skb->cloned = 1;
  689. return n;
  690. #undef C
  691. }
  692. /**
  693. * skb_morph - morph one skb into another
  694. * @dst: the skb to receive the contents
  695. * @src: the skb to supply the contents
  696. *
  697. * This is identical to skb_clone except that the target skb is
  698. * supplied by the user.
  699. *
  700. * The target skb is returned upon exit.
  701. */
  702. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
  703. {
  704. skb_release_all(dst);
  705. return __skb_clone(dst, src);
  706. }
  707. EXPORT_SYMBOL_GPL(skb_morph);
  708. /**
  709. * skb_copy_ubufs - copy userspace skb frags buffers to kernel
  710. * @skb: the skb to modify
  711. * @gfp_mask: allocation priority
  712. *
  713. * This must be called on SKBTX_DEV_ZEROCOPY skb.
  714. * It will copy all frags into kernel and drop the reference
  715. * to userspace pages.
  716. *
  717. * If this function is called from an interrupt gfp_mask() must be
  718. * %GFP_ATOMIC.
  719. *
  720. * Returns 0 on success or a negative error code on failure
  721. * to allocate kernel memory to copy to.
  722. */
  723. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
  724. {
  725. int i;
  726. int num_frags = skb_shinfo(skb)->nr_frags;
  727. struct page *page, *head = NULL;
  728. struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
  729. for (i = 0; i < num_frags; i++) {
  730. u8 *vaddr;
  731. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  732. page = alloc_page(gfp_mask);
  733. if (!page) {
  734. while (head) {
  735. struct page *next = (struct page *)page_private(head);
  736. put_page(head);
  737. head = next;
  738. }
  739. return -ENOMEM;
  740. }
  741. vaddr = kmap_atomic(skb_frag_page(f));
  742. memcpy(page_address(page),
  743. vaddr + f->page_offset, skb_frag_size(f));
  744. kunmap_atomic(vaddr);
  745. set_page_private(page, (unsigned long)head);
  746. head = page;
  747. }
  748. /* skb frags release userspace buffers */
  749. for (i = 0; i < num_frags; i++)
  750. skb_frag_unref(skb, i);
  751. uarg->callback(uarg, false);
  752. /* skb frags point to kernel buffers */
  753. for (i = num_frags - 1; i >= 0; i--) {
  754. __skb_fill_page_desc(skb, i, head, 0,
  755. skb_shinfo(skb)->frags[i].size);
  756. head = (struct page *)page_private(head);
  757. }
  758. skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
  759. return 0;
  760. }
  761. EXPORT_SYMBOL_GPL(skb_copy_ubufs);
  762. /**
  763. * skb_clone - duplicate an sk_buff
  764. * @skb: buffer to clone
  765. * @gfp_mask: allocation priority
  766. *
  767. * Duplicate an &sk_buff. The new one is not owned by a socket. Both
  768. * copies share the same packet data but not structure. The new
  769. * buffer has a reference count of 1. If the allocation fails the
  770. * function returns %NULL otherwise the new buffer is returned.
  771. *
  772. * If this function is called from an interrupt gfp_mask() must be
  773. * %GFP_ATOMIC.
  774. */
  775. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  776. {
  777. struct sk_buff *n;
  778. if (skb_orphan_frags(skb, gfp_mask))
  779. return NULL;
  780. n = skb + 1;
  781. if (skb->fclone == SKB_FCLONE_ORIG &&
  782. n->fclone == SKB_FCLONE_UNAVAILABLE) {
  783. atomic_t *fclone_ref = (atomic_t *) (n + 1);
  784. n->fclone = SKB_FCLONE_CLONE;
  785. atomic_inc(fclone_ref);
  786. } else {
  787. if (skb_pfmemalloc(skb))
  788. gfp_mask |= __GFP_MEMALLOC;
  789. n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
  790. if (!n)
  791. return NULL;
  792. kmemcheck_annotate_bitfield(n, flags1);
  793. kmemcheck_annotate_bitfield(n, flags2);
  794. n->fclone = SKB_FCLONE_UNAVAILABLE;
  795. }
  796. return __skb_clone(n, skb);
  797. }
  798. EXPORT_SYMBOL(skb_clone);
  799. static void skb_headers_offset_update(struct sk_buff *skb, int off)
  800. {
  801. /* {transport,network,mac}_header and tail are relative to skb->head */
  802. skb->transport_header += off;
  803. skb->network_header += off;
  804. if (skb_mac_header_was_set(skb))
  805. skb->mac_header += off;
  806. skb->inner_transport_header += off;
  807. skb->inner_network_header += off;
  808. skb->inner_mac_header += off;
  809. }
  810. static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
  811. {
  812. __copy_skb_header(new, old);
  813. skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
  814. skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
  815. skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
  816. }
  817. static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
  818. {
  819. if (skb_pfmemalloc(skb))
  820. return SKB_ALLOC_RX;
  821. return 0;
  822. }
  823. /**
  824. * skb_copy - create private copy of an sk_buff
  825. * @skb: buffer to copy
  826. * @gfp_mask: allocation priority
  827. *
  828. * Make a copy of both an &sk_buff and its data. This is used when the
  829. * caller wishes to modify the data and needs a private copy of the
  830. * data to alter. Returns %NULL on failure or the pointer to the buffer
  831. * on success. The returned buffer has a reference count of 1.
  832. *
  833. * As by-product this function converts non-linear &sk_buff to linear
  834. * one, so that &sk_buff becomes completely private and caller is allowed
  835. * to modify all the data of returned buffer. This means that this
  836. * function is not recommended for use in circumstances when only
  837. * header is going to be modified. Use pskb_copy() instead.
  838. */
  839. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
  840. {
  841. int headerlen = skb_headroom(skb);
  842. unsigned int size = skb_end_offset(skb) + skb->data_len;
  843. struct sk_buff *n = __alloc_skb(size, gfp_mask,
  844. skb_alloc_rx_flag(skb), NUMA_NO_NODE);
  845. if (!n)
  846. return NULL;
  847. /* Set the data pointer */
  848. skb_reserve(n, headerlen);
  849. /* Set the tail pointer and length */
  850. skb_put(n, skb->len);
  851. if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
  852. BUG();
  853. copy_skb_header(n, skb);
  854. return n;
  855. }
  856. EXPORT_SYMBOL(skb_copy);
  857. /**
  858. * __pskb_copy - create copy of an sk_buff with private head.
  859. * @skb: buffer to copy
  860. * @headroom: headroom of new skb
  861. * @gfp_mask: allocation priority
  862. *
  863. * Make a copy of both an &sk_buff and part of its data, located
  864. * in header. Fragmented data remain shared. This is used when
  865. * the caller wishes to modify only header of &sk_buff and needs
  866. * private copy of the header to alter. Returns %NULL on failure
  867. * or the pointer to the buffer on success.
  868. * The returned buffer has a reference count of 1.
  869. */
  870. struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, gfp_t gfp_mask)
  871. {
  872. unsigned int size = skb_headlen(skb) + headroom;
  873. struct sk_buff *n = __alloc_skb(size, gfp_mask,
  874. skb_alloc_rx_flag(skb), NUMA_NO_NODE);
  875. if (!n)
  876. goto out;
  877. /* Set the data pointer */
  878. skb_reserve(n, headroom);
  879. /* Set the tail pointer and length */
  880. skb_put(n, skb_headlen(skb));
  881. /* Copy the bytes */
  882. skb_copy_from_linear_data(skb, n->data, n->len);
  883. n->truesize += skb->data_len;
  884. n->data_len = skb->data_len;
  885. n->len = skb->len;
  886. if (skb_shinfo(skb)->nr_frags) {
  887. int i;
  888. if (skb_orphan_frags(skb, gfp_mask)) {
  889. kfree_skb(n);
  890. n = NULL;
  891. goto out;
  892. }
  893. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  894. skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
  895. skb_frag_ref(skb, i);
  896. }
  897. skb_shinfo(n)->nr_frags = i;
  898. }
  899. if (skb_has_frag_list(skb)) {
  900. skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
  901. skb_clone_fraglist(n);
  902. }
  903. copy_skb_header(n, skb);
  904. out:
  905. return n;
  906. }
  907. EXPORT_SYMBOL(__pskb_copy);
  908. /**
  909. * pskb_expand_head - reallocate header of &sk_buff
  910. * @skb: buffer to reallocate
  911. * @nhead: room to add at head
  912. * @ntail: room to add at tail
  913. * @gfp_mask: allocation priority
  914. *
  915. * Expands (or creates identical copy, if &nhead and &ntail are zero)
  916. * header of skb. &sk_buff itself is not changed. &sk_buff MUST have
  917. * reference count of 1. Returns zero in the case of success or error,
  918. * if expansion failed. In the last case, &sk_buff is not changed.
  919. *
  920. * All the pointers pointing into skb header may change and must be
  921. * reloaded after call to this function.
  922. */
  923. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
  924. gfp_t gfp_mask)
  925. {
  926. int i;
  927. u8 *data;
  928. int size = nhead + skb_end_offset(skb) + ntail;
  929. long off;
  930. BUG_ON(nhead < 0);
  931. if (skb_shared(skb))
  932. BUG();
  933. size = SKB_DATA_ALIGN(size);
  934. if (skb_pfmemalloc(skb))
  935. gfp_mask |= __GFP_MEMALLOC;
  936. data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  937. gfp_mask, NUMA_NO_NODE, NULL);
  938. if (!data)
  939. goto nodata;
  940. size = SKB_WITH_OVERHEAD(ksize(data));
  941. /* Copy only real data... and, alas, header. This should be
  942. * optimized for the cases when header is void.
  943. */
  944. memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
  945. memcpy((struct skb_shared_info *)(data + size),
  946. skb_shinfo(skb),
  947. offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
  948. /*
  949. * if shinfo is shared we must drop the old head gracefully, but if it
  950. * is not we can just drop the old head and let the existing refcount
  951. * be since all we did is relocate the values
  952. */
  953. if (skb_cloned(skb)) {
  954. /* copy this zero copy skb frags */
  955. if (skb_orphan_frags(skb, gfp_mask))
  956. goto nofrags;
  957. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  958. skb_frag_ref(skb, i);
  959. if (skb_has_frag_list(skb))
  960. skb_clone_fraglist(skb);
  961. skb_release_data(skb);
  962. } else {
  963. skb_free_head(skb);
  964. }
  965. off = (data + nhead) - skb->head;
  966. skb->head = data;
  967. skb->head_frag = 0;
  968. skb->data += off;
  969. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  970. skb->end = size;
  971. off = nhead;
  972. #else
  973. skb->end = skb->head + size;
  974. #endif
  975. skb->tail += off;
  976. skb_headers_offset_update(skb, nhead);
  977. /* Only adjust this if it actually is csum_start rather than csum */
  978. if (skb->ip_summed == CHECKSUM_PARTIAL)
  979. skb->csum_start += nhead;
  980. skb->cloned = 0;
  981. skb->hdr_len = 0;
  982. skb->nohdr = 0;
  983. atomic_set(&skb_shinfo(skb)->dataref, 1);
  984. return 0;
  985. nofrags:
  986. kfree(data);
  987. nodata:
  988. return -ENOMEM;
  989. }
  990. EXPORT_SYMBOL(pskb_expand_head);
  991. /* Make private copy of skb with writable head and some headroom */
  992. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
  993. {
  994. struct sk_buff *skb2;
  995. int delta = headroom - skb_headroom(skb);
  996. if (delta <= 0)
  997. skb2 = pskb_copy(skb, GFP_ATOMIC);
  998. else {
  999. skb2 = skb_clone(skb, GFP_ATOMIC);
  1000. if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
  1001. GFP_ATOMIC)) {
  1002. kfree_skb(skb2);
  1003. skb2 = NULL;
  1004. }
  1005. }
  1006. return skb2;
  1007. }
  1008. EXPORT_SYMBOL(skb_realloc_headroom);
  1009. /**
  1010. * skb_copy_expand - copy and expand sk_buff
  1011. * @skb: buffer to copy
  1012. * @newheadroom: new free bytes at head
  1013. * @newtailroom: new free bytes at tail
  1014. * @gfp_mask: allocation priority
  1015. *
  1016. * Make a copy of both an &sk_buff and its data and while doing so
  1017. * allocate additional space.
  1018. *
  1019. * This is used when the caller wishes to modify the data and needs a
  1020. * private copy of the data to alter as well as more space for new fields.
  1021. * Returns %NULL on failure or the pointer to the buffer
  1022. * on success. The returned buffer has a reference count of 1.
  1023. *
  1024. * You must pass %GFP_ATOMIC as the allocation priority if this function
  1025. * is called from an interrupt.
  1026. */
  1027. struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  1028. int newheadroom, int newtailroom,
  1029. gfp_t gfp_mask)
  1030. {
  1031. /*
  1032. * Allocate the copy buffer
  1033. */
  1034. struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
  1035. gfp_mask, skb_alloc_rx_flag(skb),
  1036. NUMA_NO_NODE);
  1037. int oldheadroom = skb_headroom(skb);
  1038. int head_copy_len, head_copy_off;
  1039. int off;
  1040. if (!n)
  1041. return NULL;
  1042. skb_reserve(n, newheadroom);
  1043. /* Set the tail pointer and length */
  1044. skb_put(n, skb->len);
  1045. head_copy_len = oldheadroom;
  1046. head_copy_off = 0;
  1047. if (newheadroom <= head_copy_len)
  1048. head_copy_len = newheadroom;
  1049. else
  1050. head_copy_off = newheadroom - head_copy_len;
  1051. /* Copy the linear header and data. */
  1052. if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
  1053. skb->len + head_copy_len))
  1054. BUG();
  1055. copy_skb_header(n, skb);
  1056. off = newheadroom - oldheadroom;
  1057. if (n->ip_summed == CHECKSUM_PARTIAL)
  1058. n->csum_start += off;
  1059. skb_headers_offset_update(n, off);
  1060. return n;
  1061. }
  1062. EXPORT_SYMBOL(skb_copy_expand);
  1063. /**
  1064. * skb_pad - zero pad the tail of an skb
  1065. * @skb: buffer to pad
  1066. * @pad: space to pad
  1067. *
  1068. * Ensure that a buffer is followed by a padding area that is zero
  1069. * filled. Used by network drivers which may DMA or transfer data
  1070. * beyond the buffer end onto the wire.
  1071. *
  1072. * May return error in out of memory cases. The skb is freed on error.
  1073. */
  1074. int skb_pad(struct sk_buff *skb, int pad)
  1075. {
  1076. int err;
  1077. int ntail;
  1078. /* If the skbuff is non linear tailroom is always zero.. */
  1079. if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
  1080. memset(skb->data+skb->len, 0, pad);
  1081. return 0;
  1082. }
  1083. ntail = skb->data_len + pad - (skb->end - skb->tail);
  1084. if (likely(skb_cloned(skb) || ntail > 0)) {
  1085. err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
  1086. if (unlikely(err))
  1087. goto free_skb;
  1088. }
  1089. /* FIXME: The use of this function with non-linear skb's really needs
  1090. * to be audited.
  1091. */
  1092. err = skb_linearize(skb);
  1093. if (unlikely(err))
  1094. goto free_skb;
  1095. memset(skb->data + skb->len, 0, pad);
  1096. return 0;
  1097. free_skb:
  1098. kfree_skb(skb);
  1099. return err;
  1100. }
  1101. EXPORT_SYMBOL(skb_pad);
  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 = &sock_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,
  1687. int len, __wsum csum)
  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 = csum_partial(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 = csum_partial(vaddr + frag->page_offset +
  1715. offset - start, copy, 0);
  1716. kunmap_atomic(vaddr);
  1717. csum = csum_block_add(csum, csum2, pos);
  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);
  1735. csum = csum_block_add(csum, csum2, pos);
  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. /* Both of above in one bottle. */
  1748. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1749. u8 *to, int len, __wsum csum)
  1750. {
  1751. int start = skb_headlen(skb);
  1752. int i, copy = start - offset;
  1753. struct sk_buff *frag_iter;
  1754. int pos = 0;
  1755. /* Copy header. */
  1756. if (copy > 0) {
  1757. if (copy > len)
  1758. copy = len;
  1759. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1760. copy, csum);
  1761. if ((len -= copy) == 0)
  1762. return csum;
  1763. offset += copy;
  1764. to += copy;
  1765. pos = copy;
  1766. }
  1767. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1768. int end;
  1769. WARN_ON(start > offset + len);
  1770. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1771. if ((copy = end - offset) > 0) {
  1772. __wsum csum2;
  1773. u8 *vaddr;
  1774. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1775. if (copy > len)
  1776. copy = len;
  1777. vaddr = kmap_atomic(skb_frag_page(frag));
  1778. csum2 = csum_partial_copy_nocheck(vaddr +
  1779. frag->page_offset +
  1780. offset - start, to,
  1781. copy, 0);
  1782. kunmap_atomic(vaddr);
  1783. csum = csum_block_add(csum, csum2, pos);
  1784. if (!(len -= copy))
  1785. return csum;
  1786. offset += copy;
  1787. to += copy;
  1788. pos += copy;
  1789. }
  1790. start = end;
  1791. }
  1792. skb_walk_frags(skb, frag_iter) {
  1793. __wsum csum2;
  1794. int end;
  1795. WARN_ON(start > offset + len);
  1796. end = start + frag_iter->len;
  1797. if ((copy = end - offset) > 0) {
  1798. if (copy > len)
  1799. copy = len;
  1800. csum2 = skb_copy_and_csum_bits(frag_iter,
  1801. offset - start,
  1802. to, copy, 0);
  1803. csum = csum_block_add(csum, csum2, pos);
  1804. if ((len -= copy) == 0)
  1805. return csum;
  1806. offset += copy;
  1807. to += copy;
  1808. pos += copy;
  1809. }
  1810. start = end;
  1811. }
  1812. BUG_ON(len);
  1813. return csum;
  1814. }
  1815. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1816. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  1817. {
  1818. __wsum csum;
  1819. long csstart;
  1820. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1821. csstart = skb_checksum_start_offset(skb);
  1822. else
  1823. csstart = skb_headlen(skb);
  1824. BUG_ON(csstart > skb_headlen(skb));
  1825. skb_copy_from_linear_data(skb, to, csstart);
  1826. csum = 0;
  1827. if (csstart != skb->len)
  1828. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  1829. skb->len - csstart, 0);
  1830. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1831. long csstuff = csstart + skb->csum_offset;
  1832. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  1833. }
  1834. }
  1835. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  1836. /**
  1837. * skb_dequeue - remove from the head of the queue
  1838. * @list: list to dequeue from
  1839. *
  1840. * Remove the head of the list. The list lock is taken so the function
  1841. * may be used safely with other locking list functions. The head item is
  1842. * returned or %NULL if the list is empty.
  1843. */
  1844. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  1845. {
  1846. unsigned long flags;
  1847. struct sk_buff *result;
  1848. spin_lock_irqsave(&list->lock, flags);
  1849. result = __skb_dequeue(list);
  1850. spin_unlock_irqrestore(&list->lock, flags);
  1851. return result;
  1852. }
  1853. EXPORT_SYMBOL(skb_dequeue);
  1854. /**
  1855. * skb_dequeue_tail - remove from the tail of the queue
  1856. * @list: list to dequeue from
  1857. *
  1858. * Remove the tail of the list. The list lock is taken so the function
  1859. * may be used safely with other locking list functions. The tail item is
  1860. * returned or %NULL if the list is empty.
  1861. */
  1862. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  1863. {
  1864. unsigned long flags;
  1865. struct sk_buff *result;
  1866. spin_lock_irqsave(&list->lock, flags);
  1867. result = __skb_dequeue_tail(list);
  1868. spin_unlock_irqrestore(&list->lock, flags);
  1869. return result;
  1870. }
  1871. EXPORT_SYMBOL(skb_dequeue_tail);
  1872. /**
  1873. * skb_queue_purge - empty a list
  1874. * @list: list to empty
  1875. *
  1876. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1877. * the list and one reference dropped. This function takes the list
  1878. * lock and is atomic with respect to other list locking functions.
  1879. */
  1880. void skb_queue_purge(struct sk_buff_head *list)
  1881. {
  1882. struct sk_buff *skb;
  1883. while ((skb = skb_dequeue(list)) != NULL)
  1884. kfree_skb(skb);
  1885. }
  1886. EXPORT_SYMBOL(skb_queue_purge);
  1887. /**
  1888. * skb_queue_head - queue a buffer at the list head
  1889. * @list: list to use
  1890. * @newsk: buffer to queue
  1891. *
  1892. * Queue a buffer at the start of the list. This function takes the
  1893. * list lock and can be used safely with other locking &sk_buff functions
  1894. * safely.
  1895. *
  1896. * A buffer cannot be placed on two lists at the same time.
  1897. */
  1898. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  1899. {
  1900. unsigned long flags;
  1901. spin_lock_irqsave(&list->lock, flags);
  1902. __skb_queue_head(list, newsk);
  1903. spin_unlock_irqrestore(&list->lock, flags);
  1904. }
  1905. EXPORT_SYMBOL(skb_queue_head);
  1906. /**
  1907. * skb_queue_tail - queue a buffer at the list tail
  1908. * @list: list to use
  1909. * @newsk: buffer to queue
  1910. *
  1911. * Queue a buffer at the tail of the list. This function takes the
  1912. * list lock and can be used safely with other locking &sk_buff functions
  1913. * safely.
  1914. *
  1915. * A buffer cannot be placed on two lists at the same time.
  1916. */
  1917. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  1918. {
  1919. unsigned long flags;
  1920. spin_lock_irqsave(&list->lock, flags);
  1921. __skb_queue_tail(list, newsk);
  1922. spin_unlock_irqrestore(&list->lock, flags);
  1923. }
  1924. EXPORT_SYMBOL(skb_queue_tail);
  1925. /**
  1926. * skb_unlink - remove a buffer from a list
  1927. * @skb: buffer to remove
  1928. * @list: list to use
  1929. *
  1930. * Remove a packet from a list. The list locks are taken and this
  1931. * function is atomic with respect to other list locked calls
  1932. *
  1933. * You must know what list the SKB is on.
  1934. */
  1935. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1936. {
  1937. unsigned long flags;
  1938. spin_lock_irqsave(&list->lock, flags);
  1939. __skb_unlink(skb, list);
  1940. spin_unlock_irqrestore(&list->lock, flags);
  1941. }
  1942. EXPORT_SYMBOL(skb_unlink);
  1943. /**
  1944. * skb_append - append a buffer
  1945. * @old: buffer to insert after
  1946. * @newsk: buffer to insert
  1947. * @list: list to use
  1948. *
  1949. * Place a packet after a given packet in a list. The list locks are taken
  1950. * and this function is atomic with respect to other list locked calls.
  1951. * A buffer cannot be placed on two lists at the same time.
  1952. */
  1953. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1954. {
  1955. unsigned long flags;
  1956. spin_lock_irqsave(&list->lock, flags);
  1957. __skb_queue_after(list, old, newsk);
  1958. spin_unlock_irqrestore(&list->lock, flags);
  1959. }
  1960. EXPORT_SYMBOL(skb_append);
  1961. /**
  1962. * skb_insert - insert a buffer
  1963. * @old: buffer to insert before
  1964. * @newsk: buffer to insert
  1965. * @list: list to use
  1966. *
  1967. * Place a packet before a given packet in a list. The list locks are
  1968. * taken and this function is atomic with respect to other list locked
  1969. * calls.
  1970. *
  1971. * A buffer cannot be placed on two lists at the same time.
  1972. */
  1973. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  1974. {
  1975. unsigned long flags;
  1976. spin_lock_irqsave(&list->lock, flags);
  1977. __skb_insert(newsk, old->prev, old, list);
  1978. spin_unlock_irqrestore(&list->lock, flags);
  1979. }
  1980. EXPORT_SYMBOL(skb_insert);
  1981. static inline void skb_split_inside_header(struct sk_buff *skb,
  1982. struct sk_buff* skb1,
  1983. const u32 len, const int pos)
  1984. {
  1985. int i;
  1986. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  1987. pos - len);
  1988. /* And move data appendix as is. */
  1989. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  1990. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  1991. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  1992. skb_shinfo(skb)->nr_frags = 0;
  1993. skb1->data_len = skb->data_len;
  1994. skb1->len += skb1->data_len;
  1995. skb->data_len = 0;
  1996. skb->len = len;
  1997. skb_set_tail_pointer(skb, len);
  1998. }
  1999. static inline void skb_split_no_header(struct sk_buff *skb,
  2000. struct sk_buff* skb1,
  2001. const u32 len, int pos)
  2002. {
  2003. int i, k = 0;
  2004. const int nfrags = skb_shinfo(skb)->nr_frags;
  2005. skb_shinfo(skb)->nr_frags = 0;
  2006. skb1->len = skb1->data_len = skb->len - len;
  2007. skb->len = len;
  2008. skb->data_len = len - pos;
  2009. for (i = 0; i < nfrags; i++) {
  2010. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2011. if (pos + size > len) {
  2012. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  2013. if (pos < len) {
  2014. /* Split frag.
  2015. * We have two variants in this case:
  2016. * 1. Move all the frag to the second
  2017. * part, if it is possible. F.e.
  2018. * this approach is mandatory for TUX,
  2019. * where splitting is expensive.
  2020. * 2. Split is accurately. We make this.
  2021. */
  2022. skb_frag_ref(skb, i);
  2023. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  2024. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  2025. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  2026. skb_shinfo(skb)->nr_frags++;
  2027. }
  2028. k++;
  2029. } else
  2030. skb_shinfo(skb)->nr_frags++;
  2031. pos += size;
  2032. }
  2033. skb_shinfo(skb1)->nr_frags = k;
  2034. }
  2035. /**
  2036. * skb_split - Split fragmented skb to two parts at length len.
  2037. * @skb: the buffer to split
  2038. * @skb1: the buffer to receive the second part
  2039. * @len: new length for skb
  2040. */
  2041. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2042. {
  2043. int pos = skb_headlen(skb);
  2044. skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
  2045. if (len < pos) /* Split line is inside header. */
  2046. skb_split_inside_header(skb, skb1, len, pos);
  2047. else /* Second chunk has no header, nothing to copy. */
  2048. skb_split_no_header(skb, skb1, len, pos);
  2049. }
  2050. EXPORT_SYMBOL(skb_split);
  2051. /* Shifting from/to a cloned skb is a no-go.
  2052. *
  2053. * Caller cannot keep skb_shinfo related pointers past calling here!
  2054. */
  2055. static int skb_prepare_for_shift(struct sk_buff *skb)
  2056. {
  2057. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2058. }
  2059. /**
  2060. * skb_shift - Shifts paged data partially from skb to another
  2061. * @tgt: buffer into which tail data gets added
  2062. * @skb: buffer from which the paged data comes from
  2063. * @shiftlen: shift up to this many bytes
  2064. *
  2065. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2066. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2067. * It's up to caller to free skb if everything was shifted.
  2068. *
  2069. * If @tgt runs out of frags, the whole operation is aborted.
  2070. *
  2071. * Skb cannot include anything else but paged data while tgt is allowed
  2072. * to have non-paged data as well.
  2073. *
  2074. * TODO: full sized shift could be optimized but that would need
  2075. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2076. */
  2077. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2078. {
  2079. int from, to, merge, todo;
  2080. struct skb_frag_struct *fragfrom, *fragto;
  2081. BUG_ON(shiftlen > skb->len);
  2082. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2083. todo = shiftlen;
  2084. from = 0;
  2085. to = skb_shinfo(tgt)->nr_frags;
  2086. fragfrom = &skb_shinfo(skb)->frags[from];
  2087. /* Actual merge is delayed until the point when we know we can
  2088. * commit all, so that we don't have to undo partial changes
  2089. */
  2090. if (!to ||
  2091. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2092. fragfrom->page_offset)) {
  2093. merge = -1;
  2094. } else {
  2095. merge = to - 1;
  2096. todo -= skb_frag_size(fragfrom);
  2097. if (todo < 0) {
  2098. if (skb_prepare_for_shift(skb) ||
  2099. skb_prepare_for_shift(tgt))
  2100. return 0;
  2101. /* All previous frag pointers might be stale! */
  2102. fragfrom = &skb_shinfo(skb)->frags[from];
  2103. fragto = &skb_shinfo(tgt)->frags[merge];
  2104. skb_frag_size_add(fragto, shiftlen);
  2105. skb_frag_size_sub(fragfrom, shiftlen);
  2106. fragfrom->page_offset += shiftlen;
  2107. goto onlymerged;
  2108. }
  2109. from++;
  2110. }
  2111. /* Skip full, not-fitting skb to avoid expensive operations */
  2112. if ((shiftlen == skb->len) &&
  2113. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2114. return 0;
  2115. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2116. return 0;
  2117. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2118. if (to == MAX_SKB_FRAGS)
  2119. return 0;
  2120. fragfrom = &skb_shinfo(skb)->frags[from];
  2121. fragto = &skb_shinfo(tgt)->frags[to];
  2122. if (todo >= skb_frag_size(fragfrom)) {
  2123. *fragto = *fragfrom;
  2124. todo -= skb_frag_size(fragfrom);
  2125. from++;
  2126. to++;
  2127. } else {
  2128. __skb_frag_ref(fragfrom);
  2129. fragto->page = fragfrom->page;
  2130. fragto->page_offset = fragfrom->page_offset;
  2131. skb_frag_size_set(fragto, todo);
  2132. fragfrom->page_offset += todo;
  2133. skb_frag_size_sub(fragfrom, todo);
  2134. todo = 0;
  2135. to++;
  2136. break;
  2137. }
  2138. }
  2139. /* Ready to "commit" this state change to tgt */
  2140. skb_shinfo(tgt)->nr_frags = to;
  2141. if (merge >= 0) {
  2142. fragfrom = &skb_shinfo(skb)->frags[0];
  2143. fragto = &skb_shinfo(tgt)->frags[merge];
  2144. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2145. __skb_frag_unref(fragfrom);
  2146. }
  2147. /* Reposition in the original skb */
  2148. to = 0;
  2149. while (from < skb_shinfo(skb)->nr_frags)
  2150. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2151. skb_shinfo(skb)->nr_frags = to;
  2152. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2153. onlymerged:
  2154. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2155. * the other hand might need it if it needs to be resent
  2156. */
  2157. tgt->ip_summed = CHECKSUM_PARTIAL;
  2158. skb->ip_summed = CHECKSUM_PARTIAL;
  2159. /* Yak, is it really working this way? Some helper please? */
  2160. skb->len -= shiftlen;
  2161. skb->data_len -= shiftlen;
  2162. skb->truesize -= shiftlen;
  2163. tgt->len += shiftlen;
  2164. tgt->data_len += shiftlen;
  2165. tgt->truesize += shiftlen;
  2166. return shiftlen;
  2167. }
  2168. /**
  2169. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2170. * @skb: the buffer to read
  2171. * @from: lower offset of data to be read
  2172. * @to: upper offset of data to be read
  2173. * @st: state variable
  2174. *
  2175. * Initializes the specified state variable. Must be called before
  2176. * invoking skb_seq_read() for the first time.
  2177. */
  2178. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2179. unsigned int to, struct skb_seq_state *st)
  2180. {
  2181. st->lower_offset = from;
  2182. st->upper_offset = to;
  2183. st->root_skb = st->cur_skb = skb;
  2184. st->frag_idx = st->stepped_offset = 0;
  2185. st->frag_data = NULL;
  2186. }
  2187. EXPORT_SYMBOL(skb_prepare_seq_read);
  2188. /**
  2189. * skb_seq_read - Sequentially read skb data
  2190. * @consumed: number of bytes consumed by the caller so far
  2191. * @data: destination pointer for data to be returned
  2192. * @st: state variable
  2193. *
  2194. * Reads a block of skb data at &consumed relative to the
  2195. * lower offset specified to skb_prepare_seq_read(). Assigns
  2196. * the head of the data block to &data and returns the length
  2197. * of the block or 0 if the end of the skb data or the upper
  2198. * offset has been reached.
  2199. *
  2200. * The caller is not required to consume all of the data
  2201. * returned, i.e. &consumed is typically set to the number
  2202. * of bytes already consumed and the next call to
  2203. * skb_seq_read() will return the remaining part of the block.
  2204. *
  2205. * Note 1: The size of each block of data returned can be arbitrary,
  2206. * this limitation is the cost for zerocopy seqeuental
  2207. * reads of potentially non linear data.
  2208. *
  2209. * Note 2: Fragment lists within fragments are not implemented
  2210. * at the moment, state->root_skb could be replaced with
  2211. * a stack for this purpose.
  2212. */
  2213. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2214. struct skb_seq_state *st)
  2215. {
  2216. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2217. skb_frag_t *frag;
  2218. if (unlikely(abs_offset >= st->upper_offset)) {
  2219. if (st->frag_data) {
  2220. kunmap_atomic(st->frag_data);
  2221. st->frag_data = NULL;
  2222. }
  2223. return 0;
  2224. }
  2225. next_skb:
  2226. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2227. if (abs_offset < block_limit && !st->frag_data) {
  2228. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2229. return block_limit - abs_offset;
  2230. }
  2231. if (st->frag_idx == 0 && !st->frag_data)
  2232. st->stepped_offset += skb_headlen(st->cur_skb);
  2233. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2234. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2235. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2236. if (abs_offset < block_limit) {
  2237. if (!st->frag_data)
  2238. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2239. *data = (u8 *) st->frag_data + frag->page_offset +
  2240. (abs_offset - st->stepped_offset);
  2241. return block_limit - abs_offset;
  2242. }
  2243. if (st->frag_data) {
  2244. kunmap_atomic(st->frag_data);
  2245. st->frag_data = NULL;
  2246. }
  2247. st->frag_idx++;
  2248. st->stepped_offset += skb_frag_size(frag);
  2249. }
  2250. if (st->frag_data) {
  2251. kunmap_atomic(st->frag_data);
  2252. st->frag_data = NULL;
  2253. }
  2254. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2255. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2256. st->frag_idx = 0;
  2257. goto next_skb;
  2258. } else if (st->cur_skb->next) {
  2259. st->cur_skb = st->cur_skb->next;
  2260. st->frag_idx = 0;
  2261. goto next_skb;
  2262. }
  2263. return 0;
  2264. }
  2265. EXPORT_SYMBOL(skb_seq_read);
  2266. /**
  2267. * skb_abort_seq_read - Abort a sequential read of skb data
  2268. * @st: state variable
  2269. *
  2270. * Must be called if skb_seq_read() was not called until it
  2271. * returned 0.
  2272. */
  2273. void skb_abort_seq_read(struct skb_seq_state *st)
  2274. {
  2275. if (st->frag_data)
  2276. kunmap_atomic(st->frag_data);
  2277. }
  2278. EXPORT_SYMBOL(skb_abort_seq_read);
  2279. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2280. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2281. struct ts_config *conf,
  2282. struct ts_state *state)
  2283. {
  2284. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2285. }
  2286. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2287. {
  2288. skb_abort_seq_read(TS_SKB_CB(state));
  2289. }
  2290. /**
  2291. * skb_find_text - Find a text pattern in skb data
  2292. * @skb: the buffer to look in
  2293. * @from: search offset
  2294. * @to: search limit
  2295. * @config: textsearch configuration
  2296. * @state: uninitialized textsearch state variable
  2297. *
  2298. * Finds a pattern in the skb data according to the specified
  2299. * textsearch configuration. Use textsearch_next() to retrieve
  2300. * subsequent occurrences of the pattern. Returns the offset
  2301. * to the first occurrence or UINT_MAX if no match was found.
  2302. */
  2303. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2304. unsigned int to, struct ts_config *config,
  2305. struct ts_state *state)
  2306. {
  2307. unsigned int ret;
  2308. config->get_next_block = skb_ts_get_next_block;
  2309. config->finish = skb_ts_finish;
  2310. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
  2311. ret = textsearch_find(config, state);
  2312. return (ret <= to - from ? ret : UINT_MAX);
  2313. }
  2314. EXPORT_SYMBOL(skb_find_text);
  2315. /**
  2316. * skb_append_datato_frags - append the user data to a skb
  2317. * @sk: sock structure
  2318. * @skb: skb structure to be appened with user data.
  2319. * @getfrag: call back function to be used for getting the user data
  2320. * @from: pointer to user message iov
  2321. * @length: length of the iov message
  2322. *
  2323. * Description: This procedure append the user data in the fragment part
  2324. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2325. */
  2326. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2327. int (*getfrag)(void *from, char *to, int offset,
  2328. int len, int odd, struct sk_buff *skb),
  2329. void *from, int length)
  2330. {
  2331. int frg_cnt = skb_shinfo(skb)->nr_frags;
  2332. int copy;
  2333. int offset = 0;
  2334. int ret;
  2335. struct page_frag *pfrag = &current->task_frag;
  2336. do {
  2337. /* Return error if we don't have space for new frag */
  2338. if (frg_cnt >= MAX_SKB_FRAGS)
  2339. return -EMSGSIZE;
  2340. if (!sk_page_frag_refill(sk, pfrag))
  2341. return -ENOMEM;
  2342. /* copy the user data to page */
  2343. copy = min_t(int, length, pfrag->size - pfrag->offset);
  2344. ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
  2345. offset, copy, 0, skb);
  2346. if (ret < 0)
  2347. return -EFAULT;
  2348. /* copy was successful so update the size parameters */
  2349. skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
  2350. copy);
  2351. frg_cnt++;
  2352. pfrag->offset += copy;
  2353. get_page(pfrag->page);
  2354. skb->truesize += copy;
  2355. atomic_add(copy, &sk->sk_wmem_alloc);
  2356. skb->len += copy;
  2357. skb->data_len += copy;
  2358. offset += copy;
  2359. length -= copy;
  2360. } while (length > 0);
  2361. return 0;
  2362. }
  2363. EXPORT_SYMBOL(skb_append_datato_frags);
  2364. /**
  2365. * skb_pull_rcsum - pull skb and update receive checksum
  2366. * @skb: buffer to update
  2367. * @len: length of data pulled
  2368. *
  2369. * This function performs an skb_pull on the packet and updates
  2370. * the CHECKSUM_COMPLETE checksum. It should be used on
  2371. * receive path processing instead of skb_pull unless you know
  2372. * that the checksum difference is zero (e.g., a valid IP header)
  2373. * or you are setting ip_summed to CHECKSUM_NONE.
  2374. */
  2375. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2376. {
  2377. BUG_ON(len > skb->len);
  2378. skb->len -= len;
  2379. BUG_ON(skb->len < skb->data_len);
  2380. skb_postpull_rcsum(skb, skb->data, len);
  2381. return skb->data += len;
  2382. }
  2383. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2384. /**
  2385. * skb_segment - Perform protocol segmentation on skb.
  2386. * @skb: buffer to segment
  2387. * @features: features for the output path (see dev->features)
  2388. *
  2389. * This function performs segmentation on the given skb. It returns
  2390. * a pointer to the first in a list of new skbs for the segments.
  2391. * In case of error it returns ERR_PTR(err).
  2392. */
  2393. struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
  2394. {
  2395. struct sk_buff *segs = NULL;
  2396. struct sk_buff *tail = NULL;
  2397. struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
  2398. unsigned int mss = skb_shinfo(skb)->gso_size;
  2399. unsigned int doffset = skb->data - skb_mac_header(skb);
  2400. unsigned int offset = doffset;
  2401. unsigned int tnl_hlen = skb_tnl_header_len(skb);
  2402. unsigned int headroom;
  2403. unsigned int len;
  2404. __be16 proto;
  2405. bool csum;
  2406. int sg = !!(features & NETIF_F_SG);
  2407. int nfrags = skb_shinfo(skb)->nr_frags;
  2408. int err = -ENOMEM;
  2409. int i = 0;
  2410. int pos;
  2411. proto = skb_network_protocol(skb);
  2412. if (unlikely(!proto))
  2413. return ERR_PTR(-EINVAL);
  2414. csum = !!can_checksum_protocol(features, proto);
  2415. __skb_push(skb, doffset);
  2416. headroom = skb_headroom(skb);
  2417. pos = skb_headlen(skb);
  2418. do {
  2419. struct sk_buff *nskb;
  2420. skb_frag_t *frag;
  2421. int hsize;
  2422. int size;
  2423. len = skb->len - offset;
  2424. if (len > mss)
  2425. len = mss;
  2426. hsize = skb_headlen(skb) - offset;
  2427. if (hsize < 0)
  2428. hsize = 0;
  2429. if (hsize > len || !sg)
  2430. hsize = len;
  2431. if (!hsize && i >= nfrags) {
  2432. BUG_ON(fskb->len != len);
  2433. pos += len;
  2434. nskb = skb_clone(fskb, GFP_ATOMIC);
  2435. fskb = fskb->next;
  2436. if (unlikely(!nskb))
  2437. goto err;
  2438. hsize = skb_end_offset(nskb);
  2439. if (skb_cow_head(nskb, doffset + headroom)) {
  2440. kfree_skb(nskb);
  2441. goto err;
  2442. }
  2443. nskb->truesize += skb_end_offset(nskb) - hsize;
  2444. skb_release_head_state(nskb);
  2445. __skb_push(nskb, doffset);
  2446. } else {
  2447. nskb = __alloc_skb(hsize + doffset + headroom,
  2448. GFP_ATOMIC, skb_alloc_rx_flag(skb),
  2449. NUMA_NO_NODE);
  2450. if (unlikely(!nskb))
  2451. goto err;
  2452. skb_reserve(nskb, headroom);
  2453. __skb_put(nskb, doffset);
  2454. }
  2455. if (segs)
  2456. tail->next = nskb;
  2457. else
  2458. segs = nskb;
  2459. tail = nskb;
  2460. __copy_skb_header(nskb, skb);
  2461. nskb->mac_len = skb->mac_len;
  2462. /* nskb and skb might have different headroom */
  2463. if (nskb->ip_summed == CHECKSUM_PARTIAL)
  2464. nskb->csum_start += skb_headroom(nskb) - headroom;
  2465. skb_reset_mac_header(nskb);
  2466. skb_set_network_header(nskb, skb->mac_len);
  2467. nskb->transport_header = (nskb->network_header +
  2468. skb_network_header_len(skb));
  2469. skb_copy_from_linear_data_offset(skb, -tnl_hlen,
  2470. nskb->data - tnl_hlen,
  2471. doffset + tnl_hlen);
  2472. if (fskb != skb_shinfo(skb)->frag_list)
  2473. goto perform_csum_check;
  2474. if (!sg) {
  2475. nskb->ip_summed = CHECKSUM_NONE;
  2476. nskb->csum = skb_copy_and_csum_bits(skb, offset,
  2477. skb_put(nskb, len),
  2478. len, 0);
  2479. continue;
  2480. }
  2481. frag = skb_shinfo(nskb)->frags;
  2482. skb_copy_from_linear_data_offset(skb, offset,
  2483. skb_put(nskb, hsize), hsize);
  2484. skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
  2485. while (pos < offset + len && i < nfrags) {
  2486. *frag = skb_shinfo(skb)->frags[i];
  2487. __skb_frag_ref(frag);
  2488. size = skb_frag_size(frag);
  2489. if (pos < offset) {
  2490. frag->page_offset += offset - pos;
  2491. skb_frag_size_sub(frag, offset - pos);
  2492. }
  2493. skb_shinfo(nskb)->nr_frags++;
  2494. if (pos + size <= offset + len) {
  2495. i++;
  2496. pos += size;
  2497. } else {
  2498. skb_frag_size_sub(frag, pos + size - (offset + len));
  2499. goto skip_fraglist;
  2500. }
  2501. frag++;
  2502. }
  2503. if (pos < offset + len) {
  2504. struct sk_buff *fskb2 = fskb;
  2505. BUG_ON(pos + fskb->len != offset + len);
  2506. pos += fskb->len;
  2507. fskb = fskb->next;
  2508. if (fskb2->next) {
  2509. fskb2 = skb_clone(fskb2, GFP_ATOMIC);
  2510. if (!fskb2)
  2511. goto err;
  2512. } else
  2513. skb_get(fskb2);
  2514. SKB_FRAG_ASSERT(nskb);
  2515. skb_shinfo(nskb)->frag_list = fskb2;
  2516. }
  2517. skip_fraglist:
  2518. nskb->data_len = len - hsize;
  2519. nskb->len += nskb->data_len;
  2520. nskb->truesize += nskb->data_len;
  2521. perform_csum_check:
  2522. if (!csum) {
  2523. nskb->csum = skb_checksum(nskb, doffset,
  2524. nskb->len - doffset, 0);
  2525. nskb->ip_summed = CHECKSUM_NONE;
  2526. }
  2527. } while ((offset += len) < skb->len);
  2528. return segs;
  2529. err:
  2530. while ((skb = segs)) {
  2531. segs = skb->next;
  2532. kfree_skb(skb);
  2533. }
  2534. return ERR_PTR(err);
  2535. }
  2536. EXPORT_SYMBOL_GPL(skb_segment);
  2537. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2538. {
  2539. struct sk_buff *p = *head;
  2540. struct sk_buff *nskb;
  2541. struct skb_shared_info *skbinfo = skb_shinfo(skb);
  2542. struct skb_shared_info *pinfo = skb_shinfo(p);
  2543. unsigned int headroom;
  2544. unsigned int len = skb_gro_len(skb);
  2545. unsigned int offset = skb_gro_offset(skb);
  2546. unsigned int headlen = skb_headlen(skb);
  2547. unsigned int delta_truesize;
  2548. if (p->len + len >= 65536)
  2549. return -E2BIG;
  2550. if (pinfo->frag_list)
  2551. goto merge;
  2552. else if (headlen <= offset) {
  2553. skb_frag_t *frag;
  2554. skb_frag_t *frag2;
  2555. int i = skbinfo->nr_frags;
  2556. int nr_frags = pinfo->nr_frags + i;
  2557. offset -= headlen;
  2558. if (nr_frags > MAX_SKB_FRAGS)
  2559. return -E2BIG;
  2560. pinfo->nr_frags = nr_frags;
  2561. skbinfo->nr_frags = 0;
  2562. frag = pinfo->frags + nr_frags;
  2563. frag2 = skbinfo->frags + i;
  2564. do {
  2565. *--frag = *--frag2;
  2566. } while (--i);
  2567. frag->page_offset += offset;
  2568. skb_frag_size_sub(frag, offset);
  2569. /* all fragments truesize : remove (head size + sk_buff) */
  2570. delta_truesize = skb->truesize -
  2571. SKB_TRUESIZE(skb_end_offset(skb));
  2572. skb->truesize -= skb->data_len;
  2573. skb->len -= skb->data_len;
  2574. skb->data_len = 0;
  2575. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  2576. goto done;
  2577. } else if (skb->head_frag) {
  2578. int nr_frags = pinfo->nr_frags;
  2579. skb_frag_t *frag = pinfo->frags + nr_frags;
  2580. struct page *page = virt_to_head_page(skb->head);
  2581. unsigned int first_size = headlen - offset;
  2582. unsigned int first_offset;
  2583. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  2584. return -E2BIG;
  2585. first_offset = skb->data -
  2586. (unsigned char *)page_address(page) +
  2587. offset;
  2588. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  2589. frag->page.p = page;
  2590. frag->page_offset = first_offset;
  2591. skb_frag_size_set(frag, first_size);
  2592. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  2593. /* We dont need to clear skbinfo->nr_frags here */
  2594. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2595. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  2596. goto done;
  2597. } else if (skb_gro_len(p) != pinfo->gso_size)
  2598. return -E2BIG;
  2599. headroom = skb_headroom(p);
  2600. nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
  2601. if (unlikely(!nskb))
  2602. return -ENOMEM;
  2603. __copy_skb_header(nskb, p);
  2604. nskb->mac_len = p->mac_len;
  2605. skb_reserve(nskb, headroom);
  2606. __skb_put(nskb, skb_gro_offset(p));
  2607. skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
  2608. skb_set_network_header(nskb, skb_network_offset(p));
  2609. skb_set_transport_header(nskb, skb_transport_offset(p));
  2610. __skb_pull(p, skb_gro_offset(p));
  2611. memcpy(skb_mac_header(nskb), skb_mac_header(p),
  2612. p->data - skb_mac_header(p));
  2613. skb_shinfo(nskb)->frag_list = p;
  2614. skb_shinfo(nskb)->gso_size = pinfo->gso_size;
  2615. pinfo->gso_size = 0;
  2616. skb_header_release(p);
  2617. NAPI_GRO_CB(nskb)->last = p;
  2618. nskb->data_len += p->len;
  2619. nskb->truesize += p->truesize;
  2620. nskb->len += p->len;
  2621. *head = nskb;
  2622. nskb->next = p->next;
  2623. p->next = NULL;
  2624. p = nskb;
  2625. merge:
  2626. delta_truesize = skb->truesize;
  2627. if (offset > headlen) {
  2628. unsigned int eat = offset - headlen;
  2629. skbinfo->frags[0].page_offset += eat;
  2630. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2631. skb->data_len -= eat;
  2632. skb->len -= eat;
  2633. offset = headlen;
  2634. }
  2635. __skb_pull(skb, offset);
  2636. NAPI_GRO_CB(p)->last->next = skb;
  2637. NAPI_GRO_CB(p)->last = skb;
  2638. skb_header_release(skb);
  2639. done:
  2640. NAPI_GRO_CB(p)->count++;
  2641. p->data_len += len;
  2642. p->truesize += delta_truesize;
  2643. p->len += len;
  2644. NAPI_GRO_CB(skb)->same_flow = 1;
  2645. return 0;
  2646. }
  2647. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2648. void __init skb_init(void)
  2649. {
  2650. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2651. sizeof(struct sk_buff),
  2652. 0,
  2653. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2654. NULL);
  2655. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2656. (2*sizeof(struct sk_buff)) +
  2657. sizeof(atomic_t),
  2658. 0,
  2659. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2660. NULL);
  2661. }
  2662. /**
  2663. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2664. * @skb: Socket buffer containing the buffers to be mapped
  2665. * @sg: The scatter-gather list to map into
  2666. * @offset: The offset into the buffer's contents to start mapping
  2667. * @len: Length of buffer space to be mapped
  2668. *
  2669. * Fill the specified scatter-gather list with mappings/pointers into a
  2670. * region of the buffer space attached to a socket buffer.
  2671. */
  2672. static int
  2673. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2674. {
  2675. int start = skb_headlen(skb);
  2676. int i, copy = start - offset;
  2677. struct sk_buff *frag_iter;
  2678. int elt = 0;
  2679. if (copy > 0) {
  2680. if (copy > len)
  2681. copy = len;
  2682. sg_set_buf(sg, skb->data + offset, copy);
  2683. elt++;
  2684. if ((len -= copy) == 0)
  2685. return elt;
  2686. offset += copy;
  2687. }
  2688. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2689. int end;
  2690. WARN_ON(start > offset + len);
  2691. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2692. if ((copy = end - offset) > 0) {
  2693. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2694. if (copy > len)
  2695. copy = len;
  2696. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2697. frag->page_offset+offset-start);
  2698. elt++;
  2699. if (!(len -= copy))
  2700. return elt;
  2701. offset += copy;
  2702. }
  2703. start = end;
  2704. }
  2705. skb_walk_frags(skb, frag_iter) {
  2706. int end;
  2707. WARN_ON(start > offset + len);
  2708. end = start + frag_iter->len;
  2709. if ((copy = end - offset) > 0) {
  2710. if (copy > len)
  2711. copy = len;
  2712. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  2713. copy);
  2714. if ((len -= copy) == 0)
  2715. return elt;
  2716. offset += copy;
  2717. }
  2718. start = end;
  2719. }
  2720. BUG_ON(len);
  2721. return elt;
  2722. }
  2723. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2724. {
  2725. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  2726. sg_mark_end(&sg[nsg - 1]);
  2727. return nsg;
  2728. }
  2729. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  2730. /**
  2731. * skb_cow_data - Check that a socket buffer's data buffers are writable
  2732. * @skb: The socket buffer to check.
  2733. * @tailbits: Amount of trailing space to be added
  2734. * @trailer: Returned pointer to the skb where the @tailbits space begins
  2735. *
  2736. * Make sure that the data buffers attached to a socket buffer are
  2737. * writable. If they are not, private copies are made of the data buffers
  2738. * and the socket buffer is set to use these instead.
  2739. *
  2740. * If @tailbits is given, make sure that there is space to write @tailbits
  2741. * bytes of data beyond current end of socket buffer. @trailer will be
  2742. * set to point to the skb in which this space begins.
  2743. *
  2744. * The number of scatterlist elements required to completely map the
  2745. * COW'd and extended socket buffer will be returned.
  2746. */
  2747. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  2748. {
  2749. int copyflag;
  2750. int elt;
  2751. struct sk_buff *skb1, **skb_p;
  2752. /* If skb is cloned or its head is paged, reallocate
  2753. * head pulling out all the pages (pages are considered not writable
  2754. * at the moment even if they are anonymous).
  2755. */
  2756. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  2757. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  2758. return -ENOMEM;
  2759. /* Easy case. Most of packets will go this way. */
  2760. if (!skb_has_frag_list(skb)) {
  2761. /* A little of trouble, not enough of space for trailer.
  2762. * This should not happen, when stack is tuned to generate
  2763. * good frames. OK, on miss we reallocate and reserve even more
  2764. * space, 128 bytes is fair. */
  2765. if (skb_tailroom(skb) < tailbits &&
  2766. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  2767. return -ENOMEM;
  2768. /* Voila! */
  2769. *trailer = skb;
  2770. return 1;
  2771. }
  2772. /* Misery. We are in troubles, going to mincer fragments... */
  2773. elt = 1;
  2774. skb_p = &skb_shinfo(skb)->frag_list;
  2775. copyflag = 0;
  2776. while ((skb1 = *skb_p) != NULL) {
  2777. int ntail = 0;
  2778. /* The fragment is partially pulled by someone,
  2779. * this can happen on input. Copy it and everything
  2780. * after it. */
  2781. if (skb_shared(skb1))
  2782. copyflag = 1;
  2783. /* If the skb is the last, worry about trailer. */
  2784. if (skb1->next == NULL && tailbits) {
  2785. if (skb_shinfo(skb1)->nr_frags ||
  2786. skb_has_frag_list(skb1) ||
  2787. skb_tailroom(skb1) < tailbits)
  2788. ntail = tailbits + 128;
  2789. }
  2790. if (copyflag ||
  2791. skb_cloned(skb1) ||
  2792. ntail ||
  2793. skb_shinfo(skb1)->nr_frags ||
  2794. skb_has_frag_list(skb1)) {
  2795. struct sk_buff *skb2;
  2796. /* Fuck, we are miserable poor guys... */
  2797. if (ntail == 0)
  2798. skb2 = skb_copy(skb1, GFP_ATOMIC);
  2799. else
  2800. skb2 = skb_copy_expand(skb1,
  2801. skb_headroom(skb1),
  2802. ntail,
  2803. GFP_ATOMIC);
  2804. if (unlikely(skb2 == NULL))
  2805. return -ENOMEM;
  2806. if (skb1->sk)
  2807. skb_set_owner_w(skb2, skb1->sk);
  2808. /* Looking around. Are we still alive?
  2809. * OK, link new skb, drop old one */
  2810. skb2->next = skb1->next;
  2811. *skb_p = skb2;
  2812. kfree_skb(skb1);
  2813. skb1 = skb2;
  2814. }
  2815. elt++;
  2816. *trailer = skb1;
  2817. skb_p = &skb1->next;
  2818. }
  2819. return elt;
  2820. }
  2821. EXPORT_SYMBOL_GPL(skb_cow_data);
  2822. static void sock_rmem_free(struct sk_buff *skb)
  2823. {
  2824. struct sock *sk = skb->sk;
  2825. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  2826. }
  2827. /*
  2828. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  2829. */
  2830. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  2831. {
  2832. int len = skb->len;
  2833. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  2834. (unsigned int)sk->sk_rcvbuf)
  2835. return -ENOMEM;
  2836. skb_orphan(skb);
  2837. skb->sk = sk;
  2838. skb->destructor = sock_rmem_free;
  2839. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  2840. /* before exiting rcu section, make sure dst is refcounted */
  2841. skb_dst_force(skb);
  2842. skb_queue_tail(&sk->sk_error_queue, skb);
  2843. if (!sock_flag(sk, SOCK_DEAD))
  2844. sk->sk_data_ready(sk, len);
  2845. return 0;
  2846. }
  2847. EXPORT_SYMBOL(sock_queue_err_skb);
  2848. void skb_tstamp_tx(struct sk_buff *orig_skb,
  2849. struct skb_shared_hwtstamps *hwtstamps)
  2850. {
  2851. struct sock *sk = orig_skb->sk;
  2852. struct sock_exterr_skb *serr;
  2853. struct sk_buff *skb;
  2854. int err;
  2855. if (!sk)
  2856. return;
  2857. if (hwtstamps) {
  2858. *skb_hwtstamps(orig_skb) =
  2859. *hwtstamps;
  2860. } else {
  2861. /*
  2862. * no hardware time stamps available,
  2863. * so keep the shared tx_flags and only
  2864. * store software time stamp
  2865. */
  2866. orig_skb->tstamp = ktime_get_real();
  2867. }
  2868. skb = skb_clone(orig_skb, GFP_ATOMIC);
  2869. if (!skb)
  2870. return;
  2871. serr = SKB_EXT_ERR(skb);
  2872. memset(serr, 0, sizeof(*serr));
  2873. serr->ee.ee_errno = ENOMSG;
  2874. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  2875. err = sock_queue_err_skb(sk, skb);
  2876. if (err)
  2877. kfree_skb(skb);
  2878. }
  2879. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  2880. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  2881. {
  2882. struct sock *sk = skb->sk;
  2883. struct sock_exterr_skb *serr;
  2884. int err;
  2885. skb->wifi_acked_valid = 1;
  2886. skb->wifi_acked = acked;
  2887. serr = SKB_EXT_ERR(skb);
  2888. memset(serr, 0, sizeof(*serr));
  2889. serr->ee.ee_errno = ENOMSG;
  2890. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  2891. err = sock_queue_err_skb(sk, skb);
  2892. if (err)
  2893. kfree_skb(skb);
  2894. }
  2895. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  2896. /**
  2897. * skb_partial_csum_set - set up and verify partial csum values for packet
  2898. * @skb: the skb to set
  2899. * @start: the number of bytes after skb->data to start checksumming.
  2900. * @off: the offset from start to place the checksum.
  2901. *
  2902. * For untrusted partially-checksummed packets, we need to make sure the values
  2903. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  2904. *
  2905. * This function checks and sets those values and skb->ip_summed: if this
  2906. * returns false you should drop the packet.
  2907. */
  2908. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  2909. {
  2910. if (unlikely(start > skb_headlen(skb)) ||
  2911. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  2912. net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
  2913. start, off, skb_headlen(skb));
  2914. return false;
  2915. }
  2916. skb->ip_summed = CHECKSUM_PARTIAL;
  2917. skb->csum_start = skb_headroom(skb) + start;
  2918. skb->csum_offset = off;
  2919. skb_set_transport_header(skb, start);
  2920. return true;
  2921. }
  2922. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  2923. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  2924. {
  2925. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  2926. skb->dev->name);
  2927. }
  2928. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  2929. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  2930. {
  2931. if (head_stolen) {
  2932. skb_release_head_state(skb);
  2933. kmem_cache_free(skbuff_head_cache, skb);
  2934. } else {
  2935. __kfree_skb(skb);
  2936. }
  2937. }
  2938. EXPORT_SYMBOL(kfree_skb_partial);
  2939. /**
  2940. * skb_try_coalesce - try to merge skb to prior one
  2941. * @to: prior buffer
  2942. * @from: buffer to add
  2943. * @fragstolen: pointer to boolean
  2944. * @delta_truesize: how much more was allocated than was requested
  2945. */
  2946. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  2947. bool *fragstolen, int *delta_truesize)
  2948. {
  2949. int i, delta, len = from->len;
  2950. *fragstolen = false;
  2951. if (skb_cloned(to))
  2952. return false;
  2953. if (len <= skb_tailroom(to)) {
  2954. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  2955. *delta_truesize = 0;
  2956. return true;
  2957. }
  2958. if (skb_has_frag_list(to) || skb_has_frag_list(from))
  2959. return false;
  2960. if (skb_headlen(from) != 0) {
  2961. struct page *page;
  2962. unsigned int offset;
  2963. if (skb_shinfo(to)->nr_frags +
  2964. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  2965. return false;
  2966. if (skb_head_is_locked(from))
  2967. return false;
  2968. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2969. page = virt_to_head_page(from->head);
  2970. offset = from->data - (unsigned char *)page_address(page);
  2971. skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
  2972. page, offset, skb_headlen(from));
  2973. *fragstolen = true;
  2974. } else {
  2975. if (skb_shinfo(to)->nr_frags +
  2976. skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
  2977. return false;
  2978. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  2979. }
  2980. WARN_ON_ONCE(delta < len);
  2981. memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
  2982. skb_shinfo(from)->frags,
  2983. skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
  2984. skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
  2985. if (!skb_cloned(from))
  2986. skb_shinfo(from)->nr_frags = 0;
  2987. /* if the skb is not cloned this does nothing
  2988. * since we set nr_frags to 0.
  2989. */
  2990. for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
  2991. skb_frag_ref(from, i);
  2992. to->truesize += delta;
  2993. to->len += len;
  2994. to->data_len += len;
  2995. *delta_truesize = delta;
  2996. return true;
  2997. }
  2998. EXPORT_SYMBOL(skb_try_coalesce);
  2999. /**
  3000. * skb_scrub_packet - scrub an skb before sending it to another netns
  3001. *
  3002. * @skb: buffer to clean
  3003. *
  3004. * skb_scrub_packet can be used to clean an skb before injecting it in
  3005. * another namespace. We have to clear all information in the skb that
  3006. * could impact namespace isolation.
  3007. */
  3008. void skb_scrub_packet(struct sk_buff *skb)
  3009. {
  3010. skb_orphan(skb);
  3011. skb->tstamp.tv64 = 0;
  3012. skb->pkt_type = PACKET_HOST;
  3013. skb->skb_iif = 0;
  3014. skb_dst_drop(skb);
  3015. skb->mark = 0;
  3016. secpath_reset(skb);
  3017. nf_reset(skb);
  3018. nf_reset_trace(skb);
  3019. }
  3020. EXPORT_SYMBOL_GPL(skb_scrub_packet);