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