No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / netstat / ipsec.c
blob06d003a05ea658719432ea1b152c9311ed2b0c7f
1 /* $NetBSD: ipsec.c,v 1.14 2008/04/24 04:09:27 thorpej Exp $ */
2 /* $KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 * Copyright (c) 1983, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
62 #include <sys/cdefs.h>
63 #ifndef lint
64 #if 0
65 static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94";
66 #else
67 #ifdef __NetBSD__
68 __RCSID("$NetBSD: ipsec.c,v 1.14 2008/04/24 04:09:27 thorpej Exp $");
69 #endif
70 #endif
71 #endif /* not lint */
73 #include <sys/param.h>
74 #include <sys/queue.h>
75 #include <sys/socket.h>
76 #include <sys/sysctl.h>
78 #include <netinet/in.h>
80 #ifdef IPSEC
81 #include <netinet6/ipsec.h>
82 #include <netkey/keysock.h>
83 #endif
85 #include <err.h>
86 #include <kvm.h>
87 #include <stdio.h>
88 #include <string.h>
89 #include <unistd.h>
90 #include "netstat.h"
92 #ifdef IPSEC
93 struct val2str {
94 int val;
95 const char *str;
98 static struct val2str ipsec_ahnames[] = {
99 { SADB_AALG_NONE, "none", },
100 { SADB_AALG_MD5HMAC, "hmac-md5", },
101 { SADB_AALG_SHA1HMAC, "hmac-sha1", },
102 { SADB_X_AALG_MD5, "md5", },
103 { SADB_X_AALG_SHA, "sha", },
104 { SADB_X_AALG_NULL, "null", },
105 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
106 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
107 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
108 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", },
109 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", },
110 { -1, NULL },
113 static struct val2str ipsec_espnames[] = {
114 { SADB_EALG_NONE, "none", },
115 { SADB_EALG_DESCBC, "des-cbc", },
116 { SADB_EALG_3DESCBC, "3des-cbc", },
117 { SADB_EALG_NULL, "null", },
118 { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
119 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
120 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
121 { SADB_X_EALG_AESCTR, "aes-ctr", },
122 { -1, NULL },
125 static struct val2str ipsec_compnames[] = {
126 { SADB_X_CALG_NONE, "none", },
127 { SADB_X_CALG_OUI, "oui", },
128 { SADB_X_CALG_DEFLATE, "deflate", },
129 { SADB_X_CALG_LZS, "lzs", },
130 { -1, NULL },
133 static const char *pfkey_msgtypenames[] = {
134 "reserved", "getspi", "update", "add", "delete",
135 "get", "acquire", "register", "expire", "flush",
136 "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
137 "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
138 "x_spdsetidx", "x_spdexpire", "x_spddelete2"
141 static uint64_t ipsecstat[IPSEC_NSTATS];
143 static void print_ipsecstats(void);
144 static const char *pfkey_msgtype_names(int);
145 static void ipsec_hist(const u_quad_t *, int, const struct val2str *,
146 size_t, const char *);
149 * Dump IPSEC statistics structure.
151 static void
152 ipsec_hist(const uint64_t *hist, int histmax, const struct val2str *name,
153 size_t namemax, const char *title)
155 int first;
156 int proto;
157 const struct val2str *p;
159 first = 1;
160 for (proto = 0; proto < histmax; proto++) {
161 if (hist[proto] <= 0)
162 continue;
163 if (first) {
164 printf("\t%s histogram:\n", title);
165 first = 0;
167 for (p = name; p && p->str; p++) {
168 if (p->val == proto)
169 break;
171 if (p && p->str) {
172 printf("\t\t%s: %llu\n", p->str, (unsigned long long)hist[proto]);
173 } else {
174 printf("\t\t#%ld: %llu\n", (long)proto,
175 (unsigned long long)hist[proto]);
180 static void
181 print_ipsecstats(void)
183 #define p(f, m) if (ipsecstat[f] || sflag <= 1) \
184 printf(m, (unsigned long long)ipsecstat[f], plural(ipsecstat[f]))
185 #define pes(f, m) if (ipsecstat[f] || sflag <= 1) \
186 printf(m, (unsigned long long)ipsecstat[f], plurales(ipsecstat[f]))
187 #define hist(f, c, n, t) \
188 ipsec_hist(&(f), (c), (n), sizeof(n)/sizeof(n[0]), (t));
190 p(IPSEC_STAT_IN_SUCCESS, "\t%llu inbound packet%s processed successfully\n");
191 p(IPSEC_STAT_IN_POLVIO, "\t%llu inbound packet%s violated process security "
192 "policy\n");
193 p(IPSEC_STAT_IN_NOSA, "\t%llu inbound packet%s with no SA available\n");
194 p(IPSEC_STAT_IN_INVAL, "\t%llu invalid inbound packet%s\n");
195 p(IPSEC_STAT_IN_NOMEM, "\t%llu inbound packet%s failed due to insufficient memory\n");
196 p(IPSEC_STAT_IN_BADSPI, "\t%llu inbound packet%s failed getting SPI\n");
197 p(IPSEC_STAT_IN_AHREPLAY, "\t%llu inbound packet%s failed on AH replay check\n");
198 p(IPSEC_STAT_IN_ESPREPLAY, "\t%llu inbound packet%s failed on ESP replay check\n");
199 p(IPSEC_STAT_IN_AHAUTHSUCC, "\t%llu inbound packet%s considered authentic\n");
200 p(IPSEC_STAT_IN_AHAUTHFAIL, "\t%llu inbound packet%s failed on authentication\n");
201 hist(ipsecstat[IPSEC_STAT_IN_AHHIST], 256, ipsec_ahnames, "AH input");
202 hist(ipsecstat[IPSEC_STAT_IN_ESPHIST], 256, ipsec_espnames, "ESP input");
203 hist(ipsecstat[IPSEC_STAT_IN_COMPHIST], 256, ipsec_compnames, "IPComp input");
205 p(IPSEC_STAT_OUT_SUCCESS, "\t%llu outbound packet%s processed successfully\n");
206 p(IPSEC_STAT_OUT_POLVIO, "\t%llu outbound packet%s violated process security "
207 "policy\n");
208 p(IPSEC_STAT_OUT_NOSA, "\t%llu outbound packet%s with no SA available\n");
209 p(IPSEC_STAT_OUT_INVAL, "\t%llu invalid outbound packet%s\n");
210 p(IPSEC_STAT_OUT_NOMEM, "\t%llu outbound packet%s failed due to insufficient memory\n");
211 p(IPSEC_STAT_OUT_NOROUTE, "\t%llu outbound packet%s with no route\n");
212 hist(ipsecstat[IPSEC_STAT_OUT_AHHIST], 256, ipsec_ahnames, "AH output");
213 hist(ipsecstat[IPSEC_STAT_OUT_ESPHIST], 256, ipsec_espnames, "ESP output");
214 hist(ipsecstat[IPSEC_STAT_OUT_COMPHIST], 256, ipsec_compnames, "IPComp output");
216 p(IPSEC_STAT_SPDCACHELOOKUP, "\t%llu SPD cache lookup%s\n");
217 pes(IPSEC_STAT_SPDCACHEMISS, "\t%llu SPD cache miss%s\n");
218 #undef p
219 #undef pes
220 #undef hist
223 void
224 ipsec_stats(u_long off, const char *name)
227 if (use_sysctl) {
228 size_t size = sizeof(ipsecstat);
230 if (sysctlbyname("net.inet.ipsec.stats", ipsecstat, &size,
231 NULL, 0) == -1)
232 return;
233 } else {
234 warnx("%s stats not available via KVM.", name);
235 return;
238 printf ("%s:\n", name);
240 print_ipsecstats();
243 static const char *
244 pfkey_msgtype_names(int x)
246 const int max =
247 sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
248 static char buf[20];
250 if (x < max && pfkey_msgtypenames[x])
251 return pfkey_msgtypenames[x];
252 snprintf(buf, sizeof(buf), "#%d", x);
253 return buf;
256 void
257 pfkey_stats(u_long off, const char *name)
259 uint64_t pfkeystat[PFKEY_NSTATS];
260 int first, type;
262 if (use_sysctl) {
263 size_t size = sizeof(pfkeystat);
265 if (sysctlbyname("net.key.stats", pfkeystat, &size,
266 NULL, 0) == -1)
267 return;
268 } else {
269 warnx("%s stats not available via KVM.", name);
270 return;
273 printf ("%s:\n", name);
275 #define p(f, m) if (pfkeystat[f] || sflag <= 1) \
276 printf(m, (unsigned long long)pfkeystat[f], plural(pfkeystat[f]))
278 /* userland -> kernel */
279 p(PFKEY_STAT_OUT_TOTAL, "\t%llu request%s sent from userland\n");
280 p(PFKEY_STAT_OUT_BYTES, "\t%llu byte%s sent from userland\n");
281 for (first = 1, type = 0; type < 256; type++) {
282 if (pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type] == 0)
283 continue;
284 if (first) {
285 printf("\thistogram by message type:\n");
286 first = 0;
288 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
289 (unsigned long long)pfkeystat[PFKEY_STAT_OUT_MSGTYPE + type]);
291 p(PFKEY_STAT_OUT_INVLEN, "\t%llu message%s with invalid length field\n");
292 p(PFKEY_STAT_OUT_INVVER, "\t%llu message%s with invalid version field\n");
293 p(PFKEY_STAT_OUT_INVMSGTYPE, "\t%llu message%s with invalid message type field\n");
294 p(PFKEY_STAT_OUT_TOOSHORT, "\t%llu message%s too short\n");
295 p(PFKEY_STAT_OUT_NOMEM, "\t%llu message%s with memory allocation failure\n");
296 p(PFKEY_STAT_OUT_DUPEXT, "\t%llu message%s with duplicate extension\n");
297 p(PFKEY_STAT_OUT_INVEXTTYPE, "\t%llu message%s with invalid extension type\n");
298 p(PFKEY_STAT_OUT_INVSATYPE, "\t%llu message%s with invalid sa type\n");
299 p(PFKEY_STAT_OUT_INVADDR, "\t%llu message%s with invalid address extension\n");
301 /* kernel -> userland */
302 p(PFKEY_STAT_IN_TOTAL, "\t%llu request%s sent to userland\n");
303 p(PFKEY_STAT_IN_BYTES, "\t%llu byte%s sent to userland\n");
304 for (first = 1, type = 0; type < 256; type++) {
305 if (pfkeystat[PFKEY_STAT_IN_MSGTYPE + type] == 0)
306 continue;
307 if (first) {
308 printf("\thistogram by message type:\n");
309 first = 0;
311 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type),
312 (unsigned long long)pfkeystat[PFKEY_STAT_IN_MSGTYPE + type]);
314 p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ONE,
315 "\t%llu message%s toward single socket\n");
316 p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_ALL,
317 "\t%llu message%s toward all sockets\n");
318 p(PFKEY_STAT_IN_MSGTARGET + KEY_SENDUP_REGISTERED,
319 "\t%llu message%s toward registered sockets\n");
320 p(PFKEY_STAT_IN_NOMEM, "\t%llu message%s with memory allocation failure\n");
321 #undef p
323 #endif /*IPSEC*/