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,
39 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
40 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
41 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
42 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
43 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
44 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
47 const struct config_param_list interface_attr_list = {
48 .n_params = IFACE_ATTR_MAX,
49 .params = iface_attrs,
53 interface_clear_errors(struct interface *iface)
55 struct interface_error *error, *tmp;
57 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
58 list_del(&error->list);
63 void interface_add_error(struct interface *iface, const char *subsystem,
64 const char *code, const char **data, int n_data)
66 struct interface_error *error;
72 len = n_data * sizeof(char *);
73 datalen = alloca(len);
74 for (i = 0; i < n_data; i++) {
75 datalen[i] = strlen(data[i]) + 1;
80 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
84 list_add_tail(&error->list, &iface->errors);
85 error->subsystem = subsystem;
88 dest = (char *) &error->data[n_data + 1];
89 for (i = 0; i < n_data; i++) {
90 error->data[i] = dest;
91 memcpy(dest, data[i], datalen[i]);
94 error->data[n_data] = NULL;
98 interface_data_del(struct interface *iface, struct interface_data *data)
100 avl_delete(&iface->data, &data->node);
105 interface_data_flush(struct interface *iface)
107 struct interface_data *d, *tmp;
109 avl_for_each_element_safe(&iface->data, d, node, tmp)
110 interface_data_del(iface, d);
114 interface_add_data(struct interface *iface, const struct blob_attr *data)
116 struct interface_data *n, *o;
118 if (!blobmsg_check_attr(data, true))
119 return UBUS_STATUS_INVALID_ARGUMENT;
121 n = calloc(1, sizeof(*n) + blob_pad_len(data));
122 memcpy(n->data, data, blob_pad_len(data));
123 n->node.key = blobmsg_name(data);
125 o = avl_find_element(&iface->data, n->node.key, o, node);
127 interface_data_del(iface, o);
129 avl_insert(&iface->data, &n->node);
134 interface_event(struct interface *iface, enum interface_event ev)
136 struct interface_user *dep, *tmp;
138 list_for_each_entry_safe(dep, tmp, &iface->users, list)
139 dep->cb(dep, iface, ev);
141 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
142 dep->cb(dep, iface, ev);
146 interface_flush_state(struct interface *iface)
148 if (iface->main_dev.dev)
149 device_release(&iface->main_dev);
150 if (iface->l3_dev.dev)
151 device_release(&iface->l3_dev);
152 interface_data_flush(iface);
156 mark_interface_down(struct interface *iface)
158 if (iface->state == IFS_UP)
159 interface_event(iface, IFEV_DOWN);
160 interface_ip_set_enabled(&iface->config_ip, false);
161 interface_ip_flush(&iface->proto_ip);
162 interface_flush_state(iface);
163 system_flush_routes();
164 iface->state = IFS_DOWN;
168 __interface_set_down(struct interface *iface, bool force)
170 interface_clear_errors(iface);
172 if (iface->state == IFS_DOWN ||
173 iface->state == IFS_TEARDOWN)
176 if (iface->state == IFS_UP)
177 interface_event(iface, IFEV_DOWN);
178 iface->state = IFS_TEARDOWN;
179 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
181 interface_flush_state(iface);
185 interface_cb(struct device_user *dep, enum device_event ev)
187 struct interface *iface;
190 iface = container_of(dep, struct interface, main_dev);
195 case DEV_EVENT_REMOVE:
202 interface_set_available(iface, new_state);
206 interface_set_available(struct interface *iface, bool new_state)
208 if (iface->available == new_state)
211 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
212 iface->available = new_state;
215 if (iface->autostart && !config_init)
216 interface_set_up(iface);
218 __interface_set_down(iface, true);
222 interface_add_user(struct interface_user *dep, struct interface *iface)
225 list_add(&dep->list, &iface_all_users);
230 list_add(&dep->list, &iface->users);
231 if (iface->state == IFS_UP)
232 dep->cb(dep, iface, IFEV_UP);
236 interface_remove_user(struct interface_user *dep)
238 list_del_init(&dep->list);
243 interface_claim_device(struct interface *iface)
248 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
249 dev = device_get(iface->ifname, true);
251 interface_set_main_dev(iface, dev);
253 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
254 interface_set_available(iface, true);
259 interface_cleanup(struct interface *iface, bool reload)
261 struct interface_user *dep, *tmp;
263 list_for_each_entry_safe(dep, tmp, &iface->users, list)
264 interface_remove_user(dep);
266 interface_ip_flush(&iface->config_ip);
267 interface_flush_state(iface);
268 interface_clear_errors(iface);
269 if (iface->main_dev.dev &&
270 (!reload || !iface->main_dev.hotplug))
271 interface_set_main_dev(iface, NULL);
272 interface_set_proto_state(iface, NULL);
276 interface_do_free(struct interface *iface)
278 interface_event(iface, IFEV_FREE);
279 interface_cleanup(iface, false);
281 netifd_ubus_remove_interface(iface);
282 avl_delete(&interfaces.avl, &iface->node.avl);
287 interface_do_reload(struct interface *iface)
289 interface_event(iface, IFEV_RELOAD);
290 interface_cleanup(iface, true);
291 proto_init_interface(iface, iface->config);
292 interface_claim_device(iface);
296 interface_handle_config_change(struct interface *iface)
298 switch(iface->config_state) {
302 interface_do_reload(iface);
305 interface_do_free(iface);
308 if (iface->autostart && iface->available)
309 interface_set_up(iface);
313 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
315 struct interface *iface = state->iface;
319 if (iface->state != IFS_SETUP)
322 interface_ip_set_enabled(&iface->config_ip, true);
323 system_flush_routes();
324 iface->state = IFS_UP;
325 iface->start_time = system_get_rtime();
326 interface_event(iface, IFEV_UP);
327 interface_write_resolv_conf();
328 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
331 if (iface->state == IFS_DOWN)
334 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
335 mark_interface_down(iface);
336 interface_handle_config_change(iface);
338 case IFPEV_LINK_LOST:
339 if (iface->state != IFS_UP)
342 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
343 mark_interface_down(iface);
344 iface->state = IFS_SETUP;
349 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
352 iface->proto->free(iface->proto);
355 iface->state = IFS_DOWN;
356 iface->proto = state;
360 state->proto_event = interface_proto_cb;
361 state->iface = iface;
365 interface_init(struct interface *iface, const char *name,
366 struct blob_attr *config)
368 struct blob_attr *tb[IFACE_ATTR_MAX];
369 struct blob_attr *cur;
370 const char *proto_name = NULL;
372 strncpy(iface->name, name, sizeof(iface->name) - 1);
373 INIT_LIST_HEAD(&iface->errors);
374 INIT_LIST_HEAD(&iface->users);
375 INIT_LIST_HEAD(&iface->hotplug_list);
376 interface_ip_init(iface);
377 avl_init(&iface->data, avl_strcmp, false, NULL);
378 iface->config_ip.enabled = false;
380 iface->main_dev.cb = interface_cb;
382 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
383 blob_data(config), blob_len(config));
385 if ((cur = tb[IFACE_ATTR_PROTO]))
386 proto_name = blobmsg_data(cur);
388 proto_attach_interface(iface, proto_name);
390 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
391 iface->proto_ip.no_defaultroute =
392 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
394 iface->config_autostart = iface->autostart;
398 interface_add(struct interface *iface, struct blob_attr *config)
400 struct blob_attr *tb[IFACE_ATTR_MAX];
401 struct blob_attr *cur;
403 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
404 blob_data(config), blob_len(config));
406 if ((cur = tb[IFACE_ATTR_IFNAME]))
407 iface->ifname = blobmsg_data(cur);
409 iface->config = config;
410 vlist_add(&interfaces, &iface->node, iface->name);
414 interface_set_l3_dev(struct interface *iface, struct device *dev)
416 bool enabled = iface->config_ip.enabled;
417 bool claimed = iface->l3_dev.claimed;
419 if (iface->l3_dev.dev == dev)
422 interface_ip_set_enabled(&iface->config_ip, false);
423 interface_ip_flush(&iface->proto_ip);
424 device_add_user(&iface->l3_dev, dev);
428 device_claim(&iface->l3_dev);
429 interface_ip_set_enabled(&iface->config_ip, enabled);
434 interface_set_main_dev(struct interface *iface, struct device *dev)
436 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
437 bool claimed = iface->l3_dev.claimed;
439 if (iface->main_dev.dev == dev)
443 interface_set_l3_dev(iface, dev);
445 device_add_user(&iface->main_dev, dev);
447 device_claim(&iface->l3_dev);
449 if (!iface->l3_dev.dev)
450 interface_set_l3_dev(iface, dev);
454 interface_remove_link(struct interface *iface, struct device *dev)
456 struct device *mdev = iface->main_dev.dev;
458 if (mdev && mdev->hotplug_ops)
459 return mdev->hotplug_ops->del(mdev, dev);
461 if (!iface->main_dev.hotplug)
462 return UBUS_STATUS_INVALID_ARGUMENT;
464 if (dev != iface->main_dev.dev)
465 return UBUS_STATUS_INVALID_ARGUMENT;
467 device_remove_user(&iface->main_dev);
472 interface_add_link(struct interface *iface, struct device *dev)
474 struct device *mdev = iface->main_dev.dev;
479 if (iface->main_dev.hotplug)
480 device_remove_user(&iface->main_dev);
483 if (mdev->hotplug_ops)
484 return mdev->hotplug_ops->add(mdev, dev);
486 return UBUS_STATUS_NOT_SUPPORTED;
489 interface_set_main_dev(iface, dev);
490 iface->main_dev.hotplug = true;
495 interface_set_up(struct interface *iface)
499 iface->autostart = true;
501 if (iface->state != IFS_DOWN)
504 interface_clear_errors(iface);
505 if (!iface->available) {
506 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
510 if (iface->main_dev.dev) {
511 ret = device_claim(&iface->main_dev);
516 iface->state = IFS_SETUP;
517 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
519 mark_interface_down(iface);
527 interface_set_down(struct interface *iface)
530 vlist_for_each_element(&interfaces, iface, node)
531 __interface_set_down(iface, false);
533 iface->autostart = false;
534 __interface_set_down(iface, false);
541 interface_start_pending(void)
543 struct interface *iface;
545 vlist_for_each_element(&interfaces, iface, node) {
546 if (iface->available && iface->autostart)
547 interface_set_up(iface);
552 set_config_state(struct interface *iface, enum interface_config_state s)
554 iface->config_state = s;
555 if (iface->state == IFS_DOWN)
556 interface_handle_config_change(iface);
558 __interface_set_down(iface, false);
562 interface_update_start(struct interface *iface)
564 interface_ip_update_start(&iface->proto_ip);
568 interface_update_complete(struct interface *iface)
570 interface_ip_update_complete(&iface->proto_ip);
574 interface_change_config(struct interface *if_old, struct interface *if_new)
576 struct blob_attr *old_config = if_old->config;
577 const char *old_ifname = if_old->ifname;
578 const struct proto_handler *proto = if_old->proto_handler;
580 interface_clear_errors(if_old);
581 if_old->config = if_new->config;
582 if (!if_old->config_autostart && if_new->config_autostart)
583 if_old->autostart = true;
585 if_old->config_autostart = if_new->config_autostart;
586 if_old->ifname = if_new->ifname;
587 if_old->proto_handler = if_new->proto_handler;
589 if ((!!old_ifname != !!if_new->ifname) ||
590 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
591 proto != if_new->proto_handler) {
592 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
597 if (!proto->config_params)
598 D(INTERFACE, "No config parameters for interface '%s'\n",
600 else if (!config_check_equal(old_config, if_new->config,
601 proto->config_params)) {
602 D(INTERFACE, "Reload interface '%s because of config changes\n",
607 #define UPDATE(field) ({ \
608 bool __changed = (if_old->field != if_new->field); \
609 if_old->field = if_new->field; \
613 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
614 interface_ip_set_enabled(&if_old->config_ip, false);
615 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
616 interface_ip_set_enabled(&if_old->proto_ip, false);
617 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
625 set_config_state(if_old, IFC_RELOAD);
632 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
633 struct vlist_node *node_old)
635 struct interface *if_old = container_of(node_old, struct interface, node);
636 struct interface *if_new = container_of(node_new, struct interface, node);
638 if (node_old && node_new) {
639 D(INTERFACE, "Update interface '%s'\n", if_new->name);
640 interface_change_config(if_old, if_new);
641 } else if (node_old) {
642 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
643 set_config_state(if_old, IFC_REMOVE);
644 } else if (node_new) {
645 D(INTERFACE, "Create interface '%s'\n", if_new->name);
646 proto_init_interface(if_new, if_new->config);
647 interface_claim_device(if_new);
648 netifd_ubus_add_interface(if_new);
654 interface_init_list(void)
656 vlist_init(&interfaces, avl_strcmp, interface_update);
657 interfaces.keep_old = true;
658 interfaces.no_delete = true;