skbuff.c 87 KB

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