main.c revision 1.50 1 /* $NetBSD: main.c,v 1.50 2005/09/02 22:23:13 rpaulo 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.50 2005/09/02 22:23:13 rpaulo 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 if (Bflag) {
537 if (sflag)
538 bpf_stats();
539 else
540 bpf_dump(interface);
541 exit(0);
542 }
543
544 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
545 if (nlistf)
546 errx(1, "%s: no namelist", nlistf);
547 else
548 errx(1, "no namelist");
549 }
550 if (mflag) {
551 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
552 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
553 nl[N_MCLPOOL].n_value);
554 exit(0);
555 }
556 if (Pflag) {
557 if (tp == NULL) {
558 /* Default to TCP. */
559 tp = name2protox("tcp");
560 }
561 if (tp->pr_dump)
562 (*tp->pr_dump)(pcbaddr);
563 else
564 printf("%s: no PCB dump routine\n", tp->pr_name);
565 exit(0);
566 }
567 if (pflag) {
568 if (iflag && tp->pr_istats)
569 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
570 else if (tp->pr_stats)
571 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
572 tp->pr_name);
573 else
574 printf("%s: no stats routine\n", tp->pr_name);
575 exit(0);
576 }
577 if (qflag) {
578 print_softintrq();
579 exit(0);
580 }
581 /*
582 * Keep file descriptors open to avoid overhead
583 * of open/close on each call to get* routines.
584 */
585 sethostent(1);
586 setnetent(1);
587 if (iflag) {
588 if (af != AF_UNSPEC)
589 goto protostat;
590
591 intpr(interval, nl[N_IFNET].n_value, NULL);
592 exit(0);
593 }
594 if (rflag) {
595 if (sflag)
596 rt_stats(nl[N_RTSTAT].n_value);
597 else
598 routepr(nl[N_RTREE].n_value);
599 exit(0);
600 }
601 #ifndef SMALL
602 if (gflag) {
603 if (sflag) {
604 if (af == AF_INET || af == AF_UNSPEC)
605 mrt_stats(nl[N_MRTPROTO].n_value,
606 nl[N_MRTSTAT].n_value);
607 #ifdef INET6
608 if (af == AF_INET6 || af == AF_UNSPEC)
609 mrt6_stats(nl[N_MRT6PROTO].n_value,
610 nl[N_MRT6STAT].n_value);
611 #endif
612 }
613 else {
614 if (af == AF_INET || af == AF_UNSPEC)
615 mroutepr(nl[N_MRTPROTO].n_value,
616 nl[N_MFCHASHTBL].n_value,
617 nl[N_MFCHASH].n_value,
618 nl[N_VIFTABLE].n_value);
619 #ifdef INET6
620 if (af == AF_INET6 || af == AF_UNSPEC)
621 mroute6pr(nl[N_MRT6PROTO].n_value,
622 nl[N_MF6CTABLE].n_value,
623 nl[N_MIF6TABLE].n_value);
624 #endif
625 }
626 exit(0);
627 }
628 #endif
629 protostat:
630 if (af == AF_INET || af == AF_UNSPEC) {
631 setprotoent(1);
632 setservent(1);
633 /* ugh, this is O(MN) ... why do we do this? */
634 while ((p = getprotoent()) != NULL) {
635 for (tp = protox; tp->pr_name; tp++)
636 if (strcmp(tp->pr_name, p->p_name) == 0)
637 break;
638 if (tp->pr_name == 0 || tp->pr_wanted == 0)
639 continue;
640 printproto(tp, p->p_name);
641 tp->pr_wanted = 0;
642 }
643 endprotoent();
644 for (tp = protox; tp->pr_name; tp++)
645 if (tp->pr_wanted)
646 printproto(tp, tp->pr_name);
647 }
648 #ifdef INET6
649 if (af == AF_INET6 || af == AF_UNSPEC)
650 for (tp = ip6protox; tp->pr_name; tp++)
651 printproto(tp, tp->pr_name);
652 #endif
653 if (af == AF_ARP || af == AF_UNSPEC)
654 for (tp = arpprotox; tp->pr_name; tp++)
655 printproto(tp, tp->pr_name);
656 #ifdef IPSEC
657 if (af == PF_KEY || af == AF_UNSPEC)
658 for (tp = pfkeyprotox; tp->pr_name; tp++)
659 printproto(tp, tp->pr_name);
660 #endif
661 #ifndef SMALL
662 if (af == AF_APPLETALK || af == AF_UNSPEC)
663 for (tp = atalkprotox; tp->pr_name; tp++)
664 printproto(tp, tp->pr_name);
665 if (af == AF_NS || af == AF_UNSPEC)
666 for (tp = nsprotox; tp->pr_name; tp++)
667 printproto(tp, tp->pr_name);
668 if (af == AF_ISO || af == AF_UNSPEC)
669 for (tp = isoprotox; tp->pr_name; tp++)
670 printproto(tp, tp->pr_name);
671 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
672 unixpr(nl[N_UNIXSW].n_value);
673 #endif
674 exit(0);
675 }
676
677 /*
678 * Print out protocol statistics or control blocks (per sflag).
679 * If the interface was not specifically requested, and the symbol
680 * is not in the namelist, ignore this one.
681 */
682 static void
683 printproto(tp, name)
684 struct protox *tp;
685 char *name;
686 {
687 void (*pr) __P((u_long, char *));
688 u_long off;
689
690 if (sflag) {
691 if (iflag) {
692 if (tp->pr_istats)
693 intpr(interval, nl[N_IFNET].n_value,
694 tp->pr_istats);
695 return;
696 }
697 else {
698 pr = tp->pr_stats;
699 off = nl[tp->pr_sindex].n_value;
700 }
701 } else {
702 pr = tp->pr_cblocks;
703 off = nl[tp->pr_index].n_value;
704 }
705 if (pr != NULL && (off || af != AF_UNSPEC))
706 (*pr)(off, name);
707 }
708
709 /*
710 * Print softintrq status.
711 */
712 void
713 print_softintrq()
714 {
715 struct ifqueue intrq, *ifq = &intrq;
716 const struct softintrq *siq;
717 u_long off;
718
719 for (siq = softintrq; siq->siq_name != NULL; siq++) {
720 off = nl[siq->siq_index].n_value;
721 if (off == 0)
722 continue;
723
724 kread(off, (char *)ifq, sizeof(*ifq));
725 printf("%s:\n", siq->siq_name);
726 printf("\tqueue length: %d\n", ifq->ifq_len);
727 printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
728 printf("\tpackets dropped: %d\n", ifq->ifq_drops);
729 }
730 }
731
732 /*
733 * Read kernel memory, return 0 on success.
734 */
735 int
736 kread(addr, buf, size)
737 u_long addr;
738 char *buf;
739 int size;
740 {
741
742 if (kvm_read(kvmd, addr, buf, size) != size) {
743 warnx("%s", kvm_geterr(kvmd));
744 return (-1);
745 }
746 return (0);
747 }
748
749 char *
750 plural(n)
751 int n;
752 {
753
754 return (n != 1 ? "s" : "");
755 }
756
757 char *
758 plurales(n)
759 int n;
760 {
761
762 return (n != 1 ? "es" : "");
763 }
764
765 int
766 get_hardticks(void)
767 {
768 int hardticks;
769
770 kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks,
771 sizeof(hardticks));
772 return (hardticks);
773 }
774
775 /*
776 * Find the protox for the given "well-known" name.
777 */
778 static struct protox *
779 knownname(name)
780 char *name;
781 {
782 struct protox **tpp, *tp;
783
784 for (tpp = protoprotox; *tpp; tpp++)
785 for (tp = *tpp; tp->pr_name; tp++)
786 if (strcmp(tp->pr_name, name) == 0)
787 return (tp);
788 return (NULL);
789 }
790
791 /*
792 * Find the protox corresponding to name.
793 */
794 static struct protox *
795 name2protox(name)
796 char *name;
797 {
798 struct protox *tp;
799 char **alias; /* alias from p->aliases */
800 struct protoent *p;
801
802 /*
803 * Try to find the name in the list of "well-known" names. If that
804 * fails, check if name is an alias for an Internet protocol.
805 */
806 if ((tp = knownname(name)) != NULL)
807 return (tp);
808
809 setprotoent(1); /* make protocol lookup cheaper */
810 while ((p = getprotoent()) != NULL) {
811 /* assert: name not same as p->name */
812 for (alias = p->p_aliases; *alias; alias++)
813 if (strcmp(name, *alias) == 0) {
814 endprotoent();
815 return (knownname(p->p_name));
816 }
817 }
818 endprotoent();
819 return (NULL);
820 }
821
822 static void
823 usage()
824 {
825 const char *progname = getprogname();
826
827 (void)fprintf(stderr,
828 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
829 (void)fprintf(stderr,
830 " %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
831 progname);
832 (void)fprintf(stderr,
833 " %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
834 (void)fprintf(stderr,
835 " %s [-p protocol] [-M core] [-N system]\n", progname);
836 (void)fprintf(stderr,
837 " %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
838 (void)fprintf(stderr,
839 " %s [-p protocol] [-i] [-I Interface] \n", progname);
840 (void)fprintf(stderr,
841 " %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
842 (void)fprintf(stderr,
843 " %s [-s] [-B] [-I interface]\n", progname);
844 exit(1);
845 }
846