From patchwork Fri May 10 17:51:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gio--- via openwrt-devel X-Patchwork-Id: 1933919 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Fhs364gA; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vbc0f0zVJz20fh for ; Sat, 11 May 2024 03:52:32 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type:List-Help: Reply-To:List-Archive:List-Unsubscribe:List-Subscribe:From:List-Post:List-Id: Message-ID:MIME-Version:Date:Subject:To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=XePjasOtk6xjaPtG0pJ9RW8LehZAsevL6L5u1Odh/kc=; b=Fhs364gAfTIpTkNHKacCfi/VRF DOjsNzoYXbbeLGyUZeeLP8zlcmRpDpJ/5p6OEcwYBwjxsDirkfk1ZO9Gs24W6grwb1nHunv3CMS8B 62rtCoDLcKzjdrS3LCar+FyJYZj9nghk/1p36oSLcFpXxIsdQoOQMO0lMwzsPdCtDXET+191OssjO mosnozE2hXzLp7MOoHOex289XXi/05UTy1szrGLgODamNbJbm+1eZZYsvIiSWJUUsCdhOw6JxpDPg V6zAhSeyQSjQbw7n6FW5OGRF/JpGbNq3Q8hXsAK33noDAAc8jmrQyWYmK5nq/bo0xHUeEsLMOmMKg SIVkIOEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1s5UOu-00000006413-1pNY; Fri, 10 May 2024 17:51:20 +0000 To: openwrt-devel@lists.openwrt.org Subject: [PATCH] ubus: add command to create devices dynamically Date: Fri, 10 May 2024 19:51:03 +0200 MIME-Version: 1.0 Message-ID: List-Id: OpenWrt Development List List-Post: X-Patchwork-Original-From: gio--- via openwrt-devel From: gio--- via openwrt-devel Precedence: list X-Mailman-Version: 2.1.34 X-BeenThere: openwrt-devel@lists.openwrt.org List-Subscribe: , List-Unsubscribe: , List-Archive: Reply-To: gio@eigenlab.org List-Help: Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org The sender domain has a DMARC Reject/Quarantine policy which disallows sending mailing list messages using the original "From" header. To mitigate this problem, the original message has been wrapped automatically by the mailing list software. From: Gioacchino Mazzurco Some kind of devices might become available after initial configuration, and other one like WDS stations or APuP peers can create unpredictably many linux interfaces like wlan0.peer1 wlan0.peer2... To use those linux interfaces as a base for other devices on top of those like 802.1ad devices we need to be able to create them at runtime ofter thos comes up, so an ubus command have been added for that. ubus call network add_dynamic_device \ '{"name":"NewDeviceName", "type":"8021ad", \ "ifname":"wlan0.peer1", "vid":"47"}' This will create the device that can be used for one or more interface with pre-existent commands ubus call network add_dynamic \ '{"name":"NewInterfaceName", "proto":"static", "auto":1, \ "device":"NewDeviceName", \ "ipaddr":"192.0.2.0", "netmask":"255.255.255.255"}' ubus call network.interface.NewInterfaceName up To inspect the newly created device status a pre-existent command can be used ubus call network.device status '{"name":"NewDeviceName"}' Signed-off-by: Gioacchino Mazzurco --- ubus.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/ubus.c b/ubus.c index 3d24dc7..a686d4f 100644 --- a/ubus.c +++ b/ubus.c @@ -163,6 +163,57 @@ error: return UBUS_STATUS_UNKNOWN_ERROR; } +enum { + DI_DEV_NAME, + DI_DEV_TYPE, + __DI_DEV_MAX +}; + +static const struct blobmsg_policy dynamic_device_policy[__DI_DEV_MAX] = { + [DI_DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING }, + [DI_DEV_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING } +}; + +static int +netifd_add_dynamic_device( + struct ubus_context */*ctx*/, struct ubus_object */*obj*/, + struct ubus_request_data */*req*/, const char */*method*/, + struct blob_attr *msg ) +{ + struct blob_attr *tb[__DI_DEV_MAX]; + struct device *device; + struct device_type *type; + struct blob_attr *config; + + blobmsg_parse( dynamic_device_policy, __DI_DEV_MAX, tb, + blob_data(msg), blob_len(msg) ); + + if (!tb[DI_DEV_NAME] || !tb[DI_DEV_TYPE]) + return UBUS_STATUS_INVALID_ARGUMENT; + + const char *name = blobmsg_get_string(tb[DI_DEV_NAME]); + const char *type_name = blobmsg_get_string(tb[DI_DEV_TYPE]); + + type = device_type_get(type_name); + + if (!type) + return UBUS_STATUS_INVALID_ARGUMENT; + + config = blob_memdup(msg); + if (!config) + return UBUS_STATUS_UNKNOWN_ERROR; + + device = device_create(name, type, config); + if (!device) + goto error_free_config; + + return UBUS_STATUS_OK; + +error_free_config: + free(config); + return UBUS_STATUS_UNKNOWN_ERROR; +} + enum { NETNS_UPDOWN_JAIL, NETNS_UPDOWN_START, @@ -209,6 +260,7 @@ static struct ubus_method main_object_methods[] = { UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy), { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers }, UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy), + UBUS_METHOD("add_dynamic_device", netifd_add_dynamic_device, dynamic_device_policy), UBUS_METHOD("netns_updown", netifd_netns_updown, netns_updown_policy), };