xref: /freebsd/usr.bin/netstat/pfkey.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
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 #endif
64 
65 #include <sys/cdefs.h>
66 #include <sys/param.h>
67 #include <sys/queue.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 
71 #include <netinet/in.h>
72 
73 #ifdef IPSEC
74 #include <netipsec/keysock.h>
75 #endif
76 
77 #include <stdint.h>
78 #include <stdio.h>
79 #include <string.h>
80 #include <unistd.h>
81 #include <stdbool.h>
82 #include <libxo/xo.h>
83 #include "netstat.h"
84 
85 #ifdef IPSEC
86 
87 static const char *pfkey_msgtypenames[] = {
88 	"reserved", "getspi", "update", "add", "delete",
89 	"get", "acquire", "register", "expire", "flush",
90 	"dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
91 	"x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
92 	"x_spdsetidx", "x_spdexpire", "x_spddelete2"
93 };
94 
95 static const char *pfkey_msgtype_names (int);
96 
97 
98 static const char *
99 pfkey_msgtype_names(int x)
100 {
101 	const int max = nitems(pfkey_msgtypenames);
102 	static char buf[20];
103 
104 	if (x < max && pfkey_msgtypenames[x])
105 		return pfkey_msgtypenames[x];
106 	snprintf(buf, sizeof(buf), "#%d", x);
107 	return buf;
108 }
109 
110 void
111 pfkey_stats(u_long off, const char *name, int family __unused,
112     int proto __unused)
113 {
114 	struct pfkeystat pfkeystat;
115 	unsigned first, type;
116 
117 	if (off == 0)
118 		return;
119 	xo_emit("{T:/%s}:\n", name);
120 	xo_open_container(name);
121 	kread_counters(off, (char *)&pfkeystat, sizeof(pfkeystat));
122 
123 #define	p(f, m) if (pfkeystat.f || sflag <= 1) \
124 	xo_emit(m, (uintmax_t)pfkeystat.f, plural(pfkeystat.f))
125 
126 	/* userland -> kernel */
127 	p(out_total, "\t{:sent-requests/%ju} "
128 	    "{N:/request%s sent from userland}\n");
129 	p(out_bytes, "\t{:sent-bytes/%ju} "
130 	    "{N:/byte%s sent from userland}\n");
131 	for (first = 1, type = 0;
132 	    type<sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
133 	    type++) {
134 		if (pfkeystat.out_msgtype[type] <= 0)
135 			continue;
136 		if (first) {
137 			xo_open_list("output-histogram");
138 			xo_emit("\t{T:histogram by message type}:\n");
139 			first = 0;
140 		}
141 		xo_open_instance("output-histogram");
142 		xo_emit("\t\t{k::type/%s}: {:count/%ju}\n",
143 		    pfkey_msgtype_names(type),
144 		    (uintmax_t)pfkeystat.out_msgtype[type]);
145 		xo_close_instance("output-histogram");
146 	}
147 	if (!first)
148 		xo_close_list("output-histogram");
149 
150 	p(out_invlen, "\t{:dropped-bad-length/%ju} "
151 	    "{N:/message%s with invalid length field}\n");
152 	p(out_invver, "\t{:dropped-bad-version/%ju} "
153 	    "{N:/message%s with invalid version field}\n");
154 	p(out_invmsgtype, "\t{:dropped-bad-type/%ju} "
155 	    "{N:/message%s with invalid message type field}\n");
156 	p(out_tooshort, "\t{:dropped-too-short/%ju} "
157 	    "{N:/message%s too short}\n");
158 	p(out_nomem, "\t{:dropped-no-memory/%ju} "
159 	    "{N:/message%s with memory allocation failure}\n");
160 	p(out_dupext, "\t{:dropped-duplicate-extension/%ju} "
161 	    "{N:/message%s with duplicate extension}\n");
162 	p(out_invexttype, "\t{:dropped-bad-extension/%ju} "
163 	    "{N:/message%s with invalid extension type}\n");
164 	p(out_invsatype, "\t{:dropped-bad-sa-type/%ju} "
165 	    "{N:/message%s with invalid sa type}\n");
166 	p(out_invaddr, "\t{:dropped-bad-address-extension/%ju} "
167 	    "{N:/message%s with invalid address extension}\n");
168 
169 	/* kernel -> userland */
170 	p(in_total, "\t{:received-requests/%ju} "
171 	    "{N:/request%s sent to userland}\n");
172 	p(in_bytes, "\t{:received-bytes/%ju} "
173 	    "{N:/byte%s sent to userland}\n");
174 	for (first = 1, type = 0;
175 	    type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
176 	    type++) {
177 		if (pfkeystat.in_msgtype[type] <= 0)
178 			continue;
179 		if (first) {
180 			xo_open_list("input-histogram");
181 			xo_emit("\t{T:histogram by message type}:\n");
182 			first = 0;
183 		}
184 		xo_open_instance("input-histogram");
185 		xo_emit("\t\t{k:type/%s}: {:count/%ju}\n",
186 		    pfkey_msgtype_names(type),
187 		    (uintmax_t)pfkeystat.in_msgtype[type]);
188 		xo_close_instance("input-histogram");
189 	}
190 	if (!first)
191 		xo_close_list("input-histogram");
192 	p(in_msgtarget[KEY_SENDUP_ONE], "\t{:received-one-socket/%ju} "
193 	    "{N:/message%s toward single socket}\n");
194 	p(in_msgtarget[KEY_SENDUP_ALL], "\t{:received-all-sockets/%ju} "
195 	    "{N:/message%s toward all sockets}\n");
196 	p(in_msgtarget[KEY_SENDUP_REGISTERED],
197 	    "\t{:received-registered-sockets/%ju} "
198 	    "{N:/message%s toward registered sockets}\n");
199 	p(in_nomem, "\t{:discarded-no-memory/%ju} "
200 	    "{N:/message%s with memory allocation failure}\n");
201 #undef p
202 	xo_close_container(name);
203 }
204 #endif /* IPSEC */
205