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