1/* $NetBSD: mrinfo.c,v 1.28 2011/06/22 04:02:46 mrg 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 80static 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.28 2011/06/22 04:02:46 mrg Exp $"); 84#endif 85#endif 86 87#include <ctype.h> 88#include <string.h> 89#include <netdb.h> 90#include <sys/time.h> 91#include <poll.h> 92#include "defs.h" 93#include <arpa/inet.h> 94#include <stdarg.h> 95 96#define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */ 97#define DEFAULT_RETRIES 3 /* How many times to ask each router */ 98 99u_int32_t our_addr, target_addr = 0; /* in NET order */ 100int debug = 0; 101int nflag = 0; 102int retries = DEFAULT_RETRIES; 103int timeout = DEFAULT_TIMEOUT; 104int target_level = 0; 105vifi_t numvifs; /* to keep loader happy */ 106 /* (see COPY_TABLES macro called in kern.c) */ 107 108const char * inet_name(u_int32_t addr); 109void ask(u_int32_t dst); 110void ask2(u_int32_t dst); 111int get_number(int *var, int deflt, char ***pargv, 112 int *pargc); 113u_int32_t host_addr(char *name); 114__dead void usage(void); 115 116/* logit() prototyped in defs.h */ 117 118 119const char * 120inet_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 */ 141void 142logit(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 */ 175void 176ask(u_int32_t dst) 177{ 178 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS, 179 htonl(MROUTED_LEVEL), 0); 180} 181 182void 183ask2(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 */ 192void 193accept_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), 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)); 217 printf("%s (%s) [%d/%d]\n", 218 inet_fmt(neighbor), 219 inet_name(neighbor), metric, thresh); 220 } 221 } 222} 223 224void 225accept_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), 233 inet_name(src), 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)); 260 printf("%s (%s) [%d/%d", 261 inet_fmt(neighbor), 262 inet_name(neighbor), metric, thresh); 263 if (flags & DVMRP_NF_TUNNEL) 264 printf("/tunnel"); 265 if (flags & DVMRP_NF_SRCRT) 266 printf("/srcrt"); 267 if (flags & DVMRP_NF_PIM) 268 printf("/pim"); 269 if (flags & DVMRP_NF_QUERIER) 270 printf("/querier"); 271 if (flags & DVMRP_NF_DISABLED) 272 printf("/disabled"); 273 if (flags & DVMRP_NF_DOWN) 274 printf("/down"); 275 if (flags & DVMRP_NF_LEAF) 276 printf("/leaf"); 277 printf("]\n"); 278 } 279 } 280} 281 282int 283get_number(int *var, int deflt, char ***pargv, int *pargc) 284{ 285 if ((*pargv)[0][2] == '\0') { /* Get the value from the next 286 * argument */ 287 if (*pargc > 1 && isdigit((unsigned char)(*pargv)[1][0])) { 288 (*pargv)++, (*pargc)--; 289 *var = atoi((*pargv)[0]); 290 return 1; 291 } else if (deflt >= 0) { 292 *var = deflt; 293 return 1; 294 } else 295 return 0; 296 } else { /* Get value from the rest of this argument */ 297 if (isdigit((unsigned char)(*pargv)[0][2])) { 298 *var = atoi((*pargv)[0] + 2); 299 return 1; 300 } else { 301 return 0; 302 } 303 } 304} 305 306void 307usage(void) 308{ 309 fprintf(stderr, 310 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n"); 311 exit(1); 312} 313 314int 315main(int argc, char *argv[]) 316{ 317 int tries; 318 int trynew; 319 struct timeval et; 320 struct hostent *hp; 321 struct hostent bogus; 322 const char *host; 323 int curaddr; 324 325 if (geteuid() != 0) { 326 fprintf(stderr, "mrinfo: must be root\n"); 327 exit(1); 328 } 329 init_igmp(); 330 if (setuid(getuid()) == -1) 331 logit(LOG_ERR, errno, "setuid"); 332 333 setlinebuf(stderr); 334 335 argv++, argc--; 336 while (argc > 0 && argv[0][0] == '-') { 337 switch (argv[0][1]) { 338 case 'd': 339 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc)) 340 usage(); 341 break; 342 case 'n': 343 ++nflag; 344 break; 345 case 'r': 346 if (!get_number(&retries, -1, &argv, &argc)) 347 usage(); 348 break; 349 case 't': 350 if (!get_number(&timeout, -1, &argv, &argc)) 351 usage(); 352 break; 353 default: 354 usage(); 355 } 356 argv++, argc--; 357 } 358 if (argc > 1) 359 usage(); 360 if (argc == 1) 361 host = argv[0]; 362 else 363 host = "127.0.0.1"; 364 365 if ((target_addr = inet_addr(host)) != (in_addr_t)-1) { 366 hp = &bogus; 367 hp->h_length = sizeof(target_addr); 368 hp->h_addr_list = (char **)malloc(2 * sizeof(char *)); 369 if (hp->h_addr_list == NULL) 370 logit(LOG_ERR, errno, "malloc"); 371 hp->h_addr_list[0] = malloc(hp->h_length); 372 if (hp->h_addr_list[0] == NULL) 373 logit(LOG_ERR, errno, "malloc"); 374 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length); 375 hp->h_addr_list[1] = NULL; 376 } else 377 hp = gethostbyname(host); 378 379 if (hp == NULL || hp->h_length != sizeof(target_addr)) { 380 fprintf(stderr, "mrinfo: %s: no such host\n", argv[0]); 381 exit(1); 382 } 383 if (debug) 384 fprintf(stderr, "Debug level %u\n", debug); 385 386 /* Check all addresses; mrouters often have unreachable interfaces */ 387 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) { 388 memcpy(&target_addr, hp->h_addr_list[curaddr], sizeof(target_addr)); 389 { /* Find a good local address for us. */ 390 int udp; 391 struct sockaddr_in addr; 392 socklen_t addrlen = sizeof(addr); 393 394 memset(&addr, 0, sizeof(addr)); 395 addr.sin_family = AF_INET; 396#if (defined(BSD) && (BSD >= 199103)) 397 addr.sin_len = sizeof addr; 398#endif 399 addr.sin_addr.s_addr = target_addr; 400 addr.sin_port = htons(2000); /* any port over 1024 will 401 * do... */ 402 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0 403 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0 404 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0) { 405 perror("Determining local address"); 406 exit(1); 407 } 408 close(udp); 409 our_addr = addr.sin_addr.s_addr; 410 } 411 412 tries = 0; 413 trynew = 1; 414 /* 415 * New strategy: send 'ask2' for two timeouts, then fall back 416 * to 'ask', since it's not very likely that we are going to 417 * find someone who only responds to 'ask' these days 418 */ 419 ask2(target_addr); 420 421 gettimeofday(&et, 0); 422 et.tv_sec += timeout; 423 424 /* Main receive loop */ 425 for (;;) { 426 struct pollfd set[1]; 427 struct timeval tv, now; 428 int count, recvlen; 429 socklen_t dummy; 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 < (int)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 */ 553void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen, 554 u_int32_t level) 555{ 556} 557void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group, 558 int r_type) 559{ 560} 561void accept_neighbor_request2(u_int32_t src, u_int32_t dst) 562{ 563} 564void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen, 565 u_int32_t level) 566{ 567} 568void accept_neighbor_request(u_int32_t src, u_int32_t dst) 569{ 570} 571void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen) 572{ 573} 574void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen) 575{ 576} 577void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen) 578{ 579} 580void add_table_entry(u_int32_t origin, u_int32_t mcastgrp) 581{ 582} 583void check_vif_state(void) 584{ 585} 586void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group) 587{ 588} 589void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data, 590 u_int no, int datalen) 591{ 592} 593void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group, 594 int tmo) 595{ 596} 597void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen) 598{ 599} 600void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen) 601{ 602} 603