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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/sbin/routed/rtquery/rtquery.c,v 1.13 1999/08/28 00:14:21 peter Exp $
33 "@(#) Copyright (c) 1982, 1986, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #define RIPVERSION RIPv2
43 #include <protocols/routed.h>
44 #include <arpa/inet.h>
56 #if !defined(sgi) && !defined(__NetBSD__)
57 static char sccsid[] __attribute__((unused))= "@(#)query.c 8.1 (Berkeley) 6/5/93";
58 #elif defined(__NetBSD__)
59 __RCSID("$NetBSD: rtquery.c,v 1.10 1999/02/23 10:47:41 christos Exp $");
66 #define MD5_DIGEST_LEN 16
68 u_int32_t state[4]; /* state (ABCD) */
69 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
70 unsigned char buffer[64]; /* input buffer */
72 extern void MD5Init(MD5_CTX*);
73 extern void MD5Update(MD5_CTX*, u_char*, u_int);
74 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
77 #define WTIME 15 /* Time to wait for all responses */
78 #define STIME (250*1000) /* usec to wait for another response */
86 char packet[MAXPACKETSIZE+MAXPATHLEN];
88 #define OMSG omsg_buf.rip
89 int omsg_len = sizeof(struct rip);
93 char packet[MAXPACKETSIZE+1024];
95 #define IMSG imsg_buf.rip
97 int nflag; /* numbers, no names */
98 int pflag; /* play the `gated` game */
99 int ripv2 = 1; /* use RIP version 2 */
101 int rflag; /* 1=ask about a particular route */
102 int trace, not_trace; /* send trace command or not */
103 int auth_type = RIP_AUTH_NONE;
104 char passwd[RIP_AUTH_PW_LEN];
107 struct timeval sent; /* when query sent */
109 static char localhost_str[] = "localhost";
110 static char *default_argv[] = {localhost_str, 0};
112 static void rip_input(struct sockaddr_in*, int);
113 static int out(const char *);
114 static void trace_loop(char *argv[]) __attribute((__noreturn__));
115 static void query_loop(char *argv[], int) __attribute((__noreturn__));
116 static int getnet(char *, struct netinfo *);
117 static u_int std_mask(u_int);
118 static int parse_quote(char **, const char *, char *, char *, int);
119 static void usage(void);
127 char *p, *options, *value, delim;
130 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
131 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
132 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
136 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
154 wtime = (int)strtoul(optarg, &p, 0);
164 rflag = getnet(optarg, &OMSG.rip_nets[0]);
166 struct hostent *hp = gethostbyname(optarg);
168 fprintf(stderr, "%s: %s:",
173 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
174 sizeof(OMSG.rip_nets[0].n_dst));
175 OMSG.rip_nets[0].n_family = RIP_AF_INET;
176 OMSG.rip_nets[0].n_mask = -1;
184 while (*options != '\0') {
185 /* messy complications to make -W -Wall happy */
186 static char on_str[] = "on";
187 static char more_str[] = "more";
188 static char off_str[] = "off";
189 static char dump_str[] = "dump";
190 static char *traceopts[] = {
193 # define TRACE_MORE 1
197 # define TRACE_DUMP 3
202 switch (getsubopt(&options,traceopts,&value)) {
204 OMSG.rip_cmd = RIPCMD_TRACEON;
206 || strlen(value) > MAXPATHLEN)
213 OMSG.rip_cmd = RIPCMD_TRACEON;
218 OMSG.rip_cmd = RIPCMD_TRACEOFF;
223 OMSG.rip_cmd = RIPCMD_TRACEON;
224 result = "dump/../table";
229 strcpy((char*)OMSG.rip_tracefile, result);
230 omsg_len += strlen(result) - sizeof(OMSG.ripun);
236 p = strchr(optarg,'=');
240 if (!strcasecmp("passwd",optarg))
241 auth_type = RIP_AUTH_PW;
242 else if (!strcasecmp("md5_passwd",optarg))
243 auth_type = RIP_AUTH_MD5;
246 if (0 > parse_quote(&p,"|",&delim,
247 passwd, sizeof(passwd)))
249 if (auth_type == RIP_AUTH_MD5
251 keyid = strtoul(p+1,&p,0);
252 if (keyid > 255 || *p != '\0')
254 } else if (delim != '\0') {
264 if (not_trace && trace)
271 soc = socket(AF_INET, SOCK_DGRAM, 0);
277 /* be prepared to receive a lot of routes */
278 for (bsize = 127*1024; ; bsize -= 1024) {
279 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
280 &bsize, sizeof(bsize)) == 0)
282 if (bsize <= 4*1024) {
283 perror("setsockopt SO_RCVBUF");
291 query_loop(argv, argc);
301 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
302 " [-a type=passwd] host1 [host2 ...]\n"
303 "\trtquery -t {on=filename|more|off|dump}"
304 " host1 [host2 ...]\n");
309 /* tell the target hosts about tracing
312 trace_loop(char *argv[])
314 struct sockaddr_in myaddr;
317 if (geteuid() != 0) {
318 fprintf(stderr, "-t requires UID 0\n");
323 OMSG.rip_vers = RIPv2;
325 OMSG.rip_vers = RIPv1;
328 memset(&myaddr, 0, sizeof(myaddr));
329 myaddr.sin_family = AF_INET;
331 myaddr.sin_len = sizeof(myaddr);
333 myaddr.sin_port = htons(IPPORT_RESERVED-1);
334 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
335 if (errno != EADDRINUSE
336 || myaddr.sin_port == 0) {
340 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
344 while (*argv != NULL) {
345 if (out(*argv++) <= 0)
352 /* query all of the listed hosts
355 query_loop(char *argv[], int argc)
357 # define NA0 (OMSG.rip_auths[0])
358 # define NA2 (OMSG.rip_auths[2])
366 struct timeval now, delay;
367 struct sockaddr_in from;
372 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
374 OMSG.rip_vers = RIPv2;
375 if (auth_type == RIP_AUTH_PW) {
376 OMSG.rip_nets[1] = OMSG.rip_nets[0];
377 NA0.a_family = RIP_AF_AUTH;
378 NA0.a_type = RIP_AUTH_PW;
379 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
380 omsg_len += sizeof(OMSG.rip_nets[0]);
382 } else if (auth_type == RIP_AUTH_MD5) {
383 OMSG.rip_nets[1] = OMSG.rip_nets[0];
384 NA0.a_family = RIP_AF_AUTH;
385 NA0.a_type = RIP_AUTH_MD5;
386 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
387 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
388 NA0.au.a_md5.md5_seqno = 0;
389 cc = (char *)&NA2-(char *)&OMSG;
390 NA0.au.a_md5.md5_pkt_len = htons(cc);
391 NA2.a_family = RIP_AF_AUTH;
392 NA2.a_type = htons(1);
395 (u_char *)&OMSG, cc);
397 (u_char *)passwd, RIP_AUTH_MD5_LEN);
398 MD5Final(NA2.au.au_pw, &md5_ctx);
399 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
403 OMSG.rip_vers = RIPv1;
404 OMSG.rip_nets[0].n_mask = 0;
407 /* ask the first (valid) host */
409 while (0 > out(*argv++)) {
419 delay.tv_usec = STIME;
420 cc = select(soc+1, &bits, 0,0, &delay);
422 fromlen = sizeof(from);
423 cc = recvfrom(soc, imsg_buf.packet,
424 sizeof(imsg_buf.packet), 0,
425 (struct sockaddr *)&from, &fromlen);
430 /* count the distinct responding hosts.
431 * You cannot match responding hosts with
432 * addresses to which queries were transmitted,
433 * because a router might respond with a
434 * different source address.
436 for (sp = seen; sp != NULL; sp = sp->next) {
437 if (sp->addr.s_addr == from.sin_addr.s_addr)
441 sp = malloc(sizeof(*sp));
444 "rtquery: malloc failed\n");
447 sp->addr = from.sin_addr;
453 rip_input(&from, cc);
464 /* After a pause in responses, probe another host.
465 * This reduces the intermingling of answers.
467 while (*argv != NULL && 0 > out(*argv++))
470 /* continue until no more packets arrive
471 * or we have heard from all hosts
473 if (answered >= argc)
476 /* or until we have waited a long time
478 if (gettimeofday(&now, 0) < 0) {
479 perror("gettimeofday(now)");
482 if (sent.tv_sec + wtime <= now.tv_sec)
486 /* fail if there was no answer */
487 exit (answered >= argc ? 0 : 1);
494 out(const char *host)
496 struct sockaddr_in router;
499 if (gettimeofday(&sent, 0) < 0) {
500 perror("gettimeofday(sent)");
504 memset(&router, 0, sizeof(router));
505 router.sin_family = AF_INET;
507 router.sin_len = sizeof(router);
509 if (!inet_aton(host, &router.sin_addr)) {
510 hp = gethostbyname(host);
515 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
517 router.sin_port = htons(RIP_PORT);
519 if (sendto(soc, &omsg_buf, omsg_len, 0,
520 (struct sockaddr *)&router, sizeof(router)) < 0) {
530 * Convert string to printable characters
533 qstring(u_char *s, int len)
535 static char buf[8*20+1];
540 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
543 for (s2 = s+1; s2 < &s[len]; s2++) {
551 if (c >= ' ' && c < 0x7f && c != '\\') {
573 p += sprintf(p,"%o",c);
584 * Handle an incoming RIP packet.
587 rip_input(struct sockaddr_in *from,
590 struct netinfo *n, *lim;
594 u_char hash[RIP_AUTH_MD5_LEN];
596 u_char md5_authed = 0;
606 printf("%s:", inet_ntoa(from->sin_addr));
608 hp = gethostbyaddr(&from->sin_addr, sizeof(struct in_addr),
612 inet_ntoa(from->sin_addr));
614 printf("%s (%s):", hp->h_name,
615 inet_ntoa(from->sin_addr));
618 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
619 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
622 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
623 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
625 if (size > MAXPACKETSIZE) {
626 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
627 printf(" at least %d bytes too long\n",
629 size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
631 printf(" %d bytes too long\n",
634 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
635 printf(" response of bad length=%d\n", size);
639 lim = (struct netinfo *)((char*)n + size) - 1;
640 for (; n <= lim; n++) {
642 if (n->n_family == RIP_AF_INET) {
643 in.s_addr = n->n_dst;
644 strcpy(net_buf, inet_ntoa(in));
646 mask = ntohl(n->n_mask);
647 dmask = mask & -mask;
649 sp = &net_buf[strlen(net_buf)];
650 if (IMSG.rip_vers == RIPv1) {
651 sprintf(sp," mask=%#x ? ",mask);
653 } else if (mask + dmask == 0) {
656 && ((1<<i)&mask) == 0);
659 sprintf(sp, "/%d",32-i);
661 sprintf(sp," (mask %#x)", mask);
667 mask = std_mask(in.s_addr);
668 if ((ntohl(in.s_addr) & ~mask) != 0)
671 /* Without a netmask, do not worry about
672 * whether the destination is a host or a
673 * network. Try both and use the first name
676 * If we have a netmask we can make a
679 if ((in.s_addr & ~mask) == 0) {
680 np = getnetbyaddr((long)in.s_addr,
684 else if (in.s_addr == 0)
688 && ((in.s_addr & ~mask) != 0
689 || mask == 0xffffffff)) {
690 hp = gethostbyaddr(&in, sizeof(in),
697 } else if (n->n_family == RIP_AF_AUTH) {
698 na = (struct netauth*)n;
699 if (na->a_type == RIP_AUTH_PW
700 && n == IMSG.rip_nets) {
701 printf(" Password Authentication: \"%s\"\n",
702 qstring(na->au.au_pw, RIP_AUTH_PW_LEN));
706 if (na->a_type == RIP_AUTH_MD5
707 && n == IMSG.rip_nets) {
713 ntohs(na->au.a_md5.md5_pkt_len),
714 na->au.a_md5.md5_keyid,
715 na->au.a_md5.md5_auth_len,
716 (int)ntohl(na->au.a_md5.md5_seqno),
717 na->au.a_md5.rsvd[0],
718 na->au.a_md5.rsvd[1]);
722 printf(" Authentication type %d: ", ntohs(na->a_type));
723 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
724 printf("%02x ", na->au.au_pw[i]);
726 if (md5_authed && n+1 > lim
727 && na->a_type == ntohs(1)) {
729 MD5Update(&md5_ctx, (u_char *)&IMSG,
730 (char *)na-(char *)&IMSG);
731 MD5Update(&md5_ctx, (u_char *)passwd,
733 MD5Final(hash, &md5_ctx);
735 memcmp(hash, na->au.au_pw, sizeof(hash))
736 ? "WRONG" : "correct");
741 sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
743 (char)(n->n_dst >> 24),
744 (char)(n->n_dst >> 16),
745 (char)(n->n_dst >> 8),
749 printf(" %-18s metric %2d %-10s",
750 net_buf, (int)ntohl(n->n_metric), name);
752 if (n->n_nhop != 0) {
753 in.s_addr = n->n_nhop;
757 hp = gethostbyaddr(&in, sizeof(in), AF_INET);
758 printf(" nhop=%-15s%s",
759 (hp != NULL) ? hp->h_name : inet_ntoa(in),
760 (IMSG.rip_vers == RIPv1) ? " ?" : "");
763 printf(" tag=%#x%s", n->n_tag,
764 (IMSG.rip_vers == RIPv1) ? " ?" : "");
770 /* Return the classical netmask for an IP address.
773 std_mask(u_int addr) /* in network order */
775 addr = ntohl(addr); /* was a host, not a network */
777 if (addr == 0) /* default route has mask 0 */
780 return IN_CLASSA_NET;
782 return IN_CLASSB_NET;
783 return IN_CLASSC_NET;
787 /* get a network number as a name or a number, with an optional "/xx"
790 static int /* 0=bad */
795 struct netent *nentp;
798 char hname[MAXHOSTNAMELEN+1];
802 /* Detect and separate "1.2.3.4/24"
804 if (NULL != (mname = strrchr(name,'/'))) {
805 i = (int)(mname - name);
806 if (i > (int)sizeof(hname)-1) /* name too long */
808 memmove(hname, name, i);
814 nentp = getnetbyname(name);
816 in.s_addr = nentp->n_net;
817 } else if (inet_aton(name, &in) == 1) {
818 in.s_addr = ntohl(in.s_addr);
824 mask = std_mask(in.s_addr);
825 if ((~mask & in.s_addr) != 0)
828 mask = (u_int)strtoul(mname, &p, 0);
829 if (*p != '\0' || mask > 32)
831 mask = 0xffffffff << (32-mask);
834 rt->n_dst = htonl(in.s_addr);
835 rt->n_family = RIP_AF_INET;
836 rt->n_mask = htonl(mask);
841 /* strtok(), but honoring backslash
843 static int /* -1=bad */
844 parse_quote(char **linep,
865 if (c == '\\' && *pc != '\0') {
866 if ((c = *pc++) == 'n') {
868 } else if (c == 'r') {
870 } else if (c == 't') {
872 } else if (c == 'b') {
874 } else if (c >= '0' && c <= '7') {
876 if (*pc >= '0' && *pc <= '7') {
877 c = (c<<3)+(*pc++ - '0');
878 if (*pc >= '0' && *pc <= '7')
879 c = (c<<3)+(*pc++ - '0');
884 for (p = delims; *p != '\0'; ++p) {