mrinfo.c revision 1.7.10.2 1 /* $NetBSD: mrinfo.c,v 1.7.10.2 2002/09/04 00:53:00 itojun 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.2 2002/09/04 00:53:00 itojun 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
138 va_start(ap, format);
139 #else
140 void
141 log(severity, syserr, format, va_alist)
142 int severity, syserr;
143 char *format;
144 va_dcl
145 {
146 va_list ap;
147
148 va_start(ap);
149 #endif
150 switch (debug) {
151 case 0:
152 if (severity > LOG_WARNING)
153 return;
154 case 1:
155 if (severity > LOG_NOTICE)
156 return;
157 case 2:
158 if (severity > LOG_INFO)
159 return;
160 default:
161 if (severity == LOG_WARNING)
162 fprintf(stderr, "warning - ");
163 vfprintf(stderr, format, ap);
164 if (syserr == 0)
165 fprintf(stderr, "\n");
166 else
167 fprintf(stderr, ": %s\n", strerror(syserr));
168 }
169
170 if (severity <= LOG_ERR)
171 exit(-1);
172 }
173
174 /*
175 * Send a neighbors-list request.
176 */
177 void
178 ask(dst)
179 u_int32_t dst;
180 {
181 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
182 htonl(MROUTED_LEVEL), 0);
183 }
184
185 void
186 ask2(dst)
187 u_int32_t dst;
188 {
189 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
190 htonl(MROUTED_LEVEL), 0);
191 }
192
193 /*
194 * Process an incoming neighbor-list message.
195 */
196 void
197 accept_neighbors(src, dst, p, datalen, level)
198 u_int32_t src, dst, level;
199 u_char *p;
200 int datalen;
201 {
202 u_char *ep = p + datalen;
203 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
204 a += ((u_int32_t)*p++ << 8), a += *p++)
205
206 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
207 while (p < ep) {
208 register u_int32_t laddr;
209 register u_char metric;
210 register u_char thresh;
211 register int ncount;
212
213 GET_ADDR(laddr);
214 laddr = htonl(laddr);
215 metric = *p++;
216 thresh = *p++;
217 ncount = *p++;
218 while (--ncount >= 0) {
219 register u_int32_t neighbor;
220 GET_ADDR(neighbor);
221 neighbor = htonl(neighbor);
222 printf(" %s -> ", inet_fmt(laddr, s1));
223 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
224 inet_name(neighbor), metric, thresh);
225 }
226 }
227 }
228
229 void
230 accept_neighbors2(src, dst, p, datalen, level)
231 u_int32_t src, dst, level;
232 u_char *p;
233 int datalen;
234 {
235 u_char *ep = p + datalen;
236 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
237 /* well, only possibly_broken_cisco, but that's too long to type. */
238
239 printf("%s (%s) [version %d.%d", inet_fmt(src, s1), inet_name(src),
240 level & 0xff, (level >> 8) & 0xff);
241 if ((level >> 16) & NF_LEAF) { printf (",leaf"); }
242 if ((level >> 16) & NF_PRUNE) { printf (",prune"); }
243 if ((level >> 16) & NF_GENID) { printf (",genid"); }
244 if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
245 printf ("]:\n");
246
247 while (p < ep) {
248 register u_char metric;
249 register u_char thresh;
250 register u_char flags;
251 register int ncount;
252 register u_int32_t laddr = *(u_int32_t*)p;
253
254 p += 4;
255 metric = *p++;
256 thresh = *p++;
257 flags = *p++;
258 ncount = *p++;
259 if (broken_cisco && ncount == 0) /* dumb Ciscos */
260 ncount = 1;
261 if (broken_cisco && ncount > 15) /* dumb Ciscos */
262 ncount = ncount & 0xf;
263 while (--ncount >= 0 && p < ep) {
264 register u_int32_t neighbor = *(u_int32_t*)p;
265 p += 4;
266 printf(" %s -> ", inet_fmt(laddr, s1));
267 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
268 inet_name(neighbor), metric, thresh);
269 if (flags & DVMRP_NF_TUNNEL)
270 printf("/tunnel");
271 if (flags & DVMRP_NF_SRCRT)
272 printf("/srcrt");
273 if (flags & DVMRP_NF_PIM)
274 printf("/pim");
275 if (flags & DVMRP_NF_QUERIER)
276 printf("/querier");
277 if (flags & DVMRP_NF_DISABLED)
278 printf("/disabled");
279 if (flags & DVMRP_NF_DOWN)
280 printf("/down");
281 if (flags & DVMRP_NF_LEAF)
282 printf("/leaf");
283 printf("]\n");
284 }
285 }
286 }
287
288 int
289 get_number(var, deflt, pargv, pargc)
290 int *var, *pargc, deflt;
291 char ***pargv;
292 {
293 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
294 * argument */
295 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
296 (*pargv)++, (*pargc)--;
297 *var = atoi((*pargv)[0]);
298 return 1;
299 } else if (deflt >= 0) {
300 *var = deflt;
301 return 1;
302 } else
303 return 0;
304 } else { /* Get value from the rest of this argument */
305 if (isdigit((*pargv)[0][2])) {
306 *var = atoi((*pargv)[0] + 2);
307 return 1;
308 } else {
309 return 0;
310 }
311 }
312 }
313
314 void
315 usage()
316 {
317 fprintf(stderr,
318 "Usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
319 exit(1);
320 }
321
322 int
323 main(argc, argv)
324 int argc;
325 char *argv[];
326 {
327 int tries;
328 int trynew;
329 struct timeval et;
330 struct hostent *hp;
331 struct hostent bogus;
332 char *host;
333 int curaddr;
334
335 if (geteuid() != 0) {
336 fprintf(stderr, "mrinfo: must be root\n");
337 exit(1);
338 }
339 init_igmp();
340 if (setuid(getuid()) == -1)
341 log(LOG_ERR, errno, "setuid");
342
343 setlinebuf(stderr);
344
345 argv++, argc--;
346 while (argc > 0 && argv[0][0] == '-') {
347 switch (argv[0][1]) {
348 case 'd':
349 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
350 usage();
351 break;
352 case 'n':
353 ++nflag;
354 break;
355 case 'r':
356 if (!get_number(&retries, -1, &argv, &argc))
357 usage();
358 break;
359 case 't':
360 if (!get_number(&timeout, -1, &argv, &argc))
361 usage();
362 break;
363 default:
364 usage();
365 }
366 argv++, argc--;
367 }
368 if (argc > 1)
369 usage();
370 if (argc == 1)
371 host = argv[0];
372 else
373 host = "127.0.0.1";
374
375 if ((target_addr = inet_addr(host)) != -1) {
376 hp = &bogus;
377 hp->h_length = sizeof(target_addr);
378 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
379 if (hp->h_addr_list == NULL)
380 log(LOG_ERR, errno, "malloc");
381 hp->h_addr_list[0] = malloc(hp->h_length);
382 if (hp->h_addr_list[0] == NULL)
383 log(LOG_ERR, errno, "malloc");
384 memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
385 hp->h_addr_list[1] = NULL;
386 } else
387 hp = gethostbyname(host);
388
389 if (hp == NULL || hp->h_length != sizeof(target_addr)) {
390 fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
391 exit(1);
392 }
393 if (debug)
394 fprintf(stderr, "Debug level %u\n", debug);
395
396 /* Check all addresses; mrouters often have unreachable interfaces */
397 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
398 memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
399 { /* Find a good local address for us. */
400 int udp;
401 struct sockaddr_in addr;
402 int addrlen = sizeof(addr);
403
404 addr.sin_family = AF_INET;
405 #if (defined(BSD) && (BSD >= 199103))
406 addr.sin_len = sizeof addr;
407 #endif
408 addr.sin_addr.s_addr = target_addr;
409 addr.sin_port = htons(2000); /* any port over 1024 will
410 * do... */
411 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
412 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
413 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
414 perror("Determining local address");
415 exit(-1);
416 }
417 close(udp);
418 our_addr = addr.sin_addr.s_addr;
419 }
420
421 tries = 0;
422 trynew = 1;
423 /*
424 * New strategy: send 'ask2' for two timeouts, then fall back
425 * to 'ask', since it's not very likely that we are going to
426 * find someone who only responds to 'ask' these days
427 */
428 ask2(target_addr);
429
430 gettimeofday(&et, 0);
431 et.tv_sec += timeout;
432
433 /* Main receive loop */
434 for (;;) {
435 fd_set fds;
436 struct timeval tv, now;
437 int count, recvlen, dummy = 0;
438 register u_int32_t src, dst, group;
439 struct ip *ip;
440 struct igmp *igmp;
441 int ipdatalen, iphdrlen, igmpdatalen;
442
443 FD_ZERO(&fds);
444 if (igmp_socket >= FD_SETSIZE)
445 log(LOG_ERR, 0, "descriptor too big");
446 FD_SET(igmp_socket, &fds);
447
448 gettimeofday(&now, 0);
449 tv.tv_sec = et.tv_sec - now.tv_sec;
450 tv.tv_usec = et.tv_usec - now.tv_usec;
451
452 if (tv.tv_usec < 0) {
453 tv.tv_usec += 1000000L;
454 --tv.tv_sec;
455 }
456 if (tv.tv_sec < 0)
457 tv.tv_sec = tv.tv_usec = 0;
458
459 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
460
461 if (count < 0) {
462 if (errno != EINTR)
463 perror("select");
464 continue;
465 } else if (count == 0) {
466 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
467 if (++tries > retries)
468 break;
469 /* If we've tried ASK_NEIGHBORS2 twice with
470 * no response, fall back to ASK_NEIGHBORS
471 */
472 if (tries == 2 && target_level == 0)
473 trynew = 0;
474 if (target_level == 0 && trynew == 0)
475 ask(target_addr);
476 else
477 ask2(target_addr);
478 gettimeofday(&et, 0);
479 et.tv_sec += timeout;
480 continue;
481 }
482 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
483 0, NULL, &dummy);
484 if (recvlen <= 0) {
485 if (recvlen && errno != EINTR)
486 perror("recvfrom");
487 continue;
488 }
489
490 if (recvlen < sizeof(struct ip)) {
491 log(LOG_WARNING, 0,
492 "packet too short (%u bytes) for IP header",
493 recvlen);
494 continue;
495 }
496 ip = (struct ip *) recv_buf;
497 if (ip->ip_p == 0)
498 continue; /* Request to install cache entry */
499 src = ip->ip_src.s_addr;
500 dst = ip->ip_dst.s_addr;
501 iphdrlen = ip->ip_hl << 2;
502 ipdatalen = ip->ip_len;
503 if (iphdrlen + ipdatalen != recvlen) {
504 log(LOG_WARNING, 0,
505 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
506 recvlen, iphdrlen, ipdatalen);
507 continue;
508 }
509 igmp = (struct igmp *) (recv_buf + iphdrlen);
510 group = igmp->igmp_group.s_addr;
511 igmpdatalen = ipdatalen - IGMP_MINLEN;
512 if (igmpdatalen < 0) {
513 log(LOG_WARNING, 0,
514 "IP data field too short (%u bytes) for IGMP, from %s",
515 ipdatalen, inet_fmt(src, s1));
516 continue;
517 }
518 if (igmp->igmp_type != IGMP_DVMRP)
519 continue;
520
521 switch (igmp->igmp_code) {
522 case DVMRP_NEIGHBORS:
523 case DVMRP_NEIGHBORS2:
524 if (src != target_addr) {
525 fprintf(stderr, "mrinfo: got reply from %s",
526 inet_fmt(src, s1));
527 fprintf(stderr, " instead of %s\n",
528 inet_fmt(target_addr, s1));
529 /*continue;*/
530 }
531 break;
532 default:
533 continue; /* ignore all other DVMRP messages */
534 }
535
536 switch (igmp->igmp_code) {
537
538 case DVMRP_NEIGHBORS:
539 if (group) {
540 /* knows about DVMRP_NEIGHBORS2 msg */
541 if (target_level == 0) {
542 target_level = ntohl(group);
543 ask2(target_addr);
544 }
545 } else {
546 accept_neighbors(src, dst, (u_char *)(igmp + 1),
547 igmpdatalen, ntohl(group));
548 exit(0);
549 }
550 break;
551
552 case DVMRP_NEIGHBORS2:
553 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
554 igmpdatalen, ntohl(group));
555 exit(0);
556 }
557 }
558 }
559 exit(1);
560 }
561
562 /* dummies */
563 void accept_probe(src, dst, p, datalen, level)
564 u_int32_t src, dst, level;
565 char *p;
566 int datalen;
567 {
568 }
569 void accept_group_report(src, dst, group, r_type)
570 u_int32_t src, dst, group;
571 int r_type;
572 {
573 }
574 void accept_neighbor_request2(src, dst)
575 u_int32_t src, dst;
576 {
577 }
578 void accept_report(src, dst, p, datalen, level)
579 u_int32_t src, dst, level;
580 char *p;
581 int datalen;
582 {
583 }
584 void accept_neighbor_request(src, dst)
585 u_int32_t src, dst;
586 {
587 }
588 void accept_prune(src, dst, p, datalen)
589 u_int32_t src, dst;
590 char *p;
591 int datalen;
592 {
593 }
594 void accept_graft(src, dst, p, datalen)
595 u_int32_t src, dst;
596 char *p;
597 int datalen;
598 {
599 }
600 void accept_g_ack(src, dst, p, datalen)
601 u_int32_t src, dst;
602 char *p;
603 int datalen;
604 {
605 }
606 void add_table_entry(origin, mcastgrp)
607 u_int32_t origin, mcastgrp;
608 {
609 }
610 void check_vif_state()
611 {
612 }
613 void accept_leave_message(src, dst, group)
614 u_int32_t src, dst, group;
615 {
616 }
617 void accept_mtrace(src, dst, group, data, no, datalen)
618 u_int32_t src, dst, group;
619 char *data;
620 u_int no;
621 int datalen;
622 {
623 }
624 void accept_membership_query(src, dst, group, tmo)
625 u_int32_t src, dst, group;
626 int tmo;
627 {
628 }
629 void accept_info_request(src, dst, p, datalen)
630 u_int32_t src, dst;
631 u_char *p;
632 int datalen;
633 {
634 }
635 void accept_info_reply(src, dst, p, datalen)
636 u_int32_t src, dst;
637 u_char *p;
638 int datalen;
639 {
640 }
641