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.2 2003/06/17 04:29:57 dillon Exp $
70 #include <arpa/inet.h>
77 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
78 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
80 u_int32 our_addr, target_addr = 0; /* in NET order */
83 int retries = DEFAULT_RETRIES;
84 int timeout = DEFAULT_TIMEOUT;
86 vifi_t numvifs; /* to keep loader happy */
87 /* (see COPY_TABLES macro called in kern.c) */
89 char * inet_name __P((u_int32 addr));
90 void ask __P((u_int32 dst));
91 void ask2 __P((u_int32 dst));
92 int get_number __P((int *var, int deflt, char ***pargv,
94 u_int32 host_addr __P((char *name));
95 static void usage __P((void));
97 /* to shut up -Wstrict-prototypes */
98 int main __P((int argc, char *argv[]));
112 (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
114 return (inet_ntoa(in));
120 * Log errors and other messages to stderr, according to the severity of the
121 * message and the current debug level. For errors of severity LOG_ERR or
122 * worse, terminate the program.
126 log(int severity, int syserr, char *format, ...)
131 va_start(ap, format);
134 log(severity, syserr, format, va_alist)
135 int severity, syserr;
146 if (severity > LOG_WARNING)
149 if (severity > LOG_NOTICE)
152 if (severity > LOG_INFO)
156 if (severity == LOG_WARNING)
157 strcpy(fmt, "warning - ");
158 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
159 fmt[sizeof(fmt)-1]='\0';
160 vfprintf(stderr, fmt, ap);
162 fprintf(stderr, "\n");
163 else if (syserr < sys_nerr)
164 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
166 fprintf(stderr, ": errno %d\n", syserr);
169 if (severity <= LOG_ERR)
174 * Send a neighbors-list request.
180 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
181 htonl(MROUTED_LEVEL), 0);
188 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
189 htonl(MROUTED_LEVEL), 0);
193 * Process an incoming neighbor-list message.
196 accept_neighbors(src, dst, p, datalen, level)
197 u_int32 src, dst, level;
201 u_char *ep = p + datalen;
202 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
203 a += ((u_int32)*p++ << 8), a += *p++)
205 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
207 register u_int32 laddr;
208 register u_char metric;
209 register u_char thresh;
213 laddr = htonl(laddr);
217 while (--ncount >= 0) {
218 register u_int32 neighbor;
220 neighbor = htonl(neighbor);
221 printf(" %s -> ", inet_fmt(laddr, s1));
222 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
223 inet_name(neighbor), metric, thresh);
229 accept_neighbors2(src, dst, p, datalen, level)
230 u_int32 src, dst, level;
234 u_char *ep = p + datalen;
235 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
236 /* well, only possibly_broken_cisco, but that's too long to type. */
237 u_int majvers = level & 0xff;
238 u_int minvers = (level >> 8) & 0xff;
240 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
241 if (majvers == 3 && minvers == 0xff)
242 printf("DVMRPv3 compliant");
244 printf("version %d.%d", majvers, minvers);
248 register u_char metric;
249 register u_char thresh;
250 register u_char flags;
252 register u_int32 laddr = *(u_int32*)p;
259 if (broken_cisco && ncount == 0) /* dumb Ciscos */
261 if (broken_cisco && ncount > 15) /* dumb Ciscos */
262 ncount = ncount & 0xf;
263 while (--ncount >= 0 && p < ep) {
264 register u_int32 neighbor = *(u_int32*)p;
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)
271 if (flags & DVMRP_NF_SRCRT)
273 if (flags & DVMRP_NF_PIM)
275 if (flags & DVMRP_NF_QUERIER)
277 if (flags & DVMRP_NF_DISABLED)
279 if (flags & DVMRP_NF_DOWN)
281 if (flags & DVMRP_NF_LEAF)
289 get_number(var, deflt, pargv, pargc)
290 int *var, *pargc, deflt;
293 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
295 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
296 (*pargv)++, (*pargc)--;
297 *var = atoi((*pargv)[0]);
299 } else if (deflt >= 0) {
304 } else { /* Get value from the rest of this argument */
305 if (isdigit((*pargv)[0][2])) {
306 *var = atoi((*pargv)[0] + 2);
318 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
331 struct hostent bogus;
336 errx(1, "must be root");
344 while (argc > 0 && argv[0][0] == '-') {
345 switch (argv[0][1]) {
347 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
354 if (!get_number(&retries, -1, &argv, &argc))
358 if (!get_number(&timeout, -1, &argv, &argc))
373 if ((target_addr = inet_addr(host)) != -1) {
375 hp->h_length = sizeof(target_addr);
376 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
377 hp->h_addr_list[0] = malloc(hp->h_length);
378 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
379 hp->h_addr_list[1] = 0;
381 hp = gethostbyname(host);
383 if (hp == NULL || hp->h_length != sizeof(target_addr))
384 errx(1, "%s: no such host", argv[0]);
386 fprintf(stderr, "Debug level %u\n", debug);
388 /* Check all addresses; mrouters often have unreachable interfaces */
389 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
390 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
391 { /* Find a good local address for us. */
393 struct sockaddr_in addr;
394 int addrlen = sizeof(addr);
396 addr.sin_family = AF_INET;
398 addr.sin_len = sizeof addr;
400 addr.sin_addr.s_addr = target_addr;
401 addr.sin_port = htons(2000); /* any port over 1024 will
403 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
404 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
405 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
406 err(-1, "determining local address");
408 our_addr = addr.sin_addr.s_addr;
414 * New strategy: send 'ask2' for two timeouts, then fall back
415 * to 'ask', since it's not very likely that we are going to
416 * find someone who only responds to 'ask' these days
420 gettimeofday(&et, 0);
421 et.tv_sec += timeout;
423 /* Main receive loop */
426 struct timeval tv, now;
427 int count, recvlen, dummy = 0;
428 register u_int32 src, dst, group;
431 int ipdatalen, iphdrlen, igmpdatalen;
433 if (igmp_socket >= FD_SETSIZE)
434 log(LOG_ERR, 0, "descriptor too big");
436 FD_SET(igmp_socket, &fds);
438 gettimeofday(&now, 0);
439 tv.tv_sec = et.tv_sec - now.tv_sec;
440 tv.tv_usec = et.tv_usec - now.tv_usec;
442 if (tv.tv_usec < 0) {
443 tv.tv_usec += 1000000L;
447 tv.tv_sec = tv.tv_usec = 0;
449 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
455 } else if (count == 0) {
456 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
457 if (++tries > retries)
459 /* If we've tried ASK_NEIGHBORS2 twice with
460 * no response, fall back to ASK_NEIGHBORS
462 if (tries == 2 && target_level == 0)
464 if (target_level == 0 && trynew == 0)
468 gettimeofday(&et, 0);
469 et.tv_sec += timeout;
472 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
475 if (recvlen && errno != EINTR)
480 if (recvlen < sizeof(struct ip)) {
482 "packet too short (%u bytes) for IP header",
486 ip = (struct ip *) recv_buf;
488 continue; /* Request to install cache entry */
489 src = ip->ip_src.s_addr;
490 dst = ip->ip_dst.s_addr;
491 iphdrlen = ip->ip_hl << 2;
492 #ifdef RAW_INPUT_IS_RAW
493 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
495 ipdatalen = ip->ip_len;
497 if (iphdrlen + ipdatalen != recvlen) {
499 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
500 recvlen, iphdrlen, ipdatalen);
503 igmp = (struct igmp *) (recv_buf + iphdrlen);
504 group = igmp->igmp_group.s_addr;
505 igmpdatalen = ipdatalen - IGMP_MINLEN;
506 if (igmpdatalen < 0) {
508 "IP data field too short (%u bytes) for IGMP, from %s",
509 ipdatalen, inet_fmt(src, s1));
512 if (igmp->igmp_type != IGMP_DVMRP)
515 switch (igmp->igmp_code) {
516 case DVMRP_NEIGHBORS:
517 case DVMRP_NEIGHBORS2:
518 if (src != target_addr) {
519 warnx("got reply from %s instead of %s",
520 inet_fmt(src, s1), inet_fmt(target_addr, s1));
525 continue; /* ignore all other DVMRP messages */
528 switch (igmp->igmp_code) {
530 case DVMRP_NEIGHBORS:
532 /* knows about DVMRP_NEIGHBORS2 msg */
533 if (target_level == 0) {
534 target_level = ntohl(group);
538 accept_neighbors(src, dst, (u_char *)(igmp + 1),
539 igmpdatalen, ntohl(group));
544 case DVMRP_NEIGHBORS2:
545 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
546 igmpdatalen, ntohl(group));
555 void accept_probe(src, dst, p, datalen, level)
556 u_int32 src, dst, level;
561 void accept_group_report(src, dst, group, r_type)
562 u_int32 src, dst, group;
566 void accept_neighbor_request2(src, dst)
570 void accept_report(src, dst, p, datalen, level)
571 u_int32 src, dst, level;
576 void accept_neighbor_request(src, dst)
580 void accept_prune(src, dst, p, datalen)
586 void accept_graft(src, dst, p, datalen)
592 void accept_g_ack(src, dst, p, datalen)
598 void add_table_entry(origin, mcastgrp)
599 u_int32 origin, mcastgrp;
602 void check_vif_state()
605 void accept_leave_message(src, dst, group)
606 u_int32 src, dst, group;
609 void accept_mtrace(src, dst, group, data, no, datalen)
610 u_int32 src, dst, group;
616 void accept_membership_query(src, dst, group, tmo)
617 u_int32 src, dst, group;
621 void accept_info_request(src, dst, p, datalen)
627 void accept_info_reply(src, dst, p, datalen)