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.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces;
28 static LIST_HEAD(iface_all_users);
29 static unsigned int interface_serial = 0;
35 IFACE_ATTR_DEFAULTROUTE,
38 IFACE_ATTR_DNS_SEARCH,
48 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
49 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
50 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
51 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
52 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
53 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
54 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
55 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
56 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
57 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
58 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
59 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
60 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
61 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
64 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
65 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
68 const struct config_param_list interface_attr_list = {
69 .n_params = IFACE_ATTR_MAX,
70 .params = iface_attrs,
71 .info = iface_attr_info,
75 interface_clear_errors(struct interface *iface)
77 struct interface_error *error, *tmp;
79 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
80 list_del(&error->list);
85 void interface_add_error(struct interface *iface, const char *subsystem,
86 const char *code, const char **data, int n_data)
88 struct interface_error *error;
91 char *dest, *d_subsys, *d_code;
94 len = n_data * sizeof(char *);
95 datalen = alloca(len);
96 for (i = 0; i < n_data; i++) {
97 datalen[i] = strlen(data[i]) + 1;
102 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
103 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
104 &d_code, code ? strlen(code) + 1 : 0);
108 list_add_tail(&error->list, &iface->errors);
110 dest = (char *) &error->data[n_data + 1];
111 for (i = 0; i < n_data; i++) {
112 error->data[i] = dest;
113 memcpy(dest, data[i], datalen[i]);
116 error->data[n_data++] = NULL;
119 error->subsystem = strcpy(d_subsys, subsystem);
122 error->code = strcpy(d_code, code);
126 interface_data_del(struct interface *iface, struct interface_data *data)
128 avl_delete(&iface->data, &data->node);
133 interface_data_flush(struct interface *iface)
135 struct interface_data *d, *tmp;
137 avl_for_each_element_safe(&iface->data, d, node, tmp)
138 interface_data_del(iface, d);
142 interface_add_data(struct interface *iface, const struct blob_attr *data)
144 struct interface_data *n, *o;
146 if (!blobmsg_check_attr(data, true))
147 return UBUS_STATUS_INVALID_ARGUMENT;
149 n = calloc(1, sizeof(*n) + blob_pad_len(data));
150 memcpy(n->data, data, blob_pad_len(data));
151 n->node.key = blobmsg_name(data);
153 o = avl_find_element(&iface->data, n->node.key, o, node);
155 interface_data_del(iface, o);
157 avl_insert(&iface->data, &n->node);
162 interface_event(struct interface *iface, enum interface_event ev)
164 struct interface_user *dep, *tmp;
165 struct device *adev = NULL;
167 list_for_each_entry_safe(dep, tmp, &iface->users, list)
168 dep->cb(dep, iface, ev);
170 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
171 dep->cb(dep, iface, ev);
175 adev = iface->l3_dev.dev;
178 alias_notify_device(iface->name, adev);
186 interface_flush_state(struct interface *iface)
188 if (iface->l3_dev.dev)
189 device_release(&iface->l3_dev);
190 interface_data_flush(iface);
194 mark_interface_down(struct interface *iface)
196 enum interface_state state = iface->state;
198 iface->state = IFS_DOWN;
200 interface_event(iface, IFEV_DOWN);
201 interface_ip_set_enabled(&iface->config_ip, false);
202 interface_ip_flush(&iface->proto_ip);
203 interface_flush_state(iface);
204 system_flush_routes();
208 __interface_set_down(struct interface *iface, bool force)
210 if (iface->state == IFS_DOWN ||
211 iface->state == IFS_TEARDOWN)
214 if (iface->state == IFS_UP)
215 interface_event(iface, IFEV_DOWN);
216 iface->state = IFS_TEARDOWN;
217 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
219 interface_flush_state(iface);
223 interface_cb(struct device_user *dep, enum device_event ev)
225 struct interface *iface;
228 iface = container_of(dep, struct interface, main_dev);
233 case DEV_EVENT_REMOVE:
240 interface_set_available(iface, new_state);
241 if (!new_state && dep->dev->external)
242 interface_set_main_dev(iface, NULL);
246 interface_set_available(struct interface *iface, bool new_state)
248 if (iface->available == new_state)
251 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
252 iface->available = new_state;
255 if (iface->autostart && !config_init)
256 interface_set_up(iface);
258 __interface_set_down(iface, true);
262 interface_add_user(struct interface_user *dep, struct interface *iface)
265 list_add(&dep->list, &iface_all_users);
270 list_add(&dep->list, &iface->users);
271 if (iface->state == IFS_UP)
272 dep->cb(dep, iface, IFEV_UP);
276 interface_remove_user(struct interface_user *dep)
278 list_del_init(&dep->list);
283 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
285 struct interface *alias = container_of(dep, struct interface, parent_iface);
286 struct device *dev = iface->l3_dev.dev;
293 interface_set_main_dev(alias, dev);
294 interface_set_available(alias, true);
297 interface_set_available(alias, false);
298 interface_set_main_dev(alias, NULL);
301 interface_remove_user(dep);
309 interface_claim_device(struct interface *iface)
311 struct interface *parent;
312 struct device *dev = NULL;
314 if (iface->parent_iface.iface)
315 interface_remove_user(&iface->parent_iface);
317 if (iface->parent_ifname) {
318 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
319 iface->parent_iface.cb = interface_alias_cb;
320 interface_add_user(&iface->parent_iface, parent);
321 } else if (iface->ifname &&
322 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
323 dev = device_get(iface->ifname, true);
327 interface_set_main_dev(iface, dev);
329 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
330 interface_set_available(iface, true);
334 interface_cleanup_state(struct interface *iface)
336 interface_set_available(iface, false);
338 interface_flush_state(iface);
339 interface_clear_errors(iface);
340 interface_set_proto_state(iface, NULL);
342 if (iface->main_dev.dev)
343 interface_set_main_dev(iface, NULL);
347 interface_cleanup(struct interface *iface)
349 struct interface_user *dep, *tmp;
351 if (iface->parent_iface.iface)
352 interface_remove_user(&iface->parent_iface);
354 list_for_each_entry_safe(dep, tmp, &iface->users, list)
355 interface_remove_user(dep);
357 interface_ip_flush(&iface->config_ip);
358 interface_cleanup_state(iface);
362 interface_do_free(struct interface *iface)
364 interface_event(iface, IFEV_FREE);
365 interface_cleanup(iface);
367 netifd_ubus_remove_interface(iface);
368 avl_delete(&interfaces.avl, &iface->node.avl);
373 interface_do_reload(struct interface *iface)
375 interface_event(iface, IFEV_RELOAD);
376 interface_cleanup_state(iface);
377 proto_init_interface(iface, iface->config);
378 interface_claim_device(iface);
382 interface_handle_config_change(struct interface *iface)
384 enum interface_config_state state = iface->config_state;
386 iface->config_state = IFC_NORMAL;
391 interface_do_reload(iface);
394 interface_do_free(iface);
397 if (iface->autostart && iface->available)
398 interface_set_up(iface);
402 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
404 struct interface *iface = state->iface;
408 if (iface->state != IFS_SETUP)
411 interface_ip_set_enabled(&iface->config_ip, true);
412 system_flush_routes();
413 iface->state = IFS_UP;
414 iface->start_time = system_get_rtime();
415 interface_event(iface, IFEV_UP);
416 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
419 if (iface->state == IFS_DOWN)
422 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
423 mark_interface_down(iface);
424 if (iface->main_dev.dev)
425 device_release(&iface->main_dev);
426 interface_handle_config_change(iface);
428 case IFPEV_LINK_LOST:
429 if (iface->state != IFS_UP)
432 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
433 mark_interface_down(iface);
434 iface->state = IFS_SETUP;
438 interface_write_resolv_conf();
441 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
444 iface->proto->free(iface->proto);
447 iface->state = IFS_DOWN;
448 iface->proto = state;
452 state->proto_event = interface_proto_cb;
453 state->iface = iface;
457 interface_init(struct interface *iface, const char *name,
458 struct blob_attr *config)
460 struct blob_attr *tb[IFACE_ATTR_MAX];
461 struct blob_attr *cur;
462 const char *proto_name = NULL;
464 strncpy(iface->name, name, sizeof(iface->name) - 1);
465 INIT_LIST_HEAD(&iface->errors);
466 INIT_LIST_HEAD(&iface->users);
467 INIT_LIST_HEAD(&iface->hotplug_list);
468 interface_ip_init(iface);
469 avl_init(&iface->data, avl_strcmp, false, NULL);
470 iface->config_ip.enabled = false;
472 iface->main_dev.cb = interface_cb;
474 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
475 blob_data(config), blob_len(config));
477 if ((cur = tb[IFACE_ATTR_PROTO]))
478 proto_name = blobmsg_data(cur);
480 proto_attach_interface(iface, proto_name);
482 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
483 iface->proto_ip.no_defaultroute =
484 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
485 iface->proto_ip.no_dns =
486 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
488 if ((cur = tb[IFACE_ATTR_DNS]))
489 interface_add_dns_server_list(&iface->config_ip, cur);
491 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
492 interface_add_dns_search_list(&iface->config_ip, cur);
494 if ((cur = tb[IFACE_ATTR_METRIC]))
495 iface->metric = blobmsg_get_u32(cur);
497 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
498 iface->config_ip.assignment_length = blobmsg_get_u32(cur);
500 iface->config_ip.assignment_hint = -1;
501 if ((cur = tb[IFACE_ATTR_IP6HINT]))
502 iface->config_ip.assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
503 ~((1 << (64 - iface->config_ip.assignment_length)) - 1);
505 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
506 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
507 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
510 // Set a default exteranl routing table for IPv6 to do source-based-filtering
512 iface->ip6table = 1000 + ++interface_serial;
513 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
514 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
515 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
518 iface->config_autostart = iface->autostart;
521 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
523 struct blob_attr *tb[IFACE_ATTR_MAX];
524 struct blob_attr *cur;
526 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
527 blob_data(config), blob_len(config));
530 if ((cur = tb[IFACE_ATTR_INTERFACE]))
531 iface->parent_ifname = blobmsg_data(cur);
533 if (!iface->parent_ifname)
536 if ((cur = tb[IFACE_ATTR_IFNAME]))
537 iface->ifname = blobmsg_data(cur);
541 iface->config = config;
542 vlist_add(&interfaces, &iface->node, iface->name);
547 interface_add(struct interface *iface, struct blob_attr *config)
549 __interface_add(iface, config, false);
553 interface_add_alias(struct interface *iface, struct blob_attr *config)
555 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
558 return __interface_add(iface, config, true);
562 interface_set_l3_dev(struct interface *iface, struct device *dev)
564 bool enabled = iface->config_ip.enabled;
565 bool claimed = iface->l3_dev.claimed;
567 if (iface->l3_dev.dev == dev)
570 interface_ip_set_enabled(&iface->config_ip, false);
571 interface_ip_flush(&iface->proto_ip);
572 device_add_user(&iface->l3_dev, dev);
576 device_claim(&iface->l3_dev);
577 interface_ip_set_enabled(&iface->config_ip, enabled);
582 interface_set_main_dev(struct interface *iface, struct device *dev)
584 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
585 bool claimed = iface->l3_dev.claimed;
587 if (iface->main_dev.dev == dev)
591 interface_set_l3_dev(iface, dev);
593 device_add_user(&iface->main_dev, dev);
595 device_claim(&iface->l3_dev);
597 if (!iface->l3_dev.dev)
598 interface_set_l3_dev(iface, dev);
602 interface_remove_link(struct interface *iface, struct device *dev)
604 struct device *mdev = iface->main_dev.dev;
606 if (mdev && mdev->hotplug_ops)
607 return mdev->hotplug_ops->del(mdev, dev);
609 if (!iface->main_dev.hotplug)
610 return UBUS_STATUS_INVALID_ARGUMENT;
612 if (dev != iface->main_dev.dev)
613 return UBUS_STATUS_INVALID_ARGUMENT;
615 device_remove_user(&iface->main_dev);
620 interface_add_link(struct interface *iface, struct device *dev)
622 struct device *mdev = iface->main_dev.dev;
627 if (iface->main_dev.hotplug)
628 device_remove_user(&iface->main_dev);
631 if (mdev->hotplug_ops)
632 return mdev->hotplug_ops->add(mdev, dev);
634 return UBUS_STATUS_NOT_SUPPORTED;
637 interface_set_main_dev(iface, dev);
638 iface->main_dev.hotplug = true;
643 interface_set_up(struct interface *iface)
647 iface->autostart = true;
649 if (iface->state != IFS_DOWN)
652 interface_clear_errors(iface);
653 if (!iface->available) {
654 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
658 if (iface->main_dev.dev) {
659 ret = device_claim(&iface->main_dev);
664 iface->state = IFS_SETUP;
665 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
667 mark_interface_down(iface);
675 interface_set_down(struct interface *iface)
678 vlist_for_each_element(&interfaces, iface, node)
679 __interface_set_down(iface, false);
681 iface->autostart = false;
682 __interface_set_down(iface, false);
689 interface_start_pending(void)
691 struct interface *iface;
693 vlist_for_each_element(&interfaces, iface, node) {
694 if (iface->available && iface->autostart)
695 interface_set_up(iface);
700 set_config_state(struct interface *iface, enum interface_config_state s)
702 iface->config_state = s;
703 if (iface->state == IFS_DOWN)
704 interface_handle_config_change(iface);
706 __interface_set_down(iface, false);
710 interface_update_start(struct interface *iface)
712 interface_ip_update_start(&iface->proto_ip);
716 interface_update_complete(struct interface *iface)
718 interface_ip_update_complete(&iface->proto_ip);
722 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
724 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
725 vlist_simple_replace(&new->dns_search, &old->dns_search);
729 interface_change_config(struct interface *if_old, struct interface *if_new)
731 struct blob_attr *old_config = if_old->config;
732 bool reload = false, reload_ip = false, reload_assignment = false;
734 #define FIELD_CHANGED_STR(field) \
735 ((!!if_old->field != !!if_new->field) || \
737 strcmp(if_old->field, if_new->field) != 0))
739 if (FIELD_CHANGED_STR(parent_ifname)) {
740 if (if_old->parent_iface.iface)
741 interface_remove_user(&if_old->parent_iface);
745 if (FIELD_CHANGED_STR(ifname) ||
746 if_old->proto_handler != if_new->proto_handler)
749 if (!if_old->proto_handler->config_params)
750 D(INTERFACE, "No config parameters for interface '%s'\n",
752 else if (!config_check_equal(if_old->config, if_new->config,
753 if_old->proto_handler->config_params))
756 #define UPDATE(field, __var) ({ \
757 bool __changed = (if_old->field != if_new->field); \
758 if_old->field = if_new->field; \
759 __var |= __changed; \
762 if_old->config = if_new->config;
763 if (!if_old->config_autostart && if_new->config_autostart)
764 if_old->autostart = true;
766 if_old->device_config = if_new->device_config;
767 if_old->config_autostart = if_new->config_autostart;
768 if_old->ifname = if_new->ifname;
769 if_old->parent_ifname = if_new->parent_ifname;
770 if_old->proto_handler = if_new->proto_handler;
772 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
773 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
775 UPDATE(metric, reload_ip);
776 UPDATE(proto_ip.no_defaultroute, reload_ip);
777 UPDATE(config_ip.assignment_length, reload_assignment);
778 UPDATE(config_ip.assignment_hint, reload_assignment);
783 D(INTERFACE, "Reload interface '%s because of config changes\n",
785 interface_clear_errors(if_old);
786 set_config_state(if_old, IFC_RELOAD);
791 interface_ip_set_enabled(&if_old->config_ip, false);
792 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
793 interface_ip_set_enabled(&if_old->proto_ip, false);
794 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
797 if (reload_assignment)
798 interface_refresh_assignments(true);
800 interface_write_resolv_conf();
803 if_new->config = NULL;
804 interface_cleanup(if_new);
810 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
811 struct vlist_node *node_old)
813 struct interface *if_old = container_of(node_old, struct interface, node);
814 struct interface *if_new = container_of(node_new, struct interface, node);
816 if (node_old && node_new) {
817 D(INTERFACE, "Update interface '%s'\n", if_new->name);
818 interface_change_config(if_old, if_new);
819 } else if (node_old) {
820 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
821 set_config_state(if_old, IFC_REMOVE);
822 } else if (node_new) {
823 D(INTERFACE, "Create interface '%s'\n", if_new->name);
824 proto_init_interface(if_new, if_new->config);
825 interface_claim_device(if_new);
826 netifd_ubus_add_interface(if_new);
832 interface_init_list(void)
834 vlist_init(&interfaces, avl_strcmp, interface_update);
835 interfaces.keep_old = true;
836 interfaces.no_delete = true;