xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.routed/input.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * Copyright (c) 1983, 1988, 1993
6*0Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
12*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
13*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
14*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
15*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
16*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
17*0Sstevel@tonic-gate  *    must display the following acknowledgment:
18*0Sstevel@tonic-gate  *	This product includes software developed by the University of
19*0Sstevel@tonic-gate  *	California, Berkeley and its contributors.
20*0Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
21*0Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
22*0Sstevel@tonic-gate  *    without specific prior written permission.
23*0Sstevel@tonic-gate  *
24*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*0Sstevel@tonic-gate  * SUCH DAMAGE.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * $FreeBSD: src/sbin/routed/input.c,v 1.9 2001/06/06 20:52:30 phk Exp $
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "defs.h"
42*0Sstevel@tonic-gate #include <md5.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * The size of the control buffer passed to recvmsg() used to receive
46*0Sstevel@tonic-gate  * ancillary data.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate #define	CONTROL_BUFSIZE	1024
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static void input(struct sockaddr_in *, struct interface *, struct rip *, int);
51*0Sstevel@tonic-gate static boolean_t ck_passwd(struct interface *, struct rip *, uint8_t *,
52*0Sstevel@tonic-gate     in_addr_t, struct msg_limit *);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  * Find the interface which received the given message.
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate struct interface *
59*0Sstevel@tonic-gate receiving_interface(struct msghdr *msg, boolean_t findremote)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	struct interface *ifp, *ifp1, *ifp2;
62*0Sstevel@tonic-gate 	struct sockaddr_in *from;
63*0Sstevel@tonic-gate 	void *opt;
64*0Sstevel@tonic-gate 	uint_t ifindex;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	from = (struct sockaddr_in *)msg->msg_name;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	/* First see if this packet came from a remote gateway. */
69*0Sstevel@tonic-gate 	if (findremote && ((ifp = findremoteif(from->sin_addr.s_addr)) != NULL))
70*0Sstevel@tonic-gate 		return (ifp);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	/*
73*0Sstevel@tonic-gate 	 * It did not come from a remote gateway.  Determine which
74*0Sstevel@tonic-gate 	 * physical interface this packet was received on by
75*0Sstevel@tonic-gate 	 * processing the message's ancillary data to find the
76*0Sstevel@tonic-gate 	 * IP_RECVIF option we requested.
77*0Sstevel@tonic-gate 	 */
78*0Sstevel@tonic-gate 	if ((opt = find_ancillary(msg, IP_RECVIF)) == NULL) {
79*0Sstevel@tonic-gate 		msglog("unable to retrieve IP_RECVIF");
80*0Sstevel@tonic-gate 	} else {
81*0Sstevel@tonic-gate 		ifindex = *(uint_t *)opt;
82*0Sstevel@tonic-gate 		if ((ifp = ifwithindex(ifindex, _B_TRUE)) != NULL) {
83*0Sstevel@tonic-gate 			/* Find the best match of the aliases */
84*0Sstevel@tonic-gate 			ifp2 = NULL;
85*0Sstevel@tonic-gate 			for (ifp1 = ifp; ifp1 != NULL;
86*0Sstevel@tonic-gate 			    ifp1 = ifp1->int_ilist.hl_next) {
87*0Sstevel@tonic-gate 				if (ifp1->int_addr == from->sin_addr.s_addr)
88*0Sstevel@tonic-gate 					return (ifp1);
89*0Sstevel@tonic-gate 				if ((ifp2 == NULL ||
90*0Sstevel@tonic-gate 					(ifp2->int_state & IS_ALIAS)) &&
91*0Sstevel@tonic-gate 				    on_net(from->sin_addr.s_addr, ifp1->int_net,
92*0Sstevel@tonic-gate 					ifp1->int_mask))
93*0Sstevel@tonic-gate 					ifp2 = ifp1;
94*0Sstevel@tonic-gate 			}
95*0Sstevel@tonic-gate 			if (ifp2 != NULL)
96*0Sstevel@tonic-gate 				ifp = ifp2;
97*0Sstevel@tonic-gate 			return (ifp);
98*0Sstevel@tonic-gate 		}
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/*
102*0Sstevel@tonic-gate 	 * As a last resort (for some reason, ip didn't give us the
103*0Sstevel@tonic-gate 	 * IP_RECVIF index we requested), try to deduce the receiving
104*0Sstevel@tonic-gate 	 * interface based on the source address of the packet.
105*0Sstevel@tonic-gate 	 */
106*0Sstevel@tonic-gate 	return (iflookup(from->sin_addr.s_addr));
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Process RIP input on rip_sock.  Returns 0 for success, -1 for failure.
111*0Sstevel@tonic-gate  */
112*0Sstevel@tonic-gate int
113*0Sstevel@tonic-gate read_rip()
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	struct sockaddr_in from;
116*0Sstevel@tonic-gate 	struct interface *ifp;
117*0Sstevel@tonic-gate 	int cc;
118*0Sstevel@tonic-gate 	union pkt_buf inbuf;
119*0Sstevel@tonic-gate 	struct msghdr msg;
120*0Sstevel@tonic-gate 	struct iovec iov;
121*0Sstevel@tonic-gate 	uint8_t ancillary_data[CONTROL_BUFSIZE];
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	iov.iov_base = &inbuf;
124*0Sstevel@tonic-gate 	iov.iov_len = sizeof (inbuf);
125*0Sstevel@tonic-gate 	msg.msg_iov = &iov;
126*0Sstevel@tonic-gate 	msg.msg_iovlen = 1;
127*0Sstevel@tonic-gate 	msg.msg_name = &from;
128*0Sstevel@tonic-gate 	msg.msg_control = &ancillary_data;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	for (;;) {
131*0Sstevel@tonic-gate 		msg.msg_namelen = sizeof (from);
132*0Sstevel@tonic-gate 		msg.msg_controllen = sizeof (ancillary_data);
133*0Sstevel@tonic-gate 		cc = recvmsg(rip_sock, &msg, 0);
134*0Sstevel@tonic-gate 		if (cc == 0)
135*0Sstevel@tonic-gate 			return (-1);
136*0Sstevel@tonic-gate 		if (cc < 0) {
137*0Sstevel@tonic-gate 			if (errno == EWOULDBLOCK || errno == EINTR)
138*0Sstevel@tonic-gate 				return (0);
139*0Sstevel@tonic-gate 			LOGERR("recvmsg(rip_sock)");
140*0Sstevel@tonic-gate 			return (-1);
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		/*
144*0Sstevel@tonic-gate 		 * ifp is the interface via which the packet arrived.
145*0Sstevel@tonic-gate 		 */
146*0Sstevel@tonic-gate 		ifp = receiving_interface(&msg, _B_TRUE);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		input(&from, ifp, &inbuf.rip, cc);
149*0Sstevel@tonic-gate 	}
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /* Process a RIP packet */
154*0Sstevel@tonic-gate static void
155*0Sstevel@tonic-gate input(struct sockaddr_in *from,		/* received from this IP address */
156*0Sstevel@tonic-gate     struct interface *ifp,		/* interface of incoming socket */
157*0Sstevel@tonic-gate     struct rip *rip,
158*0Sstevel@tonic-gate     int cc)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate #define	FROM_NADDR from->sin_addr.s_addr
161*0Sstevel@tonic-gate 	static struct msg_limit use_auth, bad_len, bad_mask;
162*0Sstevel@tonic-gate 	static struct msg_limit unk_router, bad_router, bad_nhop;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	struct rt_entry *rt;
165*0Sstevel@tonic-gate 	struct rt_spare new;
166*0Sstevel@tonic-gate 	struct netinfo *n, *lim;
167*0Sstevel@tonic-gate 	struct interface *ifp1;
168*0Sstevel@tonic-gate 	in_addr_t gate, mask, v1_mask, dst, ddst_h = 0;
169*0Sstevel@tonic-gate 	struct auth *ap;
170*0Sstevel@tonic-gate 	struct tgate *tg = NULL;
171*0Sstevel@tonic-gate 	struct tgate_net *tn;
172*0Sstevel@tonic-gate 	int i, j;
173*0Sstevel@tonic-gate 	boolean_t poll_answer = _B_FALSE; /* Set to _B_TRUE if RIPCMD_POLL */
174*0Sstevel@tonic-gate 	uint16_t rt_state = 0;	/* Extra route state to pass to input_route() */
175*0Sstevel@tonic-gate 	uint8_t metric;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	(void) memset(&new, 0, sizeof (new));
178*0Sstevel@tonic-gate 	/* Notice when we hear from a remote gateway */
179*0Sstevel@tonic-gate 	if (ifp != NULL && (ifp->int_state & IS_REMOTE))
180*0Sstevel@tonic-gate 		ifp->int_act_time = now.tv_sec;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	trace_rip("Recv", "from", from, ifp, rip, cc);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if (ifp != NULL && (ifp->int_if_flags & IFF_NORTEXCH)) {
185*0Sstevel@tonic-gate 		trace_misc("discard RIP packet received over %s (IFF_NORTEXCH)",
186*0Sstevel@tonic-gate 		    ifp->int_name);
187*0Sstevel@tonic-gate 		return;
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	gate = ntohl(FROM_NADDR);
191*0Sstevel@tonic-gate 	if (IN_EXPERIMENTAL(gate) || (gate >> IN_CLASSA_NSHIFT) == 0) {
192*0Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR, "source address %s unusable",
193*0Sstevel@tonic-gate 		    naddr_ntoa(FROM_NADDR));
194*0Sstevel@tonic-gate 		return;
195*0Sstevel@tonic-gate 	}
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 	if (rip->rip_vers == 0) {
198*0Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
199*0Sstevel@tonic-gate 		    "RIP version 0, cmd %d, packet received from %s",
200*0Sstevel@tonic-gate 		    rip->rip_cmd, naddr_ntoa(FROM_NADDR));
201*0Sstevel@tonic-gate 		return;
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if (rip->rip_vers > RIPv2) {
205*0Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
206*0Sstevel@tonic-gate 		    "Treating RIP version %d packet received from %s as "
207*0Sstevel@tonic-gate 		    "version %d", rip->rip_vers, naddr_ntoa(FROM_NADDR),
208*0Sstevel@tonic-gate 		    RIPv2);
209*0Sstevel@tonic-gate 		rip->rip_vers = RIPv2;
210*0Sstevel@tonic-gate 	}
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	if (cc > (int)OVER_MAXPACKETSIZE) {
213*0Sstevel@tonic-gate 		msglim(&bad_router, FROM_NADDR,
214*0Sstevel@tonic-gate 		    "packet at least %d bytes too long received from %s",
215*0Sstevel@tonic-gate 		    cc-MAXPACKETSIZE, naddr_ntoa(FROM_NADDR));
216*0Sstevel@tonic-gate 	}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	n = rip->rip_nets;
219*0Sstevel@tonic-gate 	lim = n + (cc - 4) / sizeof (struct netinfo);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	/*
222*0Sstevel@tonic-gate 	 * Notice authentication.
223*0Sstevel@tonic-gate 	 * As required by section 5.2 of RFC 2453, discard authenticated
224*0Sstevel@tonic-gate 	 * RIPv2 messages, but only if configured for that silliness.
225*0Sstevel@tonic-gate 	 *
226*0Sstevel@tonic-gate 	 * RIPv2 authentication is lame.  Why authenticate queries?
227*0Sstevel@tonic-gate 	 * Why should a RIPv2 implementation with authentication disabled
228*0Sstevel@tonic-gate 	 * not be able to listen to RIPv2 packets with authentication, while
229*0Sstevel@tonic-gate 	 * RIPv1 systems will listen?  Crazy!
230*0Sstevel@tonic-gate 	 */
231*0Sstevel@tonic-gate 	if (!auth_ok && rip->rip_vers == RIPv2 && n < lim &&
232*0Sstevel@tonic-gate 	    n->n_family == RIP_AF_AUTH) {
233*0Sstevel@tonic-gate 		msglim(&use_auth, FROM_NADDR,
234*0Sstevel@tonic-gate 		    "RIPv2 message with authentication from %s discarded",
235*0Sstevel@tonic-gate 		    naddr_ntoa(FROM_NADDR));
236*0Sstevel@tonic-gate 		return;
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	switch (rip->rip_cmd) {
240*0Sstevel@tonic-gate 	case RIPCMD_POLL:
241*0Sstevel@tonic-gate 		/*
242*0Sstevel@tonic-gate 		 * Similar to RIPCMD_REQUEST, this command is used to
243*0Sstevel@tonic-gate 		 * request either a full-table or a set of entries.  Both
244*0Sstevel@tonic-gate 		 * silent processes and routers can respond to this
245*0Sstevel@tonic-gate 		 * command.
246*0Sstevel@tonic-gate 		 */
247*0Sstevel@tonic-gate 		poll_answer = _B_TRUE;
248*0Sstevel@tonic-gate 		/* FALLTHRU */
249*0Sstevel@tonic-gate 	case RIPCMD_REQUEST:
250*0Sstevel@tonic-gate 		/* Are we talking to ourself or a remote gateway? */
251*0Sstevel@tonic-gate 		ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
252*0Sstevel@tonic-gate 		if (ifp1 != NULL) {
253*0Sstevel@tonic-gate 			if (ifp1->int_state & IS_REMOTE) {
254*0Sstevel@tonic-gate 				/* remote gateway */
255*0Sstevel@tonic-gate 				ifp = ifp1;
256*0Sstevel@tonic-gate 				if (check_remote(ifp)) {
257*0Sstevel@tonic-gate 					ifp->int_act_time = now.tv_sec;
258*0Sstevel@tonic-gate 					if_ok(ifp, "remote ", _B_FALSE);
259*0Sstevel@tonic-gate 				}
260*0Sstevel@tonic-gate 			} else if (from->sin_port == htons(RIP_PORT)) {
261*0Sstevel@tonic-gate 				trace_pkt("    discard our own RIP request");
262*0Sstevel@tonic-gate 				return;
263*0Sstevel@tonic-gate 			}
264*0Sstevel@tonic-gate 		}
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 		/* did the request come from a router? */
267*0Sstevel@tonic-gate 		if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
268*0Sstevel@tonic-gate 			/*
269*0Sstevel@tonic-gate 			 * yes, ignore the request if RIP is off so that
270*0Sstevel@tonic-gate 			 * the router does not depend on us.
271*0Sstevel@tonic-gate 			 */
272*0Sstevel@tonic-gate 			if (ripout_interfaces == 0 ||
273*0Sstevel@tonic-gate 			    (ifp != NULL && (IS_RIP_OUT_OFF(ifp->int_state) ||
274*0Sstevel@tonic-gate 			    !IS_IFF_ROUTING(ifp->int_if_flags)))) {
275*0Sstevel@tonic-gate 				trace_pkt("    discard request while RIP off");
276*0Sstevel@tonic-gate 				return;
277*0Sstevel@tonic-gate 			}
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 		/*
281*0Sstevel@tonic-gate 		 * According to RFC 2453 section 5.2, we should ignore
282*0Sstevel@tonic-gate 		 * unauthenticated queries when authentication is
283*0Sstevel@tonic-gate 		 * configured.  That is too silly to bother with.  Sheesh!
284*0Sstevel@tonic-gate 		 * Are forwarding tables supposed to be secret even though
285*0Sstevel@tonic-gate 		 * a bad guy can infer them with test traffic?  RIP is
286*0Sstevel@tonic-gate 		 * still the most common router-discovery protocol, so
287*0Sstevel@tonic-gate 		 * hosts need to send queries that will be answered.  What
288*0Sstevel@tonic-gate 		 * about `rtquery`?  Maybe on firewalls you'd care, but not
289*0Sstevel@tonic-gate 		 * enough to give up the diagnostic facilities of remote
290*0Sstevel@tonic-gate 		 * probing.
291*0Sstevel@tonic-gate 		 */
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		if (n >= lim) {
294*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty request from %s",
295*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
296*0Sstevel@tonic-gate 			return;
297*0Sstevel@tonic-gate 		}
298*0Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
299*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
300*0Sstevel@tonic-gate 			    "request of bad length (%d) from %s",
301*0Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		if (rip->rip_vers == RIPv2 && (ifp == NULL ||
305*0Sstevel@tonic-gate 		    (ifp->int_state & IS_NO_RIPV1_OUT))) {
306*0Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv2;
307*0Sstevel@tonic-gate 			/*
308*0Sstevel@tonic-gate 			 * If we have a secret but it is a cleartext secret,
309*0Sstevel@tonic-gate 			 * do not disclose our secret unless the other guy
310*0Sstevel@tonic-gate 			 * already knows it.
311*0Sstevel@tonic-gate 			 */
312*0Sstevel@tonic-gate 			ap = find_auth(ifp);
313*0Sstevel@tonic-gate 			if (ap != NULL &&
314*0Sstevel@tonic-gate 			    (ulong_t)ap->end < (ulong_t)clk.tv_sec) {
315*0Sstevel@tonic-gate 				/*
316*0Sstevel@tonic-gate 				 * Don't authenticate incoming packets
317*0Sstevel@tonic-gate 				 * using an expired key.
318*0Sstevel@tonic-gate 				 */
319*0Sstevel@tonic-gate 				msglim(&use_auth, FROM_NADDR,
320*0Sstevel@tonic-gate 				    "%s attempting to authenticate using "
321*0Sstevel@tonic-gate 				    "an expired password.",
322*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
323*0Sstevel@tonic-gate 				ap = NULL;
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 			if (ap != NULL && ap->type == RIP_AUTH_PW &&
326*0Sstevel@tonic-gate 			    (n->n_family != RIP_AF_AUTH ||
327*0Sstevel@tonic-gate 			    !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR,
328*0Sstevel@tonic-gate 			    &use_auth)))
329*0Sstevel@tonic-gate 				ap = NULL;
330*0Sstevel@tonic-gate 		} else {
331*0Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv1;
332*0Sstevel@tonic-gate 			ap = NULL;
333*0Sstevel@tonic-gate 		}
334*0Sstevel@tonic-gate 		clr_ws_buf(&v12buf, ap);
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 		do {
337*0Sstevel@tonic-gate 			n->n_metric = ntohl(n->n_metric);
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 			/*
340*0Sstevel@tonic-gate 			 * A single entry with family RIP_AF_UNSPEC and
341*0Sstevel@tonic-gate 			 * metric HOPCNT_INFINITY means "all routes".
342*0Sstevel@tonic-gate 			 * We respond to routers only if we are acting
343*0Sstevel@tonic-gate 			 * as a supplier, or to anyone other than a router
344*0Sstevel@tonic-gate 			 * (i.e. a query).
345*0Sstevel@tonic-gate 			 */
346*0Sstevel@tonic-gate 			if (n->n_family == RIP_AF_UNSPEC &&
347*0Sstevel@tonic-gate 			    n->n_metric == HOPCNT_INFINITY) {
348*0Sstevel@tonic-gate 				/*
349*0Sstevel@tonic-gate 				 * Answer a full-table query from a utility
350*0Sstevel@tonic-gate 				 * program with all we know.
351*0Sstevel@tonic-gate 				 */
352*0Sstevel@tonic-gate 				if (poll_answer ||
353*0Sstevel@tonic-gate 				    (from->sin_port != htons(RIP_PORT))) {
354*0Sstevel@tonic-gate 					supply(from, ifp, OUT_QUERY, 0,
355*0Sstevel@tonic-gate 					    rip->rip_vers, ap != NULL);
356*0Sstevel@tonic-gate 					return;
357*0Sstevel@tonic-gate 				}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 				/*
360*0Sstevel@tonic-gate 				 * A router is trying to prime its tables.
361*0Sstevel@tonic-gate 				 * Filter the answer in the same way
362*0Sstevel@tonic-gate 				 * broadcasts are filtered.
363*0Sstevel@tonic-gate 				 *
364*0Sstevel@tonic-gate 				 * Only answer a router if we are a supplier
365*0Sstevel@tonic-gate 				 * to keep an unwary host that is just starting
366*0Sstevel@tonic-gate 				 * from picking us as a router.
367*0Sstevel@tonic-gate 				 */
368*0Sstevel@tonic-gate 				if (ifp == NULL) {
369*0Sstevel@tonic-gate 					trace_pkt("ignore distant router");
370*0Sstevel@tonic-gate 					return;
371*0Sstevel@tonic-gate 				}
372*0Sstevel@tonic-gate 				if (IS_RIP_OFF(ifp->int_state) ||
373*0Sstevel@tonic-gate 				    !should_supply(ifp)) {
374*0Sstevel@tonic-gate 					trace_pkt("ignore; not supplying");
375*0Sstevel@tonic-gate 					return;
376*0Sstevel@tonic-gate 				}
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 				/*
379*0Sstevel@tonic-gate 				 * Do not answer a RIPv1 router if
380*0Sstevel@tonic-gate 				 * we are sending RIPv2.  But do offer
381*0Sstevel@tonic-gate 				 * poor man's router discovery.
382*0Sstevel@tonic-gate 				 */
383*0Sstevel@tonic-gate 				if ((ifp->int_state & IS_NO_RIPV1_OUT) &&
384*0Sstevel@tonic-gate 				    rip->rip_vers == RIPv1) {
385*0Sstevel@tonic-gate 					if (!(ifp->int_state & IS_PM_RDISC)) {
386*0Sstevel@tonic-gate 					    trace_pkt("ignore; sending RIPv2");
387*0Sstevel@tonic-gate 					    return;
388*0Sstevel@tonic-gate 					}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 					v12buf.n->n_family = RIP_AF_INET;
391*0Sstevel@tonic-gate 					v12buf.n->n_dst = RIP_DEFAULT;
392*0Sstevel@tonic-gate 					metric = ifp->int_d_metric;
393*0Sstevel@tonic-gate 					if (NULL !=
394*0Sstevel@tonic-gate 					    (rt = rtget(RIP_DEFAULT, 0)))
395*0Sstevel@tonic-gate 						metric = MIN(metric,
396*0Sstevel@tonic-gate 						    (rt->rt_metric + 1));
397*0Sstevel@tonic-gate 					v12buf.n->n_metric = htonl(metric);
398*0Sstevel@tonic-gate 					v12buf.n++;
399*0Sstevel@tonic-gate 					break;
400*0Sstevel@tonic-gate 				}
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 				/*
403*0Sstevel@tonic-gate 				 * Respond with RIPv1 instead of RIPv2 if
404*0Sstevel@tonic-gate 				 * that is what we are broadcasting on the
405*0Sstevel@tonic-gate 				 * interface to keep the remote router from
406*0Sstevel@tonic-gate 				 * getting the wrong initial idea of the
407*0Sstevel@tonic-gate 				 * routes we send.
408*0Sstevel@tonic-gate 				 */
409*0Sstevel@tonic-gate 				supply(from, ifp, OUT_UNICAST, 0,
410*0Sstevel@tonic-gate 				    (ifp->int_state & IS_NO_RIPV1_OUT)
411*0Sstevel@tonic-gate 				    ? RIPv2 : RIPv1,
412*0Sstevel@tonic-gate 				    ap != NULL);
413*0Sstevel@tonic-gate 				return;
414*0Sstevel@tonic-gate 			}
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 			/* Ignore authentication */
417*0Sstevel@tonic-gate 			if (n->n_family == RIP_AF_AUTH)
418*0Sstevel@tonic-gate 				continue;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 			if (n->n_family != RIP_AF_INET) {
421*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
422*0Sstevel@tonic-gate 				    "request from %s for unsupported"
423*0Sstevel@tonic-gate 				    " (af %d) %s",
424*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR),
425*0Sstevel@tonic-gate 				    ntohs(n->n_family),
426*0Sstevel@tonic-gate 				    naddr_ntoa(n->n_dst));
427*0Sstevel@tonic-gate 				return;
428*0Sstevel@tonic-gate 			}
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 			/* We are being asked about a specific destination. */
431*0Sstevel@tonic-gate 			v12buf.n->n_dst = dst = n->n_dst;
432*0Sstevel@tonic-gate 			v12buf.n->n_family = RIP_AF_INET;
433*0Sstevel@tonic-gate 			if (!check_dst(dst)) {
434*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
435*0Sstevel@tonic-gate 				    "bad queried destination %s from %s",
436*0Sstevel@tonic-gate 				    naddr_ntoa(dst),
437*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
438*0Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
439*0Sstevel@tonic-gate 				goto rte_done;
440*0Sstevel@tonic-gate 			}
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 			/* decide what mask was intended */
443*0Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1 ||
444*0Sstevel@tonic-gate 			    0 == (mask = ntohl(n->n_mask)) ||
445*0Sstevel@tonic-gate 			    0 != (ntohl(dst) & ~mask))
446*0Sstevel@tonic-gate 				mask = ripv1_mask_host(dst, ifp);
447*0Sstevel@tonic-gate 
448*0Sstevel@tonic-gate 			/*
449*0Sstevel@tonic-gate 			 * Try to find the answer.  If we don't have an
450*0Sstevel@tonic-gate 			 * explicit route for the destination, use the best
451*0Sstevel@tonic-gate 			 * route to the destination.
452*0Sstevel@tonic-gate 			 */
453*0Sstevel@tonic-gate 			rt = rtget(dst, mask);
454*0Sstevel@tonic-gate 			if (rt == NULL && dst != RIP_DEFAULT)
455*0Sstevel@tonic-gate 				rt = rtfind(n->n_dst);
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 			if (v12buf.buf->rip_vers != RIPv1)
458*0Sstevel@tonic-gate 				v12buf.n->n_mask = htonl(mask);
459*0Sstevel@tonic-gate 			if (rt == NULL) {
460*0Sstevel@tonic-gate 				/* we do not have the answer */
461*0Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
462*0Sstevel@tonic-gate 				goto rte_done;
463*0Sstevel@tonic-gate 			}
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 			/*
466*0Sstevel@tonic-gate 			 * we have the answer, so compute the right metric
467*0Sstevel@tonic-gate 			 * and next hop.
468*0Sstevel@tonic-gate 			 */
469*0Sstevel@tonic-gate 			v12buf.n->n_metric = rt->rt_metric + 1;
470*0Sstevel@tonic-gate 			if (v12buf.n->n_metric > HOPCNT_INFINITY)
471*0Sstevel@tonic-gate 				v12buf.n->n_metric = HOPCNT_INFINITY;
472*0Sstevel@tonic-gate 			if (v12buf.buf->rip_vers != RIPv1) {
473*0Sstevel@tonic-gate 				v12buf.n->n_tag = rt->rt_tag;
474*0Sstevel@tonic-gate 				if (ifp != NULL &&
475*0Sstevel@tonic-gate 				    on_net(rt->rt_gate, ifp->int_net,
476*0Sstevel@tonic-gate 				    ifp->int_mask) &&
477*0Sstevel@tonic-gate 				    rt->rt_gate != ifp->int_addr)
478*0Sstevel@tonic-gate 					v12buf.n->n_nhop = rt->rt_gate;
479*0Sstevel@tonic-gate 			}
480*0Sstevel@tonic-gate rte_done:
481*0Sstevel@tonic-gate 			v12buf.n->n_metric = htonl(v12buf.n->n_metric);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 			/*
484*0Sstevel@tonic-gate 			 * Stop paying attention if we fill the output buffer.
485*0Sstevel@tonic-gate 			 */
486*0Sstevel@tonic-gate 			if (++v12buf.n >= v12buf.lim)
487*0Sstevel@tonic-gate 				break;
488*0Sstevel@tonic-gate 		} while (++n < lim);
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 		/*
491*0Sstevel@tonic-gate 		 * If our response is authenticated with md5, complete the
492*0Sstevel@tonic-gate 		 * md5 computation.
493*0Sstevel@tonic-gate 		 */
494*0Sstevel@tonic-gate 		if (ap != NULL && ap->type == RIP_AUTH_MD5)
495*0Sstevel@tonic-gate 			end_md5_auth(&v12buf, ap);
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 		/*
498*0Sstevel@tonic-gate 		 * Diagnostic programs make specific requests
499*0Sstevel@tonic-gate 		 * from ports other than 520.  Log other types
500*0Sstevel@tonic-gate 		 * of specific requests as suspicious.
501*0Sstevel@tonic-gate 		 */
502*0Sstevel@tonic-gate 		if (!poll_answer && (from->sin_port == htons(RIP_PORT))) {
503*0Sstevel@tonic-gate 			writelog(LOG_WARNING,
504*0Sstevel@tonic-gate 			    "Received suspicious request from %s port %d",
505*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR), RIP_PORT);
506*0Sstevel@tonic-gate 		}
507*0Sstevel@tonic-gate 		if (poll_answer || (from->sin_port != htons(RIP_PORT))) {
508*0Sstevel@tonic-gate 			/* query */
509*0Sstevel@tonic-gate 			(void) output(OUT_QUERY, from, ifp, v12buf.buf,
510*0Sstevel@tonic-gate 			    ((char *)v12buf.n - (char *)v12buf.buf));
511*0Sstevel@tonic-gate 		} else {
512*0Sstevel@tonic-gate 			(void) output(OUT_UNICAST, from, ifp,
513*0Sstevel@tonic-gate 			    v12buf.buf, ((char *)v12buf.n -
514*0Sstevel@tonic-gate 			    (char *)v12buf.buf));
515*0Sstevel@tonic-gate 		}
516*0Sstevel@tonic-gate 		return;
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 	case RIPCMD_TRACEON:
519*0Sstevel@tonic-gate 	case RIPCMD_TRACEOFF:
520*0Sstevel@tonic-gate 		/*
521*0Sstevel@tonic-gate 		 * Notice that trace messages are turned off for all possible
522*0Sstevel@tonic-gate 		 * abuse if PATH_TRACE is undefined in pathnames.h.
523*0Sstevel@tonic-gate 		 * Notice also that because of the way the trace file is
524*0Sstevel@tonic-gate 		 * handled in trace.c, no abuse is plausible even if
525*0Sstevel@tonic-gate 		 * PATH_TRACE is defined.
526*0Sstevel@tonic-gate 		 *
527*0Sstevel@tonic-gate 		 * First verify message came from a privileged port.
528*0Sstevel@tonic-gate 		 */
529*0Sstevel@tonic-gate 		if (ntohs(from->sin_port) > IPPORT_RESERVED) {
530*0Sstevel@tonic-gate 			trace_pkt("trace command from untrusted port %d on %s",
531*0Sstevel@tonic-gate 			    ntohs(from->sin_port), naddr_ntoa(FROM_NADDR));
532*0Sstevel@tonic-gate 			return;
533*0Sstevel@tonic-gate 		}
534*0Sstevel@tonic-gate 		if (ifp == NULL || !remote_address_ok(ifp, FROM_NADDR)) {
535*0Sstevel@tonic-gate 			/*
536*0Sstevel@tonic-gate 			 * Use a message here to warn about strange
537*0Sstevel@tonic-gate 			 * messages from remote systems.
538*0Sstevel@tonic-gate 			 */
539*0Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
540*0Sstevel@tonic-gate 			    "trace command from non-local host %s",
541*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
542*0Sstevel@tonic-gate 			return;
543*0Sstevel@tonic-gate 		}
544*0Sstevel@tonic-gate 		if (ifp->int_state & IS_DISTRUST) {
545*0Sstevel@tonic-gate 			tg = tgates;
546*0Sstevel@tonic-gate 			while (tg->tgate_addr != FROM_NADDR) {
547*0Sstevel@tonic-gate 				tg = tg->tgate_next;
548*0Sstevel@tonic-gate 				if (tg == NULL) {
549*0Sstevel@tonic-gate 					trace_pkt("trace command from "
550*0Sstevel@tonic-gate 					    "untrusted host %s",
551*0Sstevel@tonic-gate 					    naddr_ntoa(FROM_NADDR));
552*0Sstevel@tonic-gate 					return;
553*0Sstevel@tonic-gate 				}
554*0Sstevel@tonic-gate 			}
555*0Sstevel@tonic-gate 		}
556*0Sstevel@tonic-gate 		if (ifp->int_auth[0].type != RIP_AUTH_NONE) {
557*0Sstevel@tonic-gate 			/*
558*0Sstevel@tonic-gate 			 * Technically, it would be fairly easy to add
559*0Sstevel@tonic-gate 			 * standard authentication to the existing
560*0Sstevel@tonic-gate 			 * trace commands -- just bracket the payload
561*0Sstevel@tonic-gate 			 * with the authentication information.
562*0Sstevel@tonic-gate 			 * However, the tracing message behavior
563*0Sstevel@tonic-gate 			 * itself is marginal enough that we don't
564*0Sstevel@tonic-gate 			 * actually care.  Just discard if
565*0Sstevel@tonic-gate 			 * authentication is needed.
566*0Sstevel@tonic-gate 			 */
567*0Sstevel@tonic-gate 			trace_pkt("trace command unauthenticated from %s",
568*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
569*0Sstevel@tonic-gate 			return;
570*0Sstevel@tonic-gate 		}
571*0Sstevel@tonic-gate 		if (rip->rip_cmd == RIPCMD_TRACEON) {
572*0Sstevel@tonic-gate 			rip->rip_tracefile[cc-4] = '\0';
573*0Sstevel@tonic-gate 			set_tracefile(rip->rip_tracefile,
574*0Sstevel@tonic-gate 			    "trace command: %s\n", 0);
575*0Sstevel@tonic-gate 		} else {
576*0Sstevel@tonic-gate 			trace_off("tracing turned off by %s",
577*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
578*0Sstevel@tonic-gate 		}
579*0Sstevel@tonic-gate 		return;
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	case RIPCMD_RESPONSE:
582*0Sstevel@tonic-gate 		if (ifp != NULL && (ifp->int_if_flags & IFF_NOXMIT)) {
583*0Sstevel@tonic-gate 			trace_misc("discard RIP response received over %s "
584*0Sstevel@tonic-gate 			    "(IFF_NOXMIT)", ifp->int_name);
585*0Sstevel@tonic-gate 			return;
586*0Sstevel@tonic-gate 		}
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
589*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
590*0Sstevel@tonic-gate 			    "response of bad length (%d) from %s",
591*0Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
592*0Sstevel@tonic-gate 		}
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate 		if ((ntohl(FROM_NADDR) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
595*0Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
596*0Sstevel@tonic-gate 			    "discard RIP response from bad source address %s",
597*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
598*0Sstevel@tonic-gate 			return;
599*0Sstevel@tonic-gate 		}
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 		/* verify message came from a router */
602*0Sstevel@tonic-gate 		if (from->sin_port != htons(RIP_PORT)) {
603*0Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
604*0Sstevel@tonic-gate 			    "    discard RIP response from unknown port"
605*0Sstevel@tonic-gate 			    " %d on host %s", ntohs(from->sin_port),
606*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
607*0Sstevel@tonic-gate 			return;
608*0Sstevel@tonic-gate 		}
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate 		if (!rip_enabled) {
611*0Sstevel@tonic-gate 			trace_pkt("    discard response while RIP off");
612*0Sstevel@tonic-gate 			return;
613*0Sstevel@tonic-gate 		}
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 		/* Are we talking to ourself or a remote gateway? */
616*0Sstevel@tonic-gate 		ifp1 = ifwithaddr(FROM_NADDR, _B_FALSE, _B_TRUE);
617*0Sstevel@tonic-gate 		if (ifp1 != NULL) {
618*0Sstevel@tonic-gate 			if (ifp1->int_state & IS_REMOTE) {
619*0Sstevel@tonic-gate 				/* remote gateway */
620*0Sstevel@tonic-gate 				ifp = ifp1;
621*0Sstevel@tonic-gate 				if (check_remote(ifp)) {
622*0Sstevel@tonic-gate 					ifp->int_act_time = now.tv_sec;
623*0Sstevel@tonic-gate 					if_ok(ifp, "remote ", _B_FALSE);
624*0Sstevel@tonic-gate 				}
625*0Sstevel@tonic-gate 			} else {
626*0Sstevel@tonic-gate 				trace_pkt("    discard our own RIP response");
627*0Sstevel@tonic-gate 				return;
628*0Sstevel@tonic-gate 			}
629*0Sstevel@tonic-gate 		} else {
630*0Sstevel@tonic-gate 			/*
631*0Sstevel@tonic-gate 			 * If it's not a remote gateway, then the
632*0Sstevel@tonic-gate 			 * remote address *must* be directly
633*0Sstevel@tonic-gate 			 * connected.  Make sure that it is.
634*0Sstevel@tonic-gate 			 */
635*0Sstevel@tonic-gate 			if (ifp != NULL &&
636*0Sstevel@tonic-gate 			    !remote_address_ok(ifp, FROM_NADDR)) {
637*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
638*0Sstevel@tonic-gate 				    "discard RIP response; source %s not on "
639*0Sstevel@tonic-gate 				    "interface %s", naddr_ntoa(FROM_NADDR),
640*0Sstevel@tonic-gate 				    ifp->int_name);
641*0Sstevel@tonic-gate 				return;
642*0Sstevel@tonic-gate 			}
643*0Sstevel@tonic-gate 		}
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 		/*
646*0Sstevel@tonic-gate 		 * Accept routing packets from routers directly connected
647*0Sstevel@tonic-gate 		 * via broadcast or point-to-point networks, and from
648*0Sstevel@tonic-gate 		 * those listed in /etc/gateways.
649*0Sstevel@tonic-gate 		 */
650*0Sstevel@tonic-gate 		if (ifp == NULL) {
651*0Sstevel@tonic-gate 			msglim(&unk_router, FROM_NADDR,
652*0Sstevel@tonic-gate 			    "   discard response from %s"
653*0Sstevel@tonic-gate 			    " via unexpected interface",
654*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
655*0Sstevel@tonic-gate 			return;
656*0Sstevel@tonic-gate 		}
657*0Sstevel@tonic-gate 
658*0Sstevel@tonic-gate 		if (IS_RIP_IN_OFF(ifp->int_state)) {
659*0Sstevel@tonic-gate 			trace_pkt("    discard RIPv%d response"
660*0Sstevel@tonic-gate 			    " via disabled interface %s",
661*0Sstevel@tonic-gate 			    rip->rip_vers, ifp->int_name);
662*0Sstevel@tonic-gate 			return;
663*0Sstevel@tonic-gate 		}
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 		if (n >= lim) {
666*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty response from %s",
667*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
668*0Sstevel@tonic-gate 			return;
669*0Sstevel@tonic-gate 		}
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 		if (((ifp->int_state & IS_NO_RIPV1_IN) &&
672*0Sstevel@tonic-gate 		    rip->rip_vers == RIPv1) ||
673*0Sstevel@tonic-gate 		    ((ifp->int_state & IS_NO_RIPV2_IN) &&
674*0Sstevel@tonic-gate 		    rip->rip_vers != RIPv1)) {
675*0Sstevel@tonic-gate 			trace_pkt("    discard RIPv%d response",
676*0Sstevel@tonic-gate 			    rip->rip_vers);
677*0Sstevel@tonic-gate 			return;
678*0Sstevel@tonic-gate 		}
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 		/*
681*0Sstevel@tonic-gate 		 * Continue to listen to routes via broken interfaces
682*0Sstevel@tonic-gate 		 * which might be declared IS_BROKE because of
683*0Sstevel@tonic-gate 		 * device-driver idiosyncracies, but might otherwise
684*0Sstevel@tonic-gate 		 * be perfectly healthy.
685*0Sstevel@tonic-gate 		 */
686*0Sstevel@tonic-gate 		if (ifp->int_state & IS_BROKE) {
687*0Sstevel@tonic-gate 			trace_pkt("response via broken interface %s",
688*0Sstevel@tonic-gate 			    ifp->int_name);
689*0Sstevel@tonic-gate 		}
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 		/*
692*0Sstevel@tonic-gate 		 * If the interface cares, ignore bad routers.
693*0Sstevel@tonic-gate 		 * Trace but do not log this problem, because where it
694*0Sstevel@tonic-gate 		 * happens, it happens frequently.
695*0Sstevel@tonic-gate 		 */
696*0Sstevel@tonic-gate 		if (ifp->int_state & IS_DISTRUST) {
697*0Sstevel@tonic-gate 			tg = tgates;
698*0Sstevel@tonic-gate 			while (tg->tgate_addr != FROM_NADDR) {
699*0Sstevel@tonic-gate 				tg = tg->tgate_next;
700*0Sstevel@tonic-gate 				if (tg == NULL) {
701*0Sstevel@tonic-gate 					trace_pkt("    discard RIP response"
702*0Sstevel@tonic-gate 					    " from untrusted router %s",
703*0Sstevel@tonic-gate 					    naddr_ntoa(FROM_NADDR));
704*0Sstevel@tonic-gate 					return;
705*0Sstevel@tonic-gate 				}
706*0Sstevel@tonic-gate 			}
707*0Sstevel@tonic-gate 		}
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		/*
710*0Sstevel@tonic-gate 		 * Authenticate the packet if we have a secret.
711*0Sstevel@tonic-gate 		 * If we do not have any secrets, ignore the error in
712*0Sstevel@tonic-gate 		 * RFC 1723 and accept it regardless.
713*0Sstevel@tonic-gate 		 */
714*0Sstevel@tonic-gate 		if (ifp->int_auth[0].type != RIP_AUTH_NONE &&
715*0Sstevel@tonic-gate 		    rip->rip_vers != RIPv1 &&
716*0Sstevel@tonic-gate 		    !ck_passwd(ifp, rip, (uint8_t *)lim, FROM_NADDR, &use_auth))
717*0Sstevel@tonic-gate 			return;
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 		/*
720*0Sstevel@tonic-gate 		 * Do this only if we're supplying routes to *nobody*.
721*0Sstevel@tonic-gate 		 */
722*0Sstevel@tonic-gate 		if (!should_supply(NULL) && save_space) {
723*0Sstevel@tonic-gate 			/*
724*0Sstevel@tonic-gate 			 * "-S" option.  Instead of entering all routes,
725*0Sstevel@tonic-gate 			 * only enter a default route for the sender of
726*0Sstevel@tonic-gate 			 * this RESPONSE message
727*0Sstevel@tonic-gate 			 */
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 			/* Should we trust this route from this router? */
730*0Sstevel@tonic-gate 			if (tg != NULL && tg->tgate_nets->mask != 0) {
731*0Sstevel@tonic-gate 				trace_pkt("   ignored unauthorized %s",
732*0Sstevel@tonic-gate 				    addrname(RIP_DEFAULT, 0, 0));
733*0Sstevel@tonic-gate 				break;
734*0Sstevel@tonic-gate 			}
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 			new.rts_gate = FROM_NADDR;
737*0Sstevel@tonic-gate 			new.rts_router = FROM_NADDR;
738*0Sstevel@tonic-gate 			new.rts_metric = HOPCNT_INFINITY-1;
739*0Sstevel@tonic-gate 			new.rts_tag = n->n_tag;
740*0Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
741*0Sstevel@tonic-gate 			new.rts_ifp = ifp;
742*0Sstevel@tonic-gate 			new.rts_de_ag = 0;
743*0Sstevel@tonic-gate 			new.rts_origin = RO_RIP;
744*0Sstevel@tonic-gate 			/*
745*0Sstevel@tonic-gate 			 * Add the newly generated default route, but don't
746*0Sstevel@tonic-gate 			 * propagate the madness.  Treat it the same way as
747*0Sstevel@tonic-gate 			 * default routes learned from Router Discovery.
748*0Sstevel@tonic-gate 			 */
749*0Sstevel@tonic-gate 			input_route(RIP_DEFAULT, 0, &new, n, RS_NOPROPAGATE);
750*0Sstevel@tonic-gate 			return;
751*0Sstevel@tonic-gate 		}
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 		if (!IS_IFF_ROUTING(ifp->int_if_flags)) {
754*0Sstevel@tonic-gate 			/*
755*0Sstevel@tonic-gate 			 * We don't want to propagate routes which would
756*0Sstevel@tonic-gate 			 * result in a black-hole.
757*0Sstevel@tonic-gate 			 */
758*0Sstevel@tonic-gate 			rt_state = RS_NOPROPAGATE;
759*0Sstevel@tonic-gate 		}
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 		do {
762*0Sstevel@tonic-gate 			if (n->n_family == RIP_AF_AUTH)
763*0Sstevel@tonic-gate 				continue;
764*0Sstevel@tonic-gate 
765*0Sstevel@tonic-gate 			n->n_metric = ntohl(n->n_metric);
766*0Sstevel@tonic-gate 			dst = n->n_dst;
767*0Sstevel@tonic-gate 			if (n->n_family != RIP_AF_INET &&
768*0Sstevel@tonic-gate 			    (n->n_family != RIP_AF_UNSPEC ||
769*0Sstevel@tonic-gate 			    dst != RIP_DEFAULT)) {
770*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
771*0Sstevel@tonic-gate 				    "route from %s to unsupported"
772*0Sstevel@tonic-gate 				    " address family=%d destination=%s",
773*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR), n->n_family,
774*0Sstevel@tonic-gate 				    naddr_ntoa(dst));
775*0Sstevel@tonic-gate 				continue;
776*0Sstevel@tonic-gate 			}
777*0Sstevel@tonic-gate 			if (!check_dst(dst)) {
778*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
779*0Sstevel@tonic-gate 				    "bad destination %s from %s",
780*0Sstevel@tonic-gate 				    naddr_ntoa(dst),
781*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR));
782*0Sstevel@tonic-gate 				continue;
783*0Sstevel@tonic-gate 			}
784*0Sstevel@tonic-gate 			if (n->n_metric == 0 || n->n_metric > HOPCNT_INFINITY) {
785*0Sstevel@tonic-gate 				msglim(&bad_router, FROM_NADDR,
786*0Sstevel@tonic-gate 				    "bad metric %d from %s"
787*0Sstevel@tonic-gate 				    " for destination %s",
788*0Sstevel@tonic-gate 				    n->n_metric, naddr_ntoa(FROM_NADDR),
789*0Sstevel@tonic-gate 				    naddr_ntoa(dst));
790*0Sstevel@tonic-gate 				continue;
791*0Sstevel@tonic-gate 			}
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 			/*
794*0Sstevel@tonic-gate 			 * Notice the next-hop.
795*0Sstevel@tonic-gate 			 */
796*0Sstevel@tonic-gate 			gate = FROM_NADDR;
797*0Sstevel@tonic-gate 			if (n->n_nhop != 0) {
798*0Sstevel@tonic-gate 				if (rip->rip_vers == RIPv1) {
799*0Sstevel@tonic-gate 					n->n_nhop = 0;
800*0Sstevel@tonic-gate 				} else {
801*0Sstevel@tonic-gate 					/* Use it only if it is valid. */
802*0Sstevel@tonic-gate 					if (on_net(n->n_nhop,
803*0Sstevel@tonic-gate 					    ifp->int_net, ifp->int_mask) &&
804*0Sstevel@tonic-gate 					    check_dst(n->n_nhop)) {
805*0Sstevel@tonic-gate 						gate = n->n_nhop;
806*0Sstevel@tonic-gate 					} else {
807*0Sstevel@tonic-gate 						msglim(&bad_nhop,
808*0Sstevel@tonic-gate 						    FROM_NADDR,
809*0Sstevel@tonic-gate 						    "router %s to %s"
810*0Sstevel@tonic-gate 						    " has bad next hop %s",
811*0Sstevel@tonic-gate 						    naddr_ntoa(FROM_NADDR),
812*0Sstevel@tonic-gate 						    naddr_ntoa(dst),
813*0Sstevel@tonic-gate 						    naddr_ntoa(n->n_nhop));
814*0Sstevel@tonic-gate 						n->n_nhop = 0;
815*0Sstevel@tonic-gate 					}
816*0Sstevel@tonic-gate 				}
817*0Sstevel@tonic-gate 			}
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1 ||
820*0Sstevel@tonic-gate 			    0 == (mask = ntohl(n->n_mask))) {
821*0Sstevel@tonic-gate 				mask = ripv1_mask_host(dst, ifp);
822*0Sstevel@tonic-gate 			} else if ((ntohl(dst) & ~mask) != 0) {
823*0Sstevel@tonic-gate 				msglim(&bad_mask, FROM_NADDR,
824*0Sstevel@tonic-gate 				    "router %s sent bad netmask %s with %s",
825*0Sstevel@tonic-gate 				    naddr_ntoa(FROM_NADDR),
826*0Sstevel@tonic-gate 				    naddr_ntoa(htonl(mask)),
827*0Sstevel@tonic-gate 				    naddr_ntoa(dst));
828*0Sstevel@tonic-gate 				continue;
829*0Sstevel@tonic-gate 			}
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate 			if (mask == HOST_MASK &&
832*0Sstevel@tonic-gate 			    (ifp->int_state & IS_NO_HOST)) {
833*0Sstevel@tonic-gate 				trace_pkt("   ignored host route %s",
834*0Sstevel@tonic-gate 				    addrname(dst, mask, 0));
835*0Sstevel@tonic-gate 				continue;
836*0Sstevel@tonic-gate 			}
837*0Sstevel@tonic-gate 
838*0Sstevel@tonic-gate 			if (rip->rip_vers == RIPv1)
839*0Sstevel@tonic-gate 				n->n_tag = 0;
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 			/*
842*0Sstevel@tonic-gate 			 * Adjust metric according to incoming interface cost.
843*0Sstevel@tonic-gate 			 * We intentionally don't drop incoming routes with
844*0Sstevel@tonic-gate 			 * metric 15 on the floor even though they will
845*0Sstevel@tonic-gate 			 * not be advertised to other routers.  We can use
846*0Sstevel@tonic-gate 			 * such routes locally, resulting in a network with
847*0Sstevel@tonic-gate 			 * a maximum width of 15 hops rather than 14.
848*0Sstevel@tonic-gate 			 */
849*0Sstevel@tonic-gate 			n->n_metric += ifp->int_metric;
850*0Sstevel@tonic-gate 			if (n->n_metric > HOPCNT_INFINITY)
851*0Sstevel@tonic-gate 				n->n_metric = HOPCNT_INFINITY;
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 			/*
854*0Sstevel@tonic-gate 			 * Should we trust this route from this router?
855*0Sstevel@tonic-gate 			 */
856*0Sstevel@tonic-gate 			if (tg != NULL && (tn = tg->tgate_nets)->mask != 0) {
857*0Sstevel@tonic-gate 				for (i = 0; i < MAX_TGATE_NETS; i++, tn++) {
858*0Sstevel@tonic-gate 					if (on_net(dst, tn->net, tn->mask) &&
859*0Sstevel@tonic-gate 					    tn->mask <= mask)
860*0Sstevel@tonic-gate 						break;
861*0Sstevel@tonic-gate 				}
862*0Sstevel@tonic-gate 				if (i >= MAX_TGATE_NETS || tn->mask == 0) {
863*0Sstevel@tonic-gate 					trace_pkt("   ignored unauthorized %s",
864*0Sstevel@tonic-gate 					    addrname(dst, mask, 0));
865*0Sstevel@tonic-gate 					continue;
866*0Sstevel@tonic-gate 				}
867*0Sstevel@tonic-gate 			}
868*0Sstevel@tonic-gate 
869*0Sstevel@tonic-gate 			/*
870*0Sstevel@tonic-gate 			 * Recognize and ignore a default route we faked
871*0Sstevel@tonic-gate 			 * which is being sent back to us by a machine with
872*0Sstevel@tonic-gate 			 * broken split-horizon. Be a little more paranoid
873*0Sstevel@tonic-gate 			 * than that, and reject default routes with the
874*0Sstevel@tonic-gate 			 * same metric we advertised.
875*0Sstevel@tonic-gate 			 */
876*0Sstevel@tonic-gate 			if (ifp->int_d_metric != 0 && dst == RIP_DEFAULT &&
877*0Sstevel@tonic-gate 			    n->n_metric >= ifp->int_d_metric)
878*0Sstevel@tonic-gate 				continue;
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 			/*
881*0Sstevel@tonic-gate 			 * We can receive aggregated RIPv2 routes that must
882*0Sstevel@tonic-gate 			 * be broken down before they are transmitted by
883*0Sstevel@tonic-gate 			 * RIPv1 via an interface on a subnet. We might
884*0Sstevel@tonic-gate 			 * also receive the same routes aggregated via
885*0Sstevel@tonic-gate 			 * other RIPv2 interfaces.  This could cause
886*0Sstevel@tonic-gate 			 * duplicate routes to be sent on the RIPv1
887*0Sstevel@tonic-gate 			 * interfaces. "Longest matching variable length
888*0Sstevel@tonic-gate 			 * netmasks" lets RIPv2 listeners understand, but
889*0Sstevel@tonic-gate 			 * breaking down the aggregated routes for RIPv1
890*0Sstevel@tonic-gate 			 * listeners can produce duplicate routes.
891*0Sstevel@tonic-gate 			 *
892*0Sstevel@tonic-gate 			 * Breaking down aggregated routes here bloats the
893*0Sstevel@tonic-gate 			 * daemon table, but does not hurt the kernel
894*0Sstevel@tonic-gate 			 * table, since routes are always aggregated for
895*0Sstevel@tonic-gate 			 * the kernel.
896*0Sstevel@tonic-gate 			 *
897*0Sstevel@tonic-gate 			 * Notice that this does not break down network
898*0Sstevel@tonic-gate 			 * routes corresponding to subnets. This is part of
899*0Sstevel@tonic-gate 			 * the defense against RS_NET_SYN.
900*0Sstevel@tonic-gate 			 */
901*0Sstevel@tonic-gate 			if (have_ripv1_out &&
902*0Sstevel@tonic-gate 			    (((rt = rtget(dst, mask)) == NULL ||
903*0Sstevel@tonic-gate 			    !(rt->rt_state & RS_NET_SYN))) &&
904*0Sstevel@tonic-gate 			    (v1_mask = ripv1_mask_net(dst, 0)) > mask) {
905*0Sstevel@tonic-gate 				/* Get least significant set bit */
906*0Sstevel@tonic-gate 				ddst_h = v1_mask & -v1_mask;
907*0Sstevel@tonic-gate 				i = (v1_mask & ~mask)/ddst_h;
908*0Sstevel@tonic-gate 				/*
909*0Sstevel@tonic-gate 				 * If you're going to make 512 or more
910*0Sstevel@tonic-gate 				 * routes, then that's just too many.  The
911*0Sstevel@tonic-gate 				 * reason here is that breaking an old
912*0Sstevel@tonic-gate 				 * class B into /24 allocations is common
913*0Sstevel@tonic-gate 				 * enough that allowing for the creation of
914*0Sstevel@tonic-gate 				 * at least 256 deaggregated routes is
915*0Sstevel@tonic-gate 				 * good.  The next power of 2 is 512.
916*0Sstevel@tonic-gate 				 */
917*0Sstevel@tonic-gate 				if (i >= 511) {
918*0Sstevel@tonic-gate 					/*
919*0Sstevel@tonic-gate 					 * Punt if we would have to
920*0Sstevel@tonic-gate 					 * generate an unreasonable number
921*0Sstevel@tonic-gate 					 * of routes.
922*0Sstevel@tonic-gate 					 */
923*0Sstevel@tonic-gate 					if (TRACECONTENTS)
924*0Sstevel@tonic-gate 						trace_misc("accept %s-->%s as 1"
925*0Sstevel@tonic-gate 						    " instead of %d routes",
926*0Sstevel@tonic-gate 						    addrname(dst, mask, 0),
927*0Sstevel@tonic-gate 						    naddr_ntoa(FROM_NADDR),
928*0Sstevel@tonic-gate 						    i + 1);
929*0Sstevel@tonic-gate 					i = 0;
930*0Sstevel@tonic-gate 				} else {
931*0Sstevel@tonic-gate 					mask = v1_mask;
932*0Sstevel@tonic-gate 				}
933*0Sstevel@tonic-gate 			} else {
934*0Sstevel@tonic-gate 				i = 0;
935*0Sstevel@tonic-gate 			}
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 			new.rts_gate = gate;
938*0Sstevel@tonic-gate 			new.rts_router = FROM_NADDR;
939*0Sstevel@tonic-gate 			new.rts_metric = n->n_metric;
940*0Sstevel@tonic-gate 			new.rts_tag = n->n_tag;
941*0Sstevel@tonic-gate 			new.rts_time = now.tv_sec;
942*0Sstevel@tonic-gate 			new.rts_ifp = ifp;
943*0Sstevel@tonic-gate 			new.rts_de_ag = i;
944*0Sstevel@tonic-gate 			new.rts_origin = RO_RIP;
945*0Sstevel@tonic-gate 			j = 0;
946*0Sstevel@tonic-gate 			for (;;) {
947*0Sstevel@tonic-gate 				input_route(dst, mask, &new, n, rt_state);
948*0Sstevel@tonic-gate 				if (++j > i)
949*0Sstevel@tonic-gate 					break;
950*0Sstevel@tonic-gate 				dst = htonl(ntohl(dst) + ddst_h);
951*0Sstevel@tonic-gate 			}
952*0Sstevel@tonic-gate 		} while (++n < lim);
953*0Sstevel@tonic-gate 		return;
954*0Sstevel@tonic-gate 	case RIPCMD_POLLENTRY:
955*0Sstevel@tonic-gate 		/*
956*0Sstevel@tonic-gate 		 * With this command one can request a single entry.
957*0Sstevel@tonic-gate 		 * Both silent processes and routers can respond to this
958*0Sstevel@tonic-gate 		 * command
959*0Sstevel@tonic-gate 		 */
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate 		if (n >= lim) {
962*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR, "empty request from %s",
963*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
964*0Sstevel@tonic-gate 			return;
965*0Sstevel@tonic-gate 		}
966*0Sstevel@tonic-gate 		if (cc%sizeof (*n) != sizeof (struct rip)%sizeof (*n)) {
967*0Sstevel@tonic-gate 			msglim(&bad_len, FROM_NADDR,
968*0Sstevel@tonic-gate 			    "request of bad length (%d) from %s",
969*0Sstevel@tonic-gate 			    cc, naddr_ntoa(FROM_NADDR));
970*0Sstevel@tonic-gate 		}
971*0Sstevel@tonic-gate 
972*0Sstevel@tonic-gate 		if (rip->rip_vers == RIPv2 && (ifp == NULL ||
973*0Sstevel@tonic-gate 		    (ifp->int_state & IS_NO_RIPV1_OUT))) {
974*0Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv2;
975*0Sstevel@tonic-gate 		} else {
976*0Sstevel@tonic-gate 			v12buf.buf->rip_vers = RIPv1;
977*0Sstevel@tonic-gate 		}
978*0Sstevel@tonic-gate 		/* Dont bother with md5 authentication with POLLENTRY */
979*0Sstevel@tonic-gate 		ap = NULL;
980*0Sstevel@tonic-gate 		clr_ws_buf(&v12buf, ap);
981*0Sstevel@tonic-gate 
982*0Sstevel@tonic-gate 		n->n_metric = ntohl(n->n_metric);
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 		if (n->n_family != RIP_AF_INET) {
985*0Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
986*0Sstevel@tonic-gate 			    "POLLENTRY request from %s for unsupported"
987*0Sstevel@tonic-gate 			    " (af %d) %s",
988*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR),
989*0Sstevel@tonic-gate 			    ntohs(n->n_family),
990*0Sstevel@tonic-gate 			    naddr_ntoa(n->n_dst));
991*0Sstevel@tonic-gate 			return;
992*0Sstevel@tonic-gate 		}
993*0Sstevel@tonic-gate 
994*0Sstevel@tonic-gate 		/* We are being asked about a specific destination. */
995*0Sstevel@tonic-gate 		v12buf.n->n_dst = dst = n->n_dst;
996*0Sstevel@tonic-gate 		v12buf.n->n_family = RIP_AF_INET;
997*0Sstevel@tonic-gate 		if (!check_dst(dst)) {
998*0Sstevel@tonic-gate 			msglim(&bad_router, FROM_NADDR,
999*0Sstevel@tonic-gate 			    "bad queried destination %s from %s",
1000*0Sstevel@tonic-gate 			    naddr_ntoa(dst),
1001*0Sstevel@tonic-gate 			    naddr_ntoa(FROM_NADDR));
1002*0Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
1003*0Sstevel@tonic-gate 			goto pollentry_done;
1004*0Sstevel@tonic-gate 		}
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 		/* decide what mask was intended */
1007*0Sstevel@tonic-gate 		if (rip->rip_vers == RIPv1 ||
1008*0Sstevel@tonic-gate 		    0 == (mask = ntohl(n->n_mask)) ||
1009*0Sstevel@tonic-gate 		    0 != (ntohl(dst) & ~mask))
1010*0Sstevel@tonic-gate 			mask = ripv1_mask_host(dst, ifp);
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 		/* try to find the answer */
1013*0Sstevel@tonic-gate 		rt = rtget(dst, mask);
1014*0Sstevel@tonic-gate 		if (rt == NULL && dst != RIP_DEFAULT)
1015*0Sstevel@tonic-gate 			rt = rtfind(n->n_dst);
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 		if (v12buf.buf->rip_vers != RIPv1)
1018*0Sstevel@tonic-gate 			v12buf.n->n_mask = htonl(mask);
1019*0Sstevel@tonic-gate 		if (rt == NULL) {
1020*0Sstevel@tonic-gate 			/* we do not have the answer */
1021*0Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
1022*0Sstevel@tonic-gate 			goto pollentry_done;
1023*0Sstevel@tonic-gate 		}
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 		/*
1027*0Sstevel@tonic-gate 		 * we have the answer, so compute the right metric and next
1028*0Sstevel@tonic-gate 		 * hop.
1029*0Sstevel@tonic-gate 		 */
1030*0Sstevel@tonic-gate 		v12buf.n->n_metric = rt->rt_metric + 1;
1031*0Sstevel@tonic-gate 		if (v12buf.n->n_metric > HOPCNT_INFINITY)
1032*0Sstevel@tonic-gate 			v12buf.n->n_metric = HOPCNT_INFINITY;
1033*0Sstevel@tonic-gate 		if (v12buf.buf->rip_vers != RIPv1) {
1034*0Sstevel@tonic-gate 			v12buf.n->n_tag = rt->rt_tag;
1035*0Sstevel@tonic-gate 			if (ifp != NULL &&
1036*0Sstevel@tonic-gate 			    on_net(rt->rt_gate, ifp->int_net, ifp->int_mask) &&
1037*0Sstevel@tonic-gate 			    rt->rt_gate != ifp->int_addr)
1038*0Sstevel@tonic-gate 				v12buf.n->n_nhop = rt->rt_gate;
1039*0Sstevel@tonic-gate 		}
1040*0Sstevel@tonic-gate pollentry_done:
1041*0Sstevel@tonic-gate 		v12buf.n->n_metric = htonl(v12buf.n->n_metric);
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 		/*
1044*0Sstevel@tonic-gate 		 * Send the answer about specific routes.
1045*0Sstevel@tonic-gate 		 */
1046*0Sstevel@tonic-gate 		(void) output(OUT_QUERY, from, ifp, v12buf.buf,
1047*0Sstevel@tonic-gate 		    ((char *)v12buf.n - (char *)v12buf.buf));
1048*0Sstevel@tonic-gate 		break;
1049*0Sstevel@tonic-gate 	}
1050*0Sstevel@tonic-gate #undef FROM_NADDR
1051*0Sstevel@tonic-gate }
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate /*
1055*0Sstevel@tonic-gate  * Process a single input route.
1056*0Sstevel@tonic-gate  */
1057*0Sstevel@tonic-gate void
1058*0Sstevel@tonic-gate input_route(in_addr_t dst,			/* network order */
1059*0Sstevel@tonic-gate     in_addr_t mask,
1060*0Sstevel@tonic-gate     struct rt_spare *new,
1061*0Sstevel@tonic-gate     struct netinfo *n,
1062*0Sstevel@tonic-gate     uint16_t rt_state)
1063*0Sstevel@tonic-gate {
1064*0Sstevel@tonic-gate 	int i;
1065*0Sstevel@tonic-gate 	struct rt_entry *rt;
1066*0Sstevel@tonic-gate 	struct rt_spare *rts, *rts0;
1067*0Sstevel@tonic-gate 	struct interface *ifp1;
1068*0Sstevel@tonic-gate 	struct rt_spare *ptr;
1069*0Sstevel@tonic-gate 	size_t ptrsize;
1070*0Sstevel@tonic-gate 
1071*0Sstevel@tonic-gate 	/*
1072*0Sstevel@tonic-gate 	 * See if we can already get there by a working interface.  Ignore
1073*0Sstevel@tonic-gate 	 * if so.
1074*0Sstevel@tonic-gate 	 */
1075*0Sstevel@tonic-gate 	ifp1 = ifwithaddr(dst, _B_TRUE, _B_FALSE);
1076*0Sstevel@tonic-gate 	if (ifp1 != NULL && (ifp1->int_state & IS_PASSIVE))
1077*0Sstevel@tonic-gate 		return;
1078*0Sstevel@tonic-gate 
1079*0Sstevel@tonic-gate 	/*
1080*0Sstevel@tonic-gate 	 * Look for the route in our table.
1081*0Sstevel@tonic-gate 	 */
1082*0Sstevel@tonic-gate 	rt = rtget(dst, mask);
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate 	/* Consider adding the route if we do not already have it. */
1085*0Sstevel@tonic-gate 	if (rt == NULL) {
1086*0Sstevel@tonic-gate 		/* Ignore unknown routes being poisoned. */
1087*0Sstevel@tonic-gate 		if (new->rts_metric == HOPCNT_INFINITY)
1088*0Sstevel@tonic-gate 			return;
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 		/* Ignore the route if it points to us */
1091*0Sstevel@tonic-gate 		if (n != NULL && n->n_nhop != 0 &&
1092*0Sstevel@tonic-gate 		    NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
1093*0Sstevel@tonic-gate 			return;
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate 		/*
1096*0Sstevel@tonic-gate 		 * If something has not gone crazy and tried to fill
1097*0Sstevel@tonic-gate 		 * our memory, accept the new route.
1098*0Sstevel@tonic-gate 		 */
1099*0Sstevel@tonic-gate 		rtadd(dst, mask, rt_state, new);
1100*0Sstevel@tonic-gate 		return;
1101*0Sstevel@tonic-gate 	}
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 	/*
1104*0Sstevel@tonic-gate 	 * We already know about the route.  Consider this update.
1105*0Sstevel@tonic-gate 	 *
1106*0Sstevel@tonic-gate 	 * If (rt->rt_state & RS_NET_SYN), then this route
1107*0Sstevel@tonic-gate 	 * is the same as a network route we have inferred
1108*0Sstevel@tonic-gate 	 * for subnets we know, in order to tell RIPv1 routers
1109*0Sstevel@tonic-gate 	 * about the subnets.
1110*0Sstevel@tonic-gate 	 *
1111*0Sstevel@tonic-gate 	 * It is impossible to tell if the route is coming
1112*0Sstevel@tonic-gate 	 * from a distant RIPv2 router with the standard
1113*0Sstevel@tonic-gate 	 * netmask because that router knows about the entire
1114*0Sstevel@tonic-gate 	 * network, or if it is a round-about echo of a
1115*0Sstevel@tonic-gate 	 * synthetic, RIPv1 network route of our own.
1116*0Sstevel@tonic-gate 	 * The worst is that both kinds of routes might be
1117*0Sstevel@tonic-gate 	 * received, and the bad one might have the smaller
1118*0Sstevel@tonic-gate 	 * metric.  Partly solve this problem by never
1119*0Sstevel@tonic-gate 	 * aggregating into such a route.  Also keep it
1120*0Sstevel@tonic-gate 	 * around as long as the interface exists.
1121*0Sstevel@tonic-gate 	 */
1122*0Sstevel@tonic-gate 
1123*0Sstevel@tonic-gate 	rts0 = rt->rt_spares;
1124*0Sstevel@tonic-gate 	trace_misc("rt 0x%lx num_spares %d", rt, rt->rt_num_spares);
1125*0Sstevel@tonic-gate 	for (rts = rts0, i = rt->rt_num_spares; i != 0; i--, rts++) {
1126*0Sstevel@tonic-gate 		if (rts->rts_router == new->rts_router)
1127*0Sstevel@tonic-gate 			break;
1128*0Sstevel@tonic-gate 		/*
1129*0Sstevel@tonic-gate 		 * Note the worst slot to reuse,
1130*0Sstevel@tonic-gate 		 * other than the current slot.
1131*0Sstevel@tonic-gate 		 */
1132*0Sstevel@tonic-gate 		if (BETTER_LINK(rt, rts0, rts))
1133*0Sstevel@tonic-gate 			rts0 = rts;
1134*0Sstevel@tonic-gate 	}
1135*0Sstevel@tonic-gate 	if (i != 0) {
1136*0Sstevel@tonic-gate 		/*
1137*0Sstevel@tonic-gate 		 * Found a route from the router already in the table.
1138*0Sstevel@tonic-gate 		 */
1139*0Sstevel@tonic-gate 
1140*0Sstevel@tonic-gate 		/*
1141*0Sstevel@tonic-gate 		 * If the new route is a route broken down from an
1142*0Sstevel@tonic-gate 		 * aggregated route, and if the previous route is either
1143*0Sstevel@tonic-gate 		 * not a broken down route or was broken down from a finer
1144*0Sstevel@tonic-gate 		 * netmask, and if the previous route is current,
1145*0Sstevel@tonic-gate 		 * then forget this one.
1146*0Sstevel@tonic-gate 		 */
1147*0Sstevel@tonic-gate 		if (new->rts_de_ag > rts->rts_de_ag &&
1148*0Sstevel@tonic-gate 		    now_stale <= rts->rts_time)
1149*0Sstevel@tonic-gate 			return;
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 		/*
1152*0Sstevel@tonic-gate 		 * Keep poisoned routes around only long enough to pass
1153*0Sstevel@tonic-gate 		 * the poison on.  Use a new timestamp for good routes.
1154*0Sstevel@tonic-gate 		 */
1155*0Sstevel@tonic-gate 		if (rts->rts_metric == HOPCNT_INFINITY &&
1156*0Sstevel@tonic-gate 		    new->rts_metric == HOPCNT_INFINITY)
1157*0Sstevel@tonic-gate 			new->rts_time = rts->rts_time;
1158*0Sstevel@tonic-gate 
1159*0Sstevel@tonic-gate 		/*
1160*0Sstevel@tonic-gate 		 * If this is an update for the router we currently prefer,
1161*0Sstevel@tonic-gate 		 * then note it.
1162*0Sstevel@tonic-gate 		 */
1163*0Sstevel@tonic-gate 		if (i == rt->rt_num_spares) {
1164*0Sstevel@tonic-gate 			rtchange(rt, rt->rt_state | rt_state, new, 0);
1165*0Sstevel@tonic-gate 			/*
1166*0Sstevel@tonic-gate 			 * If the route got worse, check for something better.
1167*0Sstevel@tonic-gate 			 */
1168*0Sstevel@tonic-gate 			if (new->rts_metric != rts->rts_metric)
1169*0Sstevel@tonic-gate 				rtswitch(rt, 0);
1170*0Sstevel@tonic-gate 			return;
1171*0Sstevel@tonic-gate 		}
1172*0Sstevel@tonic-gate 
1173*0Sstevel@tonic-gate 		/*
1174*0Sstevel@tonic-gate 		 * This is an update for a spare route.
1175*0Sstevel@tonic-gate 		 * Finished if the route is unchanged.
1176*0Sstevel@tonic-gate 		 */
1177*0Sstevel@tonic-gate 		if (rts->rts_gate == new->rts_gate &&
1178*0Sstevel@tonic-gate 		    rts->rts_metric == new->rts_metric &&
1179*0Sstevel@tonic-gate 		    rts->rts_tag == new->rts_tag) {
1180*0Sstevel@tonic-gate 			if ((rt->rt_dst == RIP_DEFAULT) &&
1181*0Sstevel@tonic-gate 			    (rts->rts_ifp != new->rts_ifp))
1182*0Sstevel@tonic-gate 				trace_misc("input_route update for spare");
1183*0Sstevel@tonic-gate 			trace_upslot(rt, rts, new);
1184*0Sstevel@tonic-gate 			*rts = *new;
1185*0Sstevel@tonic-gate 			return;
1186*0Sstevel@tonic-gate 		}
1187*0Sstevel@tonic-gate 
1188*0Sstevel@tonic-gate 		/*
1189*0Sstevel@tonic-gate 		 * Forget it if it has gone bad.
1190*0Sstevel@tonic-gate 		 */
1191*0Sstevel@tonic-gate 		if (new->rts_metric == HOPCNT_INFINITY) {
1192*0Sstevel@tonic-gate 			rts_delete(rt, rts);
1193*0Sstevel@tonic-gate 			return;
1194*0Sstevel@tonic-gate 		}
1195*0Sstevel@tonic-gate 
1196*0Sstevel@tonic-gate 	} else {
1197*0Sstevel@tonic-gate 		/*
1198*0Sstevel@tonic-gate 		 * The update is for a route we know about,
1199*0Sstevel@tonic-gate 		 * but not from a familiar router.
1200*0Sstevel@tonic-gate 		 *
1201*0Sstevel@tonic-gate 		 * Ignore the route if it points to us.
1202*0Sstevel@tonic-gate 		 */
1203*0Sstevel@tonic-gate 		if (n != NULL && n->n_nhop != 0 &&
1204*0Sstevel@tonic-gate 		    NULL != ifwithaddr(n->n_nhop, _B_TRUE, _B_FALSE))
1205*0Sstevel@tonic-gate 			return;
1206*0Sstevel@tonic-gate 
1207*0Sstevel@tonic-gate 		/* the loop above set rts0=worst spare */
1208*0Sstevel@tonic-gate 		if (rts0->rts_metric < HOPCNT_INFINITY) {
1209*0Sstevel@tonic-gate 			ptrsize = (rt->rt_num_spares + SPARE_INC) *
1210*0Sstevel@tonic-gate 			    sizeof (struct rt_spare);
1211*0Sstevel@tonic-gate 			ptr = realloc(rt->rt_spares, ptrsize);
1212*0Sstevel@tonic-gate 			if (ptr != NULL) {
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 				rt->rt_spares = ptr;
1215*0Sstevel@tonic-gate 				rts0 = &rt->rt_spares[rt->rt_num_spares];
1216*0Sstevel@tonic-gate 				(void) memset(rts0, 0,
1217*0Sstevel@tonic-gate 				    SPARE_INC * sizeof (struct rt_spare));
1218*0Sstevel@tonic-gate 				rt->rt_num_spares += SPARE_INC;
1219*0Sstevel@tonic-gate 				for (rts = rts0, i = SPARE_INC;
1220*0Sstevel@tonic-gate 				    i != 0; i--, rts++)
1221*0Sstevel@tonic-gate 					rts->rts_metric = HOPCNT_INFINITY;
1222*0Sstevel@tonic-gate 			}
1223*0Sstevel@tonic-gate 		}
1224*0Sstevel@tonic-gate 		rts = rts0;
1225*0Sstevel@tonic-gate 
1226*0Sstevel@tonic-gate 		/*
1227*0Sstevel@tonic-gate 		 * Save the route as a spare only if it has
1228*0Sstevel@tonic-gate 		 * a better metric than our worst spare.
1229*0Sstevel@tonic-gate 		 * This also ignores poisoned routes (those
1230*0Sstevel@tonic-gate 		 * received with metric HOPCNT_INFINITY).
1231*0Sstevel@tonic-gate 		 */
1232*0Sstevel@tonic-gate 		if (new->rts_metric >= rts->rts_metric)
1233*0Sstevel@tonic-gate 			return;
1234*0Sstevel@tonic-gate 	}
1235*0Sstevel@tonic-gate 	trace_upslot(rt, rts, new);
1236*0Sstevel@tonic-gate 	*rts = *new;
1237*0Sstevel@tonic-gate 
1238*0Sstevel@tonic-gate 	/* try to switch to a better route */
1239*0Sstevel@tonic-gate 	rtswitch(rt, rts);
1240*0Sstevel@tonic-gate }
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate /*
1243*0Sstevel@tonic-gate  * Recorded information about peer's MD5 sequence numbers.  This is
1244*0Sstevel@tonic-gate  * used to validate that received sequence numbers are in
1245*0Sstevel@tonic-gate  * non-decreasing order as per the RFC.
1246*0Sstevel@tonic-gate  */
1247*0Sstevel@tonic-gate struct peer_hash {
1248*0Sstevel@tonic-gate 	struct peer_hash *ph_next;
1249*0Sstevel@tonic-gate 	in_addr_t ph_addr;
1250*0Sstevel@tonic-gate 	time_t ph_heard;
1251*0Sstevel@tonic-gate 	uint32_t ph_seqno;
1252*0Sstevel@tonic-gate };
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate static struct peer_hash **peer_hashes;
1255*0Sstevel@tonic-gate static int ph_index;
1256*0Sstevel@tonic-gate static int ph_num_peers;
1257*0Sstevel@tonic-gate 
1258*0Sstevel@tonic-gate /*
1259*0Sstevel@tonic-gate  * Get a peer_hash structure from the hash of known peers.  Create a
1260*0Sstevel@tonic-gate  * new one if not found.  Returns NULL on unrecoverable allocation
1261*0Sstevel@tonic-gate  * failure.
1262*0Sstevel@tonic-gate  */
1263*0Sstevel@tonic-gate static struct peer_hash *
1264*0Sstevel@tonic-gate get_peer_info(in_addr_t from)
1265*0Sstevel@tonic-gate {
1266*0Sstevel@tonic-gate 	struct peer_hash *php;
1267*0Sstevel@tonic-gate 	struct peer_hash *pnhp;
1268*0Sstevel@tonic-gate 	struct peer_hash **ph_pp;
1269*0Sstevel@tonic-gate 	struct peer_hash **ph2_pp;
1270*0Sstevel@tonic-gate 	struct peer_hash **ph3_pp;
1271*0Sstevel@tonic-gate 	int i;
1272*0Sstevel@tonic-gate 	static uint_t failed_count;
1273*0Sstevel@tonic-gate 
1274*0Sstevel@tonic-gate 	if (peer_hashes == NULL) {
1275*0Sstevel@tonic-gate 		peer_hashes = calloc(hash_table_sizes[0],
1276*0Sstevel@tonic-gate 		    sizeof (peer_hashes[0]));
1277*0Sstevel@tonic-gate 		if (peer_hashes == NULL) {
1278*0Sstevel@tonic-gate 			if (++failed_count % 100 == 1)
1279*0Sstevel@tonic-gate 				msglog("no memory for peer hash");
1280*0Sstevel@tonic-gate 			return (NULL);
1281*0Sstevel@tonic-gate 		}
1282*0Sstevel@tonic-gate 	}
1283*0Sstevel@tonic-gate 	/* Search for peer in existing hash table */
1284*0Sstevel@tonic-gate 	ph_pp = peer_hashes + (from % hash_table_sizes[ph_index]);
1285*0Sstevel@tonic-gate 	for (php = ph_pp[0]; php != NULL; php = php->ph_next) {
1286*0Sstevel@tonic-gate 		if (php->ph_addr == from)
1287*0Sstevel@tonic-gate 			return (php);
1288*0Sstevel@tonic-gate 	}
1289*0Sstevel@tonic-gate 	/*
1290*0Sstevel@tonic-gate 	 * Not found; we need to add this peer to the table.  If there
1291*0Sstevel@tonic-gate 	 * are already too many peers, then try to expand the table
1292*0Sstevel@tonic-gate 	 * first.  It's not a big deal if we can't expand the table
1293*0Sstevel@tonic-gate 	 * right now due to memory constraints.  We'll try again
1294*0Sstevel@tonic-gate 	 * later.
1295*0Sstevel@tonic-gate 	 */
1296*0Sstevel@tonic-gate 	if (ph_num_peers >= hash_table_sizes[ph_index] * 5 &&
1297*0Sstevel@tonic-gate 	    hash_table_sizes[ph_index + 1] != 0 &&
1298*0Sstevel@tonic-gate 	    (ph_pp = calloc(hash_table_sizes[ph_index + 1],
1299*0Sstevel@tonic-gate 		sizeof (peer_hashes[0]))) != NULL) {
1300*0Sstevel@tonic-gate 		ph2_pp = peer_hashes;
1301*0Sstevel@tonic-gate 		for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
1302*0Sstevel@tonic-gate 			for (php = ph2_pp[i]; php != NULL; php = pnhp) {
1303*0Sstevel@tonic-gate 				pnhp = php->ph_next;
1304*0Sstevel@tonic-gate 				ph3_pp = ph_pp + (php->ph_addr %
1305*0Sstevel@tonic-gate 				    hash_table_sizes[ph_index + 1]);
1306*0Sstevel@tonic-gate 				php->ph_next = ph3_pp[0];
1307*0Sstevel@tonic-gate 				ph3_pp[0] = php;
1308*0Sstevel@tonic-gate 			}
1309*0Sstevel@tonic-gate 		}
1310*0Sstevel@tonic-gate 		ph_index++;
1311*0Sstevel@tonic-gate 		free(peer_hashes);
1312*0Sstevel@tonic-gate 		peer_hashes = ph_pp;
1313*0Sstevel@tonic-gate 		ph_pp += from % hash_table_sizes[ph_index];
1314*0Sstevel@tonic-gate 	}
1315*0Sstevel@tonic-gate 	php = calloc(sizeof (*php), 1);
1316*0Sstevel@tonic-gate 	if (php == NULL) {
1317*0Sstevel@tonic-gate 		if (++failed_count % 100 == 1)
1318*0Sstevel@tonic-gate 			msglog("no memory for peer hash entry");
1319*0Sstevel@tonic-gate 	} else {
1320*0Sstevel@tonic-gate 		php->ph_addr = from;
1321*0Sstevel@tonic-gate 		php->ph_heard = now.tv_sec;
1322*0Sstevel@tonic-gate 		php->ph_next = ph_pp[0];
1323*0Sstevel@tonic-gate 		ph_pp[0] = php;
1324*0Sstevel@tonic-gate 		ph_num_peers++;
1325*0Sstevel@tonic-gate 	}
1326*0Sstevel@tonic-gate 	return (php);
1327*0Sstevel@tonic-gate }
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate /*
1330*0Sstevel@tonic-gate  * Age out entries in the peer table.  This is called every time we do
1331*0Sstevel@tonic-gate  * a normal 30 second broadcast.
1332*0Sstevel@tonic-gate  */
1333*0Sstevel@tonic-gate void
1334*0Sstevel@tonic-gate age_peer_info(void)
1335*0Sstevel@tonic-gate {
1336*0Sstevel@tonic-gate 	struct peer_hash *php;
1337*0Sstevel@tonic-gate 	struct peer_hash *next_ph;
1338*0Sstevel@tonic-gate 	struct peer_hash *prev_ph;
1339*0Sstevel@tonic-gate 	struct peer_hash **ph_pp;
1340*0Sstevel@tonic-gate 	int i;
1341*0Sstevel@tonic-gate 
1342*0Sstevel@tonic-gate 	/*
1343*0Sstevel@tonic-gate 	 * Scan through the list and remove peers that should not
1344*0Sstevel@tonic-gate 	 * still have valid authenticated entries in the routing
1345*0Sstevel@tonic-gate 	 * table.
1346*0Sstevel@tonic-gate 	 */
1347*0Sstevel@tonic-gate 	if ((ph_pp = peer_hashes) == NULL || ph_num_peers == 0)
1348*0Sstevel@tonic-gate 		return;
1349*0Sstevel@tonic-gate 	for (i = hash_table_sizes[ph_index] - 1; i >= 0; i--) {
1350*0Sstevel@tonic-gate 		prev_ph = NULL;
1351*0Sstevel@tonic-gate 		for (php = ph_pp[i]; php != NULL; php = next_ph) {
1352*0Sstevel@tonic-gate 			next_ph = php->ph_next;
1353*0Sstevel@tonic-gate 			if (php->ph_heard <= now_expire) {
1354*0Sstevel@tonic-gate 				if (prev_ph == NULL)
1355*0Sstevel@tonic-gate 					ph_pp[i] = next_ph;
1356*0Sstevel@tonic-gate 				else
1357*0Sstevel@tonic-gate 					prev_ph->ph_next = next_ph;
1358*0Sstevel@tonic-gate 				free(php);
1359*0Sstevel@tonic-gate 				if (--ph_num_peers == 0)
1360*0Sstevel@tonic-gate 					return;
1361*0Sstevel@tonic-gate 			} else {
1362*0Sstevel@tonic-gate 				prev_ph = php;
1363*0Sstevel@tonic-gate 			}
1364*0Sstevel@tonic-gate 		}
1365*0Sstevel@tonic-gate 	}
1366*0Sstevel@tonic-gate }
1367*0Sstevel@tonic-gate 
1368*0Sstevel@tonic-gate static boolean_t		/* _B_FALSE if bad, _B_TRUE if good */
1369*0Sstevel@tonic-gate ck_passwd(struct interface *aifp,
1370*0Sstevel@tonic-gate     struct rip *rip,
1371*0Sstevel@tonic-gate     uint8_t *lim,
1372*0Sstevel@tonic-gate     in_addr_t from,
1373*0Sstevel@tonic-gate     struct msg_limit *use_authp)
1374*0Sstevel@tonic-gate {
1375*0Sstevel@tonic-gate #define	NA (rip->rip_auths)
1376*0Sstevel@tonic-gate 	struct netauth *na2;
1377*0Sstevel@tonic-gate 	struct auth *ap;
1378*0Sstevel@tonic-gate 	MD5_CTX md5_ctx;
1379*0Sstevel@tonic-gate 	uchar_t hash[RIP_AUTH_PW_LEN];
1380*0Sstevel@tonic-gate 	int i, len;
1381*0Sstevel@tonic-gate 	struct peer_hash *php;
1382*0Sstevel@tonic-gate 	uint32_t seqno;
1383*0Sstevel@tonic-gate 
1384*0Sstevel@tonic-gate 	if ((uint8_t *)NA >= lim || NA->a_family != RIP_AF_AUTH) {
1385*0Sstevel@tonic-gate 		msglim(use_authp, from, "missing auth data from %s",
1386*0Sstevel@tonic-gate 		    naddr_ntoa(from));
1387*0Sstevel@tonic-gate 		return (_B_FALSE);
1388*0Sstevel@tonic-gate 	}
1389*0Sstevel@tonic-gate 
1390*0Sstevel@tonic-gate 	/*
1391*0Sstevel@tonic-gate 	 * Validate sequence number on RIPv2 responses using keyed MD5
1392*0Sstevel@tonic-gate 	 * authentication per RFC 2082 section 3.2.2.  Note that if we
1393*0Sstevel@tonic-gate 	 * can't locate the peer information (due to transient
1394*0Sstevel@tonic-gate 	 * allocation problems), then we don't do the test.  Also note
1395*0Sstevel@tonic-gate 	 * that we assume that all sequence numbers 0x80000000 or more
1396*0Sstevel@tonic-gate 	 * away are "less than."
1397*0Sstevel@tonic-gate 	 *
1398*0Sstevel@tonic-gate 	 * We intentionally violate RFC 2082 with respect to one case:
1399*0Sstevel@tonic-gate 	 * restablishing contact.  The RFC says that you should
1400*0Sstevel@tonic-gate 	 * continue to ignore old sequence numbers in this case but
1401*0Sstevel@tonic-gate 	 * make a special allowance for 0.  This is extremely foolish.
1402*0Sstevel@tonic-gate 	 * The problem is that if the router has crashed, it's
1403*0Sstevel@tonic-gate 	 * entirely possible that either we'll miss sequence zero (or
1404*0Sstevel@tonic-gate 	 * that it might not even send it!) or that the peer doesn't
1405*0Sstevel@tonic-gate 	 * remember what it last used for a sequence number.  In
1406*0Sstevel@tonic-gate 	 * either case, we'll create a failure state that persists
1407*0Sstevel@tonic-gate 	 * until the sequence number happens to advance past the last
1408*0Sstevel@tonic-gate 	 * one we saw.  This is bad because it means that we may have
1409*0Sstevel@tonic-gate 	 * to wait until the router has been up for at least as long
1410*0Sstevel@tonic-gate 	 * as it was last time before we even pay attention to it.
1411*0Sstevel@tonic-gate 	 * Meanwhile, other routers may listen to it if they hadn't
1412*0Sstevel@tonic-gate 	 * seen it before (i.e., if they crashed in the meantime).
1413*0Sstevel@tonic-gate 	 * This means -- perversely -- that stable systems that stay
1414*0Sstevel@tonic-gate 	 * "up" for a long time pay a penalty for doing so.
1415*0Sstevel@tonic-gate 	 */
1416*0Sstevel@tonic-gate 	if (rip->rip_cmd == RIPCMD_RESPONSE && NA->a_type == RIP_AUTH_MD5 &&
1417*0Sstevel@tonic-gate 	    (php = get_peer_info(from)) != NULL) {
1418*0Sstevel@tonic-gate 		/*
1419*0Sstevel@tonic-gate 		 * If the entry that we find has been updated
1420*0Sstevel@tonic-gate 		 * recently enough that the routes are known
1421*0Sstevel@tonic-gate 		 * to still be good, but the sequence number
1422*0Sstevel@tonic-gate 		 * looks bad, then discard the packet.
1423*0Sstevel@tonic-gate 		 */
1424*0Sstevel@tonic-gate 		seqno = ntohl(NA->au.a_md5.md5_seqno);
1425*0Sstevel@tonic-gate 		if (php->ph_heard > now_expire && php->ph_seqno != 0 &&
1426*0Sstevel@tonic-gate 		    (seqno == 0 || ((seqno - php->ph_seqno) & 0x80000000ul))) {
1427*0Sstevel@tonic-gate 			msglim(use_authp, from,
1428*0Sstevel@tonic-gate 			    "discarding sequence %x (older than %x)",
1429*0Sstevel@tonic-gate 			    (unsigned)seqno, (unsigned)php->ph_seqno);
1430*0Sstevel@tonic-gate 			return (_B_FALSE);
1431*0Sstevel@tonic-gate 		}
1432*0Sstevel@tonic-gate 		php->ph_heard = now.tv_sec;
1433*0Sstevel@tonic-gate 		php->ph_seqno = seqno;
1434*0Sstevel@tonic-gate 	}
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 	/*
1437*0Sstevel@tonic-gate 	 * accept any current (+/- 24 hours) password
1438*0Sstevel@tonic-gate 	 */
1439*0Sstevel@tonic-gate 	for (ap = aifp->int_auth, i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
1440*0Sstevel@tonic-gate 		if (ap->type != NA->a_type ||
1441*0Sstevel@tonic-gate 		    (ulong_t)ap->start > (ulong_t)clk.tv_sec+DAY ||
1442*0Sstevel@tonic-gate 		    (ulong_t)ap->end+DAY < (ulong_t)clk.tv_sec)
1443*0Sstevel@tonic-gate 			continue;
1444*0Sstevel@tonic-gate 
1445*0Sstevel@tonic-gate 		if (NA->a_type == RIP_AUTH_PW) {
1446*0Sstevel@tonic-gate 			if (0 == memcmp(NA->au.au_pw, ap->key, RIP_AUTH_PW_LEN))
1447*0Sstevel@tonic-gate 				return (_B_TRUE);
1448*0Sstevel@tonic-gate 
1449*0Sstevel@tonic-gate 		} else {
1450*0Sstevel@tonic-gate 			/*
1451*0Sstevel@tonic-gate 			 * accept MD5 secret with the right key ID
1452*0Sstevel@tonic-gate 			 */
1453*0Sstevel@tonic-gate 			if (NA->au.a_md5.md5_keyid != ap->keyid)
1454*0Sstevel@tonic-gate 				continue;
1455*0Sstevel@tonic-gate 
1456*0Sstevel@tonic-gate 			len = ntohs(NA->au.a_md5.md5_pkt_len);
1457*0Sstevel@tonic-gate 			if ((len - sizeof (*rip)) % sizeof (*NA) != 0 ||
1458*0Sstevel@tonic-gate 			    len > (lim - (uint8_t *)rip - sizeof (*NA))) {
1459*0Sstevel@tonic-gate 				msglim(use_authp, from,
1460*0Sstevel@tonic-gate 				    "wrong MD5 RIPv2 packet length of %d"
1461*0Sstevel@tonic-gate 				    " instead of %d from %s",
1462*0Sstevel@tonic-gate 				    len, lim - (uint8_t *)rip - sizeof (*NA),
1463*0Sstevel@tonic-gate 				    naddr_ntoa(from));
1464*0Sstevel@tonic-gate 				return (_B_FALSE);
1465*0Sstevel@tonic-gate 			}
1466*0Sstevel@tonic-gate 			na2 = (struct netauth *)(rip->rip_nets +
1467*0Sstevel@tonic-gate 			    (len - 4) / sizeof (struct netinfo));
1468*0Sstevel@tonic-gate 
1469*0Sstevel@tonic-gate 			/*
1470*0Sstevel@tonic-gate 			 * Given a good hash value, these are not security
1471*0Sstevel@tonic-gate 			 * problems so be generous and accept the routes,
1472*0Sstevel@tonic-gate 			 * after complaining.
1473*0Sstevel@tonic-gate 			 */
1474*0Sstevel@tonic-gate 			if (TRACEPACKETS) {
1475*0Sstevel@tonic-gate 				if (NA->au.a_md5.md5_auth_len !=
1476*0Sstevel@tonic-gate 				    RIP_AUTH_MD5_LEN)
1477*0Sstevel@tonic-gate 					msglim(use_authp, from,
1478*0Sstevel@tonic-gate 					    "unknown MD5 RIPv2 auth len %#x"
1479*0Sstevel@tonic-gate 					    " instead of %#x from %s",
1480*0Sstevel@tonic-gate 					    NA->au.a_md5.md5_auth_len,
1481*0Sstevel@tonic-gate 					    RIP_AUTH_MD5_LEN,
1482*0Sstevel@tonic-gate 					    naddr_ntoa(from));
1483*0Sstevel@tonic-gate 				if (na2->a_family != RIP_AF_AUTH)
1484*0Sstevel@tonic-gate 					msglim(use_authp, from,
1485*0Sstevel@tonic-gate 					    "unknown MD5 RIPv2 family %#x"
1486*0Sstevel@tonic-gate 					    " instead of %#x from %s",
1487*0Sstevel@tonic-gate 					    na2->a_family, RIP_AF_AUTH,
1488*0Sstevel@tonic-gate 					    naddr_ntoa(from));
1489*0Sstevel@tonic-gate 				if (na2->a_type != RIP_AUTH_TRAILER)
1490*0Sstevel@tonic-gate 					msglim(use_authp, from,
1491*0Sstevel@tonic-gate 					    "MD5 RIPv2 hash has %#x"
1492*0Sstevel@tonic-gate 					    " instead of %#x from %s",
1493*0Sstevel@tonic-gate 					    ntohs(na2->a_type),
1494*0Sstevel@tonic-gate 					    ntohs(RIP_AUTH_TRAILER),
1495*0Sstevel@tonic-gate 					    naddr_ntoa(from));
1496*0Sstevel@tonic-gate 			}
1497*0Sstevel@tonic-gate 
1498*0Sstevel@tonic-gate 			MD5Init(&md5_ctx);
1499*0Sstevel@tonic-gate 			/*
1500*0Sstevel@tonic-gate 			 * len+4 to include auth trailer's family/type in
1501*0Sstevel@tonic-gate 			 * MD5 sum
1502*0Sstevel@tonic-gate 			 */
1503*0Sstevel@tonic-gate 			MD5Update(&md5_ctx, (uchar_t *)rip, len + 4);
1504*0Sstevel@tonic-gate 			MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
1505*0Sstevel@tonic-gate 			MD5Final(hash, &md5_ctx);
1506*0Sstevel@tonic-gate 			if (0 == memcmp(hash, na2->au.au_pw, sizeof (hash)))
1507*0Sstevel@tonic-gate 				return (_B_TRUE);
1508*0Sstevel@tonic-gate 		}
1509*0Sstevel@tonic-gate 	}
1510*0Sstevel@tonic-gate 
1511*0Sstevel@tonic-gate 	msglim(use_authp, from, "bad auth data from %s",
1512*0Sstevel@tonic-gate 	    naddr_ntoa(from));
1513*0Sstevel@tonic-gate 	return (_B_FALSE);
1514*0Sstevel@tonic-gate #undef NA
1515*0Sstevel@tonic-gate }
1516