skbuff.c 87 KB

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