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, bool reload)
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 (!reload || !iface->main_dev.hotplug))
210 device_remove_user(&iface->main_dev);
211 iface->l3_dev = &iface->main_dev;
212 interface_set_proto_state(iface, NULL);
216 interface_do_free(struct interface *iface)
218 interface_cleanup(iface, false);
220 netifd_ubus_remove_interface(iface);
221 avl_delete(&interfaces.avl, &iface->node.avl);
226 interface_do_reload(struct interface *iface)
228 interface_cleanup(iface, true);
229 proto_init_interface(iface, iface->config);
230 interface_claim_device(iface);
234 interface_handle_config_change(struct interface *iface)
236 switch(iface->config_state) {
240 interface_do_reload(iface);
243 interface_do_free(iface);
246 if (iface->autostart && iface->available)
247 interface_set_up(iface);
251 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
253 struct interface *iface = state->iface;
257 if (iface->state != IFS_SETUP)
260 interface_ip_set_enabled(&iface->config_ip, true);
261 system_flush_routes();
262 iface->state = IFS_UP;
263 iface->start_time = system_get_rtime();
264 interface_event(iface, IFEV_UP);
265 interface_write_resolv_conf();
266 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
269 if (iface->state == IFS_DOWN)
272 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
273 interface_ip_set_enabled(&iface->config_ip, false);
274 system_flush_routes();
275 mark_interface_down(iface);
276 interface_handle_config_change(iface);
278 case IFPEV_LINK_LOST:
279 if (iface->state != IFS_UP)
282 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
283 if (iface->state == IFS_UP)
284 interface_event(iface, IFEV_DOWN);
285 iface->state = IFS_SETUP;
290 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
293 iface->proto->free(iface->proto);
296 iface->state = IFS_DOWN;
297 iface->proto = state;
301 state->proto_event = interface_proto_cb;
302 state->iface = iface;
306 interface_init(struct interface *iface, const char *name,
307 struct blob_attr *config)
309 struct blob_attr *tb[IFACE_ATTR_MAX];
310 struct blob_attr *cur;
311 const char *proto_name = NULL;
313 strncpy(iface->name, name, sizeof(iface->name) - 1);
314 INIT_LIST_HEAD(&iface->errors);
315 INIT_LIST_HEAD(&iface->users);
316 INIT_LIST_HEAD(&iface->hotplug_list);
317 interface_ip_init(&iface->proto_ip, iface);
318 interface_ip_init(&iface->config_ip, iface);
319 iface->config_ip.enabled = false;
321 iface->main_dev.cb = interface_cb;
322 iface->l3_dev = &iface->main_dev;
324 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
325 blob_data(config), blob_len(config));
327 if ((cur = tb[IFACE_ATTR_PROTO]))
328 proto_name = blobmsg_data(cur);
330 proto_attach_interface(iface, proto_name);
332 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
333 iface->proto_ip.no_defaultroute =
334 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
336 iface->config_autostart = iface->autostart;
340 interface_add(struct interface *iface, struct blob_attr *config)
342 struct blob_attr *tb[IFACE_ATTR_MAX];
343 struct blob_attr *cur;
345 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
346 blob_data(config), blob_len(config));
348 if ((cur = tb[IFACE_ATTR_IFNAME]))
349 iface->ifname = blobmsg_data(cur);
351 iface->config = config;
352 vlist_add(&interfaces, &iface->node, iface->name);
356 interface_remove_link(struct interface *iface, struct device *dev)
358 struct device *mdev = iface->main_dev.dev;
360 if (mdev && mdev->hotplug_ops)
361 return mdev->hotplug_ops->del(mdev, dev);
363 if (!iface->main_dev.hotplug)
364 return UBUS_STATUS_INVALID_ARGUMENT;
366 if (dev != iface->main_dev.dev)
367 return UBUS_STATUS_INVALID_ARGUMENT;
369 device_remove_user(&iface->main_dev);
374 interface_add_link(struct interface *iface, struct device *dev)
376 struct device *mdev = iface->main_dev.dev;
381 if (iface->main_dev.hotplug)
382 device_remove_user(&iface->main_dev);
385 if (mdev->hotplug_ops)
386 return mdev->hotplug_ops->add(mdev, dev);
388 return UBUS_STATUS_NOT_SUPPORTED;
391 device_add_user(&iface->main_dev, dev);
392 iface->main_dev.hotplug = true;
397 interface_set_up(struct interface *iface)
401 iface->autostart = true;
403 if (iface->state != IFS_DOWN)
406 interface_clear_errors(iface);
407 if (!iface->available) {
408 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
412 if (iface->main_dev.dev) {
413 ret = device_claim(&iface->main_dev);
418 iface->state = IFS_SETUP;
419 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
421 mark_interface_down(iface);
429 interface_set_down(struct interface *iface)
432 vlist_for_each_element(&interfaces, iface, node)
433 __interface_set_down(iface, false);
435 iface->autostart = false;
436 __interface_set_down(iface, false);
443 interface_start_pending(void)
445 struct interface *iface;
447 vlist_for_each_element(&interfaces, iface, node) {
448 if (iface->available && iface->autostart)
449 interface_set_up(iface);
454 set_config_state(struct interface *iface, enum interface_config_state s)
456 iface->config_state = s;
457 if (iface->state == IFS_DOWN)
458 interface_handle_config_change(iface);
460 __interface_set_down(iface, false);
464 interface_update_start(struct interface *iface)
466 interface_ip_update_start(&iface->proto_ip);
470 interface_update_complete(struct interface *iface)
472 struct device_route *route;
474 interface_ip_update_complete(&iface->proto_ip);
475 vlist_for_each_element(&iface->config_ip.route, route, node) {
476 if (iface->l3_dev->dev) {
477 system_add_route(iface->l3_dev->dev, route);
478 route->enabled = true;
484 interface_change_config(struct interface *if_old, struct interface *if_new)
486 struct blob_attr *old_config = if_old->config;
487 const char *old_ifname = if_old->ifname;
488 const struct proto_handler *proto = if_old->proto_handler;
490 interface_clear_errors(if_old);
491 if_old->config = if_new->config;
492 if (!if_old->config_autostart && if_new->config_autostart)
493 if_old->autostart = true;
495 if_old->config_autostart = if_new->config_autostart;
496 if_old->ifname = if_new->ifname;
497 if_old->proto_handler = if_new->proto_handler;
499 if ((!!old_ifname != !!if_new->ifname) ||
500 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
501 proto != if_new->proto_handler) {
502 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
507 if (!proto->config_params)
508 D(INTERFACE, "No config parameters for interface '%s'\n",
510 else if (!config_check_equal(old_config, if_new->config,
511 proto->config_params)) {
512 D(INTERFACE, "Reload interface '%s because of config changes\n",
517 if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
518 if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
519 interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
525 set_config_state(if_old, IFC_RELOAD);
532 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
533 struct vlist_node *node_old)
535 struct interface *if_old = container_of(node_old, struct interface, node);
536 struct interface *if_new = container_of(node_new, struct interface, node);
538 if (node_old && node_new) {
539 D(INTERFACE, "Update interface '%s'\n", if_new->name);
540 interface_change_config(if_old, if_new);
541 } else if (node_old) {
542 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
543 set_config_state(if_old, IFC_REMOVE);
544 } else if (node_new) {
545 D(INTERFACE, "Create interface '%s'\n", if_new->name);
546 proto_init_interface(if_new, if_new->config);
547 interface_claim_device(if_new);
548 netifd_ubus_add_interface(if_new);
554 interface_init_list(void)
556 vlist_init(&interfaces, avl_strcmp, interface_update);
557 interfaces.keep_old = true;
558 interfaces.no_delete = true;