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"
32 bool config_init
= false;
34 static struct uci_context
*uci_ctx
;
35 static struct uci_package
*uci_network
;
36 static struct uci_package
*uci_wireless
;
37 static struct blob_attr
*board_netdevs
;
38 static struct blob_buf b
;
41 config_section_idx(struct uci_section
*s
)
43 struct uci_element
*e
;
46 uci_foreach_element(&uci_wireless
->sections
, e
) {
47 struct uci_section
*cur
= uci_to_section(e
);
52 if (!strcmp(cur
->type
, s
->type
))
60 config_bridge_has_vlans(const char *br_name
)
62 struct uci_element
*e
;
64 uci_foreach_element(&uci_network
->sections
, e
) {
65 struct uci_section
*s
= uci_to_section(e
);
68 if (strcmp(s
->type
, "bridge-vlan") != 0)
71 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
75 if (!strcmp(name
, br_name
))
83 config_fixup_bridge_var(struct uci_section
*s
, const char *name
, const char *val
)
85 struct uci_ptr ptr
= {
92 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
96 uci_set(uci_ctx
, &ptr
);
100 * config_fixup_bridge_ports - translate deprecated configs
102 * Old configs used "ifname" option for specifying bridge ports. For backward
103 * compatibility translate it into the new "ports" option.
105 static void config_fixup_bridge_ports(struct uci_section
*s
)
107 struct uci_ptr ptr
= {
113 if (uci_lookup_option(uci_ctx
, s
, "ports"))
116 uci_lookup_ptr(uci_ctx
, &ptr
, NULL
, false);
121 uci_rename(uci_ctx
, &ptr
);
125 config_fixup_bridge_vlan_filtering(struct uci_section
*s
, const char *name
)
127 bool has_vlans
= config_bridge_has_vlans(name
);
129 config_fixup_bridge_var(s
, "__has_vlans", has_vlans
? "1" : "0");
134 config_fixup_bridge_var(s
, "vlan_filtering", "1");
138 config_parse_bridge_interface(struct uci_section
*s
, struct device_type
*devtype
)
142 name
= alloca(strlen(s
->e
.name
) + strlen(devtype
->name_prefix
) + 2);
143 sprintf(name
, "%s-%s", devtype
->name_prefix
, s
->e
.name
);
144 blobmsg_add_string(&b
, "name", name
);
146 config_fixup_bridge_ports(s
);
147 config_fixup_bridge_vlan_filtering(s
, name
);
148 uci_to_blob(&b
, s
, devtype
->config_params
);
149 if (!device_create(name
, devtype
, b
.head
)) {
150 D(INTERFACE
, "Failed to create '%s' device for interface '%s'",
151 devtype
->name
, s
->e
.name
);
154 blob_buf_init(&b
, 0);
155 blobmsg_add_string(&b
, "ifname", name
);
160 config_parse_interface(struct uci_section
*s
, bool alias
)
162 struct interface
*iface
;
163 const char *type
= NULL
, *disabled
;
164 struct blob_attr
*config
;
166 struct device_type
*devtype
= NULL
;
168 disabled
= uci_lookup_option_string(uci_ctx
, s
, "disabled");
169 if (disabled
&& !strcmp(disabled
, "1"))
172 blob_buf_init(&b
, 0);
175 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
178 devtype
= device_type_get(type
);
180 if (devtype
&& devtype
->bridge_capability
) {
181 if (config_parse_bridge_interface(s
, devtype
))
187 uci_to_blob(&b
, s
, &interface_attr_list
);
189 iface
= interface_alloc(s
->e
.name
, b
.head
, false);
193 if (iface
->proto_handler
&& iface
->proto_handler
->config_params
)
194 uci_to_blob(&b
, s
, iface
->proto_handler
->config_params
);
196 if (!bridge
&& uci_to_blob(&b
, s
, simple_device_type
.config_params
))
197 iface
->device_config
= true;
199 config
= blob_memdup(b
.head
);
204 if (!interface_add_alias(iface
, config
))
205 goto error_free_config
;
207 if (!interface_add(iface
, config
))
208 goto error_free_config
;
219 config_parse_route(struct uci_section
*s
, bool v6
)
223 blob_buf_init(&b
, 0);
224 route
= blobmsg_open_array(&b
, "route");
225 uci_to_blob(&b
, s
, &route_attr_list
);
226 blobmsg_close_array(&b
, route
);
227 interface_ip_add_route(NULL
, blob_data(b
.head
), v6
);
231 config_parse_neighbor(struct uci_section
*s
, bool v6
)
235 neighbor
= blobmsg_open_array(&b
, "neighbor");
236 uci_to_blob(&b
,s
, &neighbor_attr_list
);
237 blobmsg_close_array(&b
, neighbor
);
238 interface_ip_add_neighbor(NULL
, blob_data(b
.head
), v6
);
242 config_parse_rule(struct uci_section
*s
, bool v6
)
246 blob_buf_init(&b
, 0);
247 rule
= blobmsg_open_array(&b
, "rule");
248 uci_to_blob(&b
, s
, &rule_attr_list
);
249 blobmsg_close_array(&b
, rule
);
250 iprule_add(blob_data(b
.head
), v6
);
254 config_init_devices(bool bridge
)
256 struct uci_element
*e
;
258 uci_foreach_element(&uci_network
->sections
, e
) {
259 const struct uci_blob_param_list
*params
= NULL
;
260 struct uci_section
*s
= uci_to_section(e
);
261 struct device_type
*devtype
= NULL
;
263 const char *type
, *name
;
265 if (strcmp(s
->type
, "device") != 0)
268 name
= uci_lookup_option_string(uci_ctx
, s
, "name");
272 type
= uci_lookup_option_string(uci_ctx
, s
, "type");
274 devtype
= device_type_get(type
);
276 if (bridge
!= (devtype
&& devtype
->bridge_capability
))
280 params
= devtype
->config_params
;
282 params
= simple_device_type
.config_params
;
284 if (devtype
&& devtype
->bridge_capability
) {
285 config_fixup_bridge_ports(s
);
286 config_fixup_bridge_vlan_filtering(s
, name
);
289 blob_buf_init(&b
, 0);
290 uci_to_blob(&b
, s
, params
);
292 dev
= device_create(name
, devtype
, b
.head
);
296 dev
= device_get(name
, 1);
300 dev
->current_config
= true;
301 device_apply_config(dev
, dev
->type
, b
.head
);
303 dev
->default_config
= false;
308 config_parse_vlan(struct device
*dev
, struct uci_section
*s
)
317 static const struct blobmsg_policy vlan_attrs
[__BRVLAN_ATTR_MAX
] = {
318 [BRVLAN_ATTR_VID
] = { "vlan", BLOBMSG_TYPE_INT32
},
319 [BRVLAN_ATTR_LOCAL
] = { "local", BLOBMSG_TYPE_BOOL
},
320 [BRVLAN_ATTR_PORTS
] = { "ports", BLOBMSG_TYPE_ARRAY
},
321 [BRVLAN_ATTR_ALIAS
] = { "alias", BLOBMSG_TYPE_ARRAY
},
323 static const struct uci_blob_param_info vlan_attr_info
[__BRVLAN_ATTR_MAX
] = {
324 [BRVLAN_ATTR_PORTS
] = { .type
= BLOBMSG_TYPE_STRING
},
325 [BRVLAN_ATTR_ALIAS
] = { .type
= BLOBMSG_TYPE_STRING
},
327 static const struct uci_blob_param_list vlan_attr_list
= {
328 .n_params
= __BRVLAN_ATTR_MAX
,
329 .params
= vlan_attrs
,
330 .info
= vlan_attr_info
,
332 struct blob_attr
*tb
[__BRVLAN_ATTR_MAX
];
333 struct blob_attr
*cur
;
334 struct bridge_vlan_port
*port
;
335 struct bridge_vlan
*vlan
;
343 val
= uci_lookup_option_string(uci_ctx
, s
, "vlan");
347 blob_buf_init(&b
, 0);
348 uci_to_blob(&b
, s
, &vlan_attr_list
);
349 blobmsg_parse(vlan_attrs
, __BRVLAN_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
351 if (!tb
[BRVLAN_ATTR_VID
])
354 vid
= blobmsg_get_u32(tb
[BRVLAN_ATTR_VID
]);
355 if (!vid
|| vid
> 4095)
358 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
359 name_len
+= strlen(blobmsg_get_string(cur
)) + 1;
363 vlan
= calloc(1, sizeof(*vlan
) + n_ports
* sizeof(*port
) + name_len
);
369 if (tb
[BRVLAN_ATTR_LOCAL
])
370 vlan
->local
= blobmsg_get_bool(tb
[BRVLAN_ATTR_LOCAL
]);
372 vlan
->n_ports
= n_ports
;
373 vlan
->ports
= port
= (struct bridge_vlan_port
*)&vlan
[1];
374 INIT_LIST_HEAD(&vlan
->hotplug_ports
);
375 name_buf
= (char *)&port
[n_ports
];
377 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_PORTS
], rem
) {
380 port
->ifname
= name_buf
;
381 port
->flags
= BRVLAN_F_UNTAGGED
;
382 strcpy(name_buf
, blobmsg_get_string(cur
));
384 sep
= strchr(name_buf
, ':');
386 for (*sep
= 0, sep
++; *sep
; sep
++)
389 port
->flags
|= BRVLAN_F_PVID
;
392 port
->flags
&= ~BRVLAN_F_UNTAGGED
;
397 name_buf
+= strlen(name_buf
) + 1;
401 blobmsg_for_each_attr(cur
, tb
[BRVLAN_ATTR_ALIAS
], rem
)
402 kvlist_set(&dev
->vlan_aliases
, blobmsg_get_string(cur
), &vid
);
404 vlist_add(&dev
->vlans
, &vlan
->node
, &vlan
->vid
);
409 config_init_vlans(void)
411 struct uci_element
*e
;
414 device_vlan_update(false);
415 uci_foreach_element(&uci_network
->sections
, e
) {
416 struct uci_section
*s
= uci_to_section(e
);
419 if (strcmp(s
->type
, "bridge-vlan") != 0)
422 name
= uci_lookup_option_string(uci_ctx
, s
, "device");
426 dev
= device_get(name
, 0);
427 if (!dev
|| !dev
->vlans
.update
)
430 config_parse_vlan(dev
, s
);
432 device_vlan_update(true);
435 static struct uci_package
*
436 config_init_package(const char *config
)
438 struct uci_context
*ctx
= uci_ctx
;
439 struct uci_package
*p
= NULL
;
442 ctx
= uci_alloc_context();
445 ctx
->flags
&= ~UCI_FLAG_STRICT
;
447 uci_set_confdir(ctx
, config_path
);
450 uci_set_savedir(ctx
, "./tmp");
453 p
= uci_lookup_package(ctx
, config
);
458 if (uci_load(ctx
, config
, &p
))
465 config_init_interfaces(void)
467 struct uci_element
*e
;
469 uci_foreach_element(&uci_network
->sections
, e
) {
470 struct uci_section
*s
= uci_to_section(e
);
472 if (!strcmp(s
->type
, "interface"))
473 config_parse_interface(s
, false);
476 uci_foreach_element(&uci_network
->sections
, e
) {
477 struct uci_section
*s
= uci_to_section(e
);
479 if (!strcmp(s
->type
, "alias"))
480 config_parse_interface(s
, true);
487 struct interface
*iface
;
488 struct uci_element
*e
;
490 vlist_for_each_element(&interfaces
, iface
, node
)
491 interface_ip_update_start(&iface
->config_ip
);
493 uci_foreach_element(&uci_network
->sections
, e
) {
494 struct uci_section
*s
= uci_to_section(e
);
496 if (!strcmp(s
->type
, "route"))
497 config_parse_route(s
, false);
498 else if (!strcmp(s
->type
, "route6"))
499 config_parse_route(s
, true);
500 if (!strcmp(s
->type
, "neighbor"))
501 config_parse_neighbor(s
, false);
502 else if (!strcmp(s
->type
, "neighbor6"))
503 config_parse_neighbor(s
, true);
506 vlist_for_each_element(&interfaces
, iface
, node
)
507 interface_ip_update_complete(&iface
->config_ip
);
511 config_init_rules(void)
513 struct uci_element
*e
;
515 iprule_update_start();
517 uci_foreach_element(&uci_network
->sections
, e
) {
518 struct uci_section
*s
= uci_to_section(e
);
520 if (!strcmp(s
->type
, "rule"))
521 config_parse_rule(s
, false);
522 else if (!strcmp(s
->type
, "rule6"))
523 config_parse_rule(s
, true);
526 iprule_update_complete();
530 config_init_globals(void)
532 struct uci_section
*globals
= uci_lookup_section(
533 uci_ctx
, uci_network
, "globals");
537 const char *ula_prefix
= uci_lookup_option_string(
538 uci_ctx
, globals
, "ula_prefix");
539 interface_ip_set_ula_prefix(ula_prefix
);
543 config_parse_wireless_device(struct uci_section
*s
)
545 struct wireless_driver
*drv
;
546 const char *driver_name
;
548 driver_name
= uci_lookup_option_string(uci_ctx
, s
, "type");
552 drv
= avl_find_element(&wireless_drivers
, driver_name
, drv
, node
);
556 blob_buf_init(&b
, 0);
557 uci_to_blob(&b
, s
, drv
->device
.config
);
558 wireless_device_create(drv
, s
->e
.name
, b
.head
);
562 config_parse_wireless_vlan(struct wireless_interface
*vif
, struct uci_section
*s
)
566 name
= alloca(strlen(s
->type
) + 16);
567 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
569 blob_buf_init(&b
, 0);
570 uci_to_blob(&b
, s
, vif
->wdev
->drv
->vlan
.config
);
571 wireless_vlan_create(vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
575 config_parse_wireless_station(struct wireless_interface
*vif
, struct uci_section
*s
)
579 name
= alloca(strlen(s
->type
) + 16);
580 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
582 blob_buf_init(&b
, 0);
583 uci_to_blob(&b
, s
, vif
->wdev
->drv
->station
.config
);
584 wireless_station_create(vif
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
588 config_parse_wireless_interface(struct wireless_device
*wdev
, struct uci_section
*s
)
590 struct wireless_interface
*vif
;
591 struct uci_element
*f
;
594 name
= alloca(strlen(s
->type
) + 16);
595 sprintf(name
, "@%s[%d]", s
->type
, config_section_idx(s
));
597 blob_buf_init(&b
, 0);
598 uci_to_blob(&b
, s
, wdev
->drv
->interface
.config
);
599 vif
= wireless_interface_create(wdev
, b
.head
, s
->anonymous
? name
: s
->e
.name
);
606 uci_foreach_element(&uci_wireless
->sections
, f
) {
607 struct uci_section
*cur
= uci_to_section(f
);
608 const char *vif_name
;
610 if (strcmp(cur
->type
, "wifi-vlan") != 0)
613 vif_name
= uci_lookup_option_string(uci_ctx
, cur
, "iface");
614 if (vif_name
&& strcmp(s
->e
.name
, vif_name
))
616 config_parse_wireless_vlan(vif
, cur
);
619 uci_foreach_element(&uci_wireless
->sections
, f
) {
620 struct uci_section
*cur
= uci_to_section(f
);
621 const char *vif_name
;
623 if (strcmp(cur
->type
, "wifi-station") != 0)
626 vif_name
= uci_lookup_option_string(uci_ctx
, cur
, "iface");
627 if (vif_name
&& strcmp(s
->e
.name
, vif_name
))
629 config_parse_wireless_station(vif
, cur
);
633 vlist_flush(&vif
->vlans
);
634 vlist_flush(&vif
->stations
);
638 config_init_procd_wireless_interface(const char *wdev_name
, const char *vif_name
,
639 struct blob_attr
*config
,
640 struct blob_attr
*vlans
,
641 struct blob_attr
*stations
)
643 struct wireless_interface
*vif
;
644 struct wireless_device
*wdev
;
645 struct blob_attr
*cur
;
650 wdev
= vlist_find(&wireless_devices
, wdev_name
, wdev
, node
);
652 D(WIRELESS
, "device %s not found!", wdev_name
);
656 vif
= wireless_interface_create(wdev
, config
, vif_name
);
660 blobmsg_for_each_attr(cur
, vlans
, rem
) {
661 snprintf(name
, sizeof(name
), "%d", ++idx
);
662 wireless_vlan_create(vif
, cur
, name
);
665 blobmsg_for_each_attr(cur
, stations
, rem
) {
666 snprintf(name
, sizeof(name
), "%d", ++idx
);
667 wireless_station_create(vif
, cur
, name
);
670 vlist_flush(&vif
->vlans
);
671 vlist_flush(&vif
->stations
);
675 config_procd_wireless_interface_cb(struct blob_attr
*data
)
684 static const struct blobmsg_policy policy
[__UDATA_ATTR_MAX
] = {
685 [UDATA_ATTR_DEVICE
] = { "device", BLOBMSG_TYPE_STRING
},
686 [UDATA_ATTR_CONFIG
] = { "config", BLOBMSG_TYPE_TABLE
},
687 [UDATA_ATTR_STATIONS
] = { "stations", BLOBMSG_TYPE_ARRAY
},
688 [UDATA_ATTR_VLANS
] = { "vlans", BLOBMSG_TYPE_ARRAY
},
690 struct blob_attr
*tb
[__UDATA_ATTR_MAX
];
693 blobmsg_parse_attr(policy
, __UDATA_ATTR_MAX
, tb
, data
);
694 if (!tb
[UDATA_ATTR_DEVICE
] || !tb
[UDATA_ATTR_CONFIG
])
697 dev
= blobmsg_get_string(tb
[UDATA_ATTR_DEVICE
]);
698 config_init_procd_wireless_interface(dev
, blobmsg_name(data
),
699 tb
[UDATA_ATTR_CONFIG
],
700 tb
[UDATA_ATTR_VLANS
],
701 tb
[UDATA_ATTR_STATIONS
]);
705 config_init_wireless(void)
707 struct wireless_device
*wdev
;
708 struct uci_element
*e
;
709 const char *dev_name
;
712 D(WIRELESS
, "No wireless configuration found");
716 vlist_update(&wireless_devices
);
718 uci_foreach_element(&uci_wireless
->sections
, e
) {
719 struct uci_section
*s
= uci_to_section(e
);
720 if (strcmp(s
->type
, "wifi-device") != 0)
723 config_parse_wireless_device(s
);
726 vlist_flush(&wireless_devices
);
728 vlist_for_each_element(&wireless_devices
, wdev
, node
) {
730 vlist_update(&wdev
->interfaces
);
733 uci_foreach_element(&uci_wireless
->sections
, e
) {
734 struct uci_section
*s
= uci_to_section(e
);
736 if (strcmp(s
->type
, "wifi-iface") != 0)
739 dev_name
= uci_lookup_option_string(uci_ctx
, s
, "device");
743 wdev
= vlist_find(&wireless_devices
, dev_name
, wdev
, node
);
745 D(WIRELESS
, "device %s not found!", dev_name
);
749 config_parse_wireless_interface(wdev
, s
);
752 netifd_ubus_get_procd_data("wifi-iface", config_procd_wireless_interface_cb
);
754 vlist_for_each_element(&wireless_devices
, wdev
, node
)
755 vlist_flush(&wdev
->interfaces
);
759 static struct blob_attr
*
760 config_find_blobmsg_attr(struct blob_attr
*attr
, const char *name
, int type
)
762 struct blobmsg_policy policy
= { .name
= name
, .type
= type
};
763 struct blob_attr
*cur
;
765 blobmsg_parse(&policy
, 1, &cur
, blobmsg_data(attr
), blobmsg_len(attr
));
770 struct ether_addr
*config_get_default_macaddr(const char *ifname
)
772 struct blob_attr
*cur
;
777 cur
= config_find_blobmsg_attr(board_netdevs
, ifname
, BLOBMSG_TYPE_TABLE
);
781 cur
= config_find_blobmsg_attr(cur
, "macaddr", BLOBMSG_TYPE_STRING
);
785 return ether_aton(blobmsg_get_string(cur
));
788 int config_get_default_gro(const char *ifname
)
790 struct blob_attr
*cur
;
795 cur
= config_find_blobmsg_attr(board_netdevs
, ifname
, BLOBMSG_TYPE_TABLE
);
799 cur
= config_find_blobmsg_attr(cur
, "gro", BLOBMSG_TYPE_BOOL
);
803 return blobmsg_get_bool(cur
);
806 const char *config_get_default_conduit(const char *ifname
)
808 struct blob_attr
*cur
;
813 cur
= config_find_blobmsg_attr(board_netdevs
, ifname
, BLOBMSG_TYPE_TABLE
);
817 cur
= config_find_blobmsg_attr(cur
, "conduit", BLOBMSG_TYPE_STRING
);
821 return blobmsg_get_string(cur
);
825 config_init_board(void)
827 struct blob_attr
*cur
;
829 blob_buf_init(&b
, 0);
831 if (!blobmsg_add_json_from_file(&b
, DEFAULT_BOARD_JSON
))
835 board_netdevs
= NULL
;
837 cur
= config_find_blobmsg_attr(b
.head
, "network_device",
842 board_netdevs
= blob_memdup(cur
);
846 config_init_all(void)
851 uci_network
= config_init_package("network");
853 uci_get_errorstr(uci_ctx
, &err
, NULL
);
854 netifd_log_message(L_CRIT
, "Failed to load network config (%s)\n", err
);
859 uci_wireless
= config_init_package("wireless");
860 if (!uci_wireless
&& uci_ctx
->err
!= UCI_ERR_NOTFOUND
) {
861 uci_get_errorstr(uci_ctx
, &err
, NULL
);
862 netifd_log_message(L_CRIT
, "Failed to load wireless config (%s)\n", err
);
869 vlist_update(&interfaces
);
872 device_reset_config();
873 config_init_devices(true);
875 config_init_devices(false);
876 config_init_interfaces();
879 config_init_globals();
880 config_init_wireless();
885 device_init_pending();
886 vlist_flush(&interfaces
);
887 interface_refresh_assignments(false);
888 interface_start_pending();
889 wireless_start_pending(0);