af_packet.c 103 KB

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