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 $
69 #include <arpa/inet.h>
72 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
73 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
75 u_int32 our_addr, target_addr = 0; /* in NET order */
78 int retries = DEFAULT_RETRIES;
79 int timeout = DEFAULT_TIMEOUT;
81 vifi_t numvifs; /* to keep loader happy */
82 /* (see COPY_TABLES macro called in kern.c) */
84 char * inet_name(u_int32 addr);
85 void ask(u_int32 dst);
86 void ask2(u_int32 dst);
87 int get_number(int *var, int deflt, char ***pargv,
89 u_int32 host_addr(char *name);
90 static void usage(void);
93 inet_name(u_int32 addr)
102 (e = gethostbyaddr(&addr, sizeof(addr), AF_INET)) == NULL) {
104 return (inet_ntoa(in));
110 * Log errors and other messages to stderr, according to the severity of the
111 * message and the current debug level. For errors of severity LOG_ERR or
112 * worse, terminate the program.
115 dolog(int severity, int syserr, char *format, ...)
120 va_start(ap, format);
123 if (severity > LOG_WARNING)
126 if (severity > LOG_NOTICE)
129 if (severity > LOG_INFO)
133 if (severity == LOG_WARNING)
134 strcpy(fmt, "warning - ");
135 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
136 fmt[sizeof(fmt)-1]='\0';
137 vfprintf(stderr, fmt, ap);
139 fprintf(stderr, "\n");
140 else if (syserr < sys_nerr)
141 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
143 fprintf(stderr, ": errno %d\n", syserr);
146 if (severity <= LOG_ERR)
151 * Send a neighbors-list request.
156 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
157 htonl(MROUTED_LEVEL), 0);
163 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
164 htonl(MROUTED_LEVEL), 0);
168 * Process an incoming neighbor-list message.
171 accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
173 u_char *ep = p + datalen;
174 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
175 a += ((u_int32)*p++ << 8), a += *p++)
177 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
185 laddr = htonl(laddr);
189 while (--ncount >= 0) {
193 neighbor = htonl(neighbor);
194 printf(" %s -> ", inet_fmt(laddr, s1));
195 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
196 inet_name(neighbor), metric, thresh);
202 accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
204 u_char *ep = p + datalen;
205 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
206 /* well, only possibly_broken_cisco, but that's too long to type. */
207 u_int majvers = level & 0xff;
208 u_int minvers = (level >> 8) & 0xff;
210 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
211 if (majvers == 3 && minvers == 0xff)
212 printf("DVMRPv3 compliant");
214 printf("version %d.%d", majvers, minvers);
222 u_int32 laddr = *(u_int32*)p;
229 if (broken_cisco && ncount == 0) /* dumb Ciscos */
231 if (broken_cisco && ncount > 15) /* dumb Ciscos */
232 ncount = ncount & 0xf;
233 while (--ncount >= 0 && p < ep) {
234 u_int32 neighbor = *(u_int32*)p;
237 printf(" %s -> ", inet_fmt(laddr, s1));
238 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
239 inet_name(neighbor), metric, thresh);
240 if (flags & DVMRP_NF_TUNNEL)
242 if (flags & DVMRP_NF_SRCRT)
244 if (flags & DVMRP_NF_PIM)
246 if (flags & DVMRP_NF_QUERIER)
248 if (flags & DVMRP_NF_DISABLED)
250 if (flags & DVMRP_NF_DOWN)
252 if (flags & DVMRP_NF_LEAF)
260 get_number(int *var, int deflt, char ***pargv, int *pargc)
262 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
264 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
265 (*pargv)++, (*pargc)--;
266 *var = atoi((*pargv)[0]);
268 } else if (deflt >= 0) {
273 } else { /* Get value from the rest of this argument */
274 if (isdigit((*pargv)[0][2])) {
275 *var = atoi((*pargv)[0] + 2);
287 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
292 main(int argc, char **argv)
298 struct hostent bogus;
303 errx(1, "must be root");
311 while (argc > 0 && argv[0][0] == '-') {
312 switch (argv[0][1]) {
314 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
321 if (!get_number(&retries, -1, &argv, &argc))
325 if (!get_number(&timeout, -1, &argv, &argc))
340 if ((target_addr = inet_addr(host)) != -1) {
342 hp->h_length = sizeof(target_addr);
343 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
344 hp->h_addr_list[0] = malloc(hp->h_length);
345 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
346 hp->h_addr_list[1] = 0;
348 hp = gethostbyname(host);
350 if (hp == NULL || hp->h_length != sizeof(target_addr))
351 errx(1, "%s: no such host", argv[0]);
353 fprintf(stderr, "Debug level %u\n", debug);
355 /* Check all addresses; mrouters often have unreachable interfaces */
356 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
357 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
358 { /* Find a good local address for us. */
360 struct sockaddr_in addr;
361 int addrlen = sizeof(addr);
363 addr.sin_family = AF_INET;
365 addr.sin_len = sizeof addr;
367 addr.sin_addr.s_addr = target_addr;
368 addr.sin_port = htons(2000); /* any port over 1024 will
370 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
371 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
372 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
373 err(-1, "determining local address");
375 our_addr = addr.sin_addr.s_addr;
381 * New strategy: send 'ask2' for two timeouts, then fall back
382 * to 'ask', since it's not very likely that we are going to
383 * find someone who only responds to 'ask' these days
387 gettimeofday(&et, 0);
388 et.tv_sec += timeout;
390 /* Main receive loop */
393 struct timeval tv, now;
394 int count, recvlen, dummy = 0;
395 u_int32 src, dst, group;
398 int ipdatalen, iphdrlen, igmpdatalen;
400 if (igmp_socket >= FD_SETSIZE)
401 dolog(LOG_ERR, 0, "descriptor too big");
403 FD_SET(igmp_socket, &fds);
405 gettimeofday(&now, 0);
406 tv.tv_sec = et.tv_sec - now.tv_sec;
407 tv.tv_usec = et.tv_usec - now.tv_usec;
409 if (tv.tv_usec < 0) {
410 tv.tv_usec += 1000000L;
414 tv.tv_sec = tv.tv_usec = 0;
416 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
422 } else if (count == 0) {
423 dolog(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
424 if (++tries > retries)
426 /* If we've tried ASK_NEIGHBORS2 twice with
427 * no response, fall back to ASK_NEIGHBORS
429 if (tries == 2 && target_level == 0)
431 if (target_level == 0 && trynew == 0)
435 gettimeofday(&et, 0);
436 et.tv_sec += timeout;
439 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
442 if (recvlen && errno != EINTR)
447 if (recvlen < sizeof(struct ip)) {
448 dolog(LOG_WARNING, 0,
449 "packet too short (%u bytes) for IP header",
453 ip = (struct ip *) recv_buf;
455 continue; /* Request to install cache entry */
456 src = ip->ip_src.s_addr;
457 dst = ip->ip_dst.s_addr;
458 iphdrlen = ip->ip_hl << 2;
459 #ifdef RAW_INPUT_IS_RAW
460 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
462 ipdatalen = ip->ip_len;
464 if (iphdrlen + ipdatalen != recvlen) {
465 dolog(LOG_WARNING, 0,
466 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
467 recvlen, iphdrlen, ipdatalen);
470 igmp = (struct igmp *) (recv_buf + iphdrlen);
471 group = igmp->igmp_group.s_addr;
472 igmpdatalen = ipdatalen - IGMP_MINLEN;
473 if (igmpdatalen < 0) {
474 dolog(LOG_WARNING, 0,
475 "IP data field too short (%u bytes) for IGMP, from %s",
476 ipdatalen, inet_fmt(src, s1));
479 if (igmp->igmp_type != IGMP_DVMRP)
482 switch (igmp->igmp_code) {
483 case DVMRP_NEIGHBORS:
484 case DVMRP_NEIGHBORS2:
485 if (src != target_addr) {
486 warnx("got reply from %s instead of %s",
487 inet_fmt(src, s1), inet_fmt(target_addr, s1));
492 continue; /* ignore all other DVMRP messages */
495 switch (igmp->igmp_code) {
497 case DVMRP_NEIGHBORS:
499 /* knows about DVMRP_NEIGHBORS2 msg */
500 if (target_level == 0) {
501 target_level = ntohl(group);
505 accept_neighbors(src, dst, (u_char *)(igmp + 1),
506 igmpdatalen, ntohl(group));
511 case DVMRP_NEIGHBORS2:
512 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
513 igmpdatalen, ntohl(group));
523 accept_probe(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
528 accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
533 accept_neighbor_request2(u_int32 src, u_int32 dst)
538 accept_report(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
543 accept_neighbor_request(u_int32 src, u_int32 dst)
548 accept_prune(u_int32 src, u_int32 dst, char *p, int datalen)
553 accept_graft(u_int32 src, u_int32 dst, char *p, int datalen)
558 accept_g_ack(u_int32 src, u_int32 dst, char *p, int datalen)
563 add_table_entry(u_int32 origin, u_int32 mcastgrp)
568 check_vif_state(void)
573 accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
578 accept_mtrace(u_int32 src, u_int32 dst, u_int32 group,
579 char *data, u_int no, int datalen)
584 accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
589 accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
594 accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)