11eaf0ac3Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-4-Clause AND BSD-3-Clause
3*22ce4affSfengbojiang *
41eaf0ac3Slogwang * Copyright (C) 1998 WIDE Project.
51eaf0ac3Slogwang * All rights reserved.
61eaf0ac3Slogwang *
71eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
81eaf0ac3Slogwang * modification, are permitted provided that the following conditions
91eaf0ac3Slogwang * are met:
101eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
111eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
121eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
141eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
151eaf0ac3Slogwang * 3. Neither the name of the project nor the names of its contributors
161eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
171eaf0ac3Slogwang * without specific prior written permission.
181eaf0ac3Slogwang *
191eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
201eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
231eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291eaf0ac3Slogwang * SUCH DAMAGE.
301eaf0ac3Slogwang */
311eaf0ac3Slogwang /*-
321eaf0ac3Slogwang * Copyright (c) 1989 Stephen Deering
331eaf0ac3Slogwang * Copyright (c) 1992, 1993
341eaf0ac3Slogwang * The Regents of the University of California. All rights reserved.
351eaf0ac3Slogwang *
361eaf0ac3Slogwang * This code is derived from software contributed to Berkeley by
371eaf0ac3Slogwang * Stephen Deering of Stanford University.
381eaf0ac3Slogwang *
391eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
401eaf0ac3Slogwang * modification, are permitted provided that the following conditions
411eaf0ac3Slogwang * are met:
421eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
431eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
441eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
451eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
461eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
471eaf0ac3Slogwang * 3. All advertising materials mentioning features or use of this software
481eaf0ac3Slogwang * must display the following acknowledgement:
491eaf0ac3Slogwang * This product includes software developed by the University of
501eaf0ac3Slogwang * California, Berkeley and its contributors.
511eaf0ac3Slogwang * 4. Neither the name of the University nor the names of its contributors
521eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
531eaf0ac3Slogwang * without specific prior written permission.
541eaf0ac3Slogwang *
551eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
561eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
571eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
581eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
591eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
601eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
611eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
621eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
631eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
641eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
651eaf0ac3Slogwang * SUCH DAMAGE.
661eaf0ac3Slogwang *
671eaf0ac3Slogwang * @(#)mroute.c 8.2 (Berkeley) 4/28/95
681eaf0ac3Slogwang */
691eaf0ac3Slogwang
701eaf0ac3Slogwang #include <sys/cdefs.h>
711eaf0ac3Slogwang __FBSDID("$FreeBSD$");
721eaf0ac3Slogwang
731eaf0ac3Slogwang #ifdef INET6
741eaf0ac3Slogwang #include <sys/param.h>
751eaf0ac3Slogwang #include <sys/queue.h>
761eaf0ac3Slogwang #include <sys/socket.h>
771eaf0ac3Slogwang #include <sys/socketvar.h>
781eaf0ac3Slogwang #include <sys/sysctl.h>
791eaf0ac3Slogwang #include <sys/protosw.h>
801eaf0ac3Slogwang #include <sys/mbuf.h>
811eaf0ac3Slogwang #include <sys/time.h>
821eaf0ac3Slogwang
831eaf0ac3Slogwang #include <net/if.h>
841eaf0ac3Slogwang #include <net/route.h>
851eaf0ac3Slogwang
861eaf0ac3Slogwang #include <netinet/in.h>
871eaf0ac3Slogwang
881eaf0ac3Slogwang #include <err.h>
891eaf0ac3Slogwang #include <stdint.h>
901eaf0ac3Slogwang #include <stdio.h>
911eaf0ac3Slogwang #include <stdlib.h>
921eaf0ac3Slogwang #include <stdbool.h>
931eaf0ac3Slogwang #include <libxo/xo.h>
941eaf0ac3Slogwang
951eaf0ac3Slogwang #define KERNEL 1
961eaf0ac3Slogwang #include <netinet6/ip6_mroute.h>
971eaf0ac3Slogwang #undef KERNEL
981eaf0ac3Slogwang
991eaf0ac3Slogwang #include "netstat.h"
1001eaf0ac3Slogwang
1011eaf0ac3Slogwang #define WID_ORG (Wflag ? 39 : (numeric_addr ? 29 : 18)) /* width of origin column */
1021eaf0ac3Slogwang #define WID_GRP (Wflag ? 18 : (numeric_addr ? 16 : 18)) /* width of group column */
1031eaf0ac3Slogwang
1041eaf0ac3Slogwang void
mroute6pr()1051eaf0ac3Slogwang mroute6pr()
1061eaf0ac3Slogwang {
1071eaf0ac3Slogwang struct mf6c *mf6ctable[MF6CTBLSIZ], *mfcp;
1081eaf0ac3Slogwang struct mif6_sctl mif6table[MAXMIFS];
1091eaf0ac3Slogwang struct mf6c mfc;
1101eaf0ac3Slogwang struct rtdetq rte, *rtep;
1111eaf0ac3Slogwang struct mif6_sctl *mifp;
1121eaf0ac3Slogwang mifi_t mifi;
1131eaf0ac3Slogwang int i;
1141eaf0ac3Slogwang int banner_printed;
1151eaf0ac3Slogwang int saved_numeric_addr;
1161eaf0ac3Slogwang mifi_t maxmif = 0;
1171eaf0ac3Slogwang long int waitings;
1181eaf0ac3Slogwang size_t len;
1191eaf0ac3Slogwang
1201eaf0ac3Slogwang if (live == 0)
1211eaf0ac3Slogwang return;
1221eaf0ac3Slogwang
1231eaf0ac3Slogwang len = sizeof(mif6table);
1241eaf0ac3Slogwang if (sysctlbyname("net.inet6.ip6.mif6table", mif6table, &len, NULL, 0) <
1251eaf0ac3Slogwang 0) {
1261eaf0ac3Slogwang xo_warn("sysctl: net.inet6.ip6.mif6table");
1271eaf0ac3Slogwang return;
1281eaf0ac3Slogwang }
1291eaf0ac3Slogwang
1301eaf0ac3Slogwang saved_numeric_addr = numeric_addr;
1311eaf0ac3Slogwang numeric_addr = 1;
1321eaf0ac3Slogwang banner_printed = 0;
1331eaf0ac3Slogwang
1341eaf0ac3Slogwang for (mifi = 0, mifp = mif6table; mifi < MAXMIFS; ++mifi, ++mifp) {
1351eaf0ac3Slogwang char ifname[IFNAMSIZ];
1361eaf0ac3Slogwang
1371eaf0ac3Slogwang if (mifp->m6_ifp == 0)
1381eaf0ac3Slogwang continue;
1391eaf0ac3Slogwang
1401eaf0ac3Slogwang maxmif = mifi;
1411eaf0ac3Slogwang if (!banner_printed) {
1421eaf0ac3Slogwang xo_open_list("multicast-interface");
1431eaf0ac3Slogwang xo_emit("\n{T:IPv6 Multicast Interface Table}\n"
1441eaf0ac3Slogwang "{T: Mif Rate PhyIF Pkts-In Pkts-Out}\n");
1451eaf0ac3Slogwang banner_printed = 1;
1461eaf0ac3Slogwang }
1471eaf0ac3Slogwang
1481eaf0ac3Slogwang xo_open_instance("multicast-interface");
1491eaf0ac3Slogwang xo_emit(" {:mif/%2u} {:rate-limit/%4d}",
1501eaf0ac3Slogwang mifi, mifp->m6_rate_limit);
1511eaf0ac3Slogwang xo_emit(" {:ifname/%5s}", (mifp->m6_flags & MIFF_REGISTER) ?
1521eaf0ac3Slogwang "reg0" : if_indextoname(mifp->m6_ifp, ifname));
1531eaf0ac3Slogwang
1541eaf0ac3Slogwang xo_emit(" {:received-packets/%9ju} {:sent-packets/%9ju}\n",
1551eaf0ac3Slogwang (uintmax_t)mifp->m6_pkt_in,
1561eaf0ac3Slogwang (uintmax_t)mifp->m6_pkt_out);
1571eaf0ac3Slogwang xo_close_instance("multicast-interface");
1581eaf0ac3Slogwang }
1591eaf0ac3Slogwang if (banner_printed)
1601eaf0ac3Slogwang xo_open_list("multicast-interface");
1611eaf0ac3Slogwang else
1621eaf0ac3Slogwang xo_emit("\n{T:IPv6 Multicast Interface Table is empty}\n");
1631eaf0ac3Slogwang
1641eaf0ac3Slogwang len = sizeof(mf6ctable);
1651eaf0ac3Slogwang if (sysctlbyname("net.inet6.ip6.mf6ctable", mf6ctable, &len, NULL, 0) <
1661eaf0ac3Slogwang 0) {
1671eaf0ac3Slogwang xo_warn("sysctl: net.inet6.ip6.mf6ctable");
1681eaf0ac3Slogwang return;
1691eaf0ac3Slogwang }
1701eaf0ac3Slogwang
1711eaf0ac3Slogwang banner_printed = 0;
1721eaf0ac3Slogwang
1731eaf0ac3Slogwang for (i = 0; i < MF6CTBLSIZ; ++i) {
1741eaf0ac3Slogwang mfcp = mf6ctable[i];
1751eaf0ac3Slogwang while(mfcp) {
1761eaf0ac3Slogwang kread((u_long)mfcp, (char *)&mfc, sizeof(mfc));
1771eaf0ac3Slogwang if (!banner_printed) {
1781eaf0ac3Slogwang xo_open_list("multicast-forwarding-cache");
1791eaf0ac3Slogwang xo_emit("\n"
1801eaf0ac3Slogwang "{T:IPv6 Multicast Forwarding Cache}\n");
1811eaf0ac3Slogwang xo_emit(" {T:%-*.*s} {T:%-*.*s} {T:%s}",
1821eaf0ac3Slogwang WID_ORG, WID_ORG, "Origin",
1831eaf0ac3Slogwang WID_GRP, WID_GRP, "Group",
1841eaf0ac3Slogwang " Packets Waits In-Mif Out-Mifs\n");
1851eaf0ac3Slogwang banner_printed = 1;
1861eaf0ac3Slogwang }
1871eaf0ac3Slogwang
1881eaf0ac3Slogwang xo_open_instance("multicast-forwarding-cache");
1891eaf0ac3Slogwang
1901eaf0ac3Slogwang xo_emit(" {:origin/%-*.*s}", WID_ORG, WID_ORG,
1911eaf0ac3Slogwang routename(sin6tosa(&mfc.mf6c_origin),
1921eaf0ac3Slogwang numeric_addr));
1931eaf0ac3Slogwang xo_emit(" {:group/%-*.*s}", WID_GRP, WID_GRP,
1941eaf0ac3Slogwang routename(sin6tosa(&mfc.mf6c_mcastgrp),
1951eaf0ac3Slogwang numeric_addr));
1961eaf0ac3Slogwang xo_emit(" {:total-packets/%9ju}",
1971eaf0ac3Slogwang (uintmax_t)mfc.mf6c_pkt_cnt);
1981eaf0ac3Slogwang
1991eaf0ac3Slogwang for (waitings = 0, rtep = mfc.mf6c_stall; rtep; ) {
2001eaf0ac3Slogwang waitings++;
2011eaf0ac3Slogwang /* XXX KVM */
2021eaf0ac3Slogwang kread((u_long)rtep, (char *)&rte, sizeof(rte));
2031eaf0ac3Slogwang rtep = rte.next;
2041eaf0ac3Slogwang }
2051eaf0ac3Slogwang xo_emit(" {:waitings/%3ld}", waitings);
2061eaf0ac3Slogwang
2071eaf0ac3Slogwang if (mfc.mf6c_parent == MF6C_INCOMPLETE_PARENT)
2081eaf0ac3Slogwang xo_emit(" --- ");
2091eaf0ac3Slogwang else
2101eaf0ac3Slogwang xo_emit(" {:parent/%3d} ", mfc.mf6c_parent);
2111eaf0ac3Slogwang xo_open_list("mif");
2121eaf0ac3Slogwang for (mifi = 0; mifi <= maxmif; mifi++) {
2131eaf0ac3Slogwang if (IF_ISSET(mifi, &mfc.mf6c_ifset))
2141eaf0ac3Slogwang xo_emit(" {l:%u}", mifi);
2151eaf0ac3Slogwang }
2161eaf0ac3Slogwang xo_close_list("mif");
2171eaf0ac3Slogwang xo_emit("\n");
2181eaf0ac3Slogwang
2191eaf0ac3Slogwang mfcp = mfc.mf6c_next;
2201eaf0ac3Slogwang xo_close_instance("multicast-forwarding-cache");
2211eaf0ac3Slogwang }
2221eaf0ac3Slogwang }
2231eaf0ac3Slogwang if (banner_printed)
2241eaf0ac3Slogwang xo_close_list("multicast-forwarding-cache");
2251eaf0ac3Slogwang else
2261eaf0ac3Slogwang xo_emit("\n{T:IPv6 Multicast Forwarding Table is empty}\n");
2271eaf0ac3Slogwang
2281eaf0ac3Slogwang xo_emit("\n");
2291eaf0ac3Slogwang numeric_addr = saved_numeric_addr;
2301eaf0ac3Slogwang }
2311eaf0ac3Slogwang
2321eaf0ac3Slogwang void
mrt6_stats()2331eaf0ac3Slogwang mrt6_stats()
2341eaf0ac3Slogwang {
2351eaf0ac3Slogwang struct mrt6stat mrtstat;
2361eaf0ac3Slogwang
2371eaf0ac3Slogwang if (fetch_stats("net.inet6.ip6.mrt6stat", 0, &mrtstat,
2381eaf0ac3Slogwang sizeof(mrtstat), kread_counters) != 0)
2391eaf0ac3Slogwang return;
2401eaf0ac3Slogwang
2411eaf0ac3Slogwang xo_open_container("multicast-statistics");
2421eaf0ac3Slogwang xo_emit("{T:IPv6 multicast forwarding}:\n");
2431eaf0ac3Slogwang
2441eaf0ac3Slogwang #define p(f, m) if (mrtstat.f || sflag <= 1) \
2451eaf0ac3Slogwang xo_emit(m, (uintmax_t)mrtstat.f, plural(mrtstat.f))
2461eaf0ac3Slogwang #define p2(f, m) if (mrtstat.f || sflag <= 1) \
2471eaf0ac3Slogwang xo_emit(m, (uintmax_t)mrtstat.f, plurales(mrtstat.f))
2481eaf0ac3Slogwang
2491eaf0ac3Slogwang p(mrt6s_mfc_lookups, "\t{:cache-lookups/%ju} "
2501eaf0ac3Slogwang "{N:/multicast forwarding cache lookup%s}\n");
2511eaf0ac3Slogwang p2(mrt6s_mfc_misses, "\t{:cache-misses/%ju} "
2521eaf0ac3Slogwang "{N:/multicast forwarding cache miss%s}\n");
2531eaf0ac3Slogwang p(mrt6s_upcalls, "\t{:upcalls/%ju} "
2541eaf0ac3Slogwang "{N:/upcall%s to multicast routing daemon}\n");
2551eaf0ac3Slogwang p(mrt6s_upq_ovflw, "\t{:upcall-overflows/%ju} "
2561eaf0ac3Slogwang "{N:/upcall queue overflow%s}\n");
2571eaf0ac3Slogwang p(mrt6s_upq_sockfull, "\t{:upcalls-dropped-full-buffer/%ju} "
2581eaf0ac3Slogwang "{N:/upcall%s dropped due to full socket buffer}\n");
2591eaf0ac3Slogwang p(mrt6s_cache_cleanups, "\t{:cache-cleanups/%ju} "
2601eaf0ac3Slogwang "{N:/cache cleanup%s}\n");
2611eaf0ac3Slogwang p(mrt6s_no_route, "\t{:dropped-no-origin/%ju} "
2621eaf0ac3Slogwang "{N:/datagram%s with no route for origin}\n");
2631eaf0ac3Slogwang p(mrt6s_bad_tunnel, "\t{:dropped-bad-tunnel/%ju} "
2641eaf0ac3Slogwang "{N:/datagram%s arrived with bad tunneling}\n");
2651eaf0ac3Slogwang p(mrt6s_cant_tunnel, "\t{:dropped-could-not-tunnel/%ju} "
2661eaf0ac3Slogwang "{N:/datagram%s could not be tunneled}\n");
2671eaf0ac3Slogwang p(mrt6s_wrong_if, "\t{:dropped-wrong-incoming-interface/%ju} "
2681eaf0ac3Slogwang "{N:/datagram%s arrived on wrong interface}\n");
2691eaf0ac3Slogwang p(mrt6s_drop_sel, "\t{:dropped-selectively/%ju} "
2701eaf0ac3Slogwang "{N:/datagram%s selectively dropped}\n");
2711eaf0ac3Slogwang p(mrt6s_q_overflow, "\t{:dropped-queue-overflow/%ju} "
2721eaf0ac3Slogwang "{N:/datagram%s dropped due to queue overflow}\n");
2731eaf0ac3Slogwang p(mrt6s_pkt2large, "\t{:dropped-too-large/%ju} "
2741eaf0ac3Slogwang "{N:/datagram%s dropped for being too large}\n");
2751eaf0ac3Slogwang
2761eaf0ac3Slogwang #undef p2
2771eaf0ac3Slogwang #undef p
2781eaf0ac3Slogwang xo_close_container("multicast-statistics");
2791eaf0ac3Slogwang }
2801eaf0ac3Slogwang #endif /*INET6*/
281