11eaf0ac3Slogwang /* $NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $ */
21eaf0ac3Slogwang /* $KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $ */
31eaf0ac3Slogwang /*-
4*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-3-Clause
5*22ce4affSfengbojiang *
61eaf0ac3Slogwang * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
71eaf0ac3Slogwang * All rights reserved.
81eaf0ac3Slogwang *
91eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
101eaf0ac3Slogwang * modification, are permitted provided that the following conditions
111eaf0ac3Slogwang * are met:
121eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
131eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
141eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
151eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
161eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
171eaf0ac3Slogwang * 3. Neither the name of the project nor the names of its contributors
181eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
191eaf0ac3Slogwang * without specific prior written permission.
201eaf0ac3Slogwang *
211eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
221eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
251eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311eaf0ac3Slogwang * SUCH DAMAGE.
321eaf0ac3Slogwang */
331eaf0ac3Slogwang /*-
341eaf0ac3Slogwang * Copyright (c) 1983, 1988, 1993
351eaf0ac3Slogwang * The Regents of the University of California. All rights reserved.
361eaf0ac3Slogwang *
371eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without
381eaf0ac3Slogwang * modification, are permitted provided that the following conditions
391eaf0ac3Slogwang * are met:
401eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright
411eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer.
421eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright
431eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the
441eaf0ac3Slogwang * documentation and/or other materials provided with the distribution.
45*22ce4affSfengbojiang * 3. Neither the name of the University nor the names of its contributors
461eaf0ac3Slogwang * may be used to endorse or promote products derived from this software
471eaf0ac3Slogwang * without specific prior written permission.
481eaf0ac3Slogwang *
491eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
501eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
511eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
521eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
531eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
541eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
551eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
561eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
571eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
581eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
591eaf0ac3Slogwang * SUCH DAMAGE.
601eaf0ac3Slogwang */
611eaf0ac3Slogwang
621eaf0ac3Slogwang #if 0
631eaf0ac3Slogwang #ifndef lint
641eaf0ac3Slogwang static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95";
651eaf0ac3Slogwang #endif /* not lint */
661eaf0ac3Slogwang #endif
671eaf0ac3Slogwang
681eaf0ac3Slogwang #include <sys/cdefs.h>
691eaf0ac3Slogwang __FBSDID("$FreeBSD$");
701eaf0ac3Slogwang
711eaf0ac3Slogwang #include <sys/param.h>
721eaf0ac3Slogwang #include <sys/queue.h>
731eaf0ac3Slogwang #include <sys/socket.h>
741eaf0ac3Slogwang #include <sys/socketvar.h>
751eaf0ac3Slogwang
761eaf0ac3Slogwang #include <netinet/in.h>
771eaf0ac3Slogwang
781eaf0ac3Slogwang #ifdef IPSEC
791eaf0ac3Slogwang #include <netipsec/keysock.h>
801eaf0ac3Slogwang #endif
811eaf0ac3Slogwang
821eaf0ac3Slogwang #include <stdint.h>
831eaf0ac3Slogwang #include <stdio.h>
841eaf0ac3Slogwang #include <string.h>
851eaf0ac3Slogwang #include <unistd.h>
861eaf0ac3Slogwang #include <stdbool.h>
871eaf0ac3Slogwang #include <libxo/xo.h>
881eaf0ac3Slogwang #include "netstat.h"
891eaf0ac3Slogwang
901eaf0ac3Slogwang #ifdef IPSEC
911eaf0ac3Slogwang
921eaf0ac3Slogwang static const char *pfkey_msgtypenames[] = {
931eaf0ac3Slogwang "reserved", "getspi", "update", "add", "delete",
941eaf0ac3Slogwang "get", "acquire", "register", "expire", "flush",
951eaf0ac3Slogwang "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
961eaf0ac3Slogwang "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
971eaf0ac3Slogwang "x_spdsetidx", "x_spdexpire", "x_spddelete2"
981eaf0ac3Slogwang };
991eaf0ac3Slogwang
1001eaf0ac3Slogwang static const char *pfkey_msgtype_names (int);
1011eaf0ac3Slogwang
1021eaf0ac3Slogwang
1031eaf0ac3Slogwang static const char *
pfkey_msgtype_names(int x)1041eaf0ac3Slogwang pfkey_msgtype_names(int x)
1051eaf0ac3Slogwang {
106*22ce4affSfengbojiang const int max = nitems(pfkey_msgtypenames);
1071eaf0ac3Slogwang static char buf[20];
1081eaf0ac3Slogwang
1091eaf0ac3Slogwang if (x < max && pfkey_msgtypenames[x])
1101eaf0ac3Slogwang return pfkey_msgtypenames[x];
1111eaf0ac3Slogwang snprintf(buf, sizeof(buf), "#%d", x);
1121eaf0ac3Slogwang return buf;
1131eaf0ac3Slogwang }
1141eaf0ac3Slogwang
1151eaf0ac3Slogwang void
pfkey_stats(u_long off,const char * name,int family __unused,int proto __unused)1161eaf0ac3Slogwang pfkey_stats(u_long off, const char *name, int family __unused,
1171eaf0ac3Slogwang int proto __unused)
1181eaf0ac3Slogwang {
1191eaf0ac3Slogwang struct pfkeystat pfkeystat;
1201eaf0ac3Slogwang unsigned first, type;
1211eaf0ac3Slogwang
1221eaf0ac3Slogwang if (off == 0)
1231eaf0ac3Slogwang return;
1241eaf0ac3Slogwang xo_emit("{T:/%s}:\n", name);
1251eaf0ac3Slogwang xo_open_container(name);
1261eaf0ac3Slogwang kread_counters(off, (char *)&pfkeystat, sizeof(pfkeystat));
1271eaf0ac3Slogwang
1281eaf0ac3Slogwang #define p(f, m) if (pfkeystat.f || sflag <= 1) \
1291eaf0ac3Slogwang xo_emit(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
1301eaf0ac3Slogwang
1311eaf0ac3Slogwang /* userland -> kernel */
1321eaf0ac3Slogwang p(out_total, "\t{:sent-requests/%ju} "
1331eaf0ac3Slogwang "{N:/request%s sent from userland}\n");
1341eaf0ac3Slogwang p(out_bytes, "\t{:sent-bytes/%ju} "
1351eaf0ac3Slogwang "{N:/byte%s sent from userland}\n");
1361eaf0ac3Slogwang for (first = 1, type = 0;
1371eaf0ac3Slogwang type<sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
1381eaf0ac3Slogwang type++) {
1391eaf0ac3Slogwang if (pfkeystat.out_msgtype[type] <= 0)
1401eaf0ac3Slogwang continue;
1411eaf0ac3Slogwang if (first) {
1421eaf0ac3Slogwang xo_open_list("output-histogram");
1431eaf0ac3Slogwang xo_emit("\t{T:histogram by message type}:\n");
1441eaf0ac3Slogwang first = 0;
1451eaf0ac3Slogwang }
1461eaf0ac3Slogwang xo_open_instance("output-histogram");
1471eaf0ac3Slogwang xo_emit("\t\t{k::type/%s}: {:count/%ju}\n",
1481eaf0ac3Slogwang pfkey_msgtype_names(type),
1491eaf0ac3Slogwang (uintmax_t)pfkeystat.out_msgtype[type]);
1501eaf0ac3Slogwang xo_close_instance("output-histogram");
1511eaf0ac3Slogwang }
1521eaf0ac3Slogwang if (!first)
1531eaf0ac3Slogwang xo_close_list("output-histogram");
1541eaf0ac3Slogwang
1551eaf0ac3Slogwang p(out_invlen, "\t{:dropped-bad-length/%ju} "
1561eaf0ac3Slogwang "{N:/message%s with invalid length field}\n");
1571eaf0ac3Slogwang p(out_invver, "\t{:dropped-bad-version/%ju} "
1581eaf0ac3Slogwang "{N:/message%s with invalid version field}\n");
1591eaf0ac3Slogwang p(out_invmsgtype, "\t{:dropped-bad-type/%ju} "
1601eaf0ac3Slogwang "{N:/message%s with invalid message type field}\n");
1611eaf0ac3Slogwang p(out_tooshort, "\t{:dropped-too-short/%ju} "
1621eaf0ac3Slogwang "{N:/message%s too short}\n");
1631eaf0ac3Slogwang p(out_nomem, "\t{:dropped-no-memory/%ju} "
1641eaf0ac3Slogwang "{N:/message%s with memory allocation failure}\n");
1651eaf0ac3Slogwang p(out_dupext, "\t{:dropped-duplicate-extension/%ju} "
1661eaf0ac3Slogwang "{N:/message%s with duplicate extension}\n");
1671eaf0ac3Slogwang p(out_invexttype, "\t{:dropped-bad-extension/%ju} "
1681eaf0ac3Slogwang "{N:/message%s with invalid extension type}\n");
1691eaf0ac3Slogwang p(out_invsatype, "\t{:dropped-bad-sa-type/%ju} "
1701eaf0ac3Slogwang "{N:/message%s with invalid sa type}\n");
1711eaf0ac3Slogwang p(out_invaddr, "\t{:dropped-bad-address-extension/%ju} "
1721eaf0ac3Slogwang "{N:/message%s with invalid address extension}\n");
1731eaf0ac3Slogwang
1741eaf0ac3Slogwang /* kernel -> userland */
1751eaf0ac3Slogwang p(in_total, "\t{:received-requests/%ju} "
1761eaf0ac3Slogwang "{N:/request%s sent to userland}\n");
1771eaf0ac3Slogwang p(in_bytes, "\t{:received-bytes/%ju} "
1781eaf0ac3Slogwang "{N:/byte%s sent to userland}\n");
1791eaf0ac3Slogwang for (first = 1, type = 0;
1801eaf0ac3Slogwang type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
1811eaf0ac3Slogwang type++) {
1821eaf0ac3Slogwang if (pfkeystat.in_msgtype[type] <= 0)
1831eaf0ac3Slogwang continue;
1841eaf0ac3Slogwang if (first) {
1851eaf0ac3Slogwang xo_open_list("input-histogram");
1861eaf0ac3Slogwang xo_emit("\t{T:histogram by message type}:\n");
1871eaf0ac3Slogwang first = 0;
1881eaf0ac3Slogwang }
1891eaf0ac3Slogwang xo_open_instance("input-histogram");
1901eaf0ac3Slogwang xo_emit("\t\t{k:type/%s}: {:count/%ju}\n",
1911eaf0ac3Slogwang pfkey_msgtype_names(type),
1921eaf0ac3Slogwang (uintmax_t)pfkeystat.in_msgtype[type]);
1931eaf0ac3Slogwang xo_close_instance("input-histogram");
1941eaf0ac3Slogwang }
1951eaf0ac3Slogwang if (!first)
1961eaf0ac3Slogwang xo_close_list("input-histogram");
1971eaf0ac3Slogwang p(in_msgtarget[KEY_SENDUP_ONE], "\t{:received-one-socket/%ju} "
1981eaf0ac3Slogwang "{N:/message%s toward single socket}\n");
1991eaf0ac3Slogwang p(in_msgtarget[KEY_SENDUP_ALL], "\t{:received-all-sockets/%ju} "
2001eaf0ac3Slogwang "{N:/message%s toward all sockets}\n");
2011eaf0ac3Slogwang p(in_msgtarget[KEY_SENDUP_REGISTERED],
2021eaf0ac3Slogwang "\t{:received-registered-sockets/%ju} "
2031eaf0ac3Slogwang "{N:/message%s toward registered sockets}\n");
2041eaf0ac3Slogwang p(in_nomem, "\t{:discarded-no-memory/%ju} "
2051eaf0ac3Slogwang "{N:/message%s with memory allocation failure}\n");
2061eaf0ac3Slogwang #undef p
2071eaf0ac3Slogwang xo_close_container(name);
2081eaf0ac3Slogwang }
2091eaf0ac3Slogwang #endif /* IPSEC */
210