From ea6dec1329b08109387f091b60a13d1b40839317 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 19 Oct 2011 22:43:14 +0200 Subject: [PATCH] add support for configuring static routes --- config.c | 36 ++++++++++++++++++++++++++++++++++++ config/network | 6 ++++++ interface-ip.c | 13 +++++++++---- interface-ip.h | 2 ++ interface.c | 4 +++- 5 files changed, 56 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index 4158cc8..5a2e439 100644 --- 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(); diff --git a/config/network b/config/network index 5514b0b..782557b 100644 --- a/config/network +++ b/config/network @@ -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 diff --git a/interface-ip.c b/interface-ip.c index 6b25f75..3db7e1a 100644 --- a/interface-ip.c +++ b/interface-ip.c @@ -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; diff --git a/interface-ip.h b/interface-ip.h index 13cd10c..78f0644 100644 --- a/interface-ip.h +++ b/interface-ip.h @@ -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); diff --git a/interface.c b/interface.c index f5d8821..0f54d51 100644 --- a/interface.c +++ b/interface.c @@ -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; + } } } -- 2.30.2