main.c revision 1.10 1 /* $NetBSD: main.c,v 1.10 1997/02/28 00:14:21 jonathan 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 #ifndef lint
37 char copyright[] =
38 "@(#) 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 static char *rcsid = "$NetBSD: main.c,v 1.10 1997/02/28 00:14:21 jonathan 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 "",
140 };
141
142 struct protox {
143 u_char pr_index; /* index into nlist of cb head */
144 u_char pr_sindex; /* index into nlist of stat block */
145 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
146 void (*pr_cblocks)(); /* control blocks printing routine */
147 void (*pr_stats)(); /* statistics printing routine */
148 char *pr_name; /* well-known name */
149 } protox[] = {
150 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
151 tcp_stats, "tcp" },
152 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
153 udp_stats, "udp" },
154 { -1, N_IPSTAT, 1, 0,
155 ip_stats, "ip" },
156 { -1, N_ICMPSTAT, 1, 0,
157 icmp_stats, "icmp" },
158 { -1, N_IGMPSTAT, 1, 0,
159 igmp_stats, "igmp" },
160 { -1, -1, 0, 0,
161 0, 0 }
162 };
163
164 struct protox nsprotox[] = {
165 { N_IDP, N_IDPSTAT, 1, nsprotopr,
166 idp_stats, "idp" },
167 { N_IDP, N_SPPSTAT, 1, nsprotopr,
168 spp_stats, "spp" },
169 { -1, N_NSERR, 1, 0,
170 nserr_stats, "ns_err" },
171 { -1, -1, 0, 0,
172 0, 0 }
173 };
174
175 struct protox isoprotox[] = {
176 { ISO_TP, N_TPSTAT, 1, iso_protopr,
177 tp_stats, "tp" },
178 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
179 cltp_stats, "cltp" },
180 { -1, N_CLNPSTAT, 1, 0,
181 clnp_stats, "clnp"},
182 { -1, N_ESISSTAT, 1, 0,
183 esis_stats, "esis"},
184 { -1, -1, 0, 0,
185 0, 0 }
186 };
187
188 struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
189
190 static void printproto __P((struct protox *, char *));
191 static void usage __P((void));
192 static struct protox *name2protox __P((char *));
193 static struct protox *knownname __P((char *));
194
195 kvm_t *kvmd;
196
197 int
198 main(argc, argv)
199 int argc;
200 char *argv[];
201 {
202 extern char *optarg;
203 extern int optind;
204 register struct protoent *p;
205 register struct protox *tp; /* for printing cblocks & stats */
206 register char *cp;
207 int ch;
208 char *nlistf = NULL, *memf = NULL;
209 char buf[_POSIX2_LINE_MAX];
210
211 af = AF_UNSPEC;
212
213 while ((ch = getopt(argc, argv, "Aadf:ghI:iM:mN:np:rstuw:")) != EOF)
214 switch(ch) {
215 case 'A':
216 Aflag = 1;
217 break;
218 case 'a':
219 aflag = 1;
220 break;
221 case 'd':
222 dflag = 1;
223 break;
224 case 'f':
225 if (strcmp(optarg, "ns") == 0)
226 af = AF_NS;
227 else if (strcmp(optarg, "inet") == 0)
228 af = AF_INET;
229 else if (strcmp(optarg, "unix") == 0)
230 af = AF_UNIX;
231 else if (strcmp(optarg, "iso") == 0)
232 af = AF_ISO;
233 else {
234 (void)fprintf(stderr,
235 "%s: %s: unknown address family\n",
236 __progname, optarg);
237 exit(1);
238 }
239 break;
240 case 'g':
241 gflag = 1;
242 break;
243 case 'I':
244 iflag = 1;
245 interface = optarg;
246 break;
247 case 'i':
248 iflag = 1;
249 break;
250 case 'M':
251 memf = optarg;
252 break;
253 case 'm':
254 mflag = 1;
255 break;
256 case 'N':
257 nlistf = optarg;
258 break;
259 case 'n':
260 nflag = 1;
261 break;
262 case 'p':
263 if ((tp = name2protox(optarg)) == NULL) {
264 (void)fprintf(stderr,
265 "%s: %s: unknown or uninstrumented protocol\n",
266 __progname, optarg);
267 exit(1);
268 }
269 pflag = 1;
270 break;
271 case 'r':
272 rflag = 1;
273 break;
274 case 's':
275 ++sflag;
276 break;
277 case 't':
278 tflag = 1;
279 break;
280 case 'u':
281 af = AF_UNIX;
282 break;
283 case 'w':
284 interval = atoi(optarg);
285 iflag = 1;
286 break;
287 case '?':
288 default:
289 usage();
290 }
291 argv += optind;
292 argc -= optind;
293
294 #define BACKWARD_COMPATIBILITY
295 #ifdef BACKWARD_COMPATIBILITY
296 if (*argv) {
297 if (isdigit(**argv)) {
298 interval = atoi(*argv);
299 if (interval <= 0)
300 usage();
301 ++argv;
302 iflag = 1;
303 }
304 if (*argv) {
305 nlistf = *argv;
306 if (*++argv)
307 memf = *argv;
308 }
309 }
310 #endif
311
312 /*
313 * Discard setgid privileges if not the running kernel so that bad
314 * guys can't print interesting stuff from kernel memory.
315 */
316 if (nlistf != NULL || memf != NULL)
317 setgid(getgid());
318
319 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
320 buf)) == NULL) {
321 fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
322 exit(1);
323 }
324 if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
325 if (nlistf)
326 fprintf(stderr, "%s: %s: no namelist\n", __progname,
327 nlistf);
328 else
329 fprintf(stderr, "%s: no namelist\n", __progname);
330 exit(1);
331 }
332 if (mflag) {
333 mbpr(nl[N_MBSTAT].n_value, nl[N_MSIZE].n_value,
334 nl[N_MCLBYTES].n_value);
335 exit(0);
336 }
337 if (pflag) {
338 if (tp->pr_stats)
339 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
340 tp->pr_name);
341 else
342 printf("%s: no stats routine\n", tp->pr_name);
343 exit(0);
344 }
345 /*
346 * Keep file descriptors open to avoid overhead
347 * of open/close on each call to get* routines.
348 */
349 sethostent(1);
350 setnetent(1);
351 if (iflag) {
352 intpr(interval, nl[N_IFNET].n_value);
353 exit(0);
354 }
355 if (rflag) {
356 if (sflag)
357 rt_stats(nl[N_RTSTAT].n_value);
358 else
359 routepr(nl[N_RTREE].n_value);
360 exit(0);
361 }
362 if (gflag) {
363 if (sflag)
364 mrt_stats(nl[N_MRTPROTO].n_value,
365 nl[N_MRTSTAT].n_value);
366 else
367 mroutepr(nl[N_MRTPROTO].n_value,
368 nl[N_MFCHASHTBL].n_value,
369 nl[N_MFCHASH].n_value,
370 nl[N_VIFTABLE].n_value);
371 exit(0);
372 }
373 if (af == AF_INET || af == AF_UNSPEC) {
374 setprotoent(1);
375 setservent(1);
376 /* ugh, this is O(MN) ... why do we do this? */
377 while (p = getprotoent()) {
378 for (tp = protox; tp->pr_name; tp++)
379 if (strcmp(tp->pr_name, p->p_name) == 0)
380 break;
381 if (tp->pr_name == 0 || tp->pr_wanted == 0)
382 continue;
383 printproto(tp, p->p_name);
384 }
385 endprotoent();
386 }
387 if (af == AF_NS || af == AF_UNSPEC)
388 for (tp = nsprotox; tp->pr_name; tp++)
389 printproto(tp, tp->pr_name);
390 if (af == AF_ISO || af == AF_UNSPEC)
391 for (tp = isoprotox; tp->pr_name; tp++)
392 printproto(tp, tp->pr_name);
393 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
394 unixpr(nl[N_UNIXSW].n_value);
395 exit(0);
396 }
397
398 /*
399 * Print out protocol statistics or control blocks (per sflag).
400 * If the interface was not specifically requested, and the symbol
401 * is not in the namelist, ignore this one.
402 */
403 static void
404 printproto(tp, name)
405 register struct protox *tp;
406 char *name;
407 {
408 void (*pr)();
409 u_long off;
410
411 if (sflag) {
412 pr = tp->pr_stats;
413 off = nl[tp->pr_sindex].n_value;
414 } else {
415 pr = tp->pr_cblocks;
416 off = nl[tp->pr_index].n_value;
417 }
418 if (pr != NULL && (off || af != AF_UNSPEC))
419 (*pr)(off, name);
420 }
421
422 /*
423 * Read kernel memory, return 0 on success.
424 */
425 int
426 kread(addr, buf, size)
427 u_long addr;
428 char *buf;
429 int size;
430 {
431
432 if (kvm_read(kvmd, addr, buf, size) != size) {
433 (void)fprintf(stderr, "%s: %s\n", __progname,
434 kvm_geterr(kvmd));
435 return (-1);
436 }
437 return (0);
438 }
439
440 char *
441 plural(n)
442 int n;
443 {
444 return (n != 1 ? "s" : "");
445 }
446
447 char *
448 plurales(n)
449 int n;
450 {
451 return (n != 1 ? "es" : "");
452 }
453
454 /*
455 * Find the protox for the given "well-known" name.
456 */
457 static struct protox *
458 knownname(name)
459 char *name;
460 {
461 struct protox **tpp, *tp;
462
463 for (tpp = protoprotox; *tpp; tpp++)
464 for (tp = *tpp; tp->pr_name; tp++)
465 if (strcmp(tp->pr_name, name) == 0)
466 return (tp);
467 return (NULL);
468 }
469
470 /*
471 * Find the protox corresponding to name.
472 */
473 static struct protox *
474 name2protox(name)
475 char *name;
476 {
477 struct protox *tp;
478 char **alias; /* alias from p->aliases */
479 struct protoent *p;
480
481 /*
482 * Try to find the name in the list of "well-known" names. If that
483 * fails, check if name is an alias for an Internet protocol.
484 */
485 if (tp = knownname(name))
486 return (tp);
487
488 setprotoent(1); /* make protocol lookup cheaper */
489 while (p = getprotoent()) {
490 /* assert: name not same as p->name */
491 for (alias = p->p_aliases; *alias; alias++)
492 if (strcmp(name, *alias) == 0) {
493 endprotoent();
494 return (knownname(p->p_name));
495 }
496 }
497 endprotoent();
498 return (NULL);
499 }
500
501 static void
502 usage()
503 {
504 (void)fprintf(stderr,
505 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
506 (void)fprintf(stderr,
507 " %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
508 (void)fprintf(stderr,
509 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
510 (void)fprintf(stderr,
511 " %s [-M core] [-N system] [-p protocol]\n", __progname);
512 exit(1);
513 }
514