skbuff.c 166 KB

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