21791afa685fe63c7f9bcbc6bb4056d59ebc3941
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
, struct device_type
*devtype
)
60 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
61 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
62 blobmsg_add_string(&b
, "name", name
);
64 uci_to_blob(&b
, s
, devtype
->config_params
);
65 if (!device_create(name
, devtype
, b
.head
)) {
66 D(INTERFACE
, "Failed to create '%s' device for interface '%s'\n",
67 devtype
->name
, 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
;
82 struct device_type
*devtype
= NULL
;
84 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
85 if (disabled
&& !strcmp(disabled
, "1"))
91 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
94 devtype
= device_type_get(type
);
96 if (devtype
&& devtype
->bridge_capability
) {
97 if (config_parse_bridge_interface(s
, devtype
))
103 uci_to_blob(&b
, s
, &interface_attr_list
);
105 iface
= interface_alloc(s
->e
.name
, b
.head
);
109 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
110 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
112 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
113 iface
->device_config
= true;
115 config
= blob_memdup(b
.head
);
120 if (!interface_add_alias(iface
, config
))
121 goto error_free_config
;
123 interface_add(iface
, config
);
134 config_parse_route(struct uci_section
*s
, bool v6
)
138 blob_buf_init(&b
, 0);
139 route
= blobmsg_open_array(&b
, "route");
140 uci_to_blob(&b
, s
, &route_attr_list
);
141 blobmsg_close_array(&b
, route
);
142 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
146 config_parse_rule(struct uci_section
*s
, bool v6
)
150 blob_buf_init(&b
, 0);
151 rule
= blobmsg_open_array(&b
, "rule");
152 uci_to_blob(&b
, s
, &rule_attr_list
);
153 blobmsg_close_array(&b
, rule
);
154 iprule_add(blob_data(b
.head
), v6
);
158 config_init_devices(void)
160 struct uci_element
*e
;
162 uci_foreach_element(&uci_network
->sections
, e
) {
163 const struct uci_blob_param_list
*params
= NULL
;
164 struct uci_section
*s
= uci_to_section(e
);
165 struct device_type
*devtype
= NULL
;
167 const char *type
, *name
;
169 if (strcmp(s
->type
, "device") != 0)
172 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
176 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
178 devtype
= device_type_get(type
);
181 params
= devtype
->config_params
;
183 params
= simple_device_type
.config_params
;
185 blob_buf_init(&b
, 0);
186 uci_to_blob(&b
, s
, params
);
188 dev
= device_create(name
, devtype
, b
.head
);
192 dev
= device_get(name
, 1);
196 dev
->current_config
= true;
197 device_apply_config(dev
, dev
->type
, b
.head
);
199 dev
->default_config
= false;
203 static struct uci_package
*
204 config_init_package(const char *config
)
206 struct uci_context
*ctx
= uci_ctx
;
207 struct uci_package
*p
= NULL
;
210 ctx
= uci_alloc_context();
213 ctx
->flags
&= ~UCI_FLAG_STRICT
;
215 uci_set_confdir(ctx
, config_path
);
218 uci_set_savedir(ctx
, "./tmp");
221 p
= uci_lookup_package(ctx
, config
);
226 if (uci_load(ctx
, config
, &p
))
233 config_init_interfaces(void)
235 struct uci_element
*e
;
237 uci_foreach_element(&uci_network
->sections
, e
) {
238 struct uci_section
*s
= uci_to_section(e
);
240 if (!strcmp(s
->type
, "interface"))
241 config_parse_interface(s
, false);
244 uci_foreach_element(&uci_network
->sections
, e
) {
245 struct uci_section
*s
= uci_to_section(e
);
247 if (!strcmp(s
->type
, "alias"))
248 config_parse_interface(s
, true);
253 config_init_routes(void)
255 struct interface
*iface
;
256 struct uci_element
*e
;
258 vlist_for_each_element(&interfaces
, iface
, node
)
259 interface_ip_update_start(&iface
->config_ip
);
261 uci_foreach_element(&uci_network
->sections
, e
) {
262 struct uci_section
*s
= uci_to_section(e
);
264 if (!strcmp(s
->type
, "route"))
265 config_parse_route(s
, false);
266 else if (!strcmp(s
->type
, "route6"))
267 config_parse_route(s
, true);
270 vlist_for_each_element(&interfaces
, iface
, node
)
271 interface_ip_update_complete(&iface
->config_ip
);
275 config_init_rules(void)
277 struct uci_element
*e
;
279 iprule_update_start();
281 uci_foreach_element(&uci_network
->sections
, e
) {
282 struct uci_section
*s
= uci_to_section(e
);
284 if (!strcmp(s
->type
, "rule"))
285 config_parse_rule(s
, false);
286 else if (!strcmp(s
->type
, "rule6"))
287 config_parse_rule(s
, true);
290 iprule_update_complete();
293 config_parse_global_ps_val(struct uci_section
*globals
, const char *option
)
295 const char *val
= uci_lookup_option_string(
296 uci_ctx
, globals
, option
);
300 ret
= strtol(val
, 0, 10);
306 config_init_globals(void)
308 struct uci_section
*globals
= uci_lookup_section(
309 uci_ctx
, uci_network
, "globals");
313 const char *ula_prefix
= uci_lookup_option_string(
314 uci_ctx
, globals
, "ula_prefix");
315 interface_ip_set_ula_prefix(ula_prefix
);
317 const char *default_ps
= uci_lookup_option_string(
318 uci_ctx
, globals
, "default_ps");
321 device_set_default_ps(strcmp(default_ps
, "1") ? false : true,
322 config_parse_global_ps_val(globals
, "default_xps_val"),
323 config_parse_global_ps_val(globals
, "default_rps_val"),
324 config_parse_global_ps_val(globals
, "default_rps_flow_cnt"));
328 config_parse_wireless_device(struct uci_section
*s
)
330 struct wireless_driver
*drv
;
331 const char *driver_name
;
333 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
337 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
341 blob_buf_init(&b
, 0);
342 uci_to_blob(&b
, s
, drv
->device
.config
);
343 wireless_device_create(drv
, s
->e
.name
, b
.head
);
347 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
351 name
= alloca(strlen(s
->type
) + 16);
352 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
354 blob_buf_init(&b
, 0);
355 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
356 wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
360 config_init_wireless(void)
362 struct wireless_device
*wdev
;
363 struct uci_element
*e
;
364 const char *dev_name
;
367 DPRINTF("No wireless configuration found\n");
371 vlist_update(&wireless_devices
);
373 uci_foreach_element(&uci_wireless
->sections
, e
) {
374 struct uci_section
*s
= uci_to_section(e
);
375 if (strcmp(s
->type
, "wifi-device") != 0)
378 config_parse_wireless_device(s
);
381 vlist_flush(&wireless_devices
);
383 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
385 vlist_update(&wdev
->interfaces
);
388 uci_foreach_element(&uci_wireless
->sections
, e
) {
389 struct uci_section
*s
= uci_to_section(e
);
391 if (strcmp(s
->type
, "wifi-iface") != 0)
394 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
398 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
400 DPRINTF("device %s not found!\n", dev_name
);
404 config_parse_wireless_interface(wdev
, s
);
407 vlist_for_each_element(&wireless_devices
, wdev
, node
)
408 vlist_flush(&wdev
->interfaces
);
412 config_init_all(void)
417 uci_network
= config_init_package("network");
419 uci_get_errorstr(uci_ctx
, &err
, NULL
);
420 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
425 uci_wireless
= config_init_package("wireless");
426 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
427 uci_get_errorstr(uci_ctx
, &err
, NULL
);
428 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
433 vlist_update(&interfaces
);
437 device_reset_config();
438 config_init_devices();
439 config_init_interfaces();
440 config_init_routes();
442 config_init_globals();
443 config_init_wireless();
449 device_init_pending();
450 vlist_flush(&interfaces
);
451 device_free_unused(NULL
);
452 interface_refresh_assignments(false);
453 interface_start_pending();
454 wireless_start_pending();