Annotation of src/usr.bin/netstat/main.c, Revision 1.122
1.122 ! kn 1: /* $OpenBSD: main.c,v 1.121 2021/01/26 18:22:45 deraadt Exp $ */
1.2 deraadt 2: /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
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.
1.36 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.103 deraadt 33: #include <sys/types.h>
1.1 deraadt 34: #include <sys/protosw.h>
35: #include <sys/socket.h>
1.86 claudio 36: #include <sys/sysctl.h>
1.1 deraadt 37:
1.68 claudio 38: #include <net/route.h>
1.1 deraadt 39: #include <netinet/in.h>
40:
41: #include <ctype.h>
1.61 djm 42: #include <err.h>
1.1 deraadt 43: #include <errno.h>
1.96 guenther 44: #include <fcntl.h>
1.1 deraadt 45: #include <kvm.h>
46: #include <limits.h>
47: #include <netdb.h>
48: #include <nlist.h>
49: #include <paths.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <unistd.h>
54: #include "netstat.h"
55:
56: struct nlist nl[] = {
1.116 mpi 57: #define N_AFMAP 0
58: { "_afmap"},
59: #define N_AF2IDX 1
60: { "_af2idx" },
61: #define N_AF2IDXMAX 2
62: { "_af2idx_max" },
1.71 deraadt 63:
1.87 bluhm 64: { "" }
1.1 deraadt 65: };
66:
67: struct protox {
1.87 bluhm 68: void (*pr_stats)(char *); /* statistics printing routine */
69: char *pr_name; /* well-known name */
1.106 claudio 70: int pr_proto; /* protocol number */
1.1 deraadt 71: } protox[] = {
1.106 claudio 72: { ip_stats, "ip", IPPROTO_IPV4 },
73: { icmp_stats, "icmp", 0 },
74: { igmp_stats, "igmp", 0 },
75: { ipip_stats, "ipencap", 0 },
76: { tcp_stats, "tcp", IPPROTO_TCP },
77: { udp_stats, "udp", IPPROTO_UDP },
1.113 mpi 78: { ipsec_stats, "ipsec", 0 },
1.106 claudio 79: { esp_stats, "esp", 0 },
80: { ah_stats, "ah", 0 },
81: { etherip_stats,"etherip", 0 },
82: { ipcomp_stats, "ipcomp", 0 },
83: { carp_stats, "carp", 0 },
84: { pfsync_stats, "pfsync", 0 },
85: { div_stats, "divert", IPPROTO_DIVERT },
86: { pflow_stats, "pflow", 0 },
87: { NULL, NULL, 0 }
1.1 deraadt 88: };
89:
1.19 itojun 90: struct protox ip6protox[] = {
1.106 claudio 91: { ip6_stats, "ip6", IPPROTO_IPV6 },
92: { div6_stats, "divert6", IPPROTO_DIVERT },
93: { icmp6_stats, "icmp6", 0 },
94: { rip6_stats, "rip6", 0 },
95: { NULL, NULL, 0 }
1.19 itojun 96: };
97:
1.34 deraadt 98: struct protox *protoprotox[] = {
1.89 henning 99: protox, ip6protox, NULL
1.34 deraadt 100: };
1.1 deraadt 101:
1.30 millert 102: static void usage(void);
103: static struct protox *name2protox(char *);
104: static struct protox *knownname(char *);
1.116 mpi 105: void gettable(u_int);
1.86 claudio 106:
1.1 deraadt 107: kvm_t *kvmd;
1.121 deraadt 108:
109: int Aflag; /* show addresses of protocol control block */
110: int aflag; /* show all sockets (including servers) */
111: int Bflag; /* show TCP send and receive buffer sizes */
112: int bflag; /* show bytes instead of packets */
113: int dflag; /* show i/f dropped packets */
114: int Fflag; /* show routes whose gateways are in specified AF */
115: int gflag; /* show group (multicast) routing or stats */
116: int hflag; /* print human numbers */
117: int iflag; /* show interfaces */
118: int lflag; /* show only listening sockets (only servers), */
119: /* with -g, show routing table with use and ref */
120: int mflag; /* show memory stats */
121: int nflag; /* show addresses numerically */
122: int pflag; /* show given protocol */
123: int Pflag; /* show given PCB */
124: int qflag; /* only display non-zero values for output */
125: int rflag; /* show routing tables (or routing stats) */
126: int Rflag; /* show rdomain and rtable summary */
127: int sflag; /* show protocol statistics */
128: int vflag; /* be verbose */
129: int Wflag; /* show net80211 protocol statistics */
130:
131: int interval; /* repeat interval for i/f stats */
132:
133: char *interface; /* desired i/f for stats, or NULL for all i/fs */
134:
135: int af; /* address family */
1.1 deraadt 136:
137: int
1.34 deraadt 138: main(int argc, char *argv[])
1.1 deraadt 139: {
140: extern char *optarg;
141: extern int optind;
1.68 claudio 142: const char *errstr;
1.28 mpech 143: struct protox *tp = NULL; /* for printing cblocks & stats */
1.1 deraadt 144: int ch;
1.59 markus 145: char *nlistf = NULL, *memf = NULL, *ep;
1.1 deraadt 146: char buf[_POSIX2_LINE_MAX];
1.59 markus 147: u_long pcbaddr = 0;
1.91 mikeb 148: u_int tableid;
1.86 claudio 149: int Tflag = 0;
1.82 tedu 150: int repeatcount = 0;
1.106 claudio 151: int proto = 0;
1.109 mpi 152: int need_nlist, kvm_flags = O_RDONLY;
1.95 deraadt 153:
1.1 deraadt 154: af = AF_UNSPEC;
1.91 mikeb 155: tableid = getrtable();
1.1 deraadt 156:
1.88 jsing 157: while ((ch = getopt(argc, argv,
1.122 ! kn 158: "AaBbc:deFf:ghI:iLlM:mN:np:P:qRrsT:uvW:w:")) != -1)
1.31 deraadt 159: switch (ch) {
1.1 deraadt 160: case 'A':
161: Aflag = 1;
162: break;
163: case 'a':
164: aflag = 1;
1.24 camield 165: break;
1.88 jsing 166: case 'B':
167: Bflag = 1;
168: break;
1.24 camield 169: case 'b':
170: bflag = 1;
1.1 deraadt 171: break;
1.82 tedu 172: case 'c':
173: repeatcount = strtonum(optarg, 1, INT_MAX, &errstr);
1.100 tedu 174: if (errstr)
175: errx(1, "count is %s", errstr);
1.82 tedu 176: break;
1.1 deraadt 177: case 'd':
1.114 dlg 178: dflag = IF_SHOW_DROP;
179: break;
180: case 'e':
181: dflag = IF_SHOW_ERRS;
1.64 pyr 182: break;
183: case 'F':
184: Fflag = 1;
1.1 deraadt 185: break;
186: case 'f':
1.4 mickey 187: if (strcmp(optarg, "inet") == 0)
1.1 deraadt 188: af = AF_INET;
1.19 itojun 189: else if (strcmp(optarg, "inet6") == 0)
190: af = AF_INET6;
1.7 kstailey 191: else if (strcmp(optarg, "local") == 0)
192: af = AF_LOCAL;
1.1 deraadt 193: else if (strcmp(optarg, "unix") == 0)
194: af = AF_UNIX;
1.75 claudio 195: else if (strcmp(optarg, "mpls") == 0)
196: af = AF_MPLS;
1.1 deraadt 197: else {
198: (void)fprintf(stderr,
199: "%s: %s: unknown address family\n",
1.2 deraadt 200: __progname, optarg);
1.1 deraadt 201: exit(1);
202: }
203: break;
204: case 'g':
205: gflag = 1;
1.93 tedu 206: break;
207: case 'h':
208: hflag = 1;
1.1 deraadt 209: break;
1.2 deraadt 210: case 'I':
1.1 deraadt 211: iflag = 1;
1.2 deraadt 212: interface = optarg;
1.1 deraadt 213: break;
214: case 'i':
215: iflag = 1;
216: break;
1.19 itojun 217: case 'l':
218: lflag = 1;
219: break;
1.1 deraadt 220: case 'M':
221: memf = optarg;
222: break;
223: case 'm':
224: mflag = 1;
225: break;
226: case 'N':
227: nlistf = optarg;
228: break;
229: case 'n':
230: nflag = 1;
231: break;
232: case 'p':
233: if ((tp = name2protox(optarg)) == NULL) {
234: (void)fprintf(stderr,
1.42 jmc 235: "%s: %s: unknown protocol\n",
1.2 deraadt 236: __progname, optarg);
1.1 deraadt 237: exit(1);
238: }
239: pflag = 1;
1.27 brian 240: break;
1.59 markus 241: case 'P':
242: errno = 0;
243: pcbaddr = strtoul(optarg, &ep, 16);
244: if (optarg[0] == '\0' || *ep != '\0' ||
245: errno == ERANGE) {
246: (void)fprintf(stderr,
247: "%s: %s: invalid PCB address\n",
248: __progname, optarg);
249: exit(1);
250: }
251: Pflag = 1;
252: break;
1.27 brian 253: case 'q':
254: qflag = 1;
1.1 deraadt 255: break;
1.117 remi 256: case 'R':
257: Rflag = 1;
258: break;
1.1 deraadt 259: case 'r':
260: rflag = 1;
1.46 cedric 261: break;
1.1 deraadt 262: case 's':
263: ++sflag;
264: break;
1.68 claudio 265: case 'T':
1.116 mpi 266: tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
267: if (errstr)
268: errx(1, "invalid table id: %s", errstr);
1.86 claudio 269: Tflag = 1;
1.68 claudio 270: break;
1.1 deraadt 271: case 'u':
272: af = AF_UNIX;
1.13 peter 273: break;
274: case 'v':
275: vflag = 1;
1.1 deraadt 276: break;
1.56 reyk 277: case 'W':
278: Wflag = 1;
279: interface = optarg;
280: break;
1.1 deraadt 281: case 'w':
1.100 tedu 282: interval = strtonum(optarg, 1, INT_MAX, &errstr);
283: if (errstr)
284: errx(1, "interval is %s", errstr);
1.1 deraadt 285: iflag = 1;
286: break;
287: case '?':
288: default:
289: usage();
290: }
291: argv += optind;
292: argc -= optind;
293:
1.108 tedu 294: if (argc) {
295: interval = strtonum(*argv, 1, INT_MAX, &errstr);
296: if (errstr)
297: errx(1, "interval is %s", errstr);
298: ++argv;
299: --argc;
300: iflag = 1;
1.84 lum 301: }
1.107 tedu 302: if (argc)
303: usage();
1.84 lum 304:
1.56 reyk 305: /*
1.106 claudio 306: * Show per-interface statistics which don't need access to
307: * kernel memory (they're using IOCTLs)
1.33 deraadt 308: */
1.106 claudio 309: if (Wflag) {
310: if (interface == NULL)
311: usage();
312: net80211_ifstats(interface);
313: exit(0);
1.33 deraadt 314: }
1.61 djm 315:
1.1 deraadt 316: if (mflag) {
1.70 deraadt 317: mbpr();
1.1 deraadt 318: exit(0);
319: }
320: if (iflag) {
1.82 tedu 321: intpr(interval, repeatcount);
1.1 deraadt 322: exit(0);
323: }
1.106 claudio 324: if (sflag) {
325: if (rflag) {
1.73 claudio 326: rt_stats();
1.106 claudio 327: } else if (gflag) {
1.19 itojun 328: if (af == AF_INET || af == AF_UNSPEC)
1.71 deraadt 329: mrt_stats();
1.19 itojun 330: if (af == AF_INET6 || af == AF_UNSPEC)
1.71 deraadt 331: mrt6_stats();
1.106 claudio 332: } else if (pflag && tp->pr_name) {
333: (*tp->pr_stats)(tp->pr_name);
1.70 deraadt 334: } else {
1.19 itojun 335: if (af == AF_INET || af == AF_UNSPEC)
1.106 claudio 336: for (tp = protox; tp->pr_name; tp++)
337: (*tp->pr_stats)(tp->pr_name);
1.19 itojun 338: if (af == AF_INET6 || af == AF_UNSPEC)
1.106 claudio 339: for (tp = ip6protox; tp->pr_name; tp++)
340: (*tp->pr_stats)(tp->pr_name);
1.19 itojun 341: }
1.1 deraadt 342: exit(0);
343: }
1.106 claudio 344: if (gflag) {
345: if (af == AF_INET || af == AF_UNSPEC)
346: mroutepr();
347: if (af == AF_INET6 || af == AF_UNSPEC)
348: mroute6pr();
1.117 remi 349: exit(0);
350: }
351:
352: if (Rflag) {
353: rdomainpr();
1.106 claudio 354: exit(0);
355: }
356:
357: /*
358: * The remaining code may need kvm so lets try to open it.
359: * -r and -P are the only bits left that actually can use this.
360: */
1.109 mpi 361: need_nlist = (nlistf != NULL) || (memf != NULL) || (Aflag && rflag);
362: if (!need_nlist && !Pflag)
363: kvm_flags |= KVM_NO_FILES;
1.106 claudio 364:
1.109 mpi 365: if ((kvmd = kvm_openfiles(nlistf, memf, NULL, kvm_flags, buf)) == NULL)
1.106 claudio 366: errx(1, "kvm_openfiles: %s", buf);
367:
368: if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) {
369: if (nlistf)
370: errx(1, "%s: no namelist", nlistf);
371: else
372: errx(1, "no namelist");
1.76 gollo 373: }
1.106 claudio 374:
1.116 mpi 375: if (!need_nlist && Tflag)
376: gettable(tableid);
377:
1.106 claudio 378: if (rflag) {
379: if (Aflag || nlistf != NULL || memf != NULL)
1.116 mpi 380: routepr(nl[N_AFMAP].n_value, nl[N_AF2IDX].n_value,
381: nl[N_AF2IDXMAX].n_value, tableid);
1.106 claudio 382: else
383: p_rttables(af, tableid);
384: exit(0);
1.1 deraadt 385: }
386:
1.106 claudio 387: if (pflag) {
388: if (tp->pr_proto == 0)
389: errx(1, "no protocol handler for protocol %s",
390: tp->pr_name);
391: else
392: proto = tp->pr_proto;
1.1 deraadt 393: }
1.106 claudio 394:
395: protopr(kvmd, pcbaddr, tableid, proto);
396: exit(0);
1.1 deraadt 397: }
398:
399: /*
400: * Read kernel memory, return 0 on success.
401: */
402: int
1.53 jaredy 403: kread(u_long addr, void *buf, int size)
1.1 deraadt 404: {
405:
406: if (kvm_read(kvmd, addr, buf, size) != size) {
1.2 deraadt 407: (void)fprintf(stderr, "%s: %s\n", __progname,
1.1 deraadt 408: kvm_geterr(kvmd));
409: return (-1);
410: }
411: return (0);
412: }
413:
414: char *
1.77 claudio 415: plural(u_int64_t n)
1.1 deraadt 416: {
417: return (n != 1 ? "s" : "");
418: }
419:
420: char *
1.77 claudio 421: plurales(u_int64_t n)
1.1 deraadt 422: {
423: return (n != 1 ? "es" : "");
1.110 bluhm 424: }
425:
426: char *
427: pluralys(u_int64_t n)
428: {
429: return (n != 1 ? "ies" : "y");
1.1 deraadt 430: }
431:
432: /*
433: * Find the protox for the given "well-known" name.
434: */
435: static struct protox *
1.34 deraadt 436: knownname(char *name)
1.1 deraadt 437: {
438: struct protox **tpp, *tp;
439:
440: for (tpp = protoprotox; *tpp; tpp++)
441: for (tp = *tpp; tp->pr_name; tp++)
442: if (strcmp(tp->pr_name, name) == 0)
443: return (tp);
444: return (NULL);
445: }
446:
447: /*
448: * Find the protox corresponding to name.
449: */
450: static struct protox *
1.34 deraadt 451: name2protox(char *name)
1.1 deraadt 452: {
453: struct protox *tp;
454: char **alias; /* alias from p->aliases */
455: struct protoent *p;
456:
457: /*
458: * Try to find the name in the list of "well-known" names. If that
459: * fails, check if name is an alias for an Internet protocol.
460: */
1.11 millert 461: if ((tp = knownname(name)))
1.1 deraadt 462: return (tp);
463:
464: setprotoent(1); /* make protocol lookup cheaper */
1.11 millert 465: while ((p = getprotoent())) {
1.1 deraadt 466: /* assert: name not same as p->name */
467: for (alias = p->p_aliases; *alias; alias++)
468: if (strcmp(name, *alias) == 0) {
469: endprotoent();
470: return (knownname(p->p_name));
471: }
472: }
473: endprotoent();
474: return (NULL);
475: }
476:
477: static void
1.34 deraadt 478: usage(void)
1.1 deraadt 479: {
480: (void)fprintf(stderr,
1.120 jmc 481: "usage: netstat [-AaBln] [-M core] [-N system] [-p protocol] [-T rtable]\n"
482: " netstat -W interface\n"
483: " netstat -m\n"
1.122 ! kn 484: " netstat -I interface | -i [-bdehnq]\n"
! 485: " netstat -w wait [-bdehnq] [-c count] [-I interface]\n"
1.120 jmc 486: " netstat -s [-gru] [-f address_family] [-p protocol]\n"
487: " netstat -g [-lnu] [-f address_family]\n"
488: " netstat -R\n"
489: " netstat -r [-AFu] [-f address_family] [-M core] [-N system] [-p protocol]\n"
1.115 jmc 490: " [-T rtable]\n"
1.120 jmc 491: " netstat -P pcbaddr [-v] [-M core] [-N system]\n");
1.1 deraadt 492: exit(1);
493: }
1.86 claudio 494:
1.116 mpi 495: void
496: gettable(u_int tableid)
1.86 claudio 497: {
498: struct rt_tableinfo info;
499: int mib[6];
500: size_t len;
501:
502: mib[0] = CTL_NET;
1.101 guenther 503: mib[1] = PF_ROUTE;
1.86 claudio 504: mib[2] = 0;
505: mib[3] = 0;
506: mib[4] = NET_RT_TABLE;
507: mib[5] = tableid;
508:
509: len = sizeof(info);
510: if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
1.97 deraadt 511: err(1, "routing table %d", tableid);
1.86 claudio 512: }