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