skbuff.c 87 KB

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