in6_rmx.c revision 274118
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 274118 2014-11-05 00:58:01Z 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 139262763Sglebius if (!rt->rt_mtu && rt->rt_ifp) 140262763Sglebius rt->rt_mtu = IN6_LINKMTU(rt->rt_ifp); 14153541Sshin 14253541Sshin ret = rn_addroute(v_arg, n_arg, head, treenodes); 143186119Sqingli if (ret == NULL) { 14453541Sshin struct rtentry *rt2; 14553541Sshin /* 14653541Sshin * We are trying to add a net route, but can't. 14753541Sshin * The following case should be allowed, so we'll make a 14853541Sshin * special check for this: 14953541Sshin * Two IPv6 addresses with the same prefix is assigned 15053541Sshin * to a single interrface. 15153541Sshin * # ifconfig if0 inet6 3ffe:0501::1 prefix 64 alias (*1) 15253541Sshin * # ifconfig if0 inet6 3ffe:0501::2 prefix 64 alias (*2) 15353541Sshin * In this case, (*1) and (*2) want to add the same 15453541Sshin * net route entry, 3ffe:0501:: -> if0. 15553541Sshin * This case should not raise an error. 15653541Sshin */ 157231852Sbz rt2 = in6_rtalloc1((struct sockaddr *)sin6, 0, RTF_RNH_LOCKED, 158231852Sbz rt->rt_fibnum); 15953541Sshin if (rt2) { 160186119Sqingli if (((rt2->rt_flags & (RTF_HOST|RTF_GATEWAY)) == 0) 16153541Sshin && rt2->rt_gateway 16253541Sshin && rt2->rt_gateway->sa_family == AF_LINK 16353541Sshin && rt2->rt_ifp == rt->rt_ifp) { 16453541Sshin ret = rt2->rt_nodes; 16553541Sshin } 166120727Ssam RTFREE_LOCKED(rt2); 16753541Sshin } 16853541Sshin } 169186119Sqingli return (ret); 17053541Sshin} 17153541Sshin 17262604SitojunSYSCTL_DECL(_net_inet6_ip6); 17362604Sitojun 174215701Sdimstatic VNET_DEFINE(int, rtq_toomany6) = 128; 175207369Sbz /* 128 cached routes is ``too many'' */ 176207369Sbz#define V_rtq_toomany6 VNET(rtq_toomany6) 177195699SrwatsonSYSCTL_VNET_INT(_net_inet6_ip6, IPV6CTL_RTMAXCACHE, rtmaxcache, CTLFLAG_RW, 178195699Srwatson &VNET_NAME(rtq_toomany6) , 0, ""); 179120913Sume 18053541Sshinstruct rtqk_arg { 18153541Sshin struct radix_node_head *rnh; 18253541Sshin int mode; 18353541Sshin int updating; 18453541Sshin int draining; 18553541Sshin int killed; 18653541Sshin int found; 18753541Sshin time_t nextstop; 18853541Sshin}; 18953541Sshin 19053541Sshin/* 19153541Sshin * Age old PMTUs. 19253541Sshin */ 19353541Sshinstruct mtuex_arg { 19453541Sshin struct radix_node_head *rnh; 19553541Sshin time_t nextstop; 19653541Sshin}; 197215701Sdimstatic VNET_DEFINE(struct callout, rtq_mtutimer); 198195727Srwatson#define V_rtq_mtutimer VNET(rtq_mtutimer) 199195699Srwatson 20053541Sshinstatic int 20153541Sshinin6_mtuexpire(struct radix_node *rn, void *rock) 20253541Sshin{ 20353541Sshin struct rtentry *rt = (struct rtentry *)rn; 20453541Sshin struct mtuex_arg *ap = rock; 20553541Sshin 20653541Sshin /* sanity */ 20753541Sshin if (!rt) 20853541Sshin panic("rt == NULL in in6_mtuexpire"); 20953541Sshin 210262763Sglebius if (rt->rt_expire && !(rt->rt_flags & RTF_PROBEMTU)) { 211262763Sglebius if (rt->rt_expire <= time_uptime) { 21253541Sshin rt->rt_flags |= RTF_PROBEMTU; 21353541Sshin } else { 214262763Sglebius ap->nextstop = lmin(ap->nextstop, rt->rt_expire); 21553541Sshin } 21653541Sshin } 21753541Sshin 21853541Sshin return 0; 21953541Sshin} 22053541Sshin 22153541Sshin#define MTUTIMO_DEFAULT (60*1) 22253541Sshin 22353541Sshinstatic void 224231852Sbzin6_mtutimo_one(struct radix_node_head *rnh) 22553541Sshin{ 22653541Sshin struct mtuex_arg arg; 22753541Sshin 22853541Sshin arg.rnh = rnh; 229160123Soleg arg.nextstop = time_uptime + MTUTIMO_DEFAULT; 230108250Shsu RADIX_NODE_HEAD_LOCK(rnh); 23153541Sshin rnh->rnh_walktree(rnh, in6_mtuexpire, &arg); 232108250Shsu RADIX_NODE_HEAD_UNLOCK(rnh); 233231852Sbz} 23453541Sshin 235231852Sbzstatic void 236231852Sbzin6_mtutimo(void *rock) 237231852Sbz{ 238231852Sbz CURVNET_SET_QUIET((struct vnet *) rock); 239231852Sbz struct radix_node_head *rnh; 240231852Sbz struct timeval atv; 241231852Sbz u_int fibnum; 242231852Sbz 243231852Sbz for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 244231852Sbz rnh = rt_tables_get_rnh(fibnum, AF_INET6); 245231852Sbz if (rnh != NULL) 246231852Sbz in6_mtutimo_one(rnh); 247231852Sbz } 248231852Sbz 249231852Sbz atv.tv_sec = MTUTIMO_DEFAULT; 25053541Sshin atv.tv_usec = 0; 251181803Sbz callout_reset(&V_rtq_mtutimer, tvtohz(&atv), in6_mtutimo, rock); 252183550Szec CURVNET_RESTORE(); 25353541Sshin} 25453541Sshin 25553541Sshin/* 25653541Sshin * Initialize our routing tree. 25753541Sshin */ 258231852Sbzstatic VNET_DEFINE(int, _in6_rt_was_here); 259231852Sbz#define V__in6_rt_was_here VNET(_in6_rt_was_here) 260231852Sbz 26153541Sshinint 26253541Sshinin6_inithead(void **head, int off) 26353541Sshin{ 26453541Sshin struct radix_node_head *rnh; 26553541Sshin 266178888Sjulian if (!rn_inithead(head, offsetof(struct sockaddr_in6, sin6_addr) << 3)) 267274118Smelifaro return (0); 26853541Sshin 269272361Smelifaro rnh = *head; 270272361Smelifaro RADIX_NODE_HEAD_LOCK_INIT(rnh); 271272361Smelifaro 27253541Sshin rnh->rnh_addaddr = in6_addroute; 273231852Sbz 274231852Sbz if (V__in6_rt_was_here == 0) { 275231852Sbz callout_init(&V_rtq_mtutimer, CALLOUT_MPSAFE); 276231852Sbz in6_mtutimo(curvnet); /* kick off timeout first time */ 277231852Sbz V__in6_rt_was_here = 1; 278231852Sbz } 279231852Sbz 280274118Smelifaro return (1); 28153541Sshin} 282193731Szec 283193731Szec#ifdef VIMAGE 284193731Szecint 285193731Szecin6_detachhead(void **head, int off) 286193731Szec{ 287193731Szec 288193731Szec callout_drain(&V_rtq_mtutimer); 289193731Szec return (1); 290193731Szec} 291193731Szec#endif 292231852Sbz 293231852Sbz/* 294231852Sbz * Extended API for IPv6 FIB support. 295231852Sbz */ 296231852Sbzvoid 297231852Sbzin6_rtredirect(struct sockaddr *dst, struct sockaddr *gw, struct sockaddr *nm, 298231852Sbz int flags, struct sockaddr *src, u_int fibnum) 299231852Sbz{ 300231852Sbz 301231852Sbz rtredirect_fib(dst, gw, nm, flags, src, fibnum); 302231852Sbz} 303231852Sbz 304231852Sbzint 305231852Sbzin6_rtrequest(int req, struct sockaddr *dst, struct sockaddr *gw, 306231852Sbz struct sockaddr *mask, int flags, struct rtentry **ret_nrt, u_int fibnum) 307231852Sbz{ 308231852Sbz 309231852Sbz return (rtrequest_fib(req, dst, gw, mask, flags, ret_nrt, fibnum)); 310231852Sbz} 311231852Sbz 312231852Sbzvoid 313231852Sbzin6_rtalloc(struct route_in6 *ro, u_int fibnum) 314231852Sbz{ 315231852Sbz 316231852Sbz rtalloc_ign_fib((struct route *)ro, 0ul, fibnum); 317231852Sbz} 318231852Sbz 319231852Sbzvoid 320231852Sbzin6_rtalloc_ign(struct route_in6 *ro, u_long ignflags, u_int fibnum) 321231852Sbz{ 322231852Sbz 323231852Sbz rtalloc_ign_fib((struct route *)ro, ignflags, fibnum); 324231852Sbz} 325231852Sbz 326231852Sbzstruct rtentry * 327231852Sbzin6_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum) 328231852Sbz{ 329231852Sbz 330231852Sbz return (rtalloc1_fib(dst, report, ignflags, fibnum)); 331231852Sbz} 332