skbuff.c 133 KB

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