1/*-
2 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28#include <stdio.h>
29#include <sys/types.h>
30#include <netipsec/ipsec.h>
31#include <netipsec/ah_var.h>
32#include <netipsec/esp_var.h>
33
34struct alg {
35	int		a;
36	const char	*name;
37};
38static const struct alg aalgs[] = {
39	{ SADB_AALG_NONE,	"none", },
40	{ SADB_AALG_MD5HMAC,	"hmac-md5", },
41	{ SADB_AALG_SHA1HMAC,	"hmac-sha1", },
42	{ SADB_X_AALG_MD5,	"md5", },
43	{ SADB_X_AALG_SHA,	"sha", },
44	{ SADB_X_AALG_NULL,	"null", },
45	{ SADB_X_AALG_SHA2_256,	"hmac-sha2-256", },
46	{ SADB_X_AALG_SHA2_384,	"hmac-sha2-384", },
47	{ SADB_X_AALG_SHA2_512,	"hmac-sha2-512", },
48};
49static const struct alg espalgs[] = {
50	{ SADB_EALG_NONE,	"none", },
51	{ SADB_EALG_DESCBC,	"des-cbc", },
52	{ SADB_EALG_3DESCBC,	"3des-cbc", },
53	{ SADB_EALG_NULL,	"null", },
54	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
55	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
56	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
57};
58static const struct alg ipcompalgs[] = {
59	{ SADB_X_CALG_NONE,	"none", },
60	{ SADB_X_CALG_OUI,	"oui", },
61	{ SADB_X_CALG_DEFLATE,	"deflate", },
62	{ SADB_X_CALG_LZS,	"lzs", },
63};
64#define	N(a)	(sizeof(a)/sizeof(a[0]))
65
66static const char*
67algname(int a, const struct alg algs[], int nalgs)
68{
69	static char buf[80];
70	int i;
71
72	for (i = 0; i < nalgs; i++)
73		if (algs[i].a == a)
74			return algs[i].name;
75	snprintf(buf, sizeof(buf), "alg#%u", a);
76	return buf;
77}
78
79/*
80 * Little program to dump the statistics block for fast ipsec.
81 */
82int
83main(int argc, char *argv[])
84{
85#define	STAT(x,fmt)	if (x) printf(fmt "\n", x)
86	struct ipsecstat ips;
87	struct ahstat ahs;
88	struct espstat esps;
89	size_t slen;
90	int i;
91
92	slen = sizeof (ips);
93	if (sysctlbyname("net.inet.ipsec.ipsecstats", &ips, &slen, NULL, NULL) < 0)
94		err(1, "net.inet.ipsec.ipsecstats");
95	slen = sizeof (ahs);
96	if (sysctlbyname("net.inet.ah.stats", &ahs, &slen, NULL, NULL) < 0)
97		err(1, "net.inet.ah.stats");
98	slen = sizeof (esps);
99	if (sysctlbyname("net.inet.esp.stats", &esps, &slen, NULL, NULL) < 0)
100		err(1, "net.inet.esp.stats");
101
102#define	AHSTAT(x,fmt)	if (x) printf("ah " fmt ": %u\n", x)
103#define	AHSTAT64(x,fmt)	if (x) printf("ah " fmt ": %llu\n", x)
104	AHSTAT(ahs.ahs_input, "input packets processed");
105	AHSTAT(ahs.ahs_output, "output packets processed");
106	AHSTAT(ahs.ahs_hdrops, "headers too short");
107	AHSTAT(ahs.ahs_nopf, "headers for unsupported address family");
108	AHSTAT(ahs.ahs_notdb, "packets with no SA");
109	AHSTAT(ahs.ahs_badkcr, "packets with bad kcr");
110	AHSTAT(ahs.ahs_badauth, "packets with bad authentication");
111	AHSTAT(ahs.ahs_noxform, "packets with no xform");
112	AHSTAT(ahs.ahs_qfull, "packets dropped packet 'cuz queue full");
113	AHSTAT(ahs.ahs_wrap, "packets dropped for replace counter wrap");
114	AHSTAT(ahs.ahs_replay, "packets dropped for possible replay");
115	AHSTAT(ahs.ahs_badauthl, "packets dropped for bad authenticator length");
116	AHSTAT(ahs.ahs_invalid, "packets with an invalid SA");
117	AHSTAT(ahs.ahs_toobig, "packets too big");
118	AHSTAT(ahs.ahs_pdrops, "packets dropped due to policy");
119	AHSTAT(ahs.ahs_crypto, "failed crypto requests");
120	AHSTAT(ahs.ahs_tunnel, "tunnel sanity check failures");
121	for (i = 0; i < AH_ALG_MAX; i++)
122		if (ahs.ahs_hist[i])
123			printf("ah packets with %s: %u\n"
124				, algname(i, aalgs, N(aalgs))
125				, ahs.ahs_hist[i]
126			);
127	AHSTAT64(ahs.ahs_ibytes, "bytes received");
128	AHSTAT64(ahs.ahs_obytes, "bytes transmitted");
129#undef AHSTAT64
130#undef AHSTAT
131
132#define	ESPSTAT(x,fmt)	if (x) printf("esp " fmt ": %u\n", x)
133#define	ESPSTAT64(x,fmt)	if (x) printf("esp " fmt ": %llu\n", x)
134	ESPSTAT(esps.esps_input, "input packets processed");
135	ESPSTAT(esps.esps_output, "output packets processed");
136	ESPSTAT(esps.esps_hdrops, "headers too short");
137	ESPSTAT(esps.esps_nopf, "headers for unsupported address family");
138	ESPSTAT(esps.esps_notdb, "packets with no SA");
139	ESPSTAT(esps.esps_badkcr, "packets with bad kcr");
140	ESPSTAT(esps.esps_qfull, "packets dropped packet 'cuz queue full");
141	ESPSTAT(esps.esps_noxform, "packets with no xform");
142	ESPSTAT(esps.esps_badilen, "packets with bad ilen");
143	ESPSTAT(esps.esps_badenc, "packets with bad encryption");
144	ESPSTAT(esps.esps_badauth, "packets with bad authentication");
145	ESPSTAT(esps.esps_wrap, "packets dropped for replay counter wrap");
146	ESPSTAT(esps.esps_replay, "packets dropped for possible replay");
147	ESPSTAT(esps.esps_invalid, "packets with an invalid SA");
148	ESPSTAT(esps.esps_toobig, "packets too big");
149	ESPSTAT(esps.esps_pdrops, "packets dropped due to policy");
150	ESPSTAT(esps.esps_crypto, "failed crypto requests");
151	ESPSTAT(esps.esps_tunnel, "tunnel sanity check failures");
152	for (i = 0; i < ESP_ALG_MAX; i++)
153		if (esps.esps_hist[i])
154			printf("esp packets with %s: %u\n"
155				, algname(i, espalgs, N(espalgs))
156				, esps.esps_hist[i]
157			);
158	ESPSTAT64(esps.esps_ibytes, "bytes received");
159	ESPSTAT64(esps.esps_obytes, "bytes transmitted");
160#undef ESPSTAT64
161#undef ESPSTAT
162
163	printf("\n");
164	if (ips.ips_in_polvio+ips.ips_out_polvio)
165		printf("policy violations: input %u output %u\n",
166			ips.ips_in_polvio, ips.ips_out_polvio);
167	STAT(ips.ips_out_nosa, "no SA found %u (output)");
168	STAT(ips.ips_out_nomem, "no memory available %u (output)");
169	STAT(ips.ips_out_noroute, "no route available %u (output)");
170	STAT(ips.ips_out_inval, "generic error %u (output)");
171	STAT(ips.ips_out_bundlesa, "bundled SA processed %u (output)");
172	printf("m_clone processing: %u mbufs + %u clusters coalesced\n",
173		ips.ips_mbcoalesced, ips.ips_clcoalesced);
174	printf("m_clone processing: %u clusters copied\n", ips.ips_clcopied);
175	printf("m_makespace: %u mbufs inserted\n", ips.ips_mbinserted);
176	printf("header position [front/middle/end]: %u/%u/%u\n",
177		ips.ips_input_front, ips.ips_input_middle, ips.ips_input_end);
178	return 0;
179}
180