xref: /freebsd/usr.bin/netstat/ipsec.c (revision a15cb219c6f2b8ed16179c2fce882a2ff327b753)
1 /*	$KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2005 NTT Multimedia Communications Laboratories, Inc.
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 /*-
31  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. Neither the name of the project nor the names of its contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58 /*-
59  * Copyright (c) 1983, 1988, 1993
60  *	The Regents of the University of California.  All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. Neither the name of the University nor the names of its contributors
71  *    may be used to endorse or promote products derived from this software
72  *    without specific prior written permission.
73  *
74  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84  * SUCH DAMAGE.
85  */
86 
87 #if 0
88 #ifndef lint
89 static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
90 #endif /* not lint */
91 #endif
92 
93 #include <sys/cdefs.h>
94 __FBSDID("$FreeBSD$");
95 
96 #include <sys/param.h>
97 #include <sys/queue.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 
101 #include <netinet/in.h>
102 
103 #ifdef IPSEC
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ah_var.h>
106 #include <netipsec/esp_var.h>
107 #include <netipsec/ipcomp_var.h>
108 #endif
109 
110 #include <stdint.h>
111 #include <stdio.h>
112 #include <stdbool.h>
113 #include <string.h>
114 #include <unistd.h>
115 #include <libxo/xo.h>
116 #include "netstat.h"
117 
118 #ifdef IPSEC
119 struct val2str {
120 	int val;
121 	const char *str;
122 };
123 
124 static struct val2str ipsec_ahnames[] = {
125 	{ SADB_AALG_NONE, "none", },
126 	{ SADB_AALG_MD5HMAC, "hmac-md5", },
127 	{ SADB_AALG_SHA1HMAC, "hmac-sha1", },
128 	{ SADB_X_AALG_MD5, "keyed-md5", },
129 	{ SADB_X_AALG_SHA, "keyed-sha1", },
130 	{ SADB_X_AALG_NULL, "null", },
131 	{ SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
132 	{ SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
133 	{ SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
134 	{ SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
135 	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
136 	{ SADB_X_AALG_TCP_MD5, "tcp-md5", },
137 	{ SADB_X_AALG_AES128GMAC, "aes-gmac-128", },
138 	{ SADB_X_AALG_AES192GMAC, "aes-gmac-192", },
139 	{ SADB_X_AALG_AES256GMAC, "aes-gmac-256", },
140 	{ -1, NULL },
141 };
142 
143 static struct val2str ipsec_espnames[] = {
144 	{ SADB_EALG_NONE, "none", },
145 	{ SADB_EALG_DESCBC, "des-cbc", },
146 	{ SADB_EALG_3DESCBC, "3des-cbc", },
147 	{ SADB_EALG_NULL, "null", },
148 	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
149 	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
150 	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
151 	{ SADB_X_EALG_CAMELLIACBC, "camellia-cbc", },
152 	{ SADB_X_EALG_AESCTR, "aes-ctr", },
153 	{ SADB_X_EALG_AESGCM16, "aes-gcm-16", },
154 	{ SADB_X_EALG_AESGMAC, "aes-gmac", },
155 	{ -1, NULL },
156 };
157 
158 static struct val2str ipsec_compnames[] = {
159 	{ SADB_X_CALG_NONE, "none", },
160 	{ SADB_X_CALG_OUI, "oui", },
161 	{ SADB_X_CALG_DEFLATE, "deflate", },
162 	{ SADB_X_CALG_LZS, "lzs", },
163 	{ -1, NULL },
164 };
165 
166 static void print_ipsecstats(const struct ipsecstat *ipsecstat);
167 
168 static void
169 print_ipsecstats(const struct ipsecstat *ipsecstat)
170 {
171 	xo_open_container("ipsec-statistics");
172 
173 #define	p(f, m) if (ipsecstat->f || sflag <= 1) \
174 	xo_emit(m, (uintmax_t)ipsecstat->f, plural(ipsecstat->f))
175 #define	p2(f, m) if (ipsecstat->f || sflag <= 1) \
176 	xo_emit(m, (uintmax_t)ipsecstat->f, plurales(ipsecstat->f))
177 
178 	p(ips_in_polvio, "\t{:dropped-policy-violation/%ju} "
179 	    "{N:/inbound packet%s violated process security policy}\n");
180 	p(ips_in_nomem, "\t{:dropped-no-memory/%ju} "
181 	    "{N:/inbound packet%s failed due to insufficient memory}\n");
182 	p(ips_in_inval, "\t{:dropped-invalid/%ju} "
183 	    "{N:/invalid inbound packet%s}\n");
184 	p(ips_out_polvio, "\t{:discarded-policy-violation/%ju} "
185 	    "{N:/outbound packet%s violated process security policy}\n");
186 	p(ips_out_nosa, "\t{:discarded-no-sa/%ju} "
187 	    "{N:/outbound packet%s with no SA available}\n");
188 	p(ips_out_nomem, "\t{:discarded-no-memory/%ju} "
189 	    "{N:/outbound packet%s failed due to insufficient memory}\n");
190 	p(ips_out_noroute, "\t{:discarded-no-route/%ju} "
191 	    "{N:/outbound packet%s with no route available}\n");
192 	p(ips_out_inval, "\t{:discarded-invalid/%ju} "
193 	    "{N:/invalid outbound packet%s}\n");
194 	p(ips_out_bundlesa, "\t{:send-bundled-sa/%ju} "
195 	    "{N:/outbound packet%s with bundled SAs}\n");
196 	p(ips_spdcache_hits, "\t{:spdcache-hits/%ju} "
197 	    "{N:/spd cache hit%s}\n");
198 	p2(ips_spdcache_misses, "\t{:spdcache-misses/%ju} "
199 	    "{N:/spd cache miss%s}\n");
200 	p(ips_clcopied, "\t{:clusters-copied-during-clone/%ju} "
201 	    "{N:/cluster%s copied during clone}\n");
202 	p(ips_mbinserted, "\t{:mbufs-inserted/%ju} "
203 	    "{N:/mbuf%s inserted during makespace}\n");
204 #undef p2
205 #undef p
206 	xo_close_container("ipsec-statistics");
207 }
208 
209 void
210 ipsec_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
211 {
212 	struct ipsecstat ipsecstat;
213 
214 	if (strcmp(name, "ipsec6") == 0) {
215 		if (fetch_stats("net.inet6.ipsec6.ipsecstats", off,&ipsecstat,
216 				sizeof(ipsecstat), kread_counters) != 0)
217 			return;
218 	} else {
219 		if (fetch_stats("net.inet.ipsec.ipsecstats", off, &ipsecstat,
220 				sizeof(ipsecstat), kread_counters) != 0)
221 			return;
222 	}
223 
224 	xo_emit("{T:/%s}:\n", name);
225 
226 	print_ipsecstats(&ipsecstat);
227 }
228 
229 
230 static void print_ahstats(const struct ahstat *ahstat);
231 static void print_espstats(const struct espstat *espstat);
232 static void print_ipcompstats(const struct ipcompstat *ipcompstat);
233 
234 /*
235  * Dump IPSEC statistics structure.
236  */
237 static void
238 ipsec_hist_new(const uint64_t *hist, size_t histmax,
239     const struct val2str *name, const char *title, const char *cname)
240 {
241 	int first;
242 	size_t proto;
243 	const struct val2str *p;
244 
245 	first = 1;
246 	for (proto = 0; proto < histmax; proto++) {
247 		if (hist[proto] <= 0)
248 			continue;
249 		if (first) {
250 			xo_open_list(cname);
251 			xo_emit("\t{T:/%s histogram}:\n", title);
252 			first = 0;
253 		}
254 		xo_open_instance(cname);
255 		for (p = name; p && p->str; p++) {
256 			if (p->val == (int)proto)
257 				break;
258 		}
259 		if (p && p->str) {
260 			xo_emit("\t\t{k:name}: {:count/%ju}\n", p->str,
261 			    (uintmax_t)hist[proto]);
262 		} else {
263 			xo_emit("\t\t#{k:name/%lu}: {:count/%ju}\n",
264 			    (unsigned long)proto, (uintmax_t)hist[proto]);
265 		}
266 		xo_close_instance(cname);
267 	}
268 	if (!first)
269 		xo_close_list(cname);
270 }
271 
272 static void
273 print_ahstats(const struct ahstat *ahstat)
274 {
275 	xo_open_container("ah-statictics");
276 
277 #define	p(f, n, m) if (ahstat->f || sflag <= 1) \
278 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",	\
279 	    (uintmax_t)ahstat->f, plural(ahstat->f))
280 #define	hist(f, n, t, c) \
281 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c))
282 
283 	p(ahs_hdrops, "dropped-short-header",
284 	    "packet%s shorter than header shows");
285 	p(ahs_nopf, "dropped-bad-protocol",
286 	    "packet%s dropped; protocol family not supported");
287 	p(ahs_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
288 	p(ahs_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
289 	p(ahs_qfull, "dropped-queue-full", "packet%s dropped; queue full");
290 	p(ahs_noxform, "dropped-no-transform",
291 	    "packet%s dropped; no transform");
292 	p(ahs_wrap, "replay-counter-wraps", "replay counter wrap%s");
293 	p(ahs_badauth, "dropped-bad-auth",
294 	    "packet%s dropped; bad authentication detected");
295 	p(ahs_badauthl, "dropped-bad-auth-level",
296 	    "packet%s dropped; bad authentication length");
297 	p(ahs_replay, "possile-replay-detected",
298 	    "possible replay packet%s detected");
299 	p(ahs_input, "received-packets", "packet%s in");
300 	p(ahs_output, "send-packets", "packet%s out");
301 	p(ahs_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
302 	p(ahs_ibytes, "received-bytes", "byte%s in");
303 	p(ahs_obytes, "send-bytes", "byte%s out");
304 	p(ahs_toobig, "dropped-too-large",
305 	    "packet%s dropped; larger than IP_MAXPACKET");
306 	p(ahs_pdrops, "dropped-policy-violation",
307 	    "packet%s blocked due to policy");
308 	p(ahs_crypto, "crypto-failures", "crypto processing failure%s");
309 	p(ahs_tunnel, "tunnel-failures", "tunnel sanity check failure%s");
310 	hist(ahstat->ahs_hist, ipsec_ahnames,
311 	    "AH output", "ah-output-histogram");
312 
313 #undef p
314 #undef hist
315 	xo_close_container("ah-statictics");
316 }
317 
318 void
319 ah_stats(u_long off, const char *name, int family __unused, int proto __unused)
320 {
321 	struct ahstat ahstat;
322 
323 	if (fetch_stats("net.inet.ah.stats", off, &ahstat,
324 	    sizeof(ahstat), kread_counters) != 0)
325 		return;
326 
327 	xo_emit("{T:/%s}:\n", name);
328 
329 	print_ahstats(&ahstat);
330 }
331 
332 static void
333 print_espstats(const struct espstat *espstat)
334 {
335 	xo_open_container("esp-statictics");
336 #define	p(f, n, m) if (espstat->f || sflag <= 1)	\
337 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",		\
338 	    (uintmax_t)espstat->f, plural(espstat->f))
339 #define	hist(f, n, t, c) \
340 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c));
341 
342 	p(esps_hdrops, "dropped-short-header",
343 	    "packet%s shorter than header shows");
344 	p(esps_nopf, "dropped-bad-protocol",
345 	    "packet%s dropped; protocol family not supported");
346 	p(esps_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
347 	p(esps_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
348 	p(esps_qfull, "dropped-queue-full", "packet%s dropped; queue full");
349 	p(esps_noxform, "dropped-no-transform",
350 	    "packet%s dropped; no transform");
351 	p(esps_badilen, "dropped-bad-length", "packet%s dropped; bad ilen");
352 	p(esps_wrap, "replay-counter-wraps", "replay counter wrap%s");
353 	p(esps_badenc, "dropped-bad-crypto",
354 	    "packet%s dropped; bad encryption detected");
355 	p(esps_badauth, "dropped-bad-auth",
356 	    "packet%s dropped; bad authentication detected");
357 	p(esps_replay, "possible-replay-detected",
358 	    "possible replay packet%s detected");
359 	p(esps_input, "received-packets", "packet%s in");
360 	p(esps_output, "sent-packets", "packet%s out");
361 	p(esps_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
362 	p(esps_ibytes, "receieve-bytes", "byte%s in");
363 	p(esps_obytes, "sent-bytes", "byte%s out");
364 	p(esps_toobig, "dropped-too-large",
365 	    "packet%s dropped; larger than IP_MAXPACKET");
366 	p(esps_pdrops, "dropped-policy-violation",
367 	    "packet%s blocked due to policy");
368 	p(esps_crypto, "crypto-failures", "crypto processing failure%s");
369 	p(esps_tunnel, "tunnel-failures", "tunnel sanity check failure%s");
370 	hist(espstat->esps_hist, ipsec_espnames,
371 	    "ESP output", "esp-output-histogram");
372 
373 #undef p
374 #undef hist
375 	xo_close_container("esp-statictics");
376 }
377 
378 void
379 esp_stats(u_long off, const char *name, int family __unused, int proto __unused)
380 {
381 	struct espstat espstat;
382 
383 	if (fetch_stats("net.inet.esp.stats", off, &espstat,
384 	    sizeof(espstat), kread_counters) != 0)
385 		return;
386 
387 	xo_emit("{T:/%s}:\n", name);
388 
389 	print_espstats(&espstat);
390 }
391 
392 static void
393 print_ipcompstats(const struct ipcompstat *ipcompstat)
394 {
395 	xo_open_container("ipcomp-statictics");
396 
397 #define	p(f, n, m) if (ipcompstat->f || sflag <= 1)	\
398 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",		\
399 	    (uintmax_t)ipcompstat->f, plural(ipcompstat->f))
400 #define	hist(f, n, t, c) \
401 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c));
402 
403 	p(ipcomps_hdrops, "dropped-short-header",
404 	    "packet%s shorter than header shows");
405 	p(ipcomps_nopf, "dropped-bad-protocol",
406 	    "packet%s dropped; protocol family not supported");
407 	p(ipcomps_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
408 	p(ipcomps_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
409 	p(ipcomps_qfull, "dropped-queue-full", "packet%s dropped; queue full");
410 	p(ipcomps_noxform, "dropped-no-transform",
411 	    "packet%s dropped; no transform");
412 	p(ipcomps_wrap, "replay-counter-wraps", "replay counter wrap%s");
413 	p(ipcomps_input, "receieve-packets", "packet%s in");
414 	p(ipcomps_output, "sent-packets", "packet%s out");
415 	p(ipcomps_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
416 	p(ipcomps_ibytes, "receieved-bytes", "byte%s in");
417 	p(ipcomps_obytes, "sent-bytes", "byte%s out");
418 	p(ipcomps_toobig, "dropped-too-large",
419 	    "packet%s dropped; larger than IP_MAXPACKET");
420 	p(ipcomps_pdrops, "dropped-policy-violation",
421 	    "packet%s blocked due to policy");
422 	p(ipcomps_crypto, "crypto-failure", "crypto processing failure%s");
423 	hist(ipcompstat->ipcomps_hist, ipsec_compnames,
424 	    "COMP output", "comp-output-histogram");
425 	p(ipcomps_threshold, "sent-uncompressed-small-packets",
426 	    "packet%s sent uncompressed; size < compr. algo. threshold");
427 	p(ipcomps_uncompr, "sent-uncompressed-useless-packets",
428 	    "packet%s sent uncompressed; compression was useless");
429 
430 #undef p
431 #undef hist
432 	xo_close_container("ipcomp-statictics");
433 }
434 
435 void
436 ipcomp_stats(u_long off, const char *name, int family __unused,
437     int proto __unused)
438 {
439 	struct ipcompstat ipcompstat;
440 
441 	if (fetch_stats("net.inet.ipcomp.stats", off, &ipcompstat,
442 	    sizeof(ipcompstat), kread_counters) != 0)
443 		return;
444 
445 	xo_emit("{T:/%s}:\n", name);
446 
447 	print_ipcompstats(&ipcompstat);
448 }
449 
450 #endif /*IPSEC*/
451