17c478bd9Sstevel@tonic-gate /*
2*e704a8f2Smeem  * 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/output.c,v 1.7 2000/08/11 08:24:38 sheldonh Exp $
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "defs.h"
407c478bd9Sstevel@tonic-gate #include <md5.h>
41*e704a8f2Smeem #include <alloca.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate uint_t update_seqno;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * walk the tree of routes with this for output
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate static	struct {
507c478bd9Sstevel@tonic-gate 	struct sockaddr_in to;
517c478bd9Sstevel@tonic-gate 	in_addr_t	to_mask;
527c478bd9Sstevel@tonic-gate 	in_addr_t	to_net;
537c478bd9Sstevel@tonic-gate 	in_addr_t	to_std_mask;
547c478bd9Sstevel@tonic-gate 	in_addr_t	to_std_net;
557c478bd9Sstevel@tonic-gate 	struct interface *ifp;		/* usually output interface */
567c478bd9Sstevel@tonic-gate 	struct auth	*a;
577c478bd9Sstevel@tonic-gate 	uint8_t		metric;		/* adjust metrics by interface */
587c478bd9Sstevel@tonic-gate 	uint32_t	npackets;
597c478bd9Sstevel@tonic-gate 	uint32_t	gen_limit;
607c478bd9Sstevel@tonic-gate #define	WS_GEN_LIMIT_MAX	1024
617c478bd9Sstevel@tonic-gate 	uint16_t	state;
627c478bd9Sstevel@tonic-gate #define	WS_ST_FLASH	0x001	/* send only changed routes */
637c478bd9Sstevel@tonic-gate #define	WS_ST_RIP2_ALL	0x002	/* send full featured RIPv2 */
647c478bd9Sstevel@tonic-gate #define	WS_ST_AG	0x004	/* ok to aggregate subnets */
657c478bd9Sstevel@tonic-gate #define	WS_ST_SUPER_AG	0x008	/* ok to aggregate networks */
667c478bd9Sstevel@tonic-gate #define	WS_ST_QUERY	0x010	/* responding to a query */
677c478bd9Sstevel@tonic-gate #define	WS_ST_TO_ON_NET	0x020	/* sending onto one of our nets */
687c478bd9Sstevel@tonic-gate #define	WS_ST_DEFAULT	0x040	/* faking a default */
697c478bd9Sstevel@tonic-gate } ws;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */
727c478bd9Sstevel@tonic-gate struct ws_buf v12buf;
737c478bd9Sstevel@tonic-gate static	union pkt_buf ripv12_buf;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Another for only RIPv2 listeners */
767c478bd9Sstevel@tonic-gate static	struct ws_buf v2buf;
777c478bd9Sstevel@tonic-gate static	union pkt_buf rip_v2_buf;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate void
bufinit(void)827c478bd9Sstevel@tonic-gate bufinit(void)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE;
857c478bd9Sstevel@tonic-gate 	v12buf.buf = &ripv12_buf.rip;
867c478bd9Sstevel@tonic-gate 	v12buf.base = &v12buf.buf->rip_nets[0];
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE;
897c478bd9Sstevel@tonic-gate 	rip_v2_buf.rip.rip_vers = RIPv2;
907c478bd9Sstevel@tonic-gate 	v2buf.buf = &rip_v2_buf.rip;
917c478bd9Sstevel@tonic-gate 	v2buf.base = &v2buf.buf->rip_nets[0];
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Send the contents of the global buffer via the non-multicast socket
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate int					/* <0 on failure */
output(enum output_type type,struct sockaddr_in * dst,struct interface * ifp,struct rip * buf,int size)997c478bd9Sstevel@tonic-gate output(enum output_type type,
1007c478bd9Sstevel@tonic-gate     struct sockaddr_in *dst,		/* send to here */
1017c478bd9Sstevel@tonic-gate     struct interface *ifp,
1027c478bd9Sstevel@tonic-gate     struct rip *buf,
1037c478bd9Sstevel@tonic-gate     int size)			/* this many bytes */
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	struct sockaddr_in sin;
1067c478bd9Sstevel@tonic-gate 	int flags;
1077c478bd9Sstevel@tonic-gate 	const char *msg;
1087c478bd9Sstevel@tonic-gate 	int res;
1097c478bd9Sstevel@tonic-gate 	int ifindex;
1107c478bd9Sstevel@tonic-gate 	struct in_addr addr;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	sin = *dst;
1137c478bd9Sstevel@tonic-gate 	if (sin.sin_port == 0)
1147c478bd9Sstevel@tonic-gate 		sin.sin_port = htons(RIP_PORT);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	flags = 0;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (ifp == NULL && type == OUT_MULTICAST) {
1197c478bd9Sstevel@tonic-gate 		msglog("Cannot send RIP message to %s",
1207c478bd9Sstevel@tonic-gate 		    inet_ntoa(sin.sin_addr));
1217c478bd9Sstevel@tonic-gate 		return (-1);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	switch (type) {
1257c478bd9Sstevel@tonic-gate 	case OUT_QUERY:
1267c478bd9Sstevel@tonic-gate 		msg = "Answer Query";
1277c478bd9Sstevel@tonic-gate 		break;
1287c478bd9Sstevel@tonic-gate 	case OUT_UNICAST:
1297c478bd9Sstevel@tonic-gate 		msg = "Send";
1307c478bd9Sstevel@tonic-gate 		flags = MSG_DONTROUTE;
1317c478bd9Sstevel@tonic-gate 		break;
1327c478bd9Sstevel@tonic-gate 	case OUT_BROADCAST:
1337c478bd9Sstevel@tonic-gate 		msg = "Send bcast";
1347c478bd9Sstevel@tonic-gate 		break;
1357c478bd9Sstevel@tonic-gate 	case OUT_MULTICAST:
1367c478bd9Sstevel@tonic-gate 		msg = "Send mcast";
1377c478bd9Sstevel@tonic-gate 		break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	case NO_OUT_MULTICAST:
1407c478bd9Sstevel@tonic-gate 	case NO_OUT_RIPV2:
1417c478bd9Sstevel@tonic-gate 	default:
1427c478bd9Sstevel@tonic-gate #ifdef DEBUG
1437c478bd9Sstevel@tonic-gate 		abort();
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 		return (-1);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
149*e704a8f2Smeem 	 * IP_PKTINFO overrides IP_MULTICAST_IF, so we don't set ifindex
150*e704a8f2Smeem 	 * for multicast traffic.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	ifindex = (type != OUT_MULTICAST && type != OUT_QUERY &&
1537c478bd9Sstevel@tonic-gate 	    ifp != NULL && ifp->int_phys != NULL) ?
1547c478bd9Sstevel@tonic-gate 	    ifp->int_phys->phyi_index : 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (rip_sock_interface != ifp) {
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * For multicast, we have to choose the source
1597c478bd9Sstevel@tonic-gate 		 * address.  This is either the local address
1607c478bd9Sstevel@tonic-gate 		 * (non-point-to-point) or the remote address.
1617c478bd9Sstevel@tonic-gate 		 */
1627c478bd9Sstevel@tonic-gate 		if (ifp != NULL) {
1637c478bd9Sstevel@tonic-gate 			addr.s_addr = (ifp->int_if_flags & IFF_POINTOPOINT) ?
1647c478bd9Sstevel@tonic-gate 			    ifp->int_dstaddr : ifp->int_addr;
1657c478bd9Sstevel@tonic-gate 			if (type == OUT_MULTICAST &&
1667c478bd9Sstevel@tonic-gate 			    setsockopt(rip_sock, IPPROTO_IP,
1677c478bd9Sstevel@tonic-gate 			    IP_MULTICAST_IF, &addr, sizeof (addr)) == -1) {
1687c478bd9Sstevel@tonic-gate 				LOGERR("setsockopt(rip_sock, IP_MULTICAST_IF)");
1697c478bd9Sstevel@tonic-gate 				return (-1);
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 		}
1727c478bd9Sstevel@tonic-gate 		rip_sock_interface = ifp;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	trace_rip(msg, "to", &sin, ifp, buf, size);
1767c478bd9Sstevel@tonic-gate 
177*e704a8f2Smeem 	res = sendtoif(rip_sock, buf, size, flags, &sin, ifindex);
178*e704a8f2Smeem 	if (res < 0 && (ifp == NULL || !(ifp->int_state & IS_BROKE))) {
1797c478bd9Sstevel@tonic-gate 		writelog(LOG_WARNING, "%s sendto(%s%s%s.%d): %s", msg,
1807c478bd9Sstevel@tonic-gate 		    ifp != NULL ? ifp->int_name : "",
1817c478bd9Sstevel@tonic-gate 		    ifp != NULL ? ", " : "",
1827c478bd9Sstevel@tonic-gate 		    inet_ntoa(sin.sin_addr),
1837c478bd9Sstevel@tonic-gate 		    ntohs(sin.sin_port),
1847c478bd9Sstevel@tonic-gate 		    rip_strerror(errno));
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (res);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
190*e704a8f2Smeem /*
191*e704a8f2Smeem  * Semantically identical to sendto(), but sends the message through a
192*e704a8f2Smeem  * specific interface (if ifindex is non-zero) using IP_PKTINFO.
193*e704a8f2Smeem  */
194*e704a8f2Smeem int
sendtoif(int fd,const void * buf,uint_t bufsize,uint_t flags,struct sockaddr_in * sinp,uint_t ifindex)195*e704a8f2Smeem sendtoif(int fd, const void *buf, uint_t bufsize, uint_t flags,
196*e704a8f2Smeem     struct sockaddr_in *sinp, uint_t ifindex)
197*e704a8f2Smeem {
198*e704a8f2Smeem 	struct iovec iov;
199*e704a8f2Smeem 	struct msghdr msg;
200*e704a8f2Smeem 	struct cmsghdr *cmsgp;
201*e704a8f2Smeem 	struct in_pktinfo *ipip;
202*e704a8f2Smeem 
203*e704a8f2Smeem 	iov.iov_base = (void *)buf;
204*e704a8f2Smeem 	iov.iov_len = bufsize;
205*e704a8f2Smeem 
206*e704a8f2Smeem 	(void) memset(&msg, 0, sizeof (struct msghdr));
207*e704a8f2Smeem 	msg.msg_name = (struct sockaddr *)sinp;
208*e704a8f2Smeem 	msg.msg_namelen = sizeof (struct sockaddr_in);
209*e704a8f2Smeem 	msg.msg_iov = &iov;
210*e704a8f2Smeem 	msg.msg_iovlen = 1;
211*e704a8f2Smeem 
212*e704a8f2Smeem 	if (ifindex != 0) {
213*e704a8f2Smeem 		/*
214*e704a8f2Smeem 		 * We can't precisely predict the alignment padding we'll
215*e704a8f2Smeem 		 * need, so we allocate the maximum alignment and then
216*e704a8f2Smeem 		 * use CMSG_NXTHDR() to fix it up at the end.
217*e704a8f2Smeem 		 */
218*e704a8f2Smeem 		msg.msg_controllen = sizeof (*cmsgp) + _MAX_ALIGNMENT +
219*e704a8f2Smeem 		    sizeof (*ipip) + _MAX_ALIGNMENT + sizeof (*cmsgp);
220*e704a8f2Smeem 		msg.msg_control = alloca(msg.msg_controllen);
221*e704a8f2Smeem 
222*e704a8f2Smeem 		cmsgp = CMSG_FIRSTHDR(&msg);
223*e704a8f2Smeem 		ipip = (void *)CMSG_DATA(cmsgp);
224*e704a8f2Smeem 		(void) memset(ipip, 0, sizeof (struct in_pktinfo));
225*e704a8f2Smeem 		ipip->ipi_ifindex = ifindex;
226*e704a8f2Smeem 		cmsgp->cmsg_len = (caddr_t)(ipip + 1) - (caddr_t)cmsgp;
227*e704a8f2Smeem 		cmsgp->cmsg_type = IP_PKTINFO;
228*e704a8f2Smeem 		cmsgp->cmsg_level = IPPROTO_IP;
229*e704a8f2Smeem 
230*e704a8f2Smeem 		/*
231*e704a8f2Smeem 		 * Correct the control message length.
232*e704a8f2Smeem 		 */
233*e704a8f2Smeem 		cmsgp = CMSG_NXTHDR(&msg, cmsgp);
234*e704a8f2Smeem 		msg.msg_controllen = (caddr_t)cmsgp - (caddr_t)msg.msg_control;
235*e704a8f2Smeem 	}
236*e704a8f2Smeem 
237*e704a8f2Smeem 	return (sendmsg(fd, &msg, flags));
238*e704a8f2Smeem }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Find the first key for a packet to send.
2427c478bd9Sstevel@tonic-gate  * Try for a key that is eligible and has not expired, but settle for
2437c478bd9Sstevel@tonic-gate  * the last key if they have all expired.
2447c478bd9Sstevel@tonic-gate  * If no key is ready yet, give up.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate struct auth *
find_auth(struct interface * ifp)2477c478bd9Sstevel@tonic-gate find_auth(struct interface *ifp)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	struct auth *ap, *res = NULL;
2507c478bd9Sstevel@tonic-gate 	int i;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (ifp == NULL)
2547c478bd9Sstevel@tonic-gate 		return (NULL);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if ((ap = ifp->int_auth) == NULL)
2577c478bd9Sstevel@tonic-gate 		return (NULL);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) {
2607c478bd9Sstevel@tonic-gate 		/* stop looking after the last key */
2617c478bd9Sstevel@tonic-gate 		if (ap->type == RIP_AUTH_NONE)
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		/* ignore keys that are not ready yet */
2657c478bd9Sstevel@tonic-gate 		if ((ulong_t)ap->start > (ulong_t)clk.tv_sec)
2667c478bd9Sstevel@tonic-gate 			continue;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		if ((ulong_t)ap->end < (ulong_t)clk.tv_sec) {
2697c478bd9Sstevel@tonic-gate 			/* note best expired password as a fall-back */
2707c478bd9Sstevel@tonic-gate 			if (res == NULL ||
2717c478bd9Sstevel@tonic-gate 			    (((ulong_t)ap->end > (ulong_t)res->end)) &&
2727c478bd9Sstevel@tonic-gate 			    ((ulong_t)res->end < (ulong_t)clk.tv_sec))
2737c478bd9Sstevel@tonic-gate 				res = ap;
2747c478bd9Sstevel@tonic-gate 			continue;
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		/* note key with the best future */
2787c478bd9Sstevel@tonic-gate 		if (res == NULL || (ulong_t)res->end < (ulong_t)ap->end)
2797c478bd9Sstevel@tonic-gate 			res = ap;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	return (res);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate void
clr_ws_buf(struct ws_buf * wb,struct auth * ap)2867c478bd9Sstevel@tonic-gate clr_ws_buf(struct ws_buf *wb, struct auth *ap)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	struct netauth *na;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	wb->lim = wb->base + NETS_LEN;
2917c478bd9Sstevel@tonic-gate 	wb->n = wb->base;
2927c478bd9Sstevel@tonic-gate 	(void) memset(wb->n, 0, NETS_LEN*sizeof (*wb->n));
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/*
2957c478bd9Sstevel@tonic-gate 	 * (start to) install authentication if appropriate
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	if (ap == NULL)
2987c478bd9Sstevel@tonic-gate 		return;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	na = (struct netauth *)wb->n;
3017c478bd9Sstevel@tonic-gate 	if (ap->type == RIP_AUTH_PW) {
3027c478bd9Sstevel@tonic-gate 		na->a_family = RIP_AF_AUTH;
3037c478bd9Sstevel@tonic-gate 		na->a_type = RIP_AUTH_PW;
3047c478bd9Sstevel@tonic-gate 		(void) memcpy(na->au.au_pw, ap->key, sizeof (na->au.au_pw));
3057c478bd9Sstevel@tonic-gate 		wb->n++;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	} else if (ap->type ==  RIP_AUTH_MD5) {
3087c478bd9Sstevel@tonic-gate 		na->a_family = RIP_AF_AUTH;
3097c478bd9Sstevel@tonic-gate 		na->a_type = RIP_AUTH_MD5;
3107c478bd9Sstevel@tonic-gate 		na->au.a_md5.md5_keyid = ap->keyid;
3117c478bd9Sstevel@tonic-gate 		na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_LEN;
3127c478bd9Sstevel@tonic-gate 		na->au.a_md5.md5_seqno = htonl(clk.tv_sec);
3137c478bd9Sstevel@tonic-gate 		wb->n++;
3147c478bd9Sstevel@tonic-gate 		wb->lim--;		/* make room for trailer */
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate void
end_md5_auth(struct ws_buf * wb,struct auth * ap)3207c478bd9Sstevel@tonic-gate end_md5_auth(struct ws_buf *wb, struct auth *ap)
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 	struct netauth *na, *na2;
3237c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
3247c478bd9Sstevel@tonic-gate 	int len;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	na = (struct netauth *)wb->base;
3277c478bd9Sstevel@tonic-gate 	na2 = (struct netauth *)wb->n;
3287c478bd9Sstevel@tonic-gate 	len = (char *)na2-(char *)wb->buf;
3297c478bd9Sstevel@tonic-gate 	na2->a_family = RIP_AF_AUTH;
3307c478bd9Sstevel@tonic-gate 	na2->a_type = RIP_AUTH_TRAILER;
3317c478bd9Sstevel@tonic-gate 	na->au.a_md5.md5_pkt_len = htons(len);
3327c478bd9Sstevel@tonic-gate 	MD5Init(&md5_ctx);
3337c478bd9Sstevel@tonic-gate 	/* len+4 to include auth trailer's family/type in MD5 sum */
3347c478bd9Sstevel@tonic-gate 	MD5Update(&md5_ctx, (uchar_t *)wb->buf, len + 4);
3357c478bd9Sstevel@tonic-gate 	MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_LEN);
3367c478bd9Sstevel@tonic-gate 	MD5Final(na2->au.au_pw, &md5_ctx);
3377c478bd9Sstevel@tonic-gate 	wb->n++;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * Send the buffer
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate static void
supply_write(struct ws_buf * wb)3457c478bd9Sstevel@tonic-gate supply_write(struct ws_buf *wb)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * Output multicast only if legal.
3497c478bd9Sstevel@tonic-gate 	 * If we would multicast and it would be illegal, then discard the
3507c478bd9Sstevel@tonic-gate 	 * packet.
3517c478bd9Sstevel@tonic-gate 	 */
3527c478bd9Sstevel@tonic-gate 	switch (wb->type) {
3537c478bd9Sstevel@tonic-gate 	case NO_OUT_MULTICAST:
3547c478bd9Sstevel@tonic-gate 		trace_pkt("skip multicast to %s because impossible",
3557c478bd9Sstevel@tonic-gate 		    naddr_ntoa(ws.to.sin_addr.s_addr));
3567c478bd9Sstevel@tonic-gate 		break;
3577c478bd9Sstevel@tonic-gate 	case NO_OUT_RIPV2:
3587c478bd9Sstevel@tonic-gate 		break;
3597c478bd9Sstevel@tonic-gate 	default:
3607c478bd9Sstevel@tonic-gate 		if (ws.a != NULL && ws.a->type == RIP_AUTH_MD5)
3617c478bd9Sstevel@tonic-gate 			end_md5_auth(wb, ws.a);
3627c478bd9Sstevel@tonic-gate 		if (output(wb->type, &ws.to, ws.ifp, wb->buf,
3637c478bd9Sstevel@tonic-gate 		    ((char *)wb->n - (char *)wb->buf)) < 0 && ws.ifp != NULL)
3647c478bd9Sstevel@tonic-gate 			if_sick(ws.ifp, _B_FALSE);
3657c478bd9Sstevel@tonic-gate 		ws.npackets++;
3667c478bd9Sstevel@tonic-gate 		break;
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	clr_ws_buf(wb, ws.a);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate /*
3747c478bd9Sstevel@tonic-gate  * Put an entry into the packet
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate static void
supply_out(struct ag_info * ag)3777c478bd9Sstevel@tonic-gate supply_out(struct ag_info *ag)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	uint32_t dstcount;
3807c478bd9Sstevel@tonic-gate 	in_addr_t mask, v1_mask, dst_h, ddst_h = 0;
3817c478bd9Sstevel@tonic-gate 	struct ws_buf *wb;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * Skip this route if doing a flash update and it and the routes
3867c478bd9Sstevel@tonic-gate 	 * it aggregates have not changed recently.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	if (ag->ag_seqno < update_seqno && (ws.state & WS_ST_FLASH))
3897c478bd9Sstevel@tonic-gate 		return;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	dst_h = ag->ag_dst_h;
3927c478bd9Sstevel@tonic-gate 	mask = ag->ag_mask;
3937c478bd9Sstevel@tonic-gate 	v1_mask = ripv1_mask_host(htonl(dst_h),
3947c478bd9Sstevel@tonic-gate 	    (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : NULL);
3957c478bd9Sstevel@tonic-gate 	dstcount = 0;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * If we are sending RIPv2 packets that cannot (or must not) be
3997c478bd9Sstevel@tonic-gate 	 * heard by RIPv1 listeners, do not worry about sub- or supernets.
4007c478bd9Sstevel@tonic-gate 	 * Subnets (from other networks) can only be sent via multicast.
4017c478bd9Sstevel@tonic-gate 	 * A pair of subnet routes might have been promoted so that they
4027c478bd9Sstevel@tonic-gate 	 * are legal to send by RIPv1.
4037c478bd9Sstevel@tonic-gate 	 * If RIPv1 is off, use the multicast buffer.
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	if ((ws.state & WS_ST_RIP2_ALL) ||
4067c478bd9Sstevel@tonic-gate 	    ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) {
4077c478bd9Sstevel@tonic-gate 		/* use the RIPv2-only buffer */
4087c478bd9Sstevel@tonic-gate 		wb = &v2buf;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	} else {
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * use the RIPv1-or-RIPv2 buffer
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		wb = &v12buf;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 		/*
4177c478bd9Sstevel@tonic-gate 		 * Convert supernet route into corresponding set of network
4187c478bd9Sstevel@tonic-gate 		 * routes for RIPv1, but leave non-contiguous netmasks
4197c478bd9Sstevel@tonic-gate 		 * to ag_check().
4207c478bd9Sstevel@tonic-gate 		 */
4217c478bd9Sstevel@tonic-gate 		if (v1_mask > mask &&
4227c478bd9Sstevel@tonic-gate 		    mask + (mask & -mask) == 0) {
4237c478bd9Sstevel@tonic-gate 			ddst_h = v1_mask & -v1_mask;
4247c478bd9Sstevel@tonic-gate 			dstcount = (v1_mask & ~mask)/ddst_h;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 			if (dstcount > ws.gen_limit) {
4277c478bd9Sstevel@tonic-gate 				/*
4287c478bd9Sstevel@tonic-gate 				 * Punt if we would have to generate an
4297c478bd9Sstevel@tonic-gate 				 * unreasonable number of routes.
4307c478bd9Sstevel@tonic-gate 				 */
4317c478bd9Sstevel@tonic-gate 				if (TRACECONTENTS)
4327c478bd9Sstevel@tonic-gate 					trace_misc("sending %s-->%s as 1"
4337c478bd9Sstevel@tonic-gate 					    " instead of %d routes",
4347c478bd9Sstevel@tonic-gate 					    addrname(htonl(dst_h), mask, 1),
4357c478bd9Sstevel@tonic-gate 					    naddr_ntoa(ws.to.sin_addr.s_addr),
4367c478bd9Sstevel@tonic-gate 					    dstcount + 1);
4377c478bd9Sstevel@tonic-gate 				dstcount = 0;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 			} else {
4407c478bd9Sstevel@tonic-gate 				mask = v1_mask;
4417c478bd9Sstevel@tonic-gate 				ws.gen_limit -= dstcount;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	do {
4477c478bd9Sstevel@tonic-gate 		wb->n->n_family = RIP_AF_INET;
4487c478bd9Sstevel@tonic-gate 		wb->n->n_dst = htonl(dst_h);
4497c478bd9Sstevel@tonic-gate 		/*
4507c478bd9Sstevel@tonic-gate 		 * If the route is from router-discovery or we are
4517c478bd9Sstevel@tonic-gate 		 * shutting down, or this is a broken/sick interface,
4527c478bd9Sstevel@tonic-gate 		 * admit only a bad metric.
4537c478bd9Sstevel@tonic-gate 		 */
4547c478bd9Sstevel@tonic-gate 		wb->n->n_metric = ((stopint || ag->ag_metric < 1 ||
4557c478bd9Sstevel@tonic-gate 		    (ag->ag_ifp && (ag->ag_ifp->int_state &
4567c478bd9Sstevel@tonic-gate 		    (IS_BROKE|IS_SICK)))) ? HOPCNT_INFINITY : ag->ag_metric);
4577c478bd9Sstevel@tonic-gate 		wb->n->n_metric = htonl(wb->n->n_metric);
4587c478bd9Sstevel@tonic-gate 		/*
4597c478bd9Sstevel@tonic-gate 		 * Any non-zero bits in the supposedly unused RIPv1 fields
4607c478bd9Sstevel@tonic-gate 		 * cause the old `routed` to ignore the route.
4617c478bd9Sstevel@tonic-gate 		 * That means the mask and so forth cannot be sent
4627c478bd9Sstevel@tonic-gate 		 * in the hybrid RIPv1/RIPv2 mode.
4637c478bd9Sstevel@tonic-gate 		 */
4647c478bd9Sstevel@tonic-gate 		if (ws.state & WS_ST_RIP2_ALL) {
4657c478bd9Sstevel@tonic-gate 			if (ag->ag_nhop != 0 &&
4667c478bd9Sstevel@tonic-gate 			    ((ws.state & WS_ST_QUERY) ||
4677c478bd9Sstevel@tonic-gate 			    (ag->ag_nhop != ws.ifp->int_addr &&
4687c478bd9Sstevel@tonic-gate 			    on_net(ag->ag_nhop, ws.ifp->int_net,
4697c478bd9Sstevel@tonic-gate 			    ws.ifp->int_mask)) &&
4707c478bd9Sstevel@tonic-gate 			    ifwithaddr(ag->ag_nhop, _B_FALSE, _B_FALSE) ==
4717c478bd9Sstevel@tonic-gate 			    NULL))
4727c478bd9Sstevel@tonic-gate 				wb->n->n_nhop = ag->ag_nhop;
4737c478bd9Sstevel@tonic-gate 			wb->n->n_mask = htonl(mask);
4747c478bd9Sstevel@tonic-gate 			wb->n->n_tag = ag->ag_tag;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 		dst_h += ddst_h;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		if (++wb->n >= wb->lim)
4797c478bd9Sstevel@tonic-gate 			supply_write(wb);
4807c478bd9Sstevel@tonic-gate 	} while (dstcount-- > 0);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * Supply one route from the table
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate /* ARGSUSED */
4887c478bd9Sstevel@tonic-gate static int
walk_supply(struct radix_node * rn,void * argp)4897c478bd9Sstevel@tonic-gate walk_supply(struct radix_node *rn, void *argp)
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate #define	RT ((struct rt_entry *)rn)
4927c478bd9Sstevel@tonic-gate 	ushort_t ags;
4937c478bd9Sstevel@tonic-gate 	uint8_t metric, pref;
4947c478bd9Sstevel@tonic-gate 	in_addr_t dst, nhop;
4957c478bd9Sstevel@tonic-gate 	struct rt_spare *rts;
4967c478bd9Sstevel@tonic-gate 	uint_t sparecount;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	/*
5007c478bd9Sstevel@tonic-gate 	 * Do not advertise external remote interfaces or passive interfaces.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	if ((RT->rt_state & RS_IF) && RT->rt_ifp != NULL &&
5037c478bd9Sstevel@tonic-gate 	    (RT->rt_ifp->int_state & IS_PASSIVE) &&
5047c478bd9Sstevel@tonic-gate 	    !(RT->rt_state & RS_MHOME))
5057c478bd9Sstevel@tonic-gate 		return (0);
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * Do not advertise routes learnt from /etc/gateways.
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 	if (RT->rt_spares[0].rts_origin == RO_FILE)
5107c478bd9Sstevel@tonic-gate 		return (0);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Do not advertise routes which would lead to forwarding on a
5147c478bd9Sstevel@tonic-gate 	 * non-forwarding interface.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (RT->rt_state & RS_NOPROPAGATE)
5177c478bd9Sstevel@tonic-gate 		return (0);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * If being quiet about our ability to forward, then
5217c478bd9Sstevel@tonic-gate 	 * do not say anything unless responding to a query,
5227c478bd9Sstevel@tonic-gate 	 * except about our main interface.
5237c478bd9Sstevel@tonic-gate 	 */
5247c478bd9Sstevel@tonic-gate 	if (!should_supply(NULL) && !(ws.state & WS_ST_QUERY) &&
5257c478bd9Sstevel@tonic-gate 	    !(RT->rt_state & RS_MHOME))
5267c478bd9Sstevel@tonic-gate 		return (0);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	dst = RT->rt_dst;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/*
5317c478bd9Sstevel@tonic-gate 	 * do not collide with the fake default route
5327c478bd9Sstevel@tonic-gate 	 */
5337c478bd9Sstevel@tonic-gate 	if (dst == RIP_DEFAULT && (ws.state & WS_ST_DEFAULT))
5347c478bd9Sstevel@tonic-gate 		return (0);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (RT->rt_state & RS_NET_SYN) {
5377c478bd9Sstevel@tonic-gate 		if (RT->rt_state & RS_NET_INT) {
5387c478bd9Sstevel@tonic-gate 			/*
5397c478bd9Sstevel@tonic-gate 			 * Do not send manual synthetic network routes
5407c478bd9Sstevel@tonic-gate 			 * into the subnet.
5417c478bd9Sstevel@tonic-gate 			 */
5427c478bd9Sstevel@tonic-gate 			if (on_net(ws.to.sin_addr.s_addr,
5437c478bd9Sstevel@tonic-gate 			    ntohl(dst), RT->rt_mask))
5447c478bd9Sstevel@tonic-gate 				return (0);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		} else {
5477c478bd9Sstevel@tonic-gate 			/*
5487c478bd9Sstevel@tonic-gate 			 * Do not send automatic synthetic network routes
5497c478bd9Sstevel@tonic-gate 			 * if they are not needed because no RIPv1 listeners
5507c478bd9Sstevel@tonic-gate 			 * can hear them.
5517c478bd9Sstevel@tonic-gate 			 */
5527c478bd9Sstevel@tonic-gate 			if (ws.state & WS_ST_RIP2_ALL)
5537c478bd9Sstevel@tonic-gate 				return (0);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 			/*
5567c478bd9Sstevel@tonic-gate 			 * Do not send automatic synthetic network routes to
5577c478bd9Sstevel@tonic-gate 			 * the real subnet.
5587c478bd9Sstevel@tonic-gate 			 */
5597c478bd9Sstevel@tonic-gate 			if (on_net(ws.to.sin_addr.s_addr,
5607c478bd9Sstevel@tonic-gate 			    ntohl(dst), RT->rt_mask))
5617c478bd9Sstevel@tonic-gate 				return (0);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		nhop = 0;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	} else {
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * Advertise the next hop if this is not a route for one
5687c478bd9Sstevel@tonic-gate 		 * of our interfaces and the next hop is on the same
5697c478bd9Sstevel@tonic-gate 		 * network as the target.
5707c478bd9Sstevel@tonic-gate 		 * The final determination is made by supply_out().
5717c478bd9Sstevel@tonic-gate 		 */
5727c478bd9Sstevel@tonic-gate 		if (!(RT->rt_state & RS_IF) && !(RT->rt_state & RS_MHOME) &&
5737c478bd9Sstevel@tonic-gate 		    RT->rt_gate != loopaddr)
5747c478bd9Sstevel@tonic-gate 			nhop = RT->rt_gate;
5757c478bd9Sstevel@tonic-gate 		else
5767c478bd9Sstevel@tonic-gate 			nhop = 0;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	metric = RT->rt_metric;
5807c478bd9Sstevel@tonic-gate 	ags = 0;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (!RT_ISHOST(RT)) {
5837c478bd9Sstevel@tonic-gate 		/*
5847c478bd9Sstevel@tonic-gate 		 * Always suppress network routes into other, existing
5857c478bd9Sstevel@tonic-gate 		 * network routes
5867c478bd9Sstevel@tonic-gate 		 */
5877c478bd9Sstevel@tonic-gate 		ags |= AGS_SUPPRESS;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * Generate supernets if allowed.
5917c478bd9Sstevel@tonic-gate 		 * If we can be heard by RIPv1 systems, we will
5927c478bd9Sstevel@tonic-gate 		 * later convert back to ordinary nets.
5937c478bd9Sstevel@tonic-gate 		 * This unifies dealing with received supernets.
5947c478bd9Sstevel@tonic-gate 		 */
5957c478bd9Sstevel@tonic-gate 		if ((ws.state & WS_ST_AG) && ((RT->rt_state & RS_SUBNET) ||
5967c478bd9Sstevel@tonic-gate 		    (ws.state & WS_ST_SUPER_AG)))
5977c478bd9Sstevel@tonic-gate 			ags |= AGS_AGGREGATE;
5987c478bd9Sstevel@tonic-gate 	} else if (!(RT->rt_state & RS_MHOME)) {
5997c478bd9Sstevel@tonic-gate 		/*
6007c478bd9Sstevel@tonic-gate 		 * We should always suppress (into existing network routes)
6017c478bd9Sstevel@tonic-gate 		 * the host routes for the local end of our point-to-point
6027c478bd9Sstevel@tonic-gate 		 * links.
6037c478bd9Sstevel@tonic-gate 		 * If we are suppressing host routes in general, then do so.
6047c478bd9Sstevel@tonic-gate 		 * Avoid advertising host routes onto their own network,
6057c478bd9Sstevel@tonic-gate 		 * where they should be handled by proxy-ARP.
6067c478bd9Sstevel@tonic-gate 		 */
6077c478bd9Sstevel@tonic-gate 		if ((RT->rt_state & RS_LOCAL) || ridhosts ||
6087c478bd9Sstevel@tonic-gate 		    on_net(dst, ws.to_net, ws.to_mask))
6097c478bd9Sstevel@tonic-gate 			ags |= AGS_SUPPRESS;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		/*
6127c478bd9Sstevel@tonic-gate 		 * Aggregate stray host routes into network routes if allowed.
6137c478bd9Sstevel@tonic-gate 		 * We cannot aggregate host routes into small network routes
6147c478bd9Sstevel@tonic-gate 		 * without confusing RIPv1 listeners into thinking the
6157c478bd9Sstevel@tonic-gate 		 * network routes are host routes.
6167c478bd9Sstevel@tonic-gate 		 */
6177c478bd9Sstevel@tonic-gate 		if ((ws.state & WS_ST_AG) && (ws.state & WS_ST_RIP2_ALL))
6187c478bd9Sstevel@tonic-gate 			ags |= AGS_AGGREGATE;
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * Do not send RIPv1 advertisements of subnets to other
6237c478bd9Sstevel@tonic-gate 	 * networks. If possible, multicast them by RIPv2.
6247c478bd9Sstevel@tonic-gate 	 */
6257c478bd9Sstevel@tonic-gate 	if ((RT->rt_state & RS_SUBNET) && !(ws.state & WS_ST_RIP2_ALL) &&
6267c478bd9Sstevel@tonic-gate 	    !on_net(dst, ws.to_std_net, ws.to_std_mask))
6277c478bd9Sstevel@tonic-gate 		ags |= AGS_RIPV2 | AGS_AGGREGATE;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/*
6317c478bd9Sstevel@tonic-gate 	 * Do not send a route back to where it came from, except in
6327c478bd9Sstevel@tonic-gate 	 * response to a query.  This is "split-horizon".  That means not
6337c478bd9Sstevel@tonic-gate 	 * advertising back to the same network	and so via the same interface.
6347c478bd9Sstevel@tonic-gate 	 *
6357c478bd9Sstevel@tonic-gate 	 * We want to suppress routes that might have been fragmented
6367c478bd9Sstevel@tonic-gate 	 * from this route by a RIPv1 router and sent back to us, and so we
6377c478bd9Sstevel@tonic-gate 	 * cannot forget this route here.  Let the split-horizon route
6387c478bd9Sstevel@tonic-gate 	 * suppress the fragmented routes and then itself be forgotten.
6397c478bd9Sstevel@tonic-gate 	 *
6407c478bd9Sstevel@tonic-gate 	 * Include the routes for both ends of point-to-point interfaces
6417c478bd9Sstevel@tonic-gate 	 * among those suppressed by split-horizon, since the other side
6427c478bd9Sstevel@tonic-gate 	 * should knows them as well as we do.
6437c478bd9Sstevel@tonic-gate 	 *
6447c478bd9Sstevel@tonic-gate 	 * Notice spare routes with the same metric that we are about to
6457c478bd9Sstevel@tonic-gate 	 * advertise, to split the horizon on redundant, inactive paths.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	if (ws.ifp != NULL && !(ws.state & WS_ST_QUERY) &&
6487c478bd9Sstevel@tonic-gate 	    (ws.state & WS_ST_TO_ON_NET) && (!(RT->rt_state & RS_IF) ||
6497c478bd9Sstevel@tonic-gate 	    (ws.ifp->int_if_flags & IFF_POINTOPOINT))) {
6507c478bd9Sstevel@tonic-gate 		for (rts = RT->rt_spares, sparecount = 0;
6517c478bd9Sstevel@tonic-gate 		    sparecount < RT->rt_num_spares; sparecount++, rts++) {
6527c478bd9Sstevel@tonic-gate 			if (rts->rts_metric > metric || rts->rts_ifp != ws.ifp)
6537c478bd9Sstevel@tonic-gate 				continue;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 			/*
6567c478bd9Sstevel@tonic-gate 			 * If we do not mark the route with AGS_SPLIT_HZ here,
6577c478bd9Sstevel@tonic-gate 			 * it will be poisoned-reverse, or advertised back
6587c478bd9Sstevel@tonic-gate 			 * toward its source with an infinite metric.
6597c478bd9Sstevel@tonic-gate 			 * If we have recently advertised the route with a
6607c478bd9Sstevel@tonic-gate 			 * better metric than we now have, then we should
6617c478bd9Sstevel@tonic-gate 			 * poison-reverse the route before suppressing it for
6627c478bd9Sstevel@tonic-gate 			 * split-horizon.
6637c478bd9Sstevel@tonic-gate 			 *
6647c478bd9Sstevel@tonic-gate 			 * In almost all cases, if there is no spare for the
6657c478bd9Sstevel@tonic-gate 			 * route then it is either old and dead or a brand
6667c478bd9Sstevel@tonic-gate 			 * new route. If it is brand new, there is no need
6677c478bd9Sstevel@tonic-gate 			 * for poison-reverse. If it is old and dead, it
6687c478bd9Sstevel@tonic-gate 			 * is already poisoned.
6697c478bd9Sstevel@tonic-gate 			 */
6707c478bd9Sstevel@tonic-gate 			if (RT->rt_poison_time < now_expire ||
6717c478bd9Sstevel@tonic-gate 			    RT->rt_poison_metric >= metric ||
6727c478bd9Sstevel@tonic-gate 			    RT->rt_spares[1].rts_gate == 0) {
6737c478bd9Sstevel@tonic-gate 				ags |= AGS_SPLIT_HZ;
6747c478bd9Sstevel@tonic-gate 				ags &= ~AGS_SUPPRESS;
6757c478bd9Sstevel@tonic-gate 			}
6767c478bd9Sstevel@tonic-gate 			metric = HOPCNT_INFINITY;
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	/*
6827c478bd9Sstevel@tonic-gate 	 * Keep track of the best metric with which the
6837c478bd9Sstevel@tonic-gate 	 * route has been advertised recently.
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	if (RT->rt_poison_metric >= metric ||
6867c478bd9Sstevel@tonic-gate 	    RT->rt_poison_time < now_expire) {
6877c478bd9Sstevel@tonic-gate 		RT->rt_poison_time = now.tv_sec;
6887c478bd9Sstevel@tonic-gate 		RT->rt_poison_metric = metric;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Adjust the outgoing metric by the cost of the link.
6937c478bd9Sstevel@tonic-gate 	 * Avoid aggregation when a route is counting to infinity.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	pref = RT->rt_poison_metric + ws.metric;
6967c478bd9Sstevel@tonic-gate 	metric += ws.metric;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * If this is a static route pointing to the same interface
7007c478bd9Sstevel@tonic-gate 	 * upon which we are sending out the RIP RESPONSE
7017c478bd9Sstevel@tonic-gate 	 * adjust the preference so that we don't aggregate into this
7027c478bd9Sstevel@tonic-gate 	 * route. Note that the maximum possible hop count on a route
7037c478bd9Sstevel@tonic-gate 	 * per RFC 2453 is 16 (HOPCNT_INFINITY)
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	if ((RT->rt_state & RS_STATIC) && (ws.ifp == RT->rt_ifp))
7067c478bd9Sstevel@tonic-gate 		pref = (HOPCNT_INFINITY+1);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/*
7097c478bd9Sstevel@tonic-gate 	 * Do not advertise stable routes that will be ignored,
7107c478bd9Sstevel@tonic-gate 	 * unless we are answering a query.
7117c478bd9Sstevel@tonic-gate 	 * If the route recently was advertised with a metric that
7127c478bd9Sstevel@tonic-gate 	 * would have been less than infinity through this interface,
7137c478bd9Sstevel@tonic-gate 	 * we need to continue to advertise it in order to poison it.
7147c478bd9Sstevel@tonic-gate 	 */
7157c478bd9Sstevel@tonic-gate 	if (metric >= HOPCNT_INFINITY) {
7167c478bd9Sstevel@tonic-gate 		if (!(ws.state & WS_ST_QUERY) && (pref >= HOPCNT_INFINITY ||
7177c478bd9Sstevel@tonic-gate 		    RT->rt_poison_time < now_garbage))
7187c478bd9Sstevel@tonic-gate 			return (0);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 		metric = HOPCNT_INFINITY;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * supply this route out on the wire- we only care about dest/mask
7257c478bd9Sstevel@tonic-gate 	 * and so can ignore all rt_spares[i] with i > 0
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 	ag_check(dst, RT->rt_mask, 0, RT->rt_ifp, nhop, metric, pref,
7287c478bd9Sstevel@tonic-gate 	    RT->rt_seqno, RT->rt_tag, ags, supply_out);
7297c478bd9Sstevel@tonic-gate 	return (0);
7307c478bd9Sstevel@tonic-gate #undef RT
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate  * Supply dst with the contents of the routing tables.
7367c478bd9Sstevel@tonic-gate  * If this won't fit in one packet, chop it up into several.
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate void
supply(struct sockaddr_in * dst,struct interface * ifp,enum output_type type,int flash,int vers,boolean_t passwd_ok)7397c478bd9Sstevel@tonic-gate supply(struct sockaddr_in *dst,
7407c478bd9Sstevel@tonic-gate     struct interface *ifp,	/* output interface */
7417c478bd9Sstevel@tonic-gate     enum output_type type,
7427c478bd9Sstevel@tonic-gate     int flash,			/* 1=flash update */
7437c478bd9Sstevel@tonic-gate     int vers,			/* RIP version */
7447c478bd9Sstevel@tonic-gate     boolean_t passwd_ok)	/* OK to include cleartext password */
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
7477c478bd9Sstevel@tonic-gate 	uint8_t def_metric;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	ws.state = 0;
7517c478bd9Sstevel@tonic-gate 	ws.gen_limit = WS_GEN_LIMIT_MAX;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	ws.to = *dst;
7547c478bd9Sstevel@tonic-gate 	ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr);
7557c478bd9Sstevel@tonic-gate 	ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (ifp != NULL) {
7587c478bd9Sstevel@tonic-gate 		ws.to_mask = ifp->int_mask;
7597c478bd9Sstevel@tonic-gate 		ws.to_net = ifp->int_net;
7607c478bd9Sstevel@tonic-gate 		if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask) ||
7617c478bd9Sstevel@tonic-gate 		    type == OUT_MULTICAST)
7627c478bd9Sstevel@tonic-gate 			ws.state |= WS_ST_TO_ON_NET;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	} else {
7657c478bd9Sstevel@tonic-gate 		ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, NULL);
7667c478bd9Sstevel@tonic-gate 		ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask;
7677c478bd9Sstevel@tonic-gate 		rt = rtfind(dst->sin_addr.s_addr);
7687c478bd9Sstevel@tonic-gate 		if (rt != NULL)
7697c478bd9Sstevel@tonic-gate 			ifp = rt->rt_ifp;
7707c478bd9Sstevel@tonic-gate 		else
7717c478bd9Sstevel@tonic-gate 			return;
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	ws.npackets = 0;
7757c478bd9Sstevel@tonic-gate 	if (flash)
7767c478bd9Sstevel@tonic-gate 		ws.state |= WS_ST_FLASH;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	ws.ifp = ifp;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * Routes in the table were already adjusted by their respective
7827c478bd9Sstevel@tonic-gate 	 * destination interface costs (which are zero by default) on
7837c478bd9Sstevel@tonic-gate 	 * input.  The following is the value by which each route's metric
7847c478bd9Sstevel@tonic-gate 	 * will be bumped up on output.
7857c478bd9Sstevel@tonic-gate 	 */
7867c478bd9Sstevel@tonic-gate 	ws.metric = 1;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	ripv12_buf.rip.rip_vers = vers;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	switch (type) {
7917c478bd9Sstevel@tonic-gate 	case OUT_MULTICAST:
7927c478bd9Sstevel@tonic-gate 		if (ifp->int_if_flags & IFF_MULTICAST)
7937c478bd9Sstevel@tonic-gate 			v2buf.type = OUT_MULTICAST;
7947c478bd9Sstevel@tonic-gate 		else
7957c478bd9Sstevel@tonic-gate 			v2buf.type = NO_OUT_MULTICAST;
7967c478bd9Sstevel@tonic-gate 		v12buf.type = OUT_BROADCAST;
7977c478bd9Sstevel@tonic-gate 		break;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	case OUT_QUERY:
8007c478bd9Sstevel@tonic-gate 		ws.state |= WS_ST_QUERY;
8017c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
8027c478bd9Sstevel@tonic-gate 	case OUT_BROADCAST:
8037c478bd9Sstevel@tonic-gate 	case OUT_UNICAST:
8047c478bd9Sstevel@tonic-gate 		v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2;
8057c478bd9Sstevel@tonic-gate 		v12buf.type = type;
8067c478bd9Sstevel@tonic-gate 		break;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	case NO_OUT_MULTICAST:
8097c478bd9Sstevel@tonic-gate 	case NO_OUT_RIPV2:
8107c478bd9Sstevel@tonic-gate 		return;			/* no output */
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (vers == RIPv2) {
8147c478bd9Sstevel@tonic-gate 		/* full RIPv2 only if cannot be heard by RIPv1 listeners */
8157c478bd9Sstevel@tonic-gate 		if (type != OUT_BROADCAST)
8167c478bd9Sstevel@tonic-gate 			ws.state |= WS_ST_RIP2_ALL;
8177c478bd9Sstevel@tonic-gate 		if ((ws.state & WS_ST_QUERY) || !(ws.state & WS_ST_TO_ON_NET)) {
8187c478bd9Sstevel@tonic-gate 			ws.state |= (WS_ST_AG | WS_ST_SUPER_AG);
8197c478bd9Sstevel@tonic-gate 		} else if (ifp == NULL || !(ifp->int_state & IS_NO_AG)) {
8207c478bd9Sstevel@tonic-gate 			ws.state |= WS_ST_AG;
8217c478bd9Sstevel@tonic-gate 			if (type != OUT_BROADCAST && (ifp == NULL ||
8227c478bd9Sstevel@tonic-gate 			    !(ifp->int_state & IS_NO_SUPER_AG)))
8237c478bd9Sstevel@tonic-gate 				ws.state |= WS_ST_SUPER_AG;
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		/* See if this packet needs authenticating */
8277c478bd9Sstevel@tonic-gate 		ws.a = find_auth(ifp);
8287c478bd9Sstevel@tonic-gate 		if (!passwd_ok && ws.a != NULL && ws.a->type == RIP_AUTH_PW)
8297c478bd9Sstevel@tonic-gate 			ws.a = NULL;
8307c478bd9Sstevel@tonic-gate 		if (ws.a != NULL && (ulong_t)ws.a->end < (ulong_t)clk.tv_sec &&
8317c478bd9Sstevel@tonic-gate 		    !ws.a->warnedflag) {
8327c478bd9Sstevel@tonic-gate 			/*
8337c478bd9Sstevel@tonic-gate 			 * If the best key is an expired one, we may as
8347c478bd9Sstevel@tonic-gate 			 * well use it.  Log this event.
8357c478bd9Sstevel@tonic-gate 			 */
8367c478bd9Sstevel@tonic-gate 			writelog(LOG_WARNING,
8377c478bd9Sstevel@tonic-gate 			    "Using expired auth while transmitting to %s",
8387c478bd9Sstevel@tonic-gate 			    naddr_ntoa(ws.to.sin_addr.s_addr));
8397c478bd9Sstevel@tonic-gate 			ws.a->warnedflag = 1;
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	} else {
8427c478bd9Sstevel@tonic-gate 		ws.a = NULL;
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	clr_ws_buf(&v12buf, ws.a);
8467c478bd9Sstevel@tonic-gate 	clr_ws_buf(&v2buf, ws.a);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * Fake a default route if asked and if there is not already
8507c478bd9Sstevel@tonic-gate 	 * a better, real default route.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	if (should_supply(NULL) && (def_metric = ifp->int_d_metric) != 0) {
8537c478bd9Sstevel@tonic-gate 		if (NULL == (rt = rtget(RIP_DEFAULT, 0)) ||
8547c478bd9Sstevel@tonic-gate 		    rt->rt_metric+ws.metric >= def_metric) {
8557c478bd9Sstevel@tonic-gate 			ws.state |= WS_ST_DEFAULT;
8567c478bd9Sstevel@tonic-gate 			ag_check(0, 0, 0, NULL, 0, def_metric, def_metric,
8577c478bd9Sstevel@tonic-gate 			    0, 0, 0, supply_out);
8587c478bd9Sstevel@tonic-gate 		} else {
8597c478bd9Sstevel@tonic-gate 			def_metric = rt->rt_metric+ws.metric;
8607c478bd9Sstevel@tonic-gate 		}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		/*
8637c478bd9Sstevel@tonic-gate 		 * If both RIPv2 and the poor-man's router discovery
8647c478bd9Sstevel@tonic-gate 		 * kludge are on, arrange to advertise an extra
8657c478bd9Sstevel@tonic-gate 		 * default route via RIPv1.
8667c478bd9Sstevel@tonic-gate 		 */
8677c478bd9Sstevel@tonic-gate 		if ((ws.state & WS_ST_RIP2_ALL) &&
8687c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_PM_RDISC)) {
8697c478bd9Sstevel@tonic-gate 			ripv12_buf.rip.rip_vers = RIPv1;
8707c478bd9Sstevel@tonic-gate 			v12buf.n->n_family = RIP_AF_INET;
8717c478bd9Sstevel@tonic-gate 			v12buf.n->n_dst = htonl(RIP_DEFAULT);
8727c478bd9Sstevel@tonic-gate 			v12buf.n->n_metric = htonl(def_metric);
8737c478bd9Sstevel@tonic-gate 			v12buf.n++;
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	(void) rn_walktree(rhead, walk_supply, NULL);
8787c478bd9Sstevel@tonic-gate 	ag_flush(0, 0, supply_out);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * Flush the packet buffers, provided they are not empty and
8827c478bd9Sstevel@tonic-gate 	 * do not contain only the password.
8837c478bd9Sstevel@tonic-gate 	 */
8847c478bd9Sstevel@tonic-gate 	if (v12buf.n != v12buf.base &&
8857c478bd9Sstevel@tonic-gate 	    (v12buf.n > v12buf.base+1 ||
8867c478bd9Sstevel@tonic-gate 	    v12buf.base->n_family != RIP_AF_AUTH))
8877c478bd9Sstevel@tonic-gate 		supply_write(&v12buf);
8887c478bd9Sstevel@tonic-gate 	if (v2buf.n != v2buf.base && (v2buf.n > v2buf.base+1 ||
8897c478bd9Sstevel@tonic-gate 	    v2buf.base->n_family != RIP_AF_AUTH))
8907c478bd9Sstevel@tonic-gate 		supply_write(&v2buf);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/*
8937c478bd9Sstevel@tonic-gate 	 * If we sent nothing and this is an answer to a query, send
8947c478bd9Sstevel@tonic-gate 	 * an empty buffer.
8957c478bd9Sstevel@tonic-gate 	 */
8967c478bd9Sstevel@tonic-gate 	if (ws.npackets == 0 && (ws.state & WS_ST_QUERY)) {
8977c478bd9Sstevel@tonic-gate 		supply_write(&v2buf);
8987c478bd9Sstevel@tonic-gate 		if (ws.npackets == 0)
8997c478bd9Sstevel@tonic-gate 			supply_write(&v12buf);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * send all of the routing table or just do a flash update
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate void
rip_bcast(int flash)9087c478bd9Sstevel@tonic-gate rip_bcast(int flash)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	static struct sockaddr_in dst = {AF_INET};
9117c478bd9Sstevel@tonic-gate 	struct interface *ifp;
9127c478bd9Sstevel@tonic-gate 	enum output_type type;
9137c478bd9Sstevel@tonic-gate 	int vers;
9147c478bd9Sstevel@tonic-gate 	struct timeval rtime;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	need_flash = _B_FALSE;
9187c478bd9Sstevel@tonic-gate 	intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME);
9197c478bd9Sstevel@tonic-gate 	no_flash = rtime;
9207c478bd9Sstevel@tonic-gate 	timevaladd(&no_flash, &now);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (!rip_enabled)
9237c478bd9Sstevel@tonic-gate 		return;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	trace_act("send %s and inhibit dynamic updates for %.3f sec",
9267c478bd9Sstevel@tonic-gate 	    flash ? "dynamic update" : "all routes",
9277c478bd9Sstevel@tonic-gate 	    rtime.tv_sec + ((double)rtime.tv_usec)/1000000.0);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * Skip interfaces not doing RIP or for which IP
9327c478bd9Sstevel@tonic-gate 		 * forwarding isn't turned on.  Skip duplicate
9337c478bd9Sstevel@tonic-gate 		 * interfaces, we don't want to generate duplicate
9347c478bd9Sstevel@tonic-gate 		 * packets.  Do try broken interfaces to see if they
9357c478bd9Sstevel@tonic-gate 		 * have healed.
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		if (IS_RIP_OUT_OFF(ifp->int_state) ||
9387c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_DUP) ||
9397c478bd9Sstevel@tonic-gate 		    !IS_IFF_ROUTING(ifp->int_if_flags))
9407c478bd9Sstevel@tonic-gate 			continue;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		/* skip turned off interfaces */
9437c478bd9Sstevel@tonic-gate 		if (!IS_IFF_UP(ifp->int_if_flags))
9447c478bd9Sstevel@tonic-gate 			continue;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		/* skip interfaces we shouldn't use */
9477c478bd9Sstevel@tonic-gate 		if (IS_IFF_QUIET(ifp->int_if_flags))
9487c478bd9Sstevel@tonic-gate 			continue;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1;
9517c478bd9Sstevel@tonic-gate 		dst.sin_addr.s_addr = ifp->int_ripout_addr;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		/*
9547c478bd9Sstevel@tonic-gate 		 * Ignore the interface if it's not broadcast,
9557c478bd9Sstevel@tonic-gate 		 * point-to-point, or remote.  It must be non-broadcast
9567c478bd9Sstevel@tonic-gate 		 * multiaccess, and therefore unsupported.
9577c478bd9Sstevel@tonic-gate 		 */
9587c478bd9Sstevel@tonic-gate 		if (!(ifp->int_if_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) &&
9597c478bd9Sstevel@tonic-gate 		    !(ifp->int_state & IS_REMOTE))
9607c478bd9Sstevel@tonic-gate 			continue;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 		type = (ifp->int_if_flags & IFF_BROADCAST) ?
9637c478bd9Sstevel@tonic-gate 		    OUT_BROADCAST : OUT_UNICAST;
9647c478bd9Sstevel@tonic-gate 		if (vers == RIPv2 && (ifp->int_if_flags & IFF_MULTICAST) &&
9657c478bd9Sstevel@tonic-gate 		    !(ifp->int_state & IS_NO_RIP_MCAST))
9667c478bd9Sstevel@tonic-gate 			type = OUT_MULTICAST;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		supply(&dst, ifp, type, flash, vers, _B_TRUE);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	update_seqno++;			/* all routes are up to date */
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate /*
9767c478bd9Sstevel@tonic-gate  * Ask for routes
9777c478bd9Sstevel@tonic-gate  * Do it only once to an interface, and not even after the interface
9787c478bd9Sstevel@tonic-gate  * was broken and recovered.
9797c478bd9Sstevel@tonic-gate  */
9807c478bd9Sstevel@tonic-gate void
rip_query(void)9817c478bd9Sstevel@tonic-gate rip_query(void)
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	static struct sockaddr_in dst = {AF_INET};
9847c478bd9Sstevel@tonic-gate 	struct interface *ifp;
9857c478bd9Sstevel@tonic-gate 	struct rip buf;
9867c478bd9Sstevel@tonic-gate 	enum output_type type;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	if (!rip_enabled)
9907c478bd9Sstevel@tonic-gate 		return;
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	(void) memset(&buf, 0, sizeof (buf));
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
9957c478bd9Sstevel@tonic-gate 		/*
9967c478bd9Sstevel@tonic-gate 		 * Skip interfaces those already queried.  Do not ask
9977c478bd9Sstevel@tonic-gate 		 * via interfaces through which we don't accept input.
9987c478bd9Sstevel@tonic-gate 		 * Do not ask via interfaces that cannot send RIP
9997c478bd9Sstevel@tonic-gate 		 * packets.  Don't send queries on duplicate
10007c478bd9Sstevel@tonic-gate 		 * interfaces, that would generate duplicate packets
10017c478bd9Sstevel@tonic-gate 		 * on link.  Do try broken interfaces to see if they
10027c478bd9Sstevel@tonic-gate 		 * have healed.
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if (IS_RIP_IN_OFF(ifp->int_state) ||
10057c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_DUP) ||
10067c478bd9Sstevel@tonic-gate 		    ifp->int_query_time != NEVER)
10077c478bd9Sstevel@tonic-gate 			continue;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		/* skip turned off interfaces */
10107c478bd9Sstevel@tonic-gate 		if (!IS_IFF_UP(ifp->int_if_flags))
10117c478bd9Sstevel@tonic-gate 			continue;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		/* skip interfaces we shouldn't use */
10147c478bd9Sstevel@tonic-gate 		if (IS_IFF_QUIET(ifp->int_if_flags))
10157c478bd9Sstevel@tonic-gate 			continue;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		/*
10187c478bd9Sstevel@tonic-gate 		 * Ignore the interface if it's not broadcast,
10197c478bd9Sstevel@tonic-gate 		 * point-to-point, or remote.  It must be non-broadcast
10207c478bd9Sstevel@tonic-gate 		 * multiaccess, and therefore unsupported.
10217c478bd9Sstevel@tonic-gate 		 */
10227c478bd9Sstevel@tonic-gate 		if (!(ifp->int_if_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) &&
10237c478bd9Sstevel@tonic-gate 		    !(ifp->int_state & IS_REMOTE))
10247c478bd9Sstevel@tonic-gate 			continue;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		buf.rip_cmd = RIPCMD_REQUEST;
10277c478bd9Sstevel@tonic-gate 		buf.rip_nets[0].n_family = RIP_AF_UNSPEC;
10287c478bd9Sstevel@tonic-gate 		buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		/*
10317c478bd9Sstevel@tonic-gate 		 * Send a RIPv1 query only if allowed and if we will
10327c478bd9Sstevel@tonic-gate 		 * listen to RIPv1 routers.
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		if ((ifp->int_state & IS_NO_RIPV1_OUT) ||
10357c478bd9Sstevel@tonic-gate 		    (ifp->int_state & IS_NO_RIPV1_IN)) {
10367c478bd9Sstevel@tonic-gate 			buf.rip_vers = RIPv2;
10377c478bd9Sstevel@tonic-gate 		} else {
10387c478bd9Sstevel@tonic-gate 			buf.rip_vers = RIPv1;
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		dst.sin_addr.s_addr = ifp->int_ripout_addr;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		type = (ifp->int_if_flags & IFF_BROADCAST) ?
10447c478bd9Sstevel@tonic-gate 		    OUT_BROADCAST : OUT_UNICAST;
10457c478bd9Sstevel@tonic-gate 		if (buf.rip_vers == RIPv2 &&
10467c478bd9Sstevel@tonic-gate 		    (ifp->int_if_flags & IFF_MULTICAST) &&
10477c478bd9Sstevel@tonic-gate 		    !(ifp->int_state & IS_NO_RIP_MCAST))
10487c478bd9Sstevel@tonic-gate 			type = OUT_MULTICAST;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL;
10517c478bd9Sstevel@tonic-gate 		if (output(type, &dst, ifp, &buf, sizeof (buf)) < 0)
10527c478bd9Sstevel@tonic-gate 			if_sick(ifp, _B_FALSE);
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate }
1055