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