+enum {
+ ROUTE_INTERFACE,
+ ROUTE_TARGET,
+ ROUTE_MASK,
+ ROUTE_GATEWAY,
+ ROUTE_METRIC,
+ ROUTE_MTU,
+ __ROUTE_MAX
+};
+
+static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
+ [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
+ [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
+};
+
+const struct config_param_list route_attr_list = {
+ .n_params = __ROUTE_MAX,
+ .params = route_attr,
+};
+
+static void
+clear_if_addr(union if_addr *a, int mask)
+{
+ int m_bytes = (mask + 7) / 8;
+ uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
+ uint8_t *p = (uint8_t *) a;
+
+ if (m_bytes < sizeof(a))
+ memset(p + m_bytes, 0, sizeof(a) - m_bytes);
+
+ p[m_bytes - 1] &= ~m_clear;
+}
+
+static bool
+match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
+{
+ union if_addr *p1, *p2;
+
+ p1 = alloca(sizeof(*a1));
+ p2 = alloca(sizeof(*a2));
+
+ memcpy(p1, a1, sizeof(*a1));
+ clear_if_addr(p1, mask);
+ memcpy(p2, a2, sizeof(*a2));
+ clear_if_addr(p2, mask);
+
+ return !memcmp(p1, p2, sizeof(*p1));
+}
+
+static bool
+__find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
+{
+ struct device_addr *addr;
+
+ vlist_for_each_element(&ip->addr, addr, node) {
+ if (!addr->enabled)
+ continue;
+
+ if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
+ continue;
+
+ if (!match_if_addr(&addr->addr, a, addr->mask))
+ continue;
+
+ return true;
+ }
+
+ return false;
+}
+
+static void
+__find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
+ bool v6, struct device_route **res)
+{
+ struct device_route *route;
+
+ vlist_for_each_element(&ip->route, route, node) {
+ if (!route->enabled)
+ continue;
+
+ if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
+ continue;
+
+ if (!match_if_addr(&route->addr, a, route->mask))
+ continue;
+
+ if (!*res || route->mask < (*res)->mask)
+ *res = route;
+ }
+}
+
+static bool
+interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
+{
+ return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
+ __find_ip_addr_target(&iface->config_ip, a, v6);
+}
+
+static void
+interface_ip_find_route_target(struct interface *iface, union if_addr *a,
+ bool v6, struct device_route **route)
+{
+ __find_ip_route_target(&iface->proto_ip, a, v6, route);
+ __find_ip_route_target(&iface->config_ip, a, v6, route);
+}
+
+struct interface *
+interface_ip_add_target_route(union if_addr *addr, bool v6)
+{
+ struct interface *iface;
+ struct device_route *route, *r_next = NULL;
+ bool defaultroute_target = false;
+ int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
+
+ route = calloc(1, sizeof(*route));
+ if (!route)
+ return NULL;
+
+ route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
+ route->mask = v6 ? 128 : 32;
+ if (memcmp(&route->addr, addr, addrsize) == 0)
+ defaultroute_target = true;
+ else
+ memcpy(&route->addr, addr, addrsize);
+
+ vlist_for_each_element(&interfaces, iface, node) {
+ /* look for locally addressable target first */
+ if (interface_ip_find_addr_target(iface, addr, v6))
+ goto done;
+
+ /* do not stop at the first route, let the lookup compare
+ * masks to find the best match */
+ interface_ip_find_route_target(iface, addr, v6, &r_next);
+ }
+
+ if (!r_next) {
+ free(route);
+ return NULL;
+ }
+
+ iface = r_next->iface;
+ memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
+ route->mtu = r_next->mtu;
+ route->metric = r_next->metric;
+
+done:
+ route->iface = iface;
+ if (defaultroute_target)
+ free(route);
+ else
+ vlist_add(&iface->host_routes, &route->node, &route->flags);
+ return iface;
+}
+
+void
+interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
+{
+ struct interface_ip_settings *ip;
+ struct blob_attr *tb[__ROUTE_MAX], *cur;
+ struct device_route *route;
+ int af = v6 ? AF_INET6 : AF_INET;
+
+ blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
+
+ if (!iface) {
+ if ((cur = tb[ROUTE_INTERFACE]) == NULL)
+ return;
+
+ iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
+ if (!iface)
+ return;
+
+ ip = &iface->config_ip;
+ } else {
+ ip = &iface->proto_ip;
+ }
+
+ route = calloc(1, sizeof(*route));
+ if (!route)
+ return;
+
+ route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
+ route->mask = v6 ? 128 : 32;
+ if ((cur = tb[ROUTE_MASK]) != NULL) {
+ route->mask = parse_netmask_string(blobmsg_data(cur), v6);
+ if (route->mask > (v6 ? 128 : 32))
+ goto error;
+ }
+
+ if ((cur = tb[ROUTE_TARGET]) != NULL) {
+ if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
+ DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
+ goto error;
+ }
+ }
+
+ if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
+ if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
+ DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
+ goto error;
+ }
+ }
+
+ if ((cur = tb[ROUTE_METRIC]) != NULL) {
+ route->metric = blobmsg_get_u32(cur);
+ route->flags |= DEVROUTE_METRIC;
+ }
+
+ if ((cur = tb[ROUTE_MTU]) != NULL)
+ route->mtu = blobmsg_get_u32(cur);
+
+ vlist_add(&ip->route, &route->node, &route->flags);
+ return;
+
+error:
+ free(route);
+}
+