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