xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.sbin/in.routed/input.c (revision d6b3210d4ed626e58d72b0c439ecba06617f963e)
17c478bd9Sstevel@tonic-gate /*
2*d6b3210dSsowmini  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1988, 1993
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
97c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
107c478bd9Sstevel@tonic-gate  * are met:
117c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
127c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
137c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
147c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
157c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
167c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
177c478bd9Sstevel@tonic-gate  *    must display the following acknowledgment:
187c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
197c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
207c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
217c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
227c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
257c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
267c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
287c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
297c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
307c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
317c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
337c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
347c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sbin/routed/input.c,v 1.9 2001/06/06 20:52:30 phk Exp $
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "defs.h"
427c478bd9Sstevel@tonic-gate #include <md5.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * The size of the control buffer passed to recvmsg() used to receive
467c478bd9Sstevel@tonic-gate  * ancillary data.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate #define	CONTROL_BUFSIZE	1024
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static void input(struct sockaddr_in *, struct interface *, struct rip *, int);
517c478bd9Sstevel@tonic-gate static boolean_t ck_passwd(struct interface *, struct rip *, uint8_t *,
527c478bd9Sstevel@tonic-gate     in_addr_t, struct msg_limit *);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Find the interface which received the given message.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate struct interface *
597c478bd9Sstevel@tonic-gate receiving_interface(struct msghdr *msg, boolean_t findremote)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	struct interface *ifp, *ifp1, *ifp2;
627c478bd9Sstevel@tonic-gate 	struct sockaddr_in *from;
637c478bd9Sstevel@tonic-gate 	void *opt;
647c478bd9Sstevel@tonic-gate 	uint_t ifindex;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	from = (struct sockaddr_in *)msg->msg_name;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* First see if this packet came from a remote gateway. */
697c478bd9Sstevel@tonic-gate 	if (findremote && ((ifp = findremoteif(from->sin_addr.s_addr)) != NULL))
707c478bd9Sstevel@tonic-gate 		return (ifp);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/*
737c478bd9Sstevel@tonic-gate 	 * It did not come from a remote gateway.  Determine which
747c478bd9Sstevel@tonic-gate 	 * physical interface this packet was received on by
757c478bd9Sstevel@tonic-gate 	 * processing the message's ancillary data to find the
767c478bd9Sstevel@tonic-gate 	 * IP_RECVIF option we requested.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	if ((opt = find_ancillary(msg, IP_RECVIF)) == NULL) {
797c478bd9Sstevel@tonic-gate 		msglog("unable to retrieve IP_RECVIF");
807c478bd9Sstevel@tonic-gate 	} else {
817c478bd9Sstevel@tonic-gate 		ifindex = *(uint_t *)opt;
827c478bd9Sstevel@tonic-gate 		if ((ifp = ifwithindex(ifindex, _B_TRUE)) != NULL) {
837c478bd9Sstevel@tonic-gate 			/* Find the best match of the aliases */
847c478bd9Sstevel@tonic-gate 			ifp2 = NULL;
857c478bd9Sstevel@tonic-gate 			for (ifp1 = ifp; ifp1 != NULL;
867c478bd9Sstevel@tonic-gate 			    ifp1 = ifp1->int_ilist.hl_next) {
877c478bd9Sstevel@tonic-gate 				if (ifp1->int_addr == from->sin_addr.s_addr)
887c478bd9Sstevel@tonic-gate 					return (ifp1);
897c478bd9Sstevel@tonic-gate 				if ((ifp2 == NULL ||
907c478bd9Sstevel@tonic-gate 					(ifp2->int_state & IS_ALIAS)) &&
917c478bd9Sstevel@tonic-gate 				    on_net(from->sin_addr.s_addr, ifp1->int_net,
927c478bd9Sstevel@tonic-gate 					ifp1->int_mask))
937c478bd9Sstevel@tonic-gate 					ifp2 = ifp1;
947c478bd9Sstevel@tonic-gate 			}
957c478bd9Sstevel@tonic-gate 			if (ifp2 != NULL)
967c478bd9Sstevel@tonic-gate 				ifp = ifp2;
977c478bd9Sstevel@tonic-gate 			return (ifp);
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * As a last resort (for some reason, ip didn't give us the
1037c478bd9Sstevel@tonic-gate 	 * IP_RECVIF index we requested), try to deduce the receiving
1047c478bd9Sstevel@tonic-gate 	 * interface based on the source address of the packet.
1057c478bd9Sstevel@tonic-gate 	 */
106e1fa7f4dSsowmini 	ifp = iflookup(from->sin_addr.s_addr);
107e1fa7f4dSsowmini 	if (ifp != NULL && ifp->int_phys != NULL) {
108e1fa7f4dSsowmini 		ifp = ifwithname(ifp->int_phys->phyi_name);
109e1fa7f4dSsowmini 	}
110e1fa7f4dSsowmini 	return (ifp);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Process RIP input on rip_sock.  Returns 0 for success, -1 for failure.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate int
1177c478bd9Sstevel@tonic-gate read_rip()
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	struct sockaddr_in from;
1207c478bd9Sstevel@tonic-gate 	struct interface *ifp;
1217c478bd9Sstevel@tonic-gate 	int cc;
1227c478bd9Sstevel@tonic-gate 	union pkt_buf inbuf;
1237c478bd9Sstevel@tonic-gate 	struct msghdr msg;
1247c478bd9Sstevel@tonic-gate 	struct iovec iov;
1257c478bd9Sstevel@tonic-gate 	uint8_t ancillary_data[CONTROL_BUFSIZE];
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	iov.iov_base = &inbuf;
1287c478bd9Sstevel@tonic-gate 	iov.iov_len = sizeof (inbuf);
1297c478bd9Sstevel@tonic-gate 	msg.msg_iov = &iov;
1307c478bd9Sstevel@tonic-gate 	msg.msg_iovlen = 1;
1317c478bd9Sstevel@tonic-gate 	msg.msg_name = &from;
1327c478bd9Sstevel@tonic-gate 	msg.msg_control = &ancillary_data;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	for (;;) {
1357c478bd9Sstevel@tonic-gate 		msg.msg_namelen = sizeof (from);
1367c478bd9Sstevel@tonic-gate 		msg.msg_controllen = sizeof (ancillary_data);
1377c478bd9Sstevel@tonic-gate 		cc = recvmsg(rip_sock, &msg, 0);
1387c478bd9Sstevel@tonic-gate 		if (cc == 0)
1397c478bd9Sstevel@tonic-gate 			return (-1);
1407c478bd9Sstevel@tonic-gate 		if (cc < 0) {
1417c478bd9Sstevel@tonic-gate 			if (errno == EWOULDBLOCK || errno == EINTR)
1427c478bd9Sstevel@tonic-gate 				return (0);
1437c478bd9Sstevel@tonic-gate 			LOGERR("recvmsg(rip_sock)");
1447c478bd9Sstevel@tonic-gate 			return (-1);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * ifp is the interface via which the packet arrived.
1497c478bd9Sstevel@tonic-gate 		 */
1507c478bd9Sstevel@tonic-gate 		ifp = receiving_interface(&msg, _B_TRUE);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		input(&from, ifp, &inbuf.rip, cc);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /* Process a RIP packet */
1587c478bd9Sstevel@tonic-gate static void
1597c478bd9Sstevel@tonic-gate input(struct sockaddr_in *from,		/* received from this IP address */
1607c478bd9Sstevel@tonic-gate     struct interface *ifp,		/* interface of incoming socket */
1617c478bd9Sstevel@tonic-gate     struct rip *rip,
1627c478bd9Sstevel@tonic-gate     int cc)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate #define	FROM_NADDR from->sin_addr.s_addr
1657c478bd9Sstevel@tonic-gate 	static struct msg_limit use_auth, bad_len, bad_mask;
1667c478bd9Sstevel@tonic-gate 	static struct msg_limit unk_router, bad_router, bad_nhop;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
1697c478bd9Sstevel@tonic-gate 	struct rt_spare new;
1707c478bd9Sstevel@tonic-gate 	struct netinfo *n, *lim;
1717c478bd9Sstevel@tonic-gate 	struct interface *ifp1;
1727c478bd9Sstevel@tonic-gate 	in_addr_t gate, mask, v1_mask, dst, ddst_h = 0;
1737c478bd9Sstevel@tonic-gate 	struct auth *ap;
1747c478bd9Sstevel@tonic-gate 	struct tgate *tg = NULL;
1757c478bd9Sstevel@tonic-gate 	struct tgate_net *tn;
1767c478bd9Sstevel@tonic-gate 	int i, j;
1777c478bd9Sstevel@tonic-gate 	boolean_t poll_answer = _B_FALSE; /* Set to _B_TRUE if RIPCMD_POLL */
1787c478bd9Sstevel@tonic-gate 	uint16_t rt_state = 0;	/* Extra route state to pass to input_route() */
1797c478bd9Sstevel@tonic-gate 	uint8_t metric;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	(void) memset(&new, 0, sizeof (new));
1827c478bd9Sstevel@tonic-gate 	/* Notice when we hear from a remote gateway */
1837c478bd9Sstevel@tonic-gate 	if (ifp != NULL && (ifp->int_state & IS_REMOTE))
1847c478bd9Sstevel@tonic-gate 		ifp->int_act_time = now.tv_sec;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	trace_rip("Recv", "from", from, ifp, rip, cc);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (ifp != NULL && (ifp->int_if_flags & IFF_NORTEXCH)) {
1897c478bd9Sstevel@tonic-gate 		trace_misc("discard RIP packet received over %s (IFF_NORTEXCH)",
1907c478bd9Sstevel@tonic-gate 		    ifp->int_name);
1917c478bd9Sstevel@tonic-gate 		return;
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	gate = ntohl(FROM_NADDR);
1957c478bd9Sstevel@tonic-gate 	if (IN_EXPERIMENTAL(gate) || (gate >> IN_CLASSA_NSHIFT) == 0) {
1967c478bd9Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR, "source address %s unusable",
1977c478bd9Sstevel@tonic-gate 		    naddr_ntoa(FROM_NADDR));
1987c478bd9Sstevel@tonic-gate 		return;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (rip->rip_vers == 0) {
2027c478bd9Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
2037c478bd9Sstevel@tonic-gate 		    "RIP version 0, cmd %d, packet received from %s",
2047c478bd9Sstevel@tonic-gate 		    rip->rip_cmd, naddr_ntoa(FROM_NADDR));
2057c478bd9Sstevel@tonic-gate 		return;
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (rip->rip_vers > RIPv2) {
2097c478bd9Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
2107c478bd9Sstevel@tonic-gate 		    "Treating RIP version %d packet received from %s as "
2117c478bd9Sstevel@tonic-gate 		    "version %d", rip->rip_vers, naddr_ntoa(FROM_NADDR),
2127c478bd9Sstevel@tonic-gate 		    RIPv2);
2137c478bd9Sstevel@tonic-gate 		rip->rip_vers = RIPv2;
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if (cc > (int)OVER_MAXPACKETSIZE) {
2177c478bd9Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
2187c478bd9Sstevel@tonic-gate 		    "packet at least %d bytes too long received from %s",
2197c478bd9Sstevel@tonic-gate 		    cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	n = rip->rip_nets;
2237c478bd9Sstevel@tonic-gate 	lim = n + (cc - 4) / sizeof (struct netinfo);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/*
2267c478bd9Sstevel@tonic-gate 	 * Notice authentication.
2277c478bd9Sstevel@tonic-gate 	 * As required by section 5.2 of RFC 2453, discard authenticated
2287c478bd9Sstevel@tonic-gate 	 * RIPv2 messages, but only if configured for that silliness.
2297c478bd9Sstevel@tonic-gate 	 *
2307c478bd9Sstevel@tonic-gate 	 * RIPv2 authentication is lame.  Why authenticate queries?
2317c478bd9Sstevel@tonic-gate 	 * Why should a RIPv2 implementation with authentication disabled
2327c478bd9Sstevel@tonic-gate 	 * not be able to listen to RIPv2 packets with authentication, while
2337c478bd9Sstevel@tonic-gate 	 * RIPv1 systems will listen?  Crazy!
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (!auth_ok && rip->rip_vers == RIPv2 && n < lim &&
2367c478bd9Sstevel@tonic-gate 	    n->n_family == RIP_AF_AUTH) {
2377c478bd9Sstevel@tonic-gate 		msglim(&use_auth, FROM_NADDR,
2387c478bd9Sstevel@tonic-gate 		    "RIPv2 message with authentication from %s discarded",
2397c478bd9Sstevel@tonic-gate 		    naddr_ntoa(FROM_NADDR));
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	switch (rip->rip_cmd) {
2447c478bd9Sstevel@tonic-gate 	case RIPCMD_POLL:
2457c478bd9Sstevel@tonic-gate 		/*
2467c478bd9Sstevel@tonic-gate 		 * Similar to RIPCMD_REQUEST, this command is used to
2477c478bd9Sstevel@tonic-gate 		 * request either a full-table or a set of entries.  Both
2487c478bd9Sstevel@tonic-gate 		 * silent processes and routers can respond to this
2497c478bd9Sstevel@tonic-gate 		 * command.
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		poll_answer = _B_TRUE;
2527c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
2537c478bd9Sstevel@tonic-gate 	case RIPCMD_REQUEST:
2547c478bd9Sstevel@tonic-gate 		/* Are we talking to ourself or a remote gateway? */
2557c478bd9Sstevel@tonic-gate 		ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
2567c478bd9Sstevel@tonic-gate 		if (ifp1 != NULL) {
2577c478bd9Sstevel@tonic-gate 			if (ifp1->int_state & IS_REMOTE) {
2587c478bd9Sstevel@tonic-gate 				/* remote gateway */
2597c478bd9Sstevel@tonic-gate 				ifp = ifp1;
2607c478bd9Sstevel@tonic-gate 				if (check_remote(ifp)) {
2617c478bd9Sstevel@tonic-gate 					ifp->int_act_time = now.tv_sec;
2627c478bd9Sstevel@tonic-gate 					if_ok(ifp, "remote ", _B_FALSE);
2637c478bd9Sstevel@tonic-gate 				}
2647c478bd9Sstevel@tonic-gate 			} else if (from->sin_port == htons(RIP_PORT)) {
2657c478bd9Sstevel@tonic-gate 				trace_pkt("    discard our own RIP request");
2667c478bd9Sstevel@tonic-gate 				return;
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/* did the request come from a router? */
2717c478bd9Sstevel@tonic-gate 		if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
2727c478bd9Sstevel@tonic-gate 			/*
2737c478bd9Sstevel@tonic-gate 			 * yes, ignore the request if RIP is off so that
2747c478bd9Sstevel@tonic-gate 			 * the router does not depend on us.
2757c478bd9Sstevel@tonic-gate 			 */
2767c478bd9Sstevel@tonic-gate 			if (ripout_interfaces == 0 ||
2777c478bd9Sstevel@tonic-gate 			    (ifp != NULL && (IS_RIP_OUT_OFF(ifp->int_state) ||
2787c478bd9Sstevel@tonic-gate 			    !IS_IFF_ROUTING(ifp->int_if_flags)))) {
2797c478bd9Sstevel@tonic-gate 				trace_pkt("    discard request while RIP off");
2807c478bd9Sstevel@tonic-gate 				return;
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		/*
2857c478bd9Sstevel@tonic-gate 		 * According to RFC 2453 section 5.2, we should ignore
2867c478bd9Sstevel@tonic-gate 		 * unauthenticated queries when authentication is
2877c478bd9Sstevel@tonic-gate 		 * configured.  That is too silly to bother with.  Sheesh!
2887c478bd9Sstevel@tonic-gate 		 * Are forwarding tables supposed to be secret even though
2897c478bd9Sstevel@tonic-gate 		 * a bad guy can infer them with test traffic?  RIP is
2907c478bd9Sstevel@tonic-gate 		 * still the most common router-discovery protocol, so
2917c478bd9Sstevel@tonic-gate 		 * hosts need to send queries that will be answered.  What
2927c478bd9Sstevel@tonic-gate 		 * about `rtquery`?  Maybe on firewalls you'd care, but not
2937c478bd9Sstevel@tonic-gate 		 * enough to give up the diagnostic facilities of remote
2947c478bd9Sstevel@tonic-gate 		 * probing.
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (n >= lim) {
2987c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty request from %s",
2997c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
3007c478bd9Sstevel@tonic-gate 			return;
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
3037c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
3047c478bd9Sstevel@tonic-gate 			    "request of bad length (%d) from %s",
3057c478bd9Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 		if (rip->rip_vers == RIPv2 && (ifp == NULL ||
3097c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_NO_RIPV1_OUT))) {
3107c478bd9Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv2;
3117c478bd9Sstevel@tonic-gate 			/*
3127c478bd9Sstevel@tonic-gate 			 * If we have a secret but it is a cleartext secret,
3137c478bd9Sstevel@tonic-gate 			 * do not disclose our secret unless the other guy
3147c478bd9Sstevel@tonic-gate 			 * already knows it.
3157c478bd9Sstevel@tonic-gate 			 */
3167c478bd9Sstevel@tonic-gate 			ap = find_auth(ifp);
3177c478bd9Sstevel@tonic-gate 			if (ap != NULL &&
3187c478bd9Sstevel@tonic-gate 			    (ulong_t)ap->end < (ulong_t)clk.tv_sec) {
3197c478bd9Sstevel@tonic-gate 				/*
3207c478bd9Sstevel@tonic-gate 				 * Don't authenticate incoming packets
3217c478bd9Sstevel@tonic-gate 				 * using an expired key.
3227c478bd9Sstevel@tonic-gate 				 */
3237c478bd9Sstevel@tonic-gate 				msglim(&use_auth, FROM_NADDR,
3247c478bd9Sstevel@tonic-gate 				    "%s attempting to authenticate using "
3257c478bd9Sstevel@tonic-gate 				    "an expired password.",
3267c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
3277c478bd9Sstevel@tonic-gate 				ap = NULL;
3287c478bd9Sstevel@tonic-gate 			}
3297c478bd9Sstevel@tonic-gate 			if (ap != NULL && ap->type == RIP_AUTH_PW &&
3307c478bd9Sstevel@tonic-gate 			    (n->n_family != RIP_AF_AUTH ||
3317c478bd9Sstevel@tonic-gate 			    !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR,
3327c478bd9Sstevel@tonic-gate 			    &use_auth)))
3337c478bd9Sstevel@tonic-gate 				ap = NULL;
3347c478bd9Sstevel@tonic-gate 		} else {
3357c478bd9Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv1;
3367c478bd9Sstevel@tonic-gate 			ap = NULL;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		clr_ws_buf(&v12buf, ap);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 		do {
3417c478bd9Sstevel@tonic-gate 			n->n_metric = ntohl(n->n_metric);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 			/*
3447c478bd9Sstevel@tonic-gate 			 * A single entry with family RIP_AF_UNSPEC and
3457c478bd9Sstevel@tonic-gate 			 * metric HOPCNT_INFINITY means "all routes".
3467c478bd9Sstevel@tonic-gate 			 * We respond to routers only if we are acting
3477c478bd9Sstevel@tonic-gate 			 * as a supplier, or to anyone other than a router
3487c478bd9Sstevel@tonic-gate 			 * (i.e. a query).
3497c478bd9Sstevel@tonic-gate 			 */
3507c478bd9Sstevel@tonic-gate 			if (n->n_family == RIP_AF_UNSPEC &&
3517c478bd9Sstevel@tonic-gate 			    n->n_metric == HOPCNT_INFINITY) {
3527c478bd9Sstevel@tonic-gate 				/*
3537c478bd9Sstevel@tonic-gate 				 * Answer a full-table query from a utility
3547c478bd9Sstevel@tonic-gate 				 * program with all we know.
3557c478bd9Sstevel@tonic-gate 				 */
3567c478bd9Sstevel@tonic-gate 				if (poll_answer ||
3577c478bd9Sstevel@tonic-gate 				    (from->sin_port != htons(RIP_PORT))) {
3587c478bd9Sstevel@tonic-gate 					supply(from, ifp, OUT_QUERY, 0,
3597c478bd9Sstevel@tonic-gate 					    rip->rip_vers, ap != NULL);
3607c478bd9Sstevel@tonic-gate 					return;
3617c478bd9Sstevel@tonic-gate 				}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 				/*
3647c478bd9Sstevel@tonic-gate 				 * A router is trying to prime its tables.
3657c478bd9Sstevel@tonic-gate 				 * Filter the answer in the same way
3667c478bd9Sstevel@tonic-gate 				 * broadcasts are filtered.
3677c478bd9Sstevel@tonic-gate 				 *
3687c478bd9Sstevel@tonic-gate 				 * Only answer a router if we are a supplier
3697c478bd9Sstevel@tonic-gate 				 * to keep an unwary host that is just starting
3707c478bd9Sstevel@tonic-gate 				 * from picking us as a router.
3717c478bd9Sstevel@tonic-gate 				 */
3727c478bd9Sstevel@tonic-gate 				if (ifp == NULL) {
3737c478bd9Sstevel@tonic-gate 					trace_pkt("ignore distant router");
3747c478bd9Sstevel@tonic-gate 					return;
3757c478bd9Sstevel@tonic-gate 				}
3767c478bd9Sstevel@tonic-gate 				if (IS_RIP_OFF(ifp->int_state) ||
3777c478bd9Sstevel@tonic-gate 				    !should_supply(ifp)) {
3787c478bd9Sstevel@tonic-gate 					trace_pkt("ignore; not supplying");
3797c478bd9Sstevel@tonic-gate 					return;
3807c478bd9Sstevel@tonic-gate 				}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 				/*
3837c478bd9Sstevel@tonic-gate 				 * Do not answer a RIPv1 router if
3847c478bd9Sstevel@tonic-gate 				 * we are sending RIPv2.  But do offer
3857c478bd9Sstevel@tonic-gate 				 * poor man's router discovery.
3867c478bd9Sstevel@tonic-gate 				 */
3877c478bd9Sstevel@tonic-gate 				if ((ifp->int_state & IS_NO_RIPV1_OUT) &&
3887c478bd9Sstevel@tonic-gate 				    rip->rip_vers == RIPv1) {
3897c478bd9Sstevel@tonic-gate 					if (!(ifp->int_state & IS_PM_RDISC)) {
3907c478bd9Sstevel@tonic-gate 					    trace_pkt("ignore; sending RIPv2");
3917c478bd9Sstevel@tonic-gate 					    return;
3927c478bd9Sstevel@tonic-gate 					}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 					v12buf.n->n_family = RIP_AF_INET;
3957c478bd9Sstevel@tonic-gate 					v12buf.n->n_dst = RIP_DEFAULT;
3967c478bd9Sstevel@tonic-gate 					metric = ifp->int_d_metric;
3977c478bd9Sstevel@tonic-gate 					if (NULL !=
3987c478bd9Sstevel@tonic-gate 					    (rt = rtget(RIP_DEFAULT, 0)))
3997c478bd9Sstevel@tonic-gate 						metric = MIN(metric,
4007c478bd9Sstevel@tonic-gate 						    (rt->rt_metric + 1));
4017c478bd9Sstevel@tonic-gate 					v12buf.n->n_metric = htonl(metric);
4027c478bd9Sstevel@tonic-gate 					v12buf.n++;
4037c478bd9Sstevel@tonic-gate 					break;
4047c478bd9Sstevel@tonic-gate 				}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 				/*
4077c478bd9Sstevel@tonic-gate 				 * Respond with RIPv1 instead of RIPv2 if
4087c478bd9Sstevel@tonic-gate 				 * that is what we are broadcasting on the
4097c478bd9Sstevel@tonic-gate 				 * interface to keep the remote router from
4107c478bd9Sstevel@tonic-gate 				 * getting the wrong initial idea of the
4117c478bd9Sstevel@tonic-gate 				 * routes we send.
4127c478bd9Sstevel@tonic-gate 				 */
4137c478bd9Sstevel@tonic-gate 				supply(from, ifp, OUT_UNICAST, 0,
4147c478bd9Sstevel@tonic-gate 				    (ifp->int_state & IS_NO_RIPV1_OUT)
4157c478bd9Sstevel@tonic-gate 				    ? RIPv2 : RIPv1,
4167c478bd9Sstevel@tonic-gate 				    ap != NULL);
4177c478bd9Sstevel@tonic-gate 				return;
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			/* Ignore authentication */
4217c478bd9Sstevel@tonic-gate 			if (n->n_family == RIP_AF_AUTH)
4227c478bd9Sstevel@tonic-gate 				continue;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 			if (n->n_family != RIP_AF_INET) {
4257c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
4267c478bd9Sstevel@tonic-gate 				    "request from %s for unsupported"
4277c478bd9Sstevel@tonic-gate 				    " (af %d) %s",
4287c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR),
4297c478bd9Sstevel@tonic-gate 				    ntohs(n->n_family),
4307c478bd9Sstevel@tonic-gate 				    naddr_ntoa(n->n_dst));
4317c478bd9Sstevel@tonic-gate 				return;
4327c478bd9Sstevel@tonic-gate 			}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 			/* We are being asked about a specific destination. */
4357c478bd9Sstevel@tonic-gate 			v12buf.n->n_dst = dst = n->n_dst;
4367c478bd9Sstevel@tonic-gate 			v12buf.n->n_family = RIP_AF_INET;
4377c478bd9Sstevel@tonic-gate 			if (!check_dst(dst)) {
4387c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
4397c478bd9Sstevel@tonic-gate 				    "bad queried destination %s from %s",
4407c478bd9Sstevel@tonic-gate 				    naddr_ntoa(dst),
4417c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
4427c478bd9Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
4437c478bd9Sstevel@tonic-gate 				goto rte_done;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			/* decide what mask was intended */
4477c478bd9Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1 ||
4487c478bd9Sstevel@tonic-gate 			    0 == (mask = ntohl(n->n_mask)) ||
4497c478bd9Sstevel@tonic-gate 			    0 != (ntohl(dst) & ~mask))
4507c478bd9Sstevel@tonic-gate 				mask = ripv1_mask_host(dst, ifp);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 			/*
4537c478bd9Sstevel@tonic-gate 			 * Try to find the answer.  If we don't have an
4547c478bd9Sstevel@tonic-gate 			 * explicit route for the destination, use the best
4557c478bd9Sstevel@tonic-gate 			 * route to the destination.
4567c478bd9Sstevel@tonic-gate 			 */
4577c478bd9Sstevel@tonic-gate 			rt = rtget(dst, mask);
4587c478bd9Sstevel@tonic-gate 			if (rt == NULL && dst != RIP_DEFAULT)
4597c478bd9Sstevel@tonic-gate 				rt = rtfind(n->n_dst);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			if (v12buf.buf->rip_vers != RIPv1)
4627c478bd9Sstevel@tonic-gate 				v12buf.n->n_mask = htonl(mask);
4637c478bd9Sstevel@tonic-gate 			if (rt == NULL) {
4647c478bd9Sstevel@tonic-gate 				/* we do not have the answer */
4657c478bd9Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
4667c478bd9Sstevel@tonic-gate 				goto rte_done;
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 			/*
4707c478bd9Sstevel@tonic-gate 			 * we have the answer, so compute the right metric
4717c478bd9Sstevel@tonic-gate 			 * and next hop.
4727c478bd9Sstevel@tonic-gate 			 */
4737c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = rt->rt_metric + 1;
4747c478bd9Sstevel@tonic-gate 			if (v12buf.n->n_metric > HOPCNT_INFINITY)
4757c478bd9Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
4767c478bd9Sstevel@tonic-gate 			if (v12buf.buf->rip_vers != RIPv1) {
4777c478bd9Sstevel@tonic-gate 				v12buf.n->n_tag = rt->rt_tag;
4787c478bd9Sstevel@tonic-gate 				if (ifp != NULL &&
4797c478bd9Sstevel@tonic-gate 				    on_net(rt->rt_gate, ifp->int_net,
4807c478bd9Sstevel@tonic-gate 				    ifp->int_mask) &&
4817c478bd9Sstevel@tonic-gate 				    rt->rt_gate != ifp->int_addr)
4827c478bd9Sstevel@tonic-gate 					v12buf.n->n_nhop = rt->rt_gate;
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate rte_done:
4857c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = htonl(v12buf.n->n_metric);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 			/*
4887c478bd9Sstevel@tonic-gate 			 * Stop paying attention if we fill the output buffer.
4897c478bd9Sstevel@tonic-gate 			 */
4907c478bd9Sstevel@tonic-gate 			if (++v12buf.n >= v12buf.lim)
4917c478bd9Sstevel@tonic-gate 				break;
4927c478bd9Sstevel@tonic-gate 		} while (++n < lim);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		/*
4957c478bd9Sstevel@tonic-gate 		 * If our response is authenticated with md5, complete the
4967c478bd9Sstevel@tonic-gate 		 * md5 computation.
4977c478bd9Sstevel@tonic-gate 		 */
4987c478bd9Sstevel@tonic-gate 		if (ap != NULL && ap->type == RIP_AUTH_MD5)
4997c478bd9Sstevel@tonic-gate 			end_md5_auth(&v12buf, ap);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		/*
5027c478bd9Sstevel@tonic-gate 		 * Diagnostic programs make specific requests
5037c478bd9Sstevel@tonic-gate 		 * from ports other than 520.  Log other types
5047c478bd9Sstevel@tonic-gate 		 * of specific requests as suspicious.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
5077c478bd9Sstevel@tonic-gate 			writelog(LOG_WARNING,
5087c478bd9Sstevel@tonic-gate 			    "Received suspicious request from %s port %d",
5097c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR), RIP_PORT);
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 		if (poll_answer || (from->sin_port != htons(RIP_PORT))) {
5127c478bd9Sstevel@tonic-gate 			/* query */
5137c478bd9Sstevel@tonic-gate 			(void) output(OUT_QUERY, from, ifp, v12buf.buf,
5147c478bd9Sstevel@tonic-gate 			    ((char *)v12buf.n - (char *)v12buf.buf));
5157c478bd9Sstevel@tonic-gate 		} else {
5167c478bd9Sstevel@tonic-gate 			(void) output(OUT_UNICAST, from, ifp,
5177c478bd9Sstevel@tonic-gate 			    v12buf.buf, ((char *)v12buf.n -
5187c478bd9Sstevel@tonic-gate 			    (char *)v12buf.buf));
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		return;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	case RIPCMD_TRACEON:
5237c478bd9Sstevel@tonic-gate 	case RIPCMD_TRACEOFF:
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Notice that trace messages are turned off for all possible
5267c478bd9Sstevel@tonic-gate 		 * abuse if PATH_TRACE is undefined in pathnames.h.
5277c478bd9Sstevel@tonic-gate 		 * Notice also that because of the way the trace file is
5287c478bd9Sstevel@tonic-gate 		 * handled in trace.c, no abuse is plausible even if
5297c478bd9Sstevel@tonic-gate 		 * PATH_TRACE is defined.
5307c478bd9Sstevel@tonic-gate 		 *
5317c478bd9Sstevel@tonic-gate 		 * First verify message came from a privileged port.
5327c478bd9Sstevel@tonic-gate 		 */
5337c478bd9Sstevel@tonic-gate 		if (ntohs(from->sin_port) > IPPORT_RESERVED) {
5347c478bd9Sstevel@tonic-gate 			trace_pkt("trace command from untrusted port %d on %s",
5357c478bd9Sstevel@tonic-gate 			    ntohs(from->sin_port), naddr_ntoa(FROM_NADDR));
5367c478bd9Sstevel@tonic-gate 			return;
5377c478bd9Sstevel@tonic-gate 		}
5387c478bd9Sstevel@tonic-gate 		if (ifp == NULL || !remote_address_ok(ifp, FROM_NADDR)) {
5397c478bd9Sstevel@tonic-gate 			/*
5407c478bd9Sstevel@tonic-gate 			 * Use a message here to warn about strange
5417c478bd9Sstevel@tonic-gate 			 * messages from remote systems.
5427c478bd9Sstevel@tonic-gate 			 */
5437c478bd9Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
5447c478bd9Sstevel@tonic-gate 			    "trace command from non-local host %s",
5457c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
5467c478bd9Sstevel@tonic-gate 			return;
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_DISTRUST) {
5497c478bd9Sstevel@tonic-gate 			tg = tgates;
5507c478bd9Sstevel@tonic-gate 			while (tg->tgate_addr != FROM_NADDR) {
5517c478bd9Sstevel@tonic-gate 				tg = tg->tgate_next;
5527c478bd9Sstevel@tonic-gate 				if (tg == NULL) {
5537c478bd9Sstevel@tonic-gate 					trace_pkt("trace command from "
5547c478bd9Sstevel@tonic-gate 					    "untrusted host %s",
5557c478bd9Sstevel@tonic-gate 					    naddr_ntoa(FROM_NADDR));
5567c478bd9Sstevel@tonic-gate 					return;
5577c478bd9Sstevel@tonic-gate 				}
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		if (ifp->int_auth[0].type != RIP_AUTH_NONE) {
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * Technically, it would be fairly easy to add
5637c478bd9Sstevel@tonic-gate 			 * standard authentication to the existing
5647c478bd9Sstevel@tonic-gate 			 * trace commands -- just bracket the payload
5657c478bd9Sstevel@tonic-gate 			 * with the authentication information.
5667c478bd9Sstevel@tonic-gate 			 * However, the tracing message behavior
5677c478bd9Sstevel@tonic-gate 			 * itself is marginal enough that we don't
5687c478bd9Sstevel@tonic-gate 			 * actually care.  Just discard if
5697c478bd9Sstevel@tonic-gate 			 * authentication is needed.
5707c478bd9Sstevel@tonic-gate 			 */
5717c478bd9Sstevel@tonic-gate 			trace_pkt("trace command unauthenticated from %s",
5727c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
5737c478bd9Sstevel@tonic-gate 			return;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 		if (rip->rip_cmd == RIPCMD_TRACEON) {
5767c478bd9Sstevel@tonic-gate 			rip->rip_tracefile[cc-4] = '\0';
5777c478bd9Sstevel@tonic-gate 			set_tracefile(rip->rip_tracefile,
5787c478bd9Sstevel@tonic-gate 			    "trace command: %s\n", 0);
5797c478bd9Sstevel@tonic-gate 		} else {
5807c478bd9Sstevel@tonic-gate 			trace_off("tracing turned off by %s",
5817c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		return;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	case RIPCMD_RESPONSE:
5867c478bd9Sstevel@tonic-gate 		if (ifp != NULL && (ifp->int_if_flags & IFF_NOXMIT)) {
5877c478bd9Sstevel@tonic-gate 			trace_misc("discard RIP response received over %s "
5887c478bd9Sstevel@tonic-gate 			    "(IFF_NOXMIT)", ifp->int_name);
5897c478bd9Sstevel@tonic-gate 			return;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
5937c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
5947c478bd9Sstevel@tonic-gate 			    "response of bad length (%d) from %s",
5957c478bd9Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		if ((ntohl(FROM_NADDR) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
5997c478bd9Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
6007c478bd9Sstevel@tonic-gate 			    "discard RIP response from bad source address %s",
6017c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
6027c478bd9Sstevel@tonic-gate 			return;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		/* verify message came from a router */
6067c478bd9Sstevel@tonic-gate 		if (from->sin_port != htons(RIP_PORT)) {
6077c478bd9Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
6087c478bd9Sstevel@tonic-gate 			    "    discard RIP response from unknown port"
6097c478bd9Sstevel@tonic-gate 			    " %d on host %s", ntohs(from->sin_port),
6107c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
6117c478bd9Sstevel@tonic-gate 			return;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		if (!rip_enabled) {
6157c478bd9Sstevel@tonic-gate 			trace_pkt("    discard response while RIP off");
6167c478bd9Sstevel@tonic-gate 			return;
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		/* Are we talking to ourself or a remote gateway? */
6207c478bd9Sstevel@tonic-gate 		ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
6217c478bd9Sstevel@tonic-gate 		if (ifp1 != NULL) {
6227c478bd9Sstevel@tonic-gate 			if (ifp1->int_state & IS_REMOTE) {
6237c478bd9Sstevel@tonic-gate 				/* remote gateway */
6247c478bd9Sstevel@tonic-gate 				ifp = ifp1;
6257c478bd9Sstevel@tonic-gate 				if (check_remote(ifp)) {
6267c478bd9Sstevel@tonic-gate 					ifp->int_act_time = now.tv_sec;
6277c478bd9Sstevel@tonic-gate 					if_ok(ifp, "remote ", _B_FALSE);
6287c478bd9Sstevel@tonic-gate 				}
6297c478bd9Sstevel@tonic-gate 			} else {
6307c478bd9Sstevel@tonic-gate 				trace_pkt("    discard our own RIP response");
6317c478bd9Sstevel@tonic-gate 				return;
6327c478bd9Sstevel@tonic-gate 			}
6337c478bd9Sstevel@tonic-gate 		} else {
6347c478bd9Sstevel@tonic-gate 			/*
6357c478bd9Sstevel@tonic-gate 			 * If it's not a remote gateway, then the
6367c478bd9Sstevel@tonic-gate 			 * remote address *must* be directly
6377c478bd9Sstevel@tonic-gate 			 * connected.  Make sure that it is.
6387c478bd9Sstevel@tonic-gate 			 */
6397c478bd9Sstevel@tonic-gate 			if (ifp != NULL &&
6407c478bd9Sstevel@tonic-gate 			    !remote_address_ok(ifp, FROM_NADDR)) {
6417c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
6427c478bd9Sstevel@tonic-gate 				    "discard RIP response; source %s not on "
6437c478bd9Sstevel@tonic-gate 				    "interface %s", naddr_ntoa(FROM_NADDR),
6447c478bd9Sstevel@tonic-gate 				    ifp->int_name);
6457c478bd9Sstevel@tonic-gate 				return;
6467c478bd9Sstevel@tonic-gate 			}
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		/*
6507c478bd9Sstevel@tonic-gate 		 * Accept routing packets from routers directly connected
6517c478bd9Sstevel@tonic-gate 		 * via broadcast or point-to-point networks, and from
6527c478bd9Sstevel@tonic-gate 		 * those listed in /etc/gateways.
6537c478bd9Sstevel@tonic-gate 		 */
6547c478bd9Sstevel@tonic-gate 		if (ifp == NULL) {
6557c478bd9Sstevel@tonic-gate 			msglim(&unk_router, FROM_NADDR,
6567c478bd9Sstevel@tonic-gate 			    "   discard response from %s"
6577c478bd9Sstevel@tonic-gate 			    " via unexpected interface",
6587c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
6597c478bd9Sstevel@tonic-gate 			return;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if (IS_RIP_IN_OFF(ifp->int_state)) {
6637c478bd9Sstevel@tonic-gate 			trace_pkt("    discard RIPv%d response"
6647c478bd9Sstevel@tonic-gate 			    " via disabled interface %s",
6657c478bd9Sstevel@tonic-gate 			    rip->rip_vers, ifp->int_name);
6667c478bd9Sstevel@tonic-gate 			return;
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		if (n >= lim) {
6707c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty response from %s",
6717c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
6727c478bd9Sstevel@tonic-gate 			return;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		if (((ifp->int_state & IS_NO_RIPV1_IN) &&
6767c478bd9Sstevel@tonic-gate 		    rip->rip_vers == RIPv1) ||
6777c478bd9Sstevel@tonic-gate 		    ((ifp->int_state & IS_NO_RIPV2_IN) &&
6787c478bd9Sstevel@tonic-gate 		    rip->rip_vers != RIPv1)) {
6797c478bd9Sstevel@tonic-gate 			trace_pkt("    discard RIPv%d response",
6807c478bd9Sstevel@tonic-gate 			    rip->rip_vers);
6817c478bd9Sstevel@tonic-gate 			return;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Continue to listen to routes via broken interfaces
6867c478bd9Sstevel@tonic-gate 		 * which might be declared IS_BROKE because of
6877c478bd9Sstevel@tonic-gate 		 * device-driver idiosyncracies, but might otherwise
6887c478bd9Sstevel@tonic-gate 		 * be perfectly healthy.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_BROKE) {
6917c478bd9Sstevel@tonic-gate 			trace_pkt("response via broken interface %s",
6927c478bd9Sstevel@tonic-gate 			    ifp->int_name);
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * If the interface cares, ignore bad routers.
6977c478bd9Sstevel@tonic-gate 		 * Trace but do not log this problem, because where it
6987c478bd9Sstevel@tonic-gate 		 * happens, it happens frequently.
6997c478bd9Sstevel@tonic-gate 		 */
7007c478bd9Sstevel@tonic-gate 		if (ifp->int_state & IS_DISTRUST) {
7017c478bd9Sstevel@tonic-gate 			tg = tgates;
7027c478bd9Sstevel@tonic-gate 			while (tg->tgate_addr != FROM_NADDR) {
7037c478bd9Sstevel@tonic-gate 				tg = tg->tgate_next;
7047c478bd9Sstevel@tonic-gate 				if (tg == NULL) {
7057c478bd9Sstevel@tonic-gate 					trace_pkt("    discard RIP response"
7067c478bd9Sstevel@tonic-gate 					    " from untrusted router %s",
7077c478bd9Sstevel@tonic-gate 					    naddr_ntoa(FROM_NADDR));
7087c478bd9Sstevel@tonic-gate 					return;
7097c478bd9Sstevel@tonic-gate 				}
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		/*
7147c478bd9Sstevel@tonic-gate 		 * Authenticate the packet if we have a secret.
7157c478bd9Sstevel@tonic-gate 		 * If we do not have any secrets, ignore the error in
7167c478bd9Sstevel@tonic-gate 		 * RFC 1723 and accept it regardless.
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		if (ifp->int_auth[0].type != RIP_AUTH_NONE &&
7197c478bd9Sstevel@tonic-gate 		    rip->rip_vers != RIPv1 &&
7207c478bd9Sstevel@tonic-gate 		    !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR, &use_auth))
7217c478bd9Sstevel@tonic-gate 			return;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/*
7247c478bd9Sstevel@tonic-gate 		 * Do this only if we're supplying routes to *nobody*.
7257c478bd9Sstevel@tonic-gate 		 */
7267c478bd9Sstevel@tonic-gate 		if (!should_supply(NULL) && save_space) {
7277c478bd9Sstevel@tonic-gate 			/*
7287c478bd9Sstevel@tonic-gate 			 * "-S" option.  Instead of entering all routes,
7297c478bd9Sstevel@tonic-gate 			 * only enter a default route for the sender of
7307c478bd9Sstevel@tonic-gate 			 * this RESPONSE message
7317c478bd9Sstevel@tonic-gate 			 */
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 			/* Should we trust this route from this router? */
7347c478bd9Sstevel@tonic-gate 			if (tg != NULL && tg->tgate_nets->mask != 0) {
7357c478bd9Sstevel@tonic-gate 				trace_pkt("   ignored unauthorized %s",
7367c478bd9Sstevel@tonic-gate 				    addrname(RIP_DEFAULT, 0, 0));
7377c478bd9Sstevel@tonic-gate 				break;
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 			new.rts_gate = FROM_NADDR;
7417c478bd9Sstevel@tonic-gate 			new.rts_router = FROM_NADDR;
7427c478bd9Sstevel@tonic-gate 			new.rts_metric = HOPCNT_INFINITY-1;
7437c478bd9Sstevel@tonic-gate 			new.rts_tag = n->n_tag;
7447c478bd9Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
7457c478bd9Sstevel@tonic-gate 			new.rts_ifp = ifp;
7467c478bd9Sstevel@tonic-gate 			new.rts_de_ag = 0;
7477c478bd9Sstevel@tonic-gate 			new.rts_origin = RO_RIP;
7487c478bd9Sstevel@tonic-gate 			/*
7497c478bd9Sstevel@tonic-gate 			 * Add the newly generated default route, but don't
7507c478bd9Sstevel@tonic-gate 			 * propagate the madness.  Treat it the same way as
7517c478bd9Sstevel@tonic-gate 			 * default routes learned from Router Discovery.
7527c478bd9Sstevel@tonic-gate 			 */
7537c478bd9Sstevel@tonic-gate 			input_route(RIP_DEFAULT, 0, &new, n, RS_NOPROPAGATE);
7547c478bd9Sstevel@tonic-gate 			return;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		if (!IS_IFF_ROUTING(ifp->int_if_flags)) {
7587c478bd9Sstevel@tonic-gate 			/*
7597c478bd9Sstevel@tonic-gate 			 * We don't want to propagate routes which would
7607c478bd9Sstevel@tonic-gate 			 * result in a black-hole.
7617c478bd9Sstevel@tonic-gate 			 */
7627c478bd9Sstevel@tonic-gate 			rt_state = RS_NOPROPAGATE;
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		do {
7667c478bd9Sstevel@tonic-gate 			if (n->n_family == RIP_AF_AUTH)
7677c478bd9Sstevel@tonic-gate 				continue;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 			n->n_metric = ntohl(n->n_metric);
7707c478bd9Sstevel@tonic-gate 			dst = n->n_dst;
7717c478bd9Sstevel@tonic-gate 			if (n->n_family != RIP_AF_INET &&
7727c478bd9Sstevel@tonic-gate 			    (n->n_family != RIP_AF_UNSPEC ||
7737c478bd9Sstevel@tonic-gate 			    dst != RIP_DEFAULT)) {
7747c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
7757c478bd9Sstevel@tonic-gate 				    "route from %s to unsupported"
7767c478bd9Sstevel@tonic-gate 				    " address family=%d destination=%s",
7777c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR), n->n_family,
7787c478bd9Sstevel@tonic-gate 				    naddr_ntoa(dst));
7797c478bd9Sstevel@tonic-gate 				continue;
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 			if (!check_dst(dst)) {
7827c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
7837c478bd9Sstevel@tonic-gate 				    "bad destination %s from %s",
7847c478bd9Sstevel@tonic-gate 				    naddr_ntoa(dst),
7857c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
7867c478bd9Sstevel@tonic-gate 				continue;
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 			if (n->n_metric == 0 || n->n_metric > HOPCNT_INFINITY) {
7897c478bd9Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
7907c478bd9Sstevel@tonic-gate 				    "bad metric %d from %s"
7917c478bd9Sstevel@tonic-gate 				    " for destination %s",
7927c478bd9Sstevel@tonic-gate 				    n->n_metric, naddr_ntoa(FROM_NADDR),
7937c478bd9Sstevel@tonic-gate 				    naddr_ntoa(dst));
7947c478bd9Sstevel@tonic-gate 				continue;
7957c478bd9Sstevel@tonic-gate 			}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 			/*
7987c478bd9Sstevel@tonic-gate 			 * Notice the next-hop.
7997c478bd9Sstevel@tonic-gate 			 */
8007c478bd9Sstevel@tonic-gate 			gate = FROM_NADDR;
8017c478bd9Sstevel@tonic-gate 			if (n->n_nhop != 0) {
8027c478bd9Sstevel@tonic-gate 				if (rip->rip_vers == RIPv1) {
8037c478bd9Sstevel@tonic-gate 					n->n_nhop = 0;
8047c478bd9Sstevel@tonic-gate 				} else {
8057c478bd9Sstevel@tonic-gate 					/* Use it only if it is valid. */
8067c478bd9Sstevel@tonic-gate 					if (on_net(n->n_nhop,
8077c478bd9Sstevel@tonic-gate 					    ifp->int_net, ifp->int_mask) &&
8087c478bd9Sstevel@tonic-gate 					    check_dst(n->n_nhop)) {
8097c478bd9Sstevel@tonic-gate 						gate = n->n_nhop;
8107c478bd9Sstevel@tonic-gate 					} else {
8117c478bd9Sstevel@tonic-gate 						msglim(&bad_nhop,
8127c478bd9Sstevel@tonic-gate 						    FROM_NADDR,
8137c478bd9Sstevel@tonic-gate 						    "router %s to %s"
8147c478bd9Sstevel@tonic-gate 						    " has bad next hop %s",
8157c478bd9Sstevel@tonic-gate 						    naddr_ntoa(FROM_NADDR),
8167c478bd9Sstevel@tonic-gate 						    naddr_ntoa(dst),
8177c478bd9Sstevel@tonic-gate 						    naddr_ntoa(n->n_nhop));
8187c478bd9Sstevel@tonic-gate 						n->n_nhop = 0;
8197c478bd9Sstevel@tonic-gate 					}
8207c478bd9Sstevel@tonic-gate 				}
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1 ||
8247c478bd9Sstevel@tonic-gate 			    0 == (mask = ntohl(n->n_mask))) {
8257c478bd9Sstevel@tonic-gate 				mask = ripv1_mask_host(dst, ifp);
8267c478bd9Sstevel@tonic-gate 			} else if ((ntohl(dst) & ~mask) != 0) {
8277c478bd9Sstevel@tonic-gate 				msglim(&bad_mask, FROM_NADDR,
8287c478bd9Sstevel@tonic-gate 				    "router %s sent bad netmask %s with %s",
8297c478bd9Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR),
8307c478bd9Sstevel@tonic-gate 				    naddr_ntoa(htonl(mask)),
8317c478bd9Sstevel@tonic-gate 				    naddr_ntoa(dst));
8327c478bd9Sstevel@tonic-gate 				continue;
8337c478bd9Sstevel@tonic-gate 			}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 			if (mask == HOST_MASK &&
8367c478bd9Sstevel@tonic-gate 			    (ifp->int_state & IS_NO_HOST)) {
8377c478bd9Sstevel@tonic-gate 				trace_pkt("   ignored host route %s",
8387c478bd9Sstevel@tonic-gate 				    addrname(dst, mask, 0));
8397c478bd9Sstevel@tonic-gate 				continue;
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1)
8437c478bd9Sstevel@tonic-gate 				n->n_tag = 0;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 			/*
8467c478bd9Sstevel@tonic-gate 			 * Adjust metric according to incoming interface cost.
8477c478bd9Sstevel@tonic-gate 			 * We intentionally don't drop incoming routes with
8487c478bd9Sstevel@tonic-gate 			 * metric 15 on the floor even though they will
8497c478bd9Sstevel@tonic-gate 			 * not be advertised to other routers.  We can use
8507c478bd9Sstevel@tonic-gate 			 * such routes locally, resulting in a network with
8517c478bd9Sstevel@tonic-gate 			 * a maximum width of 15 hops rather than 14.
8527c478bd9Sstevel@tonic-gate 			 */
8537c478bd9Sstevel@tonic-gate 			n->n_metric += ifp->int_metric;
8547c478bd9Sstevel@tonic-gate 			if (n->n_metric > HOPCNT_INFINITY)
8557c478bd9Sstevel@tonic-gate 				n->n_metric = HOPCNT_INFINITY;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 			/*
8587c478bd9Sstevel@tonic-gate 			 * Should we trust this route from this router?
8597c478bd9Sstevel@tonic-gate 			 */
8607c478bd9Sstevel@tonic-gate 			if (tg != NULL && (tn = tg->tgate_nets)->mask != 0) {
8617c478bd9Sstevel@tonic-gate 				for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
8627c478bd9Sstevel@tonic-gate 					if (on_net(dst, tn->net, tn->mask) &&
8637c478bd9Sstevel@tonic-gate 					    tn->mask <= mask)
8647c478bd9Sstevel@tonic-gate 						break;
8657c478bd9Sstevel@tonic-gate 				}
8667c478bd9Sstevel@tonic-gate 				if (i >= MAX_TGATE_NETS || tn->mask == 0) {
8677c478bd9Sstevel@tonic-gate 					trace_pkt("   ignored unauthorized %s",
8687c478bd9Sstevel@tonic-gate 					    addrname(dst, mask, 0));
8697c478bd9Sstevel@tonic-gate 					continue;
8707c478bd9Sstevel@tonic-gate 				}
8717c478bd9Sstevel@tonic-gate 			}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			/*
8747c478bd9Sstevel@tonic-gate 			 * Recognize and ignore a default route we faked
8757c478bd9Sstevel@tonic-gate 			 * which is being sent back to us by a machine with
8767c478bd9Sstevel@tonic-gate 			 * broken split-horizon. Be a little more paranoid
8777c478bd9Sstevel@tonic-gate 			 * than that, and reject default routes with the
8787c478bd9Sstevel@tonic-gate 			 * same metric we advertised.
8797c478bd9Sstevel@tonic-gate 			 */
8807c478bd9Sstevel@tonic-gate 			if (ifp->int_d_metric != 0 && dst == RIP_DEFAULT &&
8817c478bd9Sstevel@tonic-gate 			    n->n_metric >= ifp->int_d_metric)
8827c478bd9Sstevel@tonic-gate 				continue;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 			/*
8857c478bd9Sstevel@tonic-gate 			 * We can receive aggregated RIPv2 routes that must
8867c478bd9Sstevel@tonic-gate 			 * be broken down before they are transmitted by
8877c478bd9Sstevel@tonic-gate 			 * RIPv1 via an interface on a subnet. We might
8887c478bd9Sstevel@tonic-gate 			 * also receive the same routes aggregated via
8897c478bd9Sstevel@tonic-gate 			 * other RIPv2 interfaces.  This could cause
8907c478bd9Sstevel@tonic-gate 			 * duplicate routes to be sent on the RIPv1
8917c478bd9Sstevel@tonic-gate 			 * interfaces. "Longest matching variable length
8927c478bd9Sstevel@tonic-gate 			 * netmasks" lets RIPv2 listeners understand, but
8937c478bd9Sstevel@tonic-gate 			 * breaking down the aggregated routes for RIPv1
8947c478bd9Sstevel@tonic-gate 			 * listeners can produce duplicate routes.
8957c478bd9Sstevel@tonic-gate 			 *
8967c478bd9Sstevel@tonic-gate 			 * Breaking down aggregated routes here bloats the
8977c478bd9Sstevel@tonic-gate 			 * daemon table, but does not hurt the kernel
8987c478bd9Sstevel@tonic-gate 			 * table, since routes are always aggregated for
8997c478bd9Sstevel@tonic-gate 			 * the kernel.
9007c478bd9Sstevel@tonic-gate 			 *
9017c478bd9Sstevel@tonic-gate 			 * Notice that this does not break down network
9027c478bd9Sstevel@tonic-gate 			 * routes corresponding to subnets. This is part of
9037c478bd9Sstevel@tonic-gate 			 * the defense against RS_NET_SYN.
9047c478bd9Sstevel@tonic-gate 			 */
9057c478bd9Sstevel@tonic-gate 			if (have_ripv1_out &&
9067c478bd9Sstevel@tonic-gate 			    (((rt = rtget(dst, mask)) == NULL ||
9077c478bd9Sstevel@tonic-gate 			    !(rt->rt_state & RS_NET_SYN))) &&
9087c478bd9Sstevel@tonic-gate 			    (v1_mask = ripv1_mask_net(dst, 0)) > mask) {
9097c478bd9Sstevel@tonic-gate 				/* Get least significant set bit */
9107c478bd9Sstevel@tonic-gate 				ddst_h = v1_mask & -v1_mask;
9117c478bd9Sstevel@tonic-gate 				i = (v1_mask & ~mask)/ddst_h;
9127c478bd9Sstevel@tonic-gate 				/*
9137c478bd9Sstevel@tonic-gate 				 * If you're going to make 512 or more
9147c478bd9Sstevel@tonic-gate 				 * routes, then that's just too many.  The
9157c478bd9Sstevel@tonic-gate 				 * reason here is that breaking an old
9167c478bd9Sstevel@tonic-gate 				 * class B into /24 allocations is common
9177c478bd9Sstevel@tonic-gate 				 * enough that allowing for the creation of
9187c478bd9Sstevel@tonic-gate 				 * at least 256 deaggregated routes is
9197c478bd9Sstevel@tonic-gate 				 * good.  The next power of 2 is 512.
9207c478bd9Sstevel@tonic-gate 				 */
9217c478bd9Sstevel@tonic-gate 				if (i >= 511) {
9227c478bd9Sstevel@tonic-gate 					/*
9237c478bd9Sstevel@tonic-gate 					 * Punt if we would have to
9247c478bd9Sstevel@tonic-gate 					 * generate an unreasonable number
9257c478bd9Sstevel@tonic-gate 					 * of routes.
9267c478bd9Sstevel@tonic-gate 					 */
9277c478bd9Sstevel@tonic-gate 					if (TRACECONTENTS)
9287c478bd9Sstevel@tonic-gate 						trace_misc("accept %s-->%s as 1"
9297c478bd9Sstevel@tonic-gate 						    " instead of %d routes",
9307c478bd9Sstevel@tonic-gate 						    addrname(dst, mask, 0),
9317c478bd9Sstevel@tonic-gate 						    naddr_ntoa(FROM_NADDR),
9327c478bd9Sstevel@tonic-gate 						    i + 1);
9337c478bd9Sstevel@tonic-gate 					i = 0;
9347c478bd9Sstevel@tonic-gate 				} else {
9357c478bd9Sstevel@tonic-gate 					mask = v1_mask;
9367c478bd9Sstevel@tonic-gate 				}
9377c478bd9Sstevel@tonic-gate 			} else {
9387c478bd9Sstevel@tonic-gate 				i = 0;
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 			new.rts_gate = gate;
9427c478bd9Sstevel@tonic-gate 			new.rts_router = FROM_NADDR;
9437c478bd9Sstevel@tonic-gate 			new.rts_metric = n->n_metric;
9447c478bd9Sstevel@tonic-gate 			new.rts_tag = n->n_tag;
9457c478bd9Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
9467c478bd9Sstevel@tonic-gate 			new.rts_ifp = ifp;
9477c478bd9Sstevel@tonic-gate 			new.rts_de_ag = i;
9487c478bd9Sstevel@tonic-gate 			new.rts_origin = RO_RIP;
9497c478bd9Sstevel@tonic-gate 			j = 0;
9507c478bd9Sstevel@tonic-gate 			for (;;) {
9517c478bd9Sstevel@tonic-gate 				input_route(dst, mask, &new, n, rt_state);
9527c478bd9Sstevel@tonic-gate 				if (++j > i)
9537c478bd9Sstevel@tonic-gate 					break;
9547c478bd9Sstevel@tonic-gate 				dst = htonl(ntohl(dst) + ddst_h);
9557c478bd9Sstevel@tonic-gate 			}
9567c478bd9Sstevel@tonic-gate 		} while (++n < lim);
9577c478bd9Sstevel@tonic-gate 		return;
9587c478bd9Sstevel@tonic-gate 	case RIPCMD_POLLENTRY:
9597c478bd9Sstevel@tonic-gate 		/*
9607c478bd9Sstevel@tonic-gate 		 * With this command one can request a single entry.
9617c478bd9Sstevel@tonic-gate 		 * Both silent processes and routers can respond to this
9627c478bd9Sstevel@tonic-gate 		 * command
9637c478bd9Sstevel@tonic-gate 		 */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		if (n >= lim) {
9667c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty request from %s",
9677c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
9687c478bd9Sstevel@tonic-gate 			return;
9697c478bd9Sstevel@tonic-gate 		}
9707c478bd9Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
9717c478bd9Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
9727c478bd9Sstevel@tonic-gate 			    "request of bad length (%d) from %s",
9737c478bd9Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		if (rip->rip_vers == RIPv2 && (ifp == NULL ||
9777c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_NO_RIPV1_OUT))) {
9787c478bd9Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv2;
9797c478bd9Sstevel@tonic-gate 		} else {
9807c478bd9Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv1;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 		/* Dont bother with md5 authentication with POLLENTRY */
9837c478bd9Sstevel@tonic-gate 		ap = NULL;
9847c478bd9Sstevel@tonic-gate 		clr_ws_buf(&v12buf, ap);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		n->n_metric = ntohl(n->n_metric);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		if (n->n_family != RIP_AF_INET) {
9897c478bd9Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
9907c478bd9Sstevel@tonic-gate 			    "POLLENTRY request from %s for unsupported"
9917c478bd9Sstevel@tonic-gate 			    " (af %d) %s",
9927c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR),
9937c478bd9Sstevel@tonic-gate 			    ntohs(n->n_family),
9947c478bd9Sstevel@tonic-gate 			    naddr_ntoa(n->n_dst));
9957c478bd9Sstevel@tonic-gate 			return;
9967c478bd9Sstevel@tonic-gate 		}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		/* We are being asked about a specific destination. */
9997c478bd9Sstevel@tonic-gate 		v12buf.n->n_dst = dst = n->n_dst;
10007c478bd9Sstevel@tonic-gate 		v12buf.n->n_family = RIP_AF_INET;
10017c478bd9Sstevel@tonic-gate 		if (!check_dst(dst)) {
10027c478bd9Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
10037c478bd9Sstevel@tonic-gate 			    "bad queried destination %s from %s",
10047c478bd9Sstevel@tonic-gate 			    naddr_ntoa(dst),
10057c478bd9Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
10067c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
10077c478bd9Sstevel@tonic-gate 			goto pollentry_done;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		/* decide what mask was intended */
10117c478bd9Sstevel@tonic-gate 		if (rip->rip_vers == RIPv1 ||
10127c478bd9Sstevel@tonic-gate 		    0 == (mask = ntohl(n->n_mask)) ||
10137c478bd9Sstevel@tonic-gate 		    0 != (ntohl(dst) & ~mask))
10147c478bd9Sstevel@tonic-gate 			mask = ripv1_mask_host(dst, ifp);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 		/* try to find the answer */
10177c478bd9Sstevel@tonic-gate 		rt = rtget(dst, mask);
10187c478bd9Sstevel@tonic-gate 		if (rt == NULL && dst != RIP_DEFAULT)
10197c478bd9Sstevel@tonic-gate 			rt = rtfind(n->n_dst);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 		if (v12buf.buf->rip_vers != RIPv1)
10227c478bd9Sstevel@tonic-gate 			v12buf.n->n_mask = htonl(mask);
10237c478bd9Sstevel@tonic-gate 		if (rt == NULL) {
10247c478bd9Sstevel@tonic-gate 			/* we do not have the answer */
10257c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
10267c478bd9Sstevel@tonic-gate 			goto pollentry_done;
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		/*
10317c478bd9Sstevel@tonic-gate 		 * we have the answer, so compute the right metric and next
10327c478bd9Sstevel@tonic-gate 		 * hop.
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		v12buf.n->n_metric = rt->rt_metric + 1;
10357c478bd9Sstevel@tonic-gate 		if (v12buf.n->n_metric > HOPCNT_INFINITY)
10367c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
10377c478bd9Sstevel@tonic-gate 		if (v12buf.buf->rip_vers != RIPv1) {
10387c478bd9Sstevel@tonic-gate 			v12buf.n->n_tag = rt->rt_tag;
10397c478bd9Sstevel@tonic-gate 			if (ifp != NULL &&
10407c478bd9Sstevel@tonic-gate 			    on_net(rt->rt_gate, ifp->int_net, ifp->int_mask) &&
10417c478bd9Sstevel@tonic-gate 			    rt->rt_gate != ifp->int_addr)
10427c478bd9Sstevel@tonic-gate 				v12buf.n->n_nhop = rt->rt_gate;
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate pollentry_done:
10457c478bd9Sstevel@tonic-gate 		v12buf.n->n_metric = htonl(v12buf.n->n_metric);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		/*
10487c478bd9Sstevel@tonic-gate 		 * Send the answer about specific routes.
10497c478bd9Sstevel@tonic-gate 		 */
10507c478bd9Sstevel@tonic-gate 		(void) output(OUT_QUERY, from, ifp, v12buf.buf,
10517c478bd9Sstevel@tonic-gate 		    ((char *)v12buf.n - (char *)v12buf.buf));
10527c478bd9Sstevel@tonic-gate 		break;
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate #undef FROM_NADDR
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate  * Process a single input route.
10607c478bd9Sstevel@tonic-gate  */
10617c478bd9Sstevel@tonic-gate void
10627c478bd9Sstevel@tonic-gate input_route(in_addr_t dst,			/* network order */
10637c478bd9Sstevel@tonic-gate     in_addr_t mask,
10647c478bd9Sstevel@tonic-gate     struct rt_spare *new,
10657c478bd9Sstevel@tonic-gate     struct netinfo *n,
10667c478bd9Sstevel@tonic-gate     uint16_t rt_state)
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate 	int i;
10697c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
10707c478bd9Sstevel@tonic-gate 	struct rt_spare *rts, *rts0;
10717c478bd9Sstevel@tonic-gate 	struct interface *ifp1;
10727c478bd9Sstevel@tonic-gate 	struct rt_spare *ptr;
10737c478bd9Sstevel@tonic-gate 	size_t ptrsize;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	/*
10767c478bd9Sstevel@tonic-gate 	 * See if we can already get there by a working interface.  Ignore
10777c478bd9Sstevel@tonic-gate 	 * if so.
10787c478bd9Sstevel@tonic-gate 	 */
10797c478bd9Sstevel@tonic-gate 	ifp1 = ifwithaddr(dst, _B_TRUE, _B_FALSE);
10807c478bd9Sstevel@tonic-gate 	if (ifp1 != NULL && (ifp1->int_state & IS_PASSIVE))
10817c478bd9Sstevel@tonic-gate 		return;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * Look for the route in our table.
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	rt = rtget(dst, mask);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	/* Consider adding the route if we do not already have it. */
10897c478bd9Sstevel@tonic-gate 	if (rt == NULL) {
10907c478bd9Sstevel@tonic-gate 		/* Ignore unknown routes being poisoned. */
10917c478bd9Sstevel@tonic-gate 		if (new->rts_metric == HOPCNT_INFINITY)
10927c478bd9Sstevel@tonic-gate 			return;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		/* Ignore the route if it points to us */
10957c478bd9Sstevel@tonic-gate 		if (n != NULL && n->n_nhop != 0 &&
10967c478bd9Sstevel@tonic-gate 		    NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
10977c478bd9Sstevel@tonic-gate 			return;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		/*
11007c478bd9Sstevel@tonic-gate 		 * If something has not gone crazy and tried to fill
11017c478bd9Sstevel@tonic-gate 		 * our memory, accept the new route.
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 		rtadd(dst, mask, rt_state, new);
11047c478bd9Sstevel@tonic-gate 		return;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/*
11087c478bd9Sstevel@tonic-gate 	 * We already know about the route.  Consider this update.
11097c478bd9Sstevel@tonic-gate 	 *
11107c478bd9Sstevel@tonic-gate 	 * If (rt->rt_state & RS_NET_SYN), then this route
11117c478bd9Sstevel@tonic-gate 	 * is the same as a network route we have inferred
11127c478bd9Sstevel@tonic-gate 	 * for subnets we know, in order to tell RIPv1 routers
11137c478bd9Sstevel@tonic-gate 	 * about the subnets.
11147c478bd9Sstevel@tonic-gate 	 *
11157c478bd9Sstevel@tonic-gate 	 * It is impossible to tell if the route is coming
11167c478bd9Sstevel@tonic-gate 	 * from a distant RIPv2 router with the standard
11177c478bd9Sstevel@tonic-gate 	 * netmask because that router knows about the entire
11187c478bd9Sstevel@tonic-gate 	 * network, or if it is a round-about echo of a
11197c478bd9Sstevel@tonic-gate 	 * synthetic, RIPv1 network route of our own.
11207c478bd9Sstevel@tonic-gate 	 * The worst is that both kinds of routes might be
11217c478bd9Sstevel@tonic-gate 	 * received, and the bad one might have the smaller
11227c478bd9Sstevel@tonic-gate 	 * metric.  Partly solve this problem by never
11237c478bd9Sstevel@tonic-gate 	 * aggregating into such a route.  Also keep it
11247c478bd9Sstevel@tonic-gate 	 * around as long as the interface exists.
11257c478bd9Sstevel@tonic-gate 	 */
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	rts0 = rt->rt_spares;
11287c478bd9Sstevel@tonic-gate 	for (rts = rts0, i = rt->rt_num_spares; i != 0; i--, rts++) {
11297c478bd9Sstevel@tonic-gate 		if (rts->rts_router == new->rts_router)
11307c478bd9Sstevel@tonic-gate 			break;
11317c478bd9Sstevel@tonic-gate 		/*
11327c478bd9Sstevel@tonic-gate 		 * Note the worst slot to reuse,
11337c478bd9Sstevel@tonic-gate 		 * other than the current slot.
11347c478bd9Sstevel@tonic-gate 		 */
11357c478bd9Sstevel@tonic-gate 		if (BETTER_LINK(rt, rts0, rts))
11367c478bd9Sstevel@tonic-gate 			rts0 = rts;
11377c478bd9Sstevel@tonic-gate 	}
11387c478bd9Sstevel@tonic-gate 	if (i != 0) {
11397c478bd9Sstevel@tonic-gate 		/*
11407c478bd9Sstevel@tonic-gate 		 * Found a route from the router already in the table.
11417c478bd9Sstevel@tonic-gate 		 */
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		/*
11447c478bd9Sstevel@tonic-gate 		 * If the new route is a route broken down from an
11457c478bd9Sstevel@tonic-gate 		 * aggregated route, and if the previous route is either
11467c478bd9Sstevel@tonic-gate 		 * not a broken down route or was broken down from a finer
11477c478bd9Sstevel@tonic-gate 		 * netmask, and if the previous route is current,
11487c478bd9Sstevel@tonic-gate 		 * then forget this one.
11497c478bd9Sstevel@tonic-gate 		 */
11507c478bd9Sstevel@tonic-gate 		if (new->rts_de_ag > rts->rts_de_ag &&
11517c478bd9Sstevel@tonic-gate 		    now_stale <= rts->rts_time)
11527c478bd9Sstevel@tonic-gate 			return;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 		/*
11557c478bd9Sstevel@tonic-gate 		 * Keep poisoned routes around only long enough to pass
11567c478bd9Sstevel@tonic-gate 		 * the poison on.  Use a new timestamp for good routes.
11577c478bd9Sstevel@tonic-gate 		 */
11587c478bd9Sstevel@tonic-gate 		if (rts->rts_metric == HOPCNT_INFINITY &&
11597c478bd9Sstevel@tonic-gate 		    new->rts_metric == HOPCNT_INFINITY)
11607c478bd9Sstevel@tonic-gate 			new->rts_time = rts->rts_time;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		/*
11637c478bd9Sstevel@tonic-gate 		 * If this is an update for the router we currently prefer,
11647c478bd9Sstevel@tonic-gate 		 * then note it.
11657c478bd9Sstevel@tonic-gate 		 */
11667c478bd9Sstevel@tonic-gate 		if (i == rt->rt_num_spares) {
1167*d6b3210dSsowmini 			uint8_t old_metric = rts->rts_metric;
1168*d6b3210dSsowmini 
11697c478bd9Sstevel@tonic-gate 			rtchange(rt, rt->rt_state | rt_state, new, 0);
11707c478bd9Sstevel@tonic-gate 			/*
11717c478bd9Sstevel@tonic-gate 			 * If the route got worse, check for something better.
11727c478bd9Sstevel@tonic-gate 			 */
1173*d6b3210dSsowmini 			if (new->rts_metric != old_metric)
11747c478bd9Sstevel@tonic-gate 				rtswitch(rt, 0);
11757c478bd9Sstevel@tonic-gate 			return;
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/*
11797c478bd9Sstevel@tonic-gate 		 * This is an update for a spare route.
11807c478bd9Sstevel@tonic-gate 		 * Finished if the route is unchanged.
11817c478bd9Sstevel@tonic-gate 		 */
11827c478bd9Sstevel@tonic-gate 		if (rts->rts_gate == new->rts_gate &&
11837c478bd9Sstevel@tonic-gate 		    rts->rts_metric == new->rts_metric &&
11847c478bd9Sstevel@tonic-gate 		    rts->rts_tag == new->rts_tag) {
11857c478bd9Sstevel@tonic-gate 			if ((rt->rt_dst == RIP_DEFAULT) &&
11867c478bd9Sstevel@tonic-gate 			    (rts->rts_ifp != new->rts_ifp))
11877c478bd9Sstevel@tonic-gate 				trace_misc("input_route update for spare");
11887c478bd9Sstevel@tonic-gate 			trace_upslot(rt, rts, new);
11897c478bd9Sstevel@tonic-gate 			*rts = *new;
11907c478bd9Sstevel@tonic-gate 			return;
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 		/*
11947c478bd9Sstevel@tonic-gate 		 * Forget it if it has gone bad.
11957c478bd9Sstevel@tonic-gate 		 */
11967c478bd9Sstevel@tonic-gate 		if (new->rts_metric == HOPCNT_INFINITY) {
11977c478bd9Sstevel@tonic-gate 			rts_delete(rt, rts);
11987c478bd9Sstevel@tonic-gate 			return;
11997c478bd9Sstevel@tonic-gate 		}
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	} else {
12027c478bd9Sstevel@tonic-gate 		/*
12037c478bd9Sstevel@tonic-gate 		 * The update is for a route we know about,
12047c478bd9Sstevel@tonic-gate 		 * but not from a familiar router.
12057c478bd9Sstevel@tonic-gate 		 *
12067c478bd9Sstevel@tonic-gate 		 * Ignore the route if it points to us.
12077c478bd9Sstevel@tonic-gate 		 */
12087c478bd9Sstevel@tonic-gate 		if (n != NULL && n->n_nhop != 0 &&
12097c478bd9Sstevel@tonic-gate 		    NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
12107c478bd9Sstevel@tonic-gate 			return;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		/* the loop above set rts0=worst spare */
12137c478bd9Sstevel@tonic-gate 		if (rts0->rts_metric < HOPCNT_INFINITY) {
12147c478bd9Sstevel@tonic-gate 			ptrsize = (rt->rt_num_spares + SPARE_INC) *
12157c478bd9Sstevel@tonic-gate 			    sizeof (struct rt_spare);
12167c478bd9Sstevel@tonic-gate 			ptr = realloc(rt->rt_spares, ptrsize);
12177c478bd9Sstevel@tonic-gate 			if (ptr != NULL) {
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 				rt->rt_spares = ptr;
12207c478bd9Sstevel@tonic-gate 				rts0 = &rt->rt_spares[rt->rt_num_spares];
12217c478bd9Sstevel@tonic-gate 				(void) memset(rts0, 0,
12227c478bd9Sstevel@tonic-gate 				    SPARE_INC * sizeof (struct rt_spare));
12237c478bd9Sstevel@tonic-gate 				rt->rt_num_spares += SPARE_INC;
12247c478bd9Sstevel@tonic-gate 				for (rts = rts0, i = SPARE_INC;
12257c478bd9Sstevel@tonic-gate 				    i != 0; i--, rts++)
12267c478bd9Sstevel@tonic-gate 					rts->rts_metric = HOPCNT_INFINITY;
12277c478bd9Sstevel@tonic-gate 			}
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		rts = rts0;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		/*
12327c478bd9Sstevel@tonic-gate 		 * Save the route as a spare only if it has
12337c478bd9Sstevel@tonic-gate 		 * a better metric than our worst spare.
12347c478bd9Sstevel@tonic-gate 		 * This also ignores poisoned routes (those
12357c478bd9Sstevel@tonic-gate 		 * received with metric HOPCNT_INFINITY).
12367c478bd9Sstevel@tonic-gate 		 */
12377c478bd9Sstevel@tonic-gate 		if (new->rts_metric >= rts->rts_metric)
12387c478bd9Sstevel@tonic-gate 			return;
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 	trace_upslot(rt, rts, new);
12417c478bd9Sstevel@tonic-gate 	*rts = *new;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/* try to switch to a better route */
12447c478bd9Sstevel@tonic-gate 	rtswitch(rt, rts);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * Recorded information about peer's MD5 sequence numbers.  This is
12497c478bd9Sstevel@tonic-gate  * used to validate that received sequence numbers are in
12507c478bd9Sstevel@tonic-gate  * non-decreasing order as per the RFC.
12517c478bd9Sstevel@tonic-gate  */
12527c478bd9Sstevel@tonic-gate struct peer_hash {
12537c478bd9Sstevel@tonic-gate 	struct peer_hash *ph_next;
12547c478bd9Sstevel@tonic-gate 	in_addr_t ph_addr;
12557c478bd9Sstevel@tonic-gate 	time_t ph_heard;
12567c478bd9Sstevel@tonic-gate 	uint32_t ph_seqno;
12577c478bd9Sstevel@tonic-gate };
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate static struct peer_hash **peer_hashes;
12607c478bd9Sstevel@tonic-gate static int ph_index;
12617c478bd9Sstevel@tonic-gate static int ph_num_peers;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate /*
12647c478bd9Sstevel@tonic-gate  * Get a peer_hash structure from the hash of known peers.  Create a
12657c478bd9Sstevel@tonic-gate  * new one if not found.  Returns NULL on unrecoverable allocation
12667c478bd9Sstevel@tonic-gate  * failure.
12677c478bd9Sstevel@tonic-gate  */
12687c478bd9Sstevel@tonic-gate static struct peer_hash *
12697c478bd9Sstevel@tonic-gate get_peer_info(in_addr_t from)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	struct peer_hash *php;
12727c478bd9Sstevel@tonic-gate 	struct peer_hash *pnhp;
12737c478bd9Sstevel@tonic-gate 	struct peer_hash **ph_pp;
12747c478bd9Sstevel@tonic-gate 	struct peer_hash **ph2_pp;
12757c478bd9Sstevel@tonic-gate 	struct peer_hash **ph3_pp;
12767c478bd9Sstevel@tonic-gate 	int i;
12777c478bd9Sstevel@tonic-gate 	static uint_t failed_count;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (peer_hashes == NULL) {
12807c478bd9Sstevel@tonic-gate 		peer_hashes = calloc(hash_table_sizes[0],
12817c478bd9Sstevel@tonic-gate 		    sizeof (peer_hashes[0]));
12827c478bd9Sstevel@tonic-gate 		if (peer_hashes == NULL) {
12837c478bd9Sstevel@tonic-gate 			if (++failed_count % 100 == 1)
12847c478bd9Sstevel@tonic-gate 				msglog("no memory for peer hash");
12857c478bd9Sstevel@tonic-gate 			return (NULL);
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 	/* Search for peer in existing hash table */
12897c478bd9Sstevel@tonic-gate 	ph_pp = peer_hashes + (from % hash_table_sizes[ph_index]);
12907c478bd9Sstevel@tonic-gate 	for (php = ph_pp[0]; php != NULL; php = php->ph_next) {
12917c478bd9Sstevel@tonic-gate 		if (php->ph_addr == from)
12927c478bd9Sstevel@tonic-gate 			return (php);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 	/*
12957c478bd9Sstevel@tonic-gate 	 * Not found; we need to add this peer to the table.  If there
12967c478bd9Sstevel@tonic-gate 	 * are already too many peers, then try to expand the table
12977c478bd9Sstevel@tonic-gate 	 * first.  It's not a big deal if we can't expand the table
12987c478bd9Sstevel@tonic-gate 	 * right now due to memory constraints.  We'll try again
12997c478bd9Sstevel@tonic-gate 	 * later.
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 	if (ph_num_peers >= hash_table_sizes[ph_index] * 5 &&
13027c478bd9Sstevel@tonic-gate 	    hash_table_sizes[ph_index + 1] != 0 &&
13037c478bd9Sstevel@tonic-gate 	    (ph_pp = calloc(hash_table_sizes[ph_index + 1],
13047c478bd9Sstevel@tonic-gate 		sizeof (peer_hashes[0]))) != NULL) {
13057c478bd9Sstevel@tonic-gate 		ph2_pp = peer_hashes;
13067c478bd9Sstevel@tonic-gate 		for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
13077c478bd9Sstevel@tonic-gate 			for (php = ph2_pp[i]; php != NULL; php = pnhp) {
13087c478bd9Sstevel@tonic-gate 				pnhp = php->ph_next;
13097c478bd9Sstevel@tonic-gate 				ph3_pp = ph_pp + (php->ph_addr %
13107c478bd9Sstevel@tonic-gate 				    hash_table_sizes[ph_index + 1]);
13117c478bd9Sstevel@tonic-gate 				php->ph_next = ph3_pp[0];
13127c478bd9Sstevel@tonic-gate 				ph3_pp[0] = php;
13137c478bd9Sstevel@tonic-gate 			}
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 		ph_index++;
13167c478bd9Sstevel@tonic-gate 		free(peer_hashes);
13177c478bd9Sstevel@tonic-gate 		peer_hashes = ph_pp;
13187c478bd9Sstevel@tonic-gate 		ph_pp += from % hash_table_sizes[ph_index];
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	php = calloc(sizeof (*php), 1);
13217c478bd9Sstevel@tonic-gate 	if (php == NULL) {
13227c478bd9Sstevel@tonic-gate 		if (++failed_count % 100 == 1)
13237c478bd9Sstevel@tonic-gate 			msglog("no memory for peer hash entry");
13247c478bd9Sstevel@tonic-gate 	} else {
13257c478bd9Sstevel@tonic-gate 		php->ph_addr = from;
13267c478bd9Sstevel@tonic-gate 		php->ph_heard = now.tv_sec;
13277c478bd9Sstevel@tonic-gate 		php->ph_next = ph_pp[0];
13287c478bd9Sstevel@tonic-gate 		ph_pp[0] = php;
13297c478bd9Sstevel@tonic-gate 		ph_num_peers++;
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	return (php);
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate /*
13357c478bd9Sstevel@tonic-gate  * Age out entries in the peer table.  This is called every time we do
13367c478bd9Sstevel@tonic-gate  * a normal 30 second broadcast.
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate void
13397c478bd9Sstevel@tonic-gate age_peer_info(void)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	struct peer_hash *php;
13427c478bd9Sstevel@tonic-gate 	struct peer_hash *next_ph;
13437c478bd9Sstevel@tonic-gate 	struct peer_hash *prev_ph;
13447c478bd9Sstevel@tonic-gate 	struct peer_hash **ph_pp;
13457c478bd9Sstevel@tonic-gate 	int i;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * Scan through the list and remove peers that should not
13497c478bd9Sstevel@tonic-gate 	 * still have valid authenticated entries in the routing
13507c478bd9Sstevel@tonic-gate 	 * table.
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	if ((ph_pp = peer_hashes) == NULL || ph_num_peers == 0)
13537c478bd9Sstevel@tonic-gate 		return;
13547c478bd9Sstevel@tonic-gate 	for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
13557c478bd9Sstevel@tonic-gate 		prev_ph = NULL;
13567c478bd9Sstevel@tonic-gate 		for (php = ph_pp[i]; php != NULL; php = next_ph) {
13577c478bd9Sstevel@tonic-gate 			next_ph = php->ph_next;
13587c478bd9Sstevel@tonic-gate 			if (php->ph_heard <= now_expire) {
13597c478bd9Sstevel@tonic-gate 				if (prev_ph == NULL)
13607c478bd9Sstevel@tonic-gate 					ph_pp[i] = next_ph;
13617c478bd9Sstevel@tonic-gate 				else
13627c478bd9Sstevel@tonic-gate 					prev_ph->ph_next = next_ph;
13637c478bd9Sstevel@tonic-gate 				free(php);
13647c478bd9Sstevel@tonic-gate 				if (--ph_num_peers == 0)
13657c478bd9Sstevel@tonic-gate 					return;
13667c478bd9Sstevel@tonic-gate 			} else {
13677c478bd9Sstevel@tonic-gate 				prev_ph = php;
13687c478bd9Sstevel@tonic-gate 			}
13697c478bd9Sstevel@tonic-gate 		}
13707c478bd9Sstevel@tonic-gate 	}
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate static boolean_t		/* _B_FALSE if bad, _B_TRUE if good */
13747c478bd9Sstevel@tonic-gate ck_passwd(struct interface *aifp,
13757c478bd9Sstevel@tonic-gate     struct rip *rip,
13767c478bd9Sstevel@tonic-gate     uint8_t *lim,
13777c478bd9Sstevel@tonic-gate     in_addr_t from,
13787c478bd9Sstevel@tonic-gate     struct msg_limit *use_authp)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate #define	NA (rip->rip_auths)
13817c478bd9Sstevel@tonic-gate 	struct netauth *na2;
13827c478bd9Sstevel@tonic-gate 	struct auth *ap;
13837c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
13847c478bd9Sstevel@tonic-gate 	uchar_t hash[RIP_AUTH_PW_LEN];
13857c478bd9Sstevel@tonic-gate 	int i, len;
13867c478bd9Sstevel@tonic-gate 	struct peer_hash *php;
13877c478bd9Sstevel@tonic-gate 	uint32_t seqno;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if ((uint8_t *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
13907c478bd9Sstevel@tonic-gate 		msglim(use_authp, from, "missing auth data from %s",
13917c478bd9Sstevel@tonic-gate 		    naddr_ntoa(from));
13927c478bd9Sstevel@tonic-gate 		return (_B_FALSE);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	/*
13967c478bd9Sstevel@tonic-gate 	 * Validate sequence number on RIPv2 responses using keyed MD5
13977c478bd9Sstevel@tonic-gate 	 * authentication per RFC 2082 section 3.2.2.  Note that if we
13987c478bd9Sstevel@tonic-gate 	 * can't locate the peer information (due to transient
13997c478bd9Sstevel@tonic-gate 	 * allocation problems), then we don't do the test.  Also note
14007c478bd9Sstevel@tonic-gate 	 * that we assume that all sequence numbers 0x80000000 or more
14017c478bd9Sstevel@tonic-gate 	 * away are "less than."
14027c478bd9Sstevel@tonic-gate 	 *
14037c478bd9Sstevel@tonic-gate 	 * We intentionally violate RFC 2082 with respect to one case:
14047c478bd9Sstevel@tonic-gate 	 * restablishing contact.  The RFC says that you should
14057c478bd9Sstevel@tonic-gate 	 * continue to ignore old sequence numbers in this case but
14067c478bd9Sstevel@tonic-gate 	 * make a special allowance for 0.  This is extremely foolish.
14077c478bd9Sstevel@tonic-gate 	 * The problem is that if the router has crashed, it's
14087c478bd9Sstevel@tonic-gate 	 * entirely possible that either we'll miss sequence zero (or
14097c478bd9Sstevel@tonic-gate 	 * that it might not even send it!) or that the peer doesn't
14107c478bd9Sstevel@tonic-gate 	 * remember what it last used for a sequence number.  In
14117c478bd9Sstevel@tonic-gate 	 * either case, we'll create a failure state that persists
14127c478bd9Sstevel@tonic-gate 	 * until the sequence number happens to advance past the last
14137c478bd9Sstevel@tonic-gate 	 * one we saw.  This is bad because it means that we may have
14147c478bd9Sstevel@tonic-gate 	 * to wait until the router has been up for at least as long
14157c478bd9Sstevel@tonic-gate 	 * as it was last time before we even pay attention to it.
14167c478bd9Sstevel@tonic-gate 	 * Meanwhile, other routers may listen to it if they hadn't
14177c478bd9Sstevel@tonic-gate 	 * seen it before (i.e., if they crashed in the meantime).
14187c478bd9Sstevel@tonic-gate 	 * This means -- perversely -- that stable systems that stay
14197c478bd9Sstevel@tonic-gate 	 * "up" for a long time pay a penalty for doing so.
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	if (rip->rip_cmd == RIPCMD_RESPONSE && NA->a_type == RIP_AUTH_MD5 &&
14227c478bd9Sstevel@tonic-gate 	    (php = get_peer_info(from)) != NULL) {
14237c478bd9Sstevel@tonic-gate 		/*
14247c478bd9Sstevel@tonic-gate 		 * If the entry that we find has been updated
14257c478bd9Sstevel@tonic-gate 		 * recently enough that the routes are known
14267c478bd9Sstevel@tonic-gate 		 * to still be good, but the sequence number
14277c478bd9Sstevel@tonic-gate 		 * looks bad, then discard the packet.
14287c478bd9Sstevel@tonic-gate 		 */
14297c478bd9Sstevel@tonic-gate 		seqno = ntohl(NA->au.a_md5.md5_seqno);
14307c478bd9Sstevel@tonic-gate 		if (php->ph_heard > now_expire && php->ph_seqno != 0 &&
14317c478bd9Sstevel@tonic-gate 		    (seqno == 0 || ((seqno - php->ph_seqno) & 0x80000000ul))) {
14327c478bd9Sstevel@tonic-gate 			msglim(use_authp, from,
14337c478bd9Sstevel@tonic-gate 			    "discarding sequence %x (older than %x)",
14347c478bd9Sstevel@tonic-gate 			    (unsigned)seqno, (unsigned)php->ph_seqno);
14357c478bd9Sstevel@tonic-gate 			return (_B_FALSE);
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 		php->ph_heard = now.tv_sec;
14387c478bd9Sstevel@tonic-gate 		php->ph_seqno = seqno;
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	/*
14427c478bd9Sstevel@tonic-gate 	 * accept any current (+/- 24 hours) password
14437c478bd9Sstevel@tonic-gate 	 */
14447c478bd9Sstevel@tonic-gate 	for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
14457c478bd9Sstevel@tonic-gate 		if (ap->type != NA->a_type ||
14467c478bd9Sstevel@tonic-gate 		    (ulong_t)ap->start > (ulong_t)clk.tv_sec+DAY ||
14477c478bd9Sstevel@tonic-gate 		    (ulong_t)ap->end+DAY < (ulong_t)clk.tv_sec)
14487c478bd9Sstevel@tonic-gate 			continue;
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 		if (NA->a_type == RIP_AUTH_PW) {
14517c478bd9Sstevel@tonic-gate 			if (0 == memcmp(NA->au.au_pw, ap->key, RIP_AUTH_PW_LEN))
14527c478bd9Sstevel@tonic-gate 				return (_B_TRUE);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 		} else {
14557c478bd9Sstevel@tonic-gate 			/*
14567c478bd9Sstevel@tonic-gate 			 * accept MD5 secret with the right key ID
14577c478bd9Sstevel@tonic-gate 			 */
14587c478bd9Sstevel@tonic-gate 			if (NA->au.a_md5.md5_keyid != ap->keyid)
14597c478bd9Sstevel@tonic-gate 				continue;
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 			len = ntohs(NA->au.a_md5.md5_pkt_len);
14627c478bd9Sstevel@tonic-gate 			if ((len - sizeof (*rip)) % sizeof (*NA) != 0 ||
14637c478bd9Sstevel@tonic-gate 			    len > (lim - (uint8_t *)rip - sizeof (*NA))) {
14647c478bd9Sstevel@tonic-gate 				msglim(use_authp, from,
14657c478bd9Sstevel@tonic-gate 				    "wrong MD5 RIPv2 packet length of %d"
14667c478bd9Sstevel@tonic-gate 				    " instead of %d from %s",
14677c478bd9Sstevel@tonic-gate 				    len, lim - (uint8_t *)rip - sizeof (*NA),
14687c478bd9Sstevel@tonic-gate 				    naddr_ntoa(from));
14697c478bd9Sstevel@tonic-gate 				return (_B_FALSE);
14707c478bd9Sstevel@tonic-gate 			}
14717c478bd9Sstevel@tonic-gate 			na2 = (struct netauth *)(rip->rip_nets +
14727c478bd9Sstevel@tonic-gate 			    (len - 4) / sizeof (struct netinfo));
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 			/*
14757c478bd9Sstevel@tonic-gate 			 * Given a good hash value, these are not security
14767c478bd9Sstevel@tonic-gate 			 * problems so be generous and accept the routes,
14777c478bd9Sstevel@tonic-gate 			 * after complaining.
14787c478bd9Sstevel@tonic-gate 			 */
14797c478bd9Sstevel@tonic-gate 			if (TRACEPACKETS) {
14807c478bd9Sstevel@tonic-gate 				if (NA->au.a_md5.md5_auth_len !=
14817c478bd9Sstevel@tonic-gate 				    RIP_AUTH_MD5_LEN)
14827c478bd9Sstevel@tonic-gate 					msglim(use_authp, from,
14837c478bd9Sstevel@tonic-gate 					    "unknown MD5 RIPv2 auth len %#x"
14847c478bd9Sstevel@tonic-gate 					    " instead of %#x from %s",
14857c478bd9Sstevel@tonic-gate 					    NA->au.a_md5.md5_auth_len,
14867c478bd9Sstevel@tonic-gate 					    RIP_AUTH_MD5_LEN,
14877c478bd9Sstevel@tonic-gate 					    naddr_ntoa(from));
14887c478bd9Sstevel@tonic-gate 				if (na2->a_family != RIP_AF_AUTH)
14897c478bd9Sstevel@tonic-gate 					msglim(use_authp, from,
14907c478bd9Sstevel@tonic-gate 					    "unknown MD5 RIPv2 family %#x"
14917c478bd9Sstevel@tonic-gate 					    " instead of %#x from %s",
14927c478bd9Sstevel@tonic-gate 					    na2->a_family, RIP_AF_AUTH,
14937c478bd9Sstevel@tonic-gate 					    naddr_ntoa(from));
14947c478bd9Sstevel@tonic-gate 				if (na2->a_type != RIP_AUTH_TRAILER)
14957c478bd9Sstevel@tonic-gate 					msglim(use_authp, from,
14967c478bd9Sstevel@tonic-gate 					    "MD5 RIPv2 hash has %#x"
14977c478bd9Sstevel@tonic-gate 					    " instead of %#x from %s",
14987c478bd9Sstevel@tonic-gate 					    ntohs(na2->a_type),
14997c478bd9Sstevel@tonic-gate 					    ntohs(RIP_AUTH_TRAILER),
15007c478bd9Sstevel@tonic-gate 					    naddr_ntoa(from));
15017c478bd9Sstevel@tonic-gate 			}
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 			MD5Init(&md5_ctx);
15047c478bd9Sstevel@tonic-gate 			/*
15057c478bd9Sstevel@tonic-gate 			 * len+4 to include auth trailer's family/type in
15067c478bd9Sstevel@tonic-gate 			 * MD5 sum
15077c478bd9Sstevel@tonic-gate 			 */
15087c478bd9Sstevel@tonic-gate 			MD5Update(&md5_ctx, (uchar_t *)rip, len + 4);
15097c478bd9Sstevel@tonic-gate 			MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
15107c478bd9Sstevel@tonic-gate 			MD5Final(hash, &md5_ctx);
15117c478bd9Sstevel@tonic-gate 			if (0 == memcmp(hash, na2->au.au_pw, sizeof (hash)))
15127c478bd9Sstevel@tonic-gate 				return (_B_TRUE);
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 	}
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	msglim(use_authp, from, "bad auth data from %s",
15177c478bd9Sstevel@tonic-gate 	    naddr_ntoa(from));
15187c478bd9Sstevel@tonic-gate 	return (_B_FALSE);
15197c478bd9Sstevel@tonic-gate #undef NA
15207c478bd9Sstevel@tonic-gate }
1521