Home | History | Annotate | Line # | Download | only in netstat
      1  1.5   msaitoh /*	$NetBSD: pfkey.c,v 1.5 2022/09/02 06:25:43 msaitoh Exp $	*/
      2  1.1  drochner /*	$KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $	*/
      3  1.1  drochner 
      4  1.1  drochner /*
      5  1.1  drochner  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
      6  1.1  drochner  * All rights reserved.
      7  1.4   msaitoh  *
      8  1.1  drochner  * Redistribution and use in source and binary forms, with or without
      9  1.1  drochner  * modification, are permitted provided that the following conditions
     10  1.1  drochner  * are met:
     11  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     12  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     13  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     16  1.1  drochner  * 3. Neither the name of the project nor the names of its contributors
     17  1.1  drochner  *    may be used to endorse or promote products derived from this software
     18  1.1  drochner  *    without specific prior written permission.
     19  1.4   msaitoh  *
     20  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1  drochner  * SUCH DAMAGE.
     31  1.1  drochner  */
     32  1.1  drochner 
     33  1.1  drochner /*
     34  1.1  drochner  * Copyright (c) 1983, 1988, 1993
     35  1.1  drochner  *	The Regents of the University of California.  All rights reserved.
     36  1.1  drochner  *
     37  1.1  drochner  * Redistribution and use in source and binary forms, with or without
     38  1.1  drochner  * modification, are permitted provided that the following conditions
     39  1.1  drochner  * are met:
     40  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     41  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     42  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     43  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     44  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     45  1.1  drochner  * 3. Neither the name of the University nor the names of its contributors
     46  1.1  drochner  *    may be used to endorse or promote products derived from this software
     47  1.1  drochner  *    without specific prior written permission.
     48  1.1  drochner  *
     49  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     50  1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     51  1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     52  1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     53  1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     54  1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     55  1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     56  1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     57  1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     58  1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     59  1.1  drochner  * SUCH DAMAGE.
     60  1.1  drochner  */
     61  1.1  drochner 
     62  1.1  drochner #include <sys/cdefs.h>
     63  1.1  drochner #ifndef lint
     64  1.1  drochner #if 0
     65  1.1  drochner static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
     66  1.1  drochner #else
     67  1.1  drochner #ifdef __NetBSD__
     68  1.5   msaitoh __RCSID("$NetBSD: pfkey.c,v 1.5 2022/09/02 06:25:43 msaitoh Exp $");
     69  1.1  drochner #endif
     70  1.1  drochner #endif
     71  1.1  drochner #endif /* not lint */
     72  1.1  drochner 
     73  1.1  drochner #include <sys/param.h>
     74  1.1  drochner #include <sys/queue.h>
     75  1.1  drochner #include <sys/socket.h>
     76  1.1  drochner #include <sys/sysctl.h>
     77  1.1  drochner 
     78  1.1  drochner #ifdef IPSEC
     79  1.1  drochner #include <netipsec/keysock.h>
     80  1.1  drochner #endif
     81  1.1  drochner 
     82  1.1  drochner #include <err.h>
     83  1.3     ozaki #include <errno.h>
     84  1.1  drochner #include <stdio.h>
     85  1.1  drochner #include <string.h>
     86  1.1  drochner #include <unistd.h>
     87  1.1  drochner #include "netstat.h"
     88  1.2     kamil #include "prog_ops.h"
     89  1.1  drochner 
     90  1.4   msaitoh #ifdef IPSEC
     91  1.1  drochner 
     92  1.1  drochner static const char *pfkey_msgtypenames[] = {
     93  1.1  drochner 	"reserved", "getspi", "update", "add", "delete",
     94  1.1  drochner 	"get", "acquire", "register", "expire", "flush",
     95  1.1  drochner 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
     96  1.1  drochner 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
     97  1.1  drochner 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
     98  1.1  drochner };
     99  1.1  drochner 
    100  1.1  drochner static const char *pfkey_msgtype_names(int);
    101  1.1  drochner 
    102  1.1  drochner static const char *
    103  1.1  drochner pfkey_msgtype_names(int x)
    104  1.1  drochner {
    105  1.1  drochner 	const int max =
    106  1.1  drochner 	    sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
    107  1.1  drochner 	static char buf[20];
    108  1.1  drochner 
    109  1.1  drochner 	if (x < max && pfkey_msgtypenames[x])
    110  1.1  drochner 		return pfkey_msgtypenames[x];
    111  1.1  drochner 	snprintf(buf, sizeof(buf), "#%d", x);
    112  1.1  drochner 	return buf;
    113  1.1  drochner }
    114  1.1  drochner 
    115  1.1  drochner void
    116  1.1  drochner pfkey_stats(u_long off, const char *name)
    117  1.1  drochner {
    118  1.1  drochner 	uint64_t pfkeystat[PFKEY_NSTATS];
    119  1.1  drochner 	int first, type;
    120  1.1  drochner 
    121  1.1  drochner 	if (use_sysctl) {
    122  1.1  drochner 		size_t size = sizeof(pfkeystat);
    123  1.1  drochner 
    124  1.2     kamil 		if (prog_sysctlbyname("net.key.stats", pfkeystat, &size,
    125  1.3     ozaki 				 NULL, 0) == -1 && errno != ENOMEM)
    126  1.1  drochner 			return;
    127  1.1  drochner 	} else {
    128  1.1  drochner 		warnx("%s stats not available via KVM.", name);
    129  1.1  drochner 		return;
    130  1.1  drochner 	}
    131  1.1  drochner 
    132  1.1  drochner 	printf ("%s:\n", name);
    133  1.1  drochner 
    134  1.5   msaitoh #define	p(f, m) if (pfkeystat[f] || sflag <= 1)				\
    135  1.1  drochner     printf(m, (unsigned long long)pfkeystat[f], plural(pfkeystat[f]))
    136  1.1  drochner 
    137  1.1  drochner 	/* userland -> kernel */
    138  1.1  drochner 	p(PFKEY_STAT_OUT_TOTAL, "\t%llu request%s sent from userland\n");
    139  1.1  drochner 	p(PFKEY_STAT_OUT_BYTES, "\t%llu byte%s sent from userland\n");
    140  1.1  drochner 	for (first = 1, type = 0; type < 256; type++) {
    141  1.1  drochner 		if (pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type] == 0)
    142  1.1  drochner 			continue;
    143  1.1  drochner 		if (first) {
    144  1.1  drochner 			printf("\thistogram by message type:\n");
    145  1.1  drochner 			first = 0;
    146  1.1  drochner 		}
    147  1.1  drochner 		printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
    148  1.1  drochner 		    (unsigned long long)pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type]);
    149  1.1  drochner 	}
    150  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVLEN,
    151  1.5   msaitoh 	    "\t%llu message%s with invalid length field\n");
    152  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVVER,
    153  1.5   msaitoh 	    "\t%llu message%s with invalid version field\n");
    154  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVMSGTYPE,
    155  1.5   msaitoh 	    "\t%llu message%s with invalid message type field\n");
    156  1.5   msaitoh 	p(PFKEY_STAT_OUT_TOOSHORT,
    157  1.5   msaitoh 	    "\t%llu message%s too short\n");
    158  1.5   msaitoh 	p(PFKEY_STAT_OUT_NOMEM,
    159  1.5   msaitoh 	    "\t%llu message%s with memory allocation failure\n");
    160  1.5   msaitoh 	p(PFKEY_STAT_OUT_DUPEXT,
    161  1.5   msaitoh 	    "\t%llu message%s with duplicate extension\n");
    162  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVEXTTYPE,
    163  1.5   msaitoh 	    "\t%llu message%s with invalid extension type\n");
    164  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVSATYPE,
    165  1.5   msaitoh 	    "\t%llu message%s with invalid sa type\n");
    166  1.5   msaitoh 	p(PFKEY_STAT_OUT_INVADDR,
    167  1.5   msaitoh 	    "\t%llu message%s with invalid address extension\n");
    168  1.1  drochner 
    169  1.1  drochner 	/* kernel -> userland */
    170  1.1  drochner 	p(PFKEY_STAT_IN_TOTAL, "\t%llu request%s sent to userland\n");
    171  1.1  drochner 	p(PFKEY_STAT_IN_BYTES, "\t%llu byte%s sent to userland\n");
    172  1.1  drochner 	for (first = 1, type = 0; type < 256; type++) {
    173  1.1  drochner 		if (pfkeystat[PFKEY_STAT_IN_MSGTYPE + type] == 0)
    174  1.1  drochner 			continue;
    175  1.1  drochner 		if (first) {
    176  1.1  drochner 			printf("\thistogram by message type:\n");
    177  1.1  drochner 			first = 0;
    178  1.1  drochner 		}
    179  1.1  drochner 		printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
    180  1.1  drochner 		    (unsigned long long)pfkeystat[PFKEY_STAT_IN_MSGTYPE + type]);
    181  1.1  drochner 	}
    182  1.1  drochner 	p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ONE,
    183  1.1  drochner 	    "\t%llu message%s toward single socket\n");
    184  1.1  drochner 	p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ALL,
    185  1.1  drochner 	    "\t%llu message%s toward all sockets\n");
    186  1.1  drochner 	p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_REGISTERED,
    187  1.1  drochner 	    "\t%llu message%s toward registered sockets\n");
    188  1.5   msaitoh 	p(PFKEY_STAT_IN_NOMEM,
    189  1.5   msaitoh 	    "\t%llu message%s with memory allocation failure\n");
    190  1.1  drochner #undef p
    191  1.1  drochner }
    192  1.1  drochner #endif /*IPSEC*/
    193