af_packet.c 117 KB

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