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