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_q; /* Don't show header */
80 static int opt_S; /* Show protocol stack if applicable */
81 static int opt_s; /* Show protocol state if applicable */
82 static int opt_U; /* Show remote UDP encapsulation port number */
83 static int opt_u; /* Show Unix domain sockets */
84 static int opt_v; /* Verbose mode */
85 static int opt_w; /* Wide print area for addresses */
88 * Default protocols to use if no -P was defined.
90 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
91 static size_t default_numprotos = nitems(default_protos);
93 static int *protos; /* protocols to use */
94 static size_t numprotos; /* allocated size of protos[] */
98 #define INT_BIT (sizeof(int)*CHAR_BIT)
99 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
100 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
103 struct sockaddr_storage address;
104 unsigned int encaps_port;
117 const char *protoname;
118 char stack[TCP_FUNCTION_NAME_LEN_MAX];
124 #define HASHSIZE 1009
125 static struct sock *sockhash[HASHSIZE];
127 static struct xfile *xfiles;
131 xprintf(const char *fmt, ...)
137 len = vprintf(fmt, ap);
145 get_proto_type(const char *proto)
147 struct protoent *pent;
149 if (strlen(proto) == 0)
151 pent = getprotobyname(proto);
153 warn("getprotobyname");
156 return (pent->p_proto);
167 /* Find the maximum number of possible protocols. */
168 while (getprotoent() != NULL)
173 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
175 numprotos = proto_count;
179 parse_protos(char *protospec)
182 int proto_type, proto_index;
184 if (protospec == NULL)
189 while ((prot = strsep(&protospec, ",")) != NULL) {
190 if (strlen(prot) == 0)
192 proto_type = get_proto_type(prot);
193 if (proto_type != -1)
194 protos[proto_index++] = proto_type;
196 numprotos = proto_index;
197 return (proto_index);
201 parse_ports(const char *portspec)
207 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
212 errx(1, "syntax error in port range");
213 for (q = p; *q != '\0' && isdigit(*q); ++q)
215 for (port = 0; p < q; ++p)
216 port = port * 10 + digittoint(*p);
217 if (port < 0 || port > 65535)
218 errx(1, "invalid port number");
231 for (q = p; *q != '\0' && isdigit(*q); ++q)
233 for (end = 0; p < q; ++p)
234 end = end * 10 + digittoint(*p);
235 if (end < port || end > 65535)
236 errx(1, "invalid port number");
245 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
247 struct sockaddr_in *sin4;
248 struct sockaddr_in6 *sin6;
250 bzero(ss, sizeof(*ss));
254 sin4->sin_len = sizeof(*sin4);
255 sin4->sin_family = af;
256 sin4->sin_port = port;
257 sin4->sin_addr = *(struct in_addr *)addr;
261 sin6->sin6_len = sizeof(*sin6);
262 sin6->sin6_family = af;
263 sin6->sin6_port = port;
264 sin6->sin6_addr = *(struct in6_addr *)addr;
265 #define s6_addr16 __u6_addr.__u6_addr16
266 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
267 sin6->sin6_scope_id =
268 ntohs(sin6->sin6_addr.s6_addr16[1]);
269 sin6->sin6_addr.s6_addr16[1] = 0;
278 free_socket(struct sock *sock)
280 struct addr *cur, *next;
283 while (cur != NULL) {
289 while (cur != NULL) {
301 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
302 struct xsctp_inpcb *xinpcb;
303 struct xsctp_tcb *xstcb;
304 struct xsctp_raddr *xraddr;
305 struct xsctp_laddr *xladdr;
310 int no_stcb, local_all_loopback, foreign_all_loopback;
318 varname = "net.inet.sctp.assoclist";
319 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
321 err(1, "sysctlbyname()");
324 if ((buf = (char *)malloc(len)) == NULL) {
328 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
329 err(1, "sysctlbyname()");
333 xinpcb = (struct xsctp_inpcb *)(void *)buf;
334 offset = sizeof(struct xsctp_inpcb);
335 while ((offset < len) && (xinpcb->last == 0)) {
336 if ((sock = calloc(1, sizeof *sock)) == NULL)
338 sock->socket = xinpcb->socket;
339 sock->proto = IPPROTO_SCTP;
340 sock->protoname = "sctp";
341 if (xinpcb->maxqlen == 0)
342 sock->state = SCTP_CLOSED;
344 sock->state = SCTP_LISTEN;
345 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
346 sock->family = AF_INET6;
348 * Currently there is no way to distinguish between
349 * IPv6 only sockets or dual family sockets.
350 * So mark it as dual socket.
352 sock->vflag = INP_IPV6 | INP_IPV4;
354 sock->family = AF_INET;
355 sock->vflag = INP_IPV4;
358 local_all_loopback = 1;
359 while (offset < len) {
360 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
361 offset += sizeof(struct xsctp_laddr);
362 if (xladdr->last == 1)
364 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
366 switch (xladdr->address.sa.sa_family) {
368 #define __IN_IS_ADDR_LOOPBACK(pina) \
369 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
370 if (!__IN_IS_ADDR_LOOPBACK(
371 &xladdr->address.sin.sin_addr))
372 local_all_loopback = 0;
373 #undef __IN_IS_ADDR_LOOPBACK
374 sockaddr(&laddr->address, AF_INET,
375 &xladdr->address.sin.sin_addr,
376 htons(xinpcb->local_port));
379 if (!IN6_IS_ADDR_LOOPBACK(
380 &xladdr->address.sin6.sin6_addr))
381 local_all_loopback = 0;
382 sockaddr(&laddr->address, AF_INET6,
383 &xladdr->address.sin6.sin6_addr,
384 htons(xinpcb->local_port));
387 errx(1, "address family %d not supported",
388 xladdr->address.sa.sa_family);
391 if (prev_laddr == NULL)
394 prev_laddr->next = laddr;
397 if (sock->laddr == NULL) {
399 calloc(1, sizeof(struct addr))) == NULL)
401 sock->laddr->address.ss_family = sock->family;
402 if (sock->family == AF_INET)
403 sock->laddr->address.ss_len =
404 sizeof(struct sockaddr_in);
406 sock->laddr->address.ss_len =
407 sizeof(struct sockaddr_in6);
408 local_all_loopback = 0;
410 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
412 sock->faddr->address.ss_family = sock->family;
413 if (sock->family == AF_INET)
414 sock->faddr->address.ss_len =
415 sizeof(struct sockaddr_in);
417 sock->faddr->address.ss_len =
418 sizeof(struct sockaddr_in6);
420 while (offset < len) {
421 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
422 offset += sizeof(struct xsctp_tcb);
424 if (opt_l && (sock->vflag & vflag) &&
425 (!opt_L || !local_all_loopback) &&
426 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
427 (xstcb->last == 1))) {
428 hash = (int)((uintptr_t)sock->socket %
430 sock->next = sockhash[hash];
431 sockhash[hash] = sock;
436 if (xstcb->last == 1)
440 if ((sock = calloc(1, sizeof *sock)) == NULL)
442 sock->socket = xinpcb->socket;
443 sock->proto = IPPROTO_SCTP;
444 sock->protoname = "sctp";
445 sock->state = (int)xstcb->state;
446 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
447 sock->family = AF_INET6;
449 * Currently there is no way to distinguish
450 * between IPv6 only sockets or dual family
451 * sockets. So mark it as dual socket.
453 sock->vflag = INP_IPV6 | INP_IPV4;
455 sock->family = AF_INET;
456 sock->vflag = INP_IPV4;
460 local_all_loopback = 1;
461 while (offset < len) {
462 xladdr = (struct xsctp_laddr *)(void *)(buf +
464 offset += sizeof(struct xsctp_laddr);
465 if (xladdr->last == 1)
469 laddr = calloc(1, sizeof(struct addr));
472 switch (xladdr->address.sa.sa_family) {
474 #define __IN_IS_ADDR_LOOPBACK(pina) \
475 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
476 if (!__IN_IS_ADDR_LOOPBACK(
477 &xladdr->address.sin.sin_addr))
478 local_all_loopback = 0;
479 #undef __IN_IS_ADDR_LOOPBACK
480 sockaddr(&laddr->address, AF_INET,
481 &xladdr->address.sin.sin_addr,
482 htons(xstcb->local_port));
485 if (!IN6_IS_ADDR_LOOPBACK(
486 &xladdr->address.sin6.sin6_addr))
487 local_all_loopback = 0;
488 sockaddr(&laddr->address, AF_INET6,
489 &xladdr->address.sin6.sin6_addr,
490 htons(xstcb->local_port));
494 "address family %d not supported",
495 xladdr->address.sa.sa_family);
498 if (prev_laddr == NULL)
501 prev_laddr->next = laddr;
505 foreign_all_loopback = 1;
506 while (offset < len) {
507 xraddr = (struct xsctp_raddr *)(void *)(buf +
509 offset += sizeof(struct xsctp_raddr);
510 if (xraddr->last == 1)
514 faddr = calloc(1, sizeof(struct addr));
517 switch (xraddr->address.sa.sa_family) {
519 #define __IN_IS_ADDR_LOOPBACK(pina) \
520 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
521 if (!__IN_IS_ADDR_LOOPBACK(
522 &xraddr->address.sin.sin_addr))
523 foreign_all_loopback = 0;
524 #undef __IN_IS_ADDR_LOOPBACK
525 sockaddr(&faddr->address, AF_INET,
526 &xraddr->address.sin.sin_addr,
527 htons(xstcb->remote_port));
530 if (!IN6_IS_ADDR_LOOPBACK(
531 &xraddr->address.sin6.sin6_addr))
532 foreign_all_loopback = 0;
533 sockaddr(&faddr->address, AF_INET6,
534 &xraddr->address.sin6.sin6_addr,
535 htons(xstcb->remote_port));
539 "address family %d not supported",
540 xraddr->address.sa.sa_family);
542 faddr->encaps_port = xraddr->encaps_port;
543 faddr->state = xraddr->state;
545 if (prev_faddr == NULL)
548 prev_faddr->next = faddr;
552 if ((sock->vflag & vflag) &&
554 !(local_all_loopback ||
555 foreign_all_loopback))) {
556 hash = (int)((uintptr_t)sock->socket %
558 sock->next = sockhash[hash];
559 sockhash[hash] = sock;
565 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
566 offset += sizeof(struct xsctp_inpcb);
572 gather_inet(int proto)
574 struct xinpgen *xig, *exig;
576 struct xtcpcb *xtp = NULL;
579 struct addr *laddr, *faddr;
580 const char *varname, *protoname;
583 int hash, retry, vflag;
593 varname = "net.inet.tcp.pcblist";
597 varname = "net.inet.udp.pcblist";
601 varname = "net.inet.divert.pcblist";
605 errx(1, "protocol %d not supported", proto);
613 if ((buf = realloc(buf, bufsize)) == NULL)
616 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
620 if (errno != ENOMEM || len != bufsize)
621 err(1, "sysctlbyname()");
624 xig = (struct xinpgen *)buf;
625 exig = (struct xinpgen *)(void *)
626 ((char *)buf + len - sizeof *exig);
627 if (xig->xig_len != sizeof *xig ||
628 exig->xig_len != sizeof *exig)
629 errx(1, "struct xinpgen size mismatch");
630 } while (xig->xig_gen != exig->xig_gen && retry--);
632 if (xig->xig_gen != exig->xig_gen && opt_v)
633 warnx("warning: data may be inconsistent");
636 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
641 xtp = (struct xtcpcb *)xig;
643 if (xtp->xt_len != sizeof(*xtp)) {
644 warnx("struct xtcpcb size mismatch");
647 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
651 xip = (struct xinpcb *)xig;
652 if (xip->xi_len != sizeof(*xip)) {
653 warnx("struct xinpcb size mismatch");
658 errx(1, "protocol %d not supported", proto);
660 so = &xip->xi_socket;
661 if ((xip->inp_vflag & vflag) == 0)
663 if (xip->inp_vflag & INP_IPV4) {
664 if ((xip->inp_fport == 0 && !opt_l) ||
665 (xip->inp_fport != 0 && !opt_c))
667 #define __IN_IS_ADDR_LOOPBACK(pina) \
668 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
670 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
671 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
673 #undef __IN_IS_ADDR_LOOPBACK
674 } else if (xip->inp_vflag & INP_IPV6) {
675 if ((xip->inp_fport == 0 && !opt_l) ||
676 (xip->inp_fport != 0 && !opt_c))
679 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
680 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
684 warnx("invalid vflag 0x%x", xip->inp_vflag);
687 if ((sock = calloc(1, sizeof(*sock))) == NULL)
689 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
691 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
693 sock->socket = so->xso_so;
695 if (xip->inp_vflag & INP_IPV4) {
696 sock->family = AF_INET;
697 sockaddr(&laddr->address, sock->family,
698 &xip->inp_laddr, xip->inp_lport);
699 sockaddr(&faddr->address, sock->family,
700 &xip->inp_faddr, xip->inp_fport);
701 } else if (xip->inp_vflag & INP_IPV6) {
702 sock->family = AF_INET6;
703 sockaddr(&laddr->address, sock->family,
704 &xip->in6p_laddr, xip->inp_lport);
705 sockaddr(&faddr->address, sock->family,
706 &xip->in6p_faddr, xip->inp_fport);
712 sock->vflag = xip->inp_vflag;
713 if (proto == IPPROTO_TCP) {
714 sock->state = xtp->t_state;
715 memcpy(sock->stack, xtp->xt_stack,
716 TCP_FUNCTION_NAME_LEN_MAX);
718 sock->protoname = protoname;
719 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
720 sock->next = sockhash[hash];
721 sockhash[hash] = sock;
728 gather_unix(int proto)
730 struct xunpgen *xug, *exug;
733 struct addr *laddr, *faddr;
734 const char *varname, *protoname;
741 varname = "net.local.stream.pcblist";
742 protoname = "stream";
745 varname = "net.local.dgram.pcblist";
749 varname = "net.local.seqpacket.pcblist";
750 protoname = "seqpac";
760 if ((buf = realloc(buf, bufsize)) == NULL)
763 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
765 if (errno != ENOMEM || len != bufsize)
766 err(1, "sysctlbyname()");
769 xug = (struct xunpgen *)buf;
770 exug = (struct xunpgen *)(void *)
771 ((char *)buf + len - sizeof(*exug));
772 if (xug->xug_len != sizeof(*xug) ||
773 exug->xug_len != sizeof(*exug)) {
774 warnx("struct xinpgen size mismatch");
777 } while (xug->xug_gen != exug->xug_gen && retry--);
779 if (xug->xug_gen != exug->xug_gen && opt_v)
780 warnx("warning: data may be inconsistent");
783 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
786 xup = (struct xunpcb *)xug;
787 if (xup->xu_len != sizeof(*xup)) {
788 warnx("struct xunpcb size mismatch");
791 if ((xup->unp_conn == NULL && !opt_l) ||
792 (xup->unp_conn != NULL && !opt_c))
794 if ((sock = calloc(1, sizeof(*sock))) == NULL)
796 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
798 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
800 sock->socket = xup->xu_socket.xso_so;
801 sock->pcb = xup->xu_unpp;
803 sock->family = AF_UNIX;
804 sock->protoname = protoname;
805 if (xup->xu_addr.sun_family == AF_UNIX)
807 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
808 else if (xup->unp_conn != NULL)
809 *(void **)&(faddr->address) = xup->unp_conn;
814 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
815 sock->next = sockhash[hash];
816 sockhash[hash] = sock;
827 olen = len = sizeof(*xfiles);
828 if ((xfiles = malloc(len)) == NULL)
830 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
831 if (errno != ENOMEM || len != olen)
832 err(1, "sysctlbyname()");
834 if ((xfiles = realloc(xfiles, len)) == NULL)
837 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
838 errx(1, "struct xfile size mismatch");
839 nxfiles = len / sizeof(*xfiles);
843 printaddr(struct sockaddr_storage *ss)
845 struct sockaddr_un *sun;
846 char addrstr[NI_MAXHOST] = { '\0', '\0' };
847 int error, off, port = 0;
849 switch (ss->ss_family) {
851 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
853 port = ntohs(sstosin(ss)->sin_port);
856 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
858 port = ntohs(sstosin6(ss)->sin6_port);
862 off = (int)((char *)&sun->sun_path - (char *)sun);
863 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
865 if (addrstr[0] == '\0') {
866 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
867 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
869 errx(1, "getnameinfo()");
872 return xprintf("%s:*", addrstr);
874 return xprintf("%s:%d", addrstr, port);
878 getprocname(pid_t pid)
880 static struct kinfo_proc proc;
886 mib[2] = KERN_PROC_PID;
889 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
890 /* Do not warn if the process exits before we get its name. */
895 return (proc.ki_comm);
899 getprocjid(pid_t pid)
901 static struct kinfo_proc proc;
907 mib[2] = KERN_PROC_PID;
910 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
911 /* Do not warn if the process exits before we get its jid. */
916 return (proc.ki_jid);
920 check_ports(struct sock *s)
927 if ((s->family != AF_INET) && (s->family != AF_INET6))
929 for (addr = s->laddr; addr != NULL; addr = addr->next) {
930 if (s->family == AF_INET)
931 port = ntohs(sstosin(&addr->address)->sin_port);
933 port = ntohs(sstosin6(&addr->address)->sin6_port);
937 for (addr = s->faddr; addr != NULL; addr = addr->next) {
938 if (s->family == AF_INET)
939 port = ntohs(sstosin(&addr->address)->sin_port);
941 port = ntohs(sstosin6(&addr->address)->sin6_port);
949 sctp_conn_state(int state)
961 case SCTP_COOKIE_WAIT:
962 return "COOKIE_WAIT";
964 case SCTP_COOKIE_ECHOED:
965 return "COOKIE_ECHOED";
967 case SCTP_ESTABLISHED:
968 return "ESTABLISHED";
970 case SCTP_SHUTDOWN_SENT:
971 return "SHUTDOWN_SENT";
973 case SCTP_SHUTDOWN_RECEIVED:
974 return "SHUTDOWN_RECEIVED";
976 case SCTP_SHUTDOWN_ACK_SENT:
977 return "SHUTDOWN_ACK_SENT";
979 case SCTP_SHUTDOWN_PENDING:
980 return "SHUTDOWN_PENDING";
989 sctp_path_state(int state)
992 case SCTP_UNCONFIRMED:
993 return "UNCONFIRMED";
1008 displaysock(struct sock *s, int pos)
1011 int hash, first, offset;
1012 struct addr *laddr, *faddr;
1016 pos += xprintf(" ");
1017 pos += xprintf("%s", s->protoname);
1018 if (s->vflag & INP_IPV4)
1019 pos += xprintf("4");
1020 if (s->vflag & INP_IPV6)
1021 pos += xprintf("6");
1022 if (s->vflag & (INP_IPV4 | INP_IPV6))
1023 pos += xprintf(" ");
1027 while (laddr != NULL || faddr != NULL) {
1029 while (pos < offset)
1030 pos += xprintf(" ");
1031 switch (s->family) {
1034 if (laddr != NULL) {
1035 pos += printaddr(&laddr->address);
1036 if (s->family == AF_INET6 && pos >= 58)
1037 pos += xprintf(" ");
1039 offset += opt_w ? 46 : 22;
1040 while (pos < offset)
1041 pos += xprintf(" ");
1043 pos += printaddr(&faddr->address);
1044 offset += opt_w ? 46 : 22;
1047 if ((laddr == NULL) || (faddr == NULL))
1048 errx(1, "laddr = %p or faddr = %p is NULL",
1049 (void *)laddr, (void *)faddr);
1051 if (laddr->address.ss_len > 0) {
1052 pos += printaddr(&laddr->address);
1056 p = *(void **)&(faddr->address);
1058 pos += xprintf("(not connected)");
1059 offset += opt_w ? 92 : 44;
1062 pos += xprintf("-> ");
1063 for (hash = 0; hash < HASHSIZE; ++hash) {
1064 for (s_tmp = sockhash[hash];
1066 s_tmp = s_tmp->next)
1067 if (s_tmp->pcb == p)
1072 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1073 s_tmp->laddr->address.ss_len == 0)
1074 pos += xprintf("??");
1076 pos += printaddr(&s_tmp->laddr->address);
1077 offset += opt_w ? 92 : 44;
1083 if (faddr != NULL &&
1084 s->proto == IPPROTO_SCTP &&
1085 s->state != SCTP_CLOSED &&
1086 s->state != SCTP_BOUND &&
1087 s->state != SCTP_LISTEN) {
1088 while (pos < offset)
1089 pos += xprintf(" ");
1090 pos += xprintf("%u",
1091 ntohs(faddr->encaps_port));
1096 if (faddr != NULL &&
1097 s->proto == IPPROTO_SCTP &&
1098 s->state != SCTP_CLOSED &&
1099 s->state != SCTP_BOUND &&
1100 s->state != SCTP_LISTEN) {
1101 while (pos < offset)
1102 pos += xprintf(" ");
1103 pos += xprintf("%s",
1104 sctp_path_state(faddr->state));
1110 if (s->proto == IPPROTO_SCTP ||
1111 s->proto == IPPROTO_TCP) {
1112 while (pos < offset)
1113 pos += xprintf(" ");
1116 pos += xprintf("%s",
1117 sctp_conn_state(s->state));
1120 if (s->state >= 0 &&
1121 s->state < TCP_NSTATES)
1122 pos += xprintf("%s",
1123 tcpstates[s->state]);
1125 pos += xprintf("?");
1131 if (opt_S && s->proto == IPPROTO_TCP) {
1132 while (pos < offset)
1133 pos += xprintf(" ");
1134 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1139 laddr = laddr->next;
1141 faddr = faddr->next;
1142 if ((laddr != NULL) || (faddr != NULL)) {
1160 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1161 "USER", "COMMAND", "PID", "FD", "PROTO",
1162 opt_w ? 45 : 21, "LOCAL ADDRESS",
1163 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1165 printf(" %-6s", "ENCAPS");
1167 printf(" %-12s", "PATH STATE");
1168 printf(" %-12s", "CONN STATE");
1171 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1175 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1176 if (xf->xf_data == NULL)
1178 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1180 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1181 for (s = sockhash[hash]; s != NULL; s = s->next) {
1182 if ((void *)s->socket != xf->xf_data)
1184 if (!check_ports(s))
1188 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1189 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1191 pos += xprintf("%s ", pwd->pw_name);
1193 pos += xprintf(" ");
1194 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1196 pos += xprintf(" ");
1197 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1199 pos += xprintf(" ");
1200 pos += xprintf("%d ", xf->xf_fd);
1201 displaysock(s, pos);
1206 for (hash = 0; hash < HASHSIZE; hash++) {
1207 for (s = sockhash[hash]; s != NULL; s = s->next) {
1210 if (!check_ports(s))
1213 pos += xprintf("%-8s %-10s %-5s %-2s ",
1214 "?", "?", "?", "?");
1215 displaysock(s, pos);
1220 static int set_default_protos(void)
1222 struct protoent *prot;
1226 init_protos(default_numprotos);
1228 for (pindex = 0; pindex < default_numprotos; pindex++) {
1229 pname = default_protos[pindex];
1230 prot = getprotobyname(pname);
1232 err(1, "getprotobyname: %s", pname);
1233 protos[pindex] = prot->p_proto;
1243 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1248 main(int argc, char *argv[])
1250 int protos_defined = -1;
1254 while ((o = getopt(argc, argv, "46cj:Llp:P:qSsUuvw")) != -1)
1266 opt_j = atoi(optarg);
1275 parse_ports(optarg);
1278 protos_defined = parse_protos(optarg);
1311 if ((!opt_4 && !opt_6) && protos_defined != -1)
1313 if (!opt_4 && !opt_6 && !opt_u)
1314 opt_4 = opt_6 = opt_u = 1;
1315 if ((opt_4 || opt_6) && protos_defined == -1)
1316 protos_defined = set_default_protos();
1317 if (!opt_c && !opt_l)
1320 if (opt_4 || opt_6) {
1321 for (i = 0; i < protos_defined; i++)
1322 if (protos[i] == IPPROTO_SCTP)
1325 gather_inet(protos[i]);
1328 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1329 gather_unix(SOCK_STREAM);
1330 gather_unix(SOCK_DGRAM);
1331 gather_unix(SOCK_SEQPACKET);