af_packet.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521
  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 vio_le() virtio_legacy_is_little_endian()
  207. #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
  208. #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
  209. #define GET_PBLOCK_DESC(x, bid) \
  210. ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
  211. #define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
  212. ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
  213. #define GET_NEXT_PRB_BLK_NUM(x) \
  214. (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
  215. ((x)->kactive_blk_num+1) : 0)
  216. static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
  217. static void __fanout_link(struct sock *sk, struct packet_sock *po);
  218. static int packet_direct_xmit(struct sk_buff *skb)
  219. {
  220. struct net_device *dev = skb->dev;
  221. netdev_features_t features;
  222. struct netdev_queue *txq;
  223. int ret = NETDEV_TX_BUSY;
  224. if (unlikely(!netif_running(dev) ||
  225. !netif_carrier_ok(dev)))
  226. goto drop;
  227. features = netif_skb_features(skb);
  228. if (skb_needs_linearize(skb, features) &&
  229. __skb_linearize(skb))
  230. goto drop;
  231. txq = skb_get_tx_queue(dev, skb);
  232. local_bh_disable();
  233. HARD_TX_LOCK(dev, txq, smp_processor_id());
  234. if (!netif_xmit_frozen_or_drv_stopped(txq))
  235. ret = netdev_start_xmit(skb, dev, txq, false);
  236. HARD_TX_UNLOCK(dev, txq);
  237. local_bh_enable();
  238. if (!dev_xmit_complete(ret))
  239. kfree_skb(skb);
  240. return ret;
  241. drop:
  242. atomic_long_inc(&dev->tx_dropped);
  243. kfree_skb(skb);
  244. return NET_XMIT_DROP;
  245. }
  246. static struct net_device *packet_cached_dev_get(struct packet_sock *po)
  247. {
  248. struct net_device *dev;
  249. rcu_read_lock();
  250. dev = rcu_dereference(po->cached_dev);
  251. if (likely(dev))
  252. dev_hold(dev);
  253. rcu_read_unlock();
  254. return dev;
  255. }
  256. static void packet_cached_dev_assign(struct packet_sock *po,
  257. struct net_device *dev)
  258. {
  259. rcu_assign_pointer(po->cached_dev, dev);
  260. }
  261. static void packet_cached_dev_reset(struct packet_sock *po)
  262. {
  263. RCU_INIT_POINTER(po->cached_dev, NULL);
  264. }
  265. static bool packet_use_direct_xmit(const struct packet_sock *po)
  266. {
  267. return po->xmit == packet_direct_xmit;
  268. }
  269. static u16 __packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
  270. {
  271. return (u16) raw_smp_processor_id() % dev->real_num_tx_queues;
  272. }
  273. static void packet_pick_tx_queue(struct net_device *dev, struct sk_buff *skb)
  274. {
  275. const struct net_device_ops *ops = dev->netdev_ops;
  276. u16 queue_index;
  277. if (ops->ndo_select_queue) {
  278. queue_index = ops->ndo_select_queue(dev, skb, NULL,
  279. __packet_pick_tx_queue);
  280. queue_index = netdev_cap_txqueue(dev, queue_index);
  281. } else {
  282. queue_index = __packet_pick_tx_queue(dev, skb);
  283. }
  284. skb_set_queue_mapping(skb, queue_index);
  285. }
  286. /* register_prot_hook must be invoked with the po->bind_lock held,
  287. * or from a context in which asynchronous accesses to the packet
  288. * socket is not possible (packet_create()).
  289. */
  290. static void register_prot_hook(struct sock *sk)
  291. {
  292. struct packet_sock *po = pkt_sk(sk);
  293. if (!po->running) {
  294. if (po->fanout)
  295. __fanout_link(sk, po);
  296. else
  297. dev_add_pack(&po->prot_hook);
  298. sock_hold(sk);
  299. po->running = 1;
  300. }
  301. }
  302. /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
  303. * held. If the sync parameter is true, we will temporarily drop
  304. * the po->bind_lock and do a synchronize_net to make sure no
  305. * asynchronous packet processing paths still refer to the elements
  306. * of po->prot_hook. If the sync parameter is false, it is the
  307. * callers responsibility to take care of this.
  308. */
  309. static void __unregister_prot_hook(struct sock *sk, bool sync)
  310. {
  311. struct packet_sock *po = pkt_sk(sk);
  312. po->running = 0;
  313. if (po->fanout)
  314. __fanout_unlink(sk, po);
  315. else
  316. __dev_remove_pack(&po->prot_hook);
  317. __sock_put(sk);
  318. if (sync) {
  319. spin_unlock(&po->bind_lock);
  320. synchronize_net();
  321. spin_lock(&po->bind_lock);
  322. }
  323. }
  324. static void unregister_prot_hook(struct sock *sk, bool sync)
  325. {
  326. struct packet_sock *po = pkt_sk(sk);
  327. if (po->running)
  328. __unregister_prot_hook(sk, sync);
  329. }
  330. static inline struct page * __pure pgv_to_page(void *addr)
  331. {
  332. if (is_vmalloc_addr(addr))
  333. return vmalloc_to_page(addr);
  334. return virt_to_page(addr);
  335. }
  336. static void __packet_set_status(struct packet_sock *po, void *frame, int status)
  337. {
  338. union tpacket_uhdr h;
  339. h.raw = frame;
  340. switch (po->tp_version) {
  341. case TPACKET_V1:
  342. h.h1->tp_status = status;
  343. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  344. break;
  345. case TPACKET_V2:
  346. h.h2->tp_status = status;
  347. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  348. break;
  349. case TPACKET_V3:
  350. default:
  351. WARN(1, "TPACKET version not supported.\n");
  352. BUG();
  353. }
  354. smp_wmb();
  355. }
  356. static int __packet_get_status(struct packet_sock *po, void *frame)
  357. {
  358. union tpacket_uhdr h;
  359. smp_rmb();
  360. h.raw = frame;
  361. switch (po->tp_version) {
  362. case TPACKET_V1:
  363. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  364. return h.h1->tp_status;
  365. case TPACKET_V2:
  366. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  367. return h.h2->tp_status;
  368. case TPACKET_V3:
  369. default:
  370. WARN(1, "TPACKET version not supported.\n");
  371. BUG();
  372. return 0;
  373. }
  374. }
  375. static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
  376. unsigned int flags)
  377. {
  378. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  379. if (shhwtstamps &&
  380. (flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  381. ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
  382. return TP_STATUS_TS_RAW_HARDWARE;
  383. if (ktime_to_timespec_cond(skb->tstamp, ts))
  384. return TP_STATUS_TS_SOFTWARE;
  385. return 0;
  386. }
  387. static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
  388. struct sk_buff *skb)
  389. {
  390. union tpacket_uhdr h;
  391. struct timespec ts;
  392. __u32 ts_status;
  393. if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
  394. return 0;
  395. h.raw = frame;
  396. switch (po->tp_version) {
  397. case TPACKET_V1:
  398. h.h1->tp_sec = ts.tv_sec;
  399. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  400. break;
  401. case TPACKET_V2:
  402. h.h2->tp_sec = ts.tv_sec;
  403. h.h2->tp_nsec = ts.tv_nsec;
  404. break;
  405. case TPACKET_V3:
  406. default:
  407. WARN(1, "TPACKET version not supported.\n");
  408. BUG();
  409. }
  410. /* one flush is safe, as both fields always lie on the same cacheline */
  411. flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
  412. smp_wmb();
  413. return ts_status;
  414. }
  415. static void *packet_lookup_frame(struct packet_sock *po,
  416. struct packet_ring_buffer *rb,
  417. unsigned int position,
  418. int status)
  419. {
  420. unsigned int pg_vec_pos, frame_offset;
  421. union tpacket_uhdr h;
  422. pg_vec_pos = position / rb->frames_per_block;
  423. frame_offset = position % rb->frames_per_block;
  424. h.raw = rb->pg_vec[pg_vec_pos].buffer +
  425. (frame_offset * rb->frame_size);
  426. if (status != __packet_get_status(po, h.raw))
  427. return NULL;
  428. return h.raw;
  429. }
  430. static void *packet_current_frame(struct packet_sock *po,
  431. struct packet_ring_buffer *rb,
  432. int status)
  433. {
  434. return packet_lookup_frame(po, rb, rb->head, status);
  435. }
  436. static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
  437. {
  438. del_timer_sync(&pkc->retire_blk_timer);
  439. }
  440. static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
  441. struct sk_buff_head *rb_queue)
  442. {
  443. struct tpacket_kbdq_core *pkc;
  444. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  445. spin_lock_bh(&rb_queue->lock);
  446. pkc->delete_blk_timer = 1;
  447. spin_unlock_bh(&rb_queue->lock);
  448. prb_del_retire_blk_timer(pkc);
  449. }
  450. static void prb_init_blk_timer(struct packet_sock *po,
  451. struct tpacket_kbdq_core *pkc,
  452. void (*func) (unsigned long))
  453. {
  454. init_timer(&pkc->retire_blk_timer);
  455. pkc->retire_blk_timer.data = (long)po;
  456. pkc->retire_blk_timer.function = func;
  457. pkc->retire_blk_timer.expires = jiffies;
  458. }
  459. static void prb_setup_retire_blk_timer(struct packet_sock *po)
  460. {
  461. struct tpacket_kbdq_core *pkc;
  462. pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
  463. prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
  464. }
  465. static int prb_calc_retire_blk_tmo(struct packet_sock *po,
  466. int blk_size_in_bytes)
  467. {
  468. struct net_device *dev;
  469. unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
  470. struct ethtool_link_ksettings ecmd;
  471. int err;
  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_link_ksettings(dev, &ecmd);
  479. rtnl_unlock();
  480. if (!err) {
  481. /*
  482. * If the link speed is so slow you don't really
  483. * need to worry about perf anyways
  484. */
  485. if (ecmd.base.speed < SPEED_1000 ||
  486. ecmd.base.speed == SPEED_UNKNOWN) {
  487. return DEFAULT_PRB_RETIRE_TOV;
  488. } else {
  489. msec = 1;
  490. div = ecmd.base.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_clear_cb(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 net *net = read_pnet(&f->net);
  1218. struct packet_sock *po;
  1219. unsigned int idx;
  1220. if (!net_eq(dev_net(dev), net) || !num) {
  1221. kfree_skb(skb);
  1222. return 0;
  1223. }
  1224. if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
  1225. skb = ip_check_defrag(net, 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 (sk->sk_family != PF_PACKET)
  1288. return false;
  1289. return ptype->af_packet_priv == pkt_sk(sk)->fanout;
  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, false);
  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 bool packet_extra_vlan_len_allowed(const struct net_device *dev,
  1483. struct sk_buff *skb)
  1484. {
  1485. /* Earlier code assumed this would be a VLAN pkt, double-check
  1486. * this now that we have the actual packet in hand. We can only
  1487. * do this check on Ethernet devices.
  1488. */
  1489. if (unlikely(dev->type != ARPHRD_ETHER))
  1490. return false;
  1491. skb_reset_mac_header(skb);
  1492. return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q));
  1493. }
  1494. static const struct proto_ops packet_ops;
  1495. static const struct proto_ops packet_ops_spkt;
  1496. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
  1497. struct packet_type *pt, struct net_device *orig_dev)
  1498. {
  1499. struct sock *sk;
  1500. struct sockaddr_pkt *spkt;
  1501. /*
  1502. * When we registered the protocol we saved the socket in the data
  1503. * field for just this event.
  1504. */
  1505. sk = pt->af_packet_priv;
  1506. /*
  1507. * Yank back the headers [hope the device set this
  1508. * right or kerboom...]
  1509. *
  1510. * Incoming packets have ll header pulled,
  1511. * push it back.
  1512. *
  1513. * For outgoing ones skb->data == skb_mac_header(skb)
  1514. * so that this procedure is noop.
  1515. */
  1516. if (skb->pkt_type == PACKET_LOOPBACK)
  1517. goto out;
  1518. if (!net_eq(dev_net(dev), sock_net(sk)))
  1519. goto out;
  1520. skb = skb_share_check(skb, GFP_ATOMIC);
  1521. if (skb == NULL)
  1522. goto oom;
  1523. /* drop any routing info */
  1524. skb_dst_drop(skb);
  1525. /* drop conntrack reference */
  1526. nf_reset(skb);
  1527. spkt = &PACKET_SKB_CB(skb)->sa.pkt;
  1528. skb_push(skb, skb->data - skb_mac_header(skb));
  1529. /*
  1530. * The SOCK_PACKET socket receives _all_ frames.
  1531. */
  1532. spkt->spkt_family = dev->type;
  1533. strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  1534. spkt->spkt_protocol = skb->protocol;
  1535. /*
  1536. * Charge the memory to the socket. This is done specifically
  1537. * to prevent sockets using all the memory up.
  1538. */
  1539. if (sock_queue_rcv_skb(sk, skb) == 0)
  1540. return 0;
  1541. out:
  1542. kfree_skb(skb);
  1543. oom:
  1544. return 0;
  1545. }
  1546. /*
  1547. * Output a raw packet to a device layer. This bypasses all the other
  1548. * protocol layers and you must therefore supply it with a complete frame
  1549. */
  1550. static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
  1551. size_t len)
  1552. {
  1553. struct sock *sk = sock->sk;
  1554. DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
  1555. struct sk_buff *skb = NULL;
  1556. struct net_device *dev;
  1557. __be16 proto = 0;
  1558. int err;
  1559. int extra_len = 0;
  1560. /*
  1561. * Get and verify the address.
  1562. */
  1563. if (saddr) {
  1564. if (msg->msg_namelen < sizeof(struct sockaddr))
  1565. return -EINVAL;
  1566. if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
  1567. proto = saddr->spkt_protocol;
  1568. } else
  1569. return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
  1570. /*
  1571. * Find the device first to size check it
  1572. */
  1573. saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
  1574. retry:
  1575. rcu_read_lock();
  1576. dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
  1577. err = -ENODEV;
  1578. if (dev == NULL)
  1579. goto out_unlock;
  1580. err = -ENETDOWN;
  1581. if (!(dev->flags & IFF_UP))
  1582. goto out_unlock;
  1583. /*
  1584. * You may not queue a frame bigger than the mtu. This is the lowest level
  1585. * raw protocol and you must do your own fragmentation at this level.
  1586. */
  1587. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  1588. if (!netif_supports_nofcs(dev)) {
  1589. err = -EPROTONOSUPPORT;
  1590. goto out_unlock;
  1591. }
  1592. extra_len = 4; /* We're doing our own CRC */
  1593. }
  1594. err = -EMSGSIZE;
  1595. if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
  1596. goto out_unlock;
  1597. if (!skb) {
  1598. size_t reserved = LL_RESERVED_SPACE(dev);
  1599. int tlen = dev->needed_tailroom;
  1600. unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
  1601. rcu_read_unlock();
  1602. skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
  1603. if (skb == NULL)
  1604. return -ENOBUFS;
  1605. /* FIXME: Save some space for broken drivers that write a hard
  1606. * header at transmission time by themselves. PPP is the notable
  1607. * one here. This should really be fixed at the driver level.
  1608. */
  1609. skb_reserve(skb, reserved);
  1610. skb_reset_network_header(skb);
  1611. /* Try to align data part correctly */
  1612. if (hhlen) {
  1613. skb->data -= hhlen;
  1614. skb->tail -= hhlen;
  1615. if (len < hhlen)
  1616. skb_reset_network_header(skb);
  1617. }
  1618. err = memcpy_from_msg(skb_put(skb, len), msg, len);
  1619. if (err)
  1620. goto out_free;
  1621. goto retry;
  1622. }
  1623. if (!dev_validate_header(dev, skb->data, len)) {
  1624. err = -EINVAL;
  1625. goto out_unlock;
  1626. }
  1627. if (len > (dev->mtu + dev->hard_header_len + extra_len) &&
  1628. !packet_extra_vlan_len_allowed(dev, skb)) {
  1629. err = -EMSGSIZE;
  1630. goto out_unlock;
  1631. }
  1632. skb->protocol = proto;
  1633. skb->dev = dev;
  1634. skb->priority = sk->sk_priority;
  1635. skb->mark = sk->sk_mark;
  1636. sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  1637. if (unlikely(extra_len == 4))
  1638. skb->no_fcs = 1;
  1639. skb_probe_transport_header(skb, 0);
  1640. dev_queue_xmit(skb);
  1641. rcu_read_unlock();
  1642. return len;
  1643. out_unlock:
  1644. rcu_read_unlock();
  1645. out_free:
  1646. kfree_skb(skb);
  1647. return err;
  1648. }
  1649. static unsigned int run_filter(struct sk_buff *skb,
  1650. const struct sock *sk,
  1651. unsigned int res)
  1652. {
  1653. struct sk_filter *filter;
  1654. rcu_read_lock();
  1655. filter = rcu_dereference(sk->sk_filter);
  1656. if (filter != NULL)
  1657. res = bpf_prog_run_clear_cb(filter->prog, skb);
  1658. rcu_read_unlock();
  1659. return res;
  1660. }
  1661. static int __packet_rcv_vnet(const struct sk_buff *skb,
  1662. struct virtio_net_hdr *vnet_hdr)
  1663. {
  1664. *vnet_hdr = (const struct virtio_net_hdr) { 0 };
  1665. if (skb_is_gso(skb)) {
  1666. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1667. /* This is a hint as to how much should be linear. */
  1668. vnet_hdr->hdr_len =
  1669. __cpu_to_virtio16(vio_le(), skb_headlen(skb));
  1670. vnet_hdr->gso_size =
  1671. __cpu_to_virtio16(vio_le(), sinfo->gso_size);
  1672. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1673. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1674. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1675. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1676. else if (sinfo->gso_type & SKB_GSO_UDP)
  1677. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1678. else if (sinfo->gso_type & SKB_GSO_FCOE)
  1679. return -EINVAL;
  1680. else
  1681. BUG();
  1682. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1683. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1684. } else
  1685. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1686. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1687. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1688. vnet_hdr->csum_start = __cpu_to_virtio16(vio_le(),
  1689. skb_checksum_start_offset(skb));
  1690. vnet_hdr->csum_offset = __cpu_to_virtio16(vio_le(),
  1691. skb->csum_offset);
  1692. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1693. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1694. } /* else everything is zero */
  1695. return 0;
  1696. }
  1697. static int packet_rcv_vnet(struct msghdr *msg, const struct sk_buff *skb,
  1698. size_t *len)
  1699. {
  1700. struct virtio_net_hdr vnet_hdr;
  1701. if (*len < sizeof(vnet_hdr))
  1702. return -EINVAL;
  1703. *len -= sizeof(vnet_hdr);
  1704. if (__packet_rcv_vnet(skb, &vnet_hdr))
  1705. return -EINVAL;
  1706. return memcpy_to_msg(msg, (void *)&vnet_hdr, sizeof(vnet_hdr));
  1707. }
  1708. /*
  1709. * This function makes lazy skb cloning in hope that most of packets
  1710. * are discarded by BPF.
  1711. *
  1712. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  1713. * and skb->cb are mangled. It works because (and until) packets
  1714. * falling here are owned by current CPU. Output packets are cloned
  1715. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  1716. * sequencially, so that if we return skb to original state on exit,
  1717. * we will not harm anyone.
  1718. */
  1719. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  1720. struct packet_type *pt, struct net_device *orig_dev)
  1721. {
  1722. struct sock *sk;
  1723. struct sockaddr_ll *sll;
  1724. struct packet_sock *po;
  1725. u8 *skb_head = skb->data;
  1726. int skb_len = skb->len;
  1727. unsigned int snaplen, res;
  1728. if (skb->pkt_type == PACKET_LOOPBACK)
  1729. goto drop;
  1730. sk = pt->af_packet_priv;
  1731. po = pkt_sk(sk);
  1732. if (!net_eq(dev_net(dev), sock_net(sk)))
  1733. goto drop;
  1734. skb->dev = dev;
  1735. if (dev->header_ops) {
  1736. /* The device has an explicit notion of ll header,
  1737. * exported to higher levels.
  1738. *
  1739. * Otherwise, the device hides details of its frame
  1740. * structure, so that corresponding packet head is
  1741. * never delivered to user.
  1742. */
  1743. if (sk->sk_type != SOCK_DGRAM)
  1744. skb_push(skb, skb->data - skb_mac_header(skb));
  1745. else if (skb->pkt_type == PACKET_OUTGOING) {
  1746. /* Special case: outgoing packets have ll header at head */
  1747. skb_pull(skb, skb_network_offset(skb));
  1748. }
  1749. }
  1750. snaplen = skb->len;
  1751. res = run_filter(skb, sk, snaplen);
  1752. if (!res)
  1753. goto drop_n_restore;
  1754. if (snaplen > res)
  1755. snaplen = res;
  1756. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  1757. goto drop_n_acct;
  1758. if (skb_shared(skb)) {
  1759. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  1760. if (nskb == NULL)
  1761. goto drop_n_acct;
  1762. if (skb_head != skb->data) {
  1763. skb->data = skb_head;
  1764. skb->len = skb_len;
  1765. }
  1766. consume_skb(skb);
  1767. skb = nskb;
  1768. }
  1769. sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
  1770. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1771. sll->sll_hatype = dev->type;
  1772. sll->sll_pkttype = skb->pkt_type;
  1773. if (unlikely(po->origdev))
  1774. sll->sll_ifindex = orig_dev->ifindex;
  1775. else
  1776. sll->sll_ifindex = dev->ifindex;
  1777. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1778. /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
  1779. * Use their space for storing the original skb length.
  1780. */
  1781. PACKET_SKB_CB(skb)->sa.origlen = skb->len;
  1782. if (pskb_trim(skb, snaplen))
  1783. goto drop_n_acct;
  1784. skb_set_owner_r(skb, sk);
  1785. skb->dev = NULL;
  1786. skb_dst_drop(skb);
  1787. /* drop conntrack reference */
  1788. nf_reset(skb);
  1789. spin_lock(&sk->sk_receive_queue.lock);
  1790. po->stats.stats1.tp_packets++;
  1791. sock_skb_set_dropcount(sk, skb);
  1792. __skb_queue_tail(&sk->sk_receive_queue, skb);
  1793. spin_unlock(&sk->sk_receive_queue.lock);
  1794. sk->sk_data_ready(sk);
  1795. return 0;
  1796. drop_n_acct:
  1797. spin_lock(&sk->sk_receive_queue.lock);
  1798. po->stats.stats1.tp_drops++;
  1799. atomic_inc(&sk->sk_drops);
  1800. spin_unlock(&sk->sk_receive_queue.lock);
  1801. drop_n_restore:
  1802. if (skb_head != skb->data && skb_shared(skb)) {
  1803. skb->data = skb_head;
  1804. skb->len = skb_len;
  1805. }
  1806. drop:
  1807. consume_skb(skb);
  1808. return 0;
  1809. }
  1810. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  1811. struct packet_type *pt, struct net_device *orig_dev)
  1812. {
  1813. struct sock *sk;
  1814. struct packet_sock *po;
  1815. struct sockaddr_ll *sll;
  1816. union tpacket_uhdr h;
  1817. u8 *skb_head = skb->data;
  1818. int skb_len = skb->len;
  1819. unsigned int snaplen, res;
  1820. unsigned long status = TP_STATUS_USER;
  1821. unsigned short macoff, netoff, hdrlen;
  1822. struct sk_buff *copy_skb = NULL;
  1823. struct timespec ts;
  1824. __u32 ts_status;
  1825. /* struct tpacket{2,3}_hdr is aligned to a multiple of TPACKET_ALIGNMENT.
  1826. * We may add members to them until current aligned size without forcing
  1827. * userspace to call getsockopt(..., PACKET_HDRLEN, ...).
  1828. */
  1829. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h2)) != 32);
  1830. BUILD_BUG_ON(TPACKET_ALIGN(sizeof(*h.h3)) != 48);
  1831. if (skb->pkt_type == PACKET_LOOPBACK)
  1832. goto drop;
  1833. sk = pt->af_packet_priv;
  1834. po = pkt_sk(sk);
  1835. if (!net_eq(dev_net(dev), sock_net(sk)))
  1836. goto drop;
  1837. if (dev->header_ops) {
  1838. if (sk->sk_type != SOCK_DGRAM)
  1839. skb_push(skb, skb->data - skb_mac_header(skb));
  1840. else if (skb->pkt_type == PACKET_OUTGOING) {
  1841. /* Special case: outgoing packets have ll header at head */
  1842. skb_pull(skb, skb_network_offset(skb));
  1843. }
  1844. }
  1845. snaplen = skb->len;
  1846. res = run_filter(skb, sk, snaplen);
  1847. if (!res)
  1848. goto drop_n_restore;
  1849. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1850. status |= TP_STATUS_CSUMNOTREADY;
  1851. else if (skb->pkt_type != PACKET_OUTGOING &&
  1852. (skb->ip_summed == CHECKSUM_COMPLETE ||
  1853. skb_csum_unnecessary(skb)))
  1854. status |= TP_STATUS_CSUM_VALID;
  1855. if (snaplen > res)
  1856. snaplen = res;
  1857. if (sk->sk_type == SOCK_DGRAM) {
  1858. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  1859. po->tp_reserve;
  1860. } else {
  1861. unsigned int maclen = skb_network_offset(skb);
  1862. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  1863. (maclen < 16 ? 16 : maclen)) +
  1864. po->tp_reserve;
  1865. if (po->has_vnet_hdr)
  1866. netoff += sizeof(struct virtio_net_hdr);
  1867. macoff = netoff - maclen;
  1868. }
  1869. if (po->tp_version <= TPACKET_V2) {
  1870. if (macoff + snaplen > po->rx_ring.frame_size) {
  1871. if (po->copy_thresh &&
  1872. atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
  1873. if (skb_shared(skb)) {
  1874. copy_skb = skb_clone(skb, GFP_ATOMIC);
  1875. } else {
  1876. copy_skb = skb_get(skb);
  1877. skb_head = skb->data;
  1878. }
  1879. if (copy_skb)
  1880. skb_set_owner_r(copy_skb, sk);
  1881. }
  1882. snaplen = po->rx_ring.frame_size - macoff;
  1883. if ((int)snaplen < 0)
  1884. snaplen = 0;
  1885. }
  1886. } else if (unlikely(macoff + snaplen >
  1887. GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) {
  1888. u32 nval;
  1889. nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff;
  1890. pr_err_once("tpacket_rcv: packet too big, clamped from %u to %u. macoff=%u\n",
  1891. snaplen, nval, macoff);
  1892. snaplen = nval;
  1893. if (unlikely((int)snaplen < 0)) {
  1894. snaplen = 0;
  1895. macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len;
  1896. }
  1897. }
  1898. spin_lock(&sk->sk_receive_queue.lock);
  1899. h.raw = packet_current_rx_frame(po, skb,
  1900. TP_STATUS_KERNEL, (macoff+snaplen));
  1901. if (!h.raw)
  1902. goto drop_n_account;
  1903. if (po->tp_version <= TPACKET_V2) {
  1904. packet_increment_rx_head(po, &po->rx_ring);
  1905. /*
  1906. * LOSING will be reported till you read the stats,
  1907. * because it's COR - Clear On Read.
  1908. * Anyways, moving it for V1/V2 only as V3 doesn't need this
  1909. * at packet level.
  1910. */
  1911. if (po->stats.stats1.tp_drops)
  1912. status |= TP_STATUS_LOSING;
  1913. }
  1914. po->stats.stats1.tp_packets++;
  1915. if (copy_skb) {
  1916. status |= TP_STATUS_COPY;
  1917. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  1918. }
  1919. spin_unlock(&sk->sk_receive_queue.lock);
  1920. if (po->has_vnet_hdr) {
  1921. if (__packet_rcv_vnet(skb, h.raw + macoff -
  1922. sizeof(struct virtio_net_hdr))) {
  1923. spin_lock(&sk->sk_receive_queue.lock);
  1924. goto drop_n_account;
  1925. }
  1926. }
  1927. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  1928. if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
  1929. getnstimeofday(&ts);
  1930. status |= ts_status;
  1931. switch (po->tp_version) {
  1932. case TPACKET_V1:
  1933. h.h1->tp_len = skb->len;
  1934. h.h1->tp_snaplen = snaplen;
  1935. h.h1->tp_mac = macoff;
  1936. h.h1->tp_net = netoff;
  1937. h.h1->tp_sec = ts.tv_sec;
  1938. h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
  1939. hdrlen = sizeof(*h.h1);
  1940. break;
  1941. case TPACKET_V2:
  1942. h.h2->tp_len = skb->len;
  1943. h.h2->tp_snaplen = snaplen;
  1944. h.h2->tp_mac = macoff;
  1945. h.h2->tp_net = netoff;
  1946. h.h2->tp_sec = ts.tv_sec;
  1947. h.h2->tp_nsec = ts.tv_nsec;
  1948. if (skb_vlan_tag_present(skb)) {
  1949. h.h2->tp_vlan_tci = skb_vlan_tag_get(skb);
  1950. h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto);
  1951. status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  1952. } else {
  1953. h.h2->tp_vlan_tci = 0;
  1954. h.h2->tp_vlan_tpid = 0;
  1955. }
  1956. memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding));
  1957. hdrlen = sizeof(*h.h2);
  1958. break;
  1959. case TPACKET_V3:
  1960. /* tp_nxt_offset,vlan are already populated above.
  1961. * So DONT clear those fields here
  1962. */
  1963. h.h3->tp_status |= status;
  1964. h.h3->tp_len = skb->len;
  1965. h.h3->tp_snaplen = snaplen;
  1966. h.h3->tp_mac = macoff;
  1967. h.h3->tp_net = netoff;
  1968. h.h3->tp_sec = ts.tv_sec;
  1969. h.h3->tp_nsec = ts.tv_nsec;
  1970. memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding));
  1971. hdrlen = sizeof(*h.h3);
  1972. break;
  1973. default:
  1974. BUG();
  1975. }
  1976. sll = h.raw + TPACKET_ALIGN(hdrlen);
  1977. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  1978. sll->sll_family = AF_PACKET;
  1979. sll->sll_hatype = dev->type;
  1980. sll->sll_protocol = skb->protocol;
  1981. sll->sll_pkttype = skb->pkt_type;
  1982. if (unlikely(po->origdev))
  1983. sll->sll_ifindex = orig_dev->ifindex;
  1984. else
  1985. sll->sll_ifindex = dev->ifindex;
  1986. smp_mb();
  1987. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  1988. if (po->tp_version <= TPACKET_V2) {
  1989. u8 *start, *end;
  1990. end = (u8 *) PAGE_ALIGN((unsigned long) h.raw +
  1991. macoff + snaplen);
  1992. for (start = h.raw; start < end; start += PAGE_SIZE)
  1993. flush_dcache_page(pgv_to_page(start));
  1994. }
  1995. smp_wmb();
  1996. #endif
  1997. if (po->tp_version <= TPACKET_V2) {
  1998. __packet_set_status(po, h.raw, status);
  1999. sk->sk_data_ready(sk);
  2000. } else {
  2001. prb_clear_blk_fill_status(&po->rx_ring);
  2002. }
  2003. drop_n_restore:
  2004. if (skb_head != skb->data && skb_shared(skb)) {
  2005. skb->data = skb_head;
  2006. skb->len = skb_len;
  2007. }
  2008. drop:
  2009. kfree_skb(skb);
  2010. return 0;
  2011. drop_n_account:
  2012. po->stats.stats1.tp_drops++;
  2013. spin_unlock(&sk->sk_receive_queue.lock);
  2014. sk->sk_data_ready(sk);
  2015. kfree_skb(copy_skb);
  2016. goto drop_n_restore;
  2017. }
  2018. static void tpacket_destruct_skb(struct sk_buff *skb)
  2019. {
  2020. struct packet_sock *po = pkt_sk(skb->sk);
  2021. if (likely(po->tx_ring.pg_vec)) {
  2022. void *ph;
  2023. __u32 ts;
  2024. ph = skb_shinfo(skb)->destructor_arg;
  2025. packet_dec_pending(&po->tx_ring);
  2026. ts = __packet_set_timestamp(po, ph, skb);
  2027. __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
  2028. }
  2029. sock_wfree(skb);
  2030. }
  2031. static void tpacket_set_protocol(const struct net_device *dev,
  2032. struct sk_buff *skb)
  2033. {
  2034. if (dev->type == ARPHRD_ETHER) {
  2035. skb_reset_mac_header(skb);
  2036. skb->protocol = eth_hdr(skb)->h_proto;
  2037. }
  2038. }
  2039. static int __packet_snd_vnet_parse(struct virtio_net_hdr *vnet_hdr, size_t len)
  2040. {
  2041. unsigned short gso_type = 0;
  2042. if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  2043. (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2044. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 >
  2045. __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len)))
  2046. vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(),
  2047. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) +
  2048. __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2);
  2049. if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len)
  2050. return -EINVAL;
  2051. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  2052. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  2053. case VIRTIO_NET_HDR_GSO_TCPV4:
  2054. gso_type = SKB_GSO_TCPV4;
  2055. break;
  2056. case VIRTIO_NET_HDR_GSO_TCPV6:
  2057. gso_type = SKB_GSO_TCPV6;
  2058. break;
  2059. case VIRTIO_NET_HDR_GSO_UDP:
  2060. gso_type = SKB_GSO_UDP;
  2061. break;
  2062. default:
  2063. return -EINVAL;
  2064. }
  2065. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  2066. gso_type |= SKB_GSO_TCP_ECN;
  2067. if (vnet_hdr->gso_size == 0)
  2068. return -EINVAL;
  2069. }
  2070. vnet_hdr->gso_type = gso_type; /* changes type, temporary storage */
  2071. return 0;
  2072. }
  2073. static int packet_snd_vnet_parse(struct msghdr *msg, size_t *len,
  2074. struct virtio_net_hdr *vnet_hdr)
  2075. {
  2076. int n;
  2077. if (*len < sizeof(*vnet_hdr))
  2078. return -EINVAL;
  2079. *len -= sizeof(*vnet_hdr);
  2080. n = copy_from_iter(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter);
  2081. if (n != sizeof(*vnet_hdr))
  2082. return -EFAULT;
  2083. return __packet_snd_vnet_parse(vnet_hdr, *len);
  2084. }
  2085. static int packet_snd_vnet_gso(struct sk_buff *skb,
  2086. struct virtio_net_hdr *vnet_hdr)
  2087. {
  2088. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  2089. u16 s = __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start);
  2090. u16 o = __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset);
  2091. if (!skb_partial_csum_set(skb, s, o))
  2092. return -EINVAL;
  2093. }
  2094. skb_shinfo(skb)->gso_size =
  2095. __virtio16_to_cpu(vio_le(), vnet_hdr->gso_size);
  2096. skb_shinfo(skb)->gso_type = vnet_hdr->gso_type;
  2097. /* Header must be checked, and gso_segs computed. */
  2098. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  2099. skb_shinfo(skb)->gso_segs = 0;
  2100. return 0;
  2101. }
  2102. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  2103. void *frame, struct net_device *dev, void *data, int tp_len,
  2104. __be16 proto, unsigned char *addr, int hlen, int copylen)
  2105. {
  2106. union tpacket_uhdr ph;
  2107. int to_write, offset, len, nr_frags, len_max;
  2108. struct socket *sock = po->sk.sk_socket;
  2109. struct page *page;
  2110. int err;
  2111. ph.raw = frame;
  2112. skb->protocol = proto;
  2113. skb->dev = dev;
  2114. skb->priority = po->sk.sk_priority;
  2115. skb->mark = po->sk.sk_mark;
  2116. sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
  2117. skb_shinfo(skb)->destructor_arg = ph.raw;
  2118. skb_reserve(skb, hlen);
  2119. skb_reset_network_header(skb);
  2120. to_write = tp_len;
  2121. if (sock->type == SOCK_DGRAM) {
  2122. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  2123. NULL, tp_len);
  2124. if (unlikely(err < 0))
  2125. return -EINVAL;
  2126. } else if (copylen) {
  2127. int hdrlen = min_t(int, copylen, tp_len);
  2128. skb_push(skb, dev->hard_header_len);
  2129. skb_put(skb, copylen - dev->hard_header_len);
  2130. err = skb_store_bits(skb, 0, data, hdrlen);
  2131. if (unlikely(err))
  2132. return err;
  2133. if (!dev_validate_header(dev, skb->data, hdrlen))
  2134. return -EINVAL;
  2135. if (!skb->protocol)
  2136. tpacket_set_protocol(dev, skb);
  2137. data += hdrlen;
  2138. to_write -= hdrlen;
  2139. }
  2140. offset = offset_in_page(data);
  2141. len_max = PAGE_SIZE - offset;
  2142. len = ((to_write > len_max) ? len_max : to_write);
  2143. skb->data_len = to_write;
  2144. skb->len += to_write;
  2145. skb->truesize += to_write;
  2146. atomic_add(to_write, &po->sk.sk_wmem_alloc);
  2147. while (likely(to_write)) {
  2148. nr_frags = skb_shinfo(skb)->nr_frags;
  2149. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  2150. pr_err("Packet exceed the number of skb frags(%lu)\n",
  2151. MAX_SKB_FRAGS);
  2152. return -EFAULT;
  2153. }
  2154. page = pgv_to_page(data);
  2155. data += len;
  2156. flush_dcache_page(page);
  2157. get_page(page);
  2158. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  2159. to_write -= len;
  2160. offset = 0;
  2161. len_max = PAGE_SIZE;
  2162. len = ((to_write > len_max) ? len_max : to_write);
  2163. }
  2164. skb_probe_transport_header(skb, 0);
  2165. return tp_len;
  2166. }
  2167. static int tpacket_parse_header(struct packet_sock *po, void *frame,
  2168. int size_max, void **data)
  2169. {
  2170. union tpacket_uhdr ph;
  2171. int tp_len, off;
  2172. ph.raw = frame;
  2173. switch (po->tp_version) {
  2174. case TPACKET_V2:
  2175. tp_len = ph.h2->tp_len;
  2176. break;
  2177. default:
  2178. tp_len = ph.h1->tp_len;
  2179. break;
  2180. }
  2181. if (unlikely(tp_len > size_max)) {
  2182. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  2183. return -EMSGSIZE;
  2184. }
  2185. if (unlikely(po->tp_tx_has_off)) {
  2186. int off_min, off_max;
  2187. off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2188. off_max = po->tx_ring.frame_size - tp_len;
  2189. if (po->sk.sk_type == SOCK_DGRAM) {
  2190. switch (po->tp_version) {
  2191. case TPACKET_V2:
  2192. off = ph.h2->tp_net;
  2193. break;
  2194. default:
  2195. off = ph.h1->tp_net;
  2196. break;
  2197. }
  2198. } else {
  2199. switch (po->tp_version) {
  2200. case TPACKET_V2:
  2201. off = ph.h2->tp_mac;
  2202. break;
  2203. default:
  2204. off = ph.h1->tp_mac;
  2205. break;
  2206. }
  2207. }
  2208. if (unlikely((off < off_min) || (off_max < off)))
  2209. return -EINVAL;
  2210. } else {
  2211. off = po->tp_hdrlen - sizeof(struct sockaddr_ll);
  2212. }
  2213. *data = frame + off;
  2214. return tp_len;
  2215. }
  2216. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  2217. {
  2218. struct sk_buff *skb;
  2219. struct net_device *dev;
  2220. struct virtio_net_hdr *vnet_hdr = NULL;
  2221. __be16 proto;
  2222. int err, reserve = 0;
  2223. void *ph;
  2224. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2225. bool need_wait = !(msg->msg_flags & MSG_DONTWAIT);
  2226. int tp_len, size_max;
  2227. unsigned char *addr;
  2228. void *data;
  2229. int len_sum = 0;
  2230. int status = TP_STATUS_AVAILABLE;
  2231. int hlen, tlen, copylen = 0;
  2232. mutex_lock(&po->pg_vec_lock);
  2233. if (likely(saddr == NULL)) {
  2234. dev = packet_cached_dev_get(po);
  2235. proto = po->num;
  2236. addr = NULL;
  2237. } else {
  2238. err = -EINVAL;
  2239. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2240. goto out;
  2241. if (msg->msg_namelen < (saddr->sll_halen
  2242. + offsetof(struct sockaddr_ll,
  2243. sll_addr)))
  2244. goto out;
  2245. proto = saddr->sll_protocol;
  2246. addr = saddr->sll_addr;
  2247. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  2248. }
  2249. err = -ENXIO;
  2250. if (unlikely(dev == NULL))
  2251. goto out;
  2252. err = -ENETDOWN;
  2253. if (unlikely(!(dev->flags & IFF_UP)))
  2254. goto out_put;
  2255. if (po->sk.sk_socket->type == SOCK_RAW)
  2256. reserve = dev->hard_header_len;
  2257. size_max = po->tx_ring.frame_size
  2258. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  2259. if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr)
  2260. size_max = dev->mtu + reserve + VLAN_HLEN;
  2261. do {
  2262. ph = packet_current_frame(po, &po->tx_ring,
  2263. TP_STATUS_SEND_REQUEST);
  2264. if (unlikely(ph == NULL)) {
  2265. if (need_wait && need_resched())
  2266. schedule();
  2267. continue;
  2268. }
  2269. skb = NULL;
  2270. tp_len = tpacket_parse_header(po, ph, size_max, &data);
  2271. if (tp_len < 0)
  2272. goto tpacket_error;
  2273. status = TP_STATUS_SEND_REQUEST;
  2274. hlen = LL_RESERVED_SPACE(dev);
  2275. tlen = dev->needed_tailroom;
  2276. if (po->has_vnet_hdr) {
  2277. vnet_hdr = data;
  2278. data += sizeof(*vnet_hdr);
  2279. tp_len -= sizeof(*vnet_hdr);
  2280. if (tp_len < 0 ||
  2281. __packet_snd_vnet_parse(vnet_hdr, tp_len)) {
  2282. tp_len = -EINVAL;
  2283. goto tpacket_error;
  2284. }
  2285. copylen = __virtio16_to_cpu(vio_le(),
  2286. vnet_hdr->hdr_len);
  2287. }
  2288. copylen = max_t(int, copylen, dev->hard_header_len);
  2289. skb = sock_alloc_send_skb(&po->sk,
  2290. hlen + tlen + sizeof(struct sockaddr_ll) +
  2291. (copylen - dev->hard_header_len),
  2292. !need_wait, &err);
  2293. if (unlikely(skb == NULL)) {
  2294. /* we assume the socket was initially writeable ... */
  2295. if (likely(len_sum > 0))
  2296. err = len_sum;
  2297. goto out_status;
  2298. }
  2299. tp_len = tpacket_fill_skb(po, skb, ph, dev, data, tp_len, proto,
  2300. addr, hlen, copylen);
  2301. if (likely(tp_len >= 0) &&
  2302. tp_len > dev->mtu + reserve &&
  2303. !po->has_vnet_hdr &&
  2304. !packet_extra_vlan_len_allowed(dev, skb))
  2305. tp_len = -EMSGSIZE;
  2306. if (unlikely(tp_len < 0)) {
  2307. tpacket_error:
  2308. if (po->tp_loss) {
  2309. __packet_set_status(po, ph,
  2310. TP_STATUS_AVAILABLE);
  2311. packet_increment_head(&po->tx_ring);
  2312. kfree_skb(skb);
  2313. continue;
  2314. } else {
  2315. status = TP_STATUS_WRONG_FORMAT;
  2316. err = tp_len;
  2317. goto out_status;
  2318. }
  2319. }
  2320. if (po->has_vnet_hdr && packet_snd_vnet_gso(skb, vnet_hdr)) {
  2321. tp_len = -EINVAL;
  2322. goto tpacket_error;
  2323. }
  2324. packet_pick_tx_queue(dev, skb);
  2325. skb->destructor = tpacket_destruct_skb;
  2326. __packet_set_status(po, ph, TP_STATUS_SENDING);
  2327. packet_inc_pending(&po->tx_ring);
  2328. status = TP_STATUS_SEND_REQUEST;
  2329. err = po->xmit(skb);
  2330. if (unlikely(err > 0)) {
  2331. err = net_xmit_errno(err);
  2332. if (err && __packet_get_status(po, ph) ==
  2333. TP_STATUS_AVAILABLE) {
  2334. /* skb was destructed already */
  2335. skb = NULL;
  2336. goto out_status;
  2337. }
  2338. /*
  2339. * skb was dropped but not destructed yet;
  2340. * let's treat it like congestion or err < 0
  2341. */
  2342. err = 0;
  2343. }
  2344. packet_increment_head(&po->tx_ring);
  2345. len_sum += tp_len;
  2346. } while (likely((ph != NULL) ||
  2347. /* Note: packet_read_pending() might be slow if we have
  2348. * to call it as it's per_cpu variable, but in fast-path
  2349. * we already short-circuit the loop with the first
  2350. * condition, and luckily don't have to go that path
  2351. * anyway.
  2352. */
  2353. (need_wait && packet_read_pending(&po->tx_ring))));
  2354. err = len_sum;
  2355. goto out_put;
  2356. out_status:
  2357. __packet_set_status(po, ph, status);
  2358. kfree_skb(skb);
  2359. out_put:
  2360. dev_put(dev);
  2361. out:
  2362. mutex_unlock(&po->pg_vec_lock);
  2363. return err;
  2364. }
  2365. static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  2366. size_t reserve, size_t len,
  2367. size_t linear, int noblock,
  2368. int *err)
  2369. {
  2370. struct sk_buff *skb;
  2371. /* Under a page? Don't bother with paged skb. */
  2372. if (prepad + len < PAGE_SIZE || !linear)
  2373. linear = len;
  2374. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  2375. err, 0);
  2376. if (!skb)
  2377. return NULL;
  2378. skb_reserve(skb, reserve);
  2379. skb_put(skb, linear);
  2380. skb->data_len = len - linear;
  2381. skb->len += len - linear;
  2382. return skb;
  2383. }
  2384. static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
  2385. {
  2386. struct sock *sk = sock->sk;
  2387. DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name);
  2388. struct sk_buff *skb;
  2389. struct net_device *dev;
  2390. __be16 proto;
  2391. unsigned char *addr;
  2392. int err, reserve = 0;
  2393. struct sockcm_cookie sockc;
  2394. struct virtio_net_hdr vnet_hdr = { 0 };
  2395. int offset = 0;
  2396. struct packet_sock *po = pkt_sk(sk);
  2397. int hlen, tlen;
  2398. int extra_len = 0;
  2399. /*
  2400. * Get and verify the address.
  2401. */
  2402. if (likely(saddr == NULL)) {
  2403. dev = packet_cached_dev_get(po);
  2404. proto = po->num;
  2405. addr = NULL;
  2406. } else {
  2407. err = -EINVAL;
  2408. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  2409. goto out;
  2410. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  2411. goto out;
  2412. proto = saddr->sll_protocol;
  2413. addr = saddr->sll_addr;
  2414. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  2415. }
  2416. err = -ENXIO;
  2417. if (unlikely(dev == NULL))
  2418. goto out_unlock;
  2419. err = -ENETDOWN;
  2420. if (unlikely(!(dev->flags & IFF_UP)))
  2421. goto out_unlock;
  2422. sockc.mark = sk->sk_mark;
  2423. if (msg->msg_controllen) {
  2424. err = sock_cmsg_send(sk, msg, &sockc);
  2425. if (unlikely(err))
  2426. goto out_unlock;
  2427. }
  2428. if (sock->type == SOCK_RAW)
  2429. reserve = dev->hard_header_len;
  2430. if (po->has_vnet_hdr) {
  2431. err = packet_snd_vnet_parse(msg, &len, &vnet_hdr);
  2432. if (err)
  2433. goto out_unlock;
  2434. }
  2435. if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
  2436. if (!netif_supports_nofcs(dev)) {
  2437. err = -EPROTONOSUPPORT;
  2438. goto out_unlock;
  2439. }
  2440. extra_len = 4; /* We're doing our own CRC */
  2441. }
  2442. err = -EMSGSIZE;
  2443. if (!vnet_hdr.gso_type &&
  2444. (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
  2445. goto out_unlock;
  2446. err = -ENOBUFS;
  2447. hlen = LL_RESERVED_SPACE(dev);
  2448. tlen = dev->needed_tailroom;
  2449. skb = packet_alloc_skb(sk, hlen + tlen, hlen, len,
  2450. __virtio16_to_cpu(vio_le(), vnet_hdr.hdr_len),
  2451. msg->msg_flags & MSG_DONTWAIT, &err);
  2452. if (skb == NULL)
  2453. goto out_unlock;
  2454. skb_set_network_header(skb, reserve);
  2455. err = -EINVAL;
  2456. if (sock->type == SOCK_DGRAM) {
  2457. offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  2458. if (unlikely(offset < 0))
  2459. goto out_free;
  2460. }
  2461. /* Returns -EFAULT on error */
  2462. err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len);
  2463. if (err)
  2464. goto out_free;
  2465. if (sock->type == SOCK_RAW &&
  2466. !dev_validate_header(dev, skb->data, len)) {
  2467. err = -EINVAL;
  2468. goto out_free;
  2469. }
  2470. sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  2471. if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) &&
  2472. !packet_extra_vlan_len_allowed(dev, skb)) {
  2473. err = -EMSGSIZE;
  2474. goto out_free;
  2475. }
  2476. skb->protocol = proto;
  2477. skb->dev = dev;
  2478. skb->priority = sk->sk_priority;
  2479. skb->mark = sockc.mark;
  2480. packet_pick_tx_queue(dev, skb);
  2481. if (po->has_vnet_hdr) {
  2482. err = packet_snd_vnet_gso(skb, &vnet_hdr);
  2483. if (err)
  2484. goto out_free;
  2485. len += sizeof(vnet_hdr);
  2486. }
  2487. skb_probe_transport_header(skb, reserve);
  2488. if (unlikely(extra_len == 4))
  2489. skb->no_fcs = 1;
  2490. err = po->xmit(skb);
  2491. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  2492. goto out_unlock;
  2493. dev_put(dev);
  2494. return len;
  2495. out_free:
  2496. kfree_skb(skb);
  2497. out_unlock:
  2498. if (dev)
  2499. dev_put(dev);
  2500. out:
  2501. return err;
  2502. }
  2503. static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  2504. {
  2505. struct sock *sk = sock->sk;
  2506. struct packet_sock *po = pkt_sk(sk);
  2507. if (po->tx_ring.pg_vec)
  2508. return tpacket_snd(po, msg);
  2509. else
  2510. return packet_snd(sock, msg, len);
  2511. }
  2512. /*
  2513. * Close a PACKET socket. This is fairly simple. We immediately go
  2514. * to 'closed' state and remove our protocol entry in the device list.
  2515. */
  2516. static int packet_release(struct socket *sock)
  2517. {
  2518. struct sock *sk = sock->sk;
  2519. struct packet_sock *po;
  2520. struct net *net;
  2521. union tpacket_req_u req_u;
  2522. if (!sk)
  2523. return 0;
  2524. net = sock_net(sk);
  2525. po = pkt_sk(sk);
  2526. mutex_lock(&net->packet.sklist_lock);
  2527. sk_del_node_init_rcu(sk);
  2528. mutex_unlock(&net->packet.sklist_lock);
  2529. preempt_disable();
  2530. sock_prot_inuse_add(net, sk->sk_prot, -1);
  2531. preempt_enable();
  2532. spin_lock(&po->bind_lock);
  2533. unregister_prot_hook(sk, false);
  2534. packet_cached_dev_reset(po);
  2535. if (po->prot_hook.dev) {
  2536. dev_put(po->prot_hook.dev);
  2537. po->prot_hook.dev = NULL;
  2538. }
  2539. spin_unlock(&po->bind_lock);
  2540. packet_flush_mclist(sk);
  2541. if (po->rx_ring.pg_vec) {
  2542. memset(&req_u, 0, sizeof(req_u));
  2543. packet_set_ring(sk, &req_u, 1, 0);
  2544. }
  2545. if (po->tx_ring.pg_vec) {
  2546. memset(&req_u, 0, sizeof(req_u));
  2547. packet_set_ring(sk, &req_u, 1, 1);
  2548. }
  2549. fanout_release(sk);
  2550. synchronize_net();
  2551. /*
  2552. * Now the socket is dead. No more input will appear.
  2553. */
  2554. sock_orphan(sk);
  2555. sock->sk = NULL;
  2556. /* Purge queues */
  2557. skb_queue_purge(&sk->sk_receive_queue);
  2558. packet_free_pending(po);
  2559. sk_refcnt_debug_release(sk);
  2560. sock_put(sk);
  2561. return 0;
  2562. }
  2563. /*
  2564. * Attach a packet hook.
  2565. */
  2566. static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
  2567. __be16 proto)
  2568. {
  2569. struct packet_sock *po = pkt_sk(sk);
  2570. struct net_device *dev_curr;
  2571. __be16 proto_curr;
  2572. bool need_rehook;
  2573. struct net_device *dev = NULL;
  2574. int ret = 0;
  2575. bool unlisted = false;
  2576. if (po->fanout)
  2577. return -EINVAL;
  2578. lock_sock(sk);
  2579. spin_lock(&po->bind_lock);
  2580. rcu_read_lock();
  2581. if (name) {
  2582. dev = dev_get_by_name_rcu(sock_net(sk), name);
  2583. if (!dev) {
  2584. ret = -ENODEV;
  2585. goto out_unlock;
  2586. }
  2587. } else if (ifindex) {
  2588. dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
  2589. if (!dev) {
  2590. ret = -ENODEV;
  2591. goto out_unlock;
  2592. }
  2593. }
  2594. if (dev)
  2595. dev_hold(dev);
  2596. proto_curr = po->prot_hook.type;
  2597. dev_curr = po->prot_hook.dev;
  2598. need_rehook = proto_curr != proto || dev_curr != dev;
  2599. if (need_rehook) {
  2600. if (po->running) {
  2601. rcu_read_unlock();
  2602. __unregister_prot_hook(sk, true);
  2603. rcu_read_lock();
  2604. dev_curr = po->prot_hook.dev;
  2605. if (dev)
  2606. unlisted = !dev_get_by_index_rcu(sock_net(sk),
  2607. dev->ifindex);
  2608. }
  2609. po->num = proto;
  2610. po->prot_hook.type = proto;
  2611. if (unlikely(unlisted)) {
  2612. dev_put(dev);
  2613. po->prot_hook.dev = NULL;
  2614. po->ifindex = -1;
  2615. packet_cached_dev_reset(po);
  2616. } else {
  2617. po->prot_hook.dev = dev;
  2618. po->ifindex = dev ? dev->ifindex : 0;
  2619. packet_cached_dev_assign(po, dev);
  2620. }
  2621. }
  2622. if (dev_curr)
  2623. dev_put(dev_curr);
  2624. if (proto == 0 || !need_rehook)
  2625. goto out_unlock;
  2626. if (!unlisted && (!dev || (dev->flags & IFF_UP))) {
  2627. register_prot_hook(sk);
  2628. } else {
  2629. sk->sk_err = ENETDOWN;
  2630. if (!sock_flag(sk, SOCK_DEAD))
  2631. sk->sk_error_report(sk);
  2632. }
  2633. out_unlock:
  2634. rcu_read_unlock();
  2635. spin_unlock(&po->bind_lock);
  2636. release_sock(sk);
  2637. return ret;
  2638. }
  2639. /*
  2640. * Bind a packet socket to a device
  2641. */
  2642. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  2643. int addr_len)
  2644. {
  2645. struct sock *sk = sock->sk;
  2646. char name[15];
  2647. /*
  2648. * Check legality
  2649. */
  2650. if (addr_len != sizeof(struct sockaddr))
  2651. return -EINVAL;
  2652. strlcpy(name, uaddr->sa_data, sizeof(name));
  2653. return packet_do_bind(sk, name, 0, pkt_sk(sk)->num);
  2654. }
  2655. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  2656. {
  2657. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  2658. struct sock *sk = sock->sk;
  2659. /*
  2660. * Check legality
  2661. */
  2662. if (addr_len < sizeof(struct sockaddr_ll))
  2663. return -EINVAL;
  2664. if (sll->sll_family != AF_PACKET)
  2665. return -EINVAL;
  2666. return packet_do_bind(sk, NULL, sll->sll_ifindex,
  2667. sll->sll_protocol ? : pkt_sk(sk)->num);
  2668. }
  2669. static struct proto packet_proto = {
  2670. .name = "PACKET",
  2671. .owner = THIS_MODULE,
  2672. .obj_size = sizeof(struct packet_sock),
  2673. };
  2674. /*
  2675. * Create a packet of type SOCK_PACKET.
  2676. */
  2677. static int packet_create(struct net *net, struct socket *sock, int protocol,
  2678. int kern)
  2679. {
  2680. struct sock *sk;
  2681. struct packet_sock *po;
  2682. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  2683. int err;
  2684. if (!ns_capable(net->user_ns, CAP_NET_RAW))
  2685. return -EPERM;
  2686. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  2687. sock->type != SOCK_PACKET)
  2688. return -ESOCKTNOSUPPORT;
  2689. sock->state = SS_UNCONNECTED;
  2690. err = -ENOBUFS;
  2691. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto, kern);
  2692. if (sk == NULL)
  2693. goto out;
  2694. sock->ops = &packet_ops;
  2695. if (sock->type == SOCK_PACKET)
  2696. sock->ops = &packet_ops_spkt;
  2697. sock_init_data(sock, sk);
  2698. po = pkt_sk(sk);
  2699. sk->sk_family = PF_PACKET;
  2700. po->num = proto;
  2701. po->xmit = dev_queue_xmit;
  2702. err = packet_alloc_pending(po);
  2703. if (err)
  2704. goto out2;
  2705. packet_cached_dev_reset(po);
  2706. sk->sk_destruct = packet_sock_destruct;
  2707. sk_refcnt_debug_inc(sk);
  2708. /*
  2709. * Attach a protocol block
  2710. */
  2711. spin_lock_init(&po->bind_lock);
  2712. mutex_init(&po->pg_vec_lock);
  2713. po->rollover = NULL;
  2714. po->prot_hook.func = packet_rcv;
  2715. if (sock->type == SOCK_PACKET)
  2716. po->prot_hook.func = packet_rcv_spkt;
  2717. po->prot_hook.af_packet_priv = sk;
  2718. if (proto) {
  2719. po->prot_hook.type = proto;
  2720. register_prot_hook(sk);
  2721. }
  2722. mutex_lock(&net->packet.sklist_lock);
  2723. sk_add_node_rcu(sk, &net->packet.sklist);
  2724. mutex_unlock(&net->packet.sklist_lock);
  2725. preempt_disable();
  2726. sock_prot_inuse_add(net, &packet_proto, 1);
  2727. preempt_enable();
  2728. return 0;
  2729. out2:
  2730. sk_free(sk);
  2731. out:
  2732. return err;
  2733. }
  2734. /*
  2735. * Pull a packet from our receive queue and hand it to the user.
  2736. * If necessary we block.
  2737. */
  2738. static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  2739. int flags)
  2740. {
  2741. struct sock *sk = sock->sk;
  2742. struct sk_buff *skb;
  2743. int copied, err;
  2744. int vnet_hdr_len = 0;
  2745. unsigned int origlen = 0;
  2746. err = -EINVAL;
  2747. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  2748. goto out;
  2749. #if 0
  2750. /* What error should we return now? EUNATTACH? */
  2751. if (pkt_sk(sk)->ifindex < 0)
  2752. return -ENODEV;
  2753. #endif
  2754. if (flags & MSG_ERRQUEUE) {
  2755. err = sock_recv_errqueue(sk, msg, len,
  2756. SOL_PACKET, PACKET_TX_TIMESTAMP);
  2757. goto out;
  2758. }
  2759. /*
  2760. * Call the generic datagram receiver. This handles all sorts
  2761. * of horrible races and re-entrancy so we can forget about it
  2762. * in the protocol layers.
  2763. *
  2764. * Now it will return ENETDOWN, if device have just gone down,
  2765. * but then it will block.
  2766. */
  2767. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  2768. /*
  2769. * An error occurred so return it. Because skb_recv_datagram()
  2770. * handles the blocking we don't see and worry about blocking
  2771. * retries.
  2772. */
  2773. if (skb == NULL)
  2774. goto out;
  2775. if (pkt_sk(sk)->pressure)
  2776. packet_rcv_has_room(pkt_sk(sk), NULL);
  2777. if (pkt_sk(sk)->has_vnet_hdr) {
  2778. err = packet_rcv_vnet(msg, skb, &len);
  2779. if (err)
  2780. goto out_free;
  2781. vnet_hdr_len = sizeof(struct virtio_net_hdr);
  2782. }
  2783. /* You lose any data beyond the buffer you gave. If it worries
  2784. * a user program they can ask the device for its MTU
  2785. * anyway.
  2786. */
  2787. copied = skb->len;
  2788. if (copied > len) {
  2789. copied = len;
  2790. msg->msg_flags |= MSG_TRUNC;
  2791. }
  2792. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  2793. if (err)
  2794. goto out_free;
  2795. if (sock->type != SOCK_PACKET) {
  2796. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2797. /* Original length was stored in sockaddr_ll fields */
  2798. origlen = PACKET_SKB_CB(skb)->sa.origlen;
  2799. sll->sll_family = AF_PACKET;
  2800. sll->sll_protocol = skb->protocol;
  2801. }
  2802. sock_recv_ts_and_drops(msg, sk, skb);
  2803. if (msg->msg_name) {
  2804. /* If the address length field is there to be filled
  2805. * in, we fill it in now.
  2806. */
  2807. if (sock->type == SOCK_PACKET) {
  2808. __sockaddr_check_size(sizeof(struct sockaddr_pkt));
  2809. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  2810. } else {
  2811. struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
  2812. msg->msg_namelen = sll->sll_halen +
  2813. offsetof(struct sockaddr_ll, sll_addr);
  2814. }
  2815. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
  2816. msg->msg_namelen);
  2817. }
  2818. if (pkt_sk(sk)->auxdata) {
  2819. struct tpacket_auxdata aux;
  2820. aux.tp_status = TP_STATUS_USER;
  2821. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2822. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  2823. else if (skb->pkt_type != PACKET_OUTGOING &&
  2824. (skb->ip_summed == CHECKSUM_COMPLETE ||
  2825. skb_csum_unnecessary(skb)))
  2826. aux.tp_status |= TP_STATUS_CSUM_VALID;
  2827. aux.tp_len = origlen;
  2828. aux.tp_snaplen = skb->len;
  2829. aux.tp_mac = 0;
  2830. aux.tp_net = skb_network_offset(skb);
  2831. if (skb_vlan_tag_present(skb)) {
  2832. aux.tp_vlan_tci = skb_vlan_tag_get(skb);
  2833. aux.tp_vlan_tpid = ntohs(skb->vlan_proto);
  2834. aux.tp_status |= TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID;
  2835. } else {
  2836. aux.tp_vlan_tci = 0;
  2837. aux.tp_vlan_tpid = 0;
  2838. }
  2839. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  2840. }
  2841. /*
  2842. * Free or return the buffer as appropriate. Again this
  2843. * hides all the races and re-entrancy issues from us.
  2844. */
  2845. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  2846. out_free:
  2847. skb_free_datagram(sk, skb);
  2848. out:
  2849. return err;
  2850. }
  2851. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  2852. int *uaddr_len, int peer)
  2853. {
  2854. struct net_device *dev;
  2855. struct sock *sk = sock->sk;
  2856. if (peer)
  2857. return -EOPNOTSUPP;
  2858. uaddr->sa_family = AF_PACKET;
  2859. memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
  2860. rcu_read_lock();
  2861. dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
  2862. if (dev)
  2863. strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
  2864. rcu_read_unlock();
  2865. *uaddr_len = sizeof(*uaddr);
  2866. return 0;
  2867. }
  2868. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  2869. int *uaddr_len, int peer)
  2870. {
  2871. struct net_device *dev;
  2872. struct sock *sk = sock->sk;
  2873. struct packet_sock *po = pkt_sk(sk);
  2874. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  2875. if (peer)
  2876. return -EOPNOTSUPP;
  2877. sll->sll_family = AF_PACKET;
  2878. sll->sll_ifindex = po->ifindex;
  2879. sll->sll_protocol = po->num;
  2880. sll->sll_pkttype = 0;
  2881. rcu_read_lock();
  2882. dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
  2883. if (dev) {
  2884. sll->sll_hatype = dev->type;
  2885. sll->sll_halen = dev->addr_len;
  2886. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  2887. } else {
  2888. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  2889. sll->sll_halen = 0;
  2890. }
  2891. rcu_read_unlock();
  2892. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  2893. return 0;
  2894. }
  2895. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  2896. int what)
  2897. {
  2898. switch (i->type) {
  2899. case PACKET_MR_MULTICAST:
  2900. if (i->alen != dev->addr_len)
  2901. return -EINVAL;
  2902. if (what > 0)
  2903. return dev_mc_add(dev, i->addr);
  2904. else
  2905. return dev_mc_del(dev, i->addr);
  2906. break;
  2907. case PACKET_MR_PROMISC:
  2908. return dev_set_promiscuity(dev, what);
  2909. case PACKET_MR_ALLMULTI:
  2910. return dev_set_allmulti(dev, what);
  2911. case PACKET_MR_UNICAST:
  2912. if (i->alen != dev->addr_len)
  2913. return -EINVAL;
  2914. if (what > 0)
  2915. return dev_uc_add(dev, i->addr);
  2916. else
  2917. return dev_uc_del(dev, i->addr);
  2918. break;
  2919. default:
  2920. break;
  2921. }
  2922. return 0;
  2923. }
  2924. static void packet_dev_mclist_delete(struct net_device *dev,
  2925. struct packet_mclist **mlp)
  2926. {
  2927. struct packet_mclist *ml;
  2928. while ((ml = *mlp) != NULL) {
  2929. if (ml->ifindex == dev->ifindex) {
  2930. packet_dev_mc(dev, ml, -1);
  2931. *mlp = ml->next;
  2932. kfree(ml);
  2933. } else
  2934. mlp = &ml->next;
  2935. }
  2936. }
  2937. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  2938. {
  2939. struct packet_sock *po = pkt_sk(sk);
  2940. struct packet_mclist *ml, *i;
  2941. struct net_device *dev;
  2942. int err;
  2943. rtnl_lock();
  2944. err = -ENODEV;
  2945. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  2946. if (!dev)
  2947. goto done;
  2948. err = -EINVAL;
  2949. if (mreq->mr_alen > dev->addr_len)
  2950. goto done;
  2951. err = -ENOBUFS;
  2952. i = kmalloc(sizeof(*i), GFP_KERNEL);
  2953. if (i == NULL)
  2954. goto done;
  2955. err = 0;
  2956. for (ml = po->mclist; ml; ml = ml->next) {
  2957. if (ml->ifindex == mreq->mr_ifindex &&
  2958. ml->type == mreq->mr_type &&
  2959. ml->alen == mreq->mr_alen &&
  2960. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  2961. ml->count++;
  2962. /* Free the new element ... */
  2963. kfree(i);
  2964. goto done;
  2965. }
  2966. }
  2967. i->type = mreq->mr_type;
  2968. i->ifindex = mreq->mr_ifindex;
  2969. i->alen = mreq->mr_alen;
  2970. memcpy(i->addr, mreq->mr_address, i->alen);
  2971. i->count = 1;
  2972. i->next = po->mclist;
  2973. po->mclist = i;
  2974. err = packet_dev_mc(dev, i, 1);
  2975. if (err) {
  2976. po->mclist = i->next;
  2977. kfree(i);
  2978. }
  2979. done:
  2980. rtnl_unlock();
  2981. return err;
  2982. }
  2983. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  2984. {
  2985. struct packet_mclist *ml, **mlp;
  2986. rtnl_lock();
  2987. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  2988. if (ml->ifindex == mreq->mr_ifindex &&
  2989. ml->type == mreq->mr_type &&
  2990. ml->alen == mreq->mr_alen &&
  2991. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  2992. if (--ml->count == 0) {
  2993. struct net_device *dev;
  2994. *mlp = ml->next;
  2995. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  2996. if (dev)
  2997. packet_dev_mc(dev, ml, -1);
  2998. kfree(ml);
  2999. }
  3000. break;
  3001. }
  3002. }
  3003. rtnl_unlock();
  3004. return 0;
  3005. }
  3006. static void packet_flush_mclist(struct sock *sk)
  3007. {
  3008. struct packet_sock *po = pkt_sk(sk);
  3009. struct packet_mclist *ml;
  3010. if (!po->mclist)
  3011. return;
  3012. rtnl_lock();
  3013. while ((ml = po->mclist) != NULL) {
  3014. struct net_device *dev;
  3015. po->mclist = ml->next;
  3016. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  3017. if (dev != NULL)
  3018. packet_dev_mc(dev, ml, -1);
  3019. kfree(ml);
  3020. }
  3021. rtnl_unlock();
  3022. }
  3023. static int
  3024. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  3025. {
  3026. struct sock *sk = sock->sk;
  3027. struct packet_sock *po = pkt_sk(sk);
  3028. int ret;
  3029. if (level != SOL_PACKET)
  3030. return -ENOPROTOOPT;
  3031. switch (optname) {
  3032. case PACKET_ADD_MEMBERSHIP:
  3033. case PACKET_DROP_MEMBERSHIP:
  3034. {
  3035. struct packet_mreq_max mreq;
  3036. int len = optlen;
  3037. memset(&mreq, 0, sizeof(mreq));
  3038. if (len < sizeof(struct packet_mreq))
  3039. return -EINVAL;
  3040. if (len > sizeof(mreq))
  3041. len = sizeof(mreq);
  3042. if (copy_from_user(&mreq, optval, len))
  3043. return -EFAULT;
  3044. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  3045. return -EINVAL;
  3046. if (optname == PACKET_ADD_MEMBERSHIP)
  3047. ret = packet_mc_add(sk, &mreq);
  3048. else
  3049. ret = packet_mc_drop(sk, &mreq);
  3050. return ret;
  3051. }
  3052. case PACKET_RX_RING:
  3053. case PACKET_TX_RING:
  3054. {
  3055. union tpacket_req_u req_u;
  3056. int len;
  3057. switch (po->tp_version) {
  3058. case TPACKET_V1:
  3059. case TPACKET_V2:
  3060. len = sizeof(req_u.req);
  3061. break;
  3062. case TPACKET_V3:
  3063. default:
  3064. len = sizeof(req_u.req3);
  3065. break;
  3066. }
  3067. if (optlen < len)
  3068. return -EINVAL;
  3069. if (copy_from_user(&req_u.req, optval, len))
  3070. return -EFAULT;
  3071. return packet_set_ring(sk, &req_u, 0,
  3072. optname == PACKET_TX_RING);
  3073. }
  3074. case PACKET_COPY_THRESH:
  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. pkt_sk(sk)->copy_thresh = val;
  3082. return 0;
  3083. }
  3084. case PACKET_VERSION:
  3085. {
  3086. int val;
  3087. if (optlen != sizeof(val))
  3088. return -EINVAL;
  3089. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  3090. return -EBUSY;
  3091. if (copy_from_user(&val, optval, sizeof(val)))
  3092. return -EFAULT;
  3093. switch (val) {
  3094. case TPACKET_V1:
  3095. case TPACKET_V2:
  3096. case TPACKET_V3:
  3097. po->tp_version = val;
  3098. return 0;
  3099. default:
  3100. return -EINVAL;
  3101. }
  3102. }
  3103. case PACKET_RESERVE:
  3104. {
  3105. unsigned int val;
  3106. if (optlen != sizeof(val))
  3107. return -EINVAL;
  3108. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  3109. return -EBUSY;
  3110. if (copy_from_user(&val, optval, sizeof(val)))
  3111. return -EFAULT;
  3112. po->tp_reserve = val;
  3113. return 0;
  3114. }
  3115. case PACKET_LOSS:
  3116. {
  3117. unsigned int val;
  3118. if (optlen != sizeof(val))
  3119. return -EINVAL;
  3120. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  3121. return -EBUSY;
  3122. if (copy_from_user(&val, optval, sizeof(val)))
  3123. return -EFAULT;
  3124. po->tp_loss = !!val;
  3125. return 0;
  3126. }
  3127. case PACKET_AUXDATA:
  3128. {
  3129. int val;
  3130. if (optlen < sizeof(val))
  3131. return -EINVAL;
  3132. if (copy_from_user(&val, optval, sizeof(val)))
  3133. return -EFAULT;
  3134. po->auxdata = !!val;
  3135. return 0;
  3136. }
  3137. case PACKET_ORIGDEV:
  3138. {
  3139. int val;
  3140. if (optlen < sizeof(val))
  3141. return -EINVAL;
  3142. if (copy_from_user(&val, optval, sizeof(val)))
  3143. return -EFAULT;
  3144. po->origdev = !!val;
  3145. return 0;
  3146. }
  3147. case PACKET_VNET_HDR:
  3148. {
  3149. int val;
  3150. if (sock->type != SOCK_RAW)
  3151. return -EINVAL;
  3152. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  3153. return -EBUSY;
  3154. if (optlen < sizeof(val))
  3155. return -EINVAL;
  3156. if (copy_from_user(&val, optval, sizeof(val)))
  3157. return -EFAULT;
  3158. po->has_vnet_hdr = !!val;
  3159. return 0;
  3160. }
  3161. case PACKET_TIMESTAMP:
  3162. {
  3163. int val;
  3164. if (optlen != sizeof(val))
  3165. return -EINVAL;
  3166. if (copy_from_user(&val, optval, sizeof(val)))
  3167. return -EFAULT;
  3168. po->tp_tstamp = val;
  3169. return 0;
  3170. }
  3171. case PACKET_FANOUT:
  3172. {
  3173. int val;
  3174. if (optlen != sizeof(val))
  3175. return -EINVAL;
  3176. if (copy_from_user(&val, optval, sizeof(val)))
  3177. return -EFAULT;
  3178. return fanout_add(sk, val & 0xffff, val >> 16);
  3179. }
  3180. case PACKET_FANOUT_DATA:
  3181. {
  3182. if (!po->fanout)
  3183. return -EINVAL;
  3184. return fanout_set_data(po, optval, optlen);
  3185. }
  3186. case PACKET_TX_HAS_OFF:
  3187. {
  3188. unsigned int val;
  3189. if (optlen != sizeof(val))
  3190. return -EINVAL;
  3191. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  3192. return -EBUSY;
  3193. if (copy_from_user(&val, optval, sizeof(val)))
  3194. return -EFAULT;
  3195. po->tp_tx_has_off = !!val;
  3196. return 0;
  3197. }
  3198. case PACKET_QDISC_BYPASS:
  3199. {
  3200. int val;
  3201. if (optlen != sizeof(val))
  3202. return -EINVAL;
  3203. if (copy_from_user(&val, optval, sizeof(val)))
  3204. return -EFAULT;
  3205. po->xmit = val ? packet_direct_xmit : dev_queue_xmit;
  3206. return 0;
  3207. }
  3208. default:
  3209. return -ENOPROTOOPT;
  3210. }
  3211. }
  3212. static int packet_getsockopt(struct socket *sock, int level, int optname,
  3213. char __user *optval, int __user *optlen)
  3214. {
  3215. int len;
  3216. int val, lv = sizeof(val);
  3217. struct sock *sk = sock->sk;
  3218. struct packet_sock *po = pkt_sk(sk);
  3219. void *data = &val;
  3220. union tpacket_stats_u st;
  3221. struct tpacket_rollover_stats rstats;
  3222. if (level != SOL_PACKET)
  3223. return -ENOPROTOOPT;
  3224. if (get_user(len, optlen))
  3225. return -EFAULT;
  3226. if (len < 0)
  3227. return -EINVAL;
  3228. switch (optname) {
  3229. case PACKET_STATISTICS:
  3230. spin_lock_bh(&sk->sk_receive_queue.lock);
  3231. memcpy(&st, &po->stats, sizeof(st));
  3232. memset(&po->stats, 0, sizeof(po->stats));
  3233. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3234. if (po->tp_version == TPACKET_V3) {
  3235. lv = sizeof(struct tpacket_stats_v3);
  3236. st.stats3.tp_packets += st.stats3.tp_drops;
  3237. data = &st.stats3;
  3238. } else {
  3239. lv = sizeof(struct tpacket_stats);
  3240. st.stats1.tp_packets += st.stats1.tp_drops;
  3241. data = &st.stats1;
  3242. }
  3243. break;
  3244. case PACKET_AUXDATA:
  3245. val = po->auxdata;
  3246. break;
  3247. case PACKET_ORIGDEV:
  3248. val = po->origdev;
  3249. break;
  3250. case PACKET_VNET_HDR:
  3251. val = po->has_vnet_hdr;
  3252. break;
  3253. case PACKET_VERSION:
  3254. val = po->tp_version;
  3255. break;
  3256. case PACKET_HDRLEN:
  3257. if (len > sizeof(int))
  3258. len = sizeof(int);
  3259. if (copy_from_user(&val, optval, len))
  3260. return -EFAULT;
  3261. switch (val) {
  3262. case TPACKET_V1:
  3263. val = sizeof(struct tpacket_hdr);
  3264. break;
  3265. case TPACKET_V2:
  3266. val = sizeof(struct tpacket2_hdr);
  3267. break;
  3268. case TPACKET_V3:
  3269. val = sizeof(struct tpacket3_hdr);
  3270. break;
  3271. default:
  3272. return -EINVAL;
  3273. }
  3274. break;
  3275. case PACKET_RESERVE:
  3276. val = po->tp_reserve;
  3277. break;
  3278. case PACKET_LOSS:
  3279. val = po->tp_loss;
  3280. break;
  3281. case PACKET_TIMESTAMP:
  3282. val = po->tp_tstamp;
  3283. break;
  3284. case PACKET_FANOUT:
  3285. val = (po->fanout ?
  3286. ((u32)po->fanout->id |
  3287. ((u32)po->fanout->type << 16) |
  3288. ((u32)po->fanout->flags << 24)) :
  3289. 0);
  3290. break;
  3291. case PACKET_ROLLOVER_STATS:
  3292. if (!po->rollover)
  3293. return -EINVAL;
  3294. rstats.tp_all = atomic_long_read(&po->rollover->num);
  3295. rstats.tp_huge = atomic_long_read(&po->rollover->num_huge);
  3296. rstats.tp_failed = atomic_long_read(&po->rollover->num_failed);
  3297. data = &rstats;
  3298. lv = sizeof(rstats);
  3299. break;
  3300. case PACKET_TX_HAS_OFF:
  3301. val = po->tp_tx_has_off;
  3302. break;
  3303. case PACKET_QDISC_BYPASS:
  3304. val = packet_use_direct_xmit(po);
  3305. break;
  3306. default:
  3307. return -ENOPROTOOPT;
  3308. }
  3309. if (len > lv)
  3310. len = lv;
  3311. if (put_user(len, optlen))
  3312. return -EFAULT;
  3313. if (copy_to_user(optval, data, len))
  3314. return -EFAULT;
  3315. return 0;
  3316. }
  3317. static int packet_notifier(struct notifier_block *this,
  3318. unsigned long msg, void *ptr)
  3319. {
  3320. struct sock *sk;
  3321. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3322. struct net *net = dev_net(dev);
  3323. rcu_read_lock();
  3324. sk_for_each_rcu(sk, &net->packet.sklist) {
  3325. struct packet_sock *po = pkt_sk(sk);
  3326. switch (msg) {
  3327. case NETDEV_UNREGISTER:
  3328. if (po->mclist)
  3329. packet_dev_mclist_delete(dev, &po->mclist);
  3330. /* fallthrough */
  3331. case NETDEV_DOWN:
  3332. if (dev->ifindex == po->ifindex) {
  3333. spin_lock(&po->bind_lock);
  3334. if (po->running) {
  3335. __unregister_prot_hook(sk, false);
  3336. sk->sk_err = ENETDOWN;
  3337. if (!sock_flag(sk, SOCK_DEAD))
  3338. sk->sk_error_report(sk);
  3339. }
  3340. if (msg == NETDEV_UNREGISTER) {
  3341. packet_cached_dev_reset(po);
  3342. po->ifindex = -1;
  3343. if (po->prot_hook.dev)
  3344. dev_put(po->prot_hook.dev);
  3345. po->prot_hook.dev = NULL;
  3346. }
  3347. spin_unlock(&po->bind_lock);
  3348. }
  3349. break;
  3350. case NETDEV_UP:
  3351. if (dev->ifindex == po->ifindex) {
  3352. spin_lock(&po->bind_lock);
  3353. if (po->num)
  3354. register_prot_hook(sk);
  3355. spin_unlock(&po->bind_lock);
  3356. }
  3357. break;
  3358. }
  3359. }
  3360. rcu_read_unlock();
  3361. return NOTIFY_DONE;
  3362. }
  3363. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  3364. unsigned long arg)
  3365. {
  3366. struct sock *sk = sock->sk;
  3367. switch (cmd) {
  3368. case SIOCOUTQ:
  3369. {
  3370. int amount = sk_wmem_alloc_get(sk);
  3371. return put_user(amount, (int __user *)arg);
  3372. }
  3373. case SIOCINQ:
  3374. {
  3375. struct sk_buff *skb;
  3376. int amount = 0;
  3377. spin_lock_bh(&sk->sk_receive_queue.lock);
  3378. skb = skb_peek(&sk->sk_receive_queue);
  3379. if (skb)
  3380. amount = skb->len;
  3381. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3382. return put_user(amount, (int __user *)arg);
  3383. }
  3384. case SIOCGSTAMP:
  3385. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  3386. case SIOCGSTAMPNS:
  3387. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  3388. #ifdef CONFIG_INET
  3389. case SIOCADDRT:
  3390. case SIOCDELRT:
  3391. case SIOCDARP:
  3392. case SIOCGARP:
  3393. case SIOCSARP:
  3394. case SIOCGIFADDR:
  3395. case SIOCSIFADDR:
  3396. case SIOCGIFBRDADDR:
  3397. case SIOCSIFBRDADDR:
  3398. case SIOCGIFNETMASK:
  3399. case SIOCSIFNETMASK:
  3400. case SIOCGIFDSTADDR:
  3401. case SIOCSIFDSTADDR:
  3402. case SIOCSIFFLAGS:
  3403. return inet_dgram_ops.ioctl(sock, cmd, arg);
  3404. #endif
  3405. default:
  3406. return -ENOIOCTLCMD;
  3407. }
  3408. return 0;
  3409. }
  3410. static unsigned int packet_poll(struct file *file, struct socket *sock,
  3411. poll_table *wait)
  3412. {
  3413. struct sock *sk = sock->sk;
  3414. struct packet_sock *po = pkt_sk(sk);
  3415. unsigned int mask = datagram_poll(file, sock, wait);
  3416. spin_lock_bh(&sk->sk_receive_queue.lock);
  3417. if (po->rx_ring.pg_vec) {
  3418. if (!packet_previous_rx_frame(po, &po->rx_ring,
  3419. TP_STATUS_KERNEL))
  3420. mask |= POLLIN | POLLRDNORM;
  3421. }
  3422. if (po->pressure && __packet_rcv_has_room(po, NULL) == ROOM_NORMAL)
  3423. po->pressure = 0;
  3424. spin_unlock_bh(&sk->sk_receive_queue.lock);
  3425. spin_lock_bh(&sk->sk_write_queue.lock);
  3426. if (po->tx_ring.pg_vec) {
  3427. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  3428. mask |= POLLOUT | POLLWRNORM;
  3429. }
  3430. spin_unlock_bh(&sk->sk_write_queue.lock);
  3431. return mask;
  3432. }
  3433. /* Dirty? Well, I still did not learn better way to account
  3434. * for user mmaps.
  3435. */
  3436. static void packet_mm_open(struct vm_area_struct *vma)
  3437. {
  3438. struct file *file = vma->vm_file;
  3439. struct socket *sock = file->private_data;
  3440. struct sock *sk = sock->sk;
  3441. if (sk)
  3442. atomic_inc(&pkt_sk(sk)->mapped);
  3443. }
  3444. static void packet_mm_close(struct vm_area_struct *vma)
  3445. {
  3446. struct file *file = vma->vm_file;
  3447. struct socket *sock = file->private_data;
  3448. struct sock *sk = sock->sk;
  3449. if (sk)
  3450. atomic_dec(&pkt_sk(sk)->mapped);
  3451. }
  3452. static const struct vm_operations_struct packet_mmap_ops = {
  3453. .open = packet_mm_open,
  3454. .close = packet_mm_close,
  3455. };
  3456. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  3457. unsigned int len)
  3458. {
  3459. int i;
  3460. for (i = 0; i < len; i++) {
  3461. if (likely(pg_vec[i].buffer)) {
  3462. if (is_vmalloc_addr(pg_vec[i].buffer))
  3463. vfree(pg_vec[i].buffer);
  3464. else
  3465. free_pages((unsigned long)pg_vec[i].buffer,
  3466. order);
  3467. pg_vec[i].buffer = NULL;
  3468. }
  3469. }
  3470. kfree(pg_vec);
  3471. }
  3472. static char *alloc_one_pg_vec_page(unsigned long order)
  3473. {
  3474. char *buffer;
  3475. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  3476. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  3477. buffer = (char *) __get_free_pages(gfp_flags, order);
  3478. if (buffer)
  3479. return buffer;
  3480. /* __get_free_pages failed, fall back to vmalloc */
  3481. buffer = vzalloc((1 << order) * PAGE_SIZE);
  3482. if (buffer)
  3483. return buffer;
  3484. /* vmalloc failed, lets dig into swap here */
  3485. gfp_flags &= ~__GFP_NORETRY;
  3486. buffer = (char *) __get_free_pages(gfp_flags, order);
  3487. if (buffer)
  3488. return buffer;
  3489. /* complete and utter failure */
  3490. return NULL;
  3491. }
  3492. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  3493. {
  3494. unsigned int block_nr = req->tp_block_nr;
  3495. struct pgv *pg_vec;
  3496. int i;
  3497. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  3498. if (unlikely(!pg_vec))
  3499. goto out;
  3500. for (i = 0; i < block_nr; i++) {
  3501. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  3502. if (unlikely(!pg_vec[i].buffer))
  3503. goto out_free_pgvec;
  3504. }
  3505. out:
  3506. return pg_vec;
  3507. out_free_pgvec:
  3508. free_pg_vec(pg_vec, order, block_nr);
  3509. pg_vec = NULL;
  3510. goto out;
  3511. }
  3512. static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
  3513. int closing, int tx_ring)
  3514. {
  3515. struct pgv *pg_vec = NULL;
  3516. struct packet_sock *po = pkt_sk(sk);
  3517. int was_running, order = 0;
  3518. struct packet_ring_buffer *rb;
  3519. struct sk_buff_head *rb_queue;
  3520. __be16 num;
  3521. int err = -EINVAL;
  3522. /* Added to avoid minimal code churn */
  3523. struct tpacket_req *req = &req_u->req;
  3524. /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
  3525. if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
  3526. WARN(1, "Tx-ring is not supported.\n");
  3527. goto out;
  3528. }
  3529. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  3530. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  3531. err = -EBUSY;
  3532. if (!closing) {
  3533. if (atomic_read(&po->mapped))
  3534. goto out;
  3535. if (packet_read_pending(rb))
  3536. goto out;
  3537. }
  3538. if (req->tp_block_nr) {
  3539. /* Sanity tests and some calculations */
  3540. err = -EBUSY;
  3541. if (unlikely(rb->pg_vec))
  3542. goto out;
  3543. switch (po->tp_version) {
  3544. case TPACKET_V1:
  3545. po->tp_hdrlen = TPACKET_HDRLEN;
  3546. break;
  3547. case TPACKET_V2:
  3548. po->tp_hdrlen = TPACKET2_HDRLEN;
  3549. break;
  3550. case TPACKET_V3:
  3551. po->tp_hdrlen = TPACKET3_HDRLEN;
  3552. break;
  3553. }
  3554. err = -EINVAL;
  3555. if (unlikely((int)req->tp_block_size <= 0))
  3556. goto out;
  3557. if (unlikely(!PAGE_ALIGNED(req->tp_block_size)))
  3558. goto out;
  3559. if (po->tp_version >= TPACKET_V3 &&
  3560. (int)(req->tp_block_size -
  3561. BLK_PLUS_PRIV(req_u->req3.tp_sizeof_priv)) <= 0)
  3562. goto out;
  3563. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  3564. po->tp_reserve))
  3565. goto out;
  3566. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  3567. goto out;
  3568. rb->frames_per_block = req->tp_block_size / req->tp_frame_size;
  3569. if (unlikely(rb->frames_per_block == 0))
  3570. goto out;
  3571. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  3572. req->tp_frame_nr))
  3573. goto out;
  3574. err = -ENOMEM;
  3575. order = get_order(req->tp_block_size);
  3576. pg_vec = alloc_pg_vec(req, order);
  3577. if (unlikely(!pg_vec))
  3578. goto out;
  3579. switch (po->tp_version) {
  3580. case TPACKET_V3:
  3581. /* Transmit path is not supported. We checked
  3582. * it above but just being paranoid
  3583. */
  3584. if (!tx_ring)
  3585. init_prb_bdqc(po, rb, pg_vec, req_u);
  3586. break;
  3587. default:
  3588. break;
  3589. }
  3590. }
  3591. /* Done */
  3592. else {
  3593. err = -EINVAL;
  3594. if (unlikely(req->tp_frame_nr))
  3595. goto out;
  3596. }
  3597. lock_sock(sk);
  3598. /* Detach socket from network */
  3599. spin_lock(&po->bind_lock);
  3600. was_running = po->running;
  3601. num = po->num;
  3602. if (was_running) {
  3603. po->num = 0;
  3604. __unregister_prot_hook(sk, false);
  3605. }
  3606. spin_unlock(&po->bind_lock);
  3607. synchronize_net();
  3608. err = -EBUSY;
  3609. mutex_lock(&po->pg_vec_lock);
  3610. if (closing || atomic_read(&po->mapped) == 0) {
  3611. err = 0;
  3612. spin_lock_bh(&rb_queue->lock);
  3613. swap(rb->pg_vec, pg_vec);
  3614. rb->frame_max = (req->tp_frame_nr - 1);
  3615. rb->head = 0;
  3616. rb->frame_size = req->tp_frame_size;
  3617. spin_unlock_bh(&rb_queue->lock);
  3618. swap(rb->pg_vec_order, order);
  3619. swap(rb->pg_vec_len, req->tp_block_nr);
  3620. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  3621. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  3622. tpacket_rcv : packet_rcv;
  3623. skb_queue_purge(rb_queue);
  3624. if (atomic_read(&po->mapped))
  3625. pr_err("packet_mmap: vma is busy: %d\n",
  3626. atomic_read(&po->mapped));
  3627. }
  3628. mutex_unlock(&po->pg_vec_lock);
  3629. spin_lock(&po->bind_lock);
  3630. if (was_running) {
  3631. po->num = num;
  3632. register_prot_hook(sk);
  3633. }
  3634. spin_unlock(&po->bind_lock);
  3635. if (closing && (po->tp_version > TPACKET_V2)) {
  3636. /* Because we don't support block-based V3 on tx-ring */
  3637. if (!tx_ring)
  3638. prb_shutdown_retire_blk_timer(po, rb_queue);
  3639. }
  3640. release_sock(sk);
  3641. if (pg_vec)
  3642. free_pg_vec(pg_vec, order, req->tp_block_nr);
  3643. out:
  3644. return err;
  3645. }
  3646. static int packet_mmap(struct file *file, struct socket *sock,
  3647. struct vm_area_struct *vma)
  3648. {
  3649. struct sock *sk = sock->sk;
  3650. struct packet_sock *po = pkt_sk(sk);
  3651. unsigned long size, expected_size;
  3652. struct packet_ring_buffer *rb;
  3653. unsigned long start;
  3654. int err = -EINVAL;
  3655. int i;
  3656. if (vma->vm_pgoff)
  3657. return -EINVAL;
  3658. mutex_lock(&po->pg_vec_lock);
  3659. expected_size = 0;
  3660. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3661. if (rb->pg_vec) {
  3662. expected_size += rb->pg_vec_len
  3663. * rb->pg_vec_pages
  3664. * PAGE_SIZE;
  3665. }
  3666. }
  3667. if (expected_size == 0)
  3668. goto out;
  3669. size = vma->vm_end - vma->vm_start;
  3670. if (size != expected_size)
  3671. goto out;
  3672. start = vma->vm_start;
  3673. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  3674. if (rb->pg_vec == NULL)
  3675. continue;
  3676. for (i = 0; i < rb->pg_vec_len; i++) {
  3677. struct page *page;
  3678. void *kaddr = rb->pg_vec[i].buffer;
  3679. int pg_num;
  3680. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  3681. page = pgv_to_page(kaddr);
  3682. err = vm_insert_page(vma, start, page);
  3683. if (unlikely(err))
  3684. goto out;
  3685. start += PAGE_SIZE;
  3686. kaddr += PAGE_SIZE;
  3687. }
  3688. }
  3689. }
  3690. atomic_inc(&po->mapped);
  3691. vma->vm_ops = &packet_mmap_ops;
  3692. err = 0;
  3693. out:
  3694. mutex_unlock(&po->pg_vec_lock);
  3695. return err;
  3696. }
  3697. static const struct proto_ops packet_ops_spkt = {
  3698. .family = PF_PACKET,
  3699. .owner = THIS_MODULE,
  3700. .release = packet_release,
  3701. .bind = packet_bind_spkt,
  3702. .connect = sock_no_connect,
  3703. .socketpair = sock_no_socketpair,
  3704. .accept = sock_no_accept,
  3705. .getname = packet_getname_spkt,
  3706. .poll = datagram_poll,
  3707. .ioctl = packet_ioctl,
  3708. .listen = sock_no_listen,
  3709. .shutdown = sock_no_shutdown,
  3710. .setsockopt = sock_no_setsockopt,
  3711. .getsockopt = sock_no_getsockopt,
  3712. .sendmsg = packet_sendmsg_spkt,
  3713. .recvmsg = packet_recvmsg,
  3714. .mmap = sock_no_mmap,
  3715. .sendpage = sock_no_sendpage,
  3716. };
  3717. static const struct proto_ops packet_ops = {
  3718. .family = PF_PACKET,
  3719. .owner = THIS_MODULE,
  3720. .release = packet_release,
  3721. .bind = packet_bind,
  3722. .connect = sock_no_connect,
  3723. .socketpair = sock_no_socketpair,
  3724. .accept = sock_no_accept,
  3725. .getname = packet_getname,
  3726. .poll = packet_poll,
  3727. .ioctl = packet_ioctl,
  3728. .listen = sock_no_listen,
  3729. .shutdown = sock_no_shutdown,
  3730. .setsockopt = packet_setsockopt,
  3731. .getsockopt = packet_getsockopt,
  3732. .sendmsg = packet_sendmsg,
  3733. .recvmsg = packet_recvmsg,
  3734. .mmap = packet_mmap,
  3735. .sendpage = sock_no_sendpage,
  3736. };
  3737. static const struct net_proto_family packet_family_ops = {
  3738. .family = PF_PACKET,
  3739. .create = packet_create,
  3740. .owner = THIS_MODULE,
  3741. };
  3742. static struct notifier_block packet_netdev_notifier = {
  3743. .notifier_call = packet_notifier,
  3744. };
  3745. #ifdef CONFIG_PROC_FS
  3746. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  3747. __acquires(RCU)
  3748. {
  3749. struct net *net = seq_file_net(seq);
  3750. rcu_read_lock();
  3751. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  3752. }
  3753. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  3754. {
  3755. struct net *net = seq_file_net(seq);
  3756. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  3757. }
  3758. static void packet_seq_stop(struct seq_file *seq, void *v)
  3759. __releases(RCU)
  3760. {
  3761. rcu_read_unlock();
  3762. }
  3763. static int packet_seq_show(struct seq_file *seq, void *v)
  3764. {
  3765. if (v == SEQ_START_TOKEN)
  3766. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  3767. else {
  3768. struct sock *s = sk_entry(v);
  3769. const struct packet_sock *po = pkt_sk(s);
  3770. seq_printf(seq,
  3771. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  3772. s,
  3773. atomic_read(&s->sk_refcnt),
  3774. s->sk_type,
  3775. ntohs(po->num),
  3776. po->ifindex,
  3777. po->running,
  3778. atomic_read(&s->sk_rmem_alloc),
  3779. from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
  3780. sock_i_ino(s));
  3781. }
  3782. return 0;
  3783. }
  3784. static const struct seq_operations packet_seq_ops = {
  3785. .start = packet_seq_start,
  3786. .next = packet_seq_next,
  3787. .stop = packet_seq_stop,
  3788. .show = packet_seq_show,
  3789. };
  3790. static int packet_seq_open(struct inode *inode, struct file *file)
  3791. {
  3792. return seq_open_net(inode, file, &packet_seq_ops,
  3793. sizeof(struct seq_net_private));
  3794. }
  3795. static const struct file_operations packet_seq_fops = {
  3796. .owner = THIS_MODULE,
  3797. .open = packet_seq_open,
  3798. .read = seq_read,
  3799. .llseek = seq_lseek,
  3800. .release = seq_release_net,
  3801. };
  3802. #endif
  3803. static int __net_init packet_net_init(struct net *net)
  3804. {
  3805. mutex_init(&net->packet.sklist_lock);
  3806. INIT_HLIST_HEAD(&net->packet.sklist);
  3807. if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
  3808. return -ENOMEM;
  3809. return 0;
  3810. }
  3811. static void __net_exit packet_net_exit(struct net *net)
  3812. {
  3813. remove_proc_entry("packet", net->proc_net);
  3814. }
  3815. static struct pernet_operations packet_net_ops = {
  3816. .init = packet_net_init,
  3817. .exit = packet_net_exit,
  3818. };
  3819. static void __exit packet_exit(void)
  3820. {
  3821. unregister_netdevice_notifier(&packet_netdev_notifier);
  3822. unregister_pernet_subsys(&packet_net_ops);
  3823. sock_unregister(PF_PACKET);
  3824. proto_unregister(&packet_proto);
  3825. }
  3826. static int __init packet_init(void)
  3827. {
  3828. int rc = proto_register(&packet_proto, 0);
  3829. if (rc != 0)
  3830. goto out;
  3831. sock_register(&packet_family_ops);
  3832. register_pernet_subsys(&packet_net_ops);
  3833. register_netdevice_notifier(&packet_netdev_notifier);
  3834. out:
  3835. return rc;
  3836. }
  3837. module_init(packet_init);
  3838. module_exit(packet_exit);
  3839. MODULE_LICENSE("GPL");
  3840. MODULE_ALIAS_NETPROTO(PF_PACKET);