xref: /freebsd/sys/netinet6/in6_rmx.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	$KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $
32  */
33 
34 /*-
35  * Copyright 1994, 1995 Massachusetts Institute of Technology
36  *
37  * Permission to use, copy, modify, and distribute this software and
38  * its documentation for any purpose and without fee is hereby
39  * granted, provided that both the above copyright notice and this
40  * permission notice appear in all copies, that both the above
41  * copyright notice and this permission notice appear in all
42  * supporting documentation, and that the name of M.I.T. not be used
43  * in advertising or publicity pertaining to distribution of the
44  * software without specific, written prior permission.  M.I.T. makes
45  * no representations about the suitability of this software for any
46  * purpose.  It is provided "as is" without express or implied
47  * warranty.
48  *
49  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
50  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
51  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
53  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  */
63 
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/lock.h>
71 #include <sys/queue.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/mbuf.h>
75 #include <sys/rwlock.h>
76 #include <sys/syslog.h>
77 #include <sys/callout.h>
78 
79 #include <net/if.h>
80 #include <net/if_var.h>
81 #include <net/route.h>
82 #include <net/route_var.h>
83 
84 #include <netinet/in.h>
85 #include <netinet/ip_var.h>
86 #include <netinet/in_var.h>
87 
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 
91 #include <netinet/icmp6.h>
92 #include <netinet6/nd6.h>
93 
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_seq.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 
99 extern int	in6_inithead(void **head, int off);
100 #ifdef VIMAGE
101 extern int	in6_detachhead(void **head, int off);
102 #endif
103 
104 /*
105  * Do what we need to do when inserting a route.
106  */
107 static struct radix_node *
108 in6_addroute(void *v_arg, void *n_arg, struct radix_head *head,
109     struct radix_node *treenodes)
110 {
111 	struct rtentry *rt = (struct rtentry *)treenodes;
112 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt);
113 
114 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
115 		rt->rt_flags |= RTF_MULTICAST;
116 
117 	/*
118 	 * A little bit of help for both IPv6 output and input:
119 	 *   For local addresses, we make sure that RTF_LOCAL is set,
120 	 *   with the thought that this might one day be used to speed up
121 	 *   ip_input().
122 	 *
123 	 * We also mark routes to multicast addresses as such, because
124 	 * it's easy to do and might be useful (but this is much more
125 	 * dubious since it's so easy to inspect the address).  (This
126 	 * is done above.)
127 	 *
128 	 * XXX
129 	 * should elaborate the code.
130 	 */
131 	if (rt->rt_flags & RTF_HOST) {
132 		if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr)
133 					->sin6_addr,
134 				       &sin6->sin6_addr)) {
135 			rt->rt_flags |= RTF_LOCAL;
136 		}
137 	}
138 
139 	if (rt->rt_ifp != NULL) {
140 
141 		/*
142 		 * Check route MTU:
143 		 * inherit interface MTU if not set or
144 		 * check if MTU is too large.
145 		 */
146 		if (rt->rt_mtu == 0) {
147 			rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp);
148 		} else if (rt->rt_mtu > IN6_LINKMTU(rt->rt_ifp))
149 			rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp);
150 	}
151 
152 	return (rn_addroute(v_arg, n_arg, head, treenodes));
153 }
154 
155 /*
156  * Age old PMTUs.
157  */
158 struct mtuex_arg {
159 	struct rib_head *rnh;
160 	time_t nextstop;
161 };
162 VNET_DEFINE_STATIC(struct callout, rtq_mtutimer);
163 #define	V_rtq_mtutimer			VNET(rtq_mtutimer)
164 
165 static int
166 in6_mtuexpire(struct rtentry *rt, void *rock)
167 {
168 	struct mtuex_arg *ap = rock;
169 
170 	if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) {
171 		if (rt->rt_expire <= time_uptime) {
172 			rt->rt_flags |= RTF_PROBEMTU;
173 		} else {
174 			ap->nextstop = lmin(ap->nextstop, rt->rt_expire);
175 		}
176 	}
177 
178 	return (0);
179 }
180 
181 #define	MTUTIMO_DEFAULT	(60*1)
182 
183 static void
184 in6_mtutimo_setwa(struct rib_head *rnh, uint32_t fibum, int af,
185     void *_arg)
186 {
187 	struct mtuex_arg *arg;
188 
189 	arg = (struct mtuex_arg *)_arg;
190 
191 	arg->rnh = rnh;
192 }
193 
194 static void
195 in6_mtutimo(void *rock)
196 {
197 	CURVNET_SET_QUIET((struct vnet *) rock);
198 	struct timeval atv;
199 	struct mtuex_arg arg;
200 
201 	rt_foreach_fib_walk(AF_INET6, in6_mtutimo_setwa, in6_mtuexpire, &arg);
202 
203 	atv.tv_sec = MTUTIMO_DEFAULT;
204 	atv.tv_usec = 0;
205 	callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock);
206 	CURVNET_RESTORE();
207 }
208 
209 /*
210  * Initialize our routing tree.
211  */
212 VNET_DEFINE_STATIC(int, _in6_rt_was_here);
213 #define	V__in6_rt_was_here	VNET(_in6_rt_was_here)
214 
215 int
216 in6_inithead(void **head, int off)
217 {
218 	struct rib_head *rh;
219 
220 	rh = rt_table_init(offsetof(struct sockaddr_in6, sin6_addr) << 3);
221 	if (rh == NULL)
222 		return (0);
223 
224 	rh->rnh_addaddr = in6_addroute;
225 	*head = (void *)rh;
226 
227 	if (V__in6_rt_was_here == 0) {
228 		callout_init(&V_rtq_mtutimer, 1);
229 		in6_mtutimo(curvnet);	/* kick off timeout first time */
230 		V__in6_rt_was_here = 1;
231 	}
232 
233 	return (1);
234 }
235 
236 #ifdef VIMAGE
237 int
238 in6_detachhead(void **head, int off)
239 {
240 
241 	callout_drain(&V_rtq_mtutimer);
242 	rt_table_destroy((struct rib_head *)(*head));
243 
244 	return (1);
245 }
246 #endif
247 
248 /*
249  * Extended API for IPv6 FIB support.
250  */
251 void
252 in6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm,
253     int flags, struct sockaddr *src, u_int fibnum)
254 {
255 
256 	rtredirect_fib(dst, gw, nm, flags, src, fibnum);
257 }
258 
259 int
260 in6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw,
261     struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum)
262 {
263 
264 	return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum));
265 }
266 
267 void
268 in6_rtalloc(struct route_in6 *ro, u_int fibnum)
269 {
270 
271 	rtalloc_ign_fib((struct route *)ro, 0ul, fibnum);
272 }
273 
274 void
275 in6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum)
276 {
277 
278 	rtalloc_ign_fib((struct route *)ro, ignflags, fibnum);
279 }
280 
281 struct rtentry *
282 in6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
283 {
284 
285 	return (rtalloc1_fib(dst, report, ignflags, fibnum));
286 }
287