1 /*- 2 * Copyright (c) 2002 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/socket.h> 34 #include <sys/socketvar.h> 35 #include <sys/sysctl.h> 36 #include <sys/file.h> 37 #include <sys/user.h> 38 39 #include <sys/un.h> 40 #include <sys/unpcb.h> 41 42 #include <net/route.h> 43 44 #include <netinet/in.h> 45 #include <netinet/in_pcb.h> 46 #include <netinet/tcp.h> 47 #include <netinet/tcp_seq.h> 48 #include <netinet/tcp_var.h> 49 #include <arpa/inet.h> 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <errno.h> 54 #include <netdb.h> 55 #include <pwd.h> 56 #include <stdarg.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 static int opt_4; /* Show IPv4 sockets */ 63 static int opt_6; /* Show IPv6 sockets */ 64 static int opt_c; /* Show connected sockets */ 65 static int opt_l; /* Show listening sockets */ 66 static int opt_u; /* Show Unix domain sockets */ 67 static int opt_v; /* Verbose mode */ 68 69 static int *ports; 70 71 #define INT_BIT (sizeof(int)*CHAR_BIT) 72 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0) 73 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT))) 74 75 struct sock { 76 void *socket; 77 void *pcb; 78 int vflag; 79 int family; 80 int proto; 81 const char *protoname; 82 struct sockaddr_storage laddr; 83 struct sockaddr_storage faddr; 84 struct sock *next; 85 }; 86 87 #define HASHSIZE 1009 88 static struct sock *sockhash[HASHSIZE]; 89 90 static struct xfile *xfiles; 91 static int nxfiles; 92 93 static int 94 xprintf(const char *fmt, ...) 95 { 96 va_list ap; 97 int len; 98 99 va_start(ap, fmt); 100 len = vprintf(fmt, ap); 101 va_end(ap); 102 if (len < 0) 103 err(1, "printf()"); 104 return (len); 105 } 106 107 static void 108 parse_ports(const char *portspec) 109 { 110 const char *p, *q; 111 int port, end; 112 113 if (ports == NULL) 114 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL) 115 err(1, "calloc()"); 116 p = portspec; 117 while (*p != '\0') { 118 if (!isdigit(*p)) 119 errx(1, "syntax error in port range"); 120 for (q = p; *q != '\0' && isdigit(*q); ++q) 121 /* nothing */ ; 122 for (port = 0; p < q; ++p) 123 port = port * 10 + digittoint(*p); 124 if (port < 0 || port > 65535) 125 errx(1, "invalid port number"); 126 SET_PORT(port); 127 switch (*p) { 128 case '-': 129 ++p; 130 break; 131 case ',': 132 ++p; 133 /* fall through */ 134 case '\0': 135 default: 136 continue; 137 } 138 for (q = p; *q != '\0' && isdigit(*q); ++q) 139 /* nothing */ ; 140 for (end = 0; p < q; ++p) 141 end = end * 10 + digittoint(*p); 142 if (end < port || end > 65535) 143 errx(1, "invalid port number"); 144 while (port++ < end) 145 SET_PORT(port); 146 if (*p == ',') 147 ++p; 148 } 149 } 150 151 static void 152 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port) 153 { 154 struct sockaddr_in *sin4; 155 struct sockaddr_in6 *sin6; 156 157 bzero(sa, sizeof *sa); 158 switch (af) { 159 case AF_INET: 160 sin4 = (struct sockaddr_in *)sa; 161 sin4->sin_len = sizeof *sin4; 162 sin4->sin_family = af; 163 sin4->sin_port = port; 164 sin4->sin_addr = *(struct in_addr *)addr; 165 break; 166 case AF_INET6: 167 sin6 = (struct sockaddr_in6 *)sa; 168 sin6->sin6_len = sizeof *sin6; 169 sin6->sin6_family = af; 170 sin6->sin6_port = port; 171 sin6->sin6_addr = *(struct in6_addr *)addr; 172 break; 173 default: 174 abort(); 175 } 176 } 177 178 static void 179 gather_inet(int proto) 180 { 181 struct xinpgen *xig, *exig; 182 struct xinpcb *xip; 183 struct xtcpcb *xtp; 184 struct inpcb *inp; 185 struct xsocket *so; 186 struct sock *sock; 187 const char *varname, *protoname; 188 size_t len, bufsize; 189 void *buf; 190 int hash, retry, vflag; 191 192 vflag = 0; 193 if (opt_4) 194 vflag |= INP_IPV4; 195 if (opt_6) 196 vflag |= INP_IPV6; 197 198 switch (proto) { 199 case IPPROTO_TCP: 200 varname = "net.inet.tcp.pcblist"; 201 protoname = "tcp"; 202 break; 203 case IPPROTO_UDP: 204 varname = "net.inet.udp.pcblist"; 205 protoname = "udp"; 206 break; 207 default: 208 abort(); 209 } 210 211 buf = NULL; 212 bufsize = 8192; 213 retry = 5; 214 do { 215 for (;;) { 216 if ((buf = realloc(buf, bufsize)) == NULL) 217 err(1, "realloc()"); 218 len = bufsize; 219 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0) 220 break; 221 if (errno != ENOMEM) 222 err(1, "sysctlbyname()"); 223 bufsize *= 2; 224 } 225 xig = (struct xinpgen *)buf; 226 exig = (struct xinpgen *)(void *) 227 ((char *)buf + len - sizeof *exig); 228 if (xig->xig_len != sizeof *xig || 229 exig->xig_len != sizeof *exig) 230 errx(1, "struct xinpgen size mismatch"); 231 } while (xig->xig_gen != exig->xig_gen && retry--); 232 233 if (xig->xig_gen != exig->xig_gen && opt_v) 234 warnx("warning: data may be inconsistent"); 235 236 for (;;) { 237 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len); 238 if (xig >= exig) 239 break; 240 switch (proto) { 241 case IPPROTO_TCP: 242 xtp = (struct xtcpcb *)xig; 243 if (xtp->xt_len != sizeof *xtp) { 244 warnx("struct xtcpcb size mismatch"); 245 goto out; 246 } 247 inp = &xtp->xt_inp; 248 so = &xtp->xt_socket; 249 break; 250 case IPPROTO_UDP: 251 xip = (struct xinpcb *)xig; 252 if (xip->xi_len != sizeof *xip) { 253 warnx("struct xinpcb size mismatch"); 254 goto out; 255 } 256 inp = &xip->xi_inp; 257 so = &xip->xi_socket; 258 break; 259 default: 260 abort(); 261 } 262 if ((inp->inp_vflag & vflag) == 0) 263 continue; 264 if (inp->inp_vflag & INP_IPV4) { 265 if ((inp->inp_fport == 0 && !opt_l) || 266 (inp->inp_fport != 0 && !opt_c)) 267 continue; 268 } else if (inp->inp_vflag & INP_IPV6) { 269 if ((inp->in6p_fport == 0 && !opt_l) || 270 (inp->in6p_fport != 0 && !opt_c)) 271 continue; 272 } else { 273 if (opt_v) 274 warnx("invalid vflag 0x%x", inp->inp_vflag); 275 free(sock); 276 continue; 277 } 278 if ((sock = calloc(1, sizeof *sock)) == NULL) 279 err(1, "malloc()"); 280 sock->socket = so->xso_so; 281 sock->proto = proto; 282 if (inp->inp_vflag & INP_IPV4) { 283 sock->family = AF_INET; 284 sockaddr(&sock->laddr, sock->family, 285 &inp->inp_laddr, inp->inp_lport); 286 sockaddr(&sock->faddr, sock->family, 287 &inp->inp_faddr, inp->inp_fport); 288 } else if (inp->inp_vflag & INP_IPV6) { 289 sock->family = AF_INET6; 290 sockaddr(&sock->laddr, sock->family, 291 &inp->in6p_laddr, inp->in6p_lport); 292 sockaddr(&sock->faddr, sock->family, 293 &inp->in6p_faddr, inp->in6p_fport); 294 } 295 sock->vflag = inp->inp_vflag; 296 sock->protoname = protoname; 297 hash = (int)((uintptr_t)sock->socket % HASHSIZE); 298 sock->next = sockhash[hash]; 299 sockhash[hash] = sock; 300 } 301 out: 302 free(buf); 303 } 304 305 static void 306 gather_unix(int proto) 307 { 308 struct xunpgen *xug, *exug; 309 struct xunpcb *xup; 310 struct sock *sock; 311 const char *varname, *protoname; 312 size_t len, bufsize; 313 void *buf; 314 int hash, retry; 315 316 switch (proto) { 317 case SOCK_STREAM: 318 varname = "net.local.stream.pcblist"; 319 protoname = "stream"; 320 break; 321 case SOCK_DGRAM: 322 varname = "net.local.dgram.pcblist"; 323 protoname = "dgram"; 324 break; 325 default: 326 abort(); 327 } 328 buf = NULL; 329 bufsize = 8192; 330 retry = 5; 331 do { 332 for (;;) { 333 if ((buf = realloc(buf, bufsize)) == NULL) 334 err(1, "realloc()"); 335 len = bufsize; 336 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0) 337 break; 338 if (errno != ENOMEM) 339 err(1, "sysctlbyname()"); 340 bufsize *= 2; 341 } 342 xug = (struct xunpgen *)buf; 343 exug = (struct xunpgen *)(void *) 344 ((char *)buf + len - sizeof *exug); 345 if (xug->xug_len != sizeof *xug || 346 exug->xug_len != sizeof *exug) { 347 warnx("struct xinpgen size mismatch"); 348 goto out; 349 } 350 } while (xug->xug_gen != exug->xug_gen && retry--); 351 352 if (xug->xug_gen != exug->xug_gen && opt_v) 353 warnx("warning: data may be inconsistent"); 354 355 for (;;) { 356 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); 357 if (xug >= exug) 358 break; 359 xup = (struct xunpcb *)xug; 360 if (xup->xu_len != sizeof *xup) { 361 warnx("struct xunpcb size mismatch"); 362 goto out; 363 } 364 if ((xup->xu_unp.unp_conn == NULL && !opt_l) || 365 (xup->xu_unp.unp_conn != NULL && !opt_c)) 366 continue; 367 if ((sock = calloc(1, sizeof *sock)) == NULL) 368 err(1, "malloc()"); 369 sock->socket = xup->xu_socket.xso_so; 370 sock->pcb = xup->xu_unpp; 371 sock->proto = proto; 372 sock->family = AF_UNIX; 373 sock->protoname = protoname; 374 if (xup->xu_unp.unp_addr != NULL) 375 sock->laddr = 376 *(struct sockaddr_storage *)(void *)&xup->xu_addr; 377 else if (xup->xu_unp.unp_conn != NULL) 378 *(void **)&sock->faddr = xup->xu_unp.unp_conn; 379 hash = (int)((uintptr_t)sock->socket % HASHSIZE); 380 sock->next = sockhash[hash]; 381 sockhash[hash] = sock; 382 } 383 out: 384 free(buf); 385 } 386 387 static void 388 getfiles(void) 389 { 390 size_t len; 391 392 if ((xfiles = malloc(len = sizeof *xfiles)) == NULL) 393 err(1, "malloc()"); 394 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) { 395 if (errno != ENOMEM) 396 err(1, "sysctlbyname()"); 397 len *= 2; 398 if ((xfiles = realloc(xfiles, len)) == NULL) 399 err(1, "realloc()"); 400 } 401 if (len > 0 && xfiles->xf_size != sizeof *xfiles) 402 errx(1, "struct xfile size mismatch"); 403 nxfiles = len / sizeof *xfiles; 404 } 405 406 static int 407 printaddr(int af, struct sockaddr_storage *ss) 408 { 409 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' }; 410 struct sockaddr_un *sun; 411 void *addr; 412 int off, port; 413 414 switch (af) { 415 case AF_INET: 416 addr = &((struct sockaddr_in *)ss)->sin_addr; 417 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY) 418 addrstr[0] = '*'; 419 port = ntohs(((struct sockaddr_in *)ss)->sin_port); 420 break; 421 case AF_INET6: 422 addr = &((struct sockaddr_in6 *)ss)->sin6_addr; 423 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr)) 424 addrstr[0] = '*'; 425 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port); 426 break; 427 case AF_UNIX: 428 sun = (struct sockaddr_un *)ss; 429 off = (int)((char *)&sun->sun_path - (char *)sun); 430 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path)); 431 } 432 if (addrstr[0] == '\0') 433 inet_ntop(af, addr, addrstr, sizeof addrstr); 434 if (port == 0) 435 return xprintf("%s:*", addrstr); 436 else 437 return xprintf("%s:%d", addrstr, port); 438 } 439 440 static const char * 441 getprocname(pid_t pid) 442 { 443 static struct kinfo_proc proc; 444 size_t len; 445 int mib[4]; 446 447 mib[0] = CTL_KERN; 448 mib[1] = KERN_PROC; 449 mib[2] = KERN_PROC_PID; 450 mib[3] = (int)pid; 451 len = sizeof proc; 452 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) { 453 warn("sysctl()"); 454 return ("??"); 455 } 456 return (proc.ki_ocomm); 457 } 458 459 static int 460 check_ports(struct sock *s) 461 { 462 int port; 463 464 if (ports == NULL) 465 return (1); 466 if ((s->family != AF_INET) && (s->family != AF_INET6)) 467 return (1); 468 if (s->family == AF_INET) 469 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port); 470 else 471 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port); 472 if (CHK_PORT(port)) 473 return (1); 474 if (s->family == AF_INET) 475 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port); 476 else 477 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port); 478 if (CHK_PORT(port)) 479 return (1); 480 return (0); 481 } 482 483 static void 484 display(void) 485 { 486 struct passwd *pwd; 487 struct xfile *xf; 488 struct sock *s; 489 void *p; 490 int hash, n, pos; 491 492 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n", 493 "USER", "COMMAND", "PID", "FD", "PROTO", 494 "LOCAL ADDRESS", "FOREIGN ADDRESS"); 495 setpassent(1); 496 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) { 497 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE); 498 for (s = sockhash[hash]; s != NULL; s = s->next) 499 if ((void *)s->socket == xf->xf_data) 500 break; 501 if (s == NULL) 502 continue; 503 if (!check_ports(s)) 504 continue; 505 pos = 0; 506 if ((pwd = getpwuid(xf->xf_uid)) == NULL) 507 pos += xprintf("%lu", (u_long)xf->xf_uid); 508 else 509 pos += xprintf("%s", pwd->pw_name); 510 while (pos < 9) 511 pos += xprintf(" "); 512 pos += xprintf("%.10s", getprocname(xf->xf_pid)); 513 while (pos < 20) 514 pos += xprintf(" "); 515 pos += xprintf("%lu", (u_long)xf->xf_pid); 516 while (pos < 26) 517 pos += xprintf(" "); 518 pos += xprintf("%d", xf->xf_fd); 519 while (pos < 29) 520 pos += xprintf(" "); 521 pos += xprintf("%s", s->protoname); 522 if (s->vflag & INP_IPV4) 523 pos += xprintf("4"); 524 if (s->vflag & INP_IPV6) 525 pos += xprintf("6"); 526 while (pos < 36) 527 pos += xprintf(" "); 528 switch (s->family) { 529 case AF_INET: 530 case AF_INET6: 531 pos += printaddr(s->family, &s->laddr); 532 while (pos < 58) 533 pos += xprintf(" "); 534 pos += printaddr(s->family, &s->faddr); 535 break; 536 case AF_UNIX: 537 /* server */ 538 if (s->laddr.ss_len > 0) { 539 pos += printaddr(s->family, &s->laddr); 540 break; 541 } 542 /* client */ 543 p = *(void **)&s->faddr; 544 if (p == NULL) { 545 pos += xprintf("(not connected)"); 546 break; 547 } 548 pos += xprintf("-> "); 549 for (hash = 0; hash < HASHSIZE; ++hash) { 550 for (s = sockhash[hash]; s != NULL; s = s->next) 551 if (s->pcb == p) 552 break; 553 if (s != NULL) 554 break; 555 } 556 if (s == NULL || s->laddr.ss_len == 0) 557 pos += xprintf("??"); 558 else 559 pos += printaddr(s->family, &s->laddr); 560 break; 561 default: 562 abort(); 563 } 564 xprintf("\n"); 565 } 566 } 567 568 static void 569 usage(void) 570 { 571 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n"); 572 exit(1); 573 } 574 575 int 576 main(int argc, char *argv[]) 577 { 578 int o; 579 580 while ((o = getopt(argc, argv, "46clp:uv")) != -1) 581 switch (o) { 582 case '4': 583 opt_4 = 1; 584 break; 585 case '6': 586 opt_6 = 1; 587 break; 588 case 'c': 589 opt_c = 1; 590 break; 591 case 'l': 592 opt_l = 1; 593 break; 594 case 'p': 595 parse_ports(optarg); 596 break; 597 case 'u': 598 opt_u = 1; 599 break; 600 case 'v': 601 ++opt_v; 602 break; 603 default: 604 usage(); 605 } 606 607 argc -= optind; 608 argv += optind; 609 610 if (argc > 0) 611 usage(); 612 613 if (!opt_4 && !opt_6 && !opt_u) 614 opt_4 = opt_6 = opt_u = 1; 615 if (!opt_c && !opt_l) 616 opt_c = opt_l = 1; 617 618 if (opt_4 || opt_6) { 619 gather_inet(IPPROTO_TCP); 620 gather_inet(IPPROTO_UDP); 621 } 622 if (opt_u) { 623 gather_unix(SOCK_STREAM); 624 gather_unix(SOCK_DGRAM); 625 } 626 getfiles(); 627 display(); 628 629 exit(0); 630 } 631