skbuff.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885
  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. ssize_t skb_socket_splice(struct sock *sk,
  1698. struct pipe_inode_info *pipe,
  1699. struct splice_pipe_desc *spd)
  1700. {
  1701. int ret;
  1702. /* Drop the socket lock, otherwise we have reverse
  1703. * locking dependencies between sk_lock and i_mutex
  1704. * here as compared to sendfile(). We enter here
  1705. * with the socket lock held, and splice_to_pipe() will
  1706. * grab the pipe inode lock. For sendfile() emulation,
  1707. * we call into ->sendpage() with the i_mutex lock held
  1708. * and networking will grab the socket lock.
  1709. */
  1710. release_sock(sk);
  1711. ret = splice_to_pipe(pipe, spd);
  1712. lock_sock(sk);
  1713. return ret;
  1714. }
  1715. /*
  1716. * Map data from the skb to a pipe. Should handle both the linear part,
  1717. * the fragments, and the frag list.
  1718. */
  1719. int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
  1720. struct pipe_inode_info *pipe, unsigned int tlen,
  1721. unsigned int flags,
  1722. ssize_t (*splice_cb)(struct sock *,
  1723. struct pipe_inode_info *,
  1724. struct splice_pipe_desc *))
  1725. {
  1726. struct partial_page partial[MAX_SKB_FRAGS];
  1727. struct page *pages[MAX_SKB_FRAGS];
  1728. struct splice_pipe_desc spd = {
  1729. .pages = pages,
  1730. .partial = partial,
  1731. .nr_pages_max = MAX_SKB_FRAGS,
  1732. .flags = flags,
  1733. .ops = &nosteal_pipe_buf_ops,
  1734. .spd_release = sock_spd_release,
  1735. };
  1736. int ret = 0;
  1737. __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
  1738. if (spd.nr_pages)
  1739. ret = splice_cb(sk, pipe, &spd);
  1740. return ret;
  1741. }
  1742. EXPORT_SYMBOL_GPL(skb_splice_bits);
  1743. /**
  1744. * skb_store_bits - store bits from kernel buffer to skb
  1745. * @skb: destination buffer
  1746. * @offset: offset in destination
  1747. * @from: source buffer
  1748. * @len: number of bytes to copy
  1749. *
  1750. * Copy the specified number of bytes from the source buffer to the
  1751. * destination skb. This function handles all the messy bits of
  1752. * traversing fragment lists and such.
  1753. */
  1754. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  1755. {
  1756. int start = skb_headlen(skb);
  1757. struct sk_buff *frag_iter;
  1758. int i, copy;
  1759. if (offset > (int)skb->len - len)
  1760. goto fault;
  1761. if ((copy = start - offset) > 0) {
  1762. if (copy > len)
  1763. copy = len;
  1764. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  1765. if ((len -= copy) == 0)
  1766. return 0;
  1767. offset += copy;
  1768. from += copy;
  1769. }
  1770. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1771. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1772. int end;
  1773. WARN_ON(start > offset + len);
  1774. end = start + skb_frag_size(frag);
  1775. if ((copy = end - offset) > 0) {
  1776. u8 *vaddr;
  1777. if (copy > len)
  1778. copy = len;
  1779. vaddr = kmap_atomic(skb_frag_page(frag));
  1780. memcpy(vaddr + frag->page_offset + offset - start,
  1781. from, copy);
  1782. kunmap_atomic(vaddr);
  1783. if ((len -= copy) == 0)
  1784. return 0;
  1785. offset += copy;
  1786. from += copy;
  1787. }
  1788. start = end;
  1789. }
  1790. skb_walk_frags(skb, frag_iter) {
  1791. int end;
  1792. WARN_ON(start > offset + len);
  1793. end = start + frag_iter->len;
  1794. if ((copy = end - offset) > 0) {
  1795. if (copy > len)
  1796. copy = len;
  1797. if (skb_store_bits(frag_iter, offset - start,
  1798. from, copy))
  1799. goto fault;
  1800. if ((len -= copy) == 0)
  1801. return 0;
  1802. offset += copy;
  1803. from += copy;
  1804. }
  1805. start = end;
  1806. }
  1807. if (!len)
  1808. return 0;
  1809. fault:
  1810. return -EFAULT;
  1811. }
  1812. EXPORT_SYMBOL(skb_store_bits);
  1813. /* Checksum skb data. */
  1814. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  1815. __wsum csum, const struct skb_checksum_ops *ops)
  1816. {
  1817. int start = skb_headlen(skb);
  1818. int i, copy = start - offset;
  1819. struct sk_buff *frag_iter;
  1820. int pos = 0;
  1821. /* Checksum header. */
  1822. if (copy > 0) {
  1823. if (copy > len)
  1824. copy = len;
  1825. csum = ops->update(skb->data + offset, copy, csum);
  1826. if ((len -= copy) == 0)
  1827. return csum;
  1828. offset += copy;
  1829. pos = copy;
  1830. }
  1831. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1832. int end;
  1833. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1834. WARN_ON(start > offset + len);
  1835. end = start + skb_frag_size(frag);
  1836. if ((copy = end - offset) > 0) {
  1837. __wsum csum2;
  1838. u8 *vaddr;
  1839. if (copy > len)
  1840. copy = len;
  1841. vaddr = kmap_atomic(skb_frag_page(frag));
  1842. csum2 = ops->update(vaddr + frag->page_offset +
  1843. offset - start, copy, 0);
  1844. kunmap_atomic(vaddr);
  1845. csum = ops->combine(csum, csum2, pos, copy);
  1846. if (!(len -= copy))
  1847. return csum;
  1848. offset += copy;
  1849. pos += copy;
  1850. }
  1851. start = end;
  1852. }
  1853. skb_walk_frags(skb, frag_iter) {
  1854. int end;
  1855. WARN_ON(start > offset + len);
  1856. end = start + frag_iter->len;
  1857. if ((copy = end - offset) > 0) {
  1858. __wsum csum2;
  1859. if (copy > len)
  1860. copy = len;
  1861. csum2 = __skb_checksum(frag_iter, offset - start,
  1862. copy, 0, ops);
  1863. csum = ops->combine(csum, csum2, pos, copy);
  1864. if ((len -= copy) == 0)
  1865. return csum;
  1866. offset += copy;
  1867. pos += copy;
  1868. }
  1869. start = end;
  1870. }
  1871. BUG_ON(len);
  1872. return csum;
  1873. }
  1874. EXPORT_SYMBOL(__skb_checksum);
  1875. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1876. int len, __wsum csum)
  1877. {
  1878. const struct skb_checksum_ops ops = {
  1879. .update = csum_partial_ext,
  1880. .combine = csum_block_add_ext,
  1881. };
  1882. return __skb_checksum(skb, offset, len, csum, &ops);
  1883. }
  1884. EXPORT_SYMBOL(skb_checksum);
  1885. /* Both of above in one bottle. */
  1886. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  1887. u8 *to, int len, __wsum csum)
  1888. {
  1889. int start = skb_headlen(skb);
  1890. int i, copy = start - offset;
  1891. struct sk_buff *frag_iter;
  1892. int pos = 0;
  1893. /* Copy header. */
  1894. if (copy > 0) {
  1895. if (copy > len)
  1896. copy = len;
  1897. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  1898. copy, csum);
  1899. if ((len -= copy) == 0)
  1900. return csum;
  1901. offset += copy;
  1902. to += copy;
  1903. pos = copy;
  1904. }
  1905. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1906. int end;
  1907. WARN_ON(start > offset + len);
  1908. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1909. if ((copy = end - offset) > 0) {
  1910. __wsum csum2;
  1911. u8 *vaddr;
  1912. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1913. if (copy > len)
  1914. copy = len;
  1915. vaddr = kmap_atomic(skb_frag_page(frag));
  1916. csum2 = csum_partial_copy_nocheck(vaddr +
  1917. frag->page_offset +
  1918. offset - start, to,
  1919. copy, 0);
  1920. kunmap_atomic(vaddr);
  1921. csum = csum_block_add(csum, csum2, pos);
  1922. if (!(len -= copy))
  1923. return csum;
  1924. offset += copy;
  1925. to += copy;
  1926. pos += copy;
  1927. }
  1928. start = end;
  1929. }
  1930. skb_walk_frags(skb, frag_iter) {
  1931. __wsum csum2;
  1932. int end;
  1933. WARN_ON(start > offset + len);
  1934. end = start + frag_iter->len;
  1935. if ((copy = end - offset) > 0) {
  1936. if (copy > len)
  1937. copy = len;
  1938. csum2 = skb_copy_and_csum_bits(frag_iter,
  1939. offset - start,
  1940. to, copy, 0);
  1941. csum = csum_block_add(csum, csum2, pos);
  1942. if ((len -= copy) == 0)
  1943. return csum;
  1944. offset += copy;
  1945. to += copy;
  1946. pos += copy;
  1947. }
  1948. start = end;
  1949. }
  1950. BUG_ON(len);
  1951. return csum;
  1952. }
  1953. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  1954. /**
  1955. * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
  1956. * @from: source buffer
  1957. *
  1958. * Calculates the amount of linear headroom needed in the 'to' skb passed
  1959. * into skb_zerocopy().
  1960. */
  1961. unsigned int
  1962. skb_zerocopy_headlen(const struct sk_buff *from)
  1963. {
  1964. unsigned int hlen = 0;
  1965. if (!from->head_frag ||
  1966. skb_headlen(from) < L1_CACHE_BYTES ||
  1967. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  1968. hlen = skb_headlen(from);
  1969. if (skb_has_frag_list(from))
  1970. hlen = from->len;
  1971. return hlen;
  1972. }
  1973. EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
  1974. /**
  1975. * skb_zerocopy - Zero copy skb to skb
  1976. * @to: destination buffer
  1977. * @from: source buffer
  1978. * @len: number of bytes to copy from source buffer
  1979. * @hlen: size of linear headroom in destination buffer
  1980. *
  1981. * Copies up to `len` bytes from `from` to `to` by creating references
  1982. * to the frags in the source buffer.
  1983. *
  1984. * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
  1985. * headroom in the `to` buffer.
  1986. *
  1987. * Return value:
  1988. * 0: everything is OK
  1989. * -ENOMEM: couldn't orphan frags of @from due to lack of memory
  1990. * -EFAULT: skb_copy_bits() found some problem with skb geometry
  1991. */
  1992. int
  1993. skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
  1994. {
  1995. int i, j = 0;
  1996. int plen = 0; /* length of skb->head fragment */
  1997. int ret;
  1998. struct page *page;
  1999. unsigned int offset;
  2000. BUG_ON(!from->head_frag && !hlen);
  2001. /* dont bother with small payloads */
  2002. if (len <= skb_tailroom(to))
  2003. return skb_copy_bits(from, 0, skb_put(to, len), len);
  2004. if (hlen) {
  2005. ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
  2006. if (unlikely(ret))
  2007. return ret;
  2008. len -= hlen;
  2009. } else {
  2010. plen = min_t(int, skb_headlen(from), len);
  2011. if (plen) {
  2012. page = virt_to_head_page(from->head);
  2013. offset = from->data - (unsigned char *)page_address(page);
  2014. __skb_fill_page_desc(to, 0, page, offset, plen);
  2015. get_page(page);
  2016. j = 1;
  2017. len -= plen;
  2018. }
  2019. }
  2020. to->truesize += len + plen;
  2021. to->len += len + plen;
  2022. to->data_len += len + plen;
  2023. if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
  2024. skb_tx_error(from);
  2025. return -ENOMEM;
  2026. }
  2027. for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
  2028. if (!len)
  2029. break;
  2030. skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
  2031. skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
  2032. len -= skb_shinfo(to)->frags[j].size;
  2033. skb_frag_ref(to, j);
  2034. j++;
  2035. }
  2036. skb_shinfo(to)->nr_frags = j;
  2037. return 0;
  2038. }
  2039. EXPORT_SYMBOL_GPL(skb_zerocopy);
  2040. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  2041. {
  2042. __wsum csum;
  2043. long csstart;
  2044. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2045. csstart = skb_checksum_start_offset(skb);
  2046. else
  2047. csstart = skb_headlen(skb);
  2048. BUG_ON(csstart > skb_headlen(skb));
  2049. skb_copy_from_linear_data(skb, to, csstart);
  2050. csum = 0;
  2051. if (csstart != skb->len)
  2052. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  2053. skb->len - csstart, 0);
  2054. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2055. long csstuff = csstart + skb->csum_offset;
  2056. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  2057. }
  2058. }
  2059. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  2060. /**
  2061. * skb_dequeue - remove from the head of the queue
  2062. * @list: list to dequeue from
  2063. *
  2064. * Remove the head of the list. The list lock is taken so the function
  2065. * may be used safely with other locking list functions. The head item is
  2066. * returned or %NULL if the list is empty.
  2067. */
  2068. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  2069. {
  2070. unsigned long flags;
  2071. struct sk_buff *result;
  2072. spin_lock_irqsave(&list->lock, flags);
  2073. result = __skb_dequeue(list);
  2074. spin_unlock_irqrestore(&list->lock, flags);
  2075. return result;
  2076. }
  2077. EXPORT_SYMBOL(skb_dequeue);
  2078. /**
  2079. * skb_dequeue_tail - remove from the tail of the queue
  2080. * @list: list to dequeue from
  2081. *
  2082. * Remove the tail of the list. The list lock is taken so the function
  2083. * may be used safely with other locking list functions. The tail item is
  2084. * returned or %NULL if the list is empty.
  2085. */
  2086. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  2087. {
  2088. unsigned long flags;
  2089. struct sk_buff *result;
  2090. spin_lock_irqsave(&list->lock, flags);
  2091. result = __skb_dequeue_tail(list);
  2092. spin_unlock_irqrestore(&list->lock, flags);
  2093. return result;
  2094. }
  2095. EXPORT_SYMBOL(skb_dequeue_tail);
  2096. /**
  2097. * skb_queue_purge - empty a list
  2098. * @list: list to empty
  2099. *
  2100. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  2101. * the list and one reference dropped. This function takes the list
  2102. * lock and is atomic with respect to other list locking functions.
  2103. */
  2104. void skb_queue_purge(struct sk_buff_head *list)
  2105. {
  2106. struct sk_buff *skb;
  2107. while ((skb = skb_dequeue(list)) != NULL)
  2108. kfree_skb(skb);
  2109. }
  2110. EXPORT_SYMBOL(skb_queue_purge);
  2111. /**
  2112. * skb_queue_head - queue a buffer at the list head
  2113. * @list: list to use
  2114. * @newsk: buffer to queue
  2115. *
  2116. * Queue a buffer at the start of the list. This function takes the
  2117. * list lock and can be used safely with other locking &sk_buff functions
  2118. * safely.
  2119. *
  2120. * A buffer cannot be placed on two lists at the same time.
  2121. */
  2122. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  2123. {
  2124. unsigned long flags;
  2125. spin_lock_irqsave(&list->lock, flags);
  2126. __skb_queue_head(list, newsk);
  2127. spin_unlock_irqrestore(&list->lock, flags);
  2128. }
  2129. EXPORT_SYMBOL(skb_queue_head);
  2130. /**
  2131. * skb_queue_tail - queue a buffer at the list tail
  2132. * @list: list to use
  2133. * @newsk: buffer to queue
  2134. *
  2135. * Queue a buffer at the tail of the list. This function takes the
  2136. * list lock and can be used safely with other locking &sk_buff functions
  2137. * safely.
  2138. *
  2139. * A buffer cannot be placed on two lists at the same time.
  2140. */
  2141. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  2142. {
  2143. unsigned long flags;
  2144. spin_lock_irqsave(&list->lock, flags);
  2145. __skb_queue_tail(list, newsk);
  2146. spin_unlock_irqrestore(&list->lock, flags);
  2147. }
  2148. EXPORT_SYMBOL(skb_queue_tail);
  2149. /**
  2150. * skb_unlink - remove a buffer from a list
  2151. * @skb: buffer to remove
  2152. * @list: list to use
  2153. *
  2154. * Remove a packet from a list. The list locks are taken and this
  2155. * function is atomic with respect to other list locked calls
  2156. *
  2157. * You must know what list the SKB is on.
  2158. */
  2159. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  2160. {
  2161. unsigned long flags;
  2162. spin_lock_irqsave(&list->lock, flags);
  2163. __skb_unlink(skb, list);
  2164. spin_unlock_irqrestore(&list->lock, flags);
  2165. }
  2166. EXPORT_SYMBOL(skb_unlink);
  2167. /**
  2168. * skb_append - append a buffer
  2169. * @old: buffer to insert after
  2170. * @newsk: buffer to insert
  2171. * @list: list to use
  2172. *
  2173. * Place a packet after a given packet in a list. The list locks are taken
  2174. * and this function is atomic with respect to other list locked calls.
  2175. * A buffer cannot be placed on two lists at the same time.
  2176. */
  2177. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2178. {
  2179. unsigned long flags;
  2180. spin_lock_irqsave(&list->lock, flags);
  2181. __skb_queue_after(list, old, newsk);
  2182. spin_unlock_irqrestore(&list->lock, flags);
  2183. }
  2184. EXPORT_SYMBOL(skb_append);
  2185. /**
  2186. * skb_insert - insert a buffer
  2187. * @old: buffer to insert before
  2188. * @newsk: buffer to insert
  2189. * @list: list to use
  2190. *
  2191. * Place a packet before a given packet in a list. The list locks are
  2192. * taken and this function is atomic with respect to other list locked
  2193. * calls.
  2194. *
  2195. * A buffer cannot be placed on two lists at the same time.
  2196. */
  2197. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2198. {
  2199. unsigned long flags;
  2200. spin_lock_irqsave(&list->lock, flags);
  2201. __skb_insert(newsk, old->prev, old, list);
  2202. spin_unlock_irqrestore(&list->lock, flags);
  2203. }
  2204. EXPORT_SYMBOL(skb_insert);
  2205. static inline void skb_split_inside_header(struct sk_buff *skb,
  2206. struct sk_buff* skb1,
  2207. const u32 len, const int pos)
  2208. {
  2209. int i;
  2210. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  2211. pos - len);
  2212. /* And move data appendix as is. */
  2213. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  2214. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  2215. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  2216. skb_shinfo(skb)->nr_frags = 0;
  2217. skb1->data_len = skb->data_len;
  2218. skb1->len += skb1->data_len;
  2219. skb->data_len = 0;
  2220. skb->len = len;
  2221. skb_set_tail_pointer(skb, len);
  2222. }
  2223. static inline void skb_split_no_header(struct sk_buff *skb,
  2224. struct sk_buff* skb1,
  2225. const u32 len, int pos)
  2226. {
  2227. int i, k = 0;
  2228. const int nfrags = skb_shinfo(skb)->nr_frags;
  2229. skb_shinfo(skb)->nr_frags = 0;
  2230. skb1->len = skb1->data_len = skb->len - len;
  2231. skb->len = len;
  2232. skb->data_len = len - pos;
  2233. for (i = 0; i < nfrags; i++) {
  2234. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2235. if (pos + size > len) {
  2236. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  2237. if (pos < len) {
  2238. /* Split frag.
  2239. * We have two variants in this case:
  2240. * 1. Move all the frag to the second
  2241. * part, if it is possible. F.e.
  2242. * this approach is mandatory for TUX,
  2243. * where splitting is expensive.
  2244. * 2. Split is accurately. We make this.
  2245. */
  2246. skb_frag_ref(skb, i);
  2247. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  2248. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  2249. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  2250. skb_shinfo(skb)->nr_frags++;
  2251. }
  2252. k++;
  2253. } else
  2254. skb_shinfo(skb)->nr_frags++;
  2255. pos += size;
  2256. }
  2257. skb_shinfo(skb1)->nr_frags = k;
  2258. }
  2259. /**
  2260. * skb_split - Split fragmented skb to two parts at length len.
  2261. * @skb: the buffer to split
  2262. * @skb1: the buffer to receive the second part
  2263. * @len: new length for skb
  2264. */
  2265. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2266. {
  2267. int pos = skb_headlen(skb);
  2268. skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
  2269. if (len < pos) /* Split line is inside header. */
  2270. skb_split_inside_header(skb, skb1, len, pos);
  2271. else /* Second chunk has no header, nothing to copy. */
  2272. skb_split_no_header(skb, skb1, len, pos);
  2273. }
  2274. EXPORT_SYMBOL(skb_split);
  2275. /* Shifting from/to a cloned skb is a no-go.
  2276. *
  2277. * Caller cannot keep skb_shinfo related pointers past calling here!
  2278. */
  2279. static int skb_prepare_for_shift(struct sk_buff *skb)
  2280. {
  2281. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2282. }
  2283. /**
  2284. * skb_shift - Shifts paged data partially from skb to another
  2285. * @tgt: buffer into which tail data gets added
  2286. * @skb: buffer from which the paged data comes from
  2287. * @shiftlen: shift up to this many bytes
  2288. *
  2289. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2290. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2291. * It's up to caller to free skb if everything was shifted.
  2292. *
  2293. * If @tgt runs out of frags, the whole operation is aborted.
  2294. *
  2295. * Skb cannot include anything else but paged data while tgt is allowed
  2296. * to have non-paged data as well.
  2297. *
  2298. * TODO: full sized shift could be optimized but that would need
  2299. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2300. */
  2301. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2302. {
  2303. int from, to, merge, todo;
  2304. struct skb_frag_struct *fragfrom, *fragto;
  2305. BUG_ON(shiftlen > skb->len);
  2306. BUG_ON(skb_headlen(skb)); /* Would corrupt stream */
  2307. todo = shiftlen;
  2308. from = 0;
  2309. to = skb_shinfo(tgt)->nr_frags;
  2310. fragfrom = &skb_shinfo(skb)->frags[from];
  2311. /* Actual merge is delayed until the point when we know we can
  2312. * commit all, so that we don't have to undo partial changes
  2313. */
  2314. if (!to ||
  2315. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2316. fragfrom->page_offset)) {
  2317. merge = -1;
  2318. } else {
  2319. merge = to - 1;
  2320. todo -= skb_frag_size(fragfrom);
  2321. if (todo < 0) {
  2322. if (skb_prepare_for_shift(skb) ||
  2323. skb_prepare_for_shift(tgt))
  2324. return 0;
  2325. /* All previous frag pointers might be stale! */
  2326. fragfrom = &skb_shinfo(skb)->frags[from];
  2327. fragto = &skb_shinfo(tgt)->frags[merge];
  2328. skb_frag_size_add(fragto, shiftlen);
  2329. skb_frag_size_sub(fragfrom, shiftlen);
  2330. fragfrom->page_offset += shiftlen;
  2331. goto onlymerged;
  2332. }
  2333. from++;
  2334. }
  2335. /* Skip full, not-fitting skb to avoid expensive operations */
  2336. if ((shiftlen == skb->len) &&
  2337. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2338. return 0;
  2339. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2340. return 0;
  2341. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2342. if (to == MAX_SKB_FRAGS)
  2343. return 0;
  2344. fragfrom = &skb_shinfo(skb)->frags[from];
  2345. fragto = &skb_shinfo(tgt)->frags[to];
  2346. if (todo >= skb_frag_size(fragfrom)) {
  2347. *fragto = *fragfrom;
  2348. todo -= skb_frag_size(fragfrom);
  2349. from++;
  2350. to++;
  2351. } else {
  2352. __skb_frag_ref(fragfrom);
  2353. fragto->page = fragfrom->page;
  2354. fragto->page_offset = fragfrom->page_offset;
  2355. skb_frag_size_set(fragto, todo);
  2356. fragfrom->page_offset += todo;
  2357. skb_frag_size_sub(fragfrom, todo);
  2358. todo = 0;
  2359. to++;
  2360. break;
  2361. }
  2362. }
  2363. /* Ready to "commit" this state change to tgt */
  2364. skb_shinfo(tgt)->nr_frags = to;
  2365. if (merge >= 0) {
  2366. fragfrom = &skb_shinfo(skb)->frags[0];
  2367. fragto = &skb_shinfo(tgt)->frags[merge];
  2368. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2369. __skb_frag_unref(fragfrom);
  2370. }
  2371. /* Reposition in the original skb */
  2372. to = 0;
  2373. while (from < skb_shinfo(skb)->nr_frags)
  2374. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2375. skb_shinfo(skb)->nr_frags = to;
  2376. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2377. onlymerged:
  2378. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2379. * the other hand might need it if it needs to be resent
  2380. */
  2381. tgt->ip_summed = CHECKSUM_PARTIAL;
  2382. skb->ip_summed = CHECKSUM_PARTIAL;
  2383. /* Yak, is it really working this way? Some helper please? */
  2384. skb->len -= shiftlen;
  2385. skb->data_len -= shiftlen;
  2386. skb->truesize -= shiftlen;
  2387. tgt->len += shiftlen;
  2388. tgt->data_len += shiftlen;
  2389. tgt->truesize += shiftlen;
  2390. return shiftlen;
  2391. }
  2392. /**
  2393. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2394. * @skb: the buffer to read
  2395. * @from: lower offset of data to be read
  2396. * @to: upper offset of data to be read
  2397. * @st: state variable
  2398. *
  2399. * Initializes the specified state variable. Must be called before
  2400. * invoking skb_seq_read() for the first time.
  2401. */
  2402. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2403. unsigned int to, struct skb_seq_state *st)
  2404. {
  2405. st->lower_offset = from;
  2406. st->upper_offset = to;
  2407. st->root_skb = st->cur_skb = skb;
  2408. st->frag_idx = st->stepped_offset = 0;
  2409. st->frag_data = NULL;
  2410. }
  2411. EXPORT_SYMBOL(skb_prepare_seq_read);
  2412. /**
  2413. * skb_seq_read - Sequentially read skb data
  2414. * @consumed: number of bytes consumed by the caller so far
  2415. * @data: destination pointer for data to be returned
  2416. * @st: state variable
  2417. *
  2418. * Reads a block of skb data at @consumed relative to the
  2419. * lower offset specified to skb_prepare_seq_read(). Assigns
  2420. * the head of the data block to @data and returns the length
  2421. * of the block or 0 if the end of the skb data or the upper
  2422. * offset has been reached.
  2423. *
  2424. * The caller is not required to consume all of the data
  2425. * returned, i.e. @consumed is typically set to the number
  2426. * of bytes already consumed and the next call to
  2427. * skb_seq_read() will return the remaining part of the block.
  2428. *
  2429. * Note 1: The size of each block of data returned can be arbitrary,
  2430. * this limitation is the cost for zerocopy sequential
  2431. * reads of potentially non linear data.
  2432. *
  2433. * Note 2: Fragment lists within fragments are not implemented
  2434. * at the moment, state->root_skb could be replaced with
  2435. * a stack for this purpose.
  2436. */
  2437. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2438. struct skb_seq_state *st)
  2439. {
  2440. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2441. skb_frag_t *frag;
  2442. if (unlikely(abs_offset >= st->upper_offset)) {
  2443. if (st->frag_data) {
  2444. kunmap_atomic(st->frag_data);
  2445. st->frag_data = NULL;
  2446. }
  2447. return 0;
  2448. }
  2449. next_skb:
  2450. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2451. if (abs_offset < block_limit && !st->frag_data) {
  2452. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2453. return block_limit - abs_offset;
  2454. }
  2455. if (st->frag_idx == 0 && !st->frag_data)
  2456. st->stepped_offset += skb_headlen(st->cur_skb);
  2457. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2458. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2459. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2460. if (abs_offset < block_limit) {
  2461. if (!st->frag_data)
  2462. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2463. *data = (u8 *) st->frag_data + frag->page_offset +
  2464. (abs_offset - st->stepped_offset);
  2465. return block_limit - abs_offset;
  2466. }
  2467. if (st->frag_data) {
  2468. kunmap_atomic(st->frag_data);
  2469. st->frag_data = NULL;
  2470. }
  2471. st->frag_idx++;
  2472. st->stepped_offset += skb_frag_size(frag);
  2473. }
  2474. if (st->frag_data) {
  2475. kunmap_atomic(st->frag_data);
  2476. st->frag_data = NULL;
  2477. }
  2478. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2479. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2480. st->frag_idx = 0;
  2481. goto next_skb;
  2482. } else if (st->cur_skb->next) {
  2483. st->cur_skb = st->cur_skb->next;
  2484. st->frag_idx = 0;
  2485. goto next_skb;
  2486. }
  2487. return 0;
  2488. }
  2489. EXPORT_SYMBOL(skb_seq_read);
  2490. /**
  2491. * skb_abort_seq_read - Abort a sequential read of skb data
  2492. * @st: state variable
  2493. *
  2494. * Must be called if skb_seq_read() was not called until it
  2495. * returned 0.
  2496. */
  2497. void skb_abort_seq_read(struct skb_seq_state *st)
  2498. {
  2499. if (st->frag_data)
  2500. kunmap_atomic(st->frag_data);
  2501. }
  2502. EXPORT_SYMBOL(skb_abort_seq_read);
  2503. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2504. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2505. struct ts_config *conf,
  2506. struct ts_state *state)
  2507. {
  2508. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2509. }
  2510. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2511. {
  2512. skb_abort_seq_read(TS_SKB_CB(state));
  2513. }
  2514. /**
  2515. * skb_find_text - Find a text pattern in skb data
  2516. * @skb: the buffer to look in
  2517. * @from: search offset
  2518. * @to: search limit
  2519. * @config: textsearch configuration
  2520. *
  2521. * Finds a pattern in the skb data according to the specified
  2522. * textsearch configuration. Use textsearch_next() to retrieve
  2523. * subsequent occurrences of the pattern. Returns the offset
  2524. * to the first occurrence or UINT_MAX if no match was found.
  2525. */
  2526. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2527. unsigned int to, struct ts_config *config)
  2528. {
  2529. struct ts_state state;
  2530. unsigned int ret;
  2531. config->get_next_block = skb_ts_get_next_block;
  2532. config->finish = skb_ts_finish;
  2533. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
  2534. ret = textsearch_find(config, &state);
  2535. return (ret <= to - from ? ret : UINT_MAX);
  2536. }
  2537. EXPORT_SYMBOL(skb_find_text);
  2538. /**
  2539. * skb_append_datato_frags - append the user data to a skb
  2540. * @sk: sock structure
  2541. * @skb: skb structure to be appended with user data.
  2542. * @getfrag: call back function to be used for getting the user data
  2543. * @from: pointer to user message iov
  2544. * @length: length of the iov message
  2545. *
  2546. * Description: This procedure append the user data in the fragment part
  2547. * of the skb if any page alloc fails user this procedure returns -ENOMEM
  2548. */
  2549. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  2550. int (*getfrag)(void *from, char *to, int offset,
  2551. int len, int odd, struct sk_buff *skb),
  2552. void *from, int length)
  2553. {
  2554. int frg_cnt = skb_shinfo(skb)->nr_frags;
  2555. int copy;
  2556. int offset = 0;
  2557. int ret;
  2558. struct page_frag *pfrag = &current->task_frag;
  2559. do {
  2560. /* Return error if we don't have space for new frag */
  2561. if (frg_cnt >= MAX_SKB_FRAGS)
  2562. return -EMSGSIZE;
  2563. if (!sk_page_frag_refill(sk, pfrag))
  2564. return -ENOMEM;
  2565. /* copy the user data to page */
  2566. copy = min_t(int, length, pfrag->size - pfrag->offset);
  2567. ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
  2568. offset, copy, 0, skb);
  2569. if (ret < 0)
  2570. return -EFAULT;
  2571. /* copy was successful so update the size parameters */
  2572. skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
  2573. copy);
  2574. frg_cnt++;
  2575. pfrag->offset += copy;
  2576. get_page(pfrag->page);
  2577. skb->truesize += copy;
  2578. atomic_add(copy, &sk->sk_wmem_alloc);
  2579. skb->len += copy;
  2580. skb->data_len += copy;
  2581. offset += copy;
  2582. length -= copy;
  2583. } while (length > 0);
  2584. return 0;
  2585. }
  2586. EXPORT_SYMBOL(skb_append_datato_frags);
  2587. int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
  2588. int offset, size_t size)
  2589. {
  2590. int i = skb_shinfo(skb)->nr_frags;
  2591. if (skb_can_coalesce(skb, i, page, offset)) {
  2592. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
  2593. } else if (i < MAX_SKB_FRAGS) {
  2594. get_page(page);
  2595. skb_fill_page_desc(skb, i, page, offset, size);
  2596. } else {
  2597. return -EMSGSIZE;
  2598. }
  2599. return 0;
  2600. }
  2601. EXPORT_SYMBOL_GPL(skb_append_pagefrags);
  2602. /**
  2603. * skb_pull_rcsum - pull skb and update receive checksum
  2604. * @skb: buffer to update
  2605. * @len: length of data pulled
  2606. *
  2607. * This function performs an skb_pull on the packet and updates
  2608. * the CHECKSUM_COMPLETE checksum. It should be used on
  2609. * receive path processing instead of skb_pull unless you know
  2610. * that the checksum difference is zero (e.g., a valid IP header)
  2611. * or you are setting ip_summed to CHECKSUM_NONE.
  2612. */
  2613. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2614. {
  2615. unsigned char *data = skb->data;
  2616. BUG_ON(len > skb->len);
  2617. __skb_pull(skb, len);
  2618. skb_postpull_rcsum(skb, data, len);
  2619. return skb->data;
  2620. }
  2621. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2622. /**
  2623. * skb_segment - Perform protocol segmentation on skb.
  2624. * @head_skb: buffer to segment
  2625. * @features: features for the output path (see dev->features)
  2626. *
  2627. * This function performs segmentation on the given skb. It returns
  2628. * a pointer to the first in a list of new skbs for the segments.
  2629. * In case of error it returns ERR_PTR(err).
  2630. */
  2631. struct sk_buff *skb_segment(struct sk_buff *head_skb,
  2632. netdev_features_t features)
  2633. {
  2634. struct sk_buff *segs = NULL;
  2635. struct sk_buff *tail = NULL;
  2636. struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
  2637. skb_frag_t *frag = skb_shinfo(head_skb)->frags;
  2638. unsigned int mss = skb_shinfo(head_skb)->gso_size;
  2639. unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
  2640. struct sk_buff *frag_skb = head_skb;
  2641. unsigned int offset = doffset;
  2642. unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
  2643. unsigned int partial_segs = 0;
  2644. unsigned int headroom;
  2645. unsigned int len = head_skb->len;
  2646. __be16 proto;
  2647. bool csum, sg;
  2648. int nfrags = skb_shinfo(head_skb)->nr_frags;
  2649. int err = -ENOMEM;
  2650. int i = 0;
  2651. int pos;
  2652. int dummy;
  2653. __skb_push(head_skb, doffset);
  2654. proto = skb_network_protocol(head_skb, &dummy);
  2655. if (unlikely(!proto))
  2656. return ERR_PTR(-EINVAL);
  2657. sg = !!(features & NETIF_F_SG);
  2658. csum = !!can_checksum_protocol(features, proto);
  2659. /* GSO partial only requires that we trim off any excess that
  2660. * doesn't fit into an MSS sized block, so take care of that
  2661. * now.
  2662. */
  2663. if (sg && csum && (features & NETIF_F_GSO_PARTIAL)) {
  2664. partial_segs = len / mss;
  2665. if (partial_segs > 1)
  2666. mss *= partial_segs;
  2667. else
  2668. partial_segs = 0;
  2669. }
  2670. headroom = skb_headroom(head_skb);
  2671. pos = skb_headlen(head_skb);
  2672. do {
  2673. struct sk_buff *nskb;
  2674. skb_frag_t *nskb_frag;
  2675. int hsize;
  2676. int size;
  2677. if (unlikely(mss == GSO_BY_FRAGS)) {
  2678. len = list_skb->len;
  2679. } else {
  2680. len = head_skb->len - offset;
  2681. if (len > mss)
  2682. len = mss;
  2683. }
  2684. hsize = skb_headlen(head_skb) - offset;
  2685. if (hsize < 0)
  2686. hsize = 0;
  2687. if (hsize > len || !sg)
  2688. hsize = len;
  2689. if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
  2690. (skb_headlen(list_skb) == len || sg)) {
  2691. BUG_ON(skb_headlen(list_skb) > len);
  2692. i = 0;
  2693. nfrags = skb_shinfo(list_skb)->nr_frags;
  2694. frag = skb_shinfo(list_skb)->frags;
  2695. frag_skb = list_skb;
  2696. pos += skb_headlen(list_skb);
  2697. while (pos < offset + len) {
  2698. BUG_ON(i >= nfrags);
  2699. size = skb_frag_size(frag);
  2700. if (pos + size > offset + len)
  2701. break;
  2702. i++;
  2703. pos += size;
  2704. frag++;
  2705. }
  2706. nskb = skb_clone(list_skb, GFP_ATOMIC);
  2707. list_skb = list_skb->next;
  2708. if (unlikely(!nskb))
  2709. goto err;
  2710. if (unlikely(pskb_trim(nskb, len))) {
  2711. kfree_skb(nskb);
  2712. goto err;
  2713. }
  2714. hsize = skb_end_offset(nskb);
  2715. if (skb_cow_head(nskb, doffset + headroom)) {
  2716. kfree_skb(nskb);
  2717. goto err;
  2718. }
  2719. nskb->truesize += skb_end_offset(nskb) - hsize;
  2720. skb_release_head_state(nskb);
  2721. __skb_push(nskb, doffset);
  2722. } else {
  2723. nskb = __alloc_skb(hsize + doffset + headroom,
  2724. GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
  2725. NUMA_NO_NODE);
  2726. if (unlikely(!nskb))
  2727. goto err;
  2728. skb_reserve(nskb, headroom);
  2729. __skb_put(nskb, doffset);
  2730. }
  2731. if (segs)
  2732. tail->next = nskb;
  2733. else
  2734. segs = nskb;
  2735. tail = nskb;
  2736. __copy_skb_header(nskb, head_skb);
  2737. skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
  2738. skb_reset_mac_len(nskb);
  2739. skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
  2740. nskb->data - tnl_hlen,
  2741. doffset + tnl_hlen);
  2742. if (nskb->len == len + doffset)
  2743. goto perform_csum_check;
  2744. if (!sg) {
  2745. if (!nskb->remcsum_offload)
  2746. nskb->ip_summed = CHECKSUM_NONE;
  2747. SKB_GSO_CB(nskb)->csum =
  2748. skb_copy_and_csum_bits(head_skb, offset,
  2749. skb_put(nskb, len),
  2750. len, 0);
  2751. SKB_GSO_CB(nskb)->csum_start =
  2752. skb_headroom(nskb) + doffset;
  2753. continue;
  2754. }
  2755. nskb_frag = skb_shinfo(nskb)->frags;
  2756. skb_copy_from_linear_data_offset(head_skb, offset,
  2757. skb_put(nskb, hsize), hsize);
  2758. skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
  2759. SKBTX_SHARED_FRAG;
  2760. while (pos < offset + len) {
  2761. if (i >= nfrags) {
  2762. BUG_ON(skb_headlen(list_skb));
  2763. i = 0;
  2764. nfrags = skb_shinfo(list_skb)->nr_frags;
  2765. frag = skb_shinfo(list_skb)->frags;
  2766. frag_skb = list_skb;
  2767. BUG_ON(!nfrags);
  2768. list_skb = list_skb->next;
  2769. }
  2770. if (unlikely(skb_shinfo(nskb)->nr_frags >=
  2771. MAX_SKB_FRAGS)) {
  2772. net_warn_ratelimited(
  2773. "skb_segment: too many frags: %u %u\n",
  2774. pos, mss);
  2775. goto err;
  2776. }
  2777. if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
  2778. goto err;
  2779. *nskb_frag = *frag;
  2780. __skb_frag_ref(nskb_frag);
  2781. size = skb_frag_size(nskb_frag);
  2782. if (pos < offset) {
  2783. nskb_frag->page_offset += offset - pos;
  2784. skb_frag_size_sub(nskb_frag, offset - pos);
  2785. }
  2786. skb_shinfo(nskb)->nr_frags++;
  2787. if (pos + size <= offset + len) {
  2788. i++;
  2789. frag++;
  2790. pos += size;
  2791. } else {
  2792. skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
  2793. goto skip_fraglist;
  2794. }
  2795. nskb_frag++;
  2796. }
  2797. skip_fraglist:
  2798. nskb->data_len = len - hsize;
  2799. nskb->len += nskb->data_len;
  2800. nskb->truesize += nskb->data_len;
  2801. perform_csum_check:
  2802. if (!csum) {
  2803. if (skb_has_shared_frag(nskb)) {
  2804. err = __skb_linearize(nskb);
  2805. if (err)
  2806. goto err;
  2807. }
  2808. if (!nskb->remcsum_offload)
  2809. nskb->ip_summed = CHECKSUM_NONE;
  2810. SKB_GSO_CB(nskb)->csum =
  2811. skb_checksum(nskb, doffset,
  2812. nskb->len - doffset, 0);
  2813. SKB_GSO_CB(nskb)->csum_start =
  2814. skb_headroom(nskb) + doffset;
  2815. }
  2816. } while ((offset += len) < head_skb->len);
  2817. /* Some callers want to get the end of the list.
  2818. * Put it in segs->prev to avoid walking the list.
  2819. * (see validate_xmit_skb_list() for example)
  2820. */
  2821. segs->prev = tail;
  2822. /* Update GSO info on first skb in partial sequence. */
  2823. if (partial_segs) {
  2824. int type = skb_shinfo(head_skb)->gso_type;
  2825. /* Update type to add partial and then remove dodgy if set */
  2826. type |= SKB_GSO_PARTIAL;
  2827. type &= ~SKB_GSO_DODGY;
  2828. /* Update GSO info and prepare to start updating headers on
  2829. * our way back down the stack of protocols.
  2830. */
  2831. skb_shinfo(segs)->gso_size = skb_shinfo(head_skb)->gso_size;
  2832. skb_shinfo(segs)->gso_segs = partial_segs;
  2833. skb_shinfo(segs)->gso_type = type;
  2834. SKB_GSO_CB(segs)->data_offset = skb_headroom(segs) + doffset;
  2835. }
  2836. /* Following permits correct backpressure, for protocols
  2837. * using skb_set_owner_w().
  2838. * Idea is to tranfert ownership from head_skb to last segment.
  2839. */
  2840. if (head_skb->destructor == sock_wfree) {
  2841. swap(tail->truesize, head_skb->truesize);
  2842. swap(tail->destructor, head_skb->destructor);
  2843. swap(tail->sk, head_skb->sk);
  2844. }
  2845. return segs;
  2846. err:
  2847. kfree_skb_list(segs);
  2848. return ERR_PTR(err);
  2849. }
  2850. EXPORT_SYMBOL_GPL(skb_segment);
  2851. int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2852. {
  2853. struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
  2854. unsigned int offset = skb_gro_offset(skb);
  2855. unsigned int headlen = skb_headlen(skb);
  2856. unsigned int len = skb_gro_len(skb);
  2857. struct sk_buff *lp, *p = *head;
  2858. unsigned int delta_truesize;
  2859. if (unlikely(p->len + len >= 65536))
  2860. return -E2BIG;
  2861. lp = NAPI_GRO_CB(p)->last;
  2862. pinfo = skb_shinfo(lp);
  2863. if (headlen <= offset) {
  2864. skb_frag_t *frag;
  2865. skb_frag_t *frag2;
  2866. int i = skbinfo->nr_frags;
  2867. int nr_frags = pinfo->nr_frags + i;
  2868. if (nr_frags > MAX_SKB_FRAGS)
  2869. goto merge;
  2870. offset -= headlen;
  2871. pinfo->nr_frags = nr_frags;
  2872. skbinfo->nr_frags = 0;
  2873. frag = pinfo->frags + nr_frags;
  2874. frag2 = skbinfo->frags + i;
  2875. do {
  2876. *--frag = *--frag2;
  2877. } while (--i);
  2878. frag->page_offset += offset;
  2879. skb_frag_size_sub(frag, offset);
  2880. /* all fragments truesize : remove (head size + sk_buff) */
  2881. delta_truesize = skb->truesize -
  2882. SKB_TRUESIZE(skb_end_offset(skb));
  2883. skb->truesize -= skb->data_len;
  2884. skb->len -= skb->data_len;
  2885. skb->data_len = 0;
  2886. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  2887. goto done;
  2888. } else if (skb->head_frag) {
  2889. int nr_frags = pinfo->nr_frags;
  2890. skb_frag_t *frag = pinfo->frags + nr_frags;
  2891. struct page *page = virt_to_head_page(skb->head);
  2892. unsigned int first_size = headlen - offset;
  2893. unsigned int first_offset;
  2894. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  2895. goto merge;
  2896. first_offset = skb->data -
  2897. (unsigned char *)page_address(page) +
  2898. offset;
  2899. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  2900. frag->page.p = page;
  2901. frag->page_offset = first_offset;
  2902. skb_frag_size_set(frag, first_size);
  2903. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  2904. /* We dont need to clear skbinfo->nr_frags here */
  2905. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  2906. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  2907. goto done;
  2908. }
  2909. merge:
  2910. delta_truesize = skb->truesize;
  2911. if (offset > headlen) {
  2912. unsigned int eat = offset - headlen;
  2913. skbinfo->frags[0].page_offset += eat;
  2914. skb_frag_size_sub(&skbinfo->frags[0], eat);
  2915. skb->data_len -= eat;
  2916. skb->len -= eat;
  2917. offset = headlen;
  2918. }
  2919. __skb_pull(skb, offset);
  2920. if (NAPI_GRO_CB(p)->last == p)
  2921. skb_shinfo(p)->frag_list = skb;
  2922. else
  2923. NAPI_GRO_CB(p)->last->next = skb;
  2924. NAPI_GRO_CB(p)->last = skb;
  2925. __skb_header_release(skb);
  2926. lp = p;
  2927. done:
  2928. NAPI_GRO_CB(p)->count++;
  2929. p->data_len += len;
  2930. p->truesize += delta_truesize;
  2931. p->len += len;
  2932. if (lp != p) {
  2933. lp->data_len += len;
  2934. lp->truesize += delta_truesize;
  2935. lp->len += len;
  2936. }
  2937. NAPI_GRO_CB(skb)->same_flow = 1;
  2938. return 0;
  2939. }
  2940. EXPORT_SYMBOL_GPL(skb_gro_receive);
  2941. void __init skb_init(void)
  2942. {
  2943. skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
  2944. sizeof(struct sk_buff),
  2945. 0,
  2946. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2947. NULL);
  2948. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  2949. sizeof(struct sk_buff_fclones),
  2950. 0,
  2951. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2952. NULL);
  2953. }
  2954. /**
  2955. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  2956. * @skb: Socket buffer containing the buffers to be mapped
  2957. * @sg: The scatter-gather list to map into
  2958. * @offset: The offset into the buffer's contents to start mapping
  2959. * @len: Length of buffer space to be mapped
  2960. *
  2961. * Fill the specified scatter-gather list with mappings/pointers into a
  2962. * region of the buffer space attached to a socket buffer.
  2963. */
  2964. static int
  2965. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  2966. {
  2967. int start = skb_headlen(skb);
  2968. int i, copy = start - offset;
  2969. struct sk_buff *frag_iter;
  2970. int elt = 0;
  2971. if (copy > 0) {
  2972. if (copy > len)
  2973. copy = len;
  2974. sg_set_buf(sg, skb->data + offset, copy);
  2975. elt++;
  2976. if ((len -= copy) == 0)
  2977. return elt;
  2978. offset += copy;
  2979. }
  2980. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2981. int end;
  2982. WARN_ON(start > offset + len);
  2983. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2984. if ((copy = end - offset) > 0) {
  2985. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2986. if (copy > len)
  2987. copy = len;
  2988. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  2989. frag->page_offset+offset-start);
  2990. elt++;
  2991. if (!(len -= copy))
  2992. return elt;
  2993. offset += copy;
  2994. }
  2995. start = end;
  2996. }
  2997. skb_walk_frags(skb, frag_iter) {
  2998. int end;
  2999. WARN_ON(start > offset + len);
  3000. end = start + frag_iter->len;
  3001. if ((copy = end - offset) > 0) {
  3002. if (copy > len)
  3003. copy = len;
  3004. elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  3005. copy);
  3006. if ((len -= copy) == 0)
  3007. return elt;
  3008. offset += copy;
  3009. }
  3010. start = end;
  3011. }
  3012. BUG_ON(len);
  3013. return elt;
  3014. }
  3015. /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
  3016. * sglist without mark the sg which contain last skb data as the end.
  3017. * So the caller can mannipulate sg list as will when padding new data after
  3018. * the first call without calling sg_unmark_end to expend sg list.
  3019. *
  3020. * Scenario to use skb_to_sgvec_nomark:
  3021. * 1. sg_init_table
  3022. * 2. skb_to_sgvec_nomark(payload1)
  3023. * 3. skb_to_sgvec_nomark(payload2)
  3024. *
  3025. * This is equivalent to:
  3026. * 1. sg_init_table
  3027. * 2. skb_to_sgvec(payload1)
  3028. * 3. sg_unmark_end
  3029. * 4. skb_to_sgvec(payload2)
  3030. *
  3031. * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
  3032. * is more preferable.
  3033. */
  3034. int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  3035. int offset, int len)
  3036. {
  3037. return __skb_to_sgvec(skb, sg, offset, len);
  3038. }
  3039. EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
  3040. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  3041. {
  3042. int nsg = __skb_to_sgvec(skb, sg, offset, len);
  3043. sg_mark_end(&sg[nsg - 1]);
  3044. return nsg;
  3045. }
  3046. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  3047. /**
  3048. * skb_cow_data - Check that a socket buffer's data buffers are writable
  3049. * @skb: The socket buffer to check.
  3050. * @tailbits: Amount of trailing space to be added
  3051. * @trailer: Returned pointer to the skb where the @tailbits space begins
  3052. *
  3053. * Make sure that the data buffers attached to a socket buffer are
  3054. * writable. If they are not, private copies are made of the data buffers
  3055. * and the socket buffer is set to use these instead.
  3056. *
  3057. * If @tailbits is given, make sure that there is space to write @tailbits
  3058. * bytes of data beyond current end of socket buffer. @trailer will be
  3059. * set to point to the skb in which this space begins.
  3060. *
  3061. * The number of scatterlist elements required to completely map the
  3062. * COW'd and extended socket buffer will be returned.
  3063. */
  3064. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  3065. {
  3066. int copyflag;
  3067. int elt;
  3068. struct sk_buff *skb1, **skb_p;
  3069. /* If skb is cloned or its head is paged, reallocate
  3070. * head pulling out all the pages (pages are considered not writable
  3071. * at the moment even if they are anonymous).
  3072. */
  3073. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  3074. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  3075. return -ENOMEM;
  3076. /* Easy case. Most of packets will go this way. */
  3077. if (!skb_has_frag_list(skb)) {
  3078. /* A little of trouble, not enough of space for trailer.
  3079. * This should not happen, when stack is tuned to generate
  3080. * good frames. OK, on miss we reallocate and reserve even more
  3081. * space, 128 bytes is fair. */
  3082. if (skb_tailroom(skb) < tailbits &&
  3083. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  3084. return -ENOMEM;
  3085. /* Voila! */
  3086. *trailer = skb;
  3087. return 1;
  3088. }
  3089. /* Misery. We are in troubles, going to mincer fragments... */
  3090. elt = 1;
  3091. skb_p = &skb_shinfo(skb)->frag_list;
  3092. copyflag = 0;
  3093. while ((skb1 = *skb_p) != NULL) {
  3094. int ntail = 0;
  3095. /* The fragment is partially pulled by someone,
  3096. * this can happen on input. Copy it and everything
  3097. * after it. */
  3098. if (skb_shared(skb1))
  3099. copyflag = 1;
  3100. /* If the skb is the last, worry about trailer. */
  3101. if (skb1->next == NULL && tailbits) {
  3102. if (skb_shinfo(skb1)->nr_frags ||
  3103. skb_has_frag_list(skb1) ||
  3104. skb_tailroom(skb1) < tailbits)
  3105. ntail = tailbits + 128;
  3106. }
  3107. if (copyflag ||
  3108. skb_cloned(skb1) ||
  3109. ntail ||
  3110. skb_shinfo(skb1)->nr_frags ||
  3111. skb_has_frag_list(skb1)) {
  3112. struct sk_buff *skb2;
  3113. /* Fuck, we are miserable poor guys... */
  3114. if (ntail == 0)
  3115. skb2 = skb_copy(skb1, GFP_ATOMIC);
  3116. else
  3117. skb2 = skb_copy_expand(skb1,
  3118. skb_headroom(skb1),
  3119. ntail,
  3120. GFP_ATOMIC);
  3121. if (unlikely(skb2 == NULL))
  3122. return -ENOMEM;
  3123. if (skb1->sk)
  3124. skb_set_owner_w(skb2, skb1->sk);
  3125. /* Looking around. Are we still alive?
  3126. * OK, link new skb, drop old one */
  3127. skb2->next = skb1->next;
  3128. *skb_p = skb2;
  3129. kfree_skb(skb1);
  3130. skb1 = skb2;
  3131. }
  3132. elt++;
  3133. *trailer = skb1;
  3134. skb_p = &skb1->next;
  3135. }
  3136. return elt;
  3137. }
  3138. EXPORT_SYMBOL_GPL(skb_cow_data);
  3139. static void sock_rmem_free(struct sk_buff *skb)
  3140. {
  3141. struct sock *sk = skb->sk;
  3142. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  3143. }
  3144. /*
  3145. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  3146. */
  3147. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  3148. {
  3149. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  3150. (unsigned int)sk->sk_rcvbuf)
  3151. return -ENOMEM;
  3152. skb_orphan(skb);
  3153. skb->sk = sk;
  3154. skb->destructor = sock_rmem_free;
  3155. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  3156. /* before exiting rcu section, make sure dst is refcounted */
  3157. skb_dst_force(skb);
  3158. skb_queue_tail(&sk->sk_error_queue, skb);
  3159. if (!sock_flag(sk, SOCK_DEAD))
  3160. sk->sk_data_ready(sk);
  3161. return 0;
  3162. }
  3163. EXPORT_SYMBOL(sock_queue_err_skb);
  3164. struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
  3165. {
  3166. struct sk_buff_head *q = &sk->sk_error_queue;
  3167. struct sk_buff *skb, *skb_next;
  3168. unsigned long flags;
  3169. int err = 0;
  3170. spin_lock_irqsave(&q->lock, flags);
  3171. skb = __skb_dequeue(q);
  3172. if (skb && (skb_next = skb_peek(q)))
  3173. err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
  3174. spin_unlock_irqrestore(&q->lock, flags);
  3175. sk->sk_err = err;
  3176. if (err)
  3177. sk->sk_error_report(sk);
  3178. return skb;
  3179. }
  3180. EXPORT_SYMBOL(sock_dequeue_err_skb);
  3181. /**
  3182. * skb_clone_sk - create clone of skb, and take reference to socket
  3183. * @skb: the skb to clone
  3184. *
  3185. * This function creates a clone of a buffer that holds a reference on
  3186. * sk_refcnt. Buffers created via this function are meant to be
  3187. * returned using sock_queue_err_skb, or free via kfree_skb.
  3188. *
  3189. * When passing buffers allocated with this function to sock_queue_err_skb
  3190. * it is necessary to wrap the call with sock_hold/sock_put in order to
  3191. * prevent the socket from being released prior to being enqueued on
  3192. * the sk_error_queue.
  3193. */
  3194. struct sk_buff *skb_clone_sk(struct sk_buff *skb)
  3195. {
  3196. struct sock *sk = skb->sk;
  3197. struct sk_buff *clone;
  3198. if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
  3199. return NULL;
  3200. clone = skb_clone(skb, GFP_ATOMIC);
  3201. if (!clone) {
  3202. sock_put(sk);
  3203. return NULL;
  3204. }
  3205. clone->sk = sk;
  3206. clone->destructor = sock_efree;
  3207. return clone;
  3208. }
  3209. EXPORT_SYMBOL(skb_clone_sk);
  3210. static void __skb_complete_tx_timestamp(struct sk_buff *skb,
  3211. struct sock *sk,
  3212. int tstype)
  3213. {
  3214. struct sock_exterr_skb *serr;
  3215. int err;
  3216. serr = SKB_EXT_ERR(skb);
  3217. memset(serr, 0, sizeof(*serr));
  3218. serr->ee.ee_errno = ENOMSG;
  3219. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  3220. serr->ee.ee_info = tstype;
  3221. if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
  3222. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  3223. if (sk->sk_protocol == IPPROTO_TCP &&
  3224. sk->sk_type == SOCK_STREAM)
  3225. serr->ee.ee_data -= sk->sk_tskey;
  3226. }
  3227. err = sock_queue_err_skb(sk, skb);
  3228. if (err)
  3229. kfree_skb(skb);
  3230. }
  3231. static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
  3232. {
  3233. bool ret;
  3234. if (likely(sysctl_tstamp_allow_data || tsonly))
  3235. return true;
  3236. read_lock_bh(&sk->sk_callback_lock);
  3237. ret = sk->sk_socket && sk->sk_socket->file &&
  3238. file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
  3239. read_unlock_bh(&sk->sk_callback_lock);
  3240. return ret;
  3241. }
  3242. void skb_complete_tx_timestamp(struct sk_buff *skb,
  3243. struct skb_shared_hwtstamps *hwtstamps)
  3244. {
  3245. struct sock *sk = skb->sk;
  3246. if (!skb_may_tx_timestamp(sk, false))
  3247. return;
  3248. /* take a reference to prevent skb_orphan() from freeing the socket */
  3249. sock_hold(sk);
  3250. *skb_hwtstamps(skb) = *hwtstamps;
  3251. __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
  3252. sock_put(sk);
  3253. }
  3254. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  3255. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  3256. struct skb_shared_hwtstamps *hwtstamps,
  3257. struct sock *sk, int tstype)
  3258. {
  3259. struct sk_buff *skb;
  3260. bool tsonly;
  3261. if (!sk)
  3262. return;
  3263. tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
  3264. if (!skb_may_tx_timestamp(sk, tsonly))
  3265. return;
  3266. if (tsonly)
  3267. skb = alloc_skb(0, GFP_ATOMIC);
  3268. else
  3269. skb = skb_clone(orig_skb, GFP_ATOMIC);
  3270. if (!skb)
  3271. return;
  3272. if (tsonly) {
  3273. skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
  3274. skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
  3275. }
  3276. if (hwtstamps)
  3277. *skb_hwtstamps(skb) = *hwtstamps;
  3278. else
  3279. skb->tstamp = ktime_get_real();
  3280. __skb_complete_tx_timestamp(skb, sk, tstype);
  3281. }
  3282. EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
  3283. void skb_tstamp_tx(struct sk_buff *orig_skb,
  3284. struct skb_shared_hwtstamps *hwtstamps)
  3285. {
  3286. return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
  3287. SCM_TSTAMP_SND);
  3288. }
  3289. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  3290. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  3291. {
  3292. struct sock *sk = skb->sk;
  3293. struct sock_exterr_skb *serr;
  3294. int err;
  3295. skb->wifi_acked_valid = 1;
  3296. skb->wifi_acked = acked;
  3297. serr = SKB_EXT_ERR(skb);
  3298. memset(serr, 0, sizeof(*serr));
  3299. serr->ee.ee_errno = ENOMSG;
  3300. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  3301. /* take a reference to prevent skb_orphan() from freeing the socket */
  3302. sock_hold(sk);
  3303. err = sock_queue_err_skb(sk, skb);
  3304. if (err)
  3305. kfree_skb(skb);
  3306. sock_put(sk);
  3307. }
  3308. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  3309. /**
  3310. * skb_partial_csum_set - set up and verify partial csum values for packet
  3311. * @skb: the skb to set
  3312. * @start: the number of bytes after skb->data to start checksumming.
  3313. * @off: the offset from start to place the checksum.
  3314. *
  3315. * For untrusted partially-checksummed packets, we need to make sure the values
  3316. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  3317. *
  3318. * This function checks and sets those values and skb->ip_summed: if this
  3319. * returns false you should drop the packet.
  3320. */
  3321. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  3322. {
  3323. if (unlikely(start > skb_headlen(skb)) ||
  3324. unlikely((int)start + off > skb_headlen(skb) - 2)) {
  3325. net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
  3326. start, off, skb_headlen(skb));
  3327. return false;
  3328. }
  3329. skb->ip_summed = CHECKSUM_PARTIAL;
  3330. skb->csum_start = skb_headroom(skb) + start;
  3331. skb->csum_offset = off;
  3332. skb_set_transport_header(skb, start);
  3333. return true;
  3334. }
  3335. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  3336. static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  3337. unsigned int max)
  3338. {
  3339. if (skb_headlen(skb) >= len)
  3340. return 0;
  3341. /* If we need to pullup then pullup to the max, so we
  3342. * won't need to do it again.
  3343. */
  3344. if (max > skb->len)
  3345. max = skb->len;
  3346. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  3347. return -ENOMEM;
  3348. if (skb_headlen(skb) < len)
  3349. return -EPROTO;
  3350. return 0;
  3351. }
  3352. #define MAX_TCP_HDR_LEN (15 * 4)
  3353. static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
  3354. typeof(IPPROTO_IP) proto,
  3355. unsigned int off)
  3356. {
  3357. switch (proto) {
  3358. int err;
  3359. case IPPROTO_TCP:
  3360. err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
  3361. off + MAX_TCP_HDR_LEN);
  3362. if (!err && !skb_partial_csum_set(skb, off,
  3363. offsetof(struct tcphdr,
  3364. check)))
  3365. err = -EPROTO;
  3366. return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
  3367. case IPPROTO_UDP:
  3368. err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
  3369. off + sizeof(struct udphdr));
  3370. if (!err && !skb_partial_csum_set(skb, off,
  3371. offsetof(struct udphdr,
  3372. check)))
  3373. err = -EPROTO;
  3374. return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
  3375. }
  3376. return ERR_PTR(-EPROTO);
  3377. }
  3378. /* This value should be large enough to cover a tagged ethernet header plus
  3379. * maximally sized IP and TCP or UDP headers.
  3380. */
  3381. #define MAX_IP_HDR_LEN 128
  3382. static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
  3383. {
  3384. unsigned int off;
  3385. bool fragment;
  3386. __sum16 *csum;
  3387. int err;
  3388. fragment = false;
  3389. err = skb_maybe_pull_tail(skb,
  3390. sizeof(struct iphdr),
  3391. MAX_IP_HDR_LEN);
  3392. if (err < 0)
  3393. goto out;
  3394. if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
  3395. fragment = true;
  3396. off = ip_hdrlen(skb);
  3397. err = -EPROTO;
  3398. if (fragment)
  3399. goto out;
  3400. csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
  3401. if (IS_ERR(csum))
  3402. return PTR_ERR(csum);
  3403. if (recalculate)
  3404. *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  3405. ip_hdr(skb)->daddr,
  3406. skb->len - off,
  3407. ip_hdr(skb)->protocol, 0);
  3408. err = 0;
  3409. out:
  3410. return err;
  3411. }
  3412. /* This value should be large enough to cover a tagged ethernet header plus
  3413. * an IPv6 header, all options, and a maximal TCP or UDP header.
  3414. */
  3415. #define MAX_IPV6_HDR_LEN 256
  3416. #define OPT_HDR(type, skb, off) \
  3417. (type *)(skb_network_header(skb) + (off))
  3418. static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
  3419. {
  3420. int err;
  3421. u8 nexthdr;
  3422. unsigned int off;
  3423. unsigned int len;
  3424. bool fragment;
  3425. bool done;
  3426. __sum16 *csum;
  3427. fragment = false;
  3428. done = false;
  3429. off = sizeof(struct ipv6hdr);
  3430. err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  3431. if (err < 0)
  3432. goto out;
  3433. nexthdr = ipv6_hdr(skb)->nexthdr;
  3434. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  3435. while (off <= len && !done) {
  3436. switch (nexthdr) {
  3437. case IPPROTO_DSTOPTS:
  3438. case IPPROTO_HOPOPTS:
  3439. case IPPROTO_ROUTING: {
  3440. struct ipv6_opt_hdr *hp;
  3441. err = skb_maybe_pull_tail(skb,
  3442. off +
  3443. sizeof(struct ipv6_opt_hdr),
  3444. MAX_IPV6_HDR_LEN);
  3445. if (err < 0)
  3446. goto out;
  3447. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  3448. nexthdr = hp->nexthdr;
  3449. off += ipv6_optlen(hp);
  3450. break;
  3451. }
  3452. case IPPROTO_AH: {
  3453. struct ip_auth_hdr *hp;
  3454. err = skb_maybe_pull_tail(skb,
  3455. off +
  3456. sizeof(struct ip_auth_hdr),
  3457. MAX_IPV6_HDR_LEN);
  3458. if (err < 0)
  3459. goto out;
  3460. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  3461. nexthdr = hp->nexthdr;
  3462. off += ipv6_authlen(hp);
  3463. break;
  3464. }
  3465. case IPPROTO_FRAGMENT: {
  3466. struct frag_hdr *hp;
  3467. err = skb_maybe_pull_tail(skb,
  3468. off +
  3469. sizeof(struct frag_hdr),
  3470. MAX_IPV6_HDR_LEN);
  3471. if (err < 0)
  3472. goto out;
  3473. hp = OPT_HDR(struct frag_hdr, skb, off);
  3474. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  3475. fragment = true;
  3476. nexthdr = hp->nexthdr;
  3477. off += sizeof(struct frag_hdr);
  3478. break;
  3479. }
  3480. default:
  3481. done = true;
  3482. break;
  3483. }
  3484. }
  3485. err = -EPROTO;
  3486. if (!done || fragment)
  3487. goto out;
  3488. csum = skb_checksum_setup_ip(skb, nexthdr, off);
  3489. if (IS_ERR(csum))
  3490. return PTR_ERR(csum);
  3491. if (recalculate)
  3492. *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  3493. &ipv6_hdr(skb)->daddr,
  3494. skb->len - off, nexthdr, 0);
  3495. err = 0;
  3496. out:
  3497. return err;
  3498. }
  3499. /**
  3500. * skb_checksum_setup - set up partial checksum offset
  3501. * @skb: the skb to set up
  3502. * @recalculate: if true the pseudo-header checksum will be recalculated
  3503. */
  3504. int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
  3505. {
  3506. int err;
  3507. switch (skb->protocol) {
  3508. case htons(ETH_P_IP):
  3509. err = skb_checksum_setup_ipv4(skb, recalculate);
  3510. break;
  3511. case htons(ETH_P_IPV6):
  3512. err = skb_checksum_setup_ipv6(skb, recalculate);
  3513. break;
  3514. default:
  3515. err = -EPROTO;
  3516. break;
  3517. }
  3518. return err;
  3519. }
  3520. EXPORT_SYMBOL(skb_checksum_setup);
  3521. /**
  3522. * skb_checksum_maybe_trim - maybe trims the given skb
  3523. * @skb: the skb to check
  3524. * @transport_len: the data length beyond the network header
  3525. *
  3526. * Checks whether the given skb has data beyond the given transport length.
  3527. * If so, returns a cloned skb trimmed to this transport length.
  3528. * Otherwise returns the provided skb. Returns NULL in error cases
  3529. * (e.g. transport_len exceeds skb length or out-of-memory).
  3530. *
  3531. * Caller needs to set the skb transport header and free any returned skb if it
  3532. * differs from the provided skb.
  3533. */
  3534. static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
  3535. unsigned int transport_len)
  3536. {
  3537. struct sk_buff *skb_chk;
  3538. unsigned int len = skb_transport_offset(skb) + transport_len;
  3539. int ret;
  3540. if (skb->len < len)
  3541. return NULL;
  3542. else if (skb->len == len)
  3543. return skb;
  3544. skb_chk = skb_clone(skb, GFP_ATOMIC);
  3545. if (!skb_chk)
  3546. return NULL;
  3547. ret = pskb_trim_rcsum(skb_chk, len);
  3548. if (ret) {
  3549. kfree_skb(skb_chk);
  3550. return NULL;
  3551. }
  3552. return skb_chk;
  3553. }
  3554. /**
  3555. * skb_checksum_trimmed - validate checksum of an skb
  3556. * @skb: the skb to check
  3557. * @transport_len: the data length beyond the network header
  3558. * @skb_chkf: checksum function to use
  3559. *
  3560. * Applies the given checksum function skb_chkf to the provided skb.
  3561. * Returns a checked and maybe trimmed skb. Returns NULL on error.
  3562. *
  3563. * If the skb has data beyond the given transport length, then a
  3564. * trimmed & cloned skb is checked and returned.
  3565. *
  3566. * Caller needs to set the skb transport header and free any returned skb if it
  3567. * differs from the provided skb.
  3568. */
  3569. struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
  3570. unsigned int transport_len,
  3571. __sum16(*skb_chkf)(struct sk_buff *skb))
  3572. {
  3573. struct sk_buff *skb_chk;
  3574. unsigned int offset = skb_transport_offset(skb);
  3575. __sum16 ret;
  3576. skb_chk = skb_checksum_maybe_trim(skb, transport_len);
  3577. if (!skb_chk)
  3578. goto err;
  3579. if (!pskb_may_pull(skb_chk, offset))
  3580. goto err;
  3581. skb_pull_rcsum(skb_chk, offset);
  3582. ret = skb_chkf(skb_chk);
  3583. skb_push_rcsum(skb_chk, offset);
  3584. if (ret)
  3585. goto err;
  3586. return skb_chk;
  3587. err:
  3588. if (skb_chk && skb_chk != skb)
  3589. kfree_skb(skb_chk);
  3590. return NULL;
  3591. }
  3592. EXPORT_SYMBOL(skb_checksum_trimmed);
  3593. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  3594. {
  3595. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  3596. skb->dev->name);
  3597. }
  3598. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  3599. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  3600. {
  3601. if (head_stolen) {
  3602. skb_release_head_state(skb);
  3603. kmem_cache_free(skbuff_head_cache, skb);
  3604. } else {
  3605. __kfree_skb(skb);
  3606. }
  3607. }
  3608. EXPORT_SYMBOL(kfree_skb_partial);
  3609. /**
  3610. * skb_try_coalesce - try to merge skb to prior one
  3611. * @to: prior buffer
  3612. * @from: buffer to add
  3613. * @fragstolen: pointer to boolean
  3614. * @delta_truesize: how much more was allocated than was requested
  3615. */
  3616. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  3617. bool *fragstolen, int *delta_truesize)
  3618. {
  3619. int i, delta, len = from->len;
  3620. *fragstolen = false;
  3621. if (skb_cloned(to))
  3622. return false;
  3623. if (len <= skb_tailroom(to)) {
  3624. if (len)
  3625. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  3626. *delta_truesize = 0;
  3627. return true;
  3628. }
  3629. if (skb_has_frag_list(to) || skb_has_frag_list(from))
  3630. return false;
  3631. if (skb_headlen(from) != 0) {
  3632. struct page *page;
  3633. unsigned int offset;
  3634. if (skb_shinfo(to)->nr_frags +
  3635. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  3636. return false;
  3637. if (skb_head_is_locked(from))
  3638. return false;
  3639. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  3640. page = virt_to_head_page(from->head);
  3641. offset = from->data - (unsigned char *)page_address(page);
  3642. skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
  3643. page, offset, skb_headlen(from));
  3644. *fragstolen = true;
  3645. } else {
  3646. if (skb_shinfo(to)->nr_frags +
  3647. skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
  3648. return false;
  3649. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  3650. }
  3651. WARN_ON_ONCE(delta < len);
  3652. memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
  3653. skb_shinfo(from)->frags,
  3654. skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
  3655. skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
  3656. if (!skb_cloned(from))
  3657. skb_shinfo(from)->nr_frags = 0;
  3658. /* if the skb is not cloned this does nothing
  3659. * since we set nr_frags to 0.
  3660. */
  3661. for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
  3662. skb_frag_ref(from, i);
  3663. to->truesize += delta;
  3664. to->len += len;
  3665. to->data_len += len;
  3666. *delta_truesize = delta;
  3667. return true;
  3668. }
  3669. EXPORT_SYMBOL(skb_try_coalesce);
  3670. /**
  3671. * skb_scrub_packet - scrub an skb
  3672. *
  3673. * @skb: buffer to clean
  3674. * @xnet: packet is crossing netns
  3675. *
  3676. * skb_scrub_packet can be used after encapsulating or decapsulting a packet
  3677. * into/from a tunnel. Some information have to be cleared during these
  3678. * operations.
  3679. * skb_scrub_packet can also be used to clean a skb before injecting it in
  3680. * another namespace (@xnet == true). We have to clear all information in the
  3681. * skb that could impact namespace isolation.
  3682. */
  3683. void skb_scrub_packet(struct sk_buff *skb, bool xnet)
  3684. {
  3685. skb->tstamp.tv64 = 0;
  3686. skb->pkt_type = PACKET_HOST;
  3687. skb->skb_iif = 0;
  3688. skb->ignore_df = 0;
  3689. skb_dst_drop(skb);
  3690. secpath_reset(skb);
  3691. nf_reset(skb);
  3692. nf_reset_trace(skb);
  3693. if (!xnet)
  3694. return;
  3695. skb_orphan(skb);
  3696. skb->mark = 0;
  3697. }
  3698. EXPORT_SYMBOL_GPL(skb_scrub_packet);
  3699. /**
  3700. * skb_gso_transport_seglen - Return length of individual segments of a gso packet
  3701. *
  3702. * @skb: GSO skb
  3703. *
  3704. * skb_gso_transport_seglen is used to determine the real size of the
  3705. * individual segments, including Layer4 headers (TCP/UDP).
  3706. *
  3707. * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
  3708. */
  3709. unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
  3710. {
  3711. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  3712. unsigned int thlen = 0;
  3713. if (skb->encapsulation) {
  3714. thlen = skb_inner_transport_header(skb) -
  3715. skb_transport_header(skb);
  3716. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  3717. thlen += inner_tcp_hdrlen(skb);
  3718. } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  3719. thlen = tcp_hdrlen(skb);
  3720. } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
  3721. thlen = sizeof(struct sctphdr);
  3722. }
  3723. /* UFO sets gso_size to the size of the fragmentation
  3724. * payload, i.e. the size of the L4 (UDP) header is already
  3725. * accounted for.
  3726. */
  3727. return thlen + shinfo->gso_size;
  3728. }
  3729. EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
  3730. /**
  3731. * skb_gso_validate_mtu - Return in case such skb fits a given MTU
  3732. *
  3733. * @skb: GSO skb
  3734. * @mtu: MTU to validate against
  3735. *
  3736. * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
  3737. * once split.
  3738. */
  3739. bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
  3740. {
  3741. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  3742. const struct sk_buff *iter;
  3743. unsigned int hlen;
  3744. hlen = skb_gso_network_seglen(skb);
  3745. if (shinfo->gso_size != GSO_BY_FRAGS)
  3746. return hlen <= mtu;
  3747. /* Undo this so we can re-use header sizes */
  3748. hlen -= GSO_BY_FRAGS;
  3749. skb_walk_frags(skb, iter) {
  3750. if (hlen + skb_headlen(iter) > mtu)
  3751. return false;
  3752. }
  3753. return true;
  3754. }
  3755. EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
  3756. static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
  3757. {
  3758. if (skb_cow(skb, skb_headroom(skb)) < 0) {
  3759. kfree_skb(skb);
  3760. return NULL;
  3761. }
  3762. memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
  3763. 2 * ETH_ALEN);
  3764. skb->mac_header += VLAN_HLEN;
  3765. return skb;
  3766. }
  3767. struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
  3768. {
  3769. struct vlan_hdr *vhdr;
  3770. u16 vlan_tci;
  3771. if (unlikely(skb_vlan_tag_present(skb))) {
  3772. /* vlan_tci is already set-up so leave this for another time */
  3773. return skb;
  3774. }
  3775. skb = skb_share_check(skb, GFP_ATOMIC);
  3776. if (unlikely(!skb))
  3777. goto err_free;
  3778. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  3779. goto err_free;
  3780. vhdr = (struct vlan_hdr *)skb->data;
  3781. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  3782. __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
  3783. skb_pull_rcsum(skb, VLAN_HLEN);
  3784. vlan_set_encap_proto(skb, vhdr);
  3785. skb = skb_reorder_vlan_header(skb);
  3786. if (unlikely(!skb))
  3787. goto err_free;
  3788. skb_reset_network_header(skb);
  3789. skb_reset_transport_header(skb);
  3790. skb_reset_mac_len(skb);
  3791. return skb;
  3792. err_free:
  3793. kfree_skb(skb);
  3794. return NULL;
  3795. }
  3796. EXPORT_SYMBOL(skb_vlan_untag);
  3797. int skb_ensure_writable(struct sk_buff *skb, int write_len)
  3798. {
  3799. if (!pskb_may_pull(skb, write_len))
  3800. return -ENOMEM;
  3801. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  3802. return 0;
  3803. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  3804. }
  3805. EXPORT_SYMBOL(skb_ensure_writable);
  3806. /* remove VLAN header from packet and update csum accordingly. */
  3807. static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
  3808. {
  3809. struct vlan_hdr *vhdr;
  3810. unsigned int offset = skb->data - skb_mac_header(skb);
  3811. int err;
  3812. __skb_push(skb, offset);
  3813. err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
  3814. if (unlikely(err))
  3815. goto pull;
  3816. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  3817. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  3818. *vlan_tci = ntohs(vhdr->h_vlan_TCI);
  3819. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  3820. __skb_pull(skb, VLAN_HLEN);
  3821. vlan_set_encap_proto(skb, vhdr);
  3822. skb->mac_header += VLAN_HLEN;
  3823. if (skb_network_offset(skb) < ETH_HLEN)
  3824. skb_set_network_header(skb, ETH_HLEN);
  3825. skb_reset_mac_len(skb);
  3826. pull:
  3827. __skb_pull(skb, offset);
  3828. return err;
  3829. }
  3830. int skb_vlan_pop(struct sk_buff *skb)
  3831. {
  3832. u16 vlan_tci;
  3833. __be16 vlan_proto;
  3834. int err;
  3835. if (likely(skb_vlan_tag_present(skb))) {
  3836. skb->vlan_tci = 0;
  3837. } else {
  3838. if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
  3839. skb->protocol != htons(ETH_P_8021AD)) ||
  3840. skb->len < VLAN_ETH_HLEN))
  3841. return 0;
  3842. err = __skb_vlan_pop(skb, &vlan_tci);
  3843. if (err)
  3844. return err;
  3845. }
  3846. /* move next vlan tag to hw accel tag */
  3847. if (likely((skb->protocol != htons(ETH_P_8021Q) &&
  3848. skb->protocol != htons(ETH_P_8021AD)) ||
  3849. skb->len < VLAN_ETH_HLEN))
  3850. return 0;
  3851. vlan_proto = skb->protocol;
  3852. err = __skb_vlan_pop(skb, &vlan_tci);
  3853. if (unlikely(err))
  3854. return err;
  3855. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  3856. return 0;
  3857. }
  3858. EXPORT_SYMBOL(skb_vlan_pop);
  3859. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  3860. {
  3861. if (skb_vlan_tag_present(skb)) {
  3862. unsigned int offset = skb->data - skb_mac_header(skb);
  3863. int err;
  3864. /* __vlan_insert_tag expect skb->data pointing to mac header.
  3865. * So change skb->data before calling it and change back to
  3866. * original position later
  3867. */
  3868. __skb_push(skb, offset);
  3869. err = __vlan_insert_tag(skb, skb->vlan_proto,
  3870. skb_vlan_tag_get(skb));
  3871. if (err) {
  3872. __skb_pull(skb, offset);
  3873. return err;
  3874. }
  3875. skb->protocol = skb->vlan_proto;
  3876. skb->mac_len += VLAN_HLEN;
  3877. skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  3878. __skb_pull(skb, offset);
  3879. }
  3880. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  3881. return 0;
  3882. }
  3883. EXPORT_SYMBOL(skb_vlan_push);
  3884. /**
  3885. * alloc_skb_with_frags - allocate skb with page frags
  3886. *
  3887. * @header_len: size of linear part
  3888. * @data_len: needed length in frags
  3889. * @max_page_order: max page order desired.
  3890. * @errcode: pointer to error code if any
  3891. * @gfp_mask: allocation mask
  3892. *
  3893. * This can be used to allocate a paged skb, given a maximal order for frags.
  3894. */
  3895. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  3896. unsigned long data_len,
  3897. int max_page_order,
  3898. int *errcode,
  3899. gfp_t gfp_mask)
  3900. {
  3901. int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  3902. unsigned long chunk;
  3903. struct sk_buff *skb;
  3904. struct page *page;
  3905. gfp_t gfp_head;
  3906. int i;
  3907. *errcode = -EMSGSIZE;
  3908. /* Note this test could be relaxed, if we succeed to allocate
  3909. * high order pages...
  3910. */
  3911. if (npages > MAX_SKB_FRAGS)
  3912. return NULL;
  3913. gfp_head = gfp_mask;
  3914. if (gfp_head & __GFP_DIRECT_RECLAIM)
  3915. gfp_head |= __GFP_REPEAT;
  3916. *errcode = -ENOBUFS;
  3917. skb = alloc_skb(header_len, gfp_head);
  3918. if (!skb)
  3919. return NULL;
  3920. skb->truesize += npages << PAGE_SHIFT;
  3921. for (i = 0; npages > 0; i++) {
  3922. int order = max_page_order;
  3923. while (order) {
  3924. if (npages >= 1 << order) {
  3925. page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
  3926. __GFP_COMP |
  3927. __GFP_NOWARN |
  3928. __GFP_NORETRY,
  3929. order);
  3930. if (page)
  3931. goto fill_page;
  3932. /* Do not retry other high order allocations */
  3933. order = 1;
  3934. max_page_order = 0;
  3935. }
  3936. order--;
  3937. }
  3938. page = alloc_page(gfp_mask);
  3939. if (!page)
  3940. goto failure;
  3941. fill_page:
  3942. chunk = min_t(unsigned long, data_len,
  3943. PAGE_SIZE << order);
  3944. skb_fill_page_desc(skb, i, page, 0, chunk);
  3945. data_len -= chunk;
  3946. npages -= 1 << order;
  3947. }
  3948. return skb;
  3949. failure:
  3950. kfree_skb(skb);
  3951. return NULL;
  3952. }
  3953. EXPORT_SYMBOL(alloc_skb_with_frags);
  3954. /* carve out the first off bytes from skb when off < headlen */
  3955. static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
  3956. const int headlen, gfp_t gfp_mask)
  3957. {
  3958. int i;
  3959. int size = skb_end_offset(skb);
  3960. int new_hlen = headlen - off;
  3961. u8 *data;
  3962. size = SKB_DATA_ALIGN(size);
  3963. if (skb_pfmemalloc(skb))
  3964. gfp_mask |= __GFP_MEMALLOC;
  3965. data = kmalloc_reserve(size +
  3966. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  3967. gfp_mask, NUMA_NO_NODE, NULL);
  3968. if (!data)
  3969. return -ENOMEM;
  3970. size = SKB_WITH_OVERHEAD(ksize(data));
  3971. /* Copy real data, and all frags */
  3972. skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
  3973. skb->len -= off;
  3974. memcpy((struct skb_shared_info *)(data + size),
  3975. skb_shinfo(skb),
  3976. offsetof(struct skb_shared_info,
  3977. frags[skb_shinfo(skb)->nr_frags]));
  3978. if (skb_cloned(skb)) {
  3979. /* drop the old head gracefully */
  3980. if (skb_orphan_frags(skb, gfp_mask)) {
  3981. kfree(data);
  3982. return -ENOMEM;
  3983. }
  3984. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  3985. skb_frag_ref(skb, i);
  3986. if (skb_has_frag_list(skb))
  3987. skb_clone_fraglist(skb);
  3988. skb_release_data(skb);
  3989. } else {
  3990. /* we can reuse existing recount- all we did was
  3991. * relocate values
  3992. */
  3993. skb_free_head(skb);
  3994. }
  3995. skb->head = data;
  3996. skb->data = data;
  3997. skb->head_frag = 0;
  3998. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  3999. skb->end = size;
  4000. #else
  4001. skb->end = skb->head + size;
  4002. #endif
  4003. skb_set_tail_pointer(skb, skb_headlen(skb));
  4004. skb_headers_offset_update(skb, 0);
  4005. skb->cloned = 0;
  4006. skb->hdr_len = 0;
  4007. skb->nohdr = 0;
  4008. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4009. return 0;
  4010. }
  4011. static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
  4012. /* carve out the first eat bytes from skb's frag_list. May recurse into
  4013. * pskb_carve()
  4014. */
  4015. static int pskb_carve_frag_list(struct sk_buff *skb,
  4016. struct skb_shared_info *shinfo, int eat,
  4017. gfp_t gfp_mask)
  4018. {
  4019. struct sk_buff *list = shinfo->frag_list;
  4020. struct sk_buff *clone = NULL;
  4021. struct sk_buff *insp = NULL;
  4022. do {
  4023. if (!list) {
  4024. pr_err("Not enough bytes to eat. Want %d\n", eat);
  4025. return -EFAULT;
  4026. }
  4027. if (list->len <= eat) {
  4028. /* Eaten as whole. */
  4029. eat -= list->len;
  4030. list = list->next;
  4031. insp = list;
  4032. } else {
  4033. /* Eaten partially. */
  4034. if (skb_shared(list)) {
  4035. clone = skb_clone(list, gfp_mask);
  4036. if (!clone)
  4037. return -ENOMEM;
  4038. insp = list->next;
  4039. list = clone;
  4040. } else {
  4041. /* This may be pulled without problems. */
  4042. insp = list;
  4043. }
  4044. if (pskb_carve(list, eat, gfp_mask) < 0) {
  4045. kfree_skb(clone);
  4046. return -ENOMEM;
  4047. }
  4048. break;
  4049. }
  4050. } while (eat);
  4051. /* Free pulled out fragments. */
  4052. while ((list = shinfo->frag_list) != insp) {
  4053. shinfo->frag_list = list->next;
  4054. kfree_skb(list);
  4055. }
  4056. /* And insert new clone at head. */
  4057. if (clone) {
  4058. clone->next = list;
  4059. shinfo->frag_list = clone;
  4060. }
  4061. return 0;
  4062. }
  4063. /* carve off first len bytes from skb. Split line (off) is in the
  4064. * non-linear part of skb
  4065. */
  4066. static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
  4067. int pos, gfp_t gfp_mask)
  4068. {
  4069. int i, k = 0;
  4070. int size = skb_end_offset(skb);
  4071. u8 *data;
  4072. const int nfrags = skb_shinfo(skb)->nr_frags;
  4073. struct skb_shared_info *shinfo;
  4074. size = SKB_DATA_ALIGN(size);
  4075. if (skb_pfmemalloc(skb))
  4076. gfp_mask |= __GFP_MEMALLOC;
  4077. data = kmalloc_reserve(size +
  4078. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  4079. gfp_mask, NUMA_NO_NODE, NULL);
  4080. if (!data)
  4081. return -ENOMEM;
  4082. size = SKB_WITH_OVERHEAD(ksize(data));
  4083. memcpy((struct skb_shared_info *)(data + size),
  4084. skb_shinfo(skb), offsetof(struct skb_shared_info,
  4085. frags[skb_shinfo(skb)->nr_frags]));
  4086. if (skb_orphan_frags(skb, gfp_mask)) {
  4087. kfree(data);
  4088. return -ENOMEM;
  4089. }
  4090. shinfo = (struct skb_shared_info *)(data + size);
  4091. for (i = 0; i < nfrags; i++) {
  4092. int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  4093. if (pos + fsize > off) {
  4094. shinfo->frags[k] = skb_shinfo(skb)->frags[i];
  4095. if (pos < off) {
  4096. /* Split frag.
  4097. * We have two variants in this case:
  4098. * 1. Move all the frag to the second
  4099. * part, if it is possible. F.e.
  4100. * this approach is mandatory for TUX,
  4101. * where splitting is expensive.
  4102. * 2. Split is accurately. We make this.
  4103. */
  4104. shinfo->frags[0].page_offset += off - pos;
  4105. skb_frag_size_sub(&shinfo->frags[0], off - pos);
  4106. }
  4107. skb_frag_ref(skb, i);
  4108. k++;
  4109. }
  4110. pos += fsize;
  4111. }
  4112. shinfo->nr_frags = k;
  4113. if (skb_has_frag_list(skb))
  4114. skb_clone_fraglist(skb);
  4115. if (k == 0) {
  4116. /* split line is in frag list */
  4117. pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
  4118. }
  4119. skb_release_data(skb);
  4120. skb->head = data;
  4121. skb->head_frag = 0;
  4122. skb->data = data;
  4123. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  4124. skb->end = size;
  4125. #else
  4126. skb->end = skb->head + size;
  4127. #endif
  4128. skb_reset_tail_pointer(skb);
  4129. skb_headers_offset_update(skb, 0);
  4130. skb->cloned = 0;
  4131. skb->hdr_len = 0;
  4132. skb->nohdr = 0;
  4133. skb->len -= off;
  4134. skb->data_len = skb->len;
  4135. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4136. return 0;
  4137. }
  4138. /* remove len bytes from the beginning of the skb */
  4139. static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
  4140. {
  4141. int headlen = skb_headlen(skb);
  4142. if (len < headlen)
  4143. return pskb_carve_inside_header(skb, len, headlen, gfp);
  4144. else
  4145. return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
  4146. }
  4147. /* Extract to_copy bytes starting at off from skb, and return this in
  4148. * a new skb
  4149. */
  4150. struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
  4151. int to_copy, gfp_t gfp)
  4152. {
  4153. struct sk_buff *clone = skb_clone(skb, gfp);
  4154. if (!clone)
  4155. return NULL;
  4156. if (pskb_carve(clone, off, gfp) < 0 ||
  4157. pskb_trim(clone, to_copy)) {
  4158. kfree_skb(clone);
  4159. return NULL;
  4160. }
  4161. return clone;
  4162. }
  4163. EXPORT_SYMBOL(pskb_extract);