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