netlink: fix IPv6 route addition with link-local gateway

Currently kernel assumes that IPv6 gateway address is in "embedded"
 form - that is, for the link-local IPv6 addresses, interface index
 is embedded in bytes 2 and 3 of the address.
Fix address embedding in netlink by wrapping nhop_set_gw() in the
 netlink-specific nl_set_nexthop_gw(), which does such embedding
 automatically.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
This commit is contained in:
Alexander V. Chernikov 2023-02-20 14:24:01 +00:00
parent 0de03c306c
commit c7c3481621
3 changed files with 50 additions and 9 deletions

View File

@ -741,8 +741,34 @@ newnhg(struct unhop_ctl *ctl, struct nl_parsed_nhop *attrs, struct user_nhop *un
return (0);
}
/*
* Sets nexthop @nh gateway specified by @gw.
* If gateway is IPv6 link-local, alters @gw to include scopeid equal to
* @ifp ifindex.
* Returns 0 on success or errno.
*/
int
nl_set_nexthop_gw(struct nhop_object *nh, struct sockaddr *gw, struct ifnet *ifp,
struct nl_pstate *npt)
{
#ifdef INET6
if (gw->sa_family == AF_INET6) {
struct sockaddr_in6 *gw6 = (struct sockaddr_in6 *)gw;
if (IN6_IS_ADDR_LINKLOCAL(&gw6->sin6_addr)) {
if (ifp == NULL) {
NLMSG_REPORT_ERR_MSG(npt, "interface not set");
return (EINVAL);
}
in6_set_unicast_scopeid(&gw6->sin6_addr, ifp->if_index);
}
}
#endif
nhop_set_gw(nh, gw, true);
return (0);
}
static int
newnhop(struct nl_parsed_nhop *attrs, struct user_nhop *unhop)
newnhop(struct nl_parsed_nhop *attrs, struct user_nhop *unhop, struct nl_pstate *npt)
{
struct ifaddr *ifa = NULL;
struct nhop_object *nh;
@ -750,17 +776,17 @@ newnhop(struct nl_parsed_nhop *attrs, struct user_nhop *unhop)
if (!attrs->nha_blackhole) {
if (attrs->nha_gw == NULL) {
NL_LOG(LOG_DEBUG, "missing NHA_GATEWAY");
NLMSG_REPORT_ERR_MSG(npt, "missing NHA_GATEWAY");
return (EINVAL);
}
if (attrs->nha_oif == NULL) {
NL_LOG(LOG_DEBUG, "missing NHA_OIF");
NLMSG_REPORT_ERR_MSG(npt, "missing NHA_OIF");
return (EINVAL);
}
if (ifa == NULL)
ifa = ifaof_ifpforaddr(attrs->nha_gw, attrs->nha_oif);
if (ifa == NULL) {
NL_LOG(LOG_DEBUG, "Unable to determine default source IP");
NLMSG_REPORT_ERR_MSG(npt, "Unable to determine default source IP");
return (EINVAL);
}
}
@ -777,7 +803,11 @@ newnhop(struct nl_parsed_nhop *attrs, struct user_nhop *unhop)
if (attrs->nha_blackhole)
nhop_set_blackhole(nh, NHF_BLACKHOLE);
else {
nhop_set_gw(nh, attrs->nha_gw, true);
error = nl_set_nexthop_gw(nh, attrs->nha_gw, attrs->nha_oif, npt);
if (error != 0) {
nhop_free(nh);
return (error);
}
nhop_set_transmit_ifp(nh, attrs->nha_oif);
nhop_set_src(nh, ifa);
}
@ -839,7 +869,7 @@ rtnl_handle_newnhop(struct nlmsghdr *hdr, struct nlpcb *nlp,
if (attrs.nha_group)
error = newnhg(ctl, &attrs, unhop);
else
error = newnhop(&attrs, unhop);
error = newnhop(&attrs, unhop, npt);
if (error != 0) {
free(unhop, M_NETLINK);

View File

@ -104,6 +104,8 @@ void rtnl_iface_drivers_register(void);
void rtnl_nexthops_init(void);
struct nhop_object *nl_find_nhop(uint32_t fibnum, int family,
uint32_t uidx, int nh_flags, int *perror);
int nl_set_nexthop_gw(struct nhop_object *nh, struct sockaddr *gw,
struct ifnet *ifp, struct nl_pstate *npt);
#endif

View File

@ -736,7 +736,11 @@ create_nexthop_one(struct nl_parsed_route *attrs, struct rta_mpath_nh *mpnh,
if (nh == NULL)
return (ENOMEM);
nhop_set_gw(nh, mpnh->gw, true);
error = nl_set_nexthop_gw(nh, mpnh->gw, mpnh->ifp, npt);
if (error != 0) {
nhop_free(nh);
return (error);
}
if (mpnh->ifp != NULL)
nhop_set_transmit_ifp(nh, mpnh->ifp);
nhop_set_rtflags(nh, attrs->rta_rtflags);
@ -800,8 +804,13 @@ create_nexthop_from_attrs(struct nl_parsed_route *attrs,
*perror = ENOMEM;
return (NULL);
}
if (attrs->rta_gw != NULL)
nhop_set_gw(nh, attrs->rta_gw, true);
if (attrs->rta_gw != NULL) {
*perror = nl_set_nexthop_gw(nh, attrs->rta_gw, attrs->rta_oif, npt);
if (*perror != 0) {
nhop_free(nh);
return (NULL);
}
}
if (attrs->rta_oif != NULL)
nhop_set_transmit_ifp(nh, attrs->rta_oif);
if (attrs->rtax_mtu != 0)