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