skbuff.c 113 KB

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