xref: /freebsd/sys/netinet/in_rmx.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1c398230bSWarner Losh /*-
2ea80aed1SGarrett Wollman  * Copyright 1994, 1995 Massachusetts Institute of Technology
3d4a8d8f2SGarrett Wollman  *
4ea80aed1SGarrett Wollman  * Permission to use, copy, modify, and distribute this software and
5ea80aed1SGarrett Wollman  * its documentation for any purpose and without fee is hereby
6ea80aed1SGarrett Wollman  * granted, provided that both the above copyright notice and this
7ea80aed1SGarrett Wollman  * permission notice appear in all copies, that both the above
8ea80aed1SGarrett Wollman  * copyright notice and this permission notice appear in all
9ea80aed1SGarrett Wollman  * supporting documentation, and that the name of M.I.T. not be used
10ea80aed1SGarrett Wollman  * in advertising or publicity pertaining to distribution of the
11ea80aed1SGarrett Wollman  * software without specific, written prior permission.  M.I.T. makes
12ea80aed1SGarrett Wollman  * no representations about the suitability of this software for any
13ea80aed1SGarrett Wollman  * purpose.  It is provided "as is" without express or implied
14ea80aed1SGarrett Wollman  * warranty.
15d4a8d8f2SGarrett Wollman  *
16ea80aed1SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17ea80aed1SGarrett Wollman  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18ea80aed1SGarrett Wollman  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19ea80aed1SGarrett Wollman  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20ea80aed1SGarrett Wollman  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21ea80aed1SGarrett Wollman  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22ea80aed1SGarrett Wollman  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23ea80aed1SGarrett Wollman  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ea80aed1SGarrett Wollman  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25ea80aed1SGarrett Wollman  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26ea80aed1SGarrett Wollman  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27ea80aed1SGarrett Wollman  * SUCH DAMAGE.
28d4a8d8f2SGarrett Wollman  */
29d4a8d8f2SGarrett Wollman 
30d4a8d8f2SGarrett Wollman #include <sys/param.h>
31d4a8d8f2SGarrett Wollman #include <sys/systm.h>
32d4a8d8f2SGarrett Wollman #include <sys/kernel.h>
3398163b98SPoul-Henning Kamp #include <sys/sysctl.h>
34d4a8d8f2SGarrett Wollman #include <sys/socket.h>
35d4a8d8f2SGarrett Wollman #include <sys/mbuf.h>
36d4a8d8f2SGarrett Wollman 
37d4a8d8f2SGarrett Wollman #include <net/if.h>
3876039bc8SGleb Smirnoff #include <net/if_var.h>
39*3d0d5b21SJustin Hibbits #include <net/if_private.h>
40d4a8d8f2SGarrett Wollman #include <net/route.h>
41da187ddbSAlexander V. Chernikov #include <net/route/route_ctl.h>
42e7d8af4fSAlexander V. Chernikov #include <net/route/route_var.h>
43a6663252SAlexander V. Chernikov #include <net/route/nhop.h>
44530c0060SRobert Watson #include <net/vnet.h>
454b79449eSBjoern A. Zeeb 
46d4a8d8f2SGarrett Wollman #include <netinet/in.h>
47d4a8d8f2SGarrett Wollman #include <netinet/in_var.h>
483c2824b9SAlexander V. Chernikov #include <netinet/ip.h>
493c2824b9SAlexander V. Chernikov #include <netinet/ip_icmp.h>
501e3d5af0SRuslan Ermilov #include <netinet/ip_var.h>
51d4a8d8f2SGarrett Wollman 
52a6663252SAlexander V. Chernikov static int
rib4_set_nh_pfxflags(u_int fibnum,const struct sockaddr * addr,const struct sockaddr * mask,struct nhop_object * nh)53800c6846SAlexander V. Chernikov rib4_set_nh_pfxflags(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,
54a6663252SAlexander V. Chernikov     struct nhop_object *nh)
55a6663252SAlexander V. Chernikov {
56a6663252SAlexander V. Chernikov 	const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
57800c6846SAlexander V. Chernikov 	const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
58800c6846SAlexander V. Chernikov 	bool is_broadcast = false;
59a6663252SAlexander V. Chernikov 
60800c6846SAlexander V. Chernikov 	if (mask == NULL) {
61800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, NHF_HOST);
62a6663252SAlexander V. Chernikov 		/*
63a6663252SAlexander V. Chernikov 		 * Backward compatibility:
64a6663252SAlexander V. Chernikov 		 * if the destination is broadcast,
65a6663252SAlexander V. Chernikov 		 * mark route as broadcast.
66a6663252SAlexander V. Chernikov 		 * This behavior was useful when route cloning
67a6663252SAlexander V. Chernikov 		 * was in place, so there was an explicit cloned
68a6663252SAlexander V. Chernikov 		 * route for every broadcasted address.
69a6663252SAlexander V. Chernikov 		 * Currently (2020-04) there is no kernel machinery
70a6663252SAlexander V. Chernikov 		 * to do route cloning, though someone might explicitly
71a6663252SAlexander V. Chernikov 		 * add these routes to support some cases with active-active
72a6663252SAlexander V. Chernikov 		 * load balancing. Given that, retain this support.
73a6663252SAlexander V. Chernikov 		 */
74800c6846SAlexander V. Chernikov 		if (in_broadcast(addr4->sin_addr, nh->nh_ifp))
75800c6846SAlexander V. Chernikov 			is_broadcast = true;
76800c6846SAlexander V. Chernikov 	} else if (mask4->sin_addr.s_addr == 0)
77800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, NHF_DEFAULT);
78800c6846SAlexander V. Chernikov 	else
79800c6846SAlexander V. Chernikov 		nhop_set_pxtype_flag(nh, 0);
80800c6846SAlexander V. Chernikov 
81800c6846SAlexander V. Chernikov 	nhop_set_broadcast(nh, is_broadcast);
82800c6846SAlexander V. Chernikov 
83800c6846SAlexander V. Chernikov 	return (0);
84a6663252SAlexander V. Chernikov }
85a6663252SAlexander V. Chernikov 
86800c6846SAlexander V. Chernikov static int
rib4_augment_nh(u_int fibnum,struct nhop_object * nh)87800c6846SAlexander V. Chernikov rib4_augment_nh(u_int fibnum, struct nhop_object *nh)
88800c6846SAlexander V. Chernikov {
89a6663252SAlexander V. Chernikov 	/*
90a6663252SAlexander V. Chernikov 	 * Check route MTU:
91a6663252SAlexander V. Chernikov 	 * inherit interface MTU if not set or
92a6663252SAlexander V. Chernikov 	 * check if MTU is too large.
93a6663252SAlexander V. Chernikov 	 */
94a6663252SAlexander V. Chernikov 	if (nh->nh_mtu == 0) {
95a6663252SAlexander V. Chernikov 		nh->nh_mtu = nh->nh_ifp->if_mtu;
96a6663252SAlexander V. Chernikov 	} else if (nh->nh_mtu > nh->nh_ifp->if_mtu)
97a6663252SAlexander V. Chernikov 		nh->nh_mtu = nh->nh_ifp->if_mtu;
98a6663252SAlexander V. Chernikov 
99a6663252SAlexander V. Chernikov 	/* Set nhop type to basic per-AF nhop */
100a6663252SAlexander V. Chernikov 	if (nhop_get_type(nh) == 0) {
101800c6846SAlexander V. Chernikov 		uint16_t nh_type;
102a6663252SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
103a6663252SAlexander V. Chernikov 			nh_type = NH_TYPE_IPV4_ETHER_NHOP;
104a6663252SAlexander V. Chernikov 		else
105a6663252SAlexander V. Chernikov 			nh_type = NH_TYPE_IPV4_ETHER_RSLV;
106a6663252SAlexander V. Chernikov 
107a6663252SAlexander V. Chernikov 		nhop_set_type(nh, nh_type);
108a6663252SAlexander V. Chernikov 	}
109a6663252SAlexander V. Chernikov 
110a6663252SAlexander V. Chernikov 	return (0);
111a6663252SAlexander V. Chernikov }
112a6663252SAlexander V. Chernikov 
113d4a8d8f2SGarrett Wollman /*
114d4a8d8f2SGarrett Wollman  * Initialize our routing tree.
115d4a8d8f2SGarrett Wollman  */
1162f23f45bSAlexander V. Chernikov struct rib_head *
in_inithead(uint32_t fibnum)1172f23f45bSAlexander V. Chernikov in_inithead(uint32_t fibnum)
118d4a8d8f2SGarrett Wollman {
11961eee0e2SAlexander V. Chernikov 	struct rib_head *rh;
120d4a8d8f2SGarrett Wollman 
121ead85fe4SAlexander V. Chernikov 	rh = rt_table_init(32, AF_INET, fibnum);
12261eee0e2SAlexander V. Chernikov 	if (rh == NULL)
1232f23f45bSAlexander V. Chernikov 		return (NULL);
124d4a8d8f2SGarrett Wollman 
125800c6846SAlexander V. Chernikov 	rh->rnh_set_nh_pfxflags = rib4_set_nh_pfxflags;
126800c6846SAlexander V. Chernikov 	rh->rnh_augment_nh = rib4_augment_nh;
12731f0d081SAlexander V. Chernikov 
1282f23f45bSAlexander V. Chernikov 	return (rh);
129d4a8d8f2SGarrett Wollman }
130d4a8d8f2SGarrett Wollman 
131bc29160dSMarko Zec #ifdef VIMAGE
1322f23f45bSAlexander V. Chernikov void
in_detachhead(struct rib_head * rh)1332f23f45bSAlexander V. Chernikov in_detachhead(struct rib_head *rh)
134bc29160dSMarko Zec {
135bc29160dSMarko Zec 
1362f23f45bSAlexander V. Chernikov 	rt_table_destroy(rh);
137bc29160dSMarko Zec }
138bc29160dSMarko Zec #endif
139bc29160dSMarko Zec 
14039191c8eSGarrett Wollman /*
14191854268SRuslan Ermilov  * This zaps old routes when the interface goes down or interface
14291854268SRuslan Ermilov  * address is deleted.  In the latter case, it deletes static routes
14391854268SRuslan Ermilov  * that point to this address.  If we don't do this, we may end up
14491854268SRuslan Ermilov  * using the old address in the future.  The ones we always want to
14591854268SRuslan Ermilov  * get rid of are things like ARP entries, since the user might down
14691854268SRuslan Ermilov  * the interface, walk over to a completely different network, and
14791854268SRuslan Ermilov  * plug back in.
14839191c8eSGarrett Wollman  */
14939191c8eSGarrett Wollman struct in_ifadown_arg {
15039191c8eSGarrett Wollman 	struct ifaddr *ifa;
15191854268SRuslan Ermilov 	int del;
15239191c8eSGarrett Wollman };
15339191c8eSGarrett Wollman 
15439191c8eSGarrett Wollman static int
in_ifadownkill(const struct rtentry * rt,const struct nhop_object * nh,void * xap)155539642a2SAlexander V. Chernikov in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,
156539642a2SAlexander V. Chernikov     void *xap)
15739191c8eSGarrett Wollman {
15839191c8eSGarrett Wollman 	struct in_ifadown_arg *ap = xap;
15939191c8eSGarrett Wollman 
160539642a2SAlexander V. Chernikov 	if (nh->nh_ifa != ap->ifa)
1614579930dSBjoern A. Zeeb 		return (0);
16239191c8eSGarrett Wollman 
163539642a2SAlexander V. Chernikov 	if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)
164e8b0643eSAlexander V. Chernikov 		return (0);
1654bdf0b6aSAlexander V. Chernikov 
166e8b0643eSAlexander V. Chernikov 	return (1);
1674bdf0b6aSAlexander V. Chernikov }
1684bdf0b6aSAlexander V. Chernikov 
169586904c2SGleb Smirnoff void
in_ifadown(struct ifaddr * ifa,int delete)17091854268SRuslan Ermilov in_ifadown(struct ifaddr *ifa, int delete)
17139191c8eSGarrett Wollman {
17239191c8eSGarrett Wollman 	struct in_ifadown_arg arg;
17339191c8eSGarrett Wollman 
174586904c2SGleb Smirnoff 	KASSERT(ifa->ifa_addr->sa_family == AF_INET,
175586904c2SGleb Smirnoff 	    ("%s: wrong family", __func__));
17639191c8eSGarrett Wollman 
17739191c8eSGarrett Wollman 	arg.ifa = ifa;
17891854268SRuslan Ermilov 	arg.del = delete;
1794bdf0b6aSAlexander V. Chernikov 
1807511a638SAlexander V. Chernikov 	rib_foreach_table_walk_del(AF_INET, in_ifadownkill, &arg);
181d1dd20beSSam Leffler 	ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
1828b07e49aSJulian Elischer }
183