skbuff.c 87 KB

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