1164032Srwatson/*-
2164032Srwatson * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
3189503Srwatson * All rights reserved.
4164032Srwatson *
5164032Srwatson * Redistribution and use in source and binary forms, with or without
6164032Srwatson * modification, are permitted provided that the following conditions
7164032Srwatson * are met:
8164032Srwatson * 1. Redistributions of source code must retain the above copyright
9189503Srwatson *    notice, this list of conditions and the following disclaimer.
10189503Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11189503Srwatson *    notice, this list of conditions and the following disclaimer in the
12164032Srwatson *    documentation and/or other materials provided with the distribution.
13164032Srwatson *
14164032Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15164032Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16164032Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17164032Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18164032Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19164032Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20164032Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21164032Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22164032Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23164032Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24164032Srwatson * SUCH DAMAGE.
25164032Srwatson *
26164032Srwatson * $FreeBSD$
27164032Srwatson */
28164032Srwatson#include <sys/types.h>
29164032Srwatson#include <netipsec/ipsec.h>
30164032Srwatson#include <netipsec/ah_var.h>
31164032Srwatson#include <netipsec/esp_var.h>
32164032Srwatson#include <stdint.h>
33164032Srwatson#include <stdio.h>
34164032Srwatson
35164032Srwatsonstruct alg {
36164032Srwatson	int		a;
37164032Srwatson	const char	*name;
38187667Srwatson};
39187667Srwatsonstatic const struct alg aalgs[] = {
40187667Srwatson	{ SADB_AALG_NONE,	"none", },
41189503Srwatson	{ SADB_AALG_MD5HMAC,	"hmac-md5", },
42164032Srwatson	{ SADB_AALG_SHA1HMAC,	"hmac-sha1", },
43164032Srwatson	{ SADB_X_AALG_MD5,	"md5", },
44164032Srwatson	{ SADB_X_AALG_SHA,	"sha", },
45189503Srwatson	{ SADB_X_AALG_NULL,	"null", },
46164032Srwatson	{ SADB_X_AALG_SHA2_256,	"hmac-sha2-256", },
47189503Srwatson	{ SADB_X_AALG_SHA2_384,	"hmac-sha2-384", },
48164032Srwatson	{ SADB_X_AALG_SHA2_512,	"hmac-sha2-512", },
49164032Srwatson};
50164032Srwatsonstatic const struct alg espalgs[] = {
51164032Srwatson	{ SADB_EALG_NONE,	"none", },
52165469Srwatson	{ SADB_EALG_DESCBC,	"des-cbc", },
53164032Srwatson	{ SADB_EALG_3DESCBC,	"3des-cbc", },
54165424Srwatson	{ SADB_EALG_NULL,	"null", },
55165424Srwatson	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
56165424Srwatson	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
57165424Srwatson	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
58165424Srwatson};
59165424Srwatsonstatic const struct alg ipcompalgs[] = {
60165424Srwatson	{ SADB_X_CALG_NONE,	"none", },
61165424Srwatson	{ SADB_X_CALG_OUI,	"oui", },
62165424Srwatson	{ SADB_X_CALG_DEFLATE,	"deflate", },
63165424Srwatson	{ SADB_X_CALG_LZS,	"lzs", },
64189503Srwatson};
65189503Srwatson#define	N(a)	(sizeof(a)/sizeof(a[0]))
66165424Srwatson
67165424Srwatsonstatic const char*
68165424Srwatsonalgname(int a, const struct alg algs[], int nalgs)
69165424Srwatson{
70164032Srwatson	static char buf[80];
71164032Srwatson	int i;
72164032Srwatson
73164032Srwatson	for (i = 0; i < nalgs; i++)
74164032Srwatson		if (algs[i].a == a)
75191731Srwatson			return algs[i].name;
76189503Srwatson	snprintf(buf, sizeof(buf), "alg#%u", a);
77164032Srwatson	return buf;
78164032Srwatson}
79164032Srwatson
80164032Srwatson/*
81189503Srwatson * Little program to dump the statistics block for fast ipsec.
82189503Srwatson */
83165424Srwatsonint
84165424Srwatsonmain(int argc, char *argv[])
85165424Srwatson{
86165424Srwatson#define	STAT(x,fmt)	if (x) printf(fmt "\n", (uintmax_t)x)
87164032Srwatson	struct ipsecstat ips;
88164032Srwatson	struct ahstat ahs;
89164032Srwatson	struct espstat esps;
90164032Srwatson	size_t slen;
91164032Srwatson	int i;
92191731Srwatson
93189503Srwatson	slen = sizeof (ips);
94164032Srwatson	if (sysctlbyname("net.inet.ipsec.ipsecstats", &ips, &slen, NULL, NULL) < 0)
95164032Srwatson		err(1, "net.inet.ipsec.ipsecstats");
96164032Srwatson	slen = sizeof (ahs);
97	if (sysctlbyname("net.inet.ah.stats", &ahs, &slen, NULL, NULL) < 0)
98		err(1, "net.inet.ah.stats");
99	slen = sizeof (esps);
100	if (sysctlbyname("net.inet.esp.stats", &esps, &slen, NULL, NULL) < 0)
101		err(1, "net.inet.esp.stats");
102
103#define	AHSTAT(x,fmt)	if (x) printf("ah " fmt ": %ju\n", (uintmax_t)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: %ju\n"
124				, algname(i, aalgs, N(aalgs))
125				, (uintmax_t)ahs.ahs_hist[i]
126			);
127	AHSTAT(ahs.ahs_ibytes, "bytes received");
128	AHSTAT(ahs.ahs_obytes, "bytes transmitted");
129#undef AHSTAT
130
131#define	ESPSTAT(x,fmt)	if (x) printf("esp " fmt ": %ju\n", (uintmax_t)x)
132	ESPSTAT(esps.esps_input, "input packets processed");
133	ESPSTAT(esps.esps_output, "output packets processed");
134	ESPSTAT(esps.esps_hdrops, "headers too short");
135	ESPSTAT(esps.esps_nopf, "headers for unsupported address family");
136	ESPSTAT(esps.esps_notdb, "packets with no SA");
137	ESPSTAT(esps.esps_badkcr, "packets with bad kcr");
138	ESPSTAT(esps.esps_qfull, "packets dropped packet 'cuz queue full");
139	ESPSTAT(esps.esps_noxform, "packets with no xform");
140	ESPSTAT(esps.esps_badilen, "packets with bad ilen");
141	ESPSTAT(esps.esps_badenc, "packets with bad encryption");
142	ESPSTAT(esps.esps_badauth, "packets with bad authentication");
143	ESPSTAT(esps.esps_wrap, "packets dropped for replay counter wrap");
144	ESPSTAT(esps.esps_replay, "packets dropped for possible replay");
145	ESPSTAT(esps.esps_invalid, "packets with an invalid SA");
146	ESPSTAT(esps.esps_toobig, "packets too big");
147	ESPSTAT(esps.esps_pdrops, "packets dropped due to policy");
148	ESPSTAT(esps.esps_crypto, "failed crypto requests");
149	ESPSTAT(esps.esps_tunnel, "tunnel sanity check failures");
150	for (i = 0; i < ESP_ALG_MAX; i++)
151		if (esps.esps_hist[i])
152			printf("esp packets with %s: %ju\n"
153				, algname(i, espalgs, N(espalgs))
154				, (uintmax_t)esps.esps_hist[i]
155			);
156	ESPSTAT(esps.esps_ibytes, "bytes received");
157	ESPSTAT(esps.esps_obytes, "bytes transmitted");
158#undef ESPSTAT
159
160	printf("\n");
161	if (ips.ips_in_polvio+ips.ips_out_polvio)
162		printf("policy violations: input %ju output %ju\n",
163		    (uintmax_t)ips.ips_in_polvio,
164		    (uintmax_t)ips.ips_out_polvio);
165	STAT(ips.ips_out_nosa, "no SA found %ju (output)");
166	STAT(ips.ips_out_nomem, "no memory available %ju (output)");
167	STAT(ips.ips_out_noroute, "no route available %ju (output)");
168	STAT(ips.ips_out_inval, "generic error %ju (output)");
169	STAT(ips.ips_out_bundlesa, "bundled SA processed %ju (output)");
170	printf("m_clone processing: %ju mbufs + %ju clusters coalesced\n",
171	    (uintmax_t)ips.ips_mbcoalesced, (uintmax_t)ips.ips_clcoalesced);
172	STAT(ips.ips_clcopied, "m_clone processing: %ju clusters copied\n");
173	STAT(ips.ips_mbinserted, "m_makespace: %ju mbufs inserted\n");
174	printf("header position [front/middle/end]: %ju/%ju/%ju\n",
175	    (uintmax_t)ips.ips_input_front, (uintmax_t)ips.ips_input_middle,
176	    (uintmax_t)ips.ips_input_end);
177	return 0;
178}
179