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/user.h> 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 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 <kinfo.h> 55 #include <netdb.h> 56 #include <pwd.h> 57 #include <stdarg.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 vflag; 80 int family; 81 int proto; 82 const char *protoname; 83 struct sockaddr_storage laddr; 84 struct sockaddr_storage faddr; 85 struct sock *next; 86 }; 87 88 static int xprintf(const char *, ...) __printflike(1, 2); 89 90 #define HASHSIZE 1009 91 static struct sock *sockhash[HASHSIZE]; 92 93 static struct kinfo_file *xfiles; 94 static size_t nxfiles; 95 96 static int 97 xprintf(const char *fmt, ...) 98 { 99 va_list ap; 100 int len; 101 102 va_start(ap, fmt); 103 len = vprintf(fmt, ap); 104 va_end(ap); 105 if (len < 0) 106 err(1, "printf()"); 107 return (len); 108 } 109 110 static void 111 parse_ports(const char *portspec) 112 { 113 const char *p, *q; 114 int port, end; 115 116 if (ports == NULL) 117 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL) 118 err(1, "calloc()"); 119 p = portspec; 120 while (*p != '\0') { 121 if (!isdigit(*p)) 122 errx(1, "syntax error in port range"); 123 for (q = p; *q != '\0' && isdigit(*q); ++q) 124 /* nothing */ ; 125 for (port = 0; p < q; ++p) 126 port = port * 10 + (*p - '0'); 127 if (port < 0 || port > 65535) 128 errx(1, "invalid port number"); 129 SET_PORT(port); 130 switch (*p) { 131 case '-': 132 ++p; 133 break; 134 case ',': 135 ++p; 136 /* fall through */ 137 case '\0': 138 default: 139 continue; 140 } 141 for (q = p; *q != '\0' && isdigit(*q); ++q) 142 /* nothing */ ; 143 for (end = 0; p < q; ++p) 144 end = end * 10 + (*p - '0'); 145 if (end < port || end > 65535) 146 errx(1, "invalid port number"); 147 while (port++ < end) 148 SET_PORT(port); 149 if (*p == ',') 150 ++p; 151 } 152 } 153 154 static void 155 sockaddr(struct sockaddr_storage *sa, int af, void *addr, int port) 156 { 157 struct sockaddr_in *sin4; 158 struct sockaddr_in6 *sin6; 159 160 bzero(sa, sizeof *sa); 161 switch (af) { 162 case AF_INET: 163 sin4 = (struct sockaddr_in *)sa; 164 sin4->sin_len = sizeof *sin4; 165 sin4->sin_family = af; 166 sin4->sin_port = port; 167 sin4->sin_addr = *(struct in_addr *)addr; 168 break; 169 case AF_INET6: 170 sin6 = (struct sockaddr_in6 *)sa; 171 sin6->sin6_len = sizeof *sin6; 172 sin6->sin6_family = af; 173 sin6->sin6_port = port; 174 sin6->sin6_addr = *(struct in6_addr *)addr; 175 break; 176 default: 177 abort(); 178 } 179 } 180 181 static void 182 gather_inet(int proto) 183 { 184 void *so_begin, *so_end; 185 struct xinpcb *xip; 186 struct xtcpcb *xtp; 187 struct inpcb *inp; 188 struct xsocket *so; 189 struct sock *sock; 190 const char *varname, *protoname; 191 size_t len; 192 void *buf; 193 int hash, vflag; 194 195 vflag = 0; 196 if (opt_4) 197 vflag |= INP_IPV4; 198 if (opt_6) 199 vflag |= INP_IPV6; 200 201 switch (proto) { 202 case IPPROTO_TCP: 203 varname = "net.inet.tcp.pcblist"; 204 protoname = "tcp"; 205 break; 206 case IPPROTO_UDP: 207 varname = "net.inet.udp.pcblist"; 208 protoname = "udp"; 209 break; 210 case IPPROTO_DIVERT: 211 varname = "net.inet.divert.pcblist"; 212 protoname = "div"; 213 break; 214 default: 215 abort(); 216 } 217 218 buf = NULL; 219 len = 0; 220 221 if (sysctlbyname(varname, NULL, &len, NULL, 0)) { 222 if (errno == ENOENT) 223 goto out; 224 err(1, "fetching %s", varname); 225 } 226 if ((buf = malloc(len)) == NULL) 227 err(1, "malloc()"); 228 if (sysctlbyname(varname, buf, &len, NULL, 0)) { 229 if (errno == ENOENT) 230 goto out; 231 err(1, "fetching %s", varname); 232 } 233 234 so_begin = buf; 235 so_end = (uint8_t *)buf + len; 236 237 for (so_begin = buf, so_end = (uint8_t *)so_begin + len; 238 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end && 239 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end; 240 so_begin = (uint8_t *)so_begin + *(size_t *)so_begin) { 241 switch (proto) { 242 case IPPROTO_TCP: 243 xtp = (struct xtcpcb *)so_begin; 244 if (xtp->xt_len != sizeof *xtp) { 245 warnx("struct xtcpcb size mismatch"); 246 goto out; 247 } 248 inp = &xtp->xt_inp; 249 so = &xtp->xt_socket; 250 break; 251 case IPPROTO_UDP: 252 case IPPROTO_DIVERT: 253 xip = (struct xinpcb *)so_begin; 254 if (xip->xi_len != sizeof *xip) { 255 warnx("struct xinpcb size mismatch"); 256 goto out; 257 } 258 inp = &xip->xi_inp; 259 so = &xip->xi_socket; 260 break; 261 default: 262 abort(); 263 } 264 if ((inp->inp_vflag & vflag) == 0) 265 continue; 266 if (inp->inp_vflag & INP_IPV4) { 267 if ((inp->inp_fport == 0 && !opt_l) || 268 (inp->inp_fport != 0 && !opt_c)) 269 continue; 270 } else if (inp->inp_vflag & INP_IPV6) { 271 if ((inp->in6p_fport == 0 && !opt_l) || 272 (inp->in6p_fport != 0 && !opt_c)) 273 continue; 274 } else { 275 if (opt_v) 276 warnx("invalid vflag 0x%x", inp->inp_vflag); 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 = (uint8_t *)buf + len; 342 (uint8_t *)so_begin + sizeof(size_t) < (uint8_t *)so_end && 343 (uint8_t *)so_begin + *(size_t *)so_begin <= (uint8_t *)so_end; 344 so_begin = (uint8_t *)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 default: 406 abort(); 407 } 408 if (addrstr[0] == '\0') 409 inet_ntop(af, addr, addrstr, sizeof addrstr); 410 if (port == 0) 411 return xprintf("%s:*", addrstr); 412 else 413 return xprintf("%s:%d", addrstr, port); 414 } 415 416 static const char * 417 getprocname(pid_t pid) 418 { 419 static struct kinfo_proc proc; 420 size_t len; 421 int mib[4]; 422 423 mib[0] = CTL_KERN; 424 mib[1] = KERN_PROC; 425 mib[2] = KERN_PROC_PID; 426 mib[3] = (int)pid; 427 len = sizeof proc; 428 if (sysctl(mib, 4, &proc, &len, NULL, 0) == -1) { 429 warn("sysctl()"); 430 return ("??"); 431 } 432 return (proc.kp_comm); 433 } 434 435 static int 436 check_ports(struct sock *s) 437 { 438 int port; 439 440 if (ports == NULL) 441 return (1); 442 if ((s->family != AF_INET) && (s->family != AF_INET6)) 443 return (1); 444 if (s->family == AF_INET) 445 port = ntohs(((struct sockaddr_in *)(&s->laddr))->sin_port); 446 else 447 port = ntohs(((struct sockaddr_in6 *)(&s->laddr))->sin6_port); 448 if (CHK_PORT(port)) 449 return (1); 450 if (s->family == AF_INET) 451 port = ntohs(((struct sockaddr_in *)(&s->faddr))->sin_port); 452 else 453 port = ntohs(((struct sockaddr_in6 *)(&s->faddr))->sin6_port); 454 if (CHK_PORT(port)) 455 return (1); 456 return (0); 457 } 458 459 static void 460 display(void) 461 { 462 struct passwd *pwd; 463 struct kinfo_file *xf; 464 struct sock *s; 465 void *p; 466 int hash, n, pos; 467 468 printf("%-8s %-10s %-5s %-2s %-6s %-21s %-21s\n", 469 "USER", "COMMAND", "PID", "FD", "PROTO", 470 "LOCAL ADDRESS", "FOREIGN ADDRESS"); 471 setpassent(1); 472 for (xf = xfiles, n = 0; n < (int)nxfiles; ++n, ++xf) { 473 if (xf->f_data == NULL) 474 continue; 475 hash = (int)((uintptr_t)xf->f_data % HASHSIZE); 476 for (s = sockhash[hash]; s != NULL; s = s->next) 477 if ((void *)s->socket == xf->f_data) 478 break; 479 if (s == NULL) 480 continue; 481 if (!check_ports(s)) 482 continue; 483 pos = 0; 484 if ((pwd = getpwuid(xf->f_uid)) == NULL) 485 pos += xprintf("%lu", (u_long)xf->f_uid); 486 else 487 pos += xprintf("%s", pwd->pw_name); 488 while (pos < 9) 489 pos += xprintf(" "); 490 pos += xprintf("%.10s", getprocname(xf->f_pid)); 491 while (pos < 20) 492 pos += xprintf(" "); 493 pos += xprintf("%lu", (u_long)xf->f_pid); 494 while (pos < 26) 495 pos += xprintf(" "); 496 pos += xprintf("%d", xf->f_fd); 497 while (pos < 29) 498 pos += xprintf(" "); 499 pos += xprintf("%s", s->protoname); 500 if (s->vflag & INP_IPV4) 501 pos += xprintf("4"); 502 if (s->vflag & INP_IPV6) 503 pos += xprintf("6"); 504 while (pos < 36) 505 pos += xprintf(" "); 506 switch (s->family) { 507 case AF_INET: 508 case AF_INET6: 509 pos += printaddr(s->family, &s->laddr); 510 while (pos < 57) 511 pos += xprintf(" "); 512 pos += xprintf(" "); 513 pos += printaddr(s->family, &s->faddr); 514 break; 515 case AF_UNIX: 516 /* server */ 517 if (s->laddr.ss_len > 0) { 518 pos += printaddr(s->family, &s->laddr); 519 break; 520 } 521 /* client */ 522 p = *(void **)&s->faddr; 523 if (p == NULL) { 524 pos += xprintf("(not connected)"); 525 break; 526 } 527 pos += xprintf("-> "); 528 for (hash = 0; hash < HASHSIZE; ++hash) { 529 for (s = sockhash[hash]; s != NULL; s = s->next) 530 if (s->pcb == p) 531 break; 532 if (s != NULL) 533 break; 534 } 535 if (s == NULL || s->laddr.ss_len == 0) 536 pos += xprintf("??"); 537 else 538 pos += printaddr(s->family, &s->laddr); 539 break; 540 default: 541 abort(); 542 } 543 xprintf("\n"); 544 } 545 } 546 547 static void 548 usage(void) 549 { 550 fprintf(stderr, "Usage: sockstat [-46clu] [-p ports]\n"); 551 exit(1); 552 } 553 554 int 555 main(int argc, char *argv[]) 556 { 557 int o; 558 559 while ((o = getopt(argc, argv, "46clp:uv")) != -1) 560 switch (o) { 561 case '4': 562 opt_4 = 1; 563 break; 564 case '6': 565 opt_6 = 1; 566 break; 567 case 'c': 568 opt_c = 1; 569 break; 570 case 'l': 571 opt_l = 1; 572 break; 573 case 'p': 574 parse_ports(optarg); 575 break; 576 case 'u': 577 opt_u = 1; 578 break; 579 case 'v': 580 ++opt_v; 581 break; 582 default: 583 usage(); 584 } 585 586 argc -= optind; 587 argv += optind; 588 589 if (argc > 0) 590 usage(); 591 592 if (!opt_4 && !opt_6 && !opt_u) 593 opt_4 = opt_6 = opt_u = 1; 594 if (!opt_c && !opt_l) 595 opt_c = opt_l = 1; 596 597 if (opt_4 || opt_6) { 598 gather_inet(IPPROTO_TCP); 599 gather_inet(IPPROTO_UDP); 600 gather_inet(IPPROTO_DIVERT); 601 } 602 if (opt_u) { 603 gather_unix(SOCK_STREAM); 604 gather_unix(SOCK_DGRAM); 605 } 606 getfiles(); 607 display(); 608 609 exit(0); 610 } 611