8 #include "interface-ip.h"
14 struct vlist_tree interfaces;
20 IFACE_ATTR_DEFAULTROUTE,
24 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
25 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
26 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
27 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
28 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
31 const struct config_param_list interface_attr_list = {
32 .n_params = IFACE_ATTR_MAX,
33 .params = iface_attrs,
37 interface_clear_errors(struct interface *iface)
39 struct interface_error *error, *tmp;
41 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
42 list_del(&error->list);
47 void interface_add_error(struct interface *iface, const char *subsystem,
48 const char *code, const char **data, int n_data)
50 struct interface_error *error;
56 len = n_data * sizeof(char *);
57 datalen = alloca(len);
58 for (i = 0; i < n_data; i++) {
59 datalen[i] = strlen(data[i]) + 1;
64 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
68 list_add_tail(&error->list, &iface->errors);
69 error->subsystem = subsystem;
72 dest = (char *) &error->data[n_data + 1];
73 for (i = 0; i < n_data; i++) {
74 error->data[i] = dest;
75 memcpy(dest, data[i], datalen[i]);
78 error->data[n_data] = NULL;
82 interface_event(struct interface *iface, enum interface_event ev)
84 struct interface_user *dep, *tmp;
86 list_for_each_entry_safe(dep, tmp, &iface->users, list)
87 dep->cb(dep, IFEV_UP);
89 interface_queue_event(iface, ev);
93 interface_flush_state(struct interface *iface)
95 interface_ip_flush(&iface->proto_ip);
96 if (iface->main_dev.dev)
97 device_release(&iface->main_dev);
98 if (iface->l3_dev != &iface->main_dev && iface->l3_dev->dev)
99 device_release(iface->l3_dev);
103 mark_interface_down(struct interface *iface)
105 if (iface->state == IFS_UP)
106 interface_event(iface, IFEV_DOWN);
107 interface_flush_state(iface);
108 iface->state = IFS_DOWN;
112 __interface_set_down(struct interface *iface, bool force)
114 interface_clear_errors(iface);
116 if (iface->state == IFS_DOWN ||
117 iface->state == IFS_TEARDOWN)
120 if (iface->state == IFS_UP)
121 interface_event(iface, IFEV_DOWN);
122 iface->state = IFS_TEARDOWN;
123 interface_ip_flush(&iface->config_ip);
124 interface_ip_flush(&iface->proto_ip);
125 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
127 interface_flush_state(iface);
131 interface_cb(struct device_user *dep, enum device_event ev)
133 struct interface *iface;
136 iface = container_of(dep, struct interface, main_dev);
141 case DEV_EVENT_REMOVE:
148 interface_set_available(iface, new_state);
152 interface_set_available(struct interface *iface, bool new_state)
154 if (iface->available == new_state)
157 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
158 iface->available = new_state;
161 if (iface->autostart && !config_init)
162 interface_set_up(iface);
164 __interface_set_down(iface, true);
168 interface_add_user(struct interface_user *dep, struct interface *iface)
171 list_add(&dep->list, &iface->users);
172 if (iface->state == IFS_UP)
173 dep->cb(dep, IFEV_UP);
177 interface_remove_user(struct interface_user *dep)
179 list_del_init(&dep->list);
184 interface_claim_device(struct interface *iface)
189 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
190 dev = device_get(iface->ifname, true);
192 device_add_user(&iface->main_dev, dev);
194 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
195 interface_set_available(iface, true);
200 interface_cleanup(struct interface *iface, bool reload)
202 struct interface_user *dep, *tmp;
204 list_for_each_entry_safe(dep, tmp, &iface->users, list)
205 interface_remove_user(dep);
207 interface_ip_flush(&iface->config_ip);
208 interface_flush_state(iface);
209 interface_clear_errors(iface);
210 if (iface->main_dev.dev &&
211 (!reload || !iface->main_dev.hotplug))
212 device_remove_user(&iface->main_dev);
213 iface->l3_dev = &iface->main_dev;
214 interface_set_proto_state(iface, NULL);
218 interface_do_free(struct interface *iface)
220 interface_cleanup(iface, false);
222 netifd_ubus_remove_interface(iface);
223 avl_delete(&interfaces.avl, &iface->node.avl);
228 interface_do_reload(struct interface *iface)
230 interface_cleanup(iface, true);
231 proto_init_interface(iface, iface->config);
232 interface_claim_device(iface);
236 interface_handle_config_change(struct interface *iface)
238 switch(iface->config_state) {
242 interface_do_reload(iface);
245 interface_do_free(iface);
248 if (iface->autostart && iface->available)
249 interface_set_up(iface);
253 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
255 struct interface *iface = state->iface;
259 if (iface->state != IFS_SETUP)
262 interface_ip_set_enabled(&iface->config_ip, true);
263 system_flush_routes();
264 iface->state = IFS_UP;
265 iface->start_time = system_get_rtime();
266 interface_event(iface, IFEV_UP);
267 interface_write_resolv_conf();
268 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
271 if (iface->state == IFS_DOWN)
274 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
275 interface_ip_set_enabled(&iface->config_ip, false);
276 system_flush_routes();
277 mark_interface_down(iface);
278 interface_handle_config_change(iface);
280 case IFPEV_LINK_LOST:
281 if (iface->state != IFS_UP)
284 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
285 if (iface->state == IFS_UP)
286 interface_event(iface, IFEV_DOWN);
287 iface->state = IFS_SETUP;
292 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
295 iface->proto->free(iface->proto);
298 iface->state = IFS_DOWN;
299 iface->proto = state;
303 state->proto_event = interface_proto_cb;
304 state->iface = iface;
308 interface_init(struct interface *iface, const char *name,
309 struct blob_attr *config)
311 struct blob_attr *tb[IFACE_ATTR_MAX];
312 struct blob_attr *cur;
313 const char *proto_name = NULL;
315 strncpy(iface->name, name, sizeof(iface->name) - 1);
316 INIT_LIST_HEAD(&iface->errors);
317 INIT_LIST_HEAD(&iface->users);
318 INIT_LIST_HEAD(&iface->hotplug_list);
319 interface_ip_init(&iface->proto_ip, iface);
320 interface_ip_init(&iface->config_ip, iface);
321 iface->config_ip.enabled = false;
323 iface->main_dev.cb = interface_cb;
324 iface->l3_dev = &iface->main_dev;
326 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
327 blob_data(config), blob_len(config));
329 if ((cur = tb[IFACE_ATTR_PROTO]))
330 proto_name = blobmsg_data(cur);
332 proto_attach_interface(iface, proto_name);
334 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
335 iface->proto_ip.no_defaultroute =
336 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
338 iface->config_autostart = iface->autostart;
342 interface_add(struct interface *iface, struct blob_attr *config)
344 struct blob_attr *tb[IFACE_ATTR_MAX];
345 struct blob_attr *cur;
347 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
348 blob_data(config), blob_len(config));
350 if ((cur = tb[IFACE_ATTR_IFNAME]))
351 iface->ifname = blobmsg_data(cur);
353 iface->config = config;
354 vlist_add(&interfaces, &iface->node, iface->name);
358 interface_remove_link(struct interface *iface, struct device *dev)
360 struct device *mdev = iface->main_dev.dev;
362 if (mdev && mdev->hotplug_ops)
363 return mdev->hotplug_ops->del(mdev, dev);
365 if (!iface->main_dev.hotplug)
366 return UBUS_STATUS_INVALID_ARGUMENT;
368 if (dev != iface->main_dev.dev)
369 return UBUS_STATUS_INVALID_ARGUMENT;
371 device_remove_user(&iface->main_dev);
376 interface_add_link(struct interface *iface, struct device *dev)
378 struct device *mdev = iface->main_dev.dev;
383 if (iface->main_dev.hotplug)
384 device_remove_user(&iface->main_dev);
387 if (mdev->hotplug_ops)
388 return mdev->hotplug_ops->add(mdev, dev);
390 return UBUS_STATUS_NOT_SUPPORTED;
393 device_add_user(&iface->main_dev, dev);
394 iface->main_dev.hotplug = true;
399 interface_set_up(struct interface *iface)
403 iface->autostart = true;
405 if (iface->state != IFS_DOWN)
408 interface_clear_errors(iface);
409 if (!iface->available) {
410 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
414 if (iface->main_dev.dev) {
415 ret = device_claim(&iface->main_dev);
420 iface->state = IFS_SETUP;
421 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
423 mark_interface_down(iface);
431 interface_set_down(struct interface *iface)
434 vlist_for_each_element(&interfaces, iface, node)
435 __interface_set_down(iface, false);
437 iface->autostart = false;
438 __interface_set_down(iface, false);
445 interface_start_pending(void)
447 struct interface *iface;
449 vlist_for_each_element(&interfaces, iface, node) {
450 if (iface->available && iface->autostart)
451 interface_set_up(iface);
456 set_config_state(struct interface *iface, enum interface_config_state s)
458 iface->config_state = s;
459 if (iface->state == IFS_DOWN)
460 interface_handle_config_change(iface);
462 __interface_set_down(iface, false);
466 interface_update_start(struct interface *iface)
468 interface_ip_update_start(&iface->proto_ip);
472 interface_update_complete(struct interface *iface)
474 struct device_route *route;
476 interface_ip_update_complete(&iface->proto_ip);
477 vlist_for_each_element(&iface->config_ip.route, route, node) {
478 if (iface->l3_dev->dev) {
479 system_add_route(iface->l3_dev->dev, route);
480 route->enabled = true;
486 interface_change_config(struct interface *if_old, struct interface *if_new)
488 struct blob_attr *old_config = if_old->config;
489 const char *old_ifname = if_old->ifname;
490 const struct proto_handler *proto = if_old->proto_handler;
492 interface_clear_errors(if_old);
493 if_old->config = if_new->config;
494 if (!if_old->config_autostart && if_new->config_autostart)
495 if_old->autostart = true;
497 if_old->config_autostart = if_new->config_autostart;
498 if_old->ifname = if_new->ifname;
499 if_old->proto_handler = if_new->proto_handler;
501 if ((!!old_ifname != !!if_new->ifname) ||
502 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
503 proto != if_new->proto_handler) {
504 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
509 if (!proto->config_params)
510 D(INTERFACE, "No config parameters for interface '%s'\n",
512 else if (!config_check_equal(old_config, if_new->config,
513 proto->config_params)) {
514 D(INTERFACE, "Reload interface '%s because of config changes\n",
519 if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
520 if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
521 interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
527 set_config_state(if_old, IFC_RELOAD);
534 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
535 struct vlist_node *node_old)
537 struct interface *if_old = container_of(node_old, struct interface, node);
538 struct interface *if_new = container_of(node_new, struct interface, node);
540 if (node_old && node_new) {
541 D(INTERFACE, "Update interface '%s'\n", if_new->name);
542 interface_change_config(if_old, if_new);
543 } else if (node_old) {
544 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
545 set_config_state(if_old, IFC_REMOVE);
546 } else if (node_new) {
547 D(INTERFACE, "Create interface '%s'\n", if_new->name);
548 proto_init_interface(if_new, if_new->config);
549 interface_claim_device(if_new);
550 netifd_ubus_add_interface(if_new);
556 interface_init_list(void)
558 vlist_init(&interfaces, avl_strcmp, interface_update);
559 interfaces.keep_old = true;
560 interfaces.no_delete = true;