main.c revision 1.37 1 /* $NetBSD: main.c,v 1.37 2002/07/03 01:42:59 enami 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.37 2002/07/03 01:42:59 enami 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 { "" },
206 };
207
208 struct protox {
209 u_char pr_index; /* index into nlist of cb head */
210 u_char pr_sindex; /* index into nlist of stat block */
211 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
212 void (*pr_cblocks) /* control blocks printing routine */
213 __P((u_long, char *));
214 void (*pr_stats) /* statistics printing routine */
215 __P((u_long, char *));
216 void (*pr_istats)
217 __P((char *)); /* per/if statistics printing routine */
218 void (*pr_dump) /* PCB state dump routine */
219 __P((u_long));
220 char *pr_name; /* well-known name */
221 } protox[] = {
222 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
223 tcp_stats, NULL, tcp_dump, "tcp" },
224 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
225 udp_stats, NULL, 0, "udp" },
226 { -1, N_IPSTAT, 1, 0,
227 ip_stats, NULL, 0, "ip" },
228 { -1, N_ICMPSTAT, 1, 0,
229 icmp_stats, NULL, 0, "icmp" },
230 { -1, N_IGMPSTAT, 1, 0,
231 igmp_stats, NULL, 0, "igmp" },
232 #ifdef IPSEC
233 { -1, N_IPSECSTAT, 1, 0,
234 ipsec_stats, NULL, 0, "ipsec" },
235 #endif
236 { -1, -1, 0, 0,
237 0, NULL, 0, 0 }
238 };
239
240 #ifdef INET6
241 struct protox ip6protox[] = {
242 { -1, N_IP6STAT, 1, 0,
243 ip6_stats, ip6_ifstats, 0, "ip6" },
244 { -1, N_ICMP6STAT, 1, 0,
245 icmp6_stats, icmp6_ifstats, 0, "icmp6" },
246 #ifdef TCP6
247 { N_TCB6, N_TCP6STAT, 1, ip6protopr,
248 tcp6_stats, NULL, tcp6_dump, "tcp6" },
249 #else
250 { N_TCB6, N_TCP6STAT, 1, ip6protopr,
251 tcp_stats, NULL, tcp_dump, "tcp6" },
252 #endif
253 { N_UDB6, N_UDP6STAT, 1, ip6protopr,
254 udp6_stats, NULL, 0, "udp6" },
255 #ifdef IPSEC
256 { -1, N_IPSEC6STAT, 1, 0,
257 ipsec_stats, NULL, 0, "ipsec6" },
258 #endif
259 { -1, N_PIM6STAT, 1, 0,
260 pim6_stats, NULL, 0, "pim6" },
261 { -1, N_RIP6STAT, 1, 0,
262 rip6_stats, NULL, 0, "rip6" },
263 { -1, -1, 0, 0,
264 0, NULL, 0, 0 }
265 };
266 #endif
267
268 struct protox arpprotox[] = {
269 { -1, N_ARPSTAT, 1, 0,
270 arp_stats, NULL, 0, "arp" },
271 { -1, -1, 0, 0,
272 0, NULL, 0, 0 }
273 };
274
275 #ifdef IPSEC
276 struct protox pfkeyprotox[] = {
277 { -1, N_PFKEYSTAT, 1, 0,
278 pfkey_stats, NULL, 0, "pfkey" },
279 { -1, -1, 0, 0,
280 0, NULL, 0, 0 }
281 };
282 #endif
283
284 #ifndef SMALL
285 struct protox atalkprotox[] = {
286 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
287 ddp_stats, NULL, 0, "ddp" },
288 { -1, -1, 0, 0,
289 0, NULL, 0 }
290 };
291
292 struct protox nsprotox[] = {
293 { N_IDP, N_IDPSTAT, 1, nsprotopr,
294 idp_stats, NULL, 0, "idp" },
295 { N_IDP, N_SPPSTAT, 1, nsprotopr,
296 spp_stats, NULL, 0, "spp" },
297 { -1, N_NSERR, 1, 0,
298 nserr_stats, NULL, 0, "ns_err" },
299 { -1, -1, 0, 0,
300 0, NULL, 0 }
301 };
302
303 struct protox isoprotox[] = {
304 { ISO_TP, N_TPSTAT, 1, iso_protopr,
305 tp_stats, NULL, 0, "tp" },
306 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
307 cltp_stats, NULL, 0, "cltp" },
308 { -1, N_CLNPSTAT, 1, 0,
309 clnp_stats, NULL, 0, "clnp"},
310 { -1, N_ESISSTAT, 1, 0,
311 esis_stats, NULL, 0, "esis"},
312 { -1, -1, 0, 0,
313 0, NULL, 0, 0 }
314 };
315 #endif
316
317 struct protox *protoprotox[] = { protox,
318 #ifdef INET6
319 ip6protox,
320 #endif
321 arpprotox,
322 #ifdef IPSEC
323 pfkeyprotox,
324 #endif
325 #ifndef SMALL
326 atalkprotox, nsprotox, isoprotox,
327 #endif
328 NULL };
329
330 const struct softintrq {
331 const char *siq_name;
332 int siq_index;
333 } softintrq[] = {
334 { "arpintrq", N_ARPINTRQ },
335 { "ipintrq", N_IPINTRQ },
336 { "ip6intrq", N_IP6INTRQ },
337 { "atintrq1", N_ATINTRQ1 },
338 { "atintrq2", N_ATINTRQ2 },
339 { "nsintrq", N_NSINTRQ },
340 { "clnlintrq", N_CLNLINTRQ },
341 { "llcintrq", N_LLCINTRQ },
342 { "hdintrq", N_HDINTRQ },
343 { "natmintrq", N_NATMINTRQ },
344 { NULL, -1 },
345 };
346
347 int main __P((int, char *[]));
348 static void printproto __P((struct protox *, char *));
349 static void print_softintrq __P((void));
350 static void usage __P((void));
351 static struct protox *name2protox __P((char *));
352 static struct protox *knownname __P((char *));
353
354 kvm_t *kvmd;
355
356 int
357 main(argc, argv)
358 int argc;
359 char *argv[];
360 {
361 struct protoent *p;
362 struct protox *tp; /* for printing cblocks & stats */
363 int ch;
364 char *nlistf = NULL, *memf = NULL;
365 char buf[_POSIX2_LINE_MAX], *cp;
366 u_long pcbaddr;
367 gid_t egid = getegid();
368
369 (void)setegid(getgid());
370 tp = NULL;
371 af = AF_UNSPEC;
372 pcbaddr = 0;
373
374 while ((ch = getopt(argc, argv,
375 "Aabdf:ghI:LliM:mN:nP:p:qrsStuvw:")) != -1)
376 switch (ch) {
377 case 'A':
378 Aflag = 1;
379 break;
380 case 'a':
381 aflag = 1;
382 break;
383 case 'b':
384 bflag = 1;
385 break;
386 case 'd':
387 dflag = 1;
388 break;
389 case 'f':
390 if (strcmp(optarg, "ns") == 0)
391 af = AF_NS;
392 else if (strcmp(optarg, "inet") == 0)
393 af = AF_INET;
394 else if (strcmp(optarg, "inet6") == 0)
395 af = AF_INET6;
396 else if (strcmp(optarg, "arp") == 0)
397 af = AF_ARP;
398 else if (strcmp(optarg, "pfkey") == 0)
399 af = PF_KEY;
400 else if (strcmp(optarg, "unix") == 0
401 || strcmp(optarg, "local") == 0)
402 af = AF_LOCAL;
403 else if (strcmp(optarg, "iso") == 0)
404 af = AF_ISO;
405 else if (strcmp(optarg, "atalk") == 0)
406 af = AF_APPLETALK;
407 else
408 errx(1, "%s: unknown address family",
409 optarg);
410 break;
411 #ifndef SMALL
412 case 'g':
413 gflag = 1;
414 break;
415 #endif
416 case 'I':
417 iflag = 1;
418 interface = optarg;
419 break;
420 case 'i':
421 iflag = 1;
422 break;
423 case 'L':
424 Lflag = 1;
425 break;
426 case 'l':
427 lflag = 1;
428 break;
429 case 'M':
430 memf = optarg;
431 break;
432 case 'm':
433 mflag = 1;
434 break;
435 case 'N':
436 nlistf = optarg;
437 break;
438 case 'n':
439 numeric_addr = numeric_port = 1;
440 break;
441 case 'P':
442 pcbaddr = strtoul(optarg, &cp, 16);
443 if (*cp != '\0' || errno == ERANGE)
444 errx(1, "invalid PCB address %s",
445 optarg);
446 Pflag = 1;
447 break;
448 case 'p':
449 if ((tp = name2protox(optarg)) == NULL)
450 errx(1, "%s: unknown or uninstrumented protocol",
451 optarg);
452 pflag = 1;
453 break;
454 case 'q':
455 qflag = 1;
456 break;
457 case 'r':
458 rflag = 1;
459 break;
460 case 's':
461 ++sflag;
462 break;
463 case 'S':
464 numeric_addr = 1;
465 break;
466 case 't':
467 tflag = 1;
468 break;
469 case 'u':
470 af = AF_LOCAL;
471 break;
472 case 'v':
473 vflag = 1;
474 break;
475 case 'w':
476 interval = atoi(optarg);
477 iflag = 1;
478 break;
479 case '?':
480 default:
481 usage();
482 }
483 argv += optind;
484 argc -= optind;
485
486 #define BACKWARD_COMPATIBILITY
487 #ifdef BACKWARD_COMPATIBILITY
488 if (*argv) {
489 if (isdigit(**argv)) {
490 interval = atoi(*argv);
491 if (interval <= 0)
492 usage();
493 ++argv;
494 iflag = 1;
495 }
496 if (*argv) {
497 nlistf = *argv;
498 if (*++argv)
499 memf = *argv;
500 }
501 }
502 #endif
503
504 /*
505 * Discard setgid privileges. If not the running kernel, we toss
506 * them away totally so that bad guys can't print interesting stuff
507 * from kernel memory, otherwise switch back to kmem for the
508 * duration of the kvm_openfiles() call.
509 */
510 if (nlistf != NULL || memf != NULL || Pflag)
511 (void)setgid(getgid());
512 else
513 (void)setegid(egid);
514
515 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
516 buf)) == NULL)
517 errx(1, "%s", buf);
518
519 /* do this now anyway */
520 if (nlistf == NULL && memf == NULL)
521 (void)setgid(getgid());
522
523 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
524 if (nlistf)
525 errx(1, "%s: no namelist", nlistf);
526 else
527 errx(1, "no namelist");
528 }
529 if (mflag) {
530 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
531 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
532 nl[N_MCLPOOL].n_value);
533 exit(0);
534 }
535 if (Pflag) {
536 if (tp == NULL) {
537 /* Default to TCP. */
538 tp = name2protox("tcp");
539 }
540 if (tp->pr_dump)
541 (*tp->pr_dump)(pcbaddr);
542 else
543 printf("%s: no PCB dump routine\n", tp->pr_name);
544 exit(0);
545 }
546 if (pflag) {
547 if (iflag && tp->pr_istats)
548 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
549 else if (tp->pr_stats)
550 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
551 tp->pr_name);
552 else
553 printf("%s: no stats routine\n", tp->pr_name);
554 exit(0);
555 }
556 if (qflag) {
557 print_softintrq();
558 exit(0);
559 }
560 /*
561 * Keep file descriptors open to avoid overhead
562 * of open/close on each call to get* routines.
563 */
564 sethostent(1);
565 setnetent(1);
566 if (iflag) {
567 if (af != AF_UNSPEC)
568 goto protostat;
569
570 intpr(interval, nl[N_IFNET].n_value, NULL);
571 exit(0);
572 }
573 if (rflag) {
574 if (sflag)
575 rt_stats(nl[N_RTSTAT].n_value);
576 else
577 routepr(nl[N_RTREE].n_value);
578 exit(0);
579 }
580 #ifndef SMALL
581 if (gflag) {
582 if (sflag) {
583 if (af == AF_INET || af == AF_UNSPEC)
584 mrt_stats(nl[N_MRTPROTO].n_value,
585 nl[N_MRTSTAT].n_value);
586 #ifdef INET6
587 if (af == AF_INET6 || af == AF_UNSPEC)
588 mrt6_stats(nl[N_MRT6PROTO].n_value,
589 nl[N_MRT6STAT].n_value);
590 #endif
591 }
592 else {
593 if (af == AF_INET || af == AF_UNSPEC)
594 mroutepr(nl[N_MRTPROTO].n_value,
595 nl[N_MFCHASHTBL].n_value,
596 nl[N_MFCHASH].n_value,
597 nl[N_VIFTABLE].n_value);
598 #ifdef INET6
599 if (af == AF_INET6 || af == AF_UNSPEC)
600 mroute6pr(nl[N_MRT6PROTO].n_value,
601 nl[N_MF6CTABLE].n_value,
602 nl[N_MIF6TABLE].n_value);
603 #endif
604 }
605 exit(0);
606 }
607 #endif
608 protostat:
609 if (af == AF_INET || af == AF_UNSPEC) {
610 setprotoent(1);
611 setservent(1);
612 /* ugh, this is O(MN) ... why do we do this? */
613 while ((p = getprotoent()) != NULL) {
614 for (tp = protox; tp->pr_name; tp++)
615 if (strcmp(tp->pr_name, p->p_name) == 0)
616 break;
617 if (tp->pr_name == 0 || tp->pr_wanted == 0)
618 continue;
619 printproto(tp, p->p_name);
620 tp->pr_wanted = 0;
621 }
622 endprotoent();
623 for (tp = protox; tp->pr_name; tp++)
624 if (tp->pr_wanted)
625 printproto(tp, tp->pr_name);
626 }
627 #ifdef INET6
628 if (af == AF_INET6 || af == AF_UNSPEC)
629 for (tp = ip6protox; tp->pr_name; tp++)
630 printproto(tp, tp->pr_name);
631 #endif
632 if (af == AF_ARP || af == AF_UNSPEC)
633 for (tp = arpprotox; tp->pr_name; tp++)
634 printproto(tp, tp->pr_name);
635 #ifdef IPSEC
636 if (af == PF_KEY || af == AF_UNSPEC)
637 for (tp = pfkeyprotox; tp->pr_name; tp++)
638 printproto(tp, tp->pr_name);
639 #endif
640 #ifndef SMALL
641 if (af == AF_APPLETALK || af == AF_UNSPEC)
642 for (tp = atalkprotox; tp->pr_name; tp++)
643 printproto(tp, tp->pr_name);
644 if (af == AF_NS || af == AF_UNSPEC)
645 for (tp = nsprotox; tp->pr_name; tp++)
646 printproto(tp, tp->pr_name);
647 if (af == AF_ISO || af == AF_UNSPEC)
648 for (tp = isoprotox; tp->pr_name; tp++)
649 printproto(tp, tp->pr_name);
650 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
651 unixpr(nl[N_UNIXSW].n_value);
652 #endif
653 exit(0);
654 }
655
656 /*
657 * Print out protocol statistics or control blocks (per sflag).
658 * If the interface was not specifically requested, and the symbol
659 * is not in the namelist, ignore this one.
660 */
661 static void
662 printproto(tp, name)
663 struct protox *tp;
664 char *name;
665 {
666 void (*pr) __P((u_long, char *));
667 u_long off;
668
669 if (sflag) {
670 if (iflag) {
671 if (tp->pr_istats)
672 intpr(interval, nl[N_IFNET].n_value,
673 tp->pr_istats);
674 return;
675 }
676 else {
677 pr = tp->pr_stats;
678 off = nl[tp->pr_sindex].n_value;
679 }
680 } else {
681 pr = tp->pr_cblocks;
682 off = nl[tp->pr_index].n_value;
683 }
684 if (pr != NULL && (off || af != AF_UNSPEC))
685 (*pr)(off, name);
686 }
687
688 /*
689 * Print softintrq status.
690 */
691 void
692 print_softintrq()
693 {
694 struct ifqueue intrq, *ifq = &intrq;
695 const struct softintrq *siq;
696 u_long off;
697
698 for (siq = softintrq; siq->siq_name != NULL; siq++) {
699 off = nl[siq->siq_index].n_value;
700 if (off == 0)
701 continue;
702
703 kread(off, (char *)ifq, sizeof(*ifq));
704 printf("%s:\n", siq->siq_name);
705 printf("\tqueue length: %d\n", ifq->ifq_len);
706 printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
707 printf("\tpackets dropped: %d\n", ifq->ifq_drops);
708 }
709 }
710
711 /*
712 * Read kernel memory, return 0 on success.
713 */
714 int
715 kread(addr, buf, size)
716 u_long addr;
717 char *buf;
718 int size;
719 {
720
721 if (kvm_read(kvmd, addr, buf, size) != size) {
722 warnx("%s", kvm_geterr(kvmd));
723 return (-1);
724 }
725 return (0);
726 }
727
728 char *
729 plural(n)
730 int n;
731 {
732
733 return (n != 1 ? "s" : "");
734 }
735
736 char *
737 plurales(n)
738 int n;
739 {
740
741 return (n != 1 ? "es" : "");
742 }
743
744 /*
745 * Find the protox for the given "well-known" name.
746 */
747 static struct protox *
748 knownname(name)
749 char *name;
750 {
751 struct protox **tpp, *tp;
752
753 for (tpp = protoprotox; *tpp; tpp++)
754 for (tp = *tpp; tp->pr_name; tp++)
755 if (strcmp(tp->pr_name, name) == 0)
756 return (tp);
757 return (NULL);
758 }
759
760 /*
761 * Find the protox corresponding to name.
762 */
763 static struct protox *
764 name2protox(name)
765 char *name;
766 {
767 struct protox *tp;
768 char **alias; /* alias from p->aliases */
769 struct protoent *p;
770
771 /*
772 * Try to find the name in the list of "well-known" names. If that
773 * fails, check if name is an alias for an Internet protocol.
774 */
775 if ((tp = knownname(name)) != NULL)
776 return (tp);
777
778 setprotoent(1); /* make protocol lookup cheaper */
779 while ((p = getprotoent()) != NULL) {
780 /* assert: name not same as p->name */
781 for (alias = p->p_aliases; *alias; alias++)
782 if (strcmp(name, *alias) == 0) {
783 endprotoent();
784 return (knownname(p->p_name));
785 }
786 }
787 endprotoent();
788 return (NULL);
789 }
790
791 static void
792 usage()
793 {
794 const char *progname = getprogname();
795
796 (void)fprintf(stderr,
797 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
798 (void)fprintf(stderr,
799 " %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
800 progname);
801 (void)fprintf(stderr,
802 " %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
803 (void)fprintf(stderr,
804 " %s [-p protocol] [-M core] [-N system]\n", progname);
805 (void)fprintf(stderr,
806 " %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
807 (void)fprintf(stderr,
808 " %s [-p protocol] [-i] [-I Interface] \n", progname);
809 (void)fprintf(stderr,
810 " %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
811 exit(1);
812 }
813