add support for configuring static routes
authorFelix Fietkau <nbd@openwrt.org>
Wed, 19 Oct 2011 20:43:14 +0000 (22:43 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Wed, 19 Oct 2011 20:43:14 +0000 (22:43 +0200)
config.c
config/network
interface-ip.c
interface-ip.h
interface.c

index 4158cc8365d12acb3fd347c6167e627e6677709f..5a2e439b9294381a38ad6a7f6579ba26eb7bb433 100644 (file)
--- a/config.c
+++ b/config.c
@@ -6,6 +6,7 @@
 
 #include "netifd.h"
 #include "interface.h"
+#include "interface-ip.h"
 #include "proto.h"
 #include "config.h"
 
@@ -187,6 +188,18 @@ config_parse_interface(struct uci_section *s)
        device_set_config(dev, dev->type, b.head);
 }
 
+static void
+config_parse_route(struct uci_section *s, bool v6)
+{
+       void *route;
+
+       blob_buf_init(&b, 0);
+       route = blobmsg_open_array(&b, "route");
+       uci_to_blob(&b, s, &route_attr_list);
+       blobmsg_close_array(&b, route);
+       interface_ip_add_route(NULL, blob_data(b.head), v6);
+}
+
 static void
 config_init_devices(void)
 {
@@ -343,6 +356,28 @@ config_init_interfaces(void)
        }
 }
 
+static void
+config_init_routes(void)
+{
+       struct interface *iface;
+       struct uci_element *e;
+
+       vlist_for_each_element(&interfaces, iface, node)
+               interface_ip_update_start(&iface->config_ip);
+
+       uci_foreach_element(&uci_network->sections, e) {
+               struct uci_section *s = uci_to_section(e);
+
+               if (!strcmp(s->type, "route"))
+                       config_parse_route(s, false);
+               else if (!strcmp(s->type, "route6"))
+                       config_parse_route(s, true);
+       }
+
+       vlist_for_each_element(&interfaces, iface, node)
+               interface_ip_update_complete(&iface->config_ip);
+}
+
 void
 config_init_all(void)
 {
@@ -358,6 +393,7 @@ config_init_all(void)
        device_reset_config();
        config_init_devices();
        config_init_interfaces();
+       config_init_routes();
 
        config_init = false;
        device_unlock();
index 5514b0b631e4135c310368109223a6e2dce24f9f..782557b01346cad439eef7a91ad89478fef8d8c9 100644 (file)
@@ -52,3 +52,9 @@ config interface test
        option proto    static
        option ipaddr   192.168.5.1
        option netmask  255.255.255.0
+
+config route
+       option target   192.168.0.1
+       option netmask  24
+       option gateway  192.168.5.2
+       option interface wan
index 6b25f75e5a7fc0308f54d5035c22ae366eee160e..3db7e1a11f18d5dbeefe3cee3e280965d33cf560 100644 (file)
@@ -21,10 +21,10 @@ enum {
        ROUTE_DEVICE,
        ROUTE_METRIC,
        ROUTE_MTU,
-       __ROUTE_LAST
+       __ROUTE_MAX
 };
 
-static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
+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 },
@@ -34,16 +34,21 @@ static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
        [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_LAST], *cur;
+       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_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
+       blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
 
        if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
                return;
index 13cd10cf9dab7eb302de3cdc5c2110d346ee3d35..78f0644c2fae100e46a6de2a4c83a1693cc835b1 100644 (file)
@@ -62,6 +62,8 @@ struct dns_search_domain {
        char name[];
 };
 
+extern const struct config_param_list route_attr_list;
+
 void interface_ip_init(struct interface_ip_settings *ip, struct interface *iface);
 void interface_add_dns_server(struct interface_ip_settings *ip, const char *str);
 void interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list);
index f5d88219ab599d278f93bf2b052726b919e0ac0b..0f54d51c13a143d8d771c4ea4ecd4743f3a860e1 100644 (file)
@@ -465,8 +465,10 @@ interface_update_complete(struct interface *iface)
 
        interface_ip_update_complete(&iface->proto_ip);
        vlist_for_each_element(&iface->config_ip.route, route, node) {
-               if (iface->l3_dev->dev)
+               if (iface->l3_dev->dev) {
                        system_add_route(iface->l3_dev->dev, route);
+                       route->enabled = true;
+               }
        }
 }