skbuff.c 127 KB

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