skbuff.c 122 KB

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