1 /* $NetBSD: inet.c,v 1.35.2.1 1999/04/29 14:57:08 perry Exp $ */ 2 /* $KAME: ipsec.c,v 1.33 2003/07/25 09:54:32 itojun Exp $ */ 3 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. 19 * 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. 31 */ 32 33 /* 34 * Copyright (c) 1983, 1988, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 */ 65 66 #if 0 67 #ifndef lint 68 static char sccsid[] = "@(#)inet.c 8.5 (Berkeley) 5/24/95"; 69 #endif /* not lint */ 70 #endif 71 72 #include <sys/cdefs.h> 73 __FBSDID("$FreeBSD$"); 74 75 #include <sys/param.h> 76 #include <sys/queue.h> 77 #include <sys/socket.h> 78 79 #include <netinet/in.h> 80 81 #ifdef IPSEC 82 #include <netinet6/ipsec.h> 83 #include <netkey/keysock.h> 84 #endif 85 86 #include <stdio.h> 87 #include <string.h> 88 #include <unistd.h> 89 #include "netstat.h" 90 91 #ifdef IPSEC 92 struct val2str { 93 int val; 94 const char *str; 95 }; 96 97 static struct val2str ipsec_ahnames[] = { 98 { SADB_AALG_NONE, "none", }, 99 { SADB_AALG_MD5HMAC, "hmac-md5", }, 100 { SADB_AALG_SHA1HMAC, "hmac-sha1", }, 101 { SADB_X_AALG_MD5, "md5", }, 102 { SADB_X_AALG_SHA, "sha", }, 103 { SADB_X_AALG_NULL, "null", }, 104 #ifdef SADB_X_AALG_SHA2_256 105 { SADB_X_AALG_SHA2_256, "hmac-sha2-256", }, 106 #endif 107 #ifdef SADB_X_AALG_SHA2_384 108 { SADB_X_AALG_SHA2_384, "hmac-sha2-384", }, 109 #endif 110 #ifdef SADB_X_AALG_SHA2_512 111 { SADB_X_AALG_SHA2_512, "hmac-sha2-512", }, 112 #endif 113 #ifdef SADB_X_AALG_RIPEMD160HMAC 114 { SADB_X_AALG_RIPEMD160HMAC, "hmac-ripemd160", }, 115 #endif 116 #ifdef SADB_X_AALG_AES_XCBC_MAC 117 { SADB_X_AALG_AES_XCBC_MAC, "aes-xcbc-mac", }, 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 { SADB_X_EALG_CAST128CBC, "cast128-cbc", }, 128 { SADB_X_EALG_BLOWFISHCBC, "blowfish-cbc", }, 129 #ifdef SADB_X_EALG_RIJNDAELCBC 130 { SADB_X_EALG_RIJNDAELCBC, "rijndael-cbc", }, 131 #endif 132 #ifdef SADB_X_EALG_AESCTR 133 { SADB_X_EALG_AESCTR, "aes-ctr", }, 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, size_t histmax, const struct val2str *name, 166 const char *title) 167 { 168 int first; 169 size_t proto; 170 const struct val2str *p; 171 172 first = 1; 173 for (proto = 0; proto < histmax; proto++) { 174 if (hist[proto] <= 0) 175 continue; 176 if (first) { 177 printf("\t%s histogram:\n", title); 178 first = 0; 179 } 180 for (p = name; p && p->str; p++) { 181 if (p->val == (int)proto) 182 break; 183 } 184 if (p && p->str) { 185 printf("\t\t%s: %llu\n", p->str, (unsigned long long)hist[proto]); 186 } else { 187 printf("\t\t#%ld: %llu\n", (long)proto, 188 (unsigned long long)hist[proto]); 189 } 190 } 191 } 192 193 static void 194 print_ipsecstats(void) 195 { 196 #define p(f, m) if (ipsecstat.f || sflag <= 1) \ 197 printf(m, (unsigned long long)ipsecstat.f, plural(ipsecstat.f)) 198 #define pes(f, m) if (ipsecstat.f || sflag <= 1) \ 199 printf(m, (unsigned long long)ipsecstat.f, plurales(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 p(spdcachelookup, "\t%llu SPD cache lookup%s\n"); 229 pes(spdcachemiss, "\t%llu SPD cache miss%s\n"); 230 #undef p 231 #undef pes 232 #undef hist 233 } 234 235 void 236 ipsec_stats(u_long off __unused, const char *name, int af1 __unused) 237 { 238 if (off == 0) 239 return; 240 printf ("%s:\n", name); 241 kread(off, (char *)&ipsecstat, sizeof (ipsecstat)); 242 243 print_ipsecstats(); 244 } 245 246 static const char * 247 pfkey_msgtype_names(int x) 248 { 249 const int max = 250 sizeof(pfkey_msgtypenames)/sizeof(pfkey_msgtypenames[0]); 251 static char buf[20]; 252 253 if (x < max && pfkey_msgtypenames[x]) 254 return pfkey_msgtypenames[x]; 255 snprintf(buf, sizeof(buf), "#%d", x); 256 return buf; 257 } 258 259 void 260 pfkey_stats(u_long off __unused, const char *name, int af1 __unused) 261 { 262 struct pfkeystat pfkeystat; 263 unsigned first, type; 264 265 if (off == 0) 266 return; 267 printf ("%s:\n", name); 268 kread(off, (char *)&pfkeystat, sizeof(pfkeystat)); 269 270 #define p(f, m) if (pfkeystat.f || sflag <= 1) \ 271 printf(m, (unsigned long long)pfkeystat.f, plural(pfkeystat.f)) 272 273 /* userland -> kernel */ 274 p(out_total, "\t%llu request%s sent from userland\n"); 275 p(out_bytes, "\t%llu byte%s sent from userland\n"); 276 for (first = 1, type = 0; 277 type < sizeof(pfkeystat.out_msgtype)/sizeof(pfkeystat.out_msgtype[0]); 278 type++) { 279 if (pfkeystat.out_msgtype[type] <= 0) 280 continue; 281 if (first) { 282 printf("\thistogram by message type:\n"); 283 first = 0; 284 } 285 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 286 (unsigned long long)pfkeystat.out_msgtype[type]); 287 } 288 p(out_invlen, "\t%llu message%s with invalid length field\n"); 289 p(out_invver, "\t%llu message%s with invalid version field\n"); 290 p(out_invmsgtype, "\t%llu message%s with invalid message type field\n"); 291 p(out_tooshort, "\t%llu message%s too short\n"); 292 p(out_nomem, "\t%llu message%s with memory allocation failure\n"); 293 p(out_dupext, "\t%llu message%s with duplicate extension\n"); 294 p(out_invexttype, "\t%llu message%s with invalid extension type\n"); 295 p(out_invsatype, "\t%llu message%s with invalid sa type\n"); 296 p(out_invaddr, "\t%llu message%s with invalid address extension\n"); 297 298 /* kernel -> userland */ 299 p(in_total, "\t%llu request%s sent to userland\n"); 300 p(in_bytes, "\t%llu byte%s sent to userland\n"); 301 for (first = 1, type = 0; 302 type < sizeof(pfkeystat.in_msgtype)/sizeof(pfkeystat.in_msgtype[0]); 303 type++) { 304 if (pfkeystat.in_msgtype[type] <= 0) 305 continue; 306 if (first) { 307 printf("\thistogram by message type:\n"); 308 first = 0; 309 } 310 printf("\t\t%s: %llu\n", pfkey_msgtype_names(type), 311 (unsigned long long)pfkeystat.in_msgtype[type]); 312 } 313 p(in_msgtarget[KEY_SENDUP_ONE], 314 "\t%llu message%s toward single socket\n"); 315 p(in_msgtarget[KEY_SENDUP_ALL], 316 "\t%llu message%s toward all sockets\n"); 317 p(in_msgtarget[KEY_SENDUP_REGISTERED], 318 "\t%llu message%s toward registered sockets\n"); 319 p(in_nomem, "\t%llu message%s with memory allocation failure\n"); 320 #undef p 321 } 322 #endif /*IPSEC*/ 323