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