mtrace.c revision 1.13 1 /* $NetBSD: mtrace.c,v 1.13 1999/05/23 16:15:18 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.13 1999/05/23 16:15:18 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, *ft;
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 ft = flag_type(r->tr_rflags);
811 if (strlen(ft) != 0)
812 printf(" %s", ft);
813 printf("\n");
814 memcpy(names[i-1], name, sizeof(names[0]) - 1);
815 names[i-1][sizeof(names[0])-1] = '\0';
816 }
817 }
818
819 /*
820 * See what kind of router is the next hop
821 */
822 int
823 what_kind(buf, why)
824 struct resp_buf *buf;
825 char *why;
826 {
827 u_int32_t smask;
828 int retval;
829 int hops = buf->len;
830 struct tr_resp *r = buf->resps + hops - 1;
831 u_int32_t next = r->tr_rmtaddr;
832
833 retval = send_recv(next, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0]);
834 print_host(next);
835 if (retval) {
836 u_int32_t version = ntohl(incr[0].igmp.igmp_group.s_addr);
837 u_int32_t *p = (u_int32_t *)incr[0].ndata;
838 u_int32_t *ep = p + (incr[0].len >> 2);
839 char *type = "";
840 retval = 0;
841 switch (version & 0xFF) {
842 case 1:
843 type = "proteon/mrouted ";
844 retval = 1;
845 break;
846
847 case 2:
848 case 3:
849 if (((version >> 8) & 0xFF) < 3) retval = 1;
850 /* Fall through */
851 case 4:
852 type = "mrouted ";
853 break;
854
855 case 10:
856 type = "cisco ";
857 }
858 printf(" [%s%d.%d] %s\n",
859 type, version & 0xFF, (version >> 8) & 0xFF,
860 why);
861 VAL_TO_MASK(smask, r->tr_smask);
862 while (p < ep) {
863 u_int32_t laddr = *p++;
864 int flags = (ntohl(*p) & 0xFF00) >> 8;
865 int n = ntohl(*p++) & 0xFF;
866 if (!(flags & (DVMRP_NF_DOWN | DVMRP_NF_DISABLED)) &&
867 (laddr & smask) == (qsrc & smask)) {
868 printf("%3d ", -(hops+2));
869 print_host(qsrc);
870 printf("\n");
871 return 1;
872 }
873 p += n;
874 }
875 return retval;
876 }
877 printf(" %s\n", why);
878 return 0;
879 }
880
881
882 char *
883 scale(hop)
884 int *hop;
885 {
886 if (*hop > -1000 && *hop < 10000) return (" ms");
887 *hop /= 1000;
888 if (*hop > -1000 && *hop < 10000) return (" s ");
889 return ("s ");
890 }
891
892 /*
893 * Calculate and print one line of packet loss and packet rate statistics.
894 * Checks for count of all ones from mrouted 2.3 that doesn't have counters.
895 */
896 #define NEITHER 0
897 #define INS 1
898 #define OUTS 2
899 #define BOTH 3
900 void
901 stat_line(r, s, have_next, rst)
902 struct tr_resp *r, *s;
903 int have_next;
904 int *rst;
905 {
906 int timediff = (fixtime(ntohl(s->tr_qarr)) -
907 fixtime(ntohl(r->tr_qarr))) >> 16;
908 int v_lost, v_pct;
909 int g_lost, g_pct;
910 int v_out = ntohl(s->tr_vifout) - ntohl(r->tr_vifout);
911 int g_out = ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt);
912 int v_pps, g_pps;
913 char v_str[8], g_str[8];
914 int have = NEITHER;
915 int res = *rst;
916
917 if (timediff == 0) timediff = 1;
918 v_pps = v_out / timediff;
919 g_pps = g_out / timediff;
920
921 if ((v_out && (s->tr_vifout != 0xFFFFFFFF && s->tr_vifout != 0)) ||
922 (r->tr_vifout != 0xFFFFFFFF && r->tr_vifout != 0))
923 have |= OUTS;
924
925 if (have_next) {
926 --r, --s, --rst;
927 if ((s->tr_vifin != 0xFFFFFFFF && s->tr_vifin != 0) ||
928 (r->tr_vifin != 0xFFFFFFFF && r->tr_vifin != 0))
929 have |= INS;
930 if (*rst)
931 res = 1;
932 }
933
934 switch (have) {
935 case BOTH:
936 v_lost = v_out - (ntohl(s->tr_vifin) - ntohl(r->tr_vifin));
937 if (v_out) v_pct = (v_lost * 100 + (v_out >> 1)) / v_out;
938 else v_pct = 0;
939 if (-100 < v_pct && v_pct < 101 && v_out > 10)
940 (void)snprintf(v_str, sizeof v_str, "%3d", v_pct);
941 else memcpy(v_str, " --", 4);
942
943 g_lost = g_out - (ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt));
944 if (g_out) g_pct = (g_lost * 100 + (g_out >> 1))/ g_out;
945 else g_pct = 0;
946 if (-100 < g_pct && g_pct < 101 && g_out > 10)
947 (void)snprintf(g_str, sizeof g_str, "%3d", g_pct);
948 else memcpy(g_str, " --", 4);
949
950 printf("%6d/%-5d=%s%%%4d pps",
951 v_lost, v_out, v_str, v_pps);
952 if (res)
953 printf("\n");
954 else
955 printf("%6d/%-5d=%s%%%4d pps\n",
956 g_lost, g_out, g_str, g_pps);
957 break;
958
959 case INS:
960 v_out = ntohl(s->tr_vifin) - ntohl(r->tr_vifin);
961 v_pps = v_out / timediff;
962 /* Fall through */
963
964 case OUTS:
965 printf(" %-5d %4d pps",
966 v_out, v_pps);
967 if (res)
968 printf("\n");
969 else
970 printf(" %-5d %4d pps\n",
971 g_out, g_pps);
972 break;
973
974 case NEITHER:
975 printf("\n");
976 break;
977 }
978
979 if (debug > 2) {
980 printf("\t\t\t\tv_in: %ld ", (long)ntohl(s->tr_vifin));
981 printf("v_out: %ld ", (long)ntohl(s->tr_vifout));
982 printf("pkts: %ld\n", (long)ntohl(s->tr_pktcnt));
983 printf("\t\t\t\tv_in: %ld ", (long)ntohl(r->tr_vifin));
984 printf("v_out: %ld ", (long)ntohl(r->tr_vifout));
985 printf("pkts: %ld\n", (long)ntohl(r->tr_pktcnt));
986 printf("\t\t\t\tv_in: %ld ",
987 (long)ntohl(s->tr_vifin)-ntohl(r->tr_vifin));
988 printf("v_out: %ld ",
989 (long)(ntohl(s->tr_vifout) - ntohl(r->tr_vifout)));
990 printf("pkts: %ld ", (long)(ntohl(s->tr_pktcnt) - ntohl(r->tr_pktcnt)));
991 printf("time: %d\n", timediff);
992 printf("\t\t\t\tres: %d\n", res);
993 }
994 }
995
996 /*
997 * A fixup to check if any pktcnt has been reset, and to fix the
998 * byteorder bugs in mrouted 3.6 on little-endian machines.
999 */
1000 void
1001 fixup_stats(base, prev, new)
1002 struct resp_buf *base, *prev, *new;
1003 {
1004 int rno = base->len;
1005 struct tr_resp *b = base->resps + rno;
1006 struct tr_resp *p = prev->resps + rno;
1007 struct tr_resp *n = new->resps + rno;
1008 int *r = reset + rno;
1009 int *s = swaps + rno;
1010 int res;
1011
1012 /* Check for byte-swappers */
1013 while (--rno >= 0) {
1014 --n; --p; --b; --s;
1015 if (*s || abs(ntohl(n->tr_vifout) - ntohl(p->tr_vifout)) > 100000) {
1016 /* This host sends byteswapped reports; swap 'em */
1017 if (!*s) {
1018 *s = 1;
1019 b->tr_qarr = byteswap(b->tr_qarr);
1020 b->tr_vifin = byteswap(b->tr_vifin);
1021 b->tr_vifout = byteswap(b->tr_vifout);
1022 b->tr_pktcnt = byteswap(b->tr_pktcnt);
1023 }
1024
1025 n->tr_qarr = byteswap(n->tr_qarr);
1026 n->tr_vifin = byteswap(n->tr_vifin);
1027 n->tr_vifout = byteswap(n->tr_vifout);
1028 n->tr_pktcnt = byteswap(n->tr_pktcnt);
1029 }
1030 }
1031
1032 rno = base->len;
1033 b = base->resps + rno;
1034 p = prev->resps + rno;
1035 n = new->resps + rno;
1036
1037 while (--rno >= 0) {
1038 --n; --p; --b; --r;
1039 res = ((ntohl(n->tr_pktcnt) < ntohl(b->tr_pktcnt)) ||
1040 (ntohl(n->tr_pktcnt) < ntohl(p->tr_pktcnt)));
1041 if (debug > 2)
1042 printf("\t\tr=%d, res=%d\n", *r, res);
1043 if (*r) {
1044 if (res || *r > 1) {
1045 /*
1046 * This router appears to be a 3.4 with that nasty ol'
1047 * neighbor version bug, which causes it to constantly
1048 * reset. Just nuke the statistics for this node, and
1049 * don't even bother giving it the benefit of the
1050 * doubt from now on.
1051 */
1052 p->tr_pktcnt = b->tr_pktcnt = n->tr_pktcnt;
1053 r++;
1054 } else {
1055 /*
1056 * This is simply the situation that the original
1057 * fixup_stats was meant to deal with -- that a
1058 * 3.3 or 3.4 router deleted a cache entry while
1059 * traffic was still active.
1060 */
1061 *r = 0;
1062 break;
1063 }
1064 } else
1065 *r = res;
1066 }
1067
1068 if (rno < 0) return;
1069
1070 rno = base->len;
1071 b = base->resps + rno;
1072 p = prev->resps + rno;
1073
1074 while (--rno >= 0) (--b)->tr_pktcnt = (--p)->tr_pktcnt;
1075 }
1076
1077 /*
1078 * Print responses with statistics for forward path (from src to dst)
1079 */
1080 int
1081 print_stats(base, prev, new)
1082 struct resp_buf *base, *prev, *new;
1083 {
1084 int rtt, hop;
1085 char *ms;
1086 u_int32_t smask;
1087 int rno = base->len - 1;
1088 struct tr_resp *b = base->resps + rno;
1089 struct tr_resp *p = prev->resps + rno;
1090 struct tr_resp *n = new->resps + rno;
1091 int *r = reset + rno;
1092 u_long resptime = new->rtime;
1093 u_long qarrtime = fixtime(ntohl(n->tr_qarr));
1094 u_int ttl = n->tr_fttl;
1095 int first = (base == prev);
1096
1097 VAL_TO_MASK(smask, b->tr_smask);
1098 printf(" Source Response Dest");
1099 printf(" Packet Statistics For Only For Traffic\n");
1100 printf("%-15s %-15s All Multicast Traffic From %s\n",
1101 ((b->tr_inaddr & smask) == (qsrc & smask)) ? s1 : " * * * ",
1102 inet_fmt(base->qhdr.tr_raddr, s2), inet_fmt(qsrc, s1));
1103 rtt = t_diff(resptime, new->qtime);
1104 ms = scale(&rtt);
1105 printf(" %c __/ rtt%5d%s Lost/Sent = Pct Rate To %s\n",
1106 first ? 'v' : '|', rtt, ms, inet_fmt(qgrp, s2));
1107 if (!first) {
1108 hop = t_diff(resptime, qarrtime);
1109 ms = scale(&hop);
1110 printf(" v / hop%5d%s", hop, ms);
1111 printf(" --------------------- --------------------\n");
1112 }
1113 if (debug > 2) {
1114 printf("\t\t\t\tv_in: %ld ", (long)ntohl(n->tr_vifin));
1115 printf("v_out: %ld ", (long)ntohl(n->tr_vifout));
1116 printf("pkts: %ld\n", (long)ntohl(n->tr_pktcnt));
1117 printf("\t\t\t\tv_in: %ld ", (long)ntohl(b->tr_vifin));
1118 printf("v_out: %ld ", (long)ntohl(b->tr_vifout));
1119 printf("pkts: %ld\n", (long)ntohl(b->tr_pktcnt));
1120 printf("\t\t\t\tv_in: %ld ",
1121 (long)(ntohl(n->tr_vifin) - ntohl(b->tr_vifin)));
1122 printf("v_out: %ld ",
1123 (long)(ntohl(n->tr_vifout) - ntohl(b->tr_vifout)));
1124 printf("pkts: %ld\n",
1125 (long)(ntohl(n->tr_pktcnt) - ntohl(b->tr_pktcnt)));
1126 printf("\t\t\t\treset: %d\n", *r);
1127 }
1128
1129 while (TRUE) {
1130 if ((n->tr_inaddr != b->tr_inaddr) || (n->tr_inaddr != b->tr_inaddr))
1131 return 1; /* Route changed */
1132
1133 if ((n->tr_inaddr != n->tr_outaddr))
1134 printf("%-15s\n", inet_fmt(n->tr_inaddr, s1));
1135 printf("%-15s %-14s %s\n", inet_fmt(n->tr_outaddr, s1), names[rno],
1136 flag_type(n->tr_rflags));
1137
1138 if (rno-- < 1) break;
1139
1140 printf(" %c ^ ttl%5d ", first ? 'v' : '|', ttl);
1141 stat_line(p, n, TRUE, r);
1142 if (!first) {
1143 resptime = qarrtime;
1144 qarrtime = fixtime(ntohl((n-1)->tr_qarr));
1145 hop = t_diff(resptime, qarrtime);
1146 ms = scale(&hop);
1147 printf(" v | hop%5d%s", hop, ms);
1148 stat_line(b, n, TRUE, r);
1149 }
1150
1151 --b, --p, --n, --r;
1152 if (ttl < n->tr_fttl) ttl = n->tr_fttl;
1153 else ++ttl;
1154 }
1155
1156 printf(" %c \\__ ttl%5d ", first ? 'v' : '|', ttl);
1157 stat_line(p, n, FALSE, r);
1158 if (!first) {
1159 hop = t_diff(qarrtime, new->qtime);
1160 ms = scale(&hop);
1161 printf(" v \\ hop%5d%s", hop, ms);
1162 stat_line(b, n, FALSE, r);
1163 }
1164 printf("%-15s %s\n", inet_fmt(qdst, s1), inet_fmt(lcl_addr, s2));
1165 printf(" Receiver Query Source\n\n");
1166 return 0;
1167 }
1168
1169
1170 /***************************************************************************
1171 * main
1172 ***************************************************************************/
1173
1174 int
1175 main(argc, argv)
1176 int argc;
1177 char *argv[];
1178 {
1179 int udp;
1180 struct sockaddr_in addr;
1181 int addrlen = sizeof(addr);
1182 int recvlen;
1183 struct timeval tv;
1184 struct resp_buf *prev, *new;
1185 struct tr_resp *r;
1186 u_int32_t smask;
1187 int rno;
1188 int hops, nexthop, tries;
1189 u_int32_t lastout = 0;
1190 int numstats = 1;
1191 int waittime;
1192 int seed;
1193
1194 if (geteuid() != 0) {
1195 fprintf(stderr, "mtrace: must be root\n");
1196 exit(1);
1197 }
1198
1199 argv++, argc--;
1200 if (argc == 0) goto usage;
1201
1202 while (argc > 0 && *argv[0] == '-') {
1203 char *p = *argv++; argc--;
1204 p++;
1205 do {
1206 char c = *p++;
1207 char *arg = (char *) 0;
1208 if (isdigit(*p)) {
1209 arg = p;
1210 p = "";
1211 } else if (argc > 0) arg = argv[0];
1212 switch (c) {
1213 case 'd': /* Unlisted debug print option */
1214 if (arg && isdigit(*arg)) {
1215 debug = atoi(arg);
1216 if (debug < 0) debug = 0;
1217 if (debug > 3) debug = 3;
1218 if (arg == argv[0]) argv++, argc--;
1219 break;
1220 } else
1221 goto usage;
1222 case 'M': /* Use multicast for reponse */
1223 multicast = TRUE;
1224 break;
1225 case 'l': /* Loop updating stats indefinitely */
1226 numstats = 3153600;
1227 break;
1228 case 'n': /* Don't reverse map host addresses */
1229 numeric = TRUE;
1230 break;
1231 case 'p': /* Passive listen for traces */
1232 passive = TRUE;
1233 break;
1234 case 'v': /* Verbosity */
1235 verbose = TRUE;
1236 break;
1237 case 's': /* Short form, don't wait for stats */
1238 numstats = 0;
1239 break;
1240 case 'w': /* Time to wait for packet arrival */
1241 if (arg && isdigit(*arg)) {
1242 timeout = atoi(arg);
1243 if (timeout < 1) timeout = 1;
1244 if (arg == argv[0]) argv++, argc--;
1245 break;
1246 } else
1247 goto usage;
1248 case 'm': /* Max number of hops to trace */
1249 if (arg && isdigit(*arg)) {
1250 qno = atoi(arg);
1251 if (qno > MAXHOPS) qno = MAXHOPS;
1252 else if (qno < 1) qno = 0;
1253 if (arg == argv[0]) argv++, argc--;
1254 break;
1255 } else
1256 goto usage;
1257 case 'q': /* Number of query retries */
1258 if (arg && isdigit(*arg)) {
1259 nqueries = atoi(arg);
1260 if (nqueries < 1) nqueries = 1;
1261 if (arg == argv[0]) argv++, argc--;
1262 break;
1263 } else
1264 goto usage;
1265 case 'g': /* Last-hop gateway (dest of query) */
1266 if (arg && (gwy = host_addr(arg))) {
1267 if (arg == argv[0]) argv++, argc--;
1268 break;
1269 } else
1270 goto usage;
1271 case 't': /* TTL for query packet */
1272 if (arg && isdigit(*arg)) {
1273 qttl = atoi(arg);
1274 if (qttl < 1) qttl = 1;
1275 rttl = qttl;
1276 if (arg == argv[0]) argv++, argc--;
1277 break;
1278 } else
1279 goto usage;
1280 case 'r': /* Dest for response packet */
1281 if (arg && (raddr = host_addr(arg))) {
1282 if (arg == argv[0]) argv++, argc--;
1283 break;
1284 } else
1285 goto usage;
1286 case 'i': /* Local interface address */
1287 if (arg && (lcl_addr = host_addr(arg))) {
1288 if (arg == argv[0]) argv++, argc--;
1289 break;
1290 } else
1291 goto usage;
1292 case 'S': /* Stat accumulation interval */
1293 if (arg && isdigit(*arg)) {
1294 statint = atoi(arg);
1295 if (statint < 1) statint = 1;
1296 if (arg == argv[0]) argv++, argc--;
1297 break;
1298 } else
1299 goto usage;
1300 default:
1301 goto usage;
1302 }
1303 } while (*p);
1304 }
1305
1306 if (argc > 0 && (qsrc = host_addr(argv[0]))) { /* Source of path */
1307 if (IN_MULTICAST(ntohl(qsrc))) goto usage;
1308 argv++, argc--;
1309 if (argc > 0 && (qdst = host_addr(argv[0]))) { /* Dest of path */
1310 argv++, argc--;
1311 if (argc > 0 && (qgrp = host_addr(argv[0]))) { /* Path via group */
1312 argv++, argc--;
1313 }
1314 if (IN_MULTICAST(ntohl(qdst))) {
1315 u_int32_t temp = qdst;
1316 qdst = qgrp;
1317 qgrp = temp;
1318 if (IN_MULTICAST(ntohl(qdst))) goto usage;
1319 } else if (qgrp && !IN_MULTICAST(ntohl(qgrp))) goto usage;
1320 }
1321 }
1322
1323 if (passive) {
1324 passive_mode();
1325 return(0);
1326 }
1327
1328 if (argc > 0 || qsrc == 0) {
1329 usage: printf("\
1330 Usage: mtrace [-Mlnps] [-w wait] [-m max_hops] [-q nqueries] [-g gateway]\n\
1331 [-S statint] [-t ttl] [-r resp_dest] [-i if_addr] source [receiver] [group]\n");
1332 exit(1);
1333 }
1334
1335 init_igmp();
1336
1337 /*
1338 * Set useful defaults for as many parameters as possible.
1339 */
1340
1341 defgrp = htonl(0xE0020001); /* MBone Audio (224.2.0.1) */
1342 query_cast = htonl(0xE0000002); /* All routers multicast addr */
1343 resp_cast = htonl(0xE0000120); /* Mtrace response multicast addr */
1344 if (qgrp == 0) qgrp = defgrp;
1345
1346 /*
1347 * Get default local address for multicasts to use in setting defaults.
1348 */
1349 addr.sin_family = AF_INET;
1350 #if (defined(BSD) && (BSD >= 199103))
1351 addr.sin_len = sizeof(addr);
1352 #endif
1353 addr.sin_addr.s_addr = qgrp;
1354 addr.sin_port = htons(2000); /* Any port above 1024 will do */
1355
1356 if (((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) ||
1357 (connect(udp, (struct sockaddr *) &addr, sizeof(addr)) < 0) ||
1358 getsockname(udp, (struct sockaddr *) &addr, &addrlen) < 0) {
1359 perror("Determining local address");
1360 exit(-1);
1361 }
1362
1363 #ifdef SUNOS5
1364 /*
1365 * SunOS 5.X prior to SunOS 2.6, getsockname returns 0 for udp socket.
1366 * This call to sysinfo will return the hostname.
1367 * If the default multicast interfface (set with the route
1368 * for 224.0.0.0) is not the same as the hostname,
1369 * mtrace -i [if_addr] will have to be used.
1370 */
1371 if (addr.sin_addr.s_addr == 0) {
1372 char myhostname[MAXHOSTNAMELEN];
1373 struct hostent *hp;
1374 int error;
1375
1376 error = sysinfo(SI_HOSTNAME, myhostname, sizeof(myhostname));
1377 if (error == -1) {
1378 perror("Getting my hostname");
1379 exit(-1);
1380 }
1381
1382 hp = gethostbyname(myhostname);
1383 if (hp == NULL || hp->h_addrtype != AF_INET ||
1384 hp->h_length != sizeof(addr.sin_addr)) {
1385 perror("Finding IP address for my hostname");
1386 exit(-1);
1387 }
1388
1389 memcpy((char *)&addr.sin_addr.s_addr, hp->h_addr,
1390 sizeof(addr.sin_addr.s_addr));
1391 }
1392 #endif
1393
1394 /*
1395 * Default destination for path to be queried is the local host.
1396 */
1397 if (qdst == 0) qdst = lcl_addr ? lcl_addr : addr.sin_addr.s_addr;
1398 dst_netmask = get_netmask(udp, qdst);
1399 close(udp);
1400 if (lcl_addr == 0) lcl_addr = addr.sin_addr.s_addr;
1401
1402 /*
1403 * Initialize the seed for random query identifiers.
1404 */
1405 gettimeofday(&tv, 0);
1406 seed = tv.tv_usec ^ lcl_addr;
1407 #ifdef SYSV
1408 srand48(seed);
1409 #else
1410 srandom(seed);
1411 #endif
1412
1413 /*
1414 * Protect against unicast queries to mrouted versions that might crash.
1415 */
1416 if (gwy && !IN_MULTICAST(ntohl(gwy)))
1417 if (send_recv(gwy, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2, 1, &incr[0])) {
1418 int version = ntohl(incr[0].igmp.igmp_group.s_addr) & 0xFFFF;
1419 if (version == 0x0303 || version == 0x0503) {
1420 printf("Don't use -g to address an mrouted 3.%d, it might crash\n",
1421 (version >> 8) & 0xFF);
1422 exit(0);
1423 }
1424 }
1425
1426 printf("Mtrace from %s to %s via group %s\n",
1427 inet_fmt(qsrc, s1), inet_fmt(qdst, s2), inet_fmt(qgrp, s3));
1428
1429 if ((qdst & dst_netmask) == (qsrc & dst_netmask)) {
1430 printf("Source & receiver are directly connected, no path to trace\n");
1431 exit(0);
1432 }
1433
1434 /*
1435 * If the response is to be a multicast address, make sure we
1436 * are listening on that multicast address.
1437 */
1438 if (raddr) {
1439 if (IN_MULTICAST(ntohl(raddr))) k_join(raddr, lcl_addr);
1440 } else k_join(resp_cast, lcl_addr);
1441
1442 /*
1443 * If the destination is on the local net, the last-hop router can
1444 * be found by multicast to the all-routers multicast group.
1445 * Otherwise, use the group address that is the subject of the
1446 * query since by definition the last-hop router will be a member.
1447 * Set default TTLs for local remote multicasts.
1448 */
1449 restart:
1450
1451 if (gwy == 0)
1452 if ((qdst & dst_netmask) == (lcl_addr & dst_netmask)) tdst = query_cast;
1453 else tdst = qgrp;
1454 else tdst = gwy;
1455
1456 if (IN_MULTICAST(ntohl(tdst))) {
1457 k_set_loop(1); /* If I am running on a router, I need to hear this */
1458 if (tdst == query_cast) k_set_ttl(qttl ? qttl : 1);
1459 else k_set_ttl(qttl ? qttl : MULTICAST_TTL1);
1460 }
1461
1462 /*
1463 * Try a query at the requested number of hops or MAXHOPS if unspecified.
1464 */
1465 if (qno == 0) {
1466 hops = MAXHOPS;
1467 tries = 1;
1468 printf("Querying full reverse path... ");
1469 fflush(stdout);
1470 } else {
1471 hops = qno;
1472 tries = nqueries;
1473 printf("Querying reverse path, maximum %d hops... ", qno);
1474 fflush(stdout);
1475 }
1476 base.rtime = 0;
1477 base.len = 0;
1478
1479 recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, tries, &base);
1480
1481 /*
1482 * If the initial query was successful, print it. Otherwise, if
1483 * the query max hop count is the default of zero, loop starting
1484 * from one until there is no response for four hops. The extra
1485 * hops allow getting past an mtrace-capable mrouter that can't
1486 * send multicast packets because all phyints are disabled.
1487 */
1488 if (recvlen) {
1489 printf("\n 0 ");
1490 print_host(qdst);
1491 printf("\n");
1492 print_trace(1, &base);
1493 r = base.resps + base.len - 1;
1494 if (r->tr_rflags == TR_OLD_ROUTER || r->tr_rflags == TR_NO_SPACE ||
1495 qno != 0) {
1496 printf("%3d ", -(base.len+1));
1497 what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ?
1498 "doesn't support mtrace"
1499 : "is the next hop");
1500 } else {
1501 VAL_TO_MASK(smask, r->tr_smask);
1502 if ((r->tr_inaddr & smask) == (qsrc & smask)) {
1503 printf("%3d ", -(base.len+1));
1504 print_host(qsrc);
1505 printf("\n");
1506 }
1507 }
1508 } else if (qno == 0) {
1509 printf("switching to hop-by-hop:\n 0 ");
1510 print_host(qdst);
1511 printf("\n");
1512
1513 for (hops = 1, nexthop = 1; hops <= MAXHOPS; ++hops) {
1514 printf("%3d ", -hops);
1515 fflush(stdout);
1516
1517 /*
1518 * After a successful first hop, try switching to the unicast
1519 * address of the last-hop router instead of multicasting the
1520 * trace query. This should be safe for mrouted versions 3.3
1521 * and 3.5 because there is a long route timeout with metric
1522 * infinity before a route disappears. Switching to unicast
1523 * reduces the amount of multicast traffic and avoids a bug
1524 * with duplicate suppression in mrouted 3.5.
1525 */
1526 if (hops == 2 && gwy == 0 &&
1527 (recvlen = send_recv(lastout, IGMP_MTRACE_QUERY, hops, 1, &base)))
1528 tdst = lastout;
1529 else recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, hops, nqueries, &base);
1530
1531 if (recvlen == 0) {
1532 if (hops == 1) break;
1533 if (hops == nexthop) {
1534 if (what_kind(&base, "didn't respond")) {
1535 /* the ask_neighbors determined that the
1536 * not-responding router is the first-hop. */
1537 break;
1538 }
1539 } else if (hops < nexthop + 3) {
1540 printf("\n");
1541 } else {
1542 printf("...giving up\n");
1543 break;
1544 }
1545 continue;
1546 }
1547 r = base.resps + base.len - 1;
1548 if (base.len == hops &&
1549 (hops == 1 || (base.resps+nexthop-2)->tr_outaddr == lastout)) {
1550 if (hops == nexthop) {
1551 print_trace(-hops, &base);
1552 } else {
1553 printf("\nResuming...\n");
1554 print_trace(nexthop, &base);
1555 }
1556 } else {
1557 if (base.len < hops) {
1558 /*
1559 * A shorter trace than requested means a fatal error
1560 * occurred along the path, or that the route changed
1561 * to a shorter one.
1562 *
1563 * If the trace is longer than the last one we received,
1564 * then we are resuming from a skipped router (but there
1565 * is still probably a problem).
1566 *
1567 * If the trace is shorter than the last one we
1568 * received, then the route must have changed (and
1569 * there is still probably a problem).
1570 */
1571 if (nexthop <= base.len) {
1572 printf("\nResuming...\n");
1573 print_trace(nexthop, &base);
1574 } else if (nexthop > base.len + 1) {
1575 hops = base.len;
1576 printf("\nRoute must have changed...\n");
1577 print_trace(1, &base);
1578 }
1579 } else {
1580 /*
1581 * The last hop address is not the same as it was;
1582 * the route probably changed underneath us.
1583 */
1584 hops = base.len;
1585 printf("\nRoute must have changed...\n");
1586 print_trace(1, &base);
1587 }
1588 }
1589 lastout = r->tr_outaddr;
1590
1591 if (base.len < hops ||
1592 r->tr_rmtaddr == 0 ||
1593 (r->tr_rflags & 0x80)) {
1594 VAL_TO_MASK(smask, r->tr_smask);
1595 if (r->tr_rmtaddr) {
1596 if (hops != nexthop) {
1597 printf("\n%3d ", -(base.len+1));
1598 }
1599 what_kind(&base, r->tr_rflags == TR_OLD_ROUTER ?
1600 "doesn't support mtrace" :
1601 "would be the next hop");
1602 /* XXX could do segmented trace if TR_NO_SPACE */
1603 } else if (r->tr_rflags == TR_NO_ERR &&
1604 (r->tr_inaddr & smask) == (qsrc & smask)) {
1605 printf("%3d ", -(hops + 1));
1606 print_host(qsrc);
1607 printf("\n");
1608 }
1609 break;
1610 }
1611
1612 nexthop = hops + 1;
1613 }
1614 }
1615
1616 if (base.rtime == 0) {
1617 printf("Timed out receiving responses\n");
1618 if (IN_MULTICAST(ntohl(tdst))) {
1619 if (tdst == query_cast)
1620 printf("Perhaps no local router has a route for source %s\n",
1621 inet_fmt(qsrc, s1));
1622 else
1623 printf("Perhaps receiver %s is not a member of group %s,\n"
1624 "or no router local to it has a route for source %s,\n"
1625 "or multicast at ttl %d doesn't reach its last-hop router"
1626 " for that source\n",
1627 inet_fmt(qdst, s2), inet_fmt(qgrp, s3), inet_fmt(qsrc, s1),
1628 qttl ? qttl : MULTICAST_TTL1);
1629 }
1630 exit(1);
1631 }
1632
1633 printf("Round trip time %d ms\n\n", t_diff(base.rtime, base.qtime));
1634
1635 /*
1636 * Use the saved response which was the longest one received,
1637 * and make additional probes after delay to measure loss.
1638 */
1639 raddr = base.qhdr.tr_raddr;
1640 rttl = base.qhdr.tr_rttl;
1641 gettimeofday(&tv, 0);
1642 waittime = statint - (((tv.tv_sec + JAN_1970) & 0xFFFF) - (base.qtime >> 16));
1643 prev = &base;
1644 new = &incr[numstats&1];
1645
1646 while (numstats--) {
1647 if (waittime < 1) printf("\n");
1648 else {
1649 printf("Waiting to accumulate statistics... ");
1650 fflush(stdout);
1651 sleep((unsigned)waittime);
1652 }
1653 rno = base.len;
1654 recvlen = send_recv(tdst, IGMP_MTRACE_QUERY, rno, nqueries, new);
1655
1656 if (recvlen == 0) {
1657 printf("Timed out.\n");
1658 exit(1);
1659 }
1660
1661 if (rno != new->len) {
1662 printf("Trace length doesn't match:\n");
1663 /*
1664 * XXX Should this trace result be printed, or is that
1665 * too verbose? Perhaps it should just say restarting.
1666 * But if the path is changing quickly, this may be the
1667 * only snapshot of the current path. But, if the path
1668 * is changing that quickly, does the current path really
1669 * matter?
1670 */
1671 print_trace(1, new);
1672 printf("Restarting.\n\n");
1673 numstats++;
1674 goto restart;
1675 }
1676
1677 printf("Results after %d seconds:\n\n",
1678 (int)((new->qtime - base.qtime) >> 16));
1679 fixup_stats(&base, prev, new);
1680 if (print_stats(&base, prev, new)) {
1681 printf("Route changed:\n");
1682 print_trace(1, new);
1683 printf("Restarting.\n\n");
1684 goto restart;
1685 }
1686 prev = new;
1687 new = &incr[numstats&1];
1688 waittime = statint;
1689 }
1690
1691 /*
1692 * If the response was multicast back, leave the group
1693 */
1694 if (raddr) {
1695 if (IN_MULTICAST(ntohl(raddr))) k_leave(raddr, lcl_addr);
1696 } else k_leave(resp_cast, lcl_addr);
1697
1698 return (0);
1699 }
1700
1701 void
1702 check_vif_state()
1703 {
1704 log(LOG_WARNING, errno, "sendto");
1705 }
1706
1707 /*
1708 * Log errors and other messages to stderr, according to the severity
1709 * of the message and the current debug level. For errors of severity
1710 * LOG_ERR or worse, terminate the program.
1711 */
1712 #ifdef __STDC__
1713 void
1714 log(int severity, int syserr, char *format, ...)
1715 {
1716 va_list ap;
1717 char fmt[100];
1718
1719 va_start(ap, format);
1720 #else
1721 /*VARARGS3*/
1722 void
1723 log(severity, syserr, format, va_alist)
1724 int severity, syserr;
1725 char *format;
1726 va_dcl
1727 {
1728 va_list ap;
1729 char fmt[100];
1730
1731 va_start(ap);
1732 #endif
1733
1734 switch (debug) {
1735 case 0: if (severity > LOG_WARNING) return;
1736 case 1: if (severity > LOG_NOTICE) return;
1737 case 2: if (severity > LOG_INFO ) return;
1738 default:
1739 fmt[0] = '\0';
1740 if (severity == LOG_WARNING) strcat(fmt, "warning - ");
1741 strncat(fmt, format, 80);
1742 vfprintf(stderr, fmt, ap);
1743 if (syserr == 0)
1744 fprintf(stderr, "\n");
1745 else
1746 fprintf(stderr, ": %s\n", strerror(syserr));
1747 }
1748 if (severity <= LOG_ERR) exit(-1);
1749 }
1750
1751 /* dummies */
1752 void accept_probe(src, dst, p, datalen, level)
1753 u_int32_t src, dst, level;
1754 char *p;
1755 int datalen;
1756 {
1757 }
1758 void accept_group_report(src, dst, group, r_type)
1759 u_int32_t src, dst, group;
1760 int r_type;
1761 {
1762 }
1763 void accept_neighbor_request2(src, dst)
1764 u_int32_t src, dst;
1765 {
1766 }
1767 void accept_report(src, dst, p, datalen, level)
1768 u_int32_t src, dst, level;
1769 char *p;
1770 int datalen;
1771 {
1772 }
1773 void accept_neighbor_request(src, dst)
1774 u_int32_t src, dst;
1775 {
1776 }
1777 void accept_prune(src, dst, p, datalen)
1778 u_int32_t src, dst;
1779 char *p;
1780 int datalen;
1781 {
1782 }
1783 void accept_graft(src, dst, p, datalen)
1784 u_int32_t src, dst;
1785 char *p;
1786 int datalen;
1787 {
1788 }
1789 void accept_g_ack(src, dst, p, datalen)
1790 u_int32_t src, dst;
1791 char *p;
1792 int datalen;
1793 {
1794 }
1795 void add_table_entry(origin, mcastgrp)
1796 u_int32_t origin, mcastgrp;
1797 {
1798 }
1799 void accept_leave_message(src, dst, group)
1800 u_int32_t src, dst, group;
1801 {
1802 }
1803 void accept_mtrace(src, dst, group, data, no, datalen)
1804 u_int32_t src, dst, group;
1805 char *data;
1806 u_int no;
1807 int datalen;
1808 {
1809 }
1810 void accept_membership_query(src, dst, group, tmo)
1811 u_int32_t src, dst, group;
1812 int tmo;
1813 {
1814 }
1815 void accept_neighbors(src, dst, p, datalen, level)
1816 u_int32_t src, dst, level;
1817 u_char *p;
1818 int datalen;
1819 {
1820 }
1821 void accept_neighbors2(src, dst, p, datalen, level)
1822 u_int32_t src, dst, level;
1823 u_char *p;
1824 int datalen;
1825 {
1826 }
1827 void accept_info_request(src, dst, p, datalen)
1828 u_int32_t src, dst;
1829 u_char *p;
1830 int datalen;
1831 {
1832 }
1833 void accept_info_reply(src, dst, p, datalen)
1834 u_int32_t src, dst;
1835 u_char *p;
1836 int datalen;
1837 {
1838 }
1839