main.c revision 1.12.2.1 1 /* $NetBSD: main.c,v 1.12.2.1 1999/01/19 07:32:39 cgd 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.12.2.1 1999/01/19 07:32:39 cgd 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 <netinet/in.h>
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <kvm.h>
60 #include <limits.h>
61 #include <netdb.h>
62 #include <nlist.h>
63 #include <paths.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include "netstat.h"
69
70 struct nlist nl[] = {
71 #define N_MBSTAT 0
72 { "_mbstat" },
73 #define N_IPSTAT 1
74 { "_ipstat" },
75 #define N_TCBTABLE 2
76 { "_tcbtable" },
77 #define N_TCPSTAT 3
78 { "_tcpstat" },
79 #define N_UDBTABLE 4
80 { "_udbtable" },
81 #define N_UDPSTAT 5
82 { "_udpstat" },
83 #define N_IFNET 6
84 { "_ifnet" },
85 #define N_IMP 7
86 { "_imp_softc" },
87 #define N_ICMPSTAT 8
88 { "_icmpstat" },
89 #define N_RTSTAT 9
90 { "_rtstat" },
91 #define N_UNIXSW 10
92 { "_unixsw" },
93 #define N_IDP 11
94 { "_nspcb"},
95 #define N_IDPSTAT 12
96 { "_idpstat"},
97 #define N_SPPSTAT 13
98 { "_spp_istat"},
99 #define N_NSERR 14
100 { "_ns_errstat"},
101 #define N_CLNPSTAT 15
102 { "_clnp_stat"},
103 #define IN_NOTUSED 16
104 { "_tp_inpcb" },
105 #define ISO_TP 17
106 { "_tp_refinfo" },
107 #define N_TPSTAT 18
108 { "_tp_stat" },
109 #define N_ESISSTAT 19
110 { "_esis_stat"},
111 #define N_NIMP 20
112 { "_nimp"},
113 #define N_RTREE 21
114 { "_rt_tables"},
115 #define N_CLTP 22
116 { "_cltb"},
117 #define N_CLTPSTAT 23
118 { "_cltpstat"},
119 #define N_NFILE 24
120 { "_nfile" },
121 #define N_FILE 25
122 { "_file" },
123 #define N_IGMPSTAT 26
124 { "_igmpstat" },
125 #define N_MRTPROTO 27
126 { "_ip_mrtproto" },
127 #define N_MRTSTAT 28
128 { "_mrtstat" },
129 #define N_MFCHASHTBL 29
130 { "_mfchashtbl" },
131 #define N_MFCHASH 30
132 { "_mfchash" },
133 #define N_VIFTABLE 31
134 { "_viftable" },
135 #define N_MSIZE 32
136 { "_msize" },
137 #define N_MCLBYTES 33
138 { "_mclbytes" },
139 #define N_DDPSTAT 34
140 { "_ddpstat"},
141 #define N_DDPCB 35
142 { "_ddpcb"},
143 { "" },
144 };
145
146 struct protox {
147 u_char pr_index; /* index into nlist of cb head */
148 u_char pr_sindex; /* index into nlist of stat block */
149 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
150 void (*pr_cblocks) /* control blocks printing routine */
151 __P((u_long, char *));
152 void (*pr_stats) /* statistics printing routine */
153 __P((u_long, char *));
154 char *pr_name; /* well-known name */
155 } protox[] = {
156 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
157 tcp_stats, "tcp" },
158 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
159 udp_stats, "udp" },
160 { -1, N_IPSTAT, 1, 0,
161 ip_stats, "ip" },
162 { -1, N_ICMPSTAT, 1, 0,
163 icmp_stats, "icmp" },
164 { -1, N_IGMPSTAT, 1, 0,
165 igmp_stats, "igmp" },
166 { -1, -1, 0, 0,
167 0, 0 }
168 };
169
170 #ifndef SMALL
171 struct protox atalkprotox[] = {
172 { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
173 ddp_stats, "ddp" },
174 { -1, -1, 0, 0,
175 0, 0 }
176 };
177
178 struct protox nsprotox[] = {
179 { N_IDP, N_IDPSTAT, 1, nsprotopr,
180 idp_stats, "idp" },
181 { N_IDP, N_SPPSTAT, 1, nsprotopr,
182 spp_stats, "spp" },
183 { -1, N_NSERR, 1, 0,
184 nserr_stats, "ns_err" },
185 { -1, -1, 0, 0,
186 0, 0 }
187 };
188
189 struct protox isoprotox[] = {
190 { ISO_TP, N_TPSTAT, 1, iso_protopr,
191 tp_stats, "tp" },
192 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
193 cltp_stats, "cltp" },
194 { -1, N_CLNPSTAT, 1, 0,
195 clnp_stats, "clnp"},
196 { -1, N_ESISSTAT, 1, 0,
197 esis_stats, "esis"},
198 { -1, -1, 0, 0,
199 0, 0 }
200 };
201 #endif
202
203 struct protox *protoprotox[] = { protox,
204 #ifndef SMALL
205 atalkprotox, nsprotox, isoprotox,
206 #endif
207 NULL };
208
209 int main __P((int, char *[]));
210 static void printproto __P((struct protox *, char *));
211 static void usage __P((void));
212 static struct protox *name2protox __P((char *));
213 static struct protox *knownname __P((char *));
214
215
216 kvm_t *kvmd;
217
218 int
219 main(argc, argv)
220 int argc;
221 char *argv[];
222 {
223 extern char *optarg;
224 extern int optind;
225 struct protoent *p;
226 struct protox *tp; /* for printing cblocks & stats */
227 int ch;
228 char *nlistf = NULL, *memf = NULL;
229 char buf[_POSIX2_LINE_MAX];
230
231 tp = NULL;
232 af = AF_UNSPEC;
233
234 while ((ch = getopt(argc, argv, "Aadf:ghI:iM:mN:np:rstuw:")) != -1)
235 switch(ch) {
236 case 'A':
237 Aflag = 1;
238 break;
239 case 'a':
240 aflag = 1;
241 break;
242 case 'd':
243 dflag = 1;
244 break;
245 case 'f':
246 if (strcmp(optarg, "ns") == 0)
247 af = AF_NS;
248 else if (strcmp(optarg, "inet") == 0)
249 af = AF_INET;
250 else if (strcmp(optarg, "unix") == 0)
251 af = AF_UNIX;
252 else if (strcmp(optarg, "iso") == 0)
253 af = AF_ISO;
254 else if (strcmp(optarg, "atalk") == 0)
255 af = AF_APPLETALK;
256 else {
257 (void)fprintf(stderr,
258 "%s: %s: unknown address family\n",
259 __progname, optarg);
260 exit(1);
261 }
262 break;
263 #ifndef SMALL
264 case 'g':
265 gflag = 1;
266 break;
267 #endif
268 case 'I':
269 iflag = 1;
270 interface = optarg;
271 break;
272 case 'i':
273 iflag = 1;
274 break;
275 case 'M':
276 memf = optarg;
277 break;
278 case 'm':
279 mflag = 1;
280 break;
281 case 'N':
282 nlistf = optarg;
283 break;
284 case 'n':
285 nflag = 1;
286 break;
287 case 'p':
288 if ((tp = name2protox(optarg)) == NULL) {
289 (void)fprintf(stderr,
290 "%s: %s: unknown or uninstrumented protocol\n",
291 __progname, optarg);
292 exit(1);
293 }
294 pflag = 1;
295 break;
296 case 'r':
297 rflag = 1;
298 break;
299 case 's':
300 ++sflag;
301 break;
302 case 't':
303 tflag = 1;
304 break;
305 case 'u':
306 af = AF_UNIX;
307 break;
308 case 'w':
309 interval = atoi(optarg);
310 iflag = 1;
311 break;
312 case '?':
313 default:
314 usage();
315 }
316 argv += optind;
317 argc -= optind;
318
319 #define BACKWARD_COMPATIBILITY
320 #ifdef BACKWARD_COMPATIBILITY
321 if (*argv) {
322 if (isdigit(**argv)) {
323 interval = atoi(*argv);
324 if (interval <= 0)
325 usage();
326 ++argv;
327 iflag = 1;
328 }
329 if (*argv) {
330 nlistf = *argv;
331 if (*++argv)
332 memf = *argv;
333 }
334 }
335 #endif
336
337 /*
338 * Discard setgid privileges if not the running kernel so that bad
339 * guys can't print interesting stuff from kernel memory.
340 */
341 if (nlistf != NULL || memf != NULL)
342 setgid(getgid());
343
344 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
345 buf)) == NULL) {
346 fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
347 exit(1);
348 }
349 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
350 if (nlistf)
351 fprintf(stderr, "%s: %s: no namelist\n", __progname,
352 nlistf);
353 else
354 fprintf(stderr, "%s: no namelist\n", __progname);
355 exit(1);
356 }
357 if (mflag) {
358 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
359 nl[N_MCLBYTES].n_value);
360 exit(0);
361 }
362 if (pflag) {
363 if (tp->pr_stats)
364 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
365 tp->pr_name);
366 else
367 printf("%s: no stats routine\n", tp->pr_name);
368 exit(0);
369 }
370 /*
371 * Keep file descriptors open to avoid overhead
372 * of open/close on each call to get* routines.
373 */
374 sethostent(1);
375 setnetent(1);
376 if (iflag) {
377 intpr(interval, nl[N_IFNET].n_value);
378 exit(0);
379 }
380 if (rflag) {
381 if (sflag)
382 rt_stats(nl[N_RTSTAT].n_value);
383 else
384 routepr(nl[N_RTREE].n_value);
385 exit(0);
386 }
387 #ifndef SMALL
388 if (gflag) {
389 if (sflag)
390 mrt_stats(nl[N_MRTPROTO].n_value,
391 nl[N_MRTSTAT].n_value);
392 else
393 mroutepr(nl[N_MRTPROTO].n_value,
394 nl[N_MFCHASHTBL].n_value,
395 nl[N_MFCHASH].n_value,
396 nl[N_VIFTABLE].n_value);
397 exit(0);
398 }
399 #endif
400 if (af == AF_INET || af == AF_UNSPEC) {
401 setprotoent(1);
402 setservent(1);
403 /* ugh, this is O(MN) ... why do we do this? */
404 while ((p = getprotoent()) != NULL) {
405 for (tp = protox; tp->pr_name; tp++)
406 if (strcmp(tp->pr_name, p->p_name) == 0)
407 break;
408 if (tp->pr_name == 0 || tp->pr_wanted == 0)
409 continue;
410 printproto(tp, p->p_name);
411 }
412 endprotoent();
413 }
414 #ifndef SMALL
415 if (af == AF_APPLETALK || af == AF_UNSPEC)
416 for (tp = atalkprotox; tp->pr_name; tp++)
417 printproto(tp, tp->pr_name);
418 if (af == AF_NS || af == AF_UNSPEC)
419 for (tp = nsprotox; tp->pr_name; tp++)
420 printproto(tp, tp->pr_name);
421 if (af == AF_ISO || af == AF_UNSPEC)
422 for (tp = isoprotox; tp->pr_name; tp++)
423 printproto(tp, tp->pr_name);
424 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
425 unixpr(nl[N_UNIXSW].n_value);
426 #endif
427 exit(0);
428 }
429
430 /*
431 * Print out protocol statistics or control blocks (per sflag).
432 * If the interface was not specifically requested, and the symbol
433 * is not in the namelist, ignore this one.
434 */
435 static void
436 printproto(tp, name)
437 struct protox *tp;
438 char *name;
439 {
440 void (*pr) __P((u_long, char *));
441 u_long off;
442
443 if (sflag) {
444 pr = tp->pr_stats;
445 off = nl[tp->pr_sindex].n_value;
446 } else {
447 pr = tp->pr_cblocks;
448 off = nl[tp->pr_index].n_value;
449 }
450 if (pr != NULL && (off || af != AF_UNSPEC))
451 (*pr)(off, name);
452 }
453
454 /*
455 * Read kernel memory, return 0 on success.
456 */
457 int
458 kread(addr, buf, size)
459 u_long addr;
460 char *buf;
461 int size;
462 {
463
464 if (kvm_read(kvmd, addr, buf, size) != size) {
465 (void)fprintf(stderr, "%s: %s\n", __progname,
466 kvm_geterr(kvmd));
467 return (-1);
468 }
469 return (0);
470 }
471
472 char *
473 plural(n)
474 int n;
475 {
476 return (n != 1 ? "s" : "");
477 }
478
479 char *
480 plurales(n)
481 int n;
482 {
483 return (n != 1 ? "es" : "");
484 }
485
486 /*
487 * Find the protox for the given "well-known" name.
488 */
489 static struct protox *
490 knownname(name)
491 char *name;
492 {
493 struct protox **tpp, *tp;
494
495 for (tpp = protoprotox; *tpp; tpp++)
496 for (tp = *tpp; tp->pr_name; tp++)
497 if (strcmp(tp->pr_name, name) == 0)
498 return (tp);
499 return (NULL);
500 }
501
502 /*
503 * Find the protox corresponding to name.
504 */
505 static struct protox *
506 name2protox(name)
507 char *name;
508 {
509 struct protox *tp;
510 char **alias; /* alias from p->aliases */
511 struct protoent *p;
512
513 /*
514 * Try to find the name in the list of "well-known" names. If that
515 * fails, check if name is an alias for an Internet protocol.
516 */
517 if ((tp = knownname(name)) != NULL)
518 return (tp);
519
520 setprotoent(1); /* make protocol lookup cheaper */
521 while ((p = getprotoent()) != NULL) {
522 /* assert: name not same as p->name */
523 for (alias = p->p_aliases; *alias; alias++)
524 if (strcmp(name, *alias) == 0) {
525 endprotoent();
526 return (knownname(p->p_name));
527 }
528 }
529 endprotoent();
530 return (NULL);
531 }
532
533 static void
534 usage()
535 {
536 (void)fprintf(stderr,
537 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
538 (void)fprintf(stderr,
539 " %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
540 (void)fprintf(stderr,
541 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
542 (void)fprintf(stderr,
543 " %s [-M core] [-N system] [-p protocol]\n", __progname);
544 exit(1);
545 }
546