8 #include "interface-ip.h"
14 struct vlist_tree interfaces;
20 IFACE_ATTR_DEFAULTROUTE,
25 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
26 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
27 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
28 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
29 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
30 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
33 const struct config_param_list interface_attr_list = {
34 .n_params = IFACE_ATTR_MAX,
35 .params = iface_attrs,
39 interface_clear_errors(struct interface *iface)
41 struct interface_error *error, *tmp;
43 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
44 list_del(&error->list);
49 void interface_add_error(struct interface *iface, const char *subsystem,
50 const char *code, const char **data, int n_data)
52 struct interface_error *error;
58 len = n_data * sizeof(char *);
59 datalen = alloca(len);
60 for (i = 0; i < n_data; i++) {
61 datalen[i] = strlen(data[i]) + 1;
66 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
70 list_add_tail(&error->list, &iface->errors);
71 error->subsystem = subsystem;
74 dest = (char *) &error->data[n_data + 1];
75 for (i = 0; i < n_data; i++) {
76 error->data[i] = dest;
77 memcpy(dest, data[i], datalen[i]);
80 error->data[n_data] = NULL;
84 interface_data_del(struct interface *iface, struct interface_data *data)
86 avl_delete(&iface->data, &data->node);
91 interface_data_flush(struct interface *iface)
93 struct interface_data *d, *tmp;
95 avl_for_each_element_safe(&iface->data, d, node, tmp)
96 interface_data_del(iface, d);
100 interface_add_data(struct interface *iface, const struct blob_attr *data)
102 struct interface_data *n, *o;
104 if (!blobmsg_check_attr(data, true))
105 return UBUS_STATUS_INVALID_ARGUMENT;
107 n = calloc(1, sizeof(*data) + blob_pad_len(data));
108 memcpy(n->data, data, blob_pad_len(data));
109 n->node.key = blobmsg_name(data);
111 o = avl_find_element(&iface->data, n->node.key, o, node);
113 interface_data_del(iface, o);
115 avl_insert(&iface->data, &n->node);
120 interface_event(struct interface *iface, enum interface_event ev)
122 struct interface_user *dep, *tmp;
124 list_for_each_entry_safe(dep, tmp, &iface->users, list)
125 dep->cb(dep, IFEV_UP);
127 interface_queue_event(iface, ev);
131 interface_flush_state(struct interface *iface)
133 if (iface->main_dev.dev)
134 device_release(&iface->main_dev);
135 if (iface->l3_dev.dev)
136 device_release(&iface->l3_dev);
137 interface_data_flush(iface);
141 mark_interface_down(struct interface *iface)
143 if (iface->state == IFS_UP)
144 interface_event(iface, IFEV_DOWN);
145 interface_ip_set_enabled(&iface->config_ip, false);
146 interface_ip_flush(&iface->proto_ip);
147 interface_flush_state(iface);
148 iface->state = IFS_DOWN;
152 __interface_set_down(struct interface *iface, bool force)
154 interface_clear_errors(iface);
156 if (iface->state == IFS_DOWN ||
157 iface->state == IFS_TEARDOWN)
160 if (iface->state == IFS_UP)
161 interface_event(iface, IFEV_DOWN);
162 iface->state = IFS_TEARDOWN;
163 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
165 interface_flush_state(iface);
169 interface_cb(struct device_user *dep, enum device_event ev)
171 struct interface *iface;
174 iface = container_of(dep, struct interface, main_dev);
179 case DEV_EVENT_REMOVE:
186 interface_set_available(iface, new_state);
190 interface_set_available(struct interface *iface, bool new_state)
192 if (iface->available == new_state)
195 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
196 iface->available = new_state;
199 if (iface->autostart && !config_init)
200 interface_set_up(iface);
202 __interface_set_down(iface, true);
206 interface_add_user(struct interface_user *dep, struct interface *iface)
209 list_add(&dep->list, &iface->users);
210 if (iface->state == IFS_UP)
211 dep->cb(dep, IFEV_UP);
215 interface_remove_user(struct interface_user *dep)
217 list_del_init(&dep->list);
222 interface_claim_device(struct interface *iface)
227 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
228 dev = device_get(iface->ifname, true);
230 interface_set_main_dev(iface, dev);
232 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
233 interface_set_available(iface, true);
238 interface_cleanup(struct interface *iface, bool reload)
240 struct interface_user *dep, *tmp;
242 list_for_each_entry_safe(dep, tmp, &iface->users, list)
243 interface_remove_user(dep);
245 interface_dequeue_event(iface);
246 interface_ip_flush(&iface->config_ip);
247 interface_flush_state(iface);
248 interface_clear_errors(iface);
249 if (iface->main_dev.dev &&
250 (!reload || !iface->main_dev.hotplug))
251 interface_set_main_dev(iface, NULL);
252 interface_set_proto_state(iface, NULL);
256 interface_do_free(struct interface *iface)
258 interface_cleanup(iface, false);
260 netifd_ubus_remove_interface(iface);
261 avl_delete(&interfaces.avl, &iface->node.avl);
266 interface_do_reload(struct interface *iface)
268 interface_cleanup(iface, true);
269 proto_init_interface(iface, iface->config);
270 interface_claim_device(iface);
274 interface_handle_config_change(struct interface *iface)
276 switch(iface->config_state) {
280 interface_do_reload(iface);
283 interface_do_free(iface);
286 if (iface->autostart && iface->available)
287 interface_set_up(iface);
291 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
293 struct interface *iface = state->iface;
297 if (iface->state != IFS_SETUP)
300 interface_ip_set_enabled(&iface->config_ip, true);
301 system_flush_routes();
302 iface->state = IFS_UP;
303 iface->start_time = system_get_rtime();
304 interface_event(iface, IFEV_UP);
305 interface_write_resolv_conf();
306 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
309 if (iface->state == IFS_DOWN)
312 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
313 mark_interface_down(iface);
314 system_flush_routes();
315 interface_handle_config_change(iface);
317 case IFPEV_LINK_LOST:
318 if (iface->state != IFS_UP)
321 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
322 mark_interface_down(iface);
323 system_flush_routes();
324 iface->state = IFS_SETUP;
329 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
332 iface->proto->free(iface->proto);
335 iface->state = IFS_DOWN;
336 iface->proto = state;
340 state->proto_event = interface_proto_cb;
341 state->iface = iface;
345 interface_init(struct interface *iface, const char *name,
346 struct blob_attr *config)
348 struct blob_attr *tb[IFACE_ATTR_MAX];
349 struct blob_attr *cur;
350 const char *proto_name = NULL;
352 strncpy(iface->name, name, sizeof(iface->name) - 1);
353 INIT_LIST_HEAD(&iface->errors);
354 INIT_LIST_HEAD(&iface->users);
355 INIT_LIST_HEAD(&iface->hotplug_list);
356 interface_ip_init(&iface->proto_ip, iface);
357 interface_ip_init(&iface->config_ip, iface);
358 avl_init(&iface->data, avl_strcmp, false, NULL);
359 iface->config_ip.enabled = false;
361 iface->main_dev.cb = interface_cb;
363 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
364 blob_data(config), blob_len(config));
366 if ((cur = tb[IFACE_ATTR_PROTO]))
367 proto_name = blobmsg_data(cur);
369 proto_attach_interface(iface, proto_name);
371 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
372 iface->proto_ip.no_defaultroute =
373 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
375 iface->config_autostart = iface->autostart;
379 interface_add(struct interface *iface, struct blob_attr *config)
381 struct blob_attr *tb[IFACE_ATTR_MAX];
382 struct blob_attr *cur;
384 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
385 blob_data(config), blob_len(config));
387 if ((cur = tb[IFACE_ATTR_IFNAME]))
388 iface->ifname = blobmsg_data(cur);
390 iface->config = config;
391 vlist_add(&interfaces, &iface->node, iface->name);
395 interface_set_l3_dev(struct interface *iface, struct device *dev)
397 bool enabled = iface->config_ip.enabled;
398 bool claimed = iface->l3_dev.claimed;
400 if (iface->l3_dev.dev == dev)
403 interface_ip_set_enabled(&iface->config_ip, false);
404 interface_ip_flush(&iface->proto_ip);
405 device_add_user(&iface->l3_dev, dev);
409 device_claim(&iface->l3_dev);
410 interface_ip_set_enabled(&iface->config_ip, enabled);
415 interface_set_main_dev(struct interface *iface, struct device *dev)
417 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
418 bool claimed = iface->l3_dev.claimed;
420 if (iface->main_dev.dev == dev)
424 interface_set_l3_dev(iface, dev);
426 device_add_user(&iface->main_dev, dev);
428 device_claim(&iface->l3_dev);
430 if (!iface->l3_dev.dev)
431 interface_set_l3_dev(iface, dev);
435 interface_remove_link(struct interface *iface, struct device *dev)
437 struct device *mdev = iface->main_dev.dev;
439 if (mdev && mdev->hotplug_ops)
440 return mdev->hotplug_ops->del(mdev, dev);
442 if (!iface->main_dev.hotplug)
443 return UBUS_STATUS_INVALID_ARGUMENT;
445 if (dev != iface->main_dev.dev)
446 return UBUS_STATUS_INVALID_ARGUMENT;
448 device_remove_user(&iface->main_dev);
453 interface_add_link(struct interface *iface, struct device *dev)
455 struct device *mdev = iface->main_dev.dev;
460 if (iface->main_dev.hotplug)
461 device_remove_user(&iface->main_dev);
464 if (mdev->hotplug_ops)
465 return mdev->hotplug_ops->add(mdev, dev);
467 return UBUS_STATUS_NOT_SUPPORTED;
470 interface_set_main_dev(iface, dev);
471 iface->main_dev.hotplug = true;
476 interface_set_up(struct interface *iface)
480 iface->autostart = true;
482 if (iface->state != IFS_DOWN)
485 interface_clear_errors(iface);
486 if (!iface->available) {
487 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
491 if (iface->main_dev.dev) {
492 ret = device_claim(&iface->main_dev);
497 iface->state = IFS_SETUP;
498 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
500 mark_interface_down(iface);
508 interface_set_down(struct interface *iface)
511 vlist_for_each_element(&interfaces, iface, node)
512 __interface_set_down(iface, false);
514 iface->autostart = false;
515 __interface_set_down(iface, false);
522 interface_start_pending(void)
524 struct interface *iface;
526 vlist_for_each_element(&interfaces, iface, node) {
527 if (iface->available && iface->autostart)
528 interface_set_up(iface);
533 set_config_state(struct interface *iface, enum interface_config_state s)
535 iface->config_state = s;
536 if (iface->state == IFS_DOWN)
537 interface_handle_config_change(iface);
539 __interface_set_down(iface, false);
543 interface_update_start(struct interface *iface)
545 interface_ip_update_start(&iface->proto_ip);
549 interface_update_complete(struct interface *iface)
551 interface_ip_update_complete(&iface->proto_ip);
555 interface_change_config(struct interface *if_old, struct interface *if_new)
557 struct blob_attr *old_config = if_old->config;
558 const char *old_ifname = if_old->ifname;
559 const struct proto_handler *proto = if_old->proto_handler;
561 interface_clear_errors(if_old);
562 if_old->config = if_new->config;
563 if (!if_old->config_autostart && if_new->config_autostart)
564 if_old->autostart = true;
566 if_old->config_autostart = if_new->config_autostart;
567 if_old->ifname = if_new->ifname;
568 if_old->proto_handler = if_new->proto_handler;
570 if ((!!old_ifname != !!if_new->ifname) ||
571 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
572 proto != if_new->proto_handler) {
573 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
578 if (!proto->config_params)
579 D(INTERFACE, "No config parameters for interface '%s'\n",
581 else if (!config_check_equal(old_config, if_new->config,
582 proto->config_params)) {
583 D(INTERFACE, "Reload interface '%s because of config changes\n",
588 #define UPDATE(field) ({ \
589 bool __changed = (if_old->field != if_new->field); \
590 if_old->field = if_new->field; \
594 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
595 interface_ip_set_enabled(&if_old->config_ip, false);
596 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
597 interface_ip_set_enabled(&if_old->proto_ip, false);
598 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
606 set_config_state(if_old, IFC_RELOAD);
613 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
614 struct vlist_node *node_old)
616 struct interface *if_old = container_of(node_old, struct interface, node);
617 struct interface *if_new = container_of(node_new, struct interface, node);
619 if (node_old && node_new) {
620 D(INTERFACE, "Update interface '%s'\n", if_new->name);
621 interface_change_config(if_old, if_new);
622 } else if (node_old) {
623 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
624 set_config_state(if_old, IFC_REMOVE);
625 } else if (node_new) {
626 D(INTERFACE, "Create interface '%s'\n", if_new->name);
627 proto_init_interface(if_new, if_new->config);
628 interface_claim_device(if_new);
629 netifd_ubus_add_interface(if_new);
635 interface_init_list(void)
637 vlist_init(&interfaces, avl_strcmp, interface_update);
638 interfaces.keep_old = true;
639 interfaces.no_delete = true;