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