skbuff.c 88 KB

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