2 * Copyright (c) 1982, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgment:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sbin/routed/rtquery/rtquery.c,v 1.13 1999/08/28 00:14:21 peter Exp $
34 * $DragonFly: src/sbin/routed/rtquery/rtquery.c,v 1.3 2004/07/28 12:27:40 joerg Exp $
38 "@(#) Copyright (c) 1982, 1986, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #define RIPVERSION RIPv2
48 #include <protocols/routed.h>
49 #include <arpa/inet.h>
61 #if !defined(sgi) && !defined(__NetBSD__)
62 static char sccsid[] __attribute__((unused))= "@(#)query.c 8.1 (Berkeley) 6/5/93";
63 #elif defined(__NetBSD__)
64 __RCSID("$NetBSD: rtquery.c,v 1.10 1999/02/23 10:47:41 christos Exp $");
66 #ident "$Revision: 1.12 $"
72 #define MD5_DIGEST_LEN 16
74 u_int32_t state[4]; /* state (ABCD) */
75 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
76 unsigned char buffer[64]; /* input buffer */
78 extern void MD5Init(MD5_CTX*);
79 extern void MD5Update(MD5_CTX*, u_char*, u_int);
80 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
83 #define WTIME 15 /* Time to wait for all responses */
84 #define STIME (250*1000) /* usec to wait for another response */
92 char packet[MAXPACKETSIZE+MAXPATHLEN];
94 #define OMSG omsg_buf.rip
95 int omsg_len = sizeof(struct rip);
99 char packet[MAXPACKETSIZE+1024];
101 #define IMSG imsg_buf.rip
103 int nflag; /* numbers, no names */
104 int pflag; /* play the `gated` game */
105 int ripv2 = 1; /* use RIP version 2 */
107 int rflag; /* 1=ask about a particular route */
108 int trace, not_trace; /* send trace command or not */
109 int auth_type = RIP_AUTH_NONE;
110 char passwd[RIP_AUTH_PW_LEN];
113 struct timeval sent; /* when query sent */
115 static char localhost_str[] = "localhost";
116 static char *default_argv[] = {localhost_str, 0};
118 static void rip_input(struct sockaddr_in*, int);
119 static int out(const char *);
120 static void trace_loop(char *argv[]) __attribute((__noreturn__));
121 static void query_loop(char *argv[], int) __attribute((__noreturn__));
122 static int getnet(char *, struct netinfo *);
123 static u_int std_mask(u_int);
124 static int parse_quote(char **, const char *, char *, char *, int);
125 static void usage(void);
133 char *p, *options, *value, delim;
136 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
137 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
138 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
141 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
159 wtime = (int)strtoul(optarg, &p, 0);
169 rflag = getnet(optarg, &OMSG.rip_nets[0]);
171 struct hostent *hp = gethostbyname(optarg);
173 fprintf(stderr, "%s: %s:",
178 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
179 sizeof(OMSG.rip_nets[0].n_dst));
180 OMSG.rip_nets[0].n_family = RIP_AF_INET;
181 OMSG.rip_nets[0].n_mask = -1;
189 while (*options != '\0') {
190 /* messy complications to make -W -Wall happy */
191 static char on_str[] = "on";
192 static char more_str[] = "more";
193 static char off_str[] = "off";
194 static char dump_str[] = "dump";
195 static char *traceopts[] = {
198 # define TRACE_MORE 1
202 # define TRACE_DUMP 3
207 switch (getsubopt(&options,traceopts,&value)) {
209 OMSG.rip_cmd = RIPCMD_TRACEON;
211 || strlen(value) > MAXPATHLEN)
218 OMSG.rip_cmd = RIPCMD_TRACEON;
223 OMSG.rip_cmd = RIPCMD_TRACEOFF;
228 OMSG.rip_cmd = RIPCMD_TRACEON;
229 result = "dump/../table";
234 strcpy((char*)OMSG.rip_tracefile, result);
235 omsg_len += strlen(result) - sizeof(OMSG.ripun);
241 p = strchr(optarg,'=');
245 if (!strcasecmp("passwd",optarg))
246 auth_type = RIP_AUTH_PW;
247 else if (!strcasecmp("md5_passwd",optarg))
248 auth_type = RIP_AUTH_MD5;
251 if (0 > parse_quote(&p,"|",&delim,
252 passwd, sizeof(passwd)))
254 if (auth_type == RIP_AUTH_MD5
256 keyid = strtoul(p+1,&p,0);
257 if (keyid > 255 || *p != '\0')
259 } else if (delim != '\0') {
269 if (not_trace && trace)
276 soc = socket(AF_INET, SOCK_DGRAM, 0);
282 /* be prepared to receive a lot of routes */
283 for (bsize = 127*1024; ; bsize -= 1024) {
284 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
285 &bsize, sizeof(bsize)) == 0)
287 if (bsize <= 4*1024) {
288 perror("setsockopt SO_RCVBUF");
296 query_loop(argv, argc);
306 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
307 " [-a type=passwd] host1 [host2 ...]\n"
308 "\trtquery -t {on=filename|more|off|dump}"
309 " host1 [host2 ...]\n");
314 /* tell the target hosts about tracing
317 trace_loop(char *argv[])
319 struct sockaddr_in myaddr;
322 if (geteuid() != 0) {
323 (void)fprintf(stderr, "-t requires UID 0\n");
328 OMSG.rip_vers = RIPv2;
330 OMSG.rip_vers = RIPv1;
333 memset(&myaddr, 0, sizeof(myaddr));
334 myaddr.sin_family = AF_INET;
336 myaddr.sin_len = sizeof(myaddr);
338 myaddr.sin_port = htons(IPPORT_RESERVED-1);
339 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
340 if (errno != EADDRINUSE
341 || myaddr.sin_port == 0) {
345 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
350 if (out(*argv++) <= 0)
357 /* query all of the listed hosts
360 query_loop(char *argv[], int argc)
362 # define NA0 (OMSG.rip_auths[0])
363 # define NA2 (OMSG.rip_auths[2])
371 struct timeval now, delay;
372 struct sockaddr_in from;
377 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
379 OMSG.rip_vers = RIPv2;
380 if (auth_type == RIP_AUTH_PW) {
381 OMSG.rip_nets[1] = OMSG.rip_nets[0];
382 NA0.a_family = RIP_AF_AUTH;
383 NA0.a_type = RIP_AUTH_PW;
384 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
385 omsg_len += sizeof(OMSG.rip_nets[0]);
387 } else if (auth_type == RIP_AUTH_MD5) {
388 OMSG.rip_nets[1] = OMSG.rip_nets[0];
389 NA0.a_family = RIP_AF_AUTH;
390 NA0.a_type = RIP_AUTH_MD5;
391 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
392 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
393 NA0.au.a_md5.md5_seqno = 0;
394 cc = (char *)&NA2-(char *)&OMSG;
395 NA0.au.a_md5.md5_pkt_len = htons(cc);
396 NA2.a_family = RIP_AF_AUTH;
397 NA2.a_type = htons(1);
400 (u_char *)&OMSG, cc);
402 (u_char *)passwd, RIP_AUTH_MD5_LEN);
403 MD5Final(NA2.au.au_pw, &md5_ctx);
404 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
408 OMSG.rip_vers = RIPv1;
409 OMSG.rip_nets[0].n_mask = 0;
412 /* ask the first (valid) host */
414 while (0 > out(*argv++)) {
424 delay.tv_usec = STIME;
425 cc = select(soc+1, &bits, 0,0, &delay);
427 fromlen = sizeof(from);
428 cc = recvfrom(soc, imsg_buf.packet,
429 sizeof(imsg_buf.packet), 0,
430 (struct sockaddr *)&from, &fromlen);
435 /* count the distinct responding hosts.
436 * You cannot match responding hosts with
437 * addresses to which queries were transmitted,
438 * because a router might respond with a
439 * different source address.
441 for (sp = seen; sp != 0; sp = sp->next) {
442 if (sp->addr.s_addr == from.sin_addr.s_addr)
446 sp = malloc(sizeof(*sp));
449 "rtquery: malloc failed\n");
452 sp->addr = from.sin_addr;
458 rip_input(&from, cc);
469 /* After a pause in responses, probe another host.
470 * This reduces the intermingling of answers.
472 while (*argv != 0 && 0 > out(*argv++))
475 /* continue until no more packets arrive
476 * or we have heard from all hosts
478 if (answered >= argc)
481 /* or until we have waited a long time
483 if (gettimeofday(&now, 0) < 0) {
484 perror("gettimeofday(now)");
487 if (sent.tv_sec + wtime <= now.tv_sec)
491 /* fail if there was no answer */
492 exit (answered >= argc ? 0 : 1);
499 out(const char *host)
501 struct sockaddr_in router;
504 if (gettimeofday(&sent, 0) < 0) {
505 perror("gettimeofday(sent)");
509 memset(&router, 0, sizeof(router));
510 router.sin_family = AF_INET;
512 router.sin_len = sizeof(router);
514 if (!inet_aton(host, &router.sin_addr)) {
515 hp = gethostbyname(host);
520 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
522 router.sin_port = htons(RIP_PORT);
524 if (sendto(soc, &omsg_buf, omsg_len, 0,
525 (struct sockaddr *)&router, sizeof(router)) < 0) {
535 * Convert string to printable characters
538 qstring(u_char *s, int len)
540 static char buf[8*20+1];
545 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
548 for (s2 = s+1; s2 < &s[len]; s2++) {
556 if (c >= ' ' && c < 0x7f && c != '\\') {
578 p += sprintf(p,"%o",c);
589 * Handle an incoming RIP packet.
592 rip_input(struct sockaddr_in *from,
595 struct netinfo *n, *lim;
599 u_char hash[RIP_AUTH_MD5_LEN];
601 u_char md5_authed = 0;
611 printf("%s:", inet_ntoa(from->sin_addr));
613 hp = gethostbyaddr((char*)&from->sin_addr,
614 sizeof(struct in_addr), AF_INET);
617 inet_ntoa(from->sin_addr));
619 printf("%s (%s):", hp->h_name,
620 inet_ntoa(from->sin_addr));
623 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
624 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
627 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
628 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
630 if (size > MAXPACKETSIZE) {
631 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
632 printf(" at least %d bytes too long\n",
634 size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
636 printf(" %d bytes too long\n",
639 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
640 printf(" response of bad length=%d\n", size);
644 lim = (struct netinfo *)((char*)n + size) - 1;
645 for (; n <= lim; n++) {
647 if (n->n_family == RIP_AF_INET) {
648 in.s_addr = n->n_dst;
649 (void)strcpy(net_buf, inet_ntoa(in));
651 mask = ntohl(n->n_mask);
652 dmask = mask & -mask;
654 sp = &net_buf[strlen(net_buf)];
655 if (IMSG.rip_vers == RIPv1) {
656 (void)sprintf(sp," mask=%#x ? ",mask);
658 } else if (mask + dmask == 0) {
661 && ((1<<i)&mask) == 0);
664 (void)sprintf(sp, "/%d",32-i);
666 (void)sprintf(sp," (mask %#x)", mask);
672 mask = std_mask(in.s_addr);
673 if ((ntohl(in.s_addr) & ~mask) != 0)
676 /* Without a netmask, do not worry about
677 * whether the destination is a host or a
678 * network. Try both and use the first name
681 * If we have a netmask we can make a
684 if ((in.s_addr & ~mask) == 0) {
685 np = getnetbyaddr((long)in.s_addr,
689 else if (in.s_addr == 0)
693 && ((in.s_addr & ~mask) != 0
694 || mask == 0xffffffff)) {
695 hp = gethostbyaddr((char*)&in,
703 } else if (n->n_family == RIP_AF_AUTH) {
704 na = (struct netauth*)n;
705 if (na->a_type == RIP_AUTH_PW
706 && n == IMSG.rip_nets) {
707 (void)printf(" Password Authentication:"
709 qstring(na->au.au_pw,
714 if (na->a_type == RIP_AUTH_MD5
715 && n == IMSG.rip_nets) {
716 (void)printf(" MD5 Auth"
721 ntohs(na->au.a_md5.md5_pkt_len),
722 na->au.a_md5.md5_keyid,
723 na->au.a_md5.md5_auth_len,
724 (int)ntohl(na->au.a_md5.md5_seqno),
725 na->au.a_md5.rsvd[0],
726 na->au.a_md5.rsvd[1]);
730 (void)printf(" Authentication type %d: ",
732 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
733 (void)printf("%02x ", na->au.au_pw[i]);
735 if (md5_authed && n+1 > lim
736 && na->a_type == ntohs(1)) {
738 MD5Update(&md5_ctx, (u_char *)&IMSG,
739 (char *)na-(char *)&IMSG);
740 MD5Update(&md5_ctx, (u_char *)passwd,
742 MD5Final(hash, &md5_ctx);
743 (void)printf(" %s hash\n",
744 memcmp(hash, na->au.au_pw,
746 ? "WRONG" : "correct");
751 (void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
753 (char)(n->n_dst >> 24),
754 (char)(n->n_dst >> 16),
755 (char)(n->n_dst >> 8),
759 (void)printf(" %-18s metric %2d %-10s",
760 net_buf, (int)ntohl(n->n_metric), name);
762 if (n->n_nhop != 0) {
763 in.s_addr = n->n_nhop;
767 hp = gethostbyaddr((char*)&in, sizeof(in),
769 (void)printf(" nhop=%-15s%s",
770 (hp != 0) ? hp->h_name : inet_ntoa(in),
771 (IMSG.rip_vers == RIPv1) ? " ?" : "");
774 (void)printf(" tag=%#x%s", n->n_tag,
775 (IMSG.rip_vers == RIPv1) ? " ?" : "");
781 /* Return the classical netmask for an IP address.
784 std_mask(u_int addr) /* in network order */
786 addr = ntohl(addr); /* was a host, not a network */
788 if (addr == 0) /* default route has mask 0 */
791 return IN_CLASSA_NET;
793 return IN_CLASSB_NET;
794 return IN_CLASSC_NET;
798 /* get a network number as a name or a number, with an optional "/xx"
801 static int /* 0=bad */
806 struct netent *nentp;
809 char hname[MAXHOSTNAMELEN+1];
813 /* Detect and separate "1.2.3.4/24"
815 if (0 != (mname = strrchr(name,'/'))) {
816 i = (int)(mname - name);
817 if (i > (int)sizeof(hname)-1) /* name too long */
819 memmove(hname, name, i);
825 nentp = getnetbyname(name);
827 in.s_addr = nentp->n_net;
828 } else if (inet_aton(name, &in) == 1) {
829 in.s_addr = ntohl(in.s_addr);
835 mask = std_mask(in.s_addr);
836 if ((~mask & in.s_addr) != 0)
839 mask = (u_int)strtoul(mname, &p, 0);
840 if (*p != '\0' || mask > 32)
842 mask = 0xffffffff << (32-mask);
845 rt->n_dst = htonl(in.s_addr);
846 rt->n_family = RIP_AF_INET;
847 rt->n_mask = htonl(mask);
852 /* strtok(), but honoring backslash
854 static int /* -1=bad */
855 parse_quote(char **linep,
876 if (c == '\\' && *pc != '\0') {
877 if ((c = *pc++) == 'n') {
879 } else if (c == 'r') {
881 } else if (c == 't') {
883 } else if (c == 'b') {
885 } else if (c >= '0' && c <= '7') {
887 if (*pc >= '0' && *pc <= '7') {
888 c = (c<<3)+(*pc++ - '0');
889 if (*pc >= '0' && *pc <= '7')
890 c = (c<<3)+(*pc++ - '0');
895 for (p = delims; *p != '\0'; ++p) {