mrinfo.c revision 1.18 1 /* $NetBSD: mrinfo.c,v 1.18 2002/09/23 12:48:06 mycroft 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.18 2002/09/23 12:48:06 mycroft 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 /* log() 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 log(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, s1), 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, s1));
218 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
219 inet_name(neighbor), metric, thresh);
220 }
221 }
222 }
223
224 void
225 accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen,
226 u_int32_t level)
227 {
228 u_char *ep = p + datalen;
229 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
230 /* well, only possibly_broken_cisco, but that's too long to type. */
231
232 printf("%s (%s) [version %d.%d", inet_fmt(src, s1), inet_name(src),
233 level & 0xff, (level >> 8) & 0xff);
234 if ((level >> 16) & NF_LEAF) { printf (",leaf"); }
235 if ((level >> 16) & NF_PRUNE) { printf (",prune"); }
236 if ((level >> 16) & NF_GENID) { printf (",genid"); }
237 if ((level >> 16) & NF_MTRACE) { printf (",mtrace"); }
238 printf ("]:\n");
239
240 while (p < ep) {
241 u_char metric;
242 u_char thresh;
243 u_char flags;
244 int ncount;
245 u_int32_t laddr = *(u_int32_t*)p;
246
247 p += 4;
248 metric = *p++;
249 thresh = *p++;
250 flags = *p++;
251 ncount = *p++;
252 if (broken_cisco && ncount == 0) /* dumb Ciscos */
253 ncount = 1;
254 if (broken_cisco && ncount > 15) /* dumb Ciscos */
255 ncount = ncount & 0xf;
256 while (--ncount >= 0 && p < ep) {
257 u_int32_t neighbor = *(u_int32_t*)p;
258 p += 4;
259 printf(" %s -> ", inet_fmt(laddr, s1));
260 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
261 inet_name(neighbor), metric, thresh);
262 if (flags & DVMRP_NF_TUNNEL)
263 printf("/tunnel");
264 if (flags & DVMRP_NF_SRCRT)
265 printf("/srcrt");
266 if (flags & DVMRP_NF_PIM)
267 printf("/pim");
268 if (flags & DVMRP_NF_QUERIER)
269 printf("/querier");
270 if (flags & DVMRP_NF_DISABLED)
271 printf("/disabled");
272 if (flags & DVMRP_NF_DOWN)
273 printf("/down");
274 if (flags & DVMRP_NF_LEAF)
275 printf("/leaf");
276 printf("]\n");
277 }
278 }
279 }
280
281 int
282 get_number(int *var, int deflt, char ***pargv, int *pargc)
283 {
284 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
285 * argument */
286 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
287 (*pargv)++, (*pargc)--;
288 *var = atoi((*pargv)[0]);
289 return 1;
290 } else if (deflt >= 0) {
291 *var = deflt;
292 return 1;
293 } else
294 return 0;
295 } else { /* Get value from the rest of this argument */
296 if (isdigit((*pargv)[0][2])) {
297 *var = atoi((*pargv)[0] + 2);
298 return 1;
299 } else {
300 return 0;
301 }
302 }
303 }
304
305 void
306 usage(void)
307 {
308 fprintf(stderr,
309 "Usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
310 exit(1);
311 }
312
313 int
314 main(int argc, char *argv[])
315 {
316 int tries;
317 int trynew;
318 struct timeval et;
319 struct hostent *hp;
320 struct hostent bogus;
321 char *host;
322 int curaddr;
323
324 if (geteuid() != 0) {
325 fprintf(stderr, "mrinfo: must be root\n");
326 exit(1);
327 }
328 init_igmp();
329 if (setuid(getuid()) == -1)
330 log(LOG_ERR, errno, "setuid");
331
332 setlinebuf(stderr);
333
334 argv++, argc--;
335 while (argc > 0 && argv[0][0] == '-') {
336 switch (argv[0][1]) {
337 case 'd':
338 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
339 usage();
340 break;
341 case 'n':
342 ++nflag;
343 break;
344 case 'r':
345 if (!get_number(&retries, -1, &argv, &argc))
346 usage();
347 break;
348 case 't':
349 if (!get_number(&timeout, -1, &argv, &argc))
350 usage();
351 break;
352 default:
353 usage();
354 }
355 argv++, argc--;
356 }
357 if (argc > 1)
358 usage();
359 if (argc == 1)
360 host = argv[0];
361 else
362 host = "127.0.0.1";
363
364 if ((target_addr = inet_addr(host)) != -1) {
365 hp = &bogus;
366 hp->h_length = sizeof(target_addr);
367 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
368 if (hp->h_addr_list == NULL)
369 log(LOG_ERR, errno, "malloc");
370 hp->h_addr_list[0] = malloc(hp->h_length);
371 if (hp->h_addr_list[0] == NULL)
372 log(LOG_ERR, errno, "malloc");
373 memcpy(hp->h_addr_list[0], &target_addr, sizeof(hp->h_addr_list[0]));
374 hp->h_addr_list[1] = NULL;
375 } else
376 hp = gethostbyname(host);
377
378 if (hp == NULL || hp->h_length != sizeof(target_addr)) {
379 fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]);
380 exit(1);
381 }
382 if (debug)
383 fprintf(stderr, "Debug level %u\n", debug);
384
385 /* Check all addresses; mrouters often have unreachable interfaces */
386 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
387 memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr));
388 { /* Find a good local address for us. */
389 int udp;
390 struct sockaddr_in addr;
391 int addrlen = sizeof(addr);
392
393 memset(&addr, 0, sizeof(addr));
394 addr.sin_family = AF_INET;
395 #if (defined(BSD) && (BSD >= 199103))
396 addr.sin_len = sizeof addr;
397 #endif
398 addr.sin_addr.s_addr = target_addr;
399 addr.sin_port = htons(2000); /* any port over 1024 will
400 * do... */
401 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
402 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
403 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) {
404 perror("Determining local address");
405 exit(1);
406 }
407 close(udp);
408 our_addr = addr.sin_addr.s_addr;
409 }
410
411 tries = 0;
412 trynew = 1;
413 /*
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
417 */
418 ask2(target_addr);
419
420 gettimeofday(&et, 0);
421 et.tv_sec += timeout;
422
423 /* Main receive loop */
424 for (;;) {
425 struct pollfd set[1];
426 struct timeval tv, now;
427 int count, recvlen, dummy = 0;
428 u_int32_t src, dst, group;
429 struct ip *ip;
430 struct igmp *igmp;
431 int ipdatalen, iphdrlen, igmpdatalen;
432
433 set[0].fd = igmp_socket;
434 set[0].events = POLLIN;
435
436 gettimeofday(&now, 0);
437 tv.tv_sec = et.tv_sec - now.tv_sec;
438 tv.tv_usec = et.tv_usec - now.tv_usec;
439
440 if (tv.tv_usec < 0) {
441 tv.tv_usec += 1000000L;
442 --tv.tv_sec;
443 }
444 if (tv.tv_sec < 0)
445 tv.tv_sec = tv.tv_usec = 0;
446
447 count = poll(set, 1, tv.tv_sec * 1000 + tv.tv_usec / 1000);
448
449 if (count < 0) {
450 if (errno != EINTR)
451 perror("select");
452 continue;
453 } else if (count == 0) {
454 log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
455 if (++tries > retries)
456 break;
457 /* If we've tried ASK_NEIGHBORS2 twice with
458 * no response, fall back to ASK_NEIGHBORS
459 */
460 if (tries == 2 && target_level == 0)
461 trynew = 0;
462 if (target_level == 0 && trynew == 0)
463 ask(target_addr);
464 else
465 ask2(target_addr);
466 gettimeofday(&et, 0);
467 et.tv_sec += timeout;
468 continue;
469 }
470 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
471 0, NULL, &dummy);
472 if (recvlen <= 0) {
473 if (recvlen && errno != EINTR)
474 perror("recvfrom");
475 continue;
476 }
477
478 if (recvlen < sizeof(struct ip)) {
479 log(LOG_WARNING, 0,
480 "packet too short (%u bytes) for IP header",
481 recvlen);
482 continue;
483 }
484 ip = (struct ip *) recv_buf;
485 if (ip->ip_p == 0)
486 continue; /* Request to install cache entry */
487 src = ip->ip_src.s_addr;
488 dst = ip->ip_dst.s_addr;
489 iphdrlen = ip->ip_hl << 2;
490 ipdatalen = ip->ip_len;
491 if (iphdrlen + ipdatalen != recvlen) {
492 log(LOG_WARNING, 0,
493 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
494 recvlen, iphdrlen, ipdatalen);
495 continue;
496 }
497 igmp = (struct igmp *) (recv_buf + iphdrlen);
498 group = igmp->igmp_group.s_addr;
499 igmpdatalen = ipdatalen - IGMP_MINLEN;
500 if (igmpdatalen < 0) {
501 log(LOG_WARNING, 0,
502 "IP data field too short (%u bytes) for IGMP, from %s",
503 ipdatalen, inet_fmt(src, s1));
504 continue;
505 }
506 if (igmp->igmp_type != IGMP_DVMRP)
507 continue;
508
509 switch (igmp->igmp_code) {
510 case DVMRP_NEIGHBORS:
511 case DVMRP_NEIGHBORS2:
512 if (src != target_addr) {
513 fprintf(stderr, "mrinfo: got reply from %s",
514 inet_fmt(src, s1));
515 fprintf(stderr, " instead of %s\n",
516 inet_fmt(target_addr, s1));
517 /*continue;*/
518 }
519 break;
520 default:
521 continue; /* ignore all other DVMRP messages */
522 }
523
524 switch (igmp->igmp_code) {
525
526 case DVMRP_NEIGHBORS:
527 if (group) {
528 /* knows about DVMRP_NEIGHBORS2 msg */
529 if (target_level == 0) {
530 target_level = ntohl(group);
531 ask2(target_addr);
532 }
533 } else {
534 accept_neighbors(src, dst, (u_char *)(igmp + 1),
535 igmpdatalen, ntohl(group));
536 exit(0);
537 }
538 break;
539
540 case DVMRP_NEIGHBORS2:
541 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
542 igmpdatalen, ntohl(group));
543 exit(0);
544 }
545 }
546 }
547 exit(1);
548 }
549
550 /* dummies */
551 void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen,
552 u_int32_t level)
553 {
554 }
555 void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group,
556 int r_type)
557 {
558 }
559 void accept_neighbor_request2(u_int32_t src, u_int32_t dst)
560 {
561 }
562 void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen,
563 u_int32_t level)
564 {
565 }
566 void accept_neighbor_request(u_int32_t src, u_int32_t dst)
567 {
568 }
569 void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen)
570 {
571 }
572 void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen)
573 {
574 }
575 void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen)
576 {
577 }
578 void add_table_entry(u_int32_t origin, u_int32_t mcastgrp)
579 {
580 }
581 void check_vif_state(void)
582 {
583 }
584 void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group)
585 {
586 }
587 void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data,
588 u_int no, int datalen)
589 {
590 }
591 void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group,
592 int tmo)
593 {
594 }
595 void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
596 {
597 }
598 void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen)
599 {
600 }
601