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;
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(1, sizeof(*error) + sizeof(char *) + len);
97 list_add_tail(&error->list, &iface->errors);
98 error->subsystem = subsystem;
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;
111 interface_data_del(struct interface *iface, struct interface_data *data)
113 avl_delete(&iface->data, &data->node);
118 interface_data_flush(struct interface *iface)
120 struct interface_data *d, *tmp;
122 avl_for_each_element_safe(&iface->data, d, node, tmp)
123 interface_data_del(iface, d);
127 interface_add_data(struct interface *iface, const struct blob_attr *data)
129 struct interface_data *n, *o;
131 if (!blobmsg_check_attr(data, true))
132 return UBUS_STATUS_INVALID_ARGUMENT;
134 n = calloc(1, sizeof(*n) + blob_pad_len(data));
135 memcpy(n->data, data, blob_pad_len(data));
136 n->node.key = blobmsg_name(data);
138 o = avl_find_element(&iface->data, n->node.key, o, node);
140 interface_data_del(iface, o);
142 avl_insert(&iface->data, &n->node);
147 interface_event(struct interface *iface, enum interface_event ev)
149 struct interface_user *dep, *tmp;
151 list_for_each_entry_safe(dep, tmp, &iface->users, list)
152 dep->cb(dep, iface, ev);
154 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
155 dep->cb(dep, iface, ev);
159 interface_flush_state(struct interface *iface)
161 if (iface->main_dev.dev)
162 device_release(&iface->main_dev);
163 if (iface->l3_dev.dev)
164 device_release(&iface->l3_dev);
165 interface_data_flush(iface);
169 mark_interface_down(struct interface *iface)
171 enum interface_state state = iface->state;
173 iface->state = IFS_DOWN;
175 interface_event(iface, IFEV_DOWN);
176 interface_ip_set_enabled(&iface->config_ip, false);
177 interface_ip_flush(&iface->proto_ip);
178 interface_flush_state(iface);
179 system_flush_routes();
183 __interface_set_down(struct interface *iface, bool force)
185 interface_clear_errors(iface);
187 if (iface->state == IFS_DOWN ||
188 iface->state == IFS_TEARDOWN)
191 if (iface->state == IFS_UP)
192 interface_event(iface, IFEV_DOWN);
193 iface->state = IFS_TEARDOWN;
194 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
196 interface_flush_state(iface);
200 interface_cb(struct device_user *dep, enum device_event ev)
202 struct interface *iface;
205 iface = container_of(dep, struct interface, main_dev);
210 case DEV_EVENT_REMOVE:
217 interface_set_available(iface, new_state);
221 interface_set_available(struct interface *iface, bool new_state)
223 if (iface->available == new_state)
226 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
227 iface->available = new_state;
230 if (iface->autostart && !config_init)
231 interface_set_up(iface);
233 __interface_set_down(iface, true);
237 interface_add_user(struct interface_user *dep, struct interface *iface)
240 list_add(&dep->list, &iface_all_users);
245 list_add(&dep->list, &iface->users);
246 if (iface->state == IFS_UP)
247 dep->cb(dep, iface, IFEV_UP);
251 interface_remove_user(struct interface_user *dep)
253 list_del_init(&dep->list);
258 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
260 struct interface *alias = container_of(dep, struct interface, parent_iface);
261 struct device *dev = iface->l3_dev.dev;
268 interface_set_main_dev(alias, dev);
269 interface_set_available(alias, true);
272 interface_set_available(alias, false);
273 interface_set_main_dev(alias, NULL);
276 interface_remove_user(dep);
284 interface_claim_device(struct interface *iface)
286 struct interface *parent;
287 struct device *dev = NULL;
289 if (iface->parent_iface.iface)
290 interface_remove_user(&iface->parent_iface);
292 if (iface->parent_ifname) {
293 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
294 iface->parent_iface.cb = interface_alias_cb;
295 interface_add_user(&iface->parent_iface, parent);
296 } else if (iface->ifname &&
297 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
298 dev = device_get(iface->ifname, true);
302 interface_set_main_dev(iface, dev);
304 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
305 interface_set_available(iface, true);
310 interface_cleanup(struct interface *iface, bool reload)
312 struct interface_user *dep, *tmp;
314 if (iface->parent_iface.iface)
315 interface_remove_user(&iface->parent_iface);
317 list_for_each_entry_safe(dep, tmp, &iface->users, list)
318 interface_remove_user(dep);
320 interface_ip_flush(&iface->config_ip);
321 interface_flush_state(iface);
322 interface_clear_errors(iface);
324 if (iface->main_dev.dev && !reload)
325 interface_set_main_dev(iface, NULL);
326 interface_set_proto_state(iface, NULL);
330 interface_do_free(struct interface *iface)
332 interface_event(iface, IFEV_FREE);
333 interface_cleanup(iface, false);
335 netifd_ubus_remove_interface(iface);
336 avl_delete(&interfaces.avl, &iface->node.avl);
341 interface_do_reload(struct interface *iface)
343 interface_event(iface, IFEV_RELOAD);
344 interface_cleanup(iface, true);
345 proto_init_interface(iface, iface->config);
346 interface_claim_device(iface);
350 interface_handle_config_change(struct interface *iface)
352 enum interface_config_state state = iface->config_state;
354 iface->config_state = IFC_NORMAL;
359 interface_do_reload(iface);
362 interface_do_free(iface);
365 if (iface->autostart && iface->available)
366 interface_set_up(iface);
370 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
372 struct interface *iface = state->iface;
376 if (iface->state != IFS_SETUP)
379 interface_ip_set_enabled(&iface->config_ip, true);
380 system_flush_routes();
381 iface->state = IFS_UP;
382 iface->start_time = system_get_rtime();
383 interface_event(iface, IFEV_UP);
384 interface_write_resolv_conf();
385 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
388 if (iface->state == IFS_DOWN)
391 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
392 mark_interface_down(iface);
393 interface_handle_config_change(iface);
395 case IFPEV_LINK_LOST:
396 if (iface->state != IFS_UP)
399 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
400 mark_interface_down(iface);
401 iface->state = IFS_SETUP;
406 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
409 iface->proto->free(iface->proto);
412 iface->state = IFS_DOWN;
413 iface->proto = state;
417 state->proto_event = interface_proto_cb;
418 state->iface = iface;
422 interface_init(struct interface *iface, const char *name,
423 struct blob_attr *config)
425 struct blob_attr *tb[IFACE_ATTR_MAX];
426 struct blob_attr *cur;
427 const char *proto_name = NULL;
429 strncpy(iface->name, name, sizeof(iface->name) - 1);
430 INIT_LIST_HEAD(&iface->errors);
431 INIT_LIST_HEAD(&iface->users);
432 INIT_LIST_HEAD(&iface->hotplug_list);
433 interface_ip_init(iface);
434 avl_init(&iface->data, avl_strcmp, false, NULL);
435 iface->config_ip.enabled = false;
437 iface->main_dev.cb = interface_cb;
439 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
440 blob_data(config), blob_len(config));
442 if ((cur = tb[IFACE_ATTR_PROTO]))
443 proto_name = blobmsg_data(cur);
445 proto_attach_interface(iface, proto_name);
447 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
448 iface->proto_ip.no_defaultroute =
449 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
450 iface->proto_ip.no_dns =
451 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
453 if ((cur = tb[IFACE_ATTR_DNS]))
454 interface_add_dns_server_list(&iface->config_ip, cur);
456 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
457 interface_add_dns_search_list(&iface->config_ip, cur);
459 if ((cur = tb[IFACE_ATTR_METRIC]))
460 iface->metric = blobmsg_get_u32(cur);
462 iface->config_autostart = iface->autostart;
465 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
467 struct blob_attr *tb[IFACE_ATTR_MAX];
468 struct blob_attr *cur;
470 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
471 blob_data(config), blob_len(config));
474 if ((cur = tb[IFACE_ATTR_INTERFACE]))
475 iface->parent_ifname = blobmsg_data(cur);
477 if (!iface->parent_ifname)
480 if ((cur = tb[IFACE_ATTR_IFNAME]))
481 iface->ifname = blobmsg_data(cur);
485 iface->config = config;
486 vlist_add(&interfaces, &iface->node, iface->name);
491 interface_add(struct interface *iface, struct blob_attr *config)
493 __interface_add(iface, config, false);
497 interface_add_alias(struct interface *iface, struct blob_attr *config)
499 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
502 return __interface_add(iface, config, true);
506 interface_set_l3_dev(struct interface *iface, struct device *dev)
508 bool enabled = iface->config_ip.enabled;
509 bool claimed = iface->l3_dev.claimed;
511 if (iface->l3_dev.dev == dev)
514 interface_ip_set_enabled(&iface->config_ip, false);
515 interface_ip_flush(&iface->proto_ip);
516 device_add_user(&iface->l3_dev, dev);
520 device_claim(&iface->l3_dev);
521 interface_ip_set_enabled(&iface->config_ip, enabled);
526 interface_set_main_dev(struct interface *iface, struct device *dev)
528 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
529 bool claimed = iface->l3_dev.claimed;
531 if (iface->main_dev.dev == dev)
535 interface_set_l3_dev(iface, dev);
537 device_add_user(&iface->main_dev, dev);
539 device_claim(&iface->l3_dev);
541 if (!iface->l3_dev.dev)
542 interface_set_l3_dev(iface, dev);
546 interface_remove_link(struct interface *iface, struct device *dev)
548 struct device *mdev = iface->main_dev.dev;
550 if (mdev && mdev->hotplug_ops)
551 return mdev->hotplug_ops->del(mdev, dev);
553 if (!iface->main_dev.hotplug)
554 return UBUS_STATUS_INVALID_ARGUMENT;
556 if (dev != iface->main_dev.dev)
557 return UBUS_STATUS_INVALID_ARGUMENT;
559 device_remove_user(&iface->main_dev);
564 interface_add_link(struct interface *iface, struct device *dev)
566 struct device *mdev = iface->main_dev.dev;
571 if (iface->main_dev.hotplug)
572 device_remove_user(&iface->main_dev);
575 if (mdev->hotplug_ops)
576 return mdev->hotplug_ops->add(mdev, dev);
578 return UBUS_STATUS_NOT_SUPPORTED;
581 interface_set_main_dev(iface, dev);
582 iface->main_dev.hotplug = true;
587 interface_set_up(struct interface *iface)
591 iface->autostart = true;
593 if (iface->state != IFS_DOWN)
596 interface_clear_errors(iface);
597 if (!iface->available) {
598 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
602 if (iface->main_dev.dev) {
603 ret = device_claim(&iface->main_dev);
608 iface->state = IFS_SETUP;
609 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
611 mark_interface_down(iface);
619 interface_set_down(struct interface *iface)
622 vlist_for_each_element(&interfaces, iface, node)
623 __interface_set_down(iface, false);
625 iface->autostart = false;
626 __interface_set_down(iface, false);
633 interface_start_pending(void)
635 struct interface *iface;
637 vlist_for_each_element(&interfaces, iface, node) {
638 if (iface->available && iface->autostart)
639 interface_set_up(iface);
644 set_config_state(struct interface *iface, enum interface_config_state s)
646 iface->config_state = s;
647 if (iface->state == IFS_DOWN)
648 interface_handle_config_change(iface);
650 __interface_set_down(iface, false);
654 interface_update_start(struct interface *iface)
656 interface_ip_update_start(&iface->proto_ip);
660 interface_update_complete(struct interface *iface)
662 interface_ip_update_complete(&iface->proto_ip);
666 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
668 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
669 vlist_simple_replace(&new->dns_search, &old->dns_search);
673 interface_change_config(struct interface *if_old, struct interface *if_new)
675 struct blob_attr *old_config = if_old->config;
676 const char *old_ifname = if_old->ifname;
677 const char *old_parent_ifname = if_old->parent_ifname;
678 const struct proto_handler *proto = if_old->proto_handler;
680 interface_clear_errors(if_old);
681 if_old->config = if_new->config;
682 if (!if_old->config_autostart && if_new->config_autostart)
683 if_old->autostart = true;
685 if_old->config_autostart = if_new->config_autostart;
686 if_old->ifname = if_new->ifname;
687 if_old->parent_ifname = if_new->parent_ifname;
688 if_old->proto_handler = if_new->proto_handler;
690 #define FIELD_CHANGED_STR(field) \
691 ((!!if_old->field != !!old_ ## field) || \
693 strcmp(old_ ## field, if_old->field) != 0))
695 if (FIELD_CHANGED_STR(parent_ifname)) {
696 if (if_old->parent_iface.iface)
697 interface_remove_user(&if_old->parent_iface);
701 if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
702 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
707 if (!proto->config_params)
708 D(INTERFACE, "No config parameters for interface '%s'\n",
710 else if (!config_check_equal(old_config, if_new->config,
711 proto->config_params)) {
712 D(INTERFACE, "Reload interface '%s because of config changes\n",
717 #define UPDATE(field) ({ \
718 bool __changed = (if_old->field != if_new->field); \
719 if_old->field = if_new->field; \
723 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
724 interface_ip_set_enabled(&if_old->config_ip, false);
725 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
726 interface_ip_set_enabled(&if_old->proto_ip, false);
727 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
730 UPDATE(proto_ip.no_dns);
731 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
732 interface_write_resolv_conf();
739 set_config_state(if_old, IFC_RELOAD);
741 if_new->config = NULL;
742 interface_cleanup(if_new, false);
748 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
749 struct vlist_node *node_old)
751 struct interface *if_old = container_of(node_old, struct interface, node);
752 struct interface *if_new = container_of(node_new, struct interface, node);
754 if (node_old && node_new) {
755 D(INTERFACE, "Update interface '%s'\n", if_new->name);
756 interface_change_config(if_old, if_new);
757 } else if (node_old) {
758 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
759 set_config_state(if_old, IFC_REMOVE);
760 } else if (node_new) {
761 D(INTERFACE, "Create interface '%s'\n", if_new->name);
762 proto_init_interface(if_new, if_new->config);
763 interface_claim_device(if_new);
764 netifd_ubus_add_interface(if_new);
770 interface_init_list(void)
772 vlist_init(&interfaces, avl_strcmp, interface_update);
773 interfaces.keep_old = true;
774 interfaces.no_delete = true;