+enum {
+ ROUTE_INTERFACE,
+ ROUTE_TARGET,
+ ROUTE_MASK,
+ ROUTE_GATEWAY,
+ ROUTE_METRIC,
+ ROUTE_MTU,
+ ROUTE_VALID,
+ ROUTE_TABLE,
+ ROUTE_SOURCE,
+ ROUTE_ONLINK,
+ ROUTE_TYPE,
+ __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 },
+ [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
+ [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
+ [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL },
+ [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }
+};
+
+const struct uci_blob_param_list route_attr_list = {
+ .n_params = __ROUTE_MAX,
+ .params = route_attr,
+};
+
+
+struct list_head prefixes = LIST_HEAD_INIT(prefixes);
+static struct device_prefix *ula_prefix = NULL;
+static struct uloop_timeout valid_until_timeout;
+
+
+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 int set_ip_source_policy(bool add, bool v6, unsigned int priority,
+ const union if_addr *addr, uint8_t mask, unsigned int table,
+ struct interface *in_iface, const char *action)
+{
+ struct iprule rule = {
+ .flags = IPRULE_PRIORITY,
+ .priority = priority
+ };
+
+ if (addr) {
+ rule.flags |= IPRULE_SRC;
+ rule.src_addr = *addr;
+ rule.src_mask = mask;
+ }
+
+ if (table) {
+ rule.flags |= IPRULE_LOOKUP;
+ rule.lookup = table;
+
+ if (!rule.lookup)
+ return 0;
+ } else if (action) {
+ rule.flags |= IPRULE_ACTION;
+ system_resolve_iprule_action(action, &rule.action);
+ }
+
+ if (in_iface && in_iface->l3_dev.dev) {
+ rule.flags |= IPRULE_IN;
+ strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
+ }
+
+ rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
+
+ return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
+}
+
+static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
+{
+ struct iprule rule = {
+ .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
+ .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
+ .lookup = (v6) ? iface->ip6table : iface->ip4table,
+ .in_dev = "lo"
+ };
+
+ if (!rule.lookup)
+ return 0;
+
+ rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
+
+ return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
+}
+
+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;
+
+ // Handle offlink addresses correctly
+ unsigned int mask = addr->mask;
+ if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
+ (addr->flags & DEVADDR_OFFLINK))
+ mask = 128;
+
+ if (!match_if_addr(&addr->addr, a, 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 (route->flags & DEVROUTE_TABLE)
+ 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);
+
+ if (iface) {
+ /* 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);
+ } else {
+ 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;
+ route->table = r_next->table;
+
+done:
+ route->iface = iface;
+ if (defaultroute_target)
+ free(route);
+ else
+ vlist_add(&iface->host_routes, &route->node, route);
+ 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;
+ bool is_proto_route = !!iface;
+
+ 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;
+ } else
+ route->metric = iface->metric;
+
+ if ((cur = tb[ROUTE_MTU]) != NULL) {
+ route->mtu = blobmsg_get_u32(cur);
+ route->flags |= DEVROUTE_MTU;
+ }
+
+ // Use source-based routing
+ if ((cur = tb[ROUTE_SOURCE]) != NULL) {
+ char *saveptr, *source = alloca(blobmsg_data_len(cur));
+ memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
+
+ const char *addr = strtok_r(source, "/", &saveptr);
+ const char *mask = strtok_r(NULL, "/", &saveptr);
+
+ if (!addr || inet_pton(af, addr, &route->source) < 1) {
+ DPRINTF("Failed to parse route source: %s\n", addr);
+ goto error;
+ }
+
+ route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
+ }
+
+ if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur))
+ route->flags |= DEVROUTE_ONLINK;
+
+ if (is_proto_route) {
+ route->table = (v6) ? iface->ip6table : iface->ip4table;
+ route->flags |= DEVROUTE_SRCTABLE;
+ }
+
+ if ((cur = tb[ROUTE_TABLE]) != NULL) {
+ if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
+ DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
+ goto error;
+ }
+
+ if (route->table)
+ route->flags |= DEVROUTE_TABLE;
+ }
+
+ if ((cur = tb[ROUTE_VALID]) != NULL) {
+ int64_t valid = blobmsg_get_u32(cur);
+ int64_t valid_until = valid + (int64_t)system_get_rtime();
+ if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
+ route->valid_until = valid_until;
+ }
+
+ if ((cur = tb[ROUTE_TYPE]) != NULL) {
+ if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
+ DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
+ goto error;
+ }
+ route->flags |= DEVROUTE_TYPE;
+ }
+
+ vlist_add(&ip->route, &route->node, route);
+ return;
+
+error:
+ free(route);
+}
+