Annotation of src/usr.bin/netstat/main.c, Revision 1.113
1.113 ! mpi 1: /* $OpenBSD: main.c,v 1.112 2017/08/12 03:21:02 benno 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.106 claudio 57: #define N_RTREE 0
1.71 deraadt 58: { "_rt_tables"},
1.106 claudio 59: #define N_RTMASK 1
1.71 deraadt 60: { "_mask_rnhead" },
1.106 claudio 61: #define N_AF2RTAFIDX 2
1.71 deraadt 62: { "_af2rtafidx" },
1.106 claudio 63: #define N_RTBLIDMAX 3
1.71 deraadt 64: { "_rtbl_id_max" },
65:
1.87 bluhm 66: { "" }
1.1 deraadt 67: };
68:
69: struct protox {
1.87 bluhm 70: void (*pr_stats)(char *); /* statistics printing routine */
71: char *pr_name; /* well-known name */
1.106 claudio 72: int pr_proto; /* protocol number */
1.1 deraadt 73: } protox[] = {
1.106 claudio 74: { ip_stats, "ip", IPPROTO_IPV4 },
75: { icmp_stats, "icmp", 0 },
76: { igmp_stats, "igmp", 0 },
77: { ipip_stats, "ipencap", 0 },
78: { tcp_stats, "tcp", IPPROTO_TCP },
79: { udp_stats, "udp", IPPROTO_UDP },
1.113 ! mpi 80: { ipsec_stats, "ipsec", 0 },
1.106 claudio 81: { esp_stats, "esp", 0 },
82: { ah_stats, "ah", 0 },
83: { etherip_stats,"etherip", 0 },
84: { ipcomp_stats, "ipcomp", 0 },
85: { carp_stats, "carp", 0 },
86: { pfsync_stats, "pfsync", 0 },
87: { div_stats, "divert", IPPROTO_DIVERT },
88: { pflow_stats, "pflow", 0 },
89: { NULL, NULL, 0 }
1.1 deraadt 90: };
91:
1.19 itojun 92: struct protox ip6protox[] = {
1.106 claudio 93: { ip6_stats, "ip6", IPPROTO_IPV6 },
94: { div6_stats, "divert6", IPPROTO_DIVERT },
95: { icmp6_stats, "icmp6", 0 },
96: { rip6_stats, "rip6", 0 },
97: { NULL, NULL, 0 }
1.19 itojun 98: };
99:
1.34 deraadt 100: struct protox *protoprotox[] = {
1.89 henning 101: protox, ip6protox, NULL
1.34 deraadt 102: };
1.1 deraadt 103:
1.30 millert 104: static void usage(void);
105: static struct protox *name2protox(char *);
106: static struct protox *knownname(char *);
1.86 claudio 107: u_int gettable(const char *);
108:
1.1 deraadt 109: kvm_t *kvmd;
110:
111: int
1.34 deraadt 112: main(int argc, char *argv[])
1.1 deraadt 113: {
114: extern char *optarg;
115: extern int optind;
1.68 claudio 116: const char *errstr;
1.28 mpech 117: struct protox *tp = NULL; /* for printing cblocks & stats */
1.1 deraadt 118: int ch;
1.59 markus 119: char *nlistf = NULL, *memf = NULL, *ep;
1.1 deraadt 120: char buf[_POSIX2_LINE_MAX];
1.59 markus 121: u_long pcbaddr = 0;
1.91 mikeb 122: u_int tableid;
1.86 claudio 123: int Tflag = 0;
1.82 tedu 124: int repeatcount = 0;
1.106 claudio 125: int proto = 0;
1.109 mpi 126: int need_nlist, kvm_flags = O_RDONLY;
1.95 deraadt 127:
1.1 deraadt 128: af = AF_UNSPEC;
1.91 mikeb 129: tableid = getrtable();
1.1 deraadt 130:
1.88 jsing 131: while ((ch = getopt(argc, argv,
1.112 benno 132: "AaBbc:dFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1)
1.31 deraadt 133: switch (ch) {
1.1 deraadt 134: case 'A':
135: Aflag = 1;
136: break;
137: case 'a':
138: aflag = 1;
1.24 camield 139: break;
1.88 jsing 140: case 'B':
141: Bflag = 1;
142: break;
1.24 camield 143: case 'b':
144: bflag = 1;
1.1 deraadt 145: break;
1.82 tedu 146: case 'c':
147: repeatcount = strtonum(optarg, 1, INT_MAX, &errstr);
1.100 tedu 148: if (errstr)
149: errx(1, "count is %s", errstr);
1.82 tedu 150: break;
1.1 deraadt 151: case 'd':
152: dflag = 1;
1.64 pyr 153: break;
154: case 'F':
155: Fflag = 1;
1.1 deraadt 156: break;
157: case 'f':
1.4 mickey 158: if (strcmp(optarg, "inet") == 0)
1.1 deraadt 159: af = AF_INET;
1.19 itojun 160: else if (strcmp(optarg, "inet6") == 0)
161: af = AF_INET6;
1.7 kstailey 162: else if (strcmp(optarg, "local") == 0)
163: af = AF_LOCAL;
1.1 deraadt 164: else if (strcmp(optarg, "unix") == 0)
165: af = AF_UNIX;
1.75 claudio 166: else if (strcmp(optarg, "mpls") == 0)
167: af = AF_MPLS;
1.63 claudio 168: else if (strcmp(optarg, "mask") == 0)
169: af = 0xff;
1.1 deraadt 170: else {
171: (void)fprintf(stderr,
172: "%s: %s: unknown address family\n",
1.2 deraadt 173: __progname, optarg);
1.1 deraadt 174: exit(1);
175: }
176: break;
177: case 'g':
178: gflag = 1;
1.93 tedu 179: break;
180: case 'h':
181: hflag = 1;
1.1 deraadt 182: break;
1.2 deraadt 183: case 'I':
1.1 deraadt 184: iflag = 1;
1.2 deraadt 185: interface = optarg;
1.1 deraadt 186: break;
187: case 'i':
188: iflag = 1;
189: break;
1.19 itojun 190: case 'l':
191: lflag = 1;
192: break;
1.1 deraadt 193: case 'M':
194: memf = optarg;
195: break;
196: case 'm':
197: mflag = 1;
198: break;
199: case 'N':
200: nlistf = optarg;
201: break;
202: case 'n':
203: nflag = 1;
204: break;
205: case 'p':
206: if ((tp = name2protox(optarg)) == NULL) {
207: (void)fprintf(stderr,
1.42 jmc 208: "%s: %s: unknown protocol\n",
1.2 deraadt 209: __progname, optarg);
1.1 deraadt 210: exit(1);
211: }
212: pflag = 1;
1.27 brian 213: break;
1.59 markus 214: case 'P':
215: errno = 0;
216: pcbaddr = strtoul(optarg, &ep, 16);
217: if (optarg[0] == '\0' || *ep != '\0' ||
218: errno == ERANGE) {
219: (void)fprintf(stderr,
220: "%s: %s: invalid PCB address\n",
221: __progname, optarg);
222: exit(1);
223: }
224: Pflag = 1;
225: break;
1.27 brian 226: case 'q':
227: qflag = 1;
1.1 deraadt 228: break;
229: case 'r':
230: rflag = 1;
1.46 cedric 231: break;
1.1 deraadt 232: case 's':
233: ++sflag;
234: break;
1.68 claudio 235: case 'T':
1.86 claudio 236: Tflag = 1;
237: tableid = gettable(optarg);
1.68 claudio 238: break;
1.1 deraadt 239: case 't':
240: tflag = 1;
241: break;
242: case 'u':
243: af = AF_UNIX;
1.13 peter 244: break;
245: case 'v':
246: vflag = 1;
1.1 deraadt 247: break;
1.56 reyk 248: case 'W':
249: Wflag = 1;
250: interface = optarg;
251: break;
1.1 deraadt 252: case 'w':
1.100 tedu 253: interval = strtonum(optarg, 1, INT_MAX, &errstr);
254: if (errstr)
255: errx(1, "interval is %s", errstr);
1.1 deraadt 256: iflag = 1;
257: break;
258: case '?':
259: default:
260: usage();
261: }
262: argv += optind;
263: argc -= optind;
264:
1.108 tedu 265: if (argc) {
266: interval = strtonum(*argv, 1, INT_MAX, &errstr);
267: if (errstr)
268: errx(1, "interval is %s", errstr);
269: ++argv;
270: --argc;
271: iflag = 1;
1.84 lum 272: }
1.107 tedu 273: if (argc)
274: usage();
1.84 lum 275:
1.56 reyk 276: /*
1.106 claudio 277: * Show per-interface statistics which don't need access to
278: * kernel memory (they're using IOCTLs)
1.33 deraadt 279: */
1.106 claudio 280: if (Wflag) {
281: if (interface == NULL)
282: usage();
283: net80211_ifstats(interface);
284: exit(0);
1.33 deraadt 285: }
1.61 djm 286:
1.1 deraadt 287: if (mflag) {
1.70 deraadt 288: mbpr();
1.1 deraadt 289: exit(0);
290: }
291: if (iflag) {
1.82 tedu 292: intpr(interval, repeatcount);
1.1 deraadt 293: exit(0);
294: }
1.106 claudio 295: if (sflag) {
296: if (rflag) {
1.73 claudio 297: rt_stats();
1.106 claudio 298: } else if (gflag) {
1.19 itojun 299: if (af == AF_INET || af == AF_UNSPEC)
1.71 deraadt 300: mrt_stats();
1.19 itojun 301: if (af == AF_INET6 || af == AF_UNSPEC)
1.71 deraadt 302: mrt6_stats();
1.106 claudio 303: } else if (pflag && tp->pr_name) {
304: (*tp->pr_stats)(tp->pr_name);
1.70 deraadt 305: } else {
1.19 itojun 306: if (af == AF_INET || af == AF_UNSPEC)
1.106 claudio 307: for (tp = protox; tp->pr_name; tp++)
308: (*tp->pr_stats)(tp->pr_name);
1.19 itojun 309: if (af == AF_INET6 || af == AF_UNSPEC)
1.106 claudio 310: for (tp = ip6protox; tp->pr_name; tp++)
311: (*tp->pr_stats)(tp->pr_name);
1.19 itojun 312: }
1.1 deraadt 313: exit(0);
314: }
1.106 claudio 315: if (gflag) {
316: if (af == AF_INET || af == AF_UNSPEC)
317: mroutepr();
318: if (af == AF_INET6 || af == AF_UNSPEC)
319: mroute6pr();
320: exit(0);
321: }
322:
323: /*
324: * The remaining code may need kvm so lets try to open it.
325: * -r and -P are the only bits left that actually can use this.
326: */
1.109 mpi 327: need_nlist = (nlistf != NULL) || (memf != NULL) || (Aflag && rflag);
328: if (!need_nlist && !Pflag)
329: kvm_flags |= KVM_NO_FILES;
1.106 claudio 330:
1.109 mpi 331: if ((kvmd = kvm_openfiles(nlistf, memf, NULL, kvm_flags, buf)) == NULL)
1.106 claudio 332: errx(1, "kvm_openfiles: %s", buf);
333:
334: if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) {
335: if (nlistf)
336: errx(1, "%s: no namelist", nlistf);
337: else
338: errx(1, "no namelist");
1.76 gollo 339: }
1.106 claudio 340:
341: if (rflag) {
342: if (Aflag || nlistf != NULL || memf != NULL)
343: routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
344: nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value,
345: tableid);
346: else
347: p_rttables(af, tableid);
348: exit(0);
1.1 deraadt 349: }
350:
1.106 claudio 351: if (pflag) {
352: if (tp->pr_proto == 0)
353: errx(1, "no protocol handler for protocol %s",
354: tp->pr_name);
355: else
356: proto = tp->pr_proto;
1.1 deraadt 357: }
1.106 claudio 358:
359: protopr(kvmd, pcbaddr, tableid, proto);
360: exit(0);
1.1 deraadt 361: }
362:
363: /*
364: * Read kernel memory, return 0 on success.
365: */
366: int
1.53 jaredy 367: kread(u_long addr, void *buf, int size)
1.1 deraadt 368: {
369:
370: if (kvm_read(kvmd, addr, buf, size) != size) {
1.2 deraadt 371: (void)fprintf(stderr, "%s: %s\n", __progname,
1.1 deraadt 372: kvm_geterr(kvmd));
373: return (-1);
374: }
375: return (0);
376: }
377:
378: char *
1.77 claudio 379: plural(u_int64_t n)
1.1 deraadt 380: {
381: return (n != 1 ? "s" : "");
382: }
383:
384: char *
1.77 claudio 385: plurales(u_int64_t n)
1.1 deraadt 386: {
387: return (n != 1 ? "es" : "");
1.110 bluhm 388: }
389:
390: char *
391: pluralys(u_int64_t n)
392: {
393: return (n != 1 ? "ies" : "y");
1.1 deraadt 394: }
395:
396: /*
397: * Find the protox for the given "well-known" name.
398: */
399: static struct protox *
1.34 deraadt 400: knownname(char *name)
1.1 deraadt 401: {
402: struct protox **tpp, *tp;
403:
404: for (tpp = protoprotox; *tpp; tpp++)
405: for (tp = *tpp; tp->pr_name; tp++)
406: if (strcmp(tp->pr_name, name) == 0)
407: return (tp);
408: return (NULL);
409: }
410:
411: /*
412: * Find the protox corresponding to name.
413: */
414: static struct protox *
1.34 deraadt 415: name2protox(char *name)
1.1 deraadt 416: {
417: struct protox *tp;
418: char **alias; /* alias from p->aliases */
419: struct protoent *p;
420:
421: /*
422: * Try to find the name in the list of "well-known" names. If that
423: * fails, check if name is an alias for an Internet protocol.
424: */
1.11 millert 425: if ((tp = knownname(name)))
1.1 deraadt 426: return (tp);
427:
428: setprotoent(1); /* make protocol lookup cheaper */
1.11 millert 429: while ((p = getprotoent())) {
1.1 deraadt 430: /* assert: name not same as p->name */
431: for (alias = p->p_aliases; *alias; alias++)
432: if (strcmp(name, *alias) == 0) {
433: endprotoent();
434: return (knownname(p->p_name));
435: }
436: }
437: endprotoent();
438: return (NULL);
439: }
440:
441: static void
1.34 deraadt 442: usage(void)
1.1 deraadt 443: {
444: (void)fprintf(stderr,
1.112 benno 445: "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n"
1.69 sobrado 446: " %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
447: " [-T tableid]\n"
1.94 tedu 448: " %s [-bdhn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n"
1.87 bluhm 449: " %s [-v] [-M core] [-N system] -P pcbaddr\n"
1.60 jaredy 450: " %s [-s] [-M core] [-N system] [-p protocol]\n"
451: " %s [-a] [-f address_family] [-i | -I interface]\n"
452: " %s [-W interface]\n",
453: __progname, __progname, __progname, __progname,
454: __progname, __progname, __progname);
1.1 deraadt 455: exit(1);
456: }
1.86 claudio 457:
458: u_int
459: gettable(const char *s)
460: {
461: const char *errstr;
462: struct rt_tableinfo info;
463: int mib[6];
464: size_t len;
465: u_int tableid;
466:
467: tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr);
468: if (errstr)
469: errx(1, "invalid table id: %s", errstr);
470:
471: mib[0] = CTL_NET;
1.101 guenther 472: mib[1] = PF_ROUTE;
1.86 claudio 473: mib[2] = 0;
474: mib[3] = 0;
475: mib[4] = NET_RT_TABLE;
476: mib[5] = tableid;
477:
478: len = sizeof(info);
479: if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
1.97 deraadt 480: err(1, "routing table %d", tableid);
1.86 claudio 481:
482: return (tableid);
483: }