if (dep->dev)
device_remove_user(dep);
+ if (!dev)
+ return;
+
dep->dev = dev;
list_add_tail(&dep->list, &dev->users);
if (dep->cb && dev->present) {
current_ev = current->hotplug_ev;
list_del_init(¤t->hotplug_list);
- if (current_ev == IFEV_UP && current->l3_dev->dev)
- device = current->l3_dev->dev->ifname;
+ if (current_ev == IFEV_UP && current->l3_dev.dev)
+ device = current->l3_dev.dev->ifname;
D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
run_cmd(current->name, device, current_ev == IFEV_UP);
ip = container_of(tree, struct interface_ip_settings, addr);
iface = ip->iface;
- dev = iface->l3_dev->dev;
+ dev = iface->l3_dev.dev;
if (node_new) {
a_new = container_of(node_new, struct device_addr, node);
ip = container_of(tree, struct interface_ip_settings, route);
iface = ip->iface;
- dev = iface->l3_dev->dev;
+ dev = iface->l3_dev.dev;
route_old = container_of(node_old, struct device_route, node);
route_new = container_of(node_new, struct device_route, node);
struct device *dev;
ip->enabled = enabled;
- dev = ip->iface->l3_dev->dev;
+ dev = ip->iface->l3_dev.dev;
if (!dev)
return;
static void
interface_flush_state(struct interface *iface)
{
- interface_ip_flush(&iface->proto_ip);
if (iface->main_dev.dev)
device_release(&iface->main_dev);
- if (iface->l3_dev != &iface->main_dev && iface->l3_dev->dev)
- device_release(iface->l3_dev);
+ if (iface->l3_dev.dev)
+ device_release(&iface->l3_dev);
}
static void
!(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
dev = device_get(iface->ifname, true);
if (dev)
- device_add_user(&iface->main_dev, dev);
+ interface_set_main_dev(iface, dev);
}
if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
interface_set_available(iface, true);
interface_clear_errors(iface);
if (iface->main_dev.dev &&
(!reload || !iface->main_dev.hotplug))
- device_remove_user(&iface->main_dev);
- iface->l3_dev = &iface->main_dev;
+ interface_set_main_dev(iface, NULL);
interface_set_proto_state(iface, NULL);
}
iface->config_ip.enabled = false;
iface->main_dev.cb = interface_cb;
- iface->l3_dev = &iface->main_dev;
blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
blob_data(config), blob_len(config));
vlist_add(&interfaces, &iface->node, iface->name);
}
+void
+interface_set_l3_dev(struct interface *iface, struct device *dev)
+{
+ bool enabled = iface->config_ip.enabled;
+ bool claimed = iface->l3_dev.claimed;
+
+ if (iface->l3_dev.dev == dev)
+ return;
+
+ interface_ip_set_enabled(&iface->config_ip, false);
+ interface_ip_flush(&iface->proto_ip);
+ device_add_user(&iface->l3_dev, dev);
+
+ if (dev) {
+ if (claimed)
+ device_claim(&iface->l3_dev);
+ interface_ip_set_enabled(&iface->config_ip, enabled);
+ }
+}
+
+void
+interface_set_main_dev(struct interface *iface, struct device *dev)
+{
+ bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
+ bool claimed = iface->l3_dev.claimed;
+
+ if (iface->main_dev.dev == dev)
+ return;
+
+ device_add_user(&iface->main_dev, dev);
+ if (set_l3)
+ interface_set_l3_dev(iface, dev);
+
+ if (claimed)
+ device_claim(&iface->l3_dev);
+
+ if (!iface->l3_dev.dev)
+ interface_set_l3_dev(iface, dev);
+}
+
int
interface_remove_link(struct interface *iface, struct device *dev)
{
return UBUS_STATUS_NOT_SUPPORTED;
}
- device_add_user(&iface->main_dev, dev);
+ interface_set_main_dev(iface, dev);
iface->main_dev.hotplug = true;
return 0;
}
interface_ip_update_complete(&iface->proto_ip);
vlist_for_each_element(&iface->config_ip.route, route, node) {
- if (iface->l3_dev->dev) {
- system_add_route(iface->l3_dev->dev, route);
+ if (iface->l3_dev.dev) {
+ system_add_route(iface->l3_dev.dev, route);
route->enabled = true;
}
}
struct device_user main_dev;
/* interface that layer 3 communication will go through */
- struct device_user *l3_dev;
+ struct device_user l3_dev;
struct blob_attr *config;
int interface_set_down(struct interface *iface);
void __interface_set_down(struct interface *iface, bool force);
+void interface_set_main_dev(struct interface *iface, struct device *dev);
+void interface_set_l3_dev(struct interface *iface, struct device *dev);
void interface_add_user(struct interface_user *dep, struct interface *iface);
void interface_remove_user(struct interface_user *dep);
struct proto_shell_handler *handler;
struct blob_attr *config;
- struct device_user l3_dev;
-
struct uloop_timeout teardown_timeout;
struct netifd_process script_task;
if (state->sm == S_TEARDOWN)
return 0;
- if (state->l3_dev.dev)
- device_remove_user(&state->l3_dev);
-
if (state->script_task.uloop.pending) {
if (state->sm != S_SETUP_ABORT) {
uloop_timeout_set(&state->teardown_timeout, 1000);
struct proto_shell_state *state;
state = container_of(proto, struct proto_shell_state, proto);
- if (state->l3_dev.dev)
- device_remove_user(&state->l3_dev);
netifd_kill_process(&state->script_task);
netifd_kill_process(&state->proto_task);
free(state->config);
if (!iface->main_dev.dev)
return UBUS_STATUS_INVALID_ARGUMENT;
} else {
- if (state->l3_dev.dev)
- device_remove_user(&state->l3_dev);
-
devname = blobmsg_data(tb[NOTIFY_IFNAME]);
if (tb[NOTIFY_TUNNEL]) {
dev = proto_shell_create_tunnel(devname,
return UBUS_STATUS_NOT_FOUND;
}
- device_add_user(&state->l3_dev, dev);
- iface->l3_dev = &state->l3_dev;
- device_claim(&state->l3_dev);
+ interface_set_l3_dev(iface, dev);
+ device_claim(&iface->l3_dev);
}
interface_update_start(iface);
if (iface->state == IFS_UP) {
time_t cur = system_get_rtime();
blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
- blobmsg_add_string(&b, "l3_device", iface->l3_dev->dev->ifname);
+ blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
}
dev = iface->main_dev.dev;