af_packet.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856
  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;
  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. timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT);
  2409. reinit_completion(&po->skb_completion);
  2410. do {
  2411. ph = packet_current_frame(po, &po->tx_ring,
  2412. TP_STATUS_SEND_REQUEST);
  2413. if (unlikely(ph == NULL)) {
  2414. /* Note: packet_read_pending() might be slow if we
  2415. * have to call it as it's per_cpu variable, but in
  2416. * fast-path we don't have to call it, only when ph
  2417. * is NULL, we need to check the pending_refcnt.
  2418. */
  2419. if (need_wait && packet_read_pending(&po->tx_ring)) {
  2420. timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo);
  2421. if (timeo <= 0) {
  2422. err = !timeo ? -ETIMEDOUT : -ERESTARTSYS;
  2423. goto out_put;
  2424. }
  2425. /* check for additional frames */
  2426. continue;
  2427. } else
  2428. break;
  2429. }
  2430. skb = NULL;
  2431. tp_len = tpacket_parse_header(po, ph, size_max, &data);
  2432. if (tp_len < 0)
  2433. goto tpacket_error;
  2434. status = TP_STATUS_SEND_REQUEST;
  2435. hlen = LL_RESERVED_SPACE(dev);
  2436. tlen = dev->needed_tailroom;
  2437. if (vnet_hdr_sz) {
  2438. vnet_hdr = data;
  2439. data += vnet_hdr_sz;
  2440. tp_len -= vnet_hdr_sz;
  2441. if (tp_len < 0 ||
  2442. __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
  2443. tp_len = -EINVAL;
  2444. goto tpacket_error;
  2445. }
  2446. copylen = __virtio16_to_cpu(vio_le(),
  2447. vnet_hdr->hdr_len);
  2448. }
  2449. copylen = max_t(int, copylen, dev->hard_header_len);
  2450. skb = sock_alloc_send_skb(&po->sk,
  2451. hlen + tlen + sizeof(struct sockaddr_ll) +
  2452. (copylen - dev->hard_header_len),
  2453. !need_wait, &err);
  2454. if (unlikely(skb == NULL)) {
  2455. /* we assume the socket was initially writeable ... */
  2456. if (likely(len_sum > 0))
  2457. err = len_sum;
  2458. goto out_status;
  2459. }
  2460. tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
  2461. addr, hlen, copylen, &sockc);
  2462. if (likely(tp_len >= 0) &&
  2463. tp_len > dev->mtu + reserve &&
  2464. !vnet_hdr_sz &&
  2465. !packet_extra_vlan_len_allowed(dev, skb))
  2466. tp_len = -EMSGSIZE;
  2467. if (unlikely(tp_len < 0)) {
  2468. tpacket_error:
  2469. if (packet_sock_flag(po, PACKET_SOCK_TP_LOSS)) {
  2470. __packet_set_status(po, ph,
  2471. TP_STATUS_AVAILABLE);
  2472. packet_increment_head(&po->tx_ring);
  2473. kfree_skb(skb);
  2474. continue;
  2475. } else {
  2476. status = TP_STATUS_WRONG_FORMAT;
  2477. err = tp_len;
  2478. goto out_status;
  2479. }
  2480. }
  2481. if (vnet_hdr_sz) {
  2482. if (virtio_net_hdr_to_skb(skb, vnet_hdr, vio_le())) {
  2483. tp_len = -EINVAL;
  2484. goto tpacket_error;
  2485. }
  2486. virtio_net_hdr_set_proto(skb, vnet_hdr);
  2487. }
  2488. skb->destructor = tpacket_destruct_skb;
  2489. __packet_set_status(po, ph, TP_STATUS_SENDING);
  2490. packet_inc_pending(&po->tx_ring);
  2491. status = TP_STATUS_SEND_REQUEST;
  2492. err = packet_xmit(po, skb);
  2493. if (unlikely(err != 0)) {
  2494. if (err > 0)
  2495. err = net_xmit_errno(err);
  2496. if (err && __packet_get_status(po, ph) ==
  2497. TP_STATUS_AVAILABLE) {
  2498. /* skb was destructed already */
  2499. skb = NULL;
  2500. goto out_status;
  2501. }
  2502. /*
  2503. * skb was dropped but not destructed yet;
  2504. * let's treat it like congestion or err < 0
  2505. */
  2506. err = 0;
  2507. }
  2508. packet_increment_head(&po->tx_ring);
  2509. len_sum += tp_len;
  2510. } while (1);
  2511. err = len_sum;
  2512. goto out_put;
  2513. out_status:
  2514. __packet_set_status(po, ph, status);
  2515. kfree_skb(skb);
  2516. out_put:
  2517. dev_put(dev);
  2518. out:
  2519. mutex_unlock(&po->pg_vec_lock);
  2520. return err;
  2521. }
  2522. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  2523. size_t reserve, size_t len,
  2524. size_t linear, int noblock,
  2525. int *err)
  2526. {
  2527. struct sk_buff *skb;
  2528. /* Under a page? Don't bother with paged skb. */
  2529. if (prepad + len < PAGE_SIZE || !linear)
  2530. linear = len;
  2531. if (len - linear > MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
  2532. linear = len - MAX_SKB_FRAGS * (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER);
  2533. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  2534. err, PAGE_ALLOC_COSTLY_ORDER);
  2535. if (!skb)
  2536. return NULL;
  2537. skb_reserve(skb, reserve);
  2538. skb_put(skb, linear);
  2539. skb->data_len = len - linear;
  2540. skb->len += len - linear;
  2541. return skb;
  2542. }
  2543. static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
  2544. {
  2545. struct sock *sk = sock->sk;
  2546. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2547. struct sk_buff *skb;
  2548. struct net_device *dev;
  2549. __be16 proto;
  2550. unsigned char *addr = NULL;
  2551. int err, reserve = 0;
  2552. struct sockcm_cookie sockc;
  2553. struct virtio_net_hdr vnet_hdr = { 0 };
  2554. int offset = 0;
  2555. struct packet_sock *po = pkt_sk(sk);
  2556. int vnet_hdr_sz = READ_ONCE(po->vnet_hdr_sz);
  2557. int hlen, tlen, linear;
  2558. int extra_len = 0;
  2559. /*
  2560. * Get and verify the address.
  2561. */
  2562. if (likely(saddr == NULL)) {
  2563. dev = packet_cached_dev_get(po);
  2564. proto = READ_ONCE(po->num);
  2565. } else {
  2566. err = -EINVAL;
  2567. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2568. goto out;
  2569. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  2570. goto out;
  2571. proto = saddr->sll_protocol;
  2572. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  2573. if (sock->type == SOCK_DGRAM) {
  2574. if (dev && msg->msg_namelen < dev->addr_len +
  2575. offsetof(struct sockaddr_ll, sll_addr))
  2576. goto out_unlock;
  2577. addr = saddr->sll_addr;
  2578. }
  2579. }
  2580. err = -ENXIO;
  2581. if (unlikely(dev == NULL))
  2582. goto out_unlock;
  2583. err = -ENETDOWN;
  2584. if (unlikely(!(dev->flags & IFF_UP)))
  2585. goto out_unlock;
  2586. sockcm_init(&sockc, sk);
  2587. sockc.mark = READ_ONCE(sk->sk_mark);
  2588. if (msg->msg_controllen) {
  2589. err = sock_cmsg_send(sk, msg, &sockc);
  2590. if (unlikely(err))
  2591. goto out_unlock;
  2592. }
  2593. if (sock->type == SOCK_RAW)
  2594. reserve = dev->hard_header_len;
  2595. if (vnet_hdr_sz) {
  2596. err = packet_snd_vnet_parse(msg, &len, &vnet_hdr, vnet_hdr_sz);
  2597. if (err)
  2598. goto out_unlock;
  2599. }
  2600. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  2601. if (!netif_supports_nofcs(dev)) {
  2602. err = -EPROTONOSUPPORT;
  2603. goto out_unlock;
  2604. }
  2605. extra_len = 4; /* We're doing our own CRC */
  2606. }
  2607. err = -EMSGSIZE;
  2608. if (!vnet_hdr.gso_type &&
  2609. (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  2610. goto out_unlock;
  2611. err = -ENOBUFS;
  2612. hlen = LL_RESERVED_SPACE(dev);
  2613. tlen = dev->needed_tailroom;
  2614. linear = __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len);
  2615. linear = max(linear, min_t(int, len, dev->hard_header_len));
  2616. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, linear,
  2617. msg->msg_flags & MSG_DONTWAIT, &err);
  2618. if (skb == NULL)
  2619. goto out_unlock;
  2620. skb_reset_network_header(skb);
  2621. err = -EINVAL;
  2622. if (sock->type == SOCK_DGRAM) {
  2623. offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  2624. if (unlikely(offset < 0))
  2625. goto out_free;
  2626. } else if (reserve) {
  2627. skb_reserve(skb, -reserve);
  2628. if (len < reserve + sizeof(struct ipv6hdr) &&
  2629. dev->min_header_len != dev->hard_header_len)
  2630. skb_reset_network_header(skb);
  2631. }
  2632. /* Returns -EFAULT on error */
  2633. err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
  2634. if (err)
  2635. goto out_free;
  2636. if ((sock->type == SOCK_RAW &&
  2637. !dev_validate_header(dev, skb->data, len)) || !skb->len) {
  2638. err = -EINVAL;
  2639. goto out_free;
  2640. }
  2641. skb_setup_tx_timestamp(skb, sockc.tsflags);
  2642. if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
  2643. !packet_extra_vlan_len_allowed(dev, skb)) {
  2644. err = -EMSGSIZE;
  2645. goto out_free;
  2646. }
  2647. skb->protocol = proto;
  2648. skb->dev = dev;
  2649. skb->priority = READ_ONCE(sk->sk_priority);
  2650. skb->mark = sockc.mark;
  2651. skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid);
  2652. if (unlikely(extra_len == 4))
  2653. skb->no_fcs = 1;
  2654. packet_parse_headers(skb, sock);
  2655. if (vnet_hdr_sz) {
  2656. err = virtio_net_hdr_to_skb(skb, &vnet_hdr, vio_le());
  2657. if (err)
  2658. goto out_free;
  2659. len += vnet_hdr_sz;
  2660. virtio_net_hdr_set_proto(skb, &vnet_hdr);
  2661. }
  2662. err = packet_xmit(po, skb);
  2663. if (unlikely(err != 0)) {
  2664. if (err > 0)
  2665. err = net_xmit_errno(err);
  2666. if (err)
  2667. goto out_unlock;
  2668. }
  2669. dev_put(dev);
  2670. return len;
  2671. out_free:
  2672. kfree_skb(skb);
  2673. out_unlock:
  2674. dev_put(dev);
  2675. out:
  2676. return err;
  2677. }
  2678. static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  2679. {
  2680. struct sock *sk = sock->sk;
  2681. struct packet_sock *po = pkt_sk(sk);
  2682. /* Reading tx_ring.pg_vec without holding pg_vec_lock is racy.
  2683. * tpacket_snd() will redo the check safely.
  2684. */
  2685. if (data_race(po->tx_ring.pg_vec))
  2686. return tpacket_snd(po, msg);
  2687. return packet_snd(sock, msg, len);
  2688. }
  2689. /*
  2690. * Close a PACKET socket. This is fairly simple. We immediately go
  2691. * to 'closed' state and remove our protocol entry in the device list.
  2692. */
  2693. static int packet_release(struct socket *sock)
  2694. {
  2695. struct sock *sk = sock->sk;
  2696. struct packet_sock *po;
  2697. struct packet_fanout *f;
  2698. struct net *net;
  2699. union tpacket_req_u req_u;
  2700. if (!sk)
  2701. return 0;
  2702. net = sock_net(sk);
  2703. po = pkt_sk(sk);
  2704. mutex_lock(&net->packet.sklist_lock);
  2705. sk_del_node_init_rcu(sk);
  2706. mutex_unlock(&net->packet.sklist_lock);
  2707. sock_prot_inuse_add(net, sk->sk_prot, -1);
  2708. spin_lock(&po->bind_lock);
  2709. unregister_prot_hook(sk, false);
  2710. packet_cached_dev_reset(po);
  2711. if (po->prot_hook.dev) {
  2712. netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker);
  2713. po->prot_hook.dev = NULL;
  2714. }
  2715. spin_unlock(&po->bind_lock);
  2716. packet_flush_mclist(sk);
  2717. lock_sock(sk);
  2718. if (po->rx_ring.pg_vec) {
  2719. memset(&req_u, 0, sizeof(req_u));
  2720. packet_set_ring(sk, &req_u, 1, 0);
  2721. }
  2722. if (po->tx_ring.pg_vec) {
  2723. memset(&req_u, 0, sizeof(req_u));
  2724. packet_set_ring(sk, &req_u, 1, 1);
  2725. }
  2726. release_sock(sk);
  2727. f = fanout_release(sk);
  2728. synchronize_net();
  2729. kfree(po->rollover);
  2730. if (f) {
  2731. fanout_release_data(f);
  2732. kvfree(f);
  2733. }
  2734. /*
  2735. * Now the socket is dead. No more input will appear.
  2736. */
  2737. sock_orphan(sk);
  2738. sock->sk = NULL;
  2739. /* Purge queues */
  2740. skb_queue_purge(&sk->sk_receive_queue);
  2741. packet_free_pending(po);
  2742. sock_put(sk);
  2743. return 0;
  2744. }
  2745. /*
  2746. * Attach a packet hook.
  2747. */
  2748. static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
  2749. __be16 proto)
  2750. {
  2751. struct packet_sock *po = pkt_sk(sk);
  2752. struct net_device *dev = NULL;
  2753. bool unlisted = false;
  2754. bool need_rehook;
  2755. int ret = 0;
  2756. lock_sock(sk);
  2757. spin_lock(&po->bind_lock);
  2758. if (!proto)
  2759. proto = po->num;
  2760. rcu_read_lock();
  2761. if (po->fanout) {
  2762. ret = -EINVAL;
  2763. goto out_unlock;
  2764. }
  2765. if (name) {
  2766. dev = dev_get_by_name_rcu(sock_net(sk), name);
  2767. if (!dev) {
  2768. ret = -ENODEV;
  2769. goto out_unlock;
  2770. }
  2771. } else if (ifindex) {
  2772. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  2773. if (!dev) {
  2774. ret = -ENODEV;
  2775. goto out_unlock;
  2776. }
  2777. }
  2778. need_rehook = po->prot_hook.type != proto || po->prot_hook.dev != dev;
  2779. if (need_rehook) {
  2780. dev_hold(dev);
  2781. if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  2782. rcu_read_unlock();
  2783. /* prevents packet_notifier() from calling
  2784. * register_prot_hook()
  2785. */
  2786. WRITE_ONCE(po->num, 0);
  2787. __unregister_prot_hook(sk, true);
  2788. rcu_read_lock();
  2789. if (dev)
  2790. unlisted = !dev_get_by_index_rcu(sock_net(sk),
  2791. dev->ifindex);
  2792. }
  2793. BUG_ON(packet_sock_flag(po, PACKET_SOCK_RUNNING));
  2794. WRITE_ONCE(po->num, proto);
  2795. po->prot_hook.type = proto;
  2796. netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker);
  2797. if (unlikely(unlisted)) {
  2798. po->prot_hook.dev = NULL;
  2799. WRITE_ONCE(po->ifindex, -1);
  2800. packet_cached_dev_reset(po);
  2801. } else {
  2802. netdev_hold(dev, &po->prot_hook.dev_tracker,
  2803. GFP_ATOMIC);
  2804. po->prot_hook.dev = dev;
  2805. WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
  2806. packet_cached_dev_assign(po, dev);
  2807. }
  2808. dev_put(dev);
  2809. }
  2810. if (proto == 0 || !need_rehook)
  2811. goto out_unlock;
  2812. if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
  2813. register_prot_hook(sk);
  2814. } else {
  2815. sk->sk_err = ENETDOWN;
  2816. if (!sock_flag(sk, SOCK_DEAD))
  2817. sk_error_report(sk);
  2818. }
  2819. out_unlock:
  2820. rcu_read_unlock();
  2821. spin_unlock(&po->bind_lock);
  2822. release_sock(sk);
  2823. return ret;
  2824. }
  2825. /*
  2826. * Bind a packet socket to a device
  2827. */
  2828. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  2829. int addr_len)
  2830. {
  2831. struct sock *sk = sock->sk;
  2832. char name[sizeof(uaddr->sa_data_min) + 1];
  2833. /*
  2834. * Check legality
  2835. */
  2836. if (addr_len != sizeof(struct sockaddr))
  2837. return -EINVAL;
  2838. /* uaddr->sa_data comes from the userspace, it's not guaranteed to be
  2839. * zero-terminated.
  2840. */
  2841. memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data_min));
  2842. name[sizeof(uaddr->sa_data_min)] = 0;
  2843. return packet_do_bind(sk, name, 0, 0);
  2844. }
  2845. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  2846. {
  2847. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  2848. struct sock *sk = sock->sk;
  2849. /*
  2850. * Check legality
  2851. */
  2852. if (addr_len < sizeof(struct sockaddr_ll))
  2853. return -EINVAL;
  2854. if (sll->sll_family != AF_PACKET)
  2855. return -EINVAL;
  2856. return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
  2857. }
  2858. static struct proto packet_proto = {
  2859. .name = "PACKET",
  2860. .owner = THIS_MODULE,
  2861. .obj_size = sizeof(struct packet_sock),
  2862. };
  2863. /*
  2864. * Create a packet of type SOCK_PACKET.
  2865. */
  2866. static int packet_create(struct net *net, struct socket *sock, int protocol,
  2867. int kern)
  2868. {
  2869. struct sock *sk;
  2870. struct packet_sock *po;
  2871. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  2872. int err;
  2873. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  2874. return -EPERM;
  2875. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  2876. sock->type != SOCK_PACKET)
  2877. return -ESOCKTNOSUPPORT;
  2878. sock->state = SS_UNCONNECTED;
  2879. err = -ENOBUFS;
  2880. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
  2881. if (sk == NULL)
  2882. goto out;
  2883. sock->ops = &packet_ops;
  2884. if (sock->type == SOCK_PACKET)
  2885. sock->ops = &packet_ops_spkt;
  2886. po = pkt_sk(sk);
  2887. err = packet_alloc_pending(po);
  2888. if (err)
  2889. goto out_sk_free;
  2890. sock_init_data(sock, sk);
  2891. init_completion(&po->skb_completion);
  2892. sk->sk_family = PF_PACKET;
  2893. po->num = proto;
  2894. packet_cached_dev_reset(po);
  2895. sk->sk_destruct = packet_sock_destruct;
  2896. /*
  2897. * Attach a protocol block
  2898. */
  2899. spin_lock_init(&po->bind_lock);
  2900. mutex_init(&po->pg_vec_lock);
  2901. po->rollover = NULL;
  2902. po->prot_hook.func = packet_rcv;
  2903. if (sock->type == SOCK_PACKET)
  2904. po->prot_hook.func = packet_rcv_spkt;
  2905. po->prot_hook.af_packet_priv = sk;
  2906. po->prot_hook.af_packet_net = sock_net(sk);
  2907. if (proto) {
  2908. po->prot_hook.type = proto;
  2909. __register_prot_hook(sk);
  2910. }
  2911. mutex_lock(&net->packet.sklist_lock);
  2912. sk_add_node_tail_rcu(sk, &net->packet.sklist);
  2913. mutex_unlock(&net->packet.sklist_lock);
  2914. sock_prot_inuse_add(net, &packet_proto, 1);
  2915. return 0;
  2916. out_sk_free:
  2917. sk_free(sk);
  2918. out:
  2919. return err;
  2920. }
  2921. /*
  2922. * Pull a packet from our receive queue and hand it to the user.
  2923. * If necessary we block.
  2924. */
  2925. static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  2926. int flags)
  2927. {
  2928. struct sock *sk = sock->sk;
  2929. struct sk_buff *skb;
  2930. int copied, err;
  2931. int vnet_hdr_len = READ_ONCE(pkt_sk(sk)->vnet_hdr_sz);
  2932. unsigned int origlen = 0;
  2933. err = -EINVAL;
  2934. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  2935. goto out;
  2936. #if 0
  2937. /* What error should we return now? EUNATTACH? */
  2938. if (pkt_sk(sk)->ifindex < 0)
  2939. return -ENODEV;
  2940. #endif
  2941. if (flags & MSG_ERRQUEUE) {
  2942. err = sock_recv_errqueue(sk, msg, len,
  2943. SOL_PACKET, PACKET_TX_TIMESTAMP);
  2944. goto out;
  2945. }
  2946. /*
  2947. * Call the generic datagram receiver. This handles all sorts
  2948. * of horrible races and re-entrancy so we can forget about it
  2949. * in the protocol layers.
  2950. *
  2951. * Now it will return ENETDOWN, if device have just gone down,
  2952. * but then it will block.
  2953. */
  2954. skb = skb_recv_datagram(sk, flags, &err);
  2955. /*
  2956. * An error occurred so return it. Because skb_recv_datagram()
  2957. * handles the blocking we don't see and worry about blocking
  2958. * retries.
  2959. */
  2960. if (skb == NULL)
  2961. goto out;
  2962. packet_rcv_try_clear_pressure(pkt_sk(sk));
  2963. if (vnet_hdr_len) {
  2964. err = packet_rcv_vnet(msg, skb, &len, vnet_hdr_len);
  2965. if (err)
  2966. goto out_free;
  2967. }
  2968. /* You lose any data beyond the buffer you gave. If it worries
  2969. * a user program they can ask the device for its MTU
  2970. * anyway.
  2971. */
  2972. copied = skb->len;
  2973. if (copied > len) {
  2974. copied = len;
  2975. msg->msg_flags |= MSG_TRUNC;
  2976. }
  2977. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2978. if (err)
  2979. goto out_free;
  2980. if (sock->type != SOCK_PACKET) {
  2981. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2982. /* Original length was stored in sockaddr_ll fields */
  2983. origlen = PACKET_SKB_CB(skb)->sa.origlen;
  2984. sll->sll_family = AF_PACKET;
  2985. sll->sll_protocol = (sock->type == SOCK_DGRAM) ?
  2986. vlan_get_protocol_dgram(skb) : skb->protocol;
  2987. }
  2988. sock_recv_cmsgs(msg, sk, skb);
  2989. if (msg->msg_name) {
  2990. const size_t max_len = min(sizeof(skb->cb),
  2991. sizeof(struct sockaddr_storage));
  2992. int copy_len;
  2993. /* If the address length field is there to be filled
  2994. * in, we fill it in now.
  2995. */
  2996. if (sock->type == SOCK_PACKET) {
  2997. __sockaddr_check_size(sizeof(struct sockaddr_pkt));
  2998. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  2999. copy_len = msg->msg_namelen;
  3000. } else {
  3001. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  3002. msg->msg_namelen = sll->sll_halen +
  3003. offsetof(struct sockaddr_ll, sll_addr);
  3004. copy_len = msg->msg_namelen;
  3005. if (msg->msg_namelen < sizeof(struct sockaddr_ll)) {
  3006. memset(msg->msg_name +
  3007. offsetof(struct sockaddr_ll, sll_addr),
  3008. 0, sizeof(sll->sll_addr));
  3009. msg->msg_namelen = sizeof(struct sockaddr_ll);
  3010. }
  3011. }
  3012. if (WARN_ON_ONCE(copy_len > max_len)) {
  3013. copy_len = max_len;
  3014. msg->msg_namelen = copy_len;
  3015. }
  3016. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len);
  3017. }
  3018. if (packet_sock_flag(pkt_sk(sk), PACKET_SOCK_AUXDATA)) {
  3019. struct tpacket_auxdata aux;
  3020. aux.tp_status = TP_STATUS_USER;
  3021. if (skb->ip_summed == CHECKSUM_PARTIAL)
  3022. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  3023. else if (skb->pkt_type != PACKET_OUTGOING &&
  3024. skb_csum_unnecessary(skb))
  3025. aux.tp_status |= TP_STATUS_CSUM_VALID;
  3026. if (skb_is_gso(skb) && skb_is_gso_tcp(skb))
  3027. aux.tp_status |= TP_STATUS_GSO_TCP;
  3028. aux.tp_len = origlen;
  3029. aux.tp_snaplen = skb->len;
  3030. aux.tp_mac = 0;
  3031. aux.tp_net = skb_network_offset(skb);
  3032. if (skb_vlan_tag_present(skb)) {
  3033. aux.tp_vlan_tci = skb_vlan_tag_get(skb);
  3034. aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
  3035. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  3036. } else if (unlikely(sock->type == SOCK_DGRAM && eth_type_vlan(skb->protocol))) {
  3037. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  3038. struct net_device *dev;
  3039. rcu_read_lock();
  3040. dev = dev_get_by_index_rcu(sock_net(sk), sll->sll_ifindex);
  3041. if (dev) {
  3042. aux.tp_vlan_tci = vlan_get_tci(skb, dev);
  3043. aux.tp_vlan_tpid = ntohs(skb->protocol);
  3044. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  3045. } else {
  3046. aux.tp_vlan_tci = 0;
  3047. aux.tp_vlan_tpid = 0;
  3048. }
  3049. rcu_read_unlock();
  3050. } else {
  3051. aux.tp_vlan_tci = 0;
  3052. aux.tp_vlan_tpid = 0;
  3053. }
  3054. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  3055. }
  3056. /*
  3057. * Free or return the buffer as appropriate. Again this
  3058. * hides all the races and re-entrancy issues from us.
  3059. */
  3060. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  3061. out_free:
  3062. skb_free_datagram(sk, skb);
  3063. out:
  3064. return err;
  3065. }
  3066. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  3067. int peer)
  3068. {
  3069. struct net_device *dev;
  3070. struct sock *sk = sock->sk;
  3071. if (peer)
  3072. return -EOPNOTSUPP;
  3073. uaddr->sa_family = AF_PACKET;
  3074. memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data_min));
  3075. rcu_read_lock();
  3076. dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
  3077. if (dev)
  3078. strscpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data_min));
  3079. rcu_read_unlock();
  3080. return sizeof(*uaddr);
  3081. }
  3082. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  3083. int peer)
  3084. {
  3085. struct net_device *dev;
  3086. struct sock *sk = sock->sk;
  3087. struct packet_sock *po = pkt_sk(sk);
  3088. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  3089. int ifindex;
  3090. if (peer)
  3091. return -EOPNOTSUPP;
  3092. ifindex = READ_ONCE(po->ifindex);
  3093. sll->sll_family = AF_PACKET;
  3094. sll->sll_ifindex = ifindex;
  3095. sll->sll_protocol = READ_ONCE(po->num);
  3096. sll->sll_pkttype = 0;
  3097. rcu_read_lock();
  3098. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  3099. if (dev) {
  3100. sll->sll_hatype = dev->type;
  3101. sll->sll_halen = dev->addr_len;
  3102. /* Let __fortify_memcpy_chk() know the actual buffer size. */
  3103. memcpy(((struct sockaddr_storage *)sll)->__data +
  3104. offsetof(struct sockaddr_ll, sll_addr) -
  3105. offsetofend(struct sockaddr_ll, sll_family),
  3106. dev->dev_addr, dev->addr_len);
  3107. } else {
  3108. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  3109. sll->sll_halen = 0;
  3110. }
  3111. rcu_read_unlock();
  3112. return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  3113. }
  3114. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  3115. int what)
  3116. {
  3117. switch (i->type) {
  3118. case PACKET_MR_MULTICAST:
  3119. if (i->alen != dev->addr_len)
  3120. return -EINVAL;
  3121. if (what > 0)
  3122. return dev_mc_add(dev, i->addr);
  3123. else
  3124. return dev_mc_del(dev, i->addr);
  3125. break;
  3126. case PACKET_MR_PROMISC:
  3127. return dev_set_promiscuity(dev, what);
  3128. case PACKET_MR_ALLMULTI:
  3129. return dev_set_allmulti(dev, what);
  3130. case PACKET_MR_UNICAST:
  3131. if (i->alen != dev->addr_len)
  3132. return -EINVAL;
  3133. if (what > 0)
  3134. return dev_uc_add(dev, i->addr);
  3135. else
  3136. return dev_uc_del(dev, i->addr);
  3137. break;
  3138. default:
  3139. break;
  3140. }
  3141. return 0;
  3142. }
  3143. static void packet_dev_mclist_delete(struct net_device *dev,
  3144. struct packet_mclist **mlp)
  3145. {
  3146. struct packet_mclist *ml;
  3147. while ((ml = *mlp) != NULL) {
  3148. if (ml->ifindex == dev->ifindex) {
  3149. packet_dev_mc(dev, ml, -1);
  3150. *mlp = ml->next;
  3151. kfree(ml);
  3152. } else
  3153. mlp = &ml->next;
  3154. }
  3155. }
  3156. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  3157. {
  3158. struct packet_sock *po = pkt_sk(sk);
  3159. struct packet_mclist *ml, *i;
  3160. struct net_device *dev;
  3161. int err;
  3162. rtnl_lock();
  3163. err = -ENODEV;
  3164. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  3165. if (!dev)
  3166. goto done;
  3167. err = -EINVAL;
  3168. if (mreq->mr_alen > dev->addr_len)
  3169. goto done;
  3170. err = -ENOBUFS;
  3171. i = kmalloc(sizeof(*i), GFP_KERNEL);
  3172. if (i == NULL)
  3173. goto done;
  3174. err = 0;
  3175. for (ml = po->mclist; ml; ml = ml->next) {
  3176. if (ml->ifindex == mreq->mr_ifindex &&
  3177. ml->type == mreq->mr_type &&
  3178. ml->alen == mreq->mr_alen &&
  3179. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3180. ml->count++;
  3181. /* Free the new element ... */
  3182. kfree(i);
  3183. goto done;
  3184. }
  3185. }
  3186. i->type = mreq->mr_type;
  3187. i->ifindex = mreq->mr_ifindex;
  3188. i->alen = mreq->mr_alen;
  3189. memcpy(i->addr, mreq->mr_address, i->alen);
  3190. memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen);
  3191. i->count = 1;
  3192. i->next = po->mclist;
  3193. po->mclist = i;
  3194. err = packet_dev_mc(dev, i, 1);
  3195. if (err) {
  3196. po->mclist = i->next;
  3197. kfree(i);
  3198. }
  3199. done:
  3200. rtnl_unlock();
  3201. return err;
  3202. }
  3203. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  3204. {
  3205. struct packet_mclist *ml, **mlp;
  3206. rtnl_lock();
  3207. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  3208. if (ml->ifindex == mreq->mr_ifindex &&
  3209. ml->type == mreq->mr_type &&
  3210. ml->alen == mreq->mr_alen &&
  3211. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  3212. if (--ml->count == 0) {
  3213. struct net_device *dev;
  3214. *mlp = ml->next;
  3215. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3216. if (dev)
  3217. packet_dev_mc(dev, ml, -1);
  3218. kfree(ml);
  3219. }
  3220. break;
  3221. }
  3222. }
  3223. rtnl_unlock();
  3224. return 0;
  3225. }
  3226. static void packet_flush_mclist(struct sock *sk)
  3227. {
  3228. struct packet_sock *po = pkt_sk(sk);
  3229. struct packet_mclist *ml;
  3230. if (!po->mclist)
  3231. return;
  3232. rtnl_lock();
  3233. while ((ml = po->mclist) != NULL) {
  3234. struct net_device *dev;
  3235. po->mclist = ml->next;
  3236. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3237. if (dev != NULL)
  3238. packet_dev_mc(dev, ml, -1);
  3239. kfree(ml);
  3240. }
  3241. rtnl_unlock();
  3242. }
  3243. static int
  3244. packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
  3245. unsigned int optlen)
  3246. {
  3247. struct sock *sk = sock->sk;
  3248. struct packet_sock *po = pkt_sk(sk);
  3249. int ret;
  3250. if (level != SOL_PACKET)
  3251. return -ENOPROTOOPT;
  3252. switch (optname) {
  3253. case PACKET_ADD_MEMBERSHIP:
  3254. case PACKET_DROP_MEMBERSHIP:
  3255. {
  3256. struct packet_mreq_max mreq;
  3257. int len = optlen;
  3258. memset(&mreq, 0, sizeof(mreq));
  3259. if (len < sizeof(struct packet_mreq))
  3260. return -EINVAL;
  3261. if (len > sizeof(mreq))
  3262. len = sizeof(mreq);
  3263. if (copy_from_sockptr(&mreq, optval, len))
  3264. return -EFAULT;
  3265. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  3266. return -EINVAL;
  3267. if (optname == PACKET_ADD_MEMBERSHIP)
  3268. ret = packet_mc_add(sk, &mreq);
  3269. else
  3270. ret = packet_mc_drop(sk, &mreq);
  3271. return ret;
  3272. }
  3273. case PACKET_RX_RING:
  3274. case PACKET_TX_RING:
  3275. {
  3276. union tpacket_req_u req_u;
  3277. ret = -EINVAL;
  3278. lock_sock(sk);
  3279. switch (po->tp_version) {
  3280. case TPACKET_V1:
  3281. case TPACKET_V2:
  3282. if (optlen < sizeof(req_u.req))
  3283. break;
  3284. ret = copy_from_sockptr(&req_u.req, optval,
  3285. sizeof(req_u.req)) ?
  3286. -EINVAL : 0;
  3287. break;
  3288. case TPACKET_V3:
  3289. default:
  3290. if (optlen < sizeof(req_u.req3))
  3291. break;
  3292. ret = copy_from_sockptr(&req_u.req3, optval,
  3293. sizeof(req_u.req3)) ?
  3294. -EINVAL : 0;
  3295. break;
  3296. }
  3297. if (!ret)
  3298. ret = packet_set_ring(sk, &req_u, 0,
  3299. optname == PACKET_TX_RING);
  3300. release_sock(sk);
  3301. return ret;
  3302. }
  3303. case PACKET_COPY_THRESH:
  3304. {
  3305. int val;
  3306. if (optlen != sizeof(val))
  3307. return -EINVAL;
  3308. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3309. return -EFAULT;
  3310. WRITE_ONCE(pkt_sk(sk)->copy_thresh, val);
  3311. return 0;
  3312. }
  3313. case PACKET_VERSION:
  3314. {
  3315. int val;
  3316. if (optlen != sizeof(val))
  3317. return -EINVAL;
  3318. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3319. return -EFAULT;
  3320. switch (val) {
  3321. case TPACKET_V1:
  3322. case TPACKET_V2:
  3323. case TPACKET_V3:
  3324. break;
  3325. default:
  3326. return -EINVAL;
  3327. }
  3328. lock_sock(sk);
  3329. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3330. ret = -EBUSY;
  3331. } else {
  3332. po->tp_version = val;
  3333. ret = 0;
  3334. }
  3335. release_sock(sk);
  3336. return ret;
  3337. }
  3338. case PACKET_RESERVE:
  3339. {
  3340. unsigned int val;
  3341. if (optlen != sizeof(val))
  3342. return -EINVAL;
  3343. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3344. return -EFAULT;
  3345. if (val > INT_MAX)
  3346. return -EINVAL;
  3347. lock_sock(sk);
  3348. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3349. ret = -EBUSY;
  3350. } else {
  3351. po->tp_reserve = val;
  3352. ret = 0;
  3353. }
  3354. release_sock(sk);
  3355. return ret;
  3356. }
  3357. case PACKET_LOSS:
  3358. {
  3359. unsigned int val;
  3360. if (optlen != sizeof(val))
  3361. return -EINVAL;
  3362. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3363. return -EFAULT;
  3364. lock_sock(sk);
  3365. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3366. ret = -EBUSY;
  3367. } else {
  3368. packet_sock_flag_set(po, PACKET_SOCK_TP_LOSS, val);
  3369. ret = 0;
  3370. }
  3371. release_sock(sk);
  3372. return ret;
  3373. }
  3374. case PACKET_AUXDATA:
  3375. {
  3376. int val;
  3377. if (optlen < sizeof(val))
  3378. return -EINVAL;
  3379. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3380. return -EFAULT;
  3381. packet_sock_flag_set(po, PACKET_SOCK_AUXDATA, val);
  3382. return 0;
  3383. }
  3384. case PACKET_ORIGDEV:
  3385. {
  3386. int val;
  3387. if (optlen < sizeof(val))
  3388. return -EINVAL;
  3389. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3390. return -EFAULT;
  3391. packet_sock_flag_set(po, PACKET_SOCK_ORIGDEV, val);
  3392. return 0;
  3393. }
  3394. case PACKET_VNET_HDR:
  3395. case PACKET_VNET_HDR_SZ:
  3396. {
  3397. int val, hdr_len;
  3398. if (sock->type != SOCK_RAW)
  3399. return -EINVAL;
  3400. if (optlen < sizeof(val))
  3401. return -EINVAL;
  3402. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3403. return -EFAULT;
  3404. if (optname == PACKET_VNET_HDR_SZ) {
  3405. if (val && val != sizeof(struct virtio_net_hdr) &&
  3406. val != sizeof(struct virtio_net_hdr_mrg_rxbuf))
  3407. return -EINVAL;
  3408. hdr_len = val;
  3409. } else {
  3410. hdr_len = val ? sizeof(struct virtio_net_hdr) : 0;
  3411. }
  3412. lock_sock(sk);
  3413. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) {
  3414. ret = -EBUSY;
  3415. } else {
  3416. WRITE_ONCE(po->vnet_hdr_sz, hdr_len);
  3417. ret = 0;
  3418. }
  3419. release_sock(sk);
  3420. return ret;
  3421. }
  3422. case PACKET_TIMESTAMP:
  3423. {
  3424. int val;
  3425. if (optlen != sizeof(val))
  3426. return -EINVAL;
  3427. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3428. return -EFAULT;
  3429. WRITE_ONCE(po->tp_tstamp, val);
  3430. return 0;
  3431. }
  3432. case PACKET_FANOUT:
  3433. {
  3434. struct fanout_args args = { 0 };
  3435. if (optlen != sizeof(int) && optlen != sizeof(args))
  3436. return -EINVAL;
  3437. if (copy_from_sockptr(&args, optval, optlen))
  3438. return -EFAULT;
  3439. return fanout_add(sk, &args);
  3440. }
  3441. case PACKET_FANOUT_DATA:
  3442. {
  3443. /* Paired with the WRITE_ONCE() in fanout_add() */
  3444. if (!READ_ONCE(po->fanout))
  3445. return -EINVAL;
  3446. return fanout_set_data(po, optval, optlen);
  3447. }
  3448. case PACKET_IGNORE_OUTGOING:
  3449. {
  3450. int val;
  3451. if (optlen != sizeof(val))
  3452. return -EINVAL;
  3453. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3454. return -EFAULT;
  3455. if (val < 0 || val > 1)
  3456. return -EINVAL;
  3457. WRITE_ONCE(po->prot_hook.ignore_outgoing, !!val);
  3458. return 0;
  3459. }
  3460. case PACKET_TX_HAS_OFF:
  3461. {
  3462. unsigned int val;
  3463. if (optlen != sizeof(val))
  3464. return -EINVAL;
  3465. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3466. return -EFAULT;
  3467. lock_sock(sk);
  3468. if (!po->rx_ring.pg_vec && !po->tx_ring.pg_vec)
  3469. packet_sock_flag_set(po, PACKET_SOCK_TX_HAS_OFF, val);
  3470. release_sock(sk);
  3471. return 0;
  3472. }
  3473. case PACKET_QDISC_BYPASS:
  3474. {
  3475. int val;
  3476. if (optlen != sizeof(val))
  3477. return -EINVAL;
  3478. if (copy_from_sockptr(&val, optval, sizeof(val)))
  3479. return -EFAULT;
  3480. packet_sock_flag_set(po, PACKET_SOCK_QDISC_BYPASS, val);
  3481. return 0;
  3482. }
  3483. default:
  3484. return -ENOPROTOOPT;
  3485. }
  3486. }
  3487. static int packet_getsockopt(struct socket *sock, int level, int optname,
  3488. char __user *optval, int __user *optlen)
  3489. {
  3490. int len;
  3491. int val, lv = sizeof(val);
  3492. struct sock *sk = sock->sk;
  3493. struct packet_sock *po = pkt_sk(sk);
  3494. void *data = &val;
  3495. union tpacket_stats_u st;
  3496. struct tpacket_rollover_stats rstats;
  3497. int drops;
  3498. if (level != SOL_PACKET)
  3499. return -ENOPROTOOPT;
  3500. if (get_user(len, optlen))
  3501. return -EFAULT;
  3502. if (len < 0)
  3503. return -EINVAL;
  3504. switch (optname) {
  3505. case PACKET_STATISTICS:
  3506. spin_lock_bh(&sk->sk_receive_queue.lock);
  3507. memcpy(&st, &po->stats, sizeof(st));
  3508. memset(&po->stats, 0, sizeof(po->stats));
  3509. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3510. drops = atomic_xchg(&po->tp_drops, 0);
  3511. if (po->tp_version == TPACKET_V3) {
  3512. lv = sizeof(struct tpacket_stats_v3);
  3513. st.stats3.tp_drops = drops;
  3514. st.stats3.tp_packets += drops;
  3515. data = &st.stats3;
  3516. } else {
  3517. lv = sizeof(struct tpacket_stats);
  3518. st.stats1.tp_drops = drops;
  3519. st.stats1.tp_packets += drops;
  3520. data = &st.stats1;
  3521. }
  3522. break;
  3523. case PACKET_AUXDATA:
  3524. val = packet_sock_flag(po, PACKET_SOCK_AUXDATA);
  3525. break;
  3526. case PACKET_ORIGDEV:
  3527. val = packet_sock_flag(po, PACKET_SOCK_ORIGDEV);
  3528. break;
  3529. case PACKET_VNET_HDR:
  3530. val = !!READ_ONCE(po->vnet_hdr_sz);
  3531. break;
  3532. case PACKET_VNET_HDR_SZ:
  3533. val = READ_ONCE(po->vnet_hdr_sz);
  3534. break;
  3535. case PACKET_COPY_THRESH:
  3536. val = READ_ONCE(pkt_sk(sk)->copy_thresh);
  3537. break;
  3538. case PACKET_VERSION:
  3539. val = po->tp_version;
  3540. break;
  3541. case PACKET_HDRLEN:
  3542. if (len > sizeof(int))
  3543. len = sizeof(int);
  3544. if (len < sizeof(int))
  3545. return -EINVAL;
  3546. if (copy_from_user(&val, optval, len))
  3547. return -EFAULT;
  3548. switch (val) {
  3549. case TPACKET_V1:
  3550. val = sizeof(struct tpacket_hdr);
  3551. break;
  3552. case TPACKET_V2:
  3553. val = sizeof(struct tpacket2_hdr);
  3554. break;
  3555. case TPACKET_V3:
  3556. val = sizeof(struct tpacket3_hdr);
  3557. break;
  3558. default:
  3559. return -EINVAL;
  3560. }
  3561. break;
  3562. case PACKET_RESERVE:
  3563. val = po->tp_reserve;
  3564. break;
  3565. case PACKET_LOSS:
  3566. val = packet_sock_flag(po, PACKET_SOCK_TP_LOSS);
  3567. break;
  3568. case PACKET_TIMESTAMP:
  3569. val = READ_ONCE(po->tp_tstamp);
  3570. break;
  3571. case PACKET_FANOUT:
  3572. val = (po->fanout ?
  3573. ((u32)po->fanout->id |
  3574. ((u32)po->fanout->type << 16) |
  3575. ((u32)po->fanout->flags << 24)) :
  3576. 0);
  3577. break;
  3578. case PACKET_IGNORE_OUTGOING:
  3579. val = READ_ONCE(po->prot_hook.ignore_outgoing);
  3580. break;
  3581. case PACKET_ROLLOVER_STATS:
  3582. if (!po->rollover)
  3583. return -EINVAL;
  3584. rstats.tp_all = atomic_long_read(&po->rollover->num);
  3585. rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
  3586. rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
  3587. data = &rstats;
  3588. lv = sizeof(rstats);
  3589. break;
  3590. case PACKET_TX_HAS_OFF:
  3591. val = packet_sock_flag(po, PACKET_SOCK_TX_HAS_OFF);
  3592. break;
  3593. case PACKET_QDISC_BYPASS:
  3594. val = packet_sock_flag(po, PACKET_SOCK_QDISC_BYPASS);
  3595. break;
  3596. default:
  3597. return -ENOPROTOOPT;
  3598. }
  3599. if (len > lv)
  3600. len = lv;
  3601. if (put_user(len, optlen))
  3602. return -EFAULT;
  3603. if (copy_to_user(optval, data, len))
  3604. return -EFAULT;
  3605. return 0;
  3606. }
  3607. static int packet_notifier(struct notifier_block *this,
  3608. unsigned long msg, void *ptr)
  3609. {
  3610. struct sock *sk;
  3611. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3612. struct net *net = dev_net(dev);
  3613. rcu_read_lock();
  3614. sk_for_each_rcu(sk, &net->packet.sklist) {
  3615. struct packet_sock *po = pkt_sk(sk);
  3616. switch (msg) {
  3617. case NETDEV_UNREGISTER:
  3618. if (po->mclist)
  3619. packet_dev_mclist_delete(dev, &po->mclist);
  3620. fallthrough;
  3621. case NETDEV_DOWN:
  3622. if (dev->ifindex == po->ifindex) {
  3623. spin_lock(&po->bind_lock);
  3624. if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
  3625. __unregister_prot_hook(sk, false);
  3626. sk->sk_err = ENETDOWN;
  3627. if (!sock_flag(sk, SOCK_DEAD))
  3628. sk_error_report(sk);
  3629. }
  3630. if (msg == NETDEV_UNREGISTER) {
  3631. packet_cached_dev_reset(po);
  3632. WRITE_ONCE(po->ifindex, -1);
  3633. netdev_put(po->prot_hook.dev,
  3634. &po->prot_hook.dev_tracker);
  3635. po->prot_hook.dev = NULL;
  3636. }
  3637. spin_unlock(&po->bind_lock);
  3638. }
  3639. break;
  3640. case NETDEV_UP:
  3641. if (dev->ifindex == po->ifindex) {
  3642. spin_lock(&po->bind_lock);
  3643. if (po->num)
  3644. register_prot_hook(sk);
  3645. spin_unlock(&po->bind_lock);
  3646. }
  3647. break;
  3648. }
  3649. }
  3650. rcu_read_unlock();
  3651. return NOTIFY_DONE;
  3652. }
  3653. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  3654. unsigned long arg)
  3655. {
  3656. struct sock *sk = sock->sk;
  3657. switch (cmd) {
  3658. case SIOCOUTQ:
  3659. {
  3660. int amount = sk_wmem_alloc_get(sk);
  3661. return put_user(amount, (int __user *)arg);
  3662. }
  3663. case SIOCINQ:
  3664. {
  3665. struct sk_buff *skb;
  3666. int amount = 0;
  3667. spin_lock_bh(&sk->sk_receive_queue.lock);
  3668. skb = skb_peek(&sk->sk_receive_queue);
  3669. if (skb)
  3670. amount = skb->len;
  3671. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3672. return put_user(amount, (int __user *)arg);
  3673. }
  3674. #ifdef CONFIG_INET
  3675. case SIOCADDRT:
  3676. case SIOCDELRT:
  3677. case SIOCDARP:
  3678. case SIOCGARP:
  3679. case SIOCSARP:
  3680. case SIOCGIFADDR:
  3681. case SIOCSIFADDR:
  3682. case SIOCGIFBRDADDR:
  3683. case SIOCSIFBRDADDR:
  3684. case SIOCGIFNETMASK:
  3685. case SIOCSIFNETMASK:
  3686. case SIOCGIFDSTADDR:
  3687. case SIOCSIFDSTADDR:
  3688. case SIOCSIFFLAGS:
  3689. return inet_dgram_ops.ioctl(sock, cmd, arg);
  3690. #endif
  3691. default:
  3692. return -ENOIOCTLCMD;
  3693. }
  3694. return 0;
  3695. }
  3696. static __poll_t packet_poll(struct file *file, struct socket *sock,
  3697. poll_table *wait)
  3698. {
  3699. struct sock *sk = sock->sk;
  3700. struct packet_sock *po = pkt_sk(sk);
  3701. __poll_t mask = datagram_poll(file, sock, wait);
  3702. spin_lock_bh(&sk->sk_receive_queue.lock);
  3703. if (po->rx_ring.pg_vec) {
  3704. if (!packet_previous_rx_frame(po, &po->rx_ring,
  3705. TP_STATUS_KERNEL))
  3706. mask |= EPOLLIN | EPOLLRDNORM;
  3707. }
  3708. packet_rcv_try_clear_pressure(po);
  3709. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3710. spin_lock_bh(&sk->sk_write_queue.lock);
  3711. if (po->tx_ring.pg_vec) {
  3712. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  3713. mask |= EPOLLOUT | EPOLLWRNORM;
  3714. }
  3715. spin_unlock_bh(&sk->sk_write_queue.lock);
  3716. return mask;
  3717. }
  3718. /* Dirty? Well, I still did not learn better way to account
  3719. * for user mmaps.
  3720. */
  3721. static void packet_mm_open(struct vm_area_struct *vma)
  3722. {
  3723. struct file *file = vma->vm_file;
  3724. struct socket *sock = file->private_data;
  3725. struct sock *sk = sock->sk;
  3726. if (sk)
  3727. atomic_long_inc(&pkt_sk(sk)->mapped);
  3728. }
  3729. static void packet_mm_close(struct vm_area_struct *vma)
  3730. {
  3731. struct file *file = vma->vm_file;
  3732. struct socket *sock = file->private_data;
  3733. struct sock *sk = sock->sk;
  3734. if (sk)
  3735. atomic_long_dec(&pkt_sk(sk)->mapped);
  3736. }
  3737. static const struct vm_operations_struct packet_mmap_ops = {
  3738. .open = packet_mm_open,
  3739. .close = packet_mm_close,
  3740. };
  3741. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  3742. unsigned int len)
  3743. {
  3744. int i;
  3745. for (i = 0; i < len; i++) {
  3746. if (likely(pg_vec[i].buffer)) {
  3747. if (is_vmalloc_addr(pg_vec[i].buffer))
  3748. vfree(pg_vec[i].buffer);
  3749. else
  3750. free_pages((unsigned long)pg_vec[i].buffer,
  3751. order);
  3752. pg_vec[i].buffer = NULL;
  3753. }
  3754. }
  3755. kfree(pg_vec);
  3756. }
  3757. static char *alloc_one_pg_vec_page(unsigned long order)
  3758. {
  3759. char *buffer;
  3760. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  3761. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  3762. buffer = (char *) __get_free_pages(gfp_flags, order);
  3763. if (buffer)
  3764. return buffer;
  3765. /* __get_free_pages failed, fall back to vmalloc */
  3766. buffer = vzalloc(array_size((1 << order), PAGE_SIZE));
  3767. if (buffer)
  3768. return buffer;
  3769. /* vmalloc failed, lets dig into swap here */
  3770. gfp_flags &= ~__GFP_NORETRY;
  3771. buffer = (char *) __get_free_pages(gfp_flags, order);
  3772. if (buffer)
  3773. return buffer;
  3774. /* complete and utter failure */
  3775. return NULL;
  3776. }
  3777. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  3778. {
  3779. unsigned int block_nr = req->tp_block_nr;
  3780. struct pgv *pg_vec;
  3781. int i;
  3782. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL | __GFP_NOWARN);
  3783. if (unlikely(!pg_vec))
  3784. goto out;
  3785. for (i = 0; i < block_nr; i++) {
  3786. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  3787. if (unlikely(!pg_vec[i].buffer))
  3788. goto out_free_pgvec;
  3789. }
  3790. out:
  3791. return pg_vec;
  3792. out_free_pgvec:
  3793. free_pg_vec(pg_vec, order, block_nr);
  3794. pg_vec = NULL;
  3795. goto out;
  3796. }
  3797. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  3798. int closing, int tx_ring)
  3799. {
  3800. struct pgv *pg_vec = NULL;
  3801. struct packet_sock *po = pkt_sk(sk);
  3802. unsigned long *rx_owner_map = NULL;
  3803. int was_running, order = 0;
  3804. struct packet_ring_buffer *rb;
  3805. struct sk_buff_head *rb_queue;
  3806. __be16 num;
  3807. int err;
  3808. /* Added to avoid minimal code churn */
  3809. struct tpacket_req *req = &req_u->req;
  3810. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3811. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3812. err = -EBUSY;
  3813. if (!closing) {
  3814. if (atomic_long_read(&po->mapped))
  3815. goto out;
  3816. if (packet_read_pending(rb))
  3817. goto out;
  3818. }
  3819. if (req->tp_block_nr) {
  3820. unsigned int min_frame_size;
  3821. /* Sanity tests and some calculations */
  3822. err = -EBUSY;
  3823. if (unlikely(rb->pg_vec))
  3824. goto out;
  3825. switch (po->tp_version) {
  3826. case TPACKET_V1:
  3827. po->tp_hdrlen = TPACKET_HDRLEN;
  3828. break;
  3829. case TPACKET_V2:
  3830. po->tp_hdrlen = TPACKET2_HDRLEN;
  3831. break;
  3832. case TPACKET_V3:
  3833. po->tp_hdrlen = TPACKET3_HDRLEN;
  3834. break;
  3835. }
  3836. err = -EINVAL;
  3837. if (unlikely((int)req->tp_block_size <= 0))
  3838. goto out;
  3839. if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
  3840. goto out;
  3841. min_frame_size = po->tp_hdrlen + po->tp_reserve;
  3842. if (po->tp_version >= TPACKET_V3 &&
  3843. req->tp_block_size <
  3844. BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size)
  3845. goto out;
  3846. if (unlikely(req->tp_frame_size < min_frame_size))
  3847. goto out;
  3848. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3849. goto out;
  3850. rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
  3851. if (unlikely(rb->frames_per_block == 0))
  3852. goto out;
  3853. if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr))
  3854. goto out;
  3855. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3856. req->tp_frame_nr))
  3857. goto out;
  3858. err = -ENOMEM;
  3859. order = get_order(req->tp_block_size);
  3860. pg_vec = alloc_pg_vec(req, order);
  3861. if (unlikely(!pg_vec))
  3862. goto out;
  3863. switch (po->tp_version) {
  3864. case TPACKET_V3:
  3865. /* Block transmit is not supported yet */
  3866. if (!tx_ring) {
  3867. init_prb_bdqc(po, rb, pg_vec, req_u);
  3868. } else {
  3869. struct tpacket_req3 *req3 = &req_u->req3;
  3870. if (req3->tp_retire_blk_tov ||
  3871. req3->tp_sizeof_priv ||
  3872. req3->tp_feature_req_word) {
  3873. err = -EINVAL;
  3874. goto out_free_pg_vec;
  3875. }
  3876. }
  3877. break;
  3878. default:
  3879. if (!tx_ring) {
  3880. rx_owner_map = bitmap_alloc(req->tp_frame_nr,
  3881. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  3882. if (!rx_owner_map)
  3883. goto out_free_pg_vec;
  3884. }
  3885. break;
  3886. }
  3887. }
  3888. /* Done */
  3889. else {
  3890. err = -EINVAL;
  3891. if (unlikely(req->tp_frame_nr))
  3892. goto out;
  3893. }
  3894. /* Detach socket from network */
  3895. spin_lock(&po->bind_lock);
  3896. was_running = packet_sock_flag(po, PACKET_SOCK_RUNNING);
  3897. num = po->num;
  3898. WRITE_ONCE(po->num, 0);
  3899. if (was_running)
  3900. __unregister_prot_hook(sk, false);
  3901. spin_unlock(&po->bind_lock);
  3902. synchronize_net();
  3903. err = -EBUSY;
  3904. mutex_lock(&po->pg_vec_lock);
  3905. if (closing || atomic_long_read(&po->mapped) == 0) {
  3906. err = 0;
  3907. spin_lock_bh(&rb_queue->lock);
  3908. swap(rb->pg_vec, pg_vec);
  3909. if (po->tp_version <= TPACKET_V2)
  3910. swap(rb->rx_owner_map, rx_owner_map);
  3911. rb->frame_max = (req->tp_frame_nr - 1);
  3912. rb->head = 0;
  3913. rb->frame_size = req->tp_frame_size;
  3914. spin_unlock_bh(&rb_queue->lock);
  3915. swap(rb->pg_vec_order, order);
  3916. swap(rb->pg_vec_len, req->tp_block_nr);
  3917. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3918. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3919. tpacket_rcv : packet_rcv;
  3920. skb_queue_purge(rb_queue);
  3921. if (atomic_long_read(&po->mapped))
  3922. pr_err("packet_mmap: vma is busy: %ld\n",
  3923. atomic_long_read(&po->mapped));
  3924. }
  3925. mutex_unlock(&po->pg_vec_lock);
  3926. spin_lock(&po->bind_lock);
  3927. WRITE_ONCE(po->num, num);
  3928. if (was_running)
  3929. register_prot_hook(sk);
  3930. spin_unlock(&po->bind_lock);
  3931. if (pg_vec && (po->tp_version > TPACKET_V2)) {
  3932. /* Because we don't support block-based V3 on tx-ring */
  3933. if (!tx_ring)
  3934. prb_shutdown_retire_blk_timer(po, rb_queue);
  3935. }
  3936. out_free_pg_vec:
  3937. if (pg_vec) {
  3938. bitmap_free(rx_owner_map);
  3939. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3940. }
  3941. out:
  3942. return err;
  3943. }
  3944. static int packet_mmap(struct file *file, struct socket *sock,
  3945. struct vm_area_struct *vma)
  3946. {
  3947. struct sock *sk = sock->sk;
  3948. struct packet_sock *po = pkt_sk(sk);
  3949. unsigned long size, expected_size;
  3950. struct packet_ring_buffer *rb;
  3951. unsigned long start;
  3952. int err = -EINVAL;
  3953. int i;
  3954. if (vma->vm_pgoff)
  3955. return -EINVAL;
  3956. mutex_lock(&po->pg_vec_lock);
  3957. expected_size = 0;
  3958. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3959. if (rb->pg_vec) {
  3960. expected_size += rb->pg_vec_len
  3961. * rb->pg_vec_pages
  3962. * PAGE_SIZE;
  3963. }
  3964. }
  3965. if (expected_size == 0)
  3966. goto out;
  3967. size = vma->vm_end - vma->vm_start;
  3968. if (size != expected_size)
  3969. goto out;
  3970. start = vma->vm_start;
  3971. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3972. if (rb->pg_vec == NULL)
  3973. continue;
  3974. for (i = 0; i < rb->pg_vec_len; i++) {
  3975. struct page *page;
  3976. void *kaddr = rb->pg_vec[i].buffer;
  3977. int pg_num;
  3978. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3979. page = pgv_to_page(kaddr);
  3980. err = vm_insert_page(vma, start, page);
  3981. if (unlikely(err))
  3982. goto out;
  3983. start += PAGE_SIZE;
  3984. kaddr += PAGE_SIZE;
  3985. }
  3986. }
  3987. }
  3988. atomic_long_inc(&po->mapped);
  3989. vma->vm_ops = &packet_mmap_ops;
  3990. err = 0;
  3991. out:
  3992. mutex_unlock(&po->pg_vec_lock);
  3993. return err;
  3994. }
  3995. static const struct proto_ops packet_ops_spkt = {
  3996. .family = PF_PACKET,
  3997. .owner = THIS_MODULE,
  3998. .release = packet_release,
  3999. .bind = packet_bind_spkt,
  4000. .connect = sock_no_connect,
  4001. .socketpair = sock_no_socketpair,
  4002. .accept = sock_no_accept,
  4003. .getname = packet_getname_spkt,
  4004. .poll = datagram_poll,
  4005. .ioctl = packet_ioctl,
  4006. .gettstamp = sock_gettstamp,
  4007. .listen = sock_no_listen,
  4008. .shutdown = sock_no_shutdown,
  4009. .sendmsg = packet_sendmsg_spkt,
  4010. .recvmsg = packet_recvmsg,
  4011. .mmap = sock_no_mmap,
  4012. };
  4013. static const struct proto_ops packet_ops = {
  4014. .family = PF_PACKET,
  4015. .owner = THIS_MODULE,
  4016. .release = packet_release,
  4017. .bind = packet_bind,
  4018. .connect = sock_no_connect,
  4019. .socketpair = sock_no_socketpair,
  4020. .accept = sock_no_accept,
  4021. .getname = packet_getname,
  4022. .poll = packet_poll,
  4023. .ioctl = packet_ioctl,
  4024. .gettstamp = sock_gettstamp,
  4025. .listen = sock_no_listen,
  4026. .shutdown = sock_no_shutdown,
  4027. .setsockopt = packet_setsockopt,
  4028. .getsockopt = packet_getsockopt,
  4029. .sendmsg = packet_sendmsg,
  4030. .recvmsg = packet_recvmsg,
  4031. .mmap = packet_mmap,
  4032. };
  4033. static const struct net_proto_family packet_family_ops = {
  4034. .family = PF_PACKET,
  4035. .create = packet_create,
  4036. .owner = THIS_MODULE,
  4037. };
  4038. static struct notifier_block packet_netdev_notifier = {
  4039. .notifier_call = packet_notifier,
  4040. };
  4041. #ifdef CONFIG_PROC_FS
  4042. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  4043. __acquires(RCU)
  4044. {
  4045. struct net *net = seq_file_net(seq);
  4046. rcu_read_lock();
  4047. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  4048. }
  4049. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  4050. {
  4051. struct net *net = seq_file_net(seq);
  4052. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  4053. }
  4054. static void packet_seq_stop(struct seq_file *seq, void *v)
  4055. __releases(RCU)
  4056. {
  4057. rcu_read_unlock();
  4058. }
  4059. static int packet_seq_show(struct seq_file *seq, void *v)
  4060. {
  4061. if (v == SEQ_START_TOKEN)
  4062. seq_printf(seq,
  4063. "%*sRefCnt Type Proto Iface R Rmem User Inode\n",
  4064. IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk");
  4065. else {
  4066. struct sock *s = sk_entry(v);
  4067. const struct packet_sock *po = pkt_sk(s);
  4068. seq_printf(seq,
  4069. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  4070. s,
  4071. refcount_read(&s->sk_refcnt),
  4072. s->sk_type,
  4073. ntohs(READ_ONCE(po->num)),
  4074. READ_ONCE(po->ifindex),
  4075. packet_sock_flag(po, PACKET_SOCK_RUNNING),
  4076. atomic_read(&s->sk_rmem_alloc),
  4077. from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
  4078. sock_i_ino(s));
  4079. }
  4080. return 0;
  4081. }
  4082. static const struct seq_operations packet_seq_ops = {
  4083. .start = packet_seq_start,
  4084. .next = packet_seq_next,
  4085. .stop = packet_seq_stop,
  4086. .show = packet_seq_show,
  4087. };
  4088. #endif
  4089. static int __net_init packet_net_init(struct net *net)
  4090. {
  4091. mutex_init(&net->packet.sklist_lock);
  4092. INIT_HLIST_HEAD(&net->packet.sklist);
  4093. #ifdef CONFIG_PROC_FS
  4094. if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops,
  4095. sizeof(struct seq_net_private)))
  4096. return -ENOMEM;
  4097. #endif /* CONFIG_PROC_FS */
  4098. return 0;
  4099. }
  4100. static void __net_exit packet_net_exit(struct net *net)
  4101. {
  4102. remove_proc_entry("packet", net->proc_net);
  4103. WARN_ON_ONCE(!hlist_empty(&net->packet.sklist));
  4104. }
  4105. static struct pernet_operations packet_net_ops = {
  4106. .init = packet_net_init,
  4107. .exit = packet_net_exit,
  4108. };
  4109. static void __exit packet_exit(void)
  4110. {
  4111. sock_unregister(PF_PACKET);
  4112. proto_unregister(&packet_proto);
  4113. unregister_netdevice_notifier(&packet_netdev_notifier);
  4114. unregister_pernet_subsys(&packet_net_ops);
  4115. }
  4116. static int __init packet_init(void)
  4117. {
  4118. int rc;
  4119. rc = register_pernet_subsys(&packet_net_ops);
  4120. if (rc)
  4121. goto out;
  4122. rc = register_netdevice_notifier(&packet_netdev_notifier);
  4123. if (rc)
  4124. goto out_pernet;
  4125. rc = proto_register(&packet_proto, 0);
  4126. if (rc)
  4127. goto out_notifier;
  4128. rc = sock_register(&packet_family_ops);
  4129. if (rc)
  4130. goto out_proto;
  4131. return 0;
  4132. out_proto:
  4133. proto_unregister(&packet_proto);
  4134. out_notifier:
  4135. unregister_netdevice_notifier(&packet_netdev_notifier);
  4136. out_pernet:
  4137. unregister_pernet_subsys(&packet_net_ops);
  4138. out:
  4139. return rc;
  4140. }
  4141. module_init(packet_init);
  4142. module_exit(packet_exit);
  4143. MODULE_DESCRIPTION("Packet socket support (AF_PACKET)");
  4144. MODULE_LICENSE("GPL");
  4145. MODULE_ALIAS_NETPROTO(PF_PACKET);