skbuff.c 88 KB

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