skbuff.c 97 KB

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