]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/sockstat/sockstat.c
Import Intel Processor Trace decoder library from
[FreeBSD/FreeBSD.git] / usr.bin / sockstat / sockstat.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2002 Dag-Erling Coïdan Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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.
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sysctl.h>
38 #include <sys/file.h>
39 #include <sys/user.h>
40
41 #include <sys/un.h>
42 #define _WANT_UNPCB
43 #include <sys/unpcb.h>
44
45 #include <net/route.h>
46
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>
56
57 #include <ctype.h>
58 #include <err.h>
59 #include <errno.h>
60 #include <netdb.h>
61 #include <pwd.h>
62 #include <stdarg.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <unistd.h>
67
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))
72
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 */
86
87 /*
88  * Default protocols to use if no -P was defined.
89  */
90 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
91 static size_t      default_numprotos = nitems(default_protos);
92
93 static int      *protos;        /* protocols to use */
94 static size_t    numprotos;     /* allocated size of protos[] */
95
96 static int      *ports;
97
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)))
101
102 struct addr {
103         struct sockaddr_storage address;
104         unsigned int encaps_port;
105         int state;
106         struct addr *next;
107 };
108
109 struct sock {
110         void *socket;
111         void *pcb;
112         int shown;
113         int vflag;
114         int family;
115         int proto;
116         int state;
117         const char *protoname;
118         char stack[TCP_FUNCTION_NAME_LEN_MAX];
119         struct addr *laddr;
120         struct addr *faddr;
121         struct sock *next;
122 };
123
124 #define HASHSIZE 1009
125 static struct sock *sockhash[HASHSIZE];
126
127 static struct xfile *xfiles;
128 static int nxfiles;
129
130 static int
131 xprintf(const char *fmt, ...)
132 {
133         va_list ap;
134         int len;
135
136         va_start(ap, fmt);
137         len = vprintf(fmt, ap);
138         va_end(ap);
139         if (len < 0)
140                 err(1, "printf()");
141         return (len);
142 }
143
144 static int
145 get_proto_type(const char *proto)
146 {
147         struct protoent *pent;
148
149         if (strlen(proto) == 0)
150                 return (0);
151         pent = getprotobyname(proto);
152         if (pent == NULL) {
153                 warn("getprotobyname");
154                 return (-1);
155         }
156         return (pent->p_proto);
157 }
158
159 static void
160 init_protos(int num)
161 {
162         int proto_count = 0;
163
164         if (num > 0) {
165                 proto_count = num;
166         } else {
167                 /* Find the maximum number of possible protocols. */
168                 while (getprotoent() != NULL)
169                         proto_count++;
170                 endprotoent();
171         }
172
173         if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
174                 err(1, "malloc");
175         numprotos = proto_count;
176 }
177
178 static int
179 parse_protos(char *protospec)
180 {
181         char *prot;
182         int proto_type, proto_index;
183
184         if (protospec == NULL)
185                 return (-1);
186
187         init_protos(0);
188         proto_index = 0;
189         while ((prot = strsep(&protospec, ",")) != NULL) {
190                 if (strlen(prot) == 0)
191                         continue;
192                 proto_type = get_proto_type(prot);
193                 if (proto_type != -1)
194                         protos[proto_index++] = proto_type;
195         }
196         numprotos = proto_index;
197         return (proto_index);
198 }
199
200 static void
201 parse_ports(const char *portspec)
202 {
203         const char *p, *q;
204         int port, end;
205
206         if (ports == NULL)
207                 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
208                         err(1, "calloc()");
209         p = portspec;
210         while (*p != '\0') {
211                 if (!isdigit(*p))
212                         errx(1, "syntax error in port range");
213                 for (q = p; *q != '\0' && isdigit(*q); ++q)
214                         /* nothing */ ;
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");
219                 SET_PORT(port);
220                 switch (*p) {
221                 case '-':
222                         ++p;
223                         break;
224                 case ',':
225                         ++p;
226                         /* fall through */
227                 case '\0':
228                 default:
229                         continue;
230                 }
231                 for (q = p; *q != '\0' && isdigit(*q); ++q)
232                         /* nothing */ ;
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");
237                 while (port++ < end)
238                         SET_PORT(port);
239                 if (*p == ',')
240                         ++p;
241         }
242 }
243
244 static void
245 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
246 {
247         struct sockaddr_in *sin4;
248         struct sockaddr_in6 *sin6;
249
250         bzero(ss, sizeof(*ss));
251         switch (af) {
252         case AF_INET:
253                 sin4 = sstosin(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;
258                 break;
259         case AF_INET6:
260                 sin6 = sstosin6(ss);
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;
270                 }
271                 break;
272         default:
273                 abort();
274         }
275 }
276
277 static void
278 free_socket(struct sock *sock)
279 {
280         struct addr *cur, *next;
281
282         cur = sock->laddr;
283         while (cur != NULL) {
284                 next = cur->next;
285                 free(cur);
286                 cur = next;
287         }
288         cur = sock->faddr;
289         while (cur != NULL) {
290                 next = cur->next;
291                 free(cur);
292                 cur = next;
293         }
294         free(sock);
295 }
296
297 static void
298 gather_sctp(void)
299 {
300         struct sock *sock;
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;
306         const char *varname;
307         size_t len, offset;
308         char *buf;
309         int hash, vflag;
310         int no_stcb, local_all_loopback, foreign_all_loopback;
311
312         vflag = 0;
313         if (opt_4)
314                 vflag |= INP_IPV4;
315         if (opt_6)
316                 vflag |= INP_IPV6;
317
318         varname = "net.inet.sctp.assoclist";
319         if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
320                 if (errno != ENOENT)
321                         err(1, "sysctlbyname()");
322                 return;
323         }
324         if ((buf = (char *)malloc(len)) == NULL) {
325                 err(1, "malloc()");
326                 return;
327         }
328         if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
329                 err(1, "sysctlbyname()");
330                 free(buf);
331                 return;
332         }
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)
337                         err(1, "malloc()");
338                 sock->socket = xinpcb->socket;
339                 sock->proto = IPPROTO_SCTP;
340                 sock->protoname = "sctp";
341                 if (xinpcb->maxqlen == 0)
342                         sock->state = SCTP_CLOSED;
343                 else
344                         sock->state = SCTP_LISTEN;
345                 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
346                         sock->family = AF_INET6;
347                         /*
348                          * Currently there is no way to distinguish between
349                          * IPv6 only sockets or dual family sockets.
350                          * So mark it as dual socket.
351                          */
352                         sock->vflag = INP_IPV6 | INP_IPV4;
353                 } else {
354                         sock->family = AF_INET;
355                         sock->vflag = INP_IPV4;
356                 }
357                 prev_laddr = NULL;
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)
363                                 break;
364                         if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
365                                 err(1, "malloc()");
366                         switch (xladdr->address.sa.sa_family) {
367                         case AF_INET:
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));
377                                 break;
378                         case AF_INET6:
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));
385                                 break;
386                         default:
387                                 errx(1, "address family %d not supported",
388                                     xladdr->address.sa.sa_family);
389                         }
390                         laddr->next = NULL;
391                         if (prev_laddr == NULL)
392                                 sock->laddr = laddr;
393                         else
394                                 prev_laddr->next = laddr;
395                         prev_laddr = laddr;
396                 }
397                 if (sock->laddr == NULL) {
398                         if ((sock->laddr =
399                             calloc(1, sizeof(struct addr))) == NULL)
400                                 err(1, "malloc()");
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);
405                         else
406                                 sock->laddr->address.ss_len =
407                                     sizeof(struct sockaddr_in6);
408                         local_all_loopback = 0;
409                 }
410                 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
411                         err(1, "malloc()");
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);
416                 else
417                         sock->faddr->address.ss_len =
418                             sizeof(struct sockaddr_in6);
419                 no_stcb = 1;
420                 while (offset < len) {
421                         xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
422                         offset += sizeof(struct xsctp_tcb);
423                         if (no_stcb) {
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 %
429                                             HASHSIZE);
430                                         sock->next = sockhash[hash];
431                                         sockhash[hash] = sock;
432                                 } else {
433                                         free_socket(sock);
434                                 }
435                         }
436                         if (xstcb->last == 1)
437                                 break;
438                         no_stcb = 0;
439                         if (opt_c) {
440                                 if ((sock = calloc(1, sizeof *sock)) == NULL)
441                                         err(1, "malloc()");
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;
448                                 /*
449                                  * Currently there is no way to distinguish
450                                  * between IPv6 only sockets or dual family
451                                  *  sockets. So mark it as dual socket.
452                                  */
453                                         sock->vflag = INP_IPV6 | INP_IPV4;
454                                 } else {
455                                         sock->family = AF_INET;
456                                         sock->vflag = INP_IPV4;
457                                 }
458                         }
459                         prev_laddr = NULL;
460                         local_all_loopback = 1;
461                         while (offset < len) {
462                                 xladdr = (struct xsctp_laddr *)(void *)(buf +
463                                     offset);
464                                 offset += sizeof(struct xsctp_laddr);
465                                 if (xladdr->last == 1)
466                                         break;
467                                 if (!opt_c)
468                                         continue;
469                                 laddr = calloc(1, sizeof(struct addr));
470                                 if (laddr == NULL)
471                                         err(1, "malloc()");
472                                 switch (xladdr->address.sa.sa_family) {
473                                 case AF_INET:
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));
483                                         break;
484                                 case AF_INET6:
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));
491                                         break;
492                                 default:
493                                         errx(1,
494                                             "address family %d not supported",
495                                             xladdr->address.sa.sa_family);
496                                 }
497                                 laddr->next = NULL;
498                                 if (prev_laddr == NULL)
499                                         sock->laddr = laddr;
500                                 else
501                                         prev_laddr->next = laddr;
502                                 prev_laddr = laddr;
503                         }
504                         prev_faddr = NULL;
505                         foreign_all_loopback = 1;
506                         while (offset < len) {
507                                 xraddr = (struct xsctp_raddr *)(void *)(buf +
508                                     offset);
509                                 offset += sizeof(struct xsctp_raddr);
510                                 if (xraddr->last == 1)
511                                         break;
512                                 if (!opt_c)
513                                         continue;
514                                 faddr = calloc(1, sizeof(struct addr));
515                                 if (faddr == NULL)
516                                         err(1, "malloc()");
517                                 switch (xraddr->address.sa.sa_family) {
518                                 case AF_INET:
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));
528                                         break;
529                                 case AF_INET6:
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));
536                                         break;
537                                 default:
538                                         errx(1,
539                                             "address family %d not supported",
540                                             xraddr->address.sa.sa_family);
541                                 }
542                                 faddr->encaps_port = xraddr->encaps_port;
543                                 faddr->state = xraddr->state;
544                                 faddr->next = NULL;
545                                 if (prev_faddr == NULL)
546                                         sock->faddr = faddr;
547                                 else
548                                         prev_faddr->next = faddr;
549                                 prev_faddr = faddr;
550                         }
551                         if (opt_c) {
552                                 if ((sock->vflag & vflag) &&
553                                     (!opt_L ||
554                                      !(local_all_loopback ||
555                                      foreign_all_loopback))) {
556                                         hash = (int)((uintptr_t)sock->socket %
557                                             HASHSIZE);
558                                         sock->next = sockhash[hash];
559                                         sockhash[hash] = sock;
560                                 } else {
561                                         free_socket(sock);
562                                 }
563                         }
564                 }
565                 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
566                 offset += sizeof(struct xsctp_inpcb);
567         }
568         free(buf);
569 }
570
571 static void
572 gather_inet(int proto)
573 {
574         struct xinpgen *xig, *exig;
575         struct xinpcb *xip;
576         struct xtcpcb *xtp = NULL;
577         struct xsocket *so;
578         struct sock *sock;
579         struct addr *laddr, *faddr;
580         const char *varname, *protoname;
581         size_t len, bufsize;
582         void *buf;
583         int hash, retry, vflag;
584
585         vflag = 0;
586         if (opt_4)
587                 vflag |= INP_IPV4;
588         if (opt_6)
589                 vflag |= INP_IPV6;
590
591         switch (proto) {
592         case IPPROTO_TCP:
593                 varname = "net.inet.tcp.pcblist";
594                 protoname = "tcp";
595                 break;
596         case IPPROTO_UDP:
597                 varname = "net.inet.udp.pcblist";
598                 protoname = "udp";
599                 break;
600         case IPPROTO_DIVERT:
601                 varname = "net.inet.divert.pcblist";
602                 protoname = "div";
603                 break;
604         default:
605                 errx(1, "protocol %d not supported", proto);
606         }
607
608         buf = NULL;
609         bufsize = 8192;
610         retry = 5;
611         do {
612                 for (;;) {
613                         if ((buf = realloc(buf, bufsize)) == NULL)
614                                 err(1, "realloc()");
615                         len = bufsize;
616                         if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
617                                 break;
618                         if (errno == ENOENT)
619                                 goto out;
620                         if (errno != ENOMEM || len != bufsize)
621                                 err(1, "sysctlbyname()");
622                         bufsize *= 2;
623                 }
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--);
631
632         if (xig->xig_gen != exig->xig_gen && opt_v)
633                 warnx("warning: data may be inconsistent");
634
635         for (;;) {
636                 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
637                 if (xig >= exig)
638                         break;
639                 switch (proto) {
640                 case IPPROTO_TCP:
641                         xtp = (struct xtcpcb *)xig;
642                         xip = &xtp->xt_inp;
643                         if (xtp->xt_len != sizeof(*xtp)) {
644                                 warnx("struct xtcpcb size mismatch");
645                                 goto out;
646                         }
647                         protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
648                         break;
649                 case IPPROTO_UDP:
650                 case IPPROTO_DIVERT:
651                         xip = (struct xinpcb *)xig;
652                         if (xip->xi_len != sizeof(*xip)) {
653                                 warnx("struct xinpcb size mismatch");
654                                 goto out;
655                         }
656                         break;
657                 default:
658                         errx(1, "protocol %d not supported", proto);
659                 }
660                 so = &xip->xi_socket;
661                 if ((xip->inp_vflag & vflag) == 0)
662                         continue;
663                 if (xip->inp_vflag & INP_IPV4) {
664                         if ((xip->inp_fport == 0 && !opt_l) ||
665                             (xip->inp_fport != 0 && !opt_c))
666                                 continue;
667 #define __IN_IS_ADDR_LOOPBACK(pina) \
668         ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
669                         if (opt_L &&
670                             (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
671                              __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
672                                 continue;
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))
677                                 continue;
678                         if (opt_L &&
679                             (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
680                              IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
681                                 continue;
682                 } else {
683                         if (opt_v)
684                                 warnx("invalid vflag 0x%x", xip->inp_vflag);
685                         continue;
686                 }
687                 if ((sock = calloc(1, sizeof(*sock))) == NULL)
688                         err(1, "malloc()");
689                 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
690                         err(1, "malloc()");
691                 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
692                         err(1, "malloc()");
693                 sock->socket = so->xso_so;
694                 sock->proto = proto;
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);
707                 }
708                 laddr->next = NULL;
709                 faddr->next = NULL;
710                 sock->laddr = laddr;
711                 sock->faddr = faddr;
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);
717                 }
718                 sock->protoname = protoname;
719                 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
720                 sock->next = sockhash[hash];
721                 sockhash[hash] = sock;
722         }
723 out:
724         free(buf);
725 }
726
727 static void
728 gather_unix(int proto)
729 {
730         struct xunpgen *xug, *exug;
731         struct xunpcb *xup;
732         struct sock *sock;
733         struct addr *laddr, *faddr;
734         const char *varname, *protoname;
735         size_t len, bufsize;
736         void *buf;
737         int hash, retry;
738
739         switch (proto) {
740         case SOCK_STREAM:
741                 varname = "net.local.stream.pcblist";
742                 protoname = "stream";
743                 break;
744         case SOCK_DGRAM:
745                 varname = "net.local.dgram.pcblist";
746                 protoname = "dgram";
747                 break;
748         case SOCK_SEQPACKET:
749                 varname = "net.local.seqpacket.pcblist";
750                 protoname = "seqpac";
751                 break;
752         default:
753                 abort();
754         }
755         buf = NULL;
756         bufsize = 8192;
757         retry = 5;
758         do {
759                 for (;;) {
760                         if ((buf = realloc(buf, bufsize)) == NULL)
761                                 err(1, "realloc()");
762                         len = bufsize;
763                         if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
764                                 break;
765                         if (errno != ENOMEM || len != bufsize)
766                                 err(1, "sysctlbyname()");
767                         bufsize *= 2;
768                 }
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");
775                         goto out;
776                 }
777         } while (xug->xug_gen != exug->xug_gen && retry--);
778
779         if (xug->xug_gen != exug->xug_gen && opt_v)
780                 warnx("warning: data may be inconsistent");
781
782         for (;;) {
783                 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
784                 if (xug >= exug)
785                         break;
786                 xup = (struct xunpcb *)xug;
787                 if (xup->xu_len != sizeof(*xup)) {
788                         warnx("struct xunpcb size mismatch");
789                         goto out;
790                 }
791                 if ((xup->unp_conn == NULL && !opt_l) ||
792                     (xup->unp_conn != NULL && !opt_c))
793                         continue;
794                 if ((sock = calloc(1, sizeof(*sock))) == NULL)
795                         err(1, "malloc()");
796                 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
797                         err(1, "malloc()");
798                 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
799                         err(1, "malloc()");
800                 sock->socket = xup->xu_socket.xso_so;
801                 sock->pcb = xup->xu_unpp;
802                 sock->proto = proto;
803                 sock->family = AF_UNIX;
804                 sock->protoname = protoname;
805                 if (xup->xu_addr.sun_family == AF_UNIX)
806                         laddr->address =
807                             *(struct sockaddr_storage *)(void *)&xup->xu_addr;
808                 else if (xup->unp_conn != NULL)
809                         *(void **)&(faddr->address) = xup->unp_conn;
810                 laddr->next = NULL;
811                 faddr->next = NULL;
812                 sock->laddr = laddr;
813                 sock->faddr = faddr;
814                 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
815                 sock->next = sockhash[hash];
816                 sockhash[hash] = sock;
817         }
818 out:
819         free(buf);
820 }
821
822 static void
823 getfiles(void)
824 {
825         size_t len, olen;
826
827         olen = len = sizeof(*xfiles);
828         if ((xfiles = malloc(len)) == NULL)
829                 err(1, "malloc()");
830         while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
831                 if (errno != ENOMEM || len != olen)
832                         err(1, "sysctlbyname()");
833                 olen = len *= 2;
834                 if ((xfiles = realloc(xfiles, len)) == NULL)
835                         err(1, "realloc()");
836         }
837         if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
838                 errx(1, "struct xfile size mismatch");
839         nxfiles = len / sizeof(*xfiles);
840 }
841
842 static int
843 printaddr(struct sockaddr_storage *ss)
844 {
845         struct sockaddr_un *sun;
846         char addrstr[NI_MAXHOST] = { '\0', '\0' };
847         int error, off, port = 0;
848
849         switch (ss->ss_family) {
850         case AF_INET:
851                 if (inet_lnaof(sstosin(ss)->sin_addr) == INADDR_ANY)
852                         addrstr[0] = '*';
853                 port = ntohs(sstosin(ss)->sin_port);
854                 break;
855         case AF_INET6:
856                 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
857                         addrstr[0] = '*';
858                 port = ntohs(sstosin6(ss)->sin6_port);
859                 break;
860         case AF_UNIX:
861                 sun = sstosun(ss);
862                 off = (int)((char *)&sun->sun_path - (char *)sun);
863                 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
864         }
865         if (addrstr[0] == '\0') {
866                 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
867                     sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
868                 if (error)
869                         errx(1, "getnameinfo()");
870         }
871         if (port == 0)
872                 return xprintf("%s:*", addrstr);
873         else
874                 return xprintf("%s:%d", addrstr, port);
875 }
876
877 static const char *
878 getprocname(pid_t pid)
879 {
880         static struct kinfo_proc proc;
881         size_t len;
882         int mib[4];
883
884         mib[0] = CTL_KERN;
885         mib[1] = KERN_PROC;
886         mib[2] = KERN_PROC_PID;
887         mib[3] = (int)pid;
888         len = sizeof(proc);
889         if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
890                 /* Do not warn if the process exits before we get its name. */
891                 if (errno != ESRCH)
892                         warn("sysctl()");
893                 return ("??");
894         }
895         return (proc.ki_comm);
896 }
897
898 static int
899 getprocjid(pid_t pid)
900 {
901         static struct kinfo_proc proc;
902         size_t len;
903         int mib[4];
904
905         mib[0] = CTL_KERN;
906         mib[1] = KERN_PROC;
907         mib[2] = KERN_PROC_PID;
908         mib[3] = (int)pid;
909         len = sizeof(proc);
910         if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
911                 /* Do not warn if the process exits before we get its jid. */
912                 if (errno != ESRCH)
913                         warn("sysctl()");
914                 return (-1);
915         }
916         return (proc.ki_jid);
917 }
918
919 static int
920 check_ports(struct sock *s)
921 {
922         int port;
923         struct addr *addr;
924
925         if (ports == NULL)
926                 return (1);
927         if ((s->family != AF_INET) && (s->family != AF_INET6))
928                 return (1);
929         for (addr = s->laddr; addr != NULL; addr = addr->next) {
930                 if (s->family == AF_INET)
931                         port = ntohs(sstosin(&addr->address)->sin_port);
932                 else
933                         port = ntohs(sstosin6(&addr->address)->sin6_port);
934                 if (CHK_PORT(port))
935                         return (1);
936         }
937         for (addr = s->faddr; addr != NULL; addr = addr->next) {
938                 if (s->family == AF_INET)
939                         port = ntohs(sstosin(&addr->address)->sin_port);
940                 else
941                         port = ntohs(sstosin6(&addr->address)->sin6_port);
942                 if (CHK_PORT(port))
943                         return (1);
944         }
945         return (0);
946 }
947
948 static const char *
949 sctp_conn_state(int state)
950 {
951         switch (state) {
952         case SCTP_CLOSED:
953                 return "CLOSED";
954                 break;
955         case SCTP_BOUND:
956                 return "BOUND";
957                 break;
958         case SCTP_LISTEN:
959                 return "LISTEN";
960                 break;
961         case SCTP_COOKIE_WAIT:
962                 return "COOKIE_WAIT";
963                 break;
964         case SCTP_COOKIE_ECHOED:
965                 return "COOKIE_ECHOED";
966                 break;
967         case SCTP_ESTABLISHED:
968                 return "ESTABLISHED";
969                 break;
970         case SCTP_SHUTDOWN_SENT:
971                 return "SHUTDOWN_SENT";
972                 break;
973         case SCTP_SHUTDOWN_RECEIVED:
974                 return "SHUTDOWN_RECEIVED";
975                 break;
976         case SCTP_SHUTDOWN_ACK_SENT:
977                 return "SHUTDOWN_ACK_SENT";
978                 break;
979         case SCTP_SHUTDOWN_PENDING:
980                 return "SHUTDOWN_PENDING";
981                 break;
982         default:
983                 return "UNKNOWN";
984                 break;
985         }
986 }
987
988 static const char *
989 sctp_path_state(int state)
990 {
991         switch (state) {
992         case SCTP_UNCONFIRMED:
993                 return "UNCONFIRMED";
994                 break;
995         case SCTP_ACTIVE:
996                 return "ACTIVE";
997                 break;
998         case SCTP_INACTIVE:
999                 return "INACTIVE";
1000                 break;
1001         default:
1002                 return "UNKNOWN";
1003                 break;
1004         }
1005 }
1006
1007 static void
1008 displaysock(struct sock *s, int pos)
1009 {
1010         void *p;
1011         int hash, first, offset;
1012         struct addr *laddr, *faddr;
1013         struct sock *s_tmp;
1014
1015         while (pos < 29)
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(" ");
1024         laddr = s->laddr;
1025         faddr = s->faddr;
1026         first = 1;
1027         while (laddr != NULL || faddr != NULL) {
1028                 offset = 36;
1029                 while (pos < offset)
1030                         pos += xprintf(" ");
1031                 switch (s->family) {
1032                 case AF_INET:
1033                 case AF_INET6:
1034                         if (laddr != NULL) {
1035                                 pos += printaddr(&laddr->address);
1036                                 if (s->family == AF_INET6 && pos >= 58)
1037                                         pos += xprintf(" ");
1038                         }
1039                         offset += opt_w ? 46 : 22;
1040                         while (pos < offset)
1041                                 pos += xprintf(" ");
1042                         if (faddr != NULL)
1043                                 pos += printaddr(&faddr->address);
1044                         offset += opt_w ? 46 : 22;
1045                         break;
1046                 case AF_UNIX:
1047                         if ((laddr == NULL) || (faddr == NULL))
1048                                 errx(1, "laddr = %p or faddr = %p is NULL",
1049                                     (void *)laddr, (void *)faddr);
1050                         /* server */
1051                         if (laddr->address.ss_len > 0) {
1052                                 pos += printaddr(&laddr->address);
1053                                 break;
1054                         }
1055                         /* client */
1056                         p = *(void **)&(faddr->address);
1057                         if (p == NULL) {
1058                                 pos += xprintf("(not connected)");
1059                                 offset += opt_w ? 92 : 44;
1060                                 break;
1061                         }
1062                         pos += xprintf("-> ");
1063                         for (hash = 0; hash < HASHSIZE; ++hash) {
1064                                 for (s_tmp = sockhash[hash];
1065                                     s_tmp != NULL;
1066                                     s_tmp = s_tmp->next)
1067                                         if (s_tmp->pcb == p)
1068                                                 break;
1069                                 if (s_tmp != NULL)
1070                                         break;
1071                         }
1072                         if (s_tmp == NULL || s_tmp->laddr == NULL ||
1073                             s_tmp->laddr->address.ss_len == 0)
1074                                 pos += xprintf("??");
1075                         else
1076                                 pos += printaddr(&s_tmp->laddr->address);
1077                         offset += opt_w ? 92 : 44;
1078                         break;
1079                 default:
1080                         abort();
1081                 }
1082                 if (opt_U) {
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));
1092                         }
1093                         offset += 7;
1094                 }
1095                 if (opt_s) {
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));
1105                         }
1106                         offset += 13;
1107                 }
1108                 if (first) {
1109                         if (opt_s) {
1110                                 if (s->proto == IPPROTO_SCTP ||
1111                                     s->proto == IPPROTO_TCP) {
1112                                         while (pos < offset)
1113                                                 pos += xprintf(" ");
1114                                         switch (s->proto) {
1115                                         case IPPROTO_SCTP:
1116                                                 pos += xprintf("%s",
1117                                                     sctp_conn_state(s->state));
1118                                                 break;
1119                                         case IPPROTO_TCP:
1120                                                 if (s->state >= 0 &&
1121                                                     s->state < TCP_NSTATES)
1122                                                         pos += xprintf("%s",
1123                                                             tcpstates[s->state]);
1124                                                 else
1125                                                         pos += xprintf("?");
1126                                                 break;
1127                                         }
1128                                 }
1129                                 offset += 13;
1130                         }
1131                         if (opt_S && s->proto == IPPROTO_TCP) {
1132                                 while (pos < offset)
1133                                         pos += xprintf(" ");
1134                                 xprintf("%.*s", TCP_FUNCTION_NAME_LEN_MAX,
1135                                     s->stack);
1136                         }
1137                 }
1138                 if (laddr != NULL)
1139                         laddr = laddr->next;
1140                 if (faddr != NULL)
1141                         faddr = faddr->next;
1142                 if ((laddr != NULL) || (faddr != NULL)) {
1143                         xprintf("\n");
1144                         pos = 0;
1145                 }
1146                 first = 0;
1147         }
1148         xprintf("\n");
1149 }
1150
1151 static void
1152 display(void)
1153 {
1154         struct passwd *pwd;
1155         struct xfile *xf;
1156         struct sock *s;
1157         int hash, n, pos;
1158
1159         if (opt_q != 1) {
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");
1164                 if (opt_U)
1165                         printf(" %-6s", "ENCAPS");
1166                 if (opt_s) {
1167                         printf(" %-12s", "PATH STATE");
1168                         printf(" %-12s", "CONN STATE");
1169                 }
1170                 if (opt_S)
1171                         printf(" %.*s", TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1172                 printf("\n");
1173         }
1174         setpassent(1);
1175         for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1176                 if (xf->xf_data == NULL)
1177                         continue;
1178                 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1179                         continue;
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)
1183                                 continue;
1184                         if (!check_ports(s))
1185                                 continue;
1186                         s->shown = 1;
1187                         pos = 0;
1188                         if ((pwd = getpwuid(xf->xf_uid)) == NULL)
1189                                 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1190                         else
1191                                 pos += xprintf("%s ", pwd->pw_name);
1192                         while (pos < 9)
1193                                 pos += xprintf(" ");
1194                         pos += xprintf("%.10s", getprocname(xf->xf_pid));
1195                         while (pos < 20)
1196                                 pos += xprintf(" ");
1197                         pos += xprintf("%lu ", (u_long)xf->xf_pid);
1198                         while (pos < 26)
1199                                 pos += xprintf(" ");
1200                         pos += xprintf("%d ", xf->xf_fd);
1201                         displaysock(s, pos);
1202                 }
1203         }
1204         if (opt_j >= 0)
1205                 return;
1206         for (hash = 0; hash < HASHSIZE; hash++) {
1207                 for (s = sockhash[hash]; s != NULL; s = s->next) {
1208                         if (s->shown)
1209                                 continue;
1210                         if (!check_ports(s))
1211                                 continue;
1212                         pos = 0;
1213                         pos += xprintf("%-8s %-10s %-5s %-2s ",
1214                             "?", "?", "?", "?");
1215                         displaysock(s, pos);
1216                 }
1217         }
1218 }
1219
1220 static int set_default_protos(void)
1221 {
1222         struct protoent *prot;
1223         const char *pname;
1224         size_t pindex;
1225
1226         init_protos(default_numprotos);
1227
1228         for (pindex = 0; pindex < default_numprotos; pindex++) {
1229                 pname = default_protos[pindex];
1230                 prot = getprotobyname(pname);
1231                 if (prot == NULL)
1232                         err(1, "getprotobyname: %s", pname);
1233                 protos[pindex] = prot->p_proto;
1234         }
1235         numprotos = pindex;
1236         return (pindex);
1237 }
1238
1239 static void
1240 usage(void)
1241 {
1242         fprintf(stderr,
1243             "usage: sockstat [-46cLlSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1244         exit(1);
1245 }
1246
1247 int
1248 main(int argc, char *argv[])
1249 {
1250         int protos_defined = -1;
1251         int o, i;
1252
1253         opt_j = -1;
1254         while ((o = getopt(argc, argv, "46cj:Llp:P:qSsUuvw")) != -1)
1255                 switch (o) {
1256                 case '4':
1257                         opt_4 = 1;
1258                         break;
1259                 case '6':
1260                         opt_6 = 1;
1261                         break;
1262                 case 'c':
1263                         opt_c = 1;
1264                         break;
1265                 case 'j':
1266                         opt_j = atoi(optarg);
1267                         break;
1268                 case 'L':
1269                         opt_L = 1;
1270                         break;
1271                 case 'l':
1272                         opt_l = 1;
1273                         break;
1274                 case 'p':
1275                         parse_ports(optarg);
1276                         break;
1277                 case 'P':
1278                         protos_defined = parse_protos(optarg);
1279                         break;
1280                 case 'q':
1281                         opt_q = 1;
1282                         break;
1283                 case 'S':
1284                         opt_S = 1;
1285                         break;
1286                 case 's':
1287                         opt_s = 1;
1288                         break;
1289                 case 'U':
1290                         opt_U = 1;
1291                         break;
1292                 case 'u':
1293                         opt_u = 1;
1294                         break;
1295                 case 'v':
1296                         ++opt_v;
1297                         break;
1298                 case 'w':
1299                         opt_w = 1;
1300                         break;
1301                 default:
1302                         usage();
1303                 }
1304
1305         argc -= optind;
1306         argv += optind;
1307
1308         if (argc > 0)
1309                 usage();
1310
1311         if ((!opt_4 && !opt_6) && protos_defined != -1)
1312                 opt_4 = opt_6 = 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)
1318                 opt_c = opt_l = 1;
1319
1320         if (opt_4 || opt_6) {
1321                 for (i = 0; i < protos_defined; i++)
1322                         if (protos[i] == IPPROTO_SCTP)
1323                                 gather_sctp();
1324                         else
1325                                 gather_inet(protos[i]);
1326         }
1327
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);
1332         }
1333         getfiles();
1334         display();
1335         exit(0);
1336 }