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
;
169 else if (!strcmp(type
, "macvlan"))
170 devtype
= &macvlan_device_type
;
174 devtype
= &simple_device_type
;
176 blob_buf_init(&b
, 0);
177 uci_to_blob(&b
, s
, devtype
->config_params
);
178 device_create(name
, devtype
, b
.head
);
182 static struct uci_package
*
183 config_init_package(const char *config
)
185 struct uci_context
*ctx
= uci_ctx
;
186 struct uci_package
*p
= NULL
;
189 ctx
= uci_alloc_context();
193 uci_set_confdir(ctx
, "./config");
194 uci_set_savedir(ctx
, "./tmp");
197 p
= uci_lookup_package(ctx
, config
);
202 if (uci_load(ctx
, config
, &p
))
209 config_init_interfaces(void)
211 struct uci_element
*e
;
213 uci_foreach_element(&uci_network
->sections
, e
) {
214 struct uci_section
*s
= uci_to_section(e
);
216 if (!strcmp(s
->type
, "interface"))
217 config_parse_interface(s
, false);
220 uci_foreach_element(&uci_network
->sections
, e
) {
221 struct uci_section
*s
= uci_to_section(e
);
223 if (!strcmp(s
->type
, "alias"))
224 config_parse_interface(s
, true);
229 config_init_routes(void)
231 struct interface
*iface
;
232 struct uci_element
*e
;
234 vlist_for_each_element(&interfaces
, iface
, node
)
235 interface_ip_update_start(&iface
->config_ip
);
237 uci_foreach_element(&uci_network
->sections
, e
) {
238 struct uci_section
*s
= uci_to_section(e
);
240 if (!strcmp(s
->type
, "route"))
241 config_parse_route(s
, false);
242 else if (!strcmp(s
->type
, "route6"))
243 config_parse_route(s
, true);
246 vlist_for_each_element(&interfaces
, iface
, node
)
247 interface_ip_update_complete(&iface
->config_ip
);
251 config_init_rules(void)
253 struct uci_element
*e
;
255 iprule_update_start();
257 uci_foreach_element(&uci_network
->sections
, e
) {
258 struct uci_section
*s
= uci_to_section(e
);
260 if (!strcmp(s
->type
, "rule"))
261 config_parse_rule(s
, false);
262 else if (!strcmp(s
->type
, "rule6"))
263 config_parse_rule(s
, true);
266 iprule_update_complete();
270 config_init_globals(void)
272 struct uci_section
*globals
= uci_lookup_section(
273 uci_ctx
, uci_network
, "globals");
277 const char *ula_prefix
= uci_lookup_option_string(
278 uci_ctx
, globals
, "ula_prefix");
279 interface_ip_set_ula_prefix(ula_prefix
);
283 config_init_all(void)
285 uci_network
= config_init_package("network");
287 fprintf(stderr
, "Failed to load network config\n");
291 vlist_update(&interfaces
);
295 device_reset_config();
296 config_init_devices();
297 config_init_interfaces();
298 config_init_routes();
300 config_init_globals();
306 device_init_pending();
307 vlist_flush(&interfaces
);
308 device_free_unused(NULL
);
309 interface_refresh_assignments(false);
310 interface_start_pending();