xref: /freebsd/sys/netinet6/in6_rmx.c (revision f6963113f49c48f30eaadf4dd8ec878f6cdd513e)
1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
482cd038dSYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
582cd038dSYoshinobu Inoue  * All rights reserved.
682cd038dSYoshinobu Inoue  *
782cd038dSYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
882cd038dSYoshinobu Inoue  * modification, are permitted provided that the following conditions
982cd038dSYoshinobu Inoue  * are met:
1082cd038dSYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
1182cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
1282cd038dSYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
1382cd038dSYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
1482cd038dSYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
1582cd038dSYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
1682cd038dSYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
1782cd038dSYoshinobu Inoue  *    without specific prior written permission.
1882cd038dSYoshinobu Inoue  *
1982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2082cd038dSYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2182cd038dSYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2282cd038dSYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2382cd038dSYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2482cd038dSYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2582cd038dSYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2682cd038dSYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2782cd038dSYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2882cd038dSYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2982cd038dSYoshinobu Inoue  * SUCH DAMAGE.
30b48287a3SDavid E. O'Brien  *
31b48287a3SDavid E. O'Brien  *	$KAME: in6_rmx.c,v 1.11 2001/07/26 06:53:16 jinmei Exp $
3282cd038dSYoshinobu Inoue  */
3382cd038dSYoshinobu Inoue 
34caf43b02SWarner Losh /*-
3582cd038dSYoshinobu Inoue  * Copyright 1994, 1995 Massachusetts Institute of Technology
3682cd038dSYoshinobu Inoue  *
3782cd038dSYoshinobu Inoue  * Permission to use, copy, modify, and distribute this software and
3882cd038dSYoshinobu Inoue  * its documentation for any purpose and without fee is hereby
3982cd038dSYoshinobu Inoue  * granted, provided that both the above copyright notice and this
4082cd038dSYoshinobu Inoue  * permission notice appear in all copies, that both the above
4182cd038dSYoshinobu Inoue  * copyright notice and this permission notice appear in all
4282cd038dSYoshinobu Inoue  * supporting documentation, and that the name of M.I.T. not be used
4382cd038dSYoshinobu Inoue  * in advertising or publicity pertaining to distribution of the
4482cd038dSYoshinobu Inoue  * software without specific, written prior permission.  M.I.T. makes
4582cd038dSYoshinobu Inoue  * no representations about the suitability of this software for any
4682cd038dSYoshinobu Inoue  * purpose.  It is provided "as is" without express or implied
4782cd038dSYoshinobu Inoue  * warranty.
4882cd038dSYoshinobu Inoue  *
4982cd038dSYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
5082cd038dSYoshinobu Inoue  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
5182cd038dSYoshinobu Inoue  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
5282cd038dSYoshinobu Inoue  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
5382cd038dSYoshinobu Inoue  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
5482cd038dSYoshinobu Inoue  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
5582cd038dSYoshinobu Inoue  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
5682cd038dSYoshinobu Inoue  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5782cd038dSYoshinobu Inoue  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
5882cd038dSYoshinobu Inoue  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
5982cd038dSYoshinobu Inoue  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6082cd038dSYoshinobu Inoue  * SUCH DAMAGE.
6182cd038dSYoshinobu Inoue  *
6282cd038dSYoshinobu Inoue  */
6382cd038dSYoshinobu Inoue 
6482cd038dSYoshinobu Inoue #include <sys/param.h>
6582cd038dSYoshinobu Inoue #include <sys/systm.h>
6682cd038dSYoshinobu Inoue #include <sys/kernel.h>
67609ff41fSWarner Losh #include <sys/lock.h>
6882cd038dSYoshinobu Inoue #include <sys/queue.h>
6982cd038dSYoshinobu Inoue #include <sys/socket.h>
7082cd038dSYoshinobu Inoue #include <sys/mbuf.h>
713120b9d4SKip Macy #include <sys/rwlock.h>
7282cd038dSYoshinobu Inoue #include <sys/syslog.h>
73d1dd20beSSam Leffler #include <sys/callout.h>
7482cd038dSYoshinobu Inoue 
7582cd038dSYoshinobu Inoue #include <net/if.h>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
77*3d0d5b21SJustin Hibbits #include <net/if_private.h>
780c88be04SBjoern A. Zeeb #include <net/route.h>
79da187ddbSAlexander V. Chernikov #include <net/route/route_ctl.h>
80e7d8af4fSAlexander V. Chernikov #include <net/route/route_var.h>
81a6663252SAlexander V. Chernikov #include <net/route/nhop.h>
824b79449eSBjoern A. Zeeb 
8382cd038dSYoshinobu Inoue #include <netinet/in.h>
8482cd038dSYoshinobu Inoue #include <netinet/ip_var.h>
8582cd038dSYoshinobu Inoue #include <netinet/in_var.h>
8682cd038dSYoshinobu Inoue 
87686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
8882cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h>
8982cd038dSYoshinobu Inoue 
90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
9131b3783cSHajimu UMEMOTO #include <netinet6/nd6.h>
9282cd038dSYoshinobu Inoue 
93a6663252SAlexander V. Chernikov static int
94800c6846SAlexander V. Chernikov rib6_set_nh_pfxflags(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,
95a6663252SAlexander V. Chernikov     struct nhop_object *nh)
96a6663252SAlexander V. Chernikov {
97800c6846SAlexander V. Chernikov 	const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *)mask;
98a6663252SAlexander V. Chernikov 
99800c6846SAlexander V. Chernikov 	if (mask6 == NULL)
100800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, NHF_HOST);
101800c6846SAlexander V. Chernikov 	else if (IN6_IS_ADDR_UNSPECIFIED(&mask6->sin6_addr))
102800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, NHF_DEFAULT);
103800c6846SAlexander V. Chernikov 	else
104800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, 0);
105a6663252SAlexander V. Chernikov 
106800c6846SAlexander V. Chernikov 	return (0);
107800c6846SAlexander V. Chernikov }
108800c6846SAlexander V. Chernikov 
109800c6846SAlexander V. Chernikov static int
110800c6846SAlexander V. Chernikov rib6_augment_nh(u_int fibnum, struct nhop_object *nh)
111800c6846SAlexander V. Chernikov {
112a6663252SAlexander V. Chernikov 	/*
113a6663252SAlexander V. Chernikov 	 * Check route MTU:
114a6663252SAlexander V. Chernikov 	 * inherit interface MTU if not set or
115a6663252SAlexander V. Chernikov 	 * check if MTU is too large.
116a6663252SAlexander V. Chernikov 	 */
117a6663252SAlexander V. Chernikov 	if (nh->nh_mtu == 0) {
118a6663252SAlexander V. Chernikov 		nh->nh_mtu = IN6_LINKMTU(nh->nh_ifp);
119a6663252SAlexander V. Chernikov 	} else if (nh->nh_mtu > IN6_LINKMTU(nh->nh_ifp))
120a6663252SAlexander V. Chernikov 		nh->nh_mtu = IN6_LINKMTU(nh->nh_ifp);
121a6663252SAlexander V. Chernikov 
122a6663252SAlexander V. Chernikov 	/* Set nexthop type */
123a6663252SAlexander V. Chernikov 	if (nhop_get_type(nh) == 0) {
124800c6846SAlexander V. Chernikov 		uint16_t nh_type;
125a6663252SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
126a6663252SAlexander V. Chernikov 			nh_type = NH_TYPE_IPV6_ETHER_NHOP;
127a6663252SAlexander V. Chernikov 		else
128a6663252SAlexander V. Chernikov 			nh_type = NH_TYPE_IPV6_ETHER_RSLV;
129a6663252SAlexander V. Chernikov 
130a6663252SAlexander V. Chernikov 		nhop_set_type(nh, nh_type);
131a6663252SAlexander V. Chernikov 	}
132a6663252SAlexander V. Chernikov 
133a6663252SAlexander V. Chernikov 	return (0);
134a6663252SAlexander V. Chernikov }
135a6663252SAlexander V. Chernikov 
13682cd038dSYoshinobu Inoue /*
13782cd038dSYoshinobu Inoue  * Initialize our routing tree.
13882cd038dSYoshinobu Inoue  */
13981d5d46bSBjoern A. Zeeb 
1402f23f45bSAlexander V. Chernikov struct rib_head *
1412f23f45bSAlexander V. Chernikov in6_inithead(uint32_t fibnum)
14282cd038dSYoshinobu Inoue {
14361eee0e2SAlexander V. Chernikov 	struct rib_head *rh;
144a98bb75fSJohn Baldwin 	struct rib_subscription *rs __diagused;
14582cd038dSYoshinobu Inoue 
146ead85fe4SAlexander V. Chernikov 	rh = rt_table_init(offsetof(struct sockaddr_in6, sin6_addr) << 3,
147ead85fe4SAlexander V. Chernikov 	    AF_INET6, fibnum);
14861eee0e2SAlexander V. Chernikov 	if (rh == NULL)
1492f23f45bSAlexander V. Chernikov 		return (NULL);
15082cd038dSYoshinobu Inoue 
151800c6846SAlexander V. Chernikov 	rh->rnh_set_nh_pfxflags = rib6_set_nh_pfxflags;
152800c6846SAlexander V. Chernikov 	rh->rnh_augment_nh = rib6_augment_nh;
15381d5d46bSBjoern A. Zeeb 
1542f23f45bSAlexander V. Chernikov 	rs = rib_subscribe_internal(rh, nd6_subscription_cb, NULL,
1559a00f6d0SAlexander V. Chernikov 	    RIB_NOTIFY_IMMEDIATE, true);
1569a00f6d0SAlexander V. Chernikov 	KASSERT(rs != NULL, ("Unable to subscribe to fib %u\n", fibnum));
1574c7ba83fSAlexander V. Chernikov 
1582f23f45bSAlexander V. Chernikov 	return (rh);
15982cd038dSYoshinobu Inoue }
160bc29160dSMarko Zec 
161bc29160dSMarko Zec #ifdef VIMAGE
1622f23f45bSAlexander V. Chernikov void
1632f23f45bSAlexander V. Chernikov in6_detachhead(struct rib_head *rh)
164bc29160dSMarko Zec {
165bc29160dSMarko Zec 
1662f23f45bSAlexander V. Chernikov 	rt_table_destroy(rh);
167bc29160dSMarko Zec }
168bc29160dSMarko Zec #endif
169