Implement support for route / route6 table attribute
authorJo-Philipp Wich <jow@openwrt.org>
Thu, 4 Apr 2013 12:41:38 +0000 (14:41 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 4 Apr 2013 12:44:16 +0000 (14:44 +0200)
interface-ip.c
interface-ip.h
system-dummy.c
system-linux.c
system.h

index ed95b2e..190c158 100644 (file)
@@ -34,6 +34,7 @@ enum {
        ROUTE_METRIC,
        ROUTE_MTU,
        ROUTE_VALID,
+       ROUTE_TABLE,
        __ROUTE_MAX
 };
 
@@ -44,6 +45,7 @@ static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
        [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 },
 };
 
@@ -124,6 +126,9 @@ __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
                if (!match_if_addr(&route->addr, a, route->mask))
                        continue;
 
+               if (route->flags & DEVROUTE_TABLE)
+                       continue;
+
                if (!*res || route->mask < (*res)->mask)
                        *res = route;
        }
@@ -251,6 +256,16 @@ interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
                route->flags |= DEVROUTE_MTU;
        }
 
+       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)
                route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
 
index 91358b8..ab78bf7 100644 (file)
@@ -36,6 +36,9 @@ enum device_addr_flags {
 
        /* address is off-link (no subnet-route) */
        DEVADDR_OFFLINK         = (1 << 6),
+
+       /* route resides in different table */
+       DEVROUTE_TABLE          = (1 << 7),
 };
 
 union if_addr {
@@ -93,6 +96,7 @@ struct device_route {
 
        union if_addr nexthop;
        int mtu;
+       unsigned int table;
        time_t valid_until;
 
        /* must be last */
index 14c7a97..2c15e2c 100644 (file)
@@ -191,6 +191,11 @@ int system_flush_routes(void)
        return 0;
 }
 
+int system_resolve_rt_table(const char *name, struct device_route *route)
+{
+       return 0;
+}
+
 time_t system_get_rtime(void)
 {
        struct timeval tv;
index 8345e5d..d083580 100644 (file)
@@ -942,10 +942,12 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
        unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
                        (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
 
+       unsigned int table = (route->flags & DEVROUTE_TABLE) ? route->table : RT_TABLE_MAIN;
+
        struct rtmsg rtm = {
                .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
                .rtm_dst_len = route->mask,
-               .rtm_table = RT_TABLE_MAIN,
+               .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
                .rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_STATIC,
                .rtm_scope = scope,
                .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
@@ -979,6 +981,9 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
        if (dev)
                nla_put_u32(msg, RTA_OIF, dev->ifindex);
 
+       if (table >= 256)
+               nla_put_u32(msg, RTA_TABLE, table);
+
        return system_rtnl_call(msg);
 }
 
@@ -1011,6 +1016,56 @@ int system_flush_routes(void)
        return 0;
 }
 
+bool system_resolve_rt_table(const char *name, unsigned int *id)
+{
+       FILE *f;
+       char *e, buf[128];
+       unsigned int n, table = RT_TABLE_UNSPEC;
+
+       /* first try to parse table as number */
+       if ((n = strtoul(name, &e, 0)) > 0 && !*e)
+               table = n;
+
+       /* handle well known aliases */
+       else if (!strcmp(name, "default"))
+               table = RT_TABLE_DEFAULT;
+       else if (!strcmp(name, "main"))
+               table = RT_TABLE_MAIN;
+       else if (!strcmp(name, "local"))
+               table = RT_TABLE_LOCAL;
+
+       /* try to look up name in /etc/iproute2/rt_tables */
+       else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
+       {
+               while (fgets(buf, sizeof(buf) - 1, f) != NULL)
+               {
+                       if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
+                               continue;
+
+                       n = strtoul(e, NULL, 10);
+                       e = strtok(NULL, " \t\n");
+
+                       if (e && !strcmp(e, name))
+                       {
+                               table = n;
+                               break;
+                       }
+               }
+
+               fclose(f);
+       }
+
+       if (table == RT_TABLE_UNSPEC)
+               return false;
+
+       /* do not consider main table special */
+       if (table == RT_TABLE_MAIN)
+               table = RT_TABLE_UNSPEC;
+
+       *id = table;
+       return true;
+}
+
 time_t system_get_rtime(void)
 {
        struct timespec ts;
index acfaa37..b093371 100644 (file)
--- a/system.h
+++ b/system.h
@@ -94,6 +94,8 @@ int system_add_route(struct device *dev, struct device_route *route);
 int system_del_route(struct device *dev, struct device_route *route);
 int system_flush_routes(void);
 
+bool system_resolve_rt_table(const char *name, unsigned int *id);
+
 int system_del_ip_tunnel(const char *name);
 int system_add_ip_tunnel(const char *name, struct blob_attr *attr);