xref: /freebsd/sys/netinet/in_fib.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * Copyright (c) 2015
3  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_inet.h"
32 #include "opt_route.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/lock.h>
37 #include <sys/rmlock.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/kernel.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <net/if_private.h>
48 #include <net/route.h>
49 #include <net/route/route_ctl.h>
50 #include <net/route/route_var.h>
51 #include <net/route/fib_algo.h>
52 #include <net/route/nhop.h>
53 #include <net/toeplitz.h>
54 #include <net/vnet.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_var.h>
58 #include <netinet/in_fib.h>
59 
60 #ifdef INET
61 
62 /* Verify struct route compatibility */
63 /* Assert 'struct route_in' is compatible with 'struct route' */
64 CHK_STRUCT_ROUTE_COMPAT(struct route_in, ro_dst4);
65 
66 #ifdef FIB_ALGO
67 VNET_DEFINE(struct fib_dp *, inet_dp);
68 #endif
69 
70 #ifdef ROUTE_MPATH
71 struct _hash_5tuple_ipv4 {
72 	struct in_addr src;
73 	struct in_addr dst;
74 	unsigned short src_port;
75 	unsigned short dst_port;
76 	char proto;
77 	char spare[3];
78 };
79 _Static_assert(sizeof(struct _hash_5tuple_ipv4) == 16,
80     "_hash_5tuple_ipv4 size is wrong");
81 
82 uint32_t
83 fib4_calc_software_hash(struct in_addr src, struct in_addr dst,
84     unsigned short src_port, unsigned short dst_port, char proto,
85     uint32_t *phashtype)
86 {
87 	struct _hash_5tuple_ipv4 data;
88 
89 	data.src = src;
90 	data.dst = dst;
91 	data.src_port = src_port;
92 	data.dst_port = dst_port;
93 	data.proto = proto;
94 	data.spare[0] = data.spare[1] = data.spare[2] = 0;
95 
96 	*phashtype = M_HASHTYPE_OPAQUE;
97 
98 	return (toeplitz_hash(MPATH_ENTROPY_KEY_LEN, mpath_entropy_key,
99 	  sizeof(data), (uint8_t *)&data));
100 }
101 #endif
102 
103 /*
104  * Looks up path in fib @fibnum specified by @dst.
105  * Returns path nexthop on success. Nexthop is safe to use
106  *  within the current network epoch. If longer lifetime is required,
107  *  one needs to pass NHR_REF as a flag. This will return referenced
108  *  nexthop.
109  */
110 #ifdef FIB_ALGO
111 struct nhop_object *
112 fib4_lookup(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
113     uint32_t flags, uint32_t flowid)
114 {
115 	struct nhop_object *nh;
116 	struct fib_dp *dp = &V_inet_dp[fibnum];
117 	struct flm_lookup_key key = {.addr4 = dst };
118 
119 	nh = dp->f(dp->arg, key, scopeid);
120 	if (nh != NULL) {
121 		nh = nhop_select(nh, flowid);
122 		/* Ensure route & ifp is UP */
123 		if (RT_LINK_IS_UP(nh->nh_ifp)) {
124 			if (flags & NHR_REF)
125 				nhop_ref_object(nh);
126 			return (nh);
127 		}
128 	}
129 	RTSTAT_INC(rts_unreach);
130 	return (NULL);
131 }
132 #else
133 struct nhop_object *
134 fib4_lookup(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
135     uint32_t flags, uint32_t flowid)
136 {
137 	RIB_RLOCK_TRACKER;
138 	struct rib_head *rh;
139 	struct radix_node *rn;
140 	struct nhop_object *nh;
141 
142 	KASSERT((fibnum < rt_numfibs), ("fib4_lookup: bad fibnum"));
143 	rh = rt_tables_get_rnh(fibnum, AF_INET);
144 	if (rh == NULL)
145 		return (NULL);
146 
147 	/* Prepare lookup key */
148 	struct sockaddr_in sin4 = {
149 		.sin_family = AF_INET,
150 		.sin_len = sizeof(struct sockaddr_in),
151 		.sin_addr = dst,
152 	};
153 
154 	nh = NULL;
155 	RIB_RLOCK(rh);
156 	rn = rh->rnh_matchaddr((void *)&sin4, &rh->head);
157 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
158 		nh = nhop_select((RNTORT(rn))->rt_nhop, flowid);
159 		/* Ensure route & ifp is UP */
160 		if (RT_LINK_IS_UP(nh->nh_ifp)) {
161 			if (flags & NHR_REF)
162 				nhop_ref_object(nh);
163 			RIB_RUNLOCK(rh);
164 			return (nh);
165 		}
166 	}
167 	RIB_RUNLOCK(rh);
168 
169 	RTSTAT_INC(rts_unreach);
170 	return (NULL);
171 }
172 #endif
173 
174 inline static int
175 check_urpf_nhop(const struct nhop_object *nh, uint32_t flags,
176     const struct ifnet *src_if)
177 {
178 
179 	if (src_if != NULL && nh->nh_aifp == src_if) {
180 		return (1);
181 	}
182 	if (src_if == NULL) {
183 		if ((flags & NHR_NODEFAULT) == 0)
184 			return (1);
185 		else if ((nh->nh_flags & NHF_DEFAULT) == 0)
186 			return (1);
187 	}
188 
189 	return (0);
190 }
191 
192 static int
193 check_urpf(struct nhop_object *nh, uint32_t flags,
194     const struct ifnet *src_if)
195 {
196 #ifdef ROUTE_MPATH
197 	if (NH_IS_NHGRP(nh)) {
198 		const struct weightened_nhop *wn;
199 		uint32_t num_nhops;
200 		wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
201 			for (int i = 0; i < num_nhops; i++) {
202 				if (check_urpf_nhop(wn[i].nh, flags, src_if) != 0)
203 				return (1);
204 		}
205 		return (0);
206 	} else
207 #endif
208 		return (check_urpf_nhop(nh, flags, src_if));
209 }
210 
211 #ifndef FIB_ALGO
212 static struct nhop_object *
213 lookup_nhop(uint32_t fibnum, struct in_addr dst, uint32_t scopeid)
214 {
215 	RIB_RLOCK_TRACKER;
216 	struct rib_head *rh;
217 	struct radix_node *rn;
218 	struct nhop_object *nh;
219 
220 	KASSERT((fibnum < rt_numfibs), ("fib4_check_urpf: bad fibnum"));
221 	rh = rt_tables_get_rnh(fibnum, AF_INET);
222 	if (rh == NULL)
223 		return (NULL);
224 
225 	/* Prepare lookup key */
226 	struct sockaddr_in sin4;
227 	memset(&sin4, 0, sizeof(sin4));
228 	sin4.sin_len = sizeof(struct sockaddr_in);
229 	sin4.sin_addr = dst;
230 
231 	nh = NULL;
232 	RIB_RLOCK(rh);
233 	rn = rh->rnh_matchaddr((void *)&sin4, &rh->head);
234 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0))
235 		nh = RNTORT(rn)->rt_nhop;
236 	RIB_RUNLOCK(rh);
237 
238 	return (nh);
239 }
240 #endif
241 
242 /*
243  * Performs reverse path forwarding lookup.
244  * If @src_if is non-zero, verifies that at least 1 path goes via
245  *   this interface.
246  * If @src_if is zero, verifies that route exist.
247  * if @flags contains NHR_NOTDEFAULT, do not consider default route.
248  *
249  * Returns 1 if route matching conditions is found, 0 otherwise.
250  */
251 int
252 fib4_check_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
253   uint32_t flags, const struct ifnet *src_if)
254 {
255 	struct nhop_object *nh;
256 #ifdef FIB_ALGO
257 	struct fib_dp *dp = &V_inet_dp[fibnum];
258 	struct flm_lookup_key key = {.addr4 = dst };
259 
260 	nh = dp->f(dp->arg, key, scopeid);
261 #else
262 	nh = lookup_nhop(fibnum, dst, scopeid);
263 #endif
264 	if (nh != NULL)
265 		return (check_urpf(nh, flags, src_if));
266 
267 	return (0);
268 }
269 
270 /*
271  * Function returning prefix match data along with the nexthop data.
272  * Intended to be used by the control plane code.
273  * Supported flags:
274  *  NHR_UNLOCKED: do not lock radix during lookup.
275  * Returns pointer to rtentry and raw nexthop in @rnd. Both rtentry
276  *  and nexthop are safe to use within current epoch. Note:
277  * Note: rnd_nhop can actually be the nexthop group.
278  */
279 struct rtentry *
280 fib4_lookup_rt(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
281     uint32_t flags, struct route_nhop_data *rnd)
282 {
283 	RIB_RLOCK_TRACKER;
284 	struct rib_head *rh;
285 	struct radix_node *rn;
286 	struct rtentry *rt;
287 
288 	KASSERT((fibnum < rt_numfibs), ("fib4_lookup_rt: bad fibnum"));
289 	rh = rt_tables_get_rnh(fibnum, AF_INET);
290 	if (rh == NULL)
291 		return (NULL);
292 
293 	/* Prepare lookup key */
294 	struct sockaddr_in sin4 = {
295 		.sin_family = AF_INET,
296 		.sin_len = sizeof(struct sockaddr_in),
297 		.sin_addr = dst,
298 	};
299 
300 	rt = NULL;
301 	if (!(flags & NHR_UNLOCKED))
302 		RIB_RLOCK(rh);
303 	rn = rh->rnh_matchaddr((void *)&sin4, &rh->head);
304 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
305 		rt = (struct rtentry *)rn;
306 		rnd->rnd_nhop = rt->rt_nhop;
307 		rnd->rnd_weight = rt->rt_weight;
308 	}
309 	if (!(flags & NHR_UNLOCKED))
310 		RIB_RUNLOCK(rh);
311 
312 	return (rt);
313 }
314 
315 struct nhop_object *
316 fib4_lookup_debugnet(uint32_t fibnum, struct in_addr dst, uint32_t scopeid,
317     uint32_t flags)
318 {
319 	struct rtentry *rt;
320 	struct route_nhop_data rnd;
321 
322 	rt = fib4_lookup_rt(fibnum, dst, scopeid, NHR_UNLOCKED, &rnd);
323 	if (rt != NULL) {
324 		struct nhop_object *nh = nhop_select(rnd.rnd_nhop, 0);
325 		/* Ensure route & ifp is UP */
326 		if (RT_LINK_IS_UP(nh->nh_ifp))
327 			return (nh);
328 	}
329 
330 	return (NULL);
331 }
332 
333 #endif
334