1 /* $OpenBSD: mtrace.c,v 1.32 2013/11/24 00:54:12 deraadt Exp $ */ 2 /* $NetBSD: mtrace.c,v 1.5 1995/12/10 10:57:15 mycroft Exp $ */ 3 4 /* 5 * mtrace.c 6 * 7 * This tool traces the branch of a multicast tree from a source to a 8 * receiver for a particular multicast group and gives statistics 9 * about packet rate and loss for each hop along the path. It can 10 * usually be invoked just as 11 * 12 * mtrace source 13 * 14 * to trace the route from that source to the local host for a default 15 * group when only the route is desired and not group-specific packet 16 * counts. See the usage line for more complex forms. 17 * 18 * 19 * Released 4 Apr 1995. This program was adapted by Steve Casner 20 * (USC/ISI) from a prototype written by Ajit Thyagarajan (UDel and 21 * Xerox PARC). It attempts to parallel in command syntax and output 22 * format the unicast traceroute program written by Van Jacobson (LBL) 23 * for the parts where that makes sense. 24 * 25 * Copyright (c) 1998-2001. 26 * The University of Southern California/Information Sciences Institute. 27 * All rights reserved. 28 * 29 * Redistribution and use in source and binary forms, with or without 30 * modification, are permitted provided that the following conditions 31 * are met: 32 * 1. Redistributions of source code must retain the above copyright 33 * notice, this list of conditions and the following disclaimer. 34 * 2. Redistributions in binary form must reproduce the above copyright 35 * notice, this list of conditions and the following disclaimer in the 36 * documentation and/or other materials provided with the distribution. 37 * 3. Neither the name of the project nor the names of its contributors 38 * may be used to endorse or promote products derived from this software 39 * without specific prior written permission. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 */ 53 54 #include <netdb.h> 55 #include <sys/time.h> 56 #include <memory.h> 57 #include <string.h> 58 #include <poll.h> 59 #include <ctype.h> 60 #include <sys/ioctl.h> 61 #include "defs.h" 62 #include <arpa/inet.h> 63 #include <stdarg.h> 64 #ifdef SUNOS5 65 #include <sys/systeminfo.h> 66 #endif 67 #include <ifaddrs.h> 68 #include <err.h> 69 70 #define DEFAULT_TIMEOUT 3 /* How long to wait before retrying requests */ 71 #define DEFAULT_RETRIES 3 /* How many times to try */ 72 #define MAXHOPS UNREACHABLE /* Don't need more hops than max metric */ 73 #define UNICAST_TTL 255 /* TTL for unicast response */ 74 #define MULTICAST_TTL1 64 /* Default TTL for multicast query/response */ 75 #define MULTICAST_TTL_INC 32 /* TTL increment for increase after timeout */ 76 #define MULTICAST_TTL_MAX 192 /* Maximum TTL allowed (protect low-BW links */ 77 78 struct resp_buf { 79 u_long qtime; /* Time query was issued */ 80 u_long rtime; /* Time response was received */ 81 int len; /* Number of reports or length of data */ 82 struct igmp igmp; /* IGMP header */ 83 union { 84 struct { 85 struct tr_query q; /* Query/response header */ 86 struct tr_resp r[MAXHOPS]; /* Per-hop reports */ 87 } t; 88 char d[MAX_DVMRP_DATA_LEN]; /* Neighbor data */ 89 } u; 90 } base, incr[2]; 91 92 #define qhdr u.t.q 93 #define resps u.t.r 94 #define ndata u.d 95 96 char names[MAXHOPS][40]; 97 int reset[MAXHOPS]; /* To get around 3.4 bug, ... */ 98 int swaps[MAXHOPS]; /* To get around 3.6 bug, ... */ 99 100 int timeout = DEFAULT_TIMEOUT; 101 int nqueries = DEFAULT_RETRIES; 102 int numeric = FALSE; 103 int debug = 0; 104 int passive = FALSE; 105 int multicast = FALSE; 106 int statint = 10; 107 int verbose = 0; 108 109 u_int32_t defgrp; /* Default group if not specified */ 110 u_int32_t query_cast; /* All routers multicast addr */ 111 u_int32_t resp_cast; /* Mtrace response multicast addr */ 112 113 u_int32_t lcl_addr = 0; /* This host address, in NET order */ 114 u_int32_t dst_netmask; /* netmask to go with qdst */ 115 116 /* 117 * Query/response parameters, all initialized to zero and set later 118 * to default values or from options. 119 */ 120 u_int32_t qsrc = 0; /* Source address in the query */ 121 u_int32_t qgrp = 0; /* Group address in the query */ 122 u_int32_t qdst = 0; /* Destination (receiver) address in query */ 123 u_char qno = 0; /* Max number of hops to query */ 124 u_int32_t raddr = 0; /* Address where response should be sent */ 125 int qttl = 0; /* TTL for the query packet */ 126 u_char rttl = 0; /* TTL for the response packet */ 127 u_int32_t gwy = 0; /* User-supplied last-hop router address */ 128 u_int32_t tdst = 0; /* Address where trace is sent (last-hop) */ 129 130 vifi_t numvifs; /* to keep loader happy */ 131 /* (see kern.c) */ 132 133 char * inet_name(u_int32_t addr); 134 u_int32_t host_addr(char *name); 135 /* u_int is promoted u_char */ 136 char * proto_type(u_int type); 137 char * flag_type(u_int type); 138 139 u_int32_t get_netmask(int s, u_int32_t dst); 140 int get_ttl(struct resp_buf *buf); 141 int t_diff(u_long a, u_long b); 142 u_long fixtime(u_long time); 143 int send_recv(u_int32_t dst, int type, int code, 144 int tries, struct resp_buf *save); 145 char * print_host(u_int32_t addr); 146 char * print_host2(u_int32_t addr1, u_int32_t addr2); 147 void print_trace(int index, struct resp_buf *buf); 148 int what_kind(struct resp_buf *buf, char *why); 149 char * scale(int *hop); 150 void stat_line(struct tr_resp *r, struct tr_resp *s, 151 int have_next, int *res); 152 void fixup_stats(struct resp_buf *base, 153 struct resp_buf *prev, struct resp_buf *new); 154 int print_stats(struct resp_buf *base, 155 struct resp_buf *prev, struct resp_buf *new); 156 void check_vif_state(void); 157 u_long byteswap(u_long v); 158 159 int main(int argc, char *argv[]); 160 161 162 163 char * 164 inet_name(u_int32_t addr) 165 { 166 struct hostent *e; 167 168 e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET); 169 170 return e ? e->h_name : "?"; 171 } 172 173 174 u_int32_t 175 host_addr(char *name) 176 { 177 struct hostent *e = (struct hostent *)0; 178 u_int32_t addr; 179 int i, dots = 3; 180 char buf[40]; 181 char *ip = name; 182 char *op = buf; 183 184 /* 185 * Undo BSD's favor -- take fewer than 4 octets as net/subnet address 186 * if the name is all numeric. 187 */ 188 for (i = sizeof(buf) - 7; i > 0; --i) { 189 if (*ip == '.') 190 --dots; 191 else if (*ip == '\0') 192 break; 193 else if (!isdigit((unsigned char)*ip)) 194 dots = 0; /* Not numeric, don't add zeroes */ 195 *op++ = *ip++; 196 } 197 for (i = 0; i < dots; ++i) { 198 *op++ = '.'; 199 *op++ = '0'; 200 } 201 *op = '\0'; 202 203 if (dots <= 0) e = gethostbyname(name); 204 if (e) memcpy((char *)&addr, e->h_addr_list[0], e->h_length); 205 else { 206 addr = inet_addr(buf); 207 if (addr == -1) { 208 addr = 0; 209 printf("Could not parse %s as host name or address\n", name); 210 } 211 } 212 return addr; 213 } 214 215 216 char * 217 proto_type(u_int type) 218 { 219 static char buf[80]; 220 221 switch (type) { 222 case PROTO_DVMRP: 223 return ("DVMRP"); 224 case PROTO_MOSPF: 225 return ("MOSPF"); 226 case PROTO_PIM: 227 return ("PIM"); 228 case PROTO_CBT: 229 return ("CBT"); 230 default: 231 (void) snprintf(buf, sizeof buf, "Unknown protocol code %d", type); 232 return (buf); 233 } 234 } 235 236 237 char * 238 flag_type(u_int type) 239 { 240 static char buf[80]; 241 242 switch (type) { 243 case TR_NO_ERR: 244 return (""); 245 case TR_WRONG_IF: 246 return ("Wrong interface"); 247 case TR_PRUNED: 248 return ("Prune sent upstream"); 249 case TR_OPRUNED: 250 return ("Output pruned"); 251 case TR_SCOPED: 252 return ("Hit scope boundary"); 253 case TR_NO_RTE: 254 return ("No route"); 255 case TR_OLD_ROUTER: 256 return ("Next router no mtrace"); 257 case TR_NO_FWD: 258 return ("Not forwarding"); 259 case TR_NO_SPACE: 260 return ("No space in packet"); 261 default: 262 (void) snprintf(buf, sizeof buf, "Unknown error code %d", type); 263 return (buf); 264 } 265 } 266 267 /* 268 * If destination is on a local net, get the netmask, else set the 269 * netmask to all ones. There are two side effects: if the local 270 * address was not explicitly set, and if the destination is on a 271 * local net, use that one; in either case, verify that the local 272 * address is valid. 273 */ 274 275 u_int32_t 276 get_netmask(int s, u_int32_t dst) 277 { 278 u_int32_t if_addr, if_mask; 279 u_int32_t retval = 0xFFFFFFFF; 280 int found = FALSE; 281 struct ifaddrs *ifap, *ifa; 282 283 if (getifaddrs(&ifap) != 0) { 284 perror("getifaddrs"); 285 return (retval); 286 } 287 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 288 if (ifa->ifa_addr->sa_family != AF_INET) 289 continue; 290 if_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; 291 if_mask = ((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr.s_addr; 292 if ((dst & if_mask) == (if_addr & if_mask)) { 293 retval = if_mask; 294 if (lcl_addr == 0) 295 lcl_addr = if_addr; 296 } 297 if (lcl_addr == if_addr) 298 found = TRUE; 299 } 300 if (!found && lcl_addr != 0) { 301 printf("Interface address is not valid\n"); 302 exit(1); 303 } 304 freeifaddrs(ifap); 305 return (retval); 306 } 307 308 309 int 310 get_ttl(struct resp_buf *buf) 311 { 312 int rno; 313 struct tr_resp *b; 314 u_int ttl; 315 316 if (buf && (rno = buf->len) > 0) { 317 b = buf->resps + rno - 1; 318 ttl = b->tr_fttl; 319 320 while (--rno > 0) { 321 --b; 322 if (ttl < b->tr_fttl) ttl = b->tr_fttl; 323 else ++ttl; 324 } 325 ttl += MULTICAST_TTL_INC; 326 if (ttl < MULTICAST_TTL1) ttl = MULTICAST_TTL1; 327 if (ttl > MULTICAST_TTL_MAX) ttl = MULTICAST_TTL_MAX; 328 return (ttl); 329 } else return(MULTICAST_TTL1); 330 } 331 332 /* 333 * Calculate the difference between two 32-bit NTP timestamps and return 334 * the result in milliseconds. 335 */ 336 int 337 t_diff(u_long a, u_long b) 338 { 339 int d = a - b; 340 341 return ((d * 125) >> 13); 342 } 343 344 /* 345 * Fixup for incorrect time format in 3.3 mrouted. 346 * This is possible because (JAN_1970 mod 64K) is quite close to 32K, 347 * so correct and incorrect times will be far apart. 348 */ 349 u_long 350 fixtime(u_long time) 351 { 352 if (abs((int)(time-base.qtime)) > 0x3FFFFFFF) 353 time = ((time & 0xFFFF0000) + (JAN_1970 << 16)) + 354 ((time & 0xFFFF) << 14) / 15625; 355 return (time); 356 } 357 358 /* 359 * Swap bytes for poor little-endian machines that don't byte-swap 360 */ 361 u_long 362 byteswap(u_long v) 363 { 364 return ((v << 24) | ((v & 0xff00) << 8) | 365 ((v >> 8) & 0xff00) | (v >> 24)); 366 } 367 368 int 369 send_recv(u_int32_t dst, int type, int code, int tries, struct resp_buf *save) 370 { 371 struct timeval tq, tr, tv; 372 struct ip *ip; 373 struct igmp *igmp; 374 struct tr_query *query, *rquery; 375 int ipdatalen, iphdrlen, igmpdatalen; 376 u_int32_t local, group; 377 int datalen; 378 struct pollfd pfd[1]; 379 int count, recvlen, dummy = 0; 380 int len; 381 int i; 382 383 if (type == IGMP_MTRACE_QUERY) { 384 group = qgrp; 385 datalen = sizeof(struct tr_query); 386 } else { 387 group = htonl(MROUTED_LEVEL); 388 datalen = 0; 389 } 390 if (IN_MULTICAST(ntohl(dst))) local = lcl_addr; 391 else local = INADDR_ANY; 392 393 /* 394 * If the reply address was not explicitly specified, start off 395 * with the unicast address of this host. Then, if there is no 396 * response after trying half the tries with unicast, switch to 397 * the standard multicast reply address. If the TTL was also not 398 * specified, set a multicast TTL and if needed increase it for the 399 * last quarter of the tries. 400 */ 401 query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN); 402 query->tr_raddr = raddr ? raddr : multicast ? resp_cast : lcl_addr; 403 query->tr_rttl = rttl ? rttl : 404 IN_MULTICAST(ntohl(query->tr_raddr)) ? get_ttl(save) : UNICAST_TTL; 405 query->tr_src = qsrc; 406 query->tr_dst = qdst; 407 408 for (i = tries ; i > 0; --i) { 409 if (tries == nqueries && raddr == 0) { 410 if (i == ((nqueries + 1) >> 1)) { 411 query->tr_raddr = resp_cast; 412 if (rttl == 0) query->tr_rttl = get_ttl(save); 413 } 414 if (i <= ((nqueries + 3) >> 2) && rttl == 0) { 415 query->tr_rttl += MULTICAST_TTL_INC; 416 if (query->tr_rttl > MULTICAST_TTL_MAX) 417 query->tr_rttl = MULTICAST_TTL_MAX; 418 } 419 } 420 421 /* 422 * Change the qid for each request sent to avoid being confused 423 * by duplicate responses 424 */ 425 query->tr_qid = arc4random(); 426 427 /* 428 * Set timer to calculate delays, then send query 429 */ 430 gettimeofday(&tq, 0); 431 send_igmp(local, dst, type, code, group, datalen); 432 433 /* 434 * Wait for response, discarding false alarms 435 */ 436 pfd[0].fd = igmp_socket; 437 pfd[0].events = POLLIN; 438 while (TRUE) { 439 gettimeofday(&tv, 0); 440 tv.tv_sec = tq.tv_sec + timeout - tv.tv_sec; 441 tv.tv_usec = tq.tv_usec - tv.tv_usec; 442 if (tv.tv_usec < 0) tv.tv_usec += 1000000L, --tv.tv_sec; 443 if (tv.tv_sec < 0) tv.tv_sec = tv.tv_usec = 0; 444 445 count = poll(pfd, 1, tv.tv_sec * 1000); 446 447 if (count < 0) { 448 if (errno != EINTR) perror("poll"); 449 continue; 450 } else if (count == 0) { 451 printf("* "); 452 fflush(stdout); 453 break; 454 } 455 456 gettimeofday(&tr, 0); 457 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 458 0, (struct sockaddr *)0, &dummy); 459 460 if (recvlen <= 0) { 461 if (recvlen && errno != EINTR) perror("recvfrom"); 462 continue; 463 } 464 465 if (recvlen < sizeof(struct ip)) { 466 fprintf(stderr, 467 "packet too short (%u bytes) for IP header", recvlen); 468 continue; 469 } 470 ip = (struct ip *) recv_buf; 471 if (ip->ip_p == 0) /* ignore cache creation requests */ 472 continue; 473 474 iphdrlen = ip->ip_hl << 2; 475 ipdatalen = ntohs(ip->ip_len) - iphdrlen; 476 if (iphdrlen + ipdatalen != recvlen) { 477 fprintf(stderr, 478 "packet shorter (%u bytes) than hdr+data len (%u+%u)\n", 479 recvlen, iphdrlen, ipdatalen); 480 continue; 481 } 482 483 igmp = (struct igmp *) (recv_buf + iphdrlen); 484 igmpdatalen = ipdatalen - IGMP_MINLEN; 485 if (igmpdatalen < 0) { 486 fprintf(stderr, 487 "IP data field too short (%u bytes) for IGMP from %s\n", 488 ipdatalen, inet_fmt(ip->ip_src.s_addr, s1)); 489 continue; 490 } 491 492 switch (igmp->igmp_type) { 493 494 case IGMP_DVMRP: 495 if (igmp->igmp_code != DVMRP_NEIGHBORS2) continue; 496 len = igmpdatalen; 497 /* 498 * Accept DVMRP_NEIGHBORS2 response if it comes from the 499 * address queried or if that address is one of the local 500 * addresses in the response. 501 */ 502 if (ip->ip_src.s_addr != dst) { 503 u_int32_t *p = (u_int32_t *)(igmp + 1); 504 u_int32_t *ep = p + (len >> 2); 505 while (p < ep) { 506 u_int32_t laddr = *p++; 507 int n = ntohl(*p++) & 0xFF; 508 if (laddr == dst) { 509 ep = p + 1; /* ensure p < ep after loop */ 510 break; 511 } 512 p += n; 513 } 514 if (p >= ep) continue; 515 } 516 break; 517 518 case IGMP_MTRACE_QUERY: /* For backward compatibility with 3.3 */ 519 case IGMP_MTRACE_REPLY: 520 if (igmpdatalen <= QLEN) continue; 521 if ((igmpdatalen - QLEN)%RLEN) { 522 printf("packet with incorrect datalen\n"); 523 continue; 524 } 525 526 /* 527 * Ignore responses that don't match query. 528 */ 529 rquery = (struct tr_query *)(igmp + 1); 530 if (rquery->tr_qid != query->tr_qid) continue; 531 if (rquery->tr_src != qsrc) continue; 532 if (rquery->tr_dst != qdst) continue; 533 len = (igmpdatalen - QLEN)/RLEN; 534 535 /* 536 * Ignore trace queries passing through this node when 537 * mtrace is run on an mrouter that is in the path 538 * (needed only because IGMP_MTRACE_QUERY is accepted above 539 * for backward compatibility with multicast release 3.3). 540 */ 541 if (igmp->igmp_type == IGMP_MTRACE_QUERY) { 542 struct tr_resp *r = (struct tr_resp *)(rquery+1) + len - 1; 543 u_int32_t smask; 544 545 VAL_TO_MASK(smask, r->tr_smask); 546 if (len < code && (r->tr_inaddr & smask) != (qsrc & smask) 547 && r->tr_rmtaddr != 0 && !(r->tr_rflags & 0x80)) 548 continue; 549 } 550 551 /* 552 * A match, we'll keep this one. 553 */ 554 if (len > code) { 555 fprintf(stderr, 556 "Num hops received (%d) exceeds request (%d)\n", 557 len, code); 558 } 559 rquery->tr_raddr = query->tr_raddr; /* Insure these are */ 560 rquery->tr_rttl = query->tr_rttl; /* as we sent them */ 561 break; 562 563 default: 564 continue; 565 } 566 567 /* 568 * Most of the sanity checking done at this point. 569 * Return this packet we have been waiting for. 570 */ 571 if (save) { 572 save->qtime = ((tq.tv_sec + JAN_1970) << 16) + 573 (tq.tv_usec << 10) / 15625; 574 save->rtime = ((tr.tv_sec + JAN_1970) << 16) + 575 (tr.tv_usec << 10) / 15625; 576 save->len = len; 577 bcopy((char *)igmp, (char *)&save->igmp, ipdatalen); 578 } 579 return (recvlen); 580 } 581 } 582 return (0); 583 } 584 585 /* 586 * Most of this code is duplicated elsewhere. I'm not sure if 587 * the duplication is absolutely required or not. 588 * 589 * Ideally, this would keep track of ongoing statistics 590 * collection and print out statistics. (& keep track 591 * of h-b-h traces and only print the longest) For now, 592 * it just snoops on what traces it can. 593 */ 594 void 595 passive_mode(void) 596 { 597 struct timeval tr; 598 struct ip *ip; 599 struct igmp *igmp; 600 struct tr_resp *r; 601 int ipdatalen, iphdrlen, igmpdatalen; 602 int len, recvlen, dummy = 0; 603 u_int32_t smask; 604 605 if (raddr) { 606 if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, INADDR_ANY); 607 } else k_join(htonl(0xE0000120), INADDR_ANY); 608 609 while (1) { 610 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE, 611 0, (struct sockaddr *)0, &dummy); 612 gettimeofday(&tr,0); 613 614 if (recvlen <= 0) { 615 if (recvlen && errno != EINTR) perror("recvfrom"); 616 continue; 617 } 618 619 if (recvlen < sizeof(struct ip)) { 620 fprintf(stderr, 621 "packet too short (%u bytes) for IP header", recvlen); 622 continue; 623 } 624 ip = (struct ip *) recv_buf; 625 if (ip->ip_p == 0) /* ignore cache creation requests */ 626 continue; 627 628 iphdrlen = ip->ip_hl << 2; 629 ipdatalen = ntohs(ip->ip_len) - iphdrlen; 630 if (iphdrlen + ipdatalen != recvlen) { 631 fprintf(stderr, 632 "packet shorter (%u bytes) than hdr+data len (%u+%u)\n", 633 recvlen, iphdrlen, ipdatalen); 634 continue; 635 } 636 637 igmp = (struct igmp *) (recv_buf + iphdrlen); 638 igmpdatalen = ipdatalen - IGMP_MINLEN; 639 if (igmpdatalen < 0) { 640 fprintf(stderr, 641 "IP data field too short (%u bytes) for IGMP from %s\n", 642 ipdatalen, inet_fmt(ip->ip_src.s_addr, s1)); 643 continue; 644 } 645 646 switch (igmp->igmp_type) { 647 648 case IGMP_MTRACE_QUERY: /* For backward compatibility with 3.3 */ 649 case IGMP_MTRACE_REPLY: 650 if (igmpdatalen < QLEN) continue; 651 if ((igmpdatalen - QLEN)%RLEN) { 652 printf("packet with incorrect datalen\n"); 653 continue; 654 } 655 656 len = (igmpdatalen - QLEN)/RLEN; 657 658 break; 659 660 default: 661 continue; 662 } 663 664 base.qtime = ((tr.tv_sec + JAN_1970) << 16) + 665 (tr.tv_usec << 10) / 15625; 666 base.rtime = ((tr.tv_sec + JAN_1970) << 16) + 667 (tr.tv_usec << 10) / 15625; 668 base.len = len; 669 bcopy((char *)igmp, (char *)&base.igmp, ipdatalen); 670 /* 671 * If the user specified which traces to monitor, 672 * only accept traces that correspond to the 673 * request 674 */ 675 if ((qsrc != 0 && qsrc != base.qhdr.tr_src) || 676 (qdst != 0 && qdst != base.qhdr.tr_dst) || 677 (qgrp != 0 && qgrp != igmp->igmp_group.s_addr)) 678 continue; 679 680 printf("Mtrace from %s to %s via group %s (mxhop=%d)\n", 681 inet_fmt(base.qhdr.tr_dst, s1), inet_fmt(base.qhdr.tr_src, s2), 682 inet_fmt(igmp->igmp_group.s_addr, s3), igmp->igmp_code); 683 if (len == 0) 684 continue; 685 printf(" 0 "); 686 print_host(base.qhdr.tr_dst); 687 printf("\n"); 688 print_trace(1, &base); 689 r = base.resps + base.len - 1; 690 VAL_TO_MASK(smask, r->tr_smask); 691 if ((r->tr_inaddr & smask) == (base.qhdr.tr_src & smask)) { 692 printf("%3d ", -(base.len+1)); 693 print_host(base.qhdr.tr_src); 694 printf("\n"); 695 } else if (r->tr_rmtaddr != 0) { 696 printf("%3d ", -(base.len+1)); 697 what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ? 698 "doesn't support mtrace" 699 : "is the next hop"); 700 } 701 printf("\n"); 702 } 703 } 704 705 char * 706 print_host(u_int32_t addr) 707 { 708 return print_host2(addr, 0); 709 } 710 711 /* 712 * On some routers, one interface has a name and the other doesn't. 713 * We always print the address of the outgoing interface, but can 714 * sometimes get the name from the incoming interface. This might be 715 * confusing but should be slightly more helpful than just a "?". 716 */ 717 char * 718 print_host2(u_int32_t addr1, u_int32_t addr2) 719 { 720 char *name; 721 722 if (numeric) { 723 printf("%s", inet_fmt(addr1, s1)); 724 return (""); 725 } 726 name = inet_name(addr1); 727 if (*name == '?' && *(name + 1) == '\0' && addr2 != 0) 728 name = inet_name(addr2); 729 printf("%s (%s)", name, inet_fmt(addr1, s1)); 730 return (name); 731 } 732 733 /* 734 * Print responses as received (reverse path from dst to src) 735 */ 736 void 737 print_trace(int index, struct resp_buf *buf) 738 { 739 struct tr_resp *r; 740 char *name; 741 int i; 742 int hop; 743 char *ms; 744 745 i = abs(index); 746 r = buf->resps + i - 1; 747 748 for (; i <= buf->len; ++i, ++r) { 749 if (index > 0) printf("%3d ", -i); 750 name = print_host2(r->tr_outaddr, r->tr_inaddr); 751 printf(" %s thresh^ %d", proto_type(r->tr_rproto), r->tr_fttl); 752 if (verbose) { 753 hop = t_diff(fixtime(ntohl(r->tr_qarr)), buf->qtime); 754 ms = scale(&hop); 755 printf(" %d%s", hop, ms); 756 } 757 printf(" %s\n", flag_type(r->tr_rflags)); 758 memcpy(names[i-1], name, sizeof(names[0]) - 1); 759 names[i-1][sizeof(names[0])-1] = '\0'; 760 } 761 } 762 763 /* 764 * See what kind of router is the next hop 765 */ 766 int 767 what_kind(struct resp_buf *buf, char *why) 768 { 769 u_int32_t smask; 770 int retval; 771 int hops = buf->len; 772 struct tr_resp *r = buf->resps + hops - 1; 773 u_int32_t next = r->tr_rmtaddr; 774 775 retval = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]); 776 print_host(next); 777 if (retval) { 778 u_int32_t version = ntohl(incr[0].igmp.igmp_group.s_addr); 779 u_int32_t *p = (u_int32_t *)incr[0].ndata; 780 u_int32_t *ep = p + (incr[0].len >> 2); 781 char *type = ""; 782 retval = 0; 783 switch (version & 0xFF) { 784 case 1: 785 type = "proteon/mrouted "; 786 retval = 1; 787 break; 788 789 case 2: 790 case 3: 791 if (((version >> 8) & 0xFF) < 3) retval = 1; 792 /* Fall through */ 793 case 4: 794 type = "mrouted "; 795 break; 796 797 case 10: 798 type = "cisco "; 799 } 800 printf(" [%s%d.%d] %s\n", 801 type, version & 0xFF, (version >> 8) & 0xFF, 802 why); 803 VAL_TO_MASK(smask, r->tr_smask); 804 while (p < ep) { 805 u_int32_t laddr = *p++; 806 int flags = (ntohl(*p) & 0xFF00) >> 8; 807 int n = ntohl(*p++) & 0xFF; 808 if (!(flags & (DVMRP_NF_DOWN | DVMRP_NF_DISABLED)) && 809 (laddr & smask) == (qsrc & smask)) { 810 printf("%3d ", -(hops+2)); 811 print_host(qsrc); 812 printf("\n"); 813 return 1; 814 } 815 p += n; 816 } 817 return retval; 818 } 819 printf(" %s\n", why); 820 return 0; 821 } 822 823 824 char * 825 scale(int *hop) 826 { 827 if (*hop > -1000 && *hop < 10000) return (" ms"); 828 *hop /= 1000; 829 if (*hop > -1000 && *hop < 10000) return (" s "); 830 return ("s "); 831 } 832 833 /* 834 * Calculate and print one line of packet loss and packet rate statistics. 835 * Checks for count of all ones from mrouted 2.3 that doesn't have counters. 836 */ 837 #define NEITHER 0 838 #define INS 1 839 #define OUTS 2 840 #define BOTH 3 841 void 842 stat_line(struct tr_resp *r, struct tr_resp *s, int have_next, int *rst) 843 { 844 int timediff = (fixtime(ntohl(s->tr_qarr)) - 845 fixtime(ntohl(r->tr_qarr))) >> 16; 846 int v_lost, v_pct; 847 int g_lost, g_pct; 848 int v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout); 849 int g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt); 850 int v_pps, g_pps; 851 char v_str[8], g_str[8]; 852 int have = NEITHER; 853 int res = *rst; 854 855 if (timediff == 0) timediff = 1; 856 v_pps = v_out / timediff; 857 g_pps = g_out / timediff; 858 859 if (v_out && (s->tr_vifout != 0xFFFFFFFF && s->tr_vifout != 0) || 860 (r->tr_vifout != 0xFFFFFFFF && r->tr_vifout != 0)) 861 have |= OUTS; 862 863 if (have_next) { 864 --r, --s, --rst; 865 if ((s->tr_vifin != 0xFFFFFFFF && s->tr_vifin != 0) || 866 (r->tr_vifin != 0xFFFFFFFF && r->tr_vifin != 0)) 867 have |= INS; 868 if (*rst) 869 res = 1; 870 } 871 872 switch (have) { 873 case BOTH: 874 v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin)); 875 if (v_out) v_pct = (v_lost * 100 + (v_out >> 1)) / v_out; 876 else v_pct = 0; 877 if (-100 < v_pct && v_pct < 101 && v_out > 10) 878 snprintf(v_str, sizeof v_str, "%3d", v_pct); 879 else memcpy(v_str, " --", 4); 880 881 g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt)); 882 if (g_out) g_pct = (g_lost * 100 + (g_out >> 1))/ g_out; 883 else g_pct = 0; 884 if (-100 < g_pct && g_pct < 101 && g_out > 10) 885 snprintf(g_str, sizeof g_str, "%3d", g_pct); 886 else memcpy(g_str, " --", 4); 887 888 printf("%6d/%-5d=%s%%%4d pps", 889 v_lost, v_out, v_str, v_pps); 890 if (res) 891 printf("\n"); 892 else 893 printf("%6d/%-5d=%s%%%4d pps\n", 894 g_lost, g_out, g_str, g_pps); 895 break; 896 897 case INS: 898 v_out = ntohl(s->tr_vifin) - ntohl(r->tr_vifin); 899 v_pps = v_out / timediff; 900 /* Fall through */ 901 902 case OUTS: 903 printf(" %-5d %4d pps", 904 v_out, v_pps); 905 if (res) 906 printf("\n"); 907 else 908 printf(" %-5d %4d pps\n", 909 g_out, g_pps); 910 break; 911 912 case NEITHER: 913 printf("\n"); 914 break; 915 } 916 917 if (debug > 2) { 918 printf("\t\t\t\tv_in: %u ", ntohl(s->tr_vifin)); 919 printf("v_out: %u ", ntohl(s->tr_vifout)); 920 printf("pkts: %u\n", ntohl(s->tr_pktcnt)); 921 printf("\t\t\t\tv_in: %u ", ntohl(r->tr_vifin)); 922 printf("v_out: %u ", ntohl(r->tr_vifout)); 923 printf("pkts: %u\n", ntohl(r->tr_pktcnt)); 924 printf("\t\t\t\tv_in: %u ", ntohl(s->tr_vifin)-ntohl(r->tr_vifin)); 925 printf("v_out: %u ", ntohl(s->tr_vifout) - ntohl(r->tr_vifout)); 926 printf("pkts: %u ", ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt)); 927 printf("time: %d\n", timediff); 928 printf("\t\t\t\tres: %d\n", res); 929 } 930 } 931 932 /* 933 * A fixup to check if any pktcnt has been reset, and to fix the 934 * byteorder bugs in mrouted 3.6 on little-endian machines. 935 */ 936 void 937 fixup_stats(struct resp_buf *base, struct resp_buf *prev, struct resp_buf *new) 938 { 939 int rno = base->len; 940 struct tr_resp *b = base->resps + rno; 941 struct tr_resp *p = prev->resps + rno; 942 struct tr_resp *n = new->resps + rno; 943 int *r = reset + rno; 944 int *s = swaps + rno; 945 int res; 946 947 /* Check for byte-swappers */ 948 while (--rno >= 0) { 949 --n; --p; --b; --s; 950 if (*s || abs(ntohl(n->tr_vifout) - ntohl(p->tr_vifout)) > 100000) { 951 /* This host sends byteswapped reports; swap 'em */ 952 if (!*s) { 953 *s = 1; 954 b->tr_qarr = byteswap(b->tr_qarr); 955 b->tr_vifin = byteswap(b->tr_vifin); 956 b->tr_vifout = byteswap(b->tr_vifout); 957 b->tr_pktcnt = byteswap(b->tr_pktcnt); 958 } 959 960 n->tr_qarr = byteswap(n->tr_qarr); 961 n->tr_vifin = byteswap(n->tr_vifin); 962 n->tr_vifout = byteswap(n->tr_vifout); 963 n->tr_pktcnt = byteswap(n->tr_pktcnt); 964 } 965 } 966 967 rno = base->len; 968 b = base->resps + rno; 969 p = prev->resps + rno; 970 n = new->resps + rno; 971 972 while (--rno >= 0) { 973 --n; --p; --b; --r; 974 res = ((ntohl(n->tr_pktcnt) < ntohl(b->tr_pktcnt)) || 975 (ntohl(n->tr_pktcnt) < ntohl(p->tr_pktcnt))); 976 if (debug > 2) 977 printf("\t\tr=%d, res=%d\n", *r, res); 978 if (*r) { 979 if (res || *r > 1) { 980 /* 981 * This router appears to be a 3.4 with that nasty ol' 982 * neighbor version bug, which causes it to constantly 983 * reset. Just nuke the statistics for this node, and 984 * don't even bother giving it the benefit of the 985 * doubt from now on. 986 */ 987 p->tr_pktcnt = b->tr_pktcnt = n->tr_pktcnt; 988 r++; 989 } else { 990 /* 991 * This is simply the situation that the original 992 * fixup_stats was meant to deal with -- that a 993 * 3.3 or 3.4 router deleted a cache entry while 994 * traffic was still active. 995 */ 996 *r = 0; 997 break; 998 } 999 } else 1000 *r = res; 1001 } 1002 1003 if (rno < 0) return; 1004 1005 rno = base->len; 1006 b = base->resps + rno; 1007 p = prev->resps + rno; 1008 1009 while (--rno >= 0) (--b)->tr_pktcnt = (--p)->tr_pktcnt; 1010 } 1011 1012 /* 1013 * Print responses with statistics for forward path (from src to dst) 1014 */ 1015 int 1016 print_stats(struct resp_buf *base, struct resp_buf *prev, struct resp_buf *new) 1017 { 1018 int rtt, hop; 1019 char *ms; 1020 u_int32_t smask; 1021 int rno = base->len - 1; 1022 struct tr_resp *b = base->resps + rno; 1023 struct tr_resp *p = prev->resps + rno; 1024 struct tr_resp *n = new->resps + rno; 1025 int *r = reset + rno; 1026 u_long resptime = new->rtime; 1027 u_long qarrtime = fixtime(ntohl(n->tr_qarr)); 1028 u_int ttl = n->tr_fttl; 1029 int first = (base == prev); 1030 1031 VAL_TO_MASK(smask, b->tr_smask); 1032 printf(" Source Response Dest"); 1033 printf(" Packet Statistics For Only For Traffic\n"); 1034 printf("%-15s %-15s All Multicast Traffic From %s\n", 1035 ((b->tr_inaddr & smask) == (qsrc & smask)) ? s1 : " * * * ", 1036 inet_fmt(base->qhdr.tr_raddr, s2), inet_fmt(qsrc, s1)); 1037 rtt = t_diff(resptime, new->qtime); 1038 ms = scale(&rtt); 1039 printf(" %c __/ rtt%5d%s Lost/Sent = Pct Rate To %s\n", 1040 first ? 'v' : '|', rtt, ms, inet_fmt(qgrp, s2)); 1041 if (!first) { 1042 hop = t_diff(resptime, qarrtime); 1043 ms = scale(&hop); 1044 printf(" v / hop%5d%s", hop, ms); 1045 printf(" --------------------- --------------------\n"); 1046 } 1047 if (debug > 2) { 1048 printf("\t\t\t\tv_in: %u ", ntohl(n->tr_vifin)); 1049 printf("v_out: %u ", ntohl(n->tr_vifout)); 1050 printf("pkts: %u\n", ntohl(n->tr_pktcnt)); 1051 printf("\t\t\t\tv_in: %u ", ntohl(b->tr_vifin)); 1052 printf("v_out: %u ", ntohl(b->tr_vifout)); 1053 printf("pkts: %u\n", ntohl(b->tr_pktcnt)); 1054 printf("\t\t\t\tv_in: %u ", ntohl(n->tr_vifin) - ntohl(b->tr_vifin)); 1055 printf("v_out: %u ", ntohl(n->tr_vifout) - ntohl(b->tr_vifout)); 1056 printf("pkts: %u\n", ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt)); 1057 printf("\t\t\t\treset: %d\n", *r); 1058 } 1059 1060 while (TRUE) { 1061 if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_inaddr != b->tr_inaddr)) 1062 return 1; /* Route changed */ 1063 1064 if ((n->tr_inaddr != n->tr_outaddr)) 1065 printf("%-15s\n", inet_fmt(n->tr_inaddr, s1)); 1066 printf("%-15s %-14s %s\n", inet_fmt(n->tr_outaddr, s1), names[rno], 1067 flag_type(n->tr_rflags)); 1068 1069 if (rno-- < 1) break; 1070 1071 printf(" %c ^ ttl%5d ", first ? 'v' : '|', ttl); 1072 stat_line(p, n, TRUE, r); 1073 if (!first) { 1074 resptime = qarrtime; 1075 qarrtime = fixtime(ntohl((n-1)->tr_qarr)); 1076 hop = t_diff(resptime, qarrtime); 1077 ms = scale(&hop); 1078 printf(" v | hop%5d%s", hop, ms); 1079 stat_line(b, n, TRUE, r); 1080 } 1081 1082 --b, --p, --n, --r; 1083 if (ttl < n->tr_fttl) ttl = n->tr_fttl; 1084 else ++ttl; 1085 } 1086 1087 printf(" %c \\__ ttl%5d ", first ? 'v' : '|', ttl); 1088 stat_line(p, n, FALSE, r); 1089 if (!first) { 1090 hop = t_diff(qarrtime, new->qtime); 1091 ms = scale(&hop); 1092 printf(" v \\ hop%5d%s", hop, ms); 1093 stat_line(b, n, FALSE, r); 1094 } 1095 printf("%-15s %s\n", inet_fmt(qdst, s1), inet_fmt(lcl_addr, s2)); 1096 printf(" Receiver Query Source\n\n"); 1097 return 0; 1098 } 1099 1100 1101 /*************************************************************************** 1102 * main 1103 ***************************************************************************/ 1104 1105 int 1106 main(int argc, char *argv[]) 1107 { 1108 int udp; 1109 struct sockaddr_in addr; 1110 int addrlen = sizeof(addr); 1111 int recvlen; 1112 struct timeval tv; 1113 struct resp_buf *prev, *new; 1114 struct tr_resp *r; 1115 u_int32_t smask; 1116 int rno; 1117 int hops, nexthop, tries; 1118 u_int32_t lastout = 0; 1119 int numstats = 1; 1120 int waittime; 1121 uid_t uid; 1122 1123 init_igmp(); 1124 1125 uid = getuid(); 1126 if (setresuid(uid, uid, uid) == -1) 1127 err(1, "setresuid"); 1128 1129 argv++, argc--; 1130 if (argc == 0) goto usage; 1131 1132 while (argc > 0 && *argv[0] == '-') { 1133 char *p = *argv++; argc--; 1134 p++; 1135 do { 1136 char c = *p++; 1137 char *arg = (char *) 0; 1138 if (isdigit((unsigned char)*p)) { 1139 arg = p; 1140 p = ""; 1141 } else if (argc > 0) arg = argv[0]; 1142 switch (c) { 1143 case 'd': /* Unlisted debug print option */ 1144 if (arg && isdigit((unsigned char)*arg)) { 1145 debug = atoi(arg); 1146 if (debug < 0) debug = 0; 1147 if (debug > 3) debug = 3; 1148 if (arg == argv[0]) argv++, argc--; 1149 break; 1150 } else 1151 goto usage; 1152 case 'M': /* Use multicast for response */ 1153 multicast = TRUE; 1154 break; 1155 case 'l': /* Loop updating stats indefinitely */ 1156 numstats = 3153600; 1157 break; 1158 case 'n': /* Don't reverse map host addresses */ 1159 numeric = TRUE; 1160 break; 1161 case 'p': /* Passive listen for traces */ 1162 passive = TRUE; 1163 break; 1164 case 'v': /* Verbosity */ 1165 verbose = TRUE; 1166 break; 1167 case 's': /* Short form, don't wait for stats */ 1168 numstats = 0; 1169 break; 1170 case 'w': /* Time to wait for packet arrival */ 1171 if (arg && isdigit((unsigned char)*arg)) { 1172 timeout = atoi(arg); 1173 if (timeout < 1) timeout = 1; 1174 if (arg == argv[0]) argv++, argc--; 1175 break; 1176 } else 1177 goto usage; 1178 case 'm': /* Max number of hops to trace */ 1179 if (arg && isdigit((unsigned char)*arg)) { 1180 qno = atoi(arg); 1181 if (qno > MAXHOPS) qno = MAXHOPS; 1182 else if (qno < 1) qno = 0; 1183 if (arg == argv[0]) argv++, argc--; 1184 break; 1185 } else 1186 goto usage; 1187 case 'q': /* Number of query retries */ 1188 if (arg && isdigit((unsigned char)*arg)) { 1189 nqueries = atoi(arg); 1190 if (nqueries < 1) nqueries = 1; 1191 if (arg == argv[0]) argv++, argc--; 1192 break; 1193 } else 1194 goto usage; 1195 case 'g': /* Last-hop gateway (dest of query) */ 1196 if (arg && (gwy = host_addr(arg))) { 1197 if (arg == argv[0]) argv++, argc--; 1198 break; 1199 } else 1200 goto usage; 1201 case 't': /* TTL for query packet */ 1202 if (arg && isdigit((unsigned char)*arg)) { 1203 qttl = atoi(arg); 1204 if (qttl < 1) qttl = 1; 1205 rttl = qttl; 1206 if (arg == argv[0]) argv++, argc--; 1207 break; 1208 } else 1209 goto usage; 1210 case 'r': /* Dest for response packet */ 1211 if (arg && (raddr = host_addr(arg))) { 1212 if (arg == argv[0]) argv++, argc--; 1213 break; 1214 } else 1215 goto usage; 1216 case 'i': /* Local interface address */ 1217 if (arg && (lcl_addr = host_addr(arg))) { 1218 if (arg == argv[0]) argv++, argc--; 1219 break; 1220 } else 1221 goto usage; 1222 case 'S': /* Stat accumulation interval */ 1223 if (arg && isdigit((unsigned char)*arg)) { 1224 statint = atoi(arg); 1225 if (statint < 1) statint = 1; 1226 if (arg == argv[0]) argv++, argc--; 1227 break; 1228 } else 1229 goto usage; 1230 default: 1231 goto usage; 1232 } 1233 } while (*p); 1234 } 1235 1236 if (argc > 0 && (qsrc = host_addr(argv[0]))) { /* Source of path */ 1237 if (IN_MULTICAST(ntohl(qsrc))) goto usage; 1238 argv++, argc--; 1239 if (argc > 0 && (qdst = host_addr(argv[0]))) { /* Dest of path */ 1240 argv++, argc--; 1241 if (argc > 0 && (qgrp = host_addr(argv[0]))) { /* Path via group */ 1242 argv++, argc--; 1243 } 1244 if (IN_MULTICAST(ntohl(qdst))) { 1245 u_int32_t temp = qdst; 1246 qdst = qgrp; 1247 qgrp = temp; 1248 if (IN_MULTICAST(ntohl(qdst))) goto usage; 1249 } else if (qgrp && !IN_MULTICAST(ntohl(qgrp))) goto usage; 1250 } 1251 } 1252 1253 if (passive) { 1254 passive_mode(); 1255 return(0); 1256 } 1257 1258 if (argc > 0 || qsrc == 0) { 1259 usage: printf("\ 1260 usage: mtrace [-lMnpsv] [-g gateway] [-i if_addr] [-m max_hops] [-q nqueries]\n\ 1261 [-r host] [-S stat_int] [-t ttl] [-w waittime] source [receiver]\n\ 1262 [group]\n"); 1263 exit(1); 1264 } 1265 1266 /* 1267 * Set useful defaults for as many parameters as possible. 1268 */ 1269 1270 defgrp = htonl(0xE0020001); /* MBone Audio (224.2.0.1) */ 1271 query_cast = htonl(0xE0000002); /* All routers multicast addr */ 1272 resp_cast = htonl(0xE0000120); /* Mtrace response multicast addr */ 1273 if (qgrp == 0) qgrp = defgrp; 1274 1275 /* 1276 * Get default local address for multicasts to use in setting defaults. 1277 */ 1278 memset(&addr, 0, sizeof addr); 1279 addr.sin_family = AF_INET; 1280 #if (defined(BSD) && (BSD >= 199103)) 1281 addr.sin_len = sizeof(addr); 1282 #endif 1283 addr.sin_addr.s_addr = qgrp; 1284 addr.sin_port = htons(2000); /* Any port above 1024 will do */ 1285 1286 if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) || 1287 (connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) || 1288 getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) { 1289 perror("Determining local address"); 1290 exit(1); 1291 } 1292 1293 #ifdef SUNOS5 1294 /* 1295 * SunOS 5.X prior to SunOS 2.6, getsockname returns 0 for udp socket. 1296 * This call to sysinfo will return the hostname. 1297 * If the default multicast interface (set with the route 1298 * for 224.0.0.0) is not the same as the hostname, 1299 * mtrace -i [if_addr] will have to be used. 1300 */ 1301 if (addr.sin_addr.s_addr == 0) { 1302 char myhostname[MAXHOSTNAMELEN]; 1303 struct hostent *hp; 1304 int error; 1305 1306 error = sysinfo(SI_HOSTNAME, myhostname, sizeof(myhostname)); 1307 if (error == -1) { 1308 perror("Getting my hostname"); 1309 exit(1); 1310 } 1311 1312 hp = gethostbyname(myhostname); 1313 if (hp == NULL || hp->h_addrtype != AF_INET || 1314 hp->h_length != sizeof(addr.sin_addr)) { 1315 perror("Finding IP address for my hostname"); 1316 exit(1); 1317 } 1318 1319 memcpy((char *)&addr.sin_addr.s_addr, hp->h_addr, hp->h_length); 1320 } 1321 #endif 1322 1323 /* 1324 * Default destination for path to be queried is the local host. 1325 */ 1326 if (qdst == 0) qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr; 1327 dst_netmask = get_netmask(udp, qdst); 1328 close(udp); 1329 if (lcl_addr == 0) lcl_addr = addr.sin_addr.s_addr; 1330 1331 /* 1332 * Protect against unicast queries to mrouted versions that might crash. 1333 */ 1334 if (gwy && !IN_MULTICAST(ntohl(gwy))) 1335 if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0])) { 1336 int version = ntohl(incr[0].igmp.igmp_group.s_addr) & 0xFFFF; 1337 if (version == 0x0303 || version == 0x0503) { 1338 printf("Don't use -g to address an mrouted 3.%d, it might crash\n", 1339 (version >> 8) & 0xFF); 1340 exit(0); 1341 } 1342 } 1343 1344 printf("Mtrace from %s to %s via group %s\n", 1345 inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3)); 1346 1347 if ((qdst & dst_netmask) == (qsrc & dst_netmask)) { 1348 printf("Source & receiver are directly connected, no path to trace\n"); 1349 exit(0); 1350 } 1351 1352 /* 1353 * If the response is to be a multicast address, make sure we 1354 * are listening on that multicast address. 1355 */ 1356 if (raddr) { 1357 if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, lcl_addr); 1358 } else k_join(resp_cast, lcl_addr); 1359 1360 /* 1361 * If the destination is on the local net, the last-hop router can 1362 * be found by multicast to the all-routers multicast group. 1363 * Otherwise, use the group address that is the subject of the 1364 * query since by definition the last-hop router will be a member. 1365 * Set default TTLs for local remote multicasts. 1366 */ 1367 restart: 1368 1369 if (gwy == 0) 1370 if ((qdst & dst_netmask) == (lcl_addr & dst_netmask)) tdst = query_cast; 1371 else tdst = qgrp; 1372 else tdst = gwy; 1373 1374 if (IN_MULTICAST(ntohl(tdst))) { 1375 k_set_loop(1); /* If I am running on a router, I need to hear this */ 1376 if (tdst == query_cast) k_set_ttl(qttl ? qttl : 1); 1377 else k_set_ttl(qttl ? qttl : MULTICAST_TTL1); 1378 } 1379 1380 /* 1381 * Try a query at the requested number of hops or MAXHOPS if unspecified. 1382 */ 1383 if (qno == 0) { 1384 hops = MAXHOPS; 1385 tries = 1; 1386 printf("Querying full reverse path... "); 1387 fflush(stdout); 1388 } else { 1389 hops = qno; 1390 tries = nqueries; 1391 printf("Querying reverse path, maximum %d hops... ", qno); 1392 fflush(stdout); 1393 } 1394 base.rtime = 0; 1395 base.len = 0; 1396 1397 recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, tries, &base); 1398 1399 /* 1400 * If the initial query was successful, print it. Otherwise, if 1401 * the query max hop count is the default of zero, loop starting 1402 * from one until there is no response for four hops. The extra 1403 * hops allow getting past an mtrace-capable mrouter that can't 1404 * send multicast packets because all phyints are disabled. 1405 */ 1406 if (recvlen) { 1407 printf("\n 0 "); 1408 print_host(qdst); 1409 printf("\n"); 1410 print_trace(1, &base); 1411 r = base.resps + base.len - 1; 1412 if (r->tr_rflags == TR_OLD_ROUTER || r->tr_rflags == TR_NO_SPACE || 1413 qno != 0) { 1414 printf("%3d ", -(base.len+1)); 1415 what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ? 1416 "doesn't support mtrace" 1417 : "is the next hop"); 1418 } else { 1419 VAL_TO_MASK(smask, r->tr_smask); 1420 if ((r->tr_inaddr & smask) == (qsrc & smask)) { 1421 printf("%3d ", -(base.len+1)); 1422 print_host(qsrc); 1423 printf("\n"); 1424 } 1425 } 1426 } else if (qno == 0) { 1427 printf("switching to hop-by-hop:\n 0 "); 1428 print_host(qdst); 1429 printf("\n"); 1430 1431 for (hops = 1, nexthop = 1; hops <= MAXHOPS; ++hops) { 1432 printf("%3d ", -hops); 1433 fflush(stdout); 1434 1435 /* 1436 * After a successful first hop, try switching to the unicast 1437 * address of the last-hop router instead of multicasting the 1438 * trace query. This should be safe for mrouted versions 3.3 1439 * and 3.5 because there is a long route timeout with metric 1440 * infinity before a route disappears. Switching to unicast 1441 * reduces the amount of multicast traffic and avoids a bug 1442 * with duplicate suppression in mrouted 3.5. 1443 */ 1444 if (hops == 2 && gwy == 0 && 1445 (recvlen = send_recv(lastout, IGMP_MTRACE_QUERY, hops, 1, &base))) 1446 tdst = lastout; 1447 else recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, nqueries, &base); 1448 1449 if (recvlen == 0) { 1450 if (hops == 1) break; 1451 if (hops == nexthop) { 1452 if (what_kind(&base, "didn't respond")) { 1453 /* the ask_neighbors determined that the 1454 * not-responding router is the first-hop. */ 1455 break; 1456 } 1457 } else if (hops < nexthop + 3) { 1458 printf("\n"); 1459 } else { 1460 printf("...giving up\n"); 1461 break; 1462 } 1463 continue; 1464 } 1465 r = base.resps + base.len - 1; 1466 if (base.len == hops && 1467 (hops == 1 || (base.resps+nexthop-2)->tr_outaddr == lastout)) { 1468 if (hops == nexthop) { 1469 print_trace(-hops, &base); 1470 } else { 1471 printf("\nResuming...\n"); 1472 print_trace(nexthop, &base); 1473 } 1474 } else { 1475 if (base.len < hops) { 1476 /* 1477 * A shorter trace than requested means a fatal error 1478 * occurred along the path, or that the route changed 1479 * to a shorter one. 1480 * 1481 * If the trace is longer than the last one we received, 1482 * then we are resuming from a skipped router (but there 1483 * is still probably a problem). 1484 * 1485 * If the trace is shorter than the last one we 1486 * received, then the route must have changed (and 1487 * there is still probably a problem). 1488 */ 1489 if (nexthop <= base.len) { 1490 printf("\nResuming...\n"); 1491 print_trace(nexthop, &base); 1492 } else if (nexthop > base.len + 1) { 1493 hops = base.len; 1494 printf("\nRoute must have changed...\n"); 1495 print_trace(1, &base); 1496 } 1497 } else { 1498 /* 1499 * The last hop address is not the same as it was; 1500 * the route probably changed underneath us. 1501 */ 1502 hops = base.len; 1503 printf("\nRoute must have changed...\n"); 1504 print_trace(1, &base); 1505 } 1506 } 1507 lastout = r->tr_outaddr; 1508 1509 if (base.len < hops || 1510 r->tr_rmtaddr == 0 || 1511 (r->tr_rflags & 0x80)) { 1512 VAL_TO_MASK(smask, r->tr_smask); 1513 if (r->tr_rmtaddr) { 1514 if (hops != nexthop) { 1515 printf("\n%3d ", -(base.len+1)); 1516 } 1517 what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ? 1518 "doesn't support mtrace" : 1519 "would be the next hop"); 1520 /* XXX could do segmented trace if TR_NO_SPACE */ 1521 } else if (r->tr_rflags == TR_NO_ERR && 1522 (r->tr_inaddr & smask) == (qsrc & smask)) { 1523 printf("%3d ", -(hops + 1)); 1524 print_host(qsrc); 1525 printf("\n"); 1526 } 1527 break; 1528 } 1529 1530 nexthop = hops + 1; 1531 } 1532 } 1533 1534 if (base.rtime == 0) { 1535 printf("Timed out receiving responses\n"); 1536 if (IN_MULTICAST(ntohl(tdst))) 1537 if (tdst == query_cast) 1538 printf("Perhaps no local router has a route for source %s\n", 1539 inet_fmt(qsrc, s1)); 1540 else 1541 printf("Perhaps receiver %s is not a member of group %s,\n\ 1542 or no router local to it has a route for source %s,\n\ 1543 or multicast at ttl %d doesn't reach its last-hop router for that source\n", 1544 inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1), 1545 qttl ? qttl : MULTICAST_TTL1); 1546 exit(1); 1547 } 1548 1549 printf("Round trip time %d ms\n\n", t_diff(base.rtime, base.qtime)); 1550 1551 /* 1552 * Use the saved response which was the longest one received, 1553 * and make additional probes after delay to measure loss. 1554 */ 1555 raddr = base.qhdr.tr_raddr; 1556 rttl = base.qhdr.tr_rttl; 1557 gettimeofday(&tv, 0); 1558 waittime = statint - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16)); 1559 prev = &base; 1560 new = &incr[numstats&1]; 1561 1562 while (numstats--) { 1563 if (waittime < 1) 1564 printf("\n"); 1565 else { 1566 printf("Waiting to accumulate statistics... "); 1567 fflush(stdout); 1568 sleep((unsigned int)waittime); 1569 } 1570 rno = base.len; 1571 recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, rno, nqueries, new); 1572 1573 if (recvlen == 0) { 1574 printf("Timed out.\n"); 1575 exit(1); 1576 } 1577 1578 if (rno != new->len) { 1579 printf("Trace length doesn't match:\n"); 1580 /* 1581 * XXX Should this trace result be printed, or is that 1582 * too verbose? Perhaps it should just say restarting. 1583 * But if the path is changing quickly, this may be the 1584 * only snapshot of the current path. But, if the path 1585 * is changing that quickly, does the current path really 1586 * matter? 1587 */ 1588 print_trace(1, new); 1589 printf("Restarting.\n\n"); 1590 numstats++; 1591 goto restart; 1592 } 1593 1594 printf("Results after %d seconds:\n\n", 1595 (int)((new->qtime - base.qtime) >> 16)); 1596 fixup_stats(&base, prev, new); 1597 if (print_stats(&base, prev, new)) { 1598 printf("Route changed:\n"); 1599 print_trace(1, new); 1600 printf("Restarting.\n\n"); 1601 goto restart; 1602 } 1603 prev = new; 1604 new = &incr[numstats&1]; 1605 waittime = statint; 1606 } 1607 1608 /* 1609 * If the response was multicast back, leave the group 1610 */ 1611 if (raddr) { 1612 if (IN_MULTICAST(ntohl(raddr))) k_leave(raddr, lcl_addr); 1613 } else k_leave(resp_cast, lcl_addr); 1614 1615 return (0); 1616 } 1617 1618 void 1619 check_vif_state(void) 1620 { 1621 logit(LOG_WARNING, errno, "sendto"); 1622 } 1623 1624 /* 1625 * Log errors and other messages to stderr, according to the severity 1626 * of the message and the current debug level. For errors of severity 1627 * LOG_ERR or worse, terminate the program. 1628 */ 1629 void 1630 logit(int severity, int syserr, char *format, ...) 1631 { 1632 va_list ap; 1633 1634 switch (debug) { 1635 case 0: if (severity > LOG_WARNING) return; 1636 case 1: if (severity > LOG_NOTICE) return; 1637 case 2: if (severity > LOG_INFO ) return; 1638 default: 1639 if (severity == LOG_WARNING) 1640 fprintf(stderr, "warning - "); 1641 va_start(ap, format); 1642 vfprintf(stderr, format, ap); 1643 va_end(ap); 1644 if (syserr == 0) 1645 fprintf(stderr, "\n"); 1646 else if(syserr < sys_nerr) 1647 fprintf(stderr, ": %s\n", sys_errlist[syserr]); 1648 else 1649 fprintf(stderr, ": errno %d\n", syserr); 1650 } 1651 if (severity <= LOG_ERR) exit(1); 1652 } 1653 1654 /* dummies */ 1655 void accept_probe(u_int32_t src, u_int32_t dst, char *p, int datalen, 1656 u_int32_t level) 1657 { 1658 } 1659 1660 void accept_group_report(u_int32_t src, u_int32_t dst, u_int32_t group, 1661 int r_type) 1662 { 1663 } 1664 1665 void accept_neighbor_request2(u_int32_t src, u_int32_t dst) 1666 { 1667 } 1668 1669 void accept_report(u_int32_t src, u_int32_t dst, char *p, int datalen, 1670 u_int32_t level) 1671 { 1672 } 1673 1674 void accept_neighbor_request(u_int32_t src, u_int32_t dst) 1675 { 1676 } 1677 1678 void accept_prune(u_int32_t src, u_int32_t dst, char *p, int datalen) 1679 { 1680 } 1681 1682 void accept_graft(u_int32_t src, u_int32_t dst, char *p, int datalen) 1683 { 1684 } 1685 1686 void accept_g_ack(u_int32_t src, u_int32_t dst, char *p, int datalen) 1687 { 1688 } 1689 1690 void add_table_entry(u_int32_t origin, u_int32_t mcastgrp) 1691 { 1692 } 1693 1694 void accept_leave_message(u_int32_t src, u_int32_t dst, u_int32_t group) 1695 { 1696 } 1697 1698 void accept_mtrace(u_int32_t src, u_int32_t dst, u_int32_t group, char *data, 1699 u_int no, int datalen) 1700 { 1701 } 1702 1703 void accept_membership_query(u_int32_t src, u_int32_t dst, u_int32_t group, 1704 int tmo) 1705 { 1706 } 1707 1708 void accept_neighbors(u_int32_t src, u_int32_t dst, u_char *p, int datalen, 1709 u_int32_t level) 1710 { 1711 } 1712 1713 void accept_neighbors2(u_int32_t src, u_int32_t dst, u_char *p, int datalen, 1714 u_int32_t level) 1715 { 1716 } 1717 1718 void accept_info_request(u_int32_t src, u_int32_t dst, u_char *p, int datalen) 1719 { 1720 } 1721 1722 void accept_info_reply(u_int32_t src, u_int32_t dst, u_char *p, int datalen) 1723 { 1724 } 1725