+ blobmsg_for_each_attr(cur, attr, rem)
+ interface_add_data(iface, cur);
+}
+
+static struct device *
+proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
+{
+ struct device *dev;
+ struct blob_buf b;
+
+ memset(&b, 0, sizeof(b));
+ blob_buf_init(&b, 0);
+ blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
+ dev = device_create(name, &tunnel_device_type, blob_data(b.head));
+ blob_buf_free(&b);
+
+ return dev;
+}
+
+enum {
+ NOTIFY_ACTION,
+ NOTIFY_ERROR,
+ NOTIFY_COMMAND,
+ NOTIFY_ENV,
+ NOTIFY_SIGNAL,
+ NOTIFY_AVAILABLE,
+ NOTIFY_LINK_UP,
+ NOTIFY_IFNAME,
+ NOTIFY_ADDR_EXT,
+ NOTIFY_ROUTES,
+ NOTIFY_ROUTES6,
+ NOTIFY_TUNNEL,
+ NOTIFY_DATA,
+ NOTIFY_KEEP,
+ NOTIFY_HOST,
+ NOTIFY_DNS,
+ NOTIFY_DNS_SEARCH,
+ __NOTIFY_LAST
+};
+
+static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
+ [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
+ [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
+ [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
+ [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
+ [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
+ [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
+ [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
+ [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
+ [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
+ [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
+ [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
+ [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
+};
+
+static int
+proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
+{
+ struct interface *iface = state->proto.iface;
+ struct blob_attr *cur;
+ struct device *dev;
+ const char *devname;
+ int dev_create = 1;
+ bool addr_ext = false;
+ bool keep = false;
+ bool up;
+
+ if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
+ return UBUS_STATUS_PERMISSION_DENIED;
+
+ if (!tb[NOTIFY_LINK_UP])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
+ if (!up) {
+ state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
+ return 0;
+ }
+
+ if ((cur = tb[NOTIFY_KEEP]) != NULL)
+ keep = blobmsg_get_bool(cur);
+
+ if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
+ addr_ext = blobmsg_get_bool(cur);
+ if (addr_ext)
+ dev_create = 2;
+ }
+
+ if (iface->state != IFS_UP || !iface->l3_dev.dev)
+ keep = false;
+
+ if (!keep) {
+ dev = iface->main_dev.dev;
+ if (tb[NOTIFY_IFNAME]) {
+ keep = false;
+ devname = blobmsg_data(tb[NOTIFY_IFNAME]);
+ if (tb[NOTIFY_TUNNEL])
+ dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
+ else
+ dev = device_get(devname, dev_create);
+ }
+
+ if (!dev)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ interface_set_l3_dev(iface, dev);
+ if (device_claim(&iface->l3_dev) < 0)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ device_set_present(dev, true);
+
+ interface_update_start(iface);
+ }
+
+ proto_apply_ip_settings(iface, data, addr_ext);
+
+ if ((cur = tb[NOTIFY_ROUTES]) != NULL)
+ proto_shell_parse_route_list(state->proto.iface, cur, false);
+
+ if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
+ proto_shell_parse_route_list(state->proto.iface, cur, true);
+
+ if ((cur = tb[NOTIFY_DNS]))
+ interface_add_dns_server_list(&iface->proto_ip, cur);
+
+ if ((cur = tb[NOTIFY_DNS_SEARCH]))
+ interface_add_dns_search_list(&iface->proto_ip, cur);
+
+ if ((cur = tb[NOTIFY_DATA]))
+ proto_shell_parse_data(state->proto.iface, cur);
+
+ interface_update_complete(state->proto.iface);
+
+ if ((state->sm != S_SETUP_ABORT) && (state->sm != S_TEARDOWN)) {
+ if (!keep)
+ state->proto.proto_event(&state->proto, IFPEV_UP);
+ state->sm = S_IDLE;
+ }
+
+ return 0;