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