sdhci - Implement ADMA2 transfer support. Keep SDMA support for now.
[dragonfly.git] / usr.sbin / mrouted / mrinfo.c
blobc407ab52fff941a002071dbf3c72493fd3eee689
1 /*
2 * This tool requests configuration info from a multicast router
3 * and prints the reply (if any). Invoke it as:
5 * mrinfo router-name-or-address
7 * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
8 * multicast mapper written by Pavel Curtis).
10 * The lawyers insist we include the following UC copyright notice.
11 * The mapper from which this is derived contained a Xerox copyright
12 * notice which follows the UC one. Try not to get depressed noting
13 * that the legal gibberish is larger than the program.
15 * Copyright (c) 1993 Regents of the University of California.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. All advertising materials mentioning features or use of this software
27 * must display the following acknowledgement:
28 * This product includes software developed by the Computer Systems
29 * Engineering Group at Lawrence Berkeley Laboratory.
30 * 4. Neither the name of the University nor of the Laboratory may be used
31 * to endorse or promote products derived from this software without
32 * specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * SUCH DAMAGE.
45 * ---------------------------------
46 * Copyright (c) Xerox Corporation 1992. All rights reserved.
48 * License is granted to copy, to use, and to make and to use derivative works
49 * for research and evaluation purposes, provided that Xerox is acknowledged
50 * in all documentation pertaining to any such copy or derivative work. Xerox
51 * grants no other licenses expressed or implied. The Xerox trade name should
52 * not be used in any advertising without its written permission.
54 * XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
55 * MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE FOR
56 * ANY PARTICULAR PURPOSE. The software is provided "as is" without express
57 * or implied warranty of any kind.
59 * These notices must be retained in any copies of any part of this software.
61 * @(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp
62 * $FreeBSD: src/usr.sbin/mrouted/mrinfo.c,v 1.17.2.1 2002/09/12 16:27:49 nectar Exp $
65 #include <err.h>
66 #include <netdb.h>
67 #include <sys/time.h>
68 #include "defs.h"
69 #include <arpa/inet.h>
70 #include <stdarg.h>
72 #define DEFAULT_TIMEOUT 4 /* How long to wait before retrying requests */
73 #define DEFAULT_RETRIES 3 /* How many times to ask each router */
75 u_int32 our_addr, target_addr = 0; /* in NET order */
76 int debug = 0;
77 int nflag = 0;
78 int retries = DEFAULT_RETRIES;
79 int timeout = DEFAULT_TIMEOUT;
80 int target_level = 0;
81 vifi_t numvifs; /* to keep loader happy */
82 /* (see COPY_TABLES macro called in kern.c) */
84 char * inet_name(u_int32 addr);
85 void ask(u_int32 dst);
86 void ask2(u_int32 dst);
87 int get_number(int *var, int deflt, char ***pargv,
88 int *pargc);
89 u_int32 host_addr(char *name);
90 static void usage(void);
92 char *
93 inet_name(u_int32 addr)
95 struct hostent *e;
96 struct in_addr in;
98 if (addr == 0)
99 return "local";
101 if (nflag ||
102 (e = gethostbyaddr(&addr, sizeof(addr), AF_INET)) == NULL) {
103 in.s_addr = addr;
104 return (inet_ntoa(in));
106 return (e->h_name);
110 * Log errors and other messages to stderr, according to the severity of the
111 * message and the current debug level. For errors of severity LOG_ERR or
112 * worse, terminate the program.
114 void
115 dolog(int severity, int syserr, char *format, ...)
117 va_list ap;
118 char fmt[100];
120 va_start(ap, format);
121 switch (debug) {
122 case 0:
123 if (severity > LOG_WARNING)
124 return;
125 case 1:
126 if (severity > LOG_NOTICE)
127 return;
128 case 2:
129 if (severity > LOG_INFO)
130 return;
131 default:
132 fmt[0] = '\0';
133 if (severity == LOG_WARNING)
134 strcpy(fmt, "warning - ");
135 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
136 fmt[sizeof(fmt)-1]='\0';
137 vfprintf(stderr, fmt, ap);
138 if (syserr == 0)
139 fprintf(stderr, "\n");
140 else if (syserr < sys_nerr)
141 fprintf(stderr, ": %s\n", sys_errlist[syserr]);
142 else
143 fprintf(stderr, ": errno %d\n", syserr);
146 if (severity <= LOG_ERR)
147 exit(1);
151 * Send a neighbors-list request.
153 void
154 ask(u_int32 dst)
156 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
157 htonl(MROUTED_LEVEL), 0);
160 void
161 ask2(u_int32 dst)
163 send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
164 htonl(MROUTED_LEVEL), 0);
168 * Process an incoming neighbor-list message.
170 void
171 accept_neighbors(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
173 u_char *ep = p + datalen;
174 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
175 a += ((u_int32)*p++ << 8), a += *p++)
177 printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
178 while (p < ep) {
179 u_int32 laddr;
180 u_char metric;
181 u_char thresh;
182 int ncount;
184 GET_ADDR(laddr);
185 laddr = htonl(laddr);
186 metric = *p++;
187 thresh = *p++;
188 ncount = *p++;
189 while (--ncount >= 0) {
190 u_int32 neighbor;
192 GET_ADDR(neighbor);
193 neighbor = htonl(neighbor);
194 printf(" %s -> ", inet_fmt(laddr, s1));
195 printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
196 inet_name(neighbor), metric, thresh);
201 void
202 accept_neighbors2(u_int32 src, u_int32 dst, u_char *p, int datalen, u_int32 level)
204 u_char *ep = p + datalen;
205 u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
206 /* well, only possibly_broken_cisco, but that's too long to type. */
207 u_int majvers = level & 0xff;
208 u_int minvers = (level >> 8) & 0xff;
210 printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
211 if (majvers == 3 && minvers == 0xff)
212 printf("DVMRPv3 compliant");
213 else
214 printf("version %d.%d", majvers, minvers);
215 printf ("]:\n");
217 while (p < ep) {
218 u_char metric;
219 u_char thresh;
220 u_char flags;
221 int ncount;
222 u_int32 laddr = *(u_int32*)p;
224 p += 4;
225 metric = *p++;
226 thresh = *p++;
227 flags = *p++;
228 ncount = *p++;
229 if (broken_cisco && ncount == 0) /* dumb Ciscos */
230 ncount = 1;
231 if (broken_cisco && ncount > 15) /* dumb Ciscos */
232 ncount = ncount & 0xf;
233 while (--ncount >= 0 && p < ep) {
234 u_int32 neighbor = *(u_int32*)p;
236 p += 4;
237 printf(" %s -> ", inet_fmt(laddr, s1));
238 printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
239 inet_name(neighbor), metric, thresh);
240 if (flags & DVMRP_NF_TUNNEL)
241 printf("/tunnel");
242 if (flags & DVMRP_NF_SRCRT)
243 printf("/srcrt");
244 if (flags & DVMRP_NF_PIM)
245 printf("/pim");
246 if (flags & DVMRP_NF_QUERIER)
247 printf("/querier");
248 if (flags & DVMRP_NF_DISABLED)
249 printf("/disabled");
250 if (flags & DVMRP_NF_DOWN)
251 printf("/down");
252 if (flags & DVMRP_NF_LEAF)
253 printf("/leaf");
254 printf("]\n");
259 int
260 get_number(int *var, int deflt, char ***pargv, int *pargc)
262 if ((*pargv)[0][2] == '\0') { /* Get the value from the next
263 * argument */
264 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
265 (*pargv)++, (*pargc)--;
266 *var = atoi((*pargv)[0]);
267 return 1;
268 } else if (deflt >= 0) {
269 *var = deflt;
270 return 1;
271 } else
272 return 0;
273 } else { /* Get value from the rest of this argument */
274 if (isdigit((*pargv)[0][2])) {
275 *var = atoi((*pargv)[0] + 2);
276 return 1;
277 } else {
278 return 0;
283 static void
284 usage(void)
286 fprintf(stderr,
287 "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
288 exit(1);
292 main(int argc, char **argv)
294 int tries;
295 int trynew;
296 struct timeval et;
297 struct hostent *hp;
298 struct hostent bogus;
299 char *host;
300 int curaddr;
302 if (geteuid() != 0)
303 errx(1, "must be root");
305 init_igmp();
306 setuid(getuid());
308 setlinebuf(stderr);
310 argv++, argc--;
311 while (argc > 0 && argv[0][0] == '-') {
312 switch (argv[0][1]) {
313 case 'd':
314 if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
315 usage();
316 break;
317 case 'n':
318 ++nflag;
319 break;
320 case 'r':
321 if (!get_number(&retries, -1, &argv, &argc))
322 usage();
323 break;
324 case 't':
325 if (!get_number(&timeout, -1, &argv, &argc))
326 usage();
327 break;
328 default:
329 usage();
331 argv++, argc--;
333 if (argc > 1)
334 usage();
335 if (argc == 1)
336 host = argv[0];
337 else
338 host = "127.0.0.1";
340 if ((target_addr = inet_addr(host)) != -1) {
341 hp = &bogus;
342 hp->h_length = sizeof(target_addr);
343 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
344 hp->h_addr_list[0] = malloc(hp->h_length);
345 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
346 hp->h_addr_list[1] = 0;
347 } else
348 hp = gethostbyname(host);
350 if (hp == NULL || hp->h_length != sizeof(target_addr))
351 errx(1, "%s: no such host", argv[0]);
352 if (debug)
353 fprintf(stderr, "Debug level %u\n", debug);
355 /* Check all addresses; mrouters often have unreachable interfaces */
356 for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
357 memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
358 { /* Find a good local address for us. */
359 int udp;
360 struct sockaddr_in addr;
361 int addrlen = sizeof(addr);
363 addr.sin_family = AF_INET;
364 #ifdef HAVE_SA_LEN
365 addr.sin_len = sizeof addr;
366 #endif
367 addr.sin_addr.s_addr = target_addr;
368 addr.sin_port = htons(2000); /* any port over 1024 will
369 * do... */
370 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
371 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
372 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
373 err(-1, "determining local address");
374 close(udp);
375 our_addr = addr.sin_addr.s_addr;
378 tries = 0;
379 trynew = 1;
381 * New strategy: send 'ask2' for two timeouts, then fall back
382 * to 'ask', since it's not very likely that we are going to
383 * find someone who only responds to 'ask' these days
385 ask2(target_addr);
387 gettimeofday(&et, 0);
388 et.tv_sec += timeout;
390 /* Main receive loop */
391 for (;;) {
392 fd_set fds;
393 struct timeval tv, now;
394 int count, recvlen, dummy = 0;
395 u_int32 src, dst, group;
396 struct ip *ip;
397 struct igmp *igmp;
398 int ipdatalen, iphdrlen, igmpdatalen;
400 if (igmp_socket >= FD_SETSIZE)
401 dolog(LOG_ERR, 0, "descriptor too big");
402 FD_ZERO(&fds);
403 FD_SET(igmp_socket, &fds);
405 gettimeofday(&now, 0);
406 tv.tv_sec = et.tv_sec - now.tv_sec;
407 tv.tv_usec = et.tv_usec - now.tv_usec;
409 if (tv.tv_usec < 0) {
410 tv.tv_usec += 1000000L;
411 --tv.tv_sec;
413 if (tv.tv_sec < 0)
414 tv.tv_sec = tv.tv_usec = 0;
416 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
418 if (count < 0) {
419 if (errno != EINTR)
420 warn("select");
421 continue;
422 } else if (count == 0) {
423 dolog(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
424 if (++tries > retries)
425 break;
426 /* If we've tried ASK_NEIGHBORS2 twice with
427 * no response, fall back to ASK_NEIGHBORS
429 if (tries == 2 && target_level == 0)
430 trynew = 0;
431 if (target_level == 0 && trynew == 0)
432 ask(target_addr);
433 else
434 ask2(target_addr);
435 gettimeofday(&et, 0);
436 et.tv_sec += timeout;
437 continue;
439 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
440 0, NULL, &dummy);
441 if (recvlen <= 0) {
442 if (recvlen && errno != EINTR)
443 warn("recvfrom");
444 continue;
447 if (recvlen < sizeof(struct ip)) {
448 dolog(LOG_WARNING, 0,
449 "packet too short (%u bytes) for IP header",
450 recvlen);
451 continue;
453 ip = (struct ip *) recv_buf;
454 if (ip->ip_p == 0)
455 continue; /* Request to install cache entry */
456 src = ip->ip_src.s_addr;
457 dst = ip->ip_dst.s_addr;
458 iphdrlen = ip->ip_hl << 2;
459 #ifdef RAW_INPUT_IS_RAW
460 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
461 #else
462 ipdatalen = ip->ip_len;
463 #endif
464 if (iphdrlen + ipdatalen != recvlen) {
465 dolog(LOG_WARNING, 0,
466 "packet shorter (%u bytes) than hdr+data length (%u+%u)",
467 recvlen, iphdrlen, ipdatalen);
468 continue;
470 igmp = (struct igmp *) (recv_buf + iphdrlen);
471 group = igmp->igmp_group.s_addr;
472 igmpdatalen = ipdatalen - IGMP_MINLEN;
473 if (igmpdatalen < 0) {
474 dolog(LOG_WARNING, 0,
475 "IP data field too short (%u bytes) for IGMP, from %s",
476 ipdatalen, inet_fmt(src, s1));
477 continue;
479 if (igmp->igmp_type != IGMP_DVMRP)
480 continue;
482 switch (igmp->igmp_code) {
483 case DVMRP_NEIGHBORS:
484 case DVMRP_NEIGHBORS2:
485 if (src != target_addr) {
486 warnx("got reply from %s instead of %s",
487 inet_fmt(src, s1), inet_fmt(target_addr, s1));
488 /*continue;*/
490 break;
491 default:
492 continue; /* ignore all other DVMRP messages */
495 switch (igmp->igmp_code) {
497 case DVMRP_NEIGHBORS:
498 if (group) {
499 /* knows about DVMRP_NEIGHBORS2 msg */
500 if (target_level == 0) {
501 target_level = ntohl(group);
502 ask2(target_addr);
504 } else {
505 accept_neighbors(src, dst, (u_char *)(igmp + 1),
506 igmpdatalen, ntohl(group));
507 exit(0);
509 break;
511 case DVMRP_NEIGHBORS2:
512 accept_neighbors2(src, dst, (u_char *)(igmp + 1),
513 igmpdatalen, ntohl(group));
514 exit(0);
518 exit(1);
521 /* dummies */
522 void
523 accept_probe(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
527 void
528 accept_group_report(u_int32 src, u_int32 dst, u_int32 group, int r_type)
532 void
533 accept_neighbor_request2(u_int32 src, u_int32 dst)
537 void
538 accept_report(u_int32 src, u_int32 dst, char *p, int datalen, u_int32 level)
542 void
543 accept_neighbor_request(u_int32 src, u_int32 dst)
547 void
548 accept_prune(u_int32 src, u_int32 dst, char *p, int datalen)
552 void
553 accept_graft(u_int32 src, u_int32 dst, char *p, int datalen)
557 void
558 accept_g_ack(u_int32 src, u_int32 dst, char *p, int datalen)
562 void
563 add_table_entry(u_int32 origin, u_int32 mcastgrp)
567 void
568 check_vif_state(void)
572 void
573 accept_leave_message(u_int32 src, u_int32 dst, u_int32 group)
577 void
578 accept_mtrace(u_int32 src, u_int32 dst, u_int32 group,
579 char *data, u_int no, int datalen)
583 void
584 accept_membership_query(u_int32 src, u_int32 dst, u_int32 group, int tmo)
588 void
589 accept_info_request(u_int32 src, u_int32 dst, u_char *p, int datalen)
593 void
594 accept_info_reply(u_int32 src, u_int32 dst, u_char *p, int datalen)