in6_rmx.c revision 274175
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: head/sys/netinet6/in6_rmx.c 274175 2014-11-06 13:13:09Z melifaro $"); 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> 79257176Sglebius#include <net/if_var.h> 80194714Sbz#include <net/route.h> 81185571Sbz 8253541Sshin#include <netinet/in.h> 8353541Sshin#include <netinet/ip_var.h> 8453541Sshin#include <netinet/in_var.h> 8553541Sshin 8662587Sitojun#include <netinet/ip6.h> 8753541Sshin#include <netinet6/ip6_var.h> 8853541Sshin 8962587Sitojun#include <netinet/icmp6.h> 90121283Sume#include <netinet6/nd6.h> 9153541Sshin 9253541Sshin#include <netinet/tcp.h> 9353541Sshin#include <netinet/tcp_seq.h> 9453541Sshin#include <netinet/tcp_timer.h> 9553541Sshin#include <netinet/tcp_var.h> 9653541Sshin 97175162Sobrienextern int in6_inithead(void **head, int off); 98193731Szec#ifdef VIMAGE 99193731Szecextern int in6_detachhead(void **head, int off); 100193731Szec#endif 10153541Sshin 10253541Sshin/* 10353541Sshin * Do what we need to do when inserting a route. 10453541Sshin */ 10553541Sshinstatic struct radix_node * 10653541Sshinin6_addroute(void *v_arg, void *n_arg, struct radix_node_head *head, 107171260Sdelphij struct radix_node *treenodes) 10853541Sshin{ 10953541Sshin struct rtentry *rt = (struct rtentry *)treenodes; 11053541Sshin struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)rt_key(rt); 11153541Sshin struct radix_node *ret; 11253541Sshin 113186119Sqingli RADIX_NODE_HEAD_WLOCK_ASSERT(head); 11453541Sshin if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 11553541Sshin rt->rt_flags |= RTF_MULTICAST; 11653541Sshin 11753541Sshin /* 11853541Sshin * A little bit of help for both IPv6 output and input: 11953541Sshin * For local addresses, we make sure that RTF_LOCAL is set, 12053541Sshin * with the thought that this might one day be used to speed up 12153541Sshin * ip_input(). 12253541Sshin * 12353541Sshin * We also mark routes to multicast addresses as such, because 12453541Sshin * it's easy to do and might be useful (but this is much more 12553541Sshin * dubious since it's so easy to inspect the address). (This 12653541Sshin * is done above.) 12753541Sshin * 12853541Sshin * XXX 12953541Sshin * should elaborate the code. 13053541Sshin */ 13153541Sshin if (rt->rt_flags & RTF_HOST) { 13253541Sshin if (IN6_ARE_ADDR_EQUAL(&satosin6(rt->rt_ifa->ifa_addr) 13353541Sshin ->sin6_addr, 13453541Sshin &sin6->sin6_addr)) { 13553541Sshin rt->rt_flags |= RTF_LOCAL; 13653541Sshin } 13753541Sshin } 13853541Sshin 139274175Smelifaro if (rt->rt_ifp != NULL) { 14053541Sshin 141274175Smelifaro /* 142274175Smelifaro * Check route MTU: 143274175Smelifaro * inherit interface MTU if not set or 144274175Smelifaro * check if MTU is too large. 145274175Smelifaro */ 146274175Smelifaro if (rt->rt_mtu == 0) { 147274175Smelifaro rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 148274175Smelifaro } else if (rt->rt_mtu > IN6_LINKMTU(rt->rt_ifp)) 149274175Smelifaro rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 150274175Smelifaro } 151274175Smelifaro 15253541Sshin ret = rn_addroute(v_arg, n_arg, head, treenodes); 153186119Sqingli if (ret == NULL) { 15453541Sshin struct rtentry *rt2; 15553541Sshin /* 15653541Sshin * We are trying to add a net route, but can't. 15753541Sshin * The following case should be allowed, so we'll make a 15853541Sshin * special check for this: 15953541Sshin * Two IPv6 addresses with the same prefix is assigned 16053541Sshin * to a single interrface. 16153541Sshin * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 16253541Sshin * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 16353541Sshin * In this case, (*1) and (*2) want to add the same 16453541Sshin * net route entry, 3ffe:0501:: -> if0. 16553541Sshin * This case should not raise an error. 16653541Sshin */ 167231852Sbz rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED, 168231852Sbz rt->rt_fibnum); 16953541Sshin if (rt2) { 170186119Sqingli if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0) 17153541Sshin && rt2->rt_gateway 17253541Sshin && rt2->rt_gateway->sa_family == AF_LINK 17353541Sshin && rt2->rt_ifp == rt->rt_ifp) { 17453541Sshin ret = rt2->rt_nodes; 17553541Sshin } 176120727Ssam RTFREE_LOCKED(rt2); 17753541Sshin } 17853541Sshin } 179186119Sqingli return (ret); 18053541Sshin} 18153541Sshin 18262604SitojunSYSCTL_DECL(_net_inet6_ip6); 18362604Sitojun 184215701Sdimstatic VNET_DEFINE(int, rtq_toomany6) = 128; 185207369Sbz /* 128 cached routes is ``too many'' */ 186207369Sbz#define V_rtq_toomany6 VNET(rtq_toomany6) 187195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 188195699Srwatson &VNET_NAME(rtq_toomany6) , 0, ""); 189120913Sume 19053541Sshinstruct rtqk_arg { 19153541Sshin struct radix_node_head *rnh; 19253541Sshin int mode; 19353541Sshin int updating; 19453541Sshin int draining; 19553541Sshin int killed; 19653541Sshin int found; 19753541Sshin time_t nextstop; 19853541Sshin}; 19953541Sshin 20053541Sshin/* 20153541Sshin * Age old PMTUs. 20253541Sshin */ 20353541Sshinstruct mtuex_arg { 20453541Sshin struct radix_node_head *rnh; 20553541Sshin time_t nextstop; 20653541Sshin}; 207215701Sdimstatic VNET_DEFINE(struct callout, rtq_mtutimer); 208195727Srwatson#define V_rtq_mtutimer VNET(rtq_mtutimer) 209195699Srwatson 21053541Sshinstatic int 21153541Sshinin6_mtuexpire(struct radix_node *rn, void *rock) 21253541Sshin{ 21353541Sshin struct rtentry *rt = (struct rtentry *)rn; 21453541Sshin struct mtuex_arg *ap = rock; 21553541Sshin 21653541Sshin /* sanity */ 21753541Sshin if (!rt) 21853541Sshin panic("rt == NULL in in6_mtuexpire"); 21953541Sshin 220262763Sglebius if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 221262763Sglebius if (rt->rt_expire <= time_uptime) { 22253541Sshin rt->rt_flags |= RTF_PROBEMTU; 22353541Sshin } else { 224262763Sglebius ap->nextstop = lmin(ap->nextstop, rt->rt_expire); 22553541Sshin } 22653541Sshin } 22753541Sshin 22853541Sshin return 0; 22953541Sshin} 23053541Sshin 23153541Sshin#define MTUTIMO_DEFAULT (60*1) 23253541Sshin 23353541Sshinstatic void 234231852Sbzin6_mtutimo_one(struct radix_node_head *rnh) 23553541Sshin{ 23653541Sshin struct mtuex_arg arg; 23753541Sshin 23853541Sshin arg.rnh = rnh; 239160123Soleg arg.nextstop = time_uptime + MTUTIMO_DEFAULT; 240108250Shsu RADIX_NODE_HEAD_LOCK(rnh); 24153541Sshin rnh->rnh_walktree(rnh, in6_mtuexpire, &arg); 242108250Shsu RADIX_NODE_HEAD_UNLOCK(rnh); 243231852Sbz} 24453541Sshin 245231852Sbzstatic void 246231852Sbzin6_mtutimo(void *rock) 247231852Sbz{ 248231852Sbz CURVNET_SET_QUIET((struct vnet *) rock); 249231852Sbz struct radix_node_head *rnh; 250231852Sbz struct timeval atv; 251231852Sbz u_int fibnum; 252231852Sbz 253231852Sbz for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 254231852Sbz rnh = rt_tables_get_rnh(fibnum, AF_INET6); 255231852Sbz if (rnh != NULL) 256231852Sbz in6_mtutimo_one(rnh); 257231852Sbz } 258231852Sbz 259231852Sbz atv.tv_sec = MTUTIMO_DEFAULT; 26053541Sshin atv.tv_usec = 0; 261181803Sbz callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 262183550Szec CURVNET_RESTORE(); 26353541Sshin} 26453541Sshin 26553541Sshin/* 26653541Sshin * Initialize our routing tree. 26753541Sshin */ 268231852Sbzstatic VNET_DEFINE(int, _in6_rt_was_here); 269231852Sbz#define V__in6_rt_was_here VNET(_in6_rt_was_here) 270231852Sbz 27153541Sshinint 27253541Sshinin6_inithead(void **head, int off) 27353541Sshin{ 27453541Sshin struct radix_node_head *rnh; 27553541Sshin 276178888Sjulian if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 277274118Smelifaro return (0); 27853541Sshin 279272361Smelifaro rnh = *head; 280272361Smelifaro RADIX_NODE_HEAD_LOCK_INIT(rnh); 281272361Smelifaro 28253541Sshin rnh->rnh_addaddr = in6_addroute; 283231852Sbz 284231852Sbz if (V__in6_rt_was_here == 0) { 285231852Sbz callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE); 286231852Sbz in6_mtutimo(curvnet); /* kick off timeout first time */ 287231852Sbz V__in6_rt_was_here = 1; 288231852Sbz } 289231852Sbz 290274118Smelifaro return (1); 29153541Sshin} 292193731Szec 293193731Szec#ifdef VIMAGE 294193731Szecint 295193731Szecin6_detachhead(void **head, int off) 296193731Szec{ 297193731Szec 298193731Szec callout_drain(&V_rtq_mtutimer); 299193731Szec return (1); 300193731Szec} 301193731Szec#endif 302231852Sbz 303231852Sbz/* 304231852Sbz * Extended API for IPv6 FIB support. 305231852Sbz */ 306231852Sbzvoid 307231852Sbzin6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm, 308231852Sbz int flags, struct sockaddr *src, u_int fibnum) 309231852Sbz{ 310231852Sbz 311231852Sbz rtredirect_fib(dst, gw, nm, flags, src, fibnum); 312231852Sbz} 313231852Sbz 314231852Sbzint 315231852Sbzin6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw, 316231852Sbz struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum) 317231852Sbz{ 318231852Sbz 319231852Sbz return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum)); 320231852Sbz} 321231852Sbz 322231852Sbzvoid 323231852Sbzin6_rtalloc(struct route_in6 *ro, u_int fibnum) 324231852Sbz{ 325231852Sbz 326231852Sbz rtalloc_ign_fib((struct route *)ro, 0ul, fibnum); 327231852Sbz} 328231852Sbz 329231852Sbzvoid 330231852Sbzin6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum) 331231852Sbz{ 332231852Sbz 333231852Sbz rtalloc_ign_fib((struct route *)ro, ignflags, fibnum); 334231852Sbz} 335231852Sbz 336231852Sbzstruct rtentry * 337231852Sbzin6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 338231852Sbz{ 339231852Sbz 340231852Sbz return (rtalloc1_fib(dst, report, ignflags, fibnum)); 341231852Sbz} 342