skbuff.c 86 KB

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