main.c revision 1.28 1 /* $NetBSD: main.c,v 1.28 2000/07/06 12:40:19 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1988, 1993
5 * Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\
39 Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94";
45 #else
46 __RCSID("$NetBSD: main.c,v 1.28 2000/07/06 12:40:19 itojun Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/file.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54
55 #include <netinet/in.h>
56
57 #include <ctype.h>
58 #include <err.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" },
136 #define N_MSIZE 32
137 { "_msize" },
138 #define N_MCLBYTES 33
139 { "_mclbytes" },
140 #define N_DDPSTAT 34
141 { "_ddpstat"},
142 #define N_DDPCB 35
143 { "_ddpcb"},
144 #define N_MBPOOL 36
145 { "_mbpool" },
146 #define N_MCLPOOL 37
147 { "_mclpool" },
148 #define N_DIVPCB 38
149 { "_divcb"},
150 #define N_DIVSTAT 39
151 { "_divstat"},
152 #define N_IP6STAT 40
153 { "_ip6stat" },
154 #define N_TCB6 41
155 { "_tcb6" },
156 #define N_TCP6STAT 42
157 { "_tcp6stat" },
158 #define N_UDB6 43
159 { "_udb6" },
160 #define N_UDP6STAT 44
161 { "_udp6stat" },
162 #define N_ICMP6STAT 45
163 { "_icmp6stat" },
164 #define N_IPSECSTAT 46
165 { "_ipsecstat" },
166 #define N_IPSEC6STAT 47
167 { "_ipsec6stat" },
168 #define N_PIM6STAT 48
169 { "_pim6stat" },
170 #define N_MRT6PROTO 49
171 { "_ip6_mrtproto" },
172 #define N_MRT6STAT 50
173 { "_mrt6stat" },
174 #define N_MF6CTABLE 51
175 { "_mf6ctable" },
176 #define N_MIF6TABLE 52
177 { "_mif6table" },
178 #define N_PFKEYSTAT 53
179 { "_pfkeystat" },
180 { "" },
181 };
182
183 struct protox {
184 u_char pr_index; /* index into nlist of cb head */
185 u_char pr_sindex; /* index into nlist of stat block */
186 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
187 void (*pr_cblocks) /* control blocks printing routine */
188 __P((u_long, char *));
189 void (*pr_stats) /* statistics printing routine */
190 __P((u_long, char *));
191 void (*pr_istats)
192 __P((char *)); /* per/if statistics printing routine */
193 void (*pr_dump) /* PCB state dump routine */
194 __P((u_long));
195 char *pr_name; /* well-known name */
196 } protox[] = {
197 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
198 tcp_stats, NULL, tcp_dump, "tcp" },
199 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
200 udp_stats, NULL, 0, "udp" },
201 { -1, N_IPSTAT, 1, 0,
202 ip_stats, NULL, 0, "ip" },
203 { -1, N_ICMPSTAT, 1, 0,
204 icmp_stats, NULL, 0, "icmp" },
205 { -1, N_IGMPSTAT, 1, 0,
206 igmp_stats, NULL, 0, "igmp" },
207 #ifdef IPSEC
208 { -1, N_IPSECSTAT, 1, 0,
209 ipsec_stats, NULL, 0, "ipsec" },
210 #endif
211 { -1, -1, 0, 0,
212 0, NULL, 0, 0 }
213 };
214
215 #ifdef INET6
216 struct protox ip6protox[] = {
217 { -1, N_IP6STAT, 1, 0,
218 ip6_stats, ip6_ifstats, 0, "ip6" },
219 { -1, N_ICMP6STAT, 1, 0,
220 icmp6_stats, icmp6_ifstats, 0, "icmp6" },
221 #ifdef TCP6
222 { N_TCB6, N_TCP6STAT, 1, ip6protopr,
223 tcp6_stats, NULL, tcp6_dump, "tcp6" },
224 #else
225 { N_TCB6, N_TCP6STAT, 1, ip6protopr,
226 tcp_stats, NULL, tcp_dump, "tcp6" },
227 #endif
228 { N_UDB6, N_UDP6STAT, 1, ip6protopr,
229 udp6_stats, NULL, 0, "udp6" },
230 #ifdef IPSEC
231 { -1, N_IPSEC6STAT, 1, 0,
232 ipsec_stats, NULL, 0, "ipsec6" },
233 #endif
234 { -1, N_PIM6STAT, 1, 0,
235 pim6_stats, NULL, 0, "pim6" },
236 { -1, -1, 0, 0,
237 0, NULL, 0, 0 }
238 };
239 #endif
240
241 #ifdef IPSEC
242 struct protox pfkeyprotox[] = {
243 { -1, N_PFKEYSTAT, 1, 0,
244 pfkey_stats, NULL, 0, "pfkey" },
245 { -1, -1, 0, 0,
246 0, NULL, 0, 0 }
247 };
248 #endif
249
250 #ifndef SMALL
251 struct protox atalkprotox[] = {
252 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
253 ddp_stats, NULL, 0, "ddp" },
254 { -1, -1, 0, 0,
255 0, NULL, 0 }
256 };
257
258 struct protox nsprotox[] = {
259 { N_IDP, N_IDPSTAT, 1, nsprotopr,
260 idp_stats, NULL, 0, "idp" },
261 { N_IDP, N_SPPSTAT, 1, nsprotopr,
262 spp_stats, NULL, 0, "spp" },
263 { -1, N_NSERR, 1, 0,
264 nserr_stats, NULL, 0, "ns_err" },
265 { -1, -1, 0, 0,
266 0, NULL, 0 }
267 };
268
269 struct protox isoprotox[] = {
270 { ISO_TP, N_TPSTAT, 1, iso_protopr,
271 tp_stats, NULL, 0, "tp" },
272 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
273 cltp_stats, NULL, 0, "cltp" },
274 { -1, N_CLNPSTAT, 1, 0,
275 clnp_stats, NULL, 0, "clnp"},
276 { -1, N_ESISSTAT, 1, 0,
277 esis_stats, NULL, 0, "esis"},
278 { -1, -1, 0, 0,
279 0, NULL, 0, 0 }
280 };
281 #endif
282
283 struct protox *protoprotox[] = { protox,
284 #ifdef INET6
285 ip6protox,
286 #endif
287 #ifdef IPSEC
288 pfkeyprotox,
289 #endif
290 #ifndef SMALL
291 atalkprotox, nsprotox, isoprotox,
292 #endif
293 NULL };
294
295 int main __P((int, char *[]));
296 static void printproto __P((struct protox *, char *));
297 static void usage __P((void));
298 static struct protox *name2protox __P((char *));
299 static struct protox *knownname __P((char *));
300
301 kvm_t *kvmd;
302
303 int
304 main(argc, argv)
305 int argc;
306 char *argv[];
307 {
308 struct protoent *p;
309 struct protox *tp; /* for printing cblocks & stats */
310 int ch;
311 char *nlistf = NULL, *memf = NULL;
312 char buf[_POSIX2_LINE_MAX], *cp;
313 u_long pcbaddr;
314 gid_t egid = getegid();
315
316 (void)setegid(getgid());
317 tp = NULL;
318 af = AF_UNSPEC;
319 pcbaddr = 0;
320
321 while ((ch = getopt(argc, argv, "Aabdf:ghI:LliM:mN:nP:p:rstuvw:")) != -1)
322 switch(ch) {
323 case 'A':
324 Aflag = 1;
325 break;
326 case 'a':
327 aflag = 1;
328 break;
329 case 'b':
330 bflag = 1;
331 break;
332 case 'd':
333 dflag = 1;
334 break;
335 case 'f':
336 if (strcmp(optarg, "ns") == 0)
337 af = AF_NS;
338 else if (strcmp(optarg, "inet") == 0)
339 af = AF_INET;
340 else if (strcmp(optarg, "inet6") == 0)
341 af = AF_INET6;
342 else if (strcmp(optarg, "pfkey") == 0)
343 af = PF_KEY;
344 else if (strcmp(optarg, "unix") == 0
345 || strcmp(optarg, "local") == 0)
346 af = AF_LOCAL;
347 else if (strcmp(optarg, "iso") == 0)
348 af = AF_ISO;
349 else if (strcmp(optarg, "atalk") == 0)
350 af = AF_APPLETALK;
351 else
352 errx(1, "%s: unknown address family",
353 optarg);
354 break;
355 #ifndef SMALL
356 case 'g':
357 gflag = 1;
358 break;
359 #endif
360 case 'I':
361 iflag = 1;
362 interface = optarg;
363 break;
364 case 'i':
365 iflag = 1;
366 break;
367 case 'L':
368 Lflag = 1;
369 break;
370 case 'l':
371 lflag = 1;
372 break;
373 case 'M':
374 memf = optarg;
375 break;
376 case 'm':
377 mflag = 1;
378 break;
379 case 'N':
380 nlistf = optarg;
381 break;
382 case 'n':
383 nflag = 1;
384 break;
385 case 'P':
386 pcbaddr = strtoul(optarg, &cp, 16);
387 if (*cp != '\0' || errno == ERANGE)
388 errx(1, "invalid PCB address %s",
389 optarg);
390 Pflag = 1;
391 break;
392 case 'p':
393 if ((tp = name2protox(optarg)) == NULL)
394 errx(1, "%s: unknown or uninstrumented protocol",
395 optarg);
396 pflag = 1;
397 break;
398 case 'r':
399 rflag = 1;
400 break;
401 case 's':
402 ++sflag;
403 break;
404 case 't':
405 tflag = 1;
406 break;
407 case 'u':
408 af = AF_LOCAL;
409 break;
410 case 'v':
411 vflag = 1;
412 break;
413 case 'w':
414 interval = atoi(optarg);
415 iflag = 1;
416 break;
417 case '?':
418 default:
419 usage();
420 }
421 argv += optind;
422 argc -= optind;
423
424 #define BACKWARD_COMPATIBILITY
425 #ifdef BACKWARD_COMPATIBILITY
426 if (*argv) {
427 if (isdigit(**argv)) {
428 interval = atoi(*argv);
429 if (interval <= 0)
430 usage();
431 ++argv;
432 iflag = 1;
433 }
434 if (*argv) {
435 nlistf = *argv;
436 if (*++argv)
437 memf = *argv;
438 }
439 }
440 #endif
441
442 /*
443 * Discard setgid privileges. If not the running kernel, we toss
444 * them away totally so that bad guys can't print interesting stuff
445 * from kernel memory, otherwise switch back to kmem for the
446 * duration of the kvm_openfiles() call.
447 */
448 if (nlistf != NULL || memf != NULL || Pflag)
449 (void)setgid(getgid());
450 else
451 (void)setegid(egid);
452
453 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
454 buf)) == NULL)
455 errx(1, "%s", buf);
456
457 /* do this now anyway */
458 if (nlistf == NULL && memf == NULL)
459 (void)setgid(getgid());
460
461 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
462 if (nlistf)
463 errx(1, "%s: no namelist", nlistf);
464 else
465 errx(1, "no namelist");
466 }
467 if (mflag) {
468 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
469 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
470 nl[N_MCLPOOL].n_value);
471 exit(0);
472 }
473 if (Pflag) {
474 if (tp == NULL) {
475 /* Default to TCP. */
476 tp = name2protox("tcp");
477 }
478 if (tp->pr_dump)
479 (*tp->pr_dump)(pcbaddr);
480 else
481 printf("%s: no PCB dump routine\n", tp->pr_name);
482 exit(0);
483 }
484 if (pflag) {
485 if (iflag && tp->pr_istats)
486 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
487 else if (tp->pr_stats)
488 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
489 tp->pr_name);
490 else
491 printf("%s: no stats routine\n", tp->pr_name);
492 exit(0);
493 }
494 /*
495 * Keep file descriptors open to avoid overhead
496 * of open/close on each call to get* routines.
497 */
498 sethostent(1);
499 setnetent(1);
500 if (iflag) {
501 if (af != AF_UNSPEC)
502 goto protostat;
503
504 intpr(interval, nl[N_IFNET].n_value, NULL);
505 exit(0);
506 }
507 if (rflag) {
508 if (sflag)
509 rt_stats(nl[N_RTSTAT].n_value);
510 else
511 routepr(nl[N_RTREE].n_value);
512 exit(0);
513 }
514 #ifndef SMALL
515 if (gflag) {
516 if (sflag) {
517 if (af == AF_INET || af == AF_UNSPEC)
518 mrt_stats(nl[N_MRTPROTO].n_value,
519 nl[N_MRTSTAT].n_value);
520 #ifdef INET6
521 if (af == AF_INET6 || af == AF_UNSPEC)
522 mrt6_stats(nl[N_MRT6PROTO].n_value,
523 nl[N_MRT6STAT].n_value);
524 #endif
525 }
526 else {
527 if (af == AF_INET || af == AF_UNSPEC)
528 mroutepr(nl[N_MRTPROTO].n_value,
529 nl[N_MFCHASHTBL].n_value,
530 nl[N_MFCHASH].n_value,
531 nl[N_VIFTABLE].n_value);
532 #ifdef INET6
533 if (af == AF_INET6 || af == AF_UNSPEC)
534 mroute6pr(nl[N_MRT6PROTO].n_value,
535 nl[N_MF6CTABLE].n_value,
536 nl[N_MIF6TABLE].n_value);
537 #endif
538 }
539 exit(0);
540 }
541 #endif
542 protostat:
543 if (af == AF_INET || af == AF_UNSPEC) {
544 setprotoent(1);
545 setservent(1);
546 /* ugh, this is O(MN) ... why do we do this? */
547 while ((p = getprotoent()) != NULL) {
548 for (tp = protox; tp->pr_name; tp++)
549 if (strcmp(tp->pr_name, p->p_name) == 0)
550 break;
551 if (tp->pr_name == 0 || tp->pr_wanted == 0)
552 continue;
553 printproto(tp, p->p_name);
554 tp->pr_wanted = 0;
555 }
556 endprotoent();
557 for (tp = protox; tp->pr_name; tp++)
558 if (tp->pr_wanted)
559 printproto(tp, tp->pr_name);
560 }
561 #ifdef INET6
562 if (af == AF_INET6 || af == AF_UNSPEC)
563 for (tp = ip6protox; tp->pr_name; tp++)
564 printproto(tp, tp->pr_name);
565 #endif
566 #ifdef IPSEC
567 if (af == PF_KEY || af == AF_UNSPEC)
568 for (tp = pfkeyprotox; tp->pr_name; tp++)
569 printproto(tp, tp->pr_name);
570 #endif
571 #ifndef SMALL
572 if (af == AF_APPLETALK || af == AF_UNSPEC)
573 for (tp = atalkprotox; tp->pr_name; tp++)
574 printproto(tp, tp->pr_name);
575 if (af == AF_NS || af == AF_UNSPEC)
576 for (tp = nsprotox; tp->pr_name; tp++)
577 printproto(tp, tp->pr_name);
578 if (af == AF_ISO || af == AF_UNSPEC)
579 for (tp = isoprotox; tp->pr_name; tp++)
580 printproto(tp, tp->pr_name);
581 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
582 unixpr(nl[N_UNIXSW].n_value);
583 #endif
584 exit(0);
585 }
586
587 /*
588 * Print out protocol statistics or control blocks (per sflag).
589 * If the interface was not specifically requested, and the symbol
590 * is not in the namelist, ignore this one.
591 */
592 static void
593 printproto(tp, name)
594 struct protox *tp;
595 char *name;
596 {
597 void (*pr) __P((u_long, char *));
598 u_long off;
599
600 if (sflag) {
601 if (iflag) {
602 if (tp->pr_istats)
603 intpr(interval, nl[N_IFNET].n_value,
604 tp->pr_istats);
605 return;
606 }
607 else {
608 pr = tp->pr_stats;
609 off = nl[tp->pr_sindex].n_value;
610 }
611 } else {
612 pr = tp->pr_cblocks;
613 off = nl[tp->pr_index].n_value;
614 }
615 if (pr != NULL && (off || af != AF_UNSPEC))
616 (*pr)(off, name);
617 }
618
619 /*
620 * Read kernel memory, return 0 on success.
621 */
622 int
623 kread(addr, buf, size)
624 u_long addr;
625 char *buf;
626 int size;
627 {
628
629 if (kvm_read(kvmd, addr, buf, size) != size) {
630 warnx("%s\n", kvm_geterr(kvmd));
631 return (-1);
632 }
633 return (0);
634 }
635
636 char *
637 plural(n)
638 int n;
639 {
640
641 return (n != 1 ? "s" : "");
642 }
643
644 char *
645 plurales(n)
646 int n;
647 {
648
649 return (n != 1 ? "es" : "");
650 }
651
652 /*
653 * Find the protox for the given "well-known" name.
654 */
655 static struct protox *
656 knownname(name)
657 char *name;
658 {
659 struct protox **tpp, *tp;
660
661 for (tpp = protoprotox; *tpp; tpp++)
662 for (tp = *tpp; tp->pr_name; tp++)
663 if (strcmp(tp->pr_name, name) == 0)
664 return (tp);
665 return (NULL);
666 }
667
668 /*
669 * Find the protox corresponding to name.
670 */
671 static struct protox *
672 name2protox(name)
673 char *name;
674 {
675 struct protox *tp;
676 char **alias; /* alias from p->aliases */
677 struct protoent *p;
678
679 /*
680 * Try to find the name in the list of "well-known" names. If that
681 * fails, check if name is an alias for an Internet protocol.
682 */
683 if ((tp = knownname(name)) != NULL)
684 return (tp);
685
686 setprotoent(1); /* make protocol lookup cheaper */
687 while ((p = getprotoent()) != NULL) {
688 /* assert: name not same as p->name */
689 for (alias = p->p_aliases; *alias; alias++)
690 if (strcmp(name, *alias) == 0) {
691 endprotoent();
692 return (knownname(p->p_name));
693 }
694 }
695 endprotoent();
696 return (NULL);
697 }
698
699 static void
700 usage()
701 {
702 (void)fprintf(stderr,
703 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
704 (void)fprintf(stderr,
705 " %s [-gimnrsv] [-f address_family] [-M core] [-N system]\n",
706 __progname);
707 (void)fprintf(stderr,
708 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
709 (void)fprintf(stderr,
710 " %s [-M core] [-N system] [-p protocol]\n", __progname);
711 (void)fprintf(stderr,
712 " %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", __progname);
713 exit(1);
714 }
715