source: rtems-libbsd/freebsd/usr.bin/netstat/mroute.c @ c40e45b

55-freebsd-126-freebsd-12
Last change on this file since c40e45b was c40e45b, checked in by Sebastian Huber <sebastian.huber@…>, on 10/07/16 at 13:10:20

Update to FreeBSD head 2016-08-23

Git mirror commit 9fe7c416e6abb28b1398fd3e5687099846800cfd.

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