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