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