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_event(struct interface *iface, enum interface_event ev)
86 struct interface_user *dep, *tmp;
88 list_for_each_entry_safe(dep, tmp, &iface->users, list)
89 dep->cb(dep, IFEV_UP);
91 interface_queue_event(iface, ev);
95 interface_flush_state(struct interface *iface)
97 if (iface->main_dev.dev)
98 device_release(&iface->main_dev);
99 if (iface->l3_dev.dev)
100 device_release(&iface->l3_dev);
104 mark_interface_down(struct interface *iface)
106 if (iface->state == IFS_UP)
107 interface_event(iface, IFEV_DOWN);
108 interface_flush_state(iface);
109 iface->state = IFS_DOWN;
113 __interface_set_down(struct interface *iface, bool force)
115 interface_clear_errors(iface);
117 if (iface->state == IFS_DOWN ||
118 iface->state == IFS_TEARDOWN)
121 if (iface->state == IFS_UP)
122 interface_event(iface, IFEV_DOWN);
123 iface->state = IFS_TEARDOWN;
124 interface_ip_flush(&iface->config_ip);
125 interface_ip_flush(&iface->proto_ip);
126 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
128 interface_flush_state(iface);
132 interface_cb(struct device_user *dep, enum device_event ev)
134 struct interface *iface;
137 iface = container_of(dep, struct interface, main_dev);
142 case DEV_EVENT_REMOVE:
149 interface_set_available(iface, new_state);
153 interface_set_available(struct interface *iface, bool new_state)
155 if (iface->available == new_state)
158 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
159 iface->available = new_state;
162 if (iface->autostart && !config_init)
163 interface_set_up(iface);
165 __interface_set_down(iface, true);
169 interface_add_user(struct interface_user *dep, struct interface *iface)
172 list_add(&dep->list, &iface->users);
173 if (iface->state == IFS_UP)
174 dep->cb(dep, IFEV_UP);
178 interface_remove_user(struct interface_user *dep)
180 list_del_init(&dep->list);
185 interface_claim_device(struct interface *iface)
190 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
191 dev = device_get(iface->ifname, true);
193 interface_set_main_dev(iface, dev);
195 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
196 interface_set_available(iface, true);
201 interface_cleanup(struct interface *iface, bool reload)
203 struct interface_user *dep, *tmp;
205 list_for_each_entry_safe(dep, tmp, &iface->users, list)
206 interface_remove_user(dep);
208 interface_ip_flush(&iface->config_ip);
209 interface_flush_state(iface);
210 interface_clear_errors(iface);
211 if (iface->main_dev.dev &&
212 (!reload || !iface->main_dev.hotplug))
213 interface_set_main_dev(iface, NULL);
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;
325 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
326 blob_data(config), blob_len(config));
328 if ((cur = tb[IFACE_ATTR_PROTO]))
329 proto_name = blobmsg_data(cur);
331 proto_attach_interface(iface, proto_name);
333 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
334 iface->proto_ip.no_defaultroute =
335 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
337 iface->config_autostart = iface->autostart;
341 interface_add(struct interface *iface, struct blob_attr *config)
343 struct blob_attr *tb[IFACE_ATTR_MAX];
344 struct blob_attr *cur;
346 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
347 blob_data(config), blob_len(config));
349 if ((cur = tb[IFACE_ATTR_IFNAME]))
350 iface->ifname = blobmsg_data(cur);
352 iface->config = config;
353 vlist_add(&interfaces, &iface->node, iface->name);
357 interface_set_l3_dev(struct interface *iface, struct device *dev)
359 bool enabled = iface->config_ip.enabled;
360 bool claimed = iface->l3_dev.claimed;
362 if (iface->l3_dev.dev == dev)
365 interface_ip_set_enabled(&iface->config_ip, false);
366 interface_ip_flush(&iface->proto_ip);
367 device_add_user(&iface->l3_dev, dev);
371 device_claim(&iface->l3_dev);
372 interface_ip_set_enabled(&iface->config_ip, enabled);
377 interface_set_main_dev(struct interface *iface, struct device *dev)
379 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
380 bool claimed = iface->l3_dev.claimed;
382 if (iface->main_dev.dev == dev)
385 device_add_user(&iface->main_dev, dev);
387 interface_set_l3_dev(iface, dev);
390 device_claim(&iface->l3_dev);
392 if (!iface->l3_dev.dev)
393 interface_set_l3_dev(iface, dev);
397 interface_remove_link(struct interface *iface, struct device *dev)
399 struct device *mdev = iface->main_dev.dev;
401 if (mdev && mdev->hotplug_ops)
402 return mdev->hotplug_ops->del(mdev, dev);
404 if (!iface->main_dev.hotplug)
405 return UBUS_STATUS_INVALID_ARGUMENT;
407 if (dev != iface->main_dev.dev)
408 return UBUS_STATUS_INVALID_ARGUMENT;
410 device_remove_user(&iface->main_dev);
415 interface_add_link(struct interface *iface, struct device *dev)
417 struct device *mdev = iface->main_dev.dev;
422 if (iface->main_dev.hotplug)
423 device_remove_user(&iface->main_dev);
426 if (mdev->hotplug_ops)
427 return mdev->hotplug_ops->add(mdev, dev);
429 return UBUS_STATUS_NOT_SUPPORTED;
432 interface_set_main_dev(iface, dev);
433 iface->main_dev.hotplug = true;
438 interface_set_up(struct interface *iface)
442 iface->autostart = true;
444 if (iface->state != IFS_DOWN)
447 interface_clear_errors(iface);
448 if (!iface->available) {
449 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
453 if (iface->main_dev.dev) {
454 ret = device_claim(&iface->main_dev);
459 iface->state = IFS_SETUP;
460 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
462 mark_interface_down(iface);
470 interface_set_down(struct interface *iface)
473 vlist_for_each_element(&interfaces, iface, node)
474 __interface_set_down(iface, false);
476 iface->autostart = false;
477 __interface_set_down(iface, false);
484 interface_start_pending(void)
486 struct interface *iface;
488 vlist_for_each_element(&interfaces, iface, node) {
489 if (iface->available && iface->autostart)
490 interface_set_up(iface);
495 set_config_state(struct interface *iface, enum interface_config_state s)
497 iface->config_state = s;
498 if (iface->state == IFS_DOWN)
499 interface_handle_config_change(iface);
501 __interface_set_down(iface, false);
505 interface_update_start(struct interface *iface)
507 interface_ip_update_start(&iface->proto_ip);
511 interface_update_complete(struct interface *iface)
513 struct device_route *route;
515 interface_ip_update_complete(&iface->proto_ip);
516 vlist_for_each_element(&iface->config_ip.route, route, node) {
517 if (iface->l3_dev.dev) {
518 system_add_route(iface->l3_dev.dev, route);
519 route->enabled = true;
525 interface_change_config(struct interface *if_old, struct interface *if_new)
527 struct blob_attr *old_config = if_old->config;
528 const char *old_ifname = if_old->ifname;
529 const struct proto_handler *proto = if_old->proto_handler;
531 interface_clear_errors(if_old);
532 if_old->config = if_new->config;
533 if (!if_old->config_autostart && if_new->config_autostart)
534 if_old->autostart = true;
536 if_old->config_autostart = if_new->config_autostart;
537 if_old->ifname = if_new->ifname;
538 if_old->proto_handler = if_new->proto_handler;
540 if ((!!old_ifname != !!if_new->ifname) ||
541 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
542 proto != if_new->proto_handler) {
543 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
548 if (!proto->config_params)
549 D(INTERFACE, "No config parameters for interface '%s'\n",
551 else if (!config_check_equal(old_config, if_new->config,
552 proto->config_params)) {
553 D(INTERFACE, "Reload interface '%s because of config changes\n",
558 #define UPDATE(field) ({ \
559 bool __changed = (if_old->field != if_new->field); \
560 if_old->field = if_new->field; \
564 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
565 interface_ip_set_enabled(&if_old->config_ip, false);
566 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
567 interface_ip_set_enabled(&if_old->proto_ip, false);
568 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
576 set_config_state(if_old, IFC_RELOAD);
583 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
584 struct vlist_node *node_old)
586 struct interface *if_old = container_of(node_old, struct interface, node);
587 struct interface *if_new = container_of(node_new, struct interface, node);
589 if (node_old && node_new) {
590 D(INTERFACE, "Update interface '%s'\n", if_new->name);
591 interface_change_config(if_old, if_new);
592 } else if (node_old) {
593 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
594 set_config_state(if_old, IFC_REMOVE);
595 } else if (node_new) {
596 D(INTERFACE, "Create interface '%s'\n", if_new->name);
597 proto_init_interface(if_new, if_new->config);
598 interface_claim_device(if_new);
599 netifd_ubus_add_interface(if_new);
605 interface_init_list(void)
607 vlist_init(&interfaces, avl_strcmp, interface_update);
608 interfaces.keep_old = true;
609 interfaces.no_delete = true;