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