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.4 2004/12/18 21:43:46 swildner 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 $");
71 #define MD5_DIGEST_LEN 16
73 u_int32_t state[4]; /* state (ABCD) */
74 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
75 unsigned char buffer[64]; /* input buffer */
77 extern void MD5Init(MD5_CTX*);
78 extern void MD5Update(MD5_CTX*, u_char*, u_int);
79 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
82 #define WTIME 15 /* Time to wait for all responses */
83 #define STIME (250*1000) /* usec to wait for another response */
91 char packet[MAXPACKETSIZE+MAXPATHLEN];
93 #define OMSG omsg_buf.rip
94 int omsg_len = sizeof(struct rip);
98 char packet[MAXPACKETSIZE+1024];
100 #define IMSG imsg_buf.rip
102 int nflag; /* numbers, no names */
103 int pflag; /* play the `gated` game */
104 int ripv2 = 1; /* use RIP version 2 */
106 int rflag; /* 1=ask about a particular route */
107 int trace, not_trace; /* send trace command or not */
108 int auth_type = RIP_AUTH_NONE;
109 char passwd[RIP_AUTH_PW_LEN];
112 struct timeval sent; /* when query sent */
114 static char localhost_str[] = "localhost";
115 static char *default_argv[] = {localhost_str, 0};
117 static void rip_input(struct sockaddr_in*, int);
118 static int out(const char *);
119 static void trace_loop(char *argv[]) __attribute((__noreturn__));
120 static void query_loop(char *argv[], int) __attribute((__noreturn__));
121 static int getnet(char *, struct netinfo *);
122 static u_int std_mask(u_int);
123 static int parse_quote(char **, const char *, char *, char *, int);
124 static void usage(void);
132 char *p, *options, *value, delim;
135 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
136 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
137 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 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(&from->sin_addr, sizeof(struct in_addr),
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 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 sprintf(sp," mask=%#x ? ",mask);
658 } else if (mask + dmask == 0) {
661 && ((1<<i)&mask) == 0);
664 sprintf(sp, "/%d",32-i);
666 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(&in, sizeof(in),
702 } else if (n->n_family == RIP_AF_AUTH) {
703 na = (struct netauth*)n;
704 if (na->a_type == RIP_AUTH_PW
705 && n == IMSG.rip_nets) {
706 printf(" Password Authentication: \"%s\"\n",
707 qstring(na->au.au_pw, RIP_AUTH_PW_LEN));
711 if (na->a_type == RIP_AUTH_MD5
712 && n == IMSG.rip_nets) {
718 ntohs(na->au.a_md5.md5_pkt_len),
719 na->au.a_md5.md5_keyid,
720 na->au.a_md5.md5_auth_len,
721 (int)ntohl(na->au.a_md5.md5_seqno),
722 na->au.a_md5.rsvd[0],
723 na->au.a_md5.rsvd[1]);
727 printf(" Authentication type %d: ", ntohs(na->a_type));
728 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
729 printf("%02x ", na->au.au_pw[i]);
731 if (md5_authed && n+1 > lim
732 && na->a_type == ntohs(1)) {
734 MD5Update(&md5_ctx, (u_char *)&IMSG,
735 (char *)na-(char *)&IMSG);
736 MD5Update(&md5_ctx, (u_char *)passwd,
738 MD5Final(hash, &md5_ctx);
740 memcmp(hash, na->au.au_pw, sizeof(hash))
741 ? "WRONG" : "correct");
746 sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
748 (char)(n->n_dst >> 24),
749 (char)(n->n_dst >> 16),
750 (char)(n->n_dst >> 8),
754 printf(" %-18s metric %2d %-10s",
755 net_buf, (int)ntohl(n->n_metric), name);
757 if (n->n_nhop != 0) {
758 in.s_addr = n->n_nhop;
762 hp = gethostbyaddr(&in, sizeof(in), AF_INET);
763 printf(" nhop=%-15s%s",
764 (hp != 0) ? hp->h_name : inet_ntoa(in),
765 (IMSG.rip_vers == RIPv1) ? " ?" : "");
768 printf(" tag=%#x%s", n->n_tag,
769 (IMSG.rip_vers == RIPv1) ? " ?" : "");
775 /* Return the classical netmask for an IP address.
778 std_mask(u_int addr) /* in network order */
780 addr = ntohl(addr); /* was a host, not a network */
782 if (addr == 0) /* default route has mask 0 */
785 return IN_CLASSA_NET;
787 return IN_CLASSB_NET;
788 return IN_CLASSC_NET;
792 /* get a network number as a name or a number, with an optional "/xx"
795 static int /* 0=bad */
800 struct netent *nentp;
803 char hname[MAXHOSTNAMELEN+1];
807 /* Detect and separate "1.2.3.4/24"
809 if (0 != (mname = strrchr(name,'/'))) {
810 i = (int)(mname - name);
811 if (i > (int)sizeof(hname)-1) /* name too long */
813 memmove(hname, name, i);
819 nentp = getnetbyname(name);
821 in.s_addr = nentp->n_net;
822 } else if (inet_aton(name, &in) == 1) {
823 in.s_addr = ntohl(in.s_addr);
829 mask = std_mask(in.s_addr);
830 if ((~mask & in.s_addr) != 0)
833 mask = (u_int)strtoul(mname, &p, 0);
834 if (*p != '\0' || mask > 32)
836 mask = 0xffffffff << (32-mask);
839 rt->n_dst = htonl(in.s_addr);
840 rt->n_family = RIP_AF_INET;
841 rt->n_mask = htonl(mask);
846 /* strtok(), but honoring backslash
848 static int /* -1=bad */
849 parse_quote(char **linep,
870 if (c == '\\' && *pc != '\0') {
871 if ((c = *pc++) == 'n') {
873 } else if (c == 'r') {
875 } else if (c == 't') {
877 } else if (c == 'b') {
879 } else if (c >= '0' && c <= '7') {
881 if (*pc >= '0' && *pc <= '7') {
882 c = (c<<3)+(*pc++ - '0');
883 if (*pc >= '0' && *pc <= '7')
884 c = (c<<3)+(*pc++ - '0');
889 for (p = delims; *p != '\0'; ++p) {