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.
22 #include "interface.h"
23 #include "interface-ip.h"
29 bool config_init
= false;
31 static struct uci_context
*uci_ctx
;
32 static struct uci_package
*uci_network
;
33 static struct uci_package
*uci_wireless
;
34 static struct blob_buf b
;
37 config_section_idx(struct uci_section
*s
)
39 struct uci_element
*e
;
42 uci_foreach_element(&uci_wireless
->sections
, e
) {
43 struct uci_section
*cur
= uci_to_section(e
);
48 if (!strcmp(cur
->type
, s
->type
))
56 config_parse_bridge_interface(struct uci_section
*s
)
60 name
= alloca(strlen(s
->e
.name
) + 4);
61 sprintf(name
, "br-%s", s
->e
.name
);
62 blobmsg_add_string(&b
, "name", name
);
64 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
65 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
66 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
71 blobmsg_add_string(&b
, "ifname", name
);
76 config_parse_interface(struct uci_section
*s
, bool alias
)
78 struct interface
*iface
;
79 const char *type
= NULL
, *disabled
;
80 struct blob_attr
*config
;
83 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
84 if (disabled
&& !strcmp(disabled
, "1"))
90 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
91 if (type
&& !strcmp(type
, "bridge")) {
92 if (config_parse_bridge_interface(s
))
98 uci_to_blob(&b
, s
, &interface_attr_list
);
100 iface
= interface_alloc(s
->e
.name
, b
.head
);
104 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
105 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
107 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
108 iface
->device_config
= true;
110 config
= blob_memdup(b
.head
);
115 if (!interface_add_alias(iface
, config
))
116 goto error_free_config
;
118 interface_add(iface
, config
);
129 config_parse_route(struct uci_section
*s
, bool v6
)
133 blob_buf_init(&b
, 0);
134 route
= blobmsg_open_array(&b
, "route");
135 uci_to_blob(&b
, s
, &route_attr_list
);
136 blobmsg_close_array(&b
, route
);
137 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
141 config_parse_rule(struct uci_section
*s
, bool v6
)
145 blob_buf_init(&b
, 0);
146 rule
= blobmsg_open_array(&b
, "rule");
147 uci_to_blob(&b
, s
, &rule_attr_list
);
148 blobmsg_close_array(&b
, rule
);
149 iprule_add(blob_data(b
.head
), v6
);
153 config_init_devices(void)
155 struct uci_element
*e
;
157 uci_foreach_element(&uci_network
->sections
, e
) {
158 const struct uci_blob_param_list
*params
= NULL
;
159 struct uci_section
*s
= uci_to_section(e
);
160 const struct device_type
*devtype
= NULL
;
162 const char *type
, *name
;
164 if (strcmp(s
->type
, "device") != 0)
167 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
171 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
173 if (!strcmp(type
, "8021ad"))
174 devtype
= &vlandev_device_type
;
175 else if (!strcmp(type
, "8021q"))
176 devtype
= &vlandev_device_type
;
177 else if (!strcmp(type
, "bridge"))
178 devtype
= &bridge_device_type
;
179 else if (!strcmp(type
, "macvlan"))
180 devtype
= &macvlan_device_type
;
181 else if (!strcmp(type
, "tunnel"))
182 devtype
= &tunnel_device_type
;
186 params
= devtype
->config_params
;
188 params
= simple_device_type
.config_params
;
190 blob_buf_init(&b
, 0);
191 uci_to_blob(&b
, s
, params
);
193 dev
= device_create(name
, devtype
, b
.head
);
197 dev
= device_get(name
, 1);
201 dev
->current_config
= true;
202 device_apply_config(dev
, dev
->type
, b
.head
);
204 dev
->default_config
= false;
208 static struct uci_package
*
209 config_init_package(const char *config
)
211 struct uci_context
*ctx
= uci_ctx
;
212 struct uci_package
*p
= NULL
;
215 ctx
= uci_alloc_context();
218 ctx
->flags
&= ~UCI_FLAG_STRICT
;
220 uci_set_confdir(ctx
, config_path
);
223 uci_set_savedir(ctx
, "./tmp");
226 p
= uci_lookup_package(ctx
, config
);
231 if (uci_load(ctx
, config
, &p
))
238 config_init_interfaces(void)
240 struct uci_element
*e
;
242 uci_foreach_element(&uci_network
->sections
, e
) {
243 struct uci_section
*s
= uci_to_section(e
);
245 if (!strcmp(s
->type
, "interface"))
246 config_parse_interface(s
, false);
249 uci_foreach_element(&uci_network
->sections
, e
) {
250 struct uci_section
*s
= uci_to_section(e
);
252 if (!strcmp(s
->type
, "alias"))
253 config_parse_interface(s
, true);
258 config_init_routes(void)
260 struct interface
*iface
;
261 struct uci_element
*e
;
263 vlist_for_each_element(&interfaces
, iface
, node
)
264 interface_ip_update_start(&iface
->config_ip
);
266 uci_foreach_element(&uci_network
->sections
, e
) {
267 struct uci_section
*s
= uci_to_section(e
);
269 if (!strcmp(s
->type
, "route"))
270 config_parse_route(s
, false);
271 else if (!strcmp(s
->type
, "route6"))
272 config_parse_route(s
, true);
275 vlist_for_each_element(&interfaces
, iface
, node
)
276 interface_ip_update_complete(&iface
->config_ip
);
280 config_init_rules(void)
282 struct uci_element
*e
;
284 iprule_update_start();
286 uci_foreach_element(&uci_network
->sections
, e
) {
287 struct uci_section
*s
= uci_to_section(e
);
289 if (!strcmp(s
->type
, "rule"))
290 config_parse_rule(s
, false);
291 else if (!strcmp(s
->type
, "rule6"))
292 config_parse_rule(s
, true);
295 iprule_update_complete();
299 config_init_globals(void)
301 struct uci_section
*globals
= uci_lookup_section(
302 uci_ctx
, uci_network
, "globals");
306 const char *ula_prefix
= uci_lookup_option_string(
307 uci_ctx
, globals
, "ula_prefix");
308 interface_ip_set_ula_prefix(ula_prefix
);
310 const char *default_ps
= uci_lookup_option_string(
311 uci_ctx
, globals
, "default_ps");
314 device_set_default_ps(strcmp(default_ps
, "1") ? false : true);
318 config_parse_wireless_device(struct uci_section
*s
)
320 struct wireless_driver
*drv
;
321 const char *driver_name
;
323 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
327 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
331 blob_buf_init(&b
, 0);
332 uci_to_blob(&b
, s
, drv
->device
.config
);
333 wireless_device_create(drv
, s
->e
.name
, b
.head
);
337 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
341 name
= alloca(strlen(s
->type
) + 16);
342 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
344 blob_buf_init(&b
, 0);
345 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
346 wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
350 config_init_wireless(void)
352 struct wireless_device
*wdev
;
353 struct uci_element
*e
;
354 const char *dev_name
;
357 DPRINTF("No wireless configuration found\n");
361 vlist_update(&wireless_devices
);
363 uci_foreach_element(&uci_wireless
->sections
, e
) {
364 struct uci_section
*s
= uci_to_section(e
);
365 if (strcmp(s
->type
, "wifi-device") != 0)
368 config_parse_wireless_device(s
);
371 vlist_flush(&wireless_devices
);
373 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
375 vlist_update(&wdev
->interfaces
);
378 uci_foreach_element(&uci_wireless
->sections
, e
) {
379 struct uci_section
*s
= uci_to_section(e
);
381 if (strcmp(s
->type
, "wifi-iface") != 0)
384 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
388 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
390 DPRINTF("device %s not found!\n", dev_name
);
394 config_parse_wireless_interface(wdev
, s
);
397 vlist_for_each_element(&wireless_devices
, wdev
, node
)
398 vlist_flush(&wdev
->interfaces
);
402 config_init_all(void)
404 uci_network
= config_init_package("network");
406 fprintf(stderr
, "Failed to load network config\n");
410 uci_wireless
= config_init_package("wireless");
412 vlist_update(&interfaces
);
416 device_reset_config();
417 config_init_devices();
418 config_init_interfaces();
419 config_init_routes();
421 config_init_globals();
422 config_init_wireless();
428 device_init_pending();
429 vlist_flush(&interfaces
);
430 device_free_unused(NULL
);
431 interface_refresh_assignments(false);
432 interface_start_pending();
433 wireless_start_pending();