1125481Sume/*	$KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $	*/
262584Sitojun
3175061Sobrien/*-
4153809Skbyanc * Copyright (c) 2005 NTT Multimedia Communications Laboratories, Inc.
5153809Skbyanc * All rights reserved.
6175061Sobrien *
7153809Skbyanc * Redistribution and use in source and binary forms, with or without
8153809Skbyanc * modification, are permitted provided that the following conditions
9153809Skbyanc * are met:
10153809Skbyanc * 1. Redistributions of source code must retain the above copyright
11153809Skbyanc *    notice, this list of conditions and the following disclaimer.
12153809Skbyanc * 2. Redistributions in binary form must reproduce the above copyright
13153809Skbyanc *    notice, this list of conditions and the following disclaimer in the
14153809Skbyanc *    documentation and/or other materials provided with the distribution.
15175061Sobrien *
16153809Skbyanc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17153809Skbyanc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18153809Skbyanc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19153809Skbyanc * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20153809Skbyanc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21153809Skbyanc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22153809Skbyanc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23153809Skbyanc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24153809Skbyanc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25153809Skbyanc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26153809Skbyanc * SUCH DAMAGE.
27153809Skbyanc */
28175061Sobrien/*-
2962584Sitojun * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
3062584Sitojun * All rights reserved.
31175061Sobrien *
3262584Sitojun * Redistribution and use in source and binary forms, with or without
3362584Sitojun * modification, are permitted provided that the following conditions
3462584Sitojun * are met:
3562584Sitojun * 1. Redistributions of source code must retain the above copyright
3662584Sitojun *    notice, this list of conditions and the following disclaimer.
3762584Sitojun * 2. Redistributions in binary form must reproduce the above copyright
3862584Sitojun *    notice, this list of conditions and the following disclaimer in the
3962584Sitojun *    documentation and/or other materials provided with the distribution.
4062584Sitojun * 3. Neither the name of the project nor the names of its contributors
4162584Sitojun *    may be used to endorse or promote products derived from this software
4262584Sitojun *    without specific prior written permission.
43175061Sobrien *
4462584Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
4562584Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4662584Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4762584Sitojun * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
4862584Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4962584Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5062584Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5162584Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5262584Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5362584Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5462584Sitojun * SUCH DAMAGE.
5562584Sitojun */
56175061Sobrien/*-
5762584Sitojun * Copyright (c) 1983, 1988, 1993
5862584Sitojun *	The Regents of the University of California.  All rights reserved.
5962584Sitojun *
6062584Sitojun * Redistribution and use in source and binary forms, with or without
6162584Sitojun * modification, are permitted provided that the following conditions
6262584Sitojun * are met:
6362584Sitojun * 1. Redistributions of source code must retain the above copyright
6462584Sitojun *    notice, this list of conditions and the following disclaimer.
6562584Sitojun * 2. Redistributions in binary form must reproduce the above copyright
6662584Sitojun *    notice, this list of conditions and the following disclaimer in the
6762584Sitojun *    documentation and/or other materials provided with the distribution.
6862584Sitojun * 4. Neither the name of the University nor the names of its contributors
6962584Sitojun *    may be used to endorse or promote products derived from this software
7062584Sitojun *    without specific prior written permission.
7162584Sitojun *
7262584Sitojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
7362584Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7462584Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7562584Sitojun * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
7662584Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7762584Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7862584Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7962584Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8062584Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8162584Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8262584Sitojun * SUCH DAMAGE.
8362584Sitojun */
8462584Sitojun
85132671Scharnier#if 0
8662584Sitojun#ifndef lint
87132671Scharnierstatic char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
88132671Scharnier#endif /* not lint */
89125479Sume#endif
9062584Sitojun
91132671Scharnier#include <sys/cdefs.h>
92132671Scharnier__FBSDID("$FreeBSD$");
93132671Scharnier
9462584Sitojun#include <sys/param.h>
9562584Sitojun#include <sys/queue.h>
9662584Sitojun#include <sys/socket.h>
97171465Sjhb#include <sys/socketvar.h>
9862584Sitojun
9962584Sitojun#include <netinet/in.h>
10062584Sitojun
101171135Sgnn#ifdef IPSEC
102153809Skbyanc#include <netipsec/ipsec.h>
103153809Skbyanc#include <netipsec/ah_var.h>
104153809Skbyanc#include <netipsec/esp_var.h>
105153809Skbyanc#include <netipsec/ipcomp_var.h>
106153809Skbyanc#endif
107153809Skbyanc
108160787Syar#include <stdint.h>
10962584Sitojun#include <stdio.h>
110200462Sdelphij#include <string.h>
111200462Sdelphij#include <unistd.h>
11262584Sitojun#include "netstat.h"
11362584Sitojun
114153809Skbyanc#ifdef IPSEC
11578064Sumestruct val2str {
11678064Sume	int val;
11778064Sume	const char *str;
11862584Sitojun};
11962584Sitojun
12078064Sumestatic struct val2str ipsec_ahnames[] = {
12178064Sume	{ SADB_AALG_NONE, "none", },
12278064Sume	{ SADB_AALG_MD5HMAC, "hmac-md5", },
12378064Sume	{ SADB_AALG_SHA1HMAC, "hmac-sha1", },
12478064Sume	{ SADB_X_AALG_MD5, "md5", },
12578064Sume	{ SADB_X_AALG_SHA, "sha", },
12678064Sume	{ SADB_X_AALG_NULL, "null", },
12778064Sume#ifdef SADB_X_AALG_SHA2_256
12878064Sume	{ SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
12978064Sume#endif
13078064Sume#ifdef SADB_X_AALG_SHA2_384
13178064Sume	{ SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
13278064Sume#endif
13378064Sume#ifdef SADB_X_AALG_SHA2_512
13478064Sume	{ SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
13578064Sume#endif
136125481Sume#ifdef SADB_X_AALG_RIPEMD160HMAC
137125481Sume	{ SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
138125481Sume#endif
139125481Sume#ifdef SADB_X_AALG_AES_XCBC_MAC
140125481Sume	{ SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
141125481Sume#endif
14278064Sume	{ -1, NULL },
14362584Sitojun};
14462584Sitojun
14578064Sumestatic struct val2str ipsec_espnames[] = {
14678064Sume	{ SADB_EALG_NONE, "none", },
14778064Sume	{ SADB_EALG_DESCBC, "des-cbc", },
14878064Sume	{ SADB_EALG_3DESCBC, "3des-cbc", },
14978064Sume	{ SADB_EALG_NULL, "null", },
15078064Sume	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
15178064Sume	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
15278064Sume#ifdef SADB_X_EALG_RIJNDAELCBC
15378064Sume	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
15478064Sume#endif
155125481Sume#ifdef SADB_X_EALG_AESCTR
156125481Sume	{ SADB_X_EALG_AESCTR, "aes-ctr", },
157125481Sume#endif
15878064Sume	{ -1, NULL },
15962584Sitojun};
16062584Sitojun
16178064Sumestatic struct val2str ipsec_compnames[] = {
16278064Sume	{ SADB_X_CALG_NONE, "none", },
16378064Sume	{ SADB_X_CALG_OUI, "oui", },
16478064Sume	{ SADB_X_CALG_DEFLATE, "deflate", },
16578064Sume	{ SADB_X_CALG_LZS, "lzs", },
16678064Sume	{ -1, NULL },
16778064Sume};
16878064Sume
169153809Skbyancstatic void ipsec_hist(const u_quad_t *hist, size_t histmax,
170153809Skbyanc		       const struct val2str *name, const char *title);
171153809Skbyancstatic void print_ipsecstats(const struct ipsecstat *ipsecstat);
17262584Sitojun
17362584Sitojun
17462584Sitojun/*
17562584Sitojun * Dump IPSEC statistics structure.
17662584Sitojun */
17762584Sitojunstatic void
178125479Sumeipsec_hist(const u_quad_t *hist, size_t histmax, const struct val2str *name,
17978314Sassar	   const char *title)
18062584Sitojun{
18162584Sitojun	int first;
18262584Sitojun	size_t proto;
18378064Sume	const struct val2str *p;
18462584Sitojun
18578064Sume	first = 1;
18678064Sume	for (proto = 0; proto < histmax; proto++) {
18762584Sitojun		if (hist[proto] <= 0)
18862584Sitojun			continue;
18962584Sitojun		if (first) {
19062584Sitojun			printf("\t%s histogram:\n", title);
19162584Sitojun			first = 0;
19262584Sitojun		}
19378064Sume		for (p = name; p && p->str; p++) {
19478316Sassar			if (p->val == (int)proto)
19578064Sume				break;
19678064Sume		}
19778064Sume		if (p && p->str) {
198160787Syar			printf("\t\t%s: %ju\n", p->str, (uintmax_t)hist[proto]);
19962584Sitojun		} else {
200160787Syar			printf("\t\t#%ld: %ju\n", (long)proto,
201160787Syar			    (uintmax_t)hist[proto]);
20262584Sitojun		}
20362584Sitojun	}
20462584Sitojun}
20562584Sitojun
20662584Sitojunstatic void
207153809Skbyancprint_ipsecstats(const struct ipsecstat *ipsecstat)
20862584Sitojun{
209153809Skbyanc#define	p(f, m) if (ipsecstat->f || sflag <= 1) \
210160787Syar    printf(m, (uintmax_t)ipsecstat->f, plural(ipsecstat->f))
211153809Skbyanc#define	pes(f, m) if (ipsecstat->f || sflag <= 1) \
212160787Syar    printf(m, (uintmax_t)ipsecstat->f, plurales(ipsecstat->f))
213175061Sobrien#define	hist(f, n, t) \
21478316Sassar    ipsec_hist((f), sizeof(f)/sizeof(f[0]), (n), (t));
21562584Sitojun
216160787Syar	p(in_success, "\t%ju inbound packet%s processed successfully\n");
217160787Syar	p(in_polvio, "\t%ju inbound packet%s violated process security "
218125479Sume	    "policy\n");
219160787Syar	p(in_nosa, "\t%ju inbound packet%s with no SA available\n");
220160787Syar	p(in_inval, "\t%ju invalid inbound packet%s\n");
221160787Syar	p(in_nomem, "\t%ju inbound packet%s failed due to insufficient memory\n");
222160787Syar	p(in_badspi, "\t%ju inbound packet%s failed getting SPI\n");
223160787Syar	p(in_ahreplay, "\t%ju inbound packet%s failed on AH replay check\n");
224160787Syar	p(in_espreplay, "\t%ju inbound packet%s failed on ESP replay check\n");
225160787Syar	p(in_ahauthsucc, "\t%ju inbound packet%s considered authentic\n");
226160787Syar	p(in_ahauthfail, "\t%ju inbound packet%s failed on authentication\n");
227153809Skbyanc	hist(ipsecstat->in_ahhist, ipsec_ahnames, "AH input");
228153809Skbyanc	hist(ipsecstat->in_esphist, ipsec_espnames, "ESP input");
229153809Skbyanc	hist(ipsecstat->in_comphist, ipsec_compnames, "IPComp input");
23062584Sitojun
231160787Syar	p(out_success, "\t%ju outbound packet%s processed successfully\n");
232160787Syar	p(out_polvio, "\t%ju outbound packet%s violated process security "
233125479Sume	    "policy\n");
234160787Syar	p(out_nosa, "\t%ju outbound packet%s with no SA available\n");
235160787Syar	p(out_inval, "\t%ju invalid outbound packet%s\n");
236160787Syar	p(out_nomem, "\t%ju outbound packet%s failed due to insufficient memory\n");
237160787Syar	p(out_noroute, "\t%ju outbound packet%s with no route\n");
238153809Skbyanc	hist(ipsecstat->out_ahhist, ipsec_ahnames, "AH output");
239153809Skbyanc	hist(ipsecstat->out_esphist, ipsec_espnames, "ESP output");
240153809Skbyanc	hist(ipsecstat->out_comphist, ipsec_compnames, "IPComp output");
241160787Syar	p(spdcachelookup, "\t%ju SPD cache lookup%s\n");
242160787Syar	pes(spdcachemiss, "\t%ju SPD cache miss%s\n");
243125480Sume#undef pes
24462584Sitojun#undef hist
245171135Sgnn	p(ips_in_polvio, "\t%ju inbound packet%s violated process "
246171135Sgnn		"security policy\n");
247171135Sgnn	p(ips_out_polvio, "\t%ju outbound packet%s violated process "
248171135Sgnn		"security policy\n");
249171135Sgnn	p(ips_out_nosa, "\t%ju outbound packet%s with no SA available\n");
250171135Sgnn	p(ips_out_nomem, "\t%ju outbound packet%s failed due to "
251171135Sgnn		"insufficient memory\n");
252171135Sgnn	p(ips_out_noroute, "\t%ju outbound packet%s with no route "
253171135Sgnn		"available\n");
254171135Sgnn	p(ips_out_inval, "\t%ju invalid outbound packet%s\n");
255171135Sgnn	p(ips_out_bundlesa, "\t%ju outbound packet%s with bundled SAs\n");
256171135Sgnn	p(ips_mbcoalesced, "\t%ju mbuf%s coalesced during clone\n");
257171135Sgnn	p(ips_clcoalesced, "\t%ju cluster%s coalesced during clone\n");
258171135Sgnn	p(ips_clcopied, "\t%ju cluster%s copied during clone\n");
259171135Sgnn	p(ips_mbinserted, "\t%ju mbuf%s inserted during makespace\n");
260171135Sgnn#undef p
26162584Sitojun}
26262584Sitojun
26362584Sitojunvoid
264171465Sjhbipsec_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
26562584Sitojun{
266153809Skbyanc	struct ipsecstat ipsecstat;
267153809Skbyanc
26862584Sitojun	if (off == 0)
26962584Sitojun		return;
27062584Sitojun	printf ("%s:\n", name);
271153809Skbyanc	kread(off, (char *)&ipsecstat, sizeof(ipsecstat));
27262584Sitojun
273153809Skbyanc	print_ipsecstats(&ipsecstat);
27462584Sitojun}
27562584Sitojun
276153809Skbyanc
277153809Skbyancstatic void ipsec_hist_new(const u_int32_t *hist, size_t histmax,
278153809Skbyanc			   const struct val2str *name, const char *title);
279153809Skbyancstatic void print_ahstats(const struct ahstat *ahstat);
280153809Skbyancstatic void print_espstats(const struct espstat *espstat);
281153809Skbyancstatic void print_ipcompstats(const struct ipcompstat *ipcompstat);
282153809Skbyanc
283153809Skbyanc/*
284153809Skbyanc * Dump IPSEC statistics structure.
285153809Skbyanc */
286153809Skbyancstatic void
287153809Skbyancipsec_hist_new(const u_int32_t *hist, size_t histmax,
288153809Skbyanc	       const struct val2str *name, const char *title)
28962584Sitojun{
290153809Skbyanc	int first;
291153809Skbyanc	size_t proto;
292153809Skbyanc	const struct val2str *p;
29362584Sitojun
294153809Skbyanc	first = 1;
295153809Skbyanc	for (proto = 0; proto < histmax; proto++) {
296153809Skbyanc		if (hist[proto] <= 0)
297153809Skbyanc			continue;
298153809Skbyanc		if (first) {
299153809Skbyanc			printf("\t%s histogram:\n", title);
300153809Skbyanc			first = 0;
301153809Skbyanc		}
302153809Skbyanc		for (p = name; p && p->str; p++) {
303153809Skbyanc			if (p->val == (int)proto)
304153809Skbyanc				break;
305153809Skbyanc		}
306153809Skbyanc		if (p && p->str) {
307153809Skbyanc			printf("\t\t%s: %u\n", p->str, hist[proto]);
308153809Skbyanc		} else {
309153809Skbyanc			printf("\t\t#%lu: %u\n", (unsigned long)proto,
310153809Skbyanc			       hist[proto]);
311153809Skbyanc		}
312153809Skbyanc	}
31362584Sitojun}
314175061Sobrien
315153809Skbyancstatic void
316153809Skbyancprint_ahstats(const struct ahstat *ahstat)
317153809Skbyanc{
318153809Skbyanc#define	p32(f, m) if (ahstat->f || sflag <= 1) \
319153809Skbyanc    printf("\t%u" m, (unsigned int)ahstat->f, plural(ahstat->f))
320153809Skbyanc#define	p64(f, m) if (ahstat->f || sflag <= 1) \
321160787Syar    printf("\t%ju" m, (uintmax_t)ahstat->f, plural(ahstat->f))
322175061Sobrien#define	hist(f, n, t) \
323153809Skbyanc    ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t));
324153809Skbyanc
325153809Skbyanc	p32(ahs_hdrops, " packet%s shorter than header shows\n");
326153809Skbyanc	p32(ahs_nopf, " packet%s dropped; protocol family not supported\n");
327153809Skbyanc	p32(ahs_notdb, " packet%s dropped; no TDB\n");
328153809Skbyanc	p32(ahs_badkcr, " packet%s dropped; bad KCR\n");
329153809Skbyanc	p32(ahs_qfull, " packet%s dropped; queue full\n");
330153809Skbyanc	p32(ahs_noxform, " packet%s dropped; no transform\n");
331153809Skbyanc	p32(ahs_wrap, " replay counter wrap%s\n");
332153809Skbyanc	p32(ahs_badauth, " packet%s dropped; bad authentication detected\n");
333153809Skbyanc	p32(ahs_badauthl, " packet%s dropped; bad authentication length\n");
334153809Skbyanc	p32(ahs_replay, " possible replay packet%s detected\n");
335153809Skbyanc	p32(ahs_input, " packet%s in\n");
336153809Skbyanc	p32(ahs_output, " packet%s out\n");
337153809Skbyanc	p32(ahs_invalid, " packet%s dropped; invalid TDB\n");
338153809Skbyanc	p64(ahs_ibytes, " byte%s in\n");
339153809Skbyanc	p64(ahs_obytes, " byte%s out\n");
340153809Skbyanc	p32(ahs_toobig, " packet%s dropped; larger than IP_MAXPACKET\n");
341153809Skbyanc	p32(ahs_pdrops, " packet%s blocked due to policy\n");
342153809Skbyanc	p32(ahs_crypto, " crypto processing failure%s\n");
343153809Skbyanc	p32(ahs_tunnel, " tunnel sanity check failure%s\n");
344153809Skbyanc	hist(ahstat->ahs_hist, ipsec_ahnames, "AH output");
345153809Skbyanc
346153809Skbyanc#undef p32
347153809Skbyanc#undef p64
348153809Skbyanc#undef hist
349153809Skbyanc}
350153809Skbyanc
351153809Skbyancvoid
352171465Sjhbah_stats(u_long off, const char *name, int family __unused, int proto __unused)
353153809Skbyanc{
354153809Skbyanc	struct ahstat ahstat;
355153809Skbyanc
356153809Skbyanc	if (off == 0)
357153809Skbyanc		return;
35862584Sitojun	printf ("%s:\n", name);
359153809Skbyanc	kread(off, (char *)&ahstat, sizeof(ahstat));
36062584Sitojun
361153809Skbyanc	print_ahstats(&ahstat);
362153809Skbyanc}
36362584Sitojun
364153809Skbyancstatic void
365153809Skbyancprint_espstats(const struct espstat *espstat)
366153809Skbyanc{
367153809Skbyanc#define	p32(f, m) if (espstat->f || sflag <= 1) \
368153809Skbyanc    printf("\t%u" m, (unsigned int)espstat->f, plural(espstat->f))
369153809Skbyanc#define	p64(f, m) if (espstat->f || sflag <= 1) \
370160787Syar    printf("\t%ju" m, (uintmax_t)espstat->f, plural(espstat->f))
371175061Sobrien#define	hist(f, n, t) \
372153809Skbyanc    ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t));
37362584Sitojun
374153809Skbyanc	p32(esps_hdrops, " packet%s shorter than header shows\n");
375153809Skbyanc	p32(esps_nopf, " packet%s dropped; protocol family not supported\n");
376153809Skbyanc	p32(esps_notdb, " packet%s dropped; no TDB\n");
377153809Skbyanc	p32(esps_badkcr, " packet%s dropped; bad KCR\n");
378153809Skbyanc	p32(esps_qfull, " packet%s dropped; queue full\n");
379153809Skbyanc	p32(esps_noxform, " packet%s dropped; no transform\n");
380153809Skbyanc	p32(esps_badilen, " packet%s dropped; bad ilen\n");
381153809Skbyanc	p32(esps_wrap, " replay counter wrap%s\n");
382153809Skbyanc	p32(esps_badenc, " packet%s dropped; bad encryption detected\n");
383153809Skbyanc	p32(esps_badauth, " packet%s dropped; bad authentication detected\n");
384153809Skbyanc	p32(esps_replay, " possible replay packet%s detected\n");
385153809Skbyanc	p32(esps_input, " packet%s in\n");
386153809Skbyanc	p32(esps_output, " packet%s out\n");
387153809Skbyanc	p32(esps_invalid, " packet%s dropped; invalid TDB\n");
388153809Skbyanc	p64(esps_ibytes, " byte%s in\n");
389153809Skbyanc	p64(esps_obytes, " byte%s out\n");
390153809Skbyanc	p32(esps_toobig, " packet%s dropped; larger than IP_MAXPACKET\n");
391153809Skbyanc	p32(esps_pdrops, " packet%s blocked due to policy\n");
392153809Skbyanc	p32(esps_crypto, " crypto processing failure%s\n");
393153809Skbyanc	p32(esps_tunnel, " tunnel sanity check failure%s\n");
394153809Skbyanc	hist(espstat->esps_hist, ipsec_espnames, "ESP output");
395153809Skbyanc
396153809Skbyanc#undef p32
397153809Skbyanc#undef p64
398153809Skbyanc#undef hist
39962584Sitojun}
400153809Skbyanc
401153809Skbyancvoid
402171465Sjhbesp_stats(u_long off, const char *name, int family __unused, int proto __unused)
403153809Skbyanc{
404153809Skbyanc	struct espstat espstat;
405153809Skbyanc
406153809Skbyanc	if (off == 0)
407153809Skbyanc		return;
408153809Skbyanc	printf ("%s:\n", name);
409153809Skbyanc	kread(off, (char *)&espstat, sizeof(espstat));
410153809Skbyanc
411153809Skbyanc	print_espstats(&espstat);
412153809Skbyanc}
413153809Skbyanc
414153809Skbyancstatic void
415153809Skbyancprint_ipcompstats(const struct ipcompstat *ipcompstat)
416153809Skbyanc{
417199946Sbz	uint32_t version;
418153809Skbyanc#define	p32(f, m) if (ipcompstat->f || sflag <= 1) \
419153809Skbyanc    printf("\t%u" m, (unsigned int)ipcompstat->f, plural(ipcompstat->f))
420153809Skbyanc#define	p64(f, m) if (ipcompstat->f || sflag <= 1) \
421160787Syar    printf("\t%ju" m, (uintmax_t)ipcompstat->f, plural(ipcompstat->f))
422175061Sobrien#define	hist(f, n, t) \
423153809Skbyanc    ipsec_hist_new((f), sizeof(f)/sizeof(f[0]), (n), (t));
424153809Skbyanc
425199946Sbz#ifndef IPCOMPSTAT_VERSION
426199946Sbz	version = 0;
427199946Sbz#else
428199946Sbz	version = ipcompstat->version;
429199946Sbz#endif
430153809Skbyanc	p32(ipcomps_hdrops, " packet%s shorter than header shows\n");
431153809Skbyanc	p32(ipcomps_nopf, " packet%s dropped; protocol family not supported\n");
432153809Skbyanc	p32(ipcomps_notdb, " packet%s dropped; no TDB\n");
433153809Skbyanc	p32(ipcomps_badkcr, " packet%s dropped; bad KCR\n");
434153809Skbyanc	p32(ipcomps_qfull, " packet%s dropped; queue full\n");
435153809Skbyanc	p32(ipcomps_noxform, " packet%s dropped; no transform\n");
436153809Skbyanc	p32(ipcomps_wrap, " replay counter wrap%s\n");
437153809Skbyanc	p32(ipcomps_input, " packet%s in\n");
438153809Skbyanc	p32(ipcomps_output, " packet%s out\n");
439153809Skbyanc	p32(ipcomps_invalid, " packet%s dropped; invalid TDB\n");
440153809Skbyanc	p64(ipcomps_ibytes, " byte%s in\n");
441153809Skbyanc	p64(ipcomps_obytes, " byte%s out\n");
442153809Skbyanc	p32(ipcomps_toobig, " packet%s dropped; larger than IP_MAXPACKET\n");
443153809Skbyanc	p32(ipcomps_pdrops, " packet%s blocked due to policy\n");
444153809Skbyanc	p32(ipcomps_crypto, " crypto processing failure%s\n");
445153809Skbyanc	hist(ipcompstat->ipcomps_hist, ipsec_compnames, "COMP output");
446199946Sbz	if (version >= 1) {
447199946Sbz	p32(ipcomps_threshold, " packet%s sent uncompressed; size < compr. algo. threshold\n");
448199946Sbz	p32(ipcomps_uncompr, " packet%s sent uncompressed; compression was useless\n");
449199946Sbz	}
450153809Skbyanc
451153809Skbyanc#undef p32
452153809Skbyanc#undef p64
453153809Skbyanc#undef hist
454153809Skbyanc}
455153809Skbyanc
456153809Skbyancvoid
457171465Sjhbipcomp_stats(u_long off, const char *name, int family __unused,
458171465Sjhb    int proto __unused)
459153809Skbyanc{
460153809Skbyanc	struct ipcompstat ipcompstat;
461153809Skbyanc
462153809Skbyanc	if (off == 0)
463153809Skbyanc		return;
464153809Skbyanc	printf ("%s:\n", name);
465153809Skbyanc	kread(off, (char *)&ipcompstat, sizeof(ipcompstat));
466153809Skbyanc
467153809Skbyanc	print_ipcompstats(&ipcompstat);
468153809Skbyanc}
469153809Skbyanc
47062584Sitojun#endif /*IPSEC*/
471