mtrace.c revision 1.2 1 /*
2 * mtrace.c
3 *
4 * This tool traces the branch of a multicast tree from a source to a
5 * receiver for a particular multicast group and gives statistics
6 * about packet rate and loss for each hop along the path. It can
7 * usually be invoked just as
8 *
9 * mtrace source
10 *
11 * to trace the route from that source to the local host for a default
12 * group when only the route is desired and not group-specific packet
13 * counts. See the usage line for more complex forms.
14 *
15 *
16 * Released 4 Apr 1995. This program was adapted by Steve Casner
17 * (USC/ISI) from a prototype written by Ajit Thyagarajan (UDel and
18 * Xerox PARC). It attempts to parallel in command syntax and output
19 * format the unicast traceroute program written by Van Jacobson (LBL)
20 * for the parts where that makes sense.
21 *
22 * Copyright (c) 1995 by the University of Southern California
23 * All rights reserved.
24 *
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation in source and binary forms for non-commercial purposes
27 * and without fee is hereby granted, provided that the above copyright
28 * notice appear in all copies and that both the copyright notice and
29 * this permission notice appear in supporting documentation, and that
30 * any documentation, advertising materials, and other materials related
31 * to such distribution and use acknowledge that the software was
32 * developed by the University of Southern California, Information
33 * Sciences Institute. The name of the University may not be used to
34 * endorse or promote products derived from this software without
35 * specific prior written permission.
36 *
37 * THE UNIVERSITY OF SOUTHERN CALIFORNIA makes no representations about
38 * the suitability of this software for any purpose. THIS SOFTWARE IS
39 * PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
40 * INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
42 *
43 * Other copyrights might apply to parts of this software and are so
44 * noted when applicable.
45 *
46 * In particular, parts of the prototype version of this program may
47 * have been derived from mrouted programs sources covered by the
48 * license in the accompanying file named "LICENSE".
49 *
50 * $Id: mtrace.c,v 1.2 1995/06/01 23:27:00 thorpej Exp $
51 */
52
53 #include <sys/filio.h>
54 #include <sys/time.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #include <memory.h>
58 #include <netdb.h>
59 #include <string.h>
60 #include <stdlib.h>
61 #include <unistd.h>
62
63 extern int optind;
64 extern char *optarg;
65
66 #include "defs.h"
67
68 #define DEFAULT_TIMEOUT 3 /* How long to wait before retrying requests */
69 #define DEFAULT_RETRIES 3 /* How many times to try */
70 #define MAXHOPS UNREACHABLE /* Don't need more hops than max metric */
71 #define UNICAST_TTL 255 /* TTL for unicast response */
72 #define MULTICAST_TTL1 64 /* Default TTL for multicast query/response */
73 #define MULTICAST_TTL_INC 32 /* TTL increment for increase after timeout */
74 #define MULTICAST_TTL_MAX 192 /* Maximum TTL allowed (protect low-BW links */
75
76 struct resp_buf {
77 u_long qtime; /* Time query was issued */
78 u_long rtime; /* Time response was received */
79 int len; /* Number of reports or length of data */
80 struct igmp igmp; /* IGMP header */
81 union {
82 struct {
83 struct tr_query q; /* Query/response header */
84 struct tr_resp r[MAXHOPS]; /* Per-hop reports */
85 } t;
86 char d[MAX_DVMRP_DATA_LEN]; /* Neighbor data */
87 } u;
88 } base, incr[2];
89
90 #define qhdr u.t.q
91 #define resps u.t.r
92 #define ndata u.d
93
94 char names[MAXHOPS][40];
95
96 int timeout = DEFAULT_TIMEOUT;
97 int nqueries = DEFAULT_RETRIES;
98 int numeric = FALSE;
99 int debug = 0;
100 int passive = FALSE;
101 int multicast = FALSE;
102
103 u_int32_t defgrp; /* Default group if not specified */
104 u_int32_t query_cast; /* All routers multicast addr */
105 u_int32_t resp_cast; /* Mtrace response multicast addr */
106
107 u_int32_t lcl_addr = 0; /* This host address, in NET order */
108 u_int32_t dst_netmask; /* netmask to go with qdst */
109
110 /*
111 * Query/response parameters, all initialized to zero and set later
112 * to default values or from options.
113 */
114 u_int32_t qsrc = 0;
115 u_int32_t qgrp = 0;
116 u_int32_t qdst = 0;
117 u_char qno = 0;
118 u_int32_t raddr = 0;
119 int qttl = 0;
120 u_char rttl = 0;
121 u_int32_t gwy = 0;
122
123 vifi_t numvifs; /* to keep loader happy */
124 /* (see kern.c) */
125 extern void k_join();
126 extern void k_leave();
127 extern void k_set_ttl();
128 extern void exit();
129 #ifndef SYSV
130 extern long random();
131 #endif
132 extern int errno;
133
134 void
135 usage()
136 {
137
138 printf("\
139 Usage: mtrace [-Mlnps] [-w wait] [-m max_hops] [-q nqueries] [-g gateway]\n\
140 [-t ttl] [-r resp_dest] [-i if_addr] source [receiver] [group]\n");
141 exit(1);
142 }
143
144
145 char *
146 inet_name(addr)
147 u_int32_t addr;
148 {
149 struct hostent *e;
150
151 e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
152
153 return e ? e->h_name : "?";
154 }
155
156
157 u_int32_t
158 host_addr(name)
159 char *name;
160 {
161 struct hostent *e;
162 struct in_addr ina;
163 int i, dots = 3;
164 char buf[40];
165 char *ip = name;
166 char *op = buf;
167
168 /*
169 * Undo the BSD-ism `127.1' == `127.0.0.1'. We change this to
170 * `127.1' == `127.1.0.0'.
171 */
172
173 for (i = sizeof(buf) - 7; i > 0; --i) {
174 if (*ip == '.')
175 --dots;
176 if (*ip == '\0')
177 break;
178 *op++ = *ip++;
179 }
180 for (i = 0; i < dots; ++i) {
181 *op++ = '.';
182 *op++ = '0';
183 }
184 *op = '\0';
185
186 if (inet_aton(buf, &ina) == 0) {
187 if ((e = gethostbyname(name)) == NULL) {
188 ina.s_addr = 0;
189 printf("Could not parse %s as host name or address\n", name);
190 } else
191 memcpy((char *)&ina.s_addr, e->h_addr_list[0], e->h_length);
192 }
193
194 return (ina.s_addr);
195 }
196
197
198 char *
199 proto_type(type)
200 u_char type;
201 {
202 static char buf[80];
203
204 switch (type) {
205 case PROTO_DVMRP:
206 return ("DVMRP");
207 case PROTO_MOSPF:
208 return ("MOSPF");
209 case PROTO_PIM:
210 return ("PIM");
211 case PROTO_CBT:
212 return ("CBT");
213 default:
214 (void) sprintf(buf, "Unknown protocol code %d", type);
215 return (buf);
216 }
217 }
218
219
220 char *
221 flag_type(type)
222 u_char type;
223 {
224 static char buf[80];
225
226 switch (type) {
227 case TR_NO_ERR:
228 return ("");
229 case TR_WRONG_IF:
230 return ("Wrong interface");
231 case TR_PRUNED:
232 return ("Prune sent upstream");
233 case TR_OPRUNED:
234 return ("Output pruned");
235 case TR_SCOPED:
236 return ("Hit scope boundary");
237 case TR_NO_RTE:
238 return ("No route");
239 case TR_OLD_ROUTER:
240 return ("Next router no mtrace");
241 case TR_NO_FWD:
242 return ("Not forwarding");
243 case TR_NO_SPACE:
244 return ("No space in packet");
245 default:
246 (void) sprintf(buf, "Unknown error code %d", type);
247 return (buf);
248 }
249 }
250
251 /*
252 * If destination is on a local net, get the netmask, else set the
253 * netmask to all ones. There are two side effects: if the local
254 * address was not explicitly set, and if the destination is on a
255 * local net, use that one; in either case, verify that the local
256 * address is valid.
257 */
258
259 u_int32_t
260 get_netmask(s, dst)
261 int s;
262 u_int32_t dst;
263 {
264 char inbuf[8192];
265 struct ifconf ifc;
266 struct ifreq *ifr;
267 int i;
268 u_int32_t if_addr, if_mask;
269 u_int32_t retval = 0xFFFFFFFF;
270 int found = FALSE;
271
272 ifc.ifc_len = sizeof(inbuf);
273 ifc.ifc_buf = inbuf;
274 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
275 perror("ioctl (SIOCGIFCONF)");
276 return (retval);
277 }
278
279 for (i = 0; i < ifc.ifc_len; ) {
280 ifr = (struct ifreq *)((char *)ifc.ifc_req + i);
281 i += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
282 if_addr = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
283 if (ioctl(s, SIOCGIFNETMASK, (char *)ifr) >= 0) {
284 if_mask = ((struct sockaddr_in *)&(ifr->ifr_addr))->sin_addr.s_addr;
285 if ((dst & if_mask) == (if_addr & if_mask)) {
286 retval = if_mask;
287 if (lcl_addr == 0)
288 lcl_addr = if_addr;
289 }
290 }
291 if (lcl_addr == if_addr)
292 found = TRUE;
293 }
294 if (!found && lcl_addr != 0) {
295 printf("Interface address is not valid\n");
296 exit(1);
297 }
298 return (retval);
299 }
300
301
302 int
303 get_ttl(buf)
304 struct resp_buf *buf;
305 {
306 register rno;
307 register struct tr_resp *b;
308 register ttl;
309
310 if (buf && (rno = buf->len) > 0) {
311 b = buf->resps + rno - 1;
312 ttl = b->tr_fttl;
313
314 while (--rno > 0) {
315 --b;
316 if (ttl < b->tr_fttl)
317 ttl = b->tr_fttl;
318 else
319 ++ttl;
320 }
321 ttl += MULTICAST_TTL_INC;
322 if (ttl < MULTICAST_TTL1)
323 ttl = MULTICAST_TTL1;
324 if (ttl > MULTICAST_TTL_MAX)
325 ttl = MULTICAST_TTL_MAX;
326 return (ttl);
327 } else
328 return(MULTICAST_TTL1);
329 }
330
331 /*
332 * Calculate the difference between two 32-bit NTP timestamps and return
333 * the result in milliseconds.
334 */
335 int
336 t_diff(a, b)
337 u_long a, b;
338 {
339 int d = a - b;
340
341 return ((d * 125) >> 13);
342 }
343
344 /*
345 * Fixup for incorrect time format in 3.3 mrouted.
346 * This is possible because (JAN_1970 mod 64K) is quite close to 32K,
347 * so correct and incorrect times will be far apart.
348 */
349 u_long
350 fixtime(time)
351 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 int
360 send_recv(dst, type, code, tries, save)
361 u_int32_t dst;
362 int type, code, tries;
363 struct resp_buf *save;
364 {
365 fd_set fds;
366 struct timeval tq, tr, tv;
367 struct ip *ip;
368 struct igmp *igmp;
369 struct tr_query *query, *rquery;
370 int ipdatalen, iphdrlen, igmpdatalen;
371 u_int32_t local, group;
372 int datalen;
373 int count, recvlen, dummy = 0;
374 int len;
375 int i;
376
377 if (type == IGMP_MTRACE_QUERY) {
378 group = qgrp;
379 datalen = sizeof(struct tr_query);
380 } else {
381 group = htonl(MROUTED_LEVEL);
382 datalen = 0;
383 }
384 if (IN_MULTICAST(ntohl(dst)))
385 local = lcl_addr;
386 else
387 local = INADDR_ANY;
388
389 /*
390 * If the reply address was not explictly specified, start off
391 * with the unicast address of this host. Then, if there is no
392 * response after trying half the tries with unicast, switch to
393 * the standard multicast reply address. If the TTL was also not
394 * specified, set a multicast TTL and if needed increase it for the
395 * last quarter of the tries.
396 */
397 query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
398 query->tr_raddr = raddr ? raddr : multicast ? resp_cast : lcl_addr;
399 query->tr_rttl = rttl ? rttl :
400 IN_MULTICAST(ntohl(query->tr_raddr)) ? get_ttl(save) : UNICAST_TTL;
401
402 for (i = tries ; i > 0; --i) {
403 if (tries == nqueries && raddr == 0) {
404 if (i == ((nqueries + 1) >> 1)) {
405 query->tr_raddr = resp_cast;
406 if (rttl == 0) query->tr_rttl = get_ttl(save);
407 }
408 if (i <= ((nqueries + 3) >> 2) && rttl == 0) {
409 query->tr_rttl += MULTICAST_TTL_INC;
410 if (query->tr_rttl > MULTICAST_TTL_MAX)
411 query->tr_rttl = MULTICAST_TTL_MAX;
412 }
413 }
414
415 /*
416 * Change the qid for each request sent to avoid being confused
417 * by duplicate responses
418 */
419 query->tr_qid = ((u_int32_t)random() >> 8);
420
421 /*
422 * Set timer to calculate delays, then send query
423 */
424 gettimeofday(&tq, 0);
425 send_igmp(local, dst, type, code, group, datalen);
426
427 /*
428 * Wait for response, discarding false alarms
429 */
430 while (TRUE) {
431 FD_ZERO(&fds);
432 FD_SET(igmp_socket, &fds);
433 gettimeofday(&tv, 0);
434 tv.tv_sec = tq.tv_sec + timeout - tv.tv_sec;
435 tv.tv_usec = tq.tv_usec - tv.tv_usec;
436 if (tv.tv_usec < 0)
437 tv.tv_usec += 1000000L, --tv.tv_sec;
438 if (tv.tv_sec < 0)
439 tv.tv_sec = tv.tv_usec = 0;
440
441 count = select(igmp_socket + 1, &fds, (fd_set *)0, (fd_set *)0,
442 &tv);
443
444 if (count < 0) {
445 if (errno != EINTR)
446 perror("select");
447 continue;
448 } else if (count == 0) {
449 printf("* ");
450 fflush(stdout);
451 break;
452 }
453
454 gettimeofday(&tr, 0);
455 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
456 0, (struct sockaddr *)0, &dummy);
457
458 if (recvlen <= 0) {
459 if (recvlen && errno != EINTR)
460 perror("recvfrom");
461 continue;
462 }
463
464 if (recvlen < sizeof(struct ip)) {
465 fprintf(stderr,
466 "packet too short (%u bytes) for IP header", recvlen);
467 continue;
468 }
469 ip = (struct ip *) recv_buf;
470 if (ip->ip_p == 0) /* ignore cache creation requests */
471 continue;
472
473 iphdrlen = ip->ip_hl << 2;
474 ipdatalen = ip->ip_len;
475 if (iphdrlen + ipdatalen != recvlen) {
476 fprintf(stderr,
477 "packet shorter (%u bytes) than hdr+data len (%u+%u)\n",
478 recvlen, iphdrlen, ipdatalen);
479 continue;
480 }
481
482 igmp = (struct igmp *) (recv_buf + iphdrlen);
483 igmpdatalen = ipdatalen - IGMP_MINLEN;
484 if (igmpdatalen < 0) {
485 fprintf(stderr,
486 "IP data field too short (%u bytes) for IGMP from %s\n",
487 ipdatalen, inet_fmt(ip->ip_src.s_addr, s1));
488 continue;
489 }
490
491 switch (igmp->igmp_type) {
492
493 case IGMP_DVMRP:
494 if (igmp->igmp_code != DVMRP_NEIGHBORS2)
495 continue;
496 if (ip->ip_src.s_addr != dst)
497 continue;
498 len = igmpdatalen;
499 break;
500
501 case IGMP_MTRACE_QUERY: /* For backward compatibility with 3.3 */
502 case IGMP_MTRACE_REPLY:
503 if (igmpdatalen <= QLEN)
504 continue;
505 if ((igmpdatalen - QLEN)%RLEN) {
506 printf("packet with incorrect datalen\n");
507 continue;
508 }
509
510 /*
511 * Ignore responses that don't match query.
512 */
513 rquery = (struct tr_query *)(igmp + 1);
514 if (rquery->tr_qid != query->tr_qid)
515 continue;
516 if (rquery->tr_src != qsrc)
517 continue;
518 if (rquery->tr_dst != qdst)
519 continue;
520 len = (igmpdatalen - QLEN)/RLEN;
521
522 /*
523 * Ignore trace queries passing through this node when
524 * mtrace is run on an mrouter that is in the path
525 * (needed only because IGMP_MTRACE is accepted above
526 * for backward compatibility with multicast release 3.3).
527 */
528 if (igmp->igmp_type == IGMP_MTRACE_QUERY) {
529 struct tr_resp *r = (struct tr_resp *)(rquery+1) + len - 1;
530 u_int32_t smask;
531
532 VAL_TO_MASK(smask, r->tr_smask);
533 if (len < code && (r->tr_inaddr & smask) != (qsrc & smask)
534 && r->tr_rmtaddr != 0 && !(r->tr_rflags & 0x80))
535 continue;
536 }
537
538 /*
539 * A match, we'll keep this one.
540 */
541 if (len > code) {
542 fprintf(stderr,
543 "Num hops received (%d) exceeds request (%d)\n",
544 len, code);
545 }
546 rquery->tr_raddr = query->tr_raddr; /* Insure these are */
547 rquery->tr_rttl = query->tr_rttl; /* as we sent them */
548 break;
549
550 default:
551 continue;
552 }
553
554 /*
555 * Most of the sanity checking done at this point.
556 * Return this packet we have been waiting for.
557 */
558 if (save) {
559 save->qtime = ((tq.tv_sec + JAN_1970) << 16) +
560 (tq.tv_usec << 10) / 15625;
561 save->rtime = ((tr.tv_sec + JAN_1970) << 16) +
562 (tr.tv_usec << 10) / 15625;
563 save->len = len;
564 bcopy((char *)igmp, (char *)&save->igmp, ipdatalen);
565 }
566 return (recvlen);
567 }
568 }
569 return (0);
570 }
571
572
573 char *
574 print_host(addr)
575 u_int32_t addr;
576 {
577 char *name;
578
579 if (numeric) {
580 printf("%s", inet_fmt(addr, s1));
581 return ("");
582 }
583 name = inet_name(addr);
584 printf("%s (%s)", name, inet_fmt(addr, s1));
585 return (name);
586 }
587
588 /*
589 * Print responses as received (reverse path from dst to src)
590 */
591 void
592 print_trace(index, buf)
593 int index;
594 struct resp_buf *buf;
595 {
596 struct tr_resp *r;
597 char *name;
598 int i;
599
600 i = abs(index);
601 r = buf->resps + i - 1;
602
603 for (; i <= buf->len; ++i, ++r) {
604 if (index > 0) printf("%3d ", -i);
605 name = print_host(r->tr_outaddr);
606 printf(" %s thresh^ %d %d ms %s\n", proto_type(r->tr_rproto),
607 r->tr_fttl, t_diff(fixtime(ntohl(r->tr_qarr)), buf->qtime),
608 flag_type(r->tr_rflags));
609 memcpy(names[i-1], name, sizeof(names[0]) - 1);
610 names[i-1][sizeof(names[0])-1] = '\0';
611 }
612 }
613
614 /*
615 * See what kind of router is the next hop
616 */
617 void
618 what_kind(buf)
619 struct resp_buf *buf;
620 {
621 u_int32_t smask;
622 int recvlen;
623 int hops = buf->len;
624 struct tr_resp *r = buf->resps + hops - 1;
625 u_int32_t next = r->tr_rmtaddr;
626
627 recvlen = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]);
628 print_host(next);
629 if (recvlen) {
630 u_int32_t version = ntohl(incr[0].igmp.igmp_group.s_addr);
631 u_int32_t *p = (u_int32_t *)incr[0].ndata;
632 u_int32_t *ep = p + (incr[0].len >> 2);
633 printf(" [%s%d.%d] didn't respond\n",
634 (version == 1) ? "proteon/mrouted " :
635 ((version & 0xff) == 2) ? "mrouted " :
636 ((version & 0xff) == 3) ? "mrouted " :
637 ((version & 0xff) == 4) ? "mrouted " :
638 ((version & 0xff) == 10) ? "cisco " : "",
639 version & 0xff, (version >> 8) & 0xff);
640 VAL_TO_MASK(smask, r->tr_smask);
641 while (p < ep) {
642 register u_int32_t laddr = *p++;
643 register int n = ntohl(*p++) & 0xFF;
644 if ((laddr & smask) == (qsrc & smask)) {
645 printf("%3d ", -(hops+2));
646 print_host(qsrc);
647 printf("\n");
648 break;
649 }
650 p += n;
651 }
652 return;
653 }
654 printf(" didn't respond\n");
655 }
656
657
658 char *
659 scale(hop)
660 int *hop;
661 {
662 if (*hop > -1000 && *hop < 10000) return (" ms");
663 *hop /= 1000;
664 if (*hop > -1000 && *hop < 10000) return (" s ");
665 return ("s ");
666 }
667
668 /*
669 * Calculate and print one line of packet loss and packet rate statistics.
670 * Checks for count of all ones from mrouted 2.3 that doesn't have counters.
671 */
672 #define NEITHER 0
673 #define INS 1
674 #define OUTS 2
675 #define BOTH 3
676 void
677 stat_line(r, s, have_next)
678 struct tr_resp *r, *s;
679 int have_next;
680 {
681 register timediff = (fixtime(ntohl(s->tr_qarr)) -
682 fixtime(ntohl(r->tr_qarr))) >> 16;
683 register v_lost, v_pct;
684 register g_lost, g_pct;
685 register v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout);
686 register g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt);
687 register v_pps, g_pps;
688 char v_str[8], g_str[8];
689 register have = NEITHER;
690
691 if (timediff == 0)
692 timediff = 1;
693 v_pps = v_out / timediff;
694 g_pps = g_out / timediff;
695
696 if (v_out || s->tr_vifout != 0xFFFFFFFF)
697 have |= OUTS;
698
699 if (have_next) {
700 --r, --s;
701 if (s->tr_vifin != 0xFFFFFFFF || r->tr_vifin != 0xFFFFFFFF)
702 have |= INS;
703 }
704
705 switch (have) {
706 case BOTH:
707 v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
708 if (v_out)
709 v_pct = (v_lost * 100 + (v_out >> 1)) / v_out;
710 else
711 v_pct = 0;
712 if (-100 < v_pct && v_pct < 101 && v_out > 10)
713 sprintf(v_str, "%3d", v_pct);
714 else
715 memcpy(v_str, " --", 4);
716
717 g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
718 if (g_out)
719 g_pct = (g_lost * 100 + (g_out >> 1))/ g_out;
720 else
721 g_pct = 0;
722 if (-100 < g_pct && g_pct < 101 && g_out > 10)
723 sprintf(g_str, "%3d", g_pct);
724 else
725 memcpy(g_str, " --", 4);
726
727 printf("%6d/%-5d=%s%%%4d pps%6d/%-5d=%s%%%4d pps\n",
728 v_lost, v_out, v_str, v_pps, g_lost, g_out, g_str, g_pps);
729 if (debug > 2) {
730 printf("\t\t\t\tv_in: %ld ", ntohl(s->tr_vifin));
731 printf("v_out: %ld ", ntohl(s->tr_vifout));
732 printf("pkts: %ld\n", ntohl(s->tr_pktcnt));
733 printf("\t\t\t\tv_in: %ld ", ntohl(r->tr_vifin));
734 printf("v_out: %ld ", ntohl(r->tr_vifout));
735 printf("pkts: %ld\n", ntohl(r->tr_pktcnt));
736 printf("\t\t\t\tv_in: %ld ",ntohl(s->tr_vifin)-ntohl(r->tr_vifin));
737 printf("v_out: %ld ", ntohl(s->tr_vifout) - ntohl(r->tr_vifout));
738 printf("pkts: %ld ", ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
739 printf("time: %d\n", timediff);
740 }
741 break;
742
743 case INS:
744 v_out = (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
745 g_out = (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
746 v_pps = v_out / timediff;
747 g_pps = g_out / timediff;
748 /* Fall through */
749
750 case OUTS:
751 printf(" %-5d %4d pps %-5d %4d pps\n",
752 v_out, v_pps, g_out, g_pps);
753 break;
754
755 case NEITHER:
756 printf("\n");
757 break;
758 }
759 }
760
761 /*
762 * A fixup to check if any pktcnt has been reset.
763 */
764 void
765 fixup_stats(base, new)
766 struct resp_buf *base, *new;
767 {
768 register rno = base->len;
769 register struct tr_resp *b = base->resps + rno;
770 register struct tr_resp *n = new->resps + rno;
771
772 while (--rno >= 0)
773 if (ntohl((--n)->tr_pktcnt) < ntohl((--b)->tr_pktcnt))
774 break;
775
776 if (rno < 0)
777 return;
778
779 rno = base->len;
780 b = base->resps + rno;
781 n = new->resps + rno;
782
783 while (--rno >= 0)
784 (--b)->tr_pktcnt = (--n)->tr_pktcnt;
785 }
786
787 /*
788 * Print responses with statistics for forward path (from src to dst)
789 */
790 void
791 print_stats(base, prev, new)
792 struct resp_buf *base, *prev, *new;
793 {
794 int rtt, hop;
795 register char *ms;
796 register u_int32_t smask;
797 register rno = base->len - 1;
798 register struct tr_resp *b = base->resps + rno;
799 register struct tr_resp *p = prev->resps + rno;
800 register struct tr_resp *n = new->resps + rno;
801 register u_long resptime = new->rtime;
802 register u_long qarrtime = fixtime(ntohl(n->tr_qarr));
803 register ttl = n->tr_fttl;
804
805 VAL_TO_MASK(smask, b->tr_smask);
806 printf(" Source Response Dest");
807 printf(" Packet Statistics For Only For Traffic\n");
808 printf("%-15s %-15s All Multicast Traffic From %s\n",
809 ((b->tr_inaddr & smask) == (qsrc & smask)) ? s1 : " * * * ",
810 inet_fmt(base->qhdr.tr_raddr, s2), inet_fmt(qsrc, s1));
811 rtt = t_diff(resptime, new->qtime);
812 ms = scale(&rtt);
813 printf(" | __/ rtt%5d%s Lost/Sent = Pct Rate To %s\n",
814 rtt, ms, inet_fmt(qgrp, s2));
815 hop = t_diff(resptime, qarrtime);
816 ms = scale(&hop);
817 printf(" v / hop%5d%s", hop, ms);
818 printf(" --------------------- --------------------\n");
819 if (debug > 2) {
820 printf("\t\t\t\tv_in: %ld ", ntohl(n->tr_vifin));
821 printf("v_out: %ld ", ntohl(n->tr_vifout));
822 printf("pkts: %ld\n", ntohl(n->tr_pktcnt));
823 printf("\t\t\t\tv_in: %ld ", ntohl(b->tr_vifin));
824 printf("v_out: %ld ", ntohl(b->tr_vifout));
825 printf("pkts: %ld\n", ntohl(b->tr_pktcnt));
826 printf("\t\t\t\tv_in: %ld ", ntohl(n->tr_vifin) - ntohl(b->tr_vifin));
827 printf("v_out: %ld ", ntohl(n->tr_vifout) - ntohl(b->tr_vifout));
828 printf("pkts: %ld\n", ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt));
829 }
830
831 while (TRUE) {
832 if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_inaddr != b->tr_inaddr)) {
833 printf("Route changed, start again.\n");
834 exit(1);
835 }
836 if ((n->tr_inaddr != n->tr_outaddr))
837 printf("%-15s\n", inet_fmt(n->tr_inaddr, s1));
838 printf("%-15s %-14s %s\n", inet_fmt(n->tr_outaddr, s1), names[rno],
839 flag_type(n->tr_rflags));
840
841 if (rno-- < 1) break;
842
843 printf(" | ^ ttl%5d ", ttl);
844 if (prev == new)
845 printf("\n");
846 else
847 stat_line(p, n, TRUE);
848 resptime = qarrtime;
849 qarrtime = fixtime(ntohl((n-1)->tr_qarr));
850 hop = t_diff(resptime, qarrtime);
851 ms = scale(&hop);
852 printf(" v | hop%5d%s", hop, ms);
853 stat_line(b, n, TRUE);
854
855 --b, --p, --n;
856 if (ttl < n->tr_fttl)
857 ttl = n->tr_fttl;
858 else
859 ++ttl;
860 }
861
862 printf(" | \\__ ttl%5d ", ttl);
863 if (prev == new)
864 printf("\n");
865 else
866 stat_line(p, n, FALSE);
867 hop = t_diff(qarrtime, new->qtime);
868 ms = scale(&hop);
869 printf(" v \\ hop%5d%s", hop, ms);
870 stat_line(b, n, FALSE);
871 printf("%-15s %s\n", inet_fmt(qdst, s1), inet_fmt(lcl_addr, s2));
872 printf(" Receiver Query Source\n\n");
873 }
874
875
876 /***************************************************************************
877 * main
878 ***************************************************************************/
879
880 int
881 main(argc, argv)
882 int argc;
883 char *argv[];
884 {
885 int udp;
886 struct sockaddr_in addr;
887 int addrlen = sizeof(addr);
888 int recvlen;
889 struct timeval tv;
890 struct resp_buf *prev, *new;
891 struct tr_query *query;
892 struct tr_resp *r;
893 u_int32_t smask;
894 int rno;
895 int hops, tries;
896 int numstats = 1;
897 int waittime;
898 int seed;
899 int ch;
900
901 if (geteuid() != 0) {
902 fprintf(stderr, "mtrace: must be root\n");
903 exit(1);
904 }
905
906 while ((ch = getopt(argc, argv, "d:g:i:lMm:npq:r:s:t:w:")) != -1) {
907 switch (ch) {
908 case 'd': /* Unlisted debug print option */
909 if (!isdigit(*optarg))
910 usage();
911 debug = atoi(optarg);
912 if (debug < 0)
913 debug = 0;
914 else if (debug > 3)
915 debug = 3;
916 break;
917
918 case 'M': /* Use multicast for reponse */
919 multicast = TRUE;
920 break;
921
922 case 'l': /* Loop updating stats indefinitely */
923 numstats = 3153600;
924 break;
925
926 case 'n': /* Don't reverse map host addresses */
927 numeric = TRUE;
928 break;
929
930 case 'p': /* Passive listen for traces */
931 passive = TRUE;
932 break;
933
934 /* XXX: This is totally wrong, according to the manpage. */
935 case 's': /* Short form, don't wait for stats */
936 numstats = 0;
937 break;
938
939 case 'w': /* Time to wait for packet arrival */
940 if (!isdigit(*optarg))
941 usage();
942 timeout = atoi(optarg);
943 if (timeout < 1)
944 timeout = 1;
945 break;
946
947 case 'm': /* Max number of hops to trace */
948 if (!isdigit(*optarg))
949 usage();
950 qno = atoi(optarg);
951 if (qno > MAXHOPS)
952 qno = MAXHOPS;
953 else if (qno < 1)
954 qno = 0;
955 break;
956
957 case 'q': /* Number of query retries */
958 if (!isdigit(*optarg))
959 usage();
960 nqueries = atoi(optarg);
961 if (nqueries < 1)
962 nqueries = 1;
963 break;
964
965 case 'g': /* Last-hop gateway (dest of query) */
966 if ((gwy = host_addr(optarg)) == 0)
967 usage();
968 break;
969
970 case 't': /* TTL for query packet */
971 if (!isdigit(*optarg))
972 usage();
973 qttl = atoi(optarg);
974 if (qttl < 1)
975 qttl = 1;
976 rttl = qttl;
977 break;
978
979 case 'r': /* Dest for response packet */
980 if ((raddr = host_addr(optarg)) == 0)
981 usage();
982 break;
983
984 case 'i': /* Local interface address */
985 if ((lcl_addr = host_addr(optarg)) == 0)
986 usage();
987 break;
988
989 default:
990 usage();
991 } /* switch */
992 } /* while */
993 argv += optind;
994 argc -= optind;
995
996 switch (argc) {
997 case 3: /* Path via group */
998 if ((qgrp = host_addr(argv[2])) == 0)
999 usage();
1000 /* FALLTHROUGH */
1001 case 2: /* dest of path */
1002 if ((qdst = host_addr(argv[1])) == 0)
1003 usage();
1004 /* FALLTHROUGH */
1005 case 1: /* source of path */
1006 if ((qsrc = host_addr(argv[0])) == 0 || IN_MULTICAST(ntohl(qsrc)))
1007 usage();
1008 break;
1009
1010 default:
1011 usage();
1012 }
1013
1014 /*
1015 * If argc is > 1 and the second argument is a multicast address,
1016 * assume that the second argument is actually qgrp and the third
1017 * (if any) is qdst; in this case, the third argument is not allowed
1018 * to be a multicast address.
1019 */
1020 if (argc > 1) {
1021 if (IN_MULTICAST(ntohl(qdst))) {
1022 u_int32_t temp = qdst;
1023 qdst = qgrp;
1024 qgrp = temp;
1025 if (IN_MULTICAST(ntohl(qdst)))
1026 usage();
1027 } else if (qgrp != 0 && !IN_MULTICAST(ntohl(qgrp)))
1028 usage();
1029 }
1030
1031 if (qsrc == 0)
1032 usage();
1033
1034 init_igmp();
1035
1036 /*
1037 * Set useful defaults for as many parameters as possible.
1038 */
1039
1040 defgrp = htonl(0xE0020001); /* MBone Audio (224.2.0.1) */
1041 query_cast = htonl(0xE0000002); /* All routers multicast addr */
1042 resp_cast = htonl(0xE0000120); /* Mtrace response multicast addr */
1043 if (qgrp == 0)
1044 qgrp = defgrp;
1045
1046 /*
1047 * Get default local address for multicasts to use in setting defaults.
1048 */
1049 addr.sin_family = AF_INET;
1050 #if (defined(BSD) && (BSD >= 199103))
1051 addr.sin_len = sizeof(addr);
1052 #endif
1053 addr.sin_addr.s_addr = qgrp;
1054 addr.sin_port = htons(2000); /* Any port above 1024 will do */
1055
1056 if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) ||
1057 (connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) ||
1058 getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
1059 perror("Determining local address");
1060 exit(-1);
1061 }
1062
1063 /*
1064 * Default destination for path to be queried is the local host.
1065 */
1066 if (qdst == 0)
1067 qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr;
1068
1069 /*
1070 * If the destination is on the local net, the last-hop router can
1071 * be found by multicast to the all-routers multicast group.
1072 * Otherwise, use the group address that is the subject of the
1073 * query since by definition the last hop router will be a member.
1074 * Set default TTLs for local remote multicasts.
1075 */
1076 dst_netmask = get_netmask(udp, qdst);
1077 close(udp);
1078 if (lcl_addr == 0)
1079 lcl_addr = addr.sin_addr.s_addr;
1080 if (gwy == 0)
1081 if ((qdst & dst_netmask) == (lcl_addr & dst_netmask))
1082 gwy = query_cast;
1083 else
1084 gwy = qgrp;
1085
1086 if (IN_MULTICAST(ntohl(gwy))) {
1087 k_set_loop(1); /* If I am running on a router, I need to hear this */
1088 if (gwy == query_cast)
1089 k_set_ttl(qttl ? qttl : 1);
1090 else
1091 k_set_ttl(qttl ? qttl : MULTICAST_TTL1);
1092 } else
1093 if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]))
1094 if (ntohl(incr[0].igmp.igmp_group.s_addr) == 0x0303) {
1095 printf("Don't use -g to address an mrouted 3.3, it might crash\n");
1096 exit(0);
1097 }
1098
1099 printf("Mtrace from %s to %s via group %s\n",
1100 inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3));
1101
1102 if ((qdst & dst_netmask) == (qsrc & dst_netmask)) {
1103 printf("Source & receiver are directly connected, no path to trace\n");
1104 exit(0);
1105 }
1106
1107 /*
1108 * Make up the IGMP_MTRACE_QUERY query packet to send (some parameters
1109 * are set later), including initializing the seed for random
1110 * query identifiers.
1111 */
1112 query = (struct tr_query *)(send_buf + MIN_IP_HEADER_LEN + IGMP_MINLEN);
1113 query->tr_src = qsrc;
1114 query->tr_dst = qdst;
1115
1116 gettimeofday(&tv, 0);
1117 seed = tv.tv_usec ^ lcl_addr;
1118 srandom(seed);
1119
1120 /*
1121 * If the response is to be a multicast address, make sure we
1122 * are listening on that multicast address.
1123 */
1124 if (raddr && IN_MULTICAST(ntohl(raddr)))
1125 k_join(raddr, lcl_addr);
1126 else
1127 k_join(resp_cast, lcl_addr);
1128
1129 /*
1130 * Try a query at the requested number of hops or MAXOPS if unspecified.
1131 */
1132 if (qno == 0) {
1133 hops = MAXHOPS;
1134 tries = 1;
1135 printf("Querying full reverse path... ");
1136 fflush(stdout);
1137 } else {
1138 hops = qno;
1139 tries = nqueries;
1140 printf("Querying reverse path, maximum %d hops... ", qno);
1141 fflush(stdout);
1142 }
1143 base.rtime = 0;
1144 base.len = 0;
1145
1146 recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, hops, tries, &base);
1147
1148 /*
1149 * If the initial query was successful, print it. Otherwise, if
1150 * the query max hop count is the default of zero, loop starting
1151 * from one until a timeout occurs.
1152 */
1153 if (recvlen) {
1154 printf("\n 0 ");
1155 print_host(qdst);
1156 printf("\n");
1157 print_trace(1, &base);
1158 r = base.resps + base.len - 1;
1159 if (r->tr_rflags == TR_OLD_ROUTER) {
1160 printf("%3d ", -(base.len+1));
1161 fflush(stdout);
1162 what_kind(&base);
1163 } else {
1164 VAL_TO_MASK(smask, r->tr_smask);
1165 if ((r->tr_inaddr & smask) == (qsrc & smask)) {
1166 printf("%3d ", -(base.len+1));
1167 print_host(qsrc);
1168 printf("\n");
1169 }
1170 }
1171 } else if (qno == 0) {
1172 printf("switching to hop-by-hop:\n 0 ");
1173 print_host(qdst);
1174 printf("\n");
1175
1176 for (hops = 1; hops <= MAXHOPS; ++hops) {
1177 printf("%3d ", -hops);
1178 fflush(stdout);
1179
1180 recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, hops, nqueries, &base);
1181
1182 if (recvlen == 0) {
1183 if (--hops == 0) break;
1184 what_kind(&base);
1185 break;
1186 }
1187 r = base.resps + base.len - 1;
1188 if (base.len == hops)
1189 print_trace(-hops, &base);
1190 else {
1191 hops = base.len;
1192 if (r->tr_rflags == TR_OLD_ROUTER) {
1193 what_kind(&base);
1194 break;
1195 }
1196 if (r->tr_rflags == TR_NO_SPACE) {
1197 printf("No space left in trace packet for further hops\n");
1198 break; /* XXX could do segmented trace */
1199 }
1200 printf("Route must have changed...\n\n");
1201 print_trace(1, &base);
1202 }
1203
1204 VAL_TO_MASK(smask, r->tr_smask);
1205 if ((r->tr_inaddr & smask) == (qsrc & smask)) {
1206 printf("%3d ", -(hops+1));
1207 print_host(qsrc);
1208 printf("\n");
1209 break;
1210 }
1211 if (r->tr_rmtaddr == 0 || (r->tr_rflags & 0x80))
1212 break;
1213 }
1214 }
1215
1216 if (base.rtime == 0) {
1217 printf("Timed out receiving responses\n");
1218 if (IN_MULTICAST(ntohl(gwy)))
1219 if (gwy == query_cast)
1220 printf("Perhaps no local router has a route for source %s\n",
1221 inet_fmt(qsrc, s1));
1222 else
1223 printf("Perhaps receiver %s is not a member of group %s,\n\
1224 or no router local to it has a route for source %s,\n\
1225 or multicast at ttl %d doesn't reach its last-hop router for that source\n",
1226 inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1),
1227 qttl ? qttl : MULTICAST_TTL1);
1228 exit(1);
1229 }
1230
1231 printf("Round trip time %d ms\n\n", t_diff(base.rtime, base.qtime));
1232
1233 /*
1234 * Use the saved response which was the longest one received,
1235 * and make additional probes after delay to measure loss.
1236 */
1237 raddr = base.qhdr.tr_raddr;
1238 rttl = base.qhdr.tr_rttl;
1239 gettimeofday(&tv, 0);
1240 waittime = 10 - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16));
1241 prev = new = &incr[numstats&1];
1242
1243 while (numstats--) {
1244 if (waittime < 1) printf("\n");
1245 else {
1246 printf("Waiting to accumulate statistics... ");
1247 fflush(stdout);
1248 sleep((unsigned)waittime);
1249 }
1250 rno = base.len;
1251 recvlen = send_recv(gwy, IGMP_MTRACE_QUERY, rno, nqueries, new);
1252
1253 if (recvlen == 0) {
1254 printf("Timed out.\n");
1255 exit(1);
1256 }
1257
1258 if (rno != new->len) {
1259 printf("Trace length doesn't match.\n");
1260 exit(1);
1261 }
1262
1263 printf("Results after %d seconds:\n\n",
1264 (new->qtime - base.qtime) >> 16);
1265 fixup_stats(&base, new);
1266 print_stats(&base, prev, new);
1267 prev = new;
1268 new = &incr[numstats&1];
1269 waittime = 10;
1270 }
1271
1272 /*
1273 * If the response was multicast back, leave the group
1274 */
1275 if (raddr && IN_MULTICAST(ntohl(raddr))) k_leave(raddr, lcl_addr);
1276 else k_leave(resp_cast, lcl_addr);
1277
1278 return (0);
1279 }
1280
1281 void
1282 check_vif_state()
1283 {
1284 log(LOG_WARNING, errno, "sendto");
1285 }
1286
1287 /*
1288 * Log errors and other messages to stderr, according to the severity
1289 * of the message and the current debug level. For errors of severity
1290 * LOG_ERR or worse, terminate the program.
1291 */
1292 /*VARARGS3*/
1293 void
1294 log(severity, syserr, format, a, b, c, d, e)
1295 int severity, syserr;
1296 char *format;
1297 int a, b, c, d, e;
1298 {
1299 char fmt[100];
1300
1301 switch (debug) {
1302 case 0:
1303 if (severity > LOG_WARNING)
1304 return;
1305 case 1:
1306 if (severity > LOG_NOTICE)
1307 return;
1308 case 2:
1309 if (severity > LOG_INFO)
1310 return;
1311 default:
1312 fmt[0] = '\0';
1313 if (severity == LOG_WARNING) strcat(fmt, "warning - ");
1314 strncat(fmt, format, 80);
1315 fprintf(stderr, fmt, a, b, c, d, e);
1316 if (syserr == 0)
1317 fprintf(stderr, "\n");
1318 else if(syserr < sys_nerr)
1319 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
1320 else
1321 fprintf(stderr, ": errno %d\n", syserr);
1322 }
1323 if (severity <= LOG_ERR)
1324 exit(-1);
1325 }
1326
1327 /* dummies */
1328
1329 /*VARARGS*/
1330 void accept_probe() {} /*VARARGS*/
1331 void accept_group_report() {} /*VARARGS*/
1332 void accept_neighbors() {} /*VARARGS*/
1333 void accept_neighbors2() {} /*VARARGS*/
1334 void accept_neighbor_request() {} /*VARARGS*/
1335 void accept_neighbor_request2() {} /*VARARGS*/
1336 void accept_report() {} /*VARARGS*/
1337 void accept_prune() {} /*VARARGS*/
1338 void accept_graft() {} /*VARARGS*/
1339 void accept_g_ack() {} /*VARARGS*/
1340 void add_table_entry() {} /*VARARGS*/
1341 void accept_mtrace() {} /*VARARGS*/
1342 void accept_leave_message() {} /*VARARGS*/
1343 void accept_membership_query() {} /*VARARGS*/
1344