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