af_packet.c 89 KB

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