skbuff.c 87 KB

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