af_packet.c 106 KB

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