]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/mroute.c
Divide statistics in the number of packets with 1000 instead of 1024
[FreeBSD/FreeBSD.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. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)mroute.c    8.2 (Berkeley) 4/28/95
38  */
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 /*
44  * Print multicast routing structures and statistics.
45  *
46  * MROUTING 1.0
47  */
48
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/protosw.h>
55 #include <sys/mbuf.h>
56 #include <sys/time.h>
57
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <netinet/igmp.h>
61 #include <net/route.h>
62
63 #define _KERNEL 1
64 #include <netinet/ip_mroute.h>
65 #undef _KERNEL
66
67 #include <err.h>
68 #include <nlist.h>
69 #include <stdint.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <stdbool.h>
73 #include <string.h>
74 #include <libxo/xo.h>
75 #include "netstat.h"
76
77 /*
78  * kvm(3) bindings for every needed symbol
79  */
80 static struct nlist mrl[] = {
81 #define N_MRTSTAT       0
82         { .n_name = "_mrtstat" },
83 #define N_MFCHASHTBL    1
84         { .n_name = "_mfchashtbl" },
85 #define N_VIFTABLE      2
86         { .n_name = "_viftable" },
87 #define N_MFCTABLESIZE  3
88         { .n_name = "_mfctablesize" },
89         { .n_name = NULL },
90 };
91
92 static void     print_bw_meter(struct bw_meter *, int *);
93 static void     print_mfc(struct mfc *, int, int *);
94
95 static void
96 print_bw_meter(struct bw_meter *bw_meter, int *banner_printed)
97 {
98         char s1[256], s2[256], s3[256];
99         struct timeval now, end, delta;
100
101         gettimeofday(&now, NULL);
102
103         if (! *banner_printed) {
104                 xo_open_list("bandwidth-meter");
105                 xo_emit(" {T:Bandwidth Meters}\n");
106                 xo_emit("  {T:/%-30s}", "Measured(Start|Packets|Bytes)");
107                 xo_emit(" {T:/%s}", "Type");
108                 xo_emit("  {T:/%-30s}", "Thresh(Interval|Packets|Bytes)");
109                 xo_emit(" {T:Remain}");
110                 xo_emit("\n");
111                 *banner_printed = 1;
112         }
113
114         xo_open_instance("bandwidth-meter");
115
116         /* The measured values */
117         if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) {
118                 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_measured.b_packets);
119                 xo_emit("{e:measured-packets/%ju}",
120                     (uintmax_t)bw_meter->bm_measured.b_packets);
121         } else
122                 sprintf(s1, "?");
123         if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) {
124                 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_measured.b_bytes);
125                 xo_emit("{e:measured-bytes/%ju}",
126                     (uintmax_t)bw_meter->bm_measured.b_bytes);
127         } else
128                 sprintf(s2, "?");
129         xo_emit("  {[:-30}{:start-time/%lu.%06lu}|{q:measured-packets/%s}"
130             "|{q:measured-bytes%s}{]:}",
131             (u_long)bw_meter->bm_start_time.tv_sec,
132             (u_long)bw_meter->bm_start_time.tv_usec, s1, s2);
133
134         /* The type of entry */
135         xo_emit("  {t:type/%-3s}", (bw_meter->bm_flags & BW_METER_GEQ) ? ">=" :
136             (bw_meter->bm_flags & BW_METER_LEQ) ? "<=" : "?");
137
138         /* The threshold values */
139         if (bw_meter->bm_flags & BW_METER_UNIT_PACKETS) {
140                 sprintf(s1, "%ju", (uintmax_t)bw_meter->bm_threshold.b_packets);
141                 xo_emit("{e:threshold-packets/%ju}",
142                     (uintmax_t)bw_meter->bm_threshold.b_packets);
143         } else
144                 sprintf(s1, "?");
145         if (bw_meter->bm_flags & BW_METER_UNIT_BYTES) {
146                 sprintf(s2, "%ju", (uintmax_t)bw_meter->bm_threshold.b_bytes);
147                 xo_emit("{e:threshold-bytes/%ju}",
148                     (uintmax_t)bw_meter->bm_threshold.b_bytes);
149         } else
150                 sprintf(s2, "?");
151
152         xo_emit("  {[:-30}{:threshold-time/%lu.%06lu}|{q:threshold-packets/%s}"
153             "|{q:threshold-bytes%s}{]:}",
154             (u_long)bw_meter->bm_threshold.b_time.tv_sec,
155             (u_long)bw_meter->bm_threshold.b_time.tv_usec, s1, s2);
156
157         /* Remaining time */
158         timeradd(&bw_meter->bm_start_time,
159                  &bw_meter->bm_threshold.b_time, &end);
160         if (timercmp(&now, &end, <=)) {
161                 timersub(&end, &now, &delta);
162                 sprintf(s3, "%lu.%06lu",
163                         (u_long)delta.tv_sec,
164                         (u_long)delta.tv_usec);
165         } else {
166                 /* Negative time */
167                 timersub(&now, &end, &delta);
168                 sprintf(s3, "-%lu.06%lu",
169                         (u_long)delta.tv_sec,
170                         (u_long)delta.tv_usec);
171         }
172         xo_emit(" {:remaining-time/%s}", s3);
173
174         xo_open_instance("bandwidth-meter");
175
176         xo_emit("\n");
177 }
178
179 static void
180 print_mfc(struct mfc *m, int maxvif, int *banner_printed)
181 {
182         struct sockaddr_in sin;
183         struct sockaddr *sa = (struct sockaddr *)&sin;
184         struct bw_meter bw_meter, *bwm;
185         int bw_banner_printed;
186         int error;
187         vifi_t vifi;
188
189         bw_banner_printed = 0;
190         memset(&sin, 0, sizeof(sin));
191         sin.sin_len = sizeof(sin);
192         sin.sin_family = AF_INET;
193
194         if (! *banner_printed) {
195                 xo_open_list("multicast-forwarding-entry");
196                 xo_emit("\n{T:IPv4 Multicast Forwarding Table}\n"
197                     " {T:Origin}          {T:Group}            "
198                     " {T:Packets In-Vif}  {T:Out-Vifs:Ttls}\n");
199                 *banner_printed = 1;
200         }
201
202         memcpy(&sin.sin_addr, &m->mfc_origin, sizeof(sin.sin_addr));
203         xo_emit(" {:origin-address/%-15.15s}", routename(sa, numeric_addr));
204         memcpy(&sin.sin_addr, &m->mfc_mcastgrp, sizeof(sin.sin_addr));
205         xo_emit(" {:group-address/%-15.15s}",
206             routename(sa, numeric_addr));
207         xo_emit(" {:sent-packets/%9lu}", m->mfc_pkt_cnt);
208         xo_emit("  {:parent/%3d}   ", m->mfc_parent);
209         xo_open_list("vif-ttl");
210         for (vifi = 0; vifi <= maxvif; vifi++) {
211                 if (m->mfc_ttls[vifi] > 0) {
212                         xo_open_instance("vif-ttl");
213                         xo_emit(" {k:vif/%u}:{:ttl/%u}", vifi,
214                             m->mfc_ttls[vifi]);
215                         xo_close_instance("vif-ttl");
216                 }
217         }
218         xo_close_list("vif-ttl");
219         xo_emit("\n");
220
221         /*
222          * XXX We break the rules and try to use KVM to read the
223          * bandwidth meters, they are not retrievable via sysctl yet.
224          */
225         bwm = m->mfc_bw_meter;
226         while (bwm != NULL) {
227                 error = kread((u_long)bwm, (char *)&bw_meter,
228                     sizeof(bw_meter));
229                 if (error)
230                         break;
231                 print_bw_meter(&bw_meter, &bw_banner_printed);
232                 bwm = bw_meter.bm_mfc_next;
233         }
234         if (banner_printed)
235                 xo_close_list("bandwidth-meter");
236 }
237
238 void
239 mroutepr()
240 {
241         struct sockaddr_in sin;
242         struct sockaddr *sa = (struct sockaddr *)&sin;
243         struct vif viftable[MAXVIFS];
244         struct vif *v;
245         struct mfc *m;
246         u_long pmfchashtbl, pmfctablesize, pviftbl;
247         int banner_printed;
248         int saved_numeric_addr;
249         size_t len;
250         vifi_t vifi, maxvif;
251
252         saved_numeric_addr = numeric_addr;
253         numeric_addr = 1;
254
255         memset(&sin, 0, sizeof(sin));
256         sin.sin_len = sizeof(sin);
257         sin.sin_family = AF_INET;
258
259         /*
260          * TODO:
261          * The VIF table will move to hanging off the struct if_info for
262          * each IPv4 configured interface. Currently it is statically
263          * allocated, and retrieved either using KVM or an opaque SYSCTL.
264          *
265          * This can't happen until the API documented in multicast(4)
266          * is itself refactored. The historical reason why VIFs use
267          * a separate ifindex space is entirely due to the legacy
268          * capability of the MROUTING code to create IPIP tunnels on
269          * the fly to support DVMRP. When gif(4) became available, this
270          * functionality was deprecated, as PIM does not use it.
271          */
272         maxvif = 0;
273         pmfchashtbl = pmfctablesize = pviftbl = 0;
274
275         len = sizeof(viftable);
276         if (live) {
277                 if (sysctlbyname("net.inet.ip.viftable", viftable, &len, NULL,
278                     0) < 0) {
279                         xo_warn("sysctl: net.inet.ip.viftable");
280                         return;
281                 }
282         } else {
283                 kresolve_list(mrl);
284                 pmfchashtbl = mrl[N_MFCHASHTBL].n_value;
285                 pmfctablesize = mrl[N_MFCTABLESIZE].n_value;
286                 pviftbl = mrl[N_VIFTABLE].n_value;
287
288                 if (pmfchashtbl == 0 || pmfctablesize == 0 || pviftbl == 0) {
289                         xo_warnx("No IPv4 MROUTING kernel support.");
290                         return;
291                 }
292
293                 kread(pviftbl, (char *)viftable, sizeof(viftable));
294         }
295
296         banner_printed = 0;
297         for (vifi = 0, v = viftable; vifi < MAXVIFS; ++vifi, ++v) {
298                 if (v->v_lcl_addr.s_addr == 0)
299                         continue;
300
301                 maxvif = vifi;
302                 if (!banner_printed) {
303                         xo_emit("\n{T:IPv4 Virtual Interface Table\n"
304                             " Vif   Thresh   Local-Address   "
305                             "Remote-Address    Pkts-In   Pkts-Out}\n");
306                         banner_printed = 1;
307                         xo_open_list("vif");
308                 }
309
310                 xo_open_instance("vif");
311                 memcpy(&sin.sin_addr, &v->v_lcl_addr, sizeof(sin.sin_addr));
312                 xo_emit(" {:vif/%2u}    {:threshold/%6u}   {:route/%-15.15s}",
313                                         /* opposite math of add_vif() */
314                     vifi, v->v_threshold,
315                     routename(sa, numeric_addr));
316                 memcpy(&sin.sin_addr, &v->v_rmt_addr, sizeof(sin.sin_addr));
317                 xo_emit(" {:source/%-15.15s}", (v->v_flags & VIFF_TUNNEL) ?
318                     routename(sa, numeric_addr) : "");
319
320                 xo_emit(" {:received-packets/%9lu}  {:sent-packets/%9lu}\n",
321                     v->v_pkt_in, v->v_pkt_out);
322                 xo_close_instance("vif");
323         }
324         if (banner_printed)
325                 xo_close_list("vif");
326         else
327                 xo_emit("\n{T:IPv4 Virtual Interface Table is empty}\n");
328
329         banner_printed = 0;
330
331         /*
332          * TODO:
333          * The MFC table will move into the AF_INET radix trie in future.
334          * In 8.x, it becomes a dynamically allocated structure referenced
335          * by a hashed LIST, allowing more than 256 entries w/o kernel tuning.
336          *
337          * If retrieved via opaque SYSCTL, the kernel will coalesce it into
338          * a static table for us.
339          * If retrieved via KVM, the hash list pointers must be followed.
340          */
341         if (live) {
342                 struct mfc *mfctable;
343
344                 len = 0;
345                 if (sysctlbyname("net.inet.ip.mfctable", NULL, &len, NULL,
346                     0) < 0) {
347                         xo_warn("sysctl: net.inet.ip.mfctable");
348                         return;
349                 }
350
351                 mfctable = malloc(len);
352                 if (mfctable == NULL) {
353                         xo_warnx("malloc %lu bytes", (u_long)len);
354                         return;
355                 }
356                 if (sysctlbyname("net.inet.ip.mfctable", mfctable, &len, NULL,
357                     0) < 0) {
358                         free(mfctable);
359                         xo_warn("sysctl: net.inet.ip.mfctable");
360                         return;
361                 }
362
363                 m = mfctable;
364                 while (len >= sizeof(*m)) {
365                         print_mfc(m++, maxvif, &banner_printed);
366                         len -= sizeof(*m);
367                 }
368                 if (banner_printed)
369                         xo_close_list("multicast-forwarding-entry");
370                 if (len != 0)
371                         xo_warnx("print_mfc: %lu trailing bytes", (u_long)len);
372
373                 free(mfctable);
374         } else {
375                 LIST_HEAD(, mfc) *mfchashtbl;
376                 u_long i, mfctablesize;
377                 struct mfc mfc;
378                 int error;
379
380                 error = kread(pmfctablesize, (char *)&mfctablesize,
381                     sizeof(u_long));
382                 if (error) {
383                         xo_warn("kread: mfctablesize");
384                         return;
385                 }
386
387                 len = sizeof(*mfchashtbl) * mfctablesize;
388                 mfchashtbl = malloc(len);
389                 if (mfchashtbl == NULL) {
390                         xo_warnx("malloc %lu bytes", (u_long)len);
391                         return;
392                 }
393                 kread(pmfchashtbl, (char *)&mfchashtbl, len);
394
395                 for (i = 0; i < mfctablesize; i++) {
396                         LIST_FOREACH(m, &mfchashtbl[i], mfc_hash) {
397                                 kread((u_long)m, (char *)&mfc, sizeof(mfc));
398                                 print_mfc(m, maxvif, &banner_printed);
399                         }
400                 }
401                 if (banner_printed)
402                         xo_close_list("multicast-forwarding-entry");
403
404                 free(mfchashtbl);
405         }
406
407         if (!banner_printed)
408                 xo_emit("\n{T:IPv4 Multicast Forwarding Table is empty}\n");
409
410         xo_emit("\n");
411         numeric_addr = saved_numeric_addr;
412 }
413
414 void
415 mrt_stats()
416 {
417         struct mrtstat mrtstat;
418         u_long mstaddr;
419         size_t len = sizeof(mrtstat);
420
421         kresolve_list(mrl);
422         mstaddr = mrl[N_MRTSTAT].n_value;
423
424         if (mstaddr == 0) {
425                 fprintf(stderr, "No IPv4 MROUTING kernel support.\n");
426                 return;
427         }
428
429         if (live) {
430                 if (sysctlbyname("net.inet.ip.mrtstat", &mrtstat, &len, NULL,
431                     0) < 0) {
432                         xo_warn("sysctl: net.inet.ip.mrtstat failed.");
433                         return;
434                 }
435         } else
436                 kread_counters(mstaddr, &mrtstat, len);
437
438         xo_emit("{T:IPv4 multicast forwarding}:\n");
439
440 #define p(f, m) if (mrtstat.f || sflag <= 1) \
441         xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
442 #define p2(f, m) if (mrtstat.f || sflag <= 1) \
443         xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
444
445         xo_open_container("multicast-statistics");
446
447         p(mrts_mfc_lookups, "\t{:cache-lookups/%ju} "
448             "{N:/multicast forwarding cache lookup%s}\n");
449         p2(mrts_mfc_misses, "\t{:cache-misses/%ju} "
450             "{N:/multicast forwarding cache miss%s}\n");
451         p(mrts_upcalls, "\t{:upcalls-total/%ju} "
452             "{N:/upcall%s to multicast routing daemon}\n");
453         p(mrts_upq_ovflw, "\t{:upcall-overflows/%ju} "
454             "{N:/upcall queue overflow%s}\n");
455         p(mrts_upq_sockfull,
456             "\t{:upcalls-dropped-full-buffer/%ju} "
457             "{N:/upcall%s dropped due to full socket buffer}\n");
458         p(mrts_cache_cleanups, "\t{:cache-cleanups/%ju} "
459             "{N:/cache cleanup%s}\n");
460         p(mrts_no_route, "\t{:dropped-no-origin/%ju} "
461             "{N:/datagram%s with no route for origin}\n");
462         p(mrts_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
463             "{N:/datagram%s arrived with bad tunneling}\n");
464         p(mrts_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
465             "{N:/datagram%s could not be tunneled}\n");
466         p(mrts_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
467             "{N:/datagram%s arrived on wrong interface}\n");
468         p(mrts_drop_sel, "\t{:dropped-selectively/%ju} "
469             "{N:/datagram%s selectively dropped}\n");
470         p(mrts_q_overflow, "\t{:dropped-queue-overflow/%ju} "
471             "{N:/datagram%s dropped due to queue overflow}\n");
472         p(mrts_pkt2large, "\t{:dropped-too-large/%ju} "
473             "{N:/datagram%s dropped for being too large}\n");
474
475 #undef  p2
476 #undef  p
477 }