skbuff.c 133 KB

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