2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sysctl.h>
44 #include <sys/unpcb.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_pcb.h>
50 #include <netinet/sctp.h>
51 #include <netinet/tcp.h>
52 #define TCPSTATES /* load state names */
53 #include <netinet/tcp_fsm.h>
54 #include <netinet/tcp_seq.h>
55 #include <netinet/tcp_var.h>
56 #include <arpa/inet.h>
71 #define sstosin(ss) ((struct sockaddr_in *)(ss))
72 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
73 #define sstosun(ss) ((struct sockaddr_un *)(ss))
74 #define sstosa(ss) ((struct sockaddr *)(ss))
76 static int opt_4; /* Show IPv4 sockets */
77 static int opt_6; /* Show IPv6 sockets */
78 static int opt_C; /* Show congestion control */
79 static int opt_c; /* Show connected sockets */
80 static int opt_i; /* Show inp_gencnt */
81 static int opt_j; /* Show specified jail */
82 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
83 static int opt_l; /* Show listening sockets */
84 static int opt_n; /* Don't resolve UIDs to user names */
85 static int opt_q; /* Don't show header */
86 static int opt_S; /* Show protocol stack if applicable */
87 static int opt_s; /* Show protocol state if applicable */
88 static int opt_U; /* Show remote UDP encapsulation port number */
89 static int opt_u; /* Show Unix domain sockets */
90 static int opt_v; /* Verbose mode */
91 static int opt_w; /* Wide print area for addresses */
94 * Default protocols to use if no -P was defined.
96 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
97 static size_t default_numprotos = nitems(default_protos);
99 static int *protos; /* protocols to use */
100 static size_t numprotos; /* allocated size of protos[] */
104 #define INT_BIT (sizeof(int)*CHAR_BIT)
105 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
106 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
109 struct sockaddr_storage address;
110 unsigned int encaps_port;
124 const char *protoname;
125 char stack[TCP_FUNCTION_NAME_LEN_MAX];
126 char cc[TCP_CA_NAME_MAX];
132 #define HASHSIZE 1009
133 static struct sock *sockhash[HASHSIZE];
135 static struct xfile *xfiles;
139 xprintf(const char *fmt, ...)
145 len = vprintf(fmt, ap);
153 get_proto_type(const char *proto)
155 struct protoent *pent;
157 if (strlen(proto) == 0)
159 pent = getprotobyname(proto);
161 warn("getprotobyname");
164 return (pent->p_proto);
175 /* Find the maximum number of possible protocols. */
176 while (getprotoent() != NULL)
181 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
183 numprotos = proto_count;
187 parse_protos(char *protospec)
190 int proto_type, proto_index;
192 if (protospec == NULL)
197 while ((prot = strsep(&protospec, ",")) != NULL) {
198 if (strlen(prot) == 0)
200 proto_type = get_proto_type(prot);
201 if (proto_type != -1)
202 protos[proto_index++] = proto_type;
204 numprotos = proto_index;
205 return (proto_index);
209 parse_ports(const char *portspec)
215 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
220 errx(1, "syntax error in port range");
221 for (q = p; *q != '\0' && isdigit(*q); ++q)
223 for (port = 0; p < q; ++p)
224 port = port * 10 + digittoint(*p);
225 if (port < 0 || port > 65535)
226 errx(1, "invalid port number");
239 for (q = p; *q != '\0' && isdigit(*q); ++q)
241 for (end = 0; p < q; ++p)
242 end = end * 10 + digittoint(*p);
243 if (end < port || end > 65535)
244 errx(1, "invalid port number");
253 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
255 struct sockaddr_in *sin4;
256 struct sockaddr_in6 *sin6;
258 bzero(ss, sizeof(*ss));
262 sin4->sin_len = sizeof(*sin4);
263 sin4->sin_family = af;
264 sin4->sin_port = port;
265 sin4->sin_addr = *(struct in_addr *)addr;
269 sin6->sin6_len = sizeof(*sin6);
270 sin6->sin6_family = af;
271 sin6->sin6_port = port;
272 sin6->sin6_addr = *(struct in6_addr *)addr;
273 #define s6_addr16 __u6_addr.__u6_addr16
274 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
275 sin6->sin6_scope_id =
276 ntohs(sin6->sin6_addr.s6_addr16[1]);
277 sin6->sin6_addr.s6_addr16[1] = 0;
286 free_socket(struct sock *sock)
288 struct addr *cur, *next;
291 while (cur != NULL) {
297 while (cur != NULL) {
309 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
310 struct xsctp_inpcb *xinpcb;
311 struct xsctp_tcb *xstcb;
312 struct xsctp_raddr *xraddr;
313 struct xsctp_laddr *xladdr;
318 int no_stcb, local_all_loopback, foreign_all_loopback;
326 varname = "net.inet.sctp.assoclist";
327 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
329 err(1, "sysctlbyname()");
332 if ((buf = (char *)malloc(len)) == NULL) {
336 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
337 err(1, "sysctlbyname()");
341 xinpcb = (struct xsctp_inpcb *)(void *)buf;
342 offset = sizeof(struct xsctp_inpcb);
343 while ((offset < len) && (xinpcb->last == 0)) {
344 if ((sock = calloc(1, sizeof *sock)) == NULL)
346 sock->socket = xinpcb->socket;
347 sock->proto = IPPROTO_SCTP;
348 sock->protoname = "sctp";
349 if (xinpcb->maxqlen == 0)
350 sock->state = SCTP_CLOSED;
352 sock->state = SCTP_LISTEN;
353 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
354 sock->family = AF_INET6;
356 * Currently there is no way to distinguish between
357 * IPv6 only sockets or dual family sockets.
358 * So mark it as dual socket.
360 sock->vflag = INP_IPV6 | INP_IPV4;
362 sock->family = AF_INET;
363 sock->vflag = INP_IPV4;
366 local_all_loopback = 1;
367 while (offset < len) {
368 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
369 offset += sizeof(struct xsctp_laddr);
370 if (xladdr->last == 1)
372 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
374 switch (xladdr->address.sa.sa_family) {
376 #define __IN_IS_ADDR_LOOPBACK(pina) \
377 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
378 if (!__IN_IS_ADDR_LOOPBACK(
379 &xladdr->address.sin.sin_addr))
380 local_all_loopback = 0;
381 #undef __IN_IS_ADDR_LOOPBACK
382 sockaddr(&laddr->address, AF_INET,
383 &xladdr->address.sin.sin_addr,
384 htons(xinpcb->local_port));
387 if (!IN6_IS_ADDR_LOOPBACK(
388 &xladdr->address.sin6.sin6_addr))
389 local_all_loopback = 0;
390 sockaddr(&laddr->address, AF_INET6,
391 &xladdr->address.sin6.sin6_addr,
392 htons(xinpcb->local_port));
395 errx(1, "address family %d not supported",
396 xladdr->address.sa.sa_family);
399 if (prev_laddr == NULL)
402 prev_laddr->next = laddr;
405 if (sock->laddr == NULL) {
407 calloc(1, sizeof(struct addr))) == NULL)
409 sock->laddr->address.ss_family = sock->family;
410 if (sock->family == AF_INET)
411 sock->laddr->address.ss_len =
412 sizeof(struct sockaddr_in);
414 sock->laddr->address.ss_len =
415 sizeof(struct sockaddr_in6);
416 local_all_loopback = 0;
418 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
420 sock->faddr->address.ss_family = sock->family;
421 if (sock->family == AF_INET)
422 sock->faddr->address.ss_len =
423 sizeof(struct sockaddr_in);
425 sock->faddr->address.ss_len =
426 sizeof(struct sockaddr_in6);
428 while (offset < len) {
429 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
430 offset += sizeof(struct xsctp_tcb);
432 if (opt_l && (sock->vflag & vflag) &&
433 (!opt_L || !local_all_loopback) &&
434 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
435 (xstcb->last == 1))) {
436 hash = (int)((uintptr_t)sock->socket %
438 sock->next = sockhash[hash];
439 sockhash[hash] = sock;
444 if (xstcb->last == 1)
448 if ((sock = calloc(1, sizeof *sock)) == NULL)
450 sock->socket = xinpcb->socket;
451 sock->proto = IPPROTO_SCTP;
452 sock->protoname = "sctp";
453 sock->state = (int)xstcb->state;
454 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
455 sock->family = AF_INET6;
457 * Currently there is no way to distinguish
458 * between IPv6 only sockets or dual family
459 * sockets. So mark it as dual socket.
461 sock->vflag = INP_IPV6 | INP_IPV4;
463 sock->family = AF_INET;
464 sock->vflag = INP_IPV4;
468 local_all_loopback = 1;
469 while (offset < len) {
470 xladdr = (struct xsctp_laddr *)(void *)(buf +
472 offset += sizeof(struct xsctp_laddr);
473 if (xladdr->last == 1)
477 laddr = calloc(1, sizeof(struct addr));
480 switch (xladdr->address.sa.sa_family) {
482 #define __IN_IS_ADDR_LOOPBACK(pina) \
483 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
484 if (!__IN_IS_ADDR_LOOPBACK(
485 &xladdr->address.sin.sin_addr))
486 local_all_loopback = 0;
487 #undef __IN_IS_ADDR_LOOPBACK
488 sockaddr(&laddr->address, AF_INET,
489 &xladdr->address.sin.sin_addr,
490 htons(xstcb->local_port));
493 if (!IN6_IS_ADDR_LOOPBACK(
494 &xladdr->address.sin6.sin6_addr))
495 local_all_loopback = 0;
496 sockaddr(&laddr->address, AF_INET6,
497 &xladdr->address.sin6.sin6_addr,
498 htons(xstcb->local_port));
502 "address family %d not supported",
503 xladdr->address.sa.sa_family);
506 if (prev_laddr == NULL)
509 prev_laddr->next = laddr;
513 foreign_all_loopback = 1;
514 while (offset < len) {
515 xraddr = (struct xsctp_raddr *)(void *)(buf +
517 offset += sizeof(struct xsctp_raddr);
518 if (xraddr->last == 1)
522 faddr = calloc(1, sizeof(struct addr));
525 switch (xraddr->address.sa.sa_family) {
527 #define __IN_IS_ADDR_LOOPBACK(pina) \
528 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
529 if (!__IN_IS_ADDR_LOOPBACK(
530 &xraddr->address.sin.sin_addr))
531 foreign_all_loopback = 0;
532 #undef __IN_IS_ADDR_LOOPBACK
533 sockaddr(&faddr->address, AF_INET,
534 &xraddr->address.sin.sin_addr,
535 htons(xstcb->remote_port));
538 if (!IN6_IS_ADDR_LOOPBACK(
539 &xraddr->address.sin6.sin6_addr))
540 foreign_all_loopback = 0;
541 sockaddr(&faddr->address, AF_INET6,
542 &xraddr->address.sin6.sin6_addr,
543 htons(xstcb->remote_port));
547 "address family %d not supported",
548 xraddr->address.sa.sa_family);
550 faddr->encaps_port = xraddr->encaps_port;
551 faddr->state = xraddr->state;
553 if (prev_faddr == NULL)
556 prev_faddr->next = faddr;
560 if ((sock->vflag & vflag) &&
562 !(local_all_loopback ||
563 foreign_all_loopback))) {
564 hash = (int)((uintptr_t)sock->socket %
566 sock->next = sockhash[hash];
567 sockhash[hash] = sock;
573 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
574 offset += sizeof(struct xsctp_inpcb);
580 gather_inet(int proto)
582 struct xinpgen *xig, *exig;
584 struct xtcpcb *xtp = NULL;
587 struct addr *laddr, *faddr;
588 const char *varname, *protoname;
591 int hash, retry, vflag;
601 varname = "net.inet.tcp.pcblist";
605 varname = "net.inet.udp.pcblist";
609 varname = "net.inet.divert.pcblist";
613 errx(1, "protocol %d not supported", proto);
621 if ((buf = realloc(buf, bufsize)) == NULL)
624 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
628 if (errno != ENOMEM || len != bufsize)
629 err(1, "sysctlbyname()");
632 xig = (struct xinpgen *)buf;
633 exig = (struct xinpgen *)(void *)
634 ((char *)buf + len - sizeof *exig);
635 if (xig->xig_len != sizeof *xig ||
636 exig->xig_len != sizeof *exig)
637 errx(1, "struct xinpgen size mismatch");
638 } while (xig->xig_gen != exig->xig_gen && retry--);
640 if (xig->xig_gen != exig->xig_gen && opt_v)
641 warnx("warning: data may be inconsistent");
644 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
649 xtp = (struct xtcpcb *)xig;
651 if (xtp->xt_len != sizeof(*xtp)) {
652 warnx("struct xtcpcb size mismatch");
655 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
659 xip = (struct xinpcb *)xig;
660 if (xip->xi_len != sizeof(*xip)) {
661 warnx("struct xinpcb size mismatch");
666 errx(1, "protocol %d not supported", proto);
668 so = &xip->xi_socket;
669 if ((xip->inp_vflag & vflag) == 0)
671 if (xip->inp_vflag & INP_IPV4) {
672 if ((xip->inp_fport == 0 && !opt_l) ||
673 (xip->inp_fport != 0 && !opt_c))
675 #define __IN_IS_ADDR_LOOPBACK(pina) \
676 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
678 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
679 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
681 #undef __IN_IS_ADDR_LOOPBACK
682 } else if (xip->inp_vflag & INP_IPV6) {
683 if ((xip->inp_fport == 0 && !opt_l) ||
684 (xip->inp_fport != 0 && !opt_c))
687 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
688 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
692 warnx("invalid vflag 0x%x", xip->inp_vflag);
695 if ((sock = calloc(1, sizeof(*sock))) == NULL)
697 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
699 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
701 sock->socket = so->xso_so;
703 sock->inp_gencnt = xip->inp_gencnt;
704 if (xip->inp_vflag & INP_IPV4) {
705 sock->family = AF_INET;
706 sockaddr(&laddr->address, sock->family,
707 &xip->inp_laddr, xip->inp_lport);
708 sockaddr(&faddr->address, sock->family,
709 &xip->inp_faddr, xip->inp_fport);
710 } else if (xip->inp_vflag & INP_IPV6) {
711 sock->family = AF_INET6;
712 sockaddr(&laddr->address, sock->family,
713 &xip->in6p_laddr, xip->inp_lport);
714 sockaddr(&faddr->address, sock->family,
715 &xip->in6p_faddr, xip->inp_fport);
717 if (proto == IPPROTO_TCP)
718 faddr->encaps_port = xtp->xt_encaps_port;
723 sock->vflag = xip->inp_vflag;
724 if (proto == IPPROTO_TCP) {
725 sock->state = xtp->t_state;
726 memcpy(sock->stack, xtp->xt_stack,
727 TCP_FUNCTION_NAME_LEN_MAX);
728 memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX);
730 sock->protoname = protoname;
731 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
732 sock->next = sockhash[hash];
733 sockhash[hash] = sock;
740 gather_unix(int proto)
742 struct xunpgen *xug, *exug;
745 struct addr *laddr, *faddr;
746 const char *varname, *protoname;
753 varname = "net.local.stream.pcblist";
754 protoname = "stream";
757 varname = "net.local.dgram.pcblist";
761 varname = "net.local.seqpacket.pcblist";
762 protoname = "seqpac";
772 if ((buf = realloc(buf, bufsize)) == NULL)
775 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
777 if (errno != ENOMEM || len != bufsize)
778 err(1, "sysctlbyname()");
781 xug = (struct xunpgen *)buf;
782 exug = (struct xunpgen *)(void *)
783 ((char *)buf + len - sizeof(*exug));
784 if (xug->xug_len != sizeof(*xug) ||
785 exug->xug_len != sizeof(*exug)) {
786 warnx("struct xinpgen size mismatch");
789 } while (xug->xug_gen != exug->xug_gen && retry--);
791 if (xug->xug_gen != exug->xug_gen && opt_v)
792 warnx("warning: data may be inconsistent");
795 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
798 xup = (struct xunpcb *)xug;
799 if (xup->xu_len != sizeof(*xup)) {
800 warnx("struct xunpcb size mismatch");
803 if ((xup->unp_conn == 0 && !opt_l) ||
804 (xup->unp_conn != 0 && !opt_c))
806 if ((sock = calloc(1, sizeof(*sock))) == NULL)
808 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
810 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
812 sock->socket = xup->xu_socket.xso_so;
813 sock->pcb = xup->xu_unpp;
815 sock->family = AF_UNIX;
816 sock->protoname = protoname;
817 if (xup->xu_addr.sun_family == AF_UNIX)
819 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
820 else if (xup->unp_conn != 0)
821 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
826 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
827 sock->next = sockhash[hash];
828 sockhash[hash] = sock;
839 olen = len = sizeof(*xfiles);
840 if ((xfiles = malloc(len)) == NULL)
842 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
843 if (errno != ENOMEM || len != olen)
844 err(1, "sysctlbyname()");
846 if ((xfiles = realloc(xfiles, len)) == NULL)
849 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
850 errx(1, "struct xfile size mismatch");
851 nxfiles = len / sizeof(*xfiles);
855 printaddr(struct sockaddr_storage *ss)
857 struct sockaddr_un *sun;
858 char addrstr[NI_MAXHOST] = { '\0', '\0' };
859 int error, off, port = 0;
861 switch (ss->ss_family) {
863 if (sstosin(ss)->sin_addr.s_addr == INADDR_ANY)
865 port = ntohs(sstosin(ss)->sin_port);
868 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
870 port = ntohs(sstosin6(ss)->sin6_port);
874 off = (int)((char *)&sun->sun_path - (char *)sun);
875 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
877 if (addrstr[0] == '\0') {
878 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
879 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
881 errx(1, "getnameinfo()");
884 return xprintf("%s:*", addrstr);
886 return xprintf("%s:%d", addrstr, port);
890 getprocname(pid_t pid)
892 static struct kinfo_proc proc;
898 mib[2] = KERN_PROC_PID;
901 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
902 /* Do not warn if the process exits before we get its name. */
907 return (proc.ki_comm);
911 getprocjid(pid_t pid)
913 static struct kinfo_proc proc;
919 mib[2] = KERN_PROC_PID;
922 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
923 /* Do not warn if the process exits before we get its jid. */
928 return (proc.ki_jid);
932 check_ports(struct sock *s)
939 if ((s->family != AF_INET) && (s->family != AF_INET6))
941 for (addr = s->laddr; addr != NULL; addr = addr->next) {
942 if (s->family == AF_INET)
943 port = ntohs(sstosin(&addr->address)->sin_port);
945 port = ntohs(sstosin6(&addr->address)->sin6_port);
949 for (addr = s->faddr; addr != NULL; addr = addr->next) {
950 if (s->family == AF_INET)
951 port = ntohs(sstosin(&addr->address)->sin_port);
953 port = ntohs(sstosin6(&addr->address)->sin6_port);
961 sctp_conn_state(int state)
973 case SCTP_COOKIE_WAIT:
974 return "COOKIE_WAIT";
976 case SCTP_COOKIE_ECHOED:
977 return "COOKIE_ECHOED";
979 case SCTP_ESTABLISHED:
980 return "ESTABLISHED";
982 case SCTP_SHUTDOWN_SENT:
983 return "SHUTDOWN_SENT";
985 case SCTP_SHUTDOWN_RECEIVED:
986 return "SHUTDOWN_RECEIVED";
988 case SCTP_SHUTDOWN_ACK_SENT:
989 return "SHUTDOWN_ACK_SENT";
991 case SCTP_SHUTDOWN_PENDING:
992 return "SHUTDOWN_PENDING";
1001 sctp_path_state(int state)
1004 case SCTP_UNCONFIRMED:
1005 return "UNCONFIRMED";
1020 displaysock(struct sock *s, int pos)
1023 int hash, first, offset;
1024 struct addr *laddr, *faddr;
1028 pos += xprintf(" ");
1029 pos += xprintf("%s", s->protoname);
1030 if (s->vflag & INP_IPV4)
1031 pos += xprintf("4");
1032 if (s->vflag & INP_IPV6)
1033 pos += xprintf("6");
1034 if (s->vflag & (INP_IPV4 | INP_IPV6))
1035 pos += xprintf(" ");
1039 while (laddr != NULL || faddr != NULL) {
1041 while (pos < offset)
1042 pos += xprintf(" ");
1043 switch (s->family) {
1046 if (laddr != NULL) {
1047 pos += printaddr(&laddr->address);
1048 if (s->family == AF_INET6 && pos >= 58)
1049 pos += xprintf(" ");
1051 offset += opt_w ? 46 : 22;
1052 while (pos < offset)
1053 pos += xprintf(" ");
1055 pos += printaddr(&faddr->address);
1056 offset += opt_w ? 46 : 22;
1059 if ((laddr == NULL) || (faddr == NULL))
1060 errx(1, "laddr = %p or faddr = %p is NULL",
1061 (void *)laddr, (void *)faddr);
1063 if (laddr->address.ss_len > 0) {
1064 pos += printaddr(&laddr->address);
1068 p = *(kvaddr_t*)&(faddr->address);
1070 pos += xprintf("(not connected)");
1071 offset += opt_w ? 92 : 44;
1074 pos += xprintf("-> ");
1075 for (hash = 0; hash < HASHSIZE; ++hash) {
1076 for (s_tmp = sockhash[hash];
1078 s_tmp = s_tmp->next)
1079 if (s_tmp->pcb == p)
1084 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1085 s_tmp->laddr->address.ss_len == 0)
1086 pos += xprintf("??");
1088 pos += printaddr(&s_tmp->laddr->address);
1089 offset += opt_w ? 92 : 44;
1095 if (s->proto == IPPROTO_TCP ||
1096 s->proto == IPPROTO_UDP) {
1097 while (pos < offset)
1098 pos += xprintf(" ");
1099 pos += xprintf("%" PRIu64, s->inp_gencnt);
1104 if (faddr != NULL &&
1105 ((s->proto == IPPROTO_SCTP &&
1106 s->state != SCTP_CLOSED &&
1107 s->state != SCTP_BOUND &&
1108 s->state != SCTP_LISTEN) ||
1109 (s->proto == IPPROTO_TCP &&
1110 s->state != TCPS_CLOSED &&
1111 s->state != TCPS_LISTEN))) {
1112 while (pos < offset)
1113 pos += xprintf(" ");
1114 pos += xprintf("%u",
1115 ntohs(faddr->encaps_port));
1120 if (faddr != NULL &&
1121 s->proto == IPPROTO_SCTP &&
1122 s->state != SCTP_CLOSED &&
1123 s->state != SCTP_BOUND &&
1124 s->state != SCTP_LISTEN) {
1125 while (pos < offset)
1126 pos += xprintf(" ");
1127 pos += xprintf("%s",
1128 sctp_path_state(faddr->state));
1134 if (s->proto == IPPROTO_SCTP ||
1135 s->proto == IPPROTO_TCP) {
1136 while (pos < offset)
1137 pos += xprintf(" ");
1140 pos += xprintf("%s",
1141 sctp_conn_state(s->state));
1144 if (s->state >= 0 &&
1145 s->state < TCP_NSTATES)
1146 pos += xprintf("%s",
1147 tcpstates[s->state]);
1149 pos += xprintf("?");
1156 if (s->proto == IPPROTO_TCP) {
1157 while (pos < offset)
1158 pos += xprintf(" ");
1159 pos += xprintf("%.*s",
1160 TCP_FUNCTION_NAME_LEN_MAX,
1163 offset += TCP_FUNCTION_NAME_LEN_MAX + 1;
1166 if (s->proto == IPPROTO_TCP) {
1167 while (pos < offset)
1168 pos += xprintf(" ");
1169 xprintf("%.*s", TCP_CA_NAME_MAX, s->cc);
1171 offset += TCP_CA_NAME_MAX + 1;
1175 laddr = laddr->next;
1177 faddr = faddr->next;
1178 if ((laddr != NULL) || (faddr != NULL)) {
1196 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1197 "USER", "COMMAND", "PID", "FD", "PROTO",
1198 opt_w ? 45 : 21, "LOCAL ADDRESS",
1199 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1201 printf(" %-8s", "ID");
1203 printf(" %-6s", "ENCAPS");
1205 printf(" %-12s", "PATH STATE");
1206 printf(" %-12s", "CONN STATE");
1209 printf(" %-*.*s", TCP_FUNCTION_NAME_LEN_MAX,
1210 TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1212 printf(" %-.*s", TCP_CA_NAME_MAX, "CC");
1216 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1217 if (xf->xf_data == 0)
1219 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1221 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1222 for (s = sockhash[hash]; s != NULL; s = s->next) {
1223 if (s->socket != xf->xf_data)
1225 if (!check_ports(s))
1229 if (opt_n || (pwd = getpwuid(xf->xf_uid)) == NULL)
1230 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1232 pos += xprintf("%s ", pwd->pw_name);
1234 pos += xprintf(" ");
1235 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1237 pos += xprintf(" ");
1238 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1240 pos += xprintf(" ");
1241 pos += xprintf("%d ", xf->xf_fd);
1242 displaysock(s, pos);
1247 for (hash = 0; hash < HASHSIZE; hash++) {
1248 for (s = sockhash[hash]; s != NULL; s = s->next) {
1251 if (!check_ports(s))
1254 pos += xprintf("%-8s %-10s %-5s %-2s ",
1255 "?", "?", "?", "?");
1256 displaysock(s, pos);
1262 set_default_protos(void)
1264 struct protoent *prot;
1268 init_protos(default_numprotos);
1270 for (pindex = 0; pindex < default_numprotos; pindex++) {
1271 pname = default_protos[pindex];
1272 prot = getprotobyname(pname);
1274 err(1, "getprotobyname: %s", pname);
1275 protos[pindex] = prot->p_proto;
1282 * Return the vnet property of the jail, or -1 on error.
1285 jail_getvnet(int jid)
1287 struct iovec jiov[6];
1291 jiov[0].iov_base = __DECONST(char *, "jid");
1292 jiov[0].iov_len = sizeof("jid");
1293 jiov[1].iov_base = &jid;
1294 jiov[1].iov_len = sizeof(jid);
1295 jiov[2].iov_base = __DECONST(char *, "vnet");
1296 jiov[2].iov_len = sizeof("vnet");
1297 jiov[3].iov_base = &vnet;
1298 jiov[3].iov_len = sizeof(vnet);
1299 jiov[4].iov_base = __DECONST(char *, "errmsg");
1300 jiov[4].iov_len = sizeof("errmsg");
1301 jiov[5].iov_base = jail_errmsg;
1302 jiov[5].iov_len = JAIL_ERRMSGLEN;
1303 jail_errmsg[0] = '\0';
1304 if (jail_get(jiov, nitems(jiov), 0) < 0) {
1305 if (!jail_errmsg[0])
1306 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1307 "jail_get: %s", strerror(errno));
1317 "usage: sockstat [-46CciLlnqSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1322 main(int argc, char *argv[])
1324 int protos_defined = -1;
1328 while ((o = getopt(argc, argv, "46Ccij:Llnp:P:qSsUuvw")) != -1)
1346 opt_j = jail_getid(optarg);
1348 errx(1, "%s", jail_errmsg);
1360 parse_ports(optarg);
1363 protos_defined = parse_protos(optarg);
1397 switch (jail_getvnet(opt_j)) {
1399 errx(2, "%s", jail_errmsg);
1401 if (jail_attach(opt_j) < 0)
1402 err(3, "jail_attach()");
1403 /* Set back to -1 for normal output in vnet jail. */
1411 if ((!opt_4 && !opt_6) && protos_defined != -1)
1413 if (!opt_4 && !opt_6 && !opt_u)
1414 opt_4 = opt_6 = opt_u = 1;
1415 if ((opt_4 || opt_6) && protos_defined == -1)
1416 protos_defined = set_default_protos();
1417 if (!opt_c && !opt_l)
1420 if (opt_4 || opt_6) {
1421 for (i = 0; i < protos_defined; i++)
1422 if (protos[i] == IPPROTO_SCTP)
1425 gather_inet(protos[i]);
1428 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1429 gather_unix(SOCK_STREAM);
1430 gather_unix(SOCK_DGRAM);
1431 gather_unix(SOCK_SEQPACKET);