xref: /freebsd/sys/net/route/route_rtentry.c (revision 3a3af6b2a160bea72509a9d5ef84e25906b0478a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021-2022 Alexander V. Chernikov
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include "opt_inet.h"
31 #include "opt_inet6.h"
32 #include "opt_route.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/rmlock.h>
41 
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/vnet.h>
45 #include <net/route.h>
46 #include <net/route/route_ctl.h>
47 #include <net/route/route_var.h>
48 #include <net/route/nhop.h>
49 #include <netinet/in.h>
50 #include <netinet6/scope6_var.h>
51 #include <netinet6/in6_var.h>
52 
53 #include <vm/uma.h>
54 
55 /* Routing table UMA zone */
56 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
57 #define	V_rtzone	VNET(rtzone)
58 
59 void
60 vnet_rtzone_init(void)
61 {
62 
63 	V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
64 		NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
65 }
66 
67 #ifdef VIMAGE
68 void
69 vnet_rtzone_destroy(void)
70 {
71 
72 	uma_zdestroy(V_rtzone);
73 }
74 #endif
75 
76 /*
77  * Creates rtentry and based on @dst/@netmask data.
78  * Return 0 and fills in rtentry into @prt on success,
79  * Note: rtentry mask ptr will be set to @netmask , thus its pointer is required
80  *  to be stable till the end of the operation (radix rt insertion/change/removal).
81  */
82 struct rtentry *
83 rt_alloc(struct rib_head *rnh, const struct sockaddr *dst,
84     struct sockaddr *netmask)
85 {
86 	MPASS(dst->sa_len <= sizeof(((struct rtentry *)NULL)->rt_dstb));
87 
88 	struct rtentry *rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
89 	if (rt == NULL)
90 		return (NULL);
91 	rt->rte_flags = RTF_UP | (netmask == NULL ? RTF_HOST : 0);
92 
93 	/* Fill in dst, ensuring it's masked if needed. */
94 	if (netmask != NULL) {
95 		rt_maskedcopy(dst, &rt->rt_dst, netmask);
96 	} else
97 		bcopy(dst, &rt->rt_dst, dst->sa_len);
98 	rt_key(rt) = &rt->rt_dst;
99 	/* Set netmask to the storage from info. It will be updated upon insertion */
100 	rt_mask(rt) = netmask;
101 
102 	return (rt);
103 }
104 
105 static void
106 destroy_rtentry(struct rtentry *rt)
107 {
108 #ifdef VIMAGE
109 	struct nhop_object *nh = rt->rt_nhop;
110 
111 	/*
112 	 * At this moment rnh, nh_control may be already freed.
113 	 * nhop interface may have been migrated to a different vnet.
114 	 * Use vnet stored in the nexthop to delete the entry.
115 	 */
116 #ifdef ROUTE_MPATH
117 	if (NH_IS_NHGRP(nh)) {
118 		const struct weightened_nhop *wn;
119 		uint32_t num_nhops;
120 		wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
121 		nh = wn[0].nh;
122 	}
123 #endif
124 	CURVNET_SET(nhop_get_vnet(nh));
125 #endif
126 
127 	/* Unreference nexthop */
128 	nhop_free_any(rt->rt_nhop);
129 
130 	rt_free_immediate(rt);
131 
132 	CURVNET_RESTORE();
133 }
134 
135 /*
136  * Epoch callback indicating rtentry is safe to destroy
137  */
138 static void
139 destroy_rtentry_epoch(epoch_context_t ctx)
140 {
141 	struct rtentry *rt;
142 
143 	rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
144 
145 	destroy_rtentry(rt);
146 }
147 
148 /*
149  * Schedule rtentry deletion
150  */
151 void
152 rt_free(struct rtentry *rt)
153 {
154 
155 	KASSERT(rt != NULL, ("%s: NULL rt", __func__));
156 
157 	epoch_call(net_epoch_preempt, destroy_rtentry_epoch,
158 	    &rt->rt_epoch_ctx);
159 }
160 
161 void
162 rt_free_immediate(struct rtentry *rt)
163 {
164 	uma_zfree(V_rtzone, rt);
165 }
166 
167 bool
168 rt_is_host(const struct rtentry *rt)
169 {
170 
171 	return (rt->rte_flags & RTF_HOST);
172 }
173 
174 sa_family_t
175 rt_get_family(const struct rtentry *rt)
176 {
177 	const struct sockaddr *dst;
178 
179 	dst = (const struct sockaddr *)rt_key_const(rt);
180 
181 	return (dst->sa_family);
182 }
183 
184 /*
185  * Returns pointer to nexthop or nexthop group
186  * associated with @rt
187  */
188 struct nhop_object *
189 rt_get_raw_nhop(const struct rtentry *rt)
190 {
191 
192 	return (rt->rt_nhop);
193 }
194 
195 #ifdef INET
196 /*
197  * Stores IPv4 address and prefix length of @rt inside
198  *  @paddr and @plen.
199  * @pscopeid is currently always set to 0.
200  */
201 void
202 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
203     int *plen, uint32_t *pscopeid)
204 {
205 	const struct sockaddr_in *dst;
206 
207 	dst = (const struct sockaddr_in *)rt_key_const(rt);
208 	KASSERT((dst->sin_family == AF_INET),
209 	    ("rt family is %d, not inet", dst->sin_family));
210 	*paddr = dst->sin_addr;
211 	dst = (const struct sockaddr_in *)rt_mask_const(rt);
212 	if (dst == NULL)
213 		*plen = 32;
214 	else
215 		*plen = bitcount32(dst->sin_addr.s_addr);
216 	*pscopeid = 0;
217 }
218 
219 /*
220  * Stores IPv4 address and prefix mask of @rt inside
221  *  @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
222  * @pscopeid is currently always set to 0.
223  */
224 void
225 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
226     struct in_addr *pmask, uint32_t *pscopeid)
227 {
228 	const struct sockaddr_in *dst;
229 
230 	dst = (const struct sockaddr_in *)rt_key_const(rt);
231 	KASSERT((dst->sin_family == AF_INET),
232 	    ("rt family is %d, not inet", dst->sin_family));
233 	*paddr = dst->sin_addr;
234 	dst = (const struct sockaddr_in *)rt_mask_const(rt);
235 	if (dst == NULL)
236 		pmask->s_addr = INADDR_BROADCAST;
237 	else
238 		*pmask = dst->sin_addr;
239 	*pscopeid = 0;
240 }
241 #endif
242 
243 #ifdef INET6
244 static int
245 inet6_get_plen(const struct in6_addr *addr)
246 {
247 
248 	return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
249 	    bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
250 }
251 
252 /*
253  * Stores IPv6 address and prefix length of @rt inside
254  *  @paddr and @plen. Addresses are returned in de-embedded form.
255  * Scopeid is set to 0 for non-LL addresses.
256  */
257 void
258 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
259     int *plen, uint32_t *pscopeid)
260 {
261 	const struct sockaddr_in6 *dst;
262 
263 	dst = (const struct sockaddr_in6 *)rt_key_const(rt);
264 	KASSERT((dst->sin6_family == AF_INET6),
265 	    ("rt family is %d, not inet6", dst->sin6_family));
266 	if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
267 		in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
268 	else
269 		*paddr = dst->sin6_addr;
270 	dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
271 	if (dst == NULL)
272 		*plen = 128;
273 	else
274 		*plen = inet6_get_plen(&dst->sin6_addr);
275 }
276 
277 /*
278  * Stores IPv6 address and prefix mask of @rt inside
279  *  @paddr and @pmask. Addresses are returned in de-embedded form.
280  * Scopeid is set to 0 for non-LL addresses.
281  */
282 void
283 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
284     struct in6_addr *pmask, uint32_t *pscopeid)
285 {
286 	const struct sockaddr_in6 *dst;
287 
288 	dst = (const struct sockaddr_in6 *)rt_key_const(rt);
289 	KASSERT((dst->sin6_family == AF_INET6),
290 	    ("rt family is %d, not inet", dst->sin6_family));
291 	if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
292 		in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
293 	else
294 		*paddr = dst->sin6_addr;
295 	dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
296 	if (dst == NULL)
297 		memset(pmask, 0xFF, sizeof(struct in6_addr));
298 	else
299 		*pmask = dst->sin6_addr;
300 }
301 #endif
302 
303 
304