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