skbuff.c 133 KB

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