skbuff.c 125 KB

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