From: Jo-Philipp Wich Date: Thu, 24 Aug 2023 12:16:00 +0000 (+0200) Subject: interface-ip: mask out host bits in IPv4 route targets X-Git-Url: http://git.openwrt.org/?p=project%2Fnetifd.git;a=commitdiff_plain;h=76eb342c397a90feb77b54be2706fa9ff61637f1 interface-ip: mask out host bits in IPv4 route targets The kernel will reject attempts to install routes with target addresses having host bits set with an "Invalid prefix for given prefix length" error. A route configuration like the one below will silently fail to apply: config route option interface lan option target 10.40.40.1/24 Attempting to do the same with iproute2 will fail as well: # ip route add 10.40.40.1/24 dev br-lan Error: Invalid prefix for given prefix length. However, for IPv6 route targets with set host bits are allowed: # ip -6 route add 3000::1/64 via fe80::1234:5678:9abcd:ef01 dev br-lan # ip -6 route list 3000::1/64 3000::/64 via fe80::1234:5678:9abc:def1 dev br-lan metric 1024 pref medium In order to stay consistent here, and to avoid unecessary configuration pitfalls, make netifd more lenient and simply mask out excess host bits while parsing IPv4 route configuration. Signed-off-by: Jo-Philipp Wich --- diff --git a/interface-ip.c b/interface-ip.c index a06a514..fee29a9 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -441,6 +441,10 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6) DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur)); goto error; } + + /* Mask out IPv4 host bits to avoid "Invalid prefix for given prefix length" */ + if (af == AF_INET && route->mask < 32) + route->addr.in.s_addr &= ((1u << route->mask) - 1); } if ((cur = tb[ROUTE_GATEWAY]) != NULL) {