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