xref: /freebsd/sys/netinet/in_rmx.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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/if_private.h>
43 #include <net/route.h>
44 #include <net/route/route_ctl.h>
45 #include <net/route/route_var.h>
46 #include <net/route/nhop.h>
47 #include <net/vnet.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_icmp.h>
53 #include <netinet/ip_var.h>
54 
55 static int
56 rib4_set_nh_pfxflags(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,
57     struct nhop_object *nh)
58 {
59 	const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
60 	const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
61 	bool is_broadcast = false;
62 
63 	if (mask == NULL) {
64 		nhop_set_pxtype_flag(nh, NHF_HOST);
65 		/*
66 		 * Backward compatibility:
67 		 * if the destination is broadcast,
68 		 * mark route as broadcast.
69 		 * This behavior was useful when route cloning
70 		 * was in place, so there was an explicit cloned
71 		 * route for every broadcasted address.
72 		 * Currently (2020-04) there is no kernel machinery
73 		 * to do route cloning, though someone might explicitly
74 		 * add these routes to support some cases with active-active
75 		 * load balancing. Given that, retain this support.
76 		 */
77 		if (in_broadcast(addr4->sin_addr, nh->nh_ifp))
78 			is_broadcast = true;
79 	} else if (mask4->sin_addr.s_addr == 0)
80 		nhop_set_pxtype_flag(nh, NHF_DEFAULT);
81 	else
82 		nhop_set_pxtype_flag(nh, 0);
83 
84 	nhop_set_broadcast(nh, is_broadcast);
85 
86 	return (0);
87 }
88 
89 static int
90 rib4_augment_nh(u_int fibnum, struct nhop_object *nh)
91 {
92 	/*
93 	 * Check route MTU:
94 	 * inherit interface MTU if not set or
95 	 * check if MTU is too large.
96 	 */
97 	if (nh->nh_mtu == 0) {
98 		nh->nh_mtu = nh->nh_ifp->if_mtu;
99 	} else if (nh->nh_mtu > nh->nh_ifp->if_mtu)
100 		nh->nh_mtu = nh->nh_ifp->if_mtu;
101 
102 	/* Set nhop type to basic per-AF nhop */
103 	if (nhop_get_type(nh) == 0) {
104 		uint16_t nh_type;
105 		if (nh->nh_flags & NHF_GATEWAY)
106 			nh_type = NH_TYPE_IPV4_ETHER_NHOP;
107 		else
108 			nh_type = NH_TYPE_IPV4_ETHER_RSLV;
109 
110 		nhop_set_type(nh, nh_type);
111 	}
112 
113 	return (0);
114 }
115 
116 /*
117  * Initialize our routing tree.
118  */
119 struct rib_head *
120 in_inithead(uint32_t fibnum)
121 {
122 	struct rib_head *rh;
123 
124 	rh = rt_table_init(32, AF_INET, fibnum);
125 	if (rh == NULL)
126 		return (NULL);
127 
128 	rh->rnh_set_nh_pfxflags = rib4_set_nh_pfxflags;
129 	rh->rnh_augment_nh = rib4_augment_nh;
130 
131 	return (rh);
132 }
133 
134 #ifdef VIMAGE
135 void
136 in_detachhead(struct rib_head *rh)
137 {
138 
139 	rt_table_destroy(rh);
140 }
141 #endif
142 
143 /*
144  * This zaps old routes when the interface goes down or interface
145  * address is deleted.  In the latter case, it deletes static routes
146  * that point to this address.  If we don't do this, we may end up
147  * using the old address in the future.  The ones we always want to
148  * get rid of are things like ARP entries, since the user might down
149  * the interface, walk over to a completely different network, and
150  * plug back in.
151  */
152 struct in_ifadown_arg {
153 	struct ifaddr *ifa;
154 	int del;
155 };
156 
157 static int
158 in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,
159     void *xap)
160 {
161 	struct in_ifadown_arg *ap = xap;
162 
163 	if (nh->nh_ifa != ap->ifa)
164 		return (0);
165 
166 	if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)
167 		return (0);
168 
169 	return (1);
170 }
171 
172 void
173 in_ifadown(struct ifaddr *ifa, int delete)
174 {
175 	struct in_ifadown_arg arg;
176 
177 	KASSERT(ifa->ifa_addr->sa_family == AF_INET,
178 	    ("%s: wrong family", __func__));
179 
180 	arg.ifa = ifa;
181 	arg.del = delete;
182 
183 	rib_foreach_table_walk_del(AF_INET, in_ifadownkill, &arg);
184 	ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
185 }
186