skbuff.c 105 KB

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