1 /* $NetBSD: netstat.c,v 1.30 2018/05/03 07:13:48 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)netstat.c 8.1 (Berkeley) 6/6/93"; 36 #endif 37 __RCSID("$NetBSD: netstat.c,v 1.30 2018/05/03 07:13:48 maxv Exp $"); 38 #endif /* not lint */ 39 40 /* 41 * netstat 42 */ 43 #include <sys/param.h> 44 #include <sys/socketvar.h> 45 #include <sys/mbuf.h> 46 #include <sys/protosw.h> 47 48 #include <netinet/in.h> 49 50 #include <arpa/inet.h> 51 #include <net/route.h> 52 53 #include <netinet/in_systm.h> 54 #include <netinet/ip.h> 55 #include <netinet/in_pcb.h> 56 #include <netinet/ip_icmp.h> 57 #include <netinet/icmp_var.h> 58 #include <netinet/ip_var.h> 59 #ifdef INET6 60 #include <netinet/ip6.h> 61 #include <netinet6/in6_pcb.h> 62 #endif 63 #include <netinet/tcp.h> 64 #include <netinet/tcp_seq.h> 65 #define TCPSTATES 66 #include <netinet/tcp_fsm.h> 67 #include <netinet/tcp_timer.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/tcp_debug.h> 70 #include <netinet/udp.h> 71 #include <netinet/udp_var.h> 72 73 #include <netdb.h> 74 #include <stdlib.h> 75 #include <string.h> 76 77 #include "systat.h" 78 #include "extern.h" 79 80 static void fetchnetstat4(void *, int); 81 static void enter(struct inpcb *, struct socket *, int, const char *); 82 static const char *inetname(struct in_addr); 83 static void inetprint(struct in_addr *, int, const char *); 84 #ifdef INET6 85 static void fetchnetstat6(void *, int); 86 static void enter6(struct in6pcb *, struct socket *, int, const char *); 87 static const char *inet6name(struct in6_addr *); 88 static void inet6print(struct in6_addr *, int, const char *); 89 #endif 90 91 #define streq(a,b) (strcmp(a,b)==0) 92 93 struct netinfo { 94 struct netinfo *ni_forw, *ni_prev; 95 int ni_family; 96 short ni_line; /* line on screen */ 97 short ni_seen; /* 0 when not present in list */ 98 short ni_flags; 99 #define NIF_LACHG 0x1 /* local address changed */ 100 #define NIF_FACHG 0x2 /* foreign address changed */ 101 short ni_state; /* tcp state */ 102 const char *ni_proto; /* protocol */ 103 struct in_addr ni_laddr; /* local address */ 104 #ifdef INET6 105 struct in6_addr ni_laddr6; /* local address */ 106 #endif 107 long ni_lport; /* local port */ 108 struct in_addr ni_faddr; /* foreign address */ 109 #ifdef INET6 110 struct in6_addr ni_faddr6; /* foreign address */ 111 #endif 112 long ni_fport; /* foreign port */ 113 long ni_rcvcc; /* rcv buffer character count */ 114 long ni_sndcc; /* snd buffer character count */ 115 }; 116 117 static struct { 118 struct netinfo *ni_forw, *ni_prev; 119 } netcb; 120 121 struct netinfo *nhead; 122 123 static int aflag = 0; 124 int nflag = 0; 125 static int lastrow = 1; 126 127 WINDOW * 128 opennetstat(void) 129 { 130 131 sethostent(1); 132 setnetent(1); 133 return (subwin(stdscr, -1, 0, 5, 0)); 134 } 135 136 void 137 closenetstat(WINDOW *w) 138 { 139 struct netinfo *p; 140 141 endhostent(); 142 endnetent(); 143 p = netcb.ni_forw; 144 while (p != nhead) { 145 if (p->ni_line != -1) 146 lastrow--; 147 p->ni_line = -1; 148 p = p->ni_forw; 149 } 150 if (w != NULL) { 151 wclear(w); 152 wrefresh(w); 153 delwin(w); 154 } 155 } 156 157 static struct nlist namelist[] = { 158 #define X_TCBTABLE 0 159 { .n_name = "_tcbtable" }, 160 #define X_UDBTABLE 1 161 { .n_name = "_udbtable" }, 162 { .n_name = NULL }, 163 }; 164 165 int 166 initnetstat(void) 167 { 168 int n; 169 170 n = kvm_nlist(kd, namelist); 171 if (n < 0) { 172 nlisterr(namelist); 173 return(0); 174 } else if (n == sizeof(namelist) / sizeof(namelist[0]) - 1) { 175 error("No symbols in namelist"); 176 return(0); 177 } 178 179 nhead = (struct netinfo *)(void *)&netcb; 180 181 netcb.ni_forw = netcb.ni_prev = nhead; 182 protos = TCP|UDP; 183 return(1); 184 } 185 186 void 187 fetchnetstat(void) 188 { 189 struct netinfo *p; 190 191 if (namelist[X_TCBTABLE].n_value == 0) 192 return; 193 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) 194 p->ni_seen = 0; 195 196 if ((protos & (TCP | UDP)) == 0) { 197 error("No protocols to display"); 198 return; 199 } 200 if ((protos & TCP) && namelist[X_TCBTABLE].n_type) 201 fetchnetstat4(NPTR(X_TCBTABLE), 1); 202 if ((protos & UDP) && namelist[X_UDBTABLE].n_type) 203 fetchnetstat4(NPTR(X_UDBTABLE), 0); 204 #ifdef INET6 205 if ((protos & TCP) && namelist[X_TCBTABLE].n_type) 206 fetchnetstat6(NPTR(X_TCBTABLE), 1); 207 if ((protos & UDP) && namelist[X_UDBTABLE].n_type) 208 fetchnetstat6(NPTR(X_UDBTABLE), 0); 209 #endif 210 } 211 212 static void 213 fetchnetstat4(void *off, int istcp) 214 { 215 struct inpcbtable pcbtable; 216 struct inpcb_hdr **pprev, *next; 217 struct netinfo *p; 218 struct inpcb inpcb, *inpcbp; 219 struct socket sockb; 220 struct tcpcb tcpcb; 221 222 KREAD(off, &pcbtable, sizeof pcbtable); 223 pprev = &((struct inpcbtable *)off)->inpt_queue.tqh_first; 224 next = TAILQ_FIRST(&pcbtable.inpt_queue); 225 while (next != TAILQ_END(&pcbtable.inpt_queue)) { 226 inpcbp = (struct inpcb *)next; 227 KREAD(inpcbp, &inpcb, sizeof (inpcb)); 228 if (inpcb.inp_queue.tqe_prev != pprev) { 229 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) 230 p->ni_seen = 1; 231 error("Kernel state in transition"); 232 return; 233 } 234 pprev = &next->inph_queue.tqe_next; 235 next = inpcb.inp_queue.tqe_next; 236 237 if (inpcb.inp_af != AF_INET) 238 continue; 239 if (!aflag && inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) 240 continue; 241 if (nhosts && !checkhost(&inpcb)) 242 continue; 243 if (nports && !checkport(&inpcb)) 244 continue; 245 KREAD(inpcb.inp_socket, &sockb, sizeof (sockb)); 246 if (istcp) { 247 KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb)); 248 enter(&inpcb, &sockb, tcpcb.t_state, "tcp"); 249 } else 250 enter(&inpcb, &sockb, 0, "udp"); 251 } 252 } 253 254 #ifdef INET6 255 static void 256 fetchnetstat6(void *off, int istcp) 257 { 258 struct inpcbtable pcbtable; 259 struct inpcb_hdr **pprev, *next; 260 struct netinfo *p; 261 struct socket sockb; 262 struct tcpcb tcpcb; 263 struct in6pcb in6pcb, *in6pcbp; 264 265 KREAD(off, &pcbtable, sizeof pcbtable); 266 pprev = &((struct inpcbtable *)off)->inpt_queue.tqh_first; 267 next = TAILQ_FIRST(&pcbtable.inpt_queue); 268 while (next != TAILQ_END(&pcbtable.inpt_queue)) { 269 in6pcbp = (struct in6pcb *)next; 270 KREAD(in6pcbp, &in6pcb, sizeof (in6pcb)); 271 if (in6pcb.in6p_queue.tqe_prev != pprev) { 272 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) 273 p->ni_seen = 1; 274 error("Kernel state in transition"); 275 return; 276 } 277 pprev = &next->inph_queue.tqe_next; 278 next = in6pcb.in6p_queue.tqe_next; 279 280 if (in6pcb.in6p_af != AF_INET6) 281 continue; 282 if (!aflag && IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr)) 283 continue; 284 if (nhosts && !checkhost6(&in6pcb)) 285 continue; 286 if (nports && !checkport6(&in6pcb)) 287 continue; 288 KREAD(in6pcb.in6p_socket, &sockb, sizeof (sockb)); 289 if (istcp) { 290 KREAD(in6pcb.in6p_ppcb, &tcpcb, sizeof (tcpcb)); 291 enter6(&in6pcb, &sockb, tcpcb.t_state, "tcp"); 292 } else 293 enter6(&in6pcb, &sockb, 0, "udp"); 294 } 295 } 296 #endif /*INET6*/ 297 298 static void 299 enter(struct inpcb *inp, struct socket *so, int state, const char *proto) 300 { 301 struct netinfo *p; 302 303 /* 304 * Only take exact matches, any sockets with 305 * previously unbound addresses will be deleted 306 * below in the display routine because they 307 * will appear as ``not seen'' in the kernel 308 * data structures. 309 */ 310 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) { 311 if (p->ni_family != AF_INET) 312 continue; 313 if (!streq(proto, p->ni_proto)) 314 continue; 315 if (p->ni_lport != inp->inp_lport || 316 p->ni_laddr.s_addr != inp->inp_laddr.s_addr) 317 continue; 318 if (p->ni_faddr.s_addr == inp->inp_faddr.s_addr && 319 p->ni_fport == inp->inp_fport) 320 break; 321 } 322 if (p == nhead) { 323 if ((p = malloc(sizeof(*p))) == NULL) { 324 error("Out of memory"); 325 return; 326 } 327 p->ni_prev = nhead; 328 p->ni_forw = netcb.ni_forw; 329 netcb.ni_forw->ni_prev = p; 330 netcb.ni_forw = p; 331 p->ni_line = -1; 332 p->ni_laddr = inp->inp_laddr; 333 p->ni_lport = inp->inp_lport; 334 p->ni_faddr = inp->inp_faddr; 335 p->ni_fport = inp->inp_fport; 336 p->ni_proto = proto; 337 p->ni_flags = NIF_LACHG | NIF_FACHG; 338 p->ni_family = AF_INET; 339 } 340 p->ni_rcvcc = so->so_rcv.sb_cc; 341 p->ni_sndcc = so->so_snd.sb_cc; 342 p->ni_state = state; 343 p->ni_seen = 1; 344 } 345 346 #ifdef INET6 347 static void 348 enter6(struct in6pcb *in6p, struct socket *so, int state, const char *proto) 349 { 350 struct netinfo *p; 351 352 /* 353 * Only take exact matches, any sockets with 354 * previously unbound addresses will be deleted 355 * below in the display routine because they 356 * will appear as ``not seen'' in the kernel 357 * data structures. 358 */ 359 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) { 360 if (p->ni_family != AF_INET6) 361 continue; 362 if (!streq(proto, p->ni_proto)) 363 continue; 364 if (p->ni_lport != in6p->in6p_lport || 365 !IN6_ARE_ADDR_EQUAL(&p->ni_laddr6, &in6p->in6p_laddr)) 366 continue; 367 if (IN6_ARE_ADDR_EQUAL(&p->ni_faddr6, &in6p->in6p_faddr) && 368 p->ni_fport == in6p->in6p_fport) 369 break; 370 } 371 if (p == nhead) { 372 if ((p = malloc(sizeof(*p))) == NULL) { 373 error("Out of memory"); 374 return; 375 } 376 p->ni_prev = nhead; 377 p->ni_forw = netcb.ni_forw; 378 netcb.ni_forw->ni_prev = p; 379 netcb.ni_forw = p; 380 p->ni_line = -1; 381 p->ni_laddr6 = in6p->in6p_laddr; 382 p->ni_lport = in6p->in6p_lport; 383 p->ni_faddr6 = in6p->in6p_faddr; 384 p->ni_fport = in6p->in6p_fport; 385 p->ni_proto = proto; 386 p->ni_flags = NIF_LACHG | NIF_FACHG; 387 p->ni_family = AF_INET6; 388 } 389 p->ni_rcvcc = so->so_rcv.sb_cc; 390 p->ni_sndcc = so->so_snd.sb_cc; 391 p->ni_state = state; 392 p->ni_seen = 1; 393 } 394 #endif 395 396 /* column locations */ 397 #define LADDR 0 398 #define FADDR LADDR+23 399 #define PROTO FADDR+23 400 #define RCVCC PROTO+6 401 #define SNDCC RCVCC+7 402 #define STATE SNDCC+7 403 404 void 405 labelnetstat(void) 406 { 407 struct netinfo *p; 408 409 if (namelist[X_TCBTABLE].n_type == 0) 410 return; 411 wmove(wnd, 0, 0); wclrtobot(wnd); 412 mvwaddstr(wnd, 0, LADDR, "Local Address"); 413 mvwaddstr(wnd, 0, FADDR, "Foreign Address"); 414 mvwaddstr(wnd, 0, PROTO, "Proto"); 415 mvwaddstr(wnd, 0, RCVCC, "Recv-Q"); 416 mvwaddstr(wnd, 0, SNDCC, "Send-Q"); 417 mvwaddstr(wnd, 0, STATE, "(state)"); 418 419 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) { 420 if (p->ni_line == -1) 421 continue; 422 p->ni_flags |= NIF_LACHG | NIF_FACHG; 423 } 424 } 425 426 void 427 shownetstat(void) 428 { 429 struct netinfo *p, *q; 430 431 /* 432 * First, delete any connections that have gone 433 * away and adjust the position of connections 434 * below to reflect the deleted line. 435 */ 436 p = netcb.ni_forw; 437 while (p != nhead) { 438 if (p->ni_line == -1 || p->ni_seen) { 439 p = p->ni_forw; 440 continue; 441 } 442 wmove(wnd, p->ni_line, 0); 443 wdeleteln(wnd); 444 for (q = netcb.ni_forw; q != nhead; q = q->ni_forw) 445 if (q != p && q->ni_line > p->ni_line) { 446 q->ni_line--; 447 /* this shouldn't be necessary */ 448 q->ni_flags |= NIF_LACHG | NIF_FACHG; 449 } 450 lastrow--; 451 q = p->ni_forw; 452 p->ni_prev->ni_forw = p->ni_forw; 453 p->ni_forw->ni_prev = p->ni_prev; 454 free(p); 455 p = q; 456 } 457 /* 458 * Update existing connections and add new ones. 459 */ 460 for (p = netcb.ni_forw; p != nhead; p = p->ni_forw) { 461 if (p->ni_line == -1) { 462 /* 463 * Add a new entry if possible. 464 */ 465 if (lastrow > getmaxy(wnd)) 466 continue; 467 p->ni_line = lastrow++; 468 p->ni_flags |= NIF_LACHG | NIF_FACHG; 469 } 470 if (p->ni_flags & NIF_LACHG) { 471 wmove(wnd, p->ni_line, LADDR); 472 switch (p->ni_family) { 473 case AF_INET: 474 inetprint(&p->ni_laddr, p->ni_lport, 475 p->ni_proto); 476 break; 477 #ifdef INET6 478 case AF_INET6: 479 inet6print(&p->ni_laddr6, p->ni_lport, 480 p->ni_proto); 481 break; 482 #endif 483 } 484 p->ni_flags &= ~NIF_LACHG; 485 } 486 if (p->ni_flags & NIF_FACHG) { 487 wmove(wnd, p->ni_line, FADDR); 488 switch (p->ni_family) { 489 case AF_INET: 490 inetprint(&p->ni_faddr, p->ni_fport, 491 p->ni_proto); 492 break; 493 #ifdef INET6 494 case AF_INET6: 495 inet6print(&p->ni_faddr6, p->ni_fport, 496 p->ni_proto); 497 break; 498 #endif 499 } 500 p->ni_flags &= ~NIF_FACHG; 501 } 502 mvwaddstr(wnd, p->ni_line, PROTO, p->ni_proto); 503 #ifdef INET6 504 if (p->ni_family == AF_INET6) 505 waddstr(wnd, "6"); 506 #endif 507 mvwprintw(wnd, p->ni_line, RCVCC, "%6ld", p->ni_rcvcc); 508 mvwprintw(wnd, p->ni_line, SNDCC, "%6ld", p->ni_sndcc); 509 if (streq(p->ni_proto, "tcp")) { 510 if (p->ni_state < 0 || p->ni_state >= TCP_NSTATES) 511 mvwprintw(wnd, p->ni_line, STATE, "%d", 512 p->ni_state); 513 else 514 mvwaddstr(wnd, p->ni_line, STATE, 515 tcpstates[p->ni_state]); 516 } 517 wclrtoeol(wnd); 518 } 519 if (lastrow < getmaxy(wnd)) { 520 wmove(wnd, lastrow, 0); wclrtobot(wnd); 521 wmove(wnd, getmaxy(wnd), 0); wdeleteln(wnd); /* XXX */ 522 } 523 } 524 525 /* 526 * Pretty print an Internet address (net address + port). 527 * If the nflag was specified, use numbers instead of names. 528 */ 529 static void 530 inetprint(struct in_addr *in, int port, const char *proto) 531 { 532 struct servent *sp = 0; 533 char line[80], *cp; 534 535 (void)snprintf(line, sizeof line, "%.*s.", 16, inetname(*in)); 536 cp = strchr(line, '\0'); 537 if (!nflag && port) 538 sp = getservbyport(port, proto); 539 if (sp || port == 0) 540 (void)snprintf(cp, line + sizeof line - cp, "%.8s", 541 sp ? sp->s_name : "*"); 542 else 543 (void)snprintf(cp, line + sizeof line - cp, "%d", 544 ntohs((u_short)port)); 545 /* pad to full column to clear any garbage */ 546 cp = strchr(line, '\0'); 547 while (cp - line < 22) 548 *cp++ = ' '; 549 *cp = '\0'; 550 waddstr(wnd, line); 551 } 552 553 #ifdef INET6 554 static void 555 inet6print(struct in6_addr *in6, int port, const char *proto) 556 { 557 struct servent *sp = 0; 558 char line[80], *cp; 559 560 (void)snprintf(line, sizeof line, "%.*s.", 16, inet6name(in6)); 561 cp = strchr(line, '\0'); 562 if (!nflag && port) 563 sp = getservbyport(port, proto); 564 if (sp || port == 0) 565 (void)snprintf(cp, line + sizeof line - cp, "%.8s", 566 sp ? sp->s_name : "*"); 567 else 568 (void)snprintf(cp, line + sizeof line - cp, "%d", 569 ntohs((u_short)port)); 570 /* pad to full column to clear any garbage */ 571 cp = strchr(line, '\0'); 572 while (cp - line < 22) 573 *cp++ = ' '; 574 *cp = '\0'; 575 waddstr(wnd, line); 576 } 577 #endif 578 579 /* 580 * Construct an Internet address representation. 581 * If the nflag has been supplied, give 582 * numeric value, otherwise try for symbolic name. 583 */ 584 static const char * 585 inetname(struct in_addr in) 586 { 587 char *cp = 0; 588 static char line[50]; 589 struct hostent *hp; 590 struct netent *np; 591 592 if (!nflag && in.s_addr != INADDR_ANY) { 593 int net = inet_netof(in); 594 int lna = inet_lnaof(in); 595 596 if (lna == INADDR_ANY) { 597 np = getnetbyaddr(net, AF_INET); 598 if (np) 599 cp = np->n_name; 600 } 601 if (cp == 0) { 602 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET); 603 if (hp) 604 cp = hp->h_name; 605 } 606 } 607 if (in.s_addr == INADDR_ANY) 608 strlcpy(line, "*", sizeof(line)); 609 else if (cp) 610 strlcpy(line, cp, sizeof(line)); 611 else { 612 in.s_addr = ntohl(in.s_addr); 613 #define C(x) ((x) & 0xff) 614 (void)snprintf(line, sizeof line, "%u.%u.%u.%u", 615 C(in.s_addr >> 24), C(in.s_addr >> 16), 616 C(in.s_addr >> 8), C(in.s_addr)); 617 #undef C 618 } 619 return (line); 620 } 621 622 #ifdef INET6 623 static const char * 624 inet6name(struct in6_addr *in6) 625 { 626 static char line[NI_MAXHOST]; 627 struct sockaddr_in6 sin6; 628 int flags; 629 630 if (nflag) 631 flags = NI_NUMERICHOST; 632 else 633 flags = 0; 634 if (IN6_IS_ADDR_UNSPECIFIED(in6)) 635 return "*"; 636 memset(&sin6, 0, sizeof(sin6)); 637 sin6.sin6_family = AF_INET6; 638 sin6.sin6_len = sizeof(struct sockaddr_in6); 639 sin6.sin6_addr = *in6; 640 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 641 line, sizeof(line), NULL, 0, flags) == 0) 642 return line; 643 return "?"; 644 } 645 #endif 646 647 /* please note: there are also some netstat commands in netcmds.c */ 648 649 void 650 netstat_all(char *args) 651 { 652 aflag = !aflag; 653 fetchnetstat(); 654 shownetstat(); 655 refresh(); 656 } 657 658 void 659 netstat_names(char *args) 660 { 661 662 if (nflag == 0) 663 return; 664 665 nflag = 0; 666 wclear(wnd); 667 labelnetstat(); 668 shownetstat(); 669 refresh(); 670 } 671 672 void 673 netstat_numbers(char *args) 674 { 675 676 if (nflag != 0) 677 return; 678 679 nflag = 1; 680 wclear(wnd); 681 labelnetstat(); 682 shownetstat(); 683 refresh(); 684 } 685