xref: /freebsd/usr.bin/netstat/ipsec.c (revision ade9ccfe211ac8b27c58a4dc11a6cd1d33767ce0)
122a4e209SHajimu UMEMOTO /*	$KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $	*/
232cd1d96SJun-ichiro itojun Hagino 
365475bc8SDavid E. O'Brien /*-
4100b98dbSKelly Yancey  * Copyright (c) 2005 NTT Multimedia Communications Laboratories, Inc.
5100b98dbSKelly Yancey  * All rights reserved.
6100b98dbSKelly Yancey  *
7100b98dbSKelly Yancey  * Redistribution and use in source and binary forms, with or without
8100b98dbSKelly Yancey  * modification, are permitted provided that the following conditions
9100b98dbSKelly Yancey  * are met:
10100b98dbSKelly Yancey  * 1. Redistributions of source code must retain the above copyright
11100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer.
12100b98dbSKelly Yancey  * 2. Redistributions in binary form must reproduce the above copyright
13100b98dbSKelly Yancey  *    notice, this list of conditions and the following disclaimer in the
14100b98dbSKelly Yancey  *    documentation and/or other materials provided with the distribution.
15100b98dbSKelly Yancey  *
16100b98dbSKelly Yancey  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17100b98dbSKelly Yancey  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18100b98dbSKelly Yancey  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19100b98dbSKelly Yancey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20100b98dbSKelly Yancey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21100b98dbSKelly Yancey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22100b98dbSKelly Yancey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23100b98dbSKelly Yancey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24100b98dbSKelly Yancey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25100b98dbSKelly Yancey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26100b98dbSKelly Yancey  * SUCH DAMAGE.
27100b98dbSKelly Yancey  */
2865475bc8SDavid E. O'Brien /*-
2932cd1d96SJun-ichiro itojun Hagino  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3032cd1d96SJun-ichiro itojun Hagino  * All rights reserved.
3132cd1d96SJun-ichiro itojun Hagino  *
3232cd1d96SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
3332cd1d96SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
3432cd1d96SJun-ichiro itojun Hagino  * are met:
3532cd1d96SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
3632cd1d96SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
3732cd1d96SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
3832cd1d96SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
3932cd1d96SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
4032cd1d96SJun-ichiro itojun Hagino  * 3. Neither the name of the project nor the names of its contributors
4132cd1d96SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
4232cd1d96SJun-ichiro itojun Hagino  *    without specific prior written permission.
4332cd1d96SJun-ichiro itojun Hagino  *
4432cd1d96SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
4532cd1d96SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4632cd1d96SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4732cd1d96SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
4832cd1d96SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4932cd1d96SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5032cd1d96SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5132cd1d96SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5232cd1d96SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5332cd1d96SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5432cd1d96SJun-ichiro itojun Hagino  * SUCH DAMAGE.
5532cd1d96SJun-ichiro itojun Hagino  */
5665475bc8SDavid E. O'Brien /*-
5732cd1d96SJun-ichiro itojun Hagino  * Copyright (c) 1983, 1988, 1993
5832cd1d96SJun-ichiro itojun Hagino  *	The Regents of the University of California.  All rights reserved.
5932cd1d96SJun-ichiro itojun Hagino  *
6032cd1d96SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
6132cd1d96SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
6232cd1d96SJun-ichiro itojun Hagino  * are met:
6332cd1d96SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
6432cd1d96SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
6532cd1d96SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
6632cd1d96SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
6732cd1d96SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
6832cd1d96SJun-ichiro itojun Hagino  * 4. Neither the name of the University nor the names of its contributors
6932cd1d96SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
7032cd1d96SJun-ichiro itojun Hagino  *    without specific prior written permission.
7132cd1d96SJun-ichiro itojun Hagino  *
7232cd1d96SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7332cd1d96SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7432cd1d96SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7532cd1d96SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
7632cd1d96SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7732cd1d96SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7832cd1d96SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7932cd1d96SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8032cd1d96SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8132cd1d96SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8232cd1d96SJun-ichiro itojun Hagino  * SUCH DAMAGE.
8332cd1d96SJun-ichiro itojun Hagino  */
8432cd1d96SJun-ichiro itojun Hagino 
855d8a878aSHajimu UMEMOTO #if 0
866cc6f122SPhilippe Charnier #ifndef lint
876cc6f122SPhilippe Charnier static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
8832cd1d96SJun-ichiro itojun Hagino #endif /* not lint */
896cc6f122SPhilippe Charnier #endif
906cc6f122SPhilippe Charnier 
916cc6f122SPhilippe Charnier #include <sys/cdefs.h>
926cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
9332cd1d96SJun-ichiro itojun Hagino 
9432cd1d96SJun-ichiro itojun Hagino #include <sys/param.h>
9532cd1d96SJun-ichiro itojun Hagino #include <sys/queue.h>
9632cd1d96SJun-ichiro itojun Hagino #include <sys/socket.h>
97feda1a43SJohn Baldwin #include <sys/socketvar.h>
9832cd1d96SJun-ichiro itojun Hagino 
9932cd1d96SJun-ichiro itojun Hagino #include <netinet/in.h>
10032cd1d96SJun-ichiro itojun Hagino 
1018409aedfSGeorge V. Neville-Neil #ifdef IPSEC
102100b98dbSKelly Yancey #include <netipsec/ipsec.h>
103100b98dbSKelly Yancey #include <netipsec/ah_var.h>
104100b98dbSKelly Yancey #include <netipsec/esp_var.h>
105100b98dbSKelly Yancey #include <netipsec/ipcomp_var.h>
10632cd1d96SJun-ichiro itojun Hagino #endif
10732cd1d96SJun-ichiro itojun Hagino 
1087b95a1ebSYaroslav Tykhiy #include <stdint.h>
10932cd1d96SJun-ichiro itojun Hagino #include <stdio.h>
110*ade9ccfeSMarcel Moolenaar #include <stdbool.h>
111821df508SXin LI #include <string.h>
112821df508SXin LI #include <unistd.h>
113*ade9ccfeSMarcel Moolenaar #include <libxo/xo.h>
11432cd1d96SJun-ichiro itojun Hagino #include "netstat.h"
11532cd1d96SJun-ichiro itojun Hagino 
11632cd1d96SJun-ichiro itojun Hagino #ifdef IPSEC
11733841545SHajimu UMEMOTO struct val2str {
11833841545SHajimu UMEMOTO 	int val;
11933841545SHajimu UMEMOTO 	const char *str;
12032cd1d96SJun-ichiro itojun Hagino };
12132cd1d96SJun-ichiro itojun Hagino 
12233841545SHajimu UMEMOTO static struct val2str ipsec_ahnames[] = {
12333841545SHajimu UMEMOTO 	{ SADB_AALG_NONE, "none", },
12433841545SHajimu UMEMOTO 	{ SADB_AALG_MD5HMAC, "hmac-md5", },
12533841545SHajimu UMEMOTO 	{ SADB_AALG_SHA1HMAC, "hmac-sha1", },
12633841545SHajimu UMEMOTO 	{ SADB_X_AALG_MD5, "md5", },
12733841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA, "sha", },
12833841545SHajimu UMEMOTO 	{ SADB_X_AALG_NULL, "null", },
12933841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_256
13033841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
13133841545SHajimu UMEMOTO #endif
13233841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_384
13333841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
13433841545SHajimu UMEMOTO #endif
13533841545SHajimu UMEMOTO #ifdef SADB_X_AALG_SHA2_512
13633841545SHajimu UMEMOTO 	{ SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
13733841545SHajimu UMEMOTO #endif
13822a4e209SHajimu UMEMOTO #ifdef SADB_X_AALG_RIPEMD160HMAC
13922a4e209SHajimu UMEMOTO 	{ SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
14022a4e209SHajimu UMEMOTO #endif
14122a4e209SHajimu UMEMOTO #ifdef SADB_X_AALG_AES_XCBC_MAC
14222a4e209SHajimu UMEMOTO 	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
14322a4e209SHajimu UMEMOTO #endif
14433841545SHajimu UMEMOTO 	{ -1, NULL },
14532cd1d96SJun-ichiro itojun Hagino };
14632cd1d96SJun-ichiro itojun Hagino 
14733841545SHajimu UMEMOTO static struct val2str ipsec_espnames[] = {
14833841545SHajimu UMEMOTO 	{ SADB_EALG_NONE, "none", },
14933841545SHajimu UMEMOTO 	{ SADB_EALG_DESCBC, "des-cbc", },
15033841545SHajimu UMEMOTO 	{ SADB_EALG_3DESCBC, "3des-cbc", },
15133841545SHajimu UMEMOTO 	{ SADB_EALG_NULL, "null", },
15233841545SHajimu UMEMOTO 	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
15333841545SHajimu UMEMOTO 	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
15433841545SHajimu UMEMOTO #ifdef SADB_X_EALG_RIJNDAELCBC
15533841545SHajimu UMEMOTO 	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
15633841545SHajimu UMEMOTO #endif
15722a4e209SHajimu UMEMOTO #ifdef SADB_X_EALG_AESCTR
15822a4e209SHajimu UMEMOTO 	{ SADB_X_EALG_AESCTR, "aes-ctr", },
15922a4e209SHajimu UMEMOTO #endif
16033841545SHajimu UMEMOTO 	{ -1, NULL },
16133841545SHajimu UMEMOTO };
16233841545SHajimu UMEMOTO 
16333841545SHajimu UMEMOTO static struct val2str ipsec_compnames[] = {
16433841545SHajimu UMEMOTO 	{ SADB_X_CALG_NONE, "none", },
16533841545SHajimu UMEMOTO 	{ SADB_X_CALG_OUI, "oui", },
16633841545SHajimu UMEMOTO 	{ SADB_X_CALG_DEFLATE, "deflate", },
16733841545SHajimu UMEMOTO 	{ SADB_X_CALG_LZS, "lzs", },
16833841545SHajimu UMEMOTO 	{ -1, NULL },
16932cd1d96SJun-ichiro itojun Hagino };
17032cd1d96SJun-ichiro itojun Hagino 
171100b98dbSKelly Yancey static void print_ipsecstats(const struct ipsecstat *ipsecstat);
17232cd1d96SJun-ichiro itojun Hagino 
17332cd1d96SJun-ichiro itojun Hagino static void
174100b98dbSKelly Yancey print_ipsecstats(const struct ipsecstat *ipsecstat)
17532cd1d96SJun-ichiro itojun Hagino {
176*ade9ccfeSMarcel Moolenaar 	xo_open_container("ipsec-statistics");
177*ade9ccfeSMarcel Moolenaar 
178100b98dbSKelly Yancey #define	p(f, m) if (ipsecstat->f || sflag <= 1) \
179*ade9ccfeSMarcel Moolenaar 	xo_emit(m, (uintmax_t)ipsecstat->f, plural(ipsecstat->f))
180*ade9ccfeSMarcel Moolenaar 
181*ade9ccfeSMarcel Moolenaar 	p(ips_in_polvio, "\t{:dropped-policy-violation/%ju} "
182*ade9ccfeSMarcel Moolenaar 	    "{N:/inbound packet%s violated process security policy}\n");
183*ade9ccfeSMarcel Moolenaar 	p(ips_in_nomem, "\t{:dropped-no-memory/%ju} "
184*ade9ccfeSMarcel Moolenaar 	    "{N:/inbound packet%s failed due to insufficient memory}\n");
185*ade9ccfeSMarcel Moolenaar 	p(ips_in_inval, "\t{:dropped-invalid/%ju} "
186*ade9ccfeSMarcel Moolenaar 	    "{N:/invalid inbound packet%s}\n");
187*ade9ccfeSMarcel Moolenaar 	p(ips_out_polvio, "\t{:discarded-policy-violation/%ju} "
188*ade9ccfeSMarcel Moolenaar 	    "{N:/outbound packet%s violated process security policy}\n");
189*ade9ccfeSMarcel Moolenaar 	p(ips_out_nosa, "\t{:discarded-no-sa/%ju} "
190*ade9ccfeSMarcel Moolenaar 	    "{N:/outbound packet%s with no SA available}\n");
191*ade9ccfeSMarcel Moolenaar 	p(ips_out_nomem, "\t{:discarded-no-memory/%ju} "
192*ade9ccfeSMarcel Moolenaar 	    "{N:/outbound packet%s failed due to insufficient memory}\n");
193*ade9ccfeSMarcel Moolenaar 	p(ips_out_noroute, "\t{:discarded-no-route/%ju} "
194*ade9ccfeSMarcel Moolenaar 	    "{N:/outbound packet%s with no route available}\n");
195*ade9ccfeSMarcel Moolenaar 	p(ips_out_inval, "\t{:discarded-invalid/%ju} "
196*ade9ccfeSMarcel Moolenaar 	    "{N:/invalid outbound packet%s}\n");
197*ade9ccfeSMarcel Moolenaar 	p(ips_out_bundlesa, "\t{:send-bundled-sa/%ju} "
198*ade9ccfeSMarcel Moolenaar 	    "{N:/outbound packet%s with bundled SAs}\n");
199*ade9ccfeSMarcel Moolenaar 	p(ips_mbcoalesced, "\t{:mbufs-coalesced-during-clone/%ju} "
200*ade9ccfeSMarcel Moolenaar 	    "{N:/mbuf%s coalesced during clone}\n");
201*ade9ccfeSMarcel Moolenaar 	p(ips_clcoalesced, "\t{:clusters-coalesced-during-clone/%ju} "
202*ade9ccfeSMarcel Moolenaar 	    "{N:/cluster%s coalesced during clone}\n");
203*ade9ccfeSMarcel Moolenaar 	p(ips_clcopied, "\t{:clusters-copied-during-clone/%ju} "
204*ade9ccfeSMarcel Moolenaar 	    "{N:/cluster%s copied during clone}\n");
205*ade9ccfeSMarcel Moolenaar 	p(ips_mbinserted, "\t{:mbufs-inserted/%ju} "
206*ade9ccfeSMarcel Moolenaar 	    "{N:/mbuf%s inserted during makespace}\n");
2078409aedfSGeorge V. Neville-Neil #undef p
208*ade9ccfeSMarcel Moolenaar 	xo_close_container("ipsec-statistics");
20932cd1d96SJun-ichiro itojun Hagino }
21032cd1d96SJun-ichiro itojun Hagino 
21132cd1d96SJun-ichiro itojun Hagino void
212feda1a43SJohn Baldwin ipsec_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
21332cd1d96SJun-ichiro itojun Hagino {
214100b98dbSKelly Yancey 	struct ipsecstat ipsecstat;
215100b98dbSKelly Yancey 
21632cd1d96SJun-ichiro itojun Hagino 	if (off == 0)
21732cd1d96SJun-ichiro itojun Hagino 		return;
218*ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
219db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&ipsecstat, sizeof(ipsecstat));
22032cd1d96SJun-ichiro itojun Hagino 
221100b98dbSKelly Yancey 	print_ipsecstats(&ipsecstat);
22232cd1d96SJun-ichiro itojun Hagino }
22332cd1d96SJun-ichiro itojun Hagino 
22432cd1d96SJun-ichiro itojun Hagino 
225100b98dbSKelly Yancey static void print_ahstats(const struct ahstat *ahstat);
226100b98dbSKelly Yancey static void print_espstats(const struct espstat *espstat);
227100b98dbSKelly Yancey static void print_ipcompstats(const struct ipcompstat *ipcompstat);
228100b98dbSKelly Yancey 
229100b98dbSKelly Yancey /*
230100b98dbSKelly Yancey  * Dump IPSEC statistics structure.
231100b98dbSKelly Yancey  */
232100b98dbSKelly Yancey static void
233c80211e3SAndrey V. Elsukov ipsec_hist_new(const uint64_t *hist, size_t histmax,
234*ade9ccfeSMarcel Moolenaar     const struct val2str *name, const char *title, const char *cname)
235100b98dbSKelly Yancey {
236100b98dbSKelly Yancey 	int first;
237100b98dbSKelly Yancey 	size_t proto;
238100b98dbSKelly Yancey 	const struct val2str *p;
239100b98dbSKelly Yancey 
240100b98dbSKelly Yancey 	first = 1;
241100b98dbSKelly Yancey 	for (proto = 0; proto < histmax; proto++) {
242100b98dbSKelly Yancey 		if (hist[proto] <= 0)
243100b98dbSKelly Yancey 			continue;
244100b98dbSKelly Yancey 		if (first) {
245*ade9ccfeSMarcel Moolenaar 			xo_open_list(cname);
246*ade9ccfeSMarcel Moolenaar 			xo_emit("\t{T:/%s histogram}:\n", title);
247100b98dbSKelly Yancey 			first = 0;
248100b98dbSKelly Yancey 		}
249*ade9ccfeSMarcel Moolenaar 		xo_open_instance(cname);
250100b98dbSKelly Yancey 		for (p = name; p && p->str; p++) {
251100b98dbSKelly Yancey 			if (p->val == (int)proto)
252100b98dbSKelly Yancey 				break;
253100b98dbSKelly Yancey 		}
254100b98dbSKelly Yancey 		if (p && p->str) {
255*ade9ccfeSMarcel Moolenaar 			xo_emit("\t\t{k:name}: {:count/%ju}\n", p->str,
256c80211e3SAndrey V. Elsukov 			    (uintmax_t)hist[proto]);
257100b98dbSKelly Yancey 		} else {
258*ade9ccfeSMarcel Moolenaar 			xo_emit("\t\t#{k:name/%lu}: {:count/%ju}\n",
259*ade9ccfeSMarcel Moolenaar 			    (unsigned long)proto, (uintmax_t)hist[proto]);
260100b98dbSKelly Yancey 		}
261*ade9ccfeSMarcel Moolenaar 		xo_close_instance(cname);
262100b98dbSKelly Yancey 	}
263*ade9ccfeSMarcel Moolenaar 	if (!first)
264*ade9ccfeSMarcel Moolenaar 		xo_close_list(cname);
265100b98dbSKelly Yancey }
266100b98dbSKelly Yancey 
267100b98dbSKelly Yancey static void
268100b98dbSKelly Yancey print_ahstats(const struct ahstat *ahstat)
269100b98dbSKelly Yancey {
270*ade9ccfeSMarcel Moolenaar 	xo_open_container("ah-statictics");
27132cd1d96SJun-ichiro itojun Hagino 
272*ade9ccfeSMarcel Moolenaar #define	p(f, n, m) if (ahstat->f || sflag <= 1) \
273*ade9ccfeSMarcel Moolenaar 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",	\
274*ade9ccfeSMarcel Moolenaar 	    (uintmax_t)ahstat->f, plural(ahstat->f))
275*ade9ccfeSMarcel Moolenaar #define	hist(f, n, t, c) \
276*ade9ccfeSMarcel Moolenaar 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c))
277*ade9ccfeSMarcel Moolenaar 
278*ade9ccfeSMarcel Moolenaar 	p(ahs_hdrops, "dropped-short-header",
279*ade9ccfeSMarcel Moolenaar 	    "packet%s shorter than header shows");
280*ade9ccfeSMarcel Moolenaar 	p(ahs_nopf, "dropped-bad-protocol",
281*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; protocol family not supported");
282*ade9ccfeSMarcel Moolenaar 	p(ahs_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
283*ade9ccfeSMarcel Moolenaar 	p(ahs_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
284*ade9ccfeSMarcel Moolenaar 	p(ahs_qfull, "dropped-queue-full", "packet%s dropped; queue full");
285*ade9ccfeSMarcel Moolenaar 	p(ahs_noxform, "dropped-no-transform",
286*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; no transform");
287*ade9ccfeSMarcel Moolenaar 	p(ahs_wrap, "replay-counter-wraps", "replay counter wrap%s");
288*ade9ccfeSMarcel Moolenaar 	p(ahs_badauth, "dropped-bad-auth",
289*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; bad authentication detected");
290*ade9ccfeSMarcel Moolenaar 	p(ahs_badauthl, "dropped-bad-auth-level",
291*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; bad authentication length");
292*ade9ccfeSMarcel Moolenaar 	p(ahs_replay, "possile-replay-detected",
293*ade9ccfeSMarcel Moolenaar 	    "possible replay packet%s detected");
294*ade9ccfeSMarcel Moolenaar 	p(ahs_input, "received-packets", "packet%s in");
295*ade9ccfeSMarcel Moolenaar 	p(ahs_output, "send-packets", "packet%s out");
296*ade9ccfeSMarcel Moolenaar 	p(ahs_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
297*ade9ccfeSMarcel Moolenaar 	p(ahs_ibytes, "received-bytes", "byte%s in");
298*ade9ccfeSMarcel Moolenaar 	p(ahs_obytes, "send-bytes", "byte%s out");
299*ade9ccfeSMarcel Moolenaar 	p(ahs_toobig, "dropped-too-large",
300*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; larger than IP_MAXPACKET");
301*ade9ccfeSMarcel Moolenaar 	p(ahs_pdrops, "dropped-policy-violation",
302*ade9ccfeSMarcel Moolenaar 	    "packet%s blocked due to policy");
303*ade9ccfeSMarcel Moolenaar 	p(ahs_crypto, "crypto-failures", "crypto processing failure%s");
304*ade9ccfeSMarcel Moolenaar 	p(ahs_tunnel, "tunnel-failures", "tunnel sanity check failure%s");
305*ade9ccfeSMarcel Moolenaar 	hist(ahstat->ahs_hist, ipsec_ahnames,
306*ade9ccfeSMarcel Moolenaar 	    "AH output", "ah-output-histogram");
307100b98dbSKelly Yancey 
308c80211e3SAndrey V. Elsukov #undef p
309100b98dbSKelly Yancey #undef hist
310*ade9ccfeSMarcel Moolenaar 	xo_close_container("ah-statictics");
31132cd1d96SJun-ichiro itojun Hagino }
312100b98dbSKelly Yancey 
313100b98dbSKelly Yancey void
314feda1a43SJohn Baldwin ah_stats(u_long off, const char *name, int family __unused, int proto __unused)
315100b98dbSKelly Yancey {
316100b98dbSKelly Yancey 	struct ahstat ahstat;
317100b98dbSKelly Yancey 
318100b98dbSKelly Yancey 	if (off == 0)
319100b98dbSKelly Yancey 		return;
320*ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
321db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&ahstat, sizeof(ahstat));
322100b98dbSKelly Yancey 
323100b98dbSKelly Yancey 	print_ahstats(&ahstat);
32432cd1d96SJun-ichiro itojun Hagino }
325100b98dbSKelly Yancey 
326100b98dbSKelly Yancey static void
327100b98dbSKelly Yancey print_espstats(const struct espstat *espstat)
328100b98dbSKelly Yancey {
329*ade9ccfeSMarcel Moolenaar 	xo_open_container("esp-statictics");
330*ade9ccfeSMarcel Moolenaar #define	p(f, n, m) if (espstat->f || sflag <= 1)	\
331*ade9ccfeSMarcel Moolenaar 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",		\
332*ade9ccfeSMarcel Moolenaar 	    (uintmax_t)espstat->f, plural(espstat->f))
333*ade9ccfeSMarcel Moolenaar #define	hist(f, n, t, c) \
334*ade9ccfeSMarcel Moolenaar 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c));
335100b98dbSKelly Yancey 
336*ade9ccfeSMarcel Moolenaar 	p(esps_hdrops, "dropped-short-header",
337*ade9ccfeSMarcel Moolenaar 	    "packet%s shorter than header shows");
338*ade9ccfeSMarcel Moolenaar 	p(esps_nopf, "dropped-bad-protocol",
339*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; protocol family not supported");
340*ade9ccfeSMarcel Moolenaar 	p(esps_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
341*ade9ccfeSMarcel Moolenaar 	p(esps_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
342*ade9ccfeSMarcel Moolenaar 	p(esps_qfull, "dropped-queue-full", "packet%s dropped; queue full");
343*ade9ccfeSMarcel Moolenaar 	p(esps_noxform, "dropped-no-transform",
344*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; no transform");
345*ade9ccfeSMarcel Moolenaar 	p(esps_badilen, "dropped-bad-length", "packet%s dropped; bad ilen");
346*ade9ccfeSMarcel Moolenaar 	p(esps_wrap, "replay-counter-wraps", "replay counter wrap%s");
347*ade9ccfeSMarcel Moolenaar 	p(esps_badenc, "dropped-bad-crypto",
348*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; bad encryption detected");
349*ade9ccfeSMarcel Moolenaar 	p(esps_badauth, "dropped-bad-auth",
350*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; bad authentication detected");
351*ade9ccfeSMarcel Moolenaar 	p(esps_replay, "possible-replay-detected",
352*ade9ccfeSMarcel Moolenaar 	    "possible replay packet%s detected");
353*ade9ccfeSMarcel Moolenaar 	p(esps_input, "received-packets", "packet%s in");
354*ade9ccfeSMarcel Moolenaar 	p(esps_output, "sent-packets", "packet%s out");
355*ade9ccfeSMarcel Moolenaar 	p(esps_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
356*ade9ccfeSMarcel Moolenaar 	p(esps_ibytes, "receieve-bytes", "byte%s in");
357*ade9ccfeSMarcel Moolenaar 	p(esps_obytes, "sent-bytes", "byte%s out");
358*ade9ccfeSMarcel Moolenaar 	p(esps_toobig, "dropped-too-large",
359*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; larger than IP_MAXPACKET");
360*ade9ccfeSMarcel Moolenaar 	p(esps_pdrops, "dropped-policy-violation",
361*ade9ccfeSMarcel Moolenaar 	    "packet%s blocked due to policy");
362*ade9ccfeSMarcel Moolenaar 	p(esps_crypto, "crypto-failures", "crypto processing failure%s");
363*ade9ccfeSMarcel Moolenaar 	p(esps_tunnel, "tunnel-failures", "tunnel sanity check failure%s");
364*ade9ccfeSMarcel Moolenaar 	hist(espstat->esps_hist, ipsec_espnames,
365*ade9ccfeSMarcel Moolenaar 	    "ESP output", "esp-output-histogram");
366100b98dbSKelly Yancey 
367c80211e3SAndrey V. Elsukov #undef p
368100b98dbSKelly Yancey #undef hist
369*ade9ccfeSMarcel Moolenaar 	xo_close_container("esp-statictics");
37032cd1d96SJun-ichiro itojun Hagino }
371100b98dbSKelly Yancey 
372100b98dbSKelly Yancey void
373feda1a43SJohn Baldwin esp_stats(u_long off, const char *name, int family __unused, int proto __unused)
374100b98dbSKelly Yancey {
375100b98dbSKelly Yancey 	struct espstat espstat;
376100b98dbSKelly Yancey 
377100b98dbSKelly Yancey 	if (off == 0)
378100b98dbSKelly Yancey 		return;
379*ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
380db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&espstat, sizeof(espstat));
381100b98dbSKelly Yancey 
382100b98dbSKelly Yancey 	print_espstats(&espstat);
383100b98dbSKelly Yancey }
384100b98dbSKelly Yancey 
385100b98dbSKelly Yancey static void
386100b98dbSKelly Yancey print_ipcompstats(const struct ipcompstat *ipcompstat)
387100b98dbSKelly Yancey {
388*ade9ccfeSMarcel Moolenaar 	xo_open_container("ipcomp-statictics");
389100b98dbSKelly Yancey 
390*ade9ccfeSMarcel Moolenaar #define	p(f, n, m) if (ipcompstat->f || sflag <= 1)	\
391*ade9ccfeSMarcel Moolenaar 	xo_emit("\t{:" n "/%ju} {N:/" m "}\n",		\
392*ade9ccfeSMarcel Moolenaar 	    (uintmax_t)ipcompstat->f, plural(ipcompstat->f))
393*ade9ccfeSMarcel Moolenaar #define	hist(f, n, t, c) \
394*ade9ccfeSMarcel Moolenaar 	ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t), (c));
395*ade9ccfeSMarcel Moolenaar 
396*ade9ccfeSMarcel Moolenaar 	p(ipcomps_hdrops, "dropped-short-header",
397*ade9ccfeSMarcel Moolenaar 	    "packet%s shorter than header shows");
398*ade9ccfeSMarcel Moolenaar 	p(ipcomps_nopf, "dropped-bad-protocol",
399*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; protocol family not supported");
400*ade9ccfeSMarcel Moolenaar 	p(ipcomps_notdb, "dropped-no-tdb", "packet%s dropped; no TDB");
401*ade9ccfeSMarcel Moolenaar 	p(ipcomps_badkcr, "dropped-bad-kcr", "packet%s dropped; bad KCR");
402*ade9ccfeSMarcel Moolenaar 	p(ipcomps_qfull, "dropped-queue-full", "packet%s dropped; queue full");
403*ade9ccfeSMarcel Moolenaar 	p(ipcomps_noxform, "dropped-no-transform",
404*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; no transform");
405*ade9ccfeSMarcel Moolenaar 	p(ipcomps_wrap, "replay-counter-wraps", "replay counter wrap%s");
406*ade9ccfeSMarcel Moolenaar 	p(ipcomps_input, "receieve-packets", "packet%s in");
407*ade9ccfeSMarcel Moolenaar 	p(ipcomps_output, "sent-packets", "packet%s out");
408*ade9ccfeSMarcel Moolenaar 	p(ipcomps_invalid, "dropped-bad-tdb", "packet%s dropped; invalid TDB");
409*ade9ccfeSMarcel Moolenaar 	p(ipcomps_ibytes, "receieved-bytes", "byte%s in");
410*ade9ccfeSMarcel Moolenaar 	p(ipcomps_obytes, "sent-bytes", "byte%s out");
411*ade9ccfeSMarcel Moolenaar 	p(ipcomps_toobig, "dropped-too-large",
412*ade9ccfeSMarcel Moolenaar 	    "packet%s dropped; larger than IP_MAXPACKET");
413*ade9ccfeSMarcel Moolenaar 	p(ipcomps_pdrops, "dropped-policy-violation",
414*ade9ccfeSMarcel Moolenaar 	    "packet%s blocked due to policy");
415*ade9ccfeSMarcel Moolenaar 	p(ipcomps_crypto, "crypto-failure", "crypto processing failure%s");
416*ade9ccfeSMarcel Moolenaar 	hist(ipcompstat->ipcomps_hist, ipsec_compnames,
417*ade9ccfeSMarcel Moolenaar 	    "COMP output", "comp-output-histogram");
418*ade9ccfeSMarcel Moolenaar 	p(ipcomps_threshold, "sent-uncompressed-small-packets",
419*ade9ccfeSMarcel Moolenaar 	    "packet%s sent uncompressed; size < compr. algo. threshold");
420*ade9ccfeSMarcel Moolenaar 	p(ipcomps_uncompr, "sent-uncompressed-useless-packets",
421*ade9ccfeSMarcel Moolenaar 	    "packet%s sent uncompressed; compression was useless");
422100b98dbSKelly Yancey 
423c80211e3SAndrey V. Elsukov #undef p
424100b98dbSKelly Yancey #undef hist
425*ade9ccfeSMarcel Moolenaar 	xo_close_container("ipcomp-statictics");
426100b98dbSKelly Yancey }
427100b98dbSKelly Yancey 
428100b98dbSKelly Yancey void
429feda1a43SJohn Baldwin ipcomp_stats(u_long off, const char *name, int family __unused,
430feda1a43SJohn Baldwin     int proto __unused)
431100b98dbSKelly Yancey {
432100b98dbSKelly Yancey 	struct ipcompstat ipcompstat;
433100b98dbSKelly Yancey 
434100b98dbSKelly Yancey 	if (off == 0)
435100b98dbSKelly Yancey 		return;
436*ade9ccfeSMarcel Moolenaar 	xo_emit("{T:/%s}:\n", name);
437db8c0879SAndrey V. Elsukov 	kread_counters(off, (char *)&ipcompstat, sizeof(ipcompstat));
438100b98dbSKelly Yancey 
439100b98dbSKelly Yancey 	print_ipcompstats(&ipcompstat);
440100b98dbSKelly Yancey }
441100b98dbSKelly Yancey 
44232cd1d96SJun-ichiro itojun Hagino #endif /*IPSEC*/
443