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 * $DragonFly: src/usr.bin/sockstat/sockstat.c,v 1.1 2005/01/04 18:56:58 joerg Exp $ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/socket.h> 35 #include <sys/socketvar.h> 36 #include <sys/sysctl.h> 37 #include <sys/file.h> 38 #include <sys/user.h> 39 40 #include <sys/un.h> 41 #include <sys/unpcb.h> 42 43 #include <net/route.h> 44 45 #include <netinet/in.h> 46 #include <netinet/in_pcb.h> 47 #include <netinet/tcp.h> 48 #include <netinet/tcp_seq.h> 49 #include <netinet/tcp_var.h> 50 #include <arpa/inet.h> 51 52 #include <ctype.h> 53 #include <err.h> 54 #include <errno.h> 55 #include <kinfo.h> 56 #include <netdb.h> 57 #include <pwd.h> 58 #include <stdarg.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 static int opt_4; /* Show IPv4 sockets */ 65 static int opt_6; /* Show IPv6 sockets */ 66 static int opt_c; /* Show connected sockets */ 67 static int opt_l; /* Show listening sockets */ 68 static int opt_u; /* Show Unix domain sockets */ 69 static int opt_v; /* Verbose mode */ 70 71 static int *ports; 72 73 #define INT_BIT (sizeof(int)*CHAR_BIT) 74 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0) 75 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT))) 76 77 struct sock { 78 void *socket; 79 void *pcb; 80 int vflag; 81 int family; 82 int proto; 83 const char *protoname; 84 struct sockaddr_storage laddr; 85 struct sockaddr_storage faddr; 86 struct sock *next; 87 }; 88 89 #define HASHSIZE 1009 90 static struct sock *sockhash[HASHSIZE]; 91 92 static struct kinfo_file *xfiles; 93 static int nxfiles; 94 95 static int 96 xprintf(const char *fmt, ...) 97 { 98 va_list ap; 99 int len; 100 101 va_start(ap, fmt); 102 len = vprintf(fmt, ap); 103 va_end(ap); 104 if (len < 0) 105 err(1, "printf()"); 106 return (len); 107 } 108 109 static void 110 parse_ports(const char *portspec) 111 { 112 const char *p, *q; 113 int port, end; 114 115 if (ports == NULL) 116 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL) 117 err(1, "calloc()"); 118 p = portspec; 119 while (*p != '\0') { 120 if (!isdigit(*p)) 121 errx(1, "syntax error in port range"); 122 for (q = p; *q != '\0' && isdigit(*q); ++q) 123 /* nothing */ ; 124 for (port = 0; p < q; ++p) 125 port = port * 10 + digittoint(*p); 126 if (port < 0 || port > 65535) 127 errx(1, "invalid port number"); 128 SET_PORT(port); 129 switch (*p) { 130 case '-': 131 ++p; 132 break; 133 case ',': 134 ++p; 135 /* fall through */ 136 case '\0': 137 default: 138 continue; 139 } 140 for (q = p; *q != '\0' && isdigit(*q); ++q) 141 /* nothing */ ; 142 for (end = 0; p < q; ++p) 143 end = end * 10 + digittoint(*p); 144 if (end < port || end > 65535) 145 errx(1, "invalid port number"); 146 while (port++ < end) 147 SET_PORT(port); 148 if (*p == ',') 149 ++p; 150 } 151 } 152 153 static void 154 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port) 155 { 156 struct sockaddr_in *sin4; 157 struct sockaddr_in6 *sin6; 158 159 bzero(sa, sizeof *sa); 160 switch (af) { 161 case AF_INET: 162 sin4 = (struct sockaddr_in *)sa; 163 sin4->sin_len = sizeof *sin4; 164 sin4->sin_family = af; 165 sin4->sin_port = port; 166 sin4->sin_addr = *(struct in_addr *)addr; 167 break; 168 case AF_INET6: 169 sin6 = (struct sockaddr_in6 *)sa; 170 sin6->sin6_len = sizeof *sin6; 171 sin6->sin6_family = af; 172 sin6->sin6_port = port; 173 sin6->sin6_addr = *(struct in6_addr *)addr; 174 break; 175 default: 176 abort(); 177 } 178 } 179 180 static void 181 gather_inet(int proto) 182 { 183 void *so_begin, *so_end; 184 struct xinpcb *xip; 185 struct xtcpcb *xtp; 186 struct inpcb *inp; 187 struct xsocket *so; 188 struct sock *sock; 189 const char *varname, *protoname; 190 size_t len; 191 void *buf; 192 int hash, vflag; 193 194 vflag = 0; 195 if (opt_4) 196 vflag |= INP_IPV4; 197 if (opt_6) 198 vflag |= INP_IPV6; 199 200 switch (proto) { 201 case IPPROTO_TCP: 202 varname = "net.inet.tcp.pcblist"; 203 protoname = "tcp"; 204 break; 205 case IPPROTO_UDP: 206 varname = "net.inet.udp.pcblist"; 207 protoname = "udp"; 208 break; 209 case IPPROTO_DIVERT: 210 varname = "net.inet.divert.pcblist"; 211 protoname = "div"; 212 break; 213 default: 214 abort(); 215 } 216 217 buf = NULL; 218 len = 0; 219 220 if (sysctlbyname(varname, NULL, &len, NULL, 0)) { 221 if (errno == ENOENT) 222 goto out; 223 err(1, "fetching %s", varname); 224 } 225 if ((buf = malloc(len)) == NULL) 226 err(1, "malloc()"); 227 if (sysctlbyname(varname, buf, &len, NULL, 0)) { 228 if (errno == ENOENT) 229 goto out; 230 err(1, "fetching %s", varname); 231 } 232 233 so_begin = buf; 234 so_end = buf + len; 235 236 for (so_begin = buf, so_end = so_begin + len; 237 so_begin + sizeof(size_t) < so_end && 238 so_begin + *(size_t *)so_begin <= so_end; 239 so_begin += *(size_t *)so_begin) { 240 switch (proto) { 241 case IPPROTO_TCP: 242 xtp = (struct xtcpcb *)so_begin; 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 case IPPROTO_DIVERT: 252 xip = (struct xinpcb *)so_begin; 253 if (xip->xi_len != sizeof *xip) { 254 warnx("struct xinpcb size mismatch"); 255 goto out; 256 } 257 inp = &xip->xi_inp; 258 so = &xip->xi_socket; 259 break; 260 default: 261 abort(); 262 } 263 if ((inp->inp_vflag & vflag) == 0) 264 continue; 265 if (inp->inp_vflag & INP_IPV4) { 266 if ((inp->inp_fport == 0 && !opt_l) || 267 (inp->inp_fport != 0 && !opt_c)) 268 continue; 269 } else if (inp->inp_vflag & INP_IPV6) { 270 if ((inp->in6p_fport == 0 && !opt_l) || 271 (inp->in6p_fport != 0 && !opt_c)) 272 continue; 273 } else { 274 if (opt_v) 275 warnx("invalid vflag 0x%x", inp->inp_vflag); 276 free(sock); 277 continue; 278 } 279 if ((sock = calloc(1, sizeof *sock)) == NULL) 280 err(1, "malloc()"); 281 sock->socket = so->xso_so; 282 sock->proto = proto; 283 if (inp->inp_vflag & INP_IPV4) { 284 sock->family = AF_INET; 285 sockaddr(&sock->laddr, sock->family, 286 &inp->inp_laddr, inp->inp_lport); 287 sockaddr(&sock->faddr, sock->family, 288 &inp->inp_faddr, inp->inp_fport); 289 } else if (inp->inp_vflag & INP_IPV6) { 290 sock->family = AF_INET6; 291 sockaddr(&sock->laddr, sock->family, 292 &inp->in6p_laddr, inp->in6p_lport); 293 sockaddr(&sock->faddr, sock->family, 294 &inp->in6p_faddr, inp->in6p_fport); 295 } 296 sock->vflag = inp->inp_vflag; 297 sock->protoname = protoname; 298 hash = (int)((uintptr_t)sock->socket % HASHSIZE); 299 sock->next = sockhash[hash]; 300 sockhash[hash] = sock; 301 } 302 out: 303 free(buf); 304 } 305 306 static void 307 gather_unix(int proto) 308 { 309 void *so_begin, *so_end; 310 struct xunpcb *xup; 311 struct sock *sock; 312 const char *varname, *protoname; 313 size_t len; 314 void *buf; 315 int hash; 316 317 switch (proto) { 318 case SOCK_STREAM: 319 varname = "net.local.stream.pcblist"; 320 protoname = "stream"; 321 break; 322 case SOCK_DGRAM: 323 varname = "net.local.dgram.pcblist"; 324 protoname = "dgram"; 325 break; 326 default: 327 abort(); 328 } 329 330 buf = NULL; 331 len = 0; 332 333 if (sysctlbyname(varname, NULL, &len, NULL, 0)) 334 err(1, "fetching %s", varname); 335 336 if ((buf = malloc(len)) == NULL) 337 err(1, "malloc()"); 338 if (sysctlbyname(varname, buf, &len, NULL, 0)) 339 err(1, "fetching %s", varname); 340 341 for (so_begin = buf, so_end = buf + len; 342 so_begin + sizeof(size_t) < so_end && 343 so_begin + *(size_t *)so_begin <= so_end; 344 so_begin += *(size_t *)so_begin) { 345 xup = so_begin; 346 if (xup->xu_len != sizeof *xup) { 347 warnx("struct xunpcb size mismatch"); 348 goto out; 349 } 350 if ((xup->xu_unp.unp_conn == NULL && !opt_l) || 351 (xup->xu_unp.unp_conn != NULL && !opt_c)) 352 continue; 353 if ((sock = calloc(1, sizeof *sock)) == NULL) 354 err(1, "malloc()"); 355 sock->socket = xup->xu_socket.xso_so; 356 sock->pcb = xup->xu_unpp; 357 sock->proto = proto; 358 sock->family = AF_UNIX; 359 sock->protoname = protoname; 360 if (xup->xu_unp.unp_addr != NULL) 361 sock->laddr = 362 *(struct sockaddr_storage *)(void *)&xup->xu_addr; 363 else if (xup->xu_unp.unp_conn != NULL) 364 *(void **)&sock->faddr = xup->xu_unp.unp_conn; 365 hash = (int)((uintptr_t)sock->socket % HASHSIZE); 366 sock->next = sockhash[hash]; 367 sockhash[hash] = sock; 368 } 369 out: 370 free(buf); 371 } 372 373 static void 374 getfiles(void) 375 { 376 if (kinfo_get_files(&xfiles, &nxfiles)) 377 err(1, "kinfo_get_files"); 378 } 379 380 static int 381 printaddr(int af, struct sockaddr_storage *ss) 382 { 383 char addrstr[INET6_ADDRSTRLEN] = { '\0', '\0' }; 384 struct sockaddr_un *sun; 385 void *addr; 386 int off, port; 387 388 switch (af) { 389 case AF_INET: 390 addr = &((struct sockaddr_in *)ss)->sin_addr; 391 if (inet_lnaof(*(struct in_addr *)addr) == INADDR_ANY) 392 addrstr[0] = '*'; 393 port = ntohs(((struct sockaddr_in *)ss)->sin_port); 394 break; 395 case AF_INET6: 396 addr = &((struct sockaddr_in6 *)ss)->sin6_addr; 397 if (IN6_IS_ADDR_UNSPECIFIED((struct in6_addr *)addr)) 398 addrstr[0] = '*'; 399 port = ntohs(((struct sockaddr_in6 *)ss)->sin6_port); 400 break; 401 case AF_UNIX: 402 sun = (struct sockaddr_un *)ss; 403 off = (int)((char *)&sun->sun_path - (char *)sun); 404 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path)); 405 } 406 if (addrstr[0] == '\0') 407 inet_ntop(af, addr, addrstr, sizeof addrstr); 408 if (port == 0) 409 return xprintf("%s:*", addrstr); 410 else 411 return xprintf("%s:%d", addrstr, port); 412 } 413 414 static const char * 415 getprocname(pid_t pid) 416 { 417 static struct kinfo_proc proc; 418 size_t len; 419 int mib[4]; 420 421 mib[0] = CTL_KERN; 422 mib[1] = KERN_PROC; 423 mib[2] = KERN_PROC_PID; 424 mib[3] = (int)pid; 425 len = sizeof proc; 426 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) { 427 warn("sysctl()"); 428 return ("??"); 429 } 430 return (proc.kp_thread.td_comm); 431 } 432 433 static int 434 check_ports(struct sock *s) 435 { 436 int port; 437 438 if (ports == NULL) 439 return (1); 440 if ((s->family != AF_INET) && (s->family != AF_INET6)) 441 return (1); 442 if (s->family == AF_INET) 443 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port); 444 else 445 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port); 446 if (CHK_PORT(port)) 447 return (1); 448 if (s->family == AF_INET) 449 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port); 450 else 451 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port); 452 if (CHK_PORT(port)) 453 return (1); 454 return (0); 455 } 456 457 static void 458 display(void) 459 { 460 struct passwd *pwd; 461 struct kinfo_file *xf; 462 struct sock *s; 463 void *p; 464 int hash, n, pos; 465 466 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n", 467 "USER", "COMMAND", "PID", "FD", "PROTO", 468 "LOCAL ADDRESS", "FOREIGN ADDRESS"); 469 setpassent(1); 470 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) { 471 if (xf->f_data == NULL) 472 continue; 473 hash = (int)((uintptr_t)xf->f_data % HASHSIZE); 474 for (s = sockhash[hash]; s != NULL; s = s->next) 475 if ((void *)s->socket == xf->f_data) 476 break; 477 if (s == NULL) 478 continue; 479 if (!check_ports(s)) 480 continue; 481 pos = 0; 482 if ((pwd = getpwuid(xf->f_uid)) == NULL) 483 pos += xprintf("%lu", (u_long)xf->f_uid); 484 else 485 pos += xprintf("%s", pwd->pw_name); 486 while (pos < 9) 487 pos += xprintf(" "); 488 pos += xprintf("%.10s", getprocname(xf->f_pid)); 489 while (pos < 20) 490 pos += xprintf(" "); 491 pos += xprintf("%lu", (u_long)xf->f_pid); 492 while (pos < 26) 493 pos += xprintf(" "); 494 pos += xprintf("%d", xf->f_fd); 495 while (pos < 29) 496 pos += xprintf(" "); 497 pos += xprintf("%s", s->protoname); 498 if (s->vflag & INP_IPV4) 499 pos += xprintf("4"); 500 if (s->vflag & INP_IPV6) 501 pos += xprintf("6"); 502 while (pos < 36) 503 pos += xprintf(" "); 504 switch (s->family) { 505 case AF_INET: 506 case AF_INET6: 507 pos += printaddr(s->family, &s->laddr); 508 while (pos < 58) 509 pos += xprintf(" "); 510 pos += printaddr(s->family, &s->faddr); 511 break; 512 case AF_UNIX: 513 /* server */ 514 if (s->laddr.ss_len > 0) { 515 pos += printaddr(s->family, &s->laddr); 516 break; 517 } 518 /* client */ 519 p = *(void **)&s->faddr; 520 if (p == NULL) { 521 pos += xprintf("(not connected)"); 522 break; 523 } 524 pos += xprintf("-> "); 525 for (hash = 0; hash < HASHSIZE; ++hash) { 526 for (s = sockhash[hash]; s != NULL; s = s->next) 527 if (s->pcb == p) 528 break; 529 if (s != NULL) 530 break; 531 } 532 if (s == NULL || s->laddr.ss_len == 0) 533 pos += xprintf("??"); 534 else 535 pos += printaddr(s->family, &s->laddr); 536 break; 537 default: 538 abort(); 539 } 540 xprintf("\n"); 541 } 542 } 543 544 static void 545 usage(void) 546 { 547 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n"); 548 exit(1); 549 } 550 551 int 552 main(int argc, char *argv[]) 553 { 554 int o; 555 556 while ((o = getopt(argc, argv, "46clp:uv")) != -1) 557 switch (o) { 558 case '4': 559 opt_4 = 1; 560 break; 561 case '6': 562 opt_6 = 1; 563 break; 564 case 'c': 565 opt_c = 1; 566 break; 567 case 'l': 568 opt_l = 1; 569 break; 570 case 'p': 571 parse_ports(optarg); 572 break; 573 case 'u': 574 opt_u = 1; 575 break; 576 case 'v': 577 ++opt_v; 578 break; 579 default: 580 usage(); 581 } 582 583 argc -= optind; 584 argv += optind; 585 586 if (argc > 0) 587 usage(); 588 589 if (!opt_4 && !opt_6 && !opt_u) 590 opt_4 = opt_6 = opt_u = 1; 591 if (!opt_c && !opt_l) 592 opt_c = opt_l = 1; 593 594 if (opt_4 || opt_6) { 595 gather_inet(IPPROTO_TCP); 596 gather_inet(IPPROTO_UDP); 597 gather_inet(IPPROTO_DIVERT); 598 } 599 if (opt_u) { 600 gather_unix(SOCK_STREAM); 601 gather_unix(SOCK_DGRAM); 602 } 603 getfiles(); 604 display(); 605 606 exit(0); 607 } 608