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>
40 #include <sys/unpcb.h>
42 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/sctp.h>
47 #include <netinet/tcp.h>
48 #define TCPSTATES /* load state names */
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/tcp_seq.h>
51 #include <netinet/tcp_var.h>
52 #include <arpa/inet.h>
65 #define sstosin(ss) ((struct sockaddr_in *)(ss))
66 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
67 #define sstosun(ss) ((struct sockaddr_un *)(ss))
68 #define sstosa(ss) ((struct sockaddr *)(ss))
70 static int opt_4; /* Show IPv4 sockets */
71 static int opt_6; /* Show IPv6 sockets */
72 static int opt_c; /* Show connected sockets */
73 static int opt_j; /* Show specified jail */
74 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
75 static int opt_l; /* Show listening sockets */
76 static int opt_S; /* Show protocol stack if applicable */
77 static int opt_s; /* Show protocol state if applicable */
78 static int opt_U; /* Show remote UDP encapsulation port number */
79 static int opt_u; /* Show Unix domain sockets */
80 static int opt_v; /* Verbose mode */
81 static int opt_w; /* Wide print area for addresses */
84 * Default protocols to use if no -P was defined.
86 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
87 static size_t default_numprotos = nitems(default_protos);
89 static int *protos; /* protocols to use */
90 static size_t numprotos; /* allocated size of protos[] */
94 #define INT_BIT (sizeof(int)*CHAR_BIT)
95 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
96 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
99 struct sockaddr_storage address;
100 unsigned int encaps_port;
113 const char *protoname;
114 char stack[TCP_FUNCTION_NAME_LEN_MAX];
120 #define HASHSIZE 1009
121 static struct sock *sockhash[HASHSIZE];
123 static struct xfile *xfiles;
127 xprintf(const char *fmt, ...)
133 len = vprintf(fmt, ap);
141 get_proto_type(const char *proto)
143 struct protoent *pent;
145 if (strlen(proto) == 0)
147 pent = getprotobyname(proto);
149 warn("getprotobyname");
152 return (pent->p_proto);
163 /* Find the maximum number of possible protocols. */
164 while (getprotoent() != NULL)
169 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
171 numprotos = proto_count;
175 parse_protos(char *protospec)
178 int proto_type, proto_index;
180 if (protospec == NULL)
185 while ((prot = strsep(&protospec, ",")) != NULL) {
186 if (strlen(prot) == 0)
188 proto_type = get_proto_type(prot);
189 if (proto_type != -1)
190 protos[proto_index++] = proto_type;
192 numprotos = proto_index;
193 return (proto_index);
197 parse_ports(const char *portspec)
203 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
208 errx(1, "syntax error in port range");
209 for (q = p; *q != '\0' && isdigit(*q); ++q)
211 for (port = 0; p < q; ++p)
212 port = port * 10 + digittoint(*p);
213 if (port < 0 || port > 65535)
214 errx(1, "invalid port number");
227 for (q = p; *q != '\0' && isdigit(*q); ++q)
229 for (end = 0; p < q; ++p)
230 end = end * 10 + digittoint(*p);
231 if (end < port || end > 65535)
232 errx(1, "invalid port number");
241 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
243 struct sockaddr_in *sin4;
244 struct sockaddr_in6 *sin6;
246 bzero(ss, sizeof(*ss));
250 sin4->sin_len = sizeof(*sin4);
251 sin4->sin_family = af;
252 sin4->sin_port = port;
253 sin4->sin_addr = *(struct in_addr *)addr;
257 sin6->sin6_len = sizeof(*sin6);
258 sin6->sin6_family = af;
259 sin6->sin6_port = port;
260 sin6->sin6_addr = *(struct in6_addr *)addr;
261 #define s6_addr16 __u6_addr.__u6_addr16
262 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
263 sin6->sin6_scope_id =
264 ntohs(sin6->sin6_addr.s6_addr16[1]);
265 sin6->sin6_addr.s6_addr16[1] = 0;
274 free_socket(struct sock *sock)
276 struct addr *cur, *next;
279 while (cur != NULL) {
285 while (cur != NULL) {
297 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
298 struct xsctp_inpcb *xinpcb;
299 struct xsctp_tcb *xstcb;
300 struct xsctp_raddr *xraddr;
301 struct xsctp_laddr *xladdr;
306 int no_stcb, local_all_loopback, foreign_all_loopback;
314 varname = "net.inet.sctp.assoclist";
315 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
317 err(1, "sysctlbyname()");
320 if ((buf = (char *)malloc(len)) == NULL) {
324 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
325 err(1, "sysctlbyname()");
329 xinpcb = (struct xsctp_inpcb *)(void *)buf;
330 offset = sizeof(struct xsctp_inpcb);
331 while ((offset < len) && (xinpcb->last == 0)) {
332 if ((sock = calloc(1, sizeof *sock)) == NULL)
334 sock->socket = xinpcb->socket;
335 sock->proto = IPPROTO_SCTP;
336 sock->protoname = "sctp";
337 if (xinpcb->maxqlen == 0)
338 sock->state = SCTP_CLOSED;
340 sock->state = SCTP_LISTEN;
341 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
342 sock->family = AF_INET6;
344 * Currently there is no way to distinguish between
345 * IPv6 only sockets or dual family sockets.
346 * So mark it as dual socket.
348 sock->vflag = INP_IPV6 | INP_IPV4;
350 sock->family = AF_INET;
351 sock->vflag = INP_IPV4;
354 local_all_loopback = 1;
355 while (offset < len) {
356 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
357 offset += sizeof(struct xsctp_laddr);
358 if (xladdr->last == 1)
360 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
362 switch (xladdr->address.sa.sa_family) {
364 #define __IN_IS_ADDR_LOOPBACK(pina) \
365 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
366 if (!__IN_IS_ADDR_LOOPBACK(
367 &xladdr->address.sin.sin_addr))
368 local_all_loopback = 0;
369 #undef __IN_IS_ADDR_LOOPBACK
370 sockaddr(&laddr->address, AF_INET,
371 &xladdr->address.sin.sin_addr,
372 htons(xinpcb->local_port));
375 if (!IN6_IS_ADDR_LOOPBACK(
376 &xladdr->address.sin6.sin6_addr))
377 local_all_loopback = 0;
378 sockaddr(&laddr->address, AF_INET6,
379 &xladdr->address.sin6.sin6_addr,
380 htons(xinpcb->local_port));
383 errx(1, "address family %d not supported",
384 xladdr->address.sa.sa_family);
387 if (prev_laddr == NULL)
390 prev_laddr->next = laddr;
393 if (sock->laddr == NULL) {
395 calloc(1, sizeof(struct addr))) == NULL)
397 sock->laddr->address.ss_family = sock->family;
398 if (sock->family == AF_INET)
399 sock->laddr->address.ss_len =
400 sizeof(struct sockaddr_in);
402 sock->laddr->address.ss_len =
403 sizeof(struct sockaddr_in6);
404 local_all_loopback = 0;
406 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
408 sock->faddr->address.ss_family = sock->family;
409 if (sock->family == AF_INET)
410 sock->faddr->address.ss_len =
411 sizeof(struct sockaddr_in);
413 sock->faddr->address.ss_len =
414 sizeof(struct sockaddr_in6);
416 while (offset < len) {
417 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
418 offset += sizeof(struct xsctp_tcb);
420 if (opt_l && (sock->vflag & vflag) &&
421 (!opt_L || !local_all_loopback) &&
422 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
423 (xstcb->last == 1))) {
424 hash = (int)((uintptr_t)sock->socket %
426 sock->next = sockhash[hash];
427 sockhash[hash] = sock;
432 if (xstcb->last == 1)
436 if ((sock = calloc(1, sizeof *sock)) == NULL)
438 sock->socket = xinpcb->socket;
439 sock->proto = IPPROTO_SCTP;
440 sock->protoname = "sctp";
441 sock->state = (int)xstcb->state;
442 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
443 sock->family = AF_INET6;
445 * Currently there is no way to distinguish
446 * between IPv6 only sockets or dual family
447 * sockets. So mark it as dual socket.
449 sock->vflag = INP_IPV6 | INP_IPV4;
451 sock->family = AF_INET;
452 sock->vflag = INP_IPV4;
456 local_all_loopback = 1;
457 while (offset < len) {
458 xladdr = (struct xsctp_laddr *)(void *)(buf +
460 offset += sizeof(struct xsctp_laddr);
461 if (xladdr->last == 1)
465 laddr = calloc(1, sizeof(struct addr));
468 switch (xladdr->address.sa.sa_family) {
470 #define __IN_IS_ADDR_LOOPBACK(pina) \
471 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
472 if (!__IN_IS_ADDR_LOOPBACK(
473 &xladdr->address.sin.sin_addr))
474 local_all_loopback = 0;
475 #undef __IN_IS_ADDR_LOOPBACK
476 sockaddr(&laddr->address, AF_INET,
477 &xladdr->address.sin.sin_addr,
478 htons(xstcb->local_port));
481 if (!IN6_IS_ADDR_LOOPBACK(
482 &xladdr->address.sin6.sin6_addr))
483 local_all_loopback = 0;
484 sockaddr(&laddr->address, AF_INET6,
485 &xladdr->address.sin6.sin6_addr,
486 htons(xstcb->local_port));
490 "address family %d not supported",
491 xladdr->address.sa.sa_family);
494 if (prev_laddr == NULL)
497 prev_laddr->next = laddr;
501 foreign_all_loopback = 1;
502 while (offset < len) {
503 xraddr = (struct xsctp_raddr *)(void *)(buf +
505 offset += sizeof(struct xsctp_raddr);
506 if (xraddr->last == 1)
510 faddr = calloc(1, sizeof(struct addr));
513 switch (xraddr->address.sa.sa_family) {
515 #define __IN_IS_ADDR_LOOPBACK(pina) \
516 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
517 if (!__IN_IS_ADDR_LOOPBACK(
518 &xraddr->address.sin.sin_addr))
519 foreign_all_loopback = 0;
520 #undef __IN_IS_ADDR_LOOPBACK
521 sockaddr(&faddr->address, AF_INET,
522 &xraddr->address.sin.sin_addr,
523 htons(xstcb->remote_port));
526 if (!IN6_IS_ADDR_LOOPBACK(
527 &xraddr->address.sin6.sin6_addr))
528 foreign_all_loopback = 0;
529 sockaddr(&faddr->address, AF_INET6,
530 &xraddr->address.sin6.sin6_addr,
531 htons(xstcb->remote_port));
535 "address family %d not supported",
536 xraddr->address.sa.sa_family);
538 faddr->encaps_port = xraddr->encaps_port;
539 faddr->state = xraddr->state;
541 if (prev_faddr == NULL)
544 prev_faddr->next = faddr;
548 if ((sock->vflag & vflag) &&
550 !(local_all_loopback ||
551 foreign_all_loopback))) {
552 hash = (int)((uintptr_t)sock->socket %
554 sock->next = sockhash[hash];
555 sockhash[hash] = sock;
561 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
562 offset += sizeof(struct xsctp_inpcb);
568 gather_inet(int proto)
570 struct xinpgen *xig, *exig;
572 struct xtcpcb *xtp = NULL;
575 struct addr *laddr, *faddr;
576 const char *varname, *protoname;
579 int hash, retry, vflag;
589 varname = "net.inet.tcp.pcblist";
593 varname = "net.inet.udp.pcblist";
597 varname = "net.inet.divert.pcblist";
601 errx(1, "protocol %d not supported", proto);
609 if ((buf = realloc(buf, bufsize)) == NULL)
612 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
616 if (errno != ENOMEM || len != bufsize)
617 err(1, "sysctlbyname()");
620 xig = (struct xinpgen *)buf;
621 exig = (struct xinpgen *)(void *)
622 ((char *)buf + len - sizeof *exig);
623 if (xig->xig_len != sizeof *xig ||
624 exig->xig_len != sizeof *exig)
625 errx(1, "struct xinpgen size mismatch");
626 } while (xig->xig_gen != exig->xig_gen && retry--);
628 if (xig->xig_gen != exig->xig_gen && opt_v)
629 warnx("warning: data may be inconsistent");
632 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
637 xtp = (struct xtcpcb *)xig;
639 if (xtp->xt_len != sizeof(*xtp)) {
640 warnx("struct xtcpcb size mismatch");
643 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
647 xip = (struct xinpcb *)xig;
648 if (xip->xi_len != sizeof(*xip)) {
649 warnx("struct xinpcb size mismatch");
654 errx(1, "protocol %d not supported", proto);
656 so = &xip->xi_socket;
657 if ((xip->inp_vflag & vflag) == 0)
659 if (xip->inp_vflag & INP_IPV4) {
660 if ((xip->inp_fport == 0 && !opt_l) ||
661 (xip->inp_fport != 0 && !opt_c))
663 #define __IN_IS_ADDR_LOOPBACK(pina) \
664 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
666 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
667 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
669 #undef __IN_IS_ADDR_LOOPBACK
670 } else if (xip->inp_vflag & INP_IPV6) {
671 if ((xip->inp_fport == 0 && !opt_l) ||
672 (xip->inp_fport != 0 && !opt_c))
675 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
676 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
680 warnx("invalid vflag 0x%x", xip->inp_vflag);
683 if ((sock = calloc(1, sizeof(*sock))) == NULL)
685 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
687 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
689 sock->socket = so->xso_so;
691 if (xip->inp_vflag & INP_IPV4) {
692 sock->family = AF_INET;
693 sockaddr(&laddr->address, sock->family,
694 &xip->inp_laddr, xip->inp_lport);
695 sockaddr(&faddr->address, sock->family,
696 &xip->inp_faddr, xip->inp_fport);
697 } else if (xip->inp_vflag & INP_IPV6) {
698 sock->family = AF_INET6;
699 sockaddr(&laddr->address, sock->family,
700 &xip->in6p_laddr, xip->inp_lport);
701 sockaddr(&faddr->address, sock->family,
702 &xip->in6p_faddr, xip->inp_fport);
708 sock->vflag = xip->inp_vflag;
709 if (proto == IPPROTO_TCP) {
710 sock->state = xtp->t_state;
711 memcpy(sock->stack, xtp->xt_stack,
712 TCP_FUNCTION_NAME_LEN_MAX);
714 sock->protoname = protoname;
715 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
716 sock->next = sockhash[hash];
717 sockhash[hash] = sock;
724 gather_unix(int proto)
726 struct xunpgen *xug, *exug;
729 struct addr *laddr, *faddr;
730 const char *varname, *protoname;
737 varname = "net.local.stream.pcblist";
738 protoname = "stream";
741 varname = "net.local.dgram.pcblist";
745 varname = "net.local.seqpacket.pcblist";
746 protoname = "seqpac";
756 if ((buf = realloc(buf, bufsize)) == NULL)
759 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
761 if (errno != ENOMEM || len != bufsize)
762 err(1, "sysctlbyname()");
765 xug = (struct xunpgen *)buf;
766 exug = (struct xunpgen *)(void *)
767 ((char *)buf + len - sizeof(*exug));
768 if (xug->xug_len != sizeof(*xug) ||
769 exug->xug_len != sizeof(*exug)) {
770 warnx("struct xinpgen size mismatch");
773 } while (xug->xug_gen != exug->xug_gen && retry--);
775 if (xug->xug_gen != exug->xug_gen && opt_v)
776 warnx("warning: data may be inconsistent");
779 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
782 xup = (struct xunpcb *)xug;
783 if (xup->xu_len != sizeof(*xup)) {
784 warnx("struct xunpcb size mismatch");
787 if ((xup->xu_unp.unp_conn == NULL && !opt_l) ||
788 (xup->xu_unp.unp_conn != NULL && !opt_c))
790 if ((sock = calloc(1, sizeof(*sock))) == NULL)
792 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
794 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
796 sock->socket = xup->xu_socket.xso_so;
797 sock->pcb = xup->xu_unpp;
799 sock->family = AF_UNIX;
800 sock->protoname = protoname;
801 if (xup->xu_unp.unp_addr != NULL)
803 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
804 else if (xup->xu_unp.unp_conn != NULL)
805 *(void **)&(faddr->address) = xup->xu_unp.unp_conn;
810 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
811 sock->next = sockhash[hash];
812 sockhash[hash] = sock;
823 olen = len = sizeof(*xfiles);
824 if ((xfiles = malloc(len)) == NULL)
826 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
827 if (errno != ENOMEM || len != olen)
828 err(1, "sysctlbyname()");
830 if ((xfiles = realloc(xfiles, len)) == NULL)
833 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
834 errx(1, "struct xfile size mismatch");
835 nxfiles = len / sizeof(*xfiles);
839 printaddr(struct sockaddr_storage *ss)
841 struct sockaddr_un *sun;
842 char addrstr[NI_MAXHOST] = { '\0', '\0' };
843 int error, off, port = 0;
845 switch (ss->ss_family) {
847 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
849 port = ntohs(sstosin(ss)->sin_port);
852 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
854 port = ntohs(sstosin6(ss)->sin6_port);
858 off = (int)((char *)&sun->sun_path - (char *)sun);
859 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
861 if (addrstr[0] == '\0') {
862 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
863 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
865 errx(1, "getnameinfo()");
868 return xprintf("%s:*", addrstr);
870 return xprintf("%s:%d", addrstr, port);
874 getprocname(pid_t pid)
876 static struct kinfo_proc proc;
882 mib[2] = KERN_PROC_PID;
885 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
886 /* Do not warn if the process exits before we get its name. */
891 return (proc.ki_comm);
895 getprocjid(pid_t pid)
897 static struct kinfo_proc proc;
903 mib[2] = KERN_PROC_PID;
906 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
907 /* Do not warn if the process exits before we get its jid. */
912 return (proc.ki_jid);
916 check_ports(struct sock *s)
923 if ((s->family != AF_INET) && (s->family != AF_INET6))
925 for (addr = s->laddr; addr != NULL; addr = addr->next) {
926 if (s->family == AF_INET)
927 port = ntohs(sstosin(&addr->address)->sin_port);
929 port = ntohs(sstosin6(&addr->address)->sin6_port);
933 for (addr = s->faddr; addr != NULL; addr = addr->next) {
934 if (s->family == AF_INET)
935 port = ntohs(sstosin(&addr->address)->sin_port);
937 port = ntohs(sstosin6(&addr->address)->sin6_port);
945 sctp_conn_state(int state)
957 case SCTP_COOKIE_WAIT:
958 return "COOKIE_WAIT";
960 case SCTP_COOKIE_ECHOED:
961 return "COOKIE_ECHOED";
963 case SCTP_ESTABLISHED:
964 return "ESTABLISHED";
966 case SCTP_SHUTDOWN_SENT:
967 return "SHUTDOWN_SENT";
969 case SCTP_SHUTDOWN_RECEIVED:
970 return "SHUTDOWN_RECEIVED";
972 case SCTP_SHUTDOWN_ACK_SENT:
973 return "SHUTDOWN_ACK_SENT";
975 case SCTP_SHUTDOWN_PENDING:
976 return "SHUTDOWN_PENDING";
985 sctp_path_state(int state)
988 case SCTP_UNCONFIRMED:
989 return "UNCONFIRMED";
1004 displaysock(struct sock *s, int pos)
1007 int hash, first, offset;
1008 struct addr *laddr, *faddr;
1012 pos += xprintf(" ");
1013 pos += xprintf("%s", s->protoname);
1014 if (s->vflag & INP_IPV4)
1015 pos += xprintf("4");
1016 if (s->vflag & INP_IPV6)
1017 pos += xprintf("6");
1018 if (s->vflag & (INP_IPV4 | INP_IPV6))
1019 pos += xprintf(" ");
1023 while (laddr != NULL || faddr != NULL) {
1025 while (pos < offset)
1026 pos += xprintf(" ");
1027 switch (s->family) {
1030 if (laddr != NULL) {
1031 pos += printaddr(&laddr->address);
1032 if (s->family == AF_INET6 && pos >= 58)
1033 pos += xprintf(" ");
1035 offset += opt_w ? 46 : 22;
1036 while (pos < offset)
1037 pos += xprintf(" ");
1039 pos += printaddr(&faddr->address);
1040 offset += opt_w ? 46 : 22;
1043 if ((laddr == NULL) || (faddr == NULL))
1044 errx(1, "laddr = %p or faddr = %p is NULL",
1045 (void *)laddr, (void *)faddr);
1047 if (laddr->address.ss_len > 0) {
1048 pos += printaddr(&laddr->address);
1052 p = *(void **)&(faddr->address);
1054 pos += xprintf("(not connected)");
1055 offset += opt_w ? 92 : 44;
1058 pos += xprintf("-> ");
1059 for (hash = 0; hash < HASHSIZE; ++hash) {
1060 for (s_tmp = sockhash[hash];
1062 s_tmp = s_tmp->next)
1063 if (s_tmp->pcb == p)
1068 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1069 s_tmp->laddr->address.ss_len == 0)
1070 pos += xprintf("??");
1072 pos += printaddr(&s_tmp->laddr->address);
1073 offset += opt_w ? 92 : 44;
1079 if (faddr != NULL &&
1080 s->proto == IPPROTO_SCTP &&
1081 s->state != SCTP_CLOSED &&
1082 s->state != SCTP_BOUND &&
1083 s->state != SCTP_LISTEN) {
1084 while (pos < offset)
1085 pos += xprintf(" ");
1086 pos += xprintf("%u",
1087 ntohs(faddr->encaps_port));
1092 if (faddr != NULL &&
1093 s->proto == IPPROTO_SCTP &&
1094 s->state != SCTP_CLOSED &&
1095 s->state != SCTP_BOUND &&
1096 s->state != SCTP_LISTEN) {
1097 while (pos < offset)
1098 pos += xprintf(" ");
1099 pos += xprintf("%s",
1100 sctp_path_state(faddr->state));
1106 if (s->proto == IPPROTO_SCTP ||
1107 s->proto == IPPROTO_TCP) {
1108 while (pos < offset)
1109 pos += xprintf(" ");
1112 pos += xprintf("%s",
1113 sctp_conn_state(s->state));
1116 if (s->state >= 0 &&
1117 s->state < TCP_NSTATES)
1118 pos += xprintf("%s",
1119 tcpstates[s->state]);
1121 pos += xprintf("?");
1127 if (opt_S && s->proto == IPPROTO_TCP) {
1128 while (pos < offset)
1129 pos += xprintf(" ");
1130 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1135 laddr = laddr->next;
1137 faddr = faddr->next;
1138 if ((laddr != NULL) || (faddr != NULL)) {
1155 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1156 "USER", "COMMAND", "PID", "FD", "PROTO",
1157 opt_w ? 45 : 21, "LOCAL ADDRESS",
1158 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1160 printf(" %-6s", "ENCAPS");
1162 printf(" %-12s", "PATH STATE");
1163 printf(" %-12s", "CONN STATE");
1166 printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1169 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1170 if (xf->xf_data == NULL)
1172 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1174 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1175 for (s = sockhash[hash]; s != NULL; s = s->next) {
1176 if ((void *)s->socket != xf->xf_data)
1178 if (!check_ports(s))
1182 if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1183 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1185 pos += xprintf("%s ", pwd->pw_name);
1187 pos += xprintf(" ");
1188 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1190 pos += xprintf(" ");
1191 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1193 pos += xprintf(" ");
1194 pos += xprintf("%d ", xf->xf_fd);
1195 displaysock(s, pos);
1200 for (hash = 0; hash < HASHSIZE; hash++) {
1201 for (s = sockhash[hash]; s != NULL; s = s->next) {
1204 if (!check_ports(s))
1207 pos += xprintf("%-8s %-10s %-5s %-2s ",
1208 "?", "?", "?", "?");
1209 displaysock(s, pos);
1214 static int set_default_protos(void)
1216 struct protoent *prot;
1220 init_protos(default_numprotos);
1222 for (pindex = 0; pindex < default_numprotos; pindex++) {
1223 pname = default_protos[pindex];
1224 prot = getprotobyname(pname);
1226 err(1, "getprotobyname: %s", pname);
1227 protos[pindex] = prot->p_proto;
1237 "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1242 main(int argc, char *argv[])
1244 int protos_defined = -1;
1248 while ((o = getopt(argc, argv, "46cj:Llp:P:SsUuvw")) != -1)
1260 opt_j = atoi(optarg);
1269 parse_ports(optarg);
1272 protos_defined = parse_protos(optarg);
1302 if ((!opt_4 && !opt_6) && protos_defined != -1)
1304 if (!opt_4 && !opt_6 && !opt_u)
1305 opt_4 = opt_6 = opt_u = 1;
1306 if ((opt_4 || opt_6) && protos_defined == -1)
1307 protos_defined = set_default_protos();
1308 if (!opt_c && !opt_l)
1311 if (opt_4 || opt_6) {
1312 for (i = 0; i < protos_defined; i++)
1313 if (protos[i] == IPPROTO_SCTP)
1316 gather_inet(protos[i]);
1319 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1320 gather_unix(SOCK_STREAM);
1321 gather_unix(SOCK_DGRAM);
1322 gather_unix(SOCK_SEQPACKET);