xref: /freebsd/tools/tools/crypto/ipsecstats.c (revision 148a8da8)
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 
29 #include <sys/param.h>
30 #include <sys/sysctl.h>
31 
32 #include <netipsec/ipsec.h>
33 #include <netipsec/ah_var.h>
34 #include <netipsec/esp_var.h>
35 
36 #include <err.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 
40 struct alg {
41 	int		a;
42 	const char	*name;
43 };
44 static const struct alg aalgs[] = {
45 	{ SADB_AALG_NONE,	"none", },
46 	{ SADB_AALG_MD5HMAC,	"hmac-md5", },
47 	{ SADB_AALG_SHA1HMAC,	"hmac-sha1", },
48 	{ SADB_X_AALG_MD5,	"md5", },
49 	{ SADB_X_AALG_SHA,	"sha", },
50 	{ SADB_X_AALG_NULL,	"null", },
51 	{ SADB_X_AALG_SHA2_256,	"hmac-sha2-256", },
52 	{ SADB_X_AALG_SHA2_384,	"hmac-sha2-384", },
53 	{ SADB_X_AALG_SHA2_512,	"hmac-sha2-512", },
54 };
55 static const struct alg espalgs[] = {
56 	{ SADB_EALG_NONE,	"none", },
57 	{ SADB_EALG_DESCBC,	"des-cbc", },
58 	{ SADB_EALG_3DESCBC,	"3des-cbc", },
59 	{ SADB_EALG_NULL,	"null", },
60 	{ SADB_X_EALG_CAST128CBC, "cast128-cbc", },
61 	{ SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
62 	{ SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
63 };
64 static const struct alg ipcompalgs[] = {
65 	{ SADB_X_CALG_NONE,	"none", },
66 	{ SADB_X_CALG_OUI,	"oui", },
67 	{ SADB_X_CALG_DEFLATE,	"deflate", },
68 	{ SADB_X_CALG_LZS,	"lzs", },
69 };
70 
71 static const char*
72 algname(int a, const struct alg algs[], int nalgs)
73 {
74 	static char buf[80];
75 	int i;
76 
77 	for (i = 0; i < nalgs; i++)
78 		if (algs[i].a == a)
79 			return algs[i].name;
80 	snprintf(buf, sizeof(buf), "alg#%u", a);
81 	return buf;
82 }
83 
84 /*
85  * Little program to dump the statistics block for fast ipsec.
86  */
87 int
88 main(int argc, char *argv[])
89 {
90 #define	STAT(x,fmt)	if (x) printf(fmt "\n", (uintmax_t)x)
91 	struct ipsecstat ips;
92 	struct ahstat ahs;
93 	struct espstat esps;
94 	size_t slen;
95 	int i;
96 
97 	slen = sizeof (ips);
98 	if (sysctlbyname("net.inet.ipsec.ipsecstats", &ips, &slen, NULL, 0) < 0)
99 		err(1, "net.inet.ipsec.ipsecstats");
100 	slen = sizeof (ahs);
101 	if (sysctlbyname("net.inet.ah.stats", &ahs, &slen, NULL, 0) < 0)
102 		err(1, "net.inet.ah.stats");
103 	slen = sizeof (esps);
104 	if (sysctlbyname("net.inet.esp.stats", &esps, &slen, NULL, 0) < 0)
105 		err(1, "net.inet.esp.stats");
106 
107 #define	AHSTAT(x,fmt)	if (x) printf("ah " fmt ": %ju\n", (uintmax_t)x)
108 	AHSTAT(ahs.ahs_input, "input packets processed");
109 	AHSTAT(ahs.ahs_output, "output packets processed");
110 	AHSTAT(ahs.ahs_hdrops, "headers too short");
111 	AHSTAT(ahs.ahs_nopf, "headers for unsupported address family");
112 	AHSTAT(ahs.ahs_notdb, "packets with no SA");
113 	AHSTAT(ahs.ahs_badkcr, "packets with bad kcr");
114 	AHSTAT(ahs.ahs_badauth, "packets with bad authentication");
115 	AHSTAT(ahs.ahs_noxform, "packets with no xform");
116 	AHSTAT(ahs.ahs_qfull, "packets dropped packet 'cuz queue full");
117 	AHSTAT(ahs.ahs_wrap, "packets dropped for replace counter wrap");
118 	AHSTAT(ahs.ahs_replay, "packets dropped for possible replay");
119 	AHSTAT(ahs.ahs_badauthl, "packets dropped for bad authenticator length");
120 	AHSTAT(ahs.ahs_invalid, "packets with an invalid SA");
121 	AHSTAT(ahs.ahs_toobig, "packets too big");
122 	AHSTAT(ahs.ahs_pdrops, "packets dropped due to policy");
123 	AHSTAT(ahs.ahs_crypto, "failed crypto requests");
124 	AHSTAT(ahs.ahs_tunnel, "tunnel sanity check failures");
125 	for (i = 0; i < AH_ALG_MAX; i++)
126 		if (ahs.ahs_hist[i])
127 			printf("ah packets with %s: %ju\n"
128 				, algname(i, aalgs, nitems(aalgs))
129 				, (uintmax_t)ahs.ahs_hist[i]
130 			);
131 	AHSTAT(ahs.ahs_ibytes, "bytes received");
132 	AHSTAT(ahs.ahs_obytes, "bytes transmitted");
133 #undef AHSTAT
134 
135 #define	ESPSTAT(x,fmt)	if (x) printf("esp " fmt ": %ju\n", (uintmax_t)x)
136 	ESPSTAT(esps.esps_input, "input packets processed");
137 	ESPSTAT(esps.esps_output, "output packets processed");
138 	ESPSTAT(esps.esps_hdrops, "headers too short");
139 	ESPSTAT(esps.esps_nopf, "headers for unsupported address family");
140 	ESPSTAT(esps.esps_notdb, "packets with no SA");
141 	ESPSTAT(esps.esps_badkcr, "packets with bad kcr");
142 	ESPSTAT(esps.esps_qfull, "packets dropped packet 'cuz queue full");
143 	ESPSTAT(esps.esps_noxform, "packets with no xform");
144 	ESPSTAT(esps.esps_badilen, "packets with bad ilen");
145 	ESPSTAT(esps.esps_badenc, "packets with bad encryption");
146 	ESPSTAT(esps.esps_badauth, "packets with bad authentication");
147 	ESPSTAT(esps.esps_wrap, "packets dropped for replay counter wrap");
148 	ESPSTAT(esps.esps_replay, "packets dropped for possible replay");
149 	ESPSTAT(esps.esps_invalid, "packets with an invalid SA");
150 	ESPSTAT(esps.esps_toobig, "packets too big");
151 	ESPSTAT(esps.esps_pdrops, "packets dropped due to policy");
152 	ESPSTAT(esps.esps_crypto, "failed crypto requests");
153 	ESPSTAT(esps.esps_tunnel, "tunnel sanity check failures");
154 	for (i = 0; i < ESP_ALG_MAX; i++)
155 		if (esps.esps_hist[i])
156 			printf("esp packets with %s: %ju\n"
157 				, algname(i, espalgs, nitems(espalgs))
158 				, (uintmax_t)esps.esps_hist[i]
159 			);
160 	ESPSTAT(esps.esps_ibytes, "bytes received");
161 	ESPSTAT(esps.esps_obytes, "bytes transmitted");
162 #undef ESPSTAT
163 
164 	printf("\n");
165 	if (ips.ips_in_polvio+ips.ips_out_polvio)
166 		printf("policy violations: input %ju output %ju\n",
167 		    (uintmax_t)ips.ips_in_polvio,
168 		    (uintmax_t)ips.ips_out_polvio);
169 	STAT(ips.ips_out_nosa, "no SA found %ju (output)");
170 	STAT(ips.ips_out_nomem, "no memory available %ju (output)");
171 	STAT(ips.ips_out_noroute, "no route available %ju (output)");
172 	STAT(ips.ips_out_inval, "generic error %ju (output)");
173 	STAT(ips.ips_out_bundlesa, "bundled SA processed %ju (output)");
174 	STAT(ips.ips_clcopied, "m_clone processing: %ju clusters copied\n");
175 	STAT(ips.ips_spdcache_hits, "spd cache hits %ju\n");
176 	STAT(ips.ips_spdcache_misses, "spd cache misses %ju\n");
177 	STAT(ips.ips_mbinserted, "m_makespace: %ju mbufs inserted\n");
178 	printf("header position [front/middle/end]: %ju/%ju/%ju\n",
179 	    (uintmax_t)ips.ips_input_front, (uintmax_t)ips.ips_input_middle,
180 	    (uintmax_t)ips.ips_input_end);
181 	return 0;
182 }
183