skbuff.c 97 KB

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