1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2002 Dag-Erling Smørgrav 5 * 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 * in this position and unchanged. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/file.h> 33 #include <sys/socket.h> 34 #include <sys/socketvar.h> 35 #include <sys/sysctl.h> 36 #include <sys/jail.h> 37 #include <sys/user.h> 38 #include <sys/queue.h> 39 #include <sys/tree.h> 40 41 #include <sys/un.h> 42 #include <sys/unpcb.h> 43 44 #include <net/route.h> 45 46 #include <netinet/in.h> 47 #include <netinet/in_pcb.h> 48 #include <netinet/sctp.h> 49 #include <netinet/tcp.h> 50 #define TCPSTATES /* load state names */ 51 #include <netinet/tcp_fsm.h> 52 #include <netinet/tcp_seq.h> 53 #include <netinet/tcp_var.h> 54 #include <netinet/tcp_log_buf.h> 55 #include <arpa/inet.h> 56 57 #include <capsicum_helpers.h> 58 #include <errno.h> 59 #include <inttypes.h> 60 #include <jail.h> 61 #include <netdb.h> 62 #include <pwd.h> 63 #include <stdarg.h> 64 #include <stdbool.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 #include <libxo/xo.h> 70 71 #include <libcasper.h> 72 #include <casper/cap_net.h> 73 #include <casper/cap_netdb.h> 74 #include <casper/cap_pwd.h> 75 #include <casper/cap_sysctl.h> 76 77 #include "sockstat.h" 78 79 #define SOCKSTAT_XO_VERSION "1" 80 #define sstosin(ss) ((struct sockaddr_in *)(ss)) 81 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss)) 82 #define sstosun(ss) ((struct sockaddr_un *)(ss)) 83 #define sstosa(ss) ((struct sockaddr *)(ss)) 84 85 static bool opt_4; /* Show IPv4 sockets */ 86 static bool opt_6; /* Show IPv6 sockets */ 87 static bool opt_A; /* Show kernel address of pcb */ 88 static bool opt_b; /* Show BBLog state */ 89 static bool opt_C; /* Show congestion control */ 90 static bool opt_c; /* Show connected sockets */ 91 static bool opt_f; /* Show FIB numbers */ 92 static bool opt_I; /* Show spliced socket addresses */ 93 static bool opt_i; /* Show inp_gencnt */ 94 static int opt_j; /* Show specified jail */ 95 static bool opt_L; /* Don't show IPv4 or IPv6 loopback sockets */ 96 static bool opt_l; /* Show listening sockets */ 97 static bool opt_n; /* Don't resolve UIDs to user names */ 98 static bool opt_q; /* Don't show header */ 99 static bool opt_S; /* Show protocol stack if applicable */ 100 static bool opt_s; /* Show protocol state if applicable */ 101 static bool opt_U; /* Show remote UDP encapsulation port number */ 102 static bool opt_u; /* Show Unix domain sockets */ 103 static u_int opt_v; /* Verbose mode */ 104 static bool opt_w; /* Automatically size the columns */ 105 static bool is_xo_style_encoding; 106 static bool show_path_state = false; 107 108 /* 109 * Default protocols to use if no -P was defined. 110 */ 111 static const char *default_protos[] = {"sctp", "tcp", "udp", "udplite", 112 "divert" }; 113 static size_t default_numprotos = nitems(default_protos); 114 115 static int *protos; /* protocols to use */ 116 static size_t numprotos; /* allocated size of protos[] */ 117 118 struct addr { 119 union { 120 struct sockaddr_storage address; 121 struct { /* unix(4) faddr */ 122 kvaddr_t conn; 123 kvaddr_t firstref; 124 kvaddr_t nextref; 125 }; 126 }; 127 unsigned int encaps_port; 128 int state; 129 struct addr *next; 130 }; 131 132 struct sock { 133 union { 134 RB_ENTRY(sock) socket_tree; /* tree of pcbs with socket */ 135 SLIST_ENTRY(sock) socket_list; /* list of pcbs w/o socket */ 136 }; 137 RB_ENTRY(sock) pcb_tree; 138 kvaddr_t socket; 139 kvaddr_t pcb; 140 kvaddr_t splice_socket; 141 uint64_t inp_gencnt; 142 int shown; 143 int vflag; 144 int family; 145 int proto; 146 int state; 147 int fibnum; 148 int bblog_state; 149 const char *protoname; 150 char stack[TCP_FUNCTION_NAME_LEN_MAX]; 151 char cc[TCP_CA_NAME_MAX]; 152 struct addr *laddr; 153 struct addr *faddr; 154 }; 155 156 static RB_HEAD(socks_t, sock) socks = RB_INITIALIZER(&socks); 157 static int64_t 158 socket_compare(const struct sock *a, const struct sock *b) 159 { 160 return ((int64_t)(a->socket/2 - b->socket/2)); 161 } 162 RB_GENERATE_STATIC(socks_t, sock, socket_tree, socket_compare); 163 164 static RB_HEAD(pcbs_t, sock) pcbs = RB_INITIALIZER(&pcbs); 165 static int64_t 166 pcb_compare(const struct sock *a, const struct sock *b) 167 { 168 return ((int64_t)(a->pcb/2 - b->pcb/2)); 169 } 170 RB_GENERATE_STATIC(pcbs_t, sock, pcb_tree, pcb_compare); 171 172 static SLIST_HEAD(, sock) nosocks = SLIST_HEAD_INITIALIZER(&nosocks); 173 174 struct file { 175 RB_ENTRY(file) file_tree; 176 kvaddr_t xf_data; 177 pid_t xf_pid; 178 uid_t xf_uid; 179 int xf_fd; 180 }; 181 182 static RB_HEAD(files_t, file) ftree = RB_INITIALIZER(&ftree); 183 static int64_t 184 file_compare(const struct file *a, const struct file *b) 185 { 186 return ((int64_t)(a->xf_data/2 - b->xf_data/2)); 187 } 188 RB_GENERATE_STATIC(files_t, file, file_tree, file_compare); 189 190 static struct file *files; 191 static int nfiles; 192 193 static cap_channel_t *capnet; 194 static cap_channel_t *capnetdb; 195 static cap_channel_t *capsysctl; 196 static cap_channel_t *cappwd; 197 198 static bool 199 _check_ksize(size_t received_size, size_t expected_size, const char *struct_name) 200 { 201 if (received_size != expected_size) { 202 xo_warnx("%s size mismatch: expected %zd, received %zd", 203 struct_name, expected_size, received_size); 204 return false; 205 } 206 return true; 207 } 208 #define check_ksize(_sz, _struct) (_check_ksize(_sz, sizeof(_struct), #_struct)) 209 210 static void 211 _enforce_ksize(size_t received_size, size_t expected_size, const char *struct_name) 212 { 213 if (received_size != expected_size) { 214 xo_errx(1, "fatal: struct %s size mismatch: expected %zd, received %zd", 215 struct_name, expected_size, received_size); 216 } 217 } 218 #define enforce_ksize(_sz, _struct) (_enforce_ksize(_sz, sizeof(_struct), #_struct)) 219 220 static int 221 get_proto_type(const char *proto) 222 { 223 struct protoent *pent; 224 225 if (strlen(proto) == 0) 226 return (0); 227 if (capnetdb != NULL) 228 pent = cap_getprotobyname(capnetdb, proto); 229 else 230 pent = getprotobyname(proto); 231 if (pent == NULL) { 232 xo_warn("cap_getprotobyname"); 233 return (-1); 234 } 235 return (pent->p_proto); 236 } 237 238 static void 239 init_protos(int num) 240 { 241 int proto_count = 0; 242 243 if (num > 0) { 244 proto_count = num; 245 } else { 246 /* Find the maximum number of possible protocols. */ 247 while (getprotoent() != NULL) 248 proto_count++; 249 endprotoent(); 250 } 251 252 if ((protos = malloc(sizeof(int) * proto_count)) == NULL) 253 xo_err(1, "malloc"); 254 numprotos = proto_count; 255 } 256 257 static int 258 parse_protos(char *protospec) 259 { 260 char *prot; 261 int proto_type, proto_index; 262 263 if (protospec == NULL) 264 return (-1); 265 266 init_protos(0); 267 proto_index = 0; 268 while ((prot = strsep(&protospec, ",")) != NULL) { 269 if (strlen(prot) == 0) 270 continue; 271 proto_type = get_proto_type(prot); 272 if (proto_type != -1) 273 protos[proto_index++] = proto_type; 274 } 275 numprotos = proto_index; 276 return (proto_index); 277 } 278 279 static void 280 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port) 281 { 282 struct sockaddr_in *sin4; 283 struct sockaddr_in6 *sin6; 284 285 bzero(ss, sizeof(*ss)); 286 switch (af) { 287 case AF_INET: 288 sin4 = sstosin(ss); 289 sin4->sin_len = sizeof(*sin4); 290 sin4->sin_family = af; 291 sin4->sin_port = port; 292 sin4->sin_addr = *(struct in_addr *)addr; 293 break; 294 case AF_INET6: 295 sin6 = sstosin6(ss); 296 sin6->sin6_len = sizeof(*sin6); 297 sin6->sin6_family = af; 298 sin6->sin6_port = port; 299 sin6->sin6_addr = *(struct in6_addr *)addr; 300 #define s6_addr16 __u6_addr.__u6_addr16 301 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 302 sin6->sin6_scope_id = 303 ntohs(sin6->sin6_addr.s6_addr16[1]); 304 sin6->sin6_addr.s6_addr16[1] = 0; 305 } 306 break; 307 default: 308 abort(); 309 } 310 } 311 312 static void 313 free_socket(struct sock *sock) 314 { 315 struct addr *cur, *next; 316 317 cur = sock->laddr; 318 while (cur != NULL) { 319 next = cur->next; 320 free(cur); 321 cur = next; 322 } 323 cur = sock->faddr; 324 while (cur != NULL) { 325 next = cur->next; 326 free(cur); 327 cur = next; 328 } 329 free(sock); 330 } 331 332 static void 333 gather_sctp(void) 334 { 335 struct sock *sock; 336 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr; 337 struct xsctp_inpcb *xinpcb; 338 struct xsctp_tcb *xstcb; 339 struct xsctp_raddr *xraddr; 340 struct xsctp_laddr *xladdr; 341 const char *varname; 342 size_t len, offset; 343 char *buf; 344 int vflag; 345 int no_stcb, local_all_loopback, foreign_all_loopback; 346 347 vflag = 0; 348 if (opt_4) 349 vflag |= INP_IPV4; 350 if (opt_6) 351 vflag |= INP_IPV6; 352 353 varname = "net.inet.sctp.assoclist"; 354 if (cap_sysctlbyname(capsysctl, varname, 0, &len, 0, 0) < 0) { 355 if (errno != ENOENT) 356 xo_err(1, "cap_sysctlbyname()"); 357 return; 358 } 359 if ((buf = (char *)malloc(len)) == NULL) { 360 xo_err(1, "malloc()"); 361 return; 362 } 363 if (cap_sysctlbyname(capsysctl, varname, buf, &len, 0, 0) < 0) { 364 xo_err(1, "cap_sysctlbyname()"); 365 free(buf); 366 return; 367 } 368 xinpcb = (struct xsctp_inpcb *)(void *)buf; 369 offset = sizeof(struct xsctp_inpcb); 370 while ((offset < len) && (xinpcb->last == 0)) { 371 if ((sock = calloc(1, sizeof *sock)) == NULL) 372 xo_err(1, "malloc()"); 373 sock->socket = xinpcb->socket; 374 sock->proto = IPPROTO_SCTP; 375 sock->protoname = "sctp"; 376 if (xinpcb->maxqlen == 0) 377 sock->state = SCTP_CLOSED; 378 else 379 sock->state = SCTP_LISTEN; 380 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) { 381 sock->family = AF_INET6; 382 /* 383 * Currently there is no way to distinguish between 384 * IPv6 only sockets or dual family sockets. 385 * So mark it as dual socket. 386 */ 387 sock->vflag = INP_IPV6 | INP_IPV4; 388 } else { 389 sock->family = AF_INET; 390 sock->vflag = INP_IPV4; 391 } 392 prev_laddr = NULL; 393 local_all_loopback = 1; 394 while (offset < len) { 395 xladdr = (struct xsctp_laddr *)(void *)(buf + offset); 396 offset += sizeof(struct xsctp_laddr); 397 if (xladdr->last == 1) 398 break; 399 if ((laddr = calloc(1, sizeof(struct addr))) == NULL) 400 xo_err(1, "malloc()"); 401 switch (xladdr->address.sa.sa_family) { 402 case AF_INET: 403 #define __IN_IS_ADDR_LOOPBACK(pina) \ 404 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 405 if (!__IN_IS_ADDR_LOOPBACK( 406 &xladdr->address.sin.sin_addr)) 407 local_all_loopback = 0; 408 #undef __IN_IS_ADDR_LOOPBACK 409 sockaddr(&laddr->address, AF_INET, 410 &xladdr->address.sin.sin_addr, 411 htons(xinpcb->local_port)); 412 break; 413 case AF_INET6: 414 if (!IN6_IS_ADDR_LOOPBACK( 415 &xladdr->address.sin6.sin6_addr)) 416 local_all_loopback = 0; 417 sockaddr(&laddr->address, AF_INET6, 418 &xladdr->address.sin6.sin6_addr, 419 htons(xinpcb->local_port)); 420 break; 421 default: 422 xo_errx(1, "address family %d not supported", 423 xladdr->address.sa.sa_family); 424 } 425 laddr->next = NULL; 426 if (prev_laddr == NULL) 427 sock->laddr = laddr; 428 else 429 prev_laddr->next = laddr; 430 prev_laddr = laddr; 431 } 432 if (sock->laddr == NULL) { 433 if ((sock->laddr = 434 calloc(1, sizeof(struct addr))) == NULL) 435 xo_err(1, "malloc()"); 436 sock->laddr->address.ss_family = sock->family; 437 if (sock->family == AF_INET) 438 sock->laddr->address.ss_len = 439 sizeof(struct sockaddr_in); 440 else 441 sock->laddr->address.ss_len = 442 sizeof(struct sockaddr_in6); 443 local_all_loopback = 0; 444 } 445 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL) 446 xo_err(1, "malloc()"); 447 sock->faddr->address.ss_family = sock->family; 448 if (sock->family == AF_INET) 449 sock->faddr->address.ss_len = 450 sizeof(struct sockaddr_in); 451 else 452 sock->faddr->address.ss_len = 453 sizeof(struct sockaddr_in6); 454 no_stcb = 1; 455 while (offset < len) { 456 xstcb = (struct xsctp_tcb *)(void *)(buf + offset); 457 offset += sizeof(struct xsctp_tcb); 458 if (no_stcb) { 459 if (opt_l && (sock->vflag & vflag) && 460 (!opt_L || !local_all_loopback) && 461 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) || 462 (xstcb->last == 1))) { 463 RB_INSERT(socks_t, &socks, sock); 464 } else { 465 free_socket(sock); 466 } 467 } 468 if (xstcb->last == 1) 469 break; 470 no_stcb = 0; 471 if (opt_c) { 472 if ((sock = calloc(1, sizeof *sock)) == NULL) 473 xo_err(1, "malloc()"); 474 sock->socket = xinpcb->socket; 475 sock->proto = IPPROTO_SCTP; 476 sock->protoname = "sctp"; 477 sock->state = (int)xstcb->state; 478 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) { 479 sock->family = AF_INET6; 480 /* 481 * Currently there is no way to distinguish 482 * between IPv6 only sockets or dual family 483 * sockets. So mark it as dual socket. 484 */ 485 sock->vflag = INP_IPV6 | INP_IPV4; 486 } else { 487 sock->family = AF_INET; 488 sock->vflag = INP_IPV4; 489 } 490 } 491 prev_laddr = NULL; 492 local_all_loopback = 1; 493 while (offset < len) { 494 xladdr = (struct xsctp_laddr *)(void *)(buf + 495 offset); 496 offset += sizeof(struct xsctp_laddr); 497 if (xladdr->last == 1) 498 break; 499 if (!opt_c) 500 continue; 501 laddr = calloc(1, sizeof(struct addr)); 502 if (laddr == NULL) 503 xo_err(1, "malloc()"); 504 switch (xladdr->address.sa.sa_family) { 505 case AF_INET: 506 #define __IN_IS_ADDR_LOOPBACK(pina) \ 507 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 508 if (!__IN_IS_ADDR_LOOPBACK( 509 &xladdr->address.sin.sin_addr)) 510 local_all_loopback = 0; 511 #undef __IN_IS_ADDR_LOOPBACK 512 sockaddr(&laddr->address, AF_INET, 513 &xladdr->address.sin.sin_addr, 514 htons(xstcb->local_port)); 515 break; 516 case AF_INET6: 517 if (!IN6_IS_ADDR_LOOPBACK( 518 &xladdr->address.sin6.sin6_addr)) 519 local_all_loopback = 0; 520 sockaddr(&laddr->address, AF_INET6, 521 &xladdr->address.sin6.sin6_addr, 522 htons(xstcb->local_port)); 523 break; 524 default: 525 xo_errx(1, 526 "address family %d not supported", 527 xladdr->address.sa.sa_family); 528 } 529 laddr->next = NULL; 530 if (prev_laddr == NULL) 531 sock->laddr = laddr; 532 else 533 prev_laddr->next = laddr; 534 prev_laddr = laddr; 535 } 536 prev_faddr = NULL; 537 foreign_all_loopback = 1; 538 while (offset < len) { 539 xraddr = (struct xsctp_raddr *)(void *)(buf + 540 offset); 541 offset += sizeof(struct xsctp_raddr); 542 if (xraddr->last == 1) 543 break; 544 if (!opt_c) 545 continue; 546 faddr = calloc(1, sizeof(struct addr)); 547 if (faddr == NULL) 548 xo_err(1, "malloc()"); 549 switch (xraddr->address.sa.sa_family) { 550 case AF_INET: 551 #define __IN_IS_ADDR_LOOPBACK(pina) \ 552 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 553 if (!__IN_IS_ADDR_LOOPBACK( 554 &xraddr->address.sin.sin_addr)) 555 foreign_all_loopback = 0; 556 #undef __IN_IS_ADDR_LOOPBACK 557 sockaddr(&faddr->address, AF_INET, 558 &xraddr->address.sin.sin_addr, 559 htons(xstcb->remote_port)); 560 break; 561 case AF_INET6: 562 if (!IN6_IS_ADDR_LOOPBACK( 563 &xraddr->address.sin6.sin6_addr)) 564 foreign_all_loopback = 0; 565 sockaddr(&faddr->address, AF_INET6, 566 &xraddr->address.sin6.sin6_addr, 567 htons(xstcb->remote_port)); 568 break; 569 default: 570 xo_errx(1, 571 "address family %d not supported", 572 xraddr->address.sa.sa_family); 573 } 574 faddr->encaps_port = xraddr->encaps_port; 575 faddr->state = xraddr->state; 576 faddr->next = NULL; 577 if (prev_faddr == NULL) 578 sock->faddr = faddr; 579 else 580 prev_faddr->next = faddr; 581 prev_faddr = faddr; 582 } 583 if (opt_c) { 584 if ((sock->vflag & vflag) && 585 (!opt_L || 586 !(local_all_loopback || 587 foreign_all_loopback))) { 588 RB_INSERT(socks_t, &socks, sock); 589 show_path_state = true; 590 } else { 591 free_socket(sock); 592 } 593 } 594 } 595 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset); 596 offset += sizeof(struct xsctp_inpcb); 597 } 598 free(buf); 599 } 600 601 static void 602 gather_inet(int proto) 603 { 604 struct xinpgen *xig, *exig; 605 struct xinpcb *xip; 606 struct xtcpcb *xtp = NULL; 607 struct xsocket *so; 608 struct sock *sock; 609 struct addr *laddr, *faddr; 610 const char *varname, *protoname; 611 size_t len, bufsize; 612 void *buf; 613 int retry, vflag; 614 615 vflag = 0; 616 if (opt_4) 617 vflag |= INP_IPV4; 618 if (opt_6) 619 vflag |= INP_IPV6; 620 621 switch (proto) { 622 case IPPROTO_TCP: 623 varname = "net.inet.tcp.pcblist"; 624 protoname = "tcp"; 625 break; 626 case IPPROTO_UDP: 627 varname = "net.inet.udp.pcblist"; 628 protoname = "udp"; 629 break; 630 case IPPROTO_UDPLITE: 631 varname = "net.inet.udplite.pcblist"; 632 protoname = "udplite"; 633 break; 634 case IPPROTO_DIVERT: 635 varname = "net.inet.divert.pcblist"; 636 protoname = "div"; 637 break; 638 default: 639 xo_errx(1, "protocol %d not supported", proto); 640 } 641 642 buf = NULL; 643 bufsize = 8192; 644 retry = 5; 645 do { 646 for (;;) { 647 if ((buf = realloc(buf, bufsize)) == NULL) 648 xo_err(1, "realloc()"); 649 len = bufsize; 650 if (cap_sysctlbyname(capsysctl, varname, buf, &len, 651 NULL, 0) == 0) 652 break; 653 if (errno == ENOENT) 654 goto out; 655 if (errno != ENOMEM || len != bufsize) 656 xo_err(1, "cap_sysctlbyname()"); 657 bufsize *= 2; 658 } 659 xig = (struct xinpgen *)buf; 660 exig = (struct xinpgen *)(void *) 661 ((char *)buf + len - sizeof *exig); 662 enforce_ksize(xig->xig_len, struct xinpgen); 663 enforce_ksize(exig->xig_len, struct xinpgen); 664 } while (xig->xig_gen != exig->xig_gen && retry--); 665 666 if (xig->xig_gen != exig->xig_gen && opt_v) 667 xo_warnx("warning: data may be inconsistent"); 668 669 for (;;) { 670 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len); 671 if (xig >= exig) 672 break; 673 switch (proto) { 674 case IPPROTO_TCP: 675 xtp = (struct xtcpcb *)xig; 676 xip = &xtp->xt_inp; 677 if (!check_ksize(xtp->xt_len, struct xtcpcb)) 678 goto out; 679 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp"; 680 break; 681 case IPPROTO_UDP: 682 case IPPROTO_UDPLITE: 683 case IPPROTO_DIVERT: 684 xip = (struct xinpcb *)xig; 685 if (!check_ksize(xip->xi_len, struct xinpcb)) 686 goto out; 687 break; 688 default: 689 xo_errx(1, "protocol %d not supported", proto); 690 } 691 so = &xip->xi_socket; 692 if ((xip->inp_vflag & vflag) == 0) 693 continue; 694 if (xip->inp_vflag & INP_IPV4) { 695 if ((xip->inp_fport == 0 && !opt_l) || 696 (xip->inp_fport != 0 && !opt_c)) 697 continue; 698 #define __IN_IS_ADDR_LOOPBACK(pina) \ 699 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) 700 if (opt_L && 701 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) || 702 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr))) 703 continue; 704 #undef __IN_IS_ADDR_LOOPBACK 705 } else if (xip->inp_vflag & INP_IPV6) { 706 if ((xip->inp_fport == 0 && !opt_l) || 707 (xip->inp_fport != 0 && !opt_c)) 708 continue; 709 if (opt_L && 710 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) || 711 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr))) 712 continue; 713 } else { 714 if (opt_v) 715 xo_warnx("invalid vflag 0x%x", xip->inp_vflag); 716 continue; 717 } 718 if ((sock = calloc(1, sizeof(*sock))) == NULL) 719 xo_err(1, "malloc()"); 720 if ((laddr = calloc(1, sizeof *laddr)) == NULL) 721 xo_err(1, "malloc()"); 722 if ((faddr = calloc(1, sizeof *faddr)) == NULL) 723 xo_err(1, "malloc()"); 724 sock->socket = so->xso_so; 725 sock->pcb = so->so_pcb; 726 sock->splice_socket = so->so_splice_so; 727 sock->proto = proto; 728 sock->inp_gencnt = xip->inp_gencnt; 729 sock->fibnum = so->so_fibnum; 730 if (xip->inp_vflag & INP_IPV4) { 731 sock->family = AF_INET; 732 sockaddr(&laddr->address, sock->family, 733 &xip->inp_laddr, xip->inp_lport); 734 sockaddr(&faddr->address, sock->family, 735 &xip->inp_faddr, xip->inp_fport); 736 } else if (xip->inp_vflag & INP_IPV6) { 737 sock->family = AF_INET6; 738 sockaddr(&laddr->address, sock->family, 739 &xip->in6p_laddr, xip->inp_lport); 740 sockaddr(&faddr->address, sock->family, 741 &xip->in6p_faddr, xip->inp_fport); 742 } 743 if (proto == IPPROTO_TCP) 744 faddr->encaps_port = xtp->xt_encaps_port; 745 laddr->next = NULL; 746 faddr->next = NULL; 747 sock->laddr = laddr; 748 sock->faddr = faddr; 749 sock->vflag = xip->inp_vflag; 750 if (proto == IPPROTO_TCP) { 751 sock->state = xtp->t_state; 752 sock->bblog_state = xtp->t_logstate; 753 memcpy(sock->stack, xtp->xt_stack, 754 TCP_FUNCTION_NAME_LEN_MAX); 755 memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX); 756 } 757 sock->protoname = protoname; 758 if (sock->socket != 0) 759 RB_INSERT(socks_t, &socks, sock); 760 else 761 SLIST_INSERT_HEAD(&nosocks, sock, socket_list); 762 } 763 out: 764 free(buf); 765 } 766 767 static void 768 gather_unix(int proto) 769 { 770 struct xunpgen *xug, *exug; 771 struct xunpcb *xup; 772 struct sock *sock; 773 struct addr *laddr, *faddr; 774 const char *varname, *protoname; 775 size_t len, bufsize; 776 void *buf; 777 int retry; 778 779 switch (proto) { 780 case SOCK_STREAM: 781 varname = "net.local.stream.pcblist"; 782 protoname = "stream"; 783 break; 784 case SOCK_DGRAM: 785 varname = "net.local.dgram.pcblist"; 786 protoname = "dgram"; 787 break; 788 case SOCK_SEQPACKET: 789 varname = "net.local.seqpacket.pcblist"; 790 protoname = is_xo_style_encoding ? "seqpacket" : "seqpack"; 791 break; 792 default: 793 abort(); 794 } 795 buf = NULL; 796 bufsize = 8192; 797 retry = 5; 798 do { 799 for (;;) { 800 if ((buf = realloc(buf, bufsize)) == NULL) 801 xo_err(1, "realloc()"); 802 len = bufsize; 803 if (cap_sysctlbyname(capsysctl, varname, buf, &len, 804 NULL, 0) == 0) 805 break; 806 if (errno != ENOMEM || len != bufsize) 807 xo_err(1, "cap_sysctlbyname()"); 808 bufsize *= 2; 809 } 810 xug = (struct xunpgen *)buf; 811 exug = (struct xunpgen *)(void *) 812 ((char *)buf + len - sizeof(*exug)); 813 if (!check_ksize(xug->xug_len, struct xunpgen) || 814 !check_ksize(exug->xug_len, struct xunpgen)) 815 goto out; 816 } while (xug->xug_gen != exug->xug_gen && retry--); 817 818 if (xug->xug_gen != exug->xug_gen && opt_v) 819 xo_warnx("warning: data may be inconsistent"); 820 821 for (;;) { 822 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); 823 if (xug >= exug) 824 break; 825 xup = (struct xunpcb *)xug; 826 if (!check_ksize(xup->xu_len, struct xunpcb)) 827 goto out; 828 if ((xup->unp_conn == 0 && !opt_l) || 829 (xup->unp_conn != 0 && !opt_c)) 830 continue; 831 if ((sock = calloc(1, sizeof(*sock))) == NULL) 832 xo_err(1, "malloc()"); 833 if ((laddr = calloc(1, sizeof *laddr)) == NULL) 834 xo_err(1, "malloc()"); 835 if ((faddr = calloc(1, sizeof *faddr)) == NULL) 836 xo_err(1, "malloc()"); 837 sock->socket = xup->xu_socket.xso_so; 838 sock->pcb = xup->xu_unpp; 839 sock->proto = proto; 840 sock->family = AF_UNIX; 841 sock->protoname = protoname; 842 if (xup->xu_addr.sun_family == AF_UNIX) 843 laddr->address = 844 *(struct sockaddr_storage *)(void *)&xup->xu_addr; 845 faddr->conn = xup->unp_conn; 846 faddr->firstref = xup->xu_firstref; 847 faddr->nextref = xup->xu_nextref; 848 laddr->next = NULL; 849 faddr->next = NULL; 850 sock->laddr = laddr; 851 sock->faddr = faddr; 852 RB_INSERT(socks_t, &socks, sock); 853 RB_INSERT(pcbs_t, &pcbs, sock); 854 } 855 out: 856 free(buf); 857 } 858 859 static void 860 getfiles(void) 861 { 862 struct xfile *xfiles; 863 size_t len, olen; 864 865 olen = len = sizeof(*xfiles); 866 if ((xfiles = malloc(len)) == NULL) 867 xo_err(1, "malloc()"); 868 while (cap_sysctlbyname(capsysctl, "kern.file", xfiles, &len, 0, 0) 869 == -1) { 870 if (errno != ENOMEM || len != olen) 871 xo_err(1, "cap_sysctlbyname()"); 872 olen = len *= 2; 873 if ((xfiles = realloc(xfiles, len)) == NULL) 874 xo_err(1, "realloc()"); 875 } 876 if (len > 0) 877 enforce_ksize(xfiles->xf_size, struct xfile); 878 nfiles = len / sizeof(*xfiles); 879 880 if ((files = malloc(nfiles * sizeof(struct file))) == NULL) 881 xo_err(1, "malloc()"); 882 883 for (int i = 0; i < nfiles; i++) { 884 files[i].xf_data = xfiles[i].xf_data; 885 files[i].xf_pid = xfiles[i].xf_pid; 886 files[i].xf_uid = xfiles[i].xf_uid; 887 files[i].xf_fd = xfiles[i].xf_fd; 888 RB_INSERT(files_t, &ftree, &files[i]); 889 } 890 891 free(xfiles); 892 } 893 894 static int 895 formataddr(struct sockaddr_storage *ss, char *buf, size_t bufsize) 896 { 897 struct sockaddr_un *sun; 898 char addrstr[NI_MAXHOST] = { '\0', '\0' }; 899 int error, off, port = 0; 900 901 switch (ss->ss_family) { 902 case AF_INET: 903 if (sstosin(ss)->sin_addr.s_addr == INADDR_ANY) 904 addrstr[0] = '*'; 905 port = ntohs(sstosin(ss)->sin_port); 906 break; 907 case AF_INET6: 908 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr)) 909 addrstr[0] = '*'; 910 port = ntohs(sstosin6(ss)->sin6_port); 911 break; 912 case AF_UNIX: 913 sun = sstosun(ss); 914 off = (int)((char *)&sun->sun_path - (char *)sun); 915 if (is_xo_style_encoding) { 916 xo_emit("{:path/%.*s}", sun->sun_len - off, 917 sun->sun_path); 918 return 0; 919 } 920 return snprintf(buf, bufsize, "%.*s", 921 sun->sun_len - off, sun->sun_path); 922 } 923 if (addrstr[0] == '\0') { 924 error = cap_getnameinfo(capnet, sstosa(ss), ss->ss_len, 925 addrstr, sizeof(addrstr), NULL, 0, NI_NUMERICHOST); 926 if (error) 927 xo_errx(1, "cap_getnameinfo()"); 928 } 929 if (is_xo_style_encoding) { 930 xo_emit("{:address/%s}", addrstr); 931 xo_emit("{:port/%d}", port); 932 return 0; 933 } 934 if (port == 0) 935 return snprintf(buf, bufsize, "%s:*", addrstr); 936 return snprintf(buf, bufsize, "%s:%d", addrstr, port); 937 } 938 939 static const char * 940 getprocname(pid_t pid) 941 { 942 static struct kinfo_proc proc; 943 size_t len; 944 int mib[4]; 945 946 mib[0] = CTL_KERN; 947 mib[1] = KERN_PROC; 948 mib[2] = KERN_PROC_PID; 949 mib[3] = (int)pid; 950 len = sizeof(proc); 951 if (cap_sysctl(capsysctl, mib, nitems(mib), &proc, &len, NULL, 0) 952 == -1) { 953 /* Do not warn if the process exits before we get its name. */ 954 if (errno != ESRCH) 955 xo_warn("cap_sysctl()"); 956 return ("??"); 957 } 958 return (proc.ki_comm); 959 } 960 961 static int 962 getprocjid(pid_t pid) 963 { 964 static struct kinfo_proc proc; 965 size_t len; 966 int mib[4]; 967 968 mib[0] = CTL_KERN; 969 mib[1] = KERN_PROC; 970 mib[2] = KERN_PROC_PID; 971 mib[3] = (int)pid; 972 len = sizeof(proc); 973 if (cap_sysctl(capsysctl, mib, nitems(mib), &proc, &len, NULL, 0) 974 == -1) { 975 /* Do not warn if the process exits before we get its jid. */ 976 if (errno != ESRCH) 977 xo_warn("cap_sysctl()"); 978 return (-1); 979 } 980 return (proc.ki_jid); 981 } 982 983 static int 984 check_ports(struct sock *s) 985 { 986 int port; 987 struct addr *addr; 988 989 if (ports == NULL) 990 return (1); 991 if ((s->family != AF_INET) && (s->family != AF_INET6)) 992 return (1); 993 for (addr = s->laddr; addr != NULL; addr = addr->next) { 994 if (s->family == AF_INET) 995 port = ntohs(sstosin(&addr->address)->sin_port); 996 else 997 port = ntohs(sstosin6(&addr->address)->sin6_port); 998 if (CHK_PORT(port)) 999 return (1); 1000 } 1001 for (addr = s->faddr; addr != NULL; addr = addr->next) { 1002 if (s->family == AF_INET) 1003 port = ntohs(sstosin(&addr->address)->sin_port); 1004 else 1005 port = ntohs(sstosin6(&addr->address)->sin6_port); 1006 if (CHK_PORT(port)) 1007 return (1); 1008 } 1009 return (0); 1010 } 1011 1012 static const char * 1013 sctp_conn_state(int state) 1014 { 1015 switch (state) { 1016 case SCTP_CLOSED: 1017 return "CLOSED"; 1018 break; 1019 case SCTP_BOUND: 1020 return "BOUND"; 1021 break; 1022 case SCTP_LISTEN: 1023 return "LISTEN"; 1024 break; 1025 case SCTP_COOKIE_WAIT: 1026 return "COOKIE_WAIT"; 1027 break; 1028 case SCTP_COOKIE_ECHOED: 1029 return "COOKIE_ECHOED"; 1030 break; 1031 case SCTP_ESTABLISHED: 1032 return "ESTABLISHED"; 1033 break; 1034 case SCTP_SHUTDOWN_SENT: 1035 return "SHUTDOWN_SENT"; 1036 break; 1037 case SCTP_SHUTDOWN_RECEIVED: 1038 return "SHUTDOWN_RECEIVED"; 1039 break; 1040 case SCTP_SHUTDOWN_ACK_SENT: 1041 return "SHUTDOWN_ACK_SENT"; 1042 break; 1043 case SCTP_SHUTDOWN_PENDING: 1044 return "SHUTDOWN_PENDING"; 1045 break; 1046 default: 1047 return "UNKNOWN"; 1048 break; 1049 } 1050 } 1051 1052 static const char * 1053 sctp_path_state(int state) 1054 { 1055 switch (state) { 1056 case SCTP_UNCONFIRMED: 1057 return "UNCONFIRMED"; 1058 break; 1059 case SCTP_ACTIVE: 1060 return "ACTIVE"; 1061 break; 1062 case SCTP_INACTIVE: 1063 return "INACTIVE"; 1064 break; 1065 default: 1066 return "UNKNOWN"; 1067 break; 1068 } 1069 } 1070 1071 static const char * 1072 bblog_state(int state) 1073 { 1074 switch (state) { 1075 case TCP_LOG_STATE_OFF: 1076 return "OFF"; 1077 break; 1078 case TCP_LOG_STATE_TAIL: 1079 return "TAIL"; 1080 break; 1081 case TCP_LOG_STATE_HEAD: 1082 return "HEAD"; 1083 break; 1084 case TCP_LOG_STATE_HEAD_AUTO: 1085 return "HEAD_AUTO"; 1086 break; 1087 case TCP_LOG_STATE_CONTINUAL: 1088 return "CONTINUAL"; 1089 break; 1090 case TCP_LOG_STATE_TAIL_AUTO: 1091 return "TAIL_AUTO"; 1092 break; 1093 case TCP_LOG_VIA_BBPOINTS: 1094 return "BBPOINTS"; 1095 break; 1096 default: 1097 return "UNKNOWN"; 1098 break; 1099 } 1100 } 1101 1102 static int 1103 format_unix_faddr(struct addr *faddr, char *buf, size_t bufsize) { 1104 #define SAFEBUF (buf == NULL ? NULL : buf + pos) 1105 #define SAFESIZE (buf == NULL ? 0 : bufsize - pos) 1106 1107 size_t pos = 0; 1108 if (faddr->conn != 0) { 1109 /* Remote peer we connect(2) to, if any. */ 1110 struct sock *p; 1111 if (!is_xo_style_encoding) 1112 pos += strlcpy(SAFEBUF, "-> ", SAFESIZE); 1113 p = RB_FIND(pcbs_t, &pcbs, 1114 &(struct sock){ .pcb = faddr->conn }); 1115 if (__predict_false(p == NULL) && !is_xo_style_encoding) { 1116 /* XXGL: can this happen at all? */ 1117 pos += snprintf(SAFEBUF, SAFESIZE, "??"); 1118 } else if (p->laddr->address.ss_len == 0) { 1119 struct file *f; 1120 f = RB_FIND(files_t, &ftree, 1121 &(struct file){ .xf_data = 1122 p->socket }); 1123 if (f != NULL) { 1124 if (!is_xo_style_encoding) { 1125 pos += snprintf(SAFEBUF, SAFESIZE, 1126 "[%lu %d]", (u_long)f->xf_pid, 1127 f->xf_fd); 1128 } else { 1129 xo_open_list("connections"); 1130 xo_open_instance("connections"); 1131 xo_emit("{:pid/%lu}", (u_long)f->xf_pid); 1132 xo_emit("{:fd/%d}", f->xf_fd); 1133 xo_close_instance("connections"); 1134 xo_close_list("connections"); 1135 } 1136 } 1137 } else 1138 pos += formataddr(&p->laddr->address, 1139 SAFEBUF, SAFESIZE); 1140 } else if (faddr->firstref != 0) { 1141 /* Remote peer(s) connect(2)ed to us, if any. */ 1142 struct sock *p; 1143 struct file *f; 1144 kvaddr_t ref = faddr->firstref; 1145 bool fref = true; 1146 1147 if (!is_xo_style_encoding) 1148 pos += snprintf(SAFEBUF, SAFESIZE, " <- "); 1149 xo_open_list("connections"); 1150 while ((p = RB_FIND(pcbs_t, &pcbs, 1151 &(struct sock){ .pcb = ref })) != 0) { 1152 f = RB_FIND(files_t, &ftree, 1153 &(struct file){ .xf_data = p->socket }); 1154 if (f != NULL) { 1155 if (!is_xo_style_encoding) { 1156 pos += snprintf(SAFEBUF, SAFESIZE, 1157 "%s[%lu %d]", fref ? "" : ",", 1158 (u_long)f->xf_pid, f->xf_fd); 1159 } else { 1160 xo_open_instance("connections"); 1161 xo_emit("{:pid/%lu}", (u_long)f->xf_pid); 1162 xo_emit("{:fd/%d}", f->xf_fd); 1163 xo_close_instance("connections"); 1164 } 1165 } 1166 ref = p->faddr->nextref; 1167 fref = false; 1168 } 1169 xo_close_list("connections"); 1170 } 1171 return pos; 1172 } 1173 1174 struct col_widths { 1175 int user; 1176 int command; 1177 int pid; 1178 int fd; 1179 int proto; 1180 int local_addr; 1181 int foreign_addr; 1182 int pcb_kva; 1183 int fib; 1184 int splice_address; 1185 int inp_gencnt; 1186 int encaps; 1187 int path_state; 1188 int conn_state; 1189 int bblog_state; 1190 int stack; 1191 int cc; 1192 }; 1193 1194 static void 1195 calculate_sock_column_widths(struct col_widths *cw, struct sock *s) 1196 { 1197 struct addr *laddr, *faddr; 1198 bool first = true; 1199 int len = 0; 1200 laddr = s->laddr; 1201 faddr = s->faddr; 1202 first = true; 1203 1204 len = strlen(s->protoname); 1205 if (s->vflag & INP_IPV4) 1206 len += 1; 1207 if (s->vflag & INP_IPV6) 1208 len += 1; 1209 cw->proto = MAX(cw->proto, len); 1210 1211 while (laddr != NULL || faddr != NULL) { 1212 if (opt_w && s->family == AF_UNIX) { 1213 if ((laddr == NULL) || (faddr == NULL)) 1214 xo_errx(1, "laddr = %p or faddr = %p is NULL", 1215 (void *)laddr, (void *)faddr); 1216 if (laddr->address.ss_len > 0) 1217 len = formataddr(&laddr->address, NULL, 0); 1218 cw->local_addr = MAX(cw->local_addr, len); 1219 len = format_unix_faddr(faddr, NULL, 0); 1220 cw->foreign_addr = MAX(cw->foreign_addr, len); 1221 } else if (opt_w) { 1222 if (laddr != NULL) { 1223 len = formataddr(&laddr->address, NULL, 0); 1224 cw->local_addr = MAX(cw->local_addr, len); 1225 } 1226 if (faddr != NULL) { 1227 len = formataddr(&faddr->address, NULL, 0); 1228 cw->foreign_addr = MAX(cw->foreign_addr, len); 1229 } 1230 } 1231 if (opt_f) { 1232 len = snprintf(NULL, 0, "%d", s->fibnum); 1233 cw->fib = MAX(cw->fib, len); 1234 } 1235 if (opt_I) { 1236 if (s->splice_socket != 0) { 1237 struct sock *sp; 1238 1239 sp = RB_FIND(socks_t, &socks, &(struct sock) 1240 { .socket = s->splice_socket }); 1241 if (sp != NULL) { 1242 len = formataddr(&sp->laddr->address, 1243 NULL, 0); 1244 cw->splice_address = MAX( 1245 cw->splice_address, len); 1246 } 1247 } 1248 } 1249 if (opt_i) { 1250 if (s->proto == IPPROTO_TCP || 1251 s->proto == IPPROTO_UDP) { 1252 len = snprintf(NULL, 0, 1253 "%" PRIu64, s->inp_gencnt); 1254 cw->inp_gencnt = MAX(cw->inp_gencnt, len); 1255 } 1256 } 1257 if (opt_U) { 1258 if (faddr != NULL && 1259 ((s->proto == IPPROTO_SCTP && 1260 s->state != SCTP_CLOSED && 1261 s->state != SCTP_BOUND && 1262 s->state != SCTP_LISTEN) || 1263 (s->proto == IPPROTO_TCP && 1264 s->state != TCPS_CLOSED && 1265 s->state != TCPS_LISTEN))) { 1266 len = snprintf(NULL, 0, "%u", 1267 ntohs(faddr->encaps_port)); 1268 cw->encaps = MAX(cw->encaps, len); 1269 } 1270 } 1271 if (opt_s) { 1272 if (faddr != NULL && 1273 s->proto == IPPROTO_SCTP && 1274 s->state != SCTP_CLOSED && 1275 s->state != SCTP_BOUND && 1276 s->state != SCTP_LISTEN) { 1277 len = strlen(sctp_path_state(faddr->state)); 1278 cw->path_state = MAX(cw->path_state, len); 1279 } 1280 } 1281 if (first) { 1282 if (opt_s) { 1283 if (s->proto == IPPROTO_SCTP || 1284 s->proto == IPPROTO_TCP) { 1285 switch (s->proto) { 1286 case IPPROTO_SCTP: 1287 len = strlen( 1288 sctp_conn_state(s->state)); 1289 cw->conn_state = MAX( 1290 cw->conn_state, len); 1291 break; 1292 case IPPROTO_TCP: 1293 if (s->state >= 0 && 1294 s->state < TCP_NSTATES) { 1295 len = strlen( 1296 tcpstates[s->state]); 1297 cw->conn_state = MAX( 1298 cw->conn_state, 1299 len); 1300 } 1301 break; 1302 } 1303 } 1304 } 1305 if (opt_S && s->proto == IPPROTO_TCP) { 1306 len = strlen(s->stack); 1307 cw->stack = MAX(cw->stack, len); 1308 } 1309 if (opt_C && s->proto == IPPROTO_TCP) { 1310 len = strlen(s->cc); 1311 cw->cc = MAX(cw->cc, len); 1312 } 1313 } 1314 if (laddr != NULL) 1315 laddr = laddr->next; 1316 if (faddr != NULL) 1317 faddr = faddr->next; 1318 first = false; 1319 } 1320 } 1321 1322 static void 1323 calculate_column_widths(struct col_widths *cw) 1324 { 1325 int n, len; 1326 struct file *xf; 1327 struct sock *s; 1328 struct passwd *pwd; 1329 1330 cap_setpassent(cappwd, 1); 1331 for (xf = files, n = 0; n < nfiles; ++n, ++xf) { 1332 if (xf->xf_data == 0) 1333 continue; 1334 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid)) 1335 continue; 1336 s = RB_FIND(socks_t, &socks, 1337 &(struct sock){ .socket = xf->xf_data}); 1338 if (s == NULL || (!check_ports(s))) 1339 continue; 1340 s->shown = 1; 1341 if (opt_n || 1342 (pwd = cap_getpwuid(cappwd, xf->xf_uid)) == NULL) 1343 len = snprintf(NULL, 0, "%lu", (u_long)xf->xf_uid); 1344 else 1345 len = snprintf(NULL, 0, "%s", pwd->pw_name); 1346 cw->user = MAX(cw->user, len); 1347 len = snprintf(NULL, 0, "%lu", (u_long)xf->xf_pid); 1348 cw->pid = MAX(cw->pid, len); 1349 len = snprintf(NULL, 0, "%d", xf->xf_fd); 1350 cw->fd = MAX(cw->fd, len); 1351 1352 calculate_sock_column_widths(cw, s); 1353 } 1354 if (opt_j >= 0) 1355 return; 1356 SLIST_FOREACH(s, &nosocks, socket_list) { 1357 if (!check_ports(s)) 1358 continue; 1359 calculate_sock_column_widths(cw, s); 1360 } 1361 RB_FOREACH(s, socks_t, &socks) { 1362 if (s->shown) 1363 continue; 1364 if (!check_ports(s)) 1365 continue; 1366 calculate_sock_column_widths(cw, s); 1367 } 1368 } 1369 1370 static void 1371 display_sock(struct sock *s, struct col_widths *cw, char *buf, size_t bufsize) 1372 { 1373 struct addr *laddr, *faddr; 1374 bool first; 1375 laddr = s->laddr; 1376 faddr = s->faddr; 1377 first = true; 1378 1379 snprintf(buf, bufsize, "%s%s%s", 1380 s->protoname, 1381 s->vflag & INP_IPV4 ? "4" : "", 1382 s->vflag & INP_IPV6 ? "6" : ""); 1383 xo_emit(" {:proto/%-*s}", cw->proto, buf); 1384 while (laddr != NULL || faddr != NULL) { 1385 if (s->family == AF_UNIX) { 1386 if ((laddr == NULL) || (faddr == NULL)) 1387 xo_errx(1, "laddr = %p or faddr = %p is NULL", 1388 (void *)laddr, (void *)faddr); 1389 if (laddr->address.ss_len > 0) { 1390 xo_open_container("local"); 1391 formataddr(&laddr->address, buf, bufsize); 1392 if (!is_xo_style_encoding) { 1393 xo_emit(" {:local-address/%-*.*s}", 1394 cw->local_addr, cw->local_addr, 1395 buf); 1396 } 1397 xo_close_container("local"); 1398 } else if (laddr->address.ss_len == 0 && 1399 faddr->conn == 0 && !is_xo_style_encoding) { 1400 xo_emit(" {:local-address/%-*.*s}", 1401 cw->local_addr, cw->local_addr, 1402 "(not connected)"); 1403 } else if (!is_xo_style_encoding) { 1404 xo_emit(" {:local-address/%-*.*s}", 1405 cw->local_addr, cw->local_addr, "??"); 1406 } 1407 if (faddr->conn != 0 || faddr->firstref != 0) { 1408 xo_open_container("foreign"); 1409 int len = format_unix_faddr(faddr, buf, 1410 bufsize); 1411 if (len == 0 && !is_xo_style_encoding) 1412 xo_emit(" {:foreign-address/%-*s}", 1413 cw->foreign_addr, "??"); 1414 else if (!is_xo_style_encoding) 1415 xo_emit(" {:foreign-address/%-*.*s}", 1416 cw->foreign_addr, 1417 cw->foreign_addr, buf); 1418 xo_close_container("foreign"); 1419 } else if (!is_xo_style_encoding) 1420 xo_emit(" {:foreign-address/%-*s}", 1421 cw->foreign_addr, "??"); 1422 } else { 1423 if (laddr != NULL) { 1424 xo_open_container("local"); 1425 formataddr(&laddr->address, buf, bufsize); 1426 if (!is_xo_style_encoding) { 1427 xo_emit(" {:local-address/%-*.*s}", 1428 cw->local_addr, cw->local_addr, 1429 buf); 1430 } 1431 xo_close_container("local"); 1432 } else if (!is_xo_style_encoding) 1433 xo_emit(" {:local-address/%-*.*s}", 1434 cw->local_addr, cw->local_addr, "??"); 1435 if (faddr != NULL) { 1436 xo_open_container("foreign"); 1437 formataddr(&faddr->address, buf, bufsize); 1438 if (!is_xo_style_encoding) { 1439 xo_emit(" {:foreign-address/%-*.*s}", 1440 cw->foreign_addr, 1441 cw->foreign_addr, buf); 1442 } 1443 xo_close_container("foreign"); 1444 } else if (!is_xo_style_encoding) { 1445 xo_emit(" {:foreign-address/%-*.*s}", 1446 cw->foreign_addr, cw->foreign_addr, 1447 "??"); 1448 } 1449 } 1450 if (opt_A) { 1451 snprintf(buf, bufsize, "%#*" PRIx64, 1452 cw->pcb_kva, s->pcb); 1453 xo_emit(" {:pcb-kva/%s}", buf); 1454 } 1455 if (opt_f) 1456 xo_emit(" {:fib/%*d}", cw->fib, s->fibnum); 1457 if (opt_I) { 1458 if (s->splice_socket != 0) { 1459 struct sock *sp; 1460 sp = RB_FIND(socks_t, &socks, &(struct sock) 1461 { .socket = s->splice_socket }); 1462 if (sp != NULL) { 1463 xo_open_container("splice"); 1464 formataddr(&sp->laddr->address, 1465 buf, bufsize); 1466 xo_close_container("splice"); 1467 } else if (!is_xo_style_encoding) 1468 strlcpy(buf, "??", bufsize); 1469 } else if (!is_xo_style_encoding) 1470 strlcpy(buf, "??", bufsize); 1471 if (!is_xo_style_encoding) 1472 xo_emit(" {:splice-address/%-*s}", 1473 cw->splice_address, buf); 1474 } 1475 if (opt_i) { 1476 if (s->proto == IPPROTO_TCP || 1477 s->proto == IPPROTO_UDP) { 1478 snprintf(buf, bufsize, "%" PRIu64, 1479 s->inp_gencnt); 1480 xo_emit(" {:id/%*s}", cw->inp_gencnt, buf); 1481 } else if (!is_xo_style_encoding) 1482 xo_emit(" {:id/%*s}", cw->inp_gencnt, "??"); 1483 } 1484 if (opt_U) { 1485 if (faddr != NULL && 1486 ((s->proto == IPPROTO_SCTP && 1487 s->state != SCTP_CLOSED && 1488 s->state != SCTP_BOUND && 1489 s->state != SCTP_LISTEN) || 1490 (s->proto == IPPROTO_TCP && 1491 s->state != TCPS_CLOSED && 1492 s->state != TCPS_LISTEN))) { 1493 xo_emit(" {:encaps/%*u}", cw->encaps, 1494 ntohs(faddr->encaps_port)); 1495 } else if (!is_xo_style_encoding) 1496 xo_emit(" {:encaps/%*s}", cw->encaps, "??"); 1497 } 1498 if (opt_s && show_path_state) { 1499 if (faddr != NULL && 1500 s->proto == IPPROTO_SCTP && 1501 s->state != SCTP_CLOSED && 1502 s->state != SCTP_BOUND && 1503 s->state != SCTP_LISTEN) { 1504 xo_emit(" {:path-state/%-*s}", cw->path_state, 1505 sctp_path_state(faddr->state)); 1506 } else if (!is_xo_style_encoding) 1507 xo_emit(" {:path-state/%-*s}", cw->path_state, 1508 "??"); 1509 } 1510 if (first) { 1511 if (opt_s) { 1512 if (s->proto == IPPROTO_SCTP || 1513 s->proto == IPPROTO_TCP) { 1514 switch (s->proto) { 1515 case IPPROTO_SCTP: 1516 xo_emit(" {:conn-state/%-*s}", 1517 cw->conn_state, 1518 sctp_conn_state(s->state)); 1519 break; 1520 case IPPROTO_TCP: 1521 if (s->state >= 0 && 1522 s->state < TCP_NSTATES) 1523 xo_emit(" {:conn-state/%-*s}", 1524 cw->conn_state, 1525 tcpstates[s->state]); 1526 else if (!is_xo_style_encoding) 1527 xo_emit(" {:conn-state/%-*s}", 1528 cw->conn_state, "??"); 1529 break; 1530 } 1531 } else if (!is_xo_style_encoding) 1532 xo_emit(" {:conn-state/%-*s}", 1533 cw->conn_state, "??"); 1534 } 1535 if (opt_b) { 1536 if (s->proto == IPPROTO_TCP) 1537 xo_emit(" {:bblog-state/%-*s}", 1538 cw->bblog_state, 1539 bblog_state(s->bblog_state)); 1540 else if (!is_xo_style_encoding) 1541 xo_emit(" {:bblog-state/%-*s}", 1542 cw->bblog_state, "??"); 1543 } 1544 if (opt_S) { 1545 if (s->proto == IPPROTO_TCP) 1546 xo_emit(" {:stack/%-*s}", 1547 cw->stack, s->stack); 1548 else if (!is_xo_style_encoding) 1549 xo_emit(" {:stack/%-*s}", 1550 cw->stack, "??"); 1551 } 1552 if (opt_C) { 1553 if (s->proto == IPPROTO_TCP) 1554 xo_emit(" {:cc/%-*s}", cw->cc, s->cc); 1555 else if (!is_xo_style_encoding) 1556 xo_emit(" {:cc/%-*s}", cw->cc, "??"); 1557 } 1558 } else if (!is_xo_style_encoding) { 1559 if (opt_s) 1560 xo_emit(" {:conn-state/%-*s}", cw->conn_state, 1561 "??"); 1562 if (opt_b) 1563 xo_emit(" {:bblog-state/%-*s}", cw->bblog_state, 1564 "??"); 1565 if (opt_S) 1566 xo_emit(" {:stack/%-*s}", cw->stack, "??"); 1567 if (opt_C) 1568 xo_emit(" {:cc/%-*s}", cw->cc, "??"); 1569 } 1570 if (laddr != NULL) 1571 laddr = laddr->next; 1572 if (faddr != NULL) 1573 faddr = faddr->next; 1574 xo_emit("\n"); 1575 if (!is_xo_style_encoding && (laddr != NULL || faddr != NULL)) 1576 xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}" 1577 " {:fd/%*s} {:proto/%-*s}", cw->user, "??", 1578 cw->command, "??", cw->pid, "??", cw->fd, "??", 1579 cw->proto, "??"); 1580 first = false; 1581 } 1582 } 1583 1584 static void 1585 display(void) 1586 { 1587 struct passwd *pwd; 1588 struct file *xf; 1589 struct sock *s; 1590 int n; 1591 struct col_widths cw; 1592 const size_t bufsize = 512; 1593 void *buf; 1594 if ((buf = (char *)malloc(bufsize)) == NULL) { 1595 xo_err(1, "malloc()"); 1596 return; 1597 } 1598 1599 if (!is_xo_style_encoding) { 1600 cw = (struct col_widths) { 1601 .user = strlen("USER"), 1602 .command = 10, 1603 .pid = strlen("PID"), 1604 .fd = strlen("FD"), 1605 .proto = strlen("PROTO"), 1606 .local_addr = opt_w ? strlen("LOCAL ADDRESS") : 21, 1607 .foreign_addr = opt_w ? strlen("FOREIGN ADDRESS") : 21, 1608 .pcb_kva = 18, 1609 .fib = strlen("FIB"), 1610 .splice_address = strlen("SPLICE ADDRESS"), 1611 .inp_gencnt = strlen("ID"), 1612 .encaps = strlen("ENCAPS"), 1613 .path_state = strlen("PATH STATE"), 1614 .conn_state = strlen("CONN STATE"), 1615 .bblog_state = strlen("BBLOG STATE"), 1616 .stack = strlen("STACK"), 1617 .cc = strlen("CC"), 1618 }; 1619 calculate_column_widths(&cw); 1620 } else 1621 memset(&cw, 0, sizeof(cw)); 1622 1623 xo_set_version(SOCKSTAT_XO_VERSION); 1624 xo_open_container("sockstat"); 1625 xo_open_list("socket"); 1626 if (!opt_q) { 1627 xo_emit("{T:/%-*s} {T:/%-*s} {T:/%*s} {T:/%*s} {T:/%-*s} " 1628 "{T:/%-*s} {T:/%-*s}", cw.user, "USER", cw.command, 1629 "COMMAND", cw.pid, "PID", cw.fd, "FD", cw.proto, 1630 "PROTO", cw.local_addr, "LOCAL ADDRESS", 1631 cw.foreign_addr, "FOREIGN ADDRESS"); 1632 if (opt_A) 1633 xo_emit(" {T:/%-*s}", cw.pcb_kva, "PCB KVA"); 1634 if (opt_f) 1635 /* RT_MAXFIBS is 65535. */ 1636 xo_emit(" {T:/%*s}", cw.fib, "FIB"); 1637 if (opt_I) 1638 xo_emit(" {T:/%-*s}", cw.splice_address, 1639 "SPLICE ADDRESS"); 1640 if (opt_i) 1641 xo_emit(" {T:/%*s}", cw.inp_gencnt, "ID"); 1642 if (opt_U) 1643 xo_emit(" {T:/%*s}", cw.encaps, "ENCAPS"); 1644 if (opt_s) { 1645 if (show_path_state) 1646 xo_emit(" {T:/%-*s}", cw.path_state, 1647 "PATH STATE"); 1648 xo_emit(" {T:/%-*s}", cw.conn_state, "CONN STATE"); 1649 } 1650 if (opt_b) 1651 xo_emit(" {T:/%-*s}", cw.bblog_state, "BBLOG STATE"); 1652 if (opt_S) 1653 xo_emit(" {T:/%-*s}", cw.stack, "STACK"); 1654 if (opt_C) 1655 xo_emit(" {T:/%-*s}", cw.cc, "CC"); 1656 xo_emit("\n"); 1657 } 1658 cap_setpassent(cappwd, 1); 1659 for (xf = files, n = 0; n < nfiles; ++n, ++xf) { 1660 if (xf->xf_data == 0) 1661 continue; 1662 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid)) 1663 continue; 1664 s = RB_FIND(socks_t, &socks, 1665 &(struct sock){ .socket = xf->xf_data}); 1666 if (s != NULL && check_ports(s)) { 1667 xo_open_instance("socket"); 1668 s->shown = 1; 1669 if (opt_n || 1670 (pwd = cap_getpwuid(cappwd, xf->xf_uid)) == NULL) 1671 xo_emit("{:user/%-*lu}", cw.user, 1672 (u_long)xf->xf_uid); 1673 else 1674 xo_emit("{:user/%-*s}", cw.user, pwd->pw_name); 1675 if (!is_xo_style_encoding) 1676 xo_emit(" {:command/%-*.10s}", cw.command, 1677 getprocname(xf->xf_pid)); 1678 else 1679 xo_emit(" {:command/%-*s}", cw.command, 1680 getprocname(xf->xf_pid)); 1681 xo_emit(" {:pid/%*lu}", cw.pid, (u_long)xf->xf_pid); 1682 xo_emit(" {:fd/%*d}", cw.fd, xf->xf_fd); 1683 display_sock(s, &cw, buf, bufsize); 1684 xo_close_instance("socket"); 1685 } 1686 } 1687 if (opt_j >= 0) 1688 goto out; 1689 SLIST_FOREACH(s, &nosocks, socket_list) { 1690 if (!check_ports(s)) 1691 continue; 1692 xo_open_instance("socket"); 1693 if (!is_xo_style_encoding) 1694 xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}" 1695 " {:fd/%*s}", cw.user, "??", cw.command, "??", 1696 cw.pid, "??", cw.fd, "??"); 1697 display_sock(s, &cw, buf, bufsize); 1698 xo_close_instance("socket"); 1699 } 1700 RB_FOREACH(s, socks_t, &socks) { 1701 if (s->shown) 1702 continue; 1703 if (!check_ports(s)) 1704 continue; 1705 xo_open_instance("socket"); 1706 if (!is_xo_style_encoding) 1707 xo_emit("{:user/%-*s} {:command/%-*s} {:pid/%*s}" 1708 " {:fd/%*s}", cw.user, "??", cw.command, "??", 1709 cw.pid, "??", cw.fd, "??"); 1710 display_sock(s, &cw, buf, bufsize); 1711 xo_close_instance("socket"); 1712 } 1713 out: 1714 xo_close_list("socket"); 1715 xo_close_container("sockstat"); 1716 if (xo_finish() < 0) 1717 xo_err(1, "stdout"); 1718 free(buf); 1719 cap_endpwent(cappwd); 1720 } 1721 1722 static int 1723 set_default_protos(void) 1724 { 1725 struct protoent *prot; 1726 const char *pname; 1727 size_t pindex; 1728 1729 init_protos(default_numprotos); 1730 1731 for (pindex = 0; pindex < default_numprotos; pindex++) { 1732 pname = default_protos[pindex]; 1733 prot = cap_getprotobyname(capnetdb, pname); 1734 if (prot == NULL) 1735 xo_err(1, "cap_getprotobyname: %s", pname); 1736 protos[pindex] = prot->p_proto; 1737 } 1738 numprotos = pindex; 1739 return (pindex); 1740 } 1741 1742 /* 1743 * Return the vnet property of the jail, or -1 on error. 1744 */ 1745 static int 1746 jail_getvnet(int jid) 1747 { 1748 struct iovec jiov[6]; 1749 int vnet; 1750 size_t len = sizeof(vnet); 1751 1752 if (sysctlbyname("kern.features.vimage", &vnet, &len, NULL, 0) != 0) 1753 return (0); 1754 1755 vnet = -1; 1756 jiov[0].iov_base = __DECONST(char *, "jid"); 1757 jiov[0].iov_len = sizeof("jid"); 1758 jiov[1].iov_base = &jid; 1759 jiov[1].iov_len = sizeof(jid); 1760 jiov[2].iov_base = __DECONST(char *, "vnet"); 1761 jiov[2].iov_len = sizeof("vnet"); 1762 jiov[3].iov_base = &vnet; 1763 jiov[3].iov_len = sizeof(vnet); 1764 jiov[4].iov_base = __DECONST(char *, "errmsg"); 1765 jiov[4].iov_len = sizeof("errmsg"); 1766 jiov[5].iov_base = jail_errmsg; 1767 jiov[5].iov_len = JAIL_ERRMSGLEN; 1768 jail_errmsg[0] = '\0'; 1769 if (jail_get(jiov, nitems(jiov), 0) < 0) { 1770 if (!jail_errmsg[0]) 1771 snprintf(jail_errmsg, JAIL_ERRMSGLEN, 1772 "jail_get: %s", strerror(errno)); 1773 return (-1); 1774 } 1775 return (vnet); 1776 } 1777 1778 static void 1779 usage(void) 1780 { 1781 xo_error( 1782 "usage: sockstat [--libxo ...] [-46AbCcfIiLlnqSsUuvw] [-j jid] [-p ports]\n" 1783 " [-P protocols]\n"); 1784 exit(1); 1785 } 1786 1787 int 1788 main(int argc, char *argv[]) 1789 { 1790 cap_channel_t *capcas; 1791 cap_net_limit_t *limit; 1792 const char *pwdcmds[] = { "setpassent", "getpwuid" }; 1793 const char *pwdfields[] = { "pw_name" }; 1794 int protos_defined = -1; 1795 int o, i, err; 1796 1797 argc = xo_parse_args(argc, argv); 1798 if (argc < 0) 1799 exit(1); 1800 if (xo_get_style(NULL) != XO_STYLE_TEXT) { 1801 show_path_state = true; 1802 if (xo_get_style(NULL) != XO_STYLE_HTML) 1803 is_xo_style_encoding = true; 1804 } 1805 opt_j = -1; 1806 while ((o = getopt(argc, argv, "46AbCcfIij:Llnp:P:qSsUuvw")) != -1) 1807 switch (o) { 1808 case '4': 1809 opt_4 = true; 1810 break; 1811 case '6': 1812 opt_6 = true; 1813 break; 1814 case 'A': 1815 opt_A = true; 1816 break; 1817 case 'b': 1818 opt_b = true; 1819 break; 1820 case 'C': 1821 opt_C = true; 1822 break; 1823 case 'c': 1824 opt_c = true; 1825 break; 1826 case 'f': 1827 opt_f = true; 1828 break; 1829 case 'I': 1830 opt_I = true; 1831 break; 1832 case 'i': 1833 opt_i = true; 1834 break; 1835 case 'j': 1836 opt_j = jail_getid(optarg); 1837 if (opt_j < 0) 1838 xo_errx(1, "jail_getid: %s", jail_errmsg); 1839 break; 1840 case 'L': 1841 opt_L = true; 1842 break; 1843 case 'l': 1844 opt_l = true; 1845 break; 1846 case 'n': 1847 opt_n = true; 1848 break; 1849 case 'p': 1850 err = parse_ports(optarg); 1851 switch (err) { 1852 case EINVAL: 1853 xo_errx(1, "syntax error in port range"); 1854 break; 1855 case ERANGE: 1856 xo_errx(1, "invalid port number"); 1857 break; 1858 } 1859 break; 1860 case 'P': 1861 protos_defined = parse_protos(optarg); 1862 break; 1863 case 'q': 1864 opt_q = true; 1865 break; 1866 case 'S': 1867 opt_S = true; 1868 break; 1869 case 's': 1870 opt_s = true; 1871 break; 1872 case 'U': 1873 opt_U = true; 1874 break; 1875 case 'u': 1876 opt_u = true; 1877 break; 1878 case 'v': 1879 ++opt_v; 1880 break; 1881 case 'w': 1882 opt_w = true; 1883 break; 1884 default: 1885 usage(); 1886 } 1887 1888 argc -= optind; 1889 argv += optind; 1890 1891 if (argc > 0) 1892 usage(); 1893 1894 if (opt_j > 0) { 1895 switch (jail_getvnet(opt_j)) { 1896 case -1: 1897 xo_errx(2, "jail_getvnet: %s", jail_errmsg); 1898 case JAIL_SYS_NEW: 1899 if (jail_attach(opt_j) < 0) 1900 xo_err(3, "jail_attach()"); 1901 /* Set back to -1 for normal output in vnet jail. */ 1902 opt_j = -1; 1903 break; 1904 default: 1905 break; 1906 } 1907 } 1908 1909 capcas = cap_init(); 1910 if (capcas == NULL) 1911 xo_err(1, "Unable to contact Casper"); 1912 if (caph_enter_casper() < 0) 1913 xo_err(1, "Unable to enter capability mode"); 1914 capnet = cap_service_open(capcas, "system.net"); 1915 if (capnet == NULL) 1916 xo_err(1, "Unable to open system.net service"); 1917 capnetdb = cap_service_open(capcas, "system.netdb"); 1918 if (capnetdb == NULL) 1919 xo_err(1, "Unable to open system.netdb service"); 1920 capsysctl = cap_service_open(capcas, "system.sysctl"); 1921 if (capsysctl == NULL) 1922 xo_err(1, "Unable to open system.sysctl service"); 1923 cappwd = cap_service_open(capcas, "system.pwd"); 1924 if (cappwd == NULL) 1925 xo_err(1, "Unable to open system.pwd service"); 1926 cap_close(capcas); 1927 limit = cap_net_limit_init(capnet, CAPNET_ADDR2NAME); 1928 if (limit == NULL) 1929 xo_err(1, "Unable to init cap_net limits"); 1930 if (cap_net_limit(limit) < 0) 1931 xo_err(1, "Unable to apply limits"); 1932 if (cap_pwd_limit_cmds(cappwd, pwdcmds, nitems(pwdcmds)) < 0) 1933 xo_err(1, "Unable to apply pwd commands limits"); 1934 if (cap_pwd_limit_fields(cappwd, pwdfields, nitems(pwdfields)) < 0) 1935 xo_err(1, "Unable to apply pwd commands limits"); 1936 1937 if ((!opt_4 && !opt_6) && protos_defined != -1) 1938 opt_4 = opt_6 = true; 1939 if (!opt_4 && !opt_6 && !opt_u) 1940 opt_4 = opt_6 = opt_u = true; 1941 if ((opt_4 || opt_6) && protos_defined == -1) 1942 protos_defined = set_default_protos(); 1943 if (!opt_c && !opt_l) 1944 opt_c = opt_l = true; 1945 1946 if (opt_4 || opt_6) { 1947 for (i = 0; i < protos_defined; i++) 1948 if (protos[i] == IPPROTO_SCTP) 1949 gather_sctp(); 1950 else 1951 gather_inet(protos[i]); 1952 } 1953 1954 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) { 1955 gather_unix(SOCK_STREAM); 1956 gather_unix(SOCK_DGRAM); 1957 gather_unix(SOCK_SEQPACKET); 1958 } 1959 getfiles(); 1960 display(); 1961 exit(0); 1962 } 1963