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>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sysctl.h>
43 #include <sys/unpcb.h>
45 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/sctp.h>
50 #include <netinet/tcp.h>
51 #define TCPSTATES /* load state names */
52 #include <netinet/tcp_fsm.h>
53 #include <netinet/tcp_seq.h>
54 #include <netinet/tcp_var.h>
55 #include <arpa/inet.h>
69 #define sstosin(ss) ((struct sockaddr_in *)(ss))
70 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
71 #define sstosun(ss) ((struct sockaddr_un *)(ss))
72 #define sstosa(ss) ((struct sockaddr *)(ss))
74 static int opt_4; /* Show IPv4 sockets */
75 static int opt_6; /* Show IPv6 sockets */
76 static int opt_c; /* Show connected sockets */
77 static int opt_j; /* Show specified jail */
78 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
79 static int opt_l; /* Show listening sockets */
80 static int opt_q; /* Don't show header */
81 static int opt_S; /* Show protocol stack if applicable */
82 static int opt_s; /* Show protocol state if applicable */
83 static int opt_U; /* Show remote UDP encapsulation port number */
84 static int opt_u; /* Show Unix domain sockets */
85 static int opt_v; /* Verbose mode */
86 static int opt_w; /* Wide print area for addresses */
89 * Default protocols to use if no -P was defined.
91 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
92 static size_t default_numprotos = nitems(default_protos);
94 static int *protos; /* protocols to use */
95 static size_t numprotos; /* allocated size of protos[] */
99 #define INT_BIT (sizeof(int)*CHAR_BIT)
100 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
101 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
104 struct sockaddr_storage address;
105 unsigned int encaps_port;
118 const char *protoname;
119 char stack[TCP_FUNCTION_NAME_LEN_MAX];
125 #define HASHSIZE 1009
126 static struct sock *sockhash[HASHSIZE];
128 static struct xfile *xfiles;
132 xprintf(const char *fmt, ...)
138 len = vprintf(fmt, ap);
146 get_proto_type(const char *proto)
148 struct protoent *pent;
150 if (strlen(proto) == 0)
152 pent = getprotobyname(proto);
154 warn("getprotobyname");
157 return (pent->p_proto);
168 /* Find the maximum number of possible protocols. */
169 while (getprotoent() != NULL)
174 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
176 numprotos = proto_count;
180 parse_protos(char *protospec)
183 int proto_type, proto_index;
185 if (protospec == NULL)
190 while ((prot = strsep(&protospec, ",")) != NULL) {
191 if (strlen(prot) == 0)
193 proto_type = get_proto_type(prot);
194 if (proto_type != -1)
195 protos[proto_index++] = proto_type;
197 numprotos = proto_index;
198 return (proto_index);
202 parse_ports(const char *portspec)
208 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
213 errx(1, "syntax error in port range");
214 for (q = p; *q != '\0' && isdigit(*q); ++q)
216 for (port = 0; p < q; ++p)
217 port = port * 10 + digittoint(*p);
218 if (port < 0 || port > 65535)
219 errx(1, "invalid port number");
232 for (q = p; *q != '\0' && isdigit(*q); ++q)
234 for (end = 0; p < q; ++p)
235 end = end * 10 + digittoint(*p);
236 if (end < port || end > 65535)
237 errx(1, "invalid port number");
246 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
248 struct sockaddr_in *sin4;
249 struct sockaddr_in6 *sin6;
251 bzero(ss, sizeof(*ss));
255 sin4->sin_len = sizeof(*sin4);
256 sin4->sin_family = af;
257 sin4->sin_port = port;
258 sin4->sin_addr = *(struct in_addr *)addr;
262 sin6->sin6_len = sizeof(*sin6);
263 sin6->sin6_family = af;
264 sin6->sin6_port = port;
265 sin6->sin6_addr = *(struct in6_addr *)addr;
266 #define s6_addr16 __u6_addr.__u6_addr16
267 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
268 sin6->sin6_scope_id =
269 ntohs(sin6->sin6_addr.s6_addr16[1]);
270 sin6->sin6_addr.s6_addr16[1] = 0;
279 free_socket(struct sock *sock)
281 struct addr *cur, *next;
284 while (cur != NULL) {
290 while (cur != NULL) {
302 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
303 struct xsctp_inpcb *xinpcb;
304 struct xsctp_tcb *xstcb;
305 struct xsctp_raddr *xraddr;
306 struct xsctp_laddr *xladdr;
311 int no_stcb, local_all_loopback, foreign_all_loopback;
319 varname = "net.inet.sctp.assoclist";
320 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
322 err(1, "sysctlbyname()");
325 if ((buf = (char *)malloc(len)) == NULL) {
329 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
330 err(1, "sysctlbyname()");
334 xinpcb = (struct xsctp_inpcb *)(void *)buf;
335 offset = sizeof(struct xsctp_inpcb);
336 while ((offset < len) && (xinpcb->last == 0)) {
337 if ((sock = calloc(1, sizeof *sock)) == NULL)
339 sock->socket = xinpcb->socket;
340 sock->proto = IPPROTO_SCTP;
341 sock->protoname = "sctp";
342 if (xinpcb->maxqlen == 0)
343 sock->state = SCTP_CLOSED;
345 sock->state = SCTP_LISTEN;
346 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
347 sock->family = AF_INET6;
349 * Currently there is no way to distinguish between
350 * IPv6 only sockets or dual family sockets.
351 * So mark it as dual socket.
353 sock->vflag = INP_IPV6 | INP_IPV4;
355 sock->family = AF_INET;
356 sock->vflag = INP_IPV4;
359 local_all_loopback = 1;
360 while (offset < len) {
361 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
362 offset += sizeof(struct xsctp_laddr);
363 if (xladdr->last == 1)
365 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
367 switch (xladdr->address.sa.sa_family) {
369 #define __IN_IS_ADDR_LOOPBACK(pina) \
370 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
371 if (!__IN_IS_ADDR_LOOPBACK(
372 &xladdr->address.sin.sin_addr))
373 local_all_loopback = 0;
374 #undef __IN_IS_ADDR_LOOPBACK
375 sockaddr(&laddr->address, AF_INET,
376 &xladdr->address.sin.sin_addr,
377 htons(xinpcb->local_port));
380 if (!IN6_IS_ADDR_LOOPBACK(
381 &xladdr->address.sin6.sin6_addr))
382 local_all_loopback = 0;
383 sockaddr(&laddr->address, AF_INET6,
384 &xladdr->address.sin6.sin6_addr,
385 htons(xinpcb->local_port));
388 errx(1, "address family %d not supported",
389 xladdr->address.sa.sa_family);
392 if (prev_laddr == NULL)
395 prev_laddr->next = laddr;
398 if (sock->laddr == NULL) {
400 calloc(1, sizeof(struct addr))) == NULL)
402 sock->laddr->address.ss_family = sock->family;
403 if (sock->family == AF_INET)
404 sock->laddr->address.ss_len =
405 sizeof(struct sockaddr_in);
407 sock->laddr->address.ss_len =
408 sizeof(struct sockaddr_in6);
409 local_all_loopback = 0;
411 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
413 sock->faddr->address.ss_family = sock->family;
414 if (sock->family == AF_INET)
415 sock->faddr->address.ss_len =
416 sizeof(struct sockaddr_in);
418 sock->faddr->address.ss_len =
419 sizeof(struct sockaddr_in6);
421 while (offset < len) {
422 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
423 offset += sizeof(struct xsctp_tcb);
425 if (opt_l && (sock->vflag & vflag) &&
426 (!opt_L || !local_all_loopback) &&
427 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
428 (xstcb->last == 1))) {
429 hash = (int)((uintptr_t)sock->socket %
431 sock->next = sockhash[hash];
432 sockhash[hash] = sock;
437 if (xstcb->last == 1)
441 if ((sock = calloc(1, sizeof *sock)) == NULL)
443 sock->socket = xinpcb->socket;
444 sock->proto = IPPROTO_SCTP;
445 sock->protoname = "sctp";
446 sock->state = (int)xstcb->state;
447 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
448 sock->family = AF_INET6;
450 * Currently there is no way to distinguish
451 * between IPv6 only sockets or dual family
452 * sockets. So mark it as dual socket.
454 sock->vflag = INP_IPV6 | INP_IPV4;
456 sock->family = AF_INET;
457 sock->vflag = INP_IPV4;
461 local_all_loopback = 1;
462 while (offset < len) {
463 xladdr = (struct xsctp_laddr *)(void *)(buf +
465 offset += sizeof(struct xsctp_laddr);
466 if (xladdr->last == 1)
470 laddr = calloc(1, sizeof(struct addr));
473 switch (xladdr->address.sa.sa_family) {
475 #define __IN_IS_ADDR_LOOPBACK(pina) \
476 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
477 if (!__IN_IS_ADDR_LOOPBACK(
478 &xladdr->address.sin.sin_addr))
479 local_all_loopback = 0;
480 #undef __IN_IS_ADDR_LOOPBACK
481 sockaddr(&laddr->address, AF_INET,
482 &xladdr->address.sin.sin_addr,
483 htons(xstcb->local_port));
486 if (!IN6_IS_ADDR_LOOPBACK(
487 &xladdr->address.sin6.sin6_addr))
488 local_all_loopback = 0;
489 sockaddr(&laddr->address, AF_INET6,
490 &xladdr->address.sin6.sin6_addr,
491 htons(xstcb->local_port));
495 "address family %d not supported",
496 xladdr->address.sa.sa_family);
499 if (prev_laddr == NULL)
502 prev_laddr->next = laddr;
506 foreign_all_loopback = 1;
507 while (offset < len) {
508 xraddr = (struct xsctp_raddr *)(void *)(buf +
510 offset += sizeof(struct xsctp_raddr);
511 if (xraddr->last == 1)
515 faddr = calloc(1, sizeof(struct addr));
518 switch (xraddr->address.sa.sa_family) {
520 #define __IN_IS_ADDR_LOOPBACK(pina) \
521 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
522 if (!__IN_IS_ADDR_LOOPBACK(
523 &xraddr->address.sin.sin_addr))
524 foreign_all_loopback = 0;
525 #undef __IN_IS_ADDR_LOOPBACK
526 sockaddr(&faddr->address, AF_INET,
527 &xraddr->address.sin.sin_addr,
528 htons(xstcb->remote_port));
531 if (!IN6_IS_ADDR_LOOPBACK(
532 &xraddr->address.sin6.sin6_addr))
533 foreign_all_loopback = 0;
534 sockaddr(&faddr->address, AF_INET6,
535 &xraddr->address.sin6.sin6_addr,
536 htons(xstcb->remote_port));
540 "address family %d not supported",
541 xraddr->address.sa.sa_family);
543 faddr->encaps_port = xraddr->encaps_port;
544 faddr->state = xraddr->state;
546 if (prev_faddr == NULL)
549 prev_faddr->next = faddr;
553 if ((sock->vflag & vflag) &&
555 !(local_all_loopback ||
556 foreign_all_loopback))) {
557 hash = (int)((uintptr_t)sock->socket %
559 sock->next = sockhash[hash];
560 sockhash[hash] = sock;
566 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
567 offset += sizeof(struct xsctp_inpcb);
573 gather_inet(int proto)
575 struct xinpgen *xig, *exig;
577 struct xtcpcb *xtp = NULL;
580 struct addr *laddr, *faddr;
581 const char *varname, *protoname;
584 int hash, retry, vflag;
594 varname = "net.inet.tcp.pcblist";
598 varname = "net.inet.udp.pcblist";
602 varname = "net.inet.divert.pcblist";
606 errx(1, "protocol %d not supported", proto);
614 if ((buf = realloc(buf, bufsize)) == NULL)
617 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
621 if (errno != ENOMEM || len != bufsize)
622 err(1, "sysctlbyname()");
625 xig = (struct xinpgen *)buf;
626 exig = (struct xinpgen *)(void *)
627 ((char *)buf + len - sizeof *exig);
628 if (xig->xig_len != sizeof *xig ||
629 exig->xig_len != sizeof *exig)
630 errx(1, "struct xinpgen size mismatch");
631 } while (xig->xig_gen != exig->xig_gen && retry--);
633 if (xig->xig_gen != exig->xig_gen && opt_v)
634 warnx("warning: data may be inconsistent");
637 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
642 xtp = (struct xtcpcb *)xig;
644 if (xtp->xt_len != sizeof(*xtp)) {
645 warnx("struct xtcpcb size mismatch");
648 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
652 xip = (struct xinpcb *)xig;
653 if (xip->xi_len != sizeof(*xip)) {
654 warnx("struct xinpcb size mismatch");
659 errx(1, "protocol %d not supported", proto);
661 so = &xip->xi_socket;
662 if ((xip->inp_vflag & vflag) == 0)
664 if (xip->inp_vflag & INP_IPV4) {
665 if ((xip->inp_fport == 0 && !opt_l) ||
666 (xip->inp_fport != 0 && !opt_c))
668 #define __IN_IS_ADDR_LOOPBACK(pina) \
669 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
671 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
672 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
674 #undef __IN_IS_ADDR_LOOPBACK
675 } else if (xip->inp_vflag & INP_IPV6) {
676 if ((xip->inp_fport == 0 && !opt_l) ||
677 (xip->inp_fport != 0 && !opt_c))
680 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
681 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
685 warnx("invalid vflag 0x%x", xip->inp_vflag);
688 if ((sock = calloc(1, sizeof(*sock))) == NULL)
690 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
692 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
694 sock->socket = so->xso_so;
696 if (xip->inp_vflag & INP_IPV4) {
697 sock->family = AF_INET;
698 sockaddr(&laddr->address, sock->family,
699 &xip->inp_laddr, xip->inp_lport);
700 sockaddr(&faddr->address, sock->family,
701 &xip->inp_faddr, xip->inp_fport);
702 } else if (xip->inp_vflag & INP_IPV6) {
703 sock->family = AF_INET6;
704 sockaddr(&laddr->address, sock->family,
705 &xip->in6p_laddr, xip->inp_lport);
706 sockaddr(&faddr->address, sock->family,
707 &xip->in6p_faddr, xip->inp_fport);
713 sock->vflag = xip->inp_vflag;
714 if (proto == IPPROTO_TCP) {
715 sock->state = xtp->t_state;
716 memcpy(sock->stack, xtp->xt_stack,
717 TCP_FUNCTION_NAME_LEN_MAX);
719 sock->protoname = protoname;
720 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
721 sock->next = sockhash[hash];
722 sockhash[hash] = sock;
729 gather_unix(int proto)
731 struct xunpgen *xug, *exug;
734 struct addr *laddr, *faddr;
735 const char *varname, *protoname;
742 varname = "net.local.stream.pcblist";
743 protoname = "stream";
746 varname = "net.local.dgram.pcblist";
750 varname = "net.local.seqpacket.pcblist";
751 protoname = "seqpac";
761 if ((buf = realloc(buf, bufsize)) == NULL)
764 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
766 if (errno != ENOMEM || len != bufsize)
767 err(1, "sysctlbyname()");
770 xug = (struct xunpgen *)buf;
771 exug = (struct xunpgen *)(void *)
772 ((char *)buf + len - sizeof(*exug));
773 if (xug->xug_len != sizeof(*xug) ||
774 exug->xug_len != sizeof(*exug)) {
775 warnx("struct xinpgen size mismatch");
778 } while (xug->xug_gen != exug->xug_gen && retry--);
780 if (xug->xug_gen != exug->xug_gen && opt_v)
781 warnx("warning: data may be inconsistent");
784 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
787 xup = (struct xunpcb *)xug;
788 if (xup->xu_len != sizeof(*xup)) {
789 warnx("struct xunpcb size mismatch");
792 if ((xup->unp_conn == 0 && !opt_l) ||
793 (xup->unp_conn != 0 && !opt_c))
795 if ((sock = calloc(1, sizeof(*sock))) == NULL)
797 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
799 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
801 sock->socket = xup->xu_socket.xso_so;
802 sock->pcb = xup->xu_unpp;
804 sock->family = AF_UNIX;
805 sock->protoname = protoname;
806 if (xup->xu_addr.sun_family == AF_UNIX)
808 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
809 else if (xup->unp_conn != 0)
810 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
815 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
816 sock->next = sockhash[hash];
817 sockhash[hash] = sock;
828 olen = len = sizeof(*xfiles);
829 if ((xfiles = malloc(len)) == NULL)
831 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
832 if (errno != ENOMEM || len != olen)
833 err(1, "sysctlbyname()");
835 if ((xfiles = realloc(xfiles, len)) == NULL)
838 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
839 errx(1, "struct xfile size mismatch");
840 nxfiles = len / sizeof(*xfiles);
844 printaddr(struct sockaddr_storage *ss)
846 struct sockaddr_un *sun;
847 char addrstr[NI_MAXHOST] = { '\0', '\0' };
848 int error, off, port = 0;
850 switch (ss->ss_family) {
852 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
854 port = ntohs(sstosin(ss)->sin_port);
857 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
859 port = ntohs(sstosin6(ss)->sin6_port);
863 off = (int)((char *)&sun->sun_path - (char *)sun);
864 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
866 if (addrstr[0] == '\0') {
867 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
868 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
870 errx(1, "getnameinfo()");
873 return xprintf("%s:*", addrstr);
875 return xprintf("%s:%d", addrstr, port);
879 getprocname(pid_t pid)
881 static struct kinfo_proc proc;
887 mib[2] = KERN_PROC_PID;
890 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
891 /* Do not warn if the process exits before we get its name. */
896 return (proc.ki_comm);
900 getprocjid(pid_t pid)
902 static struct kinfo_proc proc;
908 mib[2] = KERN_PROC_PID;
911 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
912 /* Do not warn if the process exits before we get its jid. */
917 return (proc.ki_jid);
921 check_ports(struct sock *s)
928 if ((s->family != AF_INET) && (s->family != AF_INET6))
930 for (addr = s->laddr; addr != NULL; addr = addr->next) {
931 if (s->family == AF_INET)
932 port = ntohs(sstosin(&addr->address)->sin_port);
934 port = ntohs(sstosin6(&addr->address)->sin6_port);
938 for (addr = s->faddr; addr != NULL; addr = addr->next) {
939 if (s->family == AF_INET)
940 port = ntohs(sstosin(&addr->address)->sin_port);
942 port = ntohs(sstosin6(&addr->address)->sin6_port);
950 sctp_conn_state(int state)
962 case SCTP_COOKIE_WAIT:
963 return "COOKIE_WAIT";
965 case SCTP_COOKIE_ECHOED:
966 return "COOKIE_ECHOED";
968 case SCTP_ESTABLISHED:
969 return "ESTABLISHED";
971 case SCTP_SHUTDOWN_SENT:
972 return "SHUTDOWN_SENT";
974 case SCTP_SHUTDOWN_RECEIVED:
975 return "SHUTDOWN_RECEIVED";
977 case SCTP_SHUTDOWN_ACK_SENT:
978 return "SHUTDOWN_ACK_SENT";
980 case SCTP_SHUTDOWN_PENDING:
981 return "SHUTDOWN_PENDING";
990 sctp_path_state(int state)
993 case SCTP_UNCONFIRMED:
994 return "UNCONFIRMED";
1009 displaysock(struct sock *s, int pos)
1012 int hash, first, offset;
1013 struct addr *laddr, *faddr;
1017 pos += xprintf(" ");
1018 pos += xprintf("%s", s->protoname);
1019 if (s->vflag & INP_IPV4)
1020 pos += xprintf("4");
1021 if (s->vflag & INP_IPV6)
1022 pos += xprintf("6");
1023 if (s->vflag & (INP_IPV4 | INP_IPV6))
1024 pos += xprintf(" ");
1028 while (laddr != NULL || faddr != NULL) {
1030 while (pos < offset)
1031 pos += xprintf(" ");
1032 switch (s->family) {
1035 if (laddr != NULL) {
1036 pos += printaddr(&laddr->address);
1037 if (s->family == AF_INET6 && pos >= 58)
1038 pos += xprintf(" ");
1040 offset += opt_w ? 46 : 22;
1041 while (pos < offset)
1042 pos += xprintf(" ");
1044 pos += printaddr(&faddr->address);
1045 offset += opt_w ? 46 : 22;
1048 if ((laddr == NULL) || (faddr == NULL))
1049 errx(1, "laddr = %p or faddr = %p is NULL",
1050 (void *)laddr, (void *)faddr);
1052 if (laddr->address.ss_len > 0) {
1053 pos += printaddr(&laddr->address);
1057 p = *(kvaddr_t*)&(faddr->address);
1059 pos += xprintf("(not connected)");
1060 offset += opt_w ? 92 : 44;
1063 pos += xprintf("-> ");
1064 for (hash = 0; hash < HASHSIZE; ++hash) {
1065 for (s_tmp = sockhash[hash];
1067 s_tmp = s_tmp->next)
1068 if (s_tmp->pcb == p)
1073 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1074 s_tmp->laddr->address.ss_len == 0)
1075 pos += xprintf("??");
1077 pos += printaddr(&s_tmp->laddr->address);
1078 offset += opt_w ? 92 : 44;
1084 if (faddr != NULL &&
1085 s->proto == IPPROTO_SCTP &&
1086 s->state != SCTP_CLOSED &&
1087 s->state != SCTP_BOUND &&
1088 s->state != SCTP_LISTEN) {
1089 while (pos < offset)
1090 pos += xprintf(" ");
1091 pos += xprintf("%u",
1092 ntohs(faddr->encaps_port));
1097 if (faddr != NULL &&
1098 s->proto == IPPROTO_SCTP &&
1099 s->state != SCTP_CLOSED &&
1100 s->state != SCTP_BOUND &&
1101 s->state != SCTP_LISTEN) {
1102 while (pos < offset)
1103 pos += xprintf(" ");
1104 pos += xprintf("%s",
1105 sctp_path_state(faddr->state));
1111 if (s->proto == IPPROTO_SCTP ||
1112 s->proto == IPPROTO_TCP) {
1113 while (pos < offset)
1114 pos += xprintf(" ");
1117 pos += xprintf("%s",
1118 sctp_conn_state(s->state));
1121 if (s->state >= 0 &&
1122 s->state < TCP_NSTATES)
1123 pos += xprintf("%s",
1124 tcpstates[s->state]);
1126 pos += xprintf("?");
1132 if (opt_S && s->proto == IPPROTO_TCP) {
1133 while (pos < offset)
1134 pos += xprintf(" ");
1135 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1140 laddr = laddr->next;
1142 faddr = faddr->next;
1143 if ((laddr != NULL) || (faddr != NULL)) {
1161 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1162 "USER", "COMMAND", "PID", "FD", "PROTO",
1163 opt_w ? 45 : 21, "LOCAL ADDRESS",
1164 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1166 printf(" %-6s", "ENCAPS");
1168 printf(" %-12s", "PATH STATE");
1169 printf(" %-12s", "CONN STATE");
1172 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1176 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1177 if (xf->xf_data == 0)
1179 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1181 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1182 for (s = sockhash[hash]; s != NULL; s = s->next) {
1183 if (s->socket != xf->xf_data)
1185 if (!check_ports(s))
1189 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1190 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1192 pos += xprintf("%s ", pwd->pw_name);
1194 pos += xprintf(" ");
1195 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1197 pos += xprintf(" ");
1198 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1200 pos += xprintf(" ");
1201 pos += xprintf("%d ", xf->xf_fd);
1202 displaysock(s, pos);
1207 for (hash = 0; hash < HASHSIZE; hash++) {
1208 for (s = sockhash[hash]; s != NULL; s = s->next) {
1211 if (!check_ports(s))
1214 pos += xprintf("%-8s %-10s %-5s %-2s ",
1215 "?", "?", "?", "?");
1216 displaysock(s, pos);
1221 static int set_default_protos(void)
1223 struct protoent *prot;
1227 init_protos(default_numprotos);
1229 for (pindex = 0; pindex < default_numprotos; pindex++) {
1230 pname = default_protos[pindex];
1231 prot = getprotobyname(pname);
1233 err(1, "getprotobyname: %s", pname);
1234 protos[pindex] = prot->p_proto;
1244 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1249 main(int argc, char *argv[])
1251 int protos_defined = -1;
1255 while ((o = getopt(argc, argv, "46cj:Llp:P:qSsUuvw")) != -1)
1267 opt_j = jail_getid(optarg);
1269 errx(1, "%s", jail_errmsg);
1278 parse_ports(optarg);
1281 protos_defined = parse_protos(optarg);
1314 if ((!opt_4 && !opt_6) && protos_defined != -1)
1316 if (!opt_4 && !opt_6 && !opt_u)
1317 opt_4 = opt_6 = opt_u = 1;
1318 if ((opt_4 || opt_6) && protos_defined == -1)
1319 protos_defined = set_default_protos();
1320 if (!opt_c && !opt_l)
1323 if (opt_4 || opt_6) {
1324 for (i = 0; i < protos_defined; i++)
1325 if (protos[i] == IPPROTO_SCTP)
1328 gather_inet(protos[i]);
1331 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1332 gather_unix(SOCK_STREAM);
1333 gather_unix(SOCK_DGRAM);
1334 gather_unix(SOCK_SEQPACKET);