xref: /netbsd-src/usr.sbin/mrinfo/mrinfo.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: mrinfo.c,v 1.24 2004/10/30 08:56:00 dsl 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) 1992, 2001 Xerox Corporation.  All rights reserved.
49  *
50  * Redistribution and use in source and binary forms, with or without modification,
51  * are permitted provided that the following conditions are met:
52  *
53  * Redistributions of source code must retain the above copyright notice,
54  * this list of conditions and the following disclaimer.
55  *
56  * Redistributions in binary form must reproduce the above copyright notice,
57  * this list of conditions and the following disclaimer in the documentation
58  * and/or other materials provided with the distribution.
59 
60  * Neither name of the Xerox, PARC, nor the names of its contributors may be used
61  * to endorse or promote products derived from this software
62  * without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
65  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
66  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
67  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE XEROX CORPORATION OR CONTRIBUTORS
68  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
70  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
71  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
72  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
73  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
74  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75  */
76 
77 #include <sys/cdefs.h>
78 #ifndef lint
79 #if 0
80 static char rcsid[] =
81     "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
82 #else
83 __RCSID("$NetBSD: mrinfo.c,v 1.24 2004/10/30 08:56:00 dsl Exp $");
84 #endif
85 #endif
86 
87 #include <string.h>
88 #include <netdb.h>
89 #include <sys/time.h>
90 #include <poll.h>
91 #include "defs.h"
92 #include <arpa/inet.h>
93 #include <stdarg.h>
94 
95 #define DEFAULT_TIMEOUT	4	/* How long to wait before retrying requests */
96 #define DEFAULT_RETRIES 3	/* How many times to ask each router */
97 
98 u_int32_t	our_addr, target_addr = 0;	/* in NET order */
99 int     debug = 0;
100 int	nflag = 0;
101 int     retries = DEFAULT_RETRIES;
102 int     timeout = DEFAULT_TIMEOUT;
103 int	target_level = 0;
104 vifi_t  numvifs;		/* to keep loader happy */
105 				/* (see COPY_TABLES macro called in kern.c) */
106 
107 char *			inet_name(u_int32_t addr);
108 void			ask(u_int32_t dst);
109 void			ask2(u_int32_t dst);
110 int			get_number(int *var, int deflt, char ***pargv,
111 				   int *pargc);
112 u_int32_t		host_addr(char *name);
113 void			usage(void);
114 
115 /* to shut up -Wstrict-prototypes */
116 int			main(int argc, char *argv[]);
117 /* logit() prototyped in defs.h */
118 
119 
120 char   *
121 inet_name(u_int32_t addr)
122 {
123 	struct hostent *e;
124 	struct in_addr in;
125 
126 	if (addr == 0)
127 		return "local";
128 
129 	if (nflag ||
130 	    (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
131 		in.s_addr = addr;
132 		return (inet_ntoa(in));
133 	}
134 	return (e->h_name);
135 }
136 
137 /*
138  * Log errors and other messages to stderr, according to the severity of the
139  * message and the current debug level.  For errors of severity LOG_ERR or
140  * worse, terminate the program.
141  */
142 void
143 logit(int severity, int syserr, const char *format, ...)
144 {
145 	va_list ap;
146 
147 	switch (debug) {
148 	case 0:
149 		if (severity > LOG_WARNING)
150 			return;
151 	case 1:
152 		if (severity > LOG_NOTICE)
153 			return;
154 	case 2:
155 		if (severity > LOG_INFO)
156 			return;
157 	default:
158 		if (severity == LOG_WARNING)
159 			fprintf(stderr, "warning - ");
160 		va_start(ap, format);
161 		vfprintf(stderr, format, ap);
162 		va_end(ap);
163 		if (syserr == 0)
164 			fprintf(stderr, "\n");
165 		else
166 			fprintf(stderr, ": %s\n", strerror(syserr));
167 	}
168 
169 	if (severity <= LOG_ERR)
170 		exit(1);
171 }
172 
173 /*
174  * Send a neighbors-list request.
175  */
176 void
177 ask(u_int32_t dst)
178 {
179 	send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
180 			htonl(MROUTED_LEVEL), 0);
181 }
182 
183 void
184 ask2(u_int32_t dst)
185 {
186 	send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
187 			htonl(MROUTED_LEVEL), 0);
188 }
189 
190 /*
191  * Process an incoming neighbor-list message.
192  */
193 void
194 accept_neighbors(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
195 		 u_int32_t level)
196 {
197 	u_char *ep = p + datalen;
198 #define GET_ADDR(a) (a = ((u_int32_t)*p++ << 24), a += ((u_int32_t)*p++ << 16),\
199 		     a += ((u_int32_t)*p++ << 8), a += *p++)
200 
201 	printf("%s (%s):\n", inet_fmt(src), inet_name(src));
202 	while (p < ep) {
203 		u_int32_t laddr;
204 		u_char metric;
205 		u_char thresh;
206 		int ncount;
207 
208 		GET_ADDR(laddr);
209 		laddr = htonl(laddr);
210 		metric = *p++;
211 		thresh = *p++;
212 		ncount = *p++;
213 		while (--ncount >= 0) {
214 			u_int32_t neighbor;
215 			GET_ADDR(neighbor);
216 			neighbor = htonl(neighbor);
217 			printf("  %s -> ", inet_fmt(laddr));
218 			printf("%s (%s) [%d/%d]\n",
219 				inet_fmt(neighbor),
220 			       inet_name(neighbor), metric, thresh);
221 		}
222 	}
223 }
224 
225 void
226 accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
227 		  u_int32_t level)
228 {
229 	u_char *ep = p + datalen;
230 	u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
231 	/* well, only possibly_broken_cisco, but that's too long to type. */
232 
233 	printf("%s (%s) [version %d.%d", inet_fmt(src),
234 		inet_name(src), level & 0xff, (level >> 8) & 0xff);
235 	if ((level >> 16) & NF_LEAF)   { printf (",leaf"); }
236 	if ((level >> 16) & NF_PRUNE)  { printf (",prune"); }
237 	if ((level >> 16) & NF_GENID)  { printf (",genid"); }
238 	if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
239 	printf ("]:\n");
240 
241 	while (p < ep) {
242 		u_char metric;
243 		u_char thresh;
244 		u_char flags;
245 		int ncount;
246 		u_int32_t laddr = *(u_int32_t*)p;
247 
248 		p += 4;
249 		metric = *p++;
250 		thresh = *p++;
251 		flags = *p++;
252 		ncount = *p++;
253 		if (broken_cisco && ncount == 0)	/* dumb Ciscos */
254 			ncount = 1;
255 		if (broken_cisco && ncount > 15)	/* dumb Ciscos */
256 			ncount = ncount & 0xf;
257 		while (--ncount >= 0 && p < ep) {
258 			u_int32_t neighbor = *(u_int32_t*)p;
259 			p += 4;
260 			printf("  %s -> ", inet_fmt(laddr));
261 			printf("%s (%s) [%d/%d",
262 				inet_fmt(neighbor),
263 				inet_name(neighbor), metric, thresh);
264 			if (flags & DVMRP_NF_TUNNEL)
265 				printf("/tunnel");
266 			if (flags & DVMRP_NF_SRCRT)
267 				printf("/srcrt");
268 			if (flags & DVMRP_NF_PIM)
269 				printf("/pim");
270 			if (flags & DVMRP_NF_QUERIER)
271 				printf("/querier");
272 			if (flags & DVMRP_NF_DISABLED)
273 				printf("/disabled");
274 			if (flags & DVMRP_NF_DOWN)
275 				printf("/down");
276 			if (flags & DVMRP_NF_LEAF)
277 				printf("/leaf");
278 			printf("]\n");
279 		}
280 	}
281 }
282 
283 int
284 get_number(int *var, int deflt, char ***pargv, int *pargc)
285 {
286 	if ((*pargv)[0][2] == '\0') {	/* Get the value from the next
287 					 * argument */
288 		if (*pargc > 1 && isdigit((unsigned char)(*pargv)[1][0])) {
289 			(*pargv)++, (*pargc)--;
290 			*var = atoi((*pargv)[0]);
291 			return 1;
292 		} else if (deflt >= 0) {
293 			*var = deflt;
294 			return 1;
295 		} else
296 			return 0;
297 	} else {		/* Get value from the rest of this argument */
298 		if (isdigit((unsigned char)(*pargv)[0][2])) {
299 			*var = atoi((*pargv)[0] + 2);
300 			return 1;
301 		} else {
302 			return 0;
303 		}
304 	}
305 }
306 
307 void
308 usage(void)
309 {
310 	fprintf(stderr,
311 	    "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
312 	exit(1);
313 }
314 
315 int
316 main(int argc, char *argv[])
317 {
318 	int tries;
319 	int trynew;
320 	struct timeval et;
321 	struct hostent *hp;
322 	struct hostent bogus;
323 	char *host;
324 	int curaddr;
325 
326 	if (geteuid() != 0) {
327 		fprintf(stderr, "mrinfo: must be root\n");
328 		exit(1);
329 	}
330 	init_igmp();
331 	if (setuid(getuid()) == -1)
332 		logit(LOG_ERR, errno, "setuid");
333 
334 	setlinebuf(stderr);
335 
336 	argv++, argc--;
337 	while (argc > 0 && argv[0][0] == '-') {
338 		switch (argv[0][1]) {
339 		case 'd':
340 			if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
341 				usage();
342 			break;
343 		case 'n':
344 			++nflag;
345 			break;
346 		case 'r':
347 			if (!get_number(&retries, -1, &argv, &argc))
348 				usage();
349 			break;
350 		case 't':
351 			if (!get_number(&timeout, -1, &argv, &argc))
352 				usage();
353 			break;
354 		default:
355 			usage();
356 		}
357 		argv++, argc--;
358 	}
359 	if (argc > 1)
360 		usage();
361 	if (argc == 1)
362 		host = argv[0];
363 	else
364 		host = "127.0.0.1";
365 
366 	if ((target_addr = inet_addr(host)) != -1) {
367 		hp = &bogus;
368 		hp->h_length = sizeof(target_addr);
369 		hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
370 		if (hp->h_addr_list == NULL)
371 			logit(LOG_ERR, errno, "malloc");
372 		hp->h_addr_list[0] = malloc(hp->h_length);
373 		if (hp->h_addr_list[0] == NULL)
374 			logit(LOG_ERR, errno, "malloc");
375 		memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
376 		hp->h_addr_list[1] = NULL;
377 	} else
378 		hp = gethostbyname(host);
379 
380 	if (hp == NULL || hp->h_length != sizeof(target_addr)) {
381 		fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
382 		exit(1);
383 	}
384 	if (debug)
385 		fprintf(stderr, "Debug level %u\n", debug);
386 
387 	/* Check all addresses; mrouters often have unreachable interfaces */
388 	for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
389 	    memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
390 	    {			/* Find a good local address for us. */
391 		int     udp;
392 		struct sockaddr_in addr;
393 		int     addrlen = sizeof(addr);
394 
395 		memset(&addr, 0, sizeof(addr));
396 		addr.sin_family = AF_INET;
397 #if (defined(BSD) && (BSD >= 199103))
398 		addr.sin_len = sizeof addr;
399 #endif
400 		addr.sin_addr.s_addr = target_addr;
401 		addr.sin_port = htons(2000);	/* any port over 1024 will
402 						 * do... */
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 			perror("Determining local address");
407 			exit(1);
408 		}
409 		close(udp);
410 		our_addr = addr.sin_addr.s_addr;
411 	    }
412 
413 	    tries = 0;
414 	    trynew = 1;
415 	    /*
416 	     * New strategy: send 'ask2' for two timeouts, then fall back
417 	     * to 'ask', since it's not very likely that we are going to
418 	     * find someone who only responds to 'ask' these days
419 	     */
420 	    ask2(target_addr);
421 
422 	    gettimeofday(&et, 0);
423 	    et.tv_sec += timeout;
424 
425 	    /* Main receive loop */
426 	    for (;;) {
427 		struct pollfd set[1];
428 		struct timeval tv, now;
429 		int     count, recvlen, dummy = 0;
430 		u_int32_t src, dst, group;
431 		struct ip *ip;
432 		struct igmp *igmp;
433 		int     ipdatalen, iphdrlen, igmpdatalen;
434 
435 		set[0].fd = igmp_socket;
436 		set[0].events = POLLIN;
437 
438 		gettimeofday(&now, 0);
439 		tv.tv_sec = et.tv_sec - now.tv_sec;
440 		tv.tv_usec = et.tv_usec - now.tv_usec;
441 
442 		if (tv.tv_usec < 0) {
443 			tv.tv_usec += 1000000L;
444 			--tv.tv_sec;
445 		}
446 		if (tv.tv_sec < 0)
447 			tv.tv_sec = tv.tv_usec = 0;
448 
449 		count = poll(set, 1, tv.tv_sec * 1000 + tv.tv_usec / 1000);
450 
451 		if (count < 0) {
452 			if (errno != EINTR)
453 				perror("select");
454 			continue;
455 		} else if (count == 0) {
456 			logit(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
457 			if (++tries > retries)
458 				break;
459 			/* If we've tried ASK_NEIGHBORS2 twice with
460 			 * no response, fall back to ASK_NEIGHBORS
461 			 */
462 			if (tries == 2 && target_level == 0)
463 				trynew = 0;
464 			if (target_level == 0 && trynew == 0)
465 				ask(target_addr);
466 			else
467 				ask2(target_addr);
468 			gettimeofday(&et, 0);
469 			et.tv_sec += timeout;
470 			continue;
471 		}
472 		recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
473 				   0, NULL, &dummy);
474 		if (recvlen <= 0) {
475 			if (recvlen && errno != EINTR)
476 				perror("recvfrom");
477 			continue;
478 		}
479 
480 		if (recvlen < sizeof(struct ip)) {
481 			logit(LOG_WARNING, 0,
482 			    "packet too short (%u bytes) for IP header",
483 			    recvlen);
484 			continue;
485 		}
486 		ip = (struct ip *) recv_buf;
487 		if (ip->ip_p == 0)
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 		ipdatalen = ip->ip_len;
493 		if (iphdrlen + ipdatalen != recvlen) {
494 		    logit(LOG_WARNING, 0,
495 		      "packet shorter (%u bytes) than hdr+data length (%u+%u)",
496 		      recvlen, iphdrlen, ipdatalen);
497 		    continue;
498 		}
499 		igmp = (struct igmp *) (recv_buf + iphdrlen);
500 		group = igmp->igmp_group.s_addr;
501 		igmpdatalen = ipdatalen - IGMP_MINLEN;
502 		if (igmpdatalen < 0) {
503 		    logit(LOG_WARNING, 0,
504 			"IP data field too short (%u bytes) for IGMP, from %s",
505 			ipdatalen, inet_fmt(src));
506 		    continue;
507 		}
508 		if (igmp->igmp_type != IGMP_DVMRP)
509 			continue;
510 
511 		switch (igmp->igmp_code) {
512 		case DVMRP_NEIGHBORS:
513 		case DVMRP_NEIGHBORS2:
514 			if (src != target_addr) {
515 				fprintf(stderr, "mrinfo: got reply from %s",
516 					inet_fmt(src));
517 				fprintf(stderr, " instead of %s\n",
518 					inet_fmt(target_addr));
519 				/*continue;*/
520 			}
521 			break;
522 		default:
523 			continue;	/* ignore all other DVMRP messages */
524 		}
525 
526 		switch (igmp->igmp_code) {
527 
528 		case DVMRP_NEIGHBORS:
529 			if (group) {
530 				/* knows about DVMRP_NEIGHBORS2 msg */
531 				if (target_level == 0) {
532 					target_level = ntohl(group);
533 					ask2(target_addr);
534 				}
535 			} else {
536 				accept_neighbors(src, dst, (u_char *)(igmp + 1),
537 						 igmpdatalen, ntohl(group));
538 				exit(0);
539 			}
540 			break;
541 
542 		case DVMRP_NEIGHBORS2:
543 			accept_neighbors2(src, dst, (u_char *)(igmp + 1),
544 					  igmpdatalen, ntohl(group));
545 			exit(0);
546 		}
547 	    }
548 	}
549 	exit(1);
550 }
551 
552 /* dummies */
553 void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen,
554 		  u_int32_t level)
555 {
556 }
557 void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group,
558 			 int r_type)
559 {
560 }
561 void accept_neighbor_request2(u_int32_t src, u_int32_t dst)
562 {
563 }
564 void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen,
565 		   u_int32_t level)
566 {
567 }
568 void accept_neighbor_request(u_int32_t src, u_int32_t dst)
569 {
570 }
571 void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen)
572 {
573 }
574 void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen)
575 {
576 }
577 void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen)
578 {
579 }
580 void add_table_entry(u_int32_t origin, u_int32_t mcastgrp)
581 {
582 }
583 void check_vif_state(void)
584 {
585 }
586 void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group)
587 {
588 }
589 void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data,
590 		   u_int no, int datalen)
591 {
592 }
593 void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group,
594 			     int tmo)
595 {
596 }
597 void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
598 {
599 }
600 void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
601 {
602 }
603