main.c revision 1.7 1 /*
2 * Copyright (c) 1983, 1988, 1993
3 * Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37 Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94";*/
42 static char *rcsid = "$Id: main.c,v 1.7 1995/06/12 03:03:11 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/file.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49
50 #include <netinet/in.h>
51
52 #include <ctype.h>
53 #include <errno.h>
54 #include <kvm.h>
55 #include <limits.h>
56 #include <netdb.h>
57 #include <nlist.h>
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include "netstat.h"
64
65 struct nlist nl[] = {
66 #define N_MBSTAT 0
67 { "_mbstat" },
68 #define N_IPSTAT 1
69 { "_ipstat" },
70 #define N_TCBTABLE 2
71 { "_tcbtable" },
72 #define N_TCPSTAT 3
73 { "_tcpstat" },
74 #define N_UDBTABLE 4
75 { "_udbtable" },
76 #define N_UDPSTAT 5
77 { "_udpstat" },
78 #define N_IFNET 6
79 { "_ifnet" },
80 #define N_IMP 7
81 { "_imp_softc" },
82 #define N_ICMPSTAT 8
83 { "_icmpstat" },
84 #define N_RTSTAT 9
85 { "_rtstat" },
86 #define N_UNIXSW 10
87 { "_unixsw" },
88 #define N_IDP 11
89 { "_nspcb"},
90 #define N_IDPSTAT 12
91 { "_idpstat"},
92 #define N_SPPSTAT 13
93 { "_spp_istat"},
94 #define N_NSERR 14
95 { "_ns_errstat"},
96 #define N_CLNPSTAT 15
97 { "_clnp_stat"},
98 #define IN_NOTUSED 16
99 { "_tp_inpcb" },
100 #define ISO_TP 17
101 { "_tp_refinfo" },
102 #define N_TPSTAT 18
103 { "_tp_stat" },
104 #define N_ESISSTAT 19
105 { "_esis_stat"},
106 #define N_NIMP 20
107 { "_nimp"},
108 #define N_RTREE 21
109 { "_rt_tables"},
110 #define N_CLTP 22
111 { "_cltb"},
112 #define N_CLTPSTAT 23
113 { "_cltpstat"},
114 #define N_NFILE 24
115 { "_nfile" },
116 #define N_FILE 25
117 { "_file" },
118 #define N_IGMPSTAT 26
119 { "_igmpstat" },
120 #define N_MRTPROTO 27
121 { "_ip_mrtproto" },
122 #define N_MRTSTAT 28
123 { "_mrtstat" },
124 #define N_MFCHASHTBL 29
125 { "_mfchashtbl" },
126 #define N_MFCHASH 30
127 { "_mfchash" },
128 #define N_VIFTABLE 31
129 { "_viftable" },
130 "",
131 };
132
133 struct protox {
134 u_char pr_index; /* index into nlist of cb head */
135 u_char pr_sindex; /* index into nlist of stat block */
136 u_char pr_wanted; /* 1 if wanted, 0 otherwise */
137 void (*pr_cblocks)(); /* control blocks printing routine */
138 void (*pr_stats)(); /* statistics printing routine */
139 char *pr_name; /* well-known name */
140 } protox[] = {
141 { N_TCBTABLE, N_TCPSTAT, 1, protopr,
142 tcp_stats, "tcp" },
143 { N_UDBTABLE, N_UDPSTAT, 1, protopr,
144 udp_stats, "udp" },
145 { -1, N_IPSTAT, 1, 0,
146 ip_stats, "ip" },
147 { -1, N_ICMPSTAT, 1, 0,
148 icmp_stats, "icmp" },
149 { -1, N_IGMPSTAT, 1, 0,
150 igmp_stats, "igmp" },
151 { -1, -1, 0, 0,
152 0, 0 }
153 };
154
155 struct protox nsprotox[] = {
156 { N_IDP, N_IDPSTAT, 1, nsprotopr,
157 idp_stats, "idp" },
158 { N_IDP, N_SPPSTAT, 1, nsprotopr,
159 spp_stats, "spp" },
160 { -1, N_NSERR, 1, 0,
161 nserr_stats, "ns_err" },
162 { -1, -1, 0, 0,
163 0, 0 }
164 };
165
166 struct protox isoprotox[] = {
167 { ISO_TP, N_TPSTAT, 1, iso_protopr,
168 tp_stats, "tp" },
169 { N_CLTP, N_CLTPSTAT, 1, iso_protopr,
170 cltp_stats, "cltp" },
171 { -1, N_CLNPSTAT, 1, 0,
172 clnp_stats, "clnp"},
173 { -1, N_ESISSTAT, 1, 0,
174 esis_stats, "esis"},
175 { -1, -1, 0, 0,
176 0, 0 }
177 };
178
179 struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
180
181 static void printproto __P((struct protox *, char *));
182 static void usage __P((void));
183 static struct protox *name2protox __P((char *));
184 static struct protox *knownname __P((char *));
185
186 kvm_t *kvmd;
187
188 int
189 main(argc, argv)
190 int argc;
191 char *argv[];
192 {
193 extern char *optarg;
194 extern int optind;
195 register struct protoent *p;
196 register struct protox *tp; /* for printing cblocks & stats */
197 register char *cp;
198 int ch;
199 char *nlistf = NULL, *memf = NULL;
200 char buf[_POSIX2_LINE_MAX];
201
202 if (cp = rindex(argv[0], '/'))
203 prog = cp + 1;
204 else
205 prog = argv[0];
206 af = AF_UNSPEC;
207
208 while ((ch = getopt(argc, argv, "Aadf:ghI:iM:mN:np:rstuw:")) != EOF)
209 switch(ch) {
210 case 'A':
211 Aflag = 1;
212 break;
213 case 'a':
214 aflag = 1;
215 break;
216 case 'd':
217 dflag = 1;
218 break;
219 case 'f':
220 if (strcmp(optarg, "ns") == 0)
221 af = AF_NS;
222 else if (strcmp(optarg, "inet") == 0)
223 af = AF_INET;
224 else if (strcmp(optarg, "unix") == 0)
225 af = AF_UNIX;
226 else if (strcmp(optarg, "iso") == 0)
227 af = AF_ISO;
228 else {
229 (void)fprintf(stderr,
230 "%s: %s: unknown address family\n",
231 prog, optarg);
232 exit(1);
233 }
234 break;
235 case 'g':
236 gflag = 1;
237 break;
238 case 'I': {
239 char *cp;
240
241 iflag = 1;
242 for (cp = interface = optarg; isalpha(*cp); cp++)
243 continue;
244 unit = atoi(cp);
245 *cp = '\0';
246 break;
247 }
248 case 'i':
249 iflag = 1;
250 break;
251 case 'M':
252 memf = optarg;
253 break;
254 case 'm':
255 mflag = 1;
256 break;
257 case 'N':
258 nlistf = optarg;
259 break;
260 case 'n':
261 nflag = 1;
262 break;
263 case 'p':
264 if ((tp = name2protox(optarg)) == NULL) {
265 (void)fprintf(stderr,
266 "%s: %s: unknown or uninstrumented protocol\n",
267 prog, optarg);
268 exit(1);
269 }
270 pflag = 1;
271 break;
272 case 'r':
273 rflag = 1;
274 break;
275 case 's':
276 ++sflag;
277 break;
278 case 't':
279 tflag = 1;
280 break;
281 case 'u':
282 af = AF_UNIX;
283 break;
284 case 'w':
285 interval = atoi(optarg);
286 iflag = 1;
287 break;
288 case '?':
289 default:
290 usage();
291 }
292 argv += optind;
293 argc -= optind;
294
295 #define BACKWARD_COMPATIBILITY
296 #ifdef BACKWARD_COMPATIBILITY
297 if (*argv) {
298 if (isdigit(**argv)) {
299 interval = atoi(*argv);
300 if (interval <= 0)
301 usage();
302 ++argv;
303 iflag = 1;
304 }
305 if (*argv) {
306 nlistf = *argv;
307 if (*++argv)
308 memf = *argv;
309 }
310 }
311 #endif
312
313 /*
314 * Discard setgid privileges if not the running kernel so that bad
315 * guys can't print interesting stuff from kernel memory.
316 */
317 if (nlistf != NULL || memf != NULL)
318 setgid(getgid());
319
320 if ((kvmd = kvm_open(nlistf, memf, NULL, O_RDONLY, prog)) == NULL) {
321 fprintf(stderr, "%s: kvm_open: %s\n", prog, 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", prog, nlistf);
327 else
328 fprintf(stderr, "%s: no namelist\n", prog);
329 exit(1);
330 }
331 if (mflag) {
332 mbpr(nl[N_MBSTAT].n_value);
333 exit(0);
334 }
335 if (pflag) {
336 if (tp->pr_stats)
337 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
338 tp->pr_name);
339 else
340 printf("%s: no stats routine\n", tp->pr_name);
341 exit(0);
342 }
343 /*
344 * Keep file descriptors open to avoid overhead
345 * of open/close on each call to get* routines.
346 */
347 sethostent(1);
348 setnetent(1);
349 if (iflag) {
350 intpr(interval, nl[N_IFNET].n_value);
351 exit(0);
352 }
353 if (rflag) {
354 if (sflag)
355 rt_stats(nl[N_RTSTAT].n_value);
356 else
357 routepr(nl[N_RTREE].n_value);
358 exit(0);
359 }
360 if (gflag) {
361 if (sflag)
362 mrt_stats(nl[N_MRTPROTO].n_value,
363 nl[N_MRTSTAT].n_value);
364 else
365 mroutepr(nl[N_MRTPROTO].n_value,
366 nl[N_MFCHASHTBL].n_value,
367 nl[N_MFCHASH].n_value,
368 nl[N_VIFTABLE].n_value);
369 exit(0);
370 }
371 if (af == AF_INET || af == AF_UNSPEC) {
372 setprotoent(1);
373 setservent(1);
374 /* ugh, this is O(MN) ... why do we do this? */
375 while (p = getprotoent()) {
376 for (tp = protox; tp->pr_name; tp++)
377 if (strcmp(tp->pr_name, p->p_name) == 0)
378 break;
379 if (tp->pr_name == 0 || tp->pr_wanted == 0)
380 continue;
381 printproto(tp, p->p_name);
382 }
383 endprotoent();
384 }
385 if (af == AF_NS || af == AF_UNSPEC)
386 for (tp = nsprotox; tp->pr_name; tp++)
387 printproto(tp, tp->pr_name);
388 if (af == AF_ISO || af == AF_UNSPEC)
389 for (tp = isoprotox; tp->pr_name; tp++)
390 printproto(tp, tp->pr_name);
391 if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
392 unixpr(nl[N_UNIXSW].n_value);
393 exit(0);
394 }
395
396 /*
397 * Print out protocol statistics or control blocks (per sflag).
398 * If the interface was not specifically requested, and the symbol
399 * is not in the namelist, ignore this one.
400 */
401 static void
402 printproto(tp, name)
403 register struct protox *tp;
404 char *name;
405 {
406 void (*pr)();
407 u_long off;
408
409 if (sflag) {
410 pr = tp->pr_stats;
411 off = nl[tp->pr_sindex].n_value;
412 } else {
413 pr = tp->pr_cblocks;
414 off = nl[tp->pr_index].n_value;
415 }
416 if (pr != NULL && (off || af != AF_UNSPEC))
417 (*pr)(off, name);
418 }
419
420 /*
421 * Read kernel memory, return 0 on success.
422 */
423 int
424 kread(addr, buf, size)
425 u_long addr;
426 char *buf;
427 int size;
428 {
429
430 if (kvm_read(kvmd, addr, buf, size) != size) {
431 /* XXX this duplicates kvm_read's error printout */
432 (void)fprintf(stderr, "%s: kvm_read %s\n", prog,
433 kvm_geterr(kvmd));
434 return (-1);
435 }
436 return (0);
437 }
438
439 char *
440 plural(n)
441 int n;
442 {
443 return (n != 1 ? "s" : "");
444 }
445
446 char *
447 plurales(n)
448 int n;
449 {
450 return (n != 1 ? "es" : "");
451 }
452
453 /*
454 * Find the protox for the given "well-known" name.
455 */
456 static struct protox *
457 knownname(name)
458 char *name;
459 {
460 struct protox **tpp, *tp;
461
462 for (tpp = protoprotox; *tpp; tpp++)
463 for (tp = *tpp; tp->pr_name; tp++)
464 if (strcmp(tp->pr_name, name) == 0)
465 return (tp);
466 return (NULL);
467 }
468
469 /*
470 * Find the protox corresponding to name.
471 */
472 static struct protox *
473 name2protox(name)
474 char *name;
475 {
476 struct protox *tp;
477 char **alias; /* alias from p->aliases */
478 struct protoent *p;
479
480 /*
481 * Try to find the name in the list of "well-known" names. If that
482 * fails, check if name is an alias for an Internet protocol.
483 */
484 if (tp = knownname(name))
485 return (tp);
486
487 setprotoent(1); /* make protocol lookup cheaper */
488 while (p = getprotoent()) {
489 /* assert: name not same as p->name */
490 for (alias = p->p_aliases; *alias; alias++)
491 if (strcmp(name, *alias) == 0) {
492 endprotoent();
493 return (knownname(p->p_name));
494 }
495 }
496 endprotoent();
497 return (NULL);
498 }
499
500 static void
501 usage()
502 {
503 (void)fprintf(stderr,
504 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
505 (void)fprintf(stderr,
506 " %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
507 (void)fprintf(stderr,
508 " %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
509 (void)fprintf(stderr,
510 " %s [-M core] [-N system] [-p protocol]\n", prog);
511 exit(1);
512 }
513