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 connected sockets */
78 static int opt_j; /* Show specified jail */
79 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
80 static int opt_l; /* Show listening sockets */
81 static int opt_q; /* Don't show header */
82 static int opt_S; /* Show protocol stack if applicable */
83 static int opt_s; /* Show protocol state if applicable */
84 static int opt_U; /* Show remote UDP encapsulation port number */
85 static int opt_u; /* Show Unix domain sockets */
86 static int opt_v; /* Verbose mode */
87 static int opt_w; /* Wide print area for addresses */
90 * Default protocols to use if no -P was defined.
92 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
93 static size_t default_numprotos = nitems(default_protos);
95 static int *protos; /* protocols to use */
96 static size_t numprotos; /* allocated size of protos[] */
100 #define INT_BIT (sizeof(int)*CHAR_BIT)
101 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
102 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
105 struct sockaddr_storage address;
106 unsigned int encaps_port;
119 const char *protoname;
120 char stack[TCP_FUNCTION_NAME_LEN_MAX];
126 #define HASHSIZE 1009
127 static struct sock *sockhash[HASHSIZE];
129 static struct xfile *xfiles;
133 xprintf(const char *fmt, ...)
139 len = vprintf(fmt, ap);
147 get_proto_type(const char *proto)
149 struct protoent *pent;
151 if (strlen(proto) == 0)
153 pent = getprotobyname(proto);
155 warn("getprotobyname");
158 return (pent->p_proto);
169 /* Find the maximum number of possible protocols. */
170 while (getprotoent() != NULL)
175 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
177 numprotos = proto_count;
181 parse_protos(char *protospec)
184 int proto_type, proto_index;
186 if (protospec == NULL)
191 while ((prot = strsep(&protospec, ",")) != NULL) {
192 if (strlen(prot) == 0)
194 proto_type = get_proto_type(prot);
195 if (proto_type != -1)
196 protos[proto_index++] = proto_type;
198 numprotos = proto_index;
199 return (proto_index);
203 parse_ports(const char *portspec)
209 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
214 errx(1, "syntax error in port range");
215 for (q = p; *q != '\0' && isdigit(*q); ++q)
217 for (port = 0; p < q; ++p)
218 port = port * 10 + digittoint(*p);
219 if (port < 0 || port > 65535)
220 errx(1, "invalid port number");
233 for (q = p; *q != '\0' && isdigit(*q); ++q)
235 for (end = 0; p < q; ++p)
236 end = end * 10 + digittoint(*p);
237 if (end < port || end > 65535)
238 errx(1, "invalid port number");
247 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
249 struct sockaddr_in *sin4;
250 struct sockaddr_in6 *sin6;
252 bzero(ss, sizeof(*ss));
256 sin4->sin_len = sizeof(*sin4);
257 sin4->sin_family = af;
258 sin4->sin_port = port;
259 sin4->sin_addr = *(struct in_addr *)addr;
263 sin6->sin6_len = sizeof(*sin6);
264 sin6->sin6_family = af;
265 sin6->sin6_port = port;
266 sin6->sin6_addr = *(struct in6_addr *)addr;
267 #define s6_addr16 __u6_addr.__u6_addr16
268 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
269 sin6->sin6_scope_id =
270 ntohs(sin6->sin6_addr.s6_addr16[1]);
271 sin6->sin6_addr.s6_addr16[1] = 0;
280 free_socket(struct sock *sock)
282 struct addr *cur, *next;
285 while (cur != NULL) {
291 while (cur != NULL) {
303 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
304 struct xsctp_inpcb *xinpcb;
305 struct xsctp_tcb *xstcb;
306 struct xsctp_raddr *xraddr;
307 struct xsctp_laddr *xladdr;
312 int no_stcb, local_all_loopback, foreign_all_loopback;
320 varname = "net.inet.sctp.assoclist";
321 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
323 err(1, "sysctlbyname()");
326 if ((buf = (char *)malloc(len)) == NULL) {
330 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
331 err(1, "sysctlbyname()");
335 xinpcb = (struct xsctp_inpcb *)(void *)buf;
336 offset = sizeof(struct xsctp_inpcb);
337 while ((offset < len) && (xinpcb->last == 0)) {
338 if ((sock = calloc(1, sizeof *sock)) == NULL)
340 sock->socket = xinpcb->socket;
341 sock->proto = IPPROTO_SCTP;
342 sock->protoname = "sctp";
343 if (xinpcb->maxqlen == 0)
344 sock->state = SCTP_CLOSED;
346 sock->state = SCTP_LISTEN;
347 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
348 sock->family = AF_INET6;
350 * Currently there is no way to distinguish between
351 * IPv6 only sockets or dual family sockets.
352 * So mark it as dual socket.
354 sock->vflag = INP_IPV6 | INP_IPV4;
356 sock->family = AF_INET;
357 sock->vflag = INP_IPV4;
360 local_all_loopback = 1;
361 while (offset < len) {
362 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
363 offset += sizeof(struct xsctp_laddr);
364 if (xladdr->last == 1)
366 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
368 switch (xladdr->address.sa.sa_family) {
370 #define __IN_IS_ADDR_LOOPBACK(pina) \
371 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
372 if (!__IN_IS_ADDR_LOOPBACK(
373 &xladdr->address.sin.sin_addr))
374 local_all_loopback = 0;
375 #undef __IN_IS_ADDR_LOOPBACK
376 sockaddr(&laddr->address, AF_INET,
377 &xladdr->address.sin.sin_addr,
378 htons(xinpcb->local_port));
381 if (!IN6_IS_ADDR_LOOPBACK(
382 &xladdr->address.sin6.sin6_addr))
383 local_all_loopback = 0;
384 sockaddr(&laddr->address, AF_INET6,
385 &xladdr->address.sin6.sin6_addr,
386 htons(xinpcb->local_port));
389 errx(1, "address family %d not supported",
390 xladdr->address.sa.sa_family);
393 if (prev_laddr == NULL)
396 prev_laddr->next = laddr;
399 if (sock->laddr == NULL) {
401 calloc(1, sizeof(struct addr))) == NULL)
403 sock->laddr->address.ss_family = sock->family;
404 if (sock->family == AF_INET)
405 sock->laddr->address.ss_len =
406 sizeof(struct sockaddr_in);
408 sock->laddr->address.ss_len =
409 sizeof(struct sockaddr_in6);
410 local_all_loopback = 0;
412 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
414 sock->faddr->address.ss_family = sock->family;
415 if (sock->family == AF_INET)
416 sock->faddr->address.ss_len =
417 sizeof(struct sockaddr_in);
419 sock->faddr->address.ss_len =
420 sizeof(struct sockaddr_in6);
422 while (offset < len) {
423 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
424 offset += sizeof(struct xsctp_tcb);
426 if (opt_l && (sock->vflag & vflag) &&
427 (!opt_L || !local_all_loopback) &&
428 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
429 (xstcb->last == 1))) {
430 hash = (int)((uintptr_t)sock->socket %
432 sock->next = sockhash[hash];
433 sockhash[hash] = sock;
438 if (xstcb->last == 1)
442 if ((sock = calloc(1, sizeof *sock)) == NULL)
444 sock->socket = xinpcb->socket;
445 sock->proto = IPPROTO_SCTP;
446 sock->protoname = "sctp";
447 sock->state = (int)xstcb->state;
448 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
449 sock->family = AF_INET6;
451 * Currently there is no way to distinguish
452 * between IPv6 only sockets or dual family
453 * sockets. So mark it as dual socket.
455 sock->vflag = INP_IPV6 | INP_IPV4;
457 sock->family = AF_INET;
458 sock->vflag = INP_IPV4;
462 local_all_loopback = 1;
463 while (offset < len) {
464 xladdr = (struct xsctp_laddr *)(void *)(buf +
466 offset += sizeof(struct xsctp_laddr);
467 if (xladdr->last == 1)
471 laddr = calloc(1, sizeof(struct addr));
474 switch (xladdr->address.sa.sa_family) {
476 #define __IN_IS_ADDR_LOOPBACK(pina) \
477 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
478 if (!__IN_IS_ADDR_LOOPBACK(
479 &xladdr->address.sin.sin_addr))
480 local_all_loopback = 0;
481 #undef __IN_IS_ADDR_LOOPBACK
482 sockaddr(&laddr->address, AF_INET,
483 &xladdr->address.sin.sin_addr,
484 htons(xstcb->local_port));
487 if (!IN6_IS_ADDR_LOOPBACK(
488 &xladdr->address.sin6.sin6_addr))
489 local_all_loopback = 0;
490 sockaddr(&laddr->address, AF_INET6,
491 &xladdr->address.sin6.sin6_addr,
492 htons(xstcb->local_port));
496 "address family %d not supported",
497 xladdr->address.sa.sa_family);
500 if (prev_laddr == NULL)
503 prev_laddr->next = laddr;
507 foreign_all_loopback = 1;
508 while (offset < len) {
509 xraddr = (struct xsctp_raddr *)(void *)(buf +
511 offset += sizeof(struct xsctp_raddr);
512 if (xraddr->last == 1)
516 faddr = calloc(1, sizeof(struct addr));
519 switch (xraddr->address.sa.sa_family) {
521 #define __IN_IS_ADDR_LOOPBACK(pina) \
522 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
523 if (!__IN_IS_ADDR_LOOPBACK(
524 &xraddr->address.sin.sin_addr))
525 foreign_all_loopback = 0;
526 #undef __IN_IS_ADDR_LOOPBACK
527 sockaddr(&faddr->address, AF_INET,
528 &xraddr->address.sin.sin_addr,
529 htons(xstcb->remote_port));
532 if (!IN6_IS_ADDR_LOOPBACK(
533 &xraddr->address.sin6.sin6_addr))
534 foreign_all_loopback = 0;
535 sockaddr(&faddr->address, AF_INET6,
536 &xraddr->address.sin6.sin6_addr,
537 htons(xstcb->remote_port));
541 "address family %d not supported",
542 xraddr->address.sa.sa_family);
544 faddr->encaps_port = xraddr->encaps_port;
545 faddr->state = xraddr->state;
547 if (prev_faddr == NULL)
550 prev_faddr->next = faddr;
554 if ((sock->vflag & vflag) &&
556 !(local_all_loopback ||
557 foreign_all_loopback))) {
558 hash = (int)((uintptr_t)sock->socket %
560 sock->next = sockhash[hash];
561 sockhash[hash] = sock;
567 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
568 offset += sizeof(struct xsctp_inpcb);
574 gather_inet(int proto)
576 struct xinpgen *xig, *exig;
578 struct xtcpcb *xtp = NULL;
581 struct addr *laddr, *faddr;
582 const char *varname, *protoname;
585 int hash, retry, vflag;
595 varname = "net.inet.tcp.pcblist";
599 varname = "net.inet.udp.pcblist";
603 varname = "net.inet.divert.pcblist";
607 errx(1, "protocol %d not supported", proto);
615 if ((buf = realloc(buf, bufsize)) == NULL)
618 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
622 if (errno != ENOMEM || len != bufsize)
623 err(1, "sysctlbyname()");
626 xig = (struct xinpgen *)buf;
627 exig = (struct xinpgen *)(void *)
628 ((char *)buf + len - sizeof *exig);
629 if (xig->xig_len != sizeof *xig ||
630 exig->xig_len != sizeof *exig)
631 errx(1, "struct xinpgen size mismatch");
632 } while (xig->xig_gen != exig->xig_gen && retry--);
634 if (xig->xig_gen != exig->xig_gen && opt_v)
635 warnx("warning: data may be inconsistent");
638 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
643 xtp = (struct xtcpcb *)xig;
645 if (xtp->xt_len != sizeof(*xtp)) {
646 warnx("struct xtcpcb size mismatch");
649 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
653 xip = (struct xinpcb *)xig;
654 if (xip->xi_len != sizeof(*xip)) {
655 warnx("struct xinpcb size mismatch");
660 errx(1, "protocol %d not supported", proto);
662 so = &xip->xi_socket;
663 if ((xip->inp_vflag & vflag) == 0)
665 if (xip->inp_vflag & INP_IPV4) {
666 if ((xip->inp_fport == 0 && !opt_l) ||
667 (xip->inp_fport != 0 && !opt_c))
669 #define __IN_IS_ADDR_LOOPBACK(pina) \
670 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
672 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
673 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
675 #undef __IN_IS_ADDR_LOOPBACK
676 } else if (xip->inp_vflag & INP_IPV6) {
677 if ((xip->inp_fport == 0 && !opt_l) ||
678 (xip->inp_fport != 0 && !opt_c))
681 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
682 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
686 warnx("invalid vflag 0x%x", xip->inp_vflag);
689 if ((sock = calloc(1, sizeof(*sock))) == NULL)
691 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
693 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
695 sock->socket = so->xso_so;
697 if (xip->inp_vflag & INP_IPV4) {
698 sock->family = AF_INET;
699 sockaddr(&laddr->address, sock->family,
700 &xip->inp_laddr, xip->inp_lport);
701 sockaddr(&faddr->address, sock->family,
702 &xip->inp_faddr, xip->inp_fport);
703 } else if (xip->inp_vflag & INP_IPV6) {
704 sock->family = AF_INET6;
705 sockaddr(&laddr->address, sock->family,
706 &xip->in6p_laddr, xip->inp_lport);
707 sockaddr(&faddr->address, sock->family,
708 &xip->in6p_faddr, xip->inp_fport);
714 sock->vflag = xip->inp_vflag;
715 if (proto == IPPROTO_TCP) {
716 sock->state = xtp->t_state;
717 memcpy(sock->stack, xtp->xt_stack,
718 TCP_FUNCTION_NAME_LEN_MAX);
720 sock->protoname = protoname;
721 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
722 sock->next = sockhash[hash];
723 sockhash[hash] = sock;
730 gather_unix(int proto)
732 struct xunpgen *xug, *exug;
735 struct addr *laddr, *faddr;
736 const char *varname, *protoname;
743 varname = "net.local.stream.pcblist";
744 protoname = "stream";
747 varname = "net.local.dgram.pcblist";
751 varname = "net.local.seqpacket.pcblist";
752 protoname = "seqpac";
762 if ((buf = realloc(buf, bufsize)) == NULL)
765 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
767 if (errno != ENOMEM || len != bufsize)
768 err(1, "sysctlbyname()");
771 xug = (struct xunpgen *)buf;
772 exug = (struct xunpgen *)(void *)
773 ((char *)buf + len - sizeof(*exug));
774 if (xug->xug_len != sizeof(*xug) ||
775 exug->xug_len != sizeof(*exug)) {
776 warnx("struct xinpgen size mismatch");
779 } while (xug->xug_gen != exug->xug_gen && retry--);
781 if (xug->xug_gen != exug->xug_gen && opt_v)
782 warnx("warning: data may be inconsistent");
785 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
788 xup = (struct xunpcb *)xug;
789 if (xup->xu_len != sizeof(*xup)) {
790 warnx("struct xunpcb size mismatch");
793 if ((xup->unp_conn == 0 && !opt_l) ||
794 (xup->unp_conn != 0 && !opt_c))
796 if ((sock = calloc(1, sizeof(*sock))) == NULL)
798 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
800 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
802 sock->socket = xup->xu_socket.xso_so;
803 sock->pcb = xup->xu_unpp;
805 sock->family = AF_UNIX;
806 sock->protoname = protoname;
807 if (xup->xu_addr.sun_family == AF_UNIX)
809 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
810 else if (xup->unp_conn != 0)
811 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
816 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
817 sock->next = sockhash[hash];
818 sockhash[hash] = sock;
829 olen = len = sizeof(*xfiles);
830 if ((xfiles = malloc(len)) == NULL)
832 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
833 if (errno != ENOMEM || len != olen)
834 err(1, "sysctlbyname()");
836 if ((xfiles = realloc(xfiles, len)) == NULL)
839 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
840 errx(1, "struct xfile size mismatch");
841 nxfiles = len / sizeof(*xfiles);
845 printaddr(struct sockaddr_storage *ss)
847 struct sockaddr_un *sun;
848 char addrstr[NI_MAXHOST] = { '\0', '\0' };
849 int error, off, port = 0;
851 switch (ss->ss_family) {
853 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
855 port = ntohs(sstosin(ss)->sin_port);
858 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
860 port = ntohs(sstosin6(ss)->sin6_port);
864 off = (int)((char *)&sun->sun_path - (char *)sun);
865 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
867 if (addrstr[0] == '\0') {
868 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
869 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
871 errx(1, "getnameinfo()");
874 return xprintf("%s:*", addrstr);
876 return xprintf("%s:%d", addrstr, port);
880 getprocname(pid_t pid)
882 static struct kinfo_proc proc;
888 mib[2] = KERN_PROC_PID;
891 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
892 /* Do not warn if the process exits before we get its name. */
897 return (proc.ki_comm);
901 getprocjid(pid_t pid)
903 static struct kinfo_proc proc;
909 mib[2] = KERN_PROC_PID;
912 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
913 /* Do not warn if the process exits before we get its jid. */
918 return (proc.ki_jid);
922 check_ports(struct sock *s)
929 if ((s->family != AF_INET) && (s->family != AF_INET6))
931 for (addr = s->laddr; addr != NULL; addr = addr->next) {
932 if (s->family == AF_INET)
933 port = ntohs(sstosin(&addr->address)->sin_port);
935 port = ntohs(sstosin6(&addr->address)->sin6_port);
939 for (addr = s->faddr; addr != NULL; addr = addr->next) {
940 if (s->family == AF_INET)
941 port = ntohs(sstosin(&addr->address)->sin_port);
943 port = ntohs(sstosin6(&addr->address)->sin6_port);
951 sctp_conn_state(int state)
963 case SCTP_COOKIE_WAIT:
964 return "COOKIE_WAIT";
966 case SCTP_COOKIE_ECHOED:
967 return "COOKIE_ECHOED";
969 case SCTP_ESTABLISHED:
970 return "ESTABLISHED";
972 case SCTP_SHUTDOWN_SENT:
973 return "SHUTDOWN_SENT";
975 case SCTP_SHUTDOWN_RECEIVED:
976 return "SHUTDOWN_RECEIVED";
978 case SCTP_SHUTDOWN_ACK_SENT:
979 return "SHUTDOWN_ACK_SENT";
981 case SCTP_SHUTDOWN_PENDING:
982 return "SHUTDOWN_PENDING";
991 sctp_path_state(int state)
994 case SCTP_UNCONFIRMED:
995 return "UNCONFIRMED";
1010 displaysock(struct sock *s, int pos)
1013 int hash, first, offset;
1014 struct addr *laddr, *faddr;
1018 pos += xprintf(" ");
1019 pos += xprintf("%s", s->protoname);
1020 if (s->vflag & INP_IPV4)
1021 pos += xprintf("4");
1022 if (s->vflag & INP_IPV6)
1023 pos += xprintf("6");
1024 if (s->vflag & (INP_IPV4 | INP_IPV6))
1025 pos += xprintf(" ");
1029 while (laddr != NULL || faddr != NULL) {
1031 while (pos < offset)
1032 pos += xprintf(" ");
1033 switch (s->family) {
1036 if (laddr != NULL) {
1037 pos += printaddr(&laddr->address);
1038 if (s->family == AF_INET6 && pos >= 58)
1039 pos += xprintf(" ");
1041 offset += opt_w ? 46 : 22;
1042 while (pos < offset)
1043 pos += xprintf(" ");
1045 pos += printaddr(&faddr->address);
1046 offset += opt_w ? 46 : 22;
1049 if ((laddr == NULL) || (faddr == NULL))
1050 errx(1, "laddr = %p or faddr = %p is NULL",
1051 (void *)laddr, (void *)faddr);
1053 if (laddr->address.ss_len > 0) {
1054 pos += printaddr(&laddr->address);
1058 p = *(kvaddr_t*)&(faddr->address);
1060 pos += xprintf("(not connected)");
1061 offset += opt_w ? 92 : 44;
1064 pos += xprintf("-> ");
1065 for (hash = 0; hash < HASHSIZE; ++hash) {
1066 for (s_tmp = sockhash[hash];
1068 s_tmp = s_tmp->next)
1069 if (s_tmp->pcb == p)
1074 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1075 s_tmp->laddr->address.ss_len == 0)
1076 pos += xprintf("??");
1078 pos += printaddr(&s_tmp->laddr->address);
1079 offset += opt_w ? 92 : 44;
1085 if (faddr != NULL &&
1086 s->proto == IPPROTO_SCTP &&
1087 s->state != SCTP_CLOSED &&
1088 s->state != SCTP_BOUND &&
1089 s->state != SCTP_LISTEN) {
1090 while (pos < offset)
1091 pos += xprintf(" ");
1092 pos += xprintf("%u",
1093 ntohs(faddr->encaps_port));
1098 if (faddr != NULL &&
1099 s->proto == IPPROTO_SCTP &&
1100 s->state != SCTP_CLOSED &&
1101 s->state != SCTP_BOUND &&
1102 s->state != SCTP_LISTEN) {
1103 while (pos < offset)
1104 pos += xprintf(" ");
1105 pos += xprintf("%s",
1106 sctp_path_state(faddr->state));
1112 if (s->proto == IPPROTO_SCTP ||
1113 s->proto == IPPROTO_TCP) {
1114 while (pos < offset)
1115 pos += xprintf(" ");
1118 pos += xprintf("%s",
1119 sctp_conn_state(s->state));
1122 if (s->state >= 0 &&
1123 s->state < TCP_NSTATES)
1124 pos += xprintf("%s",
1125 tcpstates[s->state]);
1127 pos += xprintf("?");
1133 if (opt_S && s->proto == IPPROTO_TCP) {
1134 while (pos < offset)
1135 pos += xprintf(" ");
1136 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1141 laddr = laddr->next;
1143 faddr = faddr->next;
1144 if ((laddr != NULL) || (faddr != NULL)) {
1162 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1163 "USER", "COMMAND", "PID", "FD", "PROTO",
1164 opt_w ? 45 : 21, "LOCAL ADDRESS",
1165 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1167 printf(" %-6s", "ENCAPS");
1169 printf(" %-12s", "PATH STATE");
1170 printf(" %-12s", "CONN STATE");
1173 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1177 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1178 if (xf->xf_data == 0)
1180 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1182 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1183 for (s = sockhash[hash]; s != NULL; s = s->next) {
1184 if (s->socket != xf->xf_data)
1186 if (!check_ports(s))
1190 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1191 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1193 pos += xprintf("%s ", pwd->pw_name);
1195 pos += xprintf(" ");
1196 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1198 pos += xprintf(" ");
1199 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1201 pos += xprintf(" ");
1202 pos += xprintf("%d ", xf->xf_fd);
1203 displaysock(s, pos);
1208 for (hash = 0; hash < HASHSIZE; hash++) {
1209 for (s = sockhash[hash]; s != NULL; s = s->next) {
1212 if (!check_ports(s))
1215 pos += xprintf("%-8s %-10s %-5s %-2s ",
1216 "?", "?", "?", "?");
1217 displaysock(s, pos);
1223 set_default_protos(void)
1225 struct protoent *prot;
1229 init_protos(default_numprotos);
1231 for (pindex = 0; pindex < default_numprotos; pindex++) {
1232 pname = default_protos[pindex];
1233 prot = getprotobyname(pname);
1235 err(1, "getprotobyname: %s", pname);
1236 protos[pindex] = prot->p_proto;
1243 * Return the vnet property of the jail, or -1 on error.
1246 jail_getvnet(int jid)
1248 struct iovec jiov[6];
1252 jiov[0].iov_base = __DECONST(char *, "jid");
1253 jiov[0].iov_len = sizeof("jid");
1254 jiov[1].iov_base = &jid;
1255 jiov[1].iov_len = sizeof(jid);
1256 jiov[2].iov_base = __DECONST(char *, "vnet");
1257 jiov[2].iov_len = sizeof("vnet");
1258 jiov[3].iov_base = &vnet;
1259 jiov[3].iov_len = sizeof(vnet);
1260 jiov[4].iov_base = __DECONST(char *, "errmsg");
1261 jiov[4].iov_len = sizeof("errmsg");
1262 jiov[5].iov_base = jail_errmsg;
1263 jiov[5].iov_len = JAIL_ERRMSGLEN;
1264 jail_errmsg[0] = '\0';
1265 if (jail_get(jiov, nitems(jiov), 0) < 0) {
1266 if (!jail_errmsg[0])
1267 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1268 "jail_get: %s", strerror(errno));
1278 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1283 main(int argc, char *argv[])
1285 int protos_defined = -1;
1289 while ((o = getopt(argc, argv, "46cj:Llp:P:qSsUuvw")) != -1)
1301 opt_j = jail_getid(optarg);
1303 errx(1, "%s", jail_errmsg);
1312 parse_ports(optarg);
1315 protos_defined = parse_protos(optarg);
1349 switch (jail_getvnet(opt_j)) {
1351 errx(2, "%s", jail_errmsg);
1353 if (jail_attach(opt_j) < 0)
1354 errx(3, "%s", jail_errmsg);
1355 /* Set back to -1 for normal output in vnet jail. */
1363 if ((!opt_4 && !opt_6) && protos_defined != -1)
1365 if (!opt_4 && !opt_6 && !opt_u)
1366 opt_4 = opt_6 = opt_u = 1;
1367 if ((opt_4 || opt_6) && protos_defined == -1)
1368 protos_defined = set_default_protos();
1369 if (!opt_c && !opt_l)
1372 if (opt_4 || opt_6) {
1373 for (i = 0; i < protos_defined; i++)
1374 if (protos[i] == IPPROTO_SCTP)
1377 gather_inet(protos[i]);
1380 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1381 gather_unix(SOCK_STREAM);
1382 gather_unix(SOCK_DGRAM);
1383 gather_unix(SOCK_SEQPACKET);