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"
28 bool config_init
= false;
30 static struct uci_context
*uci_ctx
;
31 static struct uci_package
*uci_network
;
32 static struct uci_package
*uci_wireless
;
33 static struct blob_buf b
;
36 config_parse_bridge_interface(struct uci_section
*s
)
40 name
= alloca(strlen(s
->e
.name
) + 4);
41 sprintf(name
, "br-%s", s
->e
.name
);
42 blobmsg_add_string(&b
, "name", name
);
44 uci_to_blob(&b
, s
, bridge_device_type
.config_params
);
45 if (!device_create(name
, &bridge_device_type
, b
.head
)) {
46 D(INTERFACE
, "Failed to create bridge for interface '%s'\n", s
->e
.name
);
51 blobmsg_add_string(&b
, "ifname", name
);
56 config_parse_interface(struct uci_section
*s
, bool alias
)
58 struct interface
*iface
;
59 const char *type
= NULL
;
60 struct blob_attr
*config
;
67 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
68 if (type
&& !strcmp(type
, "bridge")) {
69 if (config_parse_bridge_interface(s
))
75 uci_to_blob(&b
, s
, &interface_attr_list
);
77 iface
= interface_alloc(s
->e
.name
, b
.head
);
81 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
82 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
84 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
85 iface
->device_config
= true;
87 config
= blob_memdup(b
.head
);
92 if (!interface_add_alias(iface
, config
))
93 goto error_free_config
;
95 interface_add(iface
, config
);
99 * need to look up the interface name again, in case of config update,
100 * the pointer will have changed
102 iface
= vlist_find(&interfaces
, s
->e
.name
, iface
, node
);
106 dev
= iface
->main_dev
.dev
;
107 if (!dev
|| !dev
->default_config
)
110 blob_buf_init(&b
, 0);
111 uci_to_blob(&b
, s
, dev
->type
->config_params
);
112 if (blob_len(b
.head
) == 0)
115 device_set_config(dev
, dev
->type
, b
.head
);
124 config_parse_route(struct uci_section
*s
, bool v6
)
128 blob_buf_init(&b
, 0);
129 route
= blobmsg_open_array(&b
, "route");
130 uci_to_blob(&b
, s
, &route_attr_list
);
131 blobmsg_close_array(&b
, route
);
132 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
136 config_parse_rule(struct uci_section
*s
, bool v6
)
140 blob_buf_init(&b
, 0);
141 rule
= blobmsg_open_array(&b
, "rule");
142 uci_to_blob(&b
, s
, &rule_attr_list
);
143 blobmsg_close_array(&b
, rule
);
144 iprule_add(blob_data(b
.head
), v6
);
148 config_init_devices(void)
150 struct uci_element
*e
;
152 uci_foreach_element(&uci_network
->sections
, e
) {
153 struct uci_section
*s
= uci_to_section(e
);
154 const struct device_type
*devtype
= NULL
;
155 const char *type
, *name
;
157 if (strcmp(s
->type
, "device") != 0)
160 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
164 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
166 if (!strcmp(type
, "bridge"))
167 devtype
= &bridge_device_type
;
168 else if (!strcmp(type
, "tunnel"))
169 devtype
= &tunnel_device_type
;
170 else if (!strcmp(type
, "macvlan"))
171 devtype
= &macvlan_device_type
;
175 devtype
= &simple_device_type
;
177 blob_buf_init(&b
, 0);
178 uci_to_blob(&b
, s
, devtype
->config_params
);
179 device_create(name
, devtype
, b
.head
);
183 static struct uci_package
*
184 config_init_package(const char *config
)
186 struct uci_context
*ctx
= uci_ctx
;
187 struct uci_package
*p
= NULL
;
190 ctx
= uci_alloc_context();
194 uci_set_confdir(ctx
, "./config");
195 uci_set_savedir(ctx
, "./tmp");
198 p
= uci_lookup_package(ctx
, config
);
203 if (uci_load(ctx
, config
, &p
))
210 config_init_interfaces(void)
212 struct uci_element
*e
;
214 uci_foreach_element(&uci_network
->sections
, e
) {
215 struct uci_section
*s
= uci_to_section(e
);
217 if (!strcmp(s
->type
, "interface"))
218 config_parse_interface(s
, false);
221 uci_foreach_element(&uci_network
->sections
, e
) {
222 struct uci_section
*s
= uci_to_section(e
);
224 if (!strcmp(s
->type
, "alias"))
225 config_parse_interface(s
, true);
230 config_init_routes(void)
232 struct interface
*iface
;
233 struct uci_element
*e
;
235 vlist_for_each_element(&interfaces
, iface
, node
)
236 interface_ip_update_start(&iface
->config_ip
);
238 uci_foreach_element(&uci_network
->sections
, e
) {
239 struct uci_section
*s
= uci_to_section(e
);
241 if (!strcmp(s
->type
, "route"))
242 config_parse_route(s
, false);
243 else if (!strcmp(s
->type
, "route6"))
244 config_parse_route(s
, true);
247 vlist_for_each_element(&interfaces
, iface
, node
)
248 interface_ip_update_complete(&iface
->config_ip
);
252 config_init_rules(void)
254 struct uci_element
*e
;
256 iprule_update_start();
258 uci_foreach_element(&uci_network
->sections
, e
) {
259 struct uci_section
*s
= uci_to_section(e
);
261 if (!strcmp(s
->type
, "rule"))
262 config_parse_rule(s
, false);
263 else if (!strcmp(s
->type
, "rule6"))
264 config_parse_rule(s
, true);
267 iprule_update_complete();
271 config_init_globals(void)
273 struct uci_section
*globals
= uci_lookup_section(
274 uci_ctx
, uci_network
, "globals");
278 const char *ula_prefix
= uci_lookup_option_string(
279 uci_ctx
, globals
, "ula_prefix");
280 interface_ip_set_ula_prefix(ula_prefix
);
284 config_parse_wireless_device(struct uci_section
*s
)
286 struct wireless_driver
*drv
;
287 const char *driver_name
;
289 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
293 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
297 blob_buf_init(&b
, 0);
298 uci_to_blob(&b
, s
, drv
->device
.config
);
299 wireless_device_create(drv
, s
->e
.name
, b
.head
);
303 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
305 blob_buf_init(&b
, 0);
306 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
307 wireless_interface_create(wdev
, b
.head
, s
->e
.name
);
311 config_init_wireless(void)
313 struct wireless_device
*wdev
;
314 struct uci_element
*e
;
315 const char *dev_name
;
318 DPRINTF("No wireless configuration found\n");
322 vlist_update(&wireless_devices
);
324 uci_foreach_element(&uci_wireless
->sections
, e
) {
325 struct uci_section
*s
= uci_to_section(e
);
326 if (strcmp(s
->type
, "wifi-device") != 0)
329 config_parse_wireless_device(s
);
332 vlist_flush(&wireless_devices
);
334 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
336 vlist_update(&wdev
->interfaces
);
339 uci_foreach_element(&uci_wireless
->sections
, e
) {
340 struct uci_section
*s
= uci_to_section(e
);
342 if (strcmp(s
->type
, "wifi-iface") != 0)
345 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
349 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
351 DPRINTF("device %s not found!\n", dev_name
);
355 config_parse_wireless_interface(wdev
, s
);
358 vlist_for_each_element(&wireless_devices
, wdev
, node
)
359 vlist_flush(&wdev
->interfaces
);
363 config_init_all(void)
365 uci_network
= config_init_package("network");
367 fprintf(stderr
, "Failed to load network config\n");
371 uci_wireless
= config_init_package("wireless");
373 vlist_update(&interfaces
);
377 device_reset_config();
378 config_init_devices();
379 config_init_interfaces();
380 config_init_routes();
382 config_init_globals();
383 config_init_wireless();
389 device_init_pending();
390 vlist_flush(&interfaces
);
391 device_free_unused(NULL
);
392 interface_refresh_assignments(false);
393 interface_start_pending();
394 wireless_start_pending();