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);
34 IFACE_ATTR_DEFAULTROUTE,
37 IFACE_ATTR_DNS_SEARCH,
43 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
44 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
46 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
47 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
48 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
49 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
50 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
51 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
52 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
55 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
56 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
59 const struct config_param_list interface_attr_list = {
60 .n_params = IFACE_ATTR_MAX,
61 .params = iface_attrs,
62 .info = iface_attr_info,
66 interface_clear_errors(struct interface *iface)
68 struct interface_error *error, *tmp;
70 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
71 list_del(&error->list);
76 void interface_add_error(struct interface *iface, const char *subsystem,
77 const char *code, const char **data, int n_data)
79 struct interface_error *error;
82 char *dest, *d_subsys, *d_code;
85 len = n_data * sizeof(char *);
86 datalen = alloca(len);
87 for (i = 0; i < n_data; i++) {
88 datalen[i] = strlen(data[i]) + 1;
93 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
94 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
95 &d_code, code ? strlen(code) + 1 : 0);
99 list_add_tail(&error->list, &iface->errors);
101 dest = (char *) &error->data[n_data + 1];
102 for (i = 0; i < n_data; i++) {
103 error->data[i] = dest;
104 memcpy(dest, data[i], datalen[i]);
107 error->data[n_data++] = NULL;
110 error->subsystem = strcpy(d_subsys, subsystem);
113 error->code = strcpy(d_code, code);
117 interface_data_del(struct interface *iface, struct interface_data *data)
119 avl_delete(&iface->data, &data->node);
124 interface_data_flush(struct interface *iface)
126 struct interface_data *d, *tmp;
128 avl_for_each_element_safe(&iface->data, d, node, tmp)
129 interface_data_del(iface, d);
133 interface_add_data(struct interface *iface, const struct blob_attr *data)
135 struct interface_data *n, *o;
137 if (!blobmsg_check_attr(data, true))
138 return UBUS_STATUS_INVALID_ARGUMENT;
140 n = calloc(1, sizeof(*n) + blob_pad_len(data));
141 memcpy(n->data, data, blob_pad_len(data));
142 n->node.key = blobmsg_name(data);
144 o = avl_find_element(&iface->data, n->node.key, o, node);
146 interface_data_del(iface, o);
148 avl_insert(&iface->data, &n->node);
153 interface_event(struct interface *iface, enum interface_event ev)
155 struct interface_user *dep, *tmp;
156 struct device *adev = NULL;
158 list_for_each_entry_safe(dep, tmp, &iface->users, list)
159 dep->cb(dep, iface, ev);
161 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
162 dep->cb(dep, iface, ev);
166 adev = iface->l3_dev.dev;
169 alias_notify_device(iface->name, adev);
177 interface_flush_state(struct interface *iface)
179 if (iface->l3_dev.dev)
180 device_release(&iface->l3_dev);
181 interface_data_flush(iface);
185 mark_interface_down(struct interface *iface)
187 enum interface_state state = iface->state;
189 iface->state = IFS_DOWN;
191 interface_event(iface, IFEV_DOWN);
192 interface_ip_set_enabled(&iface->config_ip, false);
193 interface_ip_flush(&iface->proto_ip);
194 interface_flush_state(iface);
195 system_flush_routes();
199 __interface_set_down(struct interface *iface, bool force)
201 if (iface->state == IFS_DOWN ||
202 iface->state == IFS_TEARDOWN)
205 if (iface->state == IFS_UP)
206 interface_event(iface, IFEV_DOWN);
207 iface->state = IFS_TEARDOWN;
208 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
210 interface_flush_state(iface);
214 interface_cb(struct device_user *dep, enum device_event ev)
216 struct interface *iface;
219 iface = container_of(dep, struct interface, main_dev);
224 case DEV_EVENT_REMOVE:
231 interface_set_available(iface, new_state);
232 if (!new_state && dep->dev->external)
233 interface_set_main_dev(iface, NULL);
237 interface_set_available(struct interface *iface, bool new_state)
239 if (iface->available == new_state)
242 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
243 iface->available = new_state;
246 if (iface->autostart && !config_init)
247 interface_set_up(iface);
249 __interface_set_down(iface, true);
253 interface_add_user(struct interface_user *dep, struct interface *iface)
256 list_add(&dep->list, &iface_all_users);
261 list_add(&dep->list, &iface->users);
262 if (iface->state == IFS_UP)
263 dep->cb(dep, iface, IFEV_UP);
267 interface_remove_user(struct interface_user *dep)
269 list_del_init(&dep->list);
274 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
276 struct interface *alias = container_of(dep, struct interface, parent_iface);
277 struct device *dev = iface->l3_dev.dev;
284 interface_set_main_dev(alias, dev);
285 interface_set_available(alias, true);
288 interface_set_available(alias, false);
289 interface_set_main_dev(alias, NULL);
292 interface_remove_user(dep);
300 interface_claim_device(struct interface *iface)
302 struct interface *parent;
303 struct device *dev = NULL;
305 if (iface->parent_iface.iface)
306 interface_remove_user(&iface->parent_iface);
308 if (iface->parent_ifname) {
309 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
310 iface->parent_iface.cb = interface_alias_cb;
311 interface_add_user(&iface->parent_iface, parent);
312 } else if (iface->ifname &&
313 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
314 dev = device_get(iface->ifname, true);
318 interface_set_main_dev(iface, dev);
320 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
321 interface_set_available(iface, true);
325 interface_cleanup_state(struct interface *iface)
327 interface_flush_state(iface);
328 interface_clear_errors(iface);
329 interface_set_proto_state(iface, NULL);
333 interface_cleanup(struct interface *iface)
335 struct interface_user *dep, *tmp;
337 if (iface->parent_iface.iface)
338 interface_remove_user(&iface->parent_iface);
340 list_for_each_entry_safe(dep, tmp, &iface->users, list)
341 interface_remove_user(dep);
343 interface_ip_flush(&iface->config_ip);
344 if (iface->main_dev.dev)
345 interface_set_main_dev(iface, NULL);
347 interface_cleanup_state(iface);
351 interface_do_free(struct interface *iface)
353 interface_event(iface, IFEV_FREE);
354 interface_cleanup(iface);
356 netifd_ubus_remove_interface(iface);
357 avl_delete(&interfaces.avl, &iface->node.avl);
362 interface_do_reload(struct interface *iface)
364 interface_event(iface, IFEV_RELOAD);
365 interface_cleanup_state(iface);
366 proto_init_interface(iface, iface->config);
367 interface_claim_device(iface);
371 interface_handle_config_change(struct interface *iface)
373 enum interface_config_state state = iface->config_state;
375 iface->config_state = IFC_NORMAL;
380 interface_do_reload(iface);
383 interface_do_free(iface);
386 if (iface->autostart && iface->available)
387 interface_set_up(iface);
391 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
393 struct interface *iface = state->iface;
397 if (iface->state != IFS_SETUP)
400 interface_ip_set_enabled(&iface->config_ip, true);
401 system_flush_routes();
402 iface->state = IFS_UP;
403 iface->start_time = system_get_rtime();
404 interface_event(iface, IFEV_UP);
405 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
408 if (iface->state == IFS_DOWN)
411 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
412 mark_interface_down(iface);
413 if (iface->main_dev.dev)
414 device_release(&iface->main_dev);
415 interface_handle_config_change(iface);
417 case IFPEV_LINK_LOST:
418 if (iface->state != IFS_UP)
421 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
422 mark_interface_down(iface);
423 iface->state = IFS_SETUP;
427 interface_write_resolv_conf();
430 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
433 iface->proto->free(iface->proto);
436 iface->state = IFS_DOWN;
437 iface->proto = state;
441 state->proto_event = interface_proto_cb;
442 state->iface = iface;
446 interface_init(struct interface *iface, const char *name,
447 struct blob_attr *config)
449 struct blob_attr *tb[IFACE_ATTR_MAX];
450 struct blob_attr *cur;
451 const char *proto_name = NULL;
453 strncpy(iface->name, name, sizeof(iface->name) - 1);
454 INIT_LIST_HEAD(&iface->errors);
455 INIT_LIST_HEAD(&iface->users);
456 INIT_LIST_HEAD(&iface->hotplug_list);
457 interface_ip_init(iface);
458 avl_init(&iface->data, avl_strcmp, false, NULL);
459 iface->config_ip.enabled = false;
461 iface->main_dev.cb = interface_cb;
463 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
464 blob_data(config), blob_len(config));
466 if ((cur = tb[IFACE_ATTR_PROTO]))
467 proto_name = blobmsg_data(cur);
469 proto_attach_interface(iface, proto_name);
471 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
472 iface->proto_ip.no_defaultroute =
473 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
474 iface->proto_ip.no_dns =
475 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
477 if ((cur = tb[IFACE_ATTR_DNS]))
478 interface_add_dns_server_list(&iface->config_ip, cur);
480 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
481 interface_add_dns_search_list(&iface->config_ip, cur);
483 if ((cur = tb[IFACE_ATTR_METRIC]))
484 iface->metric = blobmsg_get_u32(cur);
486 iface->config_autostart = iface->autostart;
489 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
491 struct blob_attr *tb[IFACE_ATTR_MAX];
492 struct blob_attr *cur;
494 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
495 blob_data(config), blob_len(config));
498 if ((cur = tb[IFACE_ATTR_INTERFACE]))
499 iface->parent_ifname = blobmsg_data(cur);
501 if (!iface->parent_ifname)
504 if ((cur = tb[IFACE_ATTR_IFNAME]))
505 iface->ifname = blobmsg_data(cur);
509 iface->config = config;
510 vlist_add(&interfaces, &iface->node, iface->name);
515 interface_add(struct interface *iface, struct blob_attr *config)
517 __interface_add(iface, config, false);
521 interface_add_alias(struct interface *iface, struct blob_attr *config)
523 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
526 return __interface_add(iface, config, true);
530 interface_set_l3_dev(struct interface *iface, struct device *dev)
532 bool enabled = iface->config_ip.enabled;
533 bool claimed = iface->l3_dev.claimed;
535 if (iface->l3_dev.dev == dev)
538 interface_ip_set_enabled(&iface->config_ip, false);
539 interface_ip_flush(&iface->proto_ip);
540 device_add_user(&iface->l3_dev, dev);
544 device_claim(&iface->l3_dev);
545 interface_ip_set_enabled(&iface->config_ip, enabled);
550 interface_set_main_dev(struct interface *iface, struct device *dev)
552 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
553 bool claimed = iface->l3_dev.claimed;
555 if (iface->main_dev.dev == dev)
559 interface_set_l3_dev(iface, dev);
561 device_add_user(&iface->main_dev, dev);
563 device_claim(&iface->l3_dev);
565 if (!iface->l3_dev.dev)
566 interface_set_l3_dev(iface, dev);
570 interface_remove_link(struct interface *iface, struct device *dev)
572 struct device *mdev = iface->main_dev.dev;
574 if (mdev && mdev->hotplug_ops)
575 return mdev->hotplug_ops->del(mdev, dev);
577 if (!iface->main_dev.hotplug)
578 return UBUS_STATUS_INVALID_ARGUMENT;
580 if (dev != iface->main_dev.dev)
581 return UBUS_STATUS_INVALID_ARGUMENT;
583 device_remove_user(&iface->main_dev);
588 interface_add_link(struct interface *iface, struct device *dev)
590 struct device *mdev = iface->main_dev.dev;
595 if (iface->main_dev.hotplug)
596 device_remove_user(&iface->main_dev);
599 if (mdev->hotplug_ops)
600 return mdev->hotplug_ops->add(mdev, dev);
602 return UBUS_STATUS_NOT_SUPPORTED;
605 interface_set_main_dev(iface, dev);
606 iface->main_dev.hotplug = true;
611 interface_set_up(struct interface *iface)
615 iface->autostart = true;
617 if (iface->state != IFS_DOWN)
620 interface_clear_errors(iface);
621 if (!iface->available) {
622 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
626 if (iface->main_dev.dev) {
627 ret = device_claim(&iface->main_dev);
632 iface->state = IFS_SETUP;
633 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
635 mark_interface_down(iface);
643 interface_set_down(struct interface *iface)
646 vlist_for_each_element(&interfaces, iface, node)
647 __interface_set_down(iface, false);
649 iface->autostart = false;
650 __interface_set_down(iface, false);
657 interface_start_pending(void)
659 struct interface *iface;
661 vlist_for_each_element(&interfaces, iface, node) {
662 if (iface->available && iface->autostart)
663 interface_set_up(iface);
668 set_config_state(struct interface *iface, enum interface_config_state s)
670 iface->config_state = s;
671 if (iface->state == IFS_DOWN)
672 interface_handle_config_change(iface);
674 __interface_set_down(iface, false);
678 interface_update_start(struct interface *iface)
680 interface_ip_update_start(&iface->proto_ip);
684 interface_update_complete(struct interface *iface)
686 interface_ip_update_complete(&iface->proto_ip);
690 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
692 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
693 vlist_simple_replace(&new->dns_search, &old->dns_search);
697 interface_change_config(struct interface *if_old, struct interface *if_new)
699 struct blob_attr *old_config = if_old->config;
700 const char *old_ifname = if_old->ifname;
701 const char *old_parent_ifname = if_old->parent_ifname;
702 const struct proto_handler *proto = if_old->proto_handler;
704 interface_clear_errors(if_old);
705 if_old->config = if_new->config;
706 if (!if_old->config_autostart && if_new->config_autostart)
707 if_old->autostart = true;
709 if_old->device_config = if_new->device_config;
710 if_old->config_autostart = if_new->config_autostart;
711 if_old->ifname = if_new->ifname;
712 if_old->parent_ifname = if_new->parent_ifname;
713 if_old->proto_handler = if_new->proto_handler;
715 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
716 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
718 #define FIELD_CHANGED_STR(field) \
719 ((!!if_old->field != !!old_ ## field) || \
721 strcmp(old_ ## field, if_old->field) != 0))
723 if (FIELD_CHANGED_STR(parent_ifname)) {
724 if (if_old->parent_iface.iface)
725 interface_remove_user(&if_old->parent_iface);
729 if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
730 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
735 if (!proto->config_params)
736 D(INTERFACE, "No config parameters for interface '%s'\n",
738 else if (!config_check_equal(old_config, if_new->config,
739 proto->config_params)) {
740 D(INTERFACE, "Reload interface '%s because of config changes\n",
745 #define UPDATE(field) ({ \
746 bool __changed = (if_old->field != if_new->field); \
747 if_old->field = if_new->field; \
751 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
752 interface_ip_set_enabled(&if_old->config_ip, false);
753 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
754 interface_ip_set_enabled(&if_old->proto_ip, false);
755 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
758 interface_write_resolv_conf();
765 set_config_state(if_old, IFC_RELOAD);
767 if_new->config = NULL;
768 interface_cleanup(if_new);
774 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
775 struct vlist_node *node_old)
777 struct interface *if_old = container_of(node_old, struct interface, node);
778 struct interface *if_new = container_of(node_new, struct interface, node);
780 if (node_old && node_new) {
781 D(INTERFACE, "Update interface '%s'\n", if_new->name);
782 interface_change_config(if_old, if_new);
783 } else if (node_old) {
784 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
785 set_config_state(if_old, IFC_REMOVE);
786 } else if (node_new) {
787 D(INTERFACE, "Create interface '%s'\n", if_new->name);
788 proto_init_interface(if_new, if_new->config);
789 interface_claim_device(if_new);
790 netifd_ubus_add_interface(if_new);
796 interface_init_list(void)
798 vlist_init(&interfaces, avl_strcmp, interface_update);
799 interfaces.keep_old = true;
800 interfaces.no_delete = true;