main.c revision 1.78 1 /* $NetBSD: main.c,v 1.78 2011/05/03 18:28:46 dyoung 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\
35 Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94";
41 #else
42 __RCSID("$NetBSD: main.c,v 1.78 2011/05/03 18:28:46 dyoung Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50
51 #include <net/if.h>
52 #include <netinet/in.h>
53
54 #include <ctype.h>
55 #include <err.h>
56 #include <errno.h>
57 #include <kvm.h>
58 #include <limits.h>
59 #include <netdb.h>
60 #include <nlist.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 #include "netstat.h"
67 #include "prog_ops.h"
68
69 struct nlist nl[] = {
70 #define N_MBSTAT 0
71 { "_mbstat", 0, 0, 0, 0 },
72 #define N_IPSTAT 1
73 { "_ipstat", 0, 0, 0, 0 }, /* not available via kvm */
74 #define N_TCBTABLE 2
75 { "_tcbtable", 0, 0, 0, 0 },
76 #define N_TCPSTAT 3
77 { "_tcpstat", 0, 0, 0, 0 }, /* not available via kvm */
78 #define N_UDBTABLE 4
79 { "_udbtable", 0, 0, 0, 0 },
80 #define N_UDPSTAT 5
81 { "_udpstat", 0, 0, 0, 0 }, /* not available via kvm */
82 #define N_IFNET 6
83 { "_ifnet", 0, 0, 0, 0 },
84 #define N_IMP 7
85 { "_imp_softc", 0, 0, 0, 0 },
86 #define N_ICMPSTAT 8
87 { "_icmpstat", 0, 0, 0, 0 }, /* not available via kvm */
88 #define N_RTSTAT 9
89 { "_rtstat", 0, 0, 0, 0 },
90 #define N_UNIXSW 10
91 { "_unixsw", 0, 0, 0, 0 },
92 #define N_CLNPSTAT 11
93 { "_clnp_stat", 0, 0, 0, 0 },
94 #define IN_NOTUSED 12
95 { "_tp_inpcb", 0, 0, 0, 0 },
96 #define ISO_TP 13
97 { "_tp_refinfo", 0, 0, 0, 0 },
98 #define N_TPSTAT 14
99 { "_tp_stat", 0, 0, 0, 0 },
100 #define N_ESISSTAT 15
101 { "_esis_stat", 0, 0, 0, 0 },
102 #define N_NIMP 16
103 { "_nimp", 0, 0, 0, 0 },
104 #define N_RTREE 17
105 { "_rt_tables", 0, 0, 0, 0 },
106 #define N_CLTP 18
107 { "_cltb", 0, 0, 0, 0 },
108 #define N_CLTPSTAT 19
109 { "_cltpstat", 0, 0, 0, 0 },
110 #define N_NFILE 20
111 { "_nfile", 0, 0, 0, 0 },
112 #define N_FILE 21
113 { "_file", 0, 0, 0, 0 },
114 #define N_IGMPSTAT 22
115 { "_igmpstat", 0, 0, 0, 0 }, /* not available via kvm */
116 #define N_MRTPROTO 23
117 { "_ip_mrtproto", 0, 0, 0, 0 },
118 #define N_MRTSTAT 24
119 { "_mrtstat", 0, 0, 0, 0 },
120 #define N_MFCHASHTBL 25
121 { "_mfchashtbl", 0, 0, 0, 0 },
122 #define N_MFCHASH 26
123 { "_mfchash", 0, 0, 0, 0 },
124 #define N_VIFTABLE 27
125 { "_viftable", 0, 0, 0, 0 },
126 #define N_MSIZE 28
127 { "_msize", 0, 0, 0, 0 },
128 #define N_MCLBYTES 29
129 { "_mclbytes", 0, 0, 0, 0 },
130 #define N_DDPSTAT 30
131 { "_ddpstat", 0, 0, 0, 0 }, /* not available via kvm */
132 #define N_DDPCB 31
133 { "_ddpcb", 0, 0, 0, 0 },
134 #define N_MBPOOL 32
135 { "_mbpool", 0, 0, 0, 0 },
136 #define N_MCLPOOL 33
137 { "_mclpool", 0, 0, 0, 0 },
138 #define N_DIVPCB 34
139 { "_divcb", 0, 0, 0, 0 },
140 #define N_DIVSTAT 35
141 { "_divstat", 0, 0, 0, 0 },
142 #define N_IP6STAT 36
143 { "_ip6stat", 0, 0, 0, 0 }, /* not available via kvm */
144 #define N_TCP6STAT 37
145 { "_tcp6stat", 0, 0, 0, 0 }, /* not available via kvm */
146 #define N_UDP6STAT 38
147 { "_udp6stat", 0, 0, 0, 0 }, /* not available via kvm */
148 #define N_ICMP6STAT 39
149 { "_icmp6stat", 0, 0, 0, 0 }, /* not available via kvm */
150 #define N_IPSECSTAT 40
151 { "_ipsecstat", 0, 0, 0, 0 }, /* not available via kvm */
152 #define N_IPSEC6STAT 41
153 { "_ipsec6stat", 0, 0, 0, 0 }, /* not available via kvm */
154 #define N_PIM6STAT 42
155 { "_pim6stat", 0, 0, 0, 0 }, /* not available via kvm */
156 #define N_MRT6PROTO 43
157 { "_ip6_mrtproto", 0, 0, 0, 0 },
158 #define N_MRT6STAT 44
159 { "_mrt6stat", 0, 0, 0, 0 },
160 #define N_MF6CTABLE 45
161 { "_mf6ctable", 0, 0, 0, 0 },
162 #define N_MIF6TABLE 46
163 { "_mif6table", 0, 0, 0, 0 },
164 #define N_PFKEYSTAT 47
165 { "_pfkeystat", 0, 0, 0, 0 }, /* not available via kvm */
166 #define N_ARPSTAT 48
167 { "_arpstat", 0, 0, 0, 0 }, /* not available via kvm */
168 #define N_RIP6STAT 49
169 { "_rip6stat", 0, 0, 0, 0 }, /* not available via kvm */
170 #define N_ARPINTRQ 50
171 { "_arpintrq", 0, 0, 0, 0 },
172 #define N_IPINTRQ 51
173 { "_ipintrq", 0, 0, 0, 0 },
174 #define N_IP6INTRQ 52
175 { "_ip6intrq", 0, 0, 0, 0 },
176 #define N_ATINTRQ1 53
177 { "_atintrq1", 0, 0, 0, 0 },
178 #define N_ATINTRQ2 54
179 { "_atintrq2", 0, 0, 0, 0 },
180 #define N_NSINTRQ 55
181 { "_nsintrq", 0, 0, 0, 0 },
182 #define N_CLNLINTRQ 56
183 { "_clnlintrq", 0, 0, 0, 0 },
184 #define N_LLCINTRQ 57
185 { "_llcintrq", 0, 0, 0, 0 },
186 #define N_HDINTRQ 58
187 { "_hdintrq", 0, 0, 0, 0 },
188 #define N_NATMINTRQ 59
189 { "_natmintrq", 0, 0, 0, 0 },
190 #define N_PPPOEDISCINQ 61
191 { "_ppoediscinq", 0, 0, 0, 0 },
192 #define N_PPPOEINQ 61
193 { "_ppoeinq", 0, 0, 0, 0 },
194 #define N_PKINTRQ 62
195 { "_pkintrq", 0, 0, 0, 0 },
196 #define N_HARDCLOCK_TICKS 63
197 { "_hardclock_ticks", 0, 0, 0, 0 },
198 #define N_PIMSTAT 64
199 { "_pimstat", 0, 0, 0, 0 },
200 #define N_CARPSTAT 65
201 { "_carpstats", 0, 0, 0, 0 }, /* not available via kvm */
202 #define N_PFSYNCSTAT 66
203 { "_pfsyncstats", 0, 0, 0, 0}, /* not available via kvm */
204 { "", 0, 0, 0, 0 },
205 };
206
207 struct protox {
208 u_char pr_index; /* index into nlist of cb head */
209 u_char pr_sindex; /* index into nlist of stat block */
210 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
211 void (*pr_cblocks) /* control blocks printing routine */
212 __P((u_long, const char *));
213 void (*pr_stats) /* statistics printing routine */
214 __P((u_long, const char *));
215 void (*pr_istats)
216 __P((const char *)); /* per/if statistics printing routine */
217 void (*pr_dump) /* PCB state dump routine */
218 __P((u_long));
219 const char *pr_name; /* well-known name */
220 } protox[] = {
221 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
222 tcp_stats, NULL, tcp_dump, "tcp" },
223 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
224 udp_stats, NULL, 0, "udp" },
225 { -1, N_IPSTAT, 1, 0,
226 ip_stats, NULL, 0, "ip" },
227 { -1, N_ICMPSTAT, 1, 0,
228 icmp_stats, NULL, 0, "icmp" },
229 { -1, N_IGMPSTAT, 1, 0,
230 igmp_stats, NULL, 0, "igmp" },
231 { -1, N_CARPSTAT, 1, 0,
232 carp_stats, NULL, 0, "carp" },
233 #ifdef IPSEC
234 { -1, N_IPSECSTAT, 1, 0,
235 ipsec_switch, NULL, 0, "ipsec" },
236 #endif
237 { -1, N_PIMSTAT, 1, 0,
238 pim_stats, NULL, 0, "pim" },
239 { -1, N_PFSYNCSTAT, 1, 0,
240 pfsync_stats, NULL, 0, "pfsync" },
241 { -1, -1, 0, 0,
242 0, NULL, 0, 0 }
243 };
244
245 #ifdef INET6
246 struct protox ip6protox[] = {
247 { -1, N_IP6STAT, 1, 0,
248 ip6_stats, ip6_ifstats, 0, "ip6" },
249 { -1, N_ICMP6STAT, 1, 0,
250 icmp6_stats, icmp6_ifstats, 0, "icmp6" },
251 #ifdef TCP6
252 { N_TCBTABLE, N_TCP6STAT, 1, ip6protopr,
253 tcp6_stats, NULL, tcp6_dump, "tcp6" },
254 #else
255 { N_TCBTABLE, N_TCP6STAT, 1, ip6protopr,
256 tcp_stats, NULL, tcp_dump, "tcp6" },
257 #endif
258 { N_UDBTABLE, N_UDP6STAT, 1, ip6protopr,
259 udp6_stats, NULL, 0, "udp6" },
260 #ifdef IPSEC
261 { -1, N_IPSEC6STAT, 1, 0,
262 ipsec_switch, NULL, 0, "ipsec6" },
263 #endif
264 { -1, N_PIM6STAT, 1, 0,
265 pim6_stats, NULL, 0, "pim6" },
266 { -1, N_RIP6STAT, 1, 0,
267 rip6_stats, NULL, 0, "rip6" },
268 { -1, -1, 0, 0,
269 0, NULL, 0, 0 }
270 };
271 #endif
272
273 struct protox arpprotox[] = {
274 { -1, N_ARPSTAT, 1, 0,
275 arp_stats, NULL, 0, "arp" },
276 { -1, -1, 0, 0,
277 0, NULL, 0, 0 }
278 };
279
280 #ifdef IPSEC
281 struct protox pfkeyprotox[] = {
282 { -1, N_PFKEYSTAT, 1, 0,
283 pfkey_stats, NULL, 0, "pfkey" },
284 { -1, -1, 0, 0,
285 0, NULL, 0, 0 }
286 };
287 #endif
288
289 #ifndef SMALL
290 struct protox atalkprotox[] = {
291 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
292 ddp_stats, NULL, 0, "ddp" },
293 { -1, -1, 0, 0,
294 0, NULL, 0, NULL }
295 };
296
297 struct protox isoprotox[] = {
298 { ISO_TP, N_TPSTAT, 1, iso_protopr,
299 tp_stats, NULL, 0, "tp" },
300 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
301 cltp_stats, NULL, 0, "cltp" },
302 { -1, N_CLNPSTAT, 1, 0,
303 clnp_stats, NULL, 0, "clnp"},
304 { -1, N_ESISSTAT, 1, 0,
305 esis_stats, NULL, 0, "esis"},
306 { -1, -1, 0, 0,
307 0, NULL, 0, 0 }
308 };
309 #endif
310
311 struct protox *protoprotox[] = { protox,
312 #ifdef INET6
313 ip6protox,
314 #endif
315 arpprotox,
316 #ifdef IPSEC
317 pfkeyprotox,
318 #endif
319 #ifndef SMALL
320 atalkprotox,
321 isoprotox,
322 #endif
323 NULL };
324
325 const struct softintrq {
326 const char *siq_name;
327 int siq_index;
328 } softintrq[] = {
329 { "arpintrq", N_ARPINTRQ },
330 { "ipintrq", N_IPINTRQ },
331 { "ip6intrq", N_IP6INTRQ },
332 { "atintrq1", N_ATINTRQ1 },
333 { "atintrq2", N_ATINTRQ2 },
334 { "clnlintrq", N_CLNLINTRQ },
335 { "llcintrq", N_LLCINTRQ },
336 { "hdintrq", N_HDINTRQ },
337 { "natmintrq", N_NATMINTRQ },
338 { "ppoediscinq", N_PPPOEDISCINQ },
339 { "ppoeinq", N_PPPOEINQ },
340 { "pkintrq", N_PKINTRQ },
341 { NULL, -1 },
342 };
343
344 int main __P((int, char *[]));
345 static void printproto __P((struct protox *, const char *));
346 static void print_softintrq __P((void));
347 static void usage __P((void));
348 static struct protox *name2protox __P((const char *));
349 static struct protox *knownname __P((const char *));
350 static void prepare(const char *, const char *, struct protox *tp);
351 static kvm_t *prepare_kvmd(const char *, const char *);
352
353 kvm_t *kvmd = NULL;
354 gid_t egid;
355 int interval; /* repeat interval for i/f stats */
356 static const char *nlistf = NULL, *memf = NULL;
357
358 kvm_t *
359 get_kvmd(void)
360 {
361 return prepare_kvmd(nlistf, memf);
362 }
363
364 static kvm_t *
365 prepare_kvmd(const char *nf, const char *mf)
366 {
367 char buf[_POSIX2_LINE_MAX];
368
369 if (kvmd != NULL)
370 return kvmd;
371 (void)setegid(egid);
372 kvmd = kvm_openfiles(nf, mf, NULL, O_RDONLY, buf);
373 (void)setgid(getgid());
374 if (kvmd == NULL)
375 err(1, "kvm error: %s", buf);
376 return kvmd;
377 }
378
379 void
380 prepare(const char *nf, const char *mf, struct protox *tp)
381 {
382 /*
383 * Try to figure out if we can use sysctl or not.
384 */
385 if (nf != NULL && mf != NULL) {
386 /* Of course, we can't use sysctl with dumps. */
387 if (force_sysctl)
388 errx(EXIT_FAILURE, "can't use sysctl with dumps");
389
390 /* If we have -M and -N, we're not dealing with live memory. */
391 use_sysctl = 0;
392 } else if (true || qflag ||
393 rflag ||
394 iflag ||
395 #ifndef SMALL
396 gflag ||
397 (pflag && tp->pr_sindex == N_TPSTAT) ||
398 (pflag && tp->pr_sindex == N_CLTPSTAT) ||
399 (pflag && tp->pr_sindex == N_CLNPSTAT) ||
400 (pflag && tp->pr_sindex == N_ESISSTAT) ||
401 #endif
402 (pflag && tp->pr_sindex == N_PIMSTAT) ||
403 Pflag) {
404 /* These flags are not yet supported via sysctl(3). */
405 use_sysctl = 0;
406 } else {
407 /* We can use sysctl(3). */
408 use_sysctl = 1;
409 }
410
411 if (force_sysctl && !use_sysctl) {
412 /* Let the user know what's about to happen. */
413 warnx("forcing sysctl usage even though it might not be "\
414 "supported");
415 use_sysctl = 1;
416 }
417
418 if (!use_sysctl) {
419 kvmd = prepare_kvmd(nf, mf);
420
421 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
422 if (nf)
423 errx(1, "%s: no namelist", nf);
424 else
425 errx(1, "no namelist");
426 }
427 } else
428 (void)setgid(getgid());
429 }
430
431 int
432 main(argc, argv)
433 int argc;
434 char *argv[];
435 {
436 struct protoent *p;
437 struct protox *tp; /* for printing cblocks & stats */
438 int ch;
439 char *cp;
440 u_long pcbaddr;
441
442 if (prog_init) {
443 if (prog_init() == -1)
444 err(1, "init failed");
445 force_sysctl = 1; /* cheap trick */
446 }
447
448 egid = getegid();
449 (void)setegid(getgid());
450 tp = NULL;
451 af = AF_UNSPEC;
452 pcbaddr = 0;
453
454 while ((ch = getopt(argc, argv,
455 "AabBdf:ghI:LliM:mN:nP:p:qrsStTuVvw:X")) != -1)
456 switch (ch) {
457 case 'A':
458 Aflag = 1;
459 break;
460 case 'a':
461 aflag = 1;
462 break;
463 case 'b':
464 bflag = 1;
465 break;
466 case 'B':
467 Bflag = 1;
468 break;
469 case 'd':
470 dflag = 1;
471 break;
472 case 'f':
473 if (strcmp(optarg, "inet") == 0)
474 af = AF_INET;
475 else if (strcmp(optarg, "inet6") == 0)
476 af = AF_INET6;
477 else if (strcmp(optarg, "arp") == 0)
478 af = AF_ARP;
479 else if (strcmp(optarg, "pfkey") == 0)
480 af = PF_KEY;
481 else if (strcmp(optarg, "unix") == 0
482 || strcmp(optarg, "local") == 0)
483 af = AF_LOCAL;
484 else if (strcmp(optarg, "iso") == 0)
485 af = AF_ISO;
486 else if (strcmp(optarg, "atalk") == 0)
487 af = AF_APPLETALK;
488 else if (strcmp(optarg, "mpls") == 0)
489 af = AF_MPLS;
490 else
491 errx(1, "%s: unknown address family",
492 optarg);
493 break;
494 #ifndef SMALL
495 case 'g':
496 gflag = 1;
497 break;
498 #endif
499 case 'h':
500 hflag = 1;
501 break;
502 case 'I':
503 iflag = 1;
504 interface = optarg;
505 break;
506 case 'i':
507 iflag = 1;
508 break;
509 case 'L':
510 Lflag = 1;
511 break;
512 case 'l':
513 lflag = 1;
514 break;
515 case 'M':
516 memf = optarg;
517 break;
518 case 'm':
519 mflag = 1;
520 break;
521 case 'N':
522 nlistf = optarg;
523 break;
524 case 'n':
525 numeric_addr = numeric_port = nflag = 1;
526 break;
527 case 'P':
528 errno = 0;
529 pcbaddr = strtoul(optarg, &cp, 16);
530 if (*cp != '\0' || errno == ERANGE)
531 errx(1, "invalid PCB address %s",
532 optarg);
533 Pflag = 1;
534 break;
535 case 'p':
536 if ((tp = name2protox(optarg)) == NULL)
537 errx(1, "%s: unknown or uninstrumented protocol",
538 optarg);
539 pflag = 1;
540 break;
541 case 'q':
542 qflag = 1;
543 break;
544 case 'r':
545 rflag = 1;
546 break;
547 case 's':
548 ++sflag;
549 break;
550 case 'S':
551 numeric_addr = 1;
552 break;
553 case 't':
554 tflag = 1;
555 break;
556 case 'T':
557 tagflag = 1;
558 break;
559 case 'u':
560 af = AF_LOCAL;
561 break;
562 case 'V':
563 Vflag++;
564 break;
565 case 'v':
566 vflag++;
567 break;
568 case 'w':
569 interval = atoi(optarg);
570 iflag = 1;
571 break;
572 case 'X':
573 force_sysctl = 1;
574 break;
575 case '?':
576 default:
577 usage();
578 }
579 argv += optind;
580 argc -= optind;
581
582 #define BACKWARD_COMPATIBILITY
583 #ifdef BACKWARD_COMPATIBILITY
584 if (*argv) {
585 if (isdigit((unsigned char)**argv)) {
586 interval = atoi(*argv);
587 if (interval <= 0)
588 usage();
589 ++argv;
590 iflag = 1;
591 }
592 if (*argv) {
593 nlistf = *argv;
594 if (*++argv)
595 memf = *argv;
596 }
597 }
598 #endif
599
600 prepare(nlistf, memf, tp);
601
602 #ifndef SMALL
603 if (Bflag) {
604 if (sflag)
605 bpf_stats();
606 else
607 bpf_dump(interface);
608 exit(0);
609 }
610 #endif
611
612 if (mflag) {
613 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
614 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
615 nl[N_MCLPOOL].n_value);
616 exit(0);
617 }
618 if (Pflag) {
619 if (tp == NULL) {
620 /* Default to TCP. */
621 tp = name2protox("tcp");
622 }
623 if (tp->pr_dump)
624 (*tp->pr_dump)(pcbaddr);
625 else
626 printf("%s: no PCB dump routine\n", tp->pr_name);
627 exit(0);
628 }
629 if (pflag) {
630 if (iflag && tp->pr_istats)
631 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
632 else if (tp->pr_stats)
633 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
634 tp->pr_name);
635 else
636 printf("%s: no stats routine\n", tp->pr_name);
637 exit(0);
638 }
639 if (qflag) {
640 print_softintrq();
641 exit(0);
642 }
643 /*
644 * Keep file descriptors open to avoid overhead
645 * of open/close on each call to get* routines.
646 */
647 sethostent(1);
648 setnetent(1);
649 if (iflag) {
650 if (af != AF_UNSPEC)
651 goto protostat;
652
653 intpr(interval, nl[N_IFNET].n_value, NULL);
654 exit(0);
655 }
656 if (rflag) {
657 if (sflag)
658 rt_stats(use_sysctl ? 0 : nl[N_RTSTAT].n_value);
659 else {
660 if (use_sysctl)
661 p_rttables(af);
662 else
663 routepr(nl[N_RTREE].n_value);
664 }
665 exit(0);
666 }
667 #ifndef SMALL
668 if (gflag) {
669 if (sflag) {
670 if (af == AF_INET || af == AF_UNSPEC)
671 mrt_stats(nl[N_MRTPROTO].n_value,
672 nl[N_MRTSTAT].n_value);
673 #ifdef INET6
674 if (af == AF_INET6 || af == AF_UNSPEC)
675 mrt6_stats(nl[N_MRT6PROTO].n_value,
676 nl[N_MRT6STAT].n_value);
677 #endif
678 }
679 else {
680 if (af == AF_INET || af == AF_UNSPEC)
681 mroutepr(nl[N_MRTPROTO].n_value,
682 nl[N_MFCHASHTBL].n_value,
683 nl[N_MFCHASH].n_value,
684 nl[N_VIFTABLE].n_value);
685 #ifdef INET6
686 if (af == AF_INET6 || af == AF_UNSPEC)
687 mroute6pr(nl[N_MRT6PROTO].n_value,
688 nl[N_MF6CTABLE].n_value,
689 nl[N_MIF6TABLE].n_value);
690 #endif
691 }
692 exit(0);
693 }
694 #endif
695 protostat:
696 if (af == AF_INET || af == AF_UNSPEC) {
697 setprotoent(1);
698 setservent(1);
699 /* ugh, this is O(MN) ... why do we do this? */
700 while ((p = getprotoent()) != NULL) {
701 for (tp = protox; tp->pr_name; tp++)
702 if (strcmp(tp->pr_name, p->p_name) == 0)
703 break;
704 if (tp->pr_name == 0 || tp->pr_wanted == 0)
705 continue;
706 printproto(tp, p->p_name);
707 tp->pr_wanted = 0;
708 }
709 endprotoent();
710 for (tp = protox; tp->pr_name; tp++)
711 if (tp->pr_wanted)
712 printproto(tp, tp->pr_name);
713 }
714 #ifdef INET6
715 if (af == AF_INET6 || af == AF_UNSPEC)
716 for (tp = ip6protox; tp->pr_name; tp++)
717 printproto(tp, tp->pr_name);
718 #endif
719 if (af == AF_ARP || af == AF_UNSPEC)
720 for (tp = arpprotox; tp->pr_name; tp++)
721 printproto(tp, tp->pr_name);
722 #ifdef IPSEC
723 if (af == PF_KEY || af == AF_UNSPEC)
724 for (tp = pfkeyprotox; tp->pr_name; tp++)
725 printproto(tp, tp->pr_name);
726 #endif
727 #ifndef SMALL
728 if (af == AF_APPLETALK || af == AF_UNSPEC)
729 for (tp = atalkprotox; tp->pr_name; tp++)
730 printproto(tp, tp->pr_name);
731 if (af == AF_ISO || af == AF_UNSPEC)
732 for (tp = isoprotox; tp->pr_name; tp++)
733 printproto(tp, tp->pr_name);
734 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
735 unixpr(nl[N_UNIXSW].n_value);
736 #endif
737 exit(0);
738 }
739
740 /*
741 * Print out protocol statistics or control blocks (per sflag).
742 * If the interface was not specifically requested, and the symbol
743 * is not in the namelist, ignore this one.
744 */
745 static void
746 printproto(tp, name)
747 struct protox *tp;
748 const char *name;
749 {
750 void (*pr) __P((u_long, const char *));
751 u_long off;
752
753 if (sflag) {
754 if (iflag) {
755 if (tp->pr_istats)
756 intpr(interval, nl[N_IFNET].n_value,
757 tp->pr_istats);
758 return;
759 }
760 else {
761 pr = tp->pr_stats;
762 off = nl[tp->pr_sindex].n_value;
763 }
764 } else {
765 pr = tp->pr_cblocks;
766 off = nl[tp->pr_index].n_value;
767 }
768 if (pr != NULL && ((off || af != AF_UNSPEC) || use_sysctl)) {
769 (*pr)(off, name);
770 }
771 }
772
773 /*
774 * Print softintrq status.
775 */
776 void
777 print_softintrq()
778 {
779 struct ifqueue intrq, *ifq = &intrq;
780 const struct softintrq *siq;
781 u_long off;
782
783 for (siq = softintrq; siq->siq_name != NULL; siq++) {
784 off = nl[siq->siq_index].n_value;
785 if (off == 0)
786 continue;
787
788 kread(off, (char *)ifq, sizeof(*ifq));
789 printf("%s:\n", siq->siq_name);
790 printf("\tqueue length: %d\n", ifq->ifq_len);
791 printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
792 printf("\tpackets dropped: %d\n", ifq->ifq_drops);
793 }
794 }
795
796 /*
797 * Read kernel memory, return 0 on success.
798 */
799 int
800 kread(addr, buf, size)
801 u_long addr;
802 char *buf;
803 int size;
804 {
805
806 if (kvm_read(kvmd, addr, buf, size) != size) {
807 warnx("%s", kvm_geterr(kvmd));
808 return (-1);
809 }
810 return (0);
811 }
812
813 const char *
814 plural(n)
815 int n;
816 {
817
818 return (n != 1 ? "s" : "");
819 }
820
821 const char *
822 plurales(n)
823 int n;
824 {
825
826 return (n != 1 ? "es" : "");
827 }
828
829 int
830 get_hardticks(void)
831 {
832 int hardticks;
833
834 kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks,
835 sizeof(hardticks));
836 return (hardticks);
837 }
838
839 /*
840 * Find the protox for the given "well-known" name.
841 */
842 static struct protox *
843 knownname(name)
844 const char *name;
845 {
846 struct protox **tpp, *tp;
847
848 for (tpp = protoprotox; *tpp; tpp++)
849 for (tp = *tpp; tp->pr_name; tp++)
850 if (strcmp(tp->pr_name, name) == 0)
851 return (tp);
852 return (NULL);
853 }
854
855 /*
856 * Find the protox corresponding to name.
857 */
858 static struct protox *
859 name2protox(name)
860 const char *name;
861 {
862 struct protox *tp;
863 char **alias; /* alias from p->aliases */
864 struct protoent *p;
865
866 /*
867 * Try to find the name in the list of "well-known" names. If that
868 * fails, check if name is an alias for an Internet protocol.
869 */
870 if ((tp = knownname(name)) != NULL)
871 return (tp);
872
873 setprotoent(1); /* make protocol lookup cheaper */
874 while ((p = getprotoent()) != NULL) {
875 /* assert: name not same as p->name */
876 for (alias = p->p_aliases; *alias; alias++)
877 if (strcmp(name, *alias) == 0) {
878 endprotoent();
879 return (knownname(p->p_name));
880 }
881 }
882 endprotoent();
883 return (NULL);
884 }
885
886 static void
887 usage()
888 {
889 const char *progname = getprogname();
890
891 (void)fprintf(stderr,
892 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
893 (void)fprintf(stderr,
894 " %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
895 progname);
896 (void)fprintf(stderr,
897 " %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
898 (void)fprintf(stderr,
899 " %s [-p protocol] [-M core] [-N system]\n", progname);
900 (void)fprintf(stderr,
901 " %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
902 (void)fprintf(stderr,
903 " %s [-p protocol] [-i] [-I Interface] \n", progname);
904 (void)fprintf(stderr,
905 " %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
906 (void)fprintf(stderr,
907 " %s [-s] [-B] [-I interface]\n", progname);
908 exit(1);
909 }
910