af_packet.c 107 KB

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