xref: /freebsd/usr.bin/netstat/pfkey.c (revision 04f7f23bf80d5f3675e1efd0893730d7b16ba725)
1100b98dbSKelly Yancey /*	$FreeBSD$	*/
2100b98dbSKelly Yancey /*	$NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $	*/
3100b98dbSKelly Yancey /*	$KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $	*/
4100b98dbSKelly Yancey 
5100b98dbSKelly Yancey /*
6100b98dbSKelly Yancey  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7100b98dbSKelly Yancey  * All rights reserved.
8100b98dbSKelly Yancey  *
9100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
10100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
11100b98dbSKelly Yancey  * are met:
12100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
13100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
14100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
15100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
16100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
17100b98dbSKelly Yancey  * 3. Neither the name of the project nor the names of its contributors
18100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
19100b98dbSKelly Yancey  *    without specific prior written permission.
20100b98dbSKelly Yancey  *
21100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31100b98dbSKelly Yancey  * SUCH DAMAGE.
32100b98dbSKelly Yancey  */
33100b98dbSKelly Yancey 
34100b98dbSKelly Yancey /*
35100b98dbSKelly Yancey  * Copyright (c) 1983, 1988, 1993
36100b98dbSKelly Yancey  *	The Regents of the University of California.  All rights reserved.
37100b98dbSKelly Yancey  *
38100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
39100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
40100b98dbSKelly Yancey  * are met:
41100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
42100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
43100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
44100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
45100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
46100b98dbSKelly Yancey  * 3. All advertising materials mentioning features or use of this software
47100b98dbSKelly Yancey  *    must display the following acknowledgement:
48100b98dbSKelly Yancey  *	This product includes software developed by the University of
49100b98dbSKelly Yancey  *	California, Berkeley and its contributors.
50100b98dbSKelly Yancey  * 4. Neither the name of the University nor the names of its contributors
51100b98dbSKelly Yancey  *    may be used to endorse or promote products derived from this software
52100b98dbSKelly Yancey  *    without specific prior written permission.
53100b98dbSKelly Yancey  *
54100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64100b98dbSKelly Yancey  * SUCH DAMAGE.
65100b98dbSKelly Yancey  */
66100b98dbSKelly Yancey 
67100b98dbSKelly Yancey #include <sys/cdefs.h>
68100b98dbSKelly Yancey #ifndef lint
69100b98dbSKelly Yancey /*
70100b98dbSKelly Yancey static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
71100b98dbSKelly Yancey */
72100b98dbSKelly Yancey static const char rcsid[] =
73100b98dbSKelly Yancey   "$FreeBSD$";
74100b98dbSKelly Yancey #endif /* not lint */
75100b98dbSKelly Yancey 
76100b98dbSKelly Yancey #include <sys/param.h>
77100b98dbSKelly Yancey #include <sys/queue.h>
78100b98dbSKelly Yancey #include <sys/socket.h>
79100b98dbSKelly Yancey 
80100b98dbSKelly Yancey #include <netinet/in.h>
81100b98dbSKelly Yancey 
82100b98dbSKelly Yancey #if defined(IPSEC) && !defined(FAST_IPSEC)
83100b98dbSKelly Yancey #include <netkey/keysock.h>
84100b98dbSKelly Yancey #endif
85100b98dbSKelly Yancey 
86100b98dbSKelly Yancey #ifdef FAST_IPSEC
87100b98dbSKelly Yancey #include <netipsec/keysock.h>
88100b98dbSKelly Yancey #endif
89100b98dbSKelly Yancey 
907b95a1ebSYaroslav Tykhiy #include <stdint.h>
91100b98dbSKelly Yancey #include <stdio.h>
92100b98dbSKelly Yancey #include <string.h>
93100b98dbSKelly Yancey #include <unistd.h>
94100b98dbSKelly Yancey #include "netstat.h"
95100b98dbSKelly Yancey 
96100b98dbSKelly Yancey #ifdef IPSEC
97100b98dbSKelly Yancey 
98100b98dbSKelly Yancey static const char *pfkey_msgtypenames[] = {
99100b98dbSKelly Yancey 	"reserved", "getspi", "update", "add", "delete",
100100b98dbSKelly Yancey 	"get", "acquire", "register", "expire", "flush",
101100b98dbSKelly Yancey 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
102100b98dbSKelly Yancey 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
103100b98dbSKelly Yancey 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
104100b98dbSKelly Yancey };
105100b98dbSKelly Yancey 
106100b98dbSKelly Yancey static const char *pfkey_msgtype_names (int);
107100b98dbSKelly Yancey 
108100b98dbSKelly Yancey 
109100b98dbSKelly Yancey static const char *
110100b98dbSKelly Yancey pfkey_msgtype_names(int x)
111100b98dbSKelly Yancey {
112100b98dbSKelly Yancey 	const int max =
113100b98dbSKelly Yancey 	    sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
114100b98dbSKelly Yancey 	static char buf[20];
115100b98dbSKelly Yancey 
116100b98dbSKelly Yancey 	if (x < max && pfkey_msgtypenames[x])
117100b98dbSKelly Yancey 		return pfkey_msgtypenames[x];
118100b98dbSKelly Yancey 	snprintf(buf, sizeof(buf), "#%d", x);
119100b98dbSKelly Yancey 	return buf;
120100b98dbSKelly Yancey }
121100b98dbSKelly Yancey 
122100b98dbSKelly Yancey void
12304f7f23bSYaroslav Tykhiy pfkey_stats(u_long off, const char *name, int family __unused)
124100b98dbSKelly Yancey {
125100b98dbSKelly Yancey 	struct pfkeystat pfkeystat;
126100b98dbSKelly Yancey 	unsigned first, type;
127100b98dbSKelly Yancey 
128100b98dbSKelly Yancey 	if (off == 0)
129100b98dbSKelly Yancey 		return;
130100b98dbSKelly Yancey 	printf ("%s:\n", name);
131100b98dbSKelly Yancey 	kread(off, (char *)&pfkeystat, sizeof(pfkeystat));
132100b98dbSKelly Yancey 
133100b98dbSKelly Yancey #define	p(f, m) if (pfkeystat.f || sflag <= 1) \
1347b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
135100b98dbSKelly Yancey 
136100b98dbSKelly Yancey 	/* userland -> kernel */
1377b95a1ebSYaroslav Tykhiy 	p(out_total, "\t%ju request%s sent from userland\n");
1387b95a1ebSYaroslav Tykhiy 	p(out_bytes, "\t%ju byte%s sent from userland\n");
139100b98dbSKelly Yancey 	for (first = 1, type = 0;
140100b98dbSKelly Yancey 	     type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
141100b98dbSKelly Yancey 	     type++) {
142100b98dbSKelly Yancey 		if (pfkeystat.out_msgtype[type] <= 0)
143100b98dbSKelly Yancey 			continue;
144100b98dbSKelly Yancey 		if (first) {
145100b98dbSKelly Yancey 			printf("\thistogram by message type:\n");
146100b98dbSKelly Yancey 			first = 0;
147100b98dbSKelly Yancey 		}
1487b95a1ebSYaroslav Tykhiy 		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
1497b95a1ebSYaroslav Tykhiy 			(uintmax_t)pfkeystat.out_msgtype[type]);
150100b98dbSKelly Yancey 	}
1517b95a1ebSYaroslav Tykhiy 	p(out_invlen, "\t%ju message%s with invalid length field\n");
1527b95a1ebSYaroslav Tykhiy 	p(out_invver, "\t%ju message%s with invalid version field\n");
1537b95a1ebSYaroslav Tykhiy 	p(out_invmsgtype, "\t%ju message%s with invalid message type field\n");
1547b95a1ebSYaroslav Tykhiy 	p(out_tooshort, "\t%ju message%s too short\n");
1557b95a1ebSYaroslav Tykhiy 	p(out_nomem, "\t%ju message%s with memory allocation failure\n");
1567b95a1ebSYaroslav Tykhiy 	p(out_dupext, "\t%ju message%s with duplicate extension\n");
1577b95a1ebSYaroslav Tykhiy 	p(out_invexttype, "\t%ju message%s with invalid extension type\n");
1587b95a1ebSYaroslav Tykhiy 	p(out_invsatype, "\t%ju message%s with invalid sa type\n");
1597b95a1ebSYaroslav Tykhiy 	p(out_invaddr, "\t%ju message%s with invalid address extension\n");
160100b98dbSKelly Yancey 
161100b98dbSKelly Yancey 	/* kernel -> userland */
1627b95a1ebSYaroslav Tykhiy 	p(in_total, "\t%ju request%s sent to userland\n");
1637b95a1ebSYaroslav Tykhiy 	p(in_bytes, "\t%ju byte%s sent to userland\n");
164100b98dbSKelly Yancey 	for (first = 1, type = 0;
165100b98dbSKelly Yancey 	     type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
166100b98dbSKelly Yancey 	     type++) {
167100b98dbSKelly Yancey 		if (pfkeystat.in_msgtype[type] <= 0)
168100b98dbSKelly Yancey 			continue;
169100b98dbSKelly Yancey 		if (first) {
170100b98dbSKelly Yancey 			printf("\thistogram by message type:\n");
171100b98dbSKelly Yancey 			first = 0;
172100b98dbSKelly Yancey 		}
1737b95a1ebSYaroslav Tykhiy 		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
1747b95a1ebSYaroslav Tykhiy 			(uintmax_t)pfkeystat.in_msgtype[type]);
175100b98dbSKelly Yancey 	}
176100b98dbSKelly Yancey 	p(in_msgtarget[KEY_SENDUP_ONE],
1777b95a1ebSYaroslav Tykhiy 	    "\t%ju message%s toward single socket\n");
178100b98dbSKelly Yancey 	p(in_msgtarget[KEY_SENDUP_ALL],
1797b95a1ebSYaroslav Tykhiy 	    "\t%ju message%s toward all sockets\n");
180100b98dbSKelly Yancey 	p(in_msgtarget[KEY_SENDUP_REGISTERED],
1817b95a1ebSYaroslav Tykhiy 	    "\t%ju message%s toward registered sockets\n");
1827b95a1ebSYaroslav Tykhiy 	p(in_nomem, "\t%ju message%s with memory allocation failure\n");
183100b98dbSKelly Yancey #undef p
184100b98dbSKelly Yancey }
185100b98dbSKelly Yancey #endif /* IPSEC */
186