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