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 <libubox/blobmsg_json.h>
24 #include "interface.h"
25 #include "interface-ip.h"
31 bool config_init
= false;
33 static struct uci_context
*uci_ctx
;
34 static struct uci_package
*uci_network
;
35 static struct uci_package
*uci_wireless
;
36 static struct blob_attr
*board_netdevs
;
37 static struct blob_buf b
;
40 config_section_idx(struct uci_section
*s
)
42 struct uci_element
*e
;
45 uci_foreach_element(&uci_wireless
->sections
, e
) {
46 struct uci_section
*cur
= uci_to_section(e
);
51 if (!strcmp(cur
->type
, s
->type
))
59 config_bridge_has_vlans(const char *br_name
)
61 struct uci_element
*e
;
63 uci_foreach_element(&uci_network
->sections
, e
) {
64 struct uci_section
*s
= uci_to_section(e
);
67 if (strcmp(s
->type
, "bridge-vlan") != 0)
70 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
74 if (!strcmp(name
, br_name
))
82 config_fixup_bridge_var(struct uci_section
*s
, const char *name
, const char *val
)
84 struct uci_ptr ptr
= {
91 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
95 uci_set(uci_ctx
, &ptr
);
99 config_fixup_bridge_vlan_filtering(struct uci_section
*s
, const char *name
)
101 bool has_vlans
= config_bridge_has_vlans(name
);
103 config_fixup_bridge_var(s
, "__has_vlans", has_vlans
? "1" : "0");
108 config_fixup_bridge_var(s
, "vlan_filtering", "1");
112 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
116 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
117 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
118 blobmsg_add_string(&b
, "name", name
);
120 config_fixup_bridge_vlan_filtering(s
, name
);
121 uci_to_blob(&b
, s
, devtype
->config_params
);
122 if (!device_create(name
, devtype
, b
.head
)) {
123 D(INTERFACE
, "Failed to create '%s' device for interface '%s'\n",
124 devtype
->name
, s
->e
.name
);
127 blob_buf_init(&b
, 0);
128 blobmsg_add_string(&b
, "ifname", name
);
133 config_parse_interface(struct uci_section
*s
, bool alias
)
135 struct interface
*iface
;
136 const char *type
= NULL
, *disabled
;
137 struct blob_attr
*config
;
139 struct device_type
*devtype
= NULL
;
141 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
142 if (disabled
&& !strcmp(disabled
, "1"))
145 blob_buf_init(&b
, 0);
148 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
151 devtype
= device_type_get(type
);
153 if (devtype
&& devtype
->bridge_capability
) {
154 if (config_parse_bridge_interface(s
, devtype
))
160 uci_to_blob(&b
, s
, &interface_attr_list
);
162 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
166 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
167 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
169 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
170 iface
->device_config
= true;
172 config
= blob_memdup(b
.head
);
177 if (!interface_add_alias(iface
, config
))
178 goto error_free_config
;
180 if (!interface_add(iface
, config
))
181 goto error_free_config
;
192 config_parse_route(struct uci_section
*s
, bool v6
)
196 blob_buf_init(&b
, 0);
197 route
= blobmsg_open_array(&b
, "route");
198 uci_to_blob(&b
, s
, &route_attr_list
);
199 blobmsg_close_array(&b
, route
);
200 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
204 config_parse_neighbor(struct uci_section
*s
, bool v6
)
208 neighbor
= blobmsg_open_array(&b
, "neighbor");
209 uci_to_blob(&b
,s
, &neighbor_attr_list
);
210 blobmsg_close_array(&b
, neighbor
);
211 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
215 config_parse_rule(struct uci_section
*s
, bool v6
)
219 blob_buf_init(&b
, 0);
220 rule
= blobmsg_open_array(&b
, "rule");
221 uci_to_blob(&b
, s
, &rule_attr_list
);
222 blobmsg_close_array(&b
, rule
);
223 iprule_add(blob_data(b
.head
), v6
);
227 config_init_devices(bool bridge
)
229 struct uci_element
*e
;
231 uci_foreach_element(&uci_network
->sections
, e
) {
232 const struct uci_blob_param_list
*params
= NULL
;
233 struct uci_section
*s
= uci_to_section(e
);
234 struct device_type
*devtype
= NULL
;
236 const char *type
, *name
;
238 if (strcmp(s
->type
, "device") != 0)
241 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
245 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
247 devtype
= device_type_get(type
);
249 if (bridge
!= (devtype
&& devtype
->bridge_capability
))
253 params
= devtype
->config_params
;
255 params
= simple_device_type
.config_params
;
257 if (devtype
&& devtype
->bridge_capability
)
258 config_fixup_bridge_vlan_filtering(s
, name
);
260 blob_buf_init(&b
, 0);
261 uci_to_blob(&b
, s
, params
);
263 dev
= device_create(name
, devtype
, b
.head
);
267 dev
= device_get(name
, 1);
271 dev
->current_config
= true;
272 device_apply_config(dev
, dev
->type
, b
.head
);
274 dev
->default_config
= false;
279 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
288 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
289 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
290 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
291 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
292 [BRVLAN_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
294 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
295 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
296 [BRVLAN_ATTR_ALIAS
] = { .type
= BLOBMSG_TYPE_STRING
},
298 static const struct uci_blob_param_list vlan_attr_list
= {
299 .n_params
= __BRVLAN_ATTR_MAX
,
300 .params
= vlan_attrs
,
301 .info
= vlan_attr_info
,
303 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
304 struct blob_attr
*cur
;
305 struct bridge_vlan_port
*port
;
306 struct bridge_vlan
*vlan
;
314 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
318 blob_buf_init(&b
, 0);
319 uci_to_blob(&b
, s
, &vlan_attr_list
);
320 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
322 if (!tb
[BRVLAN_ATTR_VID
])
325 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
326 if (!vid
|| vid
> 4095)
329 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
330 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
334 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
340 if (tb
[BRVLAN_ATTR_LOCAL
])
341 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
343 vlan
->n_ports
= n_ports
;
344 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
345 INIT_LIST_HEAD(&vlan
->hotplug_ports
);
346 name_buf
= (char *)&port
[n_ports
];
348 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
351 port
->ifname
= name_buf
;
352 port
->flags
= BRVLAN_F_UNTAGGED
;
353 strcpy(name_buf
, blobmsg_get_string(cur
));
355 sep
= strchr(name_buf
, ':');
357 for (*sep
= 0, sep
++; *sep
; sep
++)
360 port
->flags
|= BRVLAN_F_PVID
;
363 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
368 name_buf
+= strlen(name_buf
) + 1;
372 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_ALIAS
], rem
)
373 kvlist_set(&dev
->vlan_aliases
, blobmsg_get_string(cur
), &vid
);
375 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
380 config_init_vlans(void)
382 struct uci_element
*e
;
385 device_vlan_update(false);
386 uci_foreach_element(&uci_network
->sections
, e
) {
387 struct uci_section
*s
= uci_to_section(e
);
390 if (strcmp(s
->type
, "bridge-vlan") != 0)
393 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
397 dev
= device_get(name
, 0);
398 if (!dev
|| !dev
->vlans
.update
)
401 config_parse_vlan(dev
, s
);
403 device_vlan_update(true);
406 static struct uci_package
*
407 config_init_package(const char *config
)
409 struct uci_context
*ctx
= uci_ctx
;
410 struct uci_package
*p
= NULL
;
413 ctx
= uci_alloc_context();
416 ctx
->flags
&= ~UCI_FLAG_STRICT
;
418 uci_set_confdir(ctx
, config_path
);
421 uci_set_savedir(ctx
, "./tmp");
424 p
= uci_lookup_package(ctx
, config
);
429 if (uci_load(ctx
, config
, &p
))
436 config_init_interfaces(void)
438 struct uci_element
*e
;
440 uci_foreach_element(&uci_network
->sections
, e
) {
441 struct uci_section
*s
= uci_to_section(e
);
443 if (!strcmp(s
->type
, "interface"))
444 config_parse_interface(s
, false);
447 uci_foreach_element(&uci_network
->sections
, e
) {
448 struct uci_section
*s
= uci_to_section(e
);
450 if (!strcmp(s
->type
, "alias"))
451 config_parse_interface(s
, true);
458 struct interface
*iface
;
459 struct uci_element
*e
;
461 vlist_for_each_element(&interfaces
, iface
, node
)
462 interface_ip_update_start(&iface
->config_ip
);
464 uci_foreach_element(&uci_network
->sections
, e
) {
465 struct uci_section
*s
= uci_to_section(e
);
467 if (!strcmp(s
->type
, "route"))
468 config_parse_route(s
, false);
469 else if (!strcmp(s
->type
, "route6"))
470 config_parse_route(s
, true);
471 if (!strcmp(s
->type
, "neighbor"))
472 config_parse_neighbor(s
, false);
473 else if (!strcmp(s
->type
, "neighbor6"))
474 config_parse_neighbor(s
, true);
477 vlist_for_each_element(&interfaces
, iface
, node
)
478 interface_ip_update_complete(&iface
->config_ip
);
482 config_init_rules(void)
484 struct uci_element
*e
;
486 iprule_update_start();
488 uci_foreach_element(&uci_network
->sections
, e
) {
489 struct uci_section
*s
= uci_to_section(e
);
491 if (!strcmp(s
->type
, "rule"))
492 config_parse_rule(s
, false);
493 else if (!strcmp(s
->type
, "rule6"))
494 config_parse_rule(s
, true);
497 iprule_update_complete();
501 config_init_globals(void)
503 struct uci_section
*globals
= uci_lookup_section(
504 uci_ctx
, uci_network
, "globals");
508 const char *ula_prefix
= uci_lookup_option_string(
509 uci_ctx
, globals
, "ula_prefix");
510 interface_ip_set_ula_prefix(ula_prefix
);
514 config_parse_wireless_device(struct uci_section
*s
)
516 struct wireless_driver
*drv
;
517 const char *driver_name
;
519 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
523 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
527 blob_buf_init(&b
, 0);
528 uci_to_blob(&b
, s
, drv
->device
.config
);
529 wireless_device_create(drv
, s
->e
.name
, b
.head
);
532 static struct wireless_interface
*
533 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
537 name
= alloca(strlen(s
->type
) + 16);
538 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
540 blob_buf_init(&b
, 0);
541 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
542 return wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
546 config_parse_wireless_vlan(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
550 name
= alloca(strlen(s
->type
) + 16);
551 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
553 blob_buf_init(&b
, 0);
554 uci_to_blob(&b
, s
, wdev
->drv
->vlan
.config
);
555 wireless_vlan_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
559 config_parse_wireless_station(struct wireless_device
*wdev
, char *vif
, struct uci_section
*s
)
563 name
= alloca(strlen(s
->type
) + 16);
564 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
566 blob_buf_init(&b
, 0);
567 uci_to_blob(&b
, s
, wdev
->drv
->station
.config
);
568 wireless_station_create(wdev
, vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
572 config_init_wireless(void)
574 struct wireless_device
*wdev
;
575 struct uci_element
*e
;
576 const char *dev_name
;
579 DPRINTF("No wireless configuration found\n");
583 vlist_update(&wireless_devices
);
585 uci_foreach_element(&uci_wireless
->sections
, e
) {
586 struct uci_section
*s
= uci_to_section(e
);
587 if (strcmp(s
->type
, "wifi-device") != 0)
590 config_parse_wireless_device(s
);
593 vlist_flush(&wireless_devices
);
595 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
597 vlist_update(&wdev
->interfaces
);
599 vlist_update(&wdev
->vlans
);
601 vlist_update(&wdev
->stations
);
604 uci_foreach_element(&uci_wireless
->sections
, e
) {
605 struct uci_section
*s
= uci_to_section(e
);
606 struct wireless_interface
*vif
;
607 struct uci_element
*f
;
609 if (strcmp(s
->type
, "wifi-iface") != 0)
612 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
616 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
618 DPRINTF("device %s not found!\n", dev_name
);
622 vif
= config_parse_wireless_interface(wdev
, s
);
624 if (!vif
|| s
->anonymous
)
626 uci_foreach_element(&uci_wireless
->sections
, f
) {
627 struct uci_section
*s
= uci_to_section(f
);
628 const char *vif_name
;
630 if (strcmp(s
->type
, "wifi-vlan") != 0)
633 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
634 if (vif_name
&& strcmp(e
->name
, vif_name
))
636 config_parse_wireless_vlan(wdev
, vif
->name
, s
);
639 uci_foreach_element(&uci_wireless
->sections
, f
) {
640 struct uci_section
*s
= uci_to_section(f
);
641 const char *vif_name
;
643 if (strcmp(s
->type
, "wifi-station") != 0)
646 vif_name
= uci_lookup_option_string(uci_ctx
, s
, "iface");
647 if (vif_name
&& strcmp(e
->name
, vif_name
))
649 config_parse_wireless_station(wdev
, vif
->name
, s
);
653 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
654 vlist_flush(&wdev
->interfaces
);
655 vlist_flush(&wdev
->vlans
);
656 vlist_flush(&wdev
->stations
);
661 static struct blob_attr
*
662 config_find_blobmsg_attr(struct blob_attr
*attr
, const char *name
, int type
)
664 struct blobmsg_policy policy
= { .name
= name
, .type
= type
};
665 struct blob_attr
*cur
;
667 blobmsg_parse(&policy
, 1, &cur
, blobmsg_data(attr
), blobmsg_len(attr
));
672 struct ether_addr
*config_get_default_macaddr(const char *ifname
)
674 struct blob_attr
*cur
;
679 cur
= config_find_blobmsg_attr(board_netdevs
, ifname
, BLOBMSG_TYPE_TABLE
);
683 cur
= config_find_blobmsg_attr(cur
, "macaddr", BLOBMSG_TYPE_STRING
);
687 return ether_aton(blobmsg_get_string(cur
));
691 config_init_board(void)
693 struct blob_attr
*cur
;
695 blob_buf_init(&b
, 0);
697 if (!blobmsg_add_json_from_file(&b
, DEFAULT_BOARD_JSON
))
701 board_netdevs
= NULL
;
703 cur
= config_find_blobmsg_attr(b
.head
, "network-device",
708 board_netdevs
= blob_memdup(cur
);
712 config_init_all(void)
717 uci_network
= config_init_package("network");
719 uci_get_errorstr(uci_ctx
, &err
, NULL
);
720 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
725 uci_wireless
= config_init_package("wireless");
726 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
727 uci_get_errorstr(uci_ctx
, &err
, NULL
);
728 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
735 vlist_update(&interfaces
);
739 device_reset_config();
740 config_init_devices(true);
742 config_init_devices(false);
743 config_init_interfaces();
746 config_init_globals();
747 config_init_wireless();
753 device_init_pending();
754 vlist_flush(&interfaces
);
755 device_free_unused(NULL
);
756 interface_refresh_assignments(false);
757 interface_start_pending();
758 wireless_start_pending();