routing: add rib_add_default_route() wrapper

Multiple consumers in the kernel space want to install IPv4 or IPv6
 default route. Provide convenient wrapper to simplify the code
 inside the customers.

MFC after:		1 month
Differential Revision:	https://reviews.freebsd.org/D36167
This commit is contained in:
Alexander V. Chernikov 2022-08-09 13:34:28 +00:00
parent a1b59379db
commit d8b2693414
2 changed files with 58 additions and 2 deletions

View File

@ -83,8 +83,8 @@ int rib_del_kernel_px(uint32_t fibnum, struct sockaddr *dst, int plen,
int rib_match_gw(const struct rtentry *rt, const struct nhop_object *nh,
void *_data);
int rib_match_gw(const struct rtentry *rt, const struct nhop_object *nh,
void *gw_sa);
int rib_add_default_route(uint32_t fibnum, int family, struct ifnet *ifp,
struct sockaddr *gw, struct rib_cmd_info *rc);
typedef void route_notification_t(struct rib_cmd_info *rc, void *);
void rib_decompose_notification(struct rib_cmd_info *rc,

View File

@ -409,6 +409,62 @@ rib_decompose_notification(struct rib_cmd_info *rc, route_notification_t *cb,
}
#endif
union sockaddr_union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
char _buf[32];
};
/*
* Creates nexhops suitable for using as a default route nhop.
* Helper for the various kernel subsystems adding/changing default route.
*/
int
rib_add_default_route(uint32_t fibnum, int family, struct ifnet *ifp,
struct sockaddr *gw, struct rib_cmd_info *rc)
{
struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
union sockaddr_union saun = {};
struct sockaddr *dst = &saun.sa;
int error;
switch (family) {
#ifdef INET
case AF_INET:
saun.sin.sin_family = AF_INET;
saun.sin.sin_len = sizeof(struct sockaddr_in);
break;
#endif
#ifdef INET6
case AF_INET6:
saun.sin6.sin6_family = AF_INET6;
saun.sin6.sin6_len = sizeof(struct sockaddr_in6);
break;
#endif
default:
return (EAFNOSUPPORT);
}
struct ifaddr *ifa = ifaof_ifpforaddr(gw, ifp);
if (ifa == NULL)
return (ENOENT);
struct nhop_object *nh = nhop_alloc(fibnum, family);
if (nh == NULL)
return (ENOMEM);
nhop_set_gw(nh, gw, true);
nhop_set_transmit_ifp(nh, ifp);
nhop_set_src(nh, ifa);
nhop_set_pxtype_flag(nh, NHF_DEFAULT);
rnd.rnd_nhop = nhop_get_nhop(nh, &error);
if (error == 0)
error = rib_add_route_px(fibnum, dst, 0, &rnd, RTM_F_CREATE, rc);
return (error);
}
#ifdef INET
/*
* Checks if the found key in the trie contains (<=) a prefix covering