skbuff.c 137 KB

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