mrinfo.c revision 1.12.2.1 1 /* $NetBSD: mrinfo.c,v 1.12.2.1 2002/06/04 11:56:50 lukem 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.2.1 2002/06/04 11:56:50 lukem 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 memset(&addr, 0, sizeof(addr));
418 addr.sin_family = AF_INET;
419 #if (defined(BSD) && (BSD >= 199103))
420 addr.sin_len = sizeof addr;
421 #endif
422 addr.sin_addr.s_addr = target_addr;
423 addr.sin_port = htons(2000); /* any port over 1024 will
424 * do... */
425 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
426 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
427 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
428 perror("Determining local address");
429 exit(1);
430 }
431 close(udp);
432 our_addr = addr.sin_addr.s_addr;
433 }
434
435 tries = 0;
436 trynew = 1;
437 /*
438 * New strategy: send 'ask2' for two timeouts, then fall back
439 * to 'ask', since it's not very likely that we are going to
440 * find someone who only responds to 'ask' these days
441 */
442 ask2(target_addr);
443
444 gettimeofday(&et, 0);
445 et.tv_sec += timeout;
446
447 /* Main receive loop */
448 for (;;) {
449 fd_set fds;
450 struct timeval tv, now;
451 int count, recvlen, dummy = 0;
452 register u_int32_t src, dst, group;
453 struct ip *ip;
454 struct igmp *igmp;
455 int ipdatalen, iphdrlen, igmpdatalen;
456
457 FD_ZERO(&fds);
458 FD_SET(igmp_socket, &fds);
459
460 gettimeofday(&now, 0);
461 tv.tv_sec = et.tv_sec - now.tv_sec;
462 tv.tv_usec = et.tv_usec - now.tv_usec;
463
464 if (tv.tv_usec < 0) {
465 tv.tv_usec += 1000000L;
466 --tv.tv_sec;
467 }
468 if (tv.tv_sec < 0)
469 tv.tv_sec = tv.tv_usec = 0;
470
471 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
472
473 if (count < 0) {
474 if (errno != EINTR)
475 perror("select");
476 continue;
477 } else if (count == 0) {
478 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
479 if (++tries > retries)
480 break;
481 /* If we've tried ASK_NEIGHBORS2 twice with
482 * no response, fall back to ASK_NEIGHBORS
483 */
484 if (tries == 2 && target_level == 0)
485 trynew = 0;
486 if (target_level == 0 && trynew == 0)
487 ask(target_addr);
488 else
489 ask2(target_addr);
490 gettimeofday(&et, 0);
491 et.tv_sec += timeout;
492 continue;
493 }
494 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
495 0, NULL, &dummy);
496 if (recvlen <= 0) {
497 if (recvlen && errno != EINTR)
498 perror("recvfrom");
499 continue;
500 }
501
502 if (recvlen < sizeof(struct ip)) {
503 log(LOG_WARNING, 0,
504 "packet too short (%u bytes) for IP header",
505 recvlen);
506 continue;
507 }
508 ip = (struct ip *) recv_buf;
509 if (ip->ip_p == 0)
510 continue; /* Request to install cache entry */
511 src = ip->ip_src.s_addr;
512 dst = ip->ip_dst.s_addr;
513 iphdrlen = ip->ip_hl << 2;
514 ipdatalen = ip->ip_len;
515 if (iphdrlen + ipdatalen != recvlen) {
516 log(LOG_WARNING, 0,
517 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
518 recvlen, iphdrlen, ipdatalen);
519 continue;
520 }
521 igmp = (struct igmp *) (recv_buf + iphdrlen);
522 group = igmp->igmp_group.s_addr;
523 igmpdatalen = ipdatalen - IGMP_MINLEN;
524 if (igmpdatalen < 0) {
525 log(LOG_WARNING, 0,
526 "IP data field too short (%u bytes) for IGMP, from %s",
527 ipdatalen, inet_fmt(src, s1));
528 continue;
529 }
530 if (igmp->igmp_type != IGMP_DVMRP)
531 continue;
532
533 switch (igmp->igmp_code) {
534 case DVMRP_NEIGHBORS:
535 case DVMRP_NEIGHBORS2:
536 if (src != target_addr) {
537 fprintf(stderr, "mrinfo: got reply from %s",
538 inet_fmt(src, s1));
539 fprintf(stderr, " instead of %s\n",
540 inet_fmt(target_addr, s1));
541 /*continue;*/
542 }
543 break;
544 default:
545 continue; /* ignore all other DVMRP messages */
546 }
547
548 switch (igmp->igmp_code) {
549
550 case DVMRP_NEIGHBORS:
551 if (group) {
552 /* knows about DVMRP_NEIGHBORS2 msg */
553 if (target_level == 0) {
554 target_level = ntohl(group);
555 ask2(target_addr);
556 }
557 } else {
558 accept_neighbors(src, dst, (u_char *)(igmp + 1),
559 igmpdatalen, ntohl(group));
560 exit(0);
561 }
562 break;
563
564 case DVMRP_NEIGHBORS2:
565 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
566 igmpdatalen, ntohl(group));
567 exit(0);
568 }
569 }
570 }
571 exit(1);
572 }
573
574 /* dummies */
575 void accept_probe(src, dst, p, datalen, level)
576 u_int32_t src, dst, level;
577 char *p;
578 int datalen;
579 {
580 }
581 void accept_group_report(src, dst, group, r_type)
582 u_int32_t src, dst, group;
583 int r_type;
584 {
585 }
586 void accept_neighbor_request2(src, dst)
587 u_int32_t src, dst;
588 {
589 }
590 void accept_report(src, dst, p, datalen, level)
591 u_int32_t src, dst, level;
592 char *p;
593 int datalen;
594 {
595 }
596 void accept_neighbor_request(src, dst)
597 u_int32_t src, dst;
598 {
599 }
600 void accept_prune(src, dst, p, datalen)
601 u_int32_t src, dst;
602 char *p;
603 int datalen;
604 {
605 }
606 void accept_graft(src, dst, p, datalen)
607 u_int32_t src, dst;
608 char *p;
609 int datalen;
610 {
611 }
612 void accept_g_ack(src, dst, p, datalen)
613 u_int32_t src, dst;
614 char *p;
615 int datalen;
616 {
617 }
618 void add_table_entry(origin, mcastgrp)
619 u_int32_t origin, mcastgrp;
620 {
621 }
622 void check_vif_state()
623 {
624 }
625 void accept_leave_message(src, dst, group)
626 u_int32_t src, dst, group;
627 {
628 }
629 void accept_mtrace(src, dst, group, data, no, datalen)
630 u_int32_t src, dst, group;
631 char *data;
632 u_int no;
633 int datalen;
634 {
635 }
636 void accept_membership_query(src, dst, group, tmo)
637 u_int32_t src, dst, group;
638 int tmo;
639 {
640 }
641 void accept_info_request(src, dst, p, datalen)
642 u_int32_t src, dst;
643 u_char *p;
644 int datalen;
645 {
646 }
647 void accept_info_reply(src, dst, p, datalen)
648 u_int32_t src, dst;
649 u_char *p;
650 int datalen;
651 {
652 }
653