skbuff.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527
  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_block_sub(skb->csum,
  1586. skb_checksum(skb, len, delta, 0),
  1587. len);
  1588. }
  1589. return __pskb_trim(skb, len);
  1590. }
  1591. EXPORT_SYMBOL(pskb_trim_rcsum_slow);
  1592. /**
  1593. * __pskb_pull_tail - advance tail of skb header
  1594. * @skb: buffer to reallocate
  1595. * @delta: number of bytes to advance tail
  1596. *
  1597. * The function makes a sense only on a fragmented &sk_buff,
  1598. * it expands header moving its tail forward and copying necessary
  1599. * data from fragmented part.
  1600. *
  1601. * &sk_buff MUST have reference count of 1.
  1602. *
  1603. * Returns %NULL (and &sk_buff does not change) if pull failed
  1604. * or value of new tail of skb in the case of success.
  1605. *
  1606. * All the pointers pointing into skb header may change and must be
  1607. * reloaded after call to this function.
  1608. */
  1609. /* Moves tail of skb head forward, copying data from fragmented part,
  1610. * when it is necessary.
  1611. * 1. It may fail due to malloc failure.
  1612. * 2. It may change skb pointers.
  1613. *
  1614. * It is pretty complicated. Luckily, it is called only in exceptional cases.
  1615. */
  1616. void *__pskb_pull_tail(struct sk_buff *skb, int delta)
  1617. {
  1618. /* If skb has not enough free space at tail, get new one
  1619. * plus 128 bytes for future expansions. If we have enough
  1620. * room at tail, reallocate without expansion only if skb is cloned.
  1621. */
  1622. int i, k, eat = (skb->tail + delta) - skb->end;
  1623. if (eat > 0 || skb_cloned(skb)) {
  1624. if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
  1625. GFP_ATOMIC))
  1626. return NULL;
  1627. }
  1628. BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
  1629. skb_tail_pointer(skb), delta));
  1630. /* Optimization: no fragments, no reasons to preestimate
  1631. * size of pulled pages. Superb.
  1632. */
  1633. if (!skb_has_frag_list(skb))
  1634. goto pull_pages;
  1635. /* Estimate size of pulled pages. */
  1636. eat = delta;
  1637. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1638. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1639. if (size >= eat)
  1640. goto pull_pages;
  1641. eat -= size;
  1642. }
  1643. /* If we need update frag list, we are in troubles.
  1644. * Certainly, it is possible to add an offset to skb data,
  1645. * but taking into account that pulling is expected to
  1646. * be very rare operation, it is worth to fight against
  1647. * further bloating skb head and crucify ourselves here instead.
  1648. * Pure masohism, indeed. 8)8)
  1649. */
  1650. if (eat) {
  1651. struct sk_buff *list = skb_shinfo(skb)->frag_list;
  1652. struct sk_buff *clone = NULL;
  1653. struct sk_buff *insp = NULL;
  1654. do {
  1655. BUG_ON(!list);
  1656. if (list->len <= eat) {
  1657. /* Eaten as whole. */
  1658. eat -= list->len;
  1659. list = list->next;
  1660. insp = list;
  1661. } else {
  1662. /* Eaten partially. */
  1663. if (skb_shared(list)) {
  1664. /* Sucks! We need to fork list. :-( */
  1665. clone = skb_clone(list, GFP_ATOMIC);
  1666. if (!clone)
  1667. return NULL;
  1668. insp = list->next;
  1669. list = clone;
  1670. } else {
  1671. /* This may be pulled without
  1672. * problems. */
  1673. insp = list;
  1674. }
  1675. if (!pskb_pull(list, eat)) {
  1676. kfree_skb(clone);
  1677. return NULL;
  1678. }
  1679. break;
  1680. }
  1681. } while (eat);
  1682. /* Free pulled out fragments. */
  1683. while ((list = skb_shinfo(skb)->frag_list) != insp) {
  1684. skb_shinfo(skb)->frag_list = list->next;
  1685. kfree_skb(list);
  1686. }
  1687. /* And insert new clone at head. */
  1688. if (clone) {
  1689. clone->next = list;
  1690. skb_shinfo(skb)->frag_list = clone;
  1691. }
  1692. }
  1693. /* Success! Now we may commit changes to skb data. */
  1694. pull_pages:
  1695. eat = delta;
  1696. k = 0;
  1697. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1698. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1699. if (size <= eat) {
  1700. skb_frag_unref(skb, i);
  1701. eat -= size;
  1702. } else {
  1703. skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
  1704. if (eat) {
  1705. skb_shinfo(skb)->frags[k].page_offset += eat;
  1706. skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
  1707. if (!i)
  1708. goto end;
  1709. eat = 0;
  1710. }
  1711. k++;
  1712. }
  1713. }
  1714. skb_shinfo(skb)->nr_frags = k;
  1715. end:
  1716. skb->tail += delta;
  1717. skb->data_len -= delta;
  1718. if (!skb->data_len)
  1719. skb_zcopy_clear(skb, false);
  1720. return skb_tail_pointer(skb);
  1721. }
  1722. EXPORT_SYMBOL(__pskb_pull_tail);
  1723. /**
  1724. * skb_copy_bits - copy bits from skb to kernel buffer
  1725. * @skb: source skb
  1726. * @offset: offset in source
  1727. * @to: destination buffer
  1728. * @len: number of bytes to copy
  1729. *
  1730. * Copy the specified number of bytes from the source skb to the
  1731. * destination buffer.
  1732. *
  1733. * CAUTION ! :
  1734. * If its prototype is ever changed,
  1735. * check arch/{*}/net/{*}.S files,
  1736. * since it is called from BPF assembly code.
  1737. */
  1738. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
  1739. {
  1740. int start = skb_headlen(skb);
  1741. struct sk_buff *frag_iter;
  1742. int i, copy;
  1743. if (offset > (int)skb->len - len)
  1744. goto fault;
  1745. /* Copy header. */
  1746. if ((copy = start - offset) > 0) {
  1747. if (copy > len)
  1748. copy = len;
  1749. skb_copy_from_linear_data_offset(skb, offset, to, copy);
  1750. if ((len -= copy) == 0)
  1751. return 0;
  1752. offset += copy;
  1753. to += copy;
  1754. }
  1755. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1756. int end;
  1757. skb_frag_t *f = &skb_shinfo(skb)->frags[i];
  1758. WARN_ON(start > offset + len);
  1759. end = start + skb_frag_size(f);
  1760. if ((copy = end - offset) > 0) {
  1761. u32 p_off, p_len, copied;
  1762. struct page *p;
  1763. u8 *vaddr;
  1764. if (copy > len)
  1765. copy = len;
  1766. skb_frag_foreach_page(f,
  1767. f->page_offset + offset - start,
  1768. copy, p, p_off, p_len, copied) {
  1769. vaddr = kmap_atomic(p);
  1770. memcpy(to + copied, vaddr + p_off, p_len);
  1771. kunmap_atomic(vaddr);
  1772. }
  1773. if ((len -= copy) == 0)
  1774. return 0;
  1775. offset += copy;
  1776. to += copy;
  1777. }
  1778. start = end;
  1779. }
  1780. skb_walk_frags(skb, frag_iter) {
  1781. int end;
  1782. WARN_ON(start > offset + len);
  1783. end = start + frag_iter->len;
  1784. if ((copy = end - offset) > 0) {
  1785. if (copy > len)
  1786. copy = len;
  1787. if (skb_copy_bits(frag_iter, offset - start, to, copy))
  1788. goto fault;
  1789. if ((len -= copy) == 0)
  1790. return 0;
  1791. offset += copy;
  1792. to += copy;
  1793. }
  1794. start = end;
  1795. }
  1796. if (!len)
  1797. return 0;
  1798. fault:
  1799. return -EFAULT;
  1800. }
  1801. EXPORT_SYMBOL(skb_copy_bits);
  1802. /*
  1803. * Callback from splice_to_pipe(), if we need to release some pages
  1804. * at the end of the spd in case we error'ed out in filling the pipe.
  1805. */
  1806. static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  1807. {
  1808. put_page(spd->pages[i]);
  1809. }
  1810. static struct page *linear_to_page(struct page *page, unsigned int *len,
  1811. unsigned int *offset,
  1812. struct sock *sk)
  1813. {
  1814. struct page_frag *pfrag = sk_page_frag(sk);
  1815. if (!sk_page_frag_refill(sk, pfrag))
  1816. return NULL;
  1817. *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
  1818. memcpy(page_address(pfrag->page) + pfrag->offset,
  1819. page_address(page) + *offset, *len);
  1820. *offset = pfrag->offset;
  1821. pfrag->offset += *len;
  1822. return pfrag->page;
  1823. }
  1824. static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
  1825. struct page *page,
  1826. unsigned int offset)
  1827. {
  1828. return spd->nr_pages &&
  1829. spd->pages[spd->nr_pages - 1] == page &&
  1830. (spd->partial[spd->nr_pages - 1].offset +
  1831. spd->partial[spd->nr_pages - 1].len == offset);
  1832. }
  1833. /*
  1834. * Fill page/offset/length into spd, if it can hold more pages.
  1835. */
  1836. static bool spd_fill_page(struct splice_pipe_desc *spd,
  1837. struct pipe_inode_info *pipe, struct page *page,
  1838. unsigned int *len, unsigned int offset,
  1839. bool linear,
  1840. struct sock *sk)
  1841. {
  1842. if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
  1843. return true;
  1844. if (linear) {
  1845. page = linear_to_page(page, len, &offset, sk);
  1846. if (!page)
  1847. return true;
  1848. }
  1849. if (spd_can_coalesce(spd, page, offset)) {
  1850. spd->partial[spd->nr_pages - 1].len += *len;
  1851. return false;
  1852. }
  1853. get_page(page);
  1854. spd->pages[spd->nr_pages] = page;
  1855. spd->partial[spd->nr_pages].len = *len;
  1856. spd->partial[spd->nr_pages].offset = offset;
  1857. spd->nr_pages++;
  1858. return false;
  1859. }
  1860. static bool __splice_segment(struct page *page, unsigned int poff,
  1861. unsigned int plen, unsigned int *off,
  1862. unsigned int *len,
  1863. struct splice_pipe_desc *spd, bool linear,
  1864. struct sock *sk,
  1865. struct pipe_inode_info *pipe)
  1866. {
  1867. if (!*len)
  1868. return true;
  1869. /* skip this segment if already processed */
  1870. if (*off >= plen) {
  1871. *off -= plen;
  1872. return false;
  1873. }
  1874. /* ignore any bits we already processed */
  1875. poff += *off;
  1876. plen -= *off;
  1877. *off = 0;
  1878. do {
  1879. unsigned int flen = min(*len, plen);
  1880. if (spd_fill_page(spd, pipe, page, &flen, poff,
  1881. linear, sk))
  1882. return true;
  1883. poff += flen;
  1884. plen -= flen;
  1885. *len -= flen;
  1886. } while (*len && plen);
  1887. return false;
  1888. }
  1889. /*
  1890. * Map linear and fragment data from the skb to spd. It reports true if the
  1891. * pipe is full or if we already spliced the requested length.
  1892. */
  1893. static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
  1894. unsigned int *offset, unsigned int *len,
  1895. struct splice_pipe_desc *spd, struct sock *sk)
  1896. {
  1897. int seg;
  1898. struct sk_buff *iter;
  1899. /* map the linear part :
  1900. * If skb->head_frag is set, this 'linear' part is backed by a
  1901. * fragment, and if the head is not shared with any clones then
  1902. * we can avoid a copy since we own the head portion of this page.
  1903. */
  1904. if (__splice_segment(virt_to_page(skb->data),
  1905. (unsigned long) skb->data & (PAGE_SIZE - 1),
  1906. skb_headlen(skb),
  1907. offset, len, spd,
  1908. skb_head_is_locked(skb),
  1909. sk, pipe))
  1910. return true;
  1911. /*
  1912. * then map the fragments
  1913. */
  1914. for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
  1915. const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
  1916. if (__splice_segment(skb_frag_page(f),
  1917. f->page_offset, skb_frag_size(f),
  1918. offset, len, spd, false, sk, pipe))
  1919. return true;
  1920. }
  1921. skb_walk_frags(skb, iter) {
  1922. if (*offset >= iter->len) {
  1923. *offset -= iter->len;
  1924. continue;
  1925. }
  1926. /* __skb_splice_bits() only fails if the output has no room
  1927. * left, so no point in going over the frag_list for the error
  1928. * case.
  1929. */
  1930. if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
  1931. return true;
  1932. }
  1933. return false;
  1934. }
  1935. /*
  1936. * Map data from the skb to a pipe. Should handle both the linear part,
  1937. * the fragments, and the frag list.
  1938. */
  1939. int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
  1940. struct pipe_inode_info *pipe, unsigned int tlen,
  1941. unsigned int flags)
  1942. {
  1943. struct partial_page partial[MAX_SKB_FRAGS];
  1944. struct page *pages[MAX_SKB_FRAGS];
  1945. struct splice_pipe_desc spd = {
  1946. .pages = pages,
  1947. .partial = partial,
  1948. .nr_pages_max = MAX_SKB_FRAGS,
  1949. .ops = &nosteal_pipe_buf_ops,
  1950. .spd_release = sock_spd_release,
  1951. };
  1952. int ret = 0;
  1953. __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
  1954. if (spd.nr_pages)
  1955. ret = splice_to_pipe(pipe, &spd);
  1956. return ret;
  1957. }
  1958. EXPORT_SYMBOL_GPL(skb_splice_bits);
  1959. /* Send skb data on a socket. Socket must be locked. */
  1960. int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
  1961. int len)
  1962. {
  1963. unsigned int orig_len = len;
  1964. struct sk_buff *head = skb;
  1965. unsigned short fragidx;
  1966. int slen, ret;
  1967. do_frag_list:
  1968. /* Deal with head data */
  1969. while (offset < skb_headlen(skb) && len) {
  1970. struct kvec kv;
  1971. struct msghdr msg;
  1972. slen = min_t(int, len, skb_headlen(skb) - offset);
  1973. kv.iov_base = skb->data + offset;
  1974. kv.iov_len = slen;
  1975. memset(&msg, 0, sizeof(msg));
  1976. ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen);
  1977. if (ret <= 0)
  1978. goto error;
  1979. offset += ret;
  1980. len -= ret;
  1981. }
  1982. /* All the data was skb head? */
  1983. if (!len)
  1984. goto out;
  1985. /* Make offset relative to start of frags */
  1986. offset -= skb_headlen(skb);
  1987. /* Find where we are in frag list */
  1988. for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  1989. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  1990. if (offset < frag->size)
  1991. break;
  1992. offset -= frag->size;
  1993. }
  1994. for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
  1995. skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx];
  1996. slen = min_t(size_t, len, frag->size - offset);
  1997. while (slen) {
  1998. ret = kernel_sendpage_locked(sk, frag->page.p,
  1999. frag->page_offset + offset,
  2000. slen, MSG_DONTWAIT);
  2001. if (ret <= 0)
  2002. goto error;
  2003. len -= ret;
  2004. offset += ret;
  2005. slen -= ret;
  2006. }
  2007. offset = 0;
  2008. }
  2009. if (len) {
  2010. /* Process any frag lists */
  2011. if (skb == head) {
  2012. if (skb_has_frag_list(skb)) {
  2013. skb = skb_shinfo(skb)->frag_list;
  2014. goto do_frag_list;
  2015. }
  2016. } else if (skb->next) {
  2017. skb = skb->next;
  2018. goto do_frag_list;
  2019. }
  2020. }
  2021. out:
  2022. return orig_len - len;
  2023. error:
  2024. return orig_len == len ? ret : orig_len - len;
  2025. }
  2026. EXPORT_SYMBOL_GPL(skb_send_sock_locked);
  2027. /* Send skb data on a socket. */
  2028. int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
  2029. {
  2030. int ret = 0;
  2031. lock_sock(sk);
  2032. ret = skb_send_sock_locked(sk, skb, offset, len);
  2033. release_sock(sk);
  2034. return ret;
  2035. }
  2036. EXPORT_SYMBOL_GPL(skb_send_sock);
  2037. /**
  2038. * skb_store_bits - store bits from kernel buffer to skb
  2039. * @skb: destination buffer
  2040. * @offset: offset in destination
  2041. * @from: source buffer
  2042. * @len: number of bytes to copy
  2043. *
  2044. * Copy the specified number of bytes from the source buffer to the
  2045. * destination skb. This function handles all the messy bits of
  2046. * traversing fragment lists and such.
  2047. */
  2048. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
  2049. {
  2050. int start = skb_headlen(skb);
  2051. struct sk_buff *frag_iter;
  2052. int i, copy;
  2053. if (offset > (int)skb->len - len)
  2054. goto fault;
  2055. if ((copy = start - offset) > 0) {
  2056. if (copy > len)
  2057. copy = len;
  2058. skb_copy_to_linear_data_offset(skb, offset, from, copy);
  2059. if ((len -= copy) == 0)
  2060. return 0;
  2061. offset += copy;
  2062. from += copy;
  2063. }
  2064. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2065. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2066. int end;
  2067. WARN_ON(start > offset + len);
  2068. end = start + skb_frag_size(frag);
  2069. if ((copy = end - offset) > 0) {
  2070. u32 p_off, p_len, copied;
  2071. struct page *p;
  2072. u8 *vaddr;
  2073. if (copy > len)
  2074. copy = len;
  2075. skb_frag_foreach_page(frag,
  2076. frag->page_offset + offset - start,
  2077. copy, p, p_off, p_len, copied) {
  2078. vaddr = kmap_atomic(p);
  2079. memcpy(vaddr + p_off, from + copied, p_len);
  2080. kunmap_atomic(vaddr);
  2081. }
  2082. if ((len -= copy) == 0)
  2083. return 0;
  2084. offset += copy;
  2085. from += copy;
  2086. }
  2087. start = end;
  2088. }
  2089. skb_walk_frags(skb, frag_iter) {
  2090. int end;
  2091. WARN_ON(start > offset + len);
  2092. end = start + frag_iter->len;
  2093. if ((copy = end - offset) > 0) {
  2094. if (copy > len)
  2095. copy = len;
  2096. if (skb_store_bits(frag_iter, offset - start,
  2097. from, copy))
  2098. goto fault;
  2099. if ((len -= copy) == 0)
  2100. return 0;
  2101. offset += copy;
  2102. from += copy;
  2103. }
  2104. start = end;
  2105. }
  2106. if (!len)
  2107. return 0;
  2108. fault:
  2109. return -EFAULT;
  2110. }
  2111. EXPORT_SYMBOL(skb_store_bits);
  2112. /* Checksum skb data. */
  2113. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  2114. __wsum csum, const struct skb_checksum_ops *ops)
  2115. {
  2116. int start = skb_headlen(skb);
  2117. int i, copy = start - offset;
  2118. struct sk_buff *frag_iter;
  2119. int pos = 0;
  2120. /* Checksum header. */
  2121. if (copy > 0) {
  2122. if (copy > len)
  2123. copy = len;
  2124. csum = ops->update(skb->data + offset, copy, csum);
  2125. if ((len -= copy) == 0)
  2126. return csum;
  2127. offset += copy;
  2128. pos = copy;
  2129. }
  2130. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2131. int end;
  2132. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2133. WARN_ON(start > offset + len);
  2134. end = start + skb_frag_size(frag);
  2135. if ((copy = end - offset) > 0) {
  2136. u32 p_off, p_len, copied;
  2137. struct page *p;
  2138. __wsum csum2;
  2139. u8 *vaddr;
  2140. if (copy > len)
  2141. copy = len;
  2142. skb_frag_foreach_page(frag,
  2143. frag->page_offset + offset - start,
  2144. copy, p, p_off, p_len, copied) {
  2145. vaddr = kmap_atomic(p);
  2146. csum2 = ops->update(vaddr + p_off, p_len, 0);
  2147. kunmap_atomic(vaddr);
  2148. csum = ops->combine(csum, csum2, pos, p_len);
  2149. pos += p_len;
  2150. }
  2151. if (!(len -= copy))
  2152. return csum;
  2153. offset += copy;
  2154. }
  2155. start = end;
  2156. }
  2157. skb_walk_frags(skb, frag_iter) {
  2158. int end;
  2159. WARN_ON(start > offset + len);
  2160. end = start + frag_iter->len;
  2161. if ((copy = end - offset) > 0) {
  2162. __wsum csum2;
  2163. if (copy > len)
  2164. copy = len;
  2165. csum2 = __skb_checksum(frag_iter, offset - start,
  2166. copy, 0, ops);
  2167. csum = ops->combine(csum, csum2, pos, copy);
  2168. if ((len -= copy) == 0)
  2169. return csum;
  2170. offset += copy;
  2171. pos += copy;
  2172. }
  2173. start = end;
  2174. }
  2175. BUG_ON(len);
  2176. return csum;
  2177. }
  2178. EXPORT_SYMBOL(__skb_checksum);
  2179. __wsum skb_checksum(const struct sk_buff *skb, int offset,
  2180. int len, __wsum csum)
  2181. {
  2182. const struct skb_checksum_ops ops = {
  2183. .update = csum_partial_ext,
  2184. .combine = csum_block_add_ext,
  2185. };
  2186. return __skb_checksum(skb, offset, len, csum, &ops);
  2187. }
  2188. EXPORT_SYMBOL(skb_checksum);
  2189. /* Both of above in one bottle. */
  2190. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
  2191. u8 *to, int len, __wsum csum)
  2192. {
  2193. int start = skb_headlen(skb);
  2194. int i, copy = start - offset;
  2195. struct sk_buff *frag_iter;
  2196. int pos = 0;
  2197. /* Copy header. */
  2198. if (copy > 0) {
  2199. if (copy > len)
  2200. copy = len;
  2201. csum = csum_partial_copy_nocheck(skb->data + offset, to,
  2202. copy, csum);
  2203. if ((len -= copy) == 0)
  2204. return csum;
  2205. offset += copy;
  2206. to += copy;
  2207. pos = copy;
  2208. }
  2209. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2210. int end;
  2211. WARN_ON(start > offset + len);
  2212. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2213. if ((copy = end - offset) > 0) {
  2214. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2215. u32 p_off, p_len, copied;
  2216. struct page *p;
  2217. __wsum csum2;
  2218. u8 *vaddr;
  2219. if (copy > len)
  2220. copy = len;
  2221. skb_frag_foreach_page(frag,
  2222. frag->page_offset + offset - start,
  2223. copy, p, p_off, p_len, copied) {
  2224. vaddr = kmap_atomic(p);
  2225. csum2 = csum_partial_copy_nocheck(vaddr + p_off,
  2226. to + copied,
  2227. p_len, 0);
  2228. kunmap_atomic(vaddr);
  2229. csum = csum_block_add(csum, csum2, pos);
  2230. pos += p_len;
  2231. }
  2232. if (!(len -= copy))
  2233. return csum;
  2234. offset += copy;
  2235. to += copy;
  2236. }
  2237. start = end;
  2238. }
  2239. skb_walk_frags(skb, frag_iter) {
  2240. __wsum csum2;
  2241. int end;
  2242. WARN_ON(start > offset + len);
  2243. end = start + frag_iter->len;
  2244. if ((copy = end - offset) > 0) {
  2245. if (copy > len)
  2246. copy = len;
  2247. csum2 = skb_copy_and_csum_bits(frag_iter,
  2248. offset - start,
  2249. to, copy, 0);
  2250. csum = csum_block_add(csum, csum2, pos);
  2251. if ((len -= copy) == 0)
  2252. return csum;
  2253. offset += copy;
  2254. to += copy;
  2255. pos += copy;
  2256. }
  2257. start = end;
  2258. }
  2259. BUG_ON(len);
  2260. return csum;
  2261. }
  2262. EXPORT_SYMBOL(skb_copy_and_csum_bits);
  2263. static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
  2264. {
  2265. net_warn_ratelimited(
  2266. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  2267. __func__);
  2268. return 0;
  2269. }
  2270. static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
  2271. int offset, int len)
  2272. {
  2273. net_warn_ratelimited(
  2274. "%s: attempt to compute crc32c without libcrc32c.ko\n",
  2275. __func__);
  2276. return 0;
  2277. }
  2278. static const struct skb_checksum_ops default_crc32c_ops = {
  2279. .update = warn_crc32c_csum_update,
  2280. .combine = warn_crc32c_csum_combine,
  2281. };
  2282. const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
  2283. &default_crc32c_ops;
  2284. EXPORT_SYMBOL(crc32c_csum_stub);
  2285. /**
  2286. * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
  2287. * @from: source buffer
  2288. *
  2289. * Calculates the amount of linear headroom needed in the 'to' skb passed
  2290. * into skb_zerocopy().
  2291. */
  2292. unsigned int
  2293. skb_zerocopy_headlen(const struct sk_buff *from)
  2294. {
  2295. unsigned int hlen = 0;
  2296. if (!from->head_frag ||
  2297. skb_headlen(from) < L1_CACHE_BYTES ||
  2298. skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
  2299. hlen = skb_headlen(from);
  2300. if (skb_has_frag_list(from))
  2301. hlen = from->len;
  2302. return hlen;
  2303. }
  2304. EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
  2305. /**
  2306. * skb_zerocopy - Zero copy skb to skb
  2307. * @to: destination buffer
  2308. * @from: source buffer
  2309. * @len: number of bytes to copy from source buffer
  2310. * @hlen: size of linear headroom in destination buffer
  2311. *
  2312. * Copies up to `len` bytes from `from` to `to` by creating references
  2313. * to the frags in the source buffer.
  2314. *
  2315. * The `hlen` as calculated by skb_zerocopy_headlen() specifies the
  2316. * headroom in the `to` buffer.
  2317. *
  2318. * Return value:
  2319. * 0: everything is OK
  2320. * -ENOMEM: couldn't orphan frags of @from due to lack of memory
  2321. * -EFAULT: skb_copy_bits() found some problem with skb geometry
  2322. */
  2323. int
  2324. skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
  2325. {
  2326. int i, j = 0;
  2327. int plen = 0; /* length of skb->head fragment */
  2328. int ret;
  2329. struct page *page;
  2330. unsigned int offset;
  2331. BUG_ON(!from->head_frag && !hlen);
  2332. /* dont bother with small payloads */
  2333. if (len <= skb_tailroom(to))
  2334. return skb_copy_bits(from, 0, skb_put(to, len), len);
  2335. if (hlen) {
  2336. ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
  2337. if (unlikely(ret))
  2338. return ret;
  2339. len -= hlen;
  2340. } else {
  2341. plen = min_t(int, skb_headlen(from), len);
  2342. if (plen) {
  2343. page = virt_to_head_page(from->head);
  2344. offset = from->data - (unsigned char *)page_address(page);
  2345. __skb_fill_page_desc(to, 0, page, offset, plen);
  2346. get_page(page);
  2347. j = 1;
  2348. len -= plen;
  2349. }
  2350. }
  2351. to->truesize += len + plen;
  2352. to->len += len + plen;
  2353. to->data_len += len + plen;
  2354. if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
  2355. skb_tx_error(from);
  2356. return -ENOMEM;
  2357. }
  2358. skb_zerocopy_clone(to, from, GFP_ATOMIC);
  2359. for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
  2360. if (!len)
  2361. break;
  2362. skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
  2363. skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
  2364. len -= skb_shinfo(to)->frags[j].size;
  2365. skb_frag_ref(to, j);
  2366. j++;
  2367. }
  2368. skb_shinfo(to)->nr_frags = j;
  2369. return 0;
  2370. }
  2371. EXPORT_SYMBOL_GPL(skb_zerocopy);
  2372. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
  2373. {
  2374. __wsum csum;
  2375. long csstart;
  2376. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2377. csstart = skb_checksum_start_offset(skb);
  2378. else
  2379. csstart = skb_headlen(skb);
  2380. BUG_ON(csstart > skb_headlen(skb));
  2381. skb_copy_from_linear_data(skb, to, csstart);
  2382. csum = 0;
  2383. if (csstart != skb->len)
  2384. csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
  2385. skb->len - csstart, 0);
  2386. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2387. long csstuff = csstart + skb->csum_offset;
  2388. *((__sum16 *)(to + csstuff)) = csum_fold(csum);
  2389. }
  2390. }
  2391. EXPORT_SYMBOL(skb_copy_and_csum_dev);
  2392. /**
  2393. * skb_dequeue - remove from the head of the queue
  2394. * @list: list to dequeue from
  2395. *
  2396. * Remove the head of the list. The list lock is taken so the function
  2397. * may be used safely with other locking list functions. The head item is
  2398. * returned or %NULL if the list is empty.
  2399. */
  2400. struct sk_buff *skb_dequeue(struct sk_buff_head *list)
  2401. {
  2402. unsigned long flags;
  2403. struct sk_buff *result;
  2404. spin_lock_irqsave(&list->lock, flags);
  2405. result = __skb_dequeue(list);
  2406. spin_unlock_irqrestore(&list->lock, flags);
  2407. return result;
  2408. }
  2409. EXPORT_SYMBOL(skb_dequeue);
  2410. /**
  2411. * skb_dequeue_tail - remove from the tail of the queue
  2412. * @list: list to dequeue from
  2413. *
  2414. * Remove the tail of the list. The list lock is taken so the function
  2415. * may be used safely with other locking list functions. The tail item is
  2416. * returned or %NULL if the list is empty.
  2417. */
  2418. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
  2419. {
  2420. unsigned long flags;
  2421. struct sk_buff *result;
  2422. spin_lock_irqsave(&list->lock, flags);
  2423. result = __skb_dequeue_tail(list);
  2424. spin_unlock_irqrestore(&list->lock, flags);
  2425. return result;
  2426. }
  2427. EXPORT_SYMBOL(skb_dequeue_tail);
  2428. /**
  2429. * skb_queue_purge - empty a list
  2430. * @list: list to empty
  2431. *
  2432. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  2433. * the list and one reference dropped. This function takes the list
  2434. * lock and is atomic with respect to other list locking functions.
  2435. */
  2436. void skb_queue_purge(struct sk_buff_head *list)
  2437. {
  2438. struct sk_buff *skb;
  2439. while ((skb = skb_dequeue(list)) != NULL)
  2440. kfree_skb(skb);
  2441. }
  2442. EXPORT_SYMBOL(skb_queue_purge);
  2443. /**
  2444. * skb_rbtree_purge - empty a skb rbtree
  2445. * @root: root of the rbtree to empty
  2446. * Return value: the sum of truesizes of all purged skbs.
  2447. *
  2448. * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
  2449. * the list and one reference dropped. This function does not take
  2450. * any lock. Synchronization should be handled by the caller (e.g., TCP
  2451. * out-of-order queue is protected by the socket lock).
  2452. */
  2453. unsigned int skb_rbtree_purge(struct rb_root *root)
  2454. {
  2455. struct rb_node *p = rb_first(root);
  2456. unsigned int sum = 0;
  2457. while (p) {
  2458. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  2459. p = rb_next(p);
  2460. rb_erase(&skb->rbnode, root);
  2461. sum += skb->truesize;
  2462. kfree_skb(skb);
  2463. }
  2464. return sum;
  2465. }
  2466. /**
  2467. * skb_queue_head - queue a buffer at the list head
  2468. * @list: list to use
  2469. * @newsk: buffer to queue
  2470. *
  2471. * Queue a buffer at the start of the list. This function takes the
  2472. * list lock and can be used safely with other locking &sk_buff functions
  2473. * safely.
  2474. *
  2475. * A buffer cannot be placed on two lists at the same time.
  2476. */
  2477. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
  2478. {
  2479. unsigned long flags;
  2480. spin_lock_irqsave(&list->lock, flags);
  2481. __skb_queue_head(list, newsk);
  2482. spin_unlock_irqrestore(&list->lock, flags);
  2483. }
  2484. EXPORT_SYMBOL(skb_queue_head);
  2485. /**
  2486. * skb_queue_tail - queue a buffer at the list tail
  2487. * @list: list to use
  2488. * @newsk: buffer to queue
  2489. *
  2490. * Queue a buffer at the tail of the list. This function takes the
  2491. * list lock and can be used safely with other locking &sk_buff functions
  2492. * safely.
  2493. *
  2494. * A buffer cannot be placed on two lists at the same time.
  2495. */
  2496. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
  2497. {
  2498. unsigned long flags;
  2499. spin_lock_irqsave(&list->lock, flags);
  2500. __skb_queue_tail(list, newsk);
  2501. spin_unlock_irqrestore(&list->lock, flags);
  2502. }
  2503. EXPORT_SYMBOL(skb_queue_tail);
  2504. /**
  2505. * skb_unlink - remove a buffer from a list
  2506. * @skb: buffer to remove
  2507. * @list: list to use
  2508. *
  2509. * Remove a packet from a list. The list locks are taken and this
  2510. * function is atomic with respect to other list locked calls
  2511. *
  2512. * You must know what list the SKB is on.
  2513. */
  2514. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  2515. {
  2516. unsigned long flags;
  2517. spin_lock_irqsave(&list->lock, flags);
  2518. __skb_unlink(skb, list);
  2519. spin_unlock_irqrestore(&list->lock, flags);
  2520. }
  2521. EXPORT_SYMBOL(skb_unlink);
  2522. /**
  2523. * skb_append - append a buffer
  2524. * @old: buffer to insert after
  2525. * @newsk: buffer to insert
  2526. * @list: list to use
  2527. *
  2528. * Place a packet after a given packet in a list. The list locks are taken
  2529. * and this function is atomic with respect to other list locked calls.
  2530. * A buffer cannot be placed on two lists at the same time.
  2531. */
  2532. void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2533. {
  2534. unsigned long flags;
  2535. spin_lock_irqsave(&list->lock, flags);
  2536. __skb_queue_after(list, old, newsk);
  2537. spin_unlock_irqrestore(&list->lock, flags);
  2538. }
  2539. EXPORT_SYMBOL(skb_append);
  2540. /**
  2541. * skb_insert - insert a buffer
  2542. * @old: buffer to insert before
  2543. * @newsk: buffer to insert
  2544. * @list: list to use
  2545. *
  2546. * Place a packet before a given packet in a list. The list locks are
  2547. * taken and this function is atomic with respect to other list locked
  2548. * calls.
  2549. *
  2550. * A buffer cannot be placed on two lists at the same time.
  2551. */
  2552. void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  2553. {
  2554. unsigned long flags;
  2555. spin_lock_irqsave(&list->lock, flags);
  2556. __skb_insert(newsk, old->prev, old, list);
  2557. spin_unlock_irqrestore(&list->lock, flags);
  2558. }
  2559. EXPORT_SYMBOL(skb_insert);
  2560. static inline void skb_split_inside_header(struct sk_buff *skb,
  2561. struct sk_buff* skb1,
  2562. const u32 len, const int pos)
  2563. {
  2564. int i;
  2565. skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
  2566. pos - len);
  2567. /* And move data appendix as is. */
  2568. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  2569. skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
  2570. skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
  2571. skb_shinfo(skb)->nr_frags = 0;
  2572. skb1->data_len = skb->data_len;
  2573. skb1->len += skb1->data_len;
  2574. skb->data_len = 0;
  2575. skb->len = len;
  2576. skb_set_tail_pointer(skb, len);
  2577. }
  2578. static inline void skb_split_no_header(struct sk_buff *skb,
  2579. struct sk_buff* skb1,
  2580. const u32 len, int pos)
  2581. {
  2582. int i, k = 0;
  2583. const int nfrags = skb_shinfo(skb)->nr_frags;
  2584. skb_shinfo(skb)->nr_frags = 0;
  2585. skb1->len = skb1->data_len = skb->len - len;
  2586. skb->len = len;
  2587. skb->data_len = len - pos;
  2588. for (i = 0; i < nfrags; i++) {
  2589. int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  2590. if (pos + size > len) {
  2591. skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
  2592. if (pos < len) {
  2593. /* Split frag.
  2594. * We have two variants in this case:
  2595. * 1. Move all the frag to the second
  2596. * part, if it is possible. F.e.
  2597. * this approach is mandatory for TUX,
  2598. * where splitting is expensive.
  2599. * 2. Split is accurately. We make this.
  2600. */
  2601. skb_frag_ref(skb, i);
  2602. skb_shinfo(skb1)->frags[0].page_offset += len - pos;
  2603. skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
  2604. skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
  2605. skb_shinfo(skb)->nr_frags++;
  2606. }
  2607. k++;
  2608. } else
  2609. skb_shinfo(skb)->nr_frags++;
  2610. pos += size;
  2611. }
  2612. skb_shinfo(skb1)->nr_frags = k;
  2613. }
  2614. /**
  2615. * skb_split - Split fragmented skb to two parts at length len.
  2616. * @skb: the buffer to split
  2617. * @skb1: the buffer to receive the second part
  2618. * @len: new length for skb
  2619. */
  2620. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
  2621. {
  2622. int pos = skb_headlen(skb);
  2623. skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags &
  2624. SKBTX_SHARED_FRAG;
  2625. skb_zerocopy_clone(skb1, skb, 0);
  2626. if (len < pos) /* Split line is inside header. */
  2627. skb_split_inside_header(skb, skb1, len, pos);
  2628. else /* Second chunk has no header, nothing to copy. */
  2629. skb_split_no_header(skb, skb1, len, pos);
  2630. }
  2631. EXPORT_SYMBOL(skb_split);
  2632. /* Shifting from/to a cloned skb is a no-go.
  2633. *
  2634. * Caller cannot keep skb_shinfo related pointers past calling here!
  2635. */
  2636. static int skb_prepare_for_shift(struct sk_buff *skb)
  2637. {
  2638. return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2639. }
  2640. /**
  2641. * skb_shift - Shifts paged data partially from skb to another
  2642. * @tgt: buffer into which tail data gets added
  2643. * @skb: buffer from which the paged data comes from
  2644. * @shiftlen: shift up to this many bytes
  2645. *
  2646. * Attempts to shift up to shiftlen worth of bytes, which may be less than
  2647. * the length of the skb, from skb to tgt. Returns number bytes shifted.
  2648. * It's up to caller to free skb if everything was shifted.
  2649. *
  2650. * If @tgt runs out of frags, the whole operation is aborted.
  2651. *
  2652. * Skb cannot include anything else but paged data while tgt is allowed
  2653. * to have non-paged data as well.
  2654. *
  2655. * TODO: full sized shift could be optimized but that would need
  2656. * specialized skb free'er to handle frags without up-to-date nr_frags.
  2657. */
  2658. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
  2659. {
  2660. int from, to, merge, todo;
  2661. struct skb_frag_struct *fragfrom, *fragto;
  2662. BUG_ON(shiftlen > skb->len);
  2663. if (skb_headlen(skb))
  2664. return 0;
  2665. if (skb_zcopy(tgt) || skb_zcopy(skb))
  2666. return 0;
  2667. todo = shiftlen;
  2668. from = 0;
  2669. to = skb_shinfo(tgt)->nr_frags;
  2670. fragfrom = &skb_shinfo(skb)->frags[from];
  2671. /* Actual merge is delayed until the point when we know we can
  2672. * commit all, so that we don't have to undo partial changes
  2673. */
  2674. if (!to ||
  2675. !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
  2676. fragfrom->page_offset)) {
  2677. merge = -1;
  2678. } else {
  2679. merge = to - 1;
  2680. todo -= skb_frag_size(fragfrom);
  2681. if (todo < 0) {
  2682. if (skb_prepare_for_shift(skb) ||
  2683. skb_prepare_for_shift(tgt))
  2684. return 0;
  2685. /* All previous frag pointers might be stale! */
  2686. fragfrom = &skb_shinfo(skb)->frags[from];
  2687. fragto = &skb_shinfo(tgt)->frags[merge];
  2688. skb_frag_size_add(fragto, shiftlen);
  2689. skb_frag_size_sub(fragfrom, shiftlen);
  2690. fragfrom->page_offset += shiftlen;
  2691. goto onlymerged;
  2692. }
  2693. from++;
  2694. }
  2695. /* Skip full, not-fitting skb to avoid expensive operations */
  2696. if ((shiftlen == skb->len) &&
  2697. (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
  2698. return 0;
  2699. if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
  2700. return 0;
  2701. while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
  2702. if (to == MAX_SKB_FRAGS)
  2703. return 0;
  2704. fragfrom = &skb_shinfo(skb)->frags[from];
  2705. fragto = &skb_shinfo(tgt)->frags[to];
  2706. if (todo >= skb_frag_size(fragfrom)) {
  2707. *fragto = *fragfrom;
  2708. todo -= skb_frag_size(fragfrom);
  2709. from++;
  2710. to++;
  2711. } else {
  2712. __skb_frag_ref(fragfrom);
  2713. fragto->page = fragfrom->page;
  2714. fragto->page_offset = fragfrom->page_offset;
  2715. skb_frag_size_set(fragto, todo);
  2716. fragfrom->page_offset += todo;
  2717. skb_frag_size_sub(fragfrom, todo);
  2718. todo = 0;
  2719. to++;
  2720. break;
  2721. }
  2722. }
  2723. /* Ready to "commit" this state change to tgt */
  2724. skb_shinfo(tgt)->nr_frags = to;
  2725. if (merge >= 0) {
  2726. fragfrom = &skb_shinfo(skb)->frags[0];
  2727. fragto = &skb_shinfo(tgt)->frags[merge];
  2728. skb_frag_size_add(fragto, skb_frag_size(fragfrom));
  2729. __skb_frag_unref(fragfrom);
  2730. }
  2731. /* Reposition in the original skb */
  2732. to = 0;
  2733. while (from < skb_shinfo(skb)->nr_frags)
  2734. skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
  2735. skb_shinfo(skb)->nr_frags = to;
  2736. BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
  2737. onlymerged:
  2738. /* Most likely the tgt won't ever need its checksum anymore, skb on
  2739. * the other hand might need it if it needs to be resent
  2740. */
  2741. tgt->ip_summed = CHECKSUM_PARTIAL;
  2742. skb->ip_summed = CHECKSUM_PARTIAL;
  2743. /* Yak, is it really working this way? Some helper please? */
  2744. skb->len -= shiftlen;
  2745. skb->data_len -= shiftlen;
  2746. skb->truesize -= shiftlen;
  2747. tgt->len += shiftlen;
  2748. tgt->data_len += shiftlen;
  2749. tgt->truesize += shiftlen;
  2750. return shiftlen;
  2751. }
  2752. /**
  2753. * skb_prepare_seq_read - Prepare a sequential read of skb data
  2754. * @skb: the buffer to read
  2755. * @from: lower offset of data to be read
  2756. * @to: upper offset of data to be read
  2757. * @st: state variable
  2758. *
  2759. * Initializes the specified state variable. Must be called before
  2760. * invoking skb_seq_read() for the first time.
  2761. */
  2762. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  2763. unsigned int to, struct skb_seq_state *st)
  2764. {
  2765. st->lower_offset = from;
  2766. st->upper_offset = to;
  2767. st->root_skb = st->cur_skb = skb;
  2768. st->frag_idx = st->stepped_offset = 0;
  2769. st->frag_data = NULL;
  2770. }
  2771. EXPORT_SYMBOL(skb_prepare_seq_read);
  2772. /**
  2773. * skb_seq_read - Sequentially read skb data
  2774. * @consumed: number of bytes consumed by the caller so far
  2775. * @data: destination pointer for data to be returned
  2776. * @st: state variable
  2777. *
  2778. * Reads a block of skb data at @consumed relative to the
  2779. * lower offset specified to skb_prepare_seq_read(). Assigns
  2780. * the head of the data block to @data and returns the length
  2781. * of the block or 0 if the end of the skb data or the upper
  2782. * offset has been reached.
  2783. *
  2784. * The caller is not required to consume all of the data
  2785. * returned, i.e. @consumed is typically set to the number
  2786. * of bytes already consumed and the next call to
  2787. * skb_seq_read() will return the remaining part of the block.
  2788. *
  2789. * Note 1: The size of each block of data returned can be arbitrary,
  2790. * this limitation is the cost for zerocopy sequential
  2791. * reads of potentially non linear data.
  2792. *
  2793. * Note 2: Fragment lists within fragments are not implemented
  2794. * at the moment, state->root_skb could be replaced with
  2795. * a stack for this purpose.
  2796. */
  2797. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  2798. struct skb_seq_state *st)
  2799. {
  2800. unsigned int block_limit, abs_offset = consumed + st->lower_offset;
  2801. skb_frag_t *frag;
  2802. if (unlikely(abs_offset >= st->upper_offset)) {
  2803. if (st->frag_data) {
  2804. kunmap_atomic(st->frag_data);
  2805. st->frag_data = NULL;
  2806. }
  2807. return 0;
  2808. }
  2809. next_skb:
  2810. block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
  2811. if (abs_offset < block_limit && !st->frag_data) {
  2812. *data = st->cur_skb->data + (abs_offset - st->stepped_offset);
  2813. return block_limit - abs_offset;
  2814. }
  2815. if (st->frag_idx == 0 && !st->frag_data)
  2816. st->stepped_offset += skb_headlen(st->cur_skb);
  2817. while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
  2818. frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
  2819. block_limit = skb_frag_size(frag) + st->stepped_offset;
  2820. if (abs_offset < block_limit) {
  2821. if (!st->frag_data)
  2822. st->frag_data = kmap_atomic(skb_frag_page(frag));
  2823. *data = (u8 *) st->frag_data + frag->page_offset +
  2824. (abs_offset - st->stepped_offset);
  2825. return block_limit - abs_offset;
  2826. }
  2827. if (st->frag_data) {
  2828. kunmap_atomic(st->frag_data);
  2829. st->frag_data = NULL;
  2830. }
  2831. st->frag_idx++;
  2832. st->stepped_offset += skb_frag_size(frag);
  2833. }
  2834. if (st->frag_data) {
  2835. kunmap_atomic(st->frag_data);
  2836. st->frag_data = NULL;
  2837. }
  2838. if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
  2839. st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
  2840. st->frag_idx = 0;
  2841. goto next_skb;
  2842. } else if (st->cur_skb->next) {
  2843. st->cur_skb = st->cur_skb->next;
  2844. st->frag_idx = 0;
  2845. goto next_skb;
  2846. }
  2847. return 0;
  2848. }
  2849. EXPORT_SYMBOL(skb_seq_read);
  2850. /**
  2851. * skb_abort_seq_read - Abort a sequential read of skb data
  2852. * @st: state variable
  2853. *
  2854. * Must be called if skb_seq_read() was not called until it
  2855. * returned 0.
  2856. */
  2857. void skb_abort_seq_read(struct skb_seq_state *st)
  2858. {
  2859. if (st->frag_data)
  2860. kunmap_atomic(st->frag_data);
  2861. }
  2862. EXPORT_SYMBOL(skb_abort_seq_read);
  2863. #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb))
  2864. static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
  2865. struct ts_config *conf,
  2866. struct ts_state *state)
  2867. {
  2868. return skb_seq_read(offset, text, TS_SKB_CB(state));
  2869. }
  2870. static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
  2871. {
  2872. skb_abort_seq_read(TS_SKB_CB(state));
  2873. }
  2874. /**
  2875. * skb_find_text - Find a text pattern in skb data
  2876. * @skb: the buffer to look in
  2877. * @from: search offset
  2878. * @to: search limit
  2879. * @config: textsearch configuration
  2880. *
  2881. * Finds a pattern in the skb data according to the specified
  2882. * textsearch configuration. Use textsearch_next() to retrieve
  2883. * subsequent occurrences of the pattern. Returns the offset
  2884. * to the first occurrence or UINT_MAX if no match was found.
  2885. */
  2886. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  2887. unsigned int to, struct ts_config *config)
  2888. {
  2889. struct ts_state state;
  2890. unsigned int ret;
  2891. config->get_next_block = skb_ts_get_next_block;
  2892. config->finish = skb_ts_finish;
  2893. skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
  2894. ret = textsearch_find(config, &state);
  2895. return (ret <= to - from ? ret : UINT_MAX);
  2896. }
  2897. EXPORT_SYMBOL(skb_find_text);
  2898. int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
  2899. int offset, size_t size)
  2900. {
  2901. int i = skb_shinfo(skb)->nr_frags;
  2902. if (skb_can_coalesce(skb, i, page, offset)) {
  2903. skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
  2904. } else if (i < MAX_SKB_FRAGS) {
  2905. get_page(page);
  2906. skb_fill_page_desc(skb, i, page, offset, size);
  2907. } else {
  2908. return -EMSGSIZE;
  2909. }
  2910. return 0;
  2911. }
  2912. EXPORT_SYMBOL_GPL(skb_append_pagefrags);
  2913. /**
  2914. * skb_pull_rcsum - pull skb and update receive checksum
  2915. * @skb: buffer to update
  2916. * @len: length of data pulled
  2917. *
  2918. * This function performs an skb_pull on the packet and updates
  2919. * the CHECKSUM_COMPLETE checksum. It should be used on
  2920. * receive path processing instead of skb_pull unless you know
  2921. * that the checksum difference is zero (e.g., a valid IP header)
  2922. * or you are setting ip_summed to CHECKSUM_NONE.
  2923. */
  2924. void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
  2925. {
  2926. unsigned char *data = skb->data;
  2927. BUG_ON(len > skb->len);
  2928. __skb_pull(skb, len);
  2929. skb_postpull_rcsum(skb, data, len);
  2930. return skb->data;
  2931. }
  2932. EXPORT_SYMBOL_GPL(skb_pull_rcsum);
  2933. static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
  2934. {
  2935. skb_frag_t head_frag;
  2936. struct page *page;
  2937. page = virt_to_head_page(frag_skb->head);
  2938. head_frag.page.p = page;
  2939. head_frag.page_offset = frag_skb->data -
  2940. (unsigned char *)page_address(page);
  2941. head_frag.size = skb_headlen(frag_skb);
  2942. return head_frag;
  2943. }
  2944. /**
  2945. * skb_segment - Perform protocol segmentation on skb.
  2946. * @head_skb: buffer to segment
  2947. * @features: features for the output path (see dev->features)
  2948. *
  2949. * This function performs segmentation on the given skb. It returns
  2950. * a pointer to the first in a list of new skbs for the segments.
  2951. * In case of error it returns ERR_PTR(err).
  2952. */
  2953. struct sk_buff *skb_segment(struct sk_buff *head_skb,
  2954. netdev_features_t features)
  2955. {
  2956. struct sk_buff *segs = NULL;
  2957. struct sk_buff *tail = NULL;
  2958. struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
  2959. skb_frag_t *frag = skb_shinfo(head_skb)->frags;
  2960. unsigned int mss = skb_shinfo(head_skb)->gso_size;
  2961. unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
  2962. struct sk_buff *frag_skb = head_skb;
  2963. unsigned int offset = doffset;
  2964. unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
  2965. unsigned int partial_segs = 0;
  2966. unsigned int headroom;
  2967. unsigned int len = head_skb->len;
  2968. __be16 proto;
  2969. bool csum, sg;
  2970. int nfrags = skb_shinfo(head_skb)->nr_frags;
  2971. int err = -ENOMEM;
  2972. int i = 0;
  2973. int pos;
  2974. int dummy;
  2975. __skb_push(head_skb, doffset);
  2976. proto = skb_network_protocol(head_skb, &dummy);
  2977. if (unlikely(!proto))
  2978. return ERR_PTR(-EINVAL);
  2979. sg = !!(features & NETIF_F_SG);
  2980. csum = !!can_checksum_protocol(features, proto);
  2981. if (sg && csum && (mss != GSO_BY_FRAGS)) {
  2982. if (!(features & NETIF_F_GSO_PARTIAL)) {
  2983. struct sk_buff *iter;
  2984. unsigned int frag_len;
  2985. if (!list_skb ||
  2986. !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
  2987. goto normal;
  2988. /* If we get here then all the required
  2989. * GSO features except frag_list are supported.
  2990. * Try to split the SKB to multiple GSO SKBs
  2991. * with no frag_list.
  2992. * Currently we can do that only when the buffers don't
  2993. * have a linear part and all the buffers except
  2994. * the last are of the same length.
  2995. */
  2996. frag_len = list_skb->len;
  2997. skb_walk_frags(head_skb, iter) {
  2998. if (frag_len != iter->len && iter->next)
  2999. goto normal;
  3000. if (skb_headlen(iter) && !iter->head_frag)
  3001. goto normal;
  3002. len -= iter->len;
  3003. }
  3004. if (len != frag_len)
  3005. goto normal;
  3006. }
  3007. /* GSO partial only requires that we trim off any excess that
  3008. * doesn't fit into an MSS sized block, so take care of that
  3009. * now.
  3010. */
  3011. partial_segs = len / mss;
  3012. if (partial_segs > 1)
  3013. mss *= partial_segs;
  3014. else
  3015. partial_segs = 0;
  3016. }
  3017. normal:
  3018. headroom = skb_headroom(head_skb);
  3019. pos = skb_headlen(head_skb);
  3020. do {
  3021. struct sk_buff *nskb;
  3022. skb_frag_t *nskb_frag;
  3023. int hsize;
  3024. int size;
  3025. if (unlikely(mss == GSO_BY_FRAGS)) {
  3026. len = list_skb->len;
  3027. } else {
  3028. len = head_skb->len - offset;
  3029. if (len > mss)
  3030. len = mss;
  3031. }
  3032. hsize = skb_headlen(head_skb) - offset;
  3033. if (hsize < 0)
  3034. hsize = 0;
  3035. if (hsize > len || !sg)
  3036. hsize = len;
  3037. if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
  3038. (skb_headlen(list_skb) == len || sg)) {
  3039. BUG_ON(skb_headlen(list_skb) > len);
  3040. i = 0;
  3041. nfrags = skb_shinfo(list_skb)->nr_frags;
  3042. frag = skb_shinfo(list_skb)->frags;
  3043. frag_skb = list_skb;
  3044. pos += skb_headlen(list_skb);
  3045. while (pos < offset + len) {
  3046. BUG_ON(i >= nfrags);
  3047. size = skb_frag_size(frag);
  3048. if (pos + size > offset + len)
  3049. break;
  3050. i++;
  3051. pos += size;
  3052. frag++;
  3053. }
  3054. nskb = skb_clone(list_skb, GFP_ATOMIC);
  3055. list_skb = list_skb->next;
  3056. if (unlikely(!nskb))
  3057. goto err;
  3058. if (unlikely(pskb_trim(nskb, len))) {
  3059. kfree_skb(nskb);
  3060. goto err;
  3061. }
  3062. hsize = skb_end_offset(nskb);
  3063. if (skb_cow_head(nskb, doffset + headroom)) {
  3064. kfree_skb(nskb);
  3065. goto err;
  3066. }
  3067. nskb->truesize += skb_end_offset(nskb) - hsize;
  3068. skb_release_head_state(nskb);
  3069. __skb_push(nskb, doffset);
  3070. } else {
  3071. nskb = __alloc_skb(hsize + doffset + headroom,
  3072. GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
  3073. NUMA_NO_NODE);
  3074. if (unlikely(!nskb))
  3075. goto err;
  3076. skb_reserve(nskb, headroom);
  3077. __skb_put(nskb, doffset);
  3078. }
  3079. if (segs)
  3080. tail->next = nskb;
  3081. else
  3082. segs = nskb;
  3083. tail = nskb;
  3084. __copy_skb_header(nskb, head_skb);
  3085. skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
  3086. skb_reset_mac_len(nskb);
  3087. skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
  3088. nskb->data - tnl_hlen,
  3089. doffset + tnl_hlen);
  3090. if (nskb->len == len + doffset)
  3091. goto perform_csum_check;
  3092. if (!sg) {
  3093. if (!nskb->remcsum_offload)
  3094. nskb->ip_summed = CHECKSUM_NONE;
  3095. SKB_GSO_CB(nskb)->csum =
  3096. skb_copy_and_csum_bits(head_skb, offset,
  3097. skb_put(nskb, len),
  3098. len, 0);
  3099. SKB_GSO_CB(nskb)->csum_start =
  3100. skb_headroom(nskb) + doffset;
  3101. continue;
  3102. }
  3103. nskb_frag = skb_shinfo(nskb)->frags;
  3104. skb_copy_from_linear_data_offset(head_skb, offset,
  3105. skb_put(nskb, hsize), hsize);
  3106. skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags &
  3107. SKBTX_SHARED_FRAG;
  3108. if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
  3109. skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
  3110. goto err;
  3111. while (pos < offset + len) {
  3112. if (i >= nfrags) {
  3113. i = 0;
  3114. nfrags = skb_shinfo(list_skb)->nr_frags;
  3115. frag = skb_shinfo(list_skb)->frags;
  3116. frag_skb = list_skb;
  3117. if (!skb_headlen(list_skb)) {
  3118. BUG_ON(!nfrags);
  3119. } else {
  3120. BUG_ON(!list_skb->head_frag);
  3121. /* to make room for head_frag. */
  3122. i--;
  3123. frag--;
  3124. }
  3125. if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
  3126. skb_zerocopy_clone(nskb, frag_skb,
  3127. GFP_ATOMIC))
  3128. goto err;
  3129. list_skb = list_skb->next;
  3130. }
  3131. if (unlikely(skb_shinfo(nskb)->nr_frags >=
  3132. MAX_SKB_FRAGS)) {
  3133. net_warn_ratelimited(
  3134. "skb_segment: too many frags: %u %u\n",
  3135. pos, mss);
  3136. err = -EINVAL;
  3137. goto err;
  3138. }
  3139. *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
  3140. __skb_frag_ref(nskb_frag);
  3141. size = skb_frag_size(nskb_frag);
  3142. if (pos < offset) {
  3143. nskb_frag->page_offset += offset - pos;
  3144. skb_frag_size_sub(nskb_frag, offset - pos);
  3145. }
  3146. skb_shinfo(nskb)->nr_frags++;
  3147. if (pos + size <= offset + len) {
  3148. i++;
  3149. frag++;
  3150. pos += size;
  3151. } else {
  3152. skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
  3153. goto skip_fraglist;
  3154. }
  3155. nskb_frag++;
  3156. }
  3157. skip_fraglist:
  3158. nskb->data_len = len - hsize;
  3159. nskb->len += nskb->data_len;
  3160. nskb->truesize += nskb->data_len;
  3161. perform_csum_check:
  3162. if (!csum) {
  3163. if (skb_has_shared_frag(nskb) &&
  3164. __skb_linearize(nskb))
  3165. goto err;
  3166. if (!nskb->remcsum_offload)
  3167. nskb->ip_summed = CHECKSUM_NONE;
  3168. SKB_GSO_CB(nskb)->csum =
  3169. skb_checksum(nskb, doffset,
  3170. nskb->len - doffset, 0);
  3171. SKB_GSO_CB(nskb)->csum_start =
  3172. skb_headroom(nskb) + doffset;
  3173. }
  3174. } while ((offset += len) < head_skb->len);
  3175. /* Some callers want to get the end of the list.
  3176. * Put it in segs->prev to avoid walking the list.
  3177. * (see validate_xmit_skb_list() for example)
  3178. */
  3179. segs->prev = tail;
  3180. if (partial_segs) {
  3181. struct sk_buff *iter;
  3182. int type = skb_shinfo(head_skb)->gso_type;
  3183. unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
  3184. /* Update type to add partial and then remove dodgy if set */
  3185. type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
  3186. type &= ~SKB_GSO_DODGY;
  3187. /* Update GSO info and prepare to start updating headers on
  3188. * our way back down the stack of protocols.
  3189. */
  3190. for (iter = segs; iter; iter = iter->next) {
  3191. skb_shinfo(iter)->gso_size = gso_size;
  3192. skb_shinfo(iter)->gso_segs = partial_segs;
  3193. skb_shinfo(iter)->gso_type = type;
  3194. SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
  3195. }
  3196. if (tail->len - doffset <= gso_size)
  3197. skb_shinfo(tail)->gso_size = 0;
  3198. else if (tail != segs)
  3199. skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
  3200. }
  3201. /* Following permits correct backpressure, for protocols
  3202. * using skb_set_owner_w().
  3203. * Idea is to tranfert ownership from head_skb to last segment.
  3204. */
  3205. if (head_skb->destructor == sock_wfree) {
  3206. swap(tail->truesize, head_skb->truesize);
  3207. swap(tail->destructor, head_skb->destructor);
  3208. swap(tail->sk, head_skb->sk);
  3209. }
  3210. return segs;
  3211. err:
  3212. kfree_skb_list(segs);
  3213. return ERR_PTR(err);
  3214. }
  3215. EXPORT_SYMBOL_GPL(skb_segment);
  3216. int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
  3217. {
  3218. struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
  3219. unsigned int offset = skb_gro_offset(skb);
  3220. unsigned int headlen = skb_headlen(skb);
  3221. unsigned int len = skb_gro_len(skb);
  3222. unsigned int delta_truesize;
  3223. struct sk_buff *lp;
  3224. if (unlikely(p->len + len >= 65536))
  3225. return -E2BIG;
  3226. lp = NAPI_GRO_CB(p)->last;
  3227. pinfo = skb_shinfo(lp);
  3228. if (headlen <= offset) {
  3229. skb_frag_t *frag;
  3230. skb_frag_t *frag2;
  3231. int i = skbinfo->nr_frags;
  3232. int nr_frags = pinfo->nr_frags + i;
  3233. if (nr_frags > MAX_SKB_FRAGS)
  3234. goto merge;
  3235. offset -= headlen;
  3236. pinfo->nr_frags = nr_frags;
  3237. skbinfo->nr_frags = 0;
  3238. frag = pinfo->frags + nr_frags;
  3239. frag2 = skbinfo->frags + i;
  3240. do {
  3241. *--frag = *--frag2;
  3242. } while (--i);
  3243. frag->page_offset += offset;
  3244. skb_frag_size_sub(frag, offset);
  3245. /* all fragments truesize : remove (head size + sk_buff) */
  3246. delta_truesize = skb->truesize -
  3247. SKB_TRUESIZE(skb_end_offset(skb));
  3248. skb->truesize -= skb->data_len;
  3249. skb->len -= skb->data_len;
  3250. skb->data_len = 0;
  3251. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
  3252. goto done;
  3253. } else if (skb->head_frag) {
  3254. int nr_frags = pinfo->nr_frags;
  3255. skb_frag_t *frag = pinfo->frags + nr_frags;
  3256. struct page *page = virt_to_head_page(skb->head);
  3257. unsigned int first_size = headlen - offset;
  3258. unsigned int first_offset;
  3259. if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
  3260. goto merge;
  3261. first_offset = skb->data -
  3262. (unsigned char *)page_address(page) +
  3263. offset;
  3264. pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
  3265. frag->page.p = page;
  3266. frag->page_offset = first_offset;
  3267. skb_frag_size_set(frag, first_size);
  3268. memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
  3269. /* We dont need to clear skbinfo->nr_frags here */
  3270. delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  3271. NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
  3272. goto done;
  3273. }
  3274. merge:
  3275. delta_truesize = skb->truesize;
  3276. if (offset > headlen) {
  3277. unsigned int eat = offset - headlen;
  3278. skbinfo->frags[0].page_offset += eat;
  3279. skb_frag_size_sub(&skbinfo->frags[0], eat);
  3280. skb->data_len -= eat;
  3281. skb->len -= eat;
  3282. offset = headlen;
  3283. }
  3284. __skb_pull(skb, offset);
  3285. if (NAPI_GRO_CB(p)->last == p)
  3286. skb_shinfo(p)->frag_list = skb;
  3287. else
  3288. NAPI_GRO_CB(p)->last->next = skb;
  3289. NAPI_GRO_CB(p)->last = skb;
  3290. __skb_header_release(skb);
  3291. lp = p;
  3292. done:
  3293. NAPI_GRO_CB(p)->count++;
  3294. p->data_len += len;
  3295. p->truesize += delta_truesize;
  3296. p->len += len;
  3297. if (lp != p) {
  3298. lp->data_len += len;
  3299. lp->truesize += delta_truesize;
  3300. lp->len += len;
  3301. }
  3302. NAPI_GRO_CB(skb)->same_flow = 1;
  3303. return 0;
  3304. }
  3305. EXPORT_SYMBOL_GPL(skb_gro_receive);
  3306. void __init skb_init(void)
  3307. {
  3308. skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
  3309. sizeof(struct sk_buff),
  3310. 0,
  3311. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  3312. offsetof(struct sk_buff, cb),
  3313. sizeof_field(struct sk_buff, cb),
  3314. NULL);
  3315. skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
  3316. sizeof(struct sk_buff_fclones),
  3317. 0,
  3318. SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  3319. NULL);
  3320. }
  3321. static int
  3322. __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
  3323. unsigned int recursion_level)
  3324. {
  3325. int start = skb_headlen(skb);
  3326. int i, copy = start - offset;
  3327. struct sk_buff *frag_iter;
  3328. int elt = 0;
  3329. if (unlikely(recursion_level >= 24))
  3330. return -EMSGSIZE;
  3331. if (copy > 0) {
  3332. if (copy > len)
  3333. copy = len;
  3334. sg_set_buf(sg, skb->data + offset, copy);
  3335. elt++;
  3336. if ((len -= copy) == 0)
  3337. return elt;
  3338. offset += copy;
  3339. }
  3340. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  3341. int end;
  3342. WARN_ON(start > offset + len);
  3343. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  3344. if ((copy = end - offset) > 0) {
  3345. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  3346. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  3347. return -EMSGSIZE;
  3348. if (copy > len)
  3349. copy = len;
  3350. sg_set_page(&sg[elt], skb_frag_page(frag), copy,
  3351. frag->page_offset+offset-start);
  3352. elt++;
  3353. if (!(len -= copy))
  3354. return elt;
  3355. offset += copy;
  3356. }
  3357. start = end;
  3358. }
  3359. skb_walk_frags(skb, frag_iter) {
  3360. int end, ret;
  3361. WARN_ON(start > offset + len);
  3362. end = start + frag_iter->len;
  3363. if ((copy = end - offset) > 0) {
  3364. if (unlikely(elt && sg_is_last(&sg[elt - 1])))
  3365. return -EMSGSIZE;
  3366. if (copy > len)
  3367. copy = len;
  3368. ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
  3369. copy, recursion_level + 1);
  3370. if (unlikely(ret < 0))
  3371. return ret;
  3372. elt += ret;
  3373. if ((len -= copy) == 0)
  3374. return elt;
  3375. offset += copy;
  3376. }
  3377. start = end;
  3378. }
  3379. BUG_ON(len);
  3380. return elt;
  3381. }
  3382. /**
  3383. * skb_to_sgvec - Fill a scatter-gather list from a socket buffer
  3384. * @skb: Socket buffer containing the buffers to be mapped
  3385. * @sg: The scatter-gather list to map into
  3386. * @offset: The offset into the buffer's contents to start mapping
  3387. * @len: Length of buffer space to be mapped
  3388. *
  3389. * Fill the specified scatter-gather list with mappings/pointers into a
  3390. * region of the buffer space attached to a socket buffer. Returns either
  3391. * the number of scatterlist items used, or -EMSGSIZE if the contents
  3392. * could not fit.
  3393. */
  3394. int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
  3395. {
  3396. int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
  3397. if (nsg <= 0)
  3398. return nsg;
  3399. sg_mark_end(&sg[nsg - 1]);
  3400. return nsg;
  3401. }
  3402. EXPORT_SYMBOL_GPL(skb_to_sgvec);
  3403. /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
  3404. * sglist without mark the sg which contain last skb data as the end.
  3405. * So the caller can mannipulate sg list as will when padding new data after
  3406. * the first call without calling sg_unmark_end to expend sg list.
  3407. *
  3408. * Scenario to use skb_to_sgvec_nomark:
  3409. * 1. sg_init_table
  3410. * 2. skb_to_sgvec_nomark(payload1)
  3411. * 3. skb_to_sgvec_nomark(payload2)
  3412. *
  3413. * This is equivalent to:
  3414. * 1. sg_init_table
  3415. * 2. skb_to_sgvec(payload1)
  3416. * 3. sg_unmark_end
  3417. * 4. skb_to_sgvec(payload2)
  3418. *
  3419. * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
  3420. * is more preferable.
  3421. */
  3422. int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  3423. int offset, int len)
  3424. {
  3425. return __skb_to_sgvec(skb, sg, offset, len, 0);
  3426. }
  3427. EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
  3428. /**
  3429. * skb_cow_data - Check that a socket buffer's data buffers are writable
  3430. * @skb: The socket buffer to check.
  3431. * @tailbits: Amount of trailing space to be added
  3432. * @trailer: Returned pointer to the skb where the @tailbits space begins
  3433. *
  3434. * Make sure that the data buffers attached to a socket buffer are
  3435. * writable. If they are not, private copies are made of the data buffers
  3436. * and the socket buffer is set to use these instead.
  3437. *
  3438. * If @tailbits is given, make sure that there is space to write @tailbits
  3439. * bytes of data beyond current end of socket buffer. @trailer will be
  3440. * set to point to the skb in which this space begins.
  3441. *
  3442. * The number of scatterlist elements required to completely map the
  3443. * COW'd and extended socket buffer will be returned.
  3444. */
  3445. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
  3446. {
  3447. int copyflag;
  3448. int elt;
  3449. struct sk_buff *skb1, **skb_p;
  3450. /* If skb is cloned or its head is paged, reallocate
  3451. * head pulling out all the pages (pages are considered not writable
  3452. * at the moment even if they are anonymous).
  3453. */
  3454. if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
  3455. __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
  3456. return -ENOMEM;
  3457. /* Easy case. Most of packets will go this way. */
  3458. if (!skb_has_frag_list(skb)) {
  3459. /* A little of trouble, not enough of space for trailer.
  3460. * This should not happen, when stack is tuned to generate
  3461. * good frames. OK, on miss we reallocate and reserve even more
  3462. * space, 128 bytes is fair. */
  3463. if (skb_tailroom(skb) < tailbits &&
  3464. pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
  3465. return -ENOMEM;
  3466. /* Voila! */
  3467. *trailer = skb;
  3468. return 1;
  3469. }
  3470. /* Misery. We are in troubles, going to mincer fragments... */
  3471. elt = 1;
  3472. skb_p = &skb_shinfo(skb)->frag_list;
  3473. copyflag = 0;
  3474. while ((skb1 = *skb_p) != NULL) {
  3475. int ntail = 0;
  3476. /* The fragment is partially pulled by someone,
  3477. * this can happen on input. Copy it and everything
  3478. * after it. */
  3479. if (skb_shared(skb1))
  3480. copyflag = 1;
  3481. /* If the skb is the last, worry about trailer. */
  3482. if (skb1->next == NULL && tailbits) {
  3483. if (skb_shinfo(skb1)->nr_frags ||
  3484. skb_has_frag_list(skb1) ||
  3485. skb_tailroom(skb1) < tailbits)
  3486. ntail = tailbits + 128;
  3487. }
  3488. if (copyflag ||
  3489. skb_cloned(skb1) ||
  3490. ntail ||
  3491. skb_shinfo(skb1)->nr_frags ||
  3492. skb_has_frag_list(skb1)) {
  3493. struct sk_buff *skb2;
  3494. /* Fuck, we are miserable poor guys... */
  3495. if (ntail == 0)
  3496. skb2 = skb_copy(skb1, GFP_ATOMIC);
  3497. else
  3498. skb2 = skb_copy_expand(skb1,
  3499. skb_headroom(skb1),
  3500. ntail,
  3501. GFP_ATOMIC);
  3502. if (unlikely(skb2 == NULL))
  3503. return -ENOMEM;
  3504. if (skb1->sk)
  3505. skb_set_owner_w(skb2, skb1->sk);
  3506. /* Looking around. Are we still alive?
  3507. * OK, link new skb, drop old one */
  3508. skb2->next = skb1->next;
  3509. *skb_p = skb2;
  3510. kfree_skb(skb1);
  3511. skb1 = skb2;
  3512. }
  3513. elt++;
  3514. *trailer = skb1;
  3515. skb_p = &skb1->next;
  3516. }
  3517. return elt;
  3518. }
  3519. EXPORT_SYMBOL_GPL(skb_cow_data);
  3520. static void sock_rmem_free(struct sk_buff *skb)
  3521. {
  3522. struct sock *sk = skb->sk;
  3523. atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
  3524. }
  3525. static void skb_set_err_queue(struct sk_buff *skb)
  3526. {
  3527. /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
  3528. * So, it is safe to (mis)use it to mark skbs on the error queue.
  3529. */
  3530. skb->pkt_type = PACKET_OUTGOING;
  3531. BUILD_BUG_ON(PACKET_OUTGOING == 0);
  3532. }
  3533. /*
  3534. * Note: We dont mem charge error packets (no sk_forward_alloc changes)
  3535. */
  3536. int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
  3537. {
  3538. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  3539. (unsigned int)sk->sk_rcvbuf)
  3540. return -ENOMEM;
  3541. skb_orphan(skb);
  3542. skb->sk = sk;
  3543. skb->destructor = sock_rmem_free;
  3544. atomic_add(skb->truesize, &sk->sk_rmem_alloc);
  3545. skb_set_err_queue(skb);
  3546. /* before exiting rcu section, make sure dst is refcounted */
  3547. skb_dst_force(skb);
  3548. skb_queue_tail(&sk->sk_error_queue, skb);
  3549. if (!sock_flag(sk, SOCK_DEAD))
  3550. sk->sk_error_report(sk);
  3551. return 0;
  3552. }
  3553. EXPORT_SYMBOL(sock_queue_err_skb);
  3554. static bool is_icmp_err_skb(const struct sk_buff *skb)
  3555. {
  3556. return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
  3557. SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
  3558. }
  3559. struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
  3560. {
  3561. struct sk_buff_head *q = &sk->sk_error_queue;
  3562. struct sk_buff *skb, *skb_next = NULL;
  3563. bool icmp_next = false;
  3564. unsigned long flags;
  3565. spin_lock_irqsave(&q->lock, flags);
  3566. skb = __skb_dequeue(q);
  3567. if (skb && (skb_next = skb_peek(q))) {
  3568. icmp_next = is_icmp_err_skb(skb_next);
  3569. if (icmp_next)
  3570. sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin;
  3571. }
  3572. spin_unlock_irqrestore(&q->lock, flags);
  3573. if (is_icmp_err_skb(skb) && !icmp_next)
  3574. sk->sk_err = 0;
  3575. if (skb_next)
  3576. sk->sk_error_report(sk);
  3577. return skb;
  3578. }
  3579. EXPORT_SYMBOL(sock_dequeue_err_skb);
  3580. /**
  3581. * skb_clone_sk - create clone of skb, and take reference to socket
  3582. * @skb: the skb to clone
  3583. *
  3584. * This function creates a clone of a buffer that holds a reference on
  3585. * sk_refcnt. Buffers created via this function are meant to be
  3586. * returned using sock_queue_err_skb, or free via kfree_skb.
  3587. *
  3588. * When passing buffers allocated with this function to sock_queue_err_skb
  3589. * it is necessary to wrap the call with sock_hold/sock_put in order to
  3590. * prevent the socket from being released prior to being enqueued on
  3591. * the sk_error_queue.
  3592. */
  3593. struct sk_buff *skb_clone_sk(struct sk_buff *skb)
  3594. {
  3595. struct sock *sk = skb->sk;
  3596. struct sk_buff *clone;
  3597. if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
  3598. return NULL;
  3599. clone = skb_clone(skb, GFP_ATOMIC);
  3600. if (!clone) {
  3601. sock_put(sk);
  3602. return NULL;
  3603. }
  3604. clone->sk = sk;
  3605. clone->destructor = sock_efree;
  3606. return clone;
  3607. }
  3608. EXPORT_SYMBOL(skb_clone_sk);
  3609. static void __skb_complete_tx_timestamp(struct sk_buff *skb,
  3610. struct sock *sk,
  3611. int tstype,
  3612. bool opt_stats)
  3613. {
  3614. struct sock_exterr_skb *serr;
  3615. int err;
  3616. BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
  3617. serr = SKB_EXT_ERR(skb);
  3618. memset(serr, 0, sizeof(*serr));
  3619. serr->ee.ee_errno = ENOMSG;
  3620. serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
  3621. serr->ee.ee_info = tstype;
  3622. serr->opt_stats = opt_stats;
  3623. serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
  3624. if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
  3625. serr->ee.ee_data = skb_shinfo(skb)->tskey;
  3626. if (sk->sk_protocol == IPPROTO_TCP &&
  3627. sk->sk_type == SOCK_STREAM)
  3628. serr->ee.ee_data -= sk->sk_tskey;
  3629. }
  3630. err = sock_queue_err_skb(sk, skb);
  3631. if (err)
  3632. kfree_skb(skb);
  3633. }
  3634. static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
  3635. {
  3636. bool ret;
  3637. if (likely(sysctl_tstamp_allow_data || tsonly))
  3638. return true;
  3639. read_lock_bh(&sk->sk_callback_lock);
  3640. ret = sk->sk_socket && sk->sk_socket->file &&
  3641. file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
  3642. read_unlock_bh(&sk->sk_callback_lock);
  3643. return ret;
  3644. }
  3645. void skb_complete_tx_timestamp(struct sk_buff *skb,
  3646. struct skb_shared_hwtstamps *hwtstamps)
  3647. {
  3648. struct sock *sk = skb->sk;
  3649. if (!skb_may_tx_timestamp(sk, false))
  3650. goto err;
  3651. /* Take a reference to prevent skb_orphan() from freeing the socket,
  3652. * but only if the socket refcount is not zero.
  3653. */
  3654. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  3655. *skb_hwtstamps(skb) = *hwtstamps;
  3656. __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
  3657. sock_put(sk);
  3658. return;
  3659. }
  3660. err:
  3661. kfree_skb(skb);
  3662. }
  3663. EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
  3664. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  3665. struct skb_shared_hwtstamps *hwtstamps,
  3666. struct sock *sk, int tstype)
  3667. {
  3668. struct sk_buff *skb;
  3669. bool tsonly, opt_stats = false;
  3670. if (!sk)
  3671. return;
  3672. if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
  3673. skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
  3674. return;
  3675. tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
  3676. if (!skb_may_tx_timestamp(sk, tsonly))
  3677. return;
  3678. if (tsonly) {
  3679. #ifdef CONFIG_INET
  3680. if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
  3681. sk->sk_protocol == IPPROTO_TCP &&
  3682. sk->sk_type == SOCK_STREAM) {
  3683. skb = tcp_get_timestamping_opt_stats(sk);
  3684. opt_stats = true;
  3685. } else
  3686. #endif
  3687. skb = alloc_skb(0, GFP_ATOMIC);
  3688. } else {
  3689. skb = skb_clone(orig_skb, GFP_ATOMIC);
  3690. }
  3691. if (!skb)
  3692. return;
  3693. if (tsonly) {
  3694. skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
  3695. SKBTX_ANY_TSTAMP;
  3696. skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
  3697. }
  3698. if (hwtstamps)
  3699. *skb_hwtstamps(skb) = *hwtstamps;
  3700. else
  3701. skb->tstamp = ktime_get_real();
  3702. __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
  3703. }
  3704. EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
  3705. void skb_tstamp_tx(struct sk_buff *orig_skb,
  3706. struct skb_shared_hwtstamps *hwtstamps)
  3707. {
  3708. return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
  3709. SCM_TSTAMP_SND);
  3710. }
  3711. EXPORT_SYMBOL_GPL(skb_tstamp_tx);
  3712. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
  3713. {
  3714. struct sock *sk = skb->sk;
  3715. struct sock_exterr_skb *serr;
  3716. int err = 1;
  3717. skb->wifi_acked_valid = 1;
  3718. skb->wifi_acked = acked;
  3719. serr = SKB_EXT_ERR(skb);
  3720. memset(serr, 0, sizeof(*serr));
  3721. serr->ee.ee_errno = ENOMSG;
  3722. serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
  3723. /* Take a reference to prevent skb_orphan() from freeing the socket,
  3724. * but only if the socket refcount is not zero.
  3725. */
  3726. if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
  3727. err = sock_queue_err_skb(sk, skb);
  3728. sock_put(sk);
  3729. }
  3730. if (err)
  3731. kfree_skb(skb);
  3732. }
  3733. EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
  3734. /**
  3735. * skb_partial_csum_set - set up and verify partial csum values for packet
  3736. * @skb: the skb to set
  3737. * @start: the number of bytes after skb->data to start checksumming.
  3738. * @off: the offset from start to place the checksum.
  3739. *
  3740. * For untrusted partially-checksummed packets, we need to make sure the values
  3741. * for skb->csum_start and skb->csum_offset are valid so we don't oops.
  3742. *
  3743. * This function checks and sets those values and skb->ip_summed: if this
  3744. * returns false you should drop the packet.
  3745. */
  3746. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
  3747. {
  3748. u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
  3749. u32 csum_start = skb_headroom(skb) + (u32)start;
  3750. if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
  3751. net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
  3752. start, off, skb_headroom(skb), skb_headlen(skb));
  3753. return false;
  3754. }
  3755. skb->ip_summed = CHECKSUM_PARTIAL;
  3756. skb->csum_start = csum_start;
  3757. skb->csum_offset = off;
  3758. skb_set_transport_header(skb, start);
  3759. return true;
  3760. }
  3761. EXPORT_SYMBOL_GPL(skb_partial_csum_set);
  3762. static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
  3763. unsigned int max)
  3764. {
  3765. if (skb_headlen(skb) >= len)
  3766. return 0;
  3767. /* If we need to pullup then pullup to the max, so we
  3768. * won't need to do it again.
  3769. */
  3770. if (max > skb->len)
  3771. max = skb->len;
  3772. if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
  3773. return -ENOMEM;
  3774. if (skb_headlen(skb) < len)
  3775. return -EPROTO;
  3776. return 0;
  3777. }
  3778. #define MAX_TCP_HDR_LEN (15 * 4)
  3779. static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
  3780. typeof(IPPROTO_IP) proto,
  3781. unsigned int off)
  3782. {
  3783. switch (proto) {
  3784. int err;
  3785. case IPPROTO_TCP:
  3786. err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
  3787. off + MAX_TCP_HDR_LEN);
  3788. if (!err && !skb_partial_csum_set(skb, off,
  3789. offsetof(struct tcphdr,
  3790. check)))
  3791. err = -EPROTO;
  3792. return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
  3793. case IPPROTO_UDP:
  3794. err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
  3795. off + sizeof(struct udphdr));
  3796. if (!err && !skb_partial_csum_set(skb, off,
  3797. offsetof(struct udphdr,
  3798. check)))
  3799. err = -EPROTO;
  3800. return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
  3801. }
  3802. return ERR_PTR(-EPROTO);
  3803. }
  3804. /* This value should be large enough to cover a tagged ethernet header plus
  3805. * maximally sized IP and TCP or UDP headers.
  3806. */
  3807. #define MAX_IP_HDR_LEN 128
  3808. static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
  3809. {
  3810. unsigned int off;
  3811. bool fragment;
  3812. __sum16 *csum;
  3813. int err;
  3814. fragment = false;
  3815. err = skb_maybe_pull_tail(skb,
  3816. sizeof(struct iphdr),
  3817. MAX_IP_HDR_LEN);
  3818. if (err < 0)
  3819. goto out;
  3820. if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
  3821. fragment = true;
  3822. off = ip_hdrlen(skb);
  3823. err = -EPROTO;
  3824. if (fragment)
  3825. goto out;
  3826. csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
  3827. if (IS_ERR(csum))
  3828. return PTR_ERR(csum);
  3829. if (recalculate)
  3830. *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
  3831. ip_hdr(skb)->daddr,
  3832. skb->len - off,
  3833. ip_hdr(skb)->protocol, 0);
  3834. err = 0;
  3835. out:
  3836. return err;
  3837. }
  3838. /* This value should be large enough to cover a tagged ethernet header plus
  3839. * an IPv6 header, all options, and a maximal TCP or UDP header.
  3840. */
  3841. #define MAX_IPV6_HDR_LEN 256
  3842. #define OPT_HDR(type, skb, off) \
  3843. (type *)(skb_network_header(skb) + (off))
  3844. static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
  3845. {
  3846. int err;
  3847. u8 nexthdr;
  3848. unsigned int off;
  3849. unsigned int len;
  3850. bool fragment;
  3851. bool done;
  3852. __sum16 *csum;
  3853. fragment = false;
  3854. done = false;
  3855. off = sizeof(struct ipv6hdr);
  3856. err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
  3857. if (err < 0)
  3858. goto out;
  3859. nexthdr = ipv6_hdr(skb)->nexthdr;
  3860. len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
  3861. while (off <= len && !done) {
  3862. switch (nexthdr) {
  3863. case IPPROTO_DSTOPTS:
  3864. case IPPROTO_HOPOPTS:
  3865. case IPPROTO_ROUTING: {
  3866. struct ipv6_opt_hdr *hp;
  3867. err = skb_maybe_pull_tail(skb,
  3868. off +
  3869. sizeof(struct ipv6_opt_hdr),
  3870. MAX_IPV6_HDR_LEN);
  3871. if (err < 0)
  3872. goto out;
  3873. hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
  3874. nexthdr = hp->nexthdr;
  3875. off += ipv6_optlen(hp);
  3876. break;
  3877. }
  3878. case IPPROTO_AH: {
  3879. struct ip_auth_hdr *hp;
  3880. err = skb_maybe_pull_tail(skb,
  3881. off +
  3882. sizeof(struct ip_auth_hdr),
  3883. MAX_IPV6_HDR_LEN);
  3884. if (err < 0)
  3885. goto out;
  3886. hp = OPT_HDR(struct ip_auth_hdr, skb, off);
  3887. nexthdr = hp->nexthdr;
  3888. off += ipv6_authlen(hp);
  3889. break;
  3890. }
  3891. case IPPROTO_FRAGMENT: {
  3892. struct frag_hdr *hp;
  3893. err = skb_maybe_pull_tail(skb,
  3894. off +
  3895. sizeof(struct frag_hdr),
  3896. MAX_IPV6_HDR_LEN);
  3897. if (err < 0)
  3898. goto out;
  3899. hp = OPT_HDR(struct frag_hdr, skb, off);
  3900. if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
  3901. fragment = true;
  3902. nexthdr = hp->nexthdr;
  3903. off += sizeof(struct frag_hdr);
  3904. break;
  3905. }
  3906. default:
  3907. done = true;
  3908. break;
  3909. }
  3910. }
  3911. err = -EPROTO;
  3912. if (!done || fragment)
  3913. goto out;
  3914. csum = skb_checksum_setup_ip(skb, nexthdr, off);
  3915. if (IS_ERR(csum))
  3916. return PTR_ERR(csum);
  3917. if (recalculate)
  3918. *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  3919. &ipv6_hdr(skb)->daddr,
  3920. skb->len - off, nexthdr, 0);
  3921. err = 0;
  3922. out:
  3923. return err;
  3924. }
  3925. /**
  3926. * skb_checksum_setup - set up partial checksum offset
  3927. * @skb: the skb to set up
  3928. * @recalculate: if true the pseudo-header checksum will be recalculated
  3929. */
  3930. int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
  3931. {
  3932. int err;
  3933. switch (skb->protocol) {
  3934. case htons(ETH_P_IP):
  3935. err = skb_checksum_setup_ipv4(skb, recalculate);
  3936. break;
  3937. case htons(ETH_P_IPV6):
  3938. err = skb_checksum_setup_ipv6(skb, recalculate);
  3939. break;
  3940. default:
  3941. err = -EPROTO;
  3942. break;
  3943. }
  3944. return err;
  3945. }
  3946. EXPORT_SYMBOL(skb_checksum_setup);
  3947. /**
  3948. * skb_checksum_maybe_trim - maybe trims the given skb
  3949. * @skb: the skb to check
  3950. * @transport_len: the data length beyond the network header
  3951. *
  3952. * Checks whether the given skb has data beyond the given transport length.
  3953. * If so, returns a cloned skb trimmed to this transport length.
  3954. * Otherwise returns the provided skb. Returns NULL in error cases
  3955. * (e.g. transport_len exceeds skb length or out-of-memory).
  3956. *
  3957. * Caller needs to set the skb transport header and free any returned skb if it
  3958. * differs from the provided skb.
  3959. */
  3960. static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
  3961. unsigned int transport_len)
  3962. {
  3963. struct sk_buff *skb_chk;
  3964. unsigned int len = skb_transport_offset(skb) + transport_len;
  3965. int ret;
  3966. if (skb->len < len)
  3967. return NULL;
  3968. else if (skb->len == len)
  3969. return skb;
  3970. skb_chk = skb_clone(skb, GFP_ATOMIC);
  3971. if (!skb_chk)
  3972. return NULL;
  3973. ret = pskb_trim_rcsum(skb_chk, len);
  3974. if (ret) {
  3975. kfree_skb(skb_chk);
  3976. return NULL;
  3977. }
  3978. return skb_chk;
  3979. }
  3980. /**
  3981. * skb_checksum_trimmed - validate checksum of an skb
  3982. * @skb: the skb to check
  3983. * @transport_len: the data length beyond the network header
  3984. * @skb_chkf: checksum function to use
  3985. *
  3986. * Applies the given checksum function skb_chkf to the provided skb.
  3987. * Returns a checked and maybe trimmed skb. Returns NULL on error.
  3988. *
  3989. * If the skb has data beyond the given transport length, then a
  3990. * trimmed & cloned skb is checked and returned.
  3991. *
  3992. * Caller needs to set the skb transport header and free any returned skb if it
  3993. * differs from the provided skb.
  3994. */
  3995. struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
  3996. unsigned int transport_len,
  3997. __sum16(*skb_chkf)(struct sk_buff *skb))
  3998. {
  3999. struct sk_buff *skb_chk;
  4000. unsigned int offset = skb_transport_offset(skb);
  4001. __sum16 ret;
  4002. skb_chk = skb_checksum_maybe_trim(skb, transport_len);
  4003. if (!skb_chk)
  4004. goto err;
  4005. if (!pskb_may_pull(skb_chk, offset))
  4006. goto err;
  4007. skb_pull_rcsum(skb_chk, offset);
  4008. ret = skb_chkf(skb_chk);
  4009. skb_push_rcsum(skb_chk, offset);
  4010. if (ret)
  4011. goto err;
  4012. return skb_chk;
  4013. err:
  4014. if (skb_chk && skb_chk != skb)
  4015. kfree_skb(skb_chk);
  4016. return NULL;
  4017. }
  4018. EXPORT_SYMBOL(skb_checksum_trimmed);
  4019. void __skb_warn_lro_forwarding(const struct sk_buff *skb)
  4020. {
  4021. net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
  4022. skb->dev->name);
  4023. }
  4024. EXPORT_SYMBOL(__skb_warn_lro_forwarding);
  4025. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
  4026. {
  4027. if (head_stolen) {
  4028. skb_release_head_state(skb);
  4029. kmem_cache_free(skbuff_head_cache, skb);
  4030. } else {
  4031. __kfree_skb(skb);
  4032. }
  4033. }
  4034. EXPORT_SYMBOL(kfree_skb_partial);
  4035. /**
  4036. * skb_try_coalesce - try to merge skb to prior one
  4037. * @to: prior buffer
  4038. * @from: buffer to add
  4039. * @fragstolen: pointer to boolean
  4040. * @delta_truesize: how much more was allocated than was requested
  4041. */
  4042. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  4043. bool *fragstolen, int *delta_truesize)
  4044. {
  4045. struct skb_shared_info *to_shinfo, *from_shinfo;
  4046. int i, delta, len = from->len;
  4047. *fragstolen = false;
  4048. if (skb_cloned(to))
  4049. return false;
  4050. if (len <= skb_tailroom(to)) {
  4051. if (len)
  4052. BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
  4053. *delta_truesize = 0;
  4054. return true;
  4055. }
  4056. to_shinfo = skb_shinfo(to);
  4057. from_shinfo = skb_shinfo(from);
  4058. if (to_shinfo->frag_list || from_shinfo->frag_list)
  4059. return false;
  4060. if (skb_zcopy(to) || skb_zcopy(from))
  4061. return false;
  4062. if (skb_headlen(from) != 0) {
  4063. struct page *page;
  4064. unsigned int offset;
  4065. if (to_shinfo->nr_frags +
  4066. from_shinfo->nr_frags >= MAX_SKB_FRAGS)
  4067. return false;
  4068. if (skb_head_is_locked(from))
  4069. return false;
  4070. delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
  4071. page = virt_to_head_page(from->head);
  4072. offset = from->data - (unsigned char *)page_address(page);
  4073. skb_fill_page_desc(to, to_shinfo->nr_frags,
  4074. page, offset, skb_headlen(from));
  4075. *fragstolen = true;
  4076. } else {
  4077. if (to_shinfo->nr_frags +
  4078. from_shinfo->nr_frags > MAX_SKB_FRAGS)
  4079. return false;
  4080. delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
  4081. }
  4082. WARN_ON_ONCE(delta < len);
  4083. memcpy(to_shinfo->frags + to_shinfo->nr_frags,
  4084. from_shinfo->frags,
  4085. from_shinfo->nr_frags * sizeof(skb_frag_t));
  4086. to_shinfo->nr_frags += from_shinfo->nr_frags;
  4087. if (!skb_cloned(from))
  4088. from_shinfo->nr_frags = 0;
  4089. /* if the skb is not cloned this does nothing
  4090. * since we set nr_frags to 0.
  4091. */
  4092. for (i = 0; i < from_shinfo->nr_frags; i++)
  4093. __skb_frag_ref(&from_shinfo->frags[i]);
  4094. to->truesize += delta;
  4095. to->len += len;
  4096. to->data_len += len;
  4097. *delta_truesize = delta;
  4098. return true;
  4099. }
  4100. EXPORT_SYMBOL(skb_try_coalesce);
  4101. /**
  4102. * skb_scrub_packet - scrub an skb
  4103. *
  4104. * @skb: buffer to clean
  4105. * @xnet: packet is crossing netns
  4106. *
  4107. * skb_scrub_packet can be used after encapsulating or decapsulting a packet
  4108. * into/from a tunnel. Some information have to be cleared during these
  4109. * operations.
  4110. * skb_scrub_packet can also be used to clean a skb before injecting it in
  4111. * another namespace (@xnet == true). We have to clear all information in the
  4112. * skb that could impact namespace isolation.
  4113. */
  4114. void skb_scrub_packet(struct sk_buff *skb, bool xnet)
  4115. {
  4116. skb->pkt_type = PACKET_HOST;
  4117. skb->skb_iif = 0;
  4118. skb->ignore_df = 0;
  4119. skb_dst_drop(skb);
  4120. secpath_reset(skb);
  4121. nf_reset(skb);
  4122. nf_reset_trace(skb);
  4123. #ifdef CONFIG_NET_SWITCHDEV
  4124. skb->offload_fwd_mark = 0;
  4125. skb->offload_mr_fwd_mark = 0;
  4126. #endif
  4127. if (!xnet)
  4128. return;
  4129. ipvs_reset(skb);
  4130. skb->mark = 0;
  4131. skb->tstamp = 0;
  4132. }
  4133. EXPORT_SYMBOL_GPL(skb_scrub_packet);
  4134. /**
  4135. * skb_gso_transport_seglen - Return length of individual segments of a gso packet
  4136. *
  4137. * @skb: GSO skb
  4138. *
  4139. * skb_gso_transport_seglen is used to determine the real size of the
  4140. * individual segments, including Layer4 headers (TCP/UDP).
  4141. *
  4142. * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
  4143. */
  4144. static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
  4145. {
  4146. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  4147. unsigned int thlen = 0;
  4148. if (skb->encapsulation) {
  4149. thlen = skb_inner_transport_header(skb) -
  4150. skb_transport_header(skb);
  4151. if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
  4152. thlen += inner_tcp_hdrlen(skb);
  4153. } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
  4154. thlen = tcp_hdrlen(skb);
  4155. } else if (unlikely(skb_is_gso_sctp(skb))) {
  4156. thlen = sizeof(struct sctphdr);
  4157. } else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
  4158. thlen = sizeof(struct udphdr);
  4159. }
  4160. /* UFO sets gso_size to the size of the fragmentation
  4161. * payload, i.e. the size of the L4 (UDP) header is already
  4162. * accounted for.
  4163. */
  4164. return thlen + shinfo->gso_size;
  4165. }
  4166. /**
  4167. * skb_gso_network_seglen - Return length of individual segments of a gso packet
  4168. *
  4169. * @skb: GSO skb
  4170. *
  4171. * skb_gso_network_seglen is used to determine the real size of the
  4172. * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
  4173. *
  4174. * The MAC/L2 header is not accounted for.
  4175. */
  4176. static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
  4177. {
  4178. unsigned int hdr_len = skb_transport_header(skb) -
  4179. skb_network_header(skb);
  4180. return hdr_len + skb_gso_transport_seglen(skb);
  4181. }
  4182. /**
  4183. * skb_gso_mac_seglen - Return length of individual segments of a gso packet
  4184. *
  4185. * @skb: GSO skb
  4186. *
  4187. * skb_gso_mac_seglen is used to determine the real size of the
  4188. * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
  4189. * headers (TCP/UDP).
  4190. */
  4191. static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
  4192. {
  4193. unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
  4194. return hdr_len + skb_gso_transport_seglen(skb);
  4195. }
  4196. /**
  4197. * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
  4198. *
  4199. * There are a couple of instances where we have a GSO skb, and we
  4200. * want to determine what size it would be after it is segmented.
  4201. *
  4202. * We might want to check:
  4203. * - L3+L4+payload size (e.g. IP forwarding)
  4204. * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
  4205. *
  4206. * This is a helper to do that correctly considering GSO_BY_FRAGS.
  4207. *
  4208. * @skb: GSO skb
  4209. *
  4210. * @seg_len: The segmented length (from skb_gso_*_seglen). In the
  4211. * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
  4212. *
  4213. * @max_len: The maximum permissible length.
  4214. *
  4215. * Returns true if the segmented length <= max length.
  4216. */
  4217. static inline bool skb_gso_size_check(const struct sk_buff *skb,
  4218. unsigned int seg_len,
  4219. unsigned int max_len) {
  4220. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  4221. const struct sk_buff *iter;
  4222. if (shinfo->gso_size != GSO_BY_FRAGS)
  4223. return seg_len <= max_len;
  4224. /* Undo this so we can re-use header sizes */
  4225. seg_len -= GSO_BY_FRAGS;
  4226. skb_walk_frags(skb, iter) {
  4227. if (seg_len + skb_headlen(iter) > max_len)
  4228. return false;
  4229. }
  4230. return true;
  4231. }
  4232. /**
  4233. * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
  4234. *
  4235. * @skb: GSO skb
  4236. * @mtu: MTU to validate against
  4237. *
  4238. * skb_gso_validate_network_len validates if a given skb will fit a
  4239. * wanted MTU once split. It considers L3 headers, L4 headers, and the
  4240. * payload.
  4241. */
  4242. bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
  4243. {
  4244. return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
  4245. }
  4246. EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
  4247. /**
  4248. * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
  4249. *
  4250. * @skb: GSO skb
  4251. * @len: length to validate against
  4252. *
  4253. * skb_gso_validate_mac_len validates if a given skb will fit a wanted
  4254. * length once split, including L2, L3 and L4 headers and the payload.
  4255. */
  4256. bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
  4257. {
  4258. return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
  4259. }
  4260. EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
  4261. static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
  4262. {
  4263. int mac_len;
  4264. if (skb_cow(skb, skb_headroom(skb)) < 0) {
  4265. kfree_skb(skb);
  4266. return NULL;
  4267. }
  4268. mac_len = skb->data - skb_mac_header(skb);
  4269. if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
  4270. memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
  4271. mac_len - VLAN_HLEN - ETH_TLEN);
  4272. }
  4273. skb->mac_header += VLAN_HLEN;
  4274. return skb;
  4275. }
  4276. struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
  4277. {
  4278. struct vlan_hdr *vhdr;
  4279. u16 vlan_tci;
  4280. if (unlikely(skb_vlan_tag_present(skb))) {
  4281. /* vlan_tci is already set-up so leave this for another time */
  4282. return skb;
  4283. }
  4284. skb = skb_share_check(skb, GFP_ATOMIC);
  4285. if (unlikely(!skb))
  4286. goto err_free;
  4287. if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
  4288. goto err_free;
  4289. vhdr = (struct vlan_hdr *)skb->data;
  4290. vlan_tci = ntohs(vhdr->h_vlan_TCI);
  4291. __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
  4292. skb_pull_rcsum(skb, VLAN_HLEN);
  4293. vlan_set_encap_proto(skb, vhdr);
  4294. skb = skb_reorder_vlan_header(skb);
  4295. if (unlikely(!skb))
  4296. goto err_free;
  4297. skb_reset_network_header(skb);
  4298. skb_reset_transport_header(skb);
  4299. skb_reset_mac_len(skb);
  4300. return skb;
  4301. err_free:
  4302. kfree_skb(skb);
  4303. return NULL;
  4304. }
  4305. EXPORT_SYMBOL(skb_vlan_untag);
  4306. int skb_ensure_writable(struct sk_buff *skb, int write_len)
  4307. {
  4308. if (!pskb_may_pull(skb, write_len))
  4309. return -ENOMEM;
  4310. if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
  4311. return 0;
  4312. return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  4313. }
  4314. EXPORT_SYMBOL(skb_ensure_writable);
  4315. /* remove VLAN header from packet and update csum accordingly.
  4316. * expects a non skb_vlan_tag_present skb with a vlan tag payload
  4317. */
  4318. int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
  4319. {
  4320. struct vlan_hdr *vhdr;
  4321. int offset = skb->data - skb_mac_header(skb);
  4322. int err;
  4323. if (WARN_ONCE(offset,
  4324. "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
  4325. offset)) {
  4326. return -EINVAL;
  4327. }
  4328. err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
  4329. if (unlikely(err))
  4330. return err;
  4331. skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  4332. vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
  4333. *vlan_tci = ntohs(vhdr->h_vlan_TCI);
  4334. memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
  4335. __skb_pull(skb, VLAN_HLEN);
  4336. vlan_set_encap_proto(skb, vhdr);
  4337. skb->mac_header += VLAN_HLEN;
  4338. if (skb_network_offset(skb) < ETH_HLEN)
  4339. skb_set_network_header(skb, ETH_HLEN);
  4340. skb_reset_mac_len(skb);
  4341. return err;
  4342. }
  4343. EXPORT_SYMBOL(__skb_vlan_pop);
  4344. /* Pop a vlan tag either from hwaccel or from payload.
  4345. * Expects skb->data at mac header.
  4346. */
  4347. int skb_vlan_pop(struct sk_buff *skb)
  4348. {
  4349. u16 vlan_tci;
  4350. __be16 vlan_proto;
  4351. int err;
  4352. if (likely(skb_vlan_tag_present(skb))) {
  4353. skb->vlan_tci = 0;
  4354. } else {
  4355. if (unlikely(!eth_type_vlan(skb->protocol)))
  4356. return 0;
  4357. err = __skb_vlan_pop(skb, &vlan_tci);
  4358. if (err)
  4359. return err;
  4360. }
  4361. /* move next vlan tag to hw accel tag */
  4362. if (likely(!eth_type_vlan(skb->protocol)))
  4363. return 0;
  4364. vlan_proto = skb->protocol;
  4365. err = __skb_vlan_pop(skb, &vlan_tci);
  4366. if (unlikely(err))
  4367. return err;
  4368. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  4369. return 0;
  4370. }
  4371. EXPORT_SYMBOL(skb_vlan_pop);
  4372. /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
  4373. * Expects skb->data at mac header.
  4374. */
  4375. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
  4376. {
  4377. if (skb_vlan_tag_present(skb)) {
  4378. int offset = skb->data - skb_mac_header(skb);
  4379. int err;
  4380. if (WARN_ONCE(offset,
  4381. "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
  4382. offset)) {
  4383. return -EINVAL;
  4384. }
  4385. err = __vlan_insert_tag(skb, skb->vlan_proto,
  4386. skb_vlan_tag_get(skb));
  4387. if (err)
  4388. return err;
  4389. skb->protocol = skb->vlan_proto;
  4390. skb->mac_len += VLAN_HLEN;
  4391. skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
  4392. }
  4393. __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
  4394. return 0;
  4395. }
  4396. EXPORT_SYMBOL(skb_vlan_push);
  4397. /**
  4398. * alloc_skb_with_frags - allocate skb with page frags
  4399. *
  4400. * @header_len: size of linear part
  4401. * @data_len: needed length in frags
  4402. * @max_page_order: max page order desired.
  4403. * @errcode: pointer to error code if any
  4404. * @gfp_mask: allocation mask
  4405. *
  4406. * This can be used to allocate a paged skb, given a maximal order for frags.
  4407. */
  4408. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  4409. unsigned long data_len,
  4410. int max_page_order,
  4411. int *errcode,
  4412. gfp_t gfp_mask)
  4413. {
  4414. int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  4415. unsigned long chunk;
  4416. struct sk_buff *skb;
  4417. struct page *page;
  4418. gfp_t gfp_head;
  4419. int i;
  4420. *errcode = -EMSGSIZE;
  4421. /* Note this test could be relaxed, if we succeed to allocate
  4422. * high order pages...
  4423. */
  4424. if (npages > MAX_SKB_FRAGS)
  4425. return NULL;
  4426. gfp_head = gfp_mask;
  4427. if (gfp_head & __GFP_DIRECT_RECLAIM)
  4428. gfp_head |= __GFP_RETRY_MAYFAIL;
  4429. *errcode = -ENOBUFS;
  4430. skb = alloc_skb(header_len, gfp_head);
  4431. if (!skb)
  4432. return NULL;
  4433. skb->truesize += npages << PAGE_SHIFT;
  4434. for (i = 0; npages > 0; i++) {
  4435. int order = max_page_order;
  4436. while (order) {
  4437. if (npages >= 1 << order) {
  4438. page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
  4439. __GFP_COMP |
  4440. __GFP_NOWARN,
  4441. order);
  4442. if (page)
  4443. goto fill_page;
  4444. /* Do not retry other high order allocations */
  4445. order = 1;
  4446. max_page_order = 0;
  4447. }
  4448. order--;
  4449. }
  4450. page = alloc_page(gfp_mask);
  4451. if (!page)
  4452. goto failure;
  4453. fill_page:
  4454. chunk = min_t(unsigned long, data_len,
  4455. PAGE_SIZE << order);
  4456. skb_fill_page_desc(skb, i, page, 0, chunk);
  4457. data_len -= chunk;
  4458. npages -= 1 << order;
  4459. }
  4460. return skb;
  4461. failure:
  4462. kfree_skb(skb);
  4463. return NULL;
  4464. }
  4465. EXPORT_SYMBOL(alloc_skb_with_frags);
  4466. /* carve out the first off bytes from skb when off < headlen */
  4467. static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
  4468. const int headlen, gfp_t gfp_mask)
  4469. {
  4470. int i;
  4471. int size = skb_end_offset(skb);
  4472. int new_hlen = headlen - off;
  4473. u8 *data;
  4474. size = SKB_DATA_ALIGN(size);
  4475. if (skb_pfmemalloc(skb))
  4476. gfp_mask |= __GFP_MEMALLOC;
  4477. data = kmalloc_reserve(size +
  4478. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  4479. gfp_mask, NUMA_NO_NODE, NULL);
  4480. if (!data)
  4481. return -ENOMEM;
  4482. size = SKB_WITH_OVERHEAD(ksize(data));
  4483. /* Copy real data, and all frags */
  4484. skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
  4485. skb->len -= off;
  4486. memcpy((struct skb_shared_info *)(data + size),
  4487. skb_shinfo(skb),
  4488. offsetof(struct skb_shared_info,
  4489. frags[skb_shinfo(skb)->nr_frags]));
  4490. if (skb_cloned(skb)) {
  4491. /* drop the old head gracefully */
  4492. if (skb_orphan_frags(skb, gfp_mask)) {
  4493. kfree(data);
  4494. return -ENOMEM;
  4495. }
  4496. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
  4497. skb_frag_ref(skb, i);
  4498. if (skb_has_frag_list(skb))
  4499. skb_clone_fraglist(skb);
  4500. skb_release_data(skb);
  4501. } else {
  4502. /* we can reuse existing recount- all we did was
  4503. * relocate values
  4504. */
  4505. skb_free_head(skb);
  4506. }
  4507. skb->head = data;
  4508. skb->data = data;
  4509. skb->head_frag = 0;
  4510. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  4511. skb->end = size;
  4512. #else
  4513. skb->end = skb->head + size;
  4514. #endif
  4515. skb_set_tail_pointer(skb, skb_headlen(skb));
  4516. skb_headers_offset_update(skb, 0);
  4517. skb->cloned = 0;
  4518. skb->hdr_len = 0;
  4519. skb->nohdr = 0;
  4520. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4521. return 0;
  4522. }
  4523. static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
  4524. /* carve out the first eat bytes from skb's frag_list. May recurse into
  4525. * pskb_carve()
  4526. */
  4527. static int pskb_carve_frag_list(struct sk_buff *skb,
  4528. struct skb_shared_info *shinfo, int eat,
  4529. gfp_t gfp_mask)
  4530. {
  4531. struct sk_buff *list = shinfo->frag_list;
  4532. struct sk_buff *clone = NULL;
  4533. struct sk_buff *insp = NULL;
  4534. do {
  4535. if (!list) {
  4536. pr_err("Not enough bytes to eat. Want %d\n", eat);
  4537. return -EFAULT;
  4538. }
  4539. if (list->len <= eat) {
  4540. /* Eaten as whole. */
  4541. eat -= list->len;
  4542. list = list->next;
  4543. insp = list;
  4544. } else {
  4545. /* Eaten partially. */
  4546. if (skb_shared(list)) {
  4547. clone = skb_clone(list, gfp_mask);
  4548. if (!clone)
  4549. return -ENOMEM;
  4550. insp = list->next;
  4551. list = clone;
  4552. } else {
  4553. /* This may be pulled without problems. */
  4554. insp = list;
  4555. }
  4556. if (pskb_carve(list, eat, gfp_mask) < 0) {
  4557. kfree_skb(clone);
  4558. return -ENOMEM;
  4559. }
  4560. break;
  4561. }
  4562. } while (eat);
  4563. /* Free pulled out fragments. */
  4564. while ((list = shinfo->frag_list) != insp) {
  4565. shinfo->frag_list = list->next;
  4566. kfree_skb(list);
  4567. }
  4568. /* And insert new clone at head. */
  4569. if (clone) {
  4570. clone->next = list;
  4571. shinfo->frag_list = clone;
  4572. }
  4573. return 0;
  4574. }
  4575. /* carve off first len bytes from skb. Split line (off) is in the
  4576. * non-linear part of skb
  4577. */
  4578. static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
  4579. int pos, gfp_t gfp_mask)
  4580. {
  4581. int i, k = 0;
  4582. int size = skb_end_offset(skb);
  4583. u8 *data;
  4584. const int nfrags = skb_shinfo(skb)->nr_frags;
  4585. struct skb_shared_info *shinfo;
  4586. size = SKB_DATA_ALIGN(size);
  4587. if (skb_pfmemalloc(skb))
  4588. gfp_mask |= __GFP_MEMALLOC;
  4589. data = kmalloc_reserve(size +
  4590. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
  4591. gfp_mask, NUMA_NO_NODE, NULL);
  4592. if (!data)
  4593. return -ENOMEM;
  4594. size = SKB_WITH_OVERHEAD(ksize(data));
  4595. memcpy((struct skb_shared_info *)(data + size),
  4596. skb_shinfo(skb), offsetof(struct skb_shared_info,
  4597. frags[skb_shinfo(skb)->nr_frags]));
  4598. if (skb_orphan_frags(skb, gfp_mask)) {
  4599. kfree(data);
  4600. return -ENOMEM;
  4601. }
  4602. shinfo = (struct skb_shared_info *)(data + size);
  4603. for (i = 0; i < nfrags; i++) {
  4604. int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  4605. if (pos + fsize > off) {
  4606. shinfo->frags[k] = skb_shinfo(skb)->frags[i];
  4607. if (pos < off) {
  4608. /* Split frag.
  4609. * We have two variants in this case:
  4610. * 1. Move all the frag to the second
  4611. * part, if it is possible. F.e.
  4612. * this approach is mandatory for TUX,
  4613. * where splitting is expensive.
  4614. * 2. Split is accurately. We make this.
  4615. */
  4616. shinfo->frags[0].page_offset += off - pos;
  4617. skb_frag_size_sub(&shinfo->frags[0], off - pos);
  4618. }
  4619. skb_frag_ref(skb, i);
  4620. k++;
  4621. }
  4622. pos += fsize;
  4623. }
  4624. shinfo->nr_frags = k;
  4625. if (skb_has_frag_list(skb))
  4626. skb_clone_fraglist(skb);
  4627. if (k == 0) {
  4628. /* split line is in frag list */
  4629. pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
  4630. }
  4631. skb_release_data(skb);
  4632. skb->head = data;
  4633. skb->head_frag = 0;
  4634. skb->data = data;
  4635. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  4636. skb->end = size;
  4637. #else
  4638. skb->end = skb->head + size;
  4639. #endif
  4640. skb_reset_tail_pointer(skb);
  4641. skb_headers_offset_update(skb, 0);
  4642. skb->cloned = 0;
  4643. skb->hdr_len = 0;
  4644. skb->nohdr = 0;
  4645. skb->len -= off;
  4646. skb->data_len = skb->len;
  4647. atomic_set(&skb_shinfo(skb)->dataref, 1);
  4648. return 0;
  4649. }
  4650. /* remove len bytes from the beginning of the skb */
  4651. static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
  4652. {
  4653. int headlen = skb_headlen(skb);
  4654. if (len < headlen)
  4655. return pskb_carve_inside_header(skb, len, headlen, gfp);
  4656. else
  4657. return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
  4658. }
  4659. /* Extract to_copy bytes starting at off from skb, and return this in
  4660. * a new skb
  4661. */
  4662. struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
  4663. int to_copy, gfp_t gfp)
  4664. {
  4665. struct sk_buff *clone = skb_clone(skb, gfp);
  4666. if (!clone)
  4667. return NULL;
  4668. if (pskb_carve(clone, off, gfp) < 0 ||
  4669. pskb_trim(clone, to_copy)) {
  4670. kfree_skb(clone);
  4671. return NULL;
  4672. }
  4673. return clone;
  4674. }
  4675. EXPORT_SYMBOL(pskb_extract);
  4676. /**
  4677. * skb_condense - try to get rid of fragments/frag_list if possible
  4678. * @skb: buffer
  4679. *
  4680. * Can be used to save memory before skb is added to a busy queue.
  4681. * If packet has bytes in frags and enough tail room in skb->head,
  4682. * pull all of them, so that we can free the frags right now and adjust
  4683. * truesize.
  4684. * Notes:
  4685. * We do not reallocate skb->head thus can not fail.
  4686. * Caller must re-evaluate skb->truesize if needed.
  4687. */
  4688. void skb_condense(struct sk_buff *skb)
  4689. {
  4690. if (skb->data_len) {
  4691. if (skb->data_len > skb->end - skb->tail ||
  4692. skb_cloned(skb))
  4693. return;
  4694. /* Nice, we can free page frag(s) right now */
  4695. __pskb_pull_tail(skb, skb->data_len);
  4696. }
  4697. /* At this point, skb->truesize might be over estimated,
  4698. * because skb had a fragment, and fragments do not tell
  4699. * their truesize.
  4700. * When we pulled its content into skb->head, fragment
  4701. * was freed, but __pskb_pull_tail() could not possibly
  4702. * adjust skb->truesize, not knowing the frag truesize.
  4703. */
  4704. skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
  4705. }