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/sysctl.h> 70 #include <sys/queue.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/mbuf.h> 74 #include <sys/rwlock.h> 75 #include <sys/syslog.h> 76 #include <sys/callout.h> 77 78 #include <net/if.h> 79 #include <net/if_var.h> 80 #include <net/route.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_node_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 struct radix_node *ret; 112 113 RADIX_NODE_HEAD_WLOCK_ASSERT(head); 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_rmx.rmx_mtu && rt->rt_ifp) 140 rt->rt_rmx.rmx_mtu = IN6_LINKMTU(rt->rt_ifp); 141 142 ret = rn_addroute(v_arg, n_arg, head, treenodes); 143 if (ret == NULL) { 144 struct rtentry *rt2; 145 /* 146 * We are trying to add a net route, but can't. 147 * The following case should be allowed, so we'll make a 148 * special check for this: 149 * Two IPv6 addresses with the same prefix is assigned 150 * to a single interrface. 151 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 152 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 153 * In this case, (*1) and (*2) want to add the same 154 * net route entry, 3ffe:0501:: -> if0. 155 * This case should not raise an error. 156 */ 157 rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED, 158 rt->rt_fibnum); 159 if (rt2) { 160 if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0) 161 && rt2->rt_gateway 162 && rt2->rt_gateway->sa_family == AF_LINK 163 && rt2->rt_ifp == rt->rt_ifp) { 164 ret = rt2->rt_nodes; 165 } 166 RTFREE_LOCKED(rt2); 167 } 168 } 169 return (ret); 170 } 171 172 SYSCTL_DECL(_net_inet6_ip6); 173 174 static VNET_DEFINE(int, rtq_toomany6) = 128; 175 /* 128 cached routes is ``too many'' */ 176 #define V_rtq_toomany6 VNET(rtq_toomany6) 177 SYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 178 &VNET_NAME(rtq_toomany6) , 0, ""); 179 180 struct rtqk_arg { 181 struct radix_node_head *rnh; 182 int mode; 183 int updating; 184 int draining; 185 int killed; 186 int found; 187 time_t nextstop; 188 }; 189 190 /* 191 * Age old PMTUs. 192 */ 193 struct mtuex_arg { 194 struct radix_node_head *rnh; 195 time_t nextstop; 196 }; 197 static VNET_DEFINE(struct callout, rtq_mtutimer); 198 #define V_rtq_mtutimer VNET(rtq_mtutimer) 199 200 static int 201 in6_mtuexpire(struct radix_node *rn, void *rock) 202 { 203 struct rtentry *rt = (struct rtentry *)rn; 204 struct mtuex_arg *ap = rock; 205 206 /* sanity */ 207 if (!rt) 208 panic("rt == NULL in in6_mtuexpire"); 209 210 if (rt->rt_rmx.rmx_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 211 if (rt->rt_rmx.rmx_expire <= time_uptime) { 212 rt->rt_flags |= RTF_PROBEMTU; 213 } else { 214 ap->nextstop = lmin(ap->nextstop, 215 rt->rt_rmx.rmx_expire); 216 } 217 } 218 219 return 0; 220 } 221 222 #define MTUTIMO_DEFAULT (60*1) 223 224 static void 225 in6_mtutimo_one(struct radix_node_head *rnh) 226 { 227 struct mtuex_arg arg; 228 229 arg.rnh = rnh; 230 arg.nextstop = time_uptime + MTUTIMO_DEFAULT; 231 RADIX_NODE_HEAD_LOCK(rnh); 232 rnh->rnh_walktree(rnh, in6_mtuexpire, &arg); 233 RADIX_NODE_HEAD_UNLOCK(rnh); 234 } 235 236 static void 237 in6_mtutimo(void *rock) 238 { 239 CURVNET_SET_QUIET((struct vnet *) rock); 240 struct radix_node_head *rnh; 241 struct timeval atv; 242 u_int fibnum; 243 244 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 245 rnh = rt_tables_get_rnh(fibnum, AF_INET6); 246 if (rnh != NULL) 247 in6_mtutimo_one(rnh); 248 } 249 250 atv.tv_sec = MTUTIMO_DEFAULT; 251 atv.tv_usec = 0; 252 callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 253 CURVNET_RESTORE(); 254 } 255 256 /* 257 * Initialize our routing tree. 258 * XXX MRT When off == 0, we are being called from vfs_export.c 259 * so just set up their table and leave. (we know what the correct 260 * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd 261 * see also comments in in_inithead() vfs_export.c and domain.h 262 */ 263 static VNET_DEFINE(int, _in6_rt_was_here); 264 #define V__in6_rt_was_here VNET(_in6_rt_was_here) 265 266 int 267 in6_inithead(void **head, int off) 268 { 269 struct radix_node_head *rnh; 270 271 if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 272 return 0; /* See above */ 273 274 if (off == 0) /* See above */ 275 return 1; /* only do the rest for the real thing */ 276 277 rnh = *head; 278 rnh->rnh_addaddr = in6_addroute; 279 280 if (V__in6_rt_was_here == 0) { 281 callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE); 282 in6_mtutimo(curvnet); /* kick off timeout first time */ 283 V__in6_rt_was_here = 1; 284 } 285 286 return 1; 287 } 288 289 #ifdef VIMAGE 290 int 291 in6_detachhead(void **head, int off) 292 { 293 294 callout_drain(&V_rtq_mtutimer); 295 return (1); 296 } 297 #endif 298 299 /* 300 * Extended API for IPv6 FIB support. 301 */ 302 void 303 in6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm, 304 int flags, struct sockaddr *src, u_int fibnum) 305 { 306 307 rtredirect_fib(dst, gw, nm, flags, src, fibnum); 308 } 309 310 int 311 in6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw, 312 struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum) 313 { 314 315 return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum)); 316 } 317 318 void 319 in6_rtalloc(struct route_in6 *ro, u_int fibnum) 320 { 321 322 rtalloc_ign_fib((struct route *)ro, 0ul, fibnum); 323 } 324 325 void 326 in6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum) 327 { 328 329 rtalloc_ign_fib((struct route *)ro, ignflags, fibnum); 330 } 331 332 struct rtentry * 333 in6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 334 { 335 336 return (rtalloc1_fib(dst, report, ignflags, fibnum)); 337 } 338