Annotation of src/usr.bin/netstat/main.c, Revision 1.105
1.105 ! claudio 1: /* $OpenBSD: main.c,v 1.104 2015/02/06 03:22:00 reyk 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.71 deraadt 57: #define N_TCBTABLE 0
1.1 deraadt 58: { "_tcbtable" },
1.71 deraadt 59: #define N_UDBTABLE 1
1.1 deraadt 60: { "_udbtable" },
1.71 deraadt 61:
1.99 guenther 62: #define N_RTREE 7
1.71 deraadt 63: { "_rt_tables"},
1.99 guenther 64: #define N_RTMASK 8
1.71 deraadt 65: { "_mask_rnhead" },
1.99 guenther 66: #define N_AF2RTAFIDX 9
1.71 deraadt 67: { "_af2rtafidx" },
1.99 guenther 68: #define N_RTBLIDMAX 10
1.71 deraadt 69: { "_rtbl_id_max" },
70:
1.99 guenther 71: #define N_RAWIPTABLE 11
1.40 markus 72: { "_rawcbtable" },
1.99 guenther 73: #define N_RAWIP6TABLE 12
1.40 markus 74: { "_rawin6pcbtable" },
1.99 guenther 75: #define N_DIVBTABLE 13
1.80 michele 76: { "_divbtable" },
1.99 guenther 77: #define N_DIVB6TABLE 14
1.81 michele 78: { "_divb6table" },
1.71 deraadt 79:
1.87 bluhm 80: { "" }
1.1 deraadt 81: };
82:
83: struct protox {
1.87 bluhm 84: u_char pr_index; /* index into nlist of cb head */
1.90 mikeb 85: void (*pr_cblocks)(u_long, char *, int, u_int, u_long);
1.87 bluhm 86: /* control blocks printing routine */
87: void (*pr_stats)(char *); /* statistics printing routine */
88: char *pr_name; /* well-known name */
1.1 deraadt 89: } protox[] = {
1.87 bluhm 90: { N_TCBTABLE, protopr, tcp_stats, "tcp" },
91: { N_UDBTABLE, protopr, udp_stats, "udp" },
92: { N_RAWIPTABLE, protopr, ip_stats, "ip" },
93: { N_DIVBTABLE, protopr, div_stats, "divert" },
94: { -1, NULL, icmp_stats, "icmp" },
95: { -1, NULL, igmp_stats, "igmp" },
96: { -1, NULL, ah_stats, "ah" },
97: { -1, NULL, esp_stats, "esp" },
98: { -1, NULL, ipip_stats, "ipencap" },
99: { -1, NULL, etherip_stats, "etherip" },
100: { -1, NULL, ipcomp_stats, "ipcomp" },
101: { -1, NULL, carp_stats, "carp" },
102: { -1, NULL, pfsync_stats, "pfsync" },
103: { -1, NULL, pim_stats, "pim" },
104: { -1, NULL, pflow_stats, "pflow" },
105: { -1, NULL, NULL, NULL }
1.1 deraadt 106: };
107:
1.19 itojun 108: struct protox ip6protox[] = {
1.87 bluhm 109: { N_TCBTABLE, protopr, NULL, "tcp" },
110: { N_UDBTABLE, protopr, NULL, "udp" },
111: { N_RAWIP6TABLE,protopr, ip6_stats, "ip6" },
112: { N_DIVB6TABLE, protopr, div6_stats, "divert6" },
113: { -1, NULL, icmp6_stats, "icmp6" },
114: { -1, NULL, pim6_stats, "pim6" },
115: { -1, NULL, rip6_stats, "rip6" },
116: { -1, NULL, NULL, NULL }
1.19 itojun 117: };
118:
1.34 deraadt 119: struct protox *protoprotox[] = {
1.89 henning 120: protox, ip6protox, NULL
1.34 deraadt 121: };
1.1 deraadt 122:
1.90 mikeb 123: static void printproto(struct protox *, char *, int, u_int, u_long);
1.30 millert 124: static void usage(void);
125: static struct protox *name2protox(char *);
126: static struct protox *knownname(char *);
1.86 claudio 127: u_int gettable(const char *);
128:
1.95 deraadt 129: int hideroot;
1.1 deraadt 130:
131: kvm_t *kvmd;
132:
133: int
1.34 deraadt 134: main(int argc, char *argv[])
1.1 deraadt 135: {
136: extern char *optarg;
137: extern int optind;
1.68 claudio 138: const char *errstr;
1.28 mpech 139: struct protoent *p;
140: struct protox *tp = NULL; /* for printing cblocks & stats */
1.1 deraadt 141: int ch;
1.59 markus 142: char *nlistf = NULL, *memf = NULL, *ep;
1.1 deraadt 143: char buf[_POSIX2_LINE_MAX];
1.61 djm 144: gid_t gid;
1.59 markus 145: u_long pcbaddr = 0;
1.91 mikeb 146: u_int tableid;
1.86 claudio 147: int Tflag = 0;
1.82 tedu 148: int repeatcount = 0;
1.96 guenther 149: int need_nlist;
1.95 deraadt 150:
151: hideroot = getuid();
1.1 deraadt 152:
153: af = AF_UNSPEC;
1.91 mikeb 154: tableid = getrtable();
1.1 deraadt 155:
1.88 jsing 156: while ((ch = getopt(argc, argv,
1.93 tedu 157: "AaBbc:dFf:ghI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
1.31 deraadt 158: switch (ch) {
1.1 deraadt 159: case 'A':
160: Aflag = 1;
161: break;
162: case 'a':
163: aflag = 1;
1.24 camield 164: break;
1.88 jsing 165: case 'B':
166: Bflag = 1;
167: break;
1.24 camield 168: case 'b':
169: bflag = 1;
1.1 deraadt 170: break;
1.82 tedu 171: case 'c':
172: repeatcount = strtonum(optarg, 1, INT_MAX, &errstr);
1.100 tedu 173: if (errstr)
174: errx(1, "count is %s", errstr);
1.82 tedu 175: break;
1.1 deraadt 176: case 'd':
177: dflag = 1;
1.64 pyr 178: break;
179: case 'F':
180: Fflag = 1;
1.1 deraadt 181: break;
182: case 'f':
1.4 mickey 183: if (strcmp(optarg, "inet") == 0)
1.1 deraadt 184: af = AF_INET;
1.19 itojun 185: else if (strcmp(optarg, "inet6") == 0)
186: af = AF_INET6;
1.7 kstailey 187: else if (strcmp(optarg, "local") == 0)
188: af = AF_LOCAL;
1.1 deraadt 189: else if (strcmp(optarg, "unix") == 0)
190: af = AF_UNIX;
1.75 claudio 191: else if (strcmp(optarg, "mpls") == 0)
192: af = AF_MPLS;
1.76 gollo 193: else if (strcmp(optarg, "pflow") == 0)
194: af = PF_PFLOW;
1.63 claudio 195: else if (strcmp(optarg, "mask") == 0)
196: af = 0xff;
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;
256: case 'r':
257: rflag = 1;
1.46 cedric 258: break;
1.1 deraadt 259: case 's':
260: ++sflag;
261: break;
1.68 claudio 262: case 'T':
1.86 claudio 263: Tflag = 1;
264: tableid = gettable(optarg);
1.68 claudio 265: break;
1.1 deraadt 266: case 't':
267: tflag = 1;
268: break;
269: case 'u':
270: af = AF_UNIX;
1.13 peter 271: break;
272: case 'v':
273: vflag = 1;
1.1 deraadt 274: break;
1.56 reyk 275: case 'W':
276: Wflag = 1;
277: interface = optarg;
278: break;
1.1 deraadt 279: case 'w':
1.100 tedu 280: interval = strtonum(optarg, 1, INT_MAX, &errstr);
281: if (errstr)
282: errx(1, "interval is %s", errstr);
1.1 deraadt 283: iflag = 1;
284: break;
285: case '?':
286: default:
287: usage();
288: }
289: argv += optind;
290: argc -= optind;
291:
1.33 deraadt 292: /*
1.56 reyk 293: * Show per-interface statistics which don't need access to
294: * kernel memory (they're using IOCTLs)
295: */
296: if (Wflag) {
297: if (interface == NULL)
298: usage();
299: net80211_ifstats(interface);
300: exit(0);
301: }
302:
1.84 lum 303: #define BACKWARD_COMPATIBILITY
304: #ifdef BACKWARD_COMPATIBILITY
305: if (*argv) {
1.98 deraadt 306: if (isdigit((unsigned char)**argv)) {
1.100 tedu 307: interval = strtonum(*argv, 1, INT_MAX, &errstr);
308: if (errstr)
309: errx(1, "interval is %s", errstr);
1.84 lum 310: ++argv;
311: iflag = 1;
312: }
313: if (*argv) {
314: nlistf = *argv;
315: if (*++argv)
316: memf = *argv;
317: }
318: }
319: #endif
320:
1.96 guenther 321: need_nlist = !mflag && (pflag || nlistf != NULL || memf != NULL ||
1.105 ! claudio 322: (!iflag && !sflag && !gflag && (rflag ? Aflag :
! 323: (af != AF_UNIX || Pflag))));
1.96 guenther 324:
1.56 reyk 325: /*
1.33 deraadt 326: * Discard setgid privileges if not the running kernel so that bad
327: * guys can't print interesting stuff from kernel memory.
1.59 markus 328: * Dumping PCB info is also restricted.
1.33 deraadt 329: */
1.61 djm 330: gid = getgid();
331: if (nlistf != NULL || memf != NULL || Pflag)
332: if (setresgid(gid, gid, gid) == -1)
333: err(1, "setresgid");
1.73 claudio 334:
1.96 guenther 335: if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY |
336: (need_nlist ? 0 : KVM_NO_FILES), buf)) == NULL) {
1.78 deraadt 337: fprintf(stderr, "%s: kvm_openfiles: %s\n", __progname, buf);
1.33 deraadt 338: exit(1);
339: }
1.61 djm 340:
341: if (nlistf == NULL && memf == NULL && !Pflag)
342: if (setresgid(gid, gid, gid) == -1)
343: err(1, "setresgid");
1.14 deraadt 344:
1.96 guenther 345: if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) {
1.1 deraadt 346: if (nlistf)
1.2 deraadt 347: fprintf(stderr, "%s: %s: no namelist\n", __progname,
348: nlistf);
1.1 deraadt 349: else
1.2 deraadt 350: fprintf(stderr, "%s: no namelist\n", __progname);
1.1 deraadt 351: exit(1);
352: }
353: if (mflag) {
1.70 deraadt 354: mbpr();
1.1 deraadt 355: exit(0);
356: }
357: if (pflag) {
1.90 mikeb 358: printproto(tp, tp->pr_name, af, tableid, pcbaddr);
1.1 deraadt 359: exit(0);
360: }
361: if (iflag) {
1.82 tedu 362: intpr(interval, repeatcount);
1.1 deraadt 363: exit(0);
364: }
365: if (rflag) {
366: if (sflag)
1.73 claudio 367: rt_stats();
368: else if (Aflag || nlistf != NULL || memf != NULL)
1.63 claudio 369: routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
1.79 claudio 370: nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value,
371: tableid);
1.73 claudio 372: else
1.92 mikeb 373: p_rttables(af, tableid);
1.1 deraadt 374: exit(0);
375: }
376: if (gflag) {
1.19 itojun 377: if (sflag) {
378: if (af == AF_INET || af == AF_UNSPEC)
1.71 deraadt 379: mrt_stats();
1.19 itojun 380: if (af == AF_INET6 || af == AF_UNSPEC)
1.71 deraadt 381: mrt6_stats();
1.70 deraadt 382: } else {
1.19 itojun 383: if (af == AF_INET || af == AF_UNSPEC)
1.105 ! claudio 384: mroutepr();
1.19 itojun 385: if (af == AF_INET6 || af == AF_UNSPEC)
1.105 ! claudio 386: mroute6pr();
1.19 itojun 387: }
1.1 deraadt 388: exit(0);
389: }
390: if (af == AF_INET || af == AF_UNSPEC) {
391: setprotoent(1);
392: setservent(1);
393: /* ugh, this is O(MN) ... why do we do this? */
1.11 millert 394: while ((p = getprotoent())) {
1.1 deraadt 395: for (tp = protox; tp->pr_name; tp++)
396: if (strcmp(tp->pr_name, p->p_name) == 0)
397: break;
1.71 deraadt 398: if (tp->pr_name == 0)
1.1 deraadt 399: continue;
1.90 mikeb 400: printproto(tp, p->p_name, AF_INET, tableid, pcbaddr);
1.1 deraadt 401: }
402: endprotoent();
1.76 gollo 403: }
404: if (af == PF_PFLOW || af == AF_UNSPEC) {
405: tp = name2protox("pflow");
1.90 mikeb 406: printproto(tp, tp->pr_name, af, tableid, pcbaddr);
1.1 deraadt 407: }
1.19 itojun 408: if (af == AF_INET6 || af == AF_UNSPEC)
409: for (tp = ip6protox; tp->pr_name; tp++)
1.90 mikeb 410: printproto(tp, tp->pr_name, AF_INET6, tableid,
411: pcbaddr);
1.1 deraadt 412: if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
1.96 guenther 413: unixpr(kvmd, pcbaddr);
1.1 deraadt 414: exit(0);
415: }
416:
417: /*
418: * Print out protocol statistics or control blocks (per sflag).
419: * If the interface was not specifically requested, and the symbol
420: * is not in the namelist, ignore this one.
421: */
422: static void
1.90 mikeb 423: printproto(struct protox *tp, char *name, int af, u_int tableid,
424: u_long pcbaddr)
1.1 deraadt 425: {
426: if (sflag) {
1.71 deraadt 427: if (tp->pr_stats != NULL)
428: (*tp->pr_stats)(name);
1.1 deraadt 429: } else {
1.71 deraadt 430: u_char i = tp->pr_index;
431: if (tp->pr_cblocks != NULL &&
432: i < sizeof(nl) / sizeof(nl[0]) &&
433: (nl[i].n_value || af != AF_UNSPEC))
1.90 mikeb 434: (*tp->pr_cblocks)(nl[i].n_value, name, af, tableid,
435: pcbaddr);
1.1 deraadt 436: }
437: }
438:
439: /*
440: * Read kernel memory, return 0 on success.
441: */
442: int
1.53 jaredy 443: kread(u_long addr, void *buf, int size)
1.1 deraadt 444: {
445:
446: if (kvm_read(kvmd, addr, buf, size) != size) {
1.2 deraadt 447: (void)fprintf(stderr, "%s: %s\n", __progname,
1.1 deraadt 448: kvm_geterr(kvmd));
449: return (-1);
450: }
451: return (0);
452: }
453:
454: char *
1.77 claudio 455: plural(u_int64_t n)
1.1 deraadt 456: {
457: return (n != 1 ? "s" : "");
458: }
459:
460: char *
1.77 claudio 461: plurales(u_int64_t n)
1.1 deraadt 462: {
463: return (n != 1 ? "es" : "");
464: }
465:
466: /*
467: * Find the protox for the given "well-known" name.
468: */
469: static struct protox *
1.34 deraadt 470: knownname(char *name)
1.1 deraadt 471: {
472: struct protox **tpp, *tp;
473:
474: for (tpp = protoprotox; *tpp; tpp++)
475: for (tp = *tpp; tp->pr_name; tp++)
476: if (strcmp(tp->pr_name, name) == 0)
477: return (tp);
478: return (NULL);
479: }
480:
481: /*
482: * Find the protox corresponding to name.
483: */
484: static struct protox *
1.34 deraadt 485: name2protox(char *name)
1.1 deraadt 486: {
487: struct protox *tp;
488: char **alias; /* alias from p->aliases */
489: struct protoent *p;
490:
491: /*
492: * Try to find the name in the list of "well-known" names. If that
493: * fails, check if name is an alias for an Internet protocol.
494: */
1.11 millert 495: if ((tp = knownname(name)))
1.1 deraadt 496: return (tp);
497:
498: setprotoent(1); /* make protocol lookup cheaper */
1.11 millert 499: while ((p = getprotoent())) {
1.1 deraadt 500: /* assert: name not same as p->name */
501: for (alias = p->p_aliases; *alias; alias++)
502: if (strcmp(name, *alias) == 0) {
503: endprotoent();
504: return (knownname(p->p_name));
505: }
506: }
507: endprotoent();
508: return (NULL);
509: }
510:
511: static void
1.34 deraadt 512: usage(void)
1.1 deraadt 513: {
514: (void)fprintf(stderr,
1.88 jsing 515: "usage: %s [-AaBn] [-f address_family] [-M core] [-N system]\n"
1.69 sobrado 516: " %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
517: " [-T tableid]\n"
1.94 tedu 518: " %s [-bdhn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n"
1.87 bluhm 519: " %s [-v] [-M core] [-N system] -P pcbaddr\n"
1.60 jaredy 520: " %s [-s] [-M core] [-N system] [-p protocol]\n"
521: " %s [-a] [-f address_family] [-i | -I interface]\n"
522: " %s [-W interface]\n",
523: __progname, __progname, __progname, __progname,
524: __progname, __progname, __progname);
1.1 deraadt 525: exit(1);
526: }
1.86 claudio 527:
528: u_int
529: gettable(const char *s)
530: {
531: const char *errstr;
532: struct rt_tableinfo info;
533: int mib[6];
534: size_t len;
535: u_int tableid;
536:
537: tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr);
538: if (errstr)
539: errx(1, "invalid table id: %s", errstr);
540:
541: mib[0] = CTL_NET;
1.101 guenther 542: mib[1] = PF_ROUTE;
1.86 claudio 543: mib[2] = 0;
544: mib[3] = 0;
545: mib[4] = NET_RT_TABLE;
546: mib[5] = tableid;
547:
548: len = sizeof(info);
549: if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
1.97 deraadt 550: err(1, "routing table %d", tableid);
1.86 claudio 551:
552: return (tableid);
553: }