+enum {
+ ROUTE_INTERFACE,
+ ROUTE_TARGET,
+ ROUTE_MASK,
+ ROUTE_GATEWAY,
+ ROUTE_DEVICE,
+ 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_DEVICE] = { .name = "device", .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,
+};
+
+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;
+ bool config = false;
+
+ blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
+
+ if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
+ return;
+
+ 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;
+ config = true;
+ } else {
+ ip = &iface->proto_ip;
+ }
+
+ route = calloc(1, sizeof(*route));
+ if (!route)
+ return;
+
+ 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 (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
+ 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);
+
+ if ((cur = tb[ROUTE_MTU]) != NULL)
+ route->mtu = blobmsg_get_u32(cur);
+
+ if (!config && (cur = tb[ROUTE_DEVICE]) != NULL)
+ route->device = device_get(blobmsg_data(cur), true);
+
+ vlist_add(&ip->route, &route->node);
+ return;
+
+error:
+ free(route);
+}
+