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