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