Annotation of src/usr.bin/netstat/main.c, Revision 1.19
1.19 ! itojun 1: /* $OpenBSD: main.c,v 1.18 1999/10/29 03:26:40 angelos 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: char copyright[] =
39: "@(#) Copyright (c) 1983, 1988, 1993\n\
40: Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94";
46: #else
1.19 ! itojun 47: static char *rcsid = "$OpenBSD: main.c,v 1.18 1999/10/29 03:26:40 angelos Exp $";
1.1 deraadt 48: #endif
49: #endif /* not lint */
50:
51: #include <sys/param.h>
52: #include <sys/file.h>
53: #include <sys/protosw.h>
54: #include <sys/socket.h>
55:
56: #include <netinet/in.h>
57:
58: #include <ctype.h>
59: #include <errno.h>
60: #include <kvm.h>
61: #include <limits.h>
62: #include <netdb.h>
63: #include <nlist.h>
64: #include <paths.h>
65: #include <stdio.h>
66: #include <stdlib.h>
67: #include <string.h>
68: #include <unistd.h>
69: #include "netstat.h"
70:
71: struct nlist nl[] = {
72: #define N_MBSTAT 0
73: { "_mbstat" },
74: #define N_IPSTAT 1
75: { "_ipstat" },
76: #define N_TCBTABLE 2
77: { "_tcbtable" },
78: #define N_TCPSTAT 3
79: { "_tcpstat" },
80: #define N_UDBTABLE 4
81: { "_udbtable" },
82: #define N_UDPSTAT 5
83: { "_udpstat" },
84: #define N_IFNET 6
85: { "_ifnet" },
86: #define N_IMP 7
87: { "_imp_softc" },
88: #define N_ICMPSTAT 8
89: { "_icmpstat" },
90: #define N_RTSTAT 9
91: { "_rtstat" },
92: #define N_UNIXSW 10
93: { "_unixsw" },
94: #define N_IDP 11
95: { "_nspcb"},
96: #define N_IDPSTAT 12
97: { "_idpstat"},
98: #define N_SPPSTAT 13
99: { "_spp_istat"},
100: #define N_NSERR 14
101: { "_ns_errstat"},
102: #define N_CLNPSTAT 15
103: { "_clnp_stat"},
104: #define IN_NOTUSED 16
105: { "_tp_inpcb" },
106: #define ISO_TP 17
107: { "_tp_refinfo" },
108: #define N_TPSTAT 18
109: { "_tp_stat" },
110: #define N_ESISSTAT 19
111: { "_esis_stat"},
112: #define N_NIMP 20
113: { "_nimp"},
114: #define N_RTREE 21
115: { "_rt_tables"},
116: #define N_CLTP 22
117: { "_cltb"},
118: #define N_CLTPSTAT 23
119: { "_cltpstat"},
120: #define N_NFILE 24
121: { "_nfile" },
122: #define N_FILE 25
123: { "_file" },
124: #define N_IGMPSTAT 26
125: { "_igmpstat" },
126: #define N_MRTPROTO 27
127: { "_ip_mrtproto" },
128: #define N_MRTSTAT 28
129: { "_mrtstat" },
130: #define N_MFCHASHTBL 29
131: { "_mfchashtbl" },
132: #define N_MFCHASH 30
133: { "_mfchash" },
134: #define N_VIFTABLE 31
135: { "_viftable" },
1.4 mickey 136: #define N_IPX 32
137: { "_ipxcbtable"},
138: #define N_IPXSTAT 33
139: { "_ipxstat"},
140: #define N_SPXSTAT 34
141: { "_spx_istat"},
142: #define N_IPXERR 35
143: { "_ipx_errstat"},
1.8 angelos 144: #define N_AHSTAT 36
145: { "_ahstat"},
146: #define N_ESPSTAT 37
147: { "_espstat"},
148: #define N_IP4STAT 38
149: { "_ip4stat"},
1.12 denny 150: #define N_DDPSTAT 39
151: { "_ddpstat"},
152: #define N_DDPCB 40
153: { "_ddpcb"},
1.18 angelos 154: #define N_ETHERIPSTAT 41
155: { "_etheripstat"},
1.19 ! itojun 156: #define N_IP6STAT 42
! 157: { "_ip6stat" },
! 158: #define N_ICMP6STAT 43
! 159: { "_icmp6stat" },
! 160: #define N_IPSECSTAT 44
! 161: { "_ipsecstat" },
! 162: #define N_IPSEC6STAT 45
! 163: { "_ipsec6stat" },
! 164: #define N_PIM6STAT 46
! 165: { "_pim6stat" },
! 166: #define N_MRT6PROTO 47
! 167: { "_ip6_mrtproto" },
! 168: #define N_MRT6STAT 48
! 169: { "_mrt6stat" },
! 170: #define N_MF6CTABLE 49
! 171: { "_mf6ctable" },
! 172: #define N_MIF6TABLE 50
! 173: { "_mif6table" },
1.11 millert 174: { ""},
1.1 deraadt 175: };
176:
177: struct protox {
178: u_char pr_index; /* index into nlist of cb head */
179: u_char pr_sindex; /* index into nlist of stat block */
180: u_char pr_wanted; /* 1 if wanted, 0 otherwise */
181: void (*pr_cblocks)(); /* control blocks printing routine */
182: void (*pr_stats)(); /* statistics printing routine */
183: char *pr_name; /* well-known name */
184: } protox[] = {
185: { N_TCBTABLE, N_TCPSTAT, 1, protopr,
186: tcp_stats, "tcp" },
187: { N_UDBTABLE, N_UDPSTAT, 1, protopr,
188: udp_stats, "udp" },
189: { -1, N_IPSTAT, 1, 0,
190: ip_stats, "ip" },
191: { -1, N_ICMPSTAT, 1, 0,
192: icmp_stats, "icmp" },
193: { -1, N_IGMPSTAT, 1, 0,
194: igmp_stats, "igmp" },
1.8 angelos 195: { -1, N_AHSTAT, 1, 0,
1.15 angelos 196: ah_stats, "ah" },
1.8 angelos 197: { -1, N_ESPSTAT, 1, 0,
1.15 angelos 198: esp_stats, "esp" },
1.8 angelos 199: { -1, N_IP4STAT, 1, 0,
1.9 angelos 200: ip4_stats, "ipencap" },
1.18 angelos 201: { -1, N_ETHERIPSTAT, 1, 0,
202: etherip_stats,"etherip" },
1.1 deraadt 203: { -1, -1, 0, 0,
204: 0, 0 }
205: };
206:
1.19 ! itojun 207: #ifdef INET6
! 208: struct protox ip6protox[] = {
! 209: { -1, N_IP6STAT, 1, 0,
! 210: ip6_stats, "ip6" },
! 211: { -1, N_ICMP6STAT, 1, 0,
! 212: icmp6_stats, "icmp6" },
! 213: { -1, N_PIM6STAT, 1, 0,
! 214: pim6_stats, "pim6" },
! 215: { -1, -1, 0, 0,
! 216: 0, 0 }
! 217: };
! 218: #endif
! 219:
1.4 mickey 220: struct protox ipxprotox[] = {
221: { N_IPX, N_IPXSTAT, 1, ipxprotopr,
222: ipx_stats, "ipx" },
223: { N_IPX, N_SPXSTAT, 1, ipxprotopr,
224: spx_stats, "spx" },
225: { -1, N_IPXERR, 1, 0,
226: ipxerr_stats, "ipx_err" },
227: { -1, -1, 0, 0,
228: 0, 0 }
229: };
230:
1.1 deraadt 231: struct protox nsprotox[] = {
232: { N_IDP, N_IDPSTAT, 1, nsprotopr,
233: idp_stats, "idp" },
234: { N_IDP, N_SPPSTAT, 1, nsprotopr,
235: spp_stats, "spp" },
236: { -1, N_NSERR, 1, 0,
237: nserr_stats, "ns_err" },
238: { -1, -1, 0, 0,
239: 0, 0 }
240: };
241:
242: struct protox isoprotox[] = {
243: { ISO_TP, N_TPSTAT, 1, iso_protopr,
244: tp_stats, "tp" },
245: { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
246: cltp_stats, "cltp" },
247: { -1, N_CLNPSTAT, 1, 0,
248: clnp_stats, "clnp"},
249: { -1, N_ESISSTAT, 1, 0,
250: esis_stats, "esis"},
251: { -1, -1, 0, 0,
252: 0, 0 }
253: };
254:
1.12 denny 255: struct protox atalkprotox[] = {
256: { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
257: ddp_stats, "ddp" },
258: { -1, -1, 0, 0,
259: 0, 0 }
260: };
261:
1.19 ! itojun 262: #ifndef INET6
1.12 denny 263: struct protox *protoprotox[] = { protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL };
1.19 ! itojun 264: #else
! 265: struct protox *protoprotox[] = { protox, ip6protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL };
! 266: #endif
1.1 deraadt 267:
268: static void printproto __P((struct protox *, char *));
269: static void usage __P((void));
270: static struct protox *name2protox __P((char *));
271: static struct protox *knownname __P((char *));
272:
273: kvm_t *kvmd;
274:
275: int
276: main(argc, argv)
277: int argc;
278: char *argv[];
279: {
280: extern char *optarg;
281: extern int optind;
282: register struct protoent *p;
1.19 ! itojun 283: register struct protox *tp = NULL; /* for printing cblocks & stats */
1.1 deraadt 284: int ch;
285: char *nlistf = NULL, *memf = NULL;
286: char buf[_POSIX2_LINE_MAX];
287:
288: af = AF_UNSPEC;
289:
1.19 ! itojun 290: while ((ch = getopt(argc, argv, "Aadf:gI:ilM:mN:np:rstuvw:")) != -1)
1.1 deraadt 291: switch(ch) {
292: case 'A':
293: Aflag = 1;
294: break;
295: case 'a':
296: aflag = 1;
297: break;
298: case 'd':
299: dflag = 1;
300: break;
301: case 'f':
1.4 mickey 302: if (strcmp(optarg, "inet") == 0)
1.1 deraadt 303: af = AF_INET;
1.19 ! itojun 304: else if (strcmp(optarg, "inet6") == 0)
! 305: af = AF_INET6;
1.7 kstailey 306: else if (strcmp(optarg, "local") == 0)
307: af = AF_LOCAL;
1.1 deraadt 308: else if (strcmp(optarg, "unix") == 0)
309: af = AF_UNIX;
1.4 mickey 310: else if (strcmp(optarg, "ipx") == 0)
311: af = AF_IPX;
312: else if (strcmp(optarg, "ns") == 0)
313: af = AF_NS;
1.1 deraadt 314: else if (strcmp(optarg, "iso") == 0)
315: af = AF_ISO;
1.10 angelos 316: else if (strcmp(optarg, "encap") == 0)
1.16 angelos 317: af = PF_KEY;
1.12 denny 318: else if (strcmp(optarg, "atalk") == 0)
319: af = AF_APPLETALK;
1.1 deraadt 320: else {
321: (void)fprintf(stderr,
322: "%s: %s: unknown address family\n",
1.2 deraadt 323: __progname, optarg);
1.1 deraadt 324: exit(1);
325: }
326: break;
327: case 'g':
328: gflag = 1;
329: break;
1.2 deraadt 330: case 'I':
1.1 deraadt 331: iflag = 1;
1.2 deraadt 332: interface = optarg;
1.1 deraadt 333: break;
334: case 'i':
335: iflag = 1;
336: break;
1.19 ! itojun 337: case 'l':
! 338: lflag = 1;
! 339: break;
1.1 deraadt 340: case 'M':
341: memf = optarg;
342: break;
343: case 'm':
344: mflag = 1;
345: break;
346: case 'N':
347: nlistf = optarg;
348: break;
349: case 'n':
350: nflag = 1;
351: break;
352: case 'p':
353: if ((tp = name2protox(optarg)) == NULL) {
354: (void)fprintf(stderr,
355: "%s: %s: unknown or uninstrumented protocol\n",
1.2 deraadt 356: __progname, optarg);
1.1 deraadt 357: exit(1);
358: }
359: pflag = 1;
360: break;
361: case 'r':
362: rflag = 1;
363: break;
364: case 's':
365: ++sflag;
366: break;
367: case 't':
368: tflag = 1;
369: break;
370: case 'u':
371: af = AF_UNIX;
1.13 peter 372: break;
373: case 'v':
374: vflag = 1;
1.1 deraadt 375: break;
376: case 'w':
377: interval = atoi(optarg);
378: iflag = 1;
379: break;
380: case '?':
381: default:
382: usage();
383: }
384: argv += optind;
385: argc -= optind;
386:
387: #define BACKWARD_COMPATIBILITY
388: #ifdef BACKWARD_COMPATIBILITY
389: if (*argv) {
390: if (isdigit(**argv)) {
391: interval = atoi(*argv);
392: if (interval <= 0)
393: usage();
394: ++argv;
395: iflag = 1;
396: }
397: if (*argv) {
398: nlistf = *argv;
399: if (*++argv)
400: memf = *argv;
401: }
402: }
403: #endif
404:
405: /*
406: * Discard setgid privileges if not the running kernel so that bad
407: * guys can't print interesting stuff from kernel memory.
408: */
1.5 tholo 409: if (nlistf != NULL || memf != NULL) {
410: setegid(getgid());
1.1 deraadt 411: setgid(getgid());
1.5 tholo 412: }
1.1 deraadt 413:
1.2 deraadt 414: if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
415: buf)) == NULL) {
416: fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
1.1 deraadt 417: exit(1);
418: }
1.14 deraadt 419: setegid(getgid());
420: setgid(getgid());
421:
1.1 deraadt 422: if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
423: if (nlistf)
1.2 deraadt 424: fprintf(stderr, "%s: %s: no namelist\n", __progname,
425: nlistf);
1.1 deraadt 426: else
1.2 deraadt 427: fprintf(stderr, "%s: no namelist\n", __progname);
1.1 deraadt 428: exit(1);
429: }
430: if (mflag) {
431: mbpr(nl[N_MBSTAT].n_value);
432: exit(0);
433: }
434: if (pflag) {
435: if (tp->pr_stats)
436: (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
437: tp->pr_name);
438: else
439: printf("%s: no stats routine\n", tp->pr_name);
440: exit(0);
441: }
442: /*
443: * Keep file descriptors open to avoid overhead
444: * of open/close on each call to get* routines.
445: */
446: sethostent(1);
447: setnetent(1);
448: if (iflag) {
449: intpr(interval, nl[N_IFNET].n_value);
450: exit(0);
451: }
452: if (rflag) {
453: if (sflag)
454: rt_stats(nl[N_RTSTAT].n_value);
455: else
456: routepr(nl[N_RTREE].n_value);
457: exit(0);
458: }
459: if (gflag) {
1.19 ! itojun 460: if (sflag) {
! 461: if (af == AF_INET || af == AF_UNSPEC)
! 462: mrt_stats(nl[N_MRTPROTO].n_value,
! 463: nl[N_MRTSTAT].n_value);
! 464: #ifdef INET6
! 465: if (af == AF_INET6 || af == AF_UNSPEC)
! 466: mrt6_stats(nl[N_MRT6PROTO].n_value,
! 467: nl[N_MRT6STAT].n_value);
! 468: #endif
! 469: }
! 470: else {
! 471: if (af == AF_INET || af == AF_UNSPEC)
! 472: mroutepr(nl[N_MRTPROTO].n_value,
! 473: nl[N_MFCHASHTBL].n_value,
! 474: nl[N_MFCHASH].n_value,
! 475: nl[N_VIFTABLE].n_value);
! 476: #ifdef INET6
! 477: if (af == AF_INET6 || af == AF_UNSPEC)
! 478: mroute6pr(nl[N_MRT6PROTO].n_value,
! 479: nl[N_MF6CTABLE].n_value,
! 480: nl[N_MIF6TABLE].n_value);
! 481: #endif
! 482: }
1.1 deraadt 483: exit(0);
484: }
485: if (af == AF_INET || af == AF_UNSPEC) {
486: setprotoent(1);
487: setservent(1);
488: /* ugh, this is O(MN) ... why do we do this? */
1.11 millert 489: while ((p = getprotoent())) {
1.1 deraadt 490: for (tp = protox; tp->pr_name; tp++)
491: if (strcmp(tp->pr_name, p->p_name) == 0)
492: break;
493: if (tp->pr_name == 0 || tp->pr_wanted == 0)
494: continue;
495: printproto(tp, p->p_name);
496: }
497: endprotoent();
498: }
1.19 ! itojun 499: #ifdef INET6
! 500: if (af == AF_INET6 || af == AF_UNSPEC)
! 501: for (tp = ip6protox; tp->pr_name; tp++)
! 502: printproto(tp, tp->pr_name);
! 503: #endif
1.4 mickey 504: if (af == AF_IPX || af == AF_UNSPEC)
505: for (tp = ipxprotox; tp->pr_name; tp++)
506: printproto(tp, tp->pr_name);
1.1 deraadt 507: if (af == AF_NS || af == AF_UNSPEC)
508: for (tp = nsprotox; tp->pr_name; tp++)
509: printproto(tp, tp->pr_name);
510: if (af == AF_ISO || af == AF_UNSPEC)
511: for (tp = isoprotox; tp->pr_name; tp++)
512: printproto(tp, tp->pr_name);
513: if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
514: unixpr(nl[N_UNIXSW].n_value);
1.12 denny 515: if (af == AF_APPLETALK || af == AF_UNSPEC)
516: for (tp = atalkprotox; tp->pr_name; tp++)
517: printproto(tp, tp->pr_name);
1.1 deraadt 518: exit(0);
519: }
520:
521: /*
522: * Print out protocol statistics or control blocks (per sflag).
523: * If the interface was not specifically requested, and the symbol
524: * is not in the namelist, ignore this one.
525: */
526: static void
527: printproto(tp, name)
528: register struct protox *tp;
529: char *name;
530: {
531: void (*pr)();
532: u_long off;
533:
534: if (sflag) {
535: pr = tp->pr_stats;
536: off = nl[tp->pr_sindex].n_value;
537: } else {
538: pr = tp->pr_cblocks;
539: off = nl[tp->pr_index].n_value;
540: }
541: if (pr != NULL && (off || af != AF_UNSPEC))
542: (*pr)(off, name);
543: }
544:
545: /*
546: * Read kernel memory, return 0 on success.
547: */
548: int
549: kread(addr, buf, size)
550: u_long addr;
551: char *buf;
552: int size;
553: {
554:
555: if (kvm_read(kvmd, addr, buf, size) != size) {
1.2 deraadt 556: (void)fprintf(stderr, "%s: %s\n", __progname,
1.1 deraadt 557: kvm_geterr(kvmd));
558: return (-1);
559: }
560: return (0);
561: }
562:
563: char *
564: plural(n)
565: int n;
566: {
567: return (n != 1 ? "s" : "");
568: }
569:
570: char *
571: plurales(n)
572: int n;
573: {
574: return (n != 1 ? "es" : "");
575: }
576:
577: /*
578: * Find the protox for the given "well-known" name.
579: */
580: static struct protox *
581: knownname(name)
582: char *name;
583: {
584: struct protox **tpp, *tp;
585:
586: for (tpp = protoprotox; *tpp; tpp++)
587: for (tp = *tpp; tp->pr_name; tp++)
588: if (strcmp(tp->pr_name, name) == 0)
589: return (tp);
590: return (NULL);
591: }
592:
593: /*
594: * Find the protox corresponding to name.
595: */
596: static struct protox *
597: name2protox(name)
598: char *name;
599: {
600: struct protox *tp;
601: char **alias; /* alias from p->aliases */
602: struct protoent *p;
603:
604: /*
605: * Try to find the name in the list of "well-known" names. If that
606: * fails, check if name is an alias for an Internet protocol.
607: */
1.11 millert 608: if ((tp = knownname(name)))
1.1 deraadt 609: return (tp);
610:
611: setprotoent(1); /* make protocol lookup cheaper */
1.11 millert 612: while ((p = getprotoent())) {
1.1 deraadt 613: /* assert: name not same as p->name */
614: for (alias = p->p_aliases; *alias; alias++)
615: if (strcmp(name, *alias) == 0) {
616: endprotoent();
617: return (knownname(p->p_name));
618: }
619: }
620: endprotoent();
621: return (NULL);
622: }
623:
624: static void
625: usage()
626: {
627: (void)fprintf(stderr,
1.2 deraadt 628: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
1.1 deraadt 629: (void)fprintf(stderr,
1.17 d 630: " %s [-gimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
1.1 deraadt 631: (void)fprintf(stderr,
1.2 deraadt 632: " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
1.1 deraadt 633: (void)fprintf(stderr,
1.2 deraadt 634: " %s [-M core] [-N system] [-p protocol]\n", __progname);
1.1 deraadt 635: exit(1);
636: }