in6_rmx.c revision 286458
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: head/sys/netinet6/in6_rmx.c 286458 2015-08-08 18:14:59Z melifaro $"); 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 81#include <netinet/in.h> 82#include <netinet/ip_var.h> 83#include <netinet/in_var.h> 84 85#include <netinet/ip6.h> 86#include <netinet6/ip6_var.h> 87 88#include <netinet/icmp6.h> 89#include <netinet6/nd6.h> 90 91#include <netinet/tcp.h> 92#include <netinet/tcp_seq.h> 93#include <netinet/tcp_timer.h> 94#include <netinet/tcp_var.h> 95 96extern int in6_inithead(void **head, int off); 97#ifdef VIMAGE 98extern int in6_detachhead(void **head, int off); 99#endif 100 101/* 102 * Do what we need to do when inserting a route. 103 */ 104static struct radix_node * 105in6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 106 struct radix_node *treenodes) 107{ 108 struct rtentry *rt = (struct rtentry *)treenodes; 109 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt); 110 struct radix_node *ret; 111 112 RADIX_NODE_HEAD_WLOCK_ASSERT(head); 113 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 114 rt->rt_flags |= RTF_MULTICAST; 115 116 /* 117 * A little bit of help for both IPv6 output and input: 118 * For local addresses, we make sure that RTF_LOCAL is set, 119 * with the thought that this might one day be used to speed up 120 * ip_input(). 121 * 122 * We also mark routes to multicast addresses as such, because 123 * it's easy to do and might be useful (but this is much more 124 * dubious since it's so easy to inspect the address). (This 125 * is done above.) 126 * 127 * XXX 128 * should elaborate the code. 129 */ 130 if (rt->rt_flags & RTF_HOST) { 131 if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr) 132 ->sin6_addr, 133 &sin6->sin6_addr)) { 134 rt->rt_flags |= RTF_LOCAL; 135 } 136 } 137 138 if (rt->rt_ifp != NULL) { 139 140 /* 141 * Check route MTU: 142 * inherit interface MTU if not set or 143 * check if MTU is too large. 144 */ 145 if (rt->rt_mtu == 0) { 146 rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 147 } else if (rt->rt_mtu > IN6_LINKMTU(rt->rt_ifp)) 148 rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 149 } 150 151 ret = rn_addroute(v_arg, n_arg, head, treenodes); 152 if (ret == NULL) { 153 struct rtentry *rt2; 154 /* 155 * We are trying to add a net route, but can't. 156 * The following case should be allowed, so we'll make a 157 * special check for this: 158 * Two IPv6 addresses with the same prefix is assigned 159 * to a single interrface. 160 * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 161 * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 162 * In this case, (*1) and (*2) want to add the same 163 * net route entry, 3ffe:0501:: -> if0. 164 * This case should not raise an error. 165 */ 166 rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED, 167 rt->rt_fibnum); 168 if (rt2) { 169 if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0) 170 && rt2->rt_gateway 171 && rt2->rt_gateway->sa_family == AF_LINK 172 && rt2->rt_ifp == rt->rt_ifp) { 173 ret = rt2->rt_nodes; 174 } 175 RTFREE_LOCKED(rt2); 176 } 177 } 178 return (ret); 179} 180 181/* 182 * Age old PMTUs. 183 */ 184struct mtuex_arg { 185 struct radix_node_head *rnh; 186 time_t nextstop; 187}; 188static VNET_DEFINE(struct callout, rtq_mtutimer); 189#define V_rtq_mtutimer VNET(rtq_mtutimer) 190 191static int 192in6_mtuexpire(struct rtentry *rt, void *rock) 193{ 194 struct mtuex_arg *ap = rock; 195 196 if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 197 if (rt->rt_expire <= time_uptime) { 198 rt->rt_flags |= RTF_PROBEMTU; 199 } else { 200 ap->nextstop = lmin(ap->nextstop, rt->rt_expire); 201 } 202 } 203 204 return (0); 205} 206 207#define MTUTIMO_DEFAULT (60*1) 208 209static void 210in6_mtutimo_setwa(struct radix_node_head *rnh, uint32_t fibum, int af, void *_arg) 211{ 212 struct mtuex_arg *arg; 213 214 arg = (struct mtuex_arg *)_arg; 215 216 arg->rnh = rnh; 217} 218 219static void 220in6_mtutimo(void *rock) 221{ 222 CURVNET_SET_QUIET((struct vnet *) rock); 223 struct timeval atv; 224 struct mtuex_arg arg; 225 226 rt_foreach_fib(AF_INET6, in6_mtutimo_setwa, in6_mtuexpire, &arg); 227 228 atv.tv_sec = MTUTIMO_DEFAULT; 229 atv.tv_usec = 0; 230 callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 231 CURVNET_RESTORE(); 232} 233 234/* 235 * Initialize our routing tree. 236 */ 237static VNET_DEFINE(int, _in6_rt_was_here); 238#define V__in6_rt_was_here VNET(_in6_rt_was_here) 239 240int 241in6_inithead(void **head, int off) 242{ 243 struct radix_node_head *rnh; 244 245 if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 246 return (0); 247 248 rnh = *head; 249 RADIX_NODE_HEAD_LOCK_INIT(rnh); 250 251 rnh->rnh_addaddr = in6_addroute; 252 253 if (V__in6_rt_was_here == 0) { 254 callout_init(&V_rtq_mtutimer, 1); 255 in6_mtutimo(curvnet); /* kick off timeout first time */ 256 V__in6_rt_was_here = 1; 257 } 258 259 return (1); 260} 261 262#ifdef VIMAGE 263int 264in6_detachhead(void **head, int off) 265{ 266 267 callout_drain(&V_rtq_mtutimer); 268 return (1); 269} 270#endif 271 272/* 273 * Extended API for IPv6 FIB support. 274 */ 275void 276in6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm, 277 int flags, struct sockaddr *src, u_int fibnum) 278{ 279 280 rtredirect_fib(dst, gw, nm, flags, src, fibnum); 281} 282 283int 284in6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw, 285 struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum) 286{ 287 288 return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum)); 289} 290 291void 292in6_rtalloc(struct route_in6 *ro, u_int fibnum) 293{ 294 295 rtalloc_ign_fib((struct route *)ro, 0ul, fibnum); 296} 297 298void 299in6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum) 300{ 301 302 rtalloc_ign_fib((struct route *)ro, ignflags, fibnum); 303} 304 305struct rtentry * 306in6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 307{ 308 309 return (rtalloc1_fib(dst, report, ignflags, fibnum)); 310} 311