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
, false);
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 if (!interface_add(iface
, config
))
124 goto error_free_config
;
135 config_parse_route(struct uci_section
*s
, bool v6
)
139 blob_buf_init(&b
, 0);
140 route
= blobmsg_open_array(&b
, "route");
141 uci_to_blob(&b
, s
, &route_attr_list
);
142 blobmsg_close_array(&b
, route
);
143 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
147 config_parse_neighbor(struct uci_section
*s
, bool v6
)
151 neighbor
= blobmsg_open_array(&b
, "neighbor");
152 uci_to_blob(&b
,s
, &neighbor_attr_list
);
153 blobmsg_close_array(&b
, neighbor
);
154 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
158 config_parse_rule(struct uci_section
*s
, bool v6
)
162 blob_buf_init(&b
, 0);
163 rule
= blobmsg_open_array(&b
, "rule");
164 uci_to_blob(&b
, s
, &rule_attr_list
);
165 blobmsg_close_array(&b
, rule
);
166 iprule_add(blob_data(b
.head
), v6
);
170 config_init_devices(void)
172 struct uci_element
*e
;
174 uci_foreach_element(&uci_network
->sections
, e
) {
175 const struct uci_blob_param_list
*params
= NULL
;
176 struct uci_section
*s
= uci_to_section(e
);
177 struct device_type
*devtype
= NULL
;
179 const char *type
, *name
;
181 if (strcmp(s
->type
, "device") != 0)
184 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
188 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
190 devtype
= device_type_get(type
);
193 params
= devtype
->config_params
;
195 params
= simple_device_type
.config_params
;
197 blob_buf_init(&b
, 0);
198 uci_to_blob(&b
, s
, params
);
200 dev
= device_create(name
, devtype
, b
.head
);
204 dev
= device_get(name
, 1);
208 dev
->current_config
= true;
209 device_apply_config(dev
, dev
->type
, b
.head
);
211 dev
->default_config
= false;
216 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
224 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
225 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
226 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
227 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
229 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
230 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
232 static const struct uci_blob_param_list vlan_attr_list
= {
233 .n_params
= __BRVLAN_ATTR_MAX
,
234 .params
= vlan_attrs
,
235 .info
= vlan_attr_info
,
237 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
238 struct blob_attr
*cur
;
239 struct bridge_vlan_port
*port
;
240 struct bridge_vlan
*vlan
;
248 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
252 blob_buf_init(&b
, 0);
253 uci_to_blob(&b
, s
, &vlan_attr_list
);
254 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
256 if (!tb
[BRVLAN_ATTR_VID
])
259 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
260 if (!vid
|| vid
> 4095)
263 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
264 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
268 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
274 if (tb
[BRVLAN_ATTR_LOCAL
])
275 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
277 vlan
->n_ports
= n_ports
;
278 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
279 name_buf
= (char *)&port
[n_ports
];
281 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
284 port
->ifname
= name_buf
;
285 port
->flags
= BRVLAN_F_UNTAGGED
;
286 strcpy(name_buf
, blobmsg_get_string(cur
));
288 sep
= strchr(name_buf
, ':');
290 for (*sep
= 0, sep
++; *sep
; sep
++)
293 port
->flags
|= BRVLAN_F_PVID
;
296 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
301 name_buf
+= strlen(name_buf
) + 1;
305 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
310 config_init_vlans(void)
312 struct uci_element
*e
;
315 device_vlan_update(false);
316 uci_foreach_element(&uci_network
->sections
, e
) {
317 struct uci_section
*s
= uci_to_section(e
);
320 if (strcmp(s
->type
, "bridge-vlan") != 0)
323 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
327 dev
= device_get(name
, 0);
328 if (!dev
|| !dev
->vlans
.update
)
331 config_parse_vlan(dev
, s
);
333 device_vlan_update(true);
336 static struct uci_package
*
337 config_init_package(const char *config
)
339 struct uci_context
*ctx
= uci_ctx
;
340 struct uci_package
*p
= NULL
;
343 ctx
= uci_alloc_context();
346 ctx
->flags
&= ~UCI_FLAG_STRICT
;
348 uci_set_confdir(ctx
, config_path
);
351 uci_set_savedir(ctx
, "./tmp");
354 p
= uci_lookup_package(ctx
, config
);
359 if (uci_load(ctx
, config
, &p
))
366 config_init_interfaces(void)
368 struct uci_element
*e
;
370 uci_foreach_element(&uci_network
->sections
, e
) {
371 struct uci_section
*s
= uci_to_section(e
);
373 if (!strcmp(s
->type
, "interface"))
374 config_parse_interface(s
, false);
377 uci_foreach_element(&uci_network
->sections
, e
) {
378 struct uci_section
*s
= uci_to_section(e
);
380 if (!strcmp(s
->type
, "alias"))
381 config_parse_interface(s
, true);
388 struct interface
*iface
;
389 struct uci_element
*e
;
391 vlist_for_each_element(&interfaces
, iface
, node
)
392 interface_ip_update_start(&iface
->config_ip
);
394 uci_foreach_element(&uci_network
->sections
, e
) {
395 struct uci_section
*s
= uci_to_section(e
);
397 if (!strcmp(s
->type
, "route"))
398 config_parse_route(s
, false);
399 else if (!strcmp(s
->type
, "route6"))
400 config_parse_route(s
, true);
401 if (!strcmp(s
->type
, "neighbor"))
402 config_parse_neighbor(s
, false);
403 else if (!strcmp(s
->type
, "neighbor6"))
404 config_parse_neighbor(s
, true);
407 vlist_for_each_element(&interfaces
, iface
, node
)
408 interface_ip_update_complete(&iface
->config_ip
);
412 config_init_rules(void)
414 struct uci_element
*e
;
416 iprule_update_start();
418 uci_foreach_element(&uci_network
->sections
, e
) {
419 struct uci_section
*s
= uci_to_section(e
);
421 if (!strcmp(s
->type
, "rule"))
422 config_parse_rule(s
, false);
423 else if (!strcmp(s
->type
, "rule6"))
424 config_parse_rule(s
, true);
427 iprule_update_complete();
431 config_init_globals(void)
433 struct uci_section
*globals
= uci_lookup_section(
434 uci_ctx
, uci_network
, "globals");
438 const char *ula_prefix
= uci_lookup_option_string(
439 uci_ctx
, globals
, "ula_prefix");
440 interface_ip_set_ula_prefix(ula_prefix
);
444 config_parse_wireless_device(struct uci_section
*s
)
446 struct wireless_driver
*drv
;
447 const char *driver_name
;
449 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
453 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
457 blob_buf_init(&b
, 0);
458 uci_to_blob(&b
, s
, drv
->device
.config
);
459 wireless_device_create(drv
, s
->e
.name
, b
.head
);
462 static struct wireless_interface
*
463 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
467 name
= alloca(strlen(s
->type
) + 16);
468 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
470 blob_buf_init(&b
, 0);
471 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
472 return wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
476 config_parse_wireless_vlan(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
480 name
= alloca(strlen(s
->type
) + 16);
481 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
483 blob_buf_init(&b
, 0);
484 uci_to_blob(&b
, s
, wdev
->drv
->vlan
.config
);
485 wireless_vlan_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
489 config_parse_wireless_station(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
493 name
= alloca(strlen(s
->type
) + 16);
494 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
496 blob_buf_init(&b
, 0);
497 uci_to_blob(&b
, s
, wdev
->drv
->station
.config
);
498 wireless_station_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
502 config_init_wireless(void)
504 struct wireless_device
*wdev
;
505 struct uci_element
*e
;
506 const char *dev_name
;
509 DPRINTF("No wireless configuration found\n");
513 vlist_update(&wireless_devices
);
515 uci_foreach_element(&uci_wireless
->sections
, e
) {
516 struct uci_section
*s
= uci_to_section(e
);
517 if (strcmp(s
->type
, "wifi-device") != 0)
520 config_parse_wireless_device(s
);
523 vlist_flush(&wireless_devices
);
525 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
527 vlist_update(&wdev
->interfaces
);
529 vlist_update(&wdev
->vlans
);
531 vlist_update(&wdev
->stations
);
534 uci_foreach_element(&uci_wireless
->sections
, e
) {
535 struct uci_section
*s
= uci_to_section(e
);
536 struct wireless_interface
*vif
;
537 struct uci_element
*f
;
539 if (strcmp(s
->type
, "wifi-iface") != 0)
542 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
546 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
548 DPRINTF("device %s not found!\n", dev_name
);
552 vif
= config_parse_wireless_interface(wdev
, s
);
554 if (!vif
|| s
->anonymous
)
556 uci_foreach_element(&uci_wireless
->sections
, f
) {
557 struct uci_section
*s
= uci_to_section(f
);
558 const char *vif_name
;
560 if (strcmp(s
->type
, "wifi-vlan") != 0)
563 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
564 if (vif_name
&& strcmp(e
->name
, vif_name
))
566 config_parse_wireless_vlan(wdev
, vif
->name
, s
);
569 uci_foreach_element(&uci_wireless
->sections
, f
) {
570 struct uci_section
*s
= uci_to_section(f
);
571 const char *vif_name
;
573 if (strcmp(s
->type
, "wifi-station") != 0)
576 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
577 if (vif_name
&& strcmp(e
->name
, vif_name
))
579 config_parse_wireless_station(wdev
, vif
->name
, s
);
583 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
584 vlist_flush(&wdev
->interfaces
);
585 vlist_flush(&wdev
->vlans
);
586 vlist_flush(&wdev
->stations
);
591 config_init_all(void)
596 uci_network
= config_init_package("network");
598 uci_get_errorstr(uci_ctx
, &err
, NULL
);
599 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
604 uci_wireless
= config_init_package("wireless");
605 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
606 uci_get_errorstr(uci_ctx
, &err
, NULL
);
607 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
612 vlist_update(&interfaces
);
616 device_reset_config();
617 config_init_devices();
618 config_init_interfaces();
622 config_init_globals();
623 config_init_wireless();
629 device_init_pending();
630 vlist_flush(&interfaces
);
631 device_free_unused(NULL
);
632 interface_refresh_assignments(false);
633 interface_start_pending();
634 wireless_start_pending();