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 */
28287297Srodrigc
29287297Srodrigc#include <sys/param.h>
30287297Srodrigc#include <sys/sysctl.h>
31287297Srodrigc
32112394Ssam#include <netipsec/ipsec.h>
33112394Ssam#include <netipsec/ah_var.h>
34112394Ssam#include <netipsec/esp_var.h>
35287297Srodrigc
36287297Srodrigc#include <err.h>
37253767Sae#include <stdint.h>
38253767Sae#include <stdio.h>
39112394Ssam
40112394Ssamstruct alg {
41112394Ssam	int		a;
42112394Ssam	const char	*name;
43112394Ssam};
44112394Ssamstatic const struct alg aalgs[] = {
45112394Ssam	{ SADB_AALG_NONE,	"none", },
46112394Ssam	{ SADB_AALG_MD5HMAC,	"hmac-md5", },
47112394Ssam	{ SADB_AALG_SHA1HMAC,	"hmac-sha1", },
48112394Ssam	{ SADB_X_AALG_MD5,	"md5", },
49112394Ssam	{ SADB_X_AALG_SHA,	"sha", },
50112394Ssam	{ SADB_X_AALG_NULL,	"null", },
51112394Ssam	{ SADB_X_AALG_SHA2_256,	"hmac-sha2-256", },
52112394Ssam	{ SADB_X_AALG_SHA2_384,	"hmac-sha2-384", },
53112394Ssam	{ SADB_X_AALG_SHA2_512,	"hmac-sha2-512", },
54112394Ssam};
55112394Ssamstatic const struct alg espalgs[] = {
56112394Ssam	{ SADB_EALG_NONE,	"none", },
57112394Ssam	{ SADB_EALG_DESCBC,	"des-cbc", },
58112394Ssam	{ SADB_EALG_3DESCBC,	"3des-cbc", },
59112394Ssam	{ SADB_EALG_NULL,	"null", },
60112394Ssam	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
61112394Ssam	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
62112394Ssam	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
63112394Ssam};
64112394Ssamstatic const struct alg ipcompalgs[] = {
65112394Ssam	{ SADB_X_CALG_NONE,	"none", },
66112394Ssam	{ SADB_X_CALG_OUI,	"oui", },
67112394Ssam	{ SADB_X_CALG_DEFLATE,	"deflate", },
68112394Ssam	{ SADB_X_CALG_LZS,	"lzs", },
69112394Ssam};
70112394Ssam
71112394Ssamstatic const char*
72112394Ssamalgname(int a, const struct alg algs[], int nalgs)
73112394Ssam{
74112394Ssam	static char buf[80];
75112394Ssam	int i;
76112394Ssam
77112394Ssam	for (i = 0; i < nalgs; i++)
78112394Ssam		if (algs[i].a == a)
79112394Ssam			return algs[i].name;
80112394Ssam	snprintf(buf, sizeof(buf), "alg#%u", a);
81112394Ssam	return buf;
82112394Ssam}
83112394Ssam
84112394Ssam/*
85112394Ssam * Little program to dump the statistics block for fast ipsec.
86112394Ssam */
87112394Ssamint
88112394Ssammain(int argc, char *argv[])
89112394Ssam{
90253081Sae#define	STAT(x,fmt)	if (x) printf(fmt "\n", (uintmax_t)x)
91172240Sgnn	struct ipsecstat ips;
92112394Ssam	struct ahstat ahs;
93112394Ssam	struct espstat esps;
94112394Ssam	size_t slen;
95112394Ssam	int i;
96112394Ssam
97112394Ssam	slen = sizeof (ips);
98287297Srodrigc	if (sysctlbyname("net.inet.ipsec.ipsecstats", &ips, &slen, NULL, 0) < 0)
99112394Ssam		err(1, "net.inet.ipsec.ipsecstats");
100112394Ssam	slen = sizeof (ahs);
101287297Srodrigc	if (sysctlbyname("net.inet.ah.stats", &ahs, &slen, NULL, 0) < 0)
102112394Ssam		err(1, "net.inet.ah.stats");
103112394Ssam	slen = sizeof (esps);
104287297Srodrigc	if (sysctlbyname("net.inet.esp.stats", &esps, &slen, NULL, 0) < 0)
105112394Ssam		err(1, "net.inet.esp.stats");
106112394Ssam
107253081Sae#define	AHSTAT(x,fmt)	if (x) printf("ah " fmt ": %ju\n", (uintmax_t)x)
108112394Ssam	AHSTAT(ahs.ahs_input, "input packets processed");
109112394Ssam	AHSTAT(ahs.ahs_output, "output packets processed");
110112394Ssam	AHSTAT(ahs.ahs_hdrops, "headers too short");
111112394Ssam	AHSTAT(ahs.ahs_nopf, "headers for unsupported address family");
112112394Ssam	AHSTAT(ahs.ahs_notdb, "packets with no SA");
113112394Ssam	AHSTAT(ahs.ahs_badkcr, "packets with bad kcr");
114112394Ssam	AHSTAT(ahs.ahs_badauth, "packets with bad authentication");
115112394Ssam	AHSTAT(ahs.ahs_noxform, "packets with no xform");
116112394Ssam	AHSTAT(ahs.ahs_qfull, "packets dropped packet 'cuz queue full");
117112394Ssam	AHSTAT(ahs.ahs_wrap, "packets dropped for replace counter wrap");
118112394Ssam	AHSTAT(ahs.ahs_replay, "packets dropped for possible replay");
119112394Ssam	AHSTAT(ahs.ahs_badauthl, "packets dropped for bad authenticator length");
120112394Ssam	AHSTAT(ahs.ahs_invalid, "packets with an invalid SA");
121112394Ssam	AHSTAT(ahs.ahs_toobig, "packets too big");
122112394Ssam	AHSTAT(ahs.ahs_pdrops, "packets dropped due to policy");
123112394Ssam	AHSTAT(ahs.ahs_crypto, "failed crypto requests");
124112394Ssam	AHSTAT(ahs.ahs_tunnel, "tunnel sanity check failures");
125112394Ssam	for (i = 0; i < AH_ALG_MAX; i++)
126112394Ssam		if (ahs.ahs_hist[i])
127253081Sae			printf("ah packets with %s: %ju\n"
128287297Srodrigc				, algname(i, aalgs, nitems(aalgs))
129253081Sae				, (uintmax_t)ahs.ahs_hist[i]
130112394Ssam			);
131253081Sae	AHSTAT(ahs.ahs_ibytes, "bytes received");
132253081Sae	AHSTAT(ahs.ahs_obytes, "bytes transmitted");
133112394Ssam#undef AHSTAT
134112394Ssam
135253081Sae#define	ESPSTAT(x,fmt)	if (x) printf("esp " fmt ": %ju\n", (uintmax_t)x)
136112394Ssam	ESPSTAT(esps.esps_input, "input packets processed");
137112394Ssam	ESPSTAT(esps.esps_output, "output packets processed");
138112394Ssam	ESPSTAT(esps.esps_hdrops, "headers too short");
139112394Ssam	ESPSTAT(esps.esps_nopf, "headers for unsupported address family");
140112394Ssam	ESPSTAT(esps.esps_notdb, "packets with no SA");
141112394Ssam	ESPSTAT(esps.esps_badkcr, "packets with bad kcr");
142112394Ssam	ESPSTAT(esps.esps_qfull, "packets dropped packet 'cuz queue full");
143112394Ssam	ESPSTAT(esps.esps_noxform, "packets with no xform");
144112394Ssam	ESPSTAT(esps.esps_badilen, "packets with bad ilen");
145112394Ssam	ESPSTAT(esps.esps_badenc, "packets with bad encryption");
146112394Ssam	ESPSTAT(esps.esps_badauth, "packets with bad authentication");
147112394Ssam	ESPSTAT(esps.esps_wrap, "packets dropped for replay counter wrap");
148112394Ssam	ESPSTAT(esps.esps_replay, "packets dropped for possible replay");
149112394Ssam	ESPSTAT(esps.esps_invalid, "packets with an invalid SA");
150112394Ssam	ESPSTAT(esps.esps_toobig, "packets too big");
151112394Ssam	ESPSTAT(esps.esps_pdrops, "packets dropped due to policy");
152112394Ssam	ESPSTAT(esps.esps_crypto, "failed crypto requests");
153112394Ssam	ESPSTAT(esps.esps_tunnel, "tunnel sanity check failures");
154112394Ssam	for (i = 0; i < ESP_ALG_MAX; i++)
155112394Ssam		if (esps.esps_hist[i])
156253081Sae			printf("esp packets with %s: %ju\n"
157287297Srodrigc				, algname(i, espalgs, nitems(espalgs))
158253081Sae				, (uintmax_t)esps.esps_hist[i]
159112394Ssam			);
160253081Sae	ESPSTAT(esps.esps_ibytes, "bytes received");
161253081Sae	ESPSTAT(esps.esps_obytes, "bytes transmitted");
162112394Ssam#undef ESPSTAT
163112394Ssam
164112394Ssam	printf("\n");
165112394Ssam	if (ips.ips_in_polvio+ips.ips_out_polvio)
166253081Sae		printf("policy violations: input %ju output %ju\n",
167253081Sae		    (uintmax_t)ips.ips_in_polvio,
168253081Sae		    (uintmax_t)ips.ips_out_polvio);
169253081Sae	STAT(ips.ips_out_nosa, "no SA found %ju (output)");
170253081Sae	STAT(ips.ips_out_nomem, "no memory available %ju (output)");
171253081Sae	STAT(ips.ips_out_noroute, "no route available %ju (output)");
172253081Sae	STAT(ips.ips_out_inval, "generic error %ju (output)");
173253081Sae	STAT(ips.ips_out_bundlesa, "bundled SA processed %ju (output)");
174253081Sae	printf("m_clone processing: %ju mbufs + %ju clusters coalesced\n",
175253081Sae	    (uintmax_t)ips.ips_mbcoalesced, (uintmax_t)ips.ips_clcoalesced);
176253081Sae	STAT(ips.ips_clcopied, "m_clone processing: %ju clusters copied\n");
177253081Sae	STAT(ips.ips_mbinserted, "m_makespace: %ju mbufs inserted\n");
178253081Sae	printf("header position [front/middle/end]: %ju/%ju/%ju\n",
179253081Sae	    (uintmax_t)ips.ips_input_front, (uintmax_t)ips.ips_input_middle,
180253081Sae	    (uintmax_t)ips.ips_input_end);
181112394Ssam	return 0;
182112394Ssam}
183