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