Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / usr.bin / netstat / mroute.c
1 /*
2  * Copyright (c) 1989 Stephen Deering
3  * Copyright (c) 1992, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Stephen Deering of Stanford University.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)mroute.c    8.2 (Berkeley) 4/28/95
34  *
35  * $FreeBSD: src/usr.bin/netstat/mroute.c,v 1.11.2.4 2001/09/17 14:53:17 ru Exp $
36  * $DragonFly: src/usr.bin/netstat/mroute.c,v 1.5 2005/08/04 17:31:23 drhodus Exp $
37  */
38
39 /*
40  * Print multicast routing structures and statistics.
41  *
42  * MROUTING 1.0
43  */
44
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
50 #include <sys/mbuf.h>
51 #include <sys/time.h>
52
53 #include <net/if.h>
54 #include <netinet/in.h>
55 #include <netinet/igmp.h>
56 #include <net/route.h>
57 #include <net/ip_mroute/ip_mroute.h>
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include "netstat.h"
62
63 static void print_bw_meter(struct bw_meter *bw_meter, int *banner_printed);
64
65 void
66 mroutepr(u_long mfcaddr, u_long vifaddr)
67 {
68         struct mfc *mfctable[MFCTBLSIZ];
69         struct vif viftable[MAXVIFS];
70         struct mfc mfc, *m;
71         struct vif *v;
72         vifi_t vifi;
73         int i;
74         int banner_printed;
75         int saved_numeric_addr;
76         vifi_t maxvif = 0;
77
78         if (mfcaddr == 0 || vifaddr == 0) {
79                 printf("No IPv4 multicast routing compiled into this system.\n");
80                 return;
81         }
82
83         saved_numeric_addr = numeric_addr;
84         numeric_addr = 1;
85
86         kread(vifaddr, (char *)&viftable, sizeof(viftable));
87         banner_printed = 0;
88         for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
89                 if (v->v_lcl_addr.s_addr == 0)
90                         continue;
91
92                 maxvif = vifi;
93                 if (!banner_printed) {
94                         printf("\nVirtual Interface Table\n"
95                                " Vif   Thresh   Rate   Local-Address   "
96                                "Remote-Address    Pkts-In   Pkts-Out\n");
97                         banner_printed = 1;
98                 }
99
100                 printf(" %2u    %6u   %4d   %-15.15s",
101                                         /* opposite math of add_vif() */
102                     vifi, v->v_threshold, v->v_rate_limit * 1000 / 1024, 
103                     routename(v->v_lcl_addr.s_addr));
104                 printf(" %-15.15s", (v->v_flags & VIFF_TUNNEL) ?
105                     routename(v->v_rmt_addr.s_addr) : "");
106
107                 printf(" %9lu  %9lu\n", v->v_pkt_in, v->v_pkt_out);
108         }
109         if (!banner_printed)
110                 printf("\nVirtual Interface Table is empty\n");
111
112         kread(mfcaddr, (char *)&mfctable, sizeof(mfctable));
113         banner_printed = 0;
114         for (i = 0; i < MFCTBLSIZ; ++i) {
115                 m = mfctable[i];
116                 while(m) {
117                         kread((u_long)m, (char *)&mfc, sizeof mfc);
118
119                         if (!banner_printed) {
120                                 printf("\nIPv4 Multicast Forwarding Cache\n"
121                                        " Origin          Group            "
122                                        " Packets In-Vif  Out-Vifs:Ttls\n");
123                                 banner_printed = 1;
124                         }
125
126                         printf(" %-15.15s", routename(mfc.mfc_origin.s_addr));
127                         printf(" %-15.15s", routename(mfc.mfc_mcastgrp.s_addr));
128                         printf(" %9lu", mfc.mfc_pkt_cnt);
129                         printf("  %3d   ", mfc.mfc_parent);
130                         for (vifi = 0; vifi <= maxvif; vifi++) {
131                                 if (mfc.mfc_ttls[vifi] > 0)
132                                         printf(" %u:%u", vifi, 
133                                                mfc.mfc_ttls[vifi]);
134                         }
135                         printf("\n");
136
137                         /* Print the bw meter information */
138                         {
139                                 struct bw_meter bw_meter, *bwm;
140                                 int banner_printed2 = 0;
141                                 
142                                 bwm = mfc.mfc_bw_meter;
143                                 while (bwm) {
144                                     kread((u_long)bwm, (char *)&bw_meter,
145                                                 sizeof bw_meter);
146                                     print_bw_meter(&bw_meter,
147                                                 &banner_printed2);
148                                     bwm = bw_meter.bm_mfc_next;
149                                 }
150 #if 0   /* Don't ever print it? */
151                                 if (! banner_printed2)
152                                     printf("\n  No Bandwidth Meters\n");
153 #endif
154                         }
155
156                         m = mfc.mfc_next;
157                 }
158         }
159         if (!banner_printed)
160                 printf("\nMulticast Routing Table is empty\n");
161
162         printf("\n");
163         numeric_addr = saved_numeric_addr;
164 }
165
166 static void
167 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
168 {
169         char s0[256], s1[256], s2[256], s3[256];
170         struct timeval now, end, delta;
171
172         gettimeofday(&now, NULL);
173
174         if (! *banner_printed) {
175                 printf(" Bandwidth Meters\n");
176                 printf("  %-30s", "Measured(Start|Packets|Bytes)");
177                 printf(" %s", "Type");
178                 printf("  %-30s", "Thresh(Interval|Packets|Bytes)");
179                 printf(" Remain");
180                 printf("\n");
181                 *banner_printed = 1;
182         }
183
184         /* The measured values */
185         if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
186                 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_measured.b_packets);
187         else
188                 sprintf(s1, "?");
189         if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
190                 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_measured.b_bytes);
191         else
192                 sprintf(s2, "?");
193         sprintf(s0, "%lu.%lu|%s|%s",
194                 bw_meter->bm_start_time.tv_sec,
195                 bw_meter->bm_start_time.tv_usec,
196                 s1, s2);
197         printf("  %-30s", s0);
198
199         /* The type of entry */
200         sprintf(s0, "%s", "?");
201         if (bw_meter->bm_flags & BW_METER_GEQ)
202                 sprintf(s0, "%s", ">=");
203         else if (bw_meter->bm_flags & BW_METER_LEQ)
204                 sprintf(s0, "%s", "<=");
205         printf("  %-3s", s0);
206
207         /* The threshold values */
208         if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS)
209                 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_threshold.b_packets);
210         else
211                 sprintf(s1, "?");
212         if (bw_meter->bm_flags & BW_METER_UNIT_BYTES)
213                 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_threshold.b_bytes);
214         else
215                 sprintf(s2, "?");
216         sprintf(s0, "%lu.%lu|%s|%s",
217                 bw_meter->bm_threshold.b_time.tv_sec,
218                 bw_meter->bm_threshold.b_time.tv_usec,
219                 s1, s2);
220         printf("  %-30s", s0);
221
222         /* Remaining time */
223         timeradd(&bw_meter->bm_start_time,
224                  &bw_meter->bm_threshold.b_time, &end);
225         if (timercmp(&now, &end, <=)) {
226                 timersub(&end, &now, &delta);
227                 sprintf(s3, "%lu.%lu", delta.tv_sec, delta.tv_usec);
228         } else {
229                 /* Negative time */
230                 timersub(&now, &end, &delta);
231                 sprintf(s3, "-%lu.%lu", delta.tv_sec, delta.tv_usec);
232         }
233         printf(" %s", s3);
234
235         printf("\n");
236 }
237
238 void
239 mrt_stats(u_long mstaddr)
240 {
241         struct mrtstat mrtstat;
242
243         if (mstaddr == 0) {
244                 printf("No IPv4 multicast routing compiled into this system.\n");
245                 return;
246         }
247
248         kread(mstaddr, (char *)&mrtstat, sizeof(mrtstat));
249         printf("IPv4 multicast forwarding:\n");
250
251 #define p(f, m) if (mrtstat.f || sflag <= 1) \
252         printf(m, mrtstat.f, plural(mrtstat.f))
253 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
254         printf(m, mrtstat.f, plurales(mrtstat.f))
255
256         p(mrts_mfc_lookups, "\t%lu multicast forwarding cache lookup%s\n");
257         p2(mrts_mfc_misses, "\t%lu multicast forwarding cache miss%s\n");
258         p(mrts_upcalls, "\t%lu upcall%s to mrouted\n");
259         p(mrts_upq_ovflw, "\t%lu upcall queue overflow%s\n");
260         p(mrts_upq_sockfull,
261             "\t%lu upcall%s dropped due to full socket buffer\n");
262         p(mrts_cache_cleanups, "\t%lu cache cleanup%s\n");
263         p(mrts_no_route, "\t%lu datagram%s with no route for origin\n");
264         p(mrts_bad_tunnel, "\t%lu datagram%s arrived with bad tunneling\n");
265         p(mrts_cant_tunnel, "\t%lu datagram%s could not be tunneled\n");
266         p(mrts_wrong_if, "\t%lu datagram%s arrived on wrong interface\n");
267         p(mrts_drop_sel, "\t%lu datagram%s selectively dropped\n");
268         p(mrts_q_overflow, "\t%lu datagram%s dropped due to queue overflow\n");
269         p(mrts_pkt2large, "\t%lu datagram%s dropped for being too large\n");
270
271 #undef  p2
272 #undef  p
273 }