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