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_bridge_has_vlans(const char *br_name
)
58 struct uci_element
*e
;
60 uci_foreach_element(&uci_network
->sections
, e
) {
61 struct uci_section
*s
= uci_to_section(e
);
64 if (strcmp(s
->type
, "bridge-vlan") != 0)
67 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
71 if (!strcmp(name
, br_name
))
79 config_fixup_bridge_vlan_filtering(struct uci_section
*s
, const char *name
)
81 struct uci_ptr ptr
= {
84 .option
= "vlan_filtering",
88 if (!config_bridge_has_vlans(name
))
91 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
95 uci_set(uci_ctx
, &ptr
);
99 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
103 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
104 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
105 blobmsg_add_string(&b
, "name", name
);
107 config_fixup_bridge_vlan_filtering(s
, name
);
108 uci_to_blob(&b
, s
, devtype
->config_params
);
109 if (!device_create(name
, devtype
, b
.head
)) {
110 D(INTERFACE
, "Failed to create '%s' device for interface '%s'\n",
111 devtype
->name
, s
->e
.name
);
114 blob_buf_init(&b
, 0);
115 blobmsg_add_string(&b
, "ifname", name
);
120 config_parse_interface(struct uci_section
*s
, bool alias
)
122 struct interface
*iface
;
123 const char *type
= NULL
, *disabled
;
124 struct blob_attr
*config
;
126 struct device_type
*devtype
= NULL
;
128 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
129 if (disabled
&& !strcmp(disabled
, "1"))
132 blob_buf_init(&b
, 0);
135 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
138 devtype
= device_type_get(type
);
140 if (devtype
&& devtype
->bridge_capability
) {
141 if (config_parse_bridge_interface(s
, devtype
))
147 uci_to_blob(&b
, s
, &interface_attr_list
);
149 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
153 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
154 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
156 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
157 iface
->device_config
= true;
159 config
= blob_memdup(b
.head
);
164 if (!interface_add_alias(iface
, config
))
165 goto error_free_config
;
167 if (!interface_add(iface
, config
))
168 goto error_free_config
;
179 config_parse_route(struct uci_section
*s
, bool v6
)
183 blob_buf_init(&b
, 0);
184 route
= blobmsg_open_array(&b
, "route");
185 uci_to_blob(&b
, s
, &route_attr_list
);
186 blobmsg_close_array(&b
, route
);
187 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
191 config_parse_neighbor(struct uci_section
*s
, bool v6
)
195 neighbor
= blobmsg_open_array(&b
, "neighbor");
196 uci_to_blob(&b
,s
, &neighbor_attr_list
);
197 blobmsg_close_array(&b
, neighbor
);
198 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
202 config_parse_rule(struct uci_section
*s
, bool v6
)
206 blob_buf_init(&b
, 0);
207 rule
= blobmsg_open_array(&b
, "rule");
208 uci_to_blob(&b
, s
, &rule_attr_list
);
209 blobmsg_close_array(&b
, rule
);
210 iprule_add(blob_data(b
.head
), v6
);
214 config_init_devices(void)
216 struct uci_element
*e
;
218 uci_foreach_element(&uci_network
->sections
, e
) {
219 const struct uci_blob_param_list
*params
= NULL
;
220 struct uci_section
*s
= uci_to_section(e
);
221 struct device_type
*devtype
= NULL
;
223 const char *type
, *name
;
225 if (strcmp(s
->type
, "device") != 0)
228 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
232 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
234 devtype
= device_type_get(type
);
237 params
= devtype
->config_params
;
239 params
= simple_device_type
.config_params
;
241 if (devtype
&& devtype
->bridge_capability
)
242 config_fixup_bridge_vlan_filtering(s
, name
);
244 blob_buf_init(&b
, 0);
245 uci_to_blob(&b
, s
, params
);
247 dev
= device_create(name
, devtype
, b
.head
);
251 dev
= device_get(name
, 1);
255 dev
->current_config
= true;
256 device_apply_config(dev
, dev
->type
, b
.head
);
258 dev
->default_config
= false;
263 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
271 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
272 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
273 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
274 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
276 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
277 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
279 static const struct uci_blob_param_list vlan_attr_list
= {
280 .n_params
= __BRVLAN_ATTR_MAX
,
281 .params
= vlan_attrs
,
282 .info
= vlan_attr_info
,
284 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
285 struct blob_attr
*cur
;
286 struct bridge_vlan_port
*port
;
287 struct bridge_vlan
*vlan
;
295 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
299 blob_buf_init(&b
, 0);
300 uci_to_blob(&b
, s
, &vlan_attr_list
);
301 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
303 if (!tb
[BRVLAN_ATTR_VID
])
306 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
307 if (!vid
|| vid
> 4095)
310 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
311 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
315 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
321 if (tb
[BRVLAN_ATTR_LOCAL
])
322 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
324 vlan
->n_ports
= n_ports
;
325 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
326 INIT_LIST_HEAD(&vlan
->hotplug_ports
);
327 name_buf
= (char *)&port
[n_ports
];
329 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
332 port
->ifname
= name_buf
;
333 port
->flags
= BRVLAN_F_UNTAGGED
;
334 strcpy(name_buf
, blobmsg_get_string(cur
));
336 sep
= strchr(name_buf
, ':');
338 for (*sep
= 0, sep
++; *sep
; sep
++)
341 port
->flags
|= BRVLAN_F_PVID
;
344 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
349 name_buf
+= strlen(name_buf
) + 1;
353 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
358 config_init_vlans(void)
360 struct uci_element
*e
;
363 device_vlan_update(false);
364 uci_foreach_element(&uci_network
->sections
, e
) {
365 struct uci_section
*s
= uci_to_section(e
);
368 if (strcmp(s
->type
, "bridge-vlan") != 0)
371 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
375 dev
= device_get(name
, 0);
376 if (!dev
|| !dev
->vlans
.update
)
379 config_parse_vlan(dev
, s
);
381 device_vlan_update(true);
384 static struct uci_package
*
385 config_init_package(const char *config
)
387 struct uci_context
*ctx
= uci_ctx
;
388 struct uci_package
*p
= NULL
;
391 ctx
= uci_alloc_context();
394 ctx
->flags
&= ~UCI_FLAG_STRICT
;
396 uci_set_confdir(ctx
, config_path
);
399 uci_set_savedir(ctx
, "./tmp");
402 p
= uci_lookup_package(ctx
, config
);
407 if (uci_load(ctx
, config
, &p
))
414 config_init_interfaces(void)
416 struct uci_element
*e
;
418 uci_foreach_element(&uci_network
->sections
, e
) {
419 struct uci_section
*s
= uci_to_section(e
);
421 if (!strcmp(s
->type
, "interface"))
422 config_parse_interface(s
, false);
425 uci_foreach_element(&uci_network
->sections
, e
) {
426 struct uci_section
*s
= uci_to_section(e
);
428 if (!strcmp(s
->type
, "alias"))
429 config_parse_interface(s
, true);
436 struct interface
*iface
;
437 struct uci_element
*e
;
439 vlist_for_each_element(&interfaces
, iface
, node
)
440 interface_ip_update_start(&iface
->config_ip
);
442 uci_foreach_element(&uci_network
->sections
, e
) {
443 struct uci_section
*s
= uci_to_section(e
);
445 if (!strcmp(s
->type
, "route"))
446 config_parse_route(s
, false);
447 else if (!strcmp(s
->type
, "route6"))
448 config_parse_route(s
, true);
449 if (!strcmp(s
->type
, "neighbor"))
450 config_parse_neighbor(s
, false);
451 else if (!strcmp(s
->type
, "neighbor6"))
452 config_parse_neighbor(s
, true);
455 vlist_for_each_element(&interfaces
, iface
, node
)
456 interface_ip_update_complete(&iface
->config_ip
);
460 config_init_rules(void)
462 struct uci_element
*e
;
464 iprule_update_start();
466 uci_foreach_element(&uci_network
->sections
, e
) {
467 struct uci_section
*s
= uci_to_section(e
);
469 if (!strcmp(s
->type
, "rule"))
470 config_parse_rule(s
, false);
471 else if (!strcmp(s
->type
, "rule6"))
472 config_parse_rule(s
, true);
475 iprule_update_complete();
479 config_init_globals(void)
481 struct uci_section
*globals
= uci_lookup_section(
482 uci_ctx
, uci_network
, "globals");
486 const char *ula_prefix
= uci_lookup_option_string(
487 uci_ctx
, globals
, "ula_prefix");
488 interface_ip_set_ula_prefix(ula_prefix
);
492 config_parse_wireless_device(struct uci_section
*s
)
494 struct wireless_driver
*drv
;
495 const char *driver_name
;
497 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
501 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
505 blob_buf_init(&b
, 0);
506 uci_to_blob(&b
, s
, drv
->device
.config
);
507 wireless_device_create(drv
, s
->e
.name
, b
.head
);
510 static struct wireless_interface
*
511 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
515 name
= alloca(strlen(s
->type
) + 16);
516 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
518 blob_buf_init(&b
, 0);
519 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
520 return wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
524 config_parse_wireless_vlan(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
528 name
= alloca(strlen(s
->type
) + 16);
529 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
531 blob_buf_init(&b
, 0);
532 uci_to_blob(&b
, s
, wdev
->drv
->vlan
.config
);
533 wireless_vlan_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
537 config_parse_wireless_station(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
541 name
= alloca(strlen(s
->type
) + 16);
542 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
544 blob_buf_init(&b
, 0);
545 uci_to_blob(&b
, s
, wdev
->drv
->station
.config
);
546 wireless_station_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
550 config_init_wireless(void)
552 struct wireless_device
*wdev
;
553 struct uci_element
*e
;
554 const char *dev_name
;
557 DPRINTF("No wireless configuration found\n");
561 vlist_update(&wireless_devices
);
563 uci_foreach_element(&uci_wireless
->sections
, e
) {
564 struct uci_section
*s
= uci_to_section(e
);
565 if (strcmp(s
->type
, "wifi-device") != 0)
568 config_parse_wireless_device(s
);
571 vlist_flush(&wireless_devices
);
573 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
575 vlist_update(&wdev
->interfaces
);
577 vlist_update(&wdev
->vlans
);
579 vlist_update(&wdev
->stations
);
582 uci_foreach_element(&uci_wireless
->sections
, e
) {
583 struct uci_section
*s
= uci_to_section(e
);
584 struct wireless_interface
*vif
;
585 struct uci_element
*f
;
587 if (strcmp(s
->type
, "wifi-iface") != 0)
590 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
594 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
596 DPRINTF("device %s not found!\n", dev_name
);
600 vif
= config_parse_wireless_interface(wdev
, s
);
602 if (!vif
|| s
->anonymous
)
604 uci_foreach_element(&uci_wireless
->sections
, f
) {
605 struct uci_section
*s
= uci_to_section(f
);
606 const char *vif_name
;
608 if (strcmp(s
->type
, "wifi-vlan") != 0)
611 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
612 if (vif_name
&& strcmp(e
->name
, vif_name
))
614 config_parse_wireless_vlan(wdev
, vif
->name
, s
);
617 uci_foreach_element(&uci_wireless
->sections
, f
) {
618 struct uci_section
*s
= uci_to_section(f
);
619 const char *vif_name
;
621 if (strcmp(s
->type
, "wifi-station") != 0)
624 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
625 if (vif_name
&& strcmp(e
->name
, vif_name
))
627 config_parse_wireless_station(wdev
, vif
->name
, s
);
631 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
632 vlist_flush(&wdev
->interfaces
);
633 vlist_flush(&wdev
->vlans
);
634 vlist_flush(&wdev
->stations
);
639 config_init_all(void)
644 uci_network
= config_init_package("network");
646 uci_get_errorstr(uci_ctx
, &err
, NULL
);
647 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
652 uci_wireless
= config_init_package("wireless");
653 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
654 uci_get_errorstr(uci_ctx
, &err
, NULL
);
655 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
660 vlist_update(&interfaces
);
664 device_reset_config();
665 config_init_devices();
666 config_init_interfaces();
670 config_init_globals();
671 config_init_wireless();
677 device_init_pending();
678 vlist_flush(&interfaces
);
679 device_free_unused(NULL
);
680 interface_refresh_assignments(false);
681 interface_start_pending();
682 wireless_start_pending();