2 * This tool requests configuration info from a multicast router
3 * and prints the reply (if any). Invoke it as:
5 * mrinfo router-name-or-address
7 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
8 * multicast mapper written by Pavel Curtis).
10 * The lawyers insist we include the following UC copyright notice.
11 * The mapper from which this is derived contained a Xerox copyright
12 * notice which follows the UC one. Try not to get depressed noting
13 * that the legal gibberish is larger than the program.
15 * Copyright (c) 1993 Regents of the University of California.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the Computer Systems
29 * Engineering Group at Lawrence Berkeley Laboratory.
30 * 4. Neither the name of the University nor of the Laboratory may be used
31 * to endorse or promote products derived from this software without
32 * specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * ---------------------------------
46 * Copyright (c) Xerox Corporation 1992. All rights reserved.
48 * License is granted to copy, to use, and to make and to use derivative works
49 * for research and evaluation purposes, provided that Xerox is acknowledged
50 * in all documentation pertaining to any such copy or derivative work. Xerox
51 * grants no other licenses expressed or implied. The Xerox trade name should
52 * not be used in any advertising without its written permission.
54 * XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
55 * MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE FOR
56 * ANY PARTICULAR PURPOSE. The software is provided "as is" without express
57 * or implied warranty of any kind.
59 * These notices must be retained in any copies of any part of this software.
61 * @(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp
62 * $FreeBSD: src/usr.sbin/mrouted/mrinfo.c,v 1.17.2.1 2002/09/12 16:27:49 nectar Exp $
63 * $DragonFly: src/usr.sbin/mrouted/mrinfo.c,v 1.4 2004/03/15 18:10:28 dillon Exp $
70 #include <arpa/inet.h>
73 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
74 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
76 u_int32 our_addr, target_addr = 0; /* in NET order */
79 int retries = DEFAULT_RETRIES;
80 int timeout = DEFAULT_TIMEOUT;
82 vifi_t numvifs; /* to keep loader happy */
83 /* (see COPY_TABLES macro called in kern.c) */
85 char * inet_name(u_int32 addr);
86 void ask(u_int32 dst);
87 void ask2(u_int32 dst);
88 int get_number(int *var, int deflt, char ***pargv,
90 u_int32 host_addr(char *name);
91 static void usage(void);
93 /* to shut up -Wstrict-prototypes */
94 int main(int argc, char **argv);
98 inet_name(u_int32 addr)
107 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
109 return (inet_ntoa(in));
115 * Log errors and other messages to stderr, according to the severity of the
116 * message and the current debug level. For errors of severity LOG_ERR or
117 * worse, terminate the program.
120 log(int severity, int syserr, char *format, ...)
125 va_start(ap, format);
128 if (severity > LOG_WARNING)
131 if (severity > LOG_NOTICE)
134 if (severity > LOG_INFO)
138 if (severity == LOG_WARNING)
139 strcpy(fmt, "warning - ");
140 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
141 fmt[sizeof(fmt)-1]='\0';
142 vfprintf(stderr, fmt, ap);
144 fprintf(stderr, "\n");
145 else if (syserr < sys_nerr)
146 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
148 fprintf(stderr, ": errno %d\n", syserr);
151 if (severity <= LOG_ERR)
156 * Send a neighbors-list request.
161 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
162 htonl(MROUTED_LEVEL), 0);
168 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
169 htonl(MROUTED_LEVEL), 0);
173 * Process an incoming neighbor-list message.
176 accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
178 u_char *ep = p + datalen;
179 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
180 a += ((u_int32)*p++ << 8), a += *p++)
182 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
190 laddr = htonl(laddr);
194 while (--ncount >= 0) {
198 neighbor = htonl(neighbor);
199 printf(" %s -> ", inet_fmt(laddr, s1));
200 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
201 inet_name(neighbor), metric, thresh);
207 accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
209 u_char *ep = p + datalen;
210 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
211 /* well, only possibly_broken_cisco, but that's too long to type. */
212 u_int majvers = level & 0xff;
213 u_int minvers = (level >> 8) & 0xff;
215 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
216 if (majvers == 3 && minvers == 0xff)
217 printf("DVMRPv3 compliant");
219 printf("version %d.%d", majvers, minvers);
227 u_int32 laddr = *(u_int32*)p;
234 if (broken_cisco && ncount == 0) /* dumb Ciscos */
236 if (broken_cisco && ncount > 15) /* dumb Ciscos */
237 ncount = ncount & 0xf;
238 while (--ncount >= 0 && p < ep) {
239 u_int32 neighbor = *(u_int32*)p;
242 printf(" %s -> ", inet_fmt(laddr, s1));
243 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
244 inet_name(neighbor), metric, thresh);
245 if (flags & DVMRP_NF_TUNNEL)
247 if (flags & DVMRP_NF_SRCRT)
249 if (flags & DVMRP_NF_PIM)
251 if (flags & DVMRP_NF_QUERIER)
253 if (flags & DVMRP_NF_DISABLED)
255 if (flags & DVMRP_NF_DOWN)
257 if (flags & DVMRP_NF_LEAF)
265 get_number(int *var, int deflt, char ***pargv, int *pargc)
267 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
269 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
270 (*pargv)++, (*pargc)--;
271 *var = atoi((*pargv)[0]);
273 } else if (deflt >= 0) {
278 } else { /* Get value from the rest of this argument */
279 if (isdigit((*pargv)[0][2])) {
280 *var = atoi((*pargv)[0] + 2);
292 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
297 main(int argc, char **argv)
303 struct hostent bogus;
308 errx(1, "must be root");
316 while (argc > 0 && argv[0][0] == '-') {
317 switch (argv[0][1]) {
319 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
326 if (!get_number(&retries, -1, &argv, &argc))
330 if (!get_number(&timeout, -1, &argv, &argc))
345 if ((target_addr = inet_addr(host)) != -1) {
347 hp->h_length = sizeof(target_addr);
348 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
349 hp->h_addr_list[0] = malloc(hp->h_length);
350 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
351 hp->h_addr_list[1] = 0;
353 hp = gethostbyname(host);
355 if (hp == NULL || hp->h_length != sizeof(target_addr))
356 errx(1, "%s: no such host", argv[0]);
358 fprintf(stderr, "Debug level %u\n", debug);
360 /* Check all addresses; mrouters often have unreachable interfaces */
361 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
362 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
363 { /* Find a good local address for us. */
365 struct sockaddr_in addr;
366 int addrlen = sizeof(addr);
368 addr.sin_family = AF_INET;
370 addr.sin_len = sizeof addr;
372 addr.sin_addr.s_addr = target_addr;
373 addr.sin_port = htons(2000); /* any port over 1024 will
375 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
376 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
377 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
378 err(-1, "determining local address");
380 our_addr = addr.sin_addr.s_addr;
386 * New strategy: send 'ask2' for two timeouts, then fall back
387 * to 'ask', since it's not very likely that we are going to
388 * find someone who only responds to 'ask' these days
392 gettimeofday(&et, 0);
393 et.tv_sec += timeout;
395 /* Main receive loop */
398 struct timeval tv, now;
399 int count, recvlen, dummy = 0;
400 u_int32 src, dst, group;
403 int ipdatalen, iphdrlen, igmpdatalen;
405 if (igmp_socket >= FD_SETSIZE)
406 log(LOG_ERR, 0, "descriptor too big");
408 FD_SET(igmp_socket, &fds);
410 gettimeofday(&now, 0);
411 tv.tv_sec = et.tv_sec - now.tv_sec;
412 tv.tv_usec = et.tv_usec - now.tv_usec;
414 if (tv.tv_usec < 0) {
415 tv.tv_usec += 1000000L;
419 tv.tv_sec = tv.tv_usec = 0;
421 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
427 } else if (count == 0) {
428 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
429 if (++tries > retries)
431 /* If we've tried ASK_NEIGHBORS2 twice with
432 * no response, fall back to ASK_NEIGHBORS
434 if (tries == 2 && target_level == 0)
436 if (target_level == 0 && trynew == 0)
440 gettimeofday(&et, 0);
441 et.tv_sec += timeout;
444 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
447 if (recvlen && errno != EINTR)
452 if (recvlen < sizeof(struct ip)) {
454 "packet too short (%u bytes) for IP header",
458 ip = (struct ip *) recv_buf;
460 continue; /* Request to install cache entry */
461 src = ip->ip_src.s_addr;
462 dst = ip->ip_dst.s_addr;
463 iphdrlen = ip->ip_hl << 2;
464 #ifdef RAW_INPUT_IS_RAW
465 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
467 ipdatalen = ip->ip_len;
469 if (iphdrlen + ipdatalen != recvlen) {
471 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
472 recvlen, iphdrlen, ipdatalen);
475 igmp = (struct igmp *) (recv_buf + iphdrlen);
476 group = igmp->igmp_group.s_addr;
477 igmpdatalen = ipdatalen - IGMP_MINLEN;
478 if (igmpdatalen < 0) {
480 "IP data field too short (%u bytes) for IGMP, from %s",
481 ipdatalen, inet_fmt(src, s1));
484 if (igmp->igmp_type != IGMP_DVMRP)
487 switch (igmp->igmp_code) {
488 case DVMRP_NEIGHBORS:
489 case DVMRP_NEIGHBORS2:
490 if (src != target_addr) {
491 warnx("got reply from %s instead of %s",
492 inet_fmt(src, s1), inet_fmt(target_addr, s1));
497 continue; /* ignore all other DVMRP messages */
500 switch (igmp->igmp_code) {
502 case DVMRP_NEIGHBORS:
504 /* knows about DVMRP_NEIGHBORS2 msg */
505 if (target_level == 0) {
506 target_level = ntohl(group);
510 accept_neighbors(src, dst, (u_char *)(igmp + 1),
511 igmpdatalen, ntohl(group));
516 case DVMRP_NEIGHBORS2:
517 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
518 igmpdatalen, ntohl(group));
528 accept_probe(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
533 accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
538 accept_neighbor_request2(u_int32 src, u_int32 dst)
543 accept_report(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
548 accept_neighbor_request(u_int32 src, u_int32 dst)
553 accept_prune(u_int32 src, u_int32 dst, char *p, int datalen)
558 accept_graft(u_int32 src, u_int32 dst, char *p, int datalen)
563 accept_g_ack(u_int32 src, u_int32 dst, char *p, int datalen)
568 add_table_entry(u_int32 origin, u_int32 mcastgrp)
573 check_vif_state(void)
578 accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
583 accept_mtrace(u_int32 src, u_int32 dst, u_int32 group,
584 char *data, u_int no, int datalen)
589 accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
594 accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
599 accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)