skbuff.c 86 KB

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