Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / mrouted / mrinfo.c
1 /*
2  * This tool requests configuration info from a multicast router
3  * and prints the reply (if any).  Invoke it as:
4  *
5  *      mrinfo router-name-or-address
6  *
7  * Written Wed Mar 24 1993 by Van Jacobson (adapted from the
8  * multicast mapper written by Pavel Curtis).
9  *
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.
14  *
15  * Copyright (c) 1993 Regents of the University of California.
16  * All rights reserved.
17  *
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.
33  *
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.
47  * 
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.
53  * 
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.
58  * 
59  * These notices must be retained in any copies of any part of this software.
60  */
61
62 #ifndef lint
63 static const char rcsid[] =
64   "$FreeBSD: src/usr.sbin/mrouted/mrinfo.c,v 1.17.2.1 2002/09/12 16:27:49 nectar Exp $";
65 /*  original rcsid:
66     "@(#) Header: mrinfo.c,v 1.6 93/04/08 15:14:16 van Exp (LBL)";
67 */
68 #endif
69
70 #include <err.h>
71 #include <netdb.h>
72 #include <sys/time.h>
73 #include "defs.h"
74 #include <arpa/inet.h>
75 #ifdef __STDC__
76 #include <stdarg.h>
77 #else
78 #include <varargs.h>
79 #endif
80
81 #define DEFAULT_TIMEOUT 4       /* How long to wait before retrying requests */
82 #define DEFAULT_RETRIES 3       /* How many times to ask each router */
83
84 u_int32 our_addr, target_addr = 0;      /* in NET order */
85 int     debug = 0;
86 int     nflag = 0;
87 int     retries = DEFAULT_RETRIES;
88 int     timeout = DEFAULT_TIMEOUT;
89 int     target_level = 0;
90 vifi_t  numvifs;                /* to keep loader happy */
91                                 /* (see COPY_TABLES macro called in kern.c) */
92
93 char *                  inet_name __P((u_int32 addr));
94 void                    ask __P((u_int32 dst));
95 void                    ask2 __P((u_int32 dst));
96 int                     get_number __P((int *var, int deflt, char ***pargv,
97                                         int *pargc));
98 u_int32                 host_addr __P((char *name));
99 static void             usage __P((void));
100
101 /* to shut up -Wstrict-prototypes */
102 int                     main __P((int argc, char *argv[]));
103
104
105 char   *
106 inet_name(addr)
107         u_int32  addr;
108 {
109         struct hostent *e;
110         struct in_addr in;
111
112         if (addr == 0)
113                 return "local";
114
115         if (nflag ||
116             (e = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET)) == NULL) {
117                 in.s_addr = addr;
118                 return (inet_ntoa(in));
119         }
120         return (e->h_name);
121 }
122
123 /*
124  * Log errors and other messages to stderr, according to the severity of the
125  * message and the current debug level.  For errors of severity LOG_ERR or
126  * worse, terminate the program.
127  */
128 #ifdef __STDC__
129 void
130 log(int severity, int syserr, char *format, ...)
131 {
132         va_list ap;
133         char    fmt[100];
134
135         va_start(ap, format);
136 #else
137 void 
138 log(severity, syserr, format, va_alist)
139         int     severity, syserr;
140         char   *format;
141         va_dcl
142 {
143         va_list ap;
144         char    fmt[100];
145
146         va_start(ap);
147 #endif
148         switch (debug) {
149         case 0:
150                 if (severity > LOG_WARNING)
151                         return;
152         case 1:
153                 if (severity > LOG_NOTICE)
154                         return;
155         case 2:
156                 if (severity > LOG_INFO)
157                         return;
158         default:
159                 fmt[0] = '\0';
160                 if (severity == LOG_WARNING)
161                         strcpy(fmt, "warning - ");
162                 strncat(fmt, format, sizeof(fmt)-strlen(fmt));
163                 fmt[sizeof(fmt)-1]='\0';
164                 vfprintf(stderr, fmt, ap);
165                 if (syserr == 0)
166                         fprintf(stderr, "\n");
167                 else if (syserr < sys_nerr)
168                         fprintf(stderr, ": %s\n", sys_errlist[syserr]);
169                 else
170                         fprintf(stderr, ": errno %d\n", syserr);
171         }
172
173         if (severity <= LOG_ERR)
174                 exit(1);
175 }
176
177 /*
178  * Send a neighbors-list request.
179  */
180 void 
181 ask(dst)
182         u_int32  dst;
183 {
184         send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS,
185                         htonl(MROUTED_LEVEL), 0);
186 }
187
188 void 
189 ask2(dst)
190         u_int32  dst;
191 {
192         send_igmp(our_addr, dst, IGMP_DVMRP, DVMRP_ASK_NEIGHBORS2,
193                         htonl(MROUTED_LEVEL), 0);
194 }
195
196 /*
197  * Process an incoming neighbor-list message.
198  */
199 void 
200 accept_neighbors(src, dst, p, datalen, level)
201         u_int32 src, dst, level;
202         u_char  *p;
203         int     datalen;
204 {
205         u_char *ep = p + datalen;
206 #define GET_ADDR(a) (a = ((u_int32)*p++ << 24), a += ((u_int32)*p++ << 16),\
207                      a += ((u_int32)*p++ << 8), a += *p++)
208
209         printf("%s (%s):\n", inet_fmt(src, s1), inet_name(src));
210         while (p < ep) {
211                 register u_int32 laddr;
212                 register u_char metric;
213                 register u_char thresh;
214                 register int ncount;
215
216                 GET_ADDR(laddr);
217                 laddr = htonl(laddr);
218                 metric = *p++;
219                 thresh = *p++;
220                 ncount = *p++;
221                 while (--ncount >= 0) {
222                         register u_int32 neighbor;
223                         GET_ADDR(neighbor);
224                         neighbor = htonl(neighbor);
225                         printf("  %s -> ", inet_fmt(laddr, s1));
226                         printf("%s (%s) [%d/%d]\n", inet_fmt(neighbor, s1),
227                                inet_name(neighbor), metric, thresh);
228                 }
229         }
230 }
231
232 void 
233 accept_neighbors2(src, dst, p, datalen, level)
234         u_int32 src, dst, level;
235         u_char  *p;
236         int     datalen;
237 {
238         u_char *ep = p + datalen;
239         u_int broken_cisco = ((level & 0xffff) == 0x020a); /* 10.2 */
240         /* well, only possibly_broken_cisco, but that's too long to type. */
241         u_int majvers = level & 0xff;
242         u_int minvers = (level >> 8) & 0xff;
243
244         printf("%s (%s) [", inet_fmt(src, s1), inet_name(src));
245         if (majvers == 3 && minvers == 0xff)
246                 printf("DVMRPv3 compliant");
247         else
248                 printf("version %d.%d", majvers, minvers);
249         printf ("]:\n");
250         
251         while (p < ep) {
252                 register u_char metric;
253                 register u_char thresh;
254                 register u_char flags;
255                 register int ncount;
256                 register u_int32 laddr = *(u_int32*)p;
257
258                 p += 4;
259                 metric = *p++;
260                 thresh = *p++;
261                 flags = *p++;
262                 ncount = *p++;
263                 if (broken_cisco && ncount == 0)        /* dumb Ciscos */
264                         ncount = 1;
265                 if (broken_cisco && ncount > 15)        /* dumb Ciscos */
266                         ncount = ncount & 0xf;
267                 while (--ncount >= 0 && p < ep) {
268                         register u_int32 neighbor = *(u_int32*)p;
269                         p += 4;
270                         printf("  %s -> ", inet_fmt(laddr, s1));
271                         printf("%s (%s) [%d/%d", inet_fmt(neighbor, s1),
272                                inet_name(neighbor), metric, thresh);
273                         if (flags & DVMRP_NF_TUNNEL)
274                                 printf("/tunnel");
275                         if (flags & DVMRP_NF_SRCRT)
276                                 printf("/srcrt");
277                         if (flags & DVMRP_NF_PIM)
278                                 printf("/pim");
279                         if (flags & DVMRP_NF_QUERIER)
280                                 printf("/querier");
281                         if (flags & DVMRP_NF_DISABLED)
282                                 printf("/disabled");
283                         if (flags & DVMRP_NF_DOWN)
284                                 printf("/down");
285                         if (flags & DVMRP_NF_LEAF)
286                                 printf("/leaf");
287                         printf("]\n");
288                 }
289         }
290 }
291
292 int 
293 get_number(var, deflt, pargv, pargc)
294         int    *var, *pargc, deflt;
295         char ***pargv;
296 {
297         if ((*pargv)[0][2] == '\0') {   /* Get the value from the next
298                                          * argument */
299                 if (*pargc > 1 && isdigit((*pargv)[1][0])) {
300                         (*pargv)++, (*pargc)--;
301                         *var = atoi((*pargv)[0]);
302                         return 1;
303                 } else if (deflt >= 0) {
304                         *var = deflt;
305                         return 1;
306                 } else
307                         return 0;
308         } else {                /* Get value from the rest of this argument */
309                 if (isdigit((*pargv)[0][2])) {
310                         *var = atoi((*pargv)[0] + 2);
311                         return 1;
312                 } else {
313                         return 0;
314                 }
315         }
316 }
317
318 static void
319 usage()
320 {
321         fprintf(stderr,
322             "usage: mrinfo [-n] [-t timeout] [-r retries] [router]\n");
323         exit(1);
324 }
325
326 int
327 main(argc, argv)
328         int     argc;
329         char   *argv[];
330 {
331         int tries;
332         int trynew;
333         struct timeval et;
334         struct hostent *hp;
335         struct hostent bogus;
336         char *host;
337         int curaddr;
338
339         if (geteuid() != 0)
340                 errx(1, "must be root");
341
342         init_igmp();
343         setuid(getuid());
344
345         setlinebuf(stderr);
346
347         argv++, argc--;
348         while (argc > 0 && argv[0][0] == '-') {
349                 switch (argv[0][1]) {
350                 case 'd':
351                         if (!get_number(&debug, DEFAULT_DEBUG, &argv, &argc))
352                                 usage();
353                         break;
354                 case 'n':
355                         ++nflag;
356                         break;
357                 case 'r':
358                         if (!get_number(&retries, -1, &argv, &argc))
359                                 usage();
360                         break;
361                 case 't':
362                         if (!get_number(&timeout, -1, &argv, &argc))
363                                 usage();
364                         break;
365                 default:
366                         usage();
367                 }
368                 argv++, argc--;
369         }
370         if (argc > 1)
371                 usage();
372         if (argc == 1)
373                 host = argv[0];
374         else
375                 host = "127.0.0.1";
376
377         if ((target_addr = inet_addr(host)) != -1) {
378                 hp = &bogus;
379                 hp->h_length = sizeof(target_addr);
380                 hp->h_addr_list = (char **)malloc(2 * sizeof(char *));
381                 hp->h_addr_list[0] = malloc(hp->h_length);
382                 memcpy(hp->h_addr_list[0], &target_addr, hp->h_length);
383                 hp->h_addr_list[1] = 0;
384         } else
385                 hp = gethostbyname(host);
386
387         if (hp == NULL || hp->h_length != sizeof(target_addr))
388                 errx(1, "%s: no such host", argv[0]);
389         if (debug)
390                 fprintf(stderr, "Debug level %u\n", debug);
391
392         /* Check all addresses; mrouters often have unreachable interfaces */
393         for (curaddr = 0; hp->h_addr_list[curaddr] != NULL; curaddr++) {
394             memcpy(&target_addr, hp->h_addr_list[curaddr], hp->h_length);
395             {                   /* Find a good local address for us. */
396                 int     udp;
397                 struct sockaddr_in addr;
398                 int     addrlen = sizeof(addr);
399
400                 addr.sin_family = AF_INET;
401 #ifdef HAVE_SA_LEN
402                 addr.sin_len = sizeof addr;
403 #endif
404                 addr.sin_addr.s_addr = target_addr;
405                 addr.sin_port = htons(2000);    /* any port over 1024 will
406                                                  * do... */
407                 if ((udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0
408                 || connect(udp, (struct sockaddr *) & addr, sizeof(addr)) < 0
409                 || getsockname(udp, (struct sockaddr *) & addr, &addrlen) < 0)
410                         err(-1, "determining local address");
411                 close(udp);
412                 our_addr = addr.sin_addr.s_addr;
413             }
414
415             tries = 0;
416             trynew = 1;
417             /*
418              * New strategy: send 'ask2' for two timeouts, then fall back
419              * to 'ask', since it's not very likely that we are going to
420              * find someone who only responds to 'ask' these days
421              */
422             ask2(target_addr);
423
424             gettimeofday(&et, 0);
425             et.tv_sec += timeout;
426
427             /* Main receive loop */
428             for (;;) {
429                 fd_set  fds;
430                 struct timeval tv, now;
431                 int     count, recvlen, dummy = 0;
432                 register u_int32 src, dst, group;
433                 struct ip *ip;
434                 struct igmp *igmp;
435                 int     ipdatalen, iphdrlen, igmpdatalen;
436
437                 if (igmp_socket >= FD_SETSIZE)
438                         log(LOG_ERR, 0, "descriptor too big");
439                 FD_ZERO(&fds);
440                 FD_SET(igmp_socket, &fds);
441
442                 gettimeofday(&now, 0);
443                 tv.tv_sec = et.tv_sec - now.tv_sec;
444                 tv.tv_usec = et.tv_usec - now.tv_usec;
445
446                 if (tv.tv_usec < 0) {
447                         tv.tv_usec += 1000000L;
448                         --tv.tv_sec;
449                 }
450                 if (tv.tv_sec < 0)
451                         tv.tv_sec = tv.tv_usec = 0;
452
453                 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
454
455                 if (count < 0) {
456                         if (errno != EINTR)
457                                 warn("select");
458                         continue;
459                 } else if (count == 0) {
460                         log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
461                         if (++tries > retries)
462                                 break;
463                         /* If we've tried ASK_NEIGHBORS2 twice with
464                          * no response, fall back to ASK_NEIGHBORS
465                          */
466                         if (tries == 2 && target_level == 0)
467                                 trynew = 0;
468                         if (target_level == 0 && trynew == 0)
469                                 ask(target_addr);
470                         else
471                                 ask2(target_addr);
472                         gettimeofday(&et, 0);
473                         et.tv_sec += timeout;
474                         continue;
475                 }
476                 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
477                                    0, NULL, &dummy);
478                 if (recvlen <= 0) {
479                         if (recvlen && errno != EINTR)
480                                 warn("recvfrom");
481                         continue;
482                 }
483
484                 if (recvlen < sizeof(struct ip)) {
485                         log(LOG_WARNING, 0,
486                             "packet too short (%u bytes) for IP header",
487                             recvlen);
488                         continue;
489                 }
490                 ip = (struct ip *) recv_buf;
491                 if (ip->ip_p == 0)
492                         continue;       /* Request to install cache entry */
493                 src = ip->ip_src.s_addr;
494                 dst = ip->ip_dst.s_addr;
495                 iphdrlen = ip->ip_hl << 2;
496 #ifdef RAW_INPUT_IS_RAW
497                 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
498 #else
499                 ipdatalen = ip->ip_len;
500 #endif
501                 if (iphdrlen + ipdatalen != recvlen) {
502                     log(LOG_WARNING, 0,
503                       "packet shorter (%u bytes) than hdr+data length (%u+%u)",
504                       recvlen, iphdrlen, ipdatalen);
505                     continue;
506                 }
507                 igmp = (struct igmp *) (recv_buf + iphdrlen);
508                 group = igmp->igmp_group.s_addr;
509                 igmpdatalen = ipdatalen - IGMP_MINLEN;
510                 if (igmpdatalen < 0) {
511                     log(LOG_WARNING, 0,
512                         "IP data field too short (%u bytes) for IGMP, from %s",
513                         ipdatalen, inet_fmt(src, s1));
514                     continue;
515                 }
516                 if (igmp->igmp_type != IGMP_DVMRP)
517                         continue;
518
519                 switch (igmp->igmp_code) {
520                 case DVMRP_NEIGHBORS:
521                 case DVMRP_NEIGHBORS2:
522                         if (src != target_addr) {
523                                 warnx("got reply from %s instead of %s",
524                                 inet_fmt(src, s1), inet_fmt(target_addr, s1));
525                                 /*continue;*/
526                         }
527                         break;
528                 default:
529                         continue;       /* ignore all other DVMRP messages */
530                 }
531
532                 switch (igmp->igmp_code) {
533
534                 case DVMRP_NEIGHBORS:
535                         if (group) {
536                                 /* knows about DVMRP_NEIGHBORS2 msg */
537                                 if (target_level == 0) {
538                                         target_level = ntohl(group);
539                                         ask2(target_addr);
540                                 }
541                         } else {
542                                 accept_neighbors(src, dst, (u_char *)(igmp + 1),
543                                                  igmpdatalen, ntohl(group));
544                                 exit(0);
545                         }
546                         break;
547
548                 case DVMRP_NEIGHBORS2:
549                         accept_neighbors2(src, dst, (u_char *)(igmp + 1),
550                                           igmpdatalen, ntohl(group));
551                         exit(0);
552                 }
553             }
554         }
555         exit(1);
556 }
557
558 /* dummies */
559 void accept_probe(src, dst, p, datalen, level)
560         u_int32 src, dst, level;
561         char *p;
562         int datalen;
563 {
564 }
565 void accept_group_report(src, dst, group, r_type)
566         u_int32 src, dst, group;
567         int r_type;
568 {
569 }
570 void accept_neighbor_request2(src, dst)
571         u_int32 src, dst;
572 {
573 }
574 void accept_report(src, dst, p, datalen, level)
575         u_int32 src, dst, level;
576         char *p;
577         int datalen;
578 {
579 }
580 void accept_neighbor_request(src, dst)
581         u_int32 src, dst;
582 {
583 }
584 void accept_prune(src, dst, p, datalen)
585         u_int32 src, dst;
586         char *p;
587         int datalen;
588 {
589 }
590 void accept_graft(src, dst, p, datalen)
591         u_int32 src, dst;
592         char *p;
593         int datalen;
594 {
595 }
596 void accept_g_ack(src, dst, p, datalen)
597         u_int32 src, dst;
598         char *p;
599         int datalen;
600 {
601 }
602 void add_table_entry(origin, mcastgrp)
603         u_int32 origin, mcastgrp;
604 {
605 }
606 void check_vif_state()
607 {
608 }
609 void accept_leave_message(src, dst, group)
610         u_int32 src, dst, group;
611 {
612 }
613 void accept_mtrace(src, dst, group, data, no, datalen)
614         u_int32 src, dst, group;
615         char *data;
616         u_int no;
617         int datalen;
618 {
619 }
620 void accept_membership_query(src, dst, group, tmo)
621         u_int32 src, dst, group;
622         int tmo;
623 {
624 }
625 void accept_info_request(src, dst, p, datalen)
626         u_int32 src, dst;
627         u_char *p;
628         int datalen;
629 {
630 }
631 void accept_info_reply(src, dst, p, datalen)
632         u_int32 src, dst;
633         u_char *p;
634         int datalen;
635 {
636 }