skbuff.c 89 KB

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