main.c revision 1.43 1 /* $NetBSD: main.c,v 1.43 2003/09/04 09:23:40 itojun 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.43 2003/09/04 09:23:40 itojun 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 { "" },
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_TCBTABLE, N_TCP6STAT, 1, ip6protopr,
248 tcp6_stats, NULL, tcp6_dump, "tcp6" },
249 #else
250 { N_TCBTABLE, N_TCP6STAT, 1, ip6protopr,
251 tcp_stats, NULL, tcp_dump, "tcp6" },
252 #endif
253 { N_UDBTABLE, 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 { "ppoediscinq", N_PPPOEDISCINQ },
345 { "ppoeinq", N_PPPOEINQ },
346 { "pkintrq", N_PKINTRQ },
347 { NULL, -1 },
348 };
349
350 int main __P((int, char *[]));
351 static void printproto __P((struct protox *, char *));
352 static void print_softintrq __P((void));
353 static void usage __P((void));
354 static struct protox *name2protox __P((char *));
355 static struct protox *knownname __P((char *));
356
357 kvm_t *kvmd;
358
359 int
360 main(argc, argv)
361 int argc;
362 char *argv[];
363 {
364 struct protoent *p;
365 struct protox *tp; /* for printing cblocks & stats */
366 int ch;
367 char *nlistf = NULL, *memf = NULL;
368 char buf[_POSIX2_LINE_MAX], *cp;
369 u_long pcbaddr;
370 gid_t egid = getegid();
371
372 (void)setegid(getgid());
373 tp = NULL;
374 af = AF_UNSPEC;
375 pcbaddr = 0;
376
377 while ((ch = getopt(argc, argv,
378 "Aabdf:ghI:LliM:mN:nP:p:qrsStuvw:")) != -1)
379 switch (ch) {
380 case 'A':
381 Aflag = 1;
382 break;
383 case 'a':
384 aflag = 1;
385 break;
386 case 'b':
387 bflag = 1;
388 break;
389 case 'd':
390 dflag = 1;
391 break;
392 case 'f':
393 if (strcmp(optarg, "ns") == 0)
394 af = AF_NS;
395 else if (strcmp(optarg, "inet") == 0)
396 af = AF_INET;
397 else if (strcmp(optarg, "inet6") == 0)
398 af = AF_INET6;
399 else if (strcmp(optarg, "arp") == 0)
400 af = AF_ARP;
401 else if (strcmp(optarg, "pfkey") == 0)
402 af = PF_KEY;
403 else if (strcmp(optarg, "unix") == 0
404 || strcmp(optarg, "local") == 0)
405 af = AF_LOCAL;
406 else if (strcmp(optarg, "iso") == 0)
407 af = AF_ISO;
408 else if (strcmp(optarg, "atalk") == 0)
409 af = AF_APPLETALK;
410 else
411 errx(1, "%s: unknown address family",
412 optarg);
413 break;
414 #ifndef SMALL
415 case 'g':
416 gflag = 1;
417 break;
418 #endif
419 case 'I':
420 iflag = 1;
421 interface = optarg;
422 break;
423 case 'i':
424 iflag = 1;
425 break;
426 case 'L':
427 Lflag = 1;
428 break;
429 case 'l':
430 lflag = 1;
431 break;
432 case 'M':
433 memf = optarg;
434 break;
435 case 'm':
436 mflag = 1;
437 break;
438 case 'N':
439 nlistf = optarg;
440 break;
441 case 'n':
442 numeric_addr = numeric_port = 1;
443 break;
444 case 'P':
445 errno = 0;
446 pcbaddr = strtoul(optarg, &cp, 16);
447 if (*cp != '\0' || errno == ERANGE)
448 errx(1, "invalid PCB address %s",
449 optarg);
450 Pflag = 1;
451 break;
452 case 'p':
453 if ((tp = name2protox(optarg)) == NULL)
454 errx(1, "%s: unknown or uninstrumented protocol",
455 optarg);
456 pflag = 1;
457 break;
458 case 'q':
459 qflag = 1;
460 break;
461 case 'r':
462 rflag = 1;
463 break;
464 case 's':
465 ++sflag;
466 break;
467 case 'S':
468 numeric_addr = 1;
469 break;
470 case 't':
471 tflag = 1;
472 break;
473 case 'u':
474 af = AF_LOCAL;
475 break;
476 case 'v':
477 vflag++;
478 break;
479 case 'w':
480 interval = atoi(optarg);
481 iflag = 1;
482 break;
483 case '?':
484 default:
485 usage();
486 }
487 argv += optind;
488 argc -= optind;
489
490 #define BACKWARD_COMPATIBILITY
491 #ifdef BACKWARD_COMPATIBILITY
492 if (*argv) {
493 if (isdigit(**argv)) {
494 interval = atoi(*argv);
495 if (interval <= 0)
496 usage();
497 ++argv;
498 iflag = 1;
499 }
500 if (*argv) {
501 nlistf = *argv;
502 if (*++argv)
503 memf = *argv;
504 }
505 }
506 #endif
507
508 /*
509 * Discard setgid privileges. If not the running kernel, we toss
510 * them away totally so that bad guys can't print interesting stuff
511 * from kernel memory, otherwise switch back to kmem for the
512 * duration of the kvm_openfiles() call.
513 */
514 if (nlistf != NULL || memf != NULL || Pflag)
515 (void)setgid(getgid());
516 else
517 (void)setegid(egid);
518
519 use_sysctl = (nlistf == NULL && memf == NULL);
520
521 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
522 buf)) == NULL)
523 errx(1, "%s", buf);
524
525 /* do this now anyway */
526 if (nlistf == NULL && memf == NULL)
527 (void)setgid(getgid());
528
529 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
530 if (nlistf)
531 errx(1, "%s: no namelist", nlistf);
532 else
533 errx(1, "no namelist");
534 }
535 if (mflag) {
536 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
537 nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
538 nl[N_MCLPOOL].n_value);
539 exit(0);
540 }
541 if (Pflag) {
542 if (tp == NULL) {
543 /* Default to TCP. */
544 tp = name2protox("tcp");
545 }
546 if (tp->pr_dump)
547 (*tp->pr_dump)(pcbaddr);
548 else
549 printf("%s: no PCB dump routine\n", tp->pr_name);
550 exit(0);
551 }
552 if (pflag) {
553 if (iflag && tp->pr_istats)
554 intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
555 else if (tp->pr_stats)
556 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
557 tp->pr_name);
558 else
559 printf("%s: no stats routine\n", tp->pr_name);
560 exit(0);
561 }
562 if (qflag) {
563 print_softintrq();
564 exit(0);
565 }
566 /*
567 * Keep file descriptors open to avoid overhead
568 * of open/close on each call to get* routines.
569 */
570 sethostent(1);
571 setnetent(1);
572 if (iflag) {
573 if (af != AF_UNSPEC)
574 goto protostat;
575
576 intpr(interval, nl[N_IFNET].n_value, NULL);
577 exit(0);
578 }
579 if (rflag) {
580 if (sflag)
581 rt_stats(nl[N_RTSTAT].n_value);
582 else
583 routepr(nl[N_RTREE].n_value);
584 exit(0);
585 }
586 #ifndef SMALL
587 if (gflag) {
588 if (sflag) {
589 if (af == AF_INET || af == AF_UNSPEC)
590 mrt_stats(nl[N_MRTPROTO].n_value,
591 nl[N_MRTSTAT].n_value);
592 #ifdef INET6
593 if (af == AF_INET6 || af == AF_UNSPEC)
594 mrt6_stats(nl[N_MRT6PROTO].n_value,
595 nl[N_MRT6STAT].n_value);
596 #endif
597 }
598 else {
599 if (af == AF_INET || af == AF_UNSPEC)
600 mroutepr(nl[N_MRTPROTO].n_value,
601 nl[N_MFCHASHTBL].n_value,
602 nl[N_MFCHASH].n_value,
603 nl[N_VIFTABLE].n_value);
604 #ifdef INET6
605 if (af == AF_INET6 || af == AF_UNSPEC)
606 mroute6pr(nl[N_MRT6PROTO].n_value,
607 nl[N_MF6CTABLE].n_value,
608 nl[N_MIF6TABLE].n_value);
609 #endif
610 }
611 exit(0);
612 }
613 #endif
614 protostat:
615 if (af == AF_INET || af == AF_UNSPEC) {
616 setprotoent(1);
617 setservent(1);
618 /* ugh, this is O(MN) ... why do we do this? */
619 while ((p = getprotoent()) != NULL) {
620 for (tp = protox; tp->pr_name; tp++)
621 if (strcmp(tp->pr_name, p->p_name) == 0)
622 break;
623 if (tp->pr_name == 0 || tp->pr_wanted == 0)
624 continue;
625 printproto(tp, p->p_name);
626 tp->pr_wanted = 0;
627 }
628 endprotoent();
629 for (tp = protox; tp->pr_name; tp++)
630 if (tp->pr_wanted)
631 printproto(tp, tp->pr_name);
632 }
633 #ifdef INET6
634 if (af == AF_INET6 || af == AF_UNSPEC)
635 for (tp = ip6protox; tp->pr_name; tp++)
636 printproto(tp, tp->pr_name);
637 #endif
638 if (af == AF_ARP || af == AF_UNSPEC)
639 for (tp = arpprotox; tp->pr_name; tp++)
640 printproto(tp, tp->pr_name);
641 #ifdef IPSEC
642 if (af == PF_KEY || af == AF_UNSPEC)
643 for (tp = pfkeyprotox; tp->pr_name; tp++)
644 printproto(tp, tp->pr_name);
645 #endif
646 #ifndef SMALL
647 if (af == AF_APPLETALK || af == AF_UNSPEC)
648 for (tp = atalkprotox; tp->pr_name; tp++)
649 printproto(tp, tp->pr_name);
650 if (af == AF_NS || af == AF_UNSPEC)
651 for (tp = nsprotox; tp->pr_name; tp++)
652 printproto(tp, tp->pr_name);
653 if (af == AF_ISO || af == AF_UNSPEC)
654 for (tp = isoprotox; tp->pr_name; tp++)
655 printproto(tp, tp->pr_name);
656 if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
657 unixpr(nl[N_UNIXSW].n_value);
658 #endif
659 exit(0);
660 }
661
662 /*
663 * Print out protocol statistics or control blocks (per sflag).
664 * If the interface was not specifically requested, and the symbol
665 * is not in the namelist, ignore this one.
666 */
667 static void
668 printproto(tp, name)
669 struct protox *tp;
670 char *name;
671 {
672 void (*pr) __P((u_long, char *));
673 u_long off;
674
675 if (sflag) {
676 if (iflag) {
677 if (tp->pr_istats)
678 intpr(interval, nl[N_IFNET].n_value,
679 tp->pr_istats);
680 return;
681 }
682 else {
683 pr = tp->pr_stats;
684 off = nl[tp->pr_sindex].n_value;
685 }
686 } else {
687 pr = tp->pr_cblocks;
688 off = nl[tp->pr_index].n_value;
689 }
690 if (pr != NULL && (off || af != AF_UNSPEC))
691 (*pr)(off, name);
692 }
693
694 /*
695 * Print softintrq status.
696 */
697 void
698 print_softintrq()
699 {
700 struct ifqueue intrq, *ifq = &intrq;
701 const struct softintrq *siq;
702 u_long off;
703
704 for (siq = softintrq; siq->siq_name != NULL; siq++) {
705 off = nl[siq->siq_index].n_value;
706 if (off == 0)
707 continue;
708
709 kread(off, (char *)ifq, sizeof(*ifq));
710 printf("%s:\n", siq->siq_name);
711 printf("\tqueue length: %d\n", ifq->ifq_len);
712 printf("\tmaximum queue length: %d\n", ifq->ifq_maxlen);
713 printf("\tpackets dropped: %d\n", ifq->ifq_drops);
714 }
715 }
716
717 /*
718 * Read kernel memory, return 0 on success.
719 */
720 int
721 kread(addr, buf, size)
722 u_long addr;
723 char *buf;
724 int size;
725 {
726
727 if (kvm_read(kvmd, addr, buf, size) != size) {
728 warnx("%s", kvm_geterr(kvmd));
729 return (-1);
730 }
731 return (0);
732 }
733
734 char *
735 plural(n)
736 int n;
737 {
738
739 return (n != 1 ? "s" : "");
740 }
741
742 char *
743 plurales(n)
744 int n;
745 {
746
747 return (n != 1 ? "es" : "");
748 }
749
750 int
751 get_hardticks(void)
752 {
753 int hardticks;
754
755 kread(nl[N_HARDCLOCK_TICKS].n_value, (char *)&hardticks,
756 sizeof(hardticks));
757 return (hardticks);
758 }
759
760 /*
761 * Find the protox for the given "well-known" name.
762 */
763 static struct protox *
764 knownname(name)
765 char *name;
766 {
767 struct protox **tpp, *tp;
768
769 for (tpp = protoprotox; *tpp; tpp++)
770 for (tp = *tpp; tp->pr_name; tp++)
771 if (strcmp(tp->pr_name, name) == 0)
772 return (tp);
773 return (NULL);
774 }
775
776 /*
777 * Find the protox corresponding to name.
778 */
779 static struct protox *
780 name2protox(name)
781 char *name;
782 {
783 struct protox *tp;
784 char **alias; /* alias from p->aliases */
785 struct protoent *p;
786
787 /*
788 * Try to find the name in the list of "well-known" names. If that
789 * fails, check if name is an alias for an Internet protocol.
790 */
791 if ((tp = knownname(name)) != NULL)
792 return (tp);
793
794 setprotoent(1); /* make protocol lookup cheaper */
795 while ((p = getprotoent()) != NULL) {
796 /* assert: name not same as p->name */
797 for (alias = p->p_aliases; *alias; alias++)
798 if (strcmp(name, *alias) == 0) {
799 endprotoent();
800 return (knownname(p->p_name));
801 }
802 }
803 endprotoent();
804 return (NULL);
805 }
806
807 static void
808 usage()
809 {
810 const char *progname = getprogname();
811
812 (void)fprintf(stderr,
813 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
814 (void)fprintf(stderr,
815 " %s [-bdgiLmnqrsSv] [-f address_family] [-M core] [-N system]\n",
816 progname);
817 (void)fprintf(stderr,
818 " %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
819 (void)fprintf(stderr,
820 " %s [-p protocol] [-M core] [-N system]\n", progname);
821 (void)fprintf(stderr,
822 " %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
823 (void)fprintf(stderr,
824 " %s [-p protocol] [-i] [-I Interface] \n", progname);
825 (void)fprintf(stderr,
826 " %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
827 exit(1);
828 }
829