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 * 4. 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
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #define RIPVERSION RIPv2
39 #include <protocols/routed.h>
40 #include <arpa/inet.h>
52 #define UNUSED __attribute__((unused))
54 #define __RCSID(_s) static const char rcsid[] UNUSED = _s
57 #define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s
59 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n"
60 "The Regents of the University of California."
61 " All rights reserved.\n");
64 #elif defined(__FreeBSD__)
67 __RCSID("$Revision: 2.26 $");
68 #ident "$Revision: 2.26 $"
78 #define MD5_DIGEST_LEN 16
80 u_int32_t state[4]; /* state (ABCD) */
81 u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */
82 unsigned char buffer[64]; /* input buffer */
84 extern void MD5Init(MD5_CTX*);
85 extern void MD5Update(MD5_CTX*, u_char*, u_int);
86 extern void MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
90 #define WTIME 15 /* Time to wait for all responses */
91 #define STIME (250*1000) /* usec to wait for another response */
99 char packet[MAXPACKETSIZE+MAXPATHLEN];
101 #define OMSG omsg_buf.rip
102 int omsg_len = sizeof(struct rip);
106 char packet[MAXPACKETSIZE+1024];
108 #define IMSG imsg_buf.rip
110 int nflag; /* numbers, no names */
111 int pflag; /* play the `gated` game */
112 int ripv2 = 1; /* use RIP version 2 */
114 int rflag; /* 1=ask about a particular route */
115 int trace, not_trace; /* send trace command or not */
116 int auth_type = RIP_AUTH_NONE;
117 char passwd[RIP_AUTH_PW_LEN];
120 struct timeval sent; /* when query sent */
122 static char localhost_str[] = "localhost";
123 static char *default_argv[] = {localhost_str, 0};
125 static void rip_input(struct sockaddr_in*, int);
126 static int out(const char *);
127 static void trace_loop(char *argv[]) __attribute((__noreturn__));
128 static void query_loop(char *argv[], int) __attribute((__noreturn__));
129 static int getnet(char *, struct netinfo *);
130 static u_int std_mask(u_int);
131 static int parse_quote(char **, const char *, char *, char *, int);
132 static void usage(void);
140 char *p, *options, *value, delim;
143 OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
144 OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
145 OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
148 while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
166 wtime = (int)strtoul(optarg, &p, 0);
176 rflag = getnet(optarg, &OMSG.rip_nets[0]);
178 struct hostent *hp = gethostbyname(optarg);
180 fprintf(stderr, "%s: %s:",
185 memcpy(&OMSG.rip_nets[0].n_dst, hp->h_addr,
186 sizeof(OMSG.rip_nets[0].n_dst));
187 OMSG.rip_nets[0].n_family = RIP_AF_INET;
188 OMSG.rip_nets[0].n_mask = -1;
196 while (*options != '\0') {
197 /* messy complications to make -W -Wall happy */
198 static char on_str[] = "on";
199 static char more_str[] = "more";
200 static char off_str[] = "off";
201 static char dump_str[] = "dump";
202 static char *traceopts[] = {
205 # define TRACE_MORE 1
209 # define TRACE_DUMP 3
214 switch (getsubopt(&options,traceopts,&value)) {
216 OMSG.rip_cmd = RIPCMD_TRACEON;
218 || strlen(value) > MAXPATHLEN)
225 OMSG.rip_cmd = RIPCMD_TRACEON;
230 OMSG.rip_cmd = RIPCMD_TRACEOFF;
235 OMSG.rip_cmd = RIPCMD_TRACEON;
236 result = "dump/../table";
241 strcpy((char*)OMSG.rip_tracefile, result);
242 omsg_len += strlen(result) - sizeof(OMSG.ripun);
248 p = strchr(optarg,'=');
252 if (!strcasecmp("passwd",optarg))
253 auth_type = RIP_AUTH_PW;
254 else if (!strcasecmp("md5_passwd",optarg))
255 auth_type = RIP_AUTH_MD5;
258 if (0 > parse_quote(&p,"|",&delim,
259 passwd, sizeof(passwd)))
261 if (auth_type == RIP_AUTH_MD5
263 keyid = strtoul(p+1,&p,0);
264 if (keyid > 255 || *p != '\0')
266 } else if (delim != '\0') {
276 if (not_trace && trace)
283 soc = socket(AF_INET, SOCK_DGRAM, 0);
289 /* be prepared to receive a lot of routes */
290 for (bsize = 127*1024; ; bsize -= 1024) {
291 if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
292 &bsize, sizeof(bsize)) == 0)
294 if (bsize <= 4*1024) {
295 perror("setsockopt SO_RCVBUF");
303 query_loop(argv, argc);
313 "usage: rtquery [-np1] [-r tgt_rt] [-w wtime]"
314 " [-a type=passwd] host1 [host2 ...]\n"
315 "\trtquery -t {on=filename|more|off|dump}"
316 " host1 [host2 ...]\n");
321 /* tell the target hosts about tracing
324 trace_loop(char *argv[])
326 struct sockaddr_in myaddr;
329 if (geteuid() != 0) {
330 (void)fprintf(stderr, "-t requires UID 0\n");
335 OMSG.rip_vers = RIPv2;
337 OMSG.rip_vers = RIPv1;
340 memset(&myaddr, 0, sizeof(myaddr));
341 myaddr.sin_family = AF_INET;
343 myaddr.sin_len = sizeof(myaddr);
345 myaddr.sin_port = htons(IPPORT_RESERVED-1);
346 while (bind(soc, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
347 if (errno != EADDRINUSE
348 || myaddr.sin_port == 0) {
352 myaddr.sin_port = htons(ntohs(myaddr.sin_port)-1);
357 if (out(*argv++) <= 0)
364 /* query all of the listed hosts
367 query_loop(char *argv[], int argc)
369 # define NA0 (OMSG.rip_auths[0])
370 # define NA2 (OMSG.rip_auths[2])
378 struct timeval now, delay;
379 struct sockaddr_in from;
384 OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
386 OMSG.rip_vers = RIPv2;
387 if (auth_type == RIP_AUTH_PW) {
388 OMSG.rip_nets[1] = OMSG.rip_nets[0];
389 NA0.a_family = RIP_AF_AUTH;
390 NA0.a_type = RIP_AUTH_PW;
391 memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
392 omsg_len += sizeof(OMSG.rip_nets[0]);
394 } else if (auth_type == RIP_AUTH_MD5) {
395 OMSG.rip_nets[1] = OMSG.rip_nets[0];
396 NA0.a_family = RIP_AF_AUTH;
397 NA0.a_type = RIP_AUTH_MD5;
398 NA0.au.a_md5.md5_keyid = (int8_t)keyid;
399 NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN;
400 NA0.au.a_md5.md5_seqno = 0;
401 cc = (char *)&NA2-(char *)&OMSG;
402 NA0.au.a_md5.md5_pkt_len = htons(cc);
403 NA2.a_family = RIP_AF_AUTH;
404 NA2.a_type = htons(1);
407 (u_char *)&OMSG, cc);
409 (u_char *)passwd, RIP_AUTH_MD5_HASH_LEN);
410 MD5Final(NA2.au.au_pw, &md5_ctx);
411 omsg_len += 2*sizeof(OMSG.rip_nets[0]);
415 OMSG.rip_vers = RIPv1;
416 OMSG.rip_nets[0].n_mask = 0;
419 /* ask the first (valid) host */
421 while (0 > out(*argv++)) {
431 delay.tv_usec = STIME;
432 cc = select(soc+1, &bits, 0,0, &delay);
434 fromlen = sizeof(from);
435 cc = recvfrom(soc, imsg_buf.packet,
436 sizeof(imsg_buf.packet), 0,
437 (struct sockaddr *)&from, &fromlen);
442 /* count the distinct responding hosts.
443 * You cannot match responding hosts with
444 * addresses to which queries were transmitted,
445 * because a router might respond with a
446 * different source address.
448 for (sp = seen; sp != 0; sp = sp->next) {
449 if (sp->addr.s_addr == from.sin_addr.s_addr)
453 sp = malloc(sizeof(*sp));
456 "rtquery: malloc failed\n");
459 sp->addr = from.sin_addr;
465 rip_input(&from, cc);
476 /* After a pause in responses, probe another host.
477 * This reduces the intermingling of answers.
479 while (*argv != 0 && 0 > out(*argv++))
482 /* continue until no more packets arrive
483 * or we have heard from all hosts
485 if (answered >= argc)
488 /* or until we have waited a long time
490 if (gettimeofday(&now, 0) < 0) {
491 perror("gettimeofday(now)");
494 if (sent.tv_sec + wtime <= now.tv_sec)
498 /* fail if there was no answer */
499 exit (answered >= argc ? 0 : 1);
506 out(const char *host)
508 struct sockaddr_in router;
511 if (gettimeofday(&sent, 0) < 0) {
512 perror("gettimeofday(sent)");
516 memset(&router, 0, sizeof(router));
517 router.sin_family = AF_INET;
519 router.sin_len = sizeof(router);
521 if (!inet_aton(host, &router.sin_addr)) {
522 hp = gethostbyname(host);
527 memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
529 router.sin_port = htons(RIP_PORT);
531 if (sendto(soc, &omsg_buf, omsg_len, 0,
532 (struct sockaddr *)&router, sizeof(router)) < 0) {
542 * Convert string to printable characters
545 qstring(u_char *s, int len)
547 static char buf[8*20+1];
552 for (p = buf; len != 0 && p < &buf[sizeof(buf)-1]; len--) {
555 for (s2 = s+1; s2 < &s[len]; s2++) {
563 if (c >= ' ' && c < 0x7f && c != '\\') {
585 p += sprintf(p,"%o",c);
596 * Handle an incoming RIP packet.
599 rip_input(struct sockaddr_in *from,
602 struct netinfo *n, *lim;
606 u_char hash[RIP_AUTH_MD5_KEY_LEN];
608 u_char md5_authed = 0;
618 printf("%s:", inet_ntoa(from->sin_addr));
620 hp = gethostbyaddr((char*)&from->sin_addr,
621 sizeof(struct in_addr), AF_INET);
624 inet_ntoa(from->sin_addr));
626 printf("%s (%s):", hp->h_name,
627 inet_ntoa(from->sin_addr));
630 if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
631 printf("\n unexpected response type %d\n", IMSG.rip_cmd);
634 printf(" RIPv%d%s %d bytes\n", IMSG.rip_vers,
635 (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
637 if (size > MAXPACKETSIZE) {
638 if (size > (int)sizeof(imsg_buf) - (int)sizeof(*n)) {
639 printf(" at least %d bytes too long\n",
641 size = (int)sizeof(imsg_buf) - (int)sizeof(*n);
643 printf(" %d bytes too long\n",
646 } else if (size%sizeof(*n) != sizeof(struct rip)%sizeof(*n)) {
647 printf(" response of bad length=%d\n", size);
651 lim = (struct netinfo *)((char*)n + size) - 1;
652 for (; n <= lim; n++) {
654 if (n->n_family == RIP_AF_INET) {
655 in.s_addr = n->n_dst;
656 (void)strcpy(net_buf, inet_ntoa(in));
658 mask = ntohl(n->n_mask);
659 dmask = mask & -mask;
661 sp = &net_buf[strlen(net_buf)];
662 if (IMSG.rip_vers == RIPv1) {
663 (void)sprintf(sp," mask=%#x ? ",mask);
665 } else if (mask + dmask == 0) {
668 && ((1<<i)&mask) == 0);
671 (void)sprintf(sp, "/%d",32-i);
673 (void)sprintf(sp," (mask %#x)", mask);
679 mask = std_mask(in.s_addr);
680 if ((ntohl(in.s_addr) & ~mask) != 0)
683 /* Without a netmask, do not worry about
684 * whether the destination is a host or a
685 * network. Try both and use the first name
688 * If we have a netmask we can make a
691 if ((in.s_addr & ~mask) == 0) {
692 np = getnetbyaddr((long)in.s_addr,
696 else if (in.s_addr == 0)
700 && ((in.s_addr & ~mask) != 0
701 || mask == 0xffffffff)) {
702 hp = gethostbyaddr((char*)&in,
710 } else if (n->n_family == RIP_AF_AUTH) {
711 na = (struct netauth*)n;
712 if (na->a_type == RIP_AUTH_PW
713 && n == IMSG.rip_nets) {
714 (void)printf(" Password Authentication:"
716 qstring(na->au.au_pw,
721 if (na->a_type == RIP_AUTH_MD5
722 && n == IMSG.rip_nets) {
723 (void)printf(" MD5 Auth"
728 ntohs(na->au.a_md5.md5_pkt_len),
729 na->au.a_md5.md5_keyid,
730 na->au.a_md5.md5_auth_len,
731 (int)ntohl(na->au.a_md5.md5_seqno),
732 na->au.a_md5.rsvd[0],
733 na->au.a_md5.rsvd[1]);
737 (void)printf(" Authentication type %d: ",
739 for (i = 0; i < (int)sizeof(na->au.au_pw); i++)
740 (void)printf("%02x ", na->au.au_pw[i]);
742 if (md5_authed && n+1 > lim
743 && na->a_type == ntohs(1)) {
745 MD5Update(&md5_ctx, (u_char *)&IMSG,
746 (char *)na-(char *)&IMSG
747 +RIP_AUTH_MD5_HASH_XTRA);
748 MD5Update(&md5_ctx, (u_char *)passwd,
749 RIP_AUTH_MD5_KEY_LEN);
750 MD5Final(hash, &md5_ctx);
751 (void)printf(" %s hash\n",
752 memcmp(hash, na->au.au_pw,
754 ? "WRONG" : "correct");
759 (void)sprintf(net_buf, "(af %#x) %d.%d.%d.%d",
761 (u_char)(n->n_dst >> 24),
762 (u_char)(n->n_dst >> 16),
763 (u_char)(n->n_dst >> 8),
767 (void)printf(" %-18s metric %2d %-10s",
768 net_buf, (int)ntohl(n->n_metric), name);
770 if (n->n_nhop != 0) {
771 in.s_addr = n->n_nhop;
775 hp = gethostbyaddr((char*)&in, sizeof(in),
777 (void)printf(" nhop=%-15s%s",
778 (hp != 0) ? hp->h_name : inet_ntoa(in),
779 (IMSG.rip_vers == RIPv1) ? " ?" : "");
782 (void)printf(" tag=%#x%s", n->n_tag,
783 (IMSG.rip_vers == RIPv1) ? " ?" : "");
789 /* Return the classical netmask for an IP address.
792 std_mask(u_int addr) /* in network order */
794 addr = ntohl(addr); /* was a host, not a network */
796 if (addr == 0) /* default route has mask 0 */
799 return IN_CLASSA_NET;
801 return IN_CLASSB_NET;
802 return IN_CLASSC_NET;
806 /* get a network number as a name or a number, with an optional "/xx"
809 static int /* 0=bad */
814 struct netent *nentp;
817 char hname[MAXHOSTNAMELEN+1];
821 /* Detect and separate "1.2.3.4/24"
823 if (0 != (mname = strrchr(name,'/'))) {
824 i = (int)(mname - name);
825 if (i > (int)sizeof(hname)-1) /* name too long */
827 memmove(hname, name, i);
833 nentp = getnetbyname(name);
835 in.s_addr = nentp->n_net;
836 } else if (inet_aton(name, &in) == 1) {
837 in.s_addr = ntohl(in.s_addr);
843 mask = std_mask(in.s_addr);
844 if ((~mask & in.s_addr) != 0)
847 mask = (u_int)strtoul(mname, &p, 0);
848 if (*p != '\0' || mask > 32)
850 mask = 0xffffffff << (32-mask);
853 rt->n_dst = htonl(in.s_addr);
854 rt->n_family = RIP_AF_INET;
855 rt->n_mask = htonl(mask);
860 /* strtok(), but honoring backslash
862 static int /* -1=bad */
863 parse_quote(char **linep,
884 if (c == '\\' && *pc != '\0') {
885 if ((c = *pc++) == 'n') {
887 } else if (c == 'r') {
889 } else if (c == 't') {
891 } else if (c == 'b') {
893 } else if (c >= '0' && c <= '7') {
895 if (*pc >= '0' && *pc <= '7') {
896 c = (c<<3)+(*pc++ - '0');
897 if (*pc >= '0' && *pc <= '7')
898 c = (c<<3)+(*pc++ - '0');
903 for (p = delims; *p != '\0'; ++p) {