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>
70 #define sstosin(ss) ((struct sockaddr_in *)(ss))
71 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
72 #define sstosun(ss) ((struct sockaddr_un *)(ss))
73 #define sstosa(ss) ((struct sockaddr *)(ss))
75 static int opt_4; /* Show IPv4 sockets */
76 static int opt_6; /* Show IPv6 sockets */
77 static int opt_C; /* Show congestion control */
78 static int opt_c; /* Show connected sockets */
79 static int opt_j; /* Show specified jail */
80 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
81 static int opt_l; /* Show listening sockets */
82 static int opt_n; /* Don't resolve UIDs to user names */
83 static int opt_q; /* Don't show header */
84 static int opt_S; /* Show protocol stack if applicable */
85 static int opt_s; /* Show protocol state if applicable */
86 static int opt_U; /* Show remote UDP encapsulation port number */
87 static int opt_u; /* Show Unix domain sockets */
88 static int opt_v; /* Verbose mode */
89 static int opt_w; /* Wide print area for addresses */
92 * Default protocols to use if no -P was defined.
94 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
95 static size_t default_numprotos = nitems(default_protos);
97 static int *protos; /* protocols to use */
98 static size_t numprotos; /* allocated size of protos[] */
102 #define INT_BIT (sizeof(int)*CHAR_BIT)
103 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
104 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
107 struct sockaddr_storage address;
108 unsigned int encaps_port;
121 const char *protoname;
122 char stack[TCP_FUNCTION_NAME_LEN_MAX];
123 char cc[TCP_CA_NAME_MAX];
129 #define HASHSIZE 1009
130 static struct sock *sockhash[HASHSIZE];
132 static struct xfile *xfiles;
136 xprintf(const char *fmt, ...)
142 len = vprintf(fmt, ap);
150 get_proto_type(const char *proto)
152 struct protoent *pent;
154 if (strlen(proto) == 0)
156 pent = getprotobyname(proto);
158 warn("getprotobyname");
161 return (pent->p_proto);
172 /* Find the maximum number of possible protocols. */
173 while (getprotoent() != NULL)
178 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
180 numprotos = proto_count;
184 parse_protos(char *protospec)
187 int proto_type, proto_index;
189 if (protospec == NULL)
194 while ((prot = strsep(&protospec, ",")) != NULL) {
195 if (strlen(prot) == 0)
197 proto_type = get_proto_type(prot);
198 if (proto_type != -1)
199 protos[proto_index++] = proto_type;
201 numprotos = proto_index;
202 return (proto_index);
206 parse_ports(const char *portspec)
212 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
217 errx(1, "syntax error in port range");
218 for (q = p; *q != '\0' && isdigit(*q); ++q)
220 for (port = 0; p < q; ++p)
221 port = port * 10 + digittoint(*p);
222 if (port < 0 || port > 65535)
223 errx(1, "invalid port number");
236 for (q = p; *q != '\0' && isdigit(*q); ++q)
238 for (end = 0; p < q; ++p)
239 end = end * 10 + digittoint(*p);
240 if (end < port || end > 65535)
241 errx(1, "invalid port number");
250 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
252 struct sockaddr_in *sin4;
253 struct sockaddr_in6 *sin6;
255 bzero(ss, sizeof(*ss));
259 sin4->sin_len = sizeof(*sin4);
260 sin4->sin_family = af;
261 sin4->sin_port = port;
262 sin4->sin_addr = *(struct in_addr *)addr;
266 sin6->sin6_len = sizeof(*sin6);
267 sin6->sin6_family = af;
268 sin6->sin6_port = port;
269 sin6->sin6_addr = *(struct in6_addr *)addr;
270 #define s6_addr16 __u6_addr.__u6_addr16
271 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
272 sin6->sin6_scope_id =
273 ntohs(sin6->sin6_addr.s6_addr16[1]);
274 sin6->sin6_addr.s6_addr16[1] = 0;
283 free_socket(struct sock *sock)
285 struct addr *cur, *next;
288 while (cur != NULL) {
294 while (cur != NULL) {
306 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
307 struct xsctp_inpcb *xinpcb;
308 struct xsctp_tcb *xstcb;
309 struct xsctp_raddr *xraddr;
310 struct xsctp_laddr *xladdr;
315 int no_stcb, local_all_loopback, foreign_all_loopback;
323 varname = "net.inet.sctp.assoclist";
324 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
326 err(1, "sysctlbyname()");
329 if ((buf = (char *)malloc(len)) == NULL) {
333 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
334 err(1, "sysctlbyname()");
338 xinpcb = (struct xsctp_inpcb *)(void *)buf;
339 offset = sizeof(struct xsctp_inpcb);
340 while ((offset < len) && (xinpcb->last == 0)) {
341 if ((sock = calloc(1, sizeof *sock)) == NULL)
343 sock->socket = xinpcb->socket;
344 sock->proto = IPPROTO_SCTP;
345 sock->protoname = "sctp";
346 if (xinpcb->maxqlen == 0)
347 sock->state = SCTP_CLOSED;
349 sock->state = SCTP_LISTEN;
350 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
351 sock->family = AF_INET6;
353 * Currently there is no way to distinguish between
354 * IPv6 only sockets or dual family sockets.
355 * So mark it as dual socket.
357 sock->vflag = INP_IPV6 | INP_IPV4;
359 sock->family = AF_INET;
360 sock->vflag = INP_IPV4;
363 local_all_loopback = 1;
364 while (offset < len) {
365 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
366 offset += sizeof(struct xsctp_laddr);
367 if (xladdr->last == 1)
369 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
371 switch (xladdr->address.sa.sa_family) {
373 #define __IN_IS_ADDR_LOOPBACK(pina) \
374 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
375 if (!__IN_IS_ADDR_LOOPBACK(
376 &xladdr->address.sin.sin_addr))
377 local_all_loopback = 0;
378 #undef __IN_IS_ADDR_LOOPBACK
379 sockaddr(&laddr->address, AF_INET,
380 &xladdr->address.sin.sin_addr,
381 htons(xinpcb->local_port));
384 if (!IN6_IS_ADDR_LOOPBACK(
385 &xladdr->address.sin6.sin6_addr))
386 local_all_loopback = 0;
387 sockaddr(&laddr->address, AF_INET6,
388 &xladdr->address.sin6.sin6_addr,
389 htons(xinpcb->local_port));
392 errx(1, "address family %d not supported",
393 xladdr->address.sa.sa_family);
396 if (prev_laddr == NULL)
399 prev_laddr->next = laddr;
402 if (sock->laddr == NULL) {
404 calloc(1, sizeof(struct addr))) == NULL)
406 sock->laddr->address.ss_family = sock->family;
407 if (sock->family == AF_INET)
408 sock->laddr->address.ss_len =
409 sizeof(struct sockaddr_in);
411 sock->laddr->address.ss_len =
412 sizeof(struct sockaddr_in6);
413 local_all_loopback = 0;
415 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
417 sock->faddr->address.ss_family = sock->family;
418 if (sock->family == AF_INET)
419 sock->faddr->address.ss_len =
420 sizeof(struct sockaddr_in);
422 sock->faddr->address.ss_len =
423 sizeof(struct sockaddr_in6);
425 while (offset < len) {
426 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
427 offset += sizeof(struct xsctp_tcb);
429 if (opt_l && (sock->vflag & vflag) &&
430 (!opt_L || !local_all_loopback) &&
431 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
432 (xstcb->last == 1))) {
433 hash = (int)((uintptr_t)sock->socket %
435 sock->next = sockhash[hash];
436 sockhash[hash] = sock;
441 if (xstcb->last == 1)
445 if ((sock = calloc(1, sizeof *sock)) == NULL)
447 sock->socket = xinpcb->socket;
448 sock->proto = IPPROTO_SCTP;
449 sock->protoname = "sctp";
450 sock->state = (int)xstcb->state;
451 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
452 sock->family = AF_INET6;
454 * Currently there is no way to distinguish
455 * between IPv6 only sockets or dual family
456 * sockets. So mark it as dual socket.
458 sock->vflag = INP_IPV6 | INP_IPV4;
460 sock->family = AF_INET;
461 sock->vflag = INP_IPV4;
465 local_all_loopback = 1;
466 while (offset < len) {
467 xladdr = (struct xsctp_laddr *)(void *)(buf +
469 offset += sizeof(struct xsctp_laddr);
470 if (xladdr->last == 1)
474 laddr = calloc(1, sizeof(struct addr));
477 switch (xladdr->address.sa.sa_family) {
479 #define __IN_IS_ADDR_LOOPBACK(pina) \
480 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
481 if (!__IN_IS_ADDR_LOOPBACK(
482 &xladdr->address.sin.sin_addr))
483 local_all_loopback = 0;
484 #undef __IN_IS_ADDR_LOOPBACK
485 sockaddr(&laddr->address, AF_INET,
486 &xladdr->address.sin.sin_addr,
487 htons(xstcb->local_port));
490 if (!IN6_IS_ADDR_LOOPBACK(
491 &xladdr->address.sin6.sin6_addr))
492 local_all_loopback = 0;
493 sockaddr(&laddr->address, AF_INET6,
494 &xladdr->address.sin6.sin6_addr,
495 htons(xstcb->local_port));
499 "address family %d not supported",
500 xladdr->address.sa.sa_family);
503 if (prev_laddr == NULL)
506 prev_laddr->next = laddr;
510 foreign_all_loopback = 1;
511 while (offset < len) {
512 xraddr = (struct xsctp_raddr *)(void *)(buf +
514 offset += sizeof(struct xsctp_raddr);
515 if (xraddr->last == 1)
519 faddr = calloc(1, sizeof(struct addr));
522 switch (xraddr->address.sa.sa_family) {
524 #define __IN_IS_ADDR_LOOPBACK(pina) \
525 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
526 if (!__IN_IS_ADDR_LOOPBACK(
527 &xraddr->address.sin.sin_addr))
528 foreign_all_loopback = 0;
529 #undef __IN_IS_ADDR_LOOPBACK
530 sockaddr(&faddr->address, AF_INET,
531 &xraddr->address.sin.sin_addr,
532 htons(xstcb->remote_port));
535 if (!IN6_IS_ADDR_LOOPBACK(
536 &xraddr->address.sin6.sin6_addr))
537 foreign_all_loopback = 0;
538 sockaddr(&faddr->address, AF_INET6,
539 &xraddr->address.sin6.sin6_addr,
540 htons(xstcb->remote_port));
544 "address family %d not supported",
545 xraddr->address.sa.sa_family);
547 faddr->encaps_port = xraddr->encaps_port;
548 faddr->state = xraddr->state;
550 if (prev_faddr == NULL)
553 prev_faddr->next = faddr;
557 if ((sock->vflag & vflag) &&
559 !(local_all_loopback ||
560 foreign_all_loopback))) {
561 hash = (int)((uintptr_t)sock->socket %
563 sock->next = sockhash[hash];
564 sockhash[hash] = sock;
570 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
571 offset += sizeof(struct xsctp_inpcb);
577 gather_inet(int proto)
579 struct xinpgen *xig, *exig;
581 struct xtcpcb *xtp = NULL;
584 struct addr *laddr, *faddr;
585 const char *varname, *protoname;
588 int hash, retry, vflag;
598 varname = "net.inet.tcp.pcblist";
602 varname = "net.inet.udp.pcblist";
606 varname = "net.inet.divert.pcblist";
610 errx(1, "protocol %d not supported", proto);
618 if ((buf = realloc(buf, bufsize)) == NULL)
621 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
625 if (errno != ENOMEM || len != bufsize)
626 err(1, "sysctlbyname()");
629 xig = (struct xinpgen *)buf;
630 exig = (struct xinpgen *)(void *)
631 ((char *)buf + len - sizeof *exig);
632 if (xig->xig_len != sizeof *xig ||
633 exig->xig_len != sizeof *exig)
634 errx(1, "struct xinpgen size mismatch");
635 } while (xig->xig_gen != exig->xig_gen && retry--);
637 if (xig->xig_gen != exig->xig_gen && opt_v)
638 warnx("warning: data may be inconsistent");
641 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
646 xtp = (struct xtcpcb *)xig;
648 if (xtp->xt_len != sizeof(*xtp)) {
649 warnx("struct xtcpcb size mismatch");
652 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
656 xip = (struct xinpcb *)xig;
657 if (xip->xi_len != sizeof(*xip)) {
658 warnx("struct xinpcb size mismatch");
663 errx(1, "protocol %d not supported", proto);
665 so = &xip->xi_socket;
666 if ((xip->inp_vflag & vflag) == 0)
668 if (xip->inp_vflag & INP_IPV4) {
669 if ((xip->inp_fport == 0 && !opt_l) ||
670 (xip->inp_fport != 0 && !opt_c))
672 #define __IN_IS_ADDR_LOOPBACK(pina) \
673 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
675 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
676 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
678 #undef __IN_IS_ADDR_LOOPBACK
679 } else if (xip->inp_vflag & INP_IPV6) {
680 if ((xip->inp_fport == 0 && !opt_l) ||
681 (xip->inp_fport != 0 && !opt_c))
684 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
685 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
689 warnx("invalid vflag 0x%x", xip->inp_vflag);
692 if ((sock = calloc(1, sizeof(*sock))) == NULL)
694 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
696 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
698 sock->socket = so->xso_so;
700 if (xip->inp_vflag & INP_IPV4) {
701 sock->family = AF_INET;
702 sockaddr(&laddr->address, sock->family,
703 &xip->inp_laddr, xip->inp_lport);
704 sockaddr(&faddr->address, sock->family,
705 &xip->inp_faddr, xip->inp_fport);
706 } else if (xip->inp_vflag & INP_IPV6) {
707 sock->family = AF_INET6;
708 sockaddr(&laddr->address, sock->family,
709 &xip->in6p_laddr, xip->inp_lport);
710 sockaddr(&faddr->address, sock->family,
711 &xip->in6p_faddr, xip->inp_fport);
713 if (proto == IPPROTO_TCP)
714 faddr->encaps_port = xtp->xt_encaps_port;
719 sock->vflag = xip->inp_vflag;
720 if (proto == IPPROTO_TCP) {
721 sock->state = xtp->t_state;
722 memcpy(sock->stack, xtp->xt_stack,
723 TCP_FUNCTION_NAME_LEN_MAX);
724 memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX);
726 sock->protoname = protoname;
727 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
728 sock->next = sockhash[hash];
729 sockhash[hash] = sock;
736 gather_unix(int proto)
738 struct xunpgen *xug, *exug;
741 struct addr *laddr, *faddr;
742 const char *varname, *protoname;
749 varname = "net.local.stream.pcblist";
750 protoname = "stream";
753 varname = "net.local.dgram.pcblist";
757 varname = "net.local.seqpacket.pcblist";
758 protoname = "seqpac";
768 if ((buf = realloc(buf, bufsize)) == NULL)
771 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
773 if (errno != ENOMEM || len != bufsize)
774 err(1, "sysctlbyname()");
777 xug = (struct xunpgen *)buf;
778 exug = (struct xunpgen *)(void *)
779 ((char *)buf + len - sizeof(*exug));
780 if (xug->xug_len != sizeof(*xug) ||
781 exug->xug_len != sizeof(*exug)) {
782 warnx("struct xinpgen size mismatch");
785 } while (xug->xug_gen != exug->xug_gen && retry--);
787 if (xug->xug_gen != exug->xug_gen && opt_v)
788 warnx("warning: data may be inconsistent");
791 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
794 xup = (struct xunpcb *)xug;
795 if (xup->xu_len != sizeof(*xup)) {
796 warnx("struct xunpcb size mismatch");
799 if ((xup->unp_conn == 0 && !opt_l) ||
800 (xup->unp_conn != 0 && !opt_c))
802 if ((sock = calloc(1, sizeof(*sock))) == NULL)
804 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
806 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
808 sock->socket = xup->xu_socket.xso_so;
809 sock->pcb = xup->xu_unpp;
811 sock->family = AF_UNIX;
812 sock->protoname = protoname;
813 if (xup->xu_addr.sun_family == AF_UNIX)
815 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
816 else if (xup->unp_conn != 0)
817 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
822 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
823 sock->next = sockhash[hash];
824 sockhash[hash] = sock;
835 olen = len = sizeof(*xfiles);
836 if ((xfiles = malloc(len)) == NULL)
838 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
839 if (errno != ENOMEM || len != olen)
840 err(1, "sysctlbyname()");
842 if ((xfiles = realloc(xfiles, len)) == NULL)
845 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
846 errx(1, "struct xfile size mismatch");
847 nxfiles = len / sizeof(*xfiles);
851 printaddr(struct sockaddr_storage *ss)
853 struct sockaddr_un *sun;
854 char addrstr[NI_MAXHOST] = { '\0', '\0' };
855 int error, off, port = 0;
857 switch (ss->ss_family) {
859 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
861 port = ntohs(sstosin(ss)->sin_port);
864 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
866 port = ntohs(sstosin6(ss)->sin6_port);
870 off = (int)((char *)&sun->sun_path - (char *)sun);
871 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
873 if (addrstr[0] == '\0') {
874 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
875 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
877 errx(1, "getnameinfo()");
880 return xprintf("%s:*", addrstr);
882 return xprintf("%s:%d", addrstr, port);
886 getprocname(pid_t pid)
888 static struct kinfo_proc proc;
894 mib[2] = KERN_PROC_PID;
897 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
898 /* Do not warn if the process exits before we get its name. */
903 return (proc.ki_comm);
907 getprocjid(pid_t pid)
909 static struct kinfo_proc proc;
915 mib[2] = KERN_PROC_PID;
918 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
919 /* Do not warn if the process exits before we get its jid. */
924 return (proc.ki_jid);
928 check_ports(struct sock *s)
935 if ((s->family != AF_INET) && (s->family != AF_INET6))
937 for (addr = s->laddr; addr != NULL; addr = addr->next) {
938 if (s->family == AF_INET)
939 port = ntohs(sstosin(&addr->address)->sin_port);
941 port = ntohs(sstosin6(&addr->address)->sin6_port);
945 for (addr = s->faddr; addr != NULL; addr = addr->next) {
946 if (s->family == AF_INET)
947 port = ntohs(sstosin(&addr->address)->sin_port);
949 port = ntohs(sstosin6(&addr->address)->sin6_port);
957 sctp_conn_state(int state)
969 case SCTP_COOKIE_WAIT:
970 return "COOKIE_WAIT";
972 case SCTP_COOKIE_ECHOED:
973 return "COOKIE_ECHOED";
975 case SCTP_ESTABLISHED:
976 return "ESTABLISHED";
978 case SCTP_SHUTDOWN_SENT:
979 return "SHUTDOWN_SENT";
981 case SCTP_SHUTDOWN_RECEIVED:
982 return "SHUTDOWN_RECEIVED";
984 case SCTP_SHUTDOWN_ACK_SENT:
985 return "SHUTDOWN_ACK_SENT";
987 case SCTP_SHUTDOWN_PENDING:
988 return "SHUTDOWN_PENDING";
997 sctp_path_state(int state)
1000 case SCTP_UNCONFIRMED:
1001 return "UNCONFIRMED";
1016 displaysock(struct sock *s, int pos)
1019 int hash, first, offset;
1020 struct addr *laddr, *faddr;
1024 pos += xprintf(" ");
1025 pos += xprintf("%s", s->protoname);
1026 if (s->vflag & INP_IPV4)
1027 pos += xprintf("4");
1028 if (s->vflag & INP_IPV6)
1029 pos += xprintf("6");
1030 if (s->vflag & (INP_IPV4 | INP_IPV6))
1031 pos += xprintf(" ");
1035 while (laddr != NULL || faddr != NULL) {
1037 while (pos < offset)
1038 pos += xprintf(" ");
1039 switch (s->family) {
1042 if (laddr != NULL) {
1043 pos += printaddr(&laddr->address);
1044 if (s->family == AF_INET6 && pos >= 58)
1045 pos += xprintf(" ");
1047 offset += opt_w ? 46 : 22;
1048 while (pos < offset)
1049 pos += xprintf(" ");
1051 pos += printaddr(&faddr->address);
1052 offset += opt_w ? 46 : 22;
1055 if ((laddr == NULL) || (faddr == NULL))
1056 errx(1, "laddr = %p or faddr = %p is NULL",
1057 (void *)laddr, (void *)faddr);
1059 if (laddr->address.ss_len > 0) {
1060 pos += printaddr(&laddr->address);
1064 p = *(kvaddr_t*)&(faddr->address);
1066 pos += xprintf("(not connected)");
1067 offset += opt_w ? 92 : 44;
1070 pos += xprintf("-> ");
1071 for (hash = 0; hash < HASHSIZE; ++hash) {
1072 for (s_tmp = sockhash[hash];
1074 s_tmp = s_tmp->next)
1075 if (s_tmp->pcb == p)
1080 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1081 s_tmp->laddr->address.ss_len == 0)
1082 pos += xprintf("??");
1084 pos += printaddr(&s_tmp->laddr->address);
1085 offset += opt_w ? 92 : 44;
1091 if (faddr != NULL &&
1092 ((s->proto == IPPROTO_SCTP &&
1093 s->state != SCTP_CLOSED &&
1094 s->state != SCTP_BOUND &&
1095 s->state != SCTP_LISTEN) ||
1096 (s->proto == IPPROTO_TCP &&
1097 s->state != TCPS_CLOSED &&
1098 s->state != TCPS_LISTEN))) {
1099 while (pos < offset)
1100 pos += xprintf(" ");
1101 pos += xprintf("%u",
1102 ntohs(faddr->encaps_port));
1107 if (faddr != NULL &&
1108 s->proto == IPPROTO_SCTP &&
1109 s->state != SCTP_CLOSED &&
1110 s->state != SCTP_BOUND &&
1111 s->state != SCTP_LISTEN) {
1112 while (pos < offset)
1113 pos += xprintf(" ");
1114 pos += xprintf("%s",
1115 sctp_path_state(faddr->state));
1121 if (s->proto == IPPROTO_SCTP ||
1122 s->proto == IPPROTO_TCP) {
1123 while (pos < offset)
1124 pos += xprintf(" ");
1127 pos += xprintf("%s",
1128 sctp_conn_state(s->state));
1131 if (s->state >= 0 &&
1132 s->state < TCP_NSTATES)
1133 pos += xprintf("%s",
1134 tcpstates[s->state]);
1136 pos += xprintf("?");
1143 if (s->proto == IPPROTO_TCP) {
1144 while (pos < offset)
1145 pos += xprintf(" ");
1146 pos += xprintf("%.*s",
1147 TCP_FUNCTION_NAME_LEN_MAX,
1150 offset += TCP_FUNCTION_NAME_LEN_MAX + 1;
1153 if (s->proto == IPPROTO_TCP) {
1154 while (pos < offset)
1155 pos += xprintf(" ");
1156 xprintf("%.*s", TCP_CA_NAME_MAX, s->cc);
1158 offset += TCP_CA_NAME_MAX + 1;
1162 laddr = laddr->next;
1164 faddr = faddr->next;
1165 if ((laddr != NULL) || (faddr != NULL)) {
1183 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1184 "USER", "COMMAND", "PID", "FD", "PROTO",
1185 opt_w ? 45 : 21, "LOCAL ADDRESS",
1186 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1188 printf(" %-6s", "ENCAPS");
1190 printf(" %-12s", "PATH STATE");
1191 printf(" %-12s", "CONN STATE");
1194 printf(" %-*.*s", TCP_FUNCTION_NAME_LEN_MAX,
1195 TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1197 printf(" %-.*s", TCP_CA_NAME_MAX, "CC");
1201 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1202 if (xf->xf_data == 0)
1204 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1206 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1207 for (s = sockhash[hash]; s != NULL; s = s->next) {
1208 if (s->socket != xf->xf_data)
1210 if (!check_ports(s))
1214 if (opt_n || (pwd = getpwuid(xf->xf_uid)) == NULL)
1215 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1217 pos += xprintf("%s ", pwd->pw_name);
1219 pos += xprintf(" ");
1220 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1222 pos += xprintf(" ");
1223 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1225 pos += xprintf(" ");
1226 pos += xprintf("%d ", xf->xf_fd);
1227 displaysock(s, pos);
1232 for (hash = 0; hash < HASHSIZE; hash++) {
1233 for (s = sockhash[hash]; s != NULL; s = s->next) {
1236 if (!check_ports(s))
1239 pos += xprintf("%-8s %-10s %-5s %-2s ",
1240 "?", "?", "?", "?");
1241 displaysock(s, pos);
1247 set_default_protos(void)
1249 struct protoent *prot;
1253 init_protos(default_numprotos);
1255 for (pindex = 0; pindex < default_numprotos; pindex++) {
1256 pname = default_protos[pindex];
1257 prot = getprotobyname(pname);
1259 err(1, "getprotobyname: %s", pname);
1260 protos[pindex] = prot->p_proto;
1267 * Return the vnet property of the jail, or -1 on error.
1270 jail_getvnet(int jid)
1272 struct iovec jiov[6];
1276 jiov[0].iov_base = __DECONST(char *, "jid");
1277 jiov[0].iov_len = sizeof("jid");
1278 jiov[1].iov_base = &jid;
1279 jiov[1].iov_len = sizeof(jid);
1280 jiov[2].iov_base = __DECONST(char *, "vnet");
1281 jiov[2].iov_len = sizeof("vnet");
1282 jiov[3].iov_base = &vnet;
1283 jiov[3].iov_len = sizeof(vnet);
1284 jiov[4].iov_base = __DECONST(char *, "errmsg");
1285 jiov[4].iov_len = sizeof("errmsg");
1286 jiov[5].iov_base = jail_errmsg;
1287 jiov[5].iov_len = JAIL_ERRMSGLEN;
1288 jail_errmsg[0] = '\0';
1289 if (jail_get(jiov, nitems(jiov), 0) < 0) {
1290 if (!jail_errmsg[0])
1291 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1292 "jail_get: %s", strerror(errno));
1302 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1307 main(int argc, char *argv[])
1309 int protos_defined = -1;
1313 while ((o = getopt(argc, argv, "46Ccj:Llnp:P:qSsUuvw")) != -1)
1328 opt_j = jail_getid(optarg);
1330 errx(1, "%s", jail_errmsg);
1342 parse_ports(optarg);
1345 protos_defined = parse_protos(optarg);
1379 switch (jail_getvnet(opt_j)) {
1381 errx(2, "%s", jail_errmsg);
1383 if (jail_attach(opt_j) < 0)
1384 err(3, "jail_attach()");
1385 /* Set back to -1 for normal output in vnet jail. */
1393 if ((!opt_4 && !opt_6) && protos_defined != -1)
1395 if (!opt_4 && !opt_6 && !opt_u)
1396 opt_4 = opt_6 = opt_u = 1;
1397 if ((opt_4 || opt_6) && protos_defined == -1)
1398 protos_defined = set_default_protos();
1399 if (!opt_c && !opt_l)
1402 if (opt_4 || opt_6) {
1403 for (i = 0; i < protos_defined; i++)
1404 if (protos[i] == IPPROTO_SCTP)
1407 gather_inet(protos[i]);
1410 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1411 gather_unix(SOCK_STREAM);
1412 gather_unix(SOCK_DGRAM);
1413 gather_unix(SOCK_SEQPACKET);