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