skbuff.c 98 KB

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