af_packet.c 106 KB

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