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_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
125 interface_flush_state(iface);
129 interface_cb(struct device_user *dep, enum device_event ev)
131 struct interface *iface;
134 iface = container_of(dep, struct interface, main_dev);
139 case DEV_EVENT_REMOVE:
146 interface_set_available(iface, new_state);
150 interface_set_available(struct interface *iface, bool new_state)
152 if (iface->available == new_state)
155 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
156 iface->available = new_state;
159 if (iface->autostart && !config_init)
160 interface_set_up(iface);
162 __interface_set_down(iface, true);
166 interface_add_user(struct interface_user *dep, struct interface *iface)
169 list_add(&dep->list, &iface->users);
170 if (iface->state == IFS_UP)
171 dep->cb(dep, IFEV_UP);
175 interface_remove_user(struct interface_user *dep)
177 list_del_init(&dep->list);
182 interface_claim_device(struct interface *iface)
187 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
188 dev = device_get(iface->ifname, true);
190 device_add_user(&iface->main_dev, dev);
192 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
193 interface_set_available(iface, true);
198 interface_cleanup(struct interface *iface)
200 struct interface_user *dep, *tmp;
202 list_for_each_entry_safe(dep, tmp, &iface->users, list)
203 interface_remove_user(dep);
205 interface_ip_flush(&iface->config_ip);
206 interface_flush_state(iface);
207 interface_clear_errors(iface);
208 if (iface->main_dev.dev)
209 device_remove_user(&iface->main_dev);
210 iface->l3_dev = &iface->main_dev;
211 interface_set_proto_state(iface, NULL);
215 interface_do_free(struct interface *iface)
217 interface_cleanup(iface);
219 netifd_ubus_remove_interface(iface);
220 avl_delete(&interfaces.avl, &iface->node.avl);
225 interface_do_reload(struct interface *iface)
227 interface_cleanup(iface);
228 proto_init_interface(iface, iface->config);
229 interface_claim_device(iface);
233 interface_handle_config_change(struct interface *iface)
235 switch(iface->config_state) {
239 interface_do_reload(iface);
242 interface_do_free(iface);
245 if (iface->autostart && iface->available)
246 interface_set_up(iface);
250 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
252 struct interface *iface = state->iface;
256 if (iface->state != IFS_SETUP)
259 interface_ip_set_enabled(&iface->config_ip, true);
260 system_flush_routes();
261 iface->state = IFS_UP;
262 iface->start_time = system_get_rtime();
263 interface_event(iface, IFEV_UP);
264 interface_write_resolv_conf();
265 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
268 if (iface->state == IFS_DOWN)
271 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
272 interface_ip_set_enabled(&iface->config_ip, false);
273 system_flush_routes();
274 mark_interface_down(iface);
275 interface_handle_config_change(iface);
277 case IFPEV_LINK_LOST:
278 if (iface->state != IFS_UP)
281 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
282 if (iface->state == IFS_UP)
283 interface_event(iface, IFEV_DOWN);
284 iface->state = IFS_SETUP;
289 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
292 iface->proto->free(iface->proto);
295 iface->state = IFS_DOWN;
296 iface->proto = state;
300 state->proto_event = interface_proto_cb;
301 state->iface = iface;
305 interface_init(struct interface *iface, const char *name,
306 struct blob_attr *config)
308 struct blob_attr *tb[IFACE_ATTR_MAX];
309 struct blob_attr *cur;
310 const char *proto_name = NULL;
312 strncpy(iface->name, name, sizeof(iface->name) - 1);
313 INIT_LIST_HEAD(&iface->errors);
314 INIT_LIST_HEAD(&iface->users);
315 INIT_LIST_HEAD(&iface->hotplug_list);
316 interface_ip_init(&iface->proto_ip, iface);
317 interface_ip_init(&iface->config_ip, iface);
318 iface->config_ip.enabled = false;
320 iface->main_dev.cb = interface_cb;
321 iface->l3_dev = &iface->main_dev;
323 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
324 blob_data(config), blob_len(config));
326 if ((cur = tb[IFACE_ATTR_PROTO]))
327 proto_name = blobmsg_data(cur);
329 proto_attach_interface(iface, proto_name);
331 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
332 iface->proto_ip.no_defaultroute =
333 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
335 iface->config_autostart = iface->autostart;
339 interface_add(struct interface *iface, struct blob_attr *config)
341 struct blob_attr *tb[IFACE_ATTR_MAX];
342 struct blob_attr *cur;
344 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
345 blob_data(config), blob_len(config));
347 if ((cur = tb[IFACE_ATTR_IFNAME]))
348 iface->ifname = blobmsg_data(cur);
350 iface->config = config;
351 vlist_add(&interfaces, &iface->node);
355 interface_remove_link(struct interface *iface, struct device *dev)
357 struct device *mdev = iface->main_dev.dev;
359 if (mdev && mdev->hotplug_ops)
360 return mdev->hotplug_ops->del(mdev, dev);
362 if (!iface->main_dev.hotplug)
363 return UBUS_STATUS_INVALID_ARGUMENT;
365 if (dev != iface->main_dev.dev)
366 return UBUS_STATUS_INVALID_ARGUMENT;
368 device_remove_user(&iface->main_dev);
373 interface_add_link(struct interface *iface, struct device *dev)
375 struct device *mdev = iface->main_dev.dev;
380 if (iface->main_dev.hotplug)
381 device_remove_user(&iface->main_dev);
384 if (mdev->hotplug_ops)
385 return mdev->hotplug_ops->add(mdev, dev);
387 return UBUS_STATUS_NOT_SUPPORTED;
390 device_add_user(&iface->main_dev, dev);
391 iface->main_dev.hotplug = true;
396 interface_set_up(struct interface *iface)
400 iface->autostart = true;
402 if (iface->state != IFS_DOWN)
405 interface_clear_errors(iface);
406 if (!iface->available) {
407 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
411 if (iface->main_dev.dev) {
412 ret = device_claim(&iface->main_dev);
417 iface->state = IFS_SETUP;
418 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
420 mark_interface_down(iface);
428 interface_set_down(struct interface *iface)
431 vlist_for_each_element(&interfaces, iface, node)
432 __interface_set_down(iface, false);
434 iface->autostart = false;
435 __interface_set_down(iface, false);
442 interface_start_pending(void)
444 struct interface *iface;
446 vlist_for_each_element(&interfaces, iface, node) {
447 if (iface->available && iface->autostart)
448 interface_set_up(iface);
453 set_config_state(struct interface *iface, enum interface_config_state s)
455 iface->config_state = s;
456 if (iface->state == IFS_DOWN)
457 interface_handle_config_change(iface);
459 __interface_set_down(iface, false);
463 interface_update_start(struct interface *iface)
465 interface_ip_update_start(&iface->proto_ip);
469 interface_update_complete(struct interface *iface)
471 struct device_route *route;
473 interface_ip_update_complete(&iface->proto_ip);
474 vlist_for_each_element(&iface->config_ip.route, route, node) {
475 if (iface->l3_dev->dev) {
476 system_add_route(iface->l3_dev->dev, route);
477 route->enabled = true;
483 interface_change_config(struct interface *if_old, struct interface *if_new)
485 struct blob_attr *old_config = if_old->config;
486 const char *old_ifname = if_old->ifname;
487 const struct proto_handler *proto = if_old->proto_handler;
489 interface_clear_errors(if_old);
490 if_old->config = if_new->config;
491 if (!if_old->config_autostart && if_new->config_autostart)
492 if_old->autostart = true;
494 if_old->config_autostart = if_new->config_autostart;
495 if_old->ifname = if_new->ifname;
496 if_old->proto_handler = if_new->proto_handler;
498 if ((!!old_ifname != !!if_new->ifname) ||
499 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
500 proto != if_new->proto_handler) {
501 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
506 if (!proto->config_params)
507 D(INTERFACE, "No config parameters for interface '%s'\n",
509 else if (!config_check_equal(old_config, if_new->config,
510 proto->config_params)) {
511 D(INTERFACE, "Reload interface '%s because of config changes\n",
516 if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
517 if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
518 interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
524 set_config_state(if_old, IFC_RELOAD);
531 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
532 struct vlist_node *node_old)
534 struct interface *if_old = container_of(node_old, struct interface, node);
535 struct interface *if_new = container_of(node_new, struct interface, node);
537 if (node_old && node_new) {
538 D(INTERFACE, "Update interface '%s'\n", if_new->name);
539 interface_change_config(if_old, if_new);
540 } else if (node_old) {
541 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
542 set_config_state(if_old, IFC_REMOVE);
543 } else if (node_new) {
544 D(INTERFACE, "Create interface '%s'\n", if_new->name);
545 proto_init_interface(if_new, if_new->config);
546 interface_claim_device(if_new);
547 netifd_ubus_add_interface(if_new);
553 interface_init_list(void)
555 vlist_init(&interfaces, avl_strcmp, interface_update,
556 struct interface, node, name);
557 interfaces.keep_old = true;
558 interfaces.no_delete = true;