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>
68 #define sstosin(ss) ((struct sockaddr_in *)(ss))
69 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
70 #define sstosun(ss) ((struct sockaddr_un *)(ss))
71 #define sstosa(ss) ((struct sockaddr *)(ss))
73 static int opt_4; /* Show IPv4 sockets */
74 static int opt_6; /* Show IPv6 sockets */
75 static int opt_c; /* Show connected sockets */
76 static int opt_j; /* Show specified jail */
77 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
78 static int opt_l; /* Show listening sockets */
79 static int opt_S; /* Show protocol stack if applicable */
80 static int opt_s; /* Show protocol state if applicable */
81 static int opt_U; /* Show remote UDP encapsulation port number */
82 static int opt_u; /* Show Unix domain sockets */
83 static int opt_v; /* Verbose mode */
84 static int opt_w; /* Wide print area for addresses */
87 * Default protocols to use if no -P was defined.
89 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
90 static size_t default_numprotos = nitems(default_protos);
92 static int *protos; /* protocols to use */
93 static size_t numprotos; /* allocated size of protos[] */
97 #define INT_BIT (sizeof(int)*CHAR_BIT)
98 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
99 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
102 struct sockaddr_storage address;
103 unsigned int encaps_port;
116 const char *protoname;
117 char stack[TCP_FUNCTION_NAME_LEN_MAX];
123 #define HASHSIZE 1009
124 static struct sock *sockhash[HASHSIZE];
126 static struct xfile *xfiles;
130 xprintf(const char *fmt, ...)
136 len = vprintf(fmt, ap);
144 get_proto_type(const char *proto)
146 struct protoent *pent;
148 if (strlen(proto) == 0)
150 pent = getprotobyname(proto);
152 warn("getprotobyname");
155 return (pent->p_proto);
166 /* Find the maximum number of possible protocols. */
167 while (getprotoent() != NULL)
172 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
174 numprotos = proto_count;
178 parse_protos(char *protospec)
181 int proto_type, proto_index;
183 if (protospec == NULL)
188 while ((prot = strsep(&protospec, ",")) != NULL) {
189 if (strlen(prot) == 0)
191 proto_type = get_proto_type(prot);
192 if (proto_type != -1)
193 protos[proto_index++] = proto_type;
195 numprotos = proto_index;
196 return (proto_index);
200 parse_ports(const char *portspec)
206 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
211 errx(1, "syntax error in port range");
212 for (q = p; *q != '\0' && isdigit(*q); ++q)
214 for (port = 0; p < q; ++p)
215 port = port * 10 + digittoint(*p);
216 if (port < 0 || port > 65535)
217 errx(1, "invalid port number");
230 for (q = p; *q != '\0' && isdigit(*q); ++q)
232 for (end = 0; p < q; ++p)
233 end = end * 10 + digittoint(*p);
234 if (end < port || end > 65535)
235 errx(1, "invalid port number");
244 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
246 struct sockaddr_in *sin4;
247 struct sockaddr_in6 *sin6;
249 bzero(ss, sizeof(*ss));
253 sin4->sin_len = sizeof(*sin4);
254 sin4->sin_family = af;
255 sin4->sin_port = port;
256 sin4->sin_addr = *(struct in_addr *)addr;
260 sin6->sin6_len = sizeof(*sin6);
261 sin6->sin6_family = af;
262 sin6->sin6_port = port;
263 sin6->sin6_addr = *(struct in6_addr *)addr;
264 #define s6_addr16 __u6_addr.__u6_addr16
265 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
266 sin6->sin6_scope_id =
267 ntohs(sin6->sin6_addr.s6_addr16[1]);
268 sin6->sin6_addr.s6_addr16[1] = 0;
277 free_socket(struct sock *sock)
279 struct addr *cur, *next;
282 while (cur != NULL) {
288 while (cur != NULL) {
300 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
301 struct xsctp_inpcb *xinpcb;
302 struct xsctp_tcb *xstcb;
303 struct xsctp_raddr *xraddr;
304 struct xsctp_laddr *xladdr;
309 int no_stcb, local_all_loopback, foreign_all_loopback;
317 varname = "net.inet.sctp.assoclist";
318 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
320 err(1, "sysctlbyname()");
323 if ((buf = (char *)malloc(len)) == NULL) {
327 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
328 err(1, "sysctlbyname()");
332 xinpcb = (struct xsctp_inpcb *)(void *)buf;
333 offset = sizeof(struct xsctp_inpcb);
334 while ((offset < len) && (xinpcb->last == 0)) {
335 if ((sock = calloc(1, sizeof *sock)) == NULL)
337 sock->socket = xinpcb->socket;
338 sock->proto = IPPROTO_SCTP;
339 sock->protoname = "sctp";
340 if (xinpcb->maxqlen == 0)
341 sock->state = SCTP_CLOSED;
343 sock->state = SCTP_LISTEN;
344 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
345 sock->family = AF_INET6;
347 * Currently there is no way to distinguish between
348 * IPv6 only sockets or dual family sockets.
349 * So mark it as dual socket.
351 sock->vflag = INP_IPV6 | INP_IPV4;
353 sock->family = AF_INET;
354 sock->vflag = INP_IPV4;
357 local_all_loopback = 1;
358 while (offset < len) {
359 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
360 offset += sizeof(struct xsctp_laddr);
361 if (xladdr->last == 1)
363 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
365 switch (xladdr->address.sa.sa_family) {
367 #define __IN_IS_ADDR_LOOPBACK(pina) \
368 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
369 if (!__IN_IS_ADDR_LOOPBACK(
370 &xladdr->address.sin.sin_addr))
371 local_all_loopback = 0;
372 #undef __IN_IS_ADDR_LOOPBACK
373 sockaddr(&laddr->address, AF_INET,
374 &xladdr->address.sin.sin_addr,
375 htons(xinpcb->local_port));
378 if (!IN6_IS_ADDR_LOOPBACK(
379 &xladdr->address.sin6.sin6_addr))
380 local_all_loopback = 0;
381 sockaddr(&laddr->address, AF_INET6,
382 &xladdr->address.sin6.sin6_addr,
383 htons(xinpcb->local_port));
386 errx(1, "address family %d not supported",
387 xladdr->address.sa.sa_family);
390 if (prev_laddr == NULL)
393 prev_laddr->next = laddr;
396 if (sock->laddr == NULL) {
398 calloc(1, sizeof(struct addr))) == NULL)
400 sock->laddr->address.ss_family = sock->family;
401 if (sock->family == AF_INET)
402 sock->laddr->address.ss_len =
403 sizeof(struct sockaddr_in);
405 sock->laddr->address.ss_len =
406 sizeof(struct sockaddr_in6);
407 local_all_loopback = 0;
409 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
411 sock->faddr->address.ss_family = sock->family;
412 if (sock->family == AF_INET)
413 sock->faddr->address.ss_len =
414 sizeof(struct sockaddr_in);
416 sock->faddr->address.ss_len =
417 sizeof(struct sockaddr_in6);
419 while (offset < len) {
420 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
421 offset += sizeof(struct xsctp_tcb);
423 if (opt_l && (sock->vflag & vflag) &&
424 (!opt_L || !local_all_loopback) &&
425 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
426 (xstcb->last == 1))) {
427 hash = (int)((uintptr_t)sock->socket %
429 sock->next = sockhash[hash];
430 sockhash[hash] = sock;
435 if (xstcb->last == 1)
439 if ((sock = calloc(1, sizeof *sock)) == NULL)
441 sock->socket = xinpcb->socket;
442 sock->proto = IPPROTO_SCTP;
443 sock->protoname = "sctp";
444 sock->state = (int)xstcb->state;
445 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
446 sock->family = AF_INET6;
448 * Currently there is no way to distinguish
449 * between IPv6 only sockets or dual family
450 * sockets. So mark it as dual socket.
452 sock->vflag = INP_IPV6 | INP_IPV4;
454 sock->family = AF_INET;
455 sock->vflag = INP_IPV4;
459 local_all_loopback = 1;
460 while (offset < len) {
461 xladdr = (struct xsctp_laddr *)(void *)(buf +
463 offset += sizeof(struct xsctp_laddr);
464 if (xladdr->last == 1)
468 laddr = calloc(1, sizeof(struct addr));
471 switch (xladdr->address.sa.sa_family) {
473 #define __IN_IS_ADDR_LOOPBACK(pina) \
474 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
475 if (!__IN_IS_ADDR_LOOPBACK(
476 &xladdr->address.sin.sin_addr))
477 local_all_loopback = 0;
478 #undef __IN_IS_ADDR_LOOPBACK
479 sockaddr(&laddr->address, AF_INET,
480 &xladdr->address.sin.sin_addr,
481 htons(xstcb->local_port));
484 if (!IN6_IS_ADDR_LOOPBACK(
485 &xladdr->address.sin6.sin6_addr))
486 local_all_loopback = 0;
487 sockaddr(&laddr->address, AF_INET6,
488 &xladdr->address.sin6.sin6_addr,
489 htons(xstcb->local_port));
493 "address family %d not supported",
494 xladdr->address.sa.sa_family);
497 if (prev_laddr == NULL)
500 prev_laddr->next = laddr;
504 foreign_all_loopback = 1;
505 while (offset < len) {
506 xraddr = (struct xsctp_raddr *)(void *)(buf +
508 offset += sizeof(struct xsctp_raddr);
509 if (xraddr->last == 1)
513 faddr = calloc(1, sizeof(struct addr));
516 switch (xraddr->address.sa.sa_family) {
518 #define __IN_IS_ADDR_LOOPBACK(pina) \
519 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
520 if (!__IN_IS_ADDR_LOOPBACK(
521 &xraddr->address.sin.sin_addr))
522 foreign_all_loopback = 0;
523 #undef __IN_IS_ADDR_LOOPBACK
524 sockaddr(&faddr->address, AF_INET,
525 &xraddr->address.sin.sin_addr,
526 htons(xstcb->remote_port));
529 if (!IN6_IS_ADDR_LOOPBACK(
530 &xraddr->address.sin6.sin6_addr))
531 foreign_all_loopback = 0;
532 sockaddr(&faddr->address, AF_INET6,
533 &xraddr->address.sin6.sin6_addr,
534 htons(xstcb->remote_port));
538 "address family %d not supported",
539 xraddr->address.sa.sa_family);
541 faddr->encaps_port = xraddr->encaps_port;
542 faddr->state = xraddr->state;
544 if (prev_faddr == NULL)
547 prev_faddr->next = faddr;
551 if ((sock->vflag & vflag) &&
553 !(local_all_loopback ||
554 foreign_all_loopback))) {
555 hash = (int)((uintptr_t)sock->socket %
557 sock->next = sockhash[hash];
558 sockhash[hash] = sock;
564 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
565 offset += sizeof(struct xsctp_inpcb);
571 gather_inet(int proto)
573 struct xinpgen *xig, *exig;
575 struct xtcpcb *xtp = NULL;
578 struct addr *laddr, *faddr;
579 const char *varname, *protoname;
582 int hash, retry, vflag;
592 varname = "net.inet.tcp.pcblist";
596 varname = "net.inet.udp.pcblist";
600 varname = "net.inet.divert.pcblist";
604 errx(1, "protocol %d not supported", proto);
612 if ((buf = realloc(buf, bufsize)) == NULL)
615 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
619 if (errno != ENOMEM || len != bufsize)
620 err(1, "sysctlbyname()");
623 xig = (struct xinpgen *)buf;
624 exig = (struct xinpgen *)(void *)
625 ((char *)buf + len - sizeof *exig);
626 if (xig->xig_len != sizeof *xig ||
627 exig->xig_len != sizeof *exig)
628 errx(1, "struct xinpgen size mismatch");
629 } while (xig->xig_gen != exig->xig_gen && retry--);
631 if (xig->xig_gen != exig->xig_gen && opt_v)
632 warnx("warning: data may be inconsistent");
635 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
640 xtp = (struct xtcpcb *)xig;
642 if (xtp->xt_len != sizeof(*xtp)) {
643 warnx("struct xtcpcb size mismatch");
646 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
650 xip = (struct xinpcb *)xig;
651 if (xip->xi_len != sizeof(*xip)) {
652 warnx("struct xinpcb size mismatch");
657 errx(1, "protocol %d not supported", proto);
659 so = &xip->xi_socket;
660 if ((xip->inp_vflag & vflag) == 0)
662 if (xip->inp_vflag & INP_IPV4) {
663 if ((xip->inp_fport == 0 && !opt_l) ||
664 (xip->inp_fport != 0 && !opt_c))
666 #define __IN_IS_ADDR_LOOPBACK(pina) \
667 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
669 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
670 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
672 #undef __IN_IS_ADDR_LOOPBACK
673 } else if (xip->inp_vflag & INP_IPV6) {
674 if ((xip->inp_fport == 0 && !opt_l) ||
675 (xip->inp_fport != 0 && !opt_c))
678 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
679 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
683 warnx("invalid vflag 0x%x", xip->inp_vflag);
686 if ((sock = calloc(1, sizeof(*sock))) == NULL)
688 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
690 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
692 sock->socket = so->xso_so;
694 if (xip->inp_vflag & INP_IPV4) {
695 sock->family = AF_INET;
696 sockaddr(&laddr->address, sock->family,
697 &xip->inp_laddr, xip->inp_lport);
698 sockaddr(&faddr->address, sock->family,
699 &xip->inp_faddr, xip->inp_fport);
700 } else if (xip->inp_vflag & INP_IPV6) {
701 sock->family = AF_INET6;
702 sockaddr(&laddr->address, sock->family,
703 &xip->in6p_laddr, xip->inp_lport);
704 sockaddr(&faddr->address, sock->family,
705 &xip->in6p_faddr, xip->inp_fport);
711 sock->vflag = xip->inp_vflag;
712 if (proto == IPPROTO_TCP) {
713 sock->state = xtp->t_state;
714 memcpy(sock->stack, xtp->xt_stack,
715 TCP_FUNCTION_NAME_LEN_MAX);
717 sock->protoname = protoname;
718 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
719 sock->next = sockhash[hash];
720 sockhash[hash] = sock;
727 gather_unix(int proto)
729 struct xunpgen *xug, *exug;
732 struct addr *laddr, *faddr;
733 const char *varname, *protoname;
740 varname = "net.local.stream.pcblist";
741 protoname = "stream";
744 varname = "net.local.dgram.pcblist";
748 varname = "net.local.seqpacket.pcblist";
749 protoname = "seqpac";
759 if ((buf = realloc(buf, bufsize)) == NULL)
762 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
764 if (errno != ENOMEM || len != bufsize)
765 err(1, "sysctlbyname()");
768 xug = (struct xunpgen *)buf;
769 exug = (struct xunpgen *)(void *)
770 ((char *)buf + len - sizeof(*exug));
771 if (xug->xug_len != sizeof(*xug) ||
772 exug->xug_len != sizeof(*exug)) {
773 warnx("struct xinpgen size mismatch");
776 } while (xug->xug_gen != exug->xug_gen && retry--);
778 if (xug->xug_gen != exug->xug_gen && opt_v)
779 warnx("warning: data may be inconsistent");
782 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
785 xup = (struct xunpcb *)xug;
786 if (xup->xu_len != sizeof(*xup)) {
787 warnx("struct xunpcb size mismatch");
790 if ((xup->unp_conn == NULL && !opt_l) ||
791 (xup->unp_conn != NULL && !opt_c))
793 if ((sock = calloc(1, sizeof(*sock))) == NULL)
795 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
797 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
799 sock->socket = xup->xu_socket.xso_so;
800 sock->pcb = xup->xu_unpp;
802 sock->family = AF_UNIX;
803 sock->protoname = protoname;
804 if (xup->xu_addr.sun_family == AF_UNIX)
806 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
807 else if (xup->unp_conn != NULL)
808 *(void **)&(faddr->address) = xup->unp_conn;
813 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
814 sock->next = sockhash[hash];
815 sockhash[hash] = sock;
826 olen = len = sizeof(*xfiles);
827 if ((xfiles = malloc(len)) == NULL)
829 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
830 if (errno != ENOMEM || len != olen)
831 err(1, "sysctlbyname()");
833 if ((xfiles = realloc(xfiles, len)) == NULL)
836 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
837 errx(1, "struct xfile size mismatch");
838 nxfiles = len / sizeof(*xfiles);
842 printaddr(struct sockaddr_storage *ss)
844 struct sockaddr_un *sun;
845 char addrstr[NI_MAXHOST] = { '\0', '\0' };
846 int error, off, port = 0;
848 switch (ss->ss_family) {
850 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
852 port = ntohs(sstosin(ss)->sin_port);
855 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
857 port = ntohs(sstosin6(ss)->sin6_port);
861 off = (int)((char *)&sun->sun_path - (char *)sun);
862 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
864 if (addrstr[0] == '\0') {
865 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
866 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
868 errx(1, "getnameinfo()");
871 return xprintf("%s:*", addrstr);
873 return xprintf("%s:%d", addrstr, port);
877 getprocname(pid_t pid)
879 static struct kinfo_proc proc;
885 mib[2] = KERN_PROC_PID;
888 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
889 /* Do not warn if the process exits before we get its name. */
894 return (proc.ki_comm);
898 getprocjid(pid_t pid)
900 static struct kinfo_proc proc;
906 mib[2] = KERN_PROC_PID;
909 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
910 /* Do not warn if the process exits before we get its jid. */
915 return (proc.ki_jid);
919 check_ports(struct sock *s)
926 if ((s->family != AF_INET) && (s->family != AF_INET6))
928 for (addr = s->laddr; addr != NULL; addr = addr->next) {
929 if (s->family == AF_INET)
930 port = ntohs(sstosin(&addr->address)->sin_port);
932 port = ntohs(sstosin6(&addr->address)->sin6_port);
936 for (addr = s->faddr; addr != NULL; addr = addr->next) {
937 if (s->family == AF_INET)
938 port = ntohs(sstosin(&addr->address)->sin_port);
940 port = ntohs(sstosin6(&addr->address)->sin6_port);
948 sctp_conn_state(int state)
960 case SCTP_COOKIE_WAIT:
961 return "COOKIE_WAIT";
963 case SCTP_COOKIE_ECHOED:
964 return "COOKIE_ECHOED";
966 case SCTP_ESTABLISHED:
967 return "ESTABLISHED";
969 case SCTP_SHUTDOWN_SENT:
970 return "SHUTDOWN_SENT";
972 case SCTP_SHUTDOWN_RECEIVED:
973 return "SHUTDOWN_RECEIVED";
975 case SCTP_SHUTDOWN_ACK_SENT:
976 return "SHUTDOWN_ACK_SENT";
978 case SCTP_SHUTDOWN_PENDING:
979 return "SHUTDOWN_PENDING";
988 sctp_path_state(int state)
991 case SCTP_UNCONFIRMED:
992 return "UNCONFIRMED";
1007 displaysock(struct sock *s, int pos)
1010 int hash, first, offset;
1011 struct addr *laddr, *faddr;
1015 pos += xprintf(" ");
1016 pos += xprintf("%s", s->protoname);
1017 if (s->vflag & INP_IPV4)
1018 pos += xprintf("4");
1019 if (s->vflag & INP_IPV6)
1020 pos += xprintf("6");
1021 if (s->vflag & (INP_IPV4 | INP_IPV6))
1022 pos += xprintf(" ");
1026 while (laddr != NULL || faddr != NULL) {
1028 while (pos < offset)
1029 pos += xprintf(" ");
1030 switch (s->family) {
1033 if (laddr != NULL) {
1034 pos += printaddr(&laddr->address);
1035 if (s->family == AF_INET6 && pos >= 58)
1036 pos += xprintf(" ");
1038 offset += opt_w ? 46 : 22;
1039 while (pos < offset)
1040 pos += xprintf(" ");
1042 pos += printaddr(&faddr->address);
1043 offset += opt_w ? 46 : 22;
1046 if ((laddr == NULL) || (faddr == NULL))
1047 errx(1, "laddr = %p or faddr = %p is NULL",
1048 (void *)laddr, (void *)faddr);
1050 if (laddr->address.ss_len > 0) {
1051 pos += printaddr(&laddr->address);
1055 p = *(void **)&(faddr->address);
1057 pos += xprintf("(not connected)");
1058 offset += opt_w ? 92 : 44;
1061 pos += xprintf("-> ");
1062 for (hash = 0; hash < HASHSIZE; ++hash) {
1063 for (s_tmp = sockhash[hash];
1065 s_tmp = s_tmp->next)
1066 if (s_tmp->pcb == p)
1071 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1072 s_tmp->laddr->address.ss_len == 0)
1073 pos += xprintf("??");
1075 pos += printaddr(&s_tmp->laddr->address);
1076 offset += opt_w ? 92 : 44;
1082 if (faddr != NULL &&
1083 s->proto == IPPROTO_SCTP &&
1084 s->state != SCTP_CLOSED &&
1085 s->state != SCTP_BOUND &&
1086 s->state != SCTP_LISTEN) {
1087 while (pos < offset)
1088 pos += xprintf(" ");
1089 pos += xprintf("%u",
1090 ntohs(faddr->encaps_port));
1095 if (faddr != NULL &&
1096 s->proto == IPPROTO_SCTP &&
1097 s->state != SCTP_CLOSED &&
1098 s->state != SCTP_BOUND &&
1099 s->state != SCTP_LISTEN) {
1100 while (pos < offset)
1101 pos += xprintf(" ");
1102 pos += xprintf("%s",
1103 sctp_path_state(faddr->state));
1109 if (s->proto == IPPROTO_SCTP ||
1110 s->proto == IPPROTO_TCP) {
1111 while (pos < offset)
1112 pos += xprintf(" ");
1115 pos += xprintf("%s",
1116 sctp_conn_state(s->state));
1119 if (s->state >= 0 &&
1120 s->state < TCP_NSTATES)
1121 pos += xprintf("%s",
1122 tcpstates[s->state]);
1124 pos += xprintf("?");
1130 if (opt_S && s->proto == IPPROTO_TCP) {
1131 while (pos < offset)
1132 pos += xprintf(" ");
1133 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1138 laddr = laddr->next;
1140 faddr = faddr->next;
1141 if ((laddr != NULL) || (faddr != NULL)) {
1158 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1159 "USER", "COMMAND", "PID", "FD", "PROTO",
1160 opt_w ? 45 : 21, "LOCAL ADDRESS",
1161 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1163 printf(" %-6s", "ENCAPS");
1165 printf(" %-12s", "PATH STATE");
1166 printf(" %-12s", "CONN STATE");
1169 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1172 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1173 if (xf->xf_data == NULL)
1175 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1177 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1178 for (s = sockhash[hash]; s != NULL; s = s->next) {
1179 if ((void *)s->socket != xf->xf_data)
1181 if (!check_ports(s))
1185 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1186 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1188 pos += xprintf("%s ", pwd->pw_name);
1190 pos += xprintf(" ");
1191 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1193 pos += xprintf(" ");
1194 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1196 pos += xprintf(" ");
1197 pos += xprintf("%d ", xf->xf_fd);
1198 displaysock(s, pos);
1203 for (hash = 0; hash < HASHSIZE; hash++) {
1204 for (s = sockhash[hash]; s != NULL; s = s->next) {
1207 if (!check_ports(s))
1210 pos += xprintf("%-8s %-10s %-5s %-2s ",
1211 "?", "?", "?", "?");
1212 displaysock(s, pos);
1217 static int set_default_protos(void)
1219 struct protoent *prot;
1223 init_protos(default_numprotos);
1225 for (pindex = 0; pindex < default_numprotos; pindex++) {
1226 pname = default_protos[pindex];
1227 prot = getprotobyname(pname);
1229 err(1, "getprotobyname: %s", pname);
1230 protos[pindex] = prot->p_proto;
1240 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1245 main(int argc, char *argv[])
1247 int protos_defined = -1;
1251 while ((o = getopt(argc, argv, "46cj:Llp:P:SsUuvw")) != -1)
1263 opt_j = atoi(optarg);
1272 parse_ports(optarg);
1275 protos_defined = parse_protos(optarg);
1305 if ((!opt_4 && !opt_6) && protos_defined != -1)
1307 if (!opt_4 && !opt_6 && !opt_u)
1308 opt_4 = opt_6 = opt_u = 1;
1309 if ((opt_4 || opt_6) && protos_defined == -1)
1310 protos_defined = set_default_protos();
1311 if (!opt_c && !opt_l)
1314 if (opt_4 || opt_6) {
1315 for (i = 0; i < protos_defined; i++)
1316 if (protos[i] == IPPROTO_SCTP)
1319 gather_inet(protos[i]);
1322 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1323 gather_unix(SOCK_STREAM);
1324 gather_unix(SOCK_DGRAM);
1325 gather_unix(SOCK_SEQPACKET);