skbuff.c 87 KB

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