af_packet.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * PACKET - implements raw packet sockets.
  8. *
  9. * Authors: Ross Biro
  10. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  11. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  12. *
  13. * Fixes:
  14. * Alan Cox : verify_area() now used correctly
  15. * Alan Cox : new skbuff lists, look ma no backlogs!
  16. * Alan Cox : tidied skbuff lists.
  17. * Alan Cox : Now uses generic datagram routines I
  18. * added. Also fixed the peek/read crash
  19. * from all old Linux datagram code.
  20. * Alan Cox : Uses the improved datagram code.
  21. * Alan Cox : Added NULL's for socket options.
  22. * Alan Cox : Re-commented the code.
  23. * Alan Cox : Use new kernel side addressing
  24. * Rob Janssen : Correct MTU usage.
  25. * Dave Platt : Counter leaks caused by incorrect
  26. * interrupt locking and some slightly
  27. * dubious gcc output. Can you read
  28. * compiler: it said _VOLATILE_
  29. * Richard Kooijman : Timestamp fixes.
  30. * Alan Cox : New buffers. Use sk->mac.raw.
  31. * Alan Cox : sendmsg/recvmsg support.
  32. * Alan Cox : Protocol setting support
  33. * Alexey Kuznetsov : Untied from IPv4 stack.
  34. * Cyrus Durgin : Fixed kerneld for kmod.
  35. * Michal Ostrowski : Module initialization cleanup.
  36. * Ulises Alonso : Frame number limit removal and
  37. * packet_set_ring memory leak.
  38. * Eric Biederman : Allow for > 8 byte hardware addresses.
  39. * The convention is that longer addresses
  40. * will simply extend the hardware address
  41. * byte arrays at the end of sockaddr_ll
  42. * and packet_mreq.
  43. * Johann Baudy : Added TX RING.
  44. * Chetan Loke : Implemented TPACKET_V3 block abstraction
  45. * layer.
  46. * Copyright (C) 2011, <lokec@ccs.neu.edu>
  47. */
  48. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49. #include <linux/ethtool.h>
  50. #include <linux/filter.h>
  51. #include <linux/types.h>
  52. #include <linux/mm.h>
  53. #include <linux/capability.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/socket.h>
  56. #include <linux/in.h>
  57. #include <linux/inet.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/if_packet.h>
  60. #include <linux/wireless.h>
  61. #include <linux/kernel.h>
  62. #include <linux/kmod.h>
  63. #include <linux/slab.h>
  64. #include <linux/vmalloc.h>
  65. #include <net/net_namespace.h>
  66. #include <net/ip.h>
  67. #include <net/protocol.h>
  68. #include <linux/skbuff.h>
  69. #include <net/sock.h>
  70. #include <linux/errno.h>
  71. #include <linux/timer.h>
  72. #include <linux/uaccess.h>
  73. #include <asm/ioctls.h>
  74. #include <asm/page.h>
  75. #include <asm/cacheflush.h>
  76. #include <asm/io.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <linux/poll.h>
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/mutex.h>
  83. #include <linux/if_vlan.h>
  84. #include <linux/virtio_net.h>
  85. #include <linux/errqueue.h>
  86. #include <linux/net_tstamp.h>
  87. #include <linux/percpu.h>
  88. #ifdef CONFIG_INET
  89. #include <net/inet_common.h>
  90. #endif
  91. #include <linux/bpf.h>
  92. #include <net/compat.h>
  93. #include <linux/netfilter_netdev.h>
  94. #include "internal.h"
  95. /*
  96. Assumptions:
  97. - If the device has no dev->header_ops->create, there is no LL header
  98. visible above the device. In this case, its hard_header_len should be 0.
  99. The device may prepend its own header internally. In this case, its
  100. needed_headroom should be set to the space needed for it to add its
  101. internal header.
  102. For example, a WiFi driver pretending to be an Ethernet driver should
  103. set its hard_header_len to be the Ethernet header length, and set its
  104. needed_headroom to be (the real WiFi header length - the fake Ethernet
  105. header length).
  106. - packet socket receives packets with pulled ll header,
  107. so that SOCK_RAW should push it back.
  108. On receive:
  109. -----------
  110. Incoming, dev_has_header(dev) == true
  111. mac_header -> ll header
  112. data -> data
  113. Outgoing, dev_has_header(dev) == true
  114. mac_header -> ll header
  115. data -> ll header
  116. Incoming, dev_has_header(dev) == false
  117. mac_header -> data
  118. However drivers often make it point to the ll header.
  119. This is incorrect because the ll header should be invisible to us.
  120. data -> data
  121. Outgoing, dev_has_header(dev) == false
  122. mac_header -> data. ll header is invisible to us.
  123. data -> data
  124. Resume
  125. If dev_has_header(dev) == false we are unable to restore the ll header,
  126. because it is invisible to us.
  127. On transmit:
  128. ------------
  129. dev_has_header(dev) == true
  130. mac_header -> ll header
  131. data -> ll header
  132. dev_has_header(dev) == false (ll header is invisible to us)
  133. mac_header -> data
  134. data -> data
  135. We should set network_header on output to the correct position,
  136. packet classifier depends on it.
  137. */
  138. /* Private packet socket structures. */
  139. /* identical to struct packet_mreq except it has
  140. * a longer address field.
  141. */
  142. struct packet_mreq_max {
  143. int mr_ifindex;
  144. unsigned short mr_type;
  145. unsigned short mr_alen;
  146. unsigned char mr_address[MAX_ADDR_LEN];
  147. };
  148. union tpacket_uhdr {
  149. struct tpacket_hdr *h1;
  150. struct tpacket2_hdr *h2;
  151. struct tpacket3_hdr *h3;
  152. void *raw;
  153. };
  154. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  155. int closing, int tx_ring);
  156. #define V3_ALIGNMENT (8)
  157. #define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
  158. #define BLK_PLUS_PRIV(sz_of_priv) \
  159. (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
  160. #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
  161. #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
  162. #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
  163. #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
  164. #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
  165. #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
  166. struct packet_sock;
  167. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  168. struct packet_type *pt, struct net_device *orig_dev);
  169. static void *packet_previous_frame(struct packet_sock *po,
  170. struct packet_ring_buffer *rb,
  171. int status);
  172. static void packet_increment_head(struct packet_ring_buffer *buff);
  173. static int prb_curr_blk_in_use(struct tpacket_block_desc *);
  174. static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
  175. struct packet_sock *);
  176. static void prb_retire_current_block(struct tpacket_kbdq_core *,
  177. struct packet_sock *, unsigned int status);
  178. static int prb_queue_frozen(struct tpacket_kbdq_core *);
  179. static void prb_open_block(struct tpacket_kbdq_core *,
  180. struct tpacket_block_desc *);
  181. static enum hrtimer_restart prb_retire_rx_blk_timer_expired(struct hrtimer *);
  182. static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
  183. static void prb_clear_rxhash(struct tpacket_kbdq_core *,
  184. struct tpacket3_hdr *);
  185. static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
  186. struct tpacket3_hdr *);
  187. static void packet_flush_mclist(struct sock *sk);
  188. static u16 packet_pick_tx_queue(struct sk_buff *skb);
  189. struct packet_skb_cb {
  190. union {
  191. struct sockaddr_pkt pkt;
  192. union {
  193. /* Trick: alias skb original length with
  194. * ll.sll_family and ll.protocol in order
  195. * to save room.
  196. */
  197. unsigned int origlen;
  198. struct sockaddr_ll ll;
  199. };
  200. } sa;
  201. };
  202. #define vio_le() virtio_legacy_is_little_endian()
  203. #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
  204. #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
  205. #define GET_PBLOCK_DESC(x, bid) \
  206. ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
  207. #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
  208. ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
  209. #define GET_NEXT_PRB_BLK_NUM(x) \
  210. (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
  211. ((x)->kactive_blk_num+1) : 0)
  212. static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
  213. static void __fanout_link(struct sock *sk, struct packet_sock *po);
  214. #ifdef CONFIG_NETFILTER_EGRESS
  215. static noinline struct sk_buff *nf_hook_direct_egress(struct sk_buff *skb)
  216. {
  217. struct sk_buff *next, *head = NULL, *tail;
  218. int rc;
  219. rcu_read_lock();
  220. for (; skb != NULL; skb = next) {
  221. next = skb->next;
  222. skb_mark_not_on_list(skb);
  223. if (!nf_hook_egress(skb, &rc, skb->dev))
  224. continue;
  225. if (!head)
  226. head = skb;
  227. else
  228. tail->next = skb;
  229. tail = skb;
  230. }
  231. rcu_read_unlock();
  232. return head;
  233. }
  234. #endif
  235. static int packet_xmit(const struct packet_sock *po, struct sk_buff *skb)
  236. {
  237. if (!packet_sock_flag(po, PACKET_SOCK_QDISC_BYPASS))
  238. return dev_queue_xmit(skb);
  239. #ifdef CONFIG_NETFILTER_EGRESS
  240. if (nf_hook_egress_active()) {
  241. skb = nf_hook_direct_egress(skb);
  242. if (!skb)
  243. return NET_XMIT_DROP;
  244. }
  245. #endif
  246. return dev_direct_xmit(skb, packet_pick_tx_queue(skb));
  247. }
  248. static struct net_device *packet_cached_dev_get(struct packet_sock *po)
  249. {
  250. struct net_device *dev;
  251. rcu_read_lock();
  252. dev = rcu_dereference(po->cached_dev);
  253. dev_hold(dev);
  254. rcu_read_unlock();
  255. return dev;
  256. }
  257. static void packet_cached_dev_assign(struct packet_sock *po,
  258. struct net_device *dev)
  259. {
  260. rcu_assign_pointer(po->cached_dev, dev);
  261. }
  262. static void packet_cached_dev_reset(struct packet_sock *po)
  263. {
  264. RCU_INIT_POINTER(po->cached_dev, NULL);
  265. }
  266. static u16 packet_pick_tx_queue(struct sk_buff *skb)
  267. {
  268. struct net_device *dev = skb->dev;
  269. const struct net_device_ops *ops = dev->netdev_ops;
  270. int cpu = raw_smp_processor_id();
  271. u16 queue_index;
  272. #ifdef CONFIG_XPS
  273. skb->sender_cpu = cpu + 1;
  274. #endif
  275. skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues);
  276. if (ops->ndo_select_queue) {
  277. queue_index = ops->ndo_select_queue(dev, skb, NULL);
  278. queue_index = netdev_cap_txqueue(dev, queue_index);
  279. } else {
  280. queue_index = netdev_pick_tx(dev, skb, NULL);
  281. }
  282. return queue_index;
  283. }
  284. /* __register_prot_hook must be invoked through register_prot_hook
  285. * or from a context in which asynchronous accesses to the packet
  286. * socket is not possible (packet_create()).
  287. */
  288. static void __register_prot_hook(struct sock *sk)
  289. {
  290. struct packet_sock *po = pkt_sk(sk);
  291. if (!packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  292. if (po->fanout)
  293. __fanout_link(sk, po);
  294. else
  295. dev_add_pack(&po->prot_hook);
  296. sock_hold(sk);
  297. packet_sock_flag_set(po, PACKET_SOCK_RUNNING, 1);
  298. }
  299. }
  300. static void register_prot_hook(struct sock *sk)
  301. {
  302. lockdep_assert_held_once(&pkt_sk(sk)->bind_lock);
  303. __register_prot_hook(sk);
  304. }
  305. /* If the sync parameter is true, we will temporarily drop
  306. * the po->bind_lock and do a synchronize_net to make sure no
  307. * asynchronous packet processing paths still refer to the elements
  308. * of po->prot_hook. If the sync parameter is false, it is the
  309. * callers responsibility to take care of this.
  310. */
  311. static void __unregister_prot_hook(struct sock *sk, bool sync)
  312. {
  313. struct packet_sock *po = pkt_sk(sk);
  314. lockdep_assert_held_once(&po->bind_lock);
  315. packet_sock_flag_set(po, PACKET_SOCK_RUNNING, 0);
  316. if (po->fanout)
  317. __fanout_unlink(sk, po);
  318. else
  319. __dev_remove_pack(&po->prot_hook);
  320. __sock_put(sk);
  321. if (sync) {
  322. spin_unlock(&po->bind_lock);
  323. synchronize_net();
  324. spin_lock(&po->bind_lock);
  325. }
  326. }
  327. static void unregister_prot_hook(struct sock *sk, bool sync)
  328. {
  329. struct packet_sock *po = pkt_sk(sk);
  330. if (packet_sock_flag(po, PACKET_SOCK_RUNNING))
  331. __unregister_prot_hook(sk, sync);
  332. }
  333. static inline struct page * __pure pgv_to_page(void *addr)
  334. {
  335. if (is_vmalloc_addr(addr))
  336. return vmalloc_to_page(addr);
  337. return virt_to_page(addr);
  338. }
  339. static void __packet_set_status(struct packet_sock *po, void *frame, int status)
  340. {
  341. union tpacket_uhdr h;
  342. /* WRITE_ONCE() are paired with READ_ONCE() in __packet_get_status */
  343. h.raw = frame;
  344. switch (po->tp_version) {
  345. case TPACKET_V1:
  346. WRITE_ONCE(h.h1->tp_status, status);
  347. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  348. break;
  349. case TPACKET_V2:
  350. WRITE_ONCE(h.h2->tp_status, status);
  351. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  352. break;
  353. case TPACKET_V3:
  354. WRITE_ONCE(h.h3->tp_status, status);
  355. flush_dcache_page(pgv_to_page(&h.h3->tp_status));
  356. break;
  357. default:
  358. WARN(1, "TPACKET version not supported.\n");
  359. BUG();
  360. }
  361. smp_wmb();
  362. }
  363. static int __packet_get_status(const struct packet_sock *po, void *frame)
  364. {
  365. union tpacket_uhdr h;
  366. smp_rmb();
  367. /* READ_ONCE() are paired with WRITE_ONCE() in __packet_set_status */
  368. h.raw = frame;
  369. switch (po->tp_version) {
  370. case TPACKET_V1:
  371. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  372. return READ_ONCE(h.h1->tp_status);
  373. case TPACKET_V2:
  374. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  375. return READ_ONCE(h.h2->tp_status);
  376. case TPACKET_V3:
  377. flush_dcache_page(pgv_to_page(&h.h3->tp_status));
  378. return READ_ONCE(h.h3->tp_status);
  379. default:
  380. WARN(1, "TPACKET version not supported.\n");
  381. BUG();
  382. return 0;
  383. }
  384. }
  385. static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec64 *ts,
  386. unsigned int flags)
  387. {
  388. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  389. if (shhwtstamps &&
  390. (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  391. ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts))
  392. return TP_STATUS_TS_RAW_HARDWARE;
  393. if ((flags & SOF_TIMESTAMPING_SOFTWARE) &&
  394. ktime_to_timespec64_cond(skb_tstamp(skb), ts))
  395. return TP_STATUS_TS_SOFTWARE;
  396. return 0;
  397. }
  398. static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
  399. struct sk_buff *skb)
  400. {
  401. union tpacket_uhdr h;
  402. struct timespec64 ts;
  403. __u32 ts_status;
  404. if (!(ts_status = tpacket_get_timestamp(skb, &ts, READ_ONCE(po->tp_tstamp))))
  405. return 0;
  406. h.raw = frame;
  407. /*
  408. * versions 1 through 3 overflow the timestamps in y2106, since they
  409. * all store the seconds in a 32-bit unsigned integer.
  410. * If we create a version 4, that should have a 64-bit timestamp,
  411. * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit
  412. * nanoseconds.
  413. */
  414. switch (po->tp_version) {
  415. case TPACKET_V1:
  416. h.h1->tp_sec = ts.tv_sec;
  417. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  418. break;
  419. case TPACKET_V2:
  420. h.h2->tp_sec = ts.tv_sec;
  421. h.h2->tp_nsec = ts.tv_nsec;
  422. break;
  423. case TPACKET_V3:
  424. h.h3->tp_sec = ts.tv_sec;
  425. h.h3->tp_nsec = ts.tv_nsec;
  426. break;
  427. default:
  428. WARN(1, "TPACKET version not supported.\n");
  429. BUG();
  430. }
  431. /* one flush is safe, as both fields always lie on the same cacheline */
  432. flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
  433. smp_wmb();
  434. return ts_status;
  435. }
  436. static void *packet_lookup_frame(const struct packet_sock *po,
  437. const struct packet_ring_buffer *rb,
  438. unsigned int position,
  439. int status)
  440. {
  441. unsigned int pg_vec_pos, frame_offset;
  442. union tpacket_uhdr h;
  443. pg_vec_pos = position / rb->frames_per_block;
  444. frame_offset = position % rb->frames_per_block;
  445. h.raw = rb->pg_vec[pg_vec_pos].buffer +
  446. (frame_offset * rb->frame_size);
  447. if (status != __packet_get_status(po, h.raw))
  448. return NULL;
  449. return h.raw;
  450. }
  451. static void *packet_current_frame(struct packet_sock *po,
  452. struct packet_ring_buffer *rb,
  453. int status)
  454. {
  455. return packet_lookup_frame(po, rb, rb->head, status);
  456. }
  457. static u16 vlan_get_tci(const struct sk_buff *skb, struct net_device *dev)
  458. {
  459. struct vlan_hdr vhdr, *vh;
  460. unsigned int header_len;
  461. if (!dev)
  462. return 0;
  463. /* In the SOCK_DGRAM scenario, skb data starts at the network
  464. * protocol, which is after the VLAN headers. The outer VLAN
  465. * header is at the hard_header_len offset in non-variable
  466. * length link layer headers. If it's a VLAN device, the
  467. * min_header_len should be used to exclude the VLAN header
  468. * size.
  469. */
  470. if (dev->min_header_len == dev->hard_header_len)
  471. header_len = dev->hard_header_len;
  472. else if (is_vlan_dev(dev))
  473. header_len = dev->min_header_len;
  474. else
  475. return 0;
  476. vh = skb_header_pointer(skb, skb_mac_offset(skb) + header_len,
  477. sizeof(vhdr), &vhdr);
  478. if (unlikely(!vh))
  479. return 0;
  480. return ntohs(vh->h_vlan_TCI);
  481. }
  482. static __be16 vlan_get_protocol_dgram(const struct sk_buff *skb)
  483. {
  484. __be16 proto = skb->protocol;
  485. if (unlikely(eth_type_vlan(proto)))
  486. proto = vlan_get_protocol_offset_inline(skb, proto,
  487. skb_mac_offset(skb),
  488. NULL);
  489. return proto;
  490. }
  491. static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
  492. struct sk_buff_head *rb_queue)
  493. {
  494. struct tpacket_kbdq_core *pkc;
  495. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  496. hrtimer_cancel(&pkc->retire_blk_timer);
  497. }
  498. static int prb_calc_retire_blk_tmo(struct packet_sock *po,
  499. int blk_size_in_bytes)
  500. {
  501. struct net_device *dev;
  502. unsigned int mbits, div;
  503. struct ethtool_link_ksettings ecmd;
  504. int err;
  505. rtnl_lock();
  506. dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
  507. if (unlikely(!dev)) {
  508. rtnl_unlock();
  509. return DEFAULT_PRB_RETIRE_TOV;
  510. }
  511. err = __ethtool_get_link_ksettings(dev, &ecmd);
  512. rtnl_unlock();
  513. if (err)
  514. return DEFAULT_PRB_RETIRE_TOV;
  515. /* If the link speed is so slow you don't really
  516. * need to worry about perf anyways
  517. */
  518. if (ecmd.base.speed < SPEED_1000 ||
  519. ecmd.base.speed == SPEED_UNKNOWN)
  520. return DEFAULT_PRB_RETIRE_TOV;
  521. div = ecmd.base.speed / 1000;
  522. mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
  523. if (div)
  524. mbits /= div;
  525. if (div)
  526. return mbits + 1;
  527. return mbits;
  528. }
  529. static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
  530. union tpacket_req_u *req_u)
  531. {
  532. p1->feature_req_word = req_u->req3.tp_feature_req_word;
  533. }
  534. static void init_prb_bdqc(struct packet_sock *po,
  535. struct packet_ring_buffer *rb,
  536. struct pgv *pg_vec,
  537. union tpacket_req_u *req_u)
  538. {
  539. struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
  540. struct tpacket_block_desc *pbd;
  541. memset(p1, 0x0, sizeof(*p1));
  542. p1->knxt_seq_num = 1;
  543. p1->pkbdq = pg_vec;
  544. pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
  545. p1->pkblk_start = pg_vec[0].buffer;
  546. p1->kblk_size = req_u->req3.tp_block_size;
  547. p1->knum_blocks = req_u->req3.tp_block_nr;
  548. p1->hdrlen = po->tp_hdrlen;
  549. p1->version = po->tp_version;
  550. po->stats.stats3.tp_freeze_q_cnt = 0;
  551. if (req_u->req3.tp_retire_blk_tov)
  552. p1->interval_ktime = ms_to_ktime(req_u->req3.tp_retire_blk_tov);
  553. else
  554. p1->interval_ktime = ms_to_ktime(prb_calc_retire_blk_tmo(po,
  555. req_u->req3.tp_block_size));
  556. p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
  557. rwlock_init(&p1->blk_fill_in_prog_lock);
  558. p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv);
  559. prb_init_ft_ops(p1, req_u);
  560. hrtimer_setup(&p1->retire_blk_timer, prb_retire_rx_blk_timer_expired,
  561. CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  562. hrtimer_start(&p1->retire_blk_timer, p1->interval_ktime,
  563. HRTIMER_MODE_REL_SOFT);
  564. prb_open_block(p1, pbd);
  565. }
  566. /*
  567. * With a 1MB block-size, on a 1Gbps line, it will take
  568. * i) ~8 ms to fill a block + ii) memcpy etc.
  569. * In this cut we are not accounting for the memcpy time.
  570. *
  571. * Since the tmo granularity is in msecs, it is not too expensive
  572. * to refresh the timer, lets say every '8' msecs.
  573. * Either the user can set the 'tmo' or we can derive it based on
  574. * a) line-speed and b) block-size.
  575. * prb_calc_retire_blk_tmo() calculates the tmo.
  576. */
  577. static enum hrtimer_restart prb_retire_rx_blk_timer_expired(struct hrtimer *t)
  578. {
  579. struct packet_sock *po =
  580. timer_container_of(po, t, rx_ring.prb_bdqc.retire_blk_timer);
  581. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  582. unsigned int frozen;
  583. struct tpacket_block_desc *pbd;
  584. spin_lock(&po->sk.sk_receive_queue.lock);
  585. frozen = prb_queue_frozen(pkc);
  586. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  587. /* We only need to plug the race when the block is partially filled.
  588. * tpacket_rcv:
  589. * lock(); increment BLOCK_NUM_PKTS; unlock()
  590. * copy_bits() is in progress ...
  591. * timer fires on other cpu:
  592. * we can't retire the current block because copy_bits
  593. * is in progress.
  594. *
  595. */
  596. if (BLOCK_NUM_PKTS(pbd)) {
  597. /* Waiting for skb_copy_bits to finish... */
  598. write_lock(&pkc->blk_fill_in_prog_lock);
  599. write_unlock(&pkc->blk_fill_in_prog_lock);
  600. }
  601. if (!frozen) {
  602. if (BLOCK_NUM_PKTS(pbd)) {
  603. /* Not an empty block. Need retire the block. */
  604. prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
  605. prb_dispatch_next_block(pkc, po);
  606. }
  607. } else {
  608. /* Case 1. Queue was frozen because user-space was
  609. * lagging behind.
  610. */
  611. if (!prb_curr_blk_in_use(pbd)) {
  612. /* Case 2. queue was frozen,user-space caught up,
  613. * now the link went idle && the timer fired.
  614. * We don't have a block to close.So we open this
  615. * block and restart the timer.
  616. * opening a block thaws the queue,restarts timer
  617. * Thawing/timer-refresh is a side effect.
  618. */
  619. prb_open_block(pkc, pbd);
  620. }
  621. }
  622. hrtimer_forward_now(&pkc->retire_blk_timer, pkc->interval_ktime);
  623. spin_unlock(&po->sk.sk_receive_queue.lock);
  624. return HRTIMER_RESTART;
  625. }
  626. static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
  627. struct tpacket_block_desc *pbd1, __u32 status)
  628. {
  629. /* Flush everything minus the block header */
  630. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  631. u8 *start, *end;
  632. start = (u8 *)pbd1;
  633. /* Skip the block header(we know header WILL fit in 4K) */
  634. start += PAGE_SIZE;
  635. end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
  636. for (; start < end; start += PAGE_SIZE)
  637. flush_dcache_page(pgv_to_page(start));
  638. smp_wmb();
  639. #endif
  640. /* Now update the block status. */
  641. BLOCK_STATUS(pbd1) = status;
  642. /* Flush the block header */
  643. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  644. start = (u8 *)pbd1;
  645. flush_dcache_page(pgv_to_page(start));
  646. smp_wmb();
  647. #endif
  648. }
  649. /*
  650. * Side effect:
  651. *
  652. * 1) flush the block
  653. * 2) Increment active_blk_num
  654. *
  655. * Note:We DONT refresh the timer on purpose.
  656. * Because almost always the next block will be opened.
  657. */
  658. static void prb_close_block(struct tpacket_kbdq_core *pkc1,
  659. struct tpacket_block_desc *pbd1,
  660. struct packet_sock *po, unsigned int stat)
  661. {
  662. __u32 status = TP_STATUS_USER | stat;
  663. struct tpacket3_hdr *last_pkt;
  664. struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
  665. struct sock *sk = &po->sk;
  666. if (atomic_read(&po->tp_drops))
  667. status |= TP_STATUS_LOSING;
  668. last_pkt = (struct tpacket3_hdr *)pkc1->prev;
  669. last_pkt->tp_next_offset = 0;
  670. /* Get the ts of the last pkt */
  671. if (BLOCK_NUM_PKTS(pbd1)) {
  672. h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
  673. h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
  674. } else {
  675. /* Ok, we tmo'd - so get the current time.
  676. *
  677. * It shouldn't really happen as we don't close empty
  678. * blocks. See prb_retire_rx_blk_timer_expired().
  679. */
  680. struct timespec64 ts;
  681. ktime_get_real_ts64(&ts);
  682. h1->ts_last_pkt.ts_sec = ts.tv_sec;
  683. h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
  684. }
  685. smp_wmb();
  686. /* Flush the block */
  687. prb_flush_block(pkc1, pbd1, status);
  688. sk->sk_data_ready(sk);
  689. pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
  690. }
  691. static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
  692. {
  693. pkc->reset_pending_on_curr_blk = 0;
  694. }
  695. /*
  696. * prb_open_block is called by tpacket_rcv or timer callback.
  697. *
  698. * Reasons why NOT update hrtimer in prb_open_block:
  699. * 1) It will increase complexity to distinguish the two caller scenario.
  700. * 2) hrtimer_cancel and hrtimer_start need to be called if you want to update
  701. * TMO of an already enqueued hrtimer, leading to complex shutdown logic.
  702. *
  703. * One side effect of NOT update hrtimer when called by tpacket_rcv is that
  704. * a newly opened block triggered by tpacket_rcv may be retired earlier than
  705. * expected. On the other hand, if timeout is updated in prb_open_block, the
  706. * frequent reception of network packets that leads to prb_open_block being
  707. * called may cause hrtimer to be removed and enqueued repeatedly.
  708. */
  709. static void prb_open_block(struct tpacket_kbdq_core *pkc1,
  710. struct tpacket_block_desc *pbd1)
  711. {
  712. struct timespec64 ts;
  713. struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
  714. smp_rmb();
  715. /* We could have just memset this but we will lose the
  716. * flexibility of making the priv area sticky
  717. */
  718. BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
  719. BLOCK_NUM_PKTS(pbd1) = 0;
  720. BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  721. ktime_get_real_ts64(&ts);
  722. h1->ts_first_pkt.ts_sec = ts.tv_sec;
  723. h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
  724. pkc1->pkblk_start = (char *)pbd1;
  725. pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  726. BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
  727. BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
  728. pbd1->version = pkc1->version;
  729. pkc1->prev = pkc1->nxt_offset;
  730. pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
  731. prb_thaw_queue(pkc1);
  732. smp_wmb();
  733. }
  734. /*
  735. * Queue freeze logic:
  736. * 1) Assume tp_block_nr = 8 blocks.
  737. * 2) At time 't0', user opens Rx ring.
  738. * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
  739. * 4) user-space is either sleeping or processing block '0'.
  740. * 5) tpacket_rcv is currently filling block '7', since there is no space left,
  741. * it will close block-7,loop around and try to fill block '0'.
  742. * call-flow:
  743. * __packet_lookup_frame_in_block
  744. * prb_retire_current_block()
  745. * prb_dispatch_next_block()
  746. * |->(BLOCK_STATUS == USER) evaluates to true
  747. * 5.1) Since block-0 is currently in-use, we just freeze the queue.
  748. * 6) Now there are two cases:
  749. * 6.1) Link goes idle right after the queue is frozen.
  750. * But remember, the last open_block() refreshed the timer.
  751. * When this timer expires,it will refresh itself so that we can
  752. * re-open block-0 in near future.
  753. * 6.2) Link is busy and keeps on receiving packets. This is a simple
  754. * case and __packet_lookup_frame_in_block will check if block-0
  755. * is free and can now be re-used.
  756. */
  757. static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
  758. struct packet_sock *po)
  759. {
  760. pkc->reset_pending_on_curr_blk = 1;
  761. po->stats.stats3.tp_freeze_q_cnt++;
  762. }
  763. #define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
  764. /*
  765. * If the next block is free then we will dispatch it
  766. * and return a good offset.
  767. * Else, we will freeze the queue.
  768. * So, caller must check the return value.
  769. */
  770. static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
  771. struct packet_sock *po)
  772. {
  773. struct tpacket_block_desc *pbd;
  774. smp_rmb();
  775. /* 1. Get current block num */
  776. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  777. /* 2. If this block is currently in_use then freeze the queue */
  778. if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
  779. prb_freeze_queue(pkc, po);
  780. return NULL;
  781. }
  782. /*
  783. * 3.
  784. * open this block and return the offset where the first packet
  785. * needs to get stored.
  786. */
  787. prb_open_block(pkc, pbd);
  788. return (void *)pkc->nxt_offset;
  789. }
  790. static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
  791. struct packet_sock *po, unsigned int status)
  792. {
  793. struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  794. /* retire/close the current block */
  795. if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
  796. /*
  797. * Plug the case where copy_bits() is in progress on
  798. * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
  799. * have space to copy the pkt in the current block and
  800. * called prb_retire_current_block()
  801. *
  802. * We don't need to worry about the TMO case because
  803. * the timer-handler already handled this case.
  804. */
  805. if (!(status & TP_STATUS_BLK_TMO)) {
  806. /* Waiting for skb_copy_bits to finish... */
  807. write_lock(&pkc->blk_fill_in_prog_lock);
  808. write_unlock(&pkc->blk_fill_in_prog_lock);
  809. }
  810. prb_close_block(pkc, pbd, po, status);
  811. return;
  812. }
  813. }
  814. static int prb_curr_blk_in_use(struct tpacket_block_desc *pbd)
  815. {
  816. return TP_STATUS_USER & BLOCK_STATUS(pbd);
  817. }
  818. static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
  819. {
  820. return pkc->reset_pending_on_curr_blk;
  821. }
  822. static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
  823. __releases(&pkc->blk_fill_in_prog_lock)
  824. {
  825. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
  826. read_unlock(&pkc->blk_fill_in_prog_lock);
  827. }
  828. static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
  829. struct tpacket3_hdr *ppd)
  830. {
  831. ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb);
  832. }
  833. static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
  834. struct tpacket3_hdr *ppd)
  835. {
  836. ppd->hv1.tp_rxhash = 0;
  837. }
  838. static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
  839. struct tpacket3_hdr *ppd)
  840. {
  841. struct packet_sock *po = container_of(pkc, struct packet_sock, rx_ring.prb_bdqc);
  842. if (skb_vlan_tag_present(pkc->skb)) {
  843. ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb);
  844. ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto);
  845. ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  846. } else if (unlikely(po->sk.sk_type == SOCK_DGRAM && eth_type_vlan(pkc->skb->protocol))) {
  847. ppd->hv1.tp_vlan_tci = vlan_get_tci(pkc->skb, pkc->skb->dev);
  848. ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->protocol);
  849. ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  850. } else {
  851. ppd->hv1.tp_vlan_tci = 0;
  852. ppd->hv1.tp_vlan_tpid = 0;
  853. ppd->tp_status = TP_STATUS_AVAILABLE;
  854. }
  855. }
  856. static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
  857. struct tpacket3_hdr *ppd)
  858. {
  859. ppd->hv1.tp_padding = 0;
  860. prb_fill_vlan_info(pkc, ppd);
  861. if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
  862. prb_fill_rxhash(pkc, ppd);
  863. else
  864. prb_clear_rxhash(pkc, ppd);
  865. }
  866. static void prb_fill_curr_block(char *curr,
  867. struct tpacket_kbdq_core *pkc,
  868. struct tpacket_block_desc *pbd,
  869. unsigned int len)
  870. __acquires(&pkc->blk_fill_in_prog_lock)
  871. {
  872. struct tpacket3_hdr *ppd;
  873. ppd = (struct tpacket3_hdr *)curr;
  874. ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
  875. pkc->prev = curr;
  876. pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
  877. BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
  878. BLOCK_NUM_PKTS(pbd) += 1;
  879. read_lock(&pkc->blk_fill_in_prog_lock);
  880. prb_run_all_ft_ops(pkc, ppd);
  881. }
  882. /* Assumes caller has the sk->rx_queue.lock */
  883. static void *__packet_lookup_frame_in_block(struct packet_sock *po,
  884. struct sk_buff *skb,
  885. unsigned int len
  886. )
  887. {
  888. struct tpacket_kbdq_core *pkc;
  889. struct tpacket_block_desc *pbd;
  890. char *curr, *end;
  891. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  892. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  893. /* Queue is frozen when user space is lagging behind */
  894. if (prb_queue_frozen(pkc)) {
  895. /*
  896. * Check if that last block which caused the queue to freeze,
  897. * is still in_use by user-space.
  898. */
  899. if (prb_curr_blk_in_use(pbd)) {
  900. /* Can't record this packet */
  901. return NULL;
  902. } else {
  903. /*
  904. * Ok, the block was released by user-space.
  905. * Now let's open that block.
  906. * opening a block also thaws the queue.
  907. * Thawing is a side effect.
  908. */
  909. prb_open_block(pkc, pbd);
  910. }
  911. }
  912. smp_mb();
  913. curr = pkc->nxt_offset;
  914. pkc->skb = skb;
  915. end = (char *)pbd + pkc->kblk_size;
  916. /* first try the current block */
  917. if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
  918. prb_fill_curr_block(curr, pkc, pbd, len);
  919. return (void *)curr;
  920. }
  921. /* Ok, close the current block */
  922. prb_retire_current_block(pkc, po, 0);
  923. /* Now, try to dispatch the next block */
  924. curr = (char *)prb_dispatch_next_block(pkc, po);
  925. if (curr) {
  926. pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
  927. prb_fill_curr_block(curr, pkc, pbd, len);
  928. return (void *)curr;
  929. }
  930. /*
  931. * No free blocks are available.user_space hasn't caught up yet.
  932. * Queue was just frozen and now this packet will get dropped.
  933. */
  934. return NULL;
  935. }
  936. static void *packet_current_rx_frame(struct packet_sock *po,
  937. struct sk_buff *skb,
  938. int status, unsigned int len)
  939. {
  940. char *curr = NULL;
  941. switch (po->tp_version) {
  942. case TPACKET_V1:
  943. case TPACKET_V2:
  944. curr = packet_lookup_frame(po, &po->rx_ring,
  945. po->rx_ring.head, status);
  946. return curr;
  947. case TPACKET_V3:
  948. return __packet_lookup_frame_in_block(po, skb, len);
  949. default:
  950. WARN(1, "TPACKET version not supported\n");
  951. BUG();
  952. return NULL;
  953. }
  954. }
  955. static void *prb_lookup_block(const struct packet_sock *po,
  956. const struct packet_ring_buffer *rb,
  957. unsigned int idx,
  958. int status)
  959. {
  960. struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
  961. struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
  962. if (status != BLOCK_STATUS(pbd))
  963. return NULL;
  964. return pbd;
  965. }
  966. static int prb_previous_blk_num(struct packet_ring_buffer *rb)
  967. {
  968. unsigned int prev;
  969. if (rb->prb_bdqc.kactive_blk_num)
  970. prev = rb->prb_bdqc.kactive_blk_num-1;
  971. else
  972. prev = rb->prb_bdqc.knum_blocks-1;
  973. return prev;
  974. }
  975. /* Assumes caller has held the rx_queue.lock */
  976. static void *__prb_previous_block(struct packet_sock *po,
  977. struct packet_ring_buffer *rb,
  978. int status)
  979. {
  980. unsigned int previous = prb_previous_blk_num(rb);
  981. return prb_lookup_block(po, rb, previous, status);
  982. }
  983. static void *packet_previous_rx_frame(struct packet_sock *po,
  984. struct packet_ring_buffer *rb,
  985. int status)
  986. {
  987. if (po->tp_version <= TPACKET_V2)
  988. return packet_previous_frame(po, rb, status);
  989. return __prb_previous_block(po, rb, status);
  990. }
  991. static void packet_increment_rx_head(struct packet_sock *po,
  992. struct packet_ring_buffer *rb)
  993. {
  994. switch (po->tp_version) {
  995. case TPACKET_V1:
  996. case TPACKET_V2:
  997. return packet_increment_head(rb);
  998. case TPACKET_V3:
  999. default:
  1000. WARN(1, "TPACKET version not supported.\n");
  1001. BUG();
  1002. return;
  1003. }
  1004. }
  1005. static void *packet_previous_frame(struct packet_sock *po,
  1006. struct packet_ring_buffer *rb,
  1007. int status)
  1008. {
  1009. unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
  1010. return packet_lookup_frame(po, rb, previous, status);
  1011. }
  1012. static void packet_increment_head(struct packet_ring_buffer *buff)
  1013. {
  1014. buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
  1015. }
  1016. static void packet_inc_pending(struct packet_ring_buffer *rb)
  1017. {
  1018. this_cpu_inc(*rb->pending_refcnt);
  1019. }
  1020. static void packet_dec_pending(struct packet_ring_buffer *rb)
  1021. {
  1022. this_cpu_dec(*rb->pending_refcnt);
  1023. }
  1024. static unsigned int packet_read_pending(const struct packet_ring_buffer *rb)
  1025. {
  1026. unsigned int refcnt = 0;
  1027. int cpu;
  1028. /* We don't use pending refcount in rx_ring. */
  1029. if (rb->pending_refcnt == NULL)
  1030. return 0;
  1031. for_each_possible_cpu(cpu)
  1032. refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu);
  1033. return refcnt;
  1034. }
  1035. static int packet_alloc_pending(struct packet_sock *po)
  1036. {
  1037. po->rx_ring.pending_refcnt = NULL;
  1038. po->tx_ring.pending_refcnt = alloc_percpu(unsigned int);
  1039. if (unlikely(po->tx_ring.pending_refcnt == NULL))
  1040. return -ENOBUFS;
  1041. return 0;
  1042. }
  1043. static void packet_free_pending(struct packet_sock *po)
  1044. {
  1045. free_percpu(po->tx_ring.pending_refcnt);
  1046. }
  1047. #define ROOM_POW_OFF 2
  1048. #define ROOM_NONE 0x0
  1049. #define ROOM_LOW 0x1
  1050. #define ROOM_NORMAL 0x2
  1051. static bool __tpacket_has_room(const struct packet_sock *po, int pow_off)
  1052. {
  1053. int idx, len;
  1054. len = READ_ONCE(po->rx_ring.frame_max) + 1;
  1055. idx = READ_ONCE(po->rx_ring.head);
  1056. if (pow_off)
  1057. idx += len >> pow_off;
  1058. if (idx >= len)
  1059. idx -= len;
  1060. return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
  1061. }
  1062. static bool __tpacket_v3_has_room(const struct packet_sock *po, int pow_off)
  1063. {
  1064. int idx, len;
  1065. len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks);
  1066. idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num);
  1067. if (pow_off)
  1068. idx += len >> pow_off;
  1069. if (idx >= len)
  1070. idx -= len;
  1071. return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL);
  1072. }
  1073. static int __packet_rcv_has_room(const struct packet_sock *po,
  1074. const struct sk_buff *skb)
  1075. {
  1076. const struct sock *sk = &po->sk;
  1077. int ret = ROOM_NONE;
  1078. if (po->prot_hook.func != tpacket_rcv) {
  1079. int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
  1080. int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc)
  1081. - (skb ? skb->truesize : 0);
  1082. if (avail > (rcvbuf >> ROOM_POW_OFF))
  1083. return ROOM_NORMAL;
  1084. else if (avail > 0)
  1085. return ROOM_LOW;
  1086. else
  1087. return ROOM_NONE;
  1088. }
  1089. if (po->tp_version == TPACKET_V3) {
  1090. if (__tpacket_v3_has_room(po, ROOM_POW_OFF))
  1091. ret = ROOM_NORMAL;
  1092. else if (__tpacket_v3_has_room(po, 0))
  1093. ret = ROOM_LOW;
  1094. } else {
  1095. if (__tpacket_has_room(po, ROOM_POW_OFF))
  1096. ret = ROOM_NORMAL;
  1097. else if (__tpacket_has_room(po, 0))
  1098. ret = ROOM_LOW;
  1099. }
  1100. return ret;
  1101. }
  1102. static int packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
  1103. {
  1104. bool pressure;
  1105. int ret;
  1106. ret = __packet_rcv_has_room(po, skb);
  1107. pressure = ret != ROOM_NORMAL;
  1108. if (packet_sock_flag(po, PACKET_SOCK_PRESSURE) != pressure)
  1109. packet_sock_flag_set(po, PACKET_SOCK_PRESSURE, pressure);
  1110. return ret;
  1111. }
  1112. static void packet_rcv_try_clear_pressure(struct packet_sock *po)
  1113. {
  1114. if (packet_sock_flag(po, PACKET_SOCK_PRESSURE) &&
  1115. __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
  1116. packet_sock_flag_set(po, PACKET_SOCK_PRESSURE, false);
  1117. }
  1118. static void packet_sock_destruct(struct sock *sk)
  1119. {
  1120. skb_queue_purge(&sk->sk_error_queue);
  1121. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  1122. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  1123. if (!sock_flag(sk, SOCK_DEAD)) {
  1124. pr_err("Attempt to release alive packet socket: %p\n", sk);
  1125. return;
  1126. }
  1127. }
  1128. static bool fanout_flow_is_huge(struct packet_sock *po, struct sk_buff *skb)
  1129. {
  1130. u32 *history = po->rollover->history;
  1131. u32 victim, rxhash;
  1132. int i, count = 0;
  1133. rxhash = skb_get_hash(skb);
  1134. for (i = 0; i < ROLLOVER_HLEN; i++)
  1135. if (READ_ONCE(history[i]) == rxhash)
  1136. count++;
  1137. victim = get_random_u32_below(ROLLOVER_HLEN);
  1138. /* Avoid dirtying the cache line if possible */
  1139. if (READ_ONCE(history[victim]) != rxhash)
  1140. WRITE_ONCE(history[victim], rxhash);
  1141. return count > (ROLLOVER_HLEN >> 1);
  1142. }
  1143. static unsigned int fanout_demux_hash(struct packet_fanout *f,
  1144. struct sk_buff *skb,
  1145. unsigned int num)
  1146. {
  1147. return reciprocal_scale(__skb_get_hash_symmetric(skb), num);
  1148. }
  1149. static unsigned int fanout_demux_lb(struct packet_fanout *f,
  1150. struct sk_buff *skb,
  1151. unsigned int num)
  1152. {
  1153. unsigned int val = atomic_inc_return(&f->rr_cur);
  1154. return val % num;
  1155. }
  1156. static unsigned int fanout_demux_cpu(struct packet_fanout *f,
  1157. struct sk_buff *skb,
  1158. unsigned int num)
  1159. {
  1160. return smp_processor_id() % num;
  1161. }
  1162. static unsigned int fanout_demux_rnd(struct packet_fanout *f,
  1163. struct sk_buff *skb,
  1164. unsigned int num)
  1165. {
  1166. return get_random_u32_below(num);
  1167. }
  1168. static unsigned int fanout_demux_rollover(struct packet_fanout *f,
  1169. struct sk_buff *skb,
  1170. unsigned int idx, bool try_self,
  1171. unsigned int num)
  1172. {
  1173. struct packet_sock *po, *po_next, *po_skip = NULL;
  1174. unsigned int i, j, room = ROOM_NONE;
  1175. po = pkt_sk(rcu_dereference(f->arr[idx]));
  1176. if (try_self) {
  1177. room = packet_rcv_has_room(po, skb);
  1178. if (room == ROOM_NORMAL ||
  1179. (room == ROOM_LOW && !fanout_flow_is_huge(po, skb)))
  1180. return idx;
  1181. po_skip = po;
  1182. }
  1183. i = j = min_t(int, po->rollover->sock, num - 1);
  1184. do {
  1185. po_next = pkt_sk(rcu_dereference(f->arr[i]));
  1186. if (po_next != po_skip &&
  1187. !packet_sock_flag(po_next, PACKET_SOCK_PRESSURE) &&
  1188. packet_rcv_has_room(po_next, skb) == ROOM_NORMAL) {
  1189. if (i != j)
  1190. po->rollover->sock = i;
  1191. atomic_long_inc(&po->rollover->num);
  1192. if (room == ROOM_LOW)
  1193. atomic_long_inc(&po->rollover->num_huge);
  1194. return i;
  1195. }
  1196. if (++i == num)
  1197. i = 0;
  1198. } while (i != j);
  1199. atomic_long_inc(&po->rollover->num_failed);
  1200. return idx;
  1201. }
  1202. static unsigned int fanout_demux_qm(struct packet_fanout *f,
  1203. struct sk_buff *skb,
  1204. unsigned int num)
  1205. {
  1206. return skb_get_queue_mapping(skb) % num;
  1207. }
  1208. static unsigned int fanout_demux_bpf(struct packet_fanout *f,
  1209. struct sk_buff *skb,
  1210. unsigned int num)
  1211. {
  1212. struct bpf_prog *prog;
  1213. unsigned int ret = 0;
  1214. rcu_read_lock();
  1215. prog = rcu_dereference(f->bpf_prog);
  1216. if (prog)
  1217. ret = bpf_prog_run_clear_cb(prog, skb) % num;
  1218. rcu_read_unlock();
  1219. return ret;
  1220. }
  1221. static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
  1222. {
  1223. return f->flags & (flag >> 8);
  1224. }
  1225. static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
  1226. struct packet_type *pt, struct net_device *orig_dev)
  1227. {
  1228. struct packet_fanout *f = pt->af_packet_priv;
  1229. unsigned int num = READ_ONCE(f->num_members);
  1230. struct net *net = read_pnet(&f->net);
  1231. struct packet_sock *po;
  1232. unsigned int idx;
  1233. if (!net_eq(dev_net(dev), net) || !num) {
  1234. kfree_skb(skb);
  1235. return 0;
  1236. }
  1237. if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
  1238. skb = ip_check_defrag(net, skb, IP_DEFRAG_AF_PACKET);
  1239. if (!skb)
  1240. return 0;
  1241. }
  1242. switch (f->type) {
  1243. case PACKET_FANOUT_HASH:
  1244. default:
  1245. idx = fanout_demux_hash(f, skb, num);
  1246. break;
  1247. case PACKET_FANOUT_LB:
  1248. idx = fanout_demux_lb(f, skb, num);
  1249. break;
  1250. case PACKET_FANOUT_CPU:
  1251. idx = fanout_demux_cpu(f, skb, num);
  1252. break;
  1253. case PACKET_FANOUT_RND:
  1254. idx = fanout_demux_rnd(f, skb, num);
  1255. break;
  1256. case PACKET_FANOUT_QM:
  1257. idx = fanout_demux_qm(f, skb, num);
  1258. break;
  1259. case PACKET_FANOUT_ROLLOVER:
  1260. idx = fanout_demux_rollover(f, skb, 0, false, num);
  1261. break;
  1262. case PACKET_FANOUT_CBPF:
  1263. case PACKET_FANOUT_EBPF:
  1264. idx = fanout_demux_bpf(f, skb, num);
  1265. break;
  1266. }
  1267. if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
  1268. idx = fanout_demux_rollover(f, skb, idx, true, num);
  1269. po = pkt_sk(rcu_dereference(f->arr[idx]));
  1270. return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
  1271. }
  1272. DEFINE_MUTEX(fanout_mutex);
  1273. EXPORT_SYMBOL_GPL(fanout_mutex);
  1274. static LIST_HEAD(fanout_list);
  1275. static u16 fanout_next_id;
  1276. static void __fanout_link(struct sock *sk, struct packet_sock *po)
  1277. {
  1278. struct packet_fanout *f = po->fanout;
  1279. spin_lock(&f->lock);
  1280. rcu_assign_pointer(f->arr[f->num_members], sk);
  1281. smp_wmb();
  1282. f->num_members++;
  1283. if (f->num_members == 1)
  1284. dev_add_pack(&f->prot_hook);
  1285. spin_unlock(&f->lock);
  1286. }
  1287. static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
  1288. {
  1289. struct packet_fanout *f = po->fanout;
  1290. int i;
  1291. spin_lock(&f->lock);
  1292. for (i = 0; i < f->num_members; i++) {
  1293. if (rcu_dereference_protected(f->arr[i],
  1294. lockdep_is_held(&f->lock)) == sk)
  1295. break;
  1296. }
  1297. BUG_ON(i >= f->num_members);
  1298. rcu_assign_pointer(f->arr[i],
  1299. rcu_dereference_protected(f->arr[f->num_members - 1],
  1300. lockdep_is_held(&f->lock)));
  1301. f->num_members--;
  1302. if (f->num_members == 0)
  1303. __dev_remove_pack(&f->prot_hook);
  1304. spin_unlock(&f->lock);
  1305. }
  1306. static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
  1307. {
  1308. if (sk->sk_family != PF_PACKET)
  1309. return false;
  1310. return ptype->af_packet_priv == pkt_sk(sk)->fanout;
  1311. }
  1312. static void fanout_init_data(struct packet_fanout *f)
  1313. {
  1314. switch (f->type) {
  1315. case PACKET_FANOUT_LB:
  1316. atomic_set(&f->rr_cur, 0);
  1317. break;
  1318. case PACKET_FANOUT_CBPF:
  1319. case PACKET_FANOUT_EBPF:
  1320. RCU_INIT_POINTER(f->bpf_prog, NULL);
  1321. break;
  1322. }
  1323. }
  1324. static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
  1325. {
  1326. struct bpf_prog *old;
  1327. spin_lock(&f->lock);
  1328. old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
  1329. rcu_assign_pointer(f->bpf_prog, new);
  1330. spin_unlock(&f->lock);
  1331. if (old) {
  1332. synchronize_net();
  1333. bpf_prog_destroy(old);
  1334. }
  1335. }
  1336. static int fanout_set_data_cbpf(struct packet_sock *po, sockptr_t data,
  1337. unsigned int len)
  1338. {
  1339. struct bpf_prog *new;
  1340. struct sock_fprog fprog;
  1341. int ret;
  1342. if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
  1343. return -EPERM;
  1344. ret = copy_bpf_fprog_from_user(&fprog, data, len);
  1345. if (ret)
  1346. return ret;
  1347. ret = bpf_prog_create_from_user(&new, &fprog, NULL, false);
  1348. if (ret)
  1349. return ret;
  1350. __fanout_set_data_bpf(po->fanout, new);
  1351. return 0;
  1352. }
  1353. static int fanout_set_data_ebpf(struct packet_sock *po, sockptr_t data,
  1354. unsigned int len)
  1355. {
  1356. struct bpf_prog *new;
  1357. u32 fd;
  1358. if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
  1359. return -EPERM;
  1360. if (len != sizeof(fd))
  1361. return -EINVAL;
  1362. if (copy_from_sockptr(&fd, data, len))
  1363. return -EFAULT;
  1364. new = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
  1365. if (IS_ERR(new))
  1366. return PTR_ERR(new);
  1367. __fanout_set_data_bpf(po->fanout, new);
  1368. return 0;
  1369. }
  1370. static int fanout_set_data(struct packet_sock *po, sockptr_t data,
  1371. unsigned int len)
  1372. {
  1373. switch (po->fanout->type) {
  1374. case PACKET_FANOUT_CBPF:
  1375. return fanout_set_data_cbpf(po, data, len);
  1376. case PACKET_FANOUT_EBPF:
  1377. return fanout_set_data_ebpf(po, data, len);
  1378. default:
  1379. return -EINVAL;
  1380. }
  1381. }
  1382. static void fanout_release_data(struct packet_fanout *f)
  1383. {
  1384. switch (f->type) {
  1385. case PACKET_FANOUT_CBPF:
  1386. case PACKET_FANOUT_EBPF:
  1387. __fanout_set_data_bpf(f, NULL);
  1388. }
  1389. }
  1390. static bool __fanout_id_is_free(struct sock *sk, u16 candidate_id)
  1391. {
  1392. struct packet_fanout *f;
  1393. list_for_each_entry(f, &fanout_list, list) {
  1394. if (f->id == candidate_id &&
  1395. read_pnet(&f->net) == sock_net(sk)) {
  1396. return false;
  1397. }
  1398. }
  1399. return true;
  1400. }
  1401. static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
  1402. {
  1403. u16 id = fanout_next_id;
  1404. do {
  1405. if (__fanout_id_is_free(sk, id)) {
  1406. *new_id = id;
  1407. fanout_next_id = id + 1;
  1408. return true;
  1409. }
  1410. id++;
  1411. } while (id != fanout_next_id);
  1412. return false;
  1413. }
  1414. static int fanout_add(struct sock *sk, struct fanout_args *args)
  1415. {
  1416. struct packet_rollover *rollover = NULL;
  1417. struct packet_sock *po = pkt_sk(sk);
  1418. u16 type_flags = args->type_flags;
  1419. struct packet_fanout *f, *match;
  1420. u8 type = type_flags & 0xff;
  1421. u8 flags = type_flags >> 8;
  1422. u16 id = args->id;
  1423. int err;
  1424. switch (type) {
  1425. case PACKET_FANOUT_ROLLOVER:
  1426. if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
  1427. return -EINVAL;
  1428. break;
  1429. case PACKET_FANOUT_HASH:
  1430. case PACKET_FANOUT_LB:
  1431. case PACKET_FANOUT_CPU:
  1432. case PACKET_FANOUT_RND:
  1433. case PACKET_FANOUT_QM:
  1434. case PACKET_FANOUT_CBPF:
  1435. case PACKET_FANOUT_EBPF:
  1436. break;
  1437. default:
  1438. return -EINVAL;
  1439. }
  1440. mutex_lock(&fanout_mutex);
  1441. err = -EALREADY;
  1442. if (po->fanout)
  1443. goto out;
  1444. if (type == PACKET_FANOUT_ROLLOVER ||
  1445. (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)) {
  1446. err = -ENOMEM;
  1447. rollover = kzalloc_obj(*rollover);
  1448. if (!rollover)
  1449. goto out;
  1450. atomic_long_set(&rollover->num, 0);
  1451. atomic_long_set(&rollover->num_huge, 0);
  1452. atomic_long_set(&rollover->num_failed, 0);
  1453. }
  1454. if (type_flags & PACKET_FANOUT_FLAG_UNIQUEID) {
  1455. if (id != 0) {
  1456. err = -EINVAL;
  1457. goto out;
  1458. }
  1459. if (!fanout_find_new_id(sk, &id)) {
  1460. err = -ENOMEM;
  1461. goto out;
  1462. }
  1463. /* ephemeral flag for the first socket in the group: drop it */
  1464. flags &= ~(PACKET_FANOUT_FLAG_UNIQUEID >> 8);
  1465. }
  1466. match = NULL;
  1467. list_for_each_entry(f, &fanout_list, list) {
  1468. if (f->id == id &&
  1469. read_pnet(&f->net) == sock_net(sk)) {
  1470. match = f;
  1471. break;
  1472. }
  1473. }
  1474. err = -EINVAL;
  1475. if (match) {
  1476. if (match->flags != flags)
  1477. goto out;
  1478. if (args->max_num_members &&
  1479. args->max_num_members != match->max_num_members)
  1480. goto out;
  1481. } else {
  1482. if (args->max_num_members > PACKET_FANOUT_MAX)
  1483. goto out;
  1484. if (!args->max_num_members)
  1485. /* legacy PACKET_FANOUT_MAX */
  1486. args->max_num_members = 256;
  1487. err = -ENOMEM;
  1488. match = kvzalloc_flex(*match, arr, args->max_num_members);
  1489. if (!match)
  1490. goto out;
  1491. write_pnet(&match->net, sock_net(sk));
  1492. match->id = id;
  1493. match->type = type;
  1494. match->flags = flags;
  1495. INIT_LIST_HEAD(&match->list);
  1496. spin_lock_init(&match->lock);
  1497. refcount_set(&match->sk_ref, 0);
  1498. fanout_init_data(match);
  1499. match->prot_hook.type = po->prot_hook.type;
  1500. match->prot_hook.dev = po->prot_hook.dev;
  1501. match->prot_hook.func = packet_rcv_fanout;
  1502. match->prot_hook.af_packet_priv = match;
  1503. match->prot_hook.af_packet_net = read_pnet(&match->net);
  1504. match->prot_hook.id_match = match_fanout_group;
  1505. match->max_num_members = args->max_num_members;
  1506. match->prot_hook.ignore_outgoing = type_flags & PACKET_FANOUT_FLAG_IGNORE_OUTGOING;
  1507. list_add(&match->list, &fanout_list);
  1508. }
  1509. err = -EINVAL;
  1510. spin_lock(&po->bind_lock);
  1511. if (po->num &&
  1512. match->type == type &&
  1513. match->prot_hook.type == po->prot_hook.type &&
  1514. match->prot_hook.dev == po->prot_hook.dev) {
  1515. err = -ENOSPC;
  1516. if (refcount_read(&match->sk_ref) < match->max_num_members) {
  1517. /* Paired with packet_setsockopt(PACKET_FANOUT_DATA) */
  1518. WRITE_ONCE(po->fanout, match);
  1519. po->rollover = rollover;
  1520. rollover = NULL;
  1521. refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
  1522. if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  1523. __dev_remove_pack(&po->prot_hook);
  1524. __fanout_link(sk, po);
  1525. }
  1526. err = 0;
  1527. }
  1528. }
  1529. spin_unlock(&po->bind_lock);
  1530. if (err && !refcount_read(&match->sk_ref)) {
  1531. list_del(&match->list);
  1532. kvfree(match);
  1533. }
  1534. out:
  1535. kfree(rollover);
  1536. mutex_unlock(&fanout_mutex);
  1537. return err;
  1538. }
  1539. /* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
  1540. * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
  1541. * It is the responsibility of the caller to call fanout_release_data() and
  1542. * free the returned packet_fanout (after synchronize_net())
  1543. */
  1544. static struct packet_fanout *fanout_release(struct sock *sk)
  1545. {
  1546. struct packet_sock *po = pkt_sk(sk);
  1547. struct packet_fanout *f;
  1548. mutex_lock(&fanout_mutex);
  1549. f = po->fanout;
  1550. if (f) {
  1551. po->fanout = NULL;
  1552. if (refcount_dec_and_test(&f->sk_ref))
  1553. list_del(&f->list);
  1554. else
  1555. f = NULL;
  1556. }
  1557. mutex_unlock(&fanout_mutex);
  1558. return f;
  1559. }
  1560. static bool packet_extra_vlan_len_allowed(const struct net_device *dev,
  1561. struct sk_buff *skb)
  1562. {
  1563. /* Earlier code assumed this would be a VLAN pkt, double-check
  1564. * this now that we have the actual packet in hand. We can only
  1565. * do this check on Ethernet devices.
  1566. */
  1567. if (unlikely(dev->type != ARPHRD_ETHER))
  1568. return false;
  1569. skb_reset_mac_header(skb);
  1570. return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
  1571. }
  1572. static const struct proto_ops packet_ops;
  1573. static const struct proto_ops packet_ops_spkt;
  1574. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
  1575. struct packet_type *pt, struct net_device *orig_dev)
  1576. {
  1577. struct sock *sk;
  1578. struct sockaddr_pkt *spkt;
  1579. /*
  1580. * When we registered the protocol we saved the socket in the data
  1581. * field for just this event.
  1582. */
  1583. sk = pt->af_packet_priv;
  1584. /*
  1585. * Yank back the headers [hope the device set this
  1586. * right or kerboom...]
  1587. *
  1588. * Incoming packets have ll header pulled,
  1589. * push it back.
  1590. *
  1591. * For outgoing ones skb->data == skb_mac_header(skb)
  1592. * so that this procedure is noop.
  1593. */
  1594. if (skb->pkt_type == PACKET_LOOPBACK)
  1595. goto out;
  1596. if (!net_eq(dev_net(dev), sock_net(sk)))
  1597. goto out;
  1598. skb = skb_share_check(skb, GFP_ATOMIC);
  1599. if (skb == NULL)
  1600. goto oom;
  1601. /* drop any routing info */
  1602. skb_dst_drop(skb);
  1603. /* drop conntrack reference */
  1604. nf_reset_ct(skb);
  1605. spkt = &PACKET_SKB_CB(skb)->sa.pkt;
  1606. skb_push(skb, skb->data - skb_mac_header(skb));
  1607. /*
  1608. * The SOCK_PACKET socket receives _all_ frames.
  1609. */
  1610. spkt->spkt_family = dev->type;
  1611. strscpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  1612. spkt->spkt_protocol = skb->protocol;
  1613. /*
  1614. * Charge the memory to the socket. This is done specifically
  1615. * to prevent sockets using all the memory up.
  1616. */
  1617. if (sock_queue_rcv_skb(sk, skb) == 0)
  1618. return 0;
  1619. out:
  1620. kfree_skb(skb);
  1621. oom:
  1622. return 0;
  1623. }
  1624. static void packet_parse_headers(struct sk_buff *skb, struct socket *sock)
  1625. {
  1626. int depth;
  1627. if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) &&
  1628. sock->type == SOCK_RAW) {
  1629. skb_reset_mac_header(skb);
  1630. skb->protocol = dev_parse_header_protocol(skb);
  1631. }
  1632. /* Move network header to the right position for VLAN tagged packets */
  1633. if (likely(skb->dev->type == ARPHRD_ETHER) &&
  1634. eth_type_vlan(skb->protocol) &&
  1635. vlan_get_protocol_and_depth(skb, skb->protocol, &depth) != 0)
  1636. skb_set_network_header(skb, depth);
  1637. skb_probe_transport_header(skb);
  1638. }
  1639. /*
  1640. * Output a raw packet to a device layer. This bypasses all the other
  1641. * protocol layers and you must therefore supply it with a complete frame
  1642. */
  1643. static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
  1644. size_t len)
  1645. {
  1646. struct sock *sk = sock->sk;
  1647. DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
  1648. struct sk_buff *skb = NULL;
  1649. struct net_device *dev;
  1650. struct sockcm_cookie sockc;
  1651. __be16 proto = 0;
  1652. int err;
  1653. int extra_len = 0;
  1654. /*
  1655. * Get and verify the address.
  1656. */
  1657. if (saddr) {
  1658. if (msg->msg_namelen < sizeof(struct sockaddr))
  1659. return -EINVAL;
  1660. if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
  1661. proto = saddr->spkt_protocol;
  1662. } else
  1663. return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
  1664. /*
  1665. * Find the device first to size check it
  1666. */
  1667. saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
  1668. retry:
  1669. rcu_read_lock();
  1670. dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
  1671. err = -ENODEV;
  1672. if (dev == NULL)
  1673. goto out_unlock;
  1674. err = -ENETDOWN;
  1675. if (!(dev->flags & IFF_UP))
  1676. goto out_unlock;
  1677. /*
  1678. * You may not queue a frame bigger than the mtu. This is the lowest level
  1679. * raw protocol and you must do your own fragmentation at this level.
  1680. */
  1681. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  1682. if (!netif_supports_nofcs(dev)) {
  1683. err = -EPROTONOSUPPORT;
  1684. goto out_unlock;
  1685. }
  1686. extra_len = 4; /* We're doing our own CRC */
  1687. }
  1688. err = -EMSGSIZE;
  1689. if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
  1690. goto out_unlock;
  1691. if (!skb) {
  1692. size_t reserved = LL_RESERVED_SPACE(dev);
  1693. int tlen = dev->needed_tailroom;
  1694. unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
  1695. rcu_read_unlock();
  1696. skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
  1697. if (skb == NULL)
  1698. return -ENOBUFS;
  1699. /* FIXME: Save some space for broken drivers that write a hard
  1700. * header at transmission time by themselves. PPP is the notable
  1701. * one here. This should really be fixed at the driver level.
  1702. */
  1703. skb_reserve(skb, reserved);
  1704. skb_reset_network_header(skb);
  1705. /* Try to align data part correctly */
  1706. if (hhlen) {
  1707. skb->data -= hhlen;
  1708. skb->tail -= hhlen;
  1709. if (len < hhlen)
  1710. skb_reset_network_header(skb);
  1711. }
  1712. err = memcpy_from_msg(skb_put(skb, len), msg, len);
  1713. if (err)
  1714. goto out_free;
  1715. goto retry;
  1716. }
  1717. if (!dev_validate_header(dev, skb->data, len) || !skb->len) {
  1718. err = -EINVAL;
  1719. goto out_unlock;
  1720. }
  1721. if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
  1722. !packet_extra_vlan_len_allowed(dev, skb)) {
  1723. err = -EMSGSIZE;
  1724. goto out_unlock;
  1725. }
  1726. sockcm_init(&sockc, sk);
  1727. if (msg->msg_controllen) {
  1728. err = sock_cmsg_send(sk, msg, &sockc);
  1729. if (unlikely(err))
  1730. goto out_unlock;
  1731. }
  1732. skb->protocol = proto;
  1733. skb->dev = dev;
  1734. skb->priority = sockc.priority;
  1735. skb->mark = sockc.mark;
  1736. skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid);
  1737. skb_setup_tx_timestamp(skb, &sockc);
  1738. if (unlikely(extra_len == 4))
  1739. skb->no_fcs = 1;
  1740. packet_parse_headers(skb, sock);
  1741. dev_queue_xmit(skb);
  1742. rcu_read_unlock();
  1743. return len;
  1744. out_unlock:
  1745. rcu_read_unlock();
  1746. out_free:
  1747. kfree_skb(skb);
  1748. return err;
  1749. }
  1750. static unsigned int run_filter(struct sk_buff *skb,
  1751. const struct sock *sk,
  1752. unsigned int res)
  1753. {
  1754. struct sk_filter *filter;
  1755. rcu_read_lock();
  1756. filter = rcu_dereference(sk->sk_filter);
  1757. if (filter != NULL)
  1758. res = bpf_prog_run_clear_cb(filter->prog, skb);
  1759. rcu_read_unlock();
  1760. return res;
  1761. }
  1762. static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
  1763. size_t *len, int vnet_hdr_sz)
  1764. {
  1765. struct virtio_net_hdr_mrg_rxbuf vnet_hdr = { .num_buffers = 0 };
  1766. if (*len < vnet_hdr_sz)
  1767. return -EINVAL;
  1768. *len -= vnet_hdr_sz;
  1769. if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)&vnet_hdr, vio_le(), true, 0))
  1770. return -EINVAL;
  1771. return memcpy_to_msg(msg, (void *)&vnet_hdr, vnet_hdr_sz);
  1772. }
  1773. /*
  1774. * This function makes lazy skb cloning in hope that most of packets
  1775. * are discarded by BPF.
  1776. *
  1777. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  1778. * and skb->cb are mangled. It works because (and until) packets
  1779. * falling here are owned by current CPU. Output packets are cloned
  1780. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  1781. * sequentially, so that if we return skb to original state on exit,
  1782. * we will not harm anyone.
  1783. */
  1784. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  1785. struct packet_type *pt, struct net_device *orig_dev)
  1786. {
  1787. enum skb_drop_reason drop_reason = SKB_CONSUMED;
  1788. struct sock *sk = NULL;
  1789. struct sockaddr_ll *sll;
  1790. struct packet_sock *po;
  1791. u8 *skb_head = skb->data;
  1792. int skb_len = skb->len;
  1793. unsigned int snaplen, res;
  1794. if (skb->pkt_type == PACKET_LOOPBACK)
  1795. goto drop;
  1796. sk = pt->af_packet_priv;
  1797. po = pkt_sk(sk);
  1798. if (!net_eq(dev_net(dev), sock_net(sk)))
  1799. goto drop;
  1800. skb->dev = dev;
  1801. if (dev_has_header(dev)) {
  1802. /* The device has an explicit notion of ll header,
  1803. * exported to higher levels.
  1804. *
  1805. * Otherwise, the device hides details of its frame
  1806. * structure, so that corresponding packet head is
  1807. * never delivered to user.
  1808. */
  1809. if (sk->sk_type != SOCK_DGRAM)
  1810. skb_push(skb, skb->data - skb_mac_header(skb));
  1811. else if (skb->pkt_type == PACKET_OUTGOING) {
  1812. /* Special case: outgoing packets have ll header at head */
  1813. skb_pull(skb, skb_network_offset(skb));
  1814. }
  1815. }
  1816. snaplen = skb_frags_readable(skb) ? skb->len : skb_headlen(skb);
  1817. res = run_filter(skb, sk, snaplen);
  1818. if (!res)
  1819. goto drop_n_restore;
  1820. if (snaplen > res)
  1821. snaplen = res;
  1822. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1823. goto drop_n_acct;
  1824. if (skb_shared(skb)) {
  1825. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  1826. if (nskb == NULL)
  1827. goto drop_n_acct;
  1828. if (skb_head != skb->data) {
  1829. skb->data = skb_head;
  1830. skb->len = skb_len;
  1831. }
  1832. consume_skb(skb);
  1833. skb = nskb;
  1834. }
  1835. sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
  1836. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1837. sll->sll_hatype = dev->type;
  1838. sll->sll_pkttype = skb->pkt_type;
  1839. if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV)))
  1840. sll->sll_ifindex = orig_dev->ifindex;
  1841. else
  1842. sll->sll_ifindex = dev->ifindex;
  1843. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1844. /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
  1845. * Use their space for storing the original skb length.
  1846. */
  1847. PACKET_SKB_CB(skb)->sa.origlen = skb->len;
  1848. if (pskb_trim(skb, snaplen))
  1849. goto drop_n_acct;
  1850. skb_set_owner_r(skb, sk);
  1851. skb->dev = NULL;
  1852. skb_dst_drop(skb);
  1853. /* drop conntrack reference */
  1854. nf_reset_ct(skb);
  1855. spin_lock(&sk->sk_receive_queue.lock);
  1856. po->stats.stats1.tp_packets++;
  1857. sock_skb_set_dropcount(sk, skb);
  1858. skb_clear_delivery_time(skb);
  1859. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1860. spin_unlock(&sk->sk_receive_queue.lock);
  1861. sk->sk_data_ready(sk);
  1862. return 0;
  1863. drop_n_acct:
  1864. atomic_inc(&po->tp_drops);
  1865. sk_drops_inc(sk);
  1866. drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
  1867. drop_n_restore:
  1868. if (skb_head != skb->data && skb_shared(skb)) {
  1869. skb->data = skb_head;
  1870. skb->len = skb_len;
  1871. }
  1872. drop:
  1873. sk_skb_reason_drop(sk, skb, drop_reason);
  1874. return 0;
  1875. }
  1876. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  1877. struct packet_type *pt, struct net_device *orig_dev)
  1878. {
  1879. enum skb_drop_reason drop_reason = SKB_CONSUMED;
  1880. struct sock *sk = NULL;
  1881. struct packet_sock *po;
  1882. struct sockaddr_ll *sll;
  1883. union tpacket_uhdr h;
  1884. u8 *skb_head = skb->data;
  1885. int skb_len = skb->len;
  1886. unsigned int snaplen, res;
  1887. unsigned long status = TP_STATUS_USER;
  1888. unsigned short macoff, hdrlen;
  1889. unsigned int netoff;
  1890. struct sk_buff *copy_skb = NULL;
  1891. struct timespec64 ts;
  1892. __u32 ts_status;
  1893. unsigned int slot_id = 0;
  1894. int vnet_hdr_sz = 0;
  1895. /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
  1896. * We may add members to them until current aligned size without forcing
  1897. * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
  1898. */
  1899. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
  1900. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
  1901. if (skb->pkt_type == PACKET_LOOPBACK)
  1902. goto drop;
  1903. sk = pt->af_packet_priv;
  1904. po = pkt_sk(sk);
  1905. if (!net_eq(dev_net(dev), sock_net(sk)))
  1906. goto drop;
  1907. if (dev_has_header(dev)) {
  1908. if (sk->sk_type != SOCK_DGRAM)
  1909. skb_push(skb, skb->data - skb_mac_header(skb));
  1910. else if (skb->pkt_type == PACKET_OUTGOING) {
  1911. /* Special case: outgoing packets have ll header at head */
  1912. skb_pull(skb, skb_network_offset(skb));
  1913. }
  1914. }
  1915. snaplen = skb_frags_readable(skb) ? skb->len : skb_headlen(skb);
  1916. res = run_filter(skb, sk, snaplen);
  1917. if (!res)
  1918. goto drop_n_restore;
  1919. /* If we are flooded, just give up */
  1920. if (__packet_rcv_has_room(po, skb) == ROOM_NONE) {
  1921. atomic_inc(&po->tp_drops);
  1922. goto drop_n_restore;
  1923. }
  1924. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1925. status |= TP_STATUS_CSUMNOTREADY;
  1926. else if (skb->pkt_type != PACKET_OUTGOING &&
  1927. skb_csum_unnecessary(skb))
  1928. status |= TP_STATUS_CSUM_VALID;
  1929. if (skb_is_gso(skb) && skb_is_gso_tcp(skb))
  1930. status |= TP_STATUS_GSO_TCP;
  1931. if (snaplen > res)
  1932. snaplen = res;
  1933. if (sk->sk_type == SOCK_DGRAM) {
  1934. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  1935. po->tp_reserve;
  1936. } else {
  1937. unsigned int maclen = skb_network_offset(skb);
  1938. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  1939. (maclen < 16 ? 16 : maclen)) +
  1940. po->tp_reserve;
  1941. vnet_hdr_sz = READ_ONCE(po->vnet_hdr_sz);
  1942. if (vnet_hdr_sz)
  1943. netoff += vnet_hdr_sz;
  1944. macoff = netoff - maclen;
  1945. }
  1946. if (netoff > USHRT_MAX) {
  1947. atomic_inc(&po->tp_drops);
  1948. goto drop_n_restore;
  1949. }
  1950. if (po->tp_version <= TPACKET_V2) {
  1951. if (macoff + snaplen > po->rx_ring.frame_size) {
  1952. if (READ_ONCE(po->copy_thresh) &&
  1953. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  1954. if (skb_shared(skb)) {
  1955. copy_skb = skb_clone(skb, GFP_ATOMIC);
  1956. } else {
  1957. copy_skb = skb_get(skb);
  1958. skb_head = skb->data;
  1959. }
  1960. if (copy_skb) {
  1961. memset(&PACKET_SKB_CB(copy_skb)->sa.ll, 0,
  1962. sizeof(PACKET_SKB_CB(copy_skb)->sa.ll));
  1963. skb_set_owner_r(copy_skb, sk);
  1964. }
  1965. }
  1966. snaplen = po->rx_ring.frame_size - macoff;
  1967. if ((int)snaplen < 0) {
  1968. snaplen = 0;
  1969. vnet_hdr_sz = 0;
  1970. }
  1971. }
  1972. } else if (unlikely(macoff + snaplen >
  1973. GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
  1974. u32 nval;
  1975. nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
  1976. pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
  1977. snaplen, nval, macoff);
  1978. snaplen = nval;
  1979. if (unlikely((int)snaplen < 0)) {
  1980. snaplen = 0;
  1981. macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
  1982. vnet_hdr_sz = 0;
  1983. }
  1984. }
  1985. spin_lock(&sk->sk_receive_queue.lock);
  1986. h.raw = packet_current_rx_frame(po, skb,
  1987. TP_STATUS_KERNEL, (macoff+snaplen));
  1988. if (!h.raw)
  1989. goto drop_n_account;
  1990. if (po->tp_version <= TPACKET_V2) {
  1991. slot_id = po->rx_ring.head;
  1992. if (test_bit(slot_id, po->rx_ring.rx_owner_map))
  1993. goto drop_n_account;
  1994. __set_bit(slot_id, po->rx_ring.rx_owner_map);
  1995. }
  1996. if (vnet_hdr_sz &&
  1997. virtio_net_hdr_from_skb(skb, h.raw + macoff -
  1998. sizeof(struct virtio_net_hdr),
  1999. vio_le(), true, 0)) {
  2000. if (po->tp_version == TPACKET_V3)
  2001. prb_clear_blk_fill_status(&po->rx_ring);
  2002. goto drop_n_account;
  2003. }
  2004. if (po->tp_version <= TPACKET_V2) {
  2005. packet_increment_rx_head(po, &po->rx_ring);
  2006. /*
  2007. * LOSING will be reported till you read the stats,
  2008. * because it's COR - Clear On Read.
  2009. * Anyways, moving it for V1/V2 only as V3 doesn't need this
  2010. * at packet level.
  2011. */
  2012. if (atomic_read(&po->tp_drops))
  2013. status |= TP_STATUS_LOSING;
  2014. }
  2015. po->stats.stats1.tp_packets++;
  2016. if (copy_skb) {
  2017. status |= TP_STATUS_COPY;
  2018. skb_clear_delivery_time(copy_skb);
  2019. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  2020. }
  2021. spin_unlock(&sk->sk_receive_queue.lock);
  2022. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  2023. /* Always timestamp; prefer an existing software timestamp taken
  2024. * closer to the time of capture.
  2025. */
  2026. ts_status = tpacket_get_timestamp(skb, &ts,
  2027. READ_ONCE(po->tp_tstamp) |
  2028. SOF_TIMESTAMPING_SOFTWARE);
  2029. if (!ts_status)
  2030. ktime_get_real_ts64(&ts);
  2031. status |= ts_status;
  2032. switch (po->tp_version) {
  2033. case TPACKET_V1:
  2034. h.h1->tp_len = skb->len;
  2035. h.h1->tp_snaplen = snaplen;
  2036. h.h1->tp_mac = macoff;
  2037. h.h1->tp_net = netoff;
  2038. h.h1->tp_sec = ts.tv_sec;
  2039. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  2040. hdrlen = sizeof(*h.h1);
  2041. break;
  2042. case TPACKET_V2:
  2043. h.h2->tp_len = skb->len;
  2044. h.h2->tp_snaplen = snaplen;
  2045. h.h2->tp_mac = macoff;
  2046. h.h2->tp_net = netoff;
  2047. h.h2->tp_sec = ts.tv_sec;
  2048. h.h2->tp_nsec = ts.tv_nsec;
  2049. if (skb_vlan_tag_present(skb)) {
  2050. h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
  2051. h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
  2052. status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2053. } else if (unlikely(sk->sk_type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
  2054. h.h2->tp_vlan_tci = vlan_get_tci(skb, skb->dev);
  2055. h.h2->tp_vlan_tpid = ntohs(skb->protocol);
  2056. status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2057. } else {
  2058. h.h2->tp_vlan_tci = 0;
  2059. h.h2->tp_vlan_tpid = 0;
  2060. }
  2061. memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
  2062. hdrlen = sizeof(*h.h2);
  2063. break;
  2064. case TPACKET_V3:
  2065. /* tp_nxt_offset,vlan are already populated above.
  2066. * So DONT clear those fields here
  2067. */
  2068. h.h3->tp_status |= status;
  2069. h.h3->tp_len = skb->len;
  2070. h.h3->tp_snaplen = snaplen;
  2071. h.h3->tp_mac = macoff;
  2072. h.h3->tp_net = netoff;
  2073. h.h3->tp_sec = ts.tv_sec;
  2074. h.h3->tp_nsec = ts.tv_nsec;
  2075. memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
  2076. hdrlen = sizeof(*h.h3);
  2077. break;
  2078. default:
  2079. BUG();
  2080. }
  2081. sll = h.raw + TPACKET_ALIGN(hdrlen);
  2082. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  2083. sll->sll_family = AF_PACKET;
  2084. sll->sll_hatype = dev->type;
  2085. sll->sll_protocol = (sk->sk_type == SOCK_DGRAM) ?
  2086. vlan_get_protocol_dgram(skb) : skb->protocol;
  2087. sll->sll_pkttype = skb->pkt_type;
  2088. if (unlikely(packet_sock_flag(po, PACKET_SOCK_ORIGDEV)))
  2089. sll->sll_ifindex = orig_dev->ifindex;
  2090. else
  2091. sll->sll_ifindex = dev->ifindex;
  2092. smp_mb();
  2093. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  2094. if (po->tp_version <= TPACKET_V2) {
  2095. u8 *start, *end;
  2096. end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
  2097. macoff + snaplen);
  2098. for (start = h.raw; start < end; start += PAGE_SIZE)
  2099. flush_dcache_page(pgv_to_page(start));
  2100. }
  2101. smp_wmb();
  2102. #endif
  2103. if (po->tp_version <= TPACKET_V2) {
  2104. spin_lock(&sk->sk_receive_queue.lock);
  2105. __packet_set_status(po, h.raw, status);
  2106. __clear_bit(slot_id, po->rx_ring.rx_owner_map);
  2107. spin_unlock(&sk->sk_receive_queue.lock);
  2108. sk->sk_data_ready(sk);
  2109. } else if (po->tp_version == TPACKET_V3) {
  2110. prb_clear_blk_fill_status(&po->rx_ring);
  2111. }
  2112. drop_n_restore:
  2113. if (skb_head != skb->data && skb_shared(skb)) {
  2114. skb->data = skb_head;
  2115. skb->len = skb_len;
  2116. }
  2117. drop:
  2118. sk_skb_reason_drop(sk, skb, drop_reason);
  2119. return 0;
  2120. drop_n_account:
  2121. spin_unlock(&sk->sk_receive_queue.lock);
  2122. atomic_inc(&po->tp_drops);
  2123. drop_reason = SKB_DROP_REASON_PACKET_SOCK_ERROR;
  2124. sk->sk_data_ready(sk);
  2125. sk_skb_reason_drop(sk, copy_skb, drop_reason);
  2126. goto drop_n_restore;
  2127. }
  2128. static void tpacket_destruct_skb(struct sk_buff *skb)
  2129. {
  2130. struct packet_sock *po = pkt_sk(skb->sk);
  2131. if (likely(po->tx_ring.pg_vec)) {
  2132. void *ph;
  2133. __u32 ts;
  2134. ph = skb_zcopy_get_nouarg(skb);
  2135. packet_dec_pending(&po->tx_ring);
  2136. ts = __packet_set_timestamp(po, ph, skb);
  2137. __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
  2138. complete(&po->skb_completion);
  2139. }
  2140. sock_wfree(skb);
  2141. }
  2142. static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
  2143. {
  2144. if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  2145. (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2146. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
  2147. __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
  2148. vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
  2149. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2150. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
  2151. if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
  2152. return -EINVAL;
  2153. return 0;
  2154. }
  2155. static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
  2156. struct virtio_net_hdr *vnet_hdr, int vnet_hdr_sz)
  2157. {
  2158. int ret;
  2159. if (*len < vnet_hdr_sz)
  2160. return -EINVAL;
  2161. *len -= vnet_hdr_sz;
  2162. if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter))
  2163. return -EFAULT;
  2164. ret = __packet_snd_vnet_parse(vnet_hdr, *len);
  2165. if (ret)
  2166. return ret;
  2167. /* move iter to point to the start of mac header */
  2168. if (vnet_hdr_sz != sizeof(struct virtio_net_hdr))
  2169. iov_iter_advance(&msg->msg_iter, vnet_hdr_sz - sizeof(struct virtio_net_hdr));
  2170. return 0;
  2171. }
  2172. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  2173. void *frame, struct net_device *dev, void *data, int tp_len,
  2174. __be16 proto, unsigned char *addr, int hlen, int copylen,
  2175. const struct sockcm_cookie *sockc)
  2176. {
  2177. union tpacket_uhdr ph;
  2178. int to_write, offset, len, nr_frags, len_max;
  2179. struct socket *sock = po->sk.sk_socket;
  2180. struct page *page;
  2181. int err;
  2182. ph.raw = frame;
  2183. skb->protocol = proto;
  2184. skb->dev = dev;
  2185. skb->priority = sockc->priority;
  2186. skb->mark = sockc->mark;
  2187. skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, po->sk.sk_clockid);
  2188. skb_setup_tx_timestamp(skb, sockc);
  2189. skb_zcopy_set_nouarg(skb, ph.raw);
  2190. skb_reserve(skb, hlen);
  2191. skb_reset_network_header(skb);
  2192. to_write = tp_len;
  2193. if (sock->type == SOCK_DGRAM) {
  2194. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  2195. NULL, tp_len);
  2196. if (unlikely(err < 0))
  2197. return -EINVAL;
  2198. } else if (copylen) {
  2199. int hdrlen = min_t(int, copylen, tp_len);
  2200. skb_push(skb, dev->hard_header_len);
  2201. skb_put(skb, copylen - dev->hard_header_len);
  2202. err = skb_store_bits(skb, 0, data, hdrlen);
  2203. if (unlikely(err))
  2204. return err;
  2205. if (!dev_validate_header(dev, skb->data, hdrlen))
  2206. return -EINVAL;
  2207. data += hdrlen;
  2208. to_write -= hdrlen;
  2209. }
  2210. offset = offset_in_page(data);
  2211. len_max = PAGE_SIZE - offset;
  2212. len = ((to_write > len_max) ? len_max : to_write);
  2213. skb->data_len = to_write;
  2214. skb->len += to_write;
  2215. skb->truesize += to_write;
  2216. refcount_add(to_write, &po->sk.sk_wmem_alloc);
  2217. while (likely(to_write)) {
  2218. nr_frags = skb_shinfo(skb)->nr_frags;
  2219. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  2220. pr_err("Packet exceed the number of skb frags(%u)\n",
  2221. (unsigned int)MAX_SKB_FRAGS);
  2222. return -EFAULT;
  2223. }
  2224. page = pgv_to_page(data);
  2225. data += len;
  2226. flush_dcache_page(page);
  2227. get_page(page);
  2228. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  2229. to_write -= len;
  2230. offset = 0;
  2231. len_max = PAGE_SIZE;
  2232. len = ((to_write > len_max) ? len_max : to_write);
  2233. }
  2234. packet_parse_headers(skb, sock);
  2235. return tp_len;
  2236. }
  2237. static int tpacket_parse_header(struct packet_sock *po, void *frame,
  2238. int size_max, void **data)
  2239. {
  2240. union tpacket_uhdr ph;
  2241. int tp_len, off;
  2242. ph.raw = frame;
  2243. switch (po->tp_version) {
  2244. case TPACKET_V3:
  2245. if (ph.h3->tp_next_offset != 0) {
  2246. pr_warn_once("variable sized slot not supported");
  2247. return -EINVAL;
  2248. }
  2249. tp_len = ph.h3->tp_len;
  2250. break;
  2251. case TPACKET_V2:
  2252. tp_len = ph.h2->tp_len;
  2253. break;
  2254. default:
  2255. tp_len = ph.h1->tp_len;
  2256. break;
  2257. }
  2258. if (unlikely(tp_len > size_max)) {
  2259. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  2260. return -EMSGSIZE;
  2261. }
  2262. if (unlikely(packet_sock_flag(po, PACKET_SOCK_TX_HAS_OFF))) {
  2263. int off_min, off_max;
  2264. off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2265. off_max = po->tx_ring.frame_size - tp_len;
  2266. if (po->sk.sk_type == SOCK_DGRAM) {
  2267. switch (po->tp_version) {
  2268. case TPACKET_V3:
  2269. off = ph.h3->tp_net;
  2270. break;
  2271. case TPACKET_V2:
  2272. off = ph.h2->tp_net;
  2273. break;
  2274. default:
  2275. off = ph.h1->tp_net;
  2276. break;
  2277. }
  2278. } else {
  2279. switch (po->tp_version) {
  2280. case TPACKET_V3:
  2281. off = ph.h3->tp_mac;
  2282. break;
  2283. case TPACKET_V2:
  2284. off = ph.h2->tp_mac;
  2285. break;
  2286. default:
  2287. off = ph.h1->tp_mac;
  2288. break;
  2289. }
  2290. }
  2291. if (unlikely((off < off_min) || (off_max < off)))
  2292. return -EINVAL;
  2293. } else {
  2294. off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2295. }
  2296. *data = frame + off;
  2297. return tp_len;
  2298. }
  2299. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  2300. {
  2301. struct sk_buff *skb = NULL;
  2302. struct net_device *dev;
  2303. struct virtio_net_hdr *vnet_hdr = NULL;
  2304. struct sockcm_cookie sockc;
  2305. __be16 proto;
  2306. int err, reserve = 0;
  2307. void *ph;
  2308. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2309. bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
  2310. int vnet_hdr_sz = READ_ONCE(po->vnet_hdr_sz);
  2311. unsigned char *addr = NULL;
  2312. int tp_len, size_max;
  2313. void *data;
  2314. int len_sum = 0;
  2315. int status = TP_STATUS_AVAILABLE;
  2316. int hlen, tlen, copylen = 0;
  2317. long timeo;
  2318. mutex_lock(&po->pg_vec_lock);
  2319. /* packet_sendmsg() check on tx_ring.pg_vec was lockless,
  2320. * we need to confirm it under protection of pg_vec_lock.
  2321. */
  2322. if (unlikely(!po->tx_ring.pg_vec)) {
  2323. err = -EBUSY;
  2324. goto out;
  2325. }
  2326. if (likely(saddr == NULL)) {
  2327. dev = packet_cached_dev_get(po);
  2328. proto = READ_ONCE(po->num);
  2329. } else {
  2330. err = -EINVAL;
  2331. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2332. goto out;
  2333. if (msg->msg_namelen < (saddr->sll_halen
  2334. + offsetof(struct sockaddr_ll,
  2335. sll_addr)))
  2336. goto out;
  2337. proto = saddr->sll_protocol;
  2338. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  2339. if (po->sk.sk_socket->type == SOCK_DGRAM) {
  2340. if (dev && msg->msg_namelen < dev->addr_len +
  2341. offsetof(struct sockaddr_ll, sll_addr))
  2342. goto out_put;
  2343. addr = saddr->sll_addr;
  2344. }
  2345. }
  2346. err = -ENXIO;
  2347. if (unlikely(dev == NULL))
  2348. goto out;
  2349. err = -ENETDOWN;
  2350. if (unlikely(!(dev->flags & IFF_UP)))
  2351. goto out_put;
  2352. sockcm_init(&sockc, &po->sk);
  2353. if (msg->msg_controllen) {
  2354. err = sock_cmsg_send(&po->sk, msg, &sockc);
  2355. if (unlikely(err))
  2356. goto out_put;
  2357. }
  2358. if (po->sk.sk_socket->type == SOCK_RAW)
  2359. reserve = dev->hard_header_len;
  2360. size_max = po->tx_ring.frame_size
  2361. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  2362. if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !vnet_hdr_sz)
  2363. size_max = dev->mtu + reserve + VLAN_HLEN;
  2364. timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
  2365. reinit_completion(&po->skb_completion);
  2366. do {
  2367. ph = packet_current_frame(po, &po->tx_ring,
  2368. TP_STATUS_SEND_REQUEST);
  2369. if (unlikely(ph == NULL)) {
  2370. /* Note: packet_read_pending() might be slow if we
  2371. * have to call it as it's per_cpu variable, but in
  2372. * fast-path we don't have to call it, only when ph
  2373. * is NULL, we need to check the pending_refcnt.
  2374. */
  2375. if (need_wait && packet_read_pending(&po->tx_ring)) {
  2376. timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
  2377. if (timeo <= 0) {
  2378. err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
  2379. goto out_put;
  2380. }
  2381. /* check for additional frames */
  2382. continue;
  2383. } else
  2384. break;
  2385. }
  2386. skb = NULL;
  2387. tp_len = tpacket_parse_header(po, ph, size_max, &data);
  2388. if (tp_len < 0)
  2389. goto tpacket_error;
  2390. status = TP_STATUS_SEND_REQUEST;
  2391. hlen = LL_RESERVED_SPACE(dev);
  2392. tlen = dev->needed_tailroom;
  2393. if (vnet_hdr_sz) {
  2394. vnet_hdr = data;
  2395. data += vnet_hdr_sz;
  2396. tp_len -= vnet_hdr_sz;
  2397. if (tp_len < 0 ||
  2398. __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
  2399. tp_len = -EINVAL;
  2400. goto tpacket_error;
  2401. }
  2402. copylen = __virtio16_to_cpu(vio_le(),
  2403. vnet_hdr->hdr_len);
  2404. }
  2405. copylen = max_t(int, copylen, dev->hard_header_len);
  2406. skb = sock_alloc_send_skb(&po->sk,
  2407. hlen + tlen + sizeof(struct sockaddr_ll) +
  2408. (copylen - dev->hard_header_len),
  2409. !need_wait, &err);
  2410. if (unlikely(skb == NULL)) {
  2411. /* we assume the socket was initially writeable ... */
  2412. if (likely(len_sum > 0))
  2413. err = len_sum;
  2414. goto out_status;
  2415. }
  2416. tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
  2417. addr, hlen, copylen, &sockc);
  2418. if (likely(tp_len >= 0) &&
  2419. tp_len > dev->mtu + reserve &&
  2420. !vnet_hdr_sz &&
  2421. !packet_extra_vlan_len_allowed(dev, skb))
  2422. tp_len = -EMSGSIZE;
  2423. if (unlikely(tp_len < 0)) {
  2424. tpacket_error:
  2425. if (packet_sock_flag(po, PACKET_SOCK_TP_LOSS)) {
  2426. __packet_set_status(po, ph,
  2427. TP_STATUS_AVAILABLE);
  2428. packet_increment_head(&po->tx_ring);
  2429. kfree_skb(skb);
  2430. continue;
  2431. } else {
  2432. status = TP_STATUS_WRONG_FORMAT;
  2433. err = tp_len;
  2434. goto out_status;
  2435. }
  2436. }
  2437. if (vnet_hdr_sz) {
  2438. if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
  2439. tp_len = -EINVAL;
  2440. goto tpacket_error;
  2441. }
  2442. virtio_net_hdr_set_proto(skb, vnet_hdr);
  2443. }
  2444. skb->destructor = tpacket_destruct_skb;
  2445. __packet_set_status(po, ph, TP_STATUS_SENDING);
  2446. packet_inc_pending(&po->tx_ring);
  2447. status = TP_STATUS_SEND_REQUEST;
  2448. err = packet_xmit(po, skb);
  2449. if (unlikely(err != 0)) {
  2450. if (err > 0)
  2451. err = net_xmit_errno(err);
  2452. if (err && __packet_get_status(po, ph) ==
  2453. TP_STATUS_AVAILABLE) {
  2454. /* skb was destructed already */
  2455. skb = NULL;
  2456. goto out_status;
  2457. }
  2458. /*
  2459. * skb was dropped but not destructed yet;
  2460. * let's treat it like congestion or err < 0
  2461. */
  2462. err = 0;
  2463. }
  2464. packet_increment_head(&po->tx_ring);
  2465. len_sum += tp_len;
  2466. } while (1);
  2467. err = len_sum;
  2468. goto out_put;
  2469. out_status:
  2470. __packet_set_status(po, ph, status);
  2471. kfree_skb(skb);
  2472. out_put:
  2473. dev_put(dev);
  2474. out:
  2475. mutex_unlock(&po->pg_vec_lock);
  2476. return err;
  2477. }
  2478. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  2479. size_t reserve, size_t len,
  2480. size_t linear, int noblock,
  2481. int *err)
  2482. {
  2483. struct sk_buff *skb;
  2484. /* Under a page? Don't bother with paged skb. */
  2485. if (prepad + len < PAGE_SIZE || !linear)
  2486. linear = len;
  2487. if (len - linear > MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
  2488. linear = len - MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
  2489. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  2490. err, PAGE_ALLOC_COSTLY_ORDER);
  2491. if (!skb)
  2492. return NULL;
  2493. skb_reserve(skb, reserve);
  2494. skb_put(skb, linear);
  2495. skb->data_len = len - linear;
  2496. skb->len += len - linear;
  2497. return skb;
  2498. }
  2499. static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
  2500. {
  2501. struct sock *sk = sock->sk;
  2502. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2503. struct sk_buff *skb;
  2504. struct net_device *dev;
  2505. __be16 proto;
  2506. unsigned char *addr = NULL;
  2507. int err, reserve = 0;
  2508. struct sockcm_cookie sockc;
  2509. struct virtio_net_hdr vnet_hdr = { 0 };
  2510. int offset = 0;
  2511. struct packet_sock *po = pkt_sk(sk);
  2512. int vnet_hdr_sz = READ_ONCE(po->vnet_hdr_sz);
  2513. int hlen, tlen, linear;
  2514. int extra_len = 0;
  2515. /*
  2516. * Get and verify the address.
  2517. */
  2518. if (likely(saddr == NULL)) {
  2519. dev = packet_cached_dev_get(po);
  2520. proto = READ_ONCE(po->num);
  2521. } else {
  2522. err = -EINVAL;
  2523. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2524. goto out;
  2525. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  2526. goto out;
  2527. proto = saddr->sll_protocol;
  2528. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  2529. if (sock->type == SOCK_DGRAM) {
  2530. if (dev && msg->msg_namelen < dev->addr_len +
  2531. offsetof(struct sockaddr_ll, sll_addr))
  2532. goto out_unlock;
  2533. addr = saddr->sll_addr;
  2534. }
  2535. }
  2536. err = -ENXIO;
  2537. if (unlikely(dev == NULL))
  2538. goto out_unlock;
  2539. err = -ENETDOWN;
  2540. if (unlikely(!(dev->flags & IFF_UP)))
  2541. goto out_unlock;
  2542. sockcm_init(&sockc, sk);
  2543. if (msg->msg_controllen) {
  2544. err = sock_cmsg_send(sk, msg, &sockc);
  2545. if (unlikely(err))
  2546. goto out_unlock;
  2547. }
  2548. if (sock->type == SOCK_RAW)
  2549. reserve = dev->hard_header_len;
  2550. if (vnet_hdr_sz) {
  2551. err = packet_snd_vnet_parse(msg, &len, &vnet_hdr, vnet_hdr_sz);
  2552. if (err)
  2553. goto out_unlock;
  2554. }
  2555. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  2556. if (!netif_supports_nofcs(dev)) {
  2557. err = -EPROTONOSUPPORT;
  2558. goto out_unlock;
  2559. }
  2560. extra_len = 4; /* We're doing our own CRC */
  2561. }
  2562. err = -EMSGSIZE;
  2563. if (!vnet_hdr.gso_type &&
  2564. (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  2565. goto out_unlock;
  2566. err = -ENOBUFS;
  2567. hlen = LL_RESERVED_SPACE(dev);
  2568. tlen = dev->needed_tailroom;
  2569. linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
  2570. linear = max(linear, min_t(int, len, dev->hard_header_len));
  2571. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
  2572. msg->msg_flags & MSG_DONTWAIT, &err);
  2573. if (skb == NULL)
  2574. goto out_unlock;
  2575. skb_reset_network_header(skb);
  2576. err = -EINVAL;
  2577. if (sock->type == SOCK_DGRAM) {
  2578. offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  2579. if (unlikely(offset < 0))
  2580. goto out_free;
  2581. } else if (reserve) {
  2582. skb_reserve(skb, -reserve);
  2583. if (len < reserve + sizeof(struct ipv6hdr) &&
  2584. dev->min_header_len != dev->hard_header_len)
  2585. skb_reset_network_header(skb);
  2586. }
  2587. /* Returns -EFAULT on error */
  2588. err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
  2589. if (err)
  2590. goto out_free;
  2591. if ((sock->type == SOCK_RAW &&
  2592. !dev_validate_header(dev, skb->data, len)) || !skb->len) {
  2593. err = -EINVAL;
  2594. goto out_free;
  2595. }
  2596. skb_setup_tx_timestamp(skb, &sockc);
  2597. if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
  2598. !packet_extra_vlan_len_allowed(dev, skb)) {
  2599. err = -EMSGSIZE;
  2600. goto out_free;
  2601. }
  2602. skb->protocol = proto;
  2603. skb->dev = dev;
  2604. skb->priority = sockc.priority;
  2605. skb->mark = sockc.mark;
  2606. skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid);
  2607. if (unlikely(extra_len == 4))
  2608. skb->no_fcs = 1;
  2609. packet_parse_headers(skb, sock);
  2610. if (vnet_hdr_sz) {
  2611. err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
  2612. if (err)
  2613. goto out_free;
  2614. len += vnet_hdr_sz;
  2615. virtio_net_hdr_set_proto(skb, &vnet_hdr);
  2616. }
  2617. err = packet_xmit(po, skb);
  2618. if (unlikely(err != 0)) {
  2619. if (err > 0)
  2620. err = net_xmit_errno(err);
  2621. if (err)
  2622. goto out_unlock;
  2623. }
  2624. dev_put(dev);
  2625. return len;
  2626. out_free:
  2627. kfree_skb(skb);
  2628. out_unlock:
  2629. dev_put(dev);
  2630. out:
  2631. return err;
  2632. }
  2633. static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  2634. {
  2635. struct sock *sk = sock->sk;
  2636. struct packet_sock *po = pkt_sk(sk);
  2637. /* Reading tx_ring.pg_vec without holding pg_vec_lock is racy.
  2638. * tpacket_snd() will redo the check safely.
  2639. */
  2640. if (data_race(po->tx_ring.pg_vec))
  2641. return tpacket_snd(po, msg);
  2642. return packet_snd(sock, msg, len);
  2643. }
  2644. /*
  2645. * Close a PACKET socket. This is fairly simple. We immediately go
  2646. * to 'closed' state and remove our protocol entry in the device list.
  2647. */
  2648. static int packet_release(struct socket *sock)
  2649. {
  2650. struct sock *sk = sock->sk;
  2651. struct packet_sock *po;
  2652. struct packet_fanout *f;
  2653. struct net *net;
  2654. union tpacket_req_u req_u;
  2655. if (!sk)
  2656. return 0;
  2657. net = sock_net(sk);
  2658. po = pkt_sk(sk);
  2659. mutex_lock(&net->packet.sklist_lock);
  2660. sk_del_node_init_rcu(sk);
  2661. mutex_unlock(&net->packet.sklist_lock);
  2662. sock_prot_inuse_add(net, sk->sk_prot, -1);
  2663. spin_lock(&po->bind_lock);
  2664. unregister_prot_hook(sk, false);
  2665. WRITE_ONCE(po->num, 0);
  2666. packet_cached_dev_reset(po);
  2667. if (po->prot_hook.dev) {
  2668. netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker);
  2669. po->prot_hook.dev = NULL;
  2670. }
  2671. spin_unlock(&po->bind_lock);
  2672. packet_flush_mclist(sk);
  2673. lock_sock(sk);
  2674. if (po->rx_ring.pg_vec) {
  2675. memset(&req_u, 0, sizeof(req_u));
  2676. packet_set_ring(sk, &req_u, 1, 0);
  2677. }
  2678. if (po->tx_ring.pg_vec) {
  2679. memset(&req_u, 0, sizeof(req_u));
  2680. packet_set_ring(sk, &req_u, 1, 1);
  2681. }
  2682. release_sock(sk);
  2683. f = fanout_release(sk);
  2684. synchronize_net();
  2685. kfree(po->rollover);
  2686. if (f) {
  2687. fanout_release_data(f);
  2688. kvfree(f);
  2689. }
  2690. /*
  2691. * Now the socket is dead. No more input will appear.
  2692. */
  2693. sock_orphan(sk);
  2694. sock->sk = NULL;
  2695. /* Purge queues */
  2696. skb_queue_purge(&sk->sk_receive_queue);
  2697. packet_free_pending(po);
  2698. sock_put(sk);
  2699. return 0;
  2700. }
  2701. /*
  2702. * Attach a packet hook.
  2703. */
  2704. static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
  2705. __be16 proto)
  2706. {
  2707. struct packet_sock *po = pkt_sk(sk);
  2708. struct net_device *dev = NULL;
  2709. bool unlisted = false;
  2710. bool need_rehook;
  2711. int ret = 0;
  2712. lock_sock(sk);
  2713. spin_lock(&po->bind_lock);
  2714. if (!proto)
  2715. proto = po->num;
  2716. rcu_read_lock();
  2717. if (po->fanout) {
  2718. ret = -EINVAL;
  2719. goto out_unlock;
  2720. }
  2721. if (name) {
  2722. dev = dev_get_by_name_rcu(sock_net(sk), name);
  2723. if (!dev) {
  2724. ret = -ENODEV;
  2725. goto out_unlock;
  2726. }
  2727. } else if (ifindex) {
  2728. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  2729. if (!dev) {
  2730. ret = -ENODEV;
  2731. goto out_unlock;
  2732. }
  2733. }
  2734. need_rehook = po->prot_hook.type != proto || po->prot_hook.dev != dev;
  2735. if (need_rehook) {
  2736. dev_hold(dev);
  2737. if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  2738. rcu_read_unlock();
  2739. /* prevents packet_notifier() from calling
  2740. * register_prot_hook()
  2741. */
  2742. WRITE_ONCE(po->num, 0);
  2743. __unregister_prot_hook(sk, true);
  2744. rcu_read_lock();
  2745. if (dev)
  2746. unlisted = !dev_get_by_index_rcu(sock_net(sk),
  2747. dev->ifindex);
  2748. }
  2749. BUG_ON(packet_sock_flag(po, PACKET_SOCK_RUNNING));
  2750. WRITE_ONCE(po->num, proto);
  2751. po->prot_hook.type = proto;
  2752. netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker);
  2753. if (unlikely(unlisted)) {
  2754. po->prot_hook.dev = NULL;
  2755. WRITE_ONCE(po->ifindex, -1);
  2756. packet_cached_dev_reset(po);
  2757. } else {
  2758. netdev_hold(dev, &po->prot_hook.dev_tracker,
  2759. GFP_ATOMIC);
  2760. po->prot_hook.dev = dev;
  2761. WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
  2762. packet_cached_dev_assign(po, dev);
  2763. }
  2764. dev_put(dev);
  2765. }
  2766. if (proto == 0 || !need_rehook)
  2767. goto out_unlock;
  2768. if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
  2769. register_prot_hook(sk);
  2770. } else {
  2771. sk->sk_err = ENETDOWN;
  2772. if (!sock_flag(sk, SOCK_DEAD))
  2773. sk_error_report(sk);
  2774. }
  2775. out_unlock:
  2776. rcu_read_unlock();
  2777. spin_unlock(&po->bind_lock);
  2778. release_sock(sk);
  2779. return ret;
  2780. }
  2781. /*
  2782. * Bind a packet socket to a device
  2783. */
  2784. static int packet_bind_spkt(struct socket *sock, struct sockaddr_unsized *uaddr,
  2785. int addr_len)
  2786. {
  2787. struct sock *sk = sock->sk;
  2788. struct sockaddr *sa = (struct sockaddr *)uaddr;
  2789. char name[sizeof(sa->sa_data) + 1];
  2790. /*
  2791. * Check legality
  2792. */
  2793. if (addr_len != sizeof(struct sockaddr))
  2794. return -EINVAL;
  2795. /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
  2796. * zero-terminated.
  2797. */
  2798. memcpy(name, sa->sa_data, sizeof(sa->sa_data));
  2799. name[sizeof(sa->sa_data)] = 0;
  2800. return packet_do_bind(sk, name, 0, 0);
  2801. }
  2802. static int packet_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len)
  2803. {
  2804. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  2805. struct sock *sk = sock->sk;
  2806. /*
  2807. * Check legality
  2808. */
  2809. if (addr_len < sizeof(struct sockaddr_ll))
  2810. return -EINVAL;
  2811. if (sll->sll_family != AF_PACKET)
  2812. return -EINVAL;
  2813. return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
  2814. }
  2815. static struct proto packet_proto = {
  2816. .name = "PACKET",
  2817. .owner = THIS_MODULE,
  2818. .obj_size = sizeof(struct packet_sock),
  2819. };
  2820. /*
  2821. * Create a packet of type SOCK_PACKET.
  2822. */
  2823. static int packet_create(struct net *net, struct socket *sock, int protocol,
  2824. int kern)
  2825. {
  2826. struct sock *sk;
  2827. struct packet_sock *po;
  2828. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  2829. int err;
  2830. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  2831. return -EPERM;
  2832. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  2833. sock->type != SOCK_PACKET)
  2834. return -ESOCKTNOSUPPORT;
  2835. sock->state = SS_UNCONNECTED;
  2836. err = -ENOBUFS;
  2837. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
  2838. if (sk == NULL)
  2839. goto out;
  2840. sock->ops = &packet_ops;
  2841. if (sock->type == SOCK_PACKET)
  2842. sock->ops = &packet_ops_spkt;
  2843. po = pkt_sk(sk);
  2844. err = packet_alloc_pending(po);
  2845. if (err)
  2846. goto out_sk_free;
  2847. sock_init_data(sock, sk);
  2848. init_completion(&po->skb_completion);
  2849. sk->sk_family = PF_PACKET;
  2850. po->num = proto;
  2851. packet_cached_dev_reset(po);
  2852. sk->sk_destruct = packet_sock_destruct;
  2853. /*
  2854. * Attach a protocol block
  2855. */
  2856. spin_lock_init(&po->bind_lock);
  2857. mutex_init(&po->pg_vec_lock);
  2858. po->rollover = NULL;
  2859. po->prot_hook.func = packet_rcv;
  2860. if (sock->type == SOCK_PACKET)
  2861. po->prot_hook.func = packet_rcv_spkt;
  2862. po->prot_hook.af_packet_priv = sk;
  2863. po->prot_hook.af_packet_net = sock_net(sk);
  2864. if (proto) {
  2865. po->prot_hook.type = proto;
  2866. __register_prot_hook(sk);
  2867. }
  2868. mutex_lock(&net->packet.sklist_lock);
  2869. sk_add_node_tail_rcu(sk, &net->packet.sklist);
  2870. mutex_unlock(&net->packet.sklist_lock);
  2871. sock_prot_inuse_add(net, &packet_proto, 1);
  2872. return 0;
  2873. out_sk_free:
  2874. sk_free(sk);
  2875. out:
  2876. return err;
  2877. }
  2878. /*
  2879. * Pull a packet from our receive queue and hand it to the user.
  2880. * If necessary we block.
  2881. */
  2882. static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  2883. int flags)
  2884. {
  2885. struct sock *sk = sock->sk;
  2886. struct sk_buff *skb;
  2887. int copied, err;
  2888. int vnet_hdr_len = READ_ONCE(pkt_sk(sk)->vnet_hdr_sz);
  2889. unsigned int origlen = 0;
  2890. err = -EINVAL;
  2891. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  2892. goto out;
  2893. #if 0
  2894. /* What error should we return now? EUNATTACH? */
  2895. if (pkt_sk(sk)->ifindex < 0)
  2896. return -ENODEV;
  2897. #endif
  2898. if (flags & MSG_ERRQUEUE) {
  2899. err = sock_recv_errqueue(sk, msg, len,
  2900. SOL_PACKET, PACKET_TX_TIMESTAMP);
  2901. goto out;
  2902. }
  2903. /*
  2904. * Call the generic datagram receiver. This handles all sorts
  2905. * of horrible races and re-entrancy so we can forget about it
  2906. * in the protocol layers.
  2907. *
  2908. * Now it will return ENETDOWN, if device have just gone down,
  2909. * but then it will block.
  2910. */
  2911. skb = skb_recv_datagram(sk, flags, &err);
  2912. /*
  2913. * An error occurred so return it. Because skb_recv_datagram()
  2914. * handles the blocking we don't see and worry about blocking
  2915. * retries.
  2916. */
  2917. if (skb == NULL)
  2918. goto out;
  2919. packet_rcv_try_clear_pressure(pkt_sk(sk));
  2920. if (vnet_hdr_len) {
  2921. err = packet_rcv_vnet(msg, skb, &len, vnet_hdr_len);
  2922. if (err)
  2923. goto out_free;
  2924. }
  2925. /* You lose any data beyond the buffer you gave. If it worries
  2926. * a user program they can ask the device for its MTU
  2927. * anyway.
  2928. */
  2929. copied = skb->len;
  2930. if (copied > len) {
  2931. copied = len;
  2932. msg->msg_flags |= MSG_TRUNC;
  2933. }
  2934. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2935. if (err)
  2936. goto out_free;
  2937. if (sock->type != SOCK_PACKET) {
  2938. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2939. /* Original length was stored in sockaddr_ll fields */
  2940. origlen = PACKET_SKB_CB(skb)->sa.origlen;
  2941. sll->sll_family = AF_PACKET;
  2942. sll->sll_protocol = (sock->type == SOCK_DGRAM) ?
  2943. vlan_get_protocol_dgram(skb) : skb->protocol;
  2944. }
  2945. sock_recv_cmsgs(msg, sk, skb);
  2946. if (msg->msg_name) {
  2947. const size_t max_len = min(sizeof(skb->cb),
  2948. sizeof(struct sockaddr_storage));
  2949. int copy_len;
  2950. /* If the address length field is there to be filled
  2951. * in, we fill it in now.
  2952. */
  2953. if (sock->type == SOCK_PACKET) {
  2954. __sockaddr_check_size(sizeof(struct sockaddr_pkt));
  2955. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  2956. copy_len = msg->msg_namelen;
  2957. } else {
  2958. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2959. msg->msg_namelen = sll->sll_halen +
  2960. offsetof(struct sockaddr_ll, sll_addr);
  2961. copy_len = msg->msg_namelen;
  2962. if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
  2963. memset(msg->msg_name +
  2964. offsetof(struct sockaddr_ll, sll_addr),
  2965. 0, sizeof(sll->sll_addr));
  2966. msg->msg_namelen = sizeof(struct sockaddr_ll);
  2967. }
  2968. }
  2969. if (WARN_ON_ONCE(copy_len > max_len)) {
  2970. copy_len = max_len;
  2971. msg->msg_namelen = copy_len;
  2972. }
  2973. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
  2974. }
  2975. if (packet_sock_flag(pkt_sk(sk), PACKET_SOCK_AUXDATA)) {
  2976. struct tpacket_auxdata aux;
  2977. aux.tp_status = TP_STATUS_USER;
  2978. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2979. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  2980. else if (skb->pkt_type != PACKET_OUTGOING &&
  2981. skb_csum_unnecessary(skb))
  2982. aux.tp_status |= TP_STATUS_CSUM_VALID;
  2983. if (skb_is_gso(skb) && skb_is_gso_tcp(skb))
  2984. aux.tp_status |= TP_STATUS_GSO_TCP;
  2985. aux.tp_len = origlen;
  2986. aux.tp_snaplen = skb->len;
  2987. aux.tp_mac = 0;
  2988. aux.tp_net = skb_network_offset(skb);
  2989. if (skb_vlan_tag_present(skb)) {
  2990. aux.tp_vlan_tci = skb_vlan_tag_get(skb);
  2991. aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
  2992. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2993. } else if (unlikely(sock->type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
  2994. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2995. struct net_device *dev;
  2996. rcu_read_lock();
  2997. dev = dev_get_by_index_rcu(sock_net(sk), sll->sll_ifindex);
  2998. if (dev) {
  2999. aux.tp_vlan_tci = vlan_get_tci(skb, dev);
  3000. aux.tp_vlan_tpid = ntohs(skb->protocol);
  3001. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  3002. } else {
  3003. aux.tp_vlan_tci = 0;
  3004. aux.tp_vlan_tpid = 0;
  3005. }
  3006. rcu_read_unlock();
  3007. } else {
  3008. aux.tp_vlan_tci = 0;
  3009. aux.tp_vlan_tpid = 0;
  3010. }
  3011. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  3012. }
  3013. /*
  3014. * Free or return the buffer as appropriate. Again this
  3015. * hides all the races and re-entrancy issues from us.
  3016. */
  3017. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  3018. out_free:
  3019. skb_free_datagram(sk, skb);
  3020. out:
  3021. return err;
  3022. }
  3023. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  3024. int peer)
  3025. {
  3026. struct net_device *dev;
  3027. struct sock *sk = sock->sk;
  3028. if (peer)
  3029. return -EOPNOTSUPP;
  3030. uaddr->sa_family = AF_PACKET;
  3031. memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
  3032. rcu_read_lock();
  3033. dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
  3034. if (dev)
  3035. strscpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
  3036. rcu_read_unlock();
  3037. return sizeof(*uaddr);
  3038. }
  3039. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  3040. int peer)
  3041. {
  3042. struct net_device *dev;
  3043. struct sock *sk = sock->sk;
  3044. struct packet_sock *po = pkt_sk(sk);
  3045. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  3046. int ifindex;
  3047. if (peer)
  3048. return -EOPNOTSUPP;
  3049. ifindex = READ_ONCE(po->ifindex);
  3050. sll->sll_family = AF_PACKET;
  3051. sll->sll_ifindex = ifindex;
  3052. sll->sll_protocol = READ_ONCE(po->num);
  3053. sll->sll_pkttype = 0;
  3054. rcu_read_lock();
  3055. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  3056. if (dev) {
  3057. sll->sll_hatype = dev->type;
  3058. sll->sll_halen = dev->addr_len;
  3059. /* Let __fortify_memcpy_chk() know the actual buffer size. */
  3060. memcpy(((struct sockaddr_storage *)sll)->__data +
  3061. offsetof(struct sockaddr_ll, sll_addr) -
  3062. offsetofend(struct sockaddr_ll, sll_family),
  3063. dev->dev_addr, dev->addr_len);
  3064. } else {
  3065. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  3066. sll->sll_halen = 0;
  3067. }
  3068. rcu_read_unlock();
  3069. return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  3070. }
  3071. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  3072. int what)
  3073. {
  3074. switch (i->type) {
  3075. case PACKET_MR_MULTICAST:
  3076. if (i->alen != dev->addr_len)
  3077. return -EINVAL;
  3078. if (what > 0)
  3079. return dev_mc_add(dev, i->addr);
  3080. else
  3081. return dev_mc_del(dev, i->addr);
  3082. break;
  3083. case PACKET_MR_PROMISC:
  3084. return dev_set_promiscuity(dev, what);
  3085. case PACKET_MR_ALLMULTI:
  3086. return dev_set_allmulti(dev, what);
  3087. case PACKET_MR_UNICAST:
  3088. if (i->alen != dev->addr_len)
  3089. return -EINVAL;
  3090. if (what > 0)
  3091. return dev_uc_add(dev, i->addr);
  3092. else
  3093. return dev_uc_del(dev, i->addr);
  3094. break;
  3095. default:
  3096. break;
  3097. }
  3098. return 0;
  3099. }
  3100. static void packet_dev_mclist_delete(struct net_device *dev,
  3101. struct packet_mclist **mlp,
  3102. struct list_head *list)
  3103. {
  3104. struct packet_mclist *ml;
  3105. while ((ml = *mlp) != NULL) {
  3106. if (ml->ifindex == dev->ifindex) {
  3107. list_add(&ml->remove_list, list);
  3108. *mlp = ml->next;
  3109. } else
  3110. mlp = &ml->next;
  3111. }
  3112. }
  3113. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  3114. {
  3115. struct packet_sock *po = pkt_sk(sk);
  3116. struct packet_mclist *ml, *i;
  3117. struct net_device *dev;
  3118. int err;
  3119. rtnl_lock();
  3120. err = -ENODEV;
  3121. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  3122. if (!dev)
  3123. goto done;
  3124. err = -EINVAL;
  3125. if (mreq->mr_alen > dev->addr_len)
  3126. goto done;
  3127. err = -ENOBUFS;
  3128. i = kmalloc_obj(*i);
  3129. if (i == NULL)
  3130. goto done;
  3131. err = 0;
  3132. for (ml = po->mclist; ml; ml = ml->next) {
  3133. if (ml->ifindex == mreq->mr_ifindex &&
  3134. ml->type == mreq->mr_type &&
  3135. ml->alen == mreq->mr_alen &&
  3136. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3137. ml->count++;
  3138. /* Free the new element ... */
  3139. kfree(i);
  3140. goto done;
  3141. }
  3142. }
  3143. i->type = mreq->mr_type;
  3144. i->ifindex = mreq->mr_ifindex;
  3145. i->alen = mreq->mr_alen;
  3146. memcpy(i->addr, mreq->mr_address, i->alen);
  3147. memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
  3148. i->count = 1;
  3149. INIT_LIST_HEAD(&i->remove_list);
  3150. i->next = po->mclist;
  3151. po->mclist = i;
  3152. err = packet_dev_mc(dev, i, 1);
  3153. if (err) {
  3154. po->mclist = i->next;
  3155. kfree(i);
  3156. }
  3157. done:
  3158. rtnl_unlock();
  3159. return err;
  3160. }
  3161. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  3162. {
  3163. struct packet_mclist *ml, **mlp;
  3164. rtnl_lock();
  3165. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  3166. if (ml->ifindex == mreq->mr_ifindex &&
  3167. ml->type == mreq->mr_type &&
  3168. ml->alen == mreq->mr_alen &&
  3169. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3170. if (--ml->count == 0) {
  3171. struct net_device *dev;
  3172. *mlp = ml->next;
  3173. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3174. if (dev)
  3175. packet_dev_mc(dev, ml, -1);
  3176. kfree(ml);
  3177. }
  3178. break;
  3179. }
  3180. }
  3181. rtnl_unlock();
  3182. return 0;
  3183. }
  3184. static void packet_flush_mclist(struct sock *sk)
  3185. {
  3186. struct packet_sock *po = pkt_sk(sk);
  3187. struct packet_mclist *ml;
  3188. if (!po->mclist)
  3189. return;
  3190. rtnl_lock();
  3191. while ((ml = po->mclist) != NULL) {
  3192. struct net_device *dev;
  3193. po->mclist = ml->next;
  3194. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3195. if (dev != NULL)
  3196. packet_dev_mc(dev, ml, -1);
  3197. kfree(ml);
  3198. }
  3199. rtnl_unlock();
  3200. }
  3201. static int
  3202. packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
  3203. unsigned int optlen)
  3204. {
  3205. struct sock *sk = sock->sk;
  3206. struct packet_sock *po = pkt_sk(sk);
  3207. int ret;
  3208. if (level != SOL_PACKET)
  3209. return -ENOPROTOOPT;
  3210. switch (optname) {
  3211. case PACKET_ADD_MEMBERSHIP:
  3212. case PACKET_DROP_MEMBERSHIP:
  3213. {
  3214. struct packet_mreq_max mreq;
  3215. int len = optlen;
  3216. memset(&mreq, 0, sizeof(mreq));
  3217. if (len < sizeof(struct packet_mreq))
  3218. return -EINVAL;
  3219. if (len > sizeof(mreq))
  3220. len = sizeof(mreq);
  3221. if (copy_from_sockptr(&mreq, optval, len))
  3222. return -EFAULT;
  3223. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  3224. return -EINVAL;
  3225. if (optname == PACKET_ADD_MEMBERSHIP)
  3226. ret = packet_mc_add(sk, &mreq);
  3227. else
  3228. ret = packet_mc_drop(sk, &mreq);
  3229. return ret;
  3230. }
  3231. case PACKET_RX_RING:
  3232. case PACKET_TX_RING:
  3233. {
  3234. union tpacket_req_u req_u;
  3235. ret = -EINVAL;
  3236. lock_sock(sk);
  3237. switch (po->tp_version) {
  3238. case TPACKET_V1:
  3239. case TPACKET_V2:
  3240. if (optlen < sizeof(req_u.req))
  3241. break;
  3242. ret = copy_from_sockptr(&req_u.req, optval,
  3243. sizeof(req_u.req)) ?
  3244. -EINVAL : 0;
  3245. break;
  3246. case TPACKET_V3:
  3247. default:
  3248. if (optlen < sizeof(req_u.req3))
  3249. break;
  3250. ret = copy_from_sockptr(&req_u.req3, optval,
  3251. sizeof(req_u.req3)) ?
  3252. -EINVAL : 0;
  3253. break;
  3254. }
  3255. if (!ret)
  3256. ret = packet_set_ring(sk, &req_u, 0,
  3257. optname == PACKET_TX_RING);
  3258. release_sock(sk);
  3259. return ret;
  3260. }
  3261. case PACKET_COPY_THRESH:
  3262. {
  3263. int val;
  3264. if (optlen != sizeof(val))
  3265. return -EINVAL;
  3266. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3267. return -EFAULT;
  3268. WRITE_ONCE(pkt_sk(sk)->copy_thresh, val);
  3269. return 0;
  3270. }
  3271. case PACKET_VERSION:
  3272. {
  3273. int val;
  3274. if (optlen != sizeof(val))
  3275. return -EINVAL;
  3276. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3277. return -EFAULT;
  3278. switch (val) {
  3279. case TPACKET_V1:
  3280. case TPACKET_V2:
  3281. case TPACKET_V3:
  3282. break;
  3283. default:
  3284. return -EINVAL;
  3285. }
  3286. lock_sock(sk);
  3287. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3288. ret = -EBUSY;
  3289. } else {
  3290. po->tp_version = val;
  3291. ret = 0;
  3292. }
  3293. release_sock(sk);
  3294. return ret;
  3295. }
  3296. case PACKET_RESERVE:
  3297. {
  3298. unsigned int val;
  3299. if (optlen != sizeof(val))
  3300. return -EINVAL;
  3301. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3302. return -EFAULT;
  3303. if (val > INT_MAX)
  3304. return -EINVAL;
  3305. lock_sock(sk);
  3306. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3307. ret = -EBUSY;
  3308. } else {
  3309. po->tp_reserve = val;
  3310. ret = 0;
  3311. }
  3312. release_sock(sk);
  3313. return ret;
  3314. }
  3315. case PACKET_LOSS:
  3316. {
  3317. unsigned int val;
  3318. if (optlen != sizeof(val))
  3319. return -EINVAL;
  3320. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3321. return -EFAULT;
  3322. lock_sock(sk);
  3323. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3324. ret = -EBUSY;
  3325. } else {
  3326. packet_sock_flag_set(po, PACKET_SOCK_TP_LOSS, val);
  3327. ret = 0;
  3328. }
  3329. release_sock(sk);
  3330. return ret;
  3331. }
  3332. case PACKET_AUXDATA:
  3333. {
  3334. int val;
  3335. if (optlen < sizeof(val))
  3336. return -EINVAL;
  3337. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3338. return -EFAULT;
  3339. packet_sock_flag_set(po, PACKET_SOCK_AUXDATA, val);
  3340. return 0;
  3341. }
  3342. case PACKET_ORIGDEV:
  3343. {
  3344. int val;
  3345. if (optlen < sizeof(val))
  3346. return -EINVAL;
  3347. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3348. return -EFAULT;
  3349. packet_sock_flag_set(po, PACKET_SOCK_ORIGDEV, val);
  3350. return 0;
  3351. }
  3352. case PACKET_VNET_HDR:
  3353. case PACKET_VNET_HDR_SZ:
  3354. {
  3355. int val, hdr_len;
  3356. if (sock->type != SOCK_RAW)
  3357. return -EINVAL;
  3358. if (optlen < sizeof(val))
  3359. return -EINVAL;
  3360. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3361. return -EFAULT;
  3362. if (optname == PACKET_VNET_HDR_SZ) {
  3363. if (val && val != sizeof(struct virtio_net_hdr) &&
  3364. val != sizeof(struct virtio_net_hdr_mrg_rxbuf))
  3365. return -EINVAL;
  3366. hdr_len = val;
  3367. } else {
  3368. hdr_len = val ? sizeof(struct virtio_net_hdr) : 0;
  3369. }
  3370. lock_sock(sk);
  3371. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3372. ret = -EBUSY;
  3373. } else {
  3374. WRITE_ONCE(po->vnet_hdr_sz, hdr_len);
  3375. ret = 0;
  3376. }
  3377. release_sock(sk);
  3378. return ret;
  3379. }
  3380. case PACKET_TIMESTAMP:
  3381. {
  3382. int val;
  3383. if (optlen != sizeof(val))
  3384. return -EINVAL;
  3385. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3386. return -EFAULT;
  3387. WRITE_ONCE(po->tp_tstamp, val);
  3388. return 0;
  3389. }
  3390. case PACKET_FANOUT:
  3391. {
  3392. struct fanout_args args = { 0 };
  3393. if (optlen != sizeof(int) && optlen != sizeof(args))
  3394. return -EINVAL;
  3395. if (copy_from_sockptr(&args, optval, optlen))
  3396. return -EFAULT;
  3397. return fanout_add(sk, &args);
  3398. }
  3399. case PACKET_FANOUT_DATA:
  3400. {
  3401. /* Paired with the WRITE_ONCE() in fanout_add() */
  3402. if (!READ_ONCE(po->fanout))
  3403. return -EINVAL;
  3404. return fanout_set_data(po, optval, optlen);
  3405. }
  3406. case PACKET_IGNORE_OUTGOING:
  3407. {
  3408. int val;
  3409. if (optlen != sizeof(val))
  3410. return -EINVAL;
  3411. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3412. return -EFAULT;
  3413. if (val < 0 || val > 1)
  3414. return -EINVAL;
  3415. WRITE_ONCE(po->prot_hook.ignore_outgoing, !!val);
  3416. return 0;
  3417. }
  3418. case PACKET_TX_HAS_OFF:
  3419. {
  3420. unsigned int val;
  3421. if (optlen != sizeof(val))
  3422. return -EINVAL;
  3423. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3424. return -EFAULT;
  3425. lock_sock(sk);
  3426. if (!po->rx_ring.pg_vec && !po->tx_ring.pg_vec)
  3427. packet_sock_flag_set(po, PACKET_SOCK_TX_HAS_OFF, val);
  3428. release_sock(sk);
  3429. return 0;
  3430. }
  3431. case PACKET_QDISC_BYPASS:
  3432. {
  3433. int val;
  3434. if (optlen != sizeof(val))
  3435. return -EINVAL;
  3436. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3437. return -EFAULT;
  3438. packet_sock_flag_set(po, PACKET_SOCK_QDISC_BYPASS, val);
  3439. return 0;
  3440. }
  3441. default:
  3442. return -ENOPROTOOPT;
  3443. }
  3444. }
  3445. static int packet_getsockopt(struct socket *sock, int level, int optname,
  3446. char __user *optval, int __user *optlen)
  3447. {
  3448. int len;
  3449. int val, lv = sizeof(val);
  3450. struct sock *sk = sock->sk;
  3451. struct packet_sock *po = pkt_sk(sk);
  3452. void *data = &val;
  3453. union tpacket_stats_u st;
  3454. struct tpacket_rollover_stats rstats;
  3455. int drops;
  3456. if (level != SOL_PACKET)
  3457. return -ENOPROTOOPT;
  3458. if (get_user(len, optlen))
  3459. return -EFAULT;
  3460. if (len < 0)
  3461. return -EINVAL;
  3462. switch (optname) {
  3463. case PACKET_STATISTICS:
  3464. spin_lock_bh(&sk->sk_receive_queue.lock);
  3465. memcpy(&st, &po->stats, sizeof(st));
  3466. memset(&po->stats, 0, sizeof(po->stats));
  3467. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3468. drops = atomic_xchg(&po->tp_drops, 0);
  3469. if (po->tp_version == TPACKET_V3) {
  3470. lv = sizeof(struct tpacket_stats_v3);
  3471. st.stats3.tp_drops = drops;
  3472. st.stats3.tp_packets += drops;
  3473. data = &st.stats3;
  3474. } else {
  3475. lv = sizeof(struct tpacket_stats);
  3476. st.stats1.tp_drops = drops;
  3477. st.stats1.tp_packets += drops;
  3478. data = &st.stats1;
  3479. }
  3480. break;
  3481. case PACKET_AUXDATA:
  3482. val = packet_sock_flag(po, PACKET_SOCK_AUXDATA);
  3483. break;
  3484. case PACKET_ORIGDEV:
  3485. val = packet_sock_flag(po, PACKET_SOCK_ORIGDEV);
  3486. break;
  3487. case PACKET_VNET_HDR:
  3488. val = !!READ_ONCE(po->vnet_hdr_sz);
  3489. break;
  3490. case PACKET_VNET_HDR_SZ:
  3491. val = READ_ONCE(po->vnet_hdr_sz);
  3492. break;
  3493. case PACKET_COPY_THRESH:
  3494. val = READ_ONCE(pkt_sk(sk)->copy_thresh);
  3495. break;
  3496. case PACKET_VERSION:
  3497. val = po->tp_version;
  3498. break;
  3499. case PACKET_HDRLEN:
  3500. if (len > sizeof(int))
  3501. len = sizeof(int);
  3502. if (len < sizeof(int))
  3503. return -EINVAL;
  3504. if (copy_from_user(&val, optval, len))
  3505. return -EFAULT;
  3506. switch (val) {
  3507. case TPACKET_V1:
  3508. val = sizeof(struct tpacket_hdr);
  3509. break;
  3510. case TPACKET_V2:
  3511. val = sizeof(struct tpacket2_hdr);
  3512. break;
  3513. case TPACKET_V3:
  3514. val = sizeof(struct tpacket3_hdr);
  3515. break;
  3516. default:
  3517. return -EINVAL;
  3518. }
  3519. break;
  3520. case PACKET_RESERVE:
  3521. val = po->tp_reserve;
  3522. break;
  3523. case PACKET_LOSS:
  3524. val = packet_sock_flag(po, PACKET_SOCK_TP_LOSS);
  3525. break;
  3526. case PACKET_TIMESTAMP:
  3527. val = READ_ONCE(po->tp_tstamp);
  3528. break;
  3529. case PACKET_FANOUT:
  3530. val = (po->fanout ?
  3531. ((u32)po->fanout->id |
  3532. ((u32)po->fanout->type << 16) |
  3533. ((u32)po->fanout->flags << 24)) :
  3534. 0);
  3535. break;
  3536. case PACKET_IGNORE_OUTGOING:
  3537. val = READ_ONCE(po->prot_hook.ignore_outgoing);
  3538. break;
  3539. case PACKET_ROLLOVER_STATS:
  3540. if (!po->rollover)
  3541. return -EINVAL;
  3542. rstats.tp_all = atomic_long_read(&po->rollover->num);
  3543. rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
  3544. rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
  3545. data = &rstats;
  3546. lv = sizeof(rstats);
  3547. break;
  3548. case PACKET_TX_HAS_OFF:
  3549. val = packet_sock_flag(po, PACKET_SOCK_TX_HAS_OFF);
  3550. break;
  3551. case PACKET_QDISC_BYPASS:
  3552. val = packet_sock_flag(po, PACKET_SOCK_QDISC_BYPASS);
  3553. break;
  3554. default:
  3555. return -ENOPROTOOPT;
  3556. }
  3557. if (len > lv)
  3558. len = lv;
  3559. if (put_user(len, optlen))
  3560. return -EFAULT;
  3561. if (copy_to_user(optval, data, len))
  3562. return -EFAULT;
  3563. return 0;
  3564. }
  3565. static int packet_notifier(struct notifier_block *this,
  3566. unsigned long msg, void *ptr)
  3567. {
  3568. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3569. struct net *net = dev_net(dev);
  3570. struct packet_mclist *ml, *tmp;
  3571. LIST_HEAD(mclist);
  3572. struct sock *sk;
  3573. rcu_read_lock();
  3574. sk_for_each_rcu(sk, &net->packet.sklist) {
  3575. struct packet_sock *po = pkt_sk(sk);
  3576. switch (msg) {
  3577. case NETDEV_UNREGISTER:
  3578. if (po->mclist)
  3579. packet_dev_mclist_delete(dev, &po->mclist,
  3580. &mclist);
  3581. fallthrough;
  3582. case NETDEV_DOWN:
  3583. if (dev->ifindex == po->ifindex) {
  3584. spin_lock(&po->bind_lock);
  3585. if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  3586. __unregister_prot_hook(sk, false);
  3587. sk->sk_err = ENETDOWN;
  3588. if (!sock_flag(sk, SOCK_DEAD))
  3589. sk_error_report(sk);
  3590. }
  3591. if (msg == NETDEV_UNREGISTER) {
  3592. packet_cached_dev_reset(po);
  3593. WRITE_ONCE(po->ifindex, -1);
  3594. netdev_put(po->prot_hook.dev,
  3595. &po->prot_hook.dev_tracker);
  3596. po->prot_hook.dev = NULL;
  3597. }
  3598. spin_unlock(&po->bind_lock);
  3599. }
  3600. break;
  3601. case NETDEV_UP:
  3602. if (dev->ifindex == po->ifindex) {
  3603. spin_lock(&po->bind_lock);
  3604. if (po->num)
  3605. register_prot_hook(sk);
  3606. spin_unlock(&po->bind_lock);
  3607. }
  3608. break;
  3609. }
  3610. }
  3611. rcu_read_unlock();
  3612. /* packet_dev_mc might grab instance locks so can't run under rcu */
  3613. list_for_each_entry_safe(ml, tmp, &mclist, remove_list) {
  3614. packet_dev_mc(dev, ml, -1);
  3615. kfree(ml);
  3616. }
  3617. return NOTIFY_DONE;
  3618. }
  3619. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  3620. unsigned long arg)
  3621. {
  3622. struct sock *sk = sock->sk;
  3623. switch (cmd) {
  3624. case SIOCOUTQ:
  3625. {
  3626. int amount = sk_wmem_alloc_get(sk);
  3627. return put_user(amount, (int __user *)arg);
  3628. }
  3629. case SIOCINQ:
  3630. {
  3631. struct sk_buff *skb;
  3632. int amount = 0;
  3633. spin_lock_bh(&sk->sk_receive_queue.lock);
  3634. skb = skb_peek(&sk->sk_receive_queue);
  3635. if (skb)
  3636. amount = skb->len;
  3637. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3638. return put_user(amount, (int __user *)arg);
  3639. }
  3640. #ifdef CONFIG_INET
  3641. case SIOCADDRT:
  3642. case SIOCDELRT:
  3643. case SIOCDARP:
  3644. case SIOCGARP:
  3645. case SIOCSARP:
  3646. case SIOCGIFADDR:
  3647. case SIOCSIFADDR:
  3648. case SIOCGIFBRDADDR:
  3649. case SIOCSIFBRDADDR:
  3650. case SIOCGIFNETMASK:
  3651. case SIOCSIFNETMASK:
  3652. case SIOCGIFDSTADDR:
  3653. case SIOCSIFDSTADDR:
  3654. case SIOCSIFFLAGS:
  3655. return inet_dgram_ops.ioctl(sock, cmd, arg);
  3656. #endif
  3657. default:
  3658. return -ENOIOCTLCMD;
  3659. }
  3660. return 0;
  3661. }
  3662. static __poll_t packet_poll(struct file *file, struct socket *sock,
  3663. poll_table *wait)
  3664. {
  3665. struct sock *sk = sock->sk;
  3666. struct packet_sock *po = pkt_sk(sk);
  3667. __poll_t mask = datagram_poll(file, sock, wait);
  3668. spin_lock_bh(&sk->sk_receive_queue.lock);
  3669. if (po->rx_ring.pg_vec) {
  3670. if (!packet_previous_rx_frame(po, &po->rx_ring,
  3671. TP_STATUS_KERNEL))
  3672. mask |= EPOLLIN | EPOLLRDNORM;
  3673. }
  3674. packet_rcv_try_clear_pressure(po);
  3675. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3676. spin_lock_bh(&sk->sk_write_queue.lock);
  3677. if (po->tx_ring.pg_vec) {
  3678. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  3679. mask |= EPOLLOUT | EPOLLWRNORM;
  3680. }
  3681. spin_unlock_bh(&sk->sk_write_queue.lock);
  3682. return mask;
  3683. }
  3684. /* Dirty? Well, I still did not learn better way to account
  3685. * for user mmaps.
  3686. */
  3687. static void packet_mm_open(struct vm_area_struct *vma)
  3688. {
  3689. struct file *file = vma->vm_file;
  3690. struct socket *sock = file->private_data;
  3691. struct sock *sk = sock->sk;
  3692. if (sk)
  3693. atomic_long_inc(&pkt_sk(sk)->mapped);
  3694. }
  3695. static void packet_mm_close(struct vm_area_struct *vma)
  3696. {
  3697. struct file *file = vma->vm_file;
  3698. struct socket *sock = file->private_data;
  3699. struct sock *sk = sock->sk;
  3700. if (sk)
  3701. atomic_long_dec(&pkt_sk(sk)->mapped);
  3702. }
  3703. static const struct vm_operations_struct packet_mmap_ops = {
  3704. .open = packet_mm_open,
  3705. .close = packet_mm_close,
  3706. };
  3707. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  3708. unsigned int len)
  3709. {
  3710. int i;
  3711. for (i = 0; i < len; i++) {
  3712. if (likely(pg_vec[i].buffer)) {
  3713. if (is_vmalloc_addr(pg_vec[i].buffer))
  3714. vfree(pg_vec[i].buffer);
  3715. else
  3716. free_pages((unsigned long)pg_vec[i].buffer,
  3717. order);
  3718. pg_vec[i].buffer = NULL;
  3719. }
  3720. }
  3721. kfree(pg_vec);
  3722. }
  3723. static char *alloc_one_pg_vec_page(unsigned long order)
  3724. {
  3725. char *buffer;
  3726. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  3727. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  3728. buffer = (char *) __get_free_pages(gfp_flags, order);
  3729. if (buffer)
  3730. return buffer;
  3731. /* __get_free_pages failed, fall back to vmalloc */
  3732. buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
  3733. if (buffer)
  3734. return buffer;
  3735. /* vmalloc failed, lets dig into swap here */
  3736. gfp_flags &= ~__GFP_NORETRY;
  3737. buffer = (char *) __get_free_pages(gfp_flags, order);
  3738. if (buffer)
  3739. return buffer;
  3740. /* complete and utter failure */
  3741. return NULL;
  3742. }
  3743. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  3744. {
  3745. unsigned int block_nr = req->tp_block_nr;
  3746. struct pgv *pg_vec;
  3747. int i;
  3748. pg_vec = kzalloc_objs(struct pgv, block_nr, GFP_KERNEL | __GFP_NOWARN);
  3749. if (unlikely(!pg_vec))
  3750. goto out;
  3751. for (i = 0; i < block_nr; i++) {
  3752. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  3753. if (unlikely(!pg_vec[i].buffer))
  3754. goto out_free_pgvec;
  3755. }
  3756. out:
  3757. return pg_vec;
  3758. out_free_pgvec:
  3759. free_pg_vec(pg_vec, order, block_nr);
  3760. pg_vec = NULL;
  3761. goto out;
  3762. }
  3763. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  3764. int closing, int tx_ring)
  3765. {
  3766. struct pgv *pg_vec = NULL;
  3767. struct packet_sock *po = pkt_sk(sk);
  3768. unsigned long *rx_owner_map = NULL;
  3769. int was_running, order = 0;
  3770. struct packet_ring_buffer *rb;
  3771. struct sk_buff_head *rb_queue;
  3772. __be16 num;
  3773. int err;
  3774. /* Added to avoid minimal code churn */
  3775. struct tpacket_req *req = &req_u->req;
  3776. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3777. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3778. err = -EBUSY;
  3779. if (!closing) {
  3780. if (atomic_long_read(&po->mapped))
  3781. goto out;
  3782. if (packet_read_pending(rb))
  3783. goto out;
  3784. }
  3785. if (req->tp_block_nr) {
  3786. unsigned int min_frame_size;
  3787. /* Sanity tests and some calculations */
  3788. err = -EBUSY;
  3789. if (unlikely(rb->pg_vec))
  3790. goto out;
  3791. switch (po->tp_version) {
  3792. case TPACKET_V1:
  3793. po->tp_hdrlen = TPACKET_HDRLEN;
  3794. break;
  3795. case TPACKET_V2:
  3796. po->tp_hdrlen = TPACKET2_HDRLEN;
  3797. break;
  3798. case TPACKET_V3:
  3799. po->tp_hdrlen = TPACKET3_HDRLEN;
  3800. break;
  3801. }
  3802. err = -EINVAL;
  3803. if (unlikely((int)req->tp_block_size <= 0))
  3804. goto out;
  3805. if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
  3806. goto out;
  3807. min_frame_size = po->tp_hdrlen + po->tp_reserve;
  3808. if (po->tp_version >= TPACKET_V3 &&
  3809. req->tp_block_size <
  3810. BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
  3811. goto out;
  3812. if (unlikely(req->tp_frame_size < min_frame_size))
  3813. goto out;
  3814. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3815. goto out;
  3816. rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
  3817. if (unlikely(rb->frames_per_block == 0))
  3818. goto out;
  3819. if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
  3820. goto out;
  3821. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3822. req->tp_frame_nr))
  3823. goto out;
  3824. err = -ENOMEM;
  3825. order = get_order(req->tp_block_size);
  3826. pg_vec = alloc_pg_vec(req, order);
  3827. if (unlikely(!pg_vec))
  3828. goto out;
  3829. switch (po->tp_version) {
  3830. case TPACKET_V3:
  3831. /* Block transmit is not supported yet */
  3832. if (!tx_ring) {
  3833. init_prb_bdqc(po, rb, pg_vec, req_u);
  3834. } else {
  3835. struct tpacket_req3 *req3 = &req_u->req3;
  3836. if (req3->tp_retire_blk_tov ||
  3837. req3->tp_sizeof_priv ||
  3838. req3->tp_feature_req_word) {
  3839. err = -EINVAL;
  3840. goto out_free_pg_vec;
  3841. }
  3842. }
  3843. break;
  3844. default:
  3845. if (!tx_ring) {
  3846. rx_owner_map = bitmap_alloc(req->tp_frame_nr,
  3847. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  3848. if (!rx_owner_map)
  3849. goto out_free_pg_vec;
  3850. }
  3851. break;
  3852. }
  3853. }
  3854. /* Done */
  3855. else {
  3856. err = -EINVAL;
  3857. if (unlikely(req->tp_frame_nr))
  3858. goto out;
  3859. }
  3860. /* Detach socket from network */
  3861. spin_lock(&po->bind_lock);
  3862. was_running = packet_sock_flag(po, PACKET_SOCK_RUNNING);
  3863. num = po->num;
  3864. WRITE_ONCE(po->num, 0);
  3865. if (was_running)
  3866. __unregister_prot_hook(sk, false);
  3867. spin_unlock(&po->bind_lock);
  3868. synchronize_net();
  3869. err = -EBUSY;
  3870. mutex_lock(&po->pg_vec_lock);
  3871. if (closing || atomic_long_read(&po->mapped) == 0) {
  3872. err = 0;
  3873. spin_lock_bh(&rb_queue->lock);
  3874. swap(rb->pg_vec, pg_vec);
  3875. if (po->tp_version <= TPACKET_V2)
  3876. swap(rb->rx_owner_map, rx_owner_map);
  3877. rb->frame_max = (req->tp_frame_nr - 1);
  3878. rb->head = 0;
  3879. rb->frame_size = req->tp_frame_size;
  3880. spin_unlock_bh(&rb_queue->lock);
  3881. swap(rb->pg_vec_order, order);
  3882. swap(rb->pg_vec_len, req->tp_block_nr);
  3883. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3884. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3885. tpacket_rcv : packet_rcv;
  3886. skb_queue_purge(rb_queue);
  3887. if (atomic_long_read(&po->mapped))
  3888. pr_err("packet_mmap: vma is busy: %ld\n",
  3889. atomic_long_read(&po->mapped));
  3890. }
  3891. mutex_unlock(&po->pg_vec_lock);
  3892. spin_lock(&po->bind_lock);
  3893. WRITE_ONCE(po->num, num);
  3894. if (was_running)
  3895. register_prot_hook(sk);
  3896. spin_unlock(&po->bind_lock);
  3897. if (pg_vec && (po->tp_version > TPACKET_V2)) {
  3898. /* Because we don't support block-based V3 on tx-ring */
  3899. if (!tx_ring)
  3900. prb_shutdown_retire_blk_timer(po, rb_queue);
  3901. }
  3902. out_free_pg_vec:
  3903. if (pg_vec) {
  3904. bitmap_free(rx_owner_map);
  3905. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3906. }
  3907. out:
  3908. return err;
  3909. }
  3910. static int packet_mmap(struct file *file, struct socket *sock,
  3911. struct vm_area_struct *vma)
  3912. {
  3913. struct sock *sk = sock->sk;
  3914. struct packet_sock *po = pkt_sk(sk);
  3915. unsigned long size, expected_size;
  3916. struct packet_ring_buffer *rb;
  3917. unsigned long start;
  3918. int err = -EINVAL;
  3919. int i;
  3920. if (vma->vm_pgoff)
  3921. return -EINVAL;
  3922. mutex_lock(&po->pg_vec_lock);
  3923. expected_size = 0;
  3924. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3925. if (rb->pg_vec) {
  3926. expected_size += rb->pg_vec_len
  3927. * rb->pg_vec_pages
  3928. * PAGE_SIZE;
  3929. }
  3930. }
  3931. if (expected_size == 0)
  3932. goto out;
  3933. size = vma->vm_end - vma->vm_start;
  3934. if (size != expected_size)
  3935. goto out;
  3936. start = vma->vm_start;
  3937. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3938. if (rb->pg_vec == NULL)
  3939. continue;
  3940. for (i = 0; i < rb->pg_vec_len; i++) {
  3941. struct page *page;
  3942. void *kaddr = rb->pg_vec[i].buffer;
  3943. int pg_num;
  3944. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3945. page = pgv_to_page(kaddr);
  3946. err = vm_insert_page(vma, start, page);
  3947. if (unlikely(err))
  3948. goto out;
  3949. start += PAGE_SIZE;
  3950. kaddr += PAGE_SIZE;
  3951. }
  3952. }
  3953. }
  3954. atomic_long_inc(&po->mapped);
  3955. vma->vm_ops = &packet_mmap_ops;
  3956. err = 0;
  3957. out:
  3958. mutex_unlock(&po->pg_vec_lock);
  3959. return err;
  3960. }
  3961. static const struct proto_ops packet_ops_spkt = {
  3962. .family = PF_PACKET,
  3963. .owner = THIS_MODULE,
  3964. .release = packet_release,
  3965. .bind = packet_bind_spkt,
  3966. .connect = sock_no_connect,
  3967. .socketpair = sock_no_socketpair,
  3968. .accept = sock_no_accept,
  3969. .getname = packet_getname_spkt,
  3970. .poll = datagram_poll,
  3971. .ioctl = packet_ioctl,
  3972. .gettstamp = sock_gettstamp,
  3973. .listen = sock_no_listen,
  3974. .shutdown = sock_no_shutdown,
  3975. .sendmsg = packet_sendmsg_spkt,
  3976. .recvmsg = packet_recvmsg,
  3977. .mmap = sock_no_mmap,
  3978. };
  3979. static const struct proto_ops packet_ops = {
  3980. .family = PF_PACKET,
  3981. .owner = THIS_MODULE,
  3982. .release = packet_release,
  3983. .bind = packet_bind,
  3984. .connect = sock_no_connect,
  3985. .socketpair = sock_no_socketpair,
  3986. .accept = sock_no_accept,
  3987. .getname = packet_getname,
  3988. .poll = packet_poll,
  3989. .ioctl = packet_ioctl,
  3990. .gettstamp = sock_gettstamp,
  3991. .listen = sock_no_listen,
  3992. .shutdown = sock_no_shutdown,
  3993. .setsockopt = packet_setsockopt,
  3994. .getsockopt = packet_getsockopt,
  3995. .sendmsg = packet_sendmsg,
  3996. .recvmsg = packet_recvmsg,
  3997. .mmap = packet_mmap,
  3998. };
  3999. static const struct net_proto_family packet_family_ops = {
  4000. .family = PF_PACKET,
  4001. .create = packet_create,
  4002. .owner = THIS_MODULE,
  4003. };
  4004. static struct notifier_block packet_netdev_notifier = {
  4005. .notifier_call = packet_notifier,
  4006. };
  4007. #ifdef CONFIG_PROC_FS
  4008. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  4009. __acquires(RCU)
  4010. {
  4011. struct net *net = seq_file_net(seq);
  4012. rcu_read_lock();
  4013. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  4014. }
  4015. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  4016. {
  4017. struct net *net = seq_file_net(seq);
  4018. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  4019. }
  4020. static void packet_seq_stop(struct seq_file *seq, void *v)
  4021. __releases(RCU)
  4022. {
  4023. rcu_read_unlock();
  4024. }
  4025. static int packet_seq_show(struct seq_file *seq, void *v)
  4026. {
  4027. if (v == SEQ_START_TOKEN)
  4028. seq_printf(seq,
  4029. "%*sRefCnt Type Proto Iface R Rmem User Inode\n",
  4030. IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk");
  4031. else {
  4032. struct sock *s = sk_entry(v);
  4033. const struct packet_sock *po = pkt_sk(s);
  4034. seq_printf(seq,
  4035. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  4036. s,
  4037. refcount_read(&s->sk_refcnt),
  4038. s->sk_type,
  4039. ntohs(READ_ONCE(po->num)),
  4040. READ_ONCE(po->ifindex),
  4041. packet_sock_flag(po, PACKET_SOCK_RUNNING),
  4042. atomic_read(&s->sk_rmem_alloc),
  4043. from_kuid_munged(seq_user_ns(seq), sk_uid(s)),
  4044. sock_i_ino(s));
  4045. }
  4046. return 0;
  4047. }
  4048. static const struct seq_operations packet_seq_ops = {
  4049. .start = packet_seq_start,
  4050. .next = packet_seq_next,
  4051. .stop = packet_seq_stop,
  4052. .show = packet_seq_show,
  4053. };
  4054. #endif
  4055. static int __net_init packet_net_init(struct net *net)
  4056. {
  4057. mutex_init(&net->packet.sklist_lock);
  4058. INIT_HLIST_HEAD(&net->packet.sklist);
  4059. #ifdef CONFIG_PROC_FS
  4060. if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
  4061. sizeof(struct seq_net_private)))
  4062. return -ENOMEM;
  4063. #endif /* CONFIG_PROC_FS */
  4064. return 0;
  4065. }
  4066. static void __net_exit packet_net_exit(struct net *net)
  4067. {
  4068. remove_proc_entry("packet", net->proc_net);
  4069. WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
  4070. }
  4071. static struct pernet_operations packet_net_ops = {
  4072. .init = packet_net_init,
  4073. .exit = packet_net_exit,
  4074. };
  4075. static void __exit packet_exit(void)
  4076. {
  4077. sock_unregister(PF_PACKET);
  4078. proto_unregister(&packet_proto);
  4079. unregister_netdevice_notifier(&packet_netdev_notifier);
  4080. unregister_pernet_subsys(&packet_net_ops);
  4081. }
  4082. static int __init packet_init(void)
  4083. {
  4084. int rc;
  4085. rc = register_pernet_subsys(&packet_net_ops);
  4086. if (rc)
  4087. goto out;
  4088. rc = register_netdevice_notifier(&packet_netdev_notifier);
  4089. if (rc)
  4090. goto out_pernet;
  4091. rc = proto_register(&packet_proto, 0);
  4092. if (rc)
  4093. goto out_notifier;
  4094. rc = sock_register(&packet_family_ops);
  4095. if (rc)
  4096. goto out_proto;
  4097. return 0;
  4098. out_proto:
  4099. proto_unregister(&packet_proto);
  4100. out_notifier:
  4101. unregister_netdevice_notifier(&packet_netdev_notifier);
  4102. out_pernet:
  4103. unregister_pernet_subsys(&packet_net_ops);
  4104. out:
  4105. return rc;
  4106. }
  4107. module_init(packet_init);
  4108. module_exit(packet_exit);
  4109. MODULE_DESCRIPTION("Packet socket support (AF_PACKET)");
  4110. MODULE_LICENSE("GPL");
  4111. MODULE_ALIAS_NETPROTO(PF_PACKET);