skbuff.c 126 KB

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