1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $ 3053541Sshin */ 3153541Sshin 32139826Simp/*- 3353541Sshin * Copyright 1994, 1995 Massachusetts Institute of Technology 3453541Sshin * 3553541Sshin * Permission to use, copy, modify, and distribute this software and 3653541Sshin * its documentation for any purpose and without fee is hereby 3753541Sshin * granted, provided that both the above copyright notice and this 3853541Sshin * permission notice appear in all copies, that both the above 3953541Sshin * copyright notice and this permission notice appear in all 4053541Sshin * supporting documentation, and that the name of M.I.T. not be used 4153541Sshin * in advertising or publicity pertaining to distribution of the 4253541Sshin * software without specific, written prior permission. M.I.T. makes 4353541Sshin * no representations about the suitability of this software for any 4453541Sshin * purpose. It is provided "as is" without express or implied 4553541Sshin * warranty. 4653541Sshin * 4753541Sshin * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 4853541Sshin * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 4953541Sshin * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 5053541Sshin * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 5153541Sshin * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 5253541Sshin * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 5353541Sshin * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 5453541Sshin * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 5553541Sshin * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 5653541Sshin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 5753541Sshin * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5853541Sshin * SUCH DAMAGE. 5953541Sshin * 6053541Sshin */ 6153541Sshin 62174510Sobrien#include <sys/cdefs.h> 63174510Sobrien__FBSDID("$FreeBSD: releng/10.3/sys/netinet6/in6_rmx.c 295389 2016-02-08 00:07:01Z bz $"); 64174510Sobrien 6553541Sshin#include <sys/param.h> 6653541Sshin#include <sys/systm.h> 6753541Sshin#include <sys/kernel.h> 68185751Simp#include <sys/lock.h> 6953541Sshin#include <sys/sysctl.h> 7053541Sshin#include <sys/queue.h> 7153541Sshin#include <sys/socket.h> 7253541Sshin#include <sys/socketvar.h> 7353541Sshin#include <sys/mbuf.h> 74185747Skmacy#include <sys/rwlock.h> 7553541Sshin#include <sys/syslog.h> 76120727Ssam#include <sys/callout.h> 7753541Sshin 7853541Sshin#include <net/if.h> 79194714Sbz#include <net/route.h> 80185571Sbz 8153541Sshin#include <netinet/in.h> 8253541Sshin#include <netinet/ip_var.h> 8353541Sshin#include <netinet/in_var.h> 8453541Sshin 8562587Sitojun#include <netinet/ip6.h> 8653541Sshin#include <netinet6/ip6_var.h> 8753541Sshin 8862587Sitojun#include <netinet/icmp6.h> 89121283Sume#include <netinet6/nd6.h> 9053541Sshin 9153541Sshin#include <netinet/tcp.h> 9253541Sshin#include <netinet/tcp_seq.h> 9353541Sshin#include <netinet/tcp_timer.h> 9453541Sshin#include <netinet/tcp_var.h> 9553541Sshin 96175162Sobrienextern int in6_inithead(void **head, int off); 97193731Szec#ifdef VIMAGE 98193731Szecextern int in6_detachhead(void **head, int off); 99193731Szec#endif 10053541Sshin 10153541Sshin/* 10253541Sshin * Do what we need to do when inserting a route. 10353541Sshin */ 10453541Sshinstatic struct radix_node * 10553541Sshinin6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 106171260Sdelphij struct radix_node *treenodes) 10753541Sshin{ 10853541Sshin struct rtentry *rt = (struct rtentry *)treenodes; 10953541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt); 11053541Sshin struct radix_node *ret; 11153541Sshin 112186119Sqingli RADIX_NODE_HEAD_WLOCK_ASSERT(head); 11353541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 11453541Sshin rt->rt_flags |= RTF_MULTICAST; 11553541Sshin 11653541Sshin /* 11753541Sshin * A little bit of help for both IPv6 output and input: 11853541Sshin * For local addresses, we make sure that RTF_LOCAL is set, 11953541Sshin * with the thought that this might one day be used to speed up 12053541Sshin * ip_input(). 12153541Sshin * 12253541Sshin * We also mark routes to multicast addresses as such, because 12353541Sshin * it's easy to do and might be useful (but this is much more 12453541Sshin * dubious since it's so easy to inspect the address). (This 12553541Sshin * is done above.) 12653541Sshin * 12753541Sshin * XXX 12853541Sshin * should elaborate the code. 12953541Sshin */ 13053541Sshin if (rt->rt_flags & RTF_HOST) { 13153541Sshin if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr) 13253541Sshin ->sin6_addr, 13353541Sshin &sin6->sin6_addr)) { 13453541Sshin rt->rt_flags |= RTF_LOCAL; 13553541Sshin } 13653541Sshin } 13753541Sshin 138263478Sglebius if (!rt->rt_mtu && rt->rt_ifp) 139263478Sglebius rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 14053541Sshin 14153541Sshin ret = rn_addroute(v_arg, n_arg, head, treenodes); 142186119Sqingli if (ret == NULL) { 14353541Sshin struct rtentry *rt2; 14453541Sshin /* 14553541Sshin * We are trying to add a net route, but can't. 14653541Sshin * The following case should be allowed, so we'll make a 14753541Sshin * special check for this: 14853541Sshin * Two IPv6 addresses with the same prefix is assigned 14953541Sshin * to a single interrface. 15053541Sshin * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 15153541Sshin * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 15253541Sshin * In this case, (*1) and (*2) want to add the same 15353541Sshin * net route entry, 3ffe:0501:: -> if0. 15453541Sshin * This case should not raise an error. 15553541Sshin */ 156231852Sbz rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED, 157231852Sbz rt->rt_fibnum); 15853541Sshin if (rt2) { 159186119Sqingli if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0) 16053541Sshin && rt2->rt_gateway 16153541Sshin && rt2->rt_gateway->sa_family == AF_LINK 16253541Sshin && rt2->rt_ifp == rt->rt_ifp) { 16353541Sshin ret = rt2->rt_nodes; 16453541Sshin } 165120727Ssam RTFREE_LOCKED(rt2); 16653541Sshin } 16753541Sshin } 168186119Sqingli return (ret); 16953541Sshin} 17053541Sshin 17162604SitojunSYSCTL_DECL(_net_inet6_ip6); 17262604Sitojun 173215701Sdimstatic VNET_DEFINE(int, rtq_toomany6) = 128; 174207369Sbz /* 128 cached routes is ``too many'' */ 175207369Sbz#define V_rtq_toomany6 VNET(rtq_toomany6) 176195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 177195699Srwatson &VNET_NAME(rtq_toomany6) , 0, ""); 178120913Sume 17953541Sshinstruct rtqk_arg { 18053541Sshin struct radix_node_head *rnh; 18153541Sshin int mode; 18253541Sshin int updating; 18353541Sshin int draining; 18453541Sshin int killed; 18553541Sshin int found; 18653541Sshin time_t nextstop; 18753541Sshin}; 18853541Sshin 18953541Sshin/* 19053541Sshin * Age old PMTUs. 19153541Sshin */ 19253541Sshinstruct mtuex_arg { 19353541Sshin struct radix_node_head *rnh; 19453541Sshin time_t nextstop; 19553541Sshin}; 196215701Sdimstatic VNET_DEFINE(struct callout, rtq_mtutimer); 197195727Srwatson#define V_rtq_mtutimer VNET(rtq_mtutimer) 198195699Srwatson 19953541Sshinstatic int 20053541Sshinin6_mtuexpire(struct radix_node *rn, void *rock) 20153541Sshin{ 20253541Sshin struct rtentry *rt = (struct rtentry *)rn; 20353541Sshin struct mtuex_arg *ap = rock; 20453541Sshin 20553541Sshin /* sanity */ 20653541Sshin if (!rt) 20753541Sshin panic("rt == NULL in in6_mtuexpire"); 20853541Sshin 209263478Sglebius if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 210263478Sglebius if (rt->rt_expire <= time_uptime) { 21153541Sshin rt->rt_flags |= RTF_PROBEMTU; 21253541Sshin } else { 213263478Sglebius ap->nextstop = lmin(ap->nextstop, rt->rt_expire); 21453541Sshin } 21553541Sshin } 21653541Sshin 21753541Sshin return 0; 21853541Sshin} 21953541Sshin 22053541Sshin#define MTUTIMO_DEFAULT (60*1) 22153541Sshin 22253541Sshinstatic void 223231852Sbzin6_mtutimo_one(struct radix_node_head *rnh) 22453541Sshin{ 22553541Sshin struct mtuex_arg arg; 22653541Sshin 22753541Sshin arg.rnh = rnh; 228160123Soleg arg.nextstop = time_uptime + MTUTIMO_DEFAULT; 229108250Shsu RADIX_NODE_HEAD_LOCK(rnh); 23053541Sshin rnh->rnh_walktree(rnh, in6_mtuexpire, &arg); 231108250Shsu RADIX_NODE_HEAD_UNLOCK(rnh); 232231852Sbz} 23353541Sshin 234231852Sbzstatic void 235231852Sbzin6_mtutimo(void *rock) 236231852Sbz{ 237231852Sbz CURVNET_SET_QUIET((struct vnet *) rock); 238231852Sbz struct radix_node_head *rnh; 239231852Sbz struct timeval atv; 240231852Sbz u_int fibnum; 241231852Sbz 242231852Sbz for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 243231852Sbz rnh = rt_tables_get_rnh(fibnum, AF_INET6); 244231852Sbz if (rnh != NULL) 245231852Sbz in6_mtutimo_one(rnh); 246231852Sbz } 247231852Sbz 248231852Sbz atv.tv_sec = MTUTIMO_DEFAULT; 24953541Sshin atv.tv_usec = 0; 250181803Sbz callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 251183550Szec CURVNET_RESTORE(); 25253541Sshin} 25353541Sshin 25453541Sshin/* 25553541Sshin * Initialize our routing tree. 256178888Sjulian * XXX MRT When off == 0, we are being called from vfs_export.c 257178888Sjulian * so just set up their table and leave. (we know what the correct 258178888Sjulian * value should be so just use that).. FIX AFTER RELENG_7 is MFC'd 259178888Sjulian * see also comments in in_inithead() vfs_export.c and domain.h 26053541Sshin */ 261231852Sbzstatic VNET_DEFINE(int, _in6_rt_was_here); 262231852Sbz#define V__in6_rt_was_here VNET(_in6_rt_was_here) 263231852Sbz 26453541Sshinint 26553541Sshinin6_inithead(void **head, int off) 26653541Sshin{ 26753541Sshin struct radix_node_head *rnh; 26853541Sshin 269178888Sjulian if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 270178888Sjulian return 0; /* See above */ 27153541Sshin 272178888Sjulian if (off == 0) /* See above */ 273178888Sjulian return 1; /* only do the rest for the real thing */ 27453541Sshin 27553541Sshin rnh = *head; 27653541Sshin rnh->rnh_addaddr = in6_addroute; 277231852Sbz 278231852Sbz if (V__in6_rt_was_here == 0) { 279231852Sbz callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE); 280231852Sbz in6_mtutimo(curvnet); /* kick off timeout first time */ 281231852Sbz V__in6_rt_was_here = 1; 282231852Sbz } 283231852Sbz 28453541Sshin return 1; 28553541Sshin} 286193731Szec 287193731Szec#ifdef VIMAGE 288193731Szecint 289193731Szecin6_detachhead(void **head, int off) 290193731Szec{ 291193731Szec 292193731Szec callout_drain(&V_rtq_mtutimer); 293295389Sbz return (rn_detachhead(head)); 294193731Szec} 295193731Szec#endif 296231852Sbz 297231852Sbz/* 298231852Sbz * Extended API for IPv6 FIB support. 299231852Sbz */ 300231852Sbzvoid 301231852Sbzin6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm, 302231852Sbz int flags, struct sockaddr *src, u_int fibnum) 303231852Sbz{ 304231852Sbz 305231852Sbz rtredirect_fib(dst, gw, nm, flags, src, fibnum); 306231852Sbz} 307231852Sbz 308231852Sbzint 309231852Sbzin6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw, 310231852Sbz struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum) 311231852Sbz{ 312231852Sbz 313231852Sbz return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum)); 314231852Sbz} 315231852Sbz 316231852Sbzvoid 317231852Sbzin6_rtalloc(struct route_in6 *ro, u_int fibnum) 318231852Sbz{ 319231852Sbz 320231852Sbz rtalloc_ign_fib((struct route *)ro, 0ul, fibnum); 321231852Sbz} 322231852Sbz 323231852Sbzvoid 324231852Sbzin6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum) 325231852Sbz{ 326231852Sbz 327231852Sbz rtalloc_ign_fib((struct route *)ro, ignflags, fibnum); 328231852Sbz} 329231852Sbz 330231852Sbzstruct rtentry * 331231852Sbzin6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 332231852Sbz{ 333231852Sbz 334231852Sbz return (rtalloc1_fib(dst, report, ignflags, fibnum)); 335231852Sbz} 336