mrinfo.c revision 1.7.10.1 1 /* $NetBSD: mrinfo.c,v 1.7.10.1 2000/10/17 19:50:24 tv Exp $ */
2
3 /*
4 * This tool requests configuration info from a multicast router
5 * and prints the reply (if any). Invoke it as:
6 *
7 * mrinfo router-name-or-address
8 *
9 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
10 * multicast mapper written by Pavel Curtis).
11 *
12 * The lawyers insist we include the following UC copyright notice.
13 * The mapper from which this is derived contained a Xerox copyright
14 * notice which follows the UC one. Try not to get depressed noting
15 * that the legal gibberish is larger than the program.
16 *
17 * Copyright (c) 1993 Regents of the University of California.
18 * All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the Computer Systems
31 * Engineering Group at Lawrence Berkeley Laboratory.
32 * 4. Neither the name of the University nor of the Laboratory may be used
33 * to endorse or promote products derived from this software without
34 * specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ---------------------------------
48 * Copyright (c) Xerox Corporation 1992. All rights reserved.
49 *
50 * License is granted to copy, to use, and to make and to use derivative works
51 * for research and evaluation purposes, provided that Xerox is acknowledged
52 * in all documentation pertaining to any such copy or derivative work. Xerox
53 * grants no other licenses expressed or implied. The Xerox trade name should
54 * not be used in any advertising without its written permission.
55 *
56 * XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
57 * MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE FOR
58 * ANY PARTICULAR PURPOSE. The software is provided "as is" without express
59 * or implied warranty of any kind.
60 *
61 * These notices must be retained in any copies of any part of this software.
62 */
63
64 #include <sys/cdefs.h>
65 #ifndef lint
66 #if 0
67 static char rcsid[] =
68 "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
69 #else
70 __RCSID("$NetBSD: mrinfo.c,v 1.7.10.1 2000/10/17 19:50:24 tv Exp $");
71 #endif
72 #endif
73
74 #include <string.h>
75 #include <netdb.h>
76 #include <sys/time.h>
77 #include "defs.h"
78 #include <arpa/inet.h>
79 #ifdef __STDC__
80 #include <stdarg.h>
81 #else
82 #include <varargs.h>
83 #endif
84
85 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
86 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
87
88 u_int32_t our_addr, target_addr = 0; /* in NET order */
89 int debug = 0;
90 int nflag = 0;
91 int retries = DEFAULT_RETRIES;
92 int timeout = DEFAULT_TIMEOUT;
93 int target_level = 0;
94 vifi_t numvifs; /* to keep loader happy */
95 /* (see COPY_TABLES macro called in kern.c) */
96
97 char * inet_name __P((u_int32_t addr));
98 void ask __P((u_int32_t dst));
99 void ask2 __P((u_int32_t dst));
100 int get_number __P((int *var, int deflt, char ***pargv,
101 int *pargc));
102 u_int32_t host_addr __P((char *name));
103 void usage __P((void));
104
105 /* to shut up -Wstrict-prototypes */
106 int main __P((int argc, char *argv[]));
107
108
109 char *
110 inet_name(addr)
111 u_int32_t addr;
112 {
113 struct hostent *e;
114 struct in_addr in;
115
116 if (addr == 0)
117 return "local";
118
119 if (nflag ||
120 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
121 in.s_addr = addr;
122 return (inet_ntoa(in));
123 }
124 return (e->h_name);
125 }
126
127 /*
128 * Log errors and other messages to stderr, according to the severity of the
129 * message and the current debug level. For errors of severity LOG_ERR or
130 * worse, terminate the program.
131 */
132 #ifdef __STDC__
133 void
134 log(int severity, int syserr, const char *format, ...)
135 {
136 va_list ap;
137 char fmt[100];
138
139 va_start(ap, format);
140 #else
141 void
142 log(severity, syserr, format, va_alist)
143 int severity, syserr;
144 char *format;
145 va_dcl
146 {
147 va_list ap;
148 char fmt[100];
149
150 va_start(ap);
151 #endif
152 switch (debug) {
153 case 0:
154 if (severity > LOG_WARNING)
155 return;
156 case 1:
157 if (severity > LOG_NOTICE)
158 return;
159 case 2:
160 if (severity > LOG_INFO)
161 return;
162 default:
163 fmt[0] = '\0';
164 if (severity == LOG_WARNING)
165 strcat(fmt, "warning - ");
166 strncat(fmt, format, 80);
167 format = fmt;
168 vfprintf(stderr, format, ap);
169 if (syserr == 0)
170 fprintf(stderr, "\n");
171 else
172 fprintf(stderr, ": %s\n", strerror(syserr));
173 }
174
175 if (severity <= LOG_ERR)
176 exit(-1);
177 }
178
179 /*
180 * Send a neighbors-list request.
181 */
182 void
183 ask(dst)
184 u_int32_t dst;
185 {
186 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
187 htonl(MROUTED_LEVEL), 0);
188 }
189
190 void
191 ask2(dst)
192 u_int32_t dst;
193 {
194 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
195 htonl(MROUTED_LEVEL), 0);
196 }
197
198 /*
199 * Process an incoming neighbor-list message.
200 */
201 void
202 accept_neighbors(src, dst, p, datalen, level)
203 u_int32_t src, dst, level;
204 u_char *p;
205 int datalen;
206 {
207 u_char *ep = p + datalen;
208 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
209 a += ((u_int32_t)*p++ << 8), a += *p++)
210
211 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
212 while (p < ep) {
213 register u_int32_t laddr;
214 register u_char metric;
215 register u_char thresh;
216 register int ncount;
217
218 GET_ADDR(laddr);
219 laddr = htonl(laddr);
220 metric = *p++;
221 thresh = *p++;
222 ncount = *p++;
223 while (--ncount >= 0) {
224 register u_int32_t neighbor;
225 GET_ADDR(neighbor);
226 neighbor = htonl(neighbor);
227 printf(" %s -> ", inet_fmt(laddr, s1));
228 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
229 inet_name(neighbor), metric, thresh);
230 }
231 }
232 }
233
234 void
235 accept_neighbors2(src, dst, p, datalen, level)
236 u_int32_t src, dst, level;
237 u_char *p;
238 int datalen;
239 {
240 u_char *ep = p + datalen;
241 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
242 /* well, only possibly_broken_cisco, but that's too long to type. */
243
244 printf("%s (%s) [version %d.%d", inet_fmt(src, s1), inet_name(src),
245 level & 0xff, (level >> 8) & 0xff);
246 if ((level >> 16) & NF_LEAF) { printf (",leaf"); }
247 if ((level >> 16) & NF_PRUNE) { printf (",prune"); }
248 if ((level >> 16) & NF_GENID) { printf (",genid"); }
249 if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
250 printf ("]:\n");
251
252 while (p < ep) {
253 register u_char metric;
254 register u_char thresh;
255 register u_char flags;
256 register int ncount;
257 register u_int32_t laddr = *(u_int32_t*)p;
258
259 p += 4;
260 metric = *p++;
261 thresh = *p++;
262 flags = *p++;
263 ncount = *p++;
264 if (broken_cisco && ncount == 0) /* dumb Ciscos */
265 ncount = 1;
266 if (broken_cisco && ncount > 15) /* dumb Ciscos */
267 ncount = ncount & 0xf;
268 while (--ncount >= 0 && p < ep) {
269 register u_int32_t neighbor = *(u_int32_t*)p;
270 p += 4;
271 printf(" %s -> ", inet_fmt(laddr, s1));
272 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
273 inet_name(neighbor), metric, thresh);
274 if (flags & DVMRP_NF_TUNNEL)
275 printf("/tunnel");
276 if (flags & DVMRP_NF_SRCRT)
277 printf("/srcrt");
278 if (flags & DVMRP_NF_PIM)
279 printf("/pim");
280 if (flags & DVMRP_NF_QUERIER)
281 printf("/querier");
282 if (flags & DVMRP_NF_DISABLED)
283 printf("/disabled");
284 if (flags & DVMRP_NF_DOWN)
285 printf("/down");
286 if (flags & DVMRP_NF_LEAF)
287 printf("/leaf");
288 printf("]\n");
289 }
290 }
291 }
292
293 int
294 get_number(var, deflt, pargv, pargc)
295 int *var, *pargc, deflt;
296 char ***pargv;
297 {
298 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
299 * argument */
300 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
301 (*pargv)++, (*pargc)--;
302 *var = atoi((*pargv)[0]);
303 return 1;
304 } else if (deflt >= 0) {
305 *var = deflt;
306 return 1;
307 } else
308 return 0;
309 } else { /* Get value from the rest of this argument */
310 if (isdigit((*pargv)[0][2])) {
311 *var = atoi((*pargv)[0] + 2);
312 return 1;
313 } else {
314 return 0;
315 }
316 }
317 }
318
319 void
320 usage()
321 {
322 fprintf(stderr,
323 "Usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
324 exit(1);
325 }
326
327 int
328 main(argc, argv)
329 int argc;
330 char *argv[];
331 {
332 int tries;
333 int trynew;
334 struct timeval et;
335 struct hostent *hp;
336 struct hostent bogus;
337 char *host;
338 int curaddr;
339
340 setlinebuf(stderr);
341
342 if (geteuid() != 0) {
343 fprintf(stderr, "mrinfo: must be root\n");
344 exit(1);
345 }
346 argv++, argc--;
347 while (argc > 0 && argv[0][0] == '-') {
348 switch (argv[0][1]) {
349 case 'd':
350 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
351 usage();
352 break;
353 case 'n':
354 ++nflag;
355 break;
356 case 'r':
357 if (!get_number(&retries, -1, &argv, &argc))
358 usage();
359 break;
360 case 't':
361 if (!get_number(&timeout, -1, &argv, &argc))
362 usage();
363 break;
364 default:
365 usage();
366 }
367 argv++, argc--;
368 }
369 if (argc > 1)
370 usage();
371 if (argc == 1)
372 host = argv[0];
373 else
374 host = "127.0.0.1";
375
376 if ((target_addr = inet_addr(host)) != -1) {
377 hp = &bogus;
378 hp->h_length = sizeof(target_addr);
379 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
380 hp->h_addr_list[0] = malloc(hp->h_length);
381 memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
382 hp->h_addr_list[1] = 0;
383 } else
384 hp = gethostbyname(host);
385
386 if (hp == NULL) {
387 fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
388 exit(1);
389 }
390 if (debug)
391 fprintf(stderr, "Debug level %u\n", debug);
392
393 init_igmp();
394
395 /* Check all addresses; mrouters often have unreachable interfaces */
396 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
397 memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
398 { /* Find a good local address for us. */
399 int udp;
400 struct sockaddr_in addr;
401 int addrlen = sizeof(addr);
402
403 addr.sin_family = AF_INET;
404 #if (defined(BSD) && (BSD >= 199103))
405 addr.sin_len = sizeof addr;
406 #endif
407 addr.sin_addr.s_addr = target_addr;
408 addr.sin_port = htons(2000); /* any port over 1024 will
409 * do... */
410 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
411 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
412 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
413 perror("Determining local address");
414 exit(-1);
415 }
416 close(udp);
417 our_addr = addr.sin_addr.s_addr;
418 }
419
420 tries = 0;
421 trynew = 1;
422 /*
423 * New strategy: send 'ask2' for two timeouts, then fall back
424 * to 'ask', since it's not very likely that we are going to
425 * find someone who only responds to 'ask' these days
426 */
427 ask2(target_addr);
428
429 gettimeofday(&et, 0);
430 et.tv_sec += timeout;
431
432 /* Main receive loop */
433 for (;;) {
434 fd_set fds;
435 struct timeval tv, now;
436 int count, recvlen, dummy = 0;
437 register u_int32_t src, dst, group;
438 struct ip *ip;
439 struct igmp *igmp;
440 int ipdatalen, iphdrlen, igmpdatalen;
441
442 FD_ZERO(&fds);
443 FD_SET(igmp_socket, &fds);
444
445 gettimeofday(&now, 0);
446 tv.tv_sec = et.tv_sec - now.tv_sec;
447 tv.tv_usec = et.tv_usec - now.tv_usec;
448
449 if (tv.tv_usec < 0) {
450 tv.tv_usec += 1000000L;
451 --tv.tv_sec;
452 }
453 if (tv.tv_sec < 0)
454 tv.tv_sec = tv.tv_usec = 0;
455
456 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
457
458 if (count < 0) {
459 if (errno != EINTR)
460 perror("select");
461 continue;
462 } else if (count == 0) {
463 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
464 if (++tries > retries)
465 break;
466 /* If we've tried ASK_NEIGHBORS2 twice with
467 * no response, fall back to ASK_NEIGHBORS
468 */
469 if (tries == 2 && target_level == 0)
470 trynew = 0;
471 if (target_level == 0 && trynew == 0)
472 ask(target_addr);
473 else
474 ask2(target_addr);
475 gettimeofday(&et, 0);
476 et.tv_sec += timeout;
477 continue;
478 }
479 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
480 0, NULL, &dummy);
481 if (recvlen <= 0) {
482 if (recvlen && errno != EINTR)
483 perror("recvfrom");
484 continue;
485 }
486
487 if (recvlen < sizeof(struct ip)) {
488 log(LOG_WARNING, 0,
489 "packet too short (%u bytes) for IP header",
490 recvlen);
491 continue;
492 }
493 ip = (struct ip *) recv_buf;
494 if (ip->ip_p == 0)
495 continue; /* Request to install cache entry */
496 src = ip->ip_src.s_addr;
497 dst = ip->ip_dst.s_addr;
498 iphdrlen = ip->ip_hl << 2;
499 ipdatalen = ip->ip_len;
500 if (iphdrlen + ipdatalen != recvlen) {
501 log(LOG_WARNING, 0,
502 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
503 recvlen, iphdrlen, ipdatalen);
504 continue;
505 }
506 igmp = (struct igmp *) (recv_buf + iphdrlen);
507 group = igmp->igmp_group.s_addr;
508 igmpdatalen = ipdatalen - IGMP_MINLEN;
509 if (igmpdatalen < 0) {
510 log(LOG_WARNING, 0,
511 "IP data field too short (%u bytes) for IGMP, from %s",
512 ipdatalen, inet_fmt(src, s1));
513 continue;
514 }
515 if (igmp->igmp_type != IGMP_DVMRP)
516 continue;
517
518 switch (igmp->igmp_code) {
519 case DVMRP_NEIGHBORS:
520 case DVMRP_NEIGHBORS2:
521 if (src != target_addr) {
522 fprintf(stderr, "mrinfo: got reply from %s",
523 inet_fmt(src, s1));
524 fprintf(stderr, " instead of %s\n",
525 inet_fmt(target_addr, s1));
526 /*continue;*/
527 }
528 break;
529 default:
530 continue; /* ignore all other DVMRP messages */
531 }
532
533 switch (igmp->igmp_code) {
534
535 case DVMRP_NEIGHBORS:
536 if (group) {
537 /* knows about DVMRP_NEIGHBORS2 msg */
538 if (target_level == 0) {
539 target_level = ntohl(group);
540 ask2(target_addr);
541 }
542 } else {
543 accept_neighbors(src, dst, (u_char *)(igmp + 1),
544 igmpdatalen, ntohl(group));
545 exit(0);
546 }
547 break;
548
549 case DVMRP_NEIGHBORS2:
550 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
551 igmpdatalen, ntohl(group));
552 exit(0);
553 }
554 }
555 }
556 exit(1);
557 }
558
559 /* dummies */
560 void accept_probe(src, dst, p, datalen, level)
561 u_int32_t src, dst, level;
562 char *p;
563 int datalen;
564 {
565 }
566 void accept_group_report(src, dst, group, r_type)
567 u_int32_t src, dst, group;
568 int r_type;
569 {
570 }
571 void accept_neighbor_request2(src, dst)
572 u_int32_t src, dst;
573 {
574 }
575 void accept_report(src, dst, p, datalen, level)
576 u_int32_t src, dst, level;
577 char *p;
578 int datalen;
579 {
580 }
581 void accept_neighbor_request(src, dst)
582 u_int32_t src, dst;
583 {
584 }
585 void accept_prune(src, dst, p, datalen)
586 u_int32_t src, dst;
587 char *p;
588 int datalen;
589 {
590 }
591 void accept_graft(src, dst, p, datalen)
592 u_int32_t src, dst;
593 char *p;
594 int datalen;
595 {
596 }
597 void accept_g_ack(src, dst, p, datalen)
598 u_int32_t src, dst;
599 char *p;
600 int datalen;
601 {
602 }
603 void add_table_entry(origin, mcastgrp)
604 u_int32_t origin, mcastgrp;
605 {
606 }
607 void check_vif_state()
608 {
609 }
610 void accept_leave_message(src, dst, group)
611 u_int32_t src, dst, group;
612 {
613 }
614 void accept_mtrace(src, dst, group, data, no, datalen)
615 u_int32_t src, dst, group;
616 char *data;
617 u_int no;
618 int datalen;
619 {
620 }
621 void accept_membership_query(src, dst, group, tmo)
622 u_int32_t src, dst, group;
623 int tmo;
624 {
625 }
626 void accept_info_request(src, dst, p, datalen)
627 u_int32_t src, dst;
628 u_char *p;
629 int datalen;
630 {
631 }
632 void accept_info_reply(src, dst, p, datalen)
633 u_int32_t src, dst;
634 u_char *p;
635 int datalen;
636 {
637 }
638