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