xref: /freebsd/usr.bin/netstat/pfkey.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*	$FreeBSD$	*/
2 /*	$NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $	*/
3 /*	$KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $	*/
4 
5 /*
6  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1983, 1988, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  */
66 
67 #include <sys/cdefs.h>
68 #ifndef lint
69 /*
70 static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
71 */
72 static const char rcsid[] =
73   "$FreeBSD$";
74 #endif /* not lint */
75 
76 #include <sys/param.h>
77 #include <sys/queue.h>
78 #include <sys/socket.h>
79 
80 #include <netinet/in.h>
81 
82 #if defined(IPSEC) && !defined(FAST_IPSEC)
83 #include <netkey/keysock.h>
84 #endif
85 
86 #ifdef FAST_IPSEC
87 #include <netipsec/keysock.h>
88 #endif
89 
90 #include <stdint.h>
91 #include <stdio.h>
92 #include <string.h>
93 #include <unistd.h>
94 #include "netstat.h"
95 
96 #ifdef IPSEC
97 
98 static const char *pfkey_msgtypenames[] = {
99 	"reserved", "getspi", "update", "add", "delete",
100 	"get", "acquire", "register", "expire", "flush",
101 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
102 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
103 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
104 };
105 
106 static const char *pfkey_msgtype_names (int);
107 
108 
109 static const char *
110 pfkey_msgtype_names(int x)
111 {
112 	const int max =
113 	    sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
114 	static char buf[20];
115 
116 	if (x < max && pfkey_msgtypenames[x])
117 		return pfkey_msgtypenames[x];
118 	snprintf(buf, sizeof(buf), "#%d", x);
119 	return buf;
120 }
121 
122 void
123 pfkey_stats(u_long off, const char *name, int family __unused)
124 {
125 	struct pfkeystat pfkeystat;
126 	unsigned first, type;
127 
128 	if (off == 0)
129 		return;
130 	printf ("%s:\n", name);
131 	kread(off, (char *)&pfkeystat, sizeof(pfkeystat));
132 
133 #define	p(f, m) if (pfkeystat.f || sflag <= 1) \
134     printf(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
135 
136 	/* userland -> kernel */
137 	p(out_total, "\t%ju request%s sent from userland\n");
138 	p(out_bytes, "\t%ju byte%s sent from userland\n");
139 	for (first = 1, type = 0;
140 	     type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
141 	     type++) {
142 		if (pfkeystat.out_msgtype[type] <= 0)
143 			continue;
144 		if (first) {
145 			printf("\thistogram by message type:\n");
146 			first = 0;
147 		}
148 		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
149 			(uintmax_t)pfkeystat.out_msgtype[type]);
150 	}
151 	p(out_invlen, "\t%ju message%s with invalid length field\n");
152 	p(out_invver, "\t%ju message%s with invalid version field\n");
153 	p(out_invmsgtype, "\t%ju message%s with invalid message type field\n");
154 	p(out_tooshort, "\t%ju message%s too short\n");
155 	p(out_nomem, "\t%ju message%s with memory allocation failure\n");
156 	p(out_dupext, "\t%ju message%s with duplicate extension\n");
157 	p(out_invexttype, "\t%ju message%s with invalid extension type\n");
158 	p(out_invsatype, "\t%ju message%s with invalid sa type\n");
159 	p(out_invaddr, "\t%ju message%s with invalid address extension\n");
160 
161 	/* kernel -> userland */
162 	p(in_total, "\t%ju request%s sent to userland\n");
163 	p(in_bytes, "\t%ju byte%s sent to userland\n");
164 	for (first = 1, type = 0;
165 	     type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
166 	     type++) {
167 		if (pfkeystat.in_msgtype[type] <= 0)
168 			continue;
169 		if (first) {
170 			printf("\thistogram by message type:\n");
171 			first = 0;
172 		}
173 		printf("\t\t%s: %ju\n", pfkey_msgtype_names(type),
174 			(uintmax_t)pfkeystat.in_msgtype[type]);
175 	}
176 	p(in_msgtarget[KEY_SENDUP_ONE],
177 	    "\t%ju message%s toward single socket\n");
178 	p(in_msgtarget[KEY_SENDUP_ALL],
179 	    "\t%ju message%s toward all sockets\n");
180 	p(in_msgtarget[KEY_SENDUP_REGISTERED],
181 	    "\t%ju message%s toward registered sockets\n");
182 	p(in_nomem, "\t%ju message%s with memory allocation failure\n");
183 #undef p
184 }
185 #endif /* IPSEC */
186