8db2b59b15514d1e3367c36781d263e28de1171e
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include "interface.h"
22 #include "interface-ip.h"
27 bool config_init
= false;
29 static struct uci_context
*uci_ctx
;
30 static struct uci_package
*uci_network
;
31 static struct blob_buf b
;
34 config_parse_bridge_interface(struct uci_section
*s
)
38 name
= alloca(strlen(s
->e
.name
) + 4);
39 sprintf(name
, "br-%s", s
->e
.name
);
40 blobmsg_add_string(&b
, "name", name
);
42 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
43 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
44 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
49 blobmsg_add_string(&b
, "ifname", name
);
54 config_parse_interface(struct uci_section
*s
, bool alias
)
56 struct interface
*iface
;
57 const char *type
= NULL
;
58 struct blob_attr
*config
;
65 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
66 if (type
&& !strcmp(type
, "bridge")) {
67 if (config_parse_bridge_interface(s
))
73 uci_to_blob(&b
, s
, &interface_attr_list
);
74 iface
= calloc(1, sizeof(*iface
));
78 interface_init(iface
, s
->e
.name
, b
.head
);
80 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
81 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
83 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
84 iface
->device_config
= true;
86 config
= blob_memdup(b
.head
);
91 if (!interface_add_alias(iface
, config
))
92 goto error_free_config
;
94 interface_add(iface
, config
);
98 * need to look up the interface name again, in case of config update,
99 * the pointer will have changed
101 iface
= vlist_find(&interfaces
, s
->e
.name
, iface
, node
);
105 dev
= iface
->main_dev
.dev
;
106 if (!dev
|| !dev
->default_config
)
109 blob_buf_init(&b
, 0);
110 uci_to_blob(&b
, s
, dev
->type
->config_params
);
111 if (blob_len(b
.head
) == 0)
114 device_set_config(dev
, dev
->type
, b
.head
);
123 config_parse_route(struct uci_section
*s
, bool v6
)
127 blob_buf_init(&b
, 0);
128 route
= blobmsg_open_array(&b
, "route");
129 uci_to_blob(&b
, s
, &route_attr_list
);
130 blobmsg_close_array(&b
, route
);
131 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
135 config_parse_rule(struct uci_section
*s
, bool v6
)
139 blob_buf_init(&b
, 0);
140 rule
= blobmsg_open_array(&b
, "rule");
141 uci_to_blob(&b
, s
, &rule_attr_list
);
142 blobmsg_close_array(&b
, rule
);
143 iprule_add(blob_data(b
.head
), v6
);
147 config_init_devices(void)
149 struct uci_element
*e
;
151 uci_foreach_element(&uci_network
->sections
, e
) {
152 struct uci_section
*s
= uci_to_section(e
);
153 const struct device_type
*devtype
= NULL
;
154 const char *type
, *name
;
156 if (strcmp(s
->type
, "device") != 0)
159 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
163 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
165 if (!strcmp(type
, "bridge"))
166 devtype
= &bridge_device_type
;
167 else if (!strcmp(type
, "tunnel"))
168 devtype
= &tunnel_device_type
;
172 devtype
= &simple_device_type
;
174 blob_buf_init(&b
, 0);
175 uci_to_blob(&b
, s
, devtype
->config_params
);
176 device_create(name
, devtype
, b
.head
);
180 static struct uci_package
*
181 config_init_package(const char *config
)
183 struct uci_context
*ctx
= uci_ctx
;
184 struct uci_package
*p
= NULL
;
187 ctx
= uci_alloc_context();
191 uci_set_confdir(ctx
, "./config");
192 uci_set_savedir(ctx
, "./tmp");
195 p
= uci_lookup_package(ctx
, config
);
200 if (uci_load(ctx
, config
, &p
))
207 config_init_interfaces(void)
209 struct uci_element
*e
;
211 uci_foreach_element(&uci_network
->sections
, e
) {
212 struct uci_section
*s
= uci_to_section(e
);
214 if (!strcmp(s
->type
, "interface"))
215 config_parse_interface(s
, false);
218 uci_foreach_element(&uci_network
->sections
, e
) {
219 struct uci_section
*s
= uci_to_section(e
);
221 if (!strcmp(s
->type
, "alias"))
222 config_parse_interface(s
, true);
227 config_init_routes(void)
229 struct interface
*iface
;
230 struct uci_element
*e
;
232 vlist_for_each_element(&interfaces
, iface
, node
)
233 interface_ip_update_start(&iface
->config_ip
);
235 uci_foreach_element(&uci_network
->sections
, e
) {
236 struct uci_section
*s
= uci_to_section(e
);
238 if (!strcmp(s
->type
, "route"))
239 config_parse_route(s
, false);
240 else if (!strcmp(s
->type
, "route6"))
241 config_parse_route(s
, true);
244 vlist_for_each_element(&interfaces
, iface
, node
)
245 interface_ip_update_complete(&iface
->config_ip
);
249 config_init_rules(void)
251 struct uci_element
*e
;
253 iprule_update_start();
255 uci_foreach_element(&uci_network
->sections
, e
) {
256 struct uci_section
*s
= uci_to_section(e
);
258 if (!strcmp(s
->type
, "rule"))
259 config_parse_rule(s
, false);
260 else if (!strcmp(s
->type
, "rule6"))
261 config_parse_rule(s
, true);
264 iprule_update_complete();
268 config_init_globals(void)
270 struct uci_section
*globals
= uci_lookup_section(
271 uci_ctx
, uci_network
, "globals");
275 const char *ula_prefix
= uci_lookup_option_string(
276 uci_ctx
, globals
, "ula_prefix");
277 interface_ip_set_ula_prefix(ula_prefix
);
281 config_init_all(void)
283 uci_network
= config_init_package("network");
285 fprintf(stderr
, "Failed to load network config\n");
289 vlist_update(&interfaces
);
293 device_reset_config();
294 config_init_devices();
295 config_init_interfaces();
296 config_init_routes();
298 config_init_globals();
304 device_init_pending();
305 vlist_flush(&interfaces
);
306 device_free_unused(NULL
);
307 interface_refresh_assignments(false);
308 interface_start_pending();