2 * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/socketvar.h>
35 #include <sys/sysctl.h>
41 #include <sys/unpcb.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_pcb.h>
47 #include <netinet/sctp.h>
48 #include <netinet/tcp.h>
49 #define TCPSTATES /* load state names */
50 #include <netinet/tcp_fsm.h>
51 #include <netinet/tcp_seq.h>
52 #include <netinet/tcp_var.h>
53 #include <arpa/inet.h>
66 #define sstosin(ss) ((struct sockaddr_in *)(ss))
67 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
68 #define sstosun(ss) ((struct sockaddr_un *)(ss))
69 #define sstosa(ss) ((struct sockaddr *)(ss))
71 static int opt_4; /* Show IPv4 sockets */
72 static int opt_6; /* Show IPv6 sockets */
73 static int opt_c; /* Show connected sockets */
74 static int opt_j; /* Show specified jail */
75 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
76 static int opt_l; /* Show listening sockets */
77 static int opt_S; /* Show protocol stack if applicable */
78 static int opt_s; /* Show protocol state if applicable */
79 static int opt_U; /* Show remote UDP encapsulation port number */
80 static int opt_u; /* Show Unix domain sockets */
81 static int opt_v; /* Verbose mode */
82 static int opt_w; /* Wide print area for addresses */
85 * Default protocols to use if no -P was defined.
87 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
88 static size_t default_numprotos = nitems(default_protos);
90 static int *protos; /* protocols to use */
91 static size_t numprotos; /* allocated size of protos[] */
95 #define INT_BIT (sizeof(int)*CHAR_BIT)
96 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
97 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
100 struct sockaddr_storage address;
101 unsigned int encaps_port;
114 const char *protoname;
115 char stack[TCP_FUNCTION_NAME_LEN_MAX];
121 #define HASHSIZE 1009
122 static struct sock *sockhash[HASHSIZE];
124 static struct xfile *xfiles;
128 xprintf(const char *fmt, ...)
134 len = vprintf(fmt, ap);
142 get_proto_type(const char *proto)
144 struct protoent *pent;
146 if (strlen(proto) == 0)
148 pent = getprotobyname(proto);
150 warn("getprotobyname");
153 return (pent->p_proto);
164 /* Find the maximum number of possible protocols. */
165 while (getprotoent() != NULL)
170 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
172 numprotos = proto_count;
176 parse_protos(char *protospec)
179 int proto_type, proto_index;
181 if (protospec == NULL)
186 while ((prot = strsep(&protospec, ",")) != NULL) {
187 if (strlen(prot) == 0)
189 proto_type = get_proto_type(prot);
190 if (proto_type != -1)
191 protos[proto_index++] = proto_type;
193 numprotos = proto_index;
194 return (proto_index);
198 parse_ports(const char *portspec)
204 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
209 errx(1, "syntax error in port range");
210 for (q = p; *q != '\0' && isdigit(*q); ++q)
212 for (port = 0; p < q; ++p)
213 port = port * 10 + digittoint(*p);
214 if (port < 0 || port > 65535)
215 errx(1, "invalid port number");
228 for (q = p; *q != '\0' && isdigit(*q); ++q)
230 for (end = 0; p < q; ++p)
231 end = end * 10 + digittoint(*p);
232 if (end < port || end > 65535)
233 errx(1, "invalid port number");
242 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
244 struct sockaddr_in *sin4;
245 struct sockaddr_in6 *sin6;
247 bzero(ss, sizeof(*ss));
251 sin4->sin_len = sizeof(*sin4);
252 sin4->sin_family = af;
253 sin4->sin_port = port;
254 sin4->sin_addr = *(struct in_addr *)addr;
258 sin6->sin6_len = sizeof(*sin6);
259 sin6->sin6_family = af;
260 sin6->sin6_port = port;
261 sin6->sin6_addr = *(struct in6_addr *)addr;
262 #define s6_addr16 __u6_addr.__u6_addr16
263 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
264 sin6->sin6_scope_id =
265 ntohs(sin6->sin6_addr.s6_addr16[1]);
266 sin6->sin6_addr.s6_addr16[1] = 0;
275 free_socket(struct sock *sock)
277 struct addr *cur, *next;
280 while (cur != NULL) {
286 while (cur != NULL) {
298 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
299 struct xsctp_inpcb *xinpcb;
300 struct xsctp_tcb *xstcb;
301 struct xsctp_raddr *xraddr;
302 struct xsctp_laddr *xladdr;
307 int no_stcb, local_all_loopback, foreign_all_loopback;
315 varname = "net.inet.sctp.assoclist";
316 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
318 err(1, "sysctlbyname()");
321 if ((buf = (char *)malloc(len)) == NULL) {
325 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
326 err(1, "sysctlbyname()");
330 xinpcb = (struct xsctp_inpcb *)(void *)buf;
331 offset = sizeof(struct xsctp_inpcb);
332 while ((offset < len) && (xinpcb->last == 0)) {
333 if ((sock = calloc(1, sizeof *sock)) == NULL)
335 sock->socket = xinpcb->socket;
336 sock->proto = IPPROTO_SCTP;
337 sock->protoname = "sctp";
338 if (xinpcb->maxqlen == 0)
339 sock->state = SCTP_CLOSED;
341 sock->state = SCTP_LISTEN;
342 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
343 sock->family = AF_INET6;
345 * Currently there is no way to distinguish between
346 * IPv6 only sockets or dual family sockets.
347 * So mark it as dual socket.
349 sock->vflag = INP_IPV6 | INP_IPV4;
351 sock->family = AF_INET;
352 sock->vflag = INP_IPV4;
355 local_all_loopback = 1;
356 while (offset < len) {
357 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
358 offset += sizeof(struct xsctp_laddr);
359 if (xladdr->last == 1)
361 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
363 switch (xladdr->address.sa.sa_family) {
365 #define __IN_IS_ADDR_LOOPBACK(pina) \
366 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
367 if (!__IN_IS_ADDR_LOOPBACK(
368 &xladdr->address.sin.sin_addr))
369 local_all_loopback = 0;
370 #undef __IN_IS_ADDR_LOOPBACK
371 sockaddr(&laddr->address, AF_INET,
372 &xladdr->address.sin.sin_addr,
373 htons(xinpcb->local_port));
376 if (!IN6_IS_ADDR_LOOPBACK(
377 &xladdr->address.sin6.sin6_addr))
378 local_all_loopback = 0;
379 sockaddr(&laddr->address, AF_INET6,
380 &xladdr->address.sin6.sin6_addr,
381 htons(xinpcb->local_port));
384 errx(1, "address family %d not supported",
385 xladdr->address.sa.sa_family);
388 if (prev_laddr == NULL)
391 prev_laddr->next = laddr;
394 if (sock->laddr == NULL) {
396 calloc(1, sizeof(struct addr))) == NULL)
398 sock->laddr->address.ss_family = sock->family;
399 if (sock->family == AF_INET)
400 sock->laddr->address.ss_len =
401 sizeof(struct sockaddr_in);
403 sock->laddr->address.ss_len =
404 sizeof(struct sockaddr_in6);
405 local_all_loopback = 0;
407 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
409 sock->faddr->address.ss_family = sock->family;
410 if (sock->family == AF_INET)
411 sock->faddr->address.ss_len =
412 sizeof(struct sockaddr_in);
414 sock->faddr->address.ss_len =
415 sizeof(struct sockaddr_in6);
417 while (offset < len) {
418 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
419 offset += sizeof(struct xsctp_tcb);
421 if (opt_l && (sock->vflag & vflag) &&
422 (!opt_L || !local_all_loopback) &&
423 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
424 (xstcb->last == 1))) {
425 hash = (int)((uintptr_t)sock->socket %
427 sock->next = sockhash[hash];
428 sockhash[hash] = sock;
433 if (xstcb->last == 1)
437 if ((sock = calloc(1, sizeof *sock)) == NULL)
439 sock->socket = xinpcb->socket;
440 sock->proto = IPPROTO_SCTP;
441 sock->protoname = "sctp";
442 sock->state = (int)xstcb->state;
443 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
444 sock->family = AF_INET6;
446 * Currently there is no way to distinguish
447 * between IPv6 only sockets or dual family
448 * sockets. So mark it as dual socket.
450 sock->vflag = INP_IPV6 | INP_IPV4;
452 sock->family = AF_INET;
453 sock->vflag = INP_IPV4;
457 local_all_loopback = 1;
458 while (offset < len) {
459 xladdr = (struct xsctp_laddr *)(void *)(buf +
461 offset += sizeof(struct xsctp_laddr);
462 if (xladdr->last == 1)
466 laddr = calloc(1, sizeof(struct addr));
469 switch (xladdr->address.sa.sa_family) {
471 #define __IN_IS_ADDR_LOOPBACK(pina) \
472 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
473 if (!__IN_IS_ADDR_LOOPBACK(
474 &xladdr->address.sin.sin_addr))
475 local_all_loopback = 0;
476 #undef __IN_IS_ADDR_LOOPBACK
477 sockaddr(&laddr->address, AF_INET,
478 &xladdr->address.sin.sin_addr,
479 htons(xstcb->local_port));
482 if (!IN6_IS_ADDR_LOOPBACK(
483 &xladdr->address.sin6.sin6_addr))
484 local_all_loopback = 0;
485 sockaddr(&laddr->address, AF_INET6,
486 &xladdr->address.sin6.sin6_addr,
487 htons(xstcb->local_port));
491 "address family %d not supported",
492 xladdr->address.sa.sa_family);
495 if (prev_laddr == NULL)
498 prev_laddr->next = laddr;
502 foreign_all_loopback = 1;
503 while (offset < len) {
504 xraddr = (struct xsctp_raddr *)(void *)(buf +
506 offset += sizeof(struct xsctp_raddr);
507 if (xraddr->last == 1)
511 faddr = calloc(1, sizeof(struct addr));
514 switch (xraddr->address.sa.sa_family) {
516 #define __IN_IS_ADDR_LOOPBACK(pina) \
517 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
518 if (!__IN_IS_ADDR_LOOPBACK(
519 &xraddr->address.sin.sin_addr))
520 foreign_all_loopback = 0;
521 #undef __IN_IS_ADDR_LOOPBACK
522 sockaddr(&faddr->address, AF_INET,
523 &xraddr->address.sin.sin_addr,
524 htons(xstcb->remote_port));
527 if (!IN6_IS_ADDR_LOOPBACK(
528 &xraddr->address.sin6.sin6_addr))
529 foreign_all_loopback = 0;
530 sockaddr(&faddr->address, AF_INET6,
531 &xraddr->address.sin6.sin6_addr,
532 htons(xstcb->remote_port));
536 "address family %d not supported",
537 xraddr->address.sa.sa_family);
539 faddr->encaps_port = xraddr->encaps_port;
540 faddr->state = xraddr->state;
542 if (prev_faddr == NULL)
545 prev_faddr->next = faddr;
549 if ((sock->vflag & vflag) &&
551 !(local_all_loopback ||
552 foreign_all_loopback))) {
553 hash = (int)((uintptr_t)sock->socket %
555 sock->next = sockhash[hash];
556 sockhash[hash] = sock;
562 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
563 offset += sizeof(struct xsctp_inpcb);
569 gather_inet(int proto)
571 struct xinpgen *xig, *exig;
573 struct xtcpcb *xtp = NULL;
576 struct addr *laddr, *faddr;
577 const char *varname, *protoname;
580 int hash, retry, vflag;
590 varname = "net.inet.tcp.pcblist";
594 varname = "net.inet.udp.pcblist";
598 varname = "net.inet.divert.pcblist";
602 errx(1, "protocol %d not supported", proto);
610 if ((buf = realloc(buf, bufsize)) == NULL)
613 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
617 if (errno != ENOMEM || len != bufsize)
618 err(1, "sysctlbyname()");
621 xig = (struct xinpgen *)buf;
622 exig = (struct xinpgen *)(void *)
623 ((char *)buf + len - sizeof *exig);
624 if (xig->xig_len != sizeof *xig ||
625 exig->xig_len != sizeof *exig)
626 errx(1, "struct xinpgen size mismatch");
627 } while (xig->xig_gen != exig->xig_gen && retry--);
629 if (xig->xig_gen != exig->xig_gen && opt_v)
630 warnx("warning: data may be inconsistent");
633 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
638 xtp = (struct xtcpcb *)xig;
640 if (xtp->xt_len != sizeof(*xtp)) {
641 warnx("struct xtcpcb size mismatch");
644 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
648 xip = (struct xinpcb *)xig;
649 if (xip->xi_len != sizeof(*xip)) {
650 warnx("struct xinpcb size mismatch");
655 errx(1, "protocol %d not supported", proto);
657 so = &xip->xi_socket;
658 if ((xip->inp_vflag & vflag) == 0)
660 if (xip->inp_vflag & INP_IPV4) {
661 if ((xip->inp_fport == 0 && !opt_l) ||
662 (xip->inp_fport != 0 && !opt_c))
664 #define __IN_IS_ADDR_LOOPBACK(pina) \
665 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
667 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
668 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
670 #undef __IN_IS_ADDR_LOOPBACK
671 } else if (xip->inp_vflag & INP_IPV6) {
672 if ((xip->inp_fport == 0 && !opt_l) ||
673 (xip->inp_fport != 0 && !opt_c))
676 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
677 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
681 warnx("invalid vflag 0x%x", xip->inp_vflag);
684 if ((sock = calloc(1, sizeof(*sock))) == NULL)
686 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
688 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
690 sock->socket = so->xso_so;
692 if (xip->inp_vflag & INP_IPV4) {
693 sock->family = AF_INET;
694 sockaddr(&laddr->address, sock->family,
695 &xip->inp_laddr, xip->inp_lport);
696 sockaddr(&faddr->address, sock->family,
697 &xip->inp_faddr, xip->inp_fport);
698 } else if (xip->inp_vflag & INP_IPV6) {
699 sock->family = AF_INET6;
700 sockaddr(&laddr->address, sock->family,
701 &xip->in6p_laddr, xip->inp_lport);
702 sockaddr(&faddr->address, sock->family,
703 &xip->in6p_faddr, xip->inp_fport);
709 sock->vflag = xip->inp_vflag;
710 if (proto == IPPROTO_TCP) {
711 sock->state = xtp->t_state;
712 memcpy(sock->stack, xtp->xt_stack,
713 TCP_FUNCTION_NAME_LEN_MAX);
715 sock->protoname = protoname;
716 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
717 sock->next = sockhash[hash];
718 sockhash[hash] = sock;
725 gather_unix(int proto)
727 struct xunpgen *xug, *exug;
730 struct addr *laddr, *faddr;
731 const char *varname, *protoname;
738 varname = "net.local.stream.pcblist";
739 protoname = "stream";
742 varname = "net.local.dgram.pcblist";
746 varname = "net.local.seqpacket.pcblist";
747 protoname = "seqpac";
757 if ((buf = realloc(buf, bufsize)) == NULL)
760 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
762 if (errno != ENOMEM || len != bufsize)
763 err(1, "sysctlbyname()");
766 xug = (struct xunpgen *)buf;
767 exug = (struct xunpgen *)(void *)
768 ((char *)buf + len - sizeof(*exug));
769 if (xug->xug_len != sizeof(*xug) ||
770 exug->xug_len != sizeof(*exug)) {
771 warnx("struct xinpgen size mismatch");
774 } while (xug->xug_gen != exug->xug_gen && retry--);
776 if (xug->xug_gen != exug->xug_gen && opt_v)
777 warnx("warning: data may be inconsistent");
780 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
783 xup = (struct xunpcb *)xug;
784 if (xup->xu_len != sizeof(*xup)) {
785 warnx("struct xunpcb size mismatch");
788 if ((xup->unp_conn == NULL && !opt_l) ||
789 (xup->unp_conn != NULL && !opt_c))
791 if ((sock = calloc(1, sizeof(*sock))) == NULL)
793 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
795 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
797 sock->socket = xup->xu_socket.xso_so;
798 sock->pcb = xup->xu_unpp;
800 sock->family = AF_UNIX;
801 sock->protoname = protoname;
802 if (xup->xu_addr.sun_family == AF_UNIX)
804 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
805 else if (xup->unp_conn != NULL)
806 *(void **)&(faddr->address) = xup->unp_conn;
811 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
812 sock->next = sockhash[hash];
813 sockhash[hash] = sock;
824 olen = len = sizeof(*xfiles);
825 if ((xfiles = malloc(len)) == NULL)
827 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
828 if (errno != ENOMEM || len != olen)
829 err(1, "sysctlbyname()");
831 if ((xfiles = realloc(xfiles, len)) == NULL)
834 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
835 errx(1, "struct xfile size mismatch");
836 nxfiles = len / sizeof(*xfiles);
840 printaddr(struct sockaddr_storage *ss)
842 struct sockaddr_un *sun;
843 char addrstr[NI_MAXHOST] = { '\0', '\0' };
844 int error, off, port = 0;
846 switch (ss->ss_family) {
848 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
850 port = ntohs(sstosin(ss)->sin_port);
853 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
855 port = ntohs(sstosin6(ss)->sin6_port);
859 off = (int)((char *)&sun->sun_path - (char *)sun);
860 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
862 if (addrstr[0] == '\0') {
863 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
864 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
866 errx(1, "getnameinfo()");
869 return xprintf("%s:*", addrstr);
871 return xprintf("%s:%d", addrstr, port);
875 getprocname(pid_t pid)
877 static struct kinfo_proc proc;
883 mib[2] = KERN_PROC_PID;
886 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
887 /* Do not warn if the process exits before we get its name. */
892 return (proc.ki_comm);
896 getprocjid(pid_t pid)
898 static struct kinfo_proc proc;
904 mib[2] = KERN_PROC_PID;
907 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
908 /* Do not warn if the process exits before we get its jid. */
913 return (proc.ki_jid);
917 check_ports(struct sock *s)
924 if ((s->family != AF_INET) && (s->family != AF_INET6))
926 for (addr = s->laddr; addr != NULL; addr = addr->next) {
927 if (s->family == AF_INET)
928 port = ntohs(sstosin(&addr->address)->sin_port);
930 port = ntohs(sstosin6(&addr->address)->sin6_port);
934 for (addr = s->faddr; 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);
946 sctp_conn_state(int state)
958 case SCTP_COOKIE_WAIT:
959 return "COOKIE_WAIT";
961 case SCTP_COOKIE_ECHOED:
962 return "COOKIE_ECHOED";
964 case SCTP_ESTABLISHED:
965 return "ESTABLISHED";
967 case SCTP_SHUTDOWN_SENT:
968 return "SHUTDOWN_SENT";
970 case SCTP_SHUTDOWN_RECEIVED:
971 return "SHUTDOWN_RECEIVED";
973 case SCTP_SHUTDOWN_ACK_SENT:
974 return "SHUTDOWN_ACK_SENT";
976 case SCTP_SHUTDOWN_PENDING:
977 return "SHUTDOWN_PENDING";
986 sctp_path_state(int state)
989 case SCTP_UNCONFIRMED:
990 return "UNCONFIRMED";
1005 displaysock(struct sock *s, int pos)
1008 int hash, first, offset;
1009 struct addr *laddr, *faddr;
1013 pos += xprintf(" ");
1014 pos += xprintf("%s", s->protoname);
1015 if (s->vflag & INP_IPV4)
1016 pos += xprintf("4");
1017 if (s->vflag & INP_IPV6)
1018 pos += xprintf("6");
1019 if (s->vflag & (INP_IPV4 | INP_IPV6))
1020 pos += xprintf(" ");
1024 while (laddr != NULL || faddr != NULL) {
1026 while (pos < offset)
1027 pos += xprintf(" ");
1028 switch (s->family) {
1031 if (laddr != NULL) {
1032 pos += printaddr(&laddr->address);
1033 if (s->family == AF_INET6 && pos >= 58)
1034 pos += xprintf(" ");
1036 offset += opt_w ? 46 : 22;
1037 while (pos < offset)
1038 pos += xprintf(" ");
1040 pos += printaddr(&faddr->address);
1041 offset += opt_w ? 46 : 22;
1044 if ((laddr == NULL) || (faddr == NULL))
1045 errx(1, "laddr = %p or faddr = %p is NULL",
1046 (void *)laddr, (void *)faddr);
1048 if (laddr->address.ss_len > 0) {
1049 pos += printaddr(&laddr->address);
1053 p = *(void **)&(faddr->address);
1055 pos += xprintf("(not connected)");
1056 offset += opt_w ? 92 : 44;
1059 pos += xprintf("-> ");
1060 for (hash = 0; hash < HASHSIZE; ++hash) {
1061 for (s_tmp = sockhash[hash];
1063 s_tmp = s_tmp->next)
1064 if (s_tmp->pcb == p)
1069 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1070 s_tmp->laddr->address.ss_len == 0)
1071 pos += xprintf("??");
1073 pos += printaddr(&s_tmp->laddr->address);
1074 offset += opt_w ? 92 : 44;
1080 if (faddr != NULL &&
1081 s->proto == IPPROTO_SCTP &&
1082 s->state != SCTP_CLOSED &&
1083 s->state != SCTP_BOUND &&
1084 s->state != SCTP_LISTEN) {
1085 while (pos < offset)
1086 pos += xprintf(" ");
1087 pos += xprintf("%u",
1088 ntohs(faddr->encaps_port));
1093 if (faddr != NULL &&
1094 s->proto == IPPROTO_SCTP &&
1095 s->state != SCTP_CLOSED &&
1096 s->state != SCTP_BOUND &&
1097 s->state != SCTP_LISTEN) {
1098 while (pos < offset)
1099 pos += xprintf(" ");
1100 pos += xprintf("%s",
1101 sctp_path_state(faddr->state));
1107 if (s->proto == IPPROTO_SCTP ||
1108 s->proto == IPPROTO_TCP) {
1109 while (pos < offset)
1110 pos += xprintf(" ");
1113 pos += xprintf("%s",
1114 sctp_conn_state(s->state));
1117 if (s->state >= 0 &&
1118 s->state < TCP_NSTATES)
1119 pos += xprintf("%s",
1120 tcpstates[s->state]);
1122 pos += xprintf("?");
1128 if (opt_S && s->proto == IPPROTO_TCP) {
1129 while (pos < offset)
1130 pos += xprintf(" ");
1131 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1136 laddr = laddr->next;
1138 faddr = faddr->next;
1139 if ((laddr != NULL) || (faddr != NULL)) {
1156 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1157 "USER", "COMMAND", "PID", "FD", "PROTO",
1158 opt_w ? 45 : 21, "LOCAL ADDRESS",
1159 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1161 printf(" %-6s", "ENCAPS");
1163 printf(" %-12s", "PATH STATE");
1164 printf(" %-12s", "CONN STATE");
1167 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1170 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1171 if (xf->xf_data == NULL)
1173 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1175 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1176 for (s = sockhash[hash]; s != NULL; s = s->next) {
1177 if ((void *)s->socket != xf->xf_data)
1179 if (!check_ports(s))
1183 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1184 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1186 pos += xprintf("%s ", pwd->pw_name);
1188 pos += xprintf(" ");
1189 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1191 pos += xprintf(" ");
1192 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1194 pos += xprintf(" ");
1195 pos += xprintf("%d ", xf->xf_fd);
1196 displaysock(s, pos);
1201 for (hash = 0; hash < HASHSIZE; hash++) {
1202 for (s = sockhash[hash]; s != NULL; s = s->next) {
1205 if (!check_ports(s))
1208 pos += xprintf("%-8s %-10s %-5s %-2s ",
1209 "?", "?", "?", "?");
1210 displaysock(s, pos);
1215 static int set_default_protos(void)
1217 struct protoent *prot;
1221 init_protos(default_numprotos);
1223 for (pindex = 0; pindex < default_numprotos; pindex++) {
1224 pname = default_protos[pindex];
1225 prot = getprotobyname(pname);
1227 err(1, "getprotobyname: %s", pname);
1228 protos[pindex] = prot->p_proto;
1238 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1243 main(int argc, char *argv[])
1245 int protos_defined = -1;
1249 while ((o = getopt(argc, argv, "46cj:Llp:P:SsUuvw")) != -1)
1261 opt_j = atoi(optarg);
1270 parse_ports(optarg);
1273 protos_defined = parse_protos(optarg);
1303 if ((!opt_4 && !opt_6) && protos_defined != -1)
1305 if (!opt_4 && !opt_6 && !opt_u)
1306 opt_4 = opt_6 = opt_u = 1;
1307 if ((opt_4 || opt_6) && protos_defined == -1)
1308 protos_defined = set_default_protos();
1309 if (!opt_c && !opt_l)
1312 if (opt_4 || opt_6) {
1313 for (i = 0; i < protos_defined; i++)
1314 if (protos[i] == IPPROTO_SCTP)
1317 gather_inet(protos[i]);
1320 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1321 gather_unix(SOCK_STREAM);
1322 gather_unix(SOCK_DGRAM);
1323 gather_unix(SOCK_SEQPACKET);