1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1982, 1986, 1993
6*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
7*7c478bd9Sstevel@tonic-gate  *
8*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*7c478bd9Sstevel@tonic-gate  * are met:
11*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
12*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
13*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
14*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
15*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
16*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
17*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgment:
18*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
19*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
20*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
21*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
22*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
23*7c478bd9Sstevel@tonic-gate  *
24*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sbin/routed/rtquery/rtquery.c,v 1.14 2000/08/11 08:24:39
37*7c478bd9Sstevel@tonic-gate  * sheldonh Exp $
38*7c478bd9Sstevel@tonic-gate  * char copyright[] = "@(#) Copyright (c) 1982, 1986, 1993\n"
39*7c478bd9Sstevel@tonic-gate  * "The Regents of the University of California.  All rights reserved.\n";
40*7c478bd9Sstevel@tonic-gate  */
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "defs.h"
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/protosw.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
48*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
49*7c478bd9Sstevel@tonic-gate #define	RIPVERSION RIPv2
50*7c478bd9Sstevel@tonic-gate #include <protocols/routed.h>
51*7c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
52*7c478bd9Sstevel@tonic-gate #include <netdb.h>
53*7c478bd9Sstevel@tonic-gate #include <errno.h>
54*7c478bd9Sstevel@tonic-gate #include <unistd.h>
55*7c478bd9Sstevel@tonic-gate #include <stdio.h>
56*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
57*7c478bd9Sstevel@tonic-gate #include <string.h>
58*7c478bd9Sstevel@tonic-gate #include <md5.h>
59*7c478bd9Sstevel@tonic-gate #include <libintl.h>
60*7c478bd9Sstevel@tonic-gate #include <locale.h>
61*7c478bd9Sstevel@tonic-gate #include <net/if.h>
62*7c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #ident "$Revision: 1.12 $"
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define	WTIME	15		/* Time to wait for all responses */
67*7c478bd9Sstevel@tonic-gate #define	STIME	(250*1000)	/* usec to wait for another response */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate  * The size of the control buffer passed to recvmsg() used to receive
71*7c478bd9Sstevel@tonic-gate  * ancillary data.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate #define	CONTROL_BUFSIZE 1024
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static	const char *pgmname;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static	union {
78*7c478bd9Sstevel@tonic-gate 	struct rip rip;
79*7c478bd9Sstevel@tonic-gate 	char	packet[MAXPACKETSIZE+MAXPATHLEN];
80*7c478bd9Sstevel@tonic-gate } omsg_buf;
81*7c478bd9Sstevel@tonic-gate #define	OMSG omsg_buf.rip
82*7c478bd9Sstevel@tonic-gate static int omsg_len = sizeof (struct rip);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate static	union {
85*7c478bd9Sstevel@tonic-gate 	struct	rip rip;
86*7c478bd9Sstevel@tonic-gate 	char	packet[MAXPACKETSIZE+1024];
87*7c478bd9Sstevel@tonic-gate } imsg_buf;
88*7c478bd9Sstevel@tonic-gate #define	IMSG imsg_buf.rip
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate static	int	wtime = WTIME;
91*7c478bd9Sstevel@tonic-gate static	int	auth_type = RIP_AUTH_NONE;
92*7c478bd9Sstevel@tonic-gate char	passwd[RIP_AUTH_PW_LEN+1];
93*7c478bd9Sstevel@tonic-gate static	ulong_t	keyid;
94*7c478bd9Sstevel@tonic-gate static	boolean_t	ripv2 = _B_TRUE;		/* use RIP version 2 */
95*7c478bd9Sstevel@tonic-gate static	boolean_t	trace, not_trace;	/* send trace command or not */
96*7c478bd9Sstevel@tonic-gate static	boolean_t	nflag;			/* numbers, no names */
97*7c478bd9Sstevel@tonic-gate static	boolean_t	pflag;			/* play the `gated` game */
98*7c478bd9Sstevel@tonic-gate static	boolean_t	rflag;		/* 1=ask about a particular route */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate static	struct timeval sent;			/* when query sent */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate static char *default_argv[] = {"localhost", 0};
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate static void rip_input(struct sockaddr_in *, int, uint_t);
105*7c478bd9Sstevel@tonic-gate static int out(const char *, int);
106*7c478bd9Sstevel@tonic-gate static void trace_loop(char *argv[], int);
107*7c478bd9Sstevel@tonic-gate static void query_loop(char *argv[], int, int);
108*7c478bd9Sstevel@tonic-gate static uint_t incoming_interface(struct msghdr *);
109*7c478bd9Sstevel@tonic-gate static void usage(void);
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])113*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate #define	MAX_RCVBUF 127*1024
116*7c478bd9Sstevel@tonic-gate #define	MIN_RCVBUF  4*1024
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	int ch, bsize, soc;
119*7c478bd9Sstevel@tonic-gate 	char *p, *tmp_ptr, *options, *value, delim;
120*7c478bd9Sstevel@tonic-gate 	const char *result;
121*7c478bd9Sstevel@tonic-gate 	in_addr_t netaddr, netmask;
122*7c478bd9Sstevel@tonic-gate 	int on;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
125*7c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)   /* Should be defined by cc -D */
126*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEXT"
127*7c478bd9Sstevel@tonic-gate #endif	/* ! TEXT_DOMAIN */
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	OMSG.rip_nets[0].n_dst = RIP_DEFAULT;
132*7c478bd9Sstevel@tonic-gate 	OMSG.rip_nets[0].n_family = RIP_AF_UNSPEC;
133*7c478bd9Sstevel@tonic-gate 	OMSG.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if ((pgmname = argv[0]) == NULL)
136*7c478bd9Sstevel@tonic-gate 		pgmname = "rtquery";
137*7c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "np1w:r:t:a:")) != -1)
138*7c478bd9Sstevel@tonic-gate 		switch (ch) {
139*7c478bd9Sstevel@tonic-gate 		case 'n':
140*7c478bd9Sstevel@tonic-gate 			not_trace = _B_TRUE;
141*7c478bd9Sstevel@tonic-gate 			nflag = _B_TRUE;
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 		case 'p':
145*7c478bd9Sstevel@tonic-gate 			not_trace = _B_TRUE;
146*7c478bd9Sstevel@tonic-gate 			pflag = _B_TRUE;
147*7c478bd9Sstevel@tonic-gate 			break;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		case '1':
150*7c478bd9Sstevel@tonic-gate 			ripv2 = _B_FALSE;
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 		case 'w':
154*7c478bd9Sstevel@tonic-gate 			not_trace = _B_TRUE;
155*7c478bd9Sstevel@tonic-gate 			wtime = (int)strtoul(optarg, &p, 0);
156*7c478bd9Sstevel@tonic-gate 			if (*p != '\0' || wtime <= 0 || p == optarg)
157*7c478bd9Sstevel@tonic-gate 				usage();
158*7c478bd9Sstevel@tonic-gate 			break;
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 		case 'r':
161*7c478bd9Sstevel@tonic-gate 			not_trace = _B_TRUE;
162*7c478bd9Sstevel@tonic-gate 			if (rflag)
163*7c478bd9Sstevel@tonic-gate 				usage();
164*7c478bd9Sstevel@tonic-gate 			rflag = getnet(optarg, &netaddr, &netmask);
165*7c478bd9Sstevel@tonic-gate 			if (rflag) {
166*7c478bd9Sstevel@tonic-gate 				OMSG.rip_nets[0].n_dst = htonl(netaddr);
167*7c478bd9Sstevel@tonic-gate 				OMSG.rip_nets[0].n_family = RIP_AF_INET;
168*7c478bd9Sstevel@tonic-gate 				OMSG.rip_nets[0].n_mask = htonl(netmask);
169*7c478bd9Sstevel@tonic-gate 			} else {
170*7c478bd9Sstevel@tonic-gate 				struct hostent *hp = gethostbyname(optarg);
171*7c478bd9Sstevel@tonic-gate 				if (hp == NULL) {
172*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "%s: %s: %s\n",
173*7c478bd9Sstevel@tonic-gate 					    pgmname, optarg,
174*7c478bd9Sstevel@tonic-gate 					    hstrerror(h_errno));
175*7c478bd9Sstevel@tonic-gate 					exit(EXIT_FAILURE);
176*7c478bd9Sstevel@tonic-gate 				}
177*7c478bd9Sstevel@tonic-gate 				(void) memcpy(&OMSG.rip_nets[0].n_dst,
178*7c478bd9Sstevel@tonic-gate 				    hp->h_addr,
179*7c478bd9Sstevel@tonic-gate 				    sizeof (OMSG.rip_nets[0].n_dst));
180*7c478bd9Sstevel@tonic-gate 				OMSG.rip_nets[0].n_family = RIP_AF_INET;
181*7c478bd9Sstevel@tonic-gate 				OMSG.rip_nets[0].n_mask = INADDR_BROADCAST;
182*7c478bd9Sstevel@tonic-gate 				rflag = _B_TRUE;
183*7c478bd9Sstevel@tonic-gate 			}
184*7c478bd9Sstevel@tonic-gate 			break;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 		case 't':
187*7c478bd9Sstevel@tonic-gate 			trace = _B_TRUE;
188*7c478bd9Sstevel@tonic-gate 			options = optarg;
189*7c478bd9Sstevel@tonic-gate 			while (*options != '\0') {
190*7c478bd9Sstevel@tonic-gate 				/* messy complications to make -W -Wall happy */
191*7c478bd9Sstevel@tonic-gate 				static char on_str[] = "on";
192*7c478bd9Sstevel@tonic-gate 				static char more_str[] = "more";
193*7c478bd9Sstevel@tonic-gate 				static char off_str[] = "off";
194*7c478bd9Sstevel@tonic-gate 				static char dump_str[] = "dump";
195*7c478bd9Sstevel@tonic-gate 				static char *traceopts[] = {
196*7c478bd9Sstevel@tonic-gate #define	TRACE_ON	0
197*7c478bd9Sstevel@tonic-gate 					on_str,
198*7c478bd9Sstevel@tonic-gate #define	TRACE_MORE	1
199*7c478bd9Sstevel@tonic-gate 					more_str,
200*7c478bd9Sstevel@tonic-gate #define	TRACE_OFF	2
201*7c478bd9Sstevel@tonic-gate 					off_str,
202*7c478bd9Sstevel@tonic-gate #define	TRACE_DUMP	3
203*7c478bd9Sstevel@tonic-gate 					dump_str,
204*7c478bd9Sstevel@tonic-gate 					0
205*7c478bd9Sstevel@tonic-gate 				};
206*7c478bd9Sstevel@tonic-gate 				result = "";
207*7c478bd9Sstevel@tonic-gate 				switch (getsubopt(&options, traceopts,
208*7c478bd9Sstevel@tonic-gate 				    &value)) {
209*7c478bd9Sstevel@tonic-gate 				case TRACE_ON:
210*7c478bd9Sstevel@tonic-gate 					OMSG.rip_cmd = RIPCMD_TRACEON;
211*7c478bd9Sstevel@tonic-gate 					if (value == NULL ||
212*7c478bd9Sstevel@tonic-gate 					    strlen(value) > MAXPATHLEN)
213*7c478bd9Sstevel@tonic-gate 					    usage();
214*7c478bd9Sstevel@tonic-gate 					result = value;
215*7c478bd9Sstevel@tonic-gate 					break;
216*7c478bd9Sstevel@tonic-gate 				case TRACE_MORE:
217*7c478bd9Sstevel@tonic-gate 					if (value != NULL)
218*7c478bd9Sstevel@tonic-gate 					    usage();
219*7c478bd9Sstevel@tonic-gate 					OMSG.rip_cmd = RIPCMD_TRACEON;
220*7c478bd9Sstevel@tonic-gate 					break;
221*7c478bd9Sstevel@tonic-gate 				case TRACE_OFF:
222*7c478bd9Sstevel@tonic-gate 					if (value != NULL)
223*7c478bd9Sstevel@tonic-gate 					    usage();
224*7c478bd9Sstevel@tonic-gate 					OMSG.rip_cmd = RIPCMD_TRACEOFF;
225*7c478bd9Sstevel@tonic-gate 					break;
226*7c478bd9Sstevel@tonic-gate 				case TRACE_DUMP:
227*7c478bd9Sstevel@tonic-gate 					if (value != NULL)
228*7c478bd9Sstevel@tonic-gate 					    usage();
229*7c478bd9Sstevel@tonic-gate 					OMSG.rip_cmd = RIPCMD_TRACEON;
230*7c478bd9Sstevel@tonic-gate 					result = "dump/../table";
231*7c478bd9Sstevel@tonic-gate 					break;
232*7c478bd9Sstevel@tonic-gate 				default:
233*7c478bd9Sstevel@tonic-gate 					usage();
234*7c478bd9Sstevel@tonic-gate 				}
235*7c478bd9Sstevel@tonic-gate 				(void) strlcpy((char *)OMSG.rip_tracefile,
236*7c478bd9Sstevel@tonic-gate 				    result, MAXPATHLEN);
237*7c478bd9Sstevel@tonic-gate 				omsg_len += strlen(result) -
238*7c478bd9Sstevel@tonic-gate 				    sizeof (OMSG.ripun);
239*7c478bd9Sstevel@tonic-gate 			}
240*7c478bd9Sstevel@tonic-gate 			break;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 		case 'a':
243*7c478bd9Sstevel@tonic-gate 			not_trace = _B_TRUE;
244*7c478bd9Sstevel@tonic-gate 			p = strchr(optarg, '=');
245*7c478bd9Sstevel@tonic-gate 			if (p == NULL)
246*7c478bd9Sstevel@tonic-gate 				usage();
247*7c478bd9Sstevel@tonic-gate 			*p++ = '\0';
248*7c478bd9Sstevel@tonic-gate 			if (0 == strcasecmp("passwd", optarg))
249*7c478bd9Sstevel@tonic-gate 				auth_type = RIP_AUTH_PW;
250*7c478bd9Sstevel@tonic-gate 			else if (0 == strcasecmp("md5_passwd", optarg))
251*7c478bd9Sstevel@tonic-gate 				auth_type = RIP_AUTH_MD5;
252*7c478bd9Sstevel@tonic-gate 			else
253*7c478bd9Sstevel@tonic-gate 				usage();
254*7c478bd9Sstevel@tonic-gate 			if (0 > parse_quote(&p, "|", &delim,
255*7c478bd9Sstevel@tonic-gate 			    passwd, sizeof (passwd)))
256*7c478bd9Sstevel@tonic-gate 				usage();
257*7c478bd9Sstevel@tonic-gate 			if (auth_type == RIP_AUTH_MD5 &&
258*7c478bd9Sstevel@tonic-gate 			    delim == '|') {
259*7c478bd9Sstevel@tonic-gate 				tmp_ptr = p+1;
260*7c478bd9Sstevel@tonic-gate 				keyid = strtoul(p+1, &p, 0);
261*7c478bd9Sstevel@tonic-gate 				if (keyid > 255 || *p != '\0' ||
262*7c478bd9Sstevel@tonic-gate 				    p == tmp_ptr)
263*7c478bd9Sstevel@tonic-gate 					usage();
264*7c478bd9Sstevel@tonic-gate 			} else if (delim != '\0') {
265*7c478bd9Sstevel@tonic-gate 				usage();
266*7c478bd9Sstevel@tonic-gate 			}
267*7c478bd9Sstevel@tonic-gate 			break;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		default:
270*7c478bd9Sstevel@tonic-gate 			usage();
271*7c478bd9Sstevel@tonic-gate 	}
272*7c478bd9Sstevel@tonic-gate 	argv += optind;
273*7c478bd9Sstevel@tonic-gate 	argc -= optind;
274*7c478bd9Sstevel@tonic-gate 	if (not_trace && trace)
275*7c478bd9Sstevel@tonic-gate 		usage();
276*7c478bd9Sstevel@tonic-gate 	if (argc == 0) {
277*7c478bd9Sstevel@tonic-gate 		argc = 1;
278*7c478bd9Sstevel@tonic-gate 		argv = default_argv;
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	soc = socket(PF_INET, SOCK_DGRAM, 0);
282*7c478bd9Sstevel@tonic-gate 	if (soc < 0) {
283*7c478bd9Sstevel@tonic-gate 		perror("rtquery: socket");
284*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	on = 1;
288*7c478bd9Sstevel@tonic-gate 	if (setsockopt(soc, IPPROTO_IP, IP_RECVIF, &on, sizeof (on)))
289*7c478bd9Sstevel@tonic-gate 		perror("rtquery: setsockopt IP_RECVIF");
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	/* be prepared to receive a lot of routes */
292*7c478bd9Sstevel@tonic-gate 	for (bsize = MAX_RCVBUF; ; bsize -= 1024) {
293*7c478bd9Sstevel@tonic-gate 		if (setsockopt(soc, SOL_SOCKET, SO_RCVBUF,
294*7c478bd9Sstevel@tonic-gate 		    &bsize, sizeof (bsize)) == 0)
295*7c478bd9Sstevel@tonic-gate 			break;
296*7c478bd9Sstevel@tonic-gate 		if (bsize <= MIN_RCVBUF) {
297*7c478bd9Sstevel@tonic-gate 			perror("rtquery: setsockopt SO_RCVBUF");
298*7c478bd9Sstevel@tonic-gate 			break;
299*7c478bd9Sstevel@tonic-gate 		}
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 	if (trace)
303*7c478bd9Sstevel@tonic-gate 		trace_loop(argv, soc);
304*7c478bd9Sstevel@tonic-gate 	else
305*7c478bd9Sstevel@tonic-gate 		query_loop(argv, argc, soc);
306*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
307*7c478bd9Sstevel@tonic-gate 	return (0);
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate static void
usage(void)312*7c478bd9Sstevel@tonic-gate usage(void)
313*7c478bd9Sstevel@tonic-gate {
314*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
315*7c478bd9Sstevel@tonic-gate 	    gettext("usage:  %s [-np1] [-r tgt_rt] [-w wtime]"
316*7c478bd9Sstevel@tonic-gate 		" [-a type=passwd] [host1 ...]\n"),
317*7c478bd9Sstevel@tonic-gate 	    pgmname);
318*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
319*7c478bd9Sstevel@tonic-gate 	    gettext("\t%s -t {on=filename|more|off|dump} [host1 ...]\n"),
320*7c478bd9Sstevel@tonic-gate 	    pgmname);
321*7c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /* Tell the target hosts about tracing */
326*7c478bd9Sstevel@tonic-gate static void
trace_loop(char * argv[],int soc)327*7c478bd9Sstevel@tonic-gate trace_loop(char *argv[], int soc)
328*7c478bd9Sstevel@tonic-gate {
329*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in myaddr;
330*7c478bd9Sstevel@tonic-gate 	int res;
331*7c478bd9Sstevel@tonic-gate 	int optval = 1;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	if (ripv2) {
334*7c478bd9Sstevel@tonic-gate 		OMSG.rip_vers = RIPv2;
335*7c478bd9Sstevel@tonic-gate 	} else {
336*7c478bd9Sstevel@tonic-gate 		OMSG.rip_vers = RIPv1;
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	(void) memset(&myaddr, 0, sizeof (myaddr));
340*7c478bd9Sstevel@tonic-gate 	myaddr.sin_family = AF_INET;
341*7c478bd9Sstevel@tonic-gate 	if (setsockopt(soc, IPPROTO_UDP, UDP_ANONPRIVBIND,
342*7c478bd9Sstevel@tonic-gate 	    &optval, sizeof (optval)) < 0) {
343*7c478bd9Sstevel@tonic-gate 		perror("rtquery: setsockopt UDP_ANONPRIVBIND");
344*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
345*7c478bd9Sstevel@tonic-gate 	}
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	if (bind(soc, (struct sockaddr *)&myaddr, sizeof (myaddr)) < 0) {
348*7c478bd9Sstevel@tonic-gate 		perror("rtquery: bind");
349*7c478bd9Sstevel@tonic-gate 		exit(EXIT_FAILURE);
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	res = EXIT_FAILURE;
353*7c478bd9Sstevel@tonic-gate 	while (*argv != NULL) {
354*7c478bd9Sstevel@tonic-gate 		if (out(*argv++, soc) == 0)
355*7c478bd9Sstevel@tonic-gate 			res = EXIT_SUCCESS;
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 	exit(res);
358*7c478bd9Sstevel@tonic-gate }
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate /* Query all of the listed hosts */
362*7c478bd9Sstevel@tonic-gate static void
query_loop(char * argv[],int argc,int soc)363*7c478bd9Sstevel@tonic-gate query_loop(char *argv[], int argc, int soc)
364*7c478bd9Sstevel@tonic-gate {
365*7c478bd9Sstevel@tonic-gate #define	NA0 (OMSG.rip_auths[0])
366*7c478bd9Sstevel@tonic-gate #define	NA2 (OMSG.rip_auths[2])
367*7c478bd9Sstevel@tonic-gate 	struct seen {
368*7c478bd9Sstevel@tonic-gate 		struct seen *next;
369*7c478bd9Sstevel@tonic-gate 		struct in_addr addr;
370*7c478bd9Sstevel@tonic-gate 	} *seen, *sp;
371*7c478bd9Sstevel@tonic-gate 	int answered = 0;
372*7c478bd9Sstevel@tonic-gate 	int cc;
373*7c478bd9Sstevel@tonic-gate 	fd_set bits;
374*7c478bd9Sstevel@tonic-gate 	struct timeval now, delay;
375*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
376*7c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
377*7c478bd9Sstevel@tonic-gate 	struct msghdr msg;
378*7c478bd9Sstevel@tonic-gate 	uint_t	ifindex;
379*7c478bd9Sstevel@tonic-gate 	struct iovec iov;
380*7c478bd9Sstevel@tonic-gate 	uint8_t ancillary_data[CONTROL_BUFSIZE];
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 	OMSG.rip_cmd = (pflag) ? RIPCMD_POLL : RIPCMD_REQUEST;
384*7c478bd9Sstevel@tonic-gate 	if (ripv2) {
385*7c478bd9Sstevel@tonic-gate 		OMSG.rip_vers = RIPv2;
386*7c478bd9Sstevel@tonic-gate 		if (auth_type == RIP_AUTH_PW) {
387*7c478bd9Sstevel@tonic-gate 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
388*7c478bd9Sstevel@tonic-gate 			NA0.a_family = RIP_AF_AUTH;
389*7c478bd9Sstevel@tonic-gate 			NA0.a_type = RIP_AUTH_PW;
390*7c478bd9Sstevel@tonic-gate 			(void) memcpy(NA0.au.au_pw, passwd, RIP_AUTH_PW_LEN);
391*7c478bd9Sstevel@tonic-gate 			omsg_len += sizeof (OMSG.rip_nets[0]);
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 		} else if (auth_type == RIP_AUTH_MD5) {
394*7c478bd9Sstevel@tonic-gate 			OMSG.rip_nets[1] = OMSG.rip_nets[0];
395*7c478bd9Sstevel@tonic-gate 			NA0.a_family = RIP_AF_AUTH;
396*7c478bd9Sstevel@tonic-gate 			NA0.a_type = RIP_AUTH_MD5;
397*7c478bd9Sstevel@tonic-gate 			NA0.au.a_md5.md5_keyid = (int8_t)keyid;
398*7c478bd9Sstevel@tonic-gate 			NA0.au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
399*7c478bd9Sstevel@tonic-gate 			NA0.au.a_md5.md5_seqno = 0;
400*7c478bd9Sstevel@tonic-gate 			cc = (char *)&NA2-(char *)&OMSG;
401*7c478bd9Sstevel@tonic-gate 			NA0.au.a_md5.md5_pkt_len = htons(cc);
402*7c478bd9Sstevel@tonic-gate 			NA2.a_family = RIP_AF_AUTH;
403*7c478bd9Sstevel@tonic-gate 			NA2.a_type = RIP_AUTH_TRAILER;
404*7c478bd9Sstevel@tonic-gate 			MD5Init(&md5_ctx);
405*7c478bd9Sstevel@tonic-gate 			MD5Update(&md5_ctx, (uchar_t *)&OMSG, cc+4);
406*7c478bd9Sstevel@tonic-gate 			MD5Update(&md5_ctx,
407*7c478bd9Sstevel@tonic-gate 			    (uchar_t *)passwd, RIP_AUTH_MD5_LEN);
408*7c478bd9Sstevel@tonic-gate 			MD5Final(NA2.au.au_pw, &md5_ctx);
409*7c478bd9Sstevel@tonic-gate 			omsg_len += 2*sizeof (OMSG.rip_nets[0]);
410*7c478bd9Sstevel@tonic-gate 		}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	} else {
413*7c478bd9Sstevel@tonic-gate 		OMSG.rip_vers = RIPv1;
414*7c478bd9Sstevel@tonic-gate 		OMSG.rip_nets[0].n_mask = 0;
415*7c478bd9Sstevel@tonic-gate 	}
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	/* ask the first (valid) host */
418*7c478bd9Sstevel@tonic-gate 	seen = NULL;
419*7c478bd9Sstevel@tonic-gate 	while (0 > out(*argv++, soc)) {
420*7c478bd9Sstevel@tonic-gate 		if (*argv == NULL)
421*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
422*7c478bd9Sstevel@tonic-gate 		answered++;
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	iov.iov_base = &imsg_buf;
426*7c478bd9Sstevel@tonic-gate 	iov.iov_len = sizeof (imsg_buf);
427*7c478bd9Sstevel@tonic-gate 	msg.msg_iov = &iov;
428*7c478bd9Sstevel@tonic-gate 	msg.msg_iovlen = 1;
429*7c478bd9Sstevel@tonic-gate 	msg.msg_name = &from;
430*7c478bd9Sstevel@tonic-gate 	msg.msg_control = &ancillary_data;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	(void) FD_ZERO(&bits);
433*7c478bd9Sstevel@tonic-gate 	FD_SET(soc, &bits);
434*7c478bd9Sstevel@tonic-gate 	for (;;) {
435*7c478bd9Sstevel@tonic-gate 		delay.tv_sec = 0;
436*7c478bd9Sstevel@tonic-gate 		delay.tv_usec = STIME;
437*7c478bd9Sstevel@tonic-gate 		cc = select(soc+1, &bits, 0, 0, &delay);
438*7c478bd9Sstevel@tonic-gate 		if (cc > 0) {
439*7c478bd9Sstevel@tonic-gate 			msg.msg_namelen = sizeof (from);
440*7c478bd9Sstevel@tonic-gate 			msg.msg_controllen = sizeof (ancillary_data);
441*7c478bd9Sstevel@tonic-gate 			cc = recvmsg(soc, &msg, 0);
442*7c478bd9Sstevel@tonic-gate 			if (cc < 0) {
443*7c478bd9Sstevel@tonic-gate 				perror("rtquery: recvmsg");
444*7c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
445*7c478bd9Sstevel@tonic-gate 			}
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 			/* avoid looping on high traffic */
448*7c478bd9Sstevel@tonic-gate 			if (answered > argc + 200)
449*7c478bd9Sstevel@tonic-gate 				break;
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate 			/*
452*7c478bd9Sstevel@tonic-gate 			 * count the distinct responding hosts.
453*7c478bd9Sstevel@tonic-gate 			 * You cannot match responding hosts with
454*7c478bd9Sstevel@tonic-gate 			 * addresses to which queries were transmitted,
455*7c478bd9Sstevel@tonic-gate 			 * because a router might respond with a
456*7c478bd9Sstevel@tonic-gate 			 * different source address.
457*7c478bd9Sstevel@tonic-gate 			 */
458*7c478bd9Sstevel@tonic-gate 			for (sp = seen; sp != NULL; sp = sp->next) {
459*7c478bd9Sstevel@tonic-gate 				if (sp->addr.s_addr == from.sin_addr.s_addr)
460*7c478bd9Sstevel@tonic-gate 					break;
461*7c478bd9Sstevel@tonic-gate 			}
462*7c478bd9Sstevel@tonic-gate 			if (sp == NULL) {
463*7c478bd9Sstevel@tonic-gate 				sp = malloc(sizeof (*sp));
464*7c478bd9Sstevel@tonic-gate 				if (sp != NULL) {
465*7c478bd9Sstevel@tonic-gate 					sp->addr = from.sin_addr;
466*7c478bd9Sstevel@tonic-gate 					sp->next = seen;
467*7c478bd9Sstevel@tonic-gate 					seen = sp;
468*7c478bd9Sstevel@tonic-gate 				} else {
469*7c478bd9Sstevel@tonic-gate 					perror("rtquery: malloc");
470*7c478bd9Sstevel@tonic-gate 				}
471*7c478bd9Sstevel@tonic-gate 				answered++;
472*7c478bd9Sstevel@tonic-gate 			}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 			ifindex = incoming_interface(&msg);
475*7c478bd9Sstevel@tonic-gate 			rip_input(&from, cc, ifindex);
476*7c478bd9Sstevel@tonic-gate 			continue;
477*7c478bd9Sstevel@tonic-gate 		}
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 		if (cc < 0) {
480*7c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
481*7c478bd9Sstevel@tonic-gate 				continue;
482*7c478bd9Sstevel@tonic-gate 			perror("rtquery: select");
483*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
484*7c478bd9Sstevel@tonic-gate 		}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 		/*
487*7c478bd9Sstevel@tonic-gate 		 * After a pause in responses, probe another host.
488*7c478bd9Sstevel@tonic-gate 		 * This reduces the intermingling of answers.
489*7c478bd9Sstevel@tonic-gate 		 */
490*7c478bd9Sstevel@tonic-gate 		while (*argv != NULL && 0 > out(*argv++, soc))
491*7c478bd9Sstevel@tonic-gate 			answered++;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 		/*
494*7c478bd9Sstevel@tonic-gate 		 * continue until no more packets arrive
495*7c478bd9Sstevel@tonic-gate 		 * or we have heard from all hosts
496*7c478bd9Sstevel@tonic-gate 		 */
497*7c478bd9Sstevel@tonic-gate 		if (answered >= argc)
498*7c478bd9Sstevel@tonic-gate 			break;
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate 		/* or until we have waited a long time */
501*7c478bd9Sstevel@tonic-gate 		if (gettimeofday(&now, 0) < 0) {
502*7c478bd9Sstevel@tonic-gate 			perror("rtquery: gettimeofday");
503*7c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
504*7c478bd9Sstevel@tonic-gate 		}
505*7c478bd9Sstevel@tonic-gate 		if (sent.tv_sec + wtime <= now.tv_sec)
506*7c478bd9Sstevel@tonic-gate 			break;
507*7c478bd9Sstevel@tonic-gate 	}
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	/* fail if there was no answer */
510*7c478bd9Sstevel@tonic-gate 	exit(answered >= argc ? EXIT_SUCCESS : EXIT_FAILURE);
511*7c478bd9Sstevel@tonic-gate }
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate /* Send to one host */
515*7c478bd9Sstevel@tonic-gate static int
out(const char * host,int soc)516*7c478bd9Sstevel@tonic-gate out(const char *host, int soc)
517*7c478bd9Sstevel@tonic-gate {
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *res;
520*7c478bd9Sstevel@tonic-gate 	int ret;
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 	if (gettimeofday(&sent, 0) < 0) {
523*7c478bd9Sstevel@tonic-gate 		perror("rtquery: gettimeofday");
524*7c478bd9Sstevel@tonic-gate 		return (-1);
525*7c478bd9Sstevel@tonic-gate 	}
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate 	(void) memset(&hints, 0, sizeof (hints));
528*7c478bd9Sstevel@tonic-gate 	hints.ai_family = PF_INET;
529*7c478bd9Sstevel@tonic-gate 	hints.ai_socktype = SOCK_DGRAM;
530*7c478bd9Sstevel@tonic-gate 	if ((ret = getaddrinfo(host, "route", &hints, &res)) != 0) {
531*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: getaddrinfo: %s: %s\n", pgmname,
532*7c478bd9Sstevel@tonic-gate 		    host, gai_strerror(ret));
533*7c478bd9Sstevel@tonic-gate 		return (-1);
534*7c478bd9Sstevel@tonic-gate 	}
535*7c478bd9Sstevel@tonic-gate 
536*7c478bd9Sstevel@tonic-gate 	if (sendto(soc, &omsg_buf, omsg_len, 0, res->ai_addr,
537*7c478bd9Sstevel@tonic-gate 	    res->ai_addrlen) < 0) {
538*7c478bd9Sstevel@tonic-gate 		perror("rtquery: sendto");
539*7c478bd9Sstevel@tonic-gate 		return (-1);
540*7c478bd9Sstevel@tonic-gate 	}
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 	freeaddrinfo(res);
543*7c478bd9Sstevel@tonic-gate 	return (0);
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate /*
547*7c478bd9Sstevel@tonic-gate  * Handle an incoming RIP packet.
548*7c478bd9Sstevel@tonic-gate  */
549*7c478bd9Sstevel@tonic-gate static void
rip_input(struct sockaddr_in * from,int size,uint_t ifindex)550*7c478bd9Sstevel@tonic-gate rip_input(struct sockaddr_in *from, int size, uint_t ifindex)
551*7c478bd9Sstevel@tonic-gate {
552*7c478bd9Sstevel@tonic-gate 	struct netinfo *n, *lim;
553*7c478bd9Sstevel@tonic-gate 	struct in_addr in;
554*7c478bd9Sstevel@tonic-gate 	const char *name;
555*7c478bd9Sstevel@tonic-gate 	char net_buf[80];
556*7c478bd9Sstevel@tonic-gate 	uchar_t hash[RIP_AUTH_MD5_LEN];
557*7c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
558*7c478bd9Sstevel@tonic-gate 	uchar_t md5_authed = 0;
559*7c478bd9Sstevel@tonic-gate 	in_addr_t mask, dmask;
560*7c478bd9Sstevel@tonic-gate 	struct in_addr tmp_addr;
561*7c478bd9Sstevel@tonic-gate 	char *sp;
562*7c478bd9Sstevel@tonic-gate 	char  ifname[IF_NAMESIZE+1];
563*7c478bd9Sstevel@tonic-gate 	int i;
564*7c478bd9Sstevel@tonic-gate 	struct hostent *hp;
565*7c478bd9Sstevel@tonic-gate 	struct netent *np;
566*7c478bd9Sstevel@tonic-gate 	struct netauth *na;
567*7c478bd9Sstevel@tonic-gate 	char srcaddr[MAXHOSTNAMELEN + sizeof (" (123.123.123.123)") + 1];
568*7c478bd9Sstevel@tonic-gate 	char ifstring[IF_NAMESIZE + 3*sizeof (ifindex) + sizeof (" ()") + 1];
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 	if (!nflag && (hp = gethostbyaddr((char *)&from->sin_addr,
571*7c478bd9Sstevel@tonic-gate 	    sizeof (struct in_addr), AF_INET)) != NULL) {
572*7c478bd9Sstevel@tonic-gate 		(void) snprintf(srcaddr, sizeof (srcaddr), "%s (%s)",
573*7c478bd9Sstevel@tonic-gate 		    hp->h_name, inet_ntoa(from->sin_addr));
574*7c478bd9Sstevel@tonic-gate 	} else {
575*7c478bd9Sstevel@tonic-gate 		/* safe; cannot overflow destination */
576*7c478bd9Sstevel@tonic-gate 		(void) strcpy(srcaddr, inet_ntoa(from->sin_addr));
577*7c478bd9Sstevel@tonic-gate 	}
578*7c478bd9Sstevel@tonic-gate 	if (ifindex == 0) {
579*7c478bd9Sstevel@tonic-gate 		(void) printf("%s:", srcaddr);
580*7c478bd9Sstevel@tonic-gate 	} else {
581*7c478bd9Sstevel@tonic-gate 		if (if_indextoname(ifindex, ifname) != NULL)
582*7c478bd9Sstevel@tonic-gate 			(void) snprintf(ifstring, sizeof (ifstring), "%s (%d)",
583*7c478bd9Sstevel@tonic-gate 			    ifname, ifindex);
584*7c478bd9Sstevel@tonic-gate 		else
585*7c478bd9Sstevel@tonic-gate 			(void) snprintf(ifstring, sizeof (ifstring), "%d",
586*7c478bd9Sstevel@tonic-gate 			    ifindex);
587*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%1$s received on interface %2$s:"),
588*7c478bd9Sstevel@tonic-gate 		    srcaddr, ifstring);
589*7c478bd9Sstevel@tonic-gate 	}
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate 	if (IMSG.rip_cmd != RIPCMD_RESPONSE) {
592*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\n    unexpected response type %d\n"),
593*7c478bd9Sstevel@tonic-gate 		    IMSG.rip_cmd);
594*7c478bd9Sstevel@tonic-gate 		return;
595*7c478bd9Sstevel@tonic-gate 	}
596*7c478bd9Sstevel@tonic-gate 	(void) printf(gettext(" RIPv%1$d%2$s %3$d bytes\n"), IMSG.rip_vers,
597*7c478bd9Sstevel@tonic-gate 	    (IMSG.rip_vers != RIPv1 && IMSG.rip_vers != RIPv2) ? " ?" : "",
598*7c478bd9Sstevel@tonic-gate 	    size);
599*7c478bd9Sstevel@tonic-gate 	if (size > MAXPACKETSIZE) {
600*7c478bd9Sstevel@tonic-gate 		if (size > sizeof (imsg_buf) - sizeof (*n)) {
601*7c478bd9Sstevel@tonic-gate 			(void) printf(
602*7c478bd9Sstevel@tonic-gate 			    gettext("       at least %d bytes too long\n"),
603*7c478bd9Sstevel@tonic-gate 			    size-MAXPACKETSIZE);
604*7c478bd9Sstevel@tonic-gate 			size = sizeof (imsg_buf) - sizeof (*n);
605*7c478bd9Sstevel@tonic-gate 		} else {
606*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext("       %d bytes too long\n"),
607*7c478bd9Sstevel@tonic-gate 			    size-MAXPACKETSIZE);
608*7c478bd9Sstevel@tonic-gate 		}
609*7c478bd9Sstevel@tonic-gate 	} else if (size%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
610*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("    response of bad length=%d\n"), size);
611*7c478bd9Sstevel@tonic-gate 	}
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 	n = IMSG.rip_nets;
614*7c478bd9Sstevel@tonic-gate 	lim = n + (size - 4) / sizeof (struct netinfo);
615*7c478bd9Sstevel@tonic-gate 	for (; n < lim; n++) {
616*7c478bd9Sstevel@tonic-gate 		name = "";
617*7c478bd9Sstevel@tonic-gate 		if (n->n_family == RIP_AF_INET) {
618*7c478bd9Sstevel@tonic-gate 			in.s_addr = n->n_dst;
619*7c478bd9Sstevel@tonic-gate 			(void) strlcpy(net_buf, inet_ntoa(in),
620*7c478bd9Sstevel@tonic-gate 			    sizeof (net_buf));
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 			tmp_addr.s_addr = (n->n_mask);
623*7c478bd9Sstevel@tonic-gate 			mask = ntohl(n->n_mask);
624*7c478bd9Sstevel@tonic-gate 			dmask = mask & -mask;
625*7c478bd9Sstevel@tonic-gate 			if (mask != 0) {
626*7c478bd9Sstevel@tonic-gate 				sp = &net_buf[strlen(net_buf)];
627*7c478bd9Sstevel@tonic-gate 				if (IMSG.rip_vers == RIPv1) {
628*7c478bd9Sstevel@tonic-gate 					(void) snprintf(sp,
629*7c478bd9Sstevel@tonic-gate 					    (sizeof (net_buf) -
630*7c478bd9Sstevel@tonic-gate 					    strlen(net_buf)),
631*7c478bd9Sstevel@tonic-gate 					    gettext(" mask=%s ? "),
632*7c478bd9Sstevel@tonic-gate 					    inet_ntoa(tmp_addr));
633*7c478bd9Sstevel@tonic-gate 					mask = 0;
634*7c478bd9Sstevel@tonic-gate 				} else if (mask + dmask == 0) {
635*7c478bd9Sstevel@tonic-gate 					i = ffs(mask) - 1;
636*7c478bd9Sstevel@tonic-gate 					(void) snprintf(sp,
637*7c478bd9Sstevel@tonic-gate 					    (sizeof (net_buf) -
638*7c478bd9Sstevel@tonic-gate 					    strlen(net_buf)), "/%d", 32-i);
639*7c478bd9Sstevel@tonic-gate 				} else {
640*7c478bd9Sstevel@tonic-gate 					(void) snprintf(sp,
641*7c478bd9Sstevel@tonic-gate 					    (sizeof (net_buf) -
642*7c478bd9Sstevel@tonic-gate 						strlen(net_buf)),
643*7c478bd9Sstevel@tonic-gate 					    gettext(" (mask %s)"),
644*7c478bd9Sstevel@tonic-gate 					    inet_ntoa(tmp_addr));
645*7c478bd9Sstevel@tonic-gate 				}
646*7c478bd9Sstevel@tonic-gate 			}
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 			if (!nflag) {
649*7c478bd9Sstevel@tonic-gate 				if (mask == 0) {
650*7c478bd9Sstevel@tonic-gate 					mask = std_mask(in.s_addr);
651*7c478bd9Sstevel@tonic-gate 					if ((ntohl(in.s_addr) & ~mask) != 0)
652*7c478bd9Sstevel@tonic-gate 						mask = 0;
653*7c478bd9Sstevel@tonic-gate 				}
654*7c478bd9Sstevel@tonic-gate 				/*
655*7c478bd9Sstevel@tonic-gate 				 * Without a netmask, do not worry about
656*7c478bd9Sstevel@tonic-gate 				 * whether the destination is a host or a
657*7c478bd9Sstevel@tonic-gate 				 * network. Try both and use the first name
658*7c478bd9Sstevel@tonic-gate 				 * we get.
659*7c478bd9Sstevel@tonic-gate 				 *
660*7c478bd9Sstevel@tonic-gate 				 * If we have a netmask we can make a
661*7c478bd9Sstevel@tonic-gate 				 * good guess.
662*7c478bd9Sstevel@tonic-gate 				 */
663*7c478bd9Sstevel@tonic-gate 				if ((in.s_addr & ~mask) == 0) {
664*7c478bd9Sstevel@tonic-gate 					np = getnetbyaddr((long)in.s_addr,
665*7c478bd9Sstevel@tonic-gate 					    AF_INET);
666*7c478bd9Sstevel@tonic-gate 					if (np != NULL)
667*7c478bd9Sstevel@tonic-gate 						name = np->n_name;
668*7c478bd9Sstevel@tonic-gate 					else if (in.s_addr == 0)
669*7c478bd9Sstevel@tonic-gate 						name = "default";
670*7c478bd9Sstevel@tonic-gate 				}
671*7c478bd9Sstevel@tonic-gate 				if (name[0] == '\0' &&
672*7c478bd9Sstevel@tonic-gate 				    ((in.s_addr & ~mask) != 0 ||
673*7c478bd9Sstevel@tonic-gate 				    mask == 0xffffffff)) {
674*7c478bd9Sstevel@tonic-gate 					hp = gethostbyaddr((char *)&in,
675*7c478bd9Sstevel@tonic-gate 					    sizeof (in), AF_INET);
676*7c478bd9Sstevel@tonic-gate 					if (hp != NULL)
677*7c478bd9Sstevel@tonic-gate 						name = hp->h_name;
678*7c478bd9Sstevel@tonic-gate 				}
679*7c478bd9Sstevel@tonic-gate 			}
680*7c478bd9Sstevel@tonic-gate 
681*7c478bd9Sstevel@tonic-gate 		} else if (n->n_family == RIP_AF_AUTH) {
682*7c478bd9Sstevel@tonic-gate 			na = (struct netauth *)n;
683*7c478bd9Sstevel@tonic-gate 			if (na->a_type == RIP_AUTH_PW &&
684*7c478bd9Sstevel@tonic-gate 			    n == IMSG.rip_nets) {
685*7c478bd9Sstevel@tonic-gate 				(void) printf(
686*7c478bd9Sstevel@tonic-gate 				    gettext("  Password Authentication:"
687*7c478bd9Sstevel@tonic-gate 				    " \"%s\"\n"),
688*7c478bd9Sstevel@tonic-gate 				    qstring(na->au.au_pw,
689*7c478bd9Sstevel@tonic-gate 				    RIP_AUTH_PW_LEN));
690*7c478bd9Sstevel@tonic-gate 				continue;
691*7c478bd9Sstevel@tonic-gate 			}
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 			if (na->a_type == RIP_AUTH_MD5 &&
694*7c478bd9Sstevel@tonic-gate 			    n == IMSG.rip_nets) {
695*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("  MD5 Auth"
696*7c478bd9Sstevel@tonic-gate 				    " len=%1$d KeyID=%2$d"
697*7c478bd9Sstevel@tonic-gate 				    " auth_len=%3$d"
698*7c478bd9Sstevel@tonic-gate 				    " seqno=%4$#x"
699*7c478bd9Sstevel@tonic-gate 				    " rsvd=%5$#x,%6$#x\n"),
700*7c478bd9Sstevel@tonic-gate 				    ntohs(na->au.a_md5.md5_pkt_len),
701*7c478bd9Sstevel@tonic-gate 				    na->au.a_md5.md5_keyid,
702*7c478bd9Sstevel@tonic-gate 				    na->au.a_md5.md5_auth_len,
703*7c478bd9Sstevel@tonic-gate 				    (int)ntohl(na->au.a_md5.md5_seqno),
704*7c478bd9Sstevel@tonic-gate 				    na->au.a_md5.rsvd[0],
705*7c478bd9Sstevel@tonic-gate 				    na->au.a_md5.rsvd[1]);
706*7c478bd9Sstevel@tonic-gate 				md5_authed = 1;
707*7c478bd9Sstevel@tonic-gate 				continue;
708*7c478bd9Sstevel@tonic-gate 			}
709*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext("  Authentication type %d: "),
710*7c478bd9Sstevel@tonic-gate 			    ntohs(na->a_type));
711*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < sizeof (na->au.au_pw); i++)
712*7c478bd9Sstevel@tonic-gate 				(void) printf("%02x ",
713*7c478bd9Sstevel@tonic-gate 				    na->au.au_pw[i]);
714*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
715*7c478bd9Sstevel@tonic-gate 			if (md5_authed && n+1 > lim &&
716*7c478bd9Sstevel@tonic-gate 			    na->a_type == RIP_AUTH_TRAILER) {
717*7c478bd9Sstevel@tonic-gate 				MD5Init(&md5_ctx);
718*7c478bd9Sstevel@tonic-gate 				MD5Update(&md5_ctx, (uchar_t *)&IMSG,
719*7c478bd9Sstevel@tonic-gate 				    (char *)na-(char *)&IMSG);
720*7c478bd9Sstevel@tonic-gate 				MD5Update(&md5_ctx,
721*7c478bd9Sstevel@tonic-gate 				    (uchar_t *)passwd, RIP_AUTH_MD5_LEN);
722*7c478bd9Sstevel@tonic-gate 				MD5Final(hash, &md5_ctx);
723*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("    %s hash\n"),
724*7c478bd9Sstevel@tonic-gate 				    memcmp(hash, na->au.au_pw, sizeof (hash)) ?
725*7c478bd9Sstevel@tonic-gate 				    gettext("WRONG") : gettext("correct"));
726*7c478bd9Sstevel@tonic-gate 			} else if (md5_authed && n+1 > lim &&
727*7c478bd9Sstevel@tonic-gate 			    na->a_type != RIP_AUTH_TRAILER) {
728*7c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Error -"
729*7c478bd9Sstevel@tonic-gate 				"authentication entry missing hash\n"));
730*7c478bd9Sstevel@tonic-gate 			}
731*7c478bd9Sstevel@tonic-gate 			continue;
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 		} else {
734*7c478bd9Sstevel@tonic-gate 			tmp_addr.s_addr = n->n_dst;
735*7c478bd9Sstevel@tonic-gate 			(void) snprintf(net_buf, sizeof (net_buf),
736*7c478bd9Sstevel@tonic-gate 			    gettext("(address family %1$u) %2$s"),
737*7c478bd9Sstevel@tonic-gate 			    ntohs(n->n_family), inet_ntoa(tmp_addr));
738*7c478bd9Sstevel@tonic-gate 		}
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate 		(void) printf(gettext("  %1$-18s metric %2$2lu %3$-10s"),
741*7c478bd9Sstevel@tonic-gate 		    net_buf, ntohl(n->n_metric), name);
742*7c478bd9Sstevel@tonic-gate 
743*7c478bd9Sstevel@tonic-gate 		if (n->n_nhop != 0) {
744*7c478bd9Sstevel@tonic-gate 			in.s_addr = n->n_nhop;
745*7c478bd9Sstevel@tonic-gate 			if (nflag)
746*7c478bd9Sstevel@tonic-gate 				hp = NULL;
747*7c478bd9Sstevel@tonic-gate 			else
748*7c478bd9Sstevel@tonic-gate 				hp = gethostbyaddr((char *)&in, sizeof (in),
749*7c478bd9Sstevel@tonic-gate 				    AF_INET);
750*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" nhop=%1$-15s%2$s"),
751*7c478bd9Sstevel@tonic-gate 			    (hp != NULL) ? hp->h_name : inet_ntoa(in),
752*7c478bd9Sstevel@tonic-gate 			    (IMSG.rip_vers == RIPv1) ? " ?" : "");
753*7c478bd9Sstevel@tonic-gate 		}
754*7c478bd9Sstevel@tonic-gate 		if (n->n_tag != 0)
755*7c478bd9Sstevel@tonic-gate 			(void) printf(gettext(" tag=%1$#x%2$s"), n->n_tag,
756*7c478bd9Sstevel@tonic-gate 			    (IMSG.rip_vers == RIPv1) ? " ?" : "");
757*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
758*7c478bd9Sstevel@tonic-gate 	}
759*7c478bd9Sstevel@tonic-gate }
760*7c478bd9Sstevel@tonic-gate 
761*7c478bd9Sstevel@tonic-gate /*
762*7c478bd9Sstevel@tonic-gate  * Find the interface which received the given message.
763*7c478bd9Sstevel@tonic-gate  */
764*7c478bd9Sstevel@tonic-gate static uint_t
incoming_interface(struct msghdr * msg)765*7c478bd9Sstevel@tonic-gate incoming_interface(struct msghdr *msg)
766*7c478bd9Sstevel@tonic-gate {
767*7c478bd9Sstevel@tonic-gate 	void *opt;
768*7c478bd9Sstevel@tonic-gate 	uint_t ifindex = 0;
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	/*
771*7c478bd9Sstevel@tonic-gate 	 * Determine which physical interface this packet was received on by
772*7c478bd9Sstevel@tonic-gate 	 * processing the message's ancillary data to find the
773*7c478bd9Sstevel@tonic-gate 	 * IP_RECVIF option we requested.
774*7c478bd9Sstevel@tonic-gate 	 */
775*7c478bd9Sstevel@tonic-gate 	if ((opt = find_ancillary(msg, IP_RECVIF)) == NULL)
776*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
777*7c478bd9Sstevel@tonic-gate 		    gettext("%s: unable to retrieve input interface\n"),
778*7c478bd9Sstevel@tonic-gate 		    pgmname);
779*7c478bd9Sstevel@tonic-gate 	else
780*7c478bd9Sstevel@tonic-gate 		ifindex = *(uint_t *)opt;
781*7c478bd9Sstevel@tonic-gate 	return (ifindex);
782*7c478bd9Sstevel@tonic-gate }
783