2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <sys/protosw.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #define RIPVERSION RIPv2
41 #include <protocols/routed.h>
42 #include <arpa/inet.h>
54 #define UNUSED __attribute__((unused))
56 #define __RCSID(_s) static const char rcsid[] UNUSED = _s
59 #define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s
61 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n"
62 "The Regents of the University of California."
63 " All rights reserved.\n");
66 #elif defined(__FreeBSD__)
69 __RCSID("$Revision: 2.26 $");
70 #ident "$Revision: 2.26 $"
80 #define MD5_DIGEST_LEN 16
82 u_int32_t state[4]; /* state (ABCD) */
83 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
84 unsigned char buffer[64]; /* input buffer */
86 extern void MD5Init(MD5_CTX*);
87 extern void MD5Update(MD5_CTX*, u_char*, u_int);
88 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
92 #define WTIME 15 /* Time to wait for all responses */
93 #define STIME (250*1000) /* usec to wait for another response */
101 char packet[MAXPACKETSIZE+MAXPATHLEN];
103 #define OMSG omsg_buf.rip
104 int omsg_len = sizeof(struct rip);
108 char packet[MAXPACKETSIZE+1024];
110 #define IMSG imsg_buf.rip
112 int nflag; /* numbers, no names */
113 int pflag; /* play the `gated` game */
114 int ripv2 = 1; /* use RIP version 2 */
116 int rflag; /* 1=ask about a particular route */
117 int trace, not_trace; /* send trace command or not */
118 int auth_type = RIP_AUTH_NONE;
119 char passwd[RIP_AUTH_PW_LEN];
122 struct timeval sent; /* when query sent */
124 static char localhost_str[] = "localhost";
125 static char *default_argv[] = {localhost_str, 0};
127 static void rip_input(struct sockaddr_in*, int);
128 static int out(const char *);
129 static void trace_loop(char *argv[]) __attribute((__noreturn__));
130 static void query_loop(char *argv[], int) __attribute((__noreturn__));
131 static int getnet(char *, struct netinfo *);
132 static u_int std_mask(u_int);
133 static int parse_quote(char **, const char *, char *, char *, int);
134 static void usage(void);
142 char *p, *options, *value, delim;
145 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
146 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
147 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
150 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
168 wtime = (int)strtoul(optarg, &p, 0);
178 rflag = getnet(optarg, &OMSG.rip_nets[0]);
180 struct hostent *hp = gethostbyname(optarg);
182 fprintf(stderr, "%s: %s:",
187 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
188 sizeof(OMSG.rip_nets[0].n_dst));
189 OMSG.rip_nets[0].n_family = RIP_AF_INET;
190 OMSG.rip_nets[0].n_mask = -1;
198 while (*options != '\0') {
199 /* messy complications to make -W -Wall happy */
200 static char on_str[] = "on";
201 static char more_str[] = "more";
202 static char off_str[] = "off";
203 static char dump_str[] = "dump";
204 static char *traceopts[] = {
207 # define TRACE_MORE 1
211 # define TRACE_DUMP 3
216 switch (getsubopt(&options,traceopts,&value)) {
218 OMSG.rip_cmd = RIPCMD_TRACEON;
220 || strlen(value) > MAXPATHLEN)
227 OMSG.rip_cmd = RIPCMD_TRACEON;
232 OMSG.rip_cmd = RIPCMD_TRACEOFF;
237 OMSG.rip_cmd = RIPCMD_TRACEON;
238 result = "dump/../table";
243 strcpy((char*)OMSG.rip_tracefile, result);
244 omsg_len += strlen(result) - sizeof(OMSG.ripun);
250 p = strchr(optarg,'=');
254 if (!strcasecmp("passwd",optarg))
255 auth_type = RIP_AUTH_PW;
256 else if (!strcasecmp("md5_passwd",optarg))
257 auth_type = RIP_AUTH_MD5;
260 if (0 > parse_quote(&p,"|",&delim,
261 passwd, sizeof(passwd)))
263 if (auth_type == RIP_AUTH_MD5
265 keyid = strtoul(p+1,&p,0);
266 if (keyid > 255 || *p != '\0')
268 } else if (delim != '\0') {
278 if (not_trace && trace)
285 soc = socket(AF_INET, SOCK_DGRAM, 0);
291 /* be prepared to receive a lot of routes */
292 for (bsize = 127*1024; ; bsize -= 1024) {
293 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
294 &bsize, sizeof(bsize)) == 0)
296 if (bsize <= 4*1024) {
297 perror("setsockopt SO_RCVBUF");
305 query_loop(argv, argc);
315 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
316 " [-a type=passwd] host1 [host2 ...]\n"
317 "\trtquery -t {on=filename|more|off|dump}"
318 " host1 [host2 ...]\n");
323 /* tell the target hosts about tracing
326 trace_loop(char *argv[])
328 struct sockaddr_in myaddr;
331 if (geteuid() != 0) {
332 (void)fprintf(stderr, "-t requires UID 0\n");
337 OMSG.rip_vers = RIPv2;
339 OMSG.rip_vers = RIPv1;
342 memset(&myaddr, 0, sizeof(myaddr));
343 myaddr.sin_family = AF_INET;
345 myaddr.sin_len = sizeof(myaddr);
347 myaddr.sin_port = htons(IPPORT_RESERVED-1);
348 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
349 if (errno != EADDRINUSE
350 || myaddr.sin_port == 0) {
354 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
358 while (*argv != NULL) {
359 if (out(*argv++) <= 0)
366 /* query all of the listed hosts
369 query_loop(char *argv[], int argc)
371 # define NA0 (OMSG.rip_auths[0])
372 # define NA2 (OMSG.rip_auths[2])
380 struct timeval now, delay;
381 struct sockaddr_in from;
386 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
388 OMSG.rip_vers = RIPv2;
389 if (auth_type == RIP_AUTH_PW) {
390 OMSG.rip_nets[1] = OMSG.rip_nets[0];
391 NA0.a_family = RIP_AF_AUTH;
392 NA0.a_type = RIP_AUTH_PW;
393 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
394 omsg_len += sizeof(OMSG.rip_nets[0]);
396 } else if (auth_type == RIP_AUTH_MD5) {
397 OMSG.rip_nets[1] = OMSG.rip_nets[0];
398 NA0.a_family = RIP_AF_AUTH;
399 NA0.a_type = RIP_AUTH_MD5;
400 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
401 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN;
402 NA0.au.a_md5.md5_seqno = 0;
403 cc = (char *)&NA2-(char *)&OMSG;
404 NA0.au.a_md5.md5_pkt_len = htons(cc);
405 NA2.a_family = RIP_AF_AUTH;
406 NA2.a_type = htons(1);
409 (u_char *)&OMSG, cc);
411 (u_char *)passwd, RIP_AUTH_MD5_HASH_LEN);
412 MD5Final(NA2.au.au_pw, &md5_ctx);
413 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
417 OMSG.rip_vers = RIPv1;
418 OMSG.rip_nets[0].n_mask = 0;
421 /* ask the first (valid) host */
423 while (0 > out(*argv++)) {
433 delay.tv_usec = STIME;
434 cc = select(soc+1, &bits, 0,0, &delay);
436 fromlen = sizeof(from);
437 cc = recvfrom(soc, imsg_buf.packet,
438 sizeof(imsg_buf.packet), 0,
439 (struct sockaddr *)&from, &fromlen);
444 /* count the distinct responding hosts.
445 * You cannot match responding hosts with
446 * addresses to which queries were transmitted,
447 * because a router might respond with a
448 * different source address.
450 for (sp = seen; sp != NULL; sp = sp->next) {
451 if (sp->addr.s_addr == from.sin_addr.s_addr)
455 sp = malloc(sizeof(*sp));
458 "rtquery: malloc failed\n");
461 sp->addr = from.sin_addr;
467 rip_input(&from, cc);
478 /* After a pause in responses, probe another host.
479 * This reduces the intermingling of answers.
481 while (*argv != NULL && out(*argv++) < 0)
484 /* continue until no more packets arrive
485 * or we have heard from all hosts
487 if (answered >= argc)
490 /* or until we have waited a long time
492 if (gettimeofday(&now, 0) < 0) {
493 perror("gettimeofday(now)");
496 if (sent.tv_sec + wtime <= now.tv_sec)
500 /* fail if there was no answer */
501 exit (answered >= argc ? 0 : 1);
508 out(const char *host)
510 struct sockaddr_in router;
513 if (gettimeofday(&sent, 0) < 0) {
514 perror("gettimeofday(sent)");
518 memset(&router, 0, sizeof(router));
519 router.sin_family = AF_INET;
521 router.sin_len = sizeof(router);
523 if (!inet_aton(host, &router.sin_addr)) {
524 hp = gethostbyname(host);
529 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
531 router.sin_port = htons(RIP_PORT);
533 if (sendto(soc, &omsg_buf, omsg_len, 0,
534 (struct sockaddr *)&router, sizeof(router)) < 0) {
544 * Convert string to printable characters
547 qstring(u_char *s, int len)
549 static char buf[8*20+1];
554 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
557 for (s2 = s+1; s2 < &s[len]; s2++) {
565 if (c >= ' ' && c < 0x7f && c != '\\') {
587 p += sprintf(p,"%o",c);
598 * Handle an incoming RIP packet.
601 rip_input(struct sockaddr_in *from,
604 struct netinfo *n, *lim;
608 u_char hash[RIP_AUTH_MD5_KEY_LEN];
610 u_char md5_authed = 0;
620 printf("%s:", inet_ntoa(from->sin_addr));
622 hp = gethostbyaddr((char*)&from->sin_addr,
623 sizeof(struct in_addr), AF_INET);
626 inet_ntoa(from->sin_addr));
628 printf("%s (%s):", hp->h_name,
629 inet_ntoa(from->sin_addr));
632 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
633 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
636 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
637 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
639 if (size > MAXPACKETSIZE) {
640 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
641 printf(" at least %d bytes too long\n",
643 size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
645 printf(" %d bytes too long\n",
648 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
649 printf(" response of bad length=%d\n", size);
653 lim = (struct netinfo *)((char*)n + size) - 1;
654 for (; n <= lim; n++) {
656 if (n->n_family == RIP_AF_INET) {
657 in.s_addr = n->n_dst;
658 (void)strcpy(net_buf, inet_ntoa(in));
660 mask = ntohl(n->n_mask);
661 dmask = mask & -mask;
663 sp = &net_buf[strlen(net_buf)];
664 if (IMSG.rip_vers == RIPv1) {
665 (void)sprintf(sp," mask=%#x ? ",mask);
667 } else if (mask + dmask == 0) {
670 && ((1<<i)&mask) == 0);
673 (void)sprintf(sp, "/%d",32-i);
675 (void)sprintf(sp," (mask %#x)", mask);
681 mask = std_mask(in.s_addr);
682 if ((ntohl(in.s_addr) & ~mask) != 0)
685 /* Without a netmask, do not worry about
686 * whether the destination is a host or a
687 * network. Try both and use the first name
690 * If we have a netmask we can make a
693 if ((in.s_addr & ~mask) == 0) {
694 np = getnetbyaddr((long)in.s_addr,
698 else if (in.s_addr == 0)
702 && ((in.s_addr & ~mask) != 0
703 || mask == 0xffffffff)) {
704 hp = gethostbyaddr((char*)&in,
712 } else if (n->n_family == RIP_AF_AUTH) {
713 na = (struct netauth*)n;
714 if (na->a_type == RIP_AUTH_PW
715 && n == IMSG.rip_nets) {
716 (void)printf(" Password Authentication:"
718 qstring(na->au.au_pw,
723 if (na->a_type == RIP_AUTH_MD5
724 && n == IMSG.rip_nets) {
725 (void)printf(" MD5 Auth"
730 ntohs(na->au.a_md5.md5_pkt_len),
731 na->au.a_md5.md5_keyid,
732 na->au.a_md5.md5_auth_len,
733 (int)ntohl(na->au.a_md5.md5_seqno),
734 na->au.a_md5.rsvd[0],
735 na->au.a_md5.rsvd[1]);
739 (void)printf(" Authentication type %d: ",
741 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
742 (void)printf("%02x ", na->au.au_pw[i]);
744 if (md5_authed && n+1 > lim
745 && na->a_type == ntohs(1)) {
747 MD5Update(&md5_ctx, (u_char *)&IMSG,
748 (char *)na-(char *)&IMSG
749 +RIP_AUTH_MD5_HASH_XTRA);
750 MD5Update(&md5_ctx, (u_char *)passwd,
751 RIP_AUTH_MD5_KEY_LEN);
752 MD5Final(hash, &md5_ctx);
753 (void)printf(" %s hash\n",
754 memcmp(hash, na->au.au_pw,
756 ? "WRONG" : "correct");
761 (void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
763 (u_char)(n->n_dst >> 24),
764 (u_char)(n->n_dst >> 16),
765 (u_char)(n->n_dst >> 8),
769 (void)printf(" %-18s metric %2d %-10s",
770 net_buf, (int)ntohl(n->n_metric), name);
772 if (n->n_nhop != 0) {
773 in.s_addr = n->n_nhop;
777 hp = gethostbyaddr((char*)&in, sizeof(in),
779 (void)printf(" nhop=%-15s%s",
780 (hp != NULL) ? hp->h_name : inet_ntoa(in),
781 (IMSG.rip_vers == RIPv1) ? " ?" : "");
784 (void)printf(" tag=%#x%s", n->n_tag,
785 (IMSG.rip_vers == RIPv1) ? " ?" : "");
791 /* Return the classical netmask for an IP address.
794 std_mask(u_int addr) /* in network order */
796 addr = ntohl(addr); /* was a host, not a network */
798 if (addr == 0) /* default route has mask 0 */
801 return IN_CLASSA_NET;
803 return IN_CLASSB_NET;
804 return IN_CLASSC_NET;
808 /* get a network number as a name or a number, with an optional "/xx"
811 static int /* 0=bad */
816 struct netent *nentp;
819 char hname[MAXHOSTNAMELEN+1];
823 /* Detect and separate "1.2.3.4/24"
825 if (NULL != (mname = strrchr(name,'/'))) {
826 i = (int)(mname - name);
827 if (i > (int)sizeof(hname)-1) /* name too long */
829 memmove(hname, name, i);
835 nentp = getnetbyname(name);
837 in.s_addr = nentp->n_net;
838 } else if (inet_aton(name, &in) == 1) {
839 in.s_addr = ntohl(in.s_addr);
845 mask = std_mask(in.s_addr);
846 if ((~mask & in.s_addr) != 0)
849 mask = (u_int)strtoul(mname, &p, 0);
850 if (*p != '\0' || mask > 32)
852 mask = 0xffffffff << (32-mask);
855 rt->n_dst = htonl(in.s_addr);
856 rt->n_family = RIP_AF_INET;
857 rt->n_mask = htonl(mask);
862 /* strtok(), but honoring backslash
864 static int /* -1=bad */
865 parse_quote(char **linep,
886 if (c == '\\' && *pc != '\0') {
887 if ((c = *pc++) == 'n') {
889 } else if (c == 'r') {
891 } else if (c == 't') {
893 } else if (c == 'b') {
895 } else if (c >= '0' && c <= '7') {
897 if (*pc >= '0' && *pc <= '7') {
898 c = (c<<3)+(*pc++ - '0');
899 if (*pc >= '0' && *pc <= '7')
900 c = (c<<3)+(*pc++ - '0');
905 for (p = delims; *p != '\0'; ++p) {