fast_ipsec.c revision 1.1 1 1.1 jonathan /* $NetBSD: */
2 1.1 jonathan /* $FreeBSD: src/tools/tools/crypto/ipsecstats.c,v 1.1.4.1 2003/06/03 00:13:13 sam Exp $ */
3 1.1 jonathan
4 1.1 jonathan /*-
5 1.1 jonathan * Copyright (c) 2003, 2004 Jonathan Stone
6 1.1 jonathan * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7 1.1 jonathan * All rights reserved.
8 1.1 jonathan *
9 1.1 jonathan * Redistribution and use in source and binary forms, with or without
10 1.1 jonathan * modification, are permitted provided that the following conditions
11 1.1 jonathan * are met:
12 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer.
14 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
16 1.1 jonathan * documentation and/or other materials provided with the distribution.
17 1.1 jonathan *
18 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 jonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 jonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 jonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 jonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 jonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 jonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 jonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 jonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 jonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 jonathan * SUCH DAMAGE.
29 1.1 jonathan *
30 1.1 jonathan * $FreeBSD: src/tools/tools/crypto/ipsecstats.c,v 1.1.4.1 2003/06/03 00:13:13 sam Exp $
31 1.1 jonathan */
32 1.1 jonathan
33 1.1 jonathan #include <sys/cdefs.h>
34 1.1 jonathan #ifndef lint
35 1.1 jonathan #ifdef __NetBSD__
36 1.1 jonathan __RCSID("$NetBSD: fast_ipsec.c,v 1.1 2004/05/07 00:55:15 jonathan Exp $");
37 1.1 jonathan #endif
38 1.1 jonathan #endif /* not lint*/
39 1.1 jonathan
40 1.1 jonathan /* Kernel headers required, but not included, by netstat.h */
41 1.1 jonathan #include <sys/types.h>
42 1.1 jonathan #include <sys/socket.h>
43 1.1 jonathan
44 1.1 jonathan /* Kernel headers for sysctl(3). */
45 1.1 jonathan #include <sys/param.h>
46 1.1 jonathan #include <sys/sysctl.h>
47 1.1 jonathan
48 1.1 jonathan /* Kernel headers for FAST_IPSEC statistics */
49 1.1 jonathan #include <net/pfkeyv2.h>
50 1.1 jonathan #include <netipsec/esp_var.h>
51 1.1 jonathan #include <netipsec/ah_var.h>
52 1.1 jonathan #include <netipsec/ipip_var.h>
53 1.1 jonathan #include <netipsec/ipcomp_var.h>
54 1.1 jonathan #include <netipsec/ipsec_var.h>
55 1.1 jonathan #include <netipsec/keydb.h>
56 1.1 jonathan
57 1.1 jonathan #include <err.h>
58 1.1 jonathan #include <stdio.h>
59 1.1 jonathan #include <string.h>
60 1.1 jonathan
61 1.1 jonathan #include "netstat.h"
62 1.1 jonathan
63 1.1 jonathan /*
64 1.1 jonathan * Dispatch between fetching and printing (KAME) IPsec statistics,
65 1.1 jonathan * and FAST_IPSEC statistics, so the rest of netstat need not know
66 1.1 jonathan * about the vagaries of the two implementations.
67 1.1 jonathan */
68 1.1 jonathan void
69 1.1 jonathan ipsec_switch(u_long off, char * name)
70 1.1 jonathan {
71 1.1 jonathan int slen, status;
72 1.1 jonathan
73 1.1 jonathan slen = 0;
74 1.1 jonathan status = sysctlbyname("net.inet.ipsec.stats", NULL, &slen, NULL, 0);
75 1.1 jonathan if (status == 0)
76 1.1 jonathan return fast_ipsec_stats(off, name);
77 1.1 jonathan
78 1.1 jonathan return ipsec_stats(off, name);
79 1.1 jonathan }
80 1.1 jonathan
81 1.1 jonathan
82 1.1 jonathan /*
83 1.1 jonathan * Table-driven mapping from SADB algorithm codes to string names.
84 1.1 jonathan */
85 1.1 jonathan struct alg {
86 1.1 jonathan int a;
87 1.1 jonathan const char *name;
88 1.1 jonathan };
89 1.1 jonathan static const struct alg aalgs[] = {
90 1.1 jonathan { SADB_AALG_NONE, "none", },
91 1.1 jonathan { SADB_AALG_MD5HMAC, "hmac-md5", },
92 1.1 jonathan { SADB_AALG_SHA1HMAC, "hmac-sha1", },
93 1.1 jonathan { SADB_X_AALG_MD5, "md5", },
94 1.1 jonathan { SADB_X_AALG_SHA, "sha", },
95 1.1 jonathan { SADB_X_AALG_NULL, "null", },
96 1.1 jonathan { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
97 1.1 jonathan { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
98 1.1 jonathan { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
99 1.1 jonathan };
100 1.1 jonathan static const struct alg espalgs[] = {
101 1.1 jonathan { SADB_EALG_NONE, "none", },
102 1.1 jonathan { SADB_EALG_DESCBC, "des-cbc", },
103 1.1 jonathan { SADB_EALG_3DESCBC, "3des-cbc", },
104 1.1 jonathan { SADB_EALG_NULL, "null", },
105 1.1 jonathan { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
106 1.1 jonathan { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
107 1.1 jonathan { SADB_X_EALG_RIJNDAELCBC, "aes-cbc", },
108 1.1 jonathan };
109 1.1 jonathan static const struct alg ipcompalgs[] = {
110 1.1 jonathan { SADB_X_CALG_NONE, "none", },
111 1.1 jonathan { SADB_X_CALG_OUI, "oui", },
112 1.1 jonathan { SADB_X_CALG_DEFLATE, "deflate", },
113 1.1 jonathan { SADB_X_CALG_LZS, "lzs", },
114 1.1 jonathan };
115 1.1 jonathan #define N(a) (sizeof(a)/sizeof(a[0]))
116 1.1 jonathan
117 1.1 jonathan static const char*
118 1.1 jonathan algname(int a, const struct alg algs[], int nalgs)
119 1.1 jonathan {
120 1.1 jonathan static char buf[80];
121 1.1 jonathan int i;
122 1.1 jonathan
123 1.1 jonathan for (i = 0; i < nalgs; i++)
124 1.1 jonathan if (algs[i].a == a)
125 1.1 jonathan return algs[i].name;
126 1.1 jonathan snprintf(buf, sizeof(buf), "alg#%u", a);
127 1.1 jonathan return buf;
128 1.1 jonathan }
129 1.1 jonathan
130 1.1 jonathan /*
131 1.1 jonathan * Print the fast_ipsec statistics.
132 1.1 jonathan * Since NetBSD's netstat(1) seems not to find us for "netstat -s",
133 1.1 jonathan * but does(?) find KAME, be prepared to be called explicitly from
134 1.1 jonathan * netstat's main program for "netstat -s"; but silently do nothing
135 1.1 jonathan * if that happens when we are running on KAME IPsec.
136 1.1 jonathan */
137 1.1 jonathan void
138 1.1 jonathan fast_ipsec_stats(u_long off, char *name)
139 1.1 jonathan {
140 1.1 jonathan struct newipsecstat ipsecstats;
141 1.1 jonathan struct ahstat ahstats;
142 1.1 jonathan struct espstat espstats;
143 1.1 jonathan struct ipcompstat ipcs;
144 1.1 jonathan struct ipipstat ipips;
145 1.1 jonathan int status, slen;
146 1.1 jonathan int i;
147 1.1 jonathan
148 1.1 jonathan memset(&ipsecstats, 0, sizeof(ipsecstats));
149 1.1 jonathan memset(&ahstats, 0, sizeof(ahstats));
150 1.1 jonathan memset(&espstats, 0, sizeof(espstats));
151 1.1 jonathan memset(&ipcs, 0, sizeof(ipcs));
152 1.1 jonathan memset(&ipips, 0, sizeof(ipips));
153 1.1 jonathan
154 1.1 jonathan /* silence check */
155 1.1 jonathan status = sysctlbyname("net.inet.ipsec.stats", NULL, &slen, NULL, 0);
156 1.1 jonathan if (status != 0)
157 1.1 jonathan return;
158 1.1 jonathan
159 1.1 jonathan slen = sizeof(ipsecstats);
160 1.1 jonathan status = sysctlbyname("net.inet.ipsec.stats", &ipsecstats, &slen,
161 1.1 jonathan NULL, 0);
162 1.1 jonathan if (status < 0)
163 1.1 jonathan err(1, "net.inet.ipsec.stats");
164 1.1 jonathan
165 1.1 jonathan slen = sizeof (ahstats);
166 1.1 jonathan if (sysctlbyname("net.inet.ah.stats", &ahstats, &slen, NULL, 0) < 0)
167 1.1 jonathan err(1, "net.inet.ah.stats");
168 1.1 jonathan slen = sizeof (espstats);
169 1.1 jonathan if (sysctlbyname("net.inet.esp.stats", &espstats, &slen, NULL, 0) < 0)
170 1.1 jonathan err(1, "net.inet.esp.stats");
171 1.1 jonathan if (sysctlbyname("net.inet.ipcomp.stats", &ipcs, &slen, NULL, 0) < 0)
172 1.1 jonathan err(1, "net.inet.ipcomp.stats");
173 1.1 jonathan if (sysctlbyname("net.inet.ipip.stats", &ipips, &slen, NULL, 0) < 0)
174 1.1 jonathan err(1, "net.inet.ipip.stats");
175 1.1 jonathan
176 1.1 jonathan printf("(Fast) IPsec:\n");
177 1.1 jonathan
178 1.1 jonathan #define STAT(x,fmt) if ((x) || sflag <= 1) printf("\t%llu " fmt "\n", x)
179 1.1 jonathan if (ipsecstats.ips_in_polvio+ipsecstats.ips_out_polvio)
180 1.1 jonathan printf("\t%llu policy violations: %llu input %llu output\n",
181 1.1 jonathan ipsecstats.ips_in_polvio + ipsecstats.ips_out_polvio,
182 1.1 jonathan ipsecstats.ips_in_polvio, ipsecstats.ips_out_polvio);
183 1.1 jonathan STAT(ipsecstats.ips_out_nosa, "no SA found (output)");
184 1.1 jonathan STAT(ipsecstats.ips_out_nomem, "no memory available (output)");
185 1.1 jonathan STAT(ipsecstats.ips_out_noroute, "no route available (output)");
186 1.1 jonathan STAT(ipsecstats.ips_out_inval, "generic errors (output)");
187 1.1 jonathan STAT(ipsecstats.ips_out_bundlesa, "bundled SA processed (output)");
188 1.1 jonathan STAT(ipsecstats.ips_spdcache_lookup, "SPD cache lookups");
189 1.1 jonathan STAT(ipsecstats.ips_spdcache_lookup, "SPD cache misses");
190 1.1 jonathan #undef STAT
191 1.1 jonathan printf("\n");
192 1.1 jonathan
193 1.1 jonathan printf("IPsec ah:\n");
194 1.1 jonathan #define AHSTAT(x,fmt) if ((x) || sflag <= 1) printf("\t%llu ah " fmt "\n", x)
195 1.1 jonathan AHSTAT(ahstats.ahs_input, "input packets processed");
196 1.1 jonathan AHSTAT(ahstats.ahs_output, "output packets processed");
197 1.1 jonathan AHSTAT(ahstats.ahs_hdrops, "headers too short");
198 1.1 jonathan AHSTAT(ahstats.ahs_nopf, "headers for unsupported address family");
199 1.1 jonathan AHSTAT(ahstats.ahs_notdb, "packets with no SA");
200 1.1 jonathan AHSTAT(ahstats.ahs_badkcr, "packets dropped by crypto returning NULL mbuf");
201 1.1 jonathan AHSTAT(ahstats.ahs_badauth, "packets with bad authentication");
202 1.1 jonathan AHSTAT(ahstats.ahs_noxform, "packets with no xform");
203 1.1 jonathan AHSTAT(ahstats.ahs_qfull, "packets dropped due to queue full");
204 1.1 jonathan AHSTAT(ahstats.ahs_wrap, "packets dropped for replay counter wrap");
205 1.1 jonathan AHSTAT(ahstats.ahs_replay, "packets dropped for possible replay");
206 1.1 jonathan AHSTAT(ahstats.ahs_badauthl,"packets dropped for bad authenticator length");
207 1.1 jonathan AHSTAT(ahstats.ahs_invalid, "packets with an invalid SA");
208 1.1 jonathan AHSTAT(ahstats.ahs_toobig, "packets too big");
209 1.1 jonathan AHSTAT(ahstats.ahs_pdrops, "packets blocked due to policy");
210 1.1 jonathan AHSTAT(ahstats.ahs_crypto, "failed crypto requests");
211 1.1 jonathan AHSTAT(ahstats.ahs_tunnel, "tunnel sanity check failures");
212 1.1 jonathan
213 1.1 jonathan printf("\tah histogram:\n");
214 1.1 jonathan for (i = 0; i < AH_ALG_MAX; i++)
215 1.1 jonathan if (ahstats.ahs_hist[i])
216 1.1 jonathan printf("\t\tah packets with %s: %llu\n"
217 1.1 jonathan , algname(i, aalgs, N(aalgs))
218 1.1 jonathan , ahstats.ahs_hist[i]
219 1.1 jonathan );
220 1.1 jonathan AHSTAT(ahstats.ahs_ibytes, "bytes received");
221 1.1 jonathan AHSTAT(ahstats.ahs_obytes, "bytes transmitted");
222 1.1 jonathan #undef AHSTAT
223 1.1 jonathan printf("\n");
224 1.1 jonathan
225 1.1 jonathan printf("IPsec esp:\n");
226 1.1 jonathan #define ESPSTAT(x,fmt) if ((x) || sflag <= 1) printf("\t%llu esp " fmt "\n", x)
227 1.1 jonathan ESPSTAT(espstats.esps_input, "input packets processed");
228 1.1 jonathan ESPSTAT(espstats.esps_output, "output packets processed");
229 1.1 jonathan ESPSTAT(espstats.esps_hdrops, "headers too short");
230 1.1 jonathan ESPSTAT(espstats.esps_nopf, "headers for unsupported address family");
231 1.1 jonathan ESPSTAT(espstats.esps_notdb, "packets with no SA");
232 1.1 jonathan ESPSTAT(espstats.esps_badkcr, "packets dropped by crypto returning NULL mbuf");
233 1.1 jonathan ESPSTAT(espstats.esps_qfull, "packets dropped due to queue full");
234 1.1 jonathan ESPSTAT(espstats.esps_noxform, "packets with no xform");
235 1.1 jonathan ESPSTAT(espstats.esps_badilen, "packets with bad ilen");
236 1.1 jonathan ESPSTAT(espstats.esps_badenc, "packets with bad encryption");
237 1.1 jonathan ESPSTAT(espstats.esps_badauth, "packets with bad authentication");
238 1.1 jonathan ESPSTAT(espstats.esps_wrap, "packets dropped for replay counter wrap");
239 1.1 jonathan ESPSTAT(espstats.esps_replay, "packets dropped for possible replay");
240 1.1 jonathan ESPSTAT(espstats.esps_invalid, "packets with an invalid SA");
241 1.1 jonathan ESPSTAT(espstats.esps_toobig, "packets too big");
242 1.1 jonathan ESPSTAT(espstats.esps_pdrops, "packets blocked due to policy");
243 1.1 jonathan ESPSTAT(espstats.esps_crypto, "failed crypto requests");
244 1.1 jonathan ESPSTAT(espstats.esps_tunnel, "tunnel sanity check failures");
245 1.1 jonathan printf("\tesp histogram:\n");
246 1.1 jonathan for (i = 0; i < ESP_ALG_MAX; i++)
247 1.1 jonathan if (espstats.esps_hist[i])
248 1.1 jonathan printf("\t\tesp packets with %s: %llu\n"
249 1.1 jonathan , algname(i, espalgs, N(espalgs))
250 1.1 jonathan , espstats.esps_hist[i]
251 1.1 jonathan );
252 1.1 jonathan ESPSTAT(espstats.esps_ibytes, "bytes received");
253 1.1 jonathan ESPSTAT(espstats.esps_obytes, "bytes transmitted");
254 1.1 jonathan #undef ESPSTAT
255 1.1 jonathan printf("IPsec ipip:\n");
256 1.1 jonathan
257 1.1 jonathan #define IPIPSTAT(x,fmt) \
258 1.1 jonathan if ((x) || sflag <= 1) printf("\t%llu ipip " fmt "\n", x)
259 1.1 jonathan IPIPSTAT(ipips.ipips_ipackets, "total input packets");
260 1.1 jonathan IPIPSTAT(ipips.ipips_opackets, "total output packets");
261 1.1 jonathan IPIPSTAT(ipips.ipips_hdrops, "packets too short for header length");
262 1.1 jonathan IPIPSTAT(ipips.ipips_qfull, "packets dropped due to queue full");
263 1.1 jonathan IPIPSTAT(ipips.ipips_pdrops, "packets blocked due to policy");
264 1.1 jonathan IPIPSTAT(ipips.ipips_spoof, "IP spoofing attempts");
265 1.1 jonathan IPIPSTAT(ipips.ipips_family, "protocol family mismatched");
266 1.1 jonathan IPIPSTAT(ipips.ipips_unspec, "missing tunnel-endpoint address");
267 1.1 jonathan IPIPSTAT(ipips.ipips_ibytes, "input bytes received");
268 1.1 jonathan IPIPSTAT(ipips.ipips_obytes, "output bytes procesesed");
269 1.1 jonathan #undef IPIPSTAT
270 1.1 jonathan
271 1.1 jonathan printf("IPsec ipcomp:\n");
272 1.1 jonathan #define IPCOMP(x,fmt) \
273 1.1 jonathan if ((x) || sflag <= 1) printf("\t%llu ipcomp " fmt "\n", x)
274 1.1 jonathan
275 1.1 jonathan IPCOMP(ipcs.ipcomps_hdrops, "packets too short for header length");
276 1.1 jonathan IPCOMP(ipcs.ipcomps_nopf, "protocol family not supported");
277 1.1 jonathan IPCOMP(ipcs.ipcomps_notdb, "not db");
278 1.1 jonathan IPCOMP(ipcs.ipcomps_badkcr, "packets dropped by crypto returning NULL mbuf");
279 1.1 jonathan IPCOMP(ipcs.ipcomps_qfull, "queue full");
280 1.1 jonathan IPCOMP(ipcs.ipcomps_noxform, "no support for transform");
281 1.1 jonathan IPCOMP(ipcs.ipcomps_wrap, "packets dropped for replay counter wrap");
282 1.1 jonathan IPCOMP(ipcs.ipcomps_input, "input IPcomp packets");
283 1.1 jonathan IPCOMP(ipcs.ipcomps_output, "output IPcomp packets");
284 1.1 jonathan IPCOMP(ipcs.ipcomps_invalid, "specified an invalid TDB");
285 1.1 jonathan IPCOMP(ipcs.ipcomps_toobig, "packets decompressed as too big");
286 1.1 jonathan IPCOMP(ipcs.ipcomps_pdrops, "packets blocked due to policy");
287 1.1 jonathan IPCOMP(ipcs.ipcomps_crypto, "failed crypto requests");
288 1.1 jonathan
289 1.1 jonathan printf("\tIPcomp histogram:\n");
290 1.1 jonathan for (i = 0; i < IPCOMP_ALG_MAX; i++)
291 1.1 jonathan if (ipcs.ipcomps_hist[i])
292 1.1 jonathan printf("\t\tIPcomp packets with %s: %llu\n"
293 1.1 jonathan , algname(i, ipcompalgs, N(ipcompalgs))
294 1.1 jonathan , ipcs.ipcomps_hist[i]
295 1.1 jonathan );
296 1.1 jonathan IPCOMP(ipcs.ipcomps_ibytes, "input bytes");
297 1.1 jonathan IPCOMP(ipcs.ipcomps_obytes, "output bytes");
298 1.1 jonathan #undef IPCOMP
299 1.1 jonathan }
300