af_packet.c 107 KB

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