]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/mrouted/mrinfo.c
Turn VM_ALLOC_ZERO into a flag.
[FreeBSD/FreeBSD.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$";
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                 FD_ZERO(&fds);
438                 FD_SET(igmp_socket, &fds);
439
440                 gettimeofday(&now, 0);
441                 tv.tv_sec = et.tv_sec - now.tv_sec;
442                 tv.tv_usec = et.tv_usec - now.tv_usec;
443
444                 if (tv.tv_usec < 0) {
445                         tv.tv_usec += 1000000L;
446                         --tv.tv_sec;
447                 }
448                 if (tv.tv_sec < 0)
449                         tv.tv_sec = tv.tv_usec = 0;
450
451                 count = select(igmp_socket + 1, &fds, 0, 0, &tv);
452
453                 if (count < 0) {
454                         if (errno != EINTR)
455                                 warn("select");
456                         continue;
457                 } else if (count == 0) {
458                         log(LOG_DEBUG, 0, "Timed out receiving neighbor lists");
459                         if (++tries > retries)
460                                 break;
461                         /* If we've tried ASK_NEIGHBORS2 twice with
462                          * no response, fall back to ASK_NEIGHBORS
463                          */
464                         if (tries == 2 && target_level == 0)
465                                 trynew = 0;
466                         if (target_level == 0 && trynew == 0)
467                                 ask(target_addr);
468                         else
469                                 ask2(target_addr);
470                         gettimeofday(&et, 0);
471                         et.tv_sec += timeout;
472                         continue;
473                 }
474                 recvlen = recvfrom(igmp_socket, recv_buf, RECV_BUF_SIZE,
475                                    0, NULL, &dummy);
476                 if (recvlen <= 0) {
477                         if (recvlen && errno != EINTR)
478                                 warn("recvfrom");
479                         continue;
480                 }
481
482                 if (recvlen < sizeof(struct ip)) {
483                         log(LOG_WARNING, 0,
484                             "packet too short (%u bytes) for IP header",
485                             recvlen);
486                         continue;
487                 }
488                 ip = (struct ip *) recv_buf;
489                 if (ip->ip_p == 0)
490                         continue;       /* Request to install cache entry */
491                 src = ip->ip_src.s_addr;
492                 dst = ip->ip_dst.s_addr;
493                 iphdrlen = ip->ip_hl << 2;
494 #ifdef RAW_INPUT_IS_RAW
495                 ipdatalen = ntohs(ip->ip_len) - iphdrlen;
496 #else
497                 ipdatalen = ip->ip_len;
498 #endif
499                 if (iphdrlen + ipdatalen != recvlen) {
500                     log(LOG_WARNING, 0,
501                       "packet shorter (%u bytes) than hdr+data length (%u+%u)",
502                       recvlen, iphdrlen, ipdatalen);
503                     continue;
504                 }
505                 igmp = (struct igmp *) (recv_buf + iphdrlen);
506                 group = igmp->igmp_group.s_addr;
507                 igmpdatalen = ipdatalen - IGMP_MINLEN;
508                 if (igmpdatalen < 0) {
509                     log(LOG_WARNING, 0,
510                         "IP data field too short (%u bytes) for IGMP, from %s",
511                         ipdatalen, inet_fmt(src, s1));
512                     continue;
513                 }
514                 if (igmp->igmp_type != IGMP_DVMRP)
515                         continue;
516
517                 switch (igmp->igmp_code) {
518                 case DVMRP_NEIGHBORS:
519                 case DVMRP_NEIGHBORS2:
520                         if (src != target_addr) {
521                                 warnx("got reply from %s instead of %s",
522                                 inet_fmt(src, s1), inet_fmt(target_addr, s1));
523                                 /*continue;*/
524                         }
525                         break;
526                 default:
527                         continue;       /* ignore all other DVMRP messages */
528                 }
529
530                 switch (igmp->igmp_code) {
531
532                 case DVMRP_NEIGHBORS:
533                         if (group) {
534                                 /* knows about DVMRP_NEIGHBORS2 msg */
535                                 if (target_level == 0) {
536                                         target_level = ntohl(group);
537                                         ask2(target_addr);
538                                 }
539                         } else {
540                                 accept_neighbors(src, dst, (u_char *)(igmp + 1),
541                                                  igmpdatalen, ntohl(group));
542                                 exit(0);
543                         }
544                         break;
545
546                 case DVMRP_NEIGHBORS2:
547                         accept_neighbors2(src, dst, (u_char *)(igmp + 1),
548                                           igmpdatalen, ntohl(group));
549                         exit(0);
550                 }
551             }
552         }
553         exit(1);
554 }
555
556 /* dummies */
557 void accept_probe(src, dst, p, datalen, level)
558         u_int32 src, dst, level;
559         char *p;
560         int datalen;
561 {
562 }
563 void accept_group_report(src, dst, group, r_type)
564         u_int32 src, dst, group;
565         int r_type;
566 {
567 }
568 void accept_neighbor_request2(src, dst)
569         u_int32 src, dst;
570 {
571 }
572 void accept_report(src, dst, p, datalen, level)
573         u_int32 src, dst, level;
574         char *p;
575         int datalen;
576 {
577 }
578 void accept_neighbor_request(src, dst)
579         u_int32 src, dst;
580 {
581 }
582 void accept_prune(src, dst, p, datalen)
583         u_int32 src, dst;
584         char *p;
585         int datalen;
586 {
587 }
588 void accept_graft(src, dst, p, datalen)
589         u_int32 src, dst;
590         char *p;
591         int datalen;
592 {
593 }
594 void accept_g_ack(src, dst, p, datalen)
595         u_int32 src, dst;
596         char *p;
597         int datalen;
598 {
599 }
600 void add_table_entry(origin, mcastgrp)
601         u_int32 origin, mcastgrp;
602 {
603 }
604 void check_vif_state()
605 {
606 }
607 void accept_leave_message(src, dst, group)
608         u_int32 src, dst, group;
609 {
610 }
611 void accept_mtrace(src, dst, group, data, no, datalen)
612         u_int32 src, dst, group;
613         char *data;
614         u_int no;
615         int datalen;
616 {
617 }
618 void accept_membership_query(src, dst, group, tmo)
619         u_int32 src, dst, group;
620         int tmo;
621 {
622 }
623 void accept_info_request(src, dst, p, datalen)
624         u_int32 src, dst;
625         u_char *p;
626         int datalen;
627 {
628 }
629 void accept_info_reply(src, dst, p, datalen)
630         u_int32 src, dst;
631         u_char *p;
632         int datalen;
633 {
634 }