af_packet.c 100 KB

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