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