2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2021-2022 Alexander V. Chernikov
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
32 #include "opt_route.h"
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>
40 #include <sys/rmlock.h>
43 #include <net/if_var.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>
55 /* Routing table UMA zone */
56 VNET_DEFINE_STATIC(uma_zone_t, rtzone);
57 #define V_rtzone VNET(rtzone)
60 vnet_rtzone_init(void)
63 V_rtzone = uma_zcreate("rtentry", sizeof(struct rtentry),
64 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
69 vnet_rtzone_destroy(void)
72 uma_zdestroy(V_rtzone);
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).
83 rt_alloc(struct rib_head *rnh, const struct sockaddr *dst,
84 struct sockaddr *netmask)
86 MPASS(dst->sa_len <= sizeof(((struct rtentry *)NULL)->rt_dstb));
88 struct rtentry *rt = uma_zalloc(V_rtzone, M_NOWAIT | M_ZERO);
91 rt->rte_flags = RTF_UP | (netmask == NULL ? RTF_HOST : 0);
93 /* Fill in dst, ensuring it's masked if needed. */
94 if (netmask != NULL) {
95 rt_maskedcopy(dst, &rt->rt_dst, netmask);
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;
106 destroy_rtentry(struct rtentry *rt)
109 struct nhop_object *nh = rt->rt_nhop;
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.
117 if (NH_IS_NHGRP(nh)) {
118 const struct weightened_nhop *wn;
120 wn = nhgrp_get_nhops((struct nhgrp_object *)nh, &num_nhops);
124 CURVNET_SET(nhop_get_vnet(nh));
127 /* Unreference nexthop */
128 nhop_free_any(rt->rt_nhop);
130 rt_free_immediate(rt);
136 * Epoch callback indicating rtentry is safe to destroy
139 destroy_rtentry_epoch(epoch_context_t ctx)
143 rt = __containerof(ctx, struct rtentry, rt_epoch_ctx);
149 * Schedule rtentry deletion
152 rt_free(struct rtentry *rt)
155 KASSERT(rt != NULL, ("%s: NULL rt", __func__));
157 NET_EPOCH_CALL(destroy_rtentry_epoch, &rt->rt_epoch_ctx);
161 rt_free_immediate(struct rtentry *rt)
163 uma_zfree(V_rtzone, rt);
167 rt_is_host(const struct rtentry *rt)
170 return (rt->rte_flags & RTF_HOST);
174 rt_get_family(const struct rtentry *rt)
176 const struct sockaddr *dst;
178 dst = (const struct sockaddr *)rt_key_const(rt);
180 return (dst->sa_family);
184 * Returns pointer to nexthop or nexthop group
185 * associated with @rt
188 rt_get_raw_nhop(const struct rtentry *rt)
191 return (rt->rt_nhop);
195 rt_get_rnd(const struct rtentry *rt, struct route_nhop_data *rnd)
197 rnd->rnd_nhop = rt->rt_nhop;
198 rnd->rnd_weight = rt->rt_weight;
203 * Stores IPv4 address and prefix length of @rt inside
205 * @pscopeid is currently always set to 0.
208 rt_get_inet_prefix_plen(const struct rtentry *rt, struct in_addr *paddr,
209 int *plen, uint32_t *pscopeid)
211 const struct sockaddr_in *dst;
213 dst = (const struct sockaddr_in *)rt_key_const(rt);
214 KASSERT((dst->sin_family == AF_INET),
215 ("rt family is %d, not inet", dst->sin_family));
216 *paddr = dst->sin_addr;
217 dst = (const struct sockaddr_in *)rt_mask_const(rt);
221 *plen = bitcount32(dst->sin_addr.s_addr);
226 * Stores IPv4 address and prefix mask of @rt inside
227 * @paddr and @pmask. Sets mask to INADDR_ANY for host routes.
228 * @pscopeid is currently always set to 0.
231 rt_get_inet_prefix_pmask(const struct rtentry *rt, struct in_addr *paddr,
232 struct in_addr *pmask, uint32_t *pscopeid)
234 const struct sockaddr_in *dst;
236 dst = (const struct sockaddr_in *)rt_key_const(rt);
237 KASSERT((dst->sin_family == AF_INET),
238 ("rt family is %d, not inet", dst->sin_family));
239 *paddr = dst->sin_addr;
240 dst = (const struct sockaddr_in *)rt_mask_const(rt);
242 pmask->s_addr = INADDR_BROADCAST;
244 *pmask = dst->sin_addr;
251 inet6_get_plen(const struct in6_addr *addr)
254 return (bitcount32(addr->s6_addr32[0]) + bitcount32(addr->s6_addr32[1]) +
255 bitcount32(addr->s6_addr32[2]) + bitcount32(addr->s6_addr32[3]));
259 * Stores IPv6 address and prefix length of @rt inside
260 * @paddr and @plen. Addresses are returned in de-embedded form.
261 * Scopeid is set to 0 for non-LL addresses.
264 rt_get_inet6_prefix_plen(const struct rtentry *rt, struct in6_addr *paddr,
265 int *plen, uint32_t *pscopeid)
267 const struct sockaddr_in6 *dst;
269 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
270 KASSERT((dst->sin6_family == AF_INET6),
271 ("rt family is %d, not inet6", dst->sin6_family));
272 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
273 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
275 *paddr = dst->sin6_addr;
276 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
280 *plen = inet6_get_plen(&dst->sin6_addr);
284 * Stores IPv6 address and prefix mask of @rt inside
285 * @paddr and @pmask. Addresses are returned in de-embedded form.
286 * Scopeid is set to 0 for non-LL addresses.
289 rt_get_inet6_prefix_pmask(const struct rtentry *rt, struct in6_addr *paddr,
290 struct in6_addr *pmask, uint32_t *pscopeid)
292 const struct sockaddr_in6 *dst;
294 dst = (const struct sockaddr_in6 *)rt_key_const(rt);
295 KASSERT((dst->sin6_family == AF_INET6),
296 ("rt family is %d, not inet", dst->sin6_family));
297 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
298 in6_splitscope(&dst->sin6_addr, paddr, pscopeid);
300 *paddr = dst->sin6_addr;
301 dst = (const struct sockaddr_in6 *)rt_mask_const(rt);
303 memset(pmask, 0xFF, sizeof(struct in6_addr));
305 *pmask = dst->sin6_addr;