interface-ip: mask out host bits in IPv4 route targets
[project/netifd.git] / interface-ip.c
index a06a514b6ddea9771d5a82f088b7ad7ee94529fc..fee29a99b299ab04f748e0da986bd05ae78a9d4f 100644 (file)
@@ -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) {