skbuff.c 137 KB

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