1 /* $NetBSD: vtw.c,v 1.10 2018/05/03 07:13:48 maxv Exp $ */ 2 3 /* 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Coyote Point Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1983, 1988, 1993 33 * The Regents of the University of California. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60 #include <sys/cdefs.h> 61 #ifndef lint 62 #if 0 63 static char sccsid[] = "from: @(#)inet.c 8.4 (Berkeley) 4/20/94"; 64 #else 65 __RCSID("$NetBSD: vtw.c,v 1.10 2018/05/03 07:13:48 maxv Exp $"); 66 #endif 67 #endif /* not lint */ 68 69 #define _CALLOUT_PRIVATE /* for defs in sys/callout.h */ 70 71 #include <sys/param.h> 72 #include <sys/queue.h> 73 #include <sys/socket.h> 74 #include <sys/socketvar.h> 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/sysctl.h> 78 79 #include <net/if_arp.h> 80 #include <net/route.h> 81 #include <netinet/in.h> 82 #include <netinet/in_systm.h> 83 #include <netinet/ip.h> 84 #include <netinet/in_pcb.h> 85 #include <netinet/ip_icmp.h> 86 87 #ifdef INET6 88 #include <netinet/ip6.h> 89 #endif 90 91 #include <netinet/icmp_var.h> 92 #include <netinet/igmp_var.h> 93 #include <netinet/ip_var.h> 94 #include <netinet/pim_var.h> 95 #include <netinet/tcp.h> 96 #include <netinet/tcp_seq.h> 97 #include <netinet/tcp_fsm.h> 98 #include <netinet/tcp_timer.h> 99 #include <netinet/tcp_var.h> 100 #include <netinet/tcp_debug.h> 101 #include <netinet/udp.h> 102 #include <netinet/ip_carp.h> 103 #include <netinet/udp_var.h> 104 #include <netinet/tcp_vtw.h> 105 106 #include <arpa/inet.h> 107 #include <kvm.h> 108 #include <netdb.h> 109 #include <stdio.h> 110 #include <string.h> 111 #include <unistd.h> 112 #include <stdlib.h> 113 #include <err.h> 114 #include "netstat.h" 115 #include "vtw.h" 116 #include "prog_ops.h" 117 118 static void snarf(const void *, void *, size_t); 119 static void *lookup(const char *); 120 static void process_vtw(const vtw_ctl_t *, void (*)(const vtw_t *)); 121 122 static void 123 snarf(const void *addr, void *buf, size_t len) 124 { 125 size_t cc; 126 127 memset(buf, 0, len); 128 129 cc = kvm_read(get_kvmd(), (unsigned long) addr, buf, len); 130 131 if (cc != len) { 132 warnx("%s: short read at %p, len %zx cc %zx", __func__, addr, 133 len, cc); 134 } 135 } 136 137 static void * 138 lookup(const char *name) 139 { 140 kvm_t *k; 141 struct nlist nl[2]; 142 143 nl[0].n_name = name; 144 nl[0].n_value = 0; 145 nl[1].n_name = NULL; 146 147 if ((k = get_kvmd()) == NULL) { 148 if (Vflag) 149 errx(EXIT_FAILURE, "kvm not available"); 150 return NULL; 151 } 152 switch (kvm_nlist(k, &nl[0])) { 153 case -1: 154 err(EXIT_FAILURE, "kvm_nlist"); 155 break; 156 157 case 0: 158 return (void *)nl[0].n_value; 159 160 default: 161 if (Vflag) 162 errx(EXIT_FAILURE, "%s missing in symbol table", name); 163 break; 164 } 165 166 return NULL; 167 } 168 169 void 170 timebase(struct timeval *tv) 171 { 172 void *p; 173 struct bintime timebasebin; 174 175 p = lookup("timebasebin"); 176 if (!p) 177 return; 178 snarf(p, &timebasebin, sizeof(timebasebin)); 179 bintime2timeval(&timebasebin, tv); 180 } 181 182 static void 183 process_vtw(const vtw_ctl_t * ctl, void (*print)(const vtw_t *)) 184 { 185 vtw_t *vp; 186 187 for (vp = ctl->base.v; vp && vp <= ctl->lim.v;) { 188 189 (*print)(vp); 190 191 if (ctl->is_v4) { 192 vtw_v4_t *v4 = (vtw_v4_t *)vp; 193 194 vp = &(++v4)->common; 195 } else if (ctl->is_v6) { 196 vtw_v6_t *v6 = (vtw_v6_t *)vp; 197 198 vp = &(++v6)->common; 199 } 200 } 201 } 202 203 void 204 show_vtw_stats(void) 205 { 206 vtw_stats_t stats; 207 void *p; 208 209 if (!Vflag) 210 return; 211 212 if ((p = lookup("vtw_stats")) == NULL) 213 return; 214 snarf(p, &stats, sizeof(stats)); 215 216 printf("\t\t%" PRIu64 " inserts\n", stats.ins); 217 printf("\t\t%" PRIu64 " deletes\n", stats.del); 218 printf("\t\t%" PRIu64 " assassinations\n", stats.kill); 219 printf("\tvestigial time-wait lookup_connect\n"); 220 printf("\t\t%" PRIu64 " look\n", stats.look[0]); 221 printf("\t\t%" PRIu64 " hit\n", stats.hit[0]); 222 printf("\t\t%" PRIu64 " miss\n", stats.miss[0]); 223 printf("\t\t%" PRIu64 " probe\n", stats.probe[0]); 224 printf("\t\t%" PRIu64 " losing\n", stats.losing[0]); 225 printf("\t\t%" PRIu64 " max_chain\n", stats.max_chain[0]); 226 printf("\t\t%" PRIu64 " max_probe\n", stats.max_probe[0]); 227 printf("\t\t%" PRIu64 " max_loss\n", stats.max_loss[0]); 228 printf("\tvestigial time-wait lookup_port\n"); 229 printf("\t\t%" PRIu64 " look\n", stats.look[1]); 230 printf("\t\t%" PRIu64 " hit\n", stats.hit[1]); 231 printf("\t\t%" PRIu64 " miss\n", stats.miss[1]); 232 printf("\t\t%" PRIu64 " probe\n", stats.probe[1]); 233 printf("\t\t%" PRIu64 " losing\n", stats.losing[1]); 234 printf("\t\t%" PRIu64 " max_chain\n", stats.max_chain[1]); 235 printf("\t\t%" PRIu64 " max_probe\n", stats.max_probe[1]); 236 printf("\t\t%" PRIu64 " max_loss\n", stats.max_loss[1]); 237 } 238 239 void 240 show_vtw_v4(void (*print)(const vtw_t *)) 241 { 242 fatp_t *base, *lim; 243 fatp_t **hash, **port; 244 size_t n; 245 fatp_ctl_t fat_tcpv4; 246 vtw_ctl_t vtw_tcpv4[VTW_NCLASS]; 247 int i; 248 int mem = 0; 249 void *p; 250 251 if ((p = lookup("fat_tcpv4")) == NULL) 252 return; 253 snarf(p, &fat_tcpv4, sizeof(fat_tcpv4)); 254 255 if ((p = lookup("vtw_tcpv4")) == NULL) 256 return; 257 snarf(p, &vtw_tcpv4[0], sizeof(vtw_tcpv4)); 258 259 mem += sizeof(fat_tcpv4); 260 mem += sizeof(vtw_tcpv4); 261 262 /* snarf/adjust vtw_ctl */ 263 for (i = 0; i < VTW_NCLASS; ++i) { 264 vtw_v4_t *kbase, *klim; 265 vtw_v4_t *ubase; 266 ptrdiff_t delta; 267 268 kbase = vtw_tcpv4[i].base.v4; 269 klim = vtw_tcpv4[i].lim.v4; 270 271 if (!kbase || !klim) 272 continue; 273 274 n = (klim - kbase + 1); 275 276 if (!i) { 277 if ((ubase = malloc(n * sizeof(*kbase))) == NULL) 278 err(EXIT_FAILURE, NULL); 279 snarf(kbase, ubase, n * sizeof(*ubase)); 280 281 mem += n * sizeof(*ubase); 282 } else { 283 ubase = vtw_tcpv4[0].base.v4; 284 } 285 286 delta = ubase - kbase; 287 288 vtw_tcpv4[i].base.v4 += delta; 289 vtw_tcpv4[i].lim.v4 += delta; 290 vtw_tcpv4[i].alloc.v4 += delta; 291 vtw_tcpv4[i].fat = &fat_tcpv4; 292 293 if (vtw_tcpv4[i].oldest.v4) 294 vtw_tcpv4[i].oldest.v4 += delta; 295 } 296 297 /* snarf/adjust fat_ctl */ 298 299 base = fat_tcpv4.base; 300 lim = fat_tcpv4.lim; 301 302 if (!base || !lim) 303 goto end; 304 305 mem += (lim - base + 1) * sizeof(*base); 306 307 fat_tcpv4.base = malloc((lim - base + 1) * sizeof(*base)); 308 if (fat_tcpv4.base == NULL) 309 err(EXIT_FAILURE, NULL); 310 fat_tcpv4.lim = fat_tcpv4.base + (lim - base); 311 312 snarf(base, fat_tcpv4.base, sizeof(*base) * (lim - base + 1)); 313 314 fat_tcpv4.vtw = &vtw_tcpv4[0]; 315 fat_tcpv4.free = fat_tcpv4.base + (fat_tcpv4.free - base); 316 317 n = fat_tcpv4.mask + 1; 318 hash = fat_tcpv4.hash; 319 port = fat_tcpv4.port; 320 321 fat_tcpv4.hash = malloc(n * sizeof(*hash)); 322 fat_tcpv4.port = malloc(n * sizeof(*port)); 323 if (fat_tcpv4.hash == NULL || fat_tcpv4.port == NULL) 324 err(EXIT_FAILURE, NULL); 325 326 snarf(hash, fat_tcpv4.hash, n * sizeof(*hash)); 327 snarf(port, fat_tcpv4.port, n * sizeof(*port)); 328 329 end: 330 process_vtw(&vtw_tcpv4[0], print); 331 332 #if 0 333 if (Vflag && vflag) { 334 printf("total memory for VTW in current config: %d bytes %f MB\n" 335 ,mem 336 ,mem / (1024.0 * 1024)); 337 } 338 #endif 339 } 340 341 void 342 show_vtw_v6(void (*print)(const vtw_t *)) 343 { 344 fatp_t *base, *lim; 345 fatp_t **hash, **port; 346 size_t n; 347 fatp_ctl_t fat_tcpv6; 348 vtw_ctl_t vtw_tcpv6[VTW_NCLASS]; 349 int i; 350 int mem = 0; 351 void *p; 352 353 if ((p = lookup("fat_tcpv6")) == NULL) 354 return; 355 snarf(p, &fat_tcpv6, sizeof(fat_tcpv6)); 356 if ((p = lookup("vtw_tcpv6")) == NULL) 357 return; 358 snarf(p, &vtw_tcpv6[0], sizeof(vtw_tcpv6)); 359 360 mem += sizeof(fat_tcpv6); 361 mem += sizeof(vtw_tcpv6); 362 363 for (i = 0; i < VTW_NCLASS; ++i) { 364 vtw_v6_t *kbase, *klim; 365 vtw_v6_t *ubase; 366 ptrdiff_t delta; 367 368 kbase = vtw_tcpv6[i].base.v6; 369 klim = vtw_tcpv6[i].lim.v6; 370 371 if (!kbase || !klim) 372 continue; 373 374 n = (klim - kbase + 1); 375 376 if (!i) { 377 if ((ubase = malloc(n * sizeof(*kbase))) == NULL) 378 err(EXIT_FAILURE, NULL); 379 380 snarf(kbase, ubase, n * sizeof(*ubase)); 381 382 mem += n * sizeof(*ubase); 383 } else { 384 ubase = vtw_tcpv6[0].base.v6; 385 } 386 387 delta = ubase - kbase; 388 389 vtw_tcpv6[i].base.v6 += delta; 390 vtw_tcpv6[i].lim.v6 += delta; 391 vtw_tcpv6[i].alloc.v6 += delta; 392 vtw_tcpv6[i].fat = &fat_tcpv6; 393 394 if (vtw_tcpv6[i].oldest.v6) 395 vtw_tcpv6[i].oldest.v6 += delta; 396 } 397 398 base = fat_tcpv6.base; 399 lim = fat_tcpv6.lim; 400 401 if (!base || !lim) 402 goto end; 403 404 mem += (lim - base + 1) * sizeof(*base); 405 406 fat_tcpv6.base = malloc((lim - base + 1) * sizeof(*base)); 407 if (fat_tcpv6.base == NULL) 408 err(EXIT_FAILURE, NULL); 409 fat_tcpv6.lim = fat_tcpv6.base + (lim - base); 410 411 snarf(base, fat_tcpv6.base, sizeof(*base) * (lim - base + 1)); 412 413 fat_tcpv6.vtw = &vtw_tcpv6[0]; 414 fat_tcpv6.free = fat_tcpv6.base + (fat_tcpv6.free - base); 415 416 n = fat_tcpv6.mask + 1; 417 hash = fat_tcpv6.hash; 418 port = fat_tcpv6.port; 419 420 fat_tcpv6.hash = malloc(n * sizeof(*hash)); 421 fat_tcpv6.port = malloc(n * sizeof(*port)); 422 if (fat_tcpv6.hash == NULL || fat_tcpv6.port == NULL) 423 err(EXIT_FAILURE, NULL); 424 425 snarf(hash, fat_tcpv6.hash, n * sizeof(*hash)); 426 snarf(port, fat_tcpv6.port, n * sizeof(*port)); 427 428 end: 429 430 process_vtw(&vtw_tcpv6[0], print); 431 #if 0 432 if (Vflag && vflag) { 433 printf("total memory for VTW in current config: %d bytes %f MB\n" 434 ,mem 435 ,mem / (1024.0 * 1024)); 436 } 437 #endif 438 } 439