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/file.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/jail.h>
40 #include <sys/user.h>
41
42 #include <sys/un.h>
43 #define _WANT_UNPCB
44 #include <sys/unpcb.h>
45
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_pcb.h>
50 #include <netinet/sctp.h>
51 #include <netinet/tcp.h>
52 #define TCPSTATES /* load state names */
53 #include <netinet/tcp_fsm.h>
54 #include <netinet/tcp_seq.h>
55 #include <netinet/tcp_var.h>
56 #include <arpa/inet.h>
57
58 #include <ctype.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <inttypes.h>
62 #include <jail.h>
63 #include <netdb.h>
64 #include <pwd.h>
65 #include <stdarg.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70
71 #define sstosin(ss) ((struct sockaddr_in *)(ss))
72 #define sstosin6(ss) ((struct sockaddr_in6 *)(ss))
73 #define sstosun(ss) ((struct sockaddr_un *)(ss))
74 #define sstosa(ss) ((struct sockaddr *)(ss))
75
76 static int opt_4; /* Show IPv4 sockets */
77 static int opt_6; /* Show IPv6 sockets */
78 static int opt_C; /* Show congestion control */
79 static int opt_c; /* Show connected sockets */
80 static int opt_i; /* Show inp_gencnt */
81 static int opt_j; /* Show specified jail */
82 static int opt_L; /* Don't show IPv4 or IPv6 loopback sockets */
83 static int opt_l; /* Show listening sockets */
84 static int opt_n; /* Don't resolve UIDs to user names */
85 static int opt_q; /* Don't show header */
86 static int opt_S; /* Show protocol stack if applicable */
87 static int opt_s; /* Show protocol state if applicable */
88 static int opt_U; /* Show remote UDP encapsulation port number */
89 static int opt_u; /* Show Unix domain sockets */
90 static int opt_v; /* Verbose mode */
91 static int opt_w; /* Wide print area for addresses */
92
93 /*
94 * Default protocols to use if no -P was defined.
95 */
96 static const char *default_protos[] = {"sctp", "tcp", "udp", "divert" };
97 static size_t default_numprotos = nitems(default_protos);
98
99 static int *protos; /* protocols to use */
100 static size_t numprotos; /* allocated size of protos[] */
101
102 static int *ports;
103
104 #define INT_BIT (sizeof(int)*CHAR_BIT)
105 #define SET_PORT(p) do { ports[p / INT_BIT] |= 1 << (p % INT_BIT); } while (0)
106 #define CHK_PORT(p) (ports[p / INT_BIT] & (1 << (p % INT_BIT)))
107
108 struct addr {
109 struct sockaddr_storage address;
110 unsigned int encaps_port;
111 int state;
112 struct addr *next;
113 };
114
115 struct sock {
116 kvaddr_t socket;
117 kvaddr_t pcb;
118 uint64_t inp_gencnt;
119 int shown;
120 int vflag;
121 int family;
122 int proto;
123 int state;
124 const char *protoname;
125 char stack[TCP_FUNCTION_NAME_LEN_MAX];
126 char cc[TCP_CA_NAME_MAX];
127 struct addr *laddr;
128 struct addr *faddr;
129 struct sock *next;
130 };
131
132 #define HASHSIZE 1009
133 static struct sock *sockhash[HASHSIZE];
134
135 static struct xfile *xfiles;
136 static int nxfiles;
137
138 static int
xprintf(const char * fmt,...)139 xprintf(const char *fmt, ...)
140 {
141 va_list ap;
142 int len;
143
144 va_start(ap, fmt);
145 len = vprintf(fmt, ap);
146 va_end(ap);
147 if (len < 0)
148 err(1, "printf()");
149 return (len);
150 }
151
152 static int
get_proto_type(const char * proto)153 get_proto_type(const char *proto)
154 {
155 struct protoent *pent;
156
157 if (strlen(proto) == 0)
158 return (0);
159 pent = getprotobyname(proto);
160 if (pent == NULL) {
161 warn("getprotobyname");
162 return (-1);
163 }
164 return (pent->p_proto);
165 }
166
167 static void
init_protos(int num)168 init_protos(int num)
169 {
170 int proto_count = 0;
171
172 if (num > 0) {
173 proto_count = num;
174 } else {
175 /* Find the maximum number of possible protocols. */
176 while (getprotoent() != NULL)
177 proto_count++;
178 endprotoent();
179 }
180
181 if ((protos = malloc(sizeof(int) * proto_count)) == NULL)
182 err(1, "malloc");
183 numprotos = proto_count;
184 }
185
186 static int
parse_protos(char * protospec)187 parse_protos(char *protospec)
188 {
189 char *prot;
190 int proto_type, proto_index;
191
192 if (protospec == NULL)
193 return (-1);
194
195 init_protos(0);
196 proto_index = 0;
197 while ((prot = strsep(&protospec, ",")) != NULL) {
198 if (strlen(prot) == 0)
199 continue;
200 proto_type = get_proto_type(prot);
201 if (proto_type != -1)
202 protos[proto_index++] = proto_type;
203 }
204 numprotos = proto_index;
205 return (proto_index);
206 }
207
208 static void
parse_ports(const char * portspec)209 parse_ports(const char *portspec)
210 {
211 const char *p, *q;
212 int port, end;
213
214 if (ports == NULL)
215 if ((ports = calloc(65536 / INT_BIT, sizeof(int))) == NULL)
216 err(1, "calloc()");
217 p = portspec;
218 while (*p != '\0') {
219 if (!isdigit(*p))
220 errx(1, "syntax error in port range");
221 for (q = p; *q != '\0' && isdigit(*q); ++q)
222 /* nothing */ ;
223 for (port = 0; p < q; ++p)
224 port = port * 10 + digittoint(*p);
225 if (port < 0 || port > 65535)
226 errx(1, "invalid port number");
227 SET_PORT(port);
228 switch (*p) {
229 case '-':
230 ++p;
231 break;
232 case ',':
233 ++p;
234 /* fall through */
235 case '\0':
236 default:
237 continue;
238 }
239 for (q = p; *q != '\0' && isdigit(*q); ++q)
240 /* nothing */ ;
241 for (end = 0; p < q; ++p)
242 end = end * 10 + digittoint(*p);
243 if (end < port || end > 65535)
244 errx(1, "invalid port number");
245 while (port++ < end)
246 SET_PORT(port);
247 if (*p == ',')
248 ++p;
249 }
250 }
251
252 static void
sockaddr(struct sockaddr_storage * ss,int af,void * addr,int port)253 sockaddr(struct sockaddr_storage *ss, int af, void *addr, int port)
254 {
255 struct sockaddr_in *sin4;
256 struct sockaddr_in6 *sin6;
257
258 bzero(ss, sizeof(*ss));
259 switch (af) {
260 case AF_INET:
261 sin4 = sstosin(ss);
262 sin4->sin_len = sizeof(*sin4);
263 sin4->sin_family = af;
264 sin4->sin_port = port;
265 sin4->sin_addr = *(struct in_addr *)addr;
266 break;
267 case AF_INET6:
268 sin6 = sstosin6(ss);
269 sin6->sin6_len = sizeof(*sin6);
270 sin6->sin6_family = af;
271 sin6->sin6_port = port;
272 sin6->sin6_addr = *(struct in6_addr *)addr;
273 #define s6_addr16 __u6_addr.__u6_addr16
274 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
275 sin6->sin6_scope_id =
276 ntohs(sin6->sin6_addr.s6_addr16[1]);
277 sin6->sin6_addr.s6_addr16[1] = 0;
278 }
279 break;
280 default:
281 abort();
282 }
283 }
284
285 static void
free_socket(struct sock * sock)286 free_socket(struct sock *sock)
287 {
288 struct addr *cur, *next;
289
290 cur = sock->laddr;
291 while (cur != NULL) {
292 next = cur->next;
293 free(cur);
294 cur = next;
295 }
296 cur = sock->faddr;
297 while (cur != NULL) {
298 next = cur->next;
299 free(cur);
300 cur = next;
301 }
302 free(sock);
303 }
304
305 static void
gather_sctp(void)306 gather_sctp(void)
307 {
308 struct sock *sock;
309 struct addr *laddr, *prev_laddr, *faddr, *prev_faddr;
310 struct xsctp_inpcb *xinpcb;
311 struct xsctp_tcb *xstcb;
312 struct xsctp_raddr *xraddr;
313 struct xsctp_laddr *xladdr;
314 const char *varname;
315 size_t len, offset;
316 char *buf;
317 int hash, vflag;
318 int no_stcb, local_all_loopback, foreign_all_loopback;
319
320 vflag = 0;
321 if (opt_4)
322 vflag |= INP_IPV4;
323 if (opt_6)
324 vflag |= INP_IPV6;
325
326 varname = "net.inet.sctp.assoclist";
327 if (sysctlbyname(varname, 0, &len, 0, 0) < 0) {
328 if (errno != ENOENT)
329 err(1, "sysctlbyname()");
330 return;
331 }
332 if ((buf = (char *)malloc(len)) == NULL) {
333 err(1, "malloc()");
334 return;
335 }
336 if (sysctlbyname(varname, buf, &len, 0, 0) < 0) {
337 err(1, "sysctlbyname()");
338 free(buf);
339 return;
340 }
341 xinpcb = (struct xsctp_inpcb *)(void *)buf;
342 offset = sizeof(struct xsctp_inpcb);
343 while ((offset < len) && (xinpcb->last == 0)) {
344 if ((sock = calloc(1, sizeof *sock)) == NULL)
345 err(1, "malloc()");
346 sock->socket = xinpcb->socket;
347 sock->proto = IPPROTO_SCTP;
348 sock->protoname = "sctp";
349 if (xinpcb->maxqlen == 0)
350 sock->state = SCTP_CLOSED;
351 else
352 sock->state = SCTP_LISTEN;
353 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
354 sock->family = AF_INET6;
355 /*
356 * Currently there is no way to distinguish between
357 * IPv6 only sockets or dual family sockets.
358 * So mark it as dual socket.
359 */
360 sock->vflag = INP_IPV6 | INP_IPV4;
361 } else {
362 sock->family = AF_INET;
363 sock->vflag = INP_IPV4;
364 }
365 prev_laddr = NULL;
366 local_all_loopback = 1;
367 while (offset < len) {
368 xladdr = (struct xsctp_laddr *)(void *)(buf + offset);
369 offset += sizeof(struct xsctp_laddr);
370 if (xladdr->last == 1)
371 break;
372 if ((laddr = calloc(1, sizeof(struct addr))) == NULL)
373 err(1, "malloc()");
374 switch (xladdr->address.sa.sa_family) {
375 case AF_INET:
376 #define __IN_IS_ADDR_LOOPBACK(pina) \
377 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
378 if (!__IN_IS_ADDR_LOOPBACK(
379 &xladdr->address.sin.sin_addr))
380 local_all_loopback = 0;
381 #undef __IN_IS_ADDR_LOOPBACK
382 sockaddr(&laddr->address, AF_INET,
383 &xladdr->address.sin.sin_addr,
384 htons(xinpcb->local_port));
385 break;
386 case AF_INET6:
387 if (!IN6_IS_ADDR_LOOPBACK(
388 &xladdr->address.sin6.sin6_addr))
389 local_all_loopback = 0;
390 sockaddr(&laddr->address, AF_INET6,
391 &xladdr->address.sin6.sin6_addr,
392 htons(xinpcb->local_port));
393 break;
394 default:
395 errx(1, "address family %d not supported",
396 xladdr->address.sa.sa_family);
397 }
398 laddr->next = NULL;
399 if (prev_laddr == NULL)
400 sock->laddr = laddr;
401 else
402 prev_laddr->next = laddr;
403 prev_laddr = laddr;
404 }
405 if (sock->laddr == NULL) {
406 if ((sock->laddr =
407 calloc(1, sizeof(struct addr))) == NULL)
408 err(1, "malloc()");
409 sock->laddr->address.ss_family = sock->family;
410 if (sock->family == AF_INET)
411 sock->laddr->address.ss_len =
412 sizeof(struct sockaddr_in);
413 else
414 sock->laddr->address.ss_len =
415 sizeof(struct sockaddr_in6);
416 local_all_loopback = 0;
417 }
418 if ((sock->faddr = calloc(1, sizeof(struct addr))) == NULL)
419 err(1, "malloc()");
420 sock->faddr->address.ss_family = sock->family;
421 if (sock->family == AF_INET)
422 sock->faddr->address.ss_len =
423 sizeof(struct sockaddr_in);
424 else
425 sock->faddr->address.ss_len =
426 sizeof(struct sockaddr_in6);
427 no_stcb = 1;
428 while (offset < len) {
429 xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
430 offset += sizeof(struct xsctp_tcb);
431 if (no_stcb) {
432 if (opt_l && (sock->vflag & vflag) &&
433 (!opt_L || !local_all_loopback) &&
434 ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
435 (xstcb->last == 1))) {
436 hash = (int)((uintptr_t)sock->socket %
437 HASHSIZE);
438 sock->next = sockhash[hash];
439 sockhash[hash] = sock;
440 } else {
441 free_socket(sock);
442 }
443 }
444 if (xstcb->last == 1)
445 break;
446 no_stcb = 0;
447 if (opt_c) {
448 if ((sock = calloc(1, sizeof *sock)) == NULL)
449 err(1, "malloc()");
450 sock->socket = xinpcb->socket;
451 sock->proto = IPPROTO_SCTP;
452 sock->protoname = "sctp";
453 sock->state = (int)xstcb->state;
454 if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
455 sock->family = AF_INET6;
456 /*
457 * Currently there is no way to distinguish
458 * between IPv6 only sockets or dual family
459 * sockets. So mark it as dual socket.
460 */
461 sock->vflag = INP_IPV6 | INP_IPV4;
462 } else {
463 sock->family = AF_INET;
464 sock->vflag = INP_IPV4;
465 }
466 }
467 prev_laddr = NULL;
468 local_all_loopback = 1;
469 while (offset < len) {
470 xladdr = (struct xsctp_laddr *)(void *)(buf +
471 offset);
472 offset += sizeof(struct xsctp_laddr);
473 if (xladdr->last == 1)
474 break;
475 if (!opt_c)
476 continue;
477 laddr = calloc(1, sizeof(struct addr));
478 if (laddr == NULL)
479 err(1, "malloc()");
480 switch (xladdr->address.sa.sa_family) {
481 case AF_INET:
482 #define __IN_IS_ADDR_LOOPBACK(pina) \
483 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
484 if (!__IN_IS_ADDR_LOOPBACK(
485 &xladdr->address.sin.sin_addr))
486 local_all_loopback = 0;
487 #undef __IN_IS_ADDR_LOOPBACK
488 sockaddr(&laddr->address, AF_INET,
489 &xladdr->address.sin.sin_addr,
490 htons(xstcb->local_port));
491 break;
492 case AF_INET6:
493 if (!IN6_IS_ADDR_LOOPBACK(
494 &xladdr->address.sin6.sin6_addr))
495 local_all_loopback = 0;
496 sockaddr(&laddr->address, AF_INET6,
497 &xladdr->address.sin6.sin6_addr,
498 htons(xstcb->local_port));
499 break;
500 default:
501 errx(1,
502 "address family %d not supported",
503 xladdr->address.sa.sa_family);
504 }
505 laddr->next = NULL;
506 if (prev_laddr == NULL)
507 sock->laddr = laddr;
508 else
509 prev_laddr->next = laddr;
510 prev_laddr = laddr;
511 }
512 prev_faddr = NULL;
513 foreign_all_loopback = 1;
514 while (offset < len) {
515 xraddr = (struct xsctp_raddr *)(void *)(buf +
516 offset);
517 offset += sizeof(struct xsctp_raddr);
518 if (xraddr->last == 1)
519 break;
520 if (!opt_c)
521 continue;
522 faddr = calloc(1, sizeof(struct addr));
523 if (faddr == NULL)
524 err(1, "malloc()");
525 switch (xraddr->address.sa.sa_family) {
526 case AF_INET:
527 #define __IN_IS_ADDR_LOOPBACK(pina) \
528 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
529 if (!__IN_IS_ADDR_LOOPBACK(
530 &xraddr->address.sin.sin_addr))
531 foreign_all_loopback = 0;
532 #undef __IN_IS_ADDR_LOOPBACK
533 sockaddr(&faddr->address, AF_INET,
534 &xraddr->address.sin.sin_addr,
535 htons(xstcb->remote_port));
536 break;
537 case AF_INET6:
538 if (!IN6_IS_ADDR_LOOPBACK(
539 &xraddr->address.sin6.sin6_addr))
540 foreign_all_loopback = 0;
541 sockaddr(&faddr->address, AF_INET6,
542 &xraddr->address.sin6.sin6_addr,
543 htons(xstcb->remote_port));
544 break;
545 default:
546 errx(1,
547 "address family %d not supported",
548 xraddr->address.sa.sa_family);
549 }
550 faddr->encaps_port = xraddr->encaps_port;
551 faddr->state = xraddr->state;
552 faddr->next = NULL;
553 if (prev_faddr == NULL)
554 sock->faddr = faddr;
555 else
556 prev_faddr->next = faddr;
557 prev_faddr = faddr;
558 }
559 if (opt_c) {
560 if ((sock->vflag & vflag) &&
561 (!opt_L ||
562 !(local_all_loopback ||
563 foreign_all_loopback))) {
564 hash = (int)((uintptr_t)sock->socket %
565 HASHSIZE);
566 sock->next = sockhash[hash];
567 sockhash[hash] = sock;
568 } else {
569 free_socket(sock);
570 }
571 }
572 }
573 xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);
574 offset += sizeof(struct xsctp_inpcb);
575 }
576 free(buf);
577 }
578
579 static void
gather_inet(int proto)580 gather_inet(int proto)
581 {
582 struct xinpgen *xig, *exig;
583 struct xinpcb *xip;
584 struct xtcpcb *xtp = NULL;
585 struct xsocket *so;
586 struct sock *sock;
587 struct addr *laddr, *faddr;
588 const char *varname, *protoname;
589 size_t len, bufsize;
590 void *buf;
591 int hash, retry, vflag;
592
593 vflag = 0;
594 if (opt_4)
595 vflag |= INP_IPV4;
596 if (opt_6)
597 vflag |= INP_IPV6;
598
599 switch (proto) {
600 case IPPROTO_TCP:
601 varname = "net.inet.tcp.pcblist";
602 protoname = "tcp";
603 break;
604 case IPPROTO_UDP:
605 varname = "net.inet.udp.pcblist";
606 protoname = "udp";
607 break;
608 case IPPROTO_DIVERT:
609 varname = "net.inet.divert.pcblist";
610 protoname = "div";
611 break;
612 default:
613 errx(1, "protocol %d not supported", proto);
614 }
615
616 buf = NULL;
617 bufsize = 8192;
618 retry = 5;
619 do {
620 for (;;) {
621 if ((buf = realloc(buf, bufsize)) == NULL)
622 err(1, "realloc()");
623 len = bufsize;
624 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
625 break;
626 if (errno == ENOENT)
627 goto out;
628 if (errno != ENOMEM || len != bufsize)
629 err(1, "sysctlbyname()");
630 bufsize *= 2;
631 }
632 xig = (struct xinpgen *)buf;
633 exig = (struct xinpgen *)(void *)
634 ((char *)buf + len - sizeof *exig);
635 if (xig->xig_len != sizeof *xig ||
636 exig->xig_len != sizeof *exig)
637 errx(1, "struct xinpgen size mismatch");
638 } while (xig->xig_gen != exig->xig_gen && retry--);
639
640 if (xig->xig_gen != exig->xig_gen && opt_v)
641 warnx("warning: data may be inconsistent");
642
643 for (;;) {
644 xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len);
645 if (xig >= exig)
646 break;
647 switch (proto) {
648 case IPPROTO_TCP:
649 xtp = (struct xtcpcb *)xig;
650 xip = &xtp->xt_inp;
651 if (xtp->xt_len != sizeof(*xtp)) {
652 warnx("struct xtcpcb size mismatch");
653 goto out;
654 }
655 protoname = xtp->t_flags & TF_TOE ? "toe" : "tcp";
656 break;
657 case IPPROTO_UDP:
658 case IPPROTO_DIVERT:
659 xip = (struct xinpcb *)xig;
660 if (xip->xi_len != sizeof(*xip)) {
661 warnx("struct xinpcb size mismatch");
662 goto out;
663 }
664 break;
665 default:
666 errx(1, "protocol %d not supported", proto);
667 }
668 so = &xip->xi_socket;
669 if ((xip->inp_vflag & vflag) == 0)
670 continue;
671 if (xip->inp_vflag & INP_IPV4) {
672 if ((xip->inp_fport == 0 && !opt_l) ||
673 (xip->inp_fport != 0 && !opt_c))
674 continue;
675 #define __IN_IS_ADDR_LOOPBACK(pina) \
676 ((ntohl((pina)->s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
677 if (opt_L &&
678 (__IN_IS_ADDR_LOOPBACK(&xip->inp_faddr) ||
679 __IN_IS_ADDR_LOOPBACK(&xip->inp_laddr)))
680 continue;
681 #undef __IN_IS_ADDR_LOOPBACK
682 } else if (xip->inp_vflag & INP_IPV6) {
683 if ((xip->inp_fport == 0 && !opt_l) ||
684 (xip->inp_fport != 0 && !opt_c))
685 continue;
686 if (opt_L &&
687 (IN6_IS_ADDR_LOOPBACK(&xip->in6p_faddr) ||
688 IN6_IS_ADDR_LOOPBACK(&xip->in6p_laddr)))
689 continue;
690 } else {
691 if (opt_v)
692 warnx("invalid vflag 0x%x", xip->inp_vflag);
693 continue;
694 }
695 if ((sock = calloc(1, sizeof(*sock))) == NULL)
696 err(1, "malloc()");
697 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
698 err(1, "malloc()");
699 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
700 err(1, "malloc()");
701 sock->socket = so->xso_so;
702 sock->proto = proto;
703 sock->inp_gencnt = xip->inp_gencnt;
704 if (xip->inp_vflag & INP_IPV4) {
705 sock->family = AF_INET;
706 sockaddr(&laddr->address, sock->family,
707 &xip->inp_laddr, xip->inp_lport);
708 sockaddr(&faddr->address, sock->family,
709 &xip->inp_faddr, xip->inp_fport);
710 } else if (xip->inp_vflag & INP_IPV6) {
711 sock->family = AF_INET6;
712 sockaddr(&laddr->address, sock->family,
713 &xip->in6p_laddr, xip->inp_lport);
714 sockaddr(&faddr->address, sock->family,
715 &xip->in6p_faddr, xip->inp_fport);
716 }
717 if (proto == IPPROTO_TCP)
718 faddr->encaps_port = xtp->xt_encaps_port;
719 laddr->next = NULL;
720 faddr->next = NULL;
721 sock->laddr = laddr;
722 sock->faddr = faddr;
723 sock->vflag = xip->inp_vflag;
724 if (proto == IPPROTO_TCP) {
725 sock->state = xtp->t_state;
726 memcpy(sock->stack, xtp->xt_stack,
727 TCP_FUNCTION_NAME_LEN_MAX);
728 memcpy(sock->cc, xtp->xt_cc, TCP_CA_NAME_MAX);
729 }
730 sock->protoname = protoname;
731 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
732 sock->next = sockhash[hash];
733 sockhash[hash] = sock;
734 }
735 out:
736 free(buf);
737 }
738
739 static void
gather_unix(int proto)740 gather_unix(int proto)
741 {
742 struct xunpgen *xug, *exug;
743 struct xunpcb *xup;
744 struct sock *sock;
745 struct addr *laddr, *faddr;
746 const char *varname, *protoname;
747 size_t len, bufsize;
748 void *buf;
749 int hash, retry;
750
751 switch (proto) {
752 case SOCK_STREAM:
753 varname = "net.local.stream.pcblist";
754 protoname = "stream";
755 break;
756 case SOCK_DGRAM:
757 varname = "net.local.dgram.pcblist";
758 protoname = "dgram";
759 break;
760 case SOCK_SEQPACKET:
761 varname = "net.local.seqpacket.pcblist";
762 protoname = "seqpac";
763 break;
764 default:
765 abort();
766 }
767 buf = NULL;
768 bufsize = 8192;
769 retry = 5;
770 do {
771 for (;;) {
772 if ((buf = realloc(buf, bufsize)) == NULL)
773 err(1, "realloc()");
774 len = bufsize;
775 if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
776 break;
777 if (errno != ENOMEM || len != bufsize)
778 err(1, "sysctlbyname()");
779 bufsize *= 2;
780 }
781 xug = (struct xunpgen *)buf;
782 exug = (struct xunpgen *)(void *)
783 ((char *)buf + len - sizeof(*exug));
784 if (xug->xug_len != sizeof(*xug) ||
785 exug->xug_len != sizeof(*exug)) {
786 warnx("struct xinpgen size mismatch");
787 goto out;
788 }
789 } while (xug->xug_gen != exug->xug_gen && retry--);
790
791 if (xug->xug_gen != exug->xug_gen && opt_v)
792 warnx("warning: data may be inconsistent");
793
794 for (;;) {
795 xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len);
796 if (xug >= exug)
797 break;
798 xup = (struct xunpcb *)xug;
799 if (xup->xu_len != sizeof(*xup)) {
800 warnx("struct xunpcb size mismatch");
801 goto out;
802 }
803 if ((xup->unp_conn == 0 && !opt_l) ||
804 (xup->unp_conn != 0 && !opt_c))
805 continue;
806 if ((sock = calloc(1, sizeof(*sock))) == NULL)
807 err(1, "malloc()");
808 if ((laddr = calloc(1, sizeof *laddr)) == NULL)
809 err(1, "malloc()");
810 if ((faddr = calloc(1, sizeof *faddr)) == NULL)
811 err(1, "malloc()");
812 sock->socket = xup->xu_socket.xso_so;
813 sock->pcb = xup->xu_unpp;
814 sock->proto = proto;
815 sock->family = AF_UNIX;
816 sock->protoname = protoname;
817 if (xup->xu_addr.sun_family == AF_UNIX)
818 laddr->address =
819 *(struct sockaddr_storage *)(void *)&xup->xu_addr;
820 else if (xup->unp_conn != 0)
821 *(kvaddr_t*)&(faddr->address) = xup->unp_conn;
822 laddr->next = NULL;
823 faddr->next = NULL;
824 sock->laddr = laddr;
825 sock->faddr = faddr;
826 hash = (int)((uintptr_t)sock->socket % HASHSIZE);
827 sock->next = sockhash[hash];
828 sockhash[hash] = sock;
829 }
830 out:
831 free(buf);
832 }
833
834 static void
getfiles(void)835 getfiles(void)
836 {
837 size_t len, olen;
838
839 olen = len = sizeof(*xfiles);
840 if ((xfiles = malloc(len)) == NULL)
841 err(1, "malloc()");
842 while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
843 if (errno != ENOMEM || len != olen)
844 err(1, "sysctlbyname()");
845 olen = len *= 2;
846 if ((xfiles = realloc(xfiles, len)) == NULL)
847 err(1, "realloc()");
848 }
849 if (len > 0 && xfiles->xf_size != sizeof(*xfiles))
850 errx(1, "struct xfile size mismatch");
851 nxfiles = len / sizeof(*xfiles);
852 }
853
854 static int
printaddr(struct sockaddr_storage * ss)855 printaddr(struct sockaddr_storage *ss)
856 {
857 struct sockaddr_un *sun;
858 char addrstr[NI_MAXHOST] = { '\0', '\0' };
859 int error, off, port = 0;
860
861 switch (ss->ss_family) {
862 case AF_INET:
863 if (sstosin(ss)->sin_addr.s_addr == INADDR_ANY)
864 addrstr[0] = '*';
865 port = ntohs(sstosin(ss)->sin_port);
866 break;
867 case AF_INET6:
868 if (IN6_IS_ADDR_UNSPECIFIED(&sstosin6(ss)->sin6_addr))
869 addrstr[0] = '*';
870 port = ntohs(sstosin6(ss)->sin6_port);
871 break;
872 case AF_UNIX:
873 sun = sstosun(ss);
874 off = (int)((char *)&sun->sun_path - (char *)sun);
875 return (xprintf("%.*s", sun->sun_len - off, sun->sun_path));
876 }
877 if (addrstr[0] == '\0') {
878 error = getnameinfo(sstosa(ss), ss->ss_len, addrstr,
879 sizeof(addrstr), NULL, 0, NI_NUMERICHOST);
880 if (error)
881 errx(1, "getnameinfo()");
882 }
883 if (port == 0)
884 return xprintf("%s:*", addrstr);
885 else
886 return xprintf("%s:%d", addrstr, port);
887 }
888
889 static const char *
getprocname(pid_t pid)890 getprocname(pid_t pid)
891 {
892 static struct kinfo_proc proc;
893 size_t len;
894 int mib[4];
895
896 mib[0] = CTL_KERN;
897 mib[1] = KERN_PROC;
898 mib[2] = KERN_PROC_PID;
899 mib[3] = (int)pid;
900 len = sizeof(proc);
901 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
902 /* Do not warn if the process exits before we get its name. */
903 if (errno != ESRCH)
904 warn("sysctl()");
905 return ("??");
906 }
907 return (proc.ki_comm);
908 }
909
910 static int
getprocjid(pid_t pid)911 getprocjid(pid_t pid)
912 {
913 static struct kinfo_proc proc;
914 size_t len;
915 int mib[4];
916
917 mib[0] = CTL_KERN;
918 mib[1] = KERN_PROC;
919 mib[2] = KERN_PROC_PID;
920 mib[3] = (int)pid;
921 len = sizeof(proc);
922 if (sysctl(mib, nitems(mib), &proc, &len, NULL, 0) == -1) {
923 /* Do not warn if the process exits before we get its jid. */
924 if (errno != ESRCH)
925 warn("sysctl()");
926 return (-1);
927 }
928 return (proc.ki_jid);
929 }
930
931 static int
check_ports(struct sock * s)932 check_ports(struct sock *s)
933 {
934 int port;
935 struct addr *addr;
936
937 if (ports == NULL)
938 return (1);
939 if ((s->family != AF_INET) && (s->family != AF_INET6))
940 return (1);
941 for (addr = s->laddr; addr != NULL; addr = addr->next) {
942 if (s->family == AF_INET)
943 port = ntohs(sstosin(&addr->address)->sin_port);
944 else
945 port = ntohs(sstosin6(&addr->address)->sin6_port);
946 if (CHK_PORT(port))
947 return (1);
948 }
949 for (addr = s->faddr; addr != NULL; addr = addr->next) {
950 if (s->family == AF_INET)
951 port = ntohs(sstosin(&addr->address)->sin_port);
952 else
953 port = ntohs(sstosin6(&addr->address)->sin6_port);
954 if (CHK_PORT(port))
955 return (1);
956 }
957 return (0);
958 }
959
960 static const char *
sctp_conn_state(int state)961 sctp_conn_state(int state)
962 {
963 switch (state) {
964 case SCTP_CLOSED:
965 return "CLOSED";
966 break;
967 case SCTP_BOUND:
968 return "BOUND";
969 break;
970 case SCTP_LISTEN:
971 return "LISTEN";
972 break;
973 case SCTP_COOKIE_WAIT:
974 return "COOKIE_WAIT";
975 break;
976 case SCTP_COOKIE_ECHOED:
977 return "COOKIE_ECHOED";
978 break;
979 case SCTP_ESTABLISHED:
980 return "ESTABLISHED";
981 break;
982 case SCTP_SHUTDOWN_SENT:
983 return "SHUTDOWN_SENT";
984 break;
985 case SCTP_SHUTDOWN_RECEIVED:
986 return "SHUTDOWN_RECEIVED";
987 break;
988 case SCTP_SHUTDOWN_ACK_SENT:
989 return "SHUTDOWN_ACK_SENT";
990 break;
991 case SCTP_SHUTDOWN_PENDING:
992 return "SHUTDOWN_PENDING";
993 break;
994 default:
995 return "UNKNOWN";
996 break;
997 }
998 }
999
1000 static const char *
sctp_path_state(int state)1001 sctp_path_state(int state)
1002 {
1003 switch (state) {
1004 case SCTP_UNCONFIRMED:
1005 return "UNCONFIRMED";
1006 break;
1007 case SCTP_ACTIVE:
1008 return "ACTIVE";
1009 break;
1010 case SCTP_INACTIVE:
1011 return "INACTIVE";
1012 break;
1013 default:
1014 return "UNKNOWN";
1015 break;
1016 }
1017 }
1018
1019 static void
displaysock(struct sock * s,int pos)1020 displaysock(struct sock *s, int pos)
1021 {
1022 kvaddr_t p;
1023 int hash, first, offset;
1024 struct addr *laddr, *faddr;
1025 struct sock *s_tmp;
1026
1027 while (pos < 29)
1028 pos += xprintf(" ");
1029 pos += xprintf("%s", s->protoname);
1030 if (s->vflag & INP_IPV4)
1031 pos += xprintf("4");
1032 if (s->vflag & INP_IPV6)
1033 pos += xprintf("6");
1034 if (s->vflag & (INP_IPV4 | INP_IPV6))
1035 pos += xprintf(" ");
1036 laddr = s->laddr;
1037 faddr = s->faddr;
1038 first = 1;
1039 while (laddr != NULL || faddr != NULL) {
1040 offset = 36;
1041 while (pos < offset)
1042 pos += xprintf(" ");
1043 switch (s->family) {
1044 case AF_INET:
1045 case AF_INET6:
1046 if (laddr != NULL) {
1047 pos += printaddr(&laddr->address);
1048 if (s->family == AF_INET6 && pos >= 58)
1049 pos += xprintf(" ");
1050 }
1051 offset += opt_w ? 46 : 22;
1052 while (pos < offset)
1053 pos += xprintf(" ");
1054 if (faddr != NULL)
1055 pos += printaddr(&faddr->address);
1056 offset += opt_w ? 46 : 22;
1057 break;
1058 case AF_UNIX:
1059 if ((laddr == NULL) || (faddr == NULL))
1060 errx(1, "laddr = %p or faddr = %p is NULL",
1061 (void *)laddr, (void *)faddr);
1062 /* server */
1063 if (laddr->address.ss_len > 0) {
1064 pos += printaddr(&laddr->address);
1065 break;
1066 }
1067 /* client */
1068 p = *(kvaddr_t*)&(faddr->address);
1069 if (p == 0) {
1070 pos += xprintf("(not connected)");
1071 offset += opt_w ? 92 : 44;
1072 break;
1073 }
1074 pos += xprintf("-> ");
1075 for (hash = 0; hash < HASHSIZE; ++hash) {
1076 for (s_tmp = sockhash[hash];
1077 s_tmp != NULL;
1078 s_tmp = s_tmp->next)
1079 if (s_tmp->pcb == p)
1080 break;
1081 if (s_tmp != NULL)
1082 break;
1083 }
1084 if (s_tmp == NULL || s_tmp->laddr == NULL ||
1085 s_tmp->laddr->address.ss_len == 0)
1086 pos += xprintf("??");
1087 else
1088 pos += printaddr(&s_tmp->laddr->address);
1089 offset += opt_w ? 92 : 44;
1090 break;
1091 default:
1092 abort();
1093 }
1094 if (opt_i) {
1095 if (s->proto == IPPROTO_TCP ||
1096 s->proto == IPPROTO_UDP) {
1097 while (pos < offset)
1098 pos += xprintf(" ");
1099 pos += xprintf("%" PRIu64, s->inp_gencnt);
1100 }
1101 offset += 9;
1102 }
1103 if (opt_U) {
1104 if (faddr != NULL &&
1105 ((s->proto == IPPROTO_SCTP &&
1106 s->state != SCTP_CLOSED &&
1107 s->state != SCTP_BOUND &&
1108 s->state != SCTP_LISTEN) ||
1109 (s->proto == IPPROTO_TCP &&
1110 s->state != TCPS_CLOSED &&
1111 s->state != TCPS_LISTEN))) {
1112 while (pos < offset)
1113 pos += xprintf(" ");
1114 pos += xprintf("%u",
1115 ntohs(faddr->encaps_port));
1116 }
1117 offset += 7;
1118 }
1119 if (opt_s) {
1120 if (faddr != NULL &&
1121 s->proto == IPPROTO_SCTP &&
1122 s->state != SCTP_CLOSED &&
1123 s->state != SCTP_BOUND &&
1124 s->state != SCTP_LISTEN) {
1125 while (pos < offset)
1126 pos += xprintf(" ");
1127 pos += xprintf("%s",
1128 sctp_path_state(faddr->state));
1129 }
1130 offset += 13;
1131 }
1132 if (first) {
1133 if (opt_s) {
1134 if (s->proto == IPPROTO_SCTP ||
1135 s->proto == IPPROTO_TCP) {
1136 while (pos < offset)
1137 pos += xprintf(" ");
1138 switch (s->proto) {
1139 case IPPROTO_SCTP:
1140 pos += xprintf("%s",
1141 sctp_conn_state(s->state));
1142 break;
1143 case IPPROTO_TCP:
1144 if (s->state >= 0 &&
1145 s->state < TCP_NSTATES)
1146 pos += xprintf("%s",
1147 tcpstates[s->state]);
1148 else
1149 pos += xprintf("?");
1150 break;
1151 }
1152 }
1153 offset += 13;
1154 }
1155 if (opt_S) {
1156 if (s->proto == IPPROTO_TCP) {
1157 while (pos < offset)
1158 pos += xprintf(" ");
1159 pos += xprintf("%.*s",
1160 TCP_FUNCTION_NAME_LEN_MAX,
1161 s->stack);
1162 }
1163 offset += TCP_FUNCTION_NAME_LEN_MAX + 1;
1164 }
1165 if (opt_C) {
1166 if (s->proto == IPPROTO_TCP) {
1167 while (pos < offset)
1168 pos += xprintf(" ");
1169 xprintf("%.*s", TCP_CA_NAME_MAX, s->cc);
1170 }
1171 offset += TCP_CA_NAME_MAX + 1;
1172 }
1173 }
1174 if (laddr != NULL)
1175 laddr = laddr->next;
1176 if (faddr != NULL)
1177 faddr = faddr->next;
1178 if ((laddr != NULL) || (faddr != NULL)) {
1179 xprintf("\n");
1180 pos = 0;
1181 }
1182 first = 0;
1183 }
1184 xprintf("\n");
1185 }
1186
1187 static void
display(void)1188 display(void)
1189 {
1190 struct passwd *pwd;
1191 struct xfile *xf;
1192 struct sock *s;
1193 int hash, n, pos;
1194
1195 if (opt_q != 1) {
1196 printf("%-8s %-10s %-5s %-2s %-6s %-*s %-*s",
1197 "USER", "COMMAND", "PID", "FD", "PROTO",
1198 opt_w ? 45 : 21, "LOCAL ADDRESS",
1199 opt_w ? 45 : 21, "FOREIGN ADDRESS");
1200 if (opt_i)
1201 printf(" %-8s", "ID");
1202 if (opt_U)
1203 printf(" %-6s", "ENCAPS");
1204 if (opt_s) {
1205 printf(" %-12s", "PATH STATE");
1206 printf(" %-12s", "CONN STATE");
1207 }
1208 if (opt_S)
1209 printf(" %-*.*s", TCP_FUNCTION_NAME_LEN_MAX,
1210 TCP_FUNCTION_NAME_LEN_MAX, "STACK");
1211 if (opt_C)
1212 printf(" %-.*s", TCP_CA_NAME_MAX, "CC");
1213 printf("\n");
1214 }
1215 setpassent(1);
1216 for (xf = xfiles, n = 0; n < nxfiles; ++n, ++xf) {
1217 if (xf->xf_data == 0)
1218 continue;
1219 if (opt_j >= 0 && opt_j != getprocjid(xf->xf_pid))
1220 continue;
1221 hash = (int)((uintptr_t)xf->xf_data % HASHSIZE);
1222 for (s = sockhash[hash]; s != NULL; s = s->next) {
1223 if (s->socket != xf->xf_data)
1224 continue;
1225 if (!check_ports(s))
1226 continue;
1227 s->shown = 1;
1228 pos = 0;
1229 if (opt_n || (pwd = getpwuid(xf->xf_uid)) == NULL)
1230 pos += xprintf("%lu ", (u_long)xf->xf_uid);
1231 else
1232 pos += xprintf("%s ", pwd->pw_name);
1233 while (pos < 9)
1234 pos += xprintf(" ");
1235 pos += xprintf("%.10s", getprocname(xf->xf_pid));
1236 while (pos < 20)
1237 pos += xprintf(" ");
1238 pos += xprintf("%lu ", (u_long)xf->xf_pid);
1239 while (pos < 26)
1240 pos += xprintf(" ");
1241 pos += xprintf("%d ", xf->xf_fd);
1242 displaysock(s, pos);
1243 }
1244 }
1245 if (opt_j >= 0)
1246 return;
1247 for (hash = 0; hash < HASHSIZE; hash++) {
1248 for (s = sockhash[hash]; s != NULL; s = s->next) {
1249 if (s->shown)
1250 continue;
1251 if (!check_ports(s))
1252 continue;
1253 pos = 0;
1254 pos += xprintf("%-8s %-10s %-5s %-2s ",
1255 "?", "?", "?", "?");
1256 displaysock(s, pos);
1257 }
1258 }
1259 }
1260
1261 static int
set_default_protos(void)1262 set_default_protos(void)
1263 {
1264 struct protoent *prot;
1265 const char *pname;
1266 size_t pindex;
1267
1268 init_protos(default_numprotos);
1269
1270 for (pindex = 0; pindex < default_numprotos; pindex++) {
1271 pname = default_protos[pindex];
1272 prot = getprotobyname(pname);
1273 if (prot == NULL)
1274 err(1, "getprotobyname: %s", pname);
1275 protos[pindex] = prot->p_proto;
1276 }
1277 numprotos = pindex;
1278 return (pindex);
1279 }
1280
1281 /*
1282 * Return the vnet property of the jail, or -1 on error.
1283 */
1284 static int
jail_getvnet(int jid)1285 jail_getvnet(int jid)
1286 {
1287 struct iovec jiov[6];
1288 int vnet;
1289
1290 vnet = -1;
1291 jiov[0].iov_base = __DECONST(char *, "jid");
1292 jiov[0].iov_len = sizeof("jid");
1293 jiov[1].iov_base = &jid;
1294 jiov[1].iov_len = sizeof(jid);
1295 jiov[2].iov_base = __DECONST(char *, "vnet");
1296 jiov[2].iov_len = sizeof("vnet");
1297 jiov[3].iov_base = &vnet;
1298 jiov[3].iov_len = sizeof(vnet);
1299 jiov[4].iov_base = __DECONST(char *, "errmsg");
1300 jiov[4].iov_len = sizeof("errmsg");
1301 jiov[5].iov_base = jail_errmsg;
1302 jiov[5].iov_len = JAIL_ERRMSGLEN;
1303 jail_errmsg[0] = '\0';
1304 if (jail_get(jiov, nitems(jiov), 0) < 0) {
1305 if (!jail_errmsg[0])
1306 snprintf(jail_errmsg, JAIL_ERRMSGLEN,
1307 "jail_get: %s", strerror(errno));
1308 return (-1);
1309 }
1310 return (vnet);
1311 }
1312
1313 static void
usage(void)1314 usage(void)
1315 {
1316 fprintf(stderr,
1317 "usage: sockstat [-46CciLlnqSsUuvw] [-j jid] [-p ports] [-P protocols]\n");
1318 exit(1);
1319 }
1320
1321 int
main(int argc,char * argv[])1322 main(int argc, char *argv[])
1323 {
1324 int protos_defined = -1;
1325 int o, i;
1326
1327 opt_j = -1;
1328 while ((o = getopt(argc, argv, "46Ccij:Llnp:P:qSsUuvw")) != -1)
1329 switch (o) {
1330 case '4':
1331 opt_4 = 1;
1332 break;
1333 case '6':
1334 opt_6 = 1;
1335 break;
1336 case 'C':
1337 opt_C = 1;
1338 break;
1339 case 'c':
1340 opt_c = 1;
1341 break;
1342 case 'i':
1343 opt_i = 1;
1344 break;
1345 case 'j':
1346 opt_j = jail_getid(optarg);
1347 if (opt_j < 0)
1348 errx(1, "%s", jail_errmsg);
1349 break;
1350 case 'L':
1351 opt_L = 1;
1352 break;
1353 case 'l':
1354 opt_l = 1;
1355 break;
1356 case 'n':
1357 opt_n = 1;
1358 break;
1359 case 'p':
1360 parse_ports(optarg);
1361 break;
1362 case 'P':
1363 protos_defined = parse_protos(optarg);
1364 break;
1365 case 'q':
1366 opt_q = 1;
1367 break;
1368 case 'S':
1369 opt_S = 1;
1370 break;
1371 case 's':
1372 opt_s = 1;
1373 break;
1374 case 'U':
1375 opt_U = 1;
1376 break;
1377 case 'u':
1378 opt_u = 1;
1379 break;
1380 case 'v':
1381 ++opt_v;
1382 break;
1383 case 'w':
1384 opt_w = 1;
1385 break;
1386 default:
1387 usage();
1388 }
1389
1390 argc -= optind;
1391 argv += optind;
1392
1393 if (argc > 0)
1394 usage();
1395
1396 if (opt_j > 0) {
1397 switch (jail_getvnet(opt_j)) {
1398 case -1:
1399 errx(2, "%s", jail_errmsg);
1400 case JAIL_SYS_NEW:
1401 if (jail_attach(opt_j) < 0)
1402 err(3, "jail_attach()");
1403 /* Set back to -1 for normal output in vnet jail. */
1404 opt_j = -1;
1405 break;
1406 default:
1407 break;
1408 }
1409 }
1410
1411 if ((!opt_4 && !opt_6) && protos_defined != -1)
1412 opt_4 = opt_6 = 1;
1413 if (!opt_4 && !opt_6 && !opt_u)
1414 opt_4 = opt_6 = opt_u = 1;
1415 if ((opt_4 || opt_6) && protos_defined == -1)
1416 protos_defined = set_default_protos();
1417 if (!opt_c && !opt_l)
1418 opt_c = opt_l = 1;
1419
1420 if (opt_4 || opt_6) {
1421 for (i = 0; i < protos_defined; i++)
1422 if (protos[i] == IPPROTO_SCTP)
1423 gather_sctp();
1424 else
1425 gather_inet(protos[i]);
1426 }
1427
1428 if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) {
1429 gather_unix(SOCK_STREAM);
1430 gather_unix(SOCK_DGRAM);
1431 gather_unix(SOCK_SEQPACKET);
1432 }
1433 getfiles();
1434 display();
1435 exit(0);
1436 }
1437