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