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