skbuff.c 113 KB

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