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