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