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