Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp1294094ybg; Fri, 18 Oct 2019 15:21:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6yaaoWylqSkXUeuIHMr4fU8Y5jB/Swdna5eF4YofBYwvCfhW483oNsPmIPaz2D21l+J5r X-Received: by 2002:a17:906:70c7:: with SMTP id g7mr11294881ejk.9.1571437302148; Fri, 18 Oct 2019 15:21:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571437302; cv=none; d=google.com; s=arc-20160816; b=kFeyocXXldBLjw+Vs4256Icha+t0SsgsVf7Ef6WkwYV7axcaGxBHnMoHrv2NmIFoo7 AonI2Na8rc7E+xAcx/4RKDJonSSPX7KFsv6lVyEQFPJXtTeKn4/KFgis6l8qto19YTQ5 1FHqS83Q2wL7l0X20U/X3O9XcY01c79M3IxvJqeNf5Vm/iu92GOsT6AlqfbkxtRGpqcK r7stSzzylTuQ/VlGADrAkk9LfgKbkUDgkcBp1554n50CsnPfqF3Ya+m0cv3o81Qpo7X1 uClcYAYitcKLpAoOgTwGlF+N4AodGn4BtaN2ZhnvrMN75+oJWBSfqlr4BHyKUIXvmZeR hGnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=RNdYOuR7tywBwRv915GyiFN2zW6aIVRHNQZduGmjsFs=; b=JJd11Fp6NwJGl3hTvxr59p58mnLpBFKdKIwMmokvWGUNIb5ibym+6gnch182sut7bC yCWI3N438PiFHVHzlcdwWHkvc6fC4yA7KF0k0uwQQAyKAPU0jvkJwsuY/+2ZHZoG7I4A Y+Do93W/mc1e/euTf17gzWcX/QQlCbzlVaQHzuzsI3xKTn0VrCodjJ8LbrKE7G3omVnr PWsFpAi43I7LLS1X01yJudXA1LCc5hYKlS0W0RTrwJfOHpphC+Li7F0VLyyj1lacRlCC Vd2yZnUoCYMl5+7WTshOzRh2u5apv81atnpeihBlU12q9x+FdYhjns1sK3sHts9CILPr vajg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w12si4204413ejb.303.2019.10.18.15.21.16; Fri, 18 Oct 2019 15:21:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-bluetooth-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406667AbfJQVzE (ORCPT + 99 others); Thu, 17 Oct 2019 17:55:04 -0400 Received: from mga05.intel.com ([192.55.52.43]:65297 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391375AbfJQVzE (ORCPT ); Thu, 17 Oct 2019 17:55:04 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Oct 2019 14:55:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,309,1566889200"; d="scan'208";a="208420475" Received: from ingas-nuc1.sea.intel.com ([10.251.143.15]) by orsmga002.jf.intel.com with ESMTP; 17 Oct 2019 14:55:01 -0700 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ v2] mesh: Implement AddAppKey and AddNetKey methods Date: Thu, 17 Oct 2019 14:55:00 -0700 Message-Id: <20191017215500.9185-1-inga.stotland@intel.com> X-Mailer: git-send-email 2.21.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org This adds implementation for the following methods on org.bluez.mesh.Node1 interface: void AddNetKey(object element_path, uint16 destination, uint16 subnet_index, uint16 net_index, boolean update) void AddAppKey(object element_path, uint16 destination, uint16 app_index, uint16 net_index, boolean update) --- mesh/node.c | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 2 deletions(-) diff --git a/mesh/node.c b/mesh/node.c index 74694d520..c91fb5d53 100644 --- a/mesh/node.c +++ b/mesh/node.c @@ -443,7 +443,7 @@ static void set_net_key(void *a, void *b) netkey->phase); } -static void set_app_key(void *a, void *b) +static void set_appkey(void *a, void *b) { struct mesh_config_appkey *appkey = a; struct mesh_node *node = b; @@ -512,7 +512,7 @@ static bool init_from_storage(struct mesh_config_node *db_node, l_queue_foreach(db_node->netkeys, set_net_key, node); if (db_node->appkeys) - l_queue_foreach(db_node->appkeys, set_app_key, node); + l_queue_foreach(db_node->appkeys, set_appkey, node); mesh_net_set_seq_num(node->net, node->seq_number); mesh_net_set_default_ttl(node->net, node->ttl); @@ -2001,6 +2001,130 @@ static struct l_dbus_message *dev_key_send_call(struct l_dbus *dbus, return l_dbus_message_new_method_return(msg); } +static struct l_dbus_message *add_netkey_call(struct l_dbus *dbus, + struct l_dbus_message *msg, + void *user_data) +{ + struct mesh_node *node = user_data; + const char *sender, *ele_path; + struct node_element *ele; + uint16_t dst, sub_idx, net_idx, src; + bool update; + struct keyring_net_key key; + uint8_t data[20]; + + l_debug("AddNetKey"); + + sender = l_dbus_message_get_sender(msg); + + if (strcmp(sender, node->owner)) + return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL); + + if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst, + &sub_idx, &net_idx, &update)) + return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL); + + ele = l_queue_find(node->elements, match_element_path, ele_path); + if (!ele) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, + "Element not found"); + + src = node_get_primary(node) + ele->idx; + + if (!keyring_get_net_key(node, sub_idx, &key)) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, + "NetKey not found"); + + if (!update) { + l_put_be16(OP_NETKEY_ADD, data); + + if (key.phase != KEY_REFRESH_PHASE_TWO) + memcpy(data + 4, key.old_key, 16); + else + memcpy(data + 4, key.new_key, 16); + } else { + if (key.phase != KEY_REFRESH_PHASE_ONE) + return dbus_error(msg, MESH_ERROR_FAILED, + "Cannot update"); + l_put_be16(OP_NETKEY_UPDATE, data); + memcpy(data + 4, key.new_key, 16); + } + + l_put_le16(sub_idx, &data[2]); + + if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx, + DEFAULT_TTL, data, 20)) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL); + + return l_dbus_message_new_method_return(msg); +} + +static struct l_dbus_message *add_appkey_call(struct l_dbus *dbus, + struct l_dbus_message *msg, + void *user_data) +{ + struct mesh_node *node = user_data; + const char *sender, *ele_path; + struct node_element *ele; + uint16_t dst, app_idx, net_idx, src; + bool update; + struct keyring_net_key net_key; + struct keyring_app_key app_key; + uint8_t data[20]; + + l_debug("AddAppKey"); + + sender = l_dbus_message_get_sender(msg); + + if (strcmp(sender, node->owner)) + return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL); + + if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst, + &app_idx, &net_idx, &update)) + return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL); + + ele = l_queue_find(node->elements, match_element_path, ele_path); + if (!ele) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, + "Element not found"); + + src = node_get_primary(node) + ele->idx; + + if (!keyring_get_app_key(node, app_idx, &app_key)) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, + "AppKey not found"); + + if (!keyring_get_net_key(node, app_key.net_idx, &net_key)) { + return dbus_error(msg, MESH_ERROR_NOT_FOUND, + "Bound NetKey not found"); + } + + if (!update) { + data[0] = OP_APPKEY_ADD; + if (net_key.phase != KEY_REFRESH_PHASE_TWO) + memcpy(data + 4, app_key.old_key, 16); + else + memcpy(data + 4, app_key.new_key, 16); + } else { + if (net_key.phase != KEY_REFRESH_PHASE_ONE) + return dbus_error(msg, MESH_ERROR_FAILED, + "Cannot update"); + data[0] = OP_APPKEY_UPDATE; + memcpy(data + 4, app_key.new_key, 16); + } + + /* Pack bound NetKey and AppKey into 3 octets */ + data[1] = app_key.net_idx; + data[2] = ((app_key.net_idx >> 8) & 0xf) | ((app_idx << 4) & 0xf0); + data[3] = app_idx >> 4; + + if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx, + DEFAULT_TTL, data, 20)) + return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL); + + return l_dbus_message_new_method_return(msg); +} + static struct l_dbus_message *publish_call(struct l_dbus *dbus, struct l_dbus_message *msg, void *user_data) @@ -2219,6 +2343,12 @@ static void setup_node_interface(struct l_dbus_interface *iface) "", "oqbqay", "element_path", "destination", "remote", "net_index", "data"); + l_dbus_interface_method(iface, "AddNetKey", 0, add_netkey_call, "", + "oqqqb", "element_path", "destination", + "subnet_index", "net_index", "update"); + l_dbus_interface_method(iface, "AddAppKey", 0, add_appkey_call, "", + "oqqqb", "element_path", "destination", + "app_index", "net_index", "update"); l_dbus_interface_method(iface, "Publish", 0, publish_call, "", "oqay", "element_path", "model_id", "data"); l_dbus_interface_method(iface, "VendorPublish", 0, vendor_publish_call, -- 2.21.0