1112394Ssam/*-
2112394Ssam * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3112394Ssam * All rights reserved.
4112394Ssam *
5112394Ssam * Redistribution and use in source and binary forms, with or without
6112394Ssam * modification, are permitted provided that the following conditions
7112394Ssam * are met:
8112394Ssam * 1. Redistributions of source code must retain the above copyright
9112394Ssam *    notice, this list of conditions and the following disclaimer.
10112394Ssam * 2. Redistributions in binary form must reproduce the above copyright
11112394Ssam *    notice, this list of conditions and the following disclaimer in the
12112394Ssam *    documentation and/or other materials provided with the distribution.
13112394Ssam *
14112394Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15112394Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16112394Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17112394Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18112394Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19112394Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20112394Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21112394Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22112394Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23112394Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24112394Ssam * SUCH DAMAGE.
25112394Ssam *
26112394Ssam * $FreeBSD$
27112394Ssam */
28112394Ssam#include <sys/types.h>
29112394Ssam#include <netipsec/ipsec.h>
30112394Ssam#include <netipsec/ah_var.h>
31112394Ssam#include <netipsec/esp_var.h>
32253767Sae#include <stdint.h>
33253767Sae#include <stdio.h>
34112394Ssam
35112394Ssamstruct alg {
36112394Ssam	int		a;
37112394Ssam	const char	*name;
38112394Ssam};
39112394Ssamstatic const struct alg aalgs[] = {
40112394Ssam	{ SADB_AALG_NONE,	"none", },
41112394Ssam	{ SADB_AALG_MD5HMAC,	"hmac-md5", },
42112394Ssam	{ SADB_AALG_SHA1HMAC,	"hmac-sha1", },
43112394Ssam	{ SADB_X_AALG_MD5,	"md5", },
44112394Ssam	{ SADB_X_AALG_SHA,	"sha", },
45112394Ssam	{ SADB_X_AALG_NULL,	"null", },
46112394Ssam	{ SADB_X_AALG_SHA2_256,	"hmac-sha2-256", },
47112394Ssam	{ SADB_X_AALG_SHA2_384,	"hmac-sha2-384", },
48112394Ssam	{ SADB_X_AALG_SHA2_512,	"hmac-sha2-512", },
49112394Ssam};
50112394Ssamstatic const struct alg espalgs[] = {
51112394Ssam	{ SADB_EALG_NONE,	"none", },
52112394Ssam	{ SADB_EALG_DESCBC,	"des-cbc", },
53112394Ssam	{ SADB_EALG_3DESCBC,	"3des-cbc", },
54112394Ssam	{ SADB_EALG_NULL,	"null", },
55112394Ssam	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
56112394Ssam	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
57112394Ssam	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
58112394Ssam};
59112394Ssamstatic const struct alg ipcompalgs[] = {
60112394Ssam	{ SADB_X_CALG_NONE,	"none", },
61112394Ssam	{ SADB_X_CALG_OUI,	"oui", },
62112394Ssam	{ SADB_X_CALG_DEFLATE,	"deflate", },
63112394Ssam	{ SADB_X_CALG_LZS,	"lzs", },
64112394Ssam};
65112394Ssam#define	N(a)	(sizeof(a)/sizeof(a[0]))
66112394Ssam
67112394Ssamstatic const char*
68112394Ssamalgname(int a, const struct alg algs[], int nalgs)
69112394Ssam{
70112394Ssam	static char buf[80];
71112394Ssam	int i;
72112394Ssam
73112394Ssam	for (i = 0; i < nalgs; i++)
74112394Ssam		if (algs[i].a == a)
75112394Ssam			return algs[i].name;
76112394Ssam	snprintf(buf, sizeof(buf), "alg#%u", a);
77112394Ssam	return buf;
78112394Ssam}
79112394Ssam
80112394Ssam/*
81112394Ssam * Little program to dump the statistics block for fast ipsec.
82112394Ssam */
83112394Ssamint
84112394Ssammain(int argc, char *argv[])
85112394Ssam{
86253081Sae#define	STAT(x,fmt)	if (x) printf(fmt "\n", (uintmax_t)x)
87172240Sgnn	struct ipsecstat ips;
88112394Ssam	struct ahstat ahs;
89112394Ssam	struct espstat esps;
90112394Ssam	size_t slen;
91112394Ssam	int i;
92112394Ssam
93112394Ssam	slen = sizeof (ips);
94112394Ssam	if (sysctlbyname("net.inet.ipsec.ipsecstats", &ips, &slen, NULL, NULL) < 0)
95112394Ssam		err(1, "net.inet.ipsec.ipsecstats");
96112394Ssam	slen = sizeof (ahs);
97112394Ssam	if (sysctlbyname("net.inet.ah.stats", &ahs, &slen, NULL, NULL) < 0)
98112394Ssam		err(1, "net.inet.ah.stats");
99112394Ssam	slen = sizeof (esps);
100112394Ssam	if (sysctlbyname("net.inet.esp.stats", &esps, &slen, NULL, NULL) < 0)
101112394Ssam		err(1, "net.inet.esp.stats");
102112394Ssam
103253081Sae#define	AHSTAT(x,fmt)	if (x) printf("ah " fmt ": %ju\n", (uintmax_t)x)
104112394Ssam	AHSTAT(ahs.ahs_input, "input packets processed");
105112394Ssam	AHSTAT(ahs.ahs_output, "output packets processed");
106112394Ssam	AHSTAT(ahs.ahs_hdrops, "headers too short");
107112394Ssam	AHSTAT(ahs.ahs_nopf, "headers for unsupported address family");
108112394Ssam	AHSTAT(ahs.ahs_notdb, "packets with no SA");
109112394Ssam	AHSTAT(ahs.ahs_badkcr, "packets with bad kcr");
110112394Ssam	AHSTAT(ahs.ahs_badauth, "packets with bad authentication");
111112394Ssam	AHSTAT(ahs.ahs_noxform, "packets with no xform");
112112394Ssam	AHSTAT(ahs.ahs_qfull, "packets dropped packet 'cuz queue full");
113112394Ssam	AHSTAT(ahs.ahs_wrap, "packets dropped for replace counter wrap");
114112394Ssam	AHSTAT(ahs.ahs_replay, "packets dropped for possible replay");
115112394Ssam	AHSTAT(ahs.ahs_badauthl, "packets dropped for bad authenticator length");
116112394Ssam	AHSTAT(ahs.ahs_invalid, "packets with an invalid SA");
117112394Ssam	AHSTAT(ahs.ahs_toobig, "packets too big");
118112394Ssam	AHSTAT(ahs.ahs_pdrops, "packets dropped due to policy");
119112394Ssam	AHSTAT(ahs.ahs_crypto, "failed crypto requests");
120112394Ssam	AHSTAT(ahs.ahs_tunnel, "tunnel sanity check failures");
121112394Ssam	for (i = 0; i < AH_ALG_MAX; i++)
122112394Ssam		if (ahs.ahs_hist[i])
123253081Sae			printf("ah packets with %s: %ju\n"
124112394Ssam				, algname(i, aalgs, N(aalgs))
125253081Sae				, (uintmax_t)ahs.ahs_hist[i]
126112394Ssam			);
127253081Sae	AHSTAT(ahs.ahs_ibytes, "bytes received");
128253081Sae	AHSTAT(ahs.ahs_obytes, "bytes transmitted");
129112394Ssam#undef AHSTAT
130112394Ssam
131253081Sae#define	ESPSTAT(x,fmt)	if (x) printf("esp " fmt ": %ju\n", (uintmax_t)x)
132112394Ssam	ESPSTAT(esps.esps_input, "input packets processed");
133112394Ssam	ESPSTAT(esps.esps_output, "output packets processed");
134112394Ssam	ESPSTAT(esps.esps_hdrops, "headers too short");
135112394Ssam	ESPSTAT(esps.esps_nopf, "headers for unsupported address family");
136112394Ssam	ESPSTAT(esps.esps_notdb, "packets with no SA");
137112394Ssam	ESPSTAT(esps.esps_badkcr, "packets with bad kcr");
138112394Ssam	ESPSTAT(esps.esps_qfull, "packets dropped packet 'cuz queue full");
139112394Ssam	ESPSTAT(esps.esps_noxform, "packets with no xform");
140112394Ssam	ESPSTAT(esps.esps_badilen, "packets with bad ilen");
141112394Ssam	ESPSTAT(esps.esps_badenc, "packets with bad encryption");
142112394Ssam	ESPSTAT(esps.esps_badauth, "packets with bad authentication");
143112394Ssam	ESPSTAT(esps.esps_wrap, "packets dropped for replay counter wrap");
144112394Ssam	ESPSTAT(esps.esps_replay, "packets dropped for possible replay");
145112394Ssam	ESPSTAT(esps.esps_invalid, "packets with an invalid SA");
146112394Ssam	ESPSTAT(esps.esps_toobig, "packets too big");
147112394Ssam	ESPSTAT(esps.esps_pdrops, "packets dropped due to policy");
148112394Ssam	ESPSTAT(esps.esps_crypto, "failed crypto requests");
149112394Ssam	ESPSTAT(esps.esps_tunnel, "tunnel sanity check failures");
150112394Ssam	for (i = 0; i < ESP_ALG_MAX; i++)
151112394Ssam		if (esps.esps_hist[i])
152253081Sae			printf("esp packets with %s: %ju\n"
153112394Ssam				, algname(i, espalgs, N(espalgs))
154253081Sae				, (uintmax_t)esps.esps_hist[i]
155112394Ssam			);
156253081Sae	ESPSTAT(esps.esps_ibytes, "bytes received");
157253081Sae	ESPSTAT(esps.esps_obytes, "bytes transmitted");
158112394Ssam#undef ESPSTAT
159112394Ssam
160112394Ssam	printf("\n");
161112394Ssam	if (ips.ips_in_polvio+ips.ips_out_polvio)
162253081Sae		printf("policy violations: input %ju output %ju\n",
163253081Sae		    (uintmax_t)ips.ips_in_polvio,
164253081Sae		    (uintmax_t)ips.ips_out_polvio);
165253081Sae	STAT(ips.ips_out_nosa, "no SA found %ju (output)");
166253081Sae	STAT(ips.ips_out_nomem, "no memory available %ju (output)");
167253081Sae	STAT(ips.ips_out_noroute, "no route available %ju (output)");
168253081Sae	STAT(ips.ips_out_inval, "generic error %ju (output)");
169253081Sae	STAT(ips.ips_out_bundlesa, "bundled SA processed %ju (output)");
170253081Sae	printf("m_clone processing: %ju mbufs + %ju clusters coalesced\n",
171253081Sae	    (uintmax_t)ips.ips_mbcoalesced, (uintmax_t)ips.ips_clcoalesced);
172253081Sae	STAT(ips.ips_clcopied, "m_clone processing: %ju clusters copied\n");
173253081Sae	STAT(ips.ips_mbinserted, "m_makespace: %ju mbufs inserted\n");
174253081Sae	printf("header position [front/middle/end]: %ju/%ju/%ju\n",
175253081Sae	    (uintmax_t)ips.ips_input_front, (uintmax_t)ips.ips_input_middle,
176253081Sae	    (uintmax_t)ips.ips_input_end);
177112394Ssam	return 0;
178112394Ssam}
179