skbuff.c 121 KB

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