skbuff.c 87 KB

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