kernel - Add vmmeter counter for token collisions
[dragonfly.git] / usr.bin / netstat / ipsec.c
1 /*      $FreeBSD: src/usr.bin/netstat/ipsec.c,v 1.1.2.3 2001/08/10 09:07:09 ru Exp $    */
2 /*      $NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $      */
3 /*      $KAME: ipsec.c,v 1.25 2001/03/12 09:04:39 itojun Exp $  */
4
5 /*
6  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
7  * All rights reserved.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)inet.c   8.5 (Berkeley) 5/24/95
34  * $FreeBSD: src/usr.bin/netstat/ipsec.c,v 1.1.2.3 2001/08/10 09:07:09 ru Exp $
35  */
36
37 /*
38  * Copyright (c) 1983, 1988, 1993
39  *      The Regents of the University of California.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *      This product includes software developed by the University of
52  *      California, Berkeley and its contributors.
53  * 4. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/socket.h>
73
74 #include <netinet/in.h>
75
76 #ifdef IPSEC
77 #include <netinet6/ipsec.h>
78 #include <netkey/keysock.h>
79 #endif
80
81 #include <stdio.h>
82 #include <string.h>
83 #include <unistd.h>
84 #include "netstat.h"
85
86 /*
87  * portability issues:
88  * - bsdi[34] uses PLURAL(), not plural().
89  * - freebsd2 can't print "unsigned long long" properly.
90  */
91 /*
92  * XXX see PORTABILITY for the twist
93  */
94 #define LLU     "%llu"
95 #define CAST    unsigned long long
96
97 #ifdef IPSEC 
98 struct val2str {
99         int val;
100         const char *str;
101 };
102
103 static struct val2str ipsec_ahnames[] = {
104         { SADB_AALG_NONE, "none", },
105         { SADB_AALG_MD5HMAC, "hmac-md5", },
106         { SADB_AALG_SHA1HMAC, "hmac-sha1", },
107         { SADB_X_AALG_MD5, "md5", },
108         { SADB_X_AALG_SHA, "sha", },
109         { SADB_X_AALG_NULL, "null", },
110 #ifdef SADB_X_AALG_SHA2_256
111         { SADB_X_AALG_SHA2_256, "hmac-sha2-256", },
112 #endif
113 #ifdef SADB_X_AALG_SHA2_384
114         { SADB_X_AALG_SHA2_384, "hmac-sha2-384", },
115 #endif
116 #ifdef SADB_X_AALG_SHA2_512
117         { SADB_X_AALG_SHA2_512, "hmac-sha2-512", },
118 #endif
119         { -1, NULL },
120 };
121
122 static struct val2str ipsec_espnames[] = {
123         { SADB_EALG_NONE, "none", },
124         { SADB_EALG_DESCBC, "des-cbc", },
125         { SADB_EALG_3DESCBC, "3des-cbc", },
126         { SADB_EALG_NULL, "null", },
127 #ifdef SADB_X_EALG_RC5CBC
128         { SADB_X_EALG_RC5CBC, "rc5-cbc", },
129 #endif
130         { SADB_X_EALG_CAST128CBC, "cast128-cbc", },
131         { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", },
132 #ifdef SADB_X_EALG_RIJNDAELCBC
133         { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", },
134 #endif
135         { -1, NULL },
136 };
137
138 static struct val2str ipsec_compnames[] = {
139         { SADB_X_CALG_NONE, "none", },
140         { SADB_X_CALG_OUI, "oui", },
141         { SADB_X_CALG_DEFLATE, "deflate", },
142         { SADB_X_CALG_LZS, "lzs", },
143         { -1, NULL },
144 };
145
146 static const char *pfkey_msgtypenames[] = {
147         "reserved", "getspi", "update", "add", "delete",
148         "get", "acquire", "register", "expire", "flush",
149         "dump", "x_promisc", "x_pchange", "x_spdupdate", "x_spdadd",
150         "x_spddelete", "x_spdget", "x_spdacquire", "x_spddump", "x_spdflush",
151         "x_spdsetidx", "x_spdexpire", "x_spddelete2"
152 };
153
154 static struct ipsecstat ipsecstat;
155
156 static void print_ipsecstats (void);
157 static const char *pfkey_msgtype_names (int);
158 static void ipsec_hist (const u_quad_t *, size_t, const struct val2str *,
159         const char *);
160
161 /*
162  * Dump IPSEC statistics structure.
163  */
164 static void
165 ipsec_hist(const u_quad_t *hist,
166            size_t histmax,
167            const struct val2str *name,
168            const char *title)
169 {
170         int first;
171         size_t proto;
172         const struct val2str *p;
173
174         first = 1;
175         for (proto = 0; proto < histmax; proto++) {
176                 if (hist[proto] <= 0)
177                         continue;
178                 if (first) {
179                         printf("\t%s histogram:\n", title);
180                         first = 0;
181                 }
182                 for (p = name; p && p->str; p++) {
183                         if (p->val == (int)proto)
184                                 break;
185                 }
186                 if (p && p->str) {
187                         printf("\t\t%s: " LLU "\n", p->str, (CAST)hist[proto]);
188                 } else {
189                         printf("\t\t#%ld: " LLU "\n", (long)proto,
190                             (CAST)hist[proto]);
191                 }
192         }
193 }
194
195 static void
196 print_ipsecstats(void)
197 {
198 #define p(f, m) if (ipsecstat.f || sflag <= 1) \
199     printf(m, (CAST)ipsecstat.f, plural(ipsecstat.f))
200 #define hist(f, n, t) \
201     ipsec_hist((f), sizeof(f)/sizeof(f[0]), (n), (t));
202
203         p(in_success, "\t" LLU " inbound packet%s processed successfully\n");
204         p(in_polvio, "\t" LLU " inbound packet%s violated process security "
205                 "policy\n");
206         p(in_nosa, "\t" LLU " inbound packet%s with no SA available\n");
207         p(in_inval, "\t" LLU " invalid inbound packet%s\n");
208         p(in_nomem, "\t" LLU " inbound packet%s failed due to insufficient memory\n");
209         p(in_badspi, "\t" LLU " inbound packet%s failed getting SPI\n");
210         p(in_ahreplay, "\t" LLU " inbound packet%s failed on AH replay check\n");
211         p(in_espreplay, "\t" LLU " inbound packet%s failed on ESP replay check\n");
212         p(in_ahauthsucc, "\t" LLU " inbound packet%s considered authentic\n");
213         p(in_ahauthfail, "\t" LLU " inbound packet%s failed on authentication\n");
214         hist(ipsecstat.in_ahhist, ipsec_ahnames, "AH input");
215         hist(ipsecstat.in_esphist, ipsec_espnames, "ESP input");
216         hist(ipsecstat.in_comphist, ipsec_compnames, "IPComp input");
217
218         p(out_success, "\t" LLU " outbound packet%s processed successfully\n");
219         p(out_polvio, "\t" LLU " outbound packet%s violated process security "
220                 "policy\n");
221         p(out_nosa, "\t" LLU " outbound packet%s with no SA available\n");
222         p(out_inval, "\t" LLU " invalid outbound packet%s\n");
223         p(out_nomem, "\t" LLU " outbound packet%s failed due to insufficient memory\n");
224         p(out_noroute, "\t" LLU " outbound packet%s with no route\n");
225         hist(ipsecstat.out_ahhist, ipsec_ahnames, "AH output");
226         hist(ipsecstat.out_esphist, ipsec_espnames, "ESP output");
227         hist(ipsecstat.out_comphist, ipsec_compnames, "IPComp output");
228 #undef p
229 #undef hist
230 }
231
232 void
233 ipsec_stats(u_long off __unused, const char *name, int af1 __unused)
234 {
235         if (off == 0)
236                 return;
237         printf ("%s:\n", name);
238         kread(off, (char *)&ipsecstat, sizeof (ipsecstat));
239
240         print_ipsecstats();
241 }
242
243 static const char *
244 pfkey_msgtype_names(int x)
245 {
246         const int max =
247             sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]);
248         static char buf[10];
249
250         if (x < max && pfkey_msgtypenames[x])
251                 return pfkey_msgtypenames[x];
252         snprintf(buf, sizeof(buf), "#%d", x);
253         return buf;
254 }
255
256 void
257 pfkey_stats(u_long off __unused, const char *name, int af1 __unused)
258 {
259         struct pfkeystat pfkeystat;
260         unsigned first, type;
261
262         if (off == 0)
263                 return;
264         printf ("%s:\n", name);
265         kread(off, (char *)&pfkeystat, sizeof(pfkeystat));
266
267 #define p(f, m) if (pfkeystat.f || sflag <= 1) \
268     printf(m, (CAST)pfkeystat.f, plural(pfkeystat.f))
269
270         /* kernel -> userland */
271         p(out_total, "\t" LLU " request%s sent to userland\n");
272         p(out_bytes, "\t" LLU " byte%s sent to userland\n");
273         for (first = 1, type = 0;
274              type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]);
275              type++) {
276                 if (pfkeystat.out_msgtype[type] <= 0)
277                         continue;
278                 if (first) {
279                         printf("\thistogram by message type:\n");
280                         first = 0;
281                 }
282                 printf("\t\t%s: " LLU "\n", pfkey_msgtype_names(type),
283                         (CAST)pfkeystat.out_msgtype[type]);
284         }
285         p(out_invlen, "\t" LLU " message%s with invalid length field\n");
286         p(out_invver, "\t" LLU " message%s with invalid version field\n");
287         p(out_invmsgtype, "\t" LLU " message%s with invalid message type field\n");
288         p(out_tooshort, "\t" LLU " message%s too short\n");
289         p(out_nomem, "\t" LLU " message%s with memory allocation failure\n");
290         p(out_dupext, "\t" LLU " message%s with duplicate extension\n");
291         p(out_invexttype, "\t" LLU " message%s with invalid extension type\n");
292         p(out_invsatype, "\t" LLU " message%s with invalid sa type\n");
293         p(out_invaddr, "\t" LLU " message%s with invalid address extension\n");
294
295         /* userland -> kernel */
296         p(in_total, "\t" LLU " request%s sent from userland\n");
297         p(in_bytes, "\t" LLU " byte%s sent from userland\n");
298         for (first = 1, type = 0;
299              type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]);
300              type++) {
301                 if (pfkeystat.in_msgtype[type] <= 0)
302                         continue;
303                 if (first) {
304                         printf("\thistogram by message type:\n");
305                         first = 0;
306                 }
307                 printf("\t\t%s: " LLU "\n", pfkey_msgtype_names(type),
308                         (CAST)pfkeystat.in_msgtype[type]);
309         }
310         p(in_msgtarget[KEY_SENDUP_ONE],
311             "\t" LLU " message%s toward single socket\n");
312         p(in_msgtarget[KEY_SENDUP_ALL],
313             "\t" LLU " message%s toward all sockets\n");
314         p(in_msgtarget[KEY_SENDUP_REGISTERED],
315             "\t" LLU " message%s toward registered sockets\n");
316         p(in_nomem, "\t" LLU " message%s with memory allocation failure\n");
317 #undef p
318 }
319 #endif /*IPSEC*/