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