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_q; /* Don't show header */
83 static int opt_S; /* Show protocol stack if applicable */
84 static int opt_s; /* Show protocol state if applicable */
85 static int opt_U; /* Show remote UDP encapsulation port number */
86 static int opt_u; /* Show Unix domain sockets */
87 static int opt_v; /* Verbose mode */
88 static int opt_w; /* Wide print area for addresses */
91 * Default protocols to use if no -P was defined.
93 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
94 static size_t default_numprotos = nitems(default_protos);
96 static int *protos; /* protocols to use */
97 static size_t numprotos; /* allocated size of protos[] */
101 #define INT_BIT (sizeof(int)*CHAR_BIT)
102 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
103 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
106 struct sockaddr_storage address;
107 unsigned int encaps_port;
120 const char *protoname;
121 char stack[TCP_FUNCTION_NAME_LEN_MAX];
122 char cc[TCP_CA_NAME_MAX];
128 #define HASHSIZE 1009
129 static struct sock *sockhash[HASHSIZE];
131 static struct xfile *xfiles;
135 xprintf(const char *fmt, ...)
141 len = vprintf(fmt, ap);
149 get_proto_type(const char *proto)
151 struct protoent *pent;
153 if (strlen(proto) == 0)
155 pent = getprotobyname(proto);
157 warn("getprotobyname");
160 return (pent->p_proto);
171 /* Find the maximum number of possible protocols. */
172 while (getprotoent() != NULL)
177 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
179 numprotos = proto_count;
183 parse_protos(char *protospec)
186 int proto_type, proto_index;
188 if (protospec == NULL)
193 while ((prot = strsep(&protospec, ",")) != NULL) {
194 if (strlen(prot) == 0)
196 proto_type = get_proto_type(prot);
197 if (proto_type != -1)
198 protos[proto_index++] = proto_type;
200 numprotos = proto_index;
201 return (proto_index);
205 parse_ports(const char *portspec)
211 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
216 errx(1, "syntax error in port range");
217 for (q = p; *q != '\0' && isdigit(*q); ++q)
219 for (port = 0; p < q; ++p)
220 port = port * 10 + digittoint(*p);
221 if (port < 0 || port > 65535)
222 errx(1, "invalid port number");
235 for (q = p; *q != '\0' && isdigit(*q); ++q)
237 for (end = 0; p < q; ++p)
238 end = end * 10 + digittoint(*p);
239 if (end < port || end > 65535)
240 errx(1, "invalid port number");
249 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
251 struct sockaddr_in *sin4;
252 struct sockaddr_in6 *sin6;
254 bzero(ss, sizeof(*ss));
258 sin4->sin_len = sizeof(*sin4);
259 sin4->sin_family = af;
260 sin4->sin_port = port;
261 sin4->sin_addr = *(struct in_addr *)addr;
265 sin6->sin6_len = sizeof(*sin6);
266 sin6->sin6_family = af;
267 sin6->sin6_port = port;
268 sin6->sin6_addr = *(struct in6_addr *)addr;
269 #define s6_addr16 __u6_addr.__u6_addr16
270 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
271 sin6->sin6_scope_id =
272 ntohs(sin6->sin6_addr.s6_addr16[1]);
273 sin6->sin6_addr.s6_addr16[1] = 0;
282 free_socket(struct sock *sock)
284 struct addr *cur, *next;
287 while (cur != NULL) {
293 while (cur != NULL) {
305 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
306 struct xsctp_inpcb *xinpcb;
307 struct xsctp_tcb *xstcb;
308 struct xsctp_raddr *xraddr;
309 struct xsctp_laddr *xladdr;
314 int no_stcb, local_all_loopback, foreign_all_loopback;
322 varname = "net.inet.sctp.assoclist";
323 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
325 err(1, "sysctlbyname()");
328 if ((buf = (char *)malloc(len)) == NULL) {
332 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
333 err(1, "sysctlbyname()");
337 xinpcb = (struct xsctp_inpcb *)(void *)buf;
338 offset = sizeof(struct xsctp_inpcb);
339 while ((offset < len) && (xinpcb->last == 0)) {
340 if ((sock = calloc(1, sizeof *sock)) == NULL)
342 sock->socket = xinpcb->socket;
343 sock->proto = IPPROTO_SCTP;
344 sock->protoname = "sctp";
345 if (xinpcb->maxqlen == 0)
346 sock->state = SCTP_CLOSED;
348 sock->state = SCTP_LISTEN;
349 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
350 sock->family = AF_INET6;
352 * Currently there is no way to distinguish between
353 * IPv6 only sockets or dual family sockets.
354 * So mark it as dual socket.
356 sock->vflag = INP_IPV6 | INP_IPV4;
358 sock->family = AF_INET;
359 sock->vflag = INP_IPV4;
362 local_all_loopback = 1;
363 while (offset < len) {
364 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
365 offset += sizeof(struct xsctp_laddr);
366 if (xladdr->last == 1)
368 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
370 switch (xladdr->address.sa.sa_family) {
372 #define __IN_IS_ADDR_LOOPBACK(pina) \
373 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
374 if (!__IN_IS_ADDR_LOOPBACK(
375 &xladdr->address.sin.sin_addr))
376 local_all_loopback = 0;
377 #undef __IN_IS_ADDR_LOOPBACK
378 sockaddr(&laddr->address, AF_INET,
379 &xladdr->address.sin.sin_addr,
380 htons(xinpcb->local_port));
383 if (!IN6_IS_ADDR_LOOPBACK(
384 &xladdr->address.sin6.sin6_addr))
385 local_all_loopback = 0;
386 sockaddr(&laddr->address, AF_INET6,
387 &xladdr->address.sin6.sin6_addr,
388 htons(xinpcb->local_port));
391 errx(1, "address family %d not supported",
392 xladdr->address.sa.sa_family);
395 if (prev_laddr == NULL)
398 prev_laddr->next = laddr;
401 if (sock->laddr == NULL) {
403 calloc(1, sizeof(struct addr))) == NULL)
405 sock->laddr->address.ss_family = sock->family;
406 if (sock->family == AF_INET)
407 sock->laddr->address.ss_len =
408 sizeof(struct sockaddr_in);
410 sock->laddr->address.ss_len =
411 sizeof(struct sockaddr_in6);
412 local_all_loopback = 0;
414 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
416 sock->faddr->address.ss_family = sock->family;
417 if (sock->family == AF_INET)
418 sock->faddr->address.ss_len =
419 sizeof(struct sockaddr_in);
421 sock->faddr->address.ss_len =
422 sizeof(struct sockaddr_in6);
424 while (offset < len) {
425 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
426 offset += sizeof(struct xsctp_tcb);
428 if (opt_l && (sock->vflag & vflag) &&
429 (!opt_L || !local_all_loopback) &&
430 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
431 (xstcb->last == 1))) {
432 hash = (int)((uintptr_t)sock->socket %
434 sock->next = sockhash[hash];
435 sockhash[hash] = sock;
440 if (xstcb->last == 1)
444 if ((sock = calloc(1, sizeof *sock)) == NULL)
446 sock->socket = xinpcb->socket;
447 sock->proto = IPPROTO_SCTP;
448 sock->protoname = "sctp";
449 sock->state = (int)xstcb->state;
450 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
451 sock->family = AF_INET6;
453 * Currently there is no way to distinguish
454 * between IPv6 only sockets or dual family
455 * sockets. So mark it as dual socket.
457 sock->vflag = INP_IPV6 | INP_IPV4;
459 sock->family = AF_INET;
460 sock->vflag = INP_IPV4;
464 local_all_loopback = 1;
465 while (offset < len) {
466 xladdr = (struct xsctp_laddr *)(void *)(buf +
468 offset += sizeof(struct xsctp_laddr);
469 if (xladdr->last == 1)
473 laddr = calloc(1, sizeof(struct addr));
476 switch (xladdr->address.sa.sa_family) {
478 #define __IN_IS_ADDR_LOOPBACK(pina) \
479 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
480 if (!__IN_IS_ADDR_LOOPBACK(
481 &xladdr->address.sin.sin_addr))
482 local_all_loopback = 0;
483 #undef __IN_IS_ADDR_LOOPBACK
484 sockaddr(&laddr->address, AF_INET,
485 &xladdr->address.sin.sin_addr,
486 htons(xstcb->local_port));
489 if (!IN6_IS_ADDR_LOOPBACK(
490 &xladdr->address.sin6.sin6_addr))
491 local_all_loopback = 0;
492 sockaddr(&laddr->address, AF_INET6,
493 &xladdr->address.sin6.sin6_addr,
494 htons(xstcb->local_port));
498 "address family %d not supported",
499 xladdr->address.sa.sa_family);
502 if (prev_laddr == NULL)
505 prev_laddr->next = laddr;
509 foreign_all_loopback = 1;
510 while (offset < len) {
511 xraddr = (struct xsctp_raddr *)(void *)(buf +
513 offset += sizeof(struct xsctp_raddr);
514 if (xraddr->last == 1)
518 faddr = calloc(1, sizeof(struct addr));
521 switch (xraddr->address.sa.sa_family) {
523 #define __IN_IS_ADDR_LOOPBACK(pina) \
524 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
525 if (!__IN_IS_ADDR_LOOPBACK(
526 &xraddr->address.sin.sin_addr))
527 foreign_all_loopback = 0;
528 #undef __IN_IS_ADDR_LOOPBACK
529 sockaddr(&faddr->address, AF_INET,
530 &xraddr->address.sin.sin_addr,
531 htons(xstcb->remote_port));
534 if (!IN6_IS_ADDR_LOOPBACK(
535 &xraddr->address.sin6.sin6_addr))
536 foreign_all_loopback = 0;
537 sockaddr(&faddr->address, AF_INET6,
538 &xraddr->address.sin6.sin6_addr,
539 htons(xstcb->remote_port));
543 "address family %d not supported",
544 xraddr->address.sa.sa_family);
546 faddr->encaps_port = xraddr->encaps_port;
547 faddr->state = xraddr->state;
549 if (prev_faddr == NULL)
552 prev_faddr->next = faddr;
556 if ((sock->vflag & vflag) &&
558 !(local_all_loopback ||
559 foreign_all_loopback))) {
560 hash = (int)((uintptr_t)sock->socket %
562 sock->next = sockhash[hash];
563 sockhash[hash] = sock;
569 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
570 offset += sizeof(struct xsctp_inpcb);
576 gather_inet(int proto)
578 struct xinpgen *xig, *exig;
580 struct xtcpcb *xtp = NULL;
583 struct addr *laddr, *faddr;
584 const char *varname, *protoname;
587 int hash, retry, vflag;
597 varname = "net.inet.tcp.pcblist";
601 varname = "net.inet.udp.pcblist";
605 varname = "net.inet.divert.pcblist";
609 errx(1, "protocol %d not supported", proto);
617 if ((buf = realloc(buf, bufsize)) == NULL)
620 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
624 if (errno != ENOMEM || len != bufsize)
625 err(1, "sysctlbyname()");
628 xig = (struct xinpgen *)buf;
629 exig = (struct xinpgen *)(void *)
630 ((char *)buf + len - sizeof *exig);
631 if (xig->xig_len != sizeof *xig ||
632 exig->xig_len != sizeof *exig)
633 errx(1, "struct xinpgen size mismatch");
634 } while (xig->xig_gen != exig->xig_gen && retry--);
636 if (xig->xig_gen != exig->xig_gen && opt_v)
637 warnx("warning: data may be inconsistent");
640 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
645 xtp = (struct xtcpcb *)xig;
647 if (xtp->xt_len != sizeof(*xtp)) {
648 warnx("struct xtcpcb size mismatch");
651 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
655 xip = (struct xinpcb *)xig;
656 if (xip->xi_len != sizeof(*xip)) {
657 warnx("struct xinpcb size mismatch");
662 errx(1, "protocol %d not supported", proto);
664 so = &xip->xi_socket;
665 if ((xip->inp_vflag & vflag) == 0)
667 if (xip->inp_vflag & INP_IPV4) {
668 if ((xip->inp_fport == 0 && !opt_l) ||
669 (xip->inp_fport != 0 && !opt_c))
671 #define __IN_IS_ADDR_LOOPBACK(pina) \
672 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
674 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
675 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
677 #undef __IN_IS_ADDR_LOOPBACK
678 } else if (xip->inp_vflag & INP_IPV6) {
679 if ((xip->inp_fport == 0 && !opt_l) ||
680 (xip->inp_fport != 0 && !opt_c))
683 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
684 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
688 warnx("invalid vflag 0x%x", xip->inp_vflag);
691 if ((sock = calloc(1, sizeof(*sock))) == NULL)
693 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
695 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
697 sock->socket = so->xso_so;
699 if (xip->inp_vflag & INP_IPV4) {
700 sock->family = AF_INET;
701 sockaddr(&laddr->address, sock->family,
702 &xip->inp_laddr, xip->inp_lport);
703 sockaddr(&faddr->address, sock->family,
704 &xip->inp_faddr, xip->inp_fport);
705 } else if (xip->inp_vflag & INP_IPV6) {
706 sock->family = AF_INET6;
707 sockaddr(&laddr->address, sock->family,
708 &xip->in6p_laddr, xip->inp_lport);
709 sockaddr(&faddr->address, sock->family,
710 &xip->in6p_faddr, xip->inp_fport);
716 sock->vflag = xip->inp_vflag;
717 if (proto == IPPROTO_TCP) {
718 sock->state = xtp->t_state;
719 memcpy(sock->stack, xtp->xt_stack,
720 TCP_FUNCTION_NAME_LEN_MAX);
721 memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX);
723 sock->protoname = protoname;
724 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
725 sock->next = sockhash[hash];
726 sockhash[hash] = sock;
733 gather_unix(int proto)
735 struct xunpgen *xug, *exug;
738 struct addr *laddr, *faddr;
739 const char *varname, *protoname;
746 varname = "net.local.stream.pcblist";
747 protoname = "stream";
750 varname = "net.local.dgram.pcblist";
754 varname = "net.local.seqpacket.pcblist";
755 protoname = "seqpac";
765 if ((buf = realloc(buf, bufsize)) == NULL)
768 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
770 if (errno != ENOMEM || len != bufsize)
771 err(1, "sysctlbyname()");
774 xug = (struct xunpgen *)buf;
775 exug = (struct xunpgen *)(void *)
776 ((char *)buf + len - sizeof(*exug));
777 if (xug->xug_len != sizeof(*xug) ||
778 exug->xug_len != sizeof(*exug)) {
779 warnx("struct xinpgen size mismatch");
782 } while (xug->xug_gen != exug->xug_gen && retry--);
784 if (xug->xug_gen != exug->xug_gen && opt_v)
785 warnx("warning: data may be inconsistent");
788 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
791 xup = (struct xunpcb *)xug;
792 if (xup->xu_len != sizeof(*xup)) {
793 warnx("struct xunpcb size mismatch");
796 if ((xup->unp_conn == 0 && !opt_l) ||
797 (xup->unp_conn != 0 && !opt_c))
799 if ((sock = calloc(1, sizeof(*sock))) == NULL)
801 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
803 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
805 sock->socket = xup->xu_socket.xso_so;
806 sock->pcb = xup->xu_unpp;
808 sock->family = AF_UNIX;
809 sock->protoname = protoname;
810 if (xup->xu_addr.sun_family == AF_UNIX)
812 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
813 else if (xup->unp_conn != 0)
814 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
819 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
820 sock->next = sockhash[hash];
821 sockhash[hash] = sock;
832 olen = len = sizeof(*xfiles);
833 if ((xfiles = malloc(len)) == NULL)
835 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
836 if (errno != ENOMEM || len != olen)
837 err(1, "sysctlbyname()");
839 if ((xfiles = realloc(xfiles, len)) == NULL)
842 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
843 errx(1, "struct xfile size mismatch");
844 nxfiles = len / sizeof(*xfiles);
848 printaddr(struct sockaddr_storage *ss)
850 struct sockaddr_un *sun;
851 char addrstr[NI_MAXHOST] = { '\0', '\0' };
852 int error, off, port = 0;
854 switch (ss->ss_family) {
856 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
858 port = ntohs(sstosin(ss)->sin_port);
861 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
863 port = ntohs(sstosin6(ss)->sin6_port);
867 off = (int)((char *)&sun->sun_path - (char *)sun);
868 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
870 if (addrstr[0] == '\0') {
871 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
872 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
874 errx(1, "getnameinfo()");
877 return xprintf("%s:*", addrstr);
879 return xprintf("%s:%d", addrstr, port);
883 getprocname(pid_t pid)
885 static struct kinfo_proc proc;
891 mib[2] = KERN_PROC_PID;
894 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
895 /* Do not warn if the process exits before we get its name. */
900 return (proc.ki_comm);
904 getprocjid(pid_t pid)
906 static struct kinfo_proc proc;
912 mib[2] = KERN_PROC_PID;
915 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
916 /* Do not warn if the process exits before we get its jid. */
921 return (proc.ki_jid);
925 check_ports(struct sock *s)
932 if ((s->family != AF_INET) && (s->family != AF_INET6))
934 for (addr = s->laddr; addr != NULL; addr = addr->next) {
935 if (s->family == AF_INET)
936 port = ntohs(sstosin(&addr->address)->sin_port);
938 port = ntohs(sstosin6(&addr->address)->sin6_port);
942 for (addr = s->faddr; addr != NULL; addr = addr->next) {
943 if (s->family == AF_INET)
944 port = ntohs(sstosin(&addr->address)->sin_port);
946 port = ntohs(sstosin6(&addr->address)->sin6_port);
954 sctp_conn_state(int state)
966 case SCTP_COOKIE_WAIT:
967 return "COOKIE_WAIT";
969 case SCTP_COOKIE_ECHOED:
970 return "COOKIE_ECHOED";
972 case SCTP_ESTABLISHED:
973 return "ESTABLISHED";
975 case SCTP_SHUTDOWN_SENT:
976 return "SHUTDOWN_SENT";
978 case SCTP_SHUTDOWN_RECEIVED:
979 return "SHUTDOWN_RECEIVED";
981 case SCTP_SHUTDOWN_ACK_SENT:
982 return "SHUTDOWN_ACK_SENT";
984 case SCTP_SHUTDOWN_PENDING:
985 return "SHUTDOWN_PENDING";
994 sctp_path_state(int state)
997 case SCTP_UNCONFIRMED:
998 return "UNCONFIRMED";
1013 displaysock(struct sock *s, int pos)
1016 int hash, first, offset;
1017 struct addr *laddr, *faddr;
1021 pos += xprintf(" ");
1022 pos += xprintf("%s", s->protoname);
1023 if (s->vflag & INP_IPV4)
1024 pos += xprintf("4");
1025 if (s->vflag & INP_IPV6)
1026 pos += xprintf("6");
1027 if (s->vflag & (INP_IPV4 | INP_IPV6))
1028 pos += xprintf(" ");
1032 while (laddr != NULL || faddr != NULL) {
1034 while (pos < offset)
1035 pos += xprintf(" ");
1036 switch (s->family) {
1039 if (laddr != NULL) {
1040 pos += printaddr(&laddr->address);
1041 if (s->family == AF_INET6 && pos >= 58)
1042 pos += xprintf(" ");
1044 offset += opt_w ? 46 : 22;
1045 while (pos < offset)
1046 pos += xprintf(" ");
1048 pos += printaddr(&faddr->address);
1049 offset += opt_w ? 46 : 22;
1052 if ((laddr == NULL) || (faddr == NULL))
1053 errx(1, "laddr = %p or faddr = %p is NULL",
1054 (void *)laddr, (void *)faddr);
1056 if (laddr->address.ss_len > 0) {
1057 pos += printaddr(&laddr->address);
1061 p = *(kvaddr_t*)&(faddr->address);
1063 pos += xprintf("(not connected)");
1064 offset += opt_w ? 92 : 44;
1067 pos += xprintf("-> ");
1068 for (hash = 0; hash < HASHSIZE; ++hash) {
1069 for (s_tmp = sockhash[hash];
1071 s_tmp = s_tmp->next)
1072 if (s_tmp->pcb == p)
1077 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1078 s_tmp->laddr->address.ss_len == 0)
1079 pos += xprintf("??");
1081 pos += printaddr(&s_tmp->laddr->address);
1082 offset += opt_w ? 92 : 44;
1088 if (faddr != NULL &&
1089 s->proto == IPPROTO_SCTP &&
1090 s->state != SCTP_CLOSED &&
1091 s->state != SCTP_BOUND &&
1092 s->state != SCTP_LISTEN) {
1093 while (pos < offset)
1094 pos += xprintf(" ");
1095 pos += xprintf("%u",
1096 ntohs(faddr->encaps_port));
1101 if (faddr != NULL &&
1102 s->proto == IPPROTO_SCTP &&
1103 s->state != SCTP_CLOSED &&
1104 s->state != SCTP_BOUND &&
1105 s->state != SCTP_LISTEN) {
1106 while (pos < offset)
1107 pos += xprintf(" ");
1108 pos += xprintf("%s",
1109 sctp_path_state(faddr->state));
1115 if (s->proto == IPPROTO_SCTP ||
1116 s->proto == IPPROTO_TCP) {
1117 while (pos < offset)
1118 pos += xprintf(" ");
1121 pos += xprintf("%s",
1122 sctp_conn_state(s->state));
1125 if (s->state >= 0 &&
1126 s->state < TCP_NSTATES)
1127 pos += xprintf("%s",
1128 tcpstates[s->state]);
1130 pos += xprintf("?");
1137 if (s->proto == IPPROTO_TCP) {
1138 while (pos < offset)
1139 pos += xprintf(" ");
1140 pos += xprintf("%.*s",
1141 TCP_FUNCTION_NAME_LEN_MAX,
1144 offset += TCP_FUNCTION_NAME_LEN_MAX + 1;
1147 if (s->proto == IPPROTO_TCP) {
1148 while (pos < offset)
1149 pos += xprintf(" ");
1150 xprintf("%.*s", TCP_CA_NAME_MAX, s->cc);
1152 offset += TCP_CA_NAME_MAX + 1;
1156 laddr = laddr->next;
1158 faddr = faddr->next;
1159 if ((laddr != NULL) || (faddr != NULL)) {
1177 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1178 "USER", "COMMAND", "PID", "FD", "PROTO",
1179 opt_w ? 45 : 21, "LOCAL ADDRESS",
1180 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1182 printf(" %-6s", "ENCAPS");
1184 printf(" %-12s", "PATH STATE");
1185 printf(" %-12s", "CONN STATE");
1188 printf(" %-*.*s", TCP_FUNCTION_NAME_LEN_MAX,
1189 TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1191 printf(" %-.*s", TCP_CA_NAME_MAX, "CC");
1195 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1196 if (xf->xf_data == 0)
1198 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1200 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1201 for (s = sockhash[hash]; s != NULL; s = s->next) {
1202 if (s->socket != xf->xf_data)
1204 if (!check_ports(s))
1208 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1209 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1211 pos += xprintf("%s ", pwd->pw_name);
1213 pos += xprintf(" ");
1214 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1216 pos += xprintf(" ");
1217 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1219 pos += xprintf(" ");
1220 pos += xprintf("%d ", xf->xf_fd);
1221 displaysock(s, pos);
1226 for (hash = 0; hash < HASHSIZE; hash++) {
1227 for (s = sockhash[hash]; s != NULL; s = s->next) {
1230 if (!check_ports(s))
1233 pos += xprintf("%-8s %-10s %-5s %-2s ",
1234 "?", "?", "?", "?");
1235 displaysock(s, pos);
1241 set_default_protos(void)
1243 struct protoent *prot;
1247 init_protos(default_numprotos);
1249 for (pindex = 0; pindex < default_numprotos; pindex++) {
1250 pname = default_protos[pindex];
1251 prot = getprotobyname(pname);
1253 err(1, "getprotobyname: %s", pname);
1254 protos[pindex] = prot->p_proto;
1261 * Return the vnet property of the jail, or -1 on error.
1264 jail_getvnet(int jid)
1266 struct iovec jiov[6];
1270 jiov[0].iov_base = __DECONST(char *, "jid");
1271 jiov[0].iov_len = sizeof("jid");
1272 jiov[1].iov_base = &jid;
1273 jiov[1].iov_len = sizeof(jid);
1274 jiov[2].iov_base = __DECONST(char *, "vnet");
1275 jiov[2].iov_len = sizeof("vnet");
1276 jiov[3].iov_base = &vnet;
1277 jiov[3].iov_len = sizeof(vnet);
1278 jiov[4].iov_base = __DECONST(char *, "errmsg");
1279 jiov[4].iov_len = sizeof("errmsg");
1280 jiov[5].iov_base = jail_errmsg;
1281 jiov[5].iov_len = JAIL_ERRMSGLEN;
1282 jail_errmsg[0] = '\0';
1283 if (jail_get(jiov, nitems(jiov), 0) < 0) {
1284 if (!jail_errmsg[0])
1285 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1286 "jail_get: %s", strerror(errno));
1296 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1301 main(int argc, char *argv[])
1303 int protos_defined = -1;
1307 while ((o = getopt(argc, argv, "46Ccj:Llp:P:qSsUuvw")) != -1)
1322 opt_j = jail_getid(optarg);
1324 errx(1, "%s", jail_errmsg);
1333 parse_ports(optarg);
1336 protos_defined = parse_protos(optarg);
1370 switch (jail_getvnet(opt_j)) {
1372 errx(2, "%s", jail_errmsg);
1374 if (jail_attach(opt_j) < 0)
1375 err(3, "jail_attach()");
1376 /* Set back to -1 for normal output in vnet jail. */
1384 if ((!opt_4 && !opt_6) && protos_defined != -1)
1386 if (!opt_4 && !opt_6 && !opt_u)
1387 opt_4 = opt_6 = opt_u = 1;
1388 if ((opt_4 || opt_6) && protos_defined == -1)
1389 protos_defined = set_default_protos();
1390 if (!opt_c && !opt_l)
1393 if (opt_4 || opt_6) {
1394 for (i = 0; i < protos_defined; i++)
1395 if (protos[i] == IPPROTO_SCTP)
1398 gather_inet(protos[i]);
1401 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1402 gather_unix(SOCK_STREAM);
1403 gather_unix(SOCK_DGRAM);
1404 gather_unix(SOCK_SEQPACKET);