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