skbuff.c 139 KB

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