xref: /freebsd/sys/netinet/in_rmx.c (revision c1d255d3)
1 /*-
2  * Copyright 1994, 1995 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/socket.h>
38 #include <sys/mbuf.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/route.h>
43 #include <net/route/route_ctl.h>
44 #include <net/route/route_var.h>
45 #include <net/route/nhop.h>
46 #include <net/vnet.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip.h>
51 #include <netinet/ip_icmp.h>
52 #include <netinet/ip_var.h>
53 
54 static int
55 rib4_preadd(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,
56     struct nhop_object *nh)
57 {
58 	const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
59 	uint16_t nh_type;
60 	int rt_flags;
61 
62 	/* XXX: RTF_LOCAL && RTF_MULTICAST */
63 
64 	rt_flags = nhop_get_rtflags(nh);
65 
66 	if (rt_flags & RTF_HOST) {
67 		/*
68 		 * Backward compatibility:
69 		 * if the destination is broadcast,
70 		 * mark route as broadcast.
71 		 * This behavior was useful when route cloning
72 		 * was in place, so there was an explicit cloned
73 		 * route for every broadcasted address.
74 		 * Currently (2020-04) there is no kernel machinery
75 		 * to do route cloning, though someone might explicitly
76 		 * add these routes to support some cases with active-active
77 		 * load balancing. Given that, retain this support.
78 		 */
79 		if (in_broadcast(addr4->sin_addr, nh->nh_ifp)) {
80 			rt_flags |= RTF_BROADCAST;
81 			nhop_set_rtflags(nh, rt_flags);
82 			nh->nh_flags |= NHF_BROADCAST;
83 		}
84 	}
85 
86 	/*
87 	 * Check route MTU:
88 	 * inherit interface MTU if not set or
89 	 * check if MTU is too large.
90 	 */
91 	if (nh->nh_mtu == 0) {
92 		nh->nh_mtu = nh->nh_ifp->if_mtu;
93 	} else if (nh->nh_mtu > nh->nh_ifp->if_mtu)
94 		nh->nh_mtu = nh->nh_ifp->if_mtu;
95 
96 	/* Ensure that default route nhop has special flag */
97 	const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
98 	if ((rt_flags & RTF_HOST) == 0 && mask4 != NULL &&
99 	    mask4->sin_addr.s_addr == 0)
100 		nh->nh_flags |= NHF_DEFAULT;
101 
102 	/* Set nhop type to basic per-AF nhop */
103 	if (nhop_get_type(nh) == 0) {
104 		if (nh->nh_flags & NHF_GATEWAY)
105 			nh_type = NH_TYPE_IPV4_ETHER_NHOP;
106 		else
107 			nh_type = NH_TYPE_IPV4_ETHER_RSLV;
108 
109 		nhop_set_type(nh, nh_type);
110 	}
111 
112 	return (0);
113 }
114 
115 /*
116  * Initialize our routing tree.
117  */
118 struct rib_head *
119 in_inithead(uint32_t fibnum)
120 {
121 	struct rib_head *rh;
122 
123 	rh = rt_table_init(32, AF_INET, fibnum);
124 	if (rh == NULL)
125 		return (NULL);
126 
127 	rh->rnh_preadd = rib4_preadd;
128 
129 	return (rh);
130 }
131 
132 #ifdef VIMAGE
133 void
134 in_detachhead(struct rib_head *rh)
135 {
136 
137 	rt_table_destroy(rh);
138 }
139 #endif
140 
141 /*
142  * This zaps old routes when the interface goes down or interface
143  * address is deleted.  In the latter case, it deletes static routes
144  * that point to this address.  If we don't do this, we may end up
145  * using the old address in the future.  The ones we always want to
146  * get rid of are things like ARP entries, since the user might down
147  * the interface, walk over to a completely different network, and
148  * plug back in.
149  */
150 struct in_ifadown_arg {
151 	struct ifaddr *ifa;
152 	int del;
153 };
154 
155 static int
156 in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,
157     void *xap)
158 {
159 	struct in_ifadown_arg *ap = xap;
160 
161 	if (nh->nh_ifa != ap->ifa)
162 		return (0);
163 
164 	if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)
165 		return (0);
166 
167 	return (1);
168 }
169 
170 void
171 in_ifadown(struct ifaddr *ifa, int delete)
172 {
173 	struct in_ifadown_arg arg;
174 
175 	KASSERT(ifa->ifa_addr->sa_family == AF_INET,
176 	    ("%s: wrong family", __func__));
177 
178 	arg.ifa = ifa;
179 	arg.del = delete;
180 
181 	rib_foreach_table_walk_del(AF_INET, in_ifadownkill, &arg);
182 	ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
183 }
184