1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * Routing Table Management Daemon
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate #include "defs.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * Apply the function "supply" to all active
44*7c478bd9Sstevel@tonic-gate  * interfaces with a link-local address.
45*7c478bd9Sstevel@tonic-gate  */
46*7c478bd9Sstevel@tonic-gate void
supplyall(struct sockaddr_in6 * sin6,int rtstate,struct interface * skipif,boolean_t splith)47*7c478bd9Sstevel@tonic-gate supplyall(struct sockaddr_in6 *sin6, int rtstate, struct interface *skipif,
48*7c478bd9Sstevel@tonic-gate     boolean_t splith)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
53*7c478bd9Sstevel@tonic-gate 		if ((ifp->int_flags & RIP6_IFF_UP) == 0)
54*7c478bd9Sstevel@tonic-gate 			continue;
55*7c478bd9Sstevel@tonic-gate 		if (ifp->int_flags & RIP6_IFF_NORTEXCH) {
56*7c478bd9Sstevel@tonic-gate 			if (tracing & OUTPUT_BIT) {
57*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ftrace,
58*7c478bd9Sstevel@tonic-gate 				    "Suppress sending RIPng response packet "
59*7c478bd9Sstevel@tonic-gate 				    "on %s (no route exchange on interface)\n",
60*7c478bd9Sstevel@tonic-gate 				    ifp->int_name);
61*7c478bd9Sstevel@tonic-gate 				(void) fflush(ftrace);
62*7c478bd9Sstevel@tonic-gate 			}
63*7c478bd9Sstevel@tonic-gate 			continue;
64*7c478bd9Sstevel@tonic-gate 		}
65*7c478bd9Sstevel@tonic-gate 		if (ifp->int_sock == -1)
66*7c478bd9Sstevel@tonic-gate 			continue;
67*7c478bd9Sstevel@tonic-gate 		if (ifp == skipif)
68*7c478bd9Sstevel@tonic-gate 			continue;
69*7c478bd9Sstevel@tonic-gate 		if (!IN6_IS_ADDR_LINKLOCAL(&ifp->int_addr))
70*7c478bd9Sstevel@tonic-gate 			continue;
71*7c478bd9Sstevel@tonic-gate 		supply(sin6, ifp, rtstate, splith);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static void
solicit(struct sockaddr_in6 * sin6,struct interface * ifp)76*7c478bd9Sstevel@tonic-gate solicit(struct sockaddr_in6 *sin6, struct interface *ifp)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	msg->rip6_cmd = RIPCMD6_REQUEST;
79*7c478bd9Sstevel@tonic-gate 	msg->rip6_vers = RIPVERSION6;
80*7c478bd9Sstevel@tonic-gate 	msg->rip6_nets[0].rip6_prefix = in6addr_any;
81*7c478bd9Sstevel@tonic-gate 	msg->rip6_nets[0].rip6_prefix_length = 0;
82*7c478bd9Sstevel@tonic-gate 	msg->rip6_nets[0].rip6_metric = HOPCNT_INFINITY;
83*7c478bd9Sstevel@tonic-gate 	sendpacket(sin6, ifp, sizeof (struct rip6), 0);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate void
solicitall(struct sockaddr_in6 * sin6)87*7c478bd9Sstevel@tonic-gate solicitall(struct sockaddr_in6 *sin6)
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	struct interface *ifp;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) {
92*7c478bd9Sstevel@tonic-gate 		if ((ifp->int_flags & RIP6_IFF_UP) == 0)
93*7c478bd9Sstevel@tonic-gate 			continue;
94*7c478bd9Sstevel@tonic-gate 		if (ifp->int_flags & RIP6_IFF_NORTEXCH) {
95*7c478bd9Sstevel@tonic-gate 			if (tracing & OUTPUT_BIT) {
96*7c478bd9Sstevel@tonic-gate 				(void) fprintf(ftrace,
97*7c478bd9Sstevel@tonic-gate 				    "Suppress sending RIPng request packet "
98*7c478bd9Sstevel@tonic-gate 				    "on %s (no route exchange on interface)\n",
99*7c478bd9Sstevel@tonic-gate 				    ifp->int_name);
100*7c478bd9Sstevel@tonic-gate 				(void) fflush(ftrace);
101*7c478bd9Sstevel@tonic-gate 			}
102*7c478bd9Sstevel@tonic-gate 			continue;
103*7c478bd9Sstevel@tonic-gate 		}
104*7c478bd9Sstevel@tonic-gate 		if (ifp->int_sock == -1)
105*7c478bd9Sstevel@tonic-gate 			continue;
106*7c478bd9Sstevel@tonic-gate 		solicit(sin6, ifp);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /*
112*7c478bd9Sstevel@tonic-gate  * Output a preformed packet.
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
115*7c478bd9Sstevel@tonic-gate void
sendpacket(struct sockaddr_in6 * sin6,struct interface * ifp,int size,int flags)116*7c478bd9Sstevel@tonic-gate sendpacket(struct sockaddr_in6 *sin6, struct interface *ifp, int size,
117*7c478bd9Sstevel@tonic-gate     int flags)
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	if (sendto(ifp->int_sock, packet, size, flags,
120*7c478bd9Sstevel@tonic-gate 	    (struct sockaddr *)sin6, sizeof (*sin6)) < 0) {
121*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "sendpacket: sendto: %m");
122*7c478bd9Sstevel@tonic-gate 		return;
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 	TRACE_OUTPUT(ifp, sin6, sizeof (struct rip6));
125*7c478bd9Sstevel@tonic-gate 	ifp->int_opackets++;
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate  * Supply dst with the contents of the routing tables.
130*7c478bd9Sstevel@tonic-gate  * If this won't fit in one packet, chop it up into several.
131*7c478bd9Sstevel@tonic-gate  */
132*7c478bd9Sstevel@tonic-gate void
supply(struct sockaddr_in6 * sin6,struct interface * ifp,int rtstate,boolean_t splith)133*7c478bd9Sstevel@tonic-gate supply(struct sockaddr_in6 *sin6, struct interface *ifp, int rtstate,
134*7c478bd9Sstevel@tonic-gate     boolean_t splith)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	struct rt_entry *rt;
137*7c478bd9Sstevel@tonic-gate 	struct netinfo6 *n = msg->rip6_nets;
138*7c478bd9Sstevel@tonic-gate 	struct rthash *rh;
139*7c478bd9Sstevel@tonic-gate 	int size, i, maxsize;
140*7c478bd9Sstevel@tonic-gate 	uint8_t rtmetric;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	msg->rip6_cmd = RIPCMD6_RESPONSE;
143*7c478bd9Sstevel@tonic-gate 	msg->rip6_vers = RIPVERSION6;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/*
146*7c478bd9Sstevel@tonic-gate 	 * Initialize maxsize to the size of the largest RIPng packet supported
147*7c478bd9Sstevel@tonic-gate 	 * on the outgoing interface.
148*7c478bd9Sstevel@tonic-gate 	 */
149*7c478bd9Sstevel@tonic-gate 	maxsize = ifp->int_mtu - sizeof (ip6_t) - sizeof (struct udphdr);
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	for (i = IPV6_ABITS; i >= 0; i--) {
152*7c478bd9Sstevel@tonic-gate 		if (net_hashes[i] == NULL)
153*7c478bd9Sstevel@tonic-gate 			continue;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 		for (rh = net_hashes[i]; rh < &net_hashes[i][ROUTEHASHSIZ];
156*7c478bd9Sstevel@tonic-gate 		    rh++) {
157*7c478bd9Sstevel@tonic-gate 			for (rt = rh->rt_forw; rt != (struct rt_entry *)rh;
158*7c478bd9Sstevel@tonic-gate 			    rt = rt->rt_forw) {
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 				if (IN6_IS_ADDR_LINKLOCAL(&rt->rt_dst))
161*7c478bd9Sstevel@tonic-gate 					continue;
162*7c478bd9Sstevel@tonic-gate 				if (IN6_IS_ADDR_UNSPECIFIED(&rt->rt_dst))
163*7c478bd9Sstevel@tonic-gate 					continue;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 				/* do not send if private */
166*7c478bd9Sstevel@tonic-gate 				if (rt->rt_state & RTS_PRIVATE)
167*7c478bd9Sstevel@tonic-gate 					continue;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 				/*
170*7c478bd9Sstevel@tonic-gate 				 * Don't resend the information
171*7c478bd9Sstevel@tonic-gate 				 * on the network from which it was received.
172*7c478bd9Sstevel@tonic-gate 				 */
173*7c478bd9Sstevel@tonic-gate 				if (splith && rt->rt_ifp != NULL &&
174*7c478bd9Sstevel@tonic-gate 				    strcmp(ifp->int_ifbase,
175*7c478bd9Sstevel@tonic-gate 					rt->rt_ifp->int_ifbase) == 0) {
176*7c478bd9Sstevel@tonic-gate 					if (dopoison)
177*7c478bd9Sstevel@tonic-gate 						rtmetric = HOPCNT_INFINITY;
178*7c478bd9Sstevel@tonic-gate 					else
179*7c478bd9Sstevel@tonic-gate 						continue;
180*7c478bd9Sstevel@tonic-gate 				} else {
181*7c478bd9Sstevel@tonic-gate 					rtmetric = rt->rt_metric;
182*7c478bd9Sstevel@tonic-gate 				}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 				/*
185*7c478bd9Sstevel@tonic-gate 				 * For dynamic updates, limit update to routes
186*7c478bd9Sstevel@tonic-gate 				 * with the specified state.
187*7c478bd9Sstevel@tonic-gate 				 */
188*7c478bd9Sstevel@tonic-gate 				if (rtstate != 0 &&
189*7c478bd9Sstevel@tonic-gate 				    (rt->rt_state & rtstate) == 0)
190*7c478bd9Sstevel@tonic-gate 					continue;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 				/*
193*7c478bd9Sstevel@tonic-gate 				 * Check if there is space for another RTE.  If
194*7c478bd9Sstevel@tonic-gate 				 * not, send the packet built up and reset n for
195*7c478bd9Sstevel@tonic-gate 				 * the remaining RTEs.
196*7c478bd9Sstevel@tonic-gate 				 */
197*7c478bd9Sstevel@tonic-gate 				size = (char *)n - packet;
198*7c478bd9Sstevel@tonic-gate 				if (size > maxsize - sizeof (struct netinfo6)) {
199*7c478bd9Sstevel@tonic-gate 					sendpacket(sin6, ifp, size, 0);
200*7c478bd9Sstevel@tonic-gate 					TRACE_OUTPUT(ifp, sin6, size);
201*7c478bd9Sstevel@tonic-gate 					n = msg->rip6_nets;
202*7c478bd9Sstevel@tonic-gate 				}
203*7c478bd9Sstevel@tonic-gate 				n->rip6_prefix = rt->rt_dst;
204*7c478bd9Sstevel@tonic-gate 				n->rip6_route_tag = rt->rt_tag;
205*7c478bd9Sstevel@tonic-gate 				n->rip6_prefix_length = rt->rt_prefix_length;
206*7c478bd9Sstevel@tonic-gate 				n->rip6_metric = min(rtmetric, HOPCNT_INFINITY);
207*7c478bd9Sstevel@tonic-gate 				n++;
208*7c478bd9Sstevel@tonic-gate 			} /* end of hash chain */
209*7c478bd9Sstevel@tonic-gate 		} /* end of particular prefix length */
210*7c478bd9Sstevel@tonic-gate 	} /* end of all prefix lengths */
211*7c478bd9Sstevel@tonic-gate 	if (n != msg->rip6_nets) {
212*7c478bd9Sstevel@tonic-gate 		size = (char *)n - packet;
213*7c478bd9Sstevel@tonic-gate 		sendpacket(sin6, ifp, size, 0);
214*7c478bd9Sstevel@tonic-gate 		TRACE_OUTPUT(ifp, sin6, size);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate }
217