Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4968998ybl; Mon, 9 Dec 2019 21:01:38 -0800 (PST) X-Google-Smtp-Source: APXvYqxik5/pa66BOrPK7IWe6bSFdacD0o6bJLPxh+yvSPaIRXEvQwnMRAlQ9x9cWrY2+Sxk4yUX X-Received: by 2002:a9d:1988:: with SMTP id k8mr25185680otk.316.1575954098529; Mon, 09 Dec 2019 21:01:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575954098; cv=none; d=google.com; s=arc-20160816; b=iLtvxcTh9yXEok0N2uimGySfTr3dwho2jLU8YaI36JML7H18c9lPo1HOEPJMjoo/fB wmjNQSj1Bh/sj+1iTNWxXVYL0Qag2MZONLotMAp2YpM22LuyUWr4v0BGccotD5dSIjmE j45og71jmkEcN0DuPL98lFB4ceqAloZDN4Zp3tx0W0MOAdlKYwRFRWR95uwCKWORC2CT VBWVgzrnxxx5xia4j8uE4PTvGuWvtQXQp9N1y9mkn/UB7vplfkfbtgGRbu/pKSzo3yw7 M03DPT7FNhmGy2fV04IRoi3kHPawwRcTSz3c66pkyRMWql7ms3WSVWjtWHjcR4HJ/Hf9 uodQ== 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=UMoVO1dgji8t9cwryFNJuG3Cq7OP4InwVKvhxdjuXxQ=; b=iSy4kj4urklycEt8XEunknYRumI1e0DvlizizgBRdDB3jqsYBleef8wSCcjWwGG/Ba LzNt6/0+qXliDecFDjuWRcQN+hvIKBWjPmzFGr9+gdHDPw2PKVOnNxgioz37CgKgg5b6 Ni4DtCD4IcaHAnp5jgmn+Tr2M2r4AW0Ernd1LswO3C75Eo2zNVoNeZjAgiV2flWAPhB5 Exw+Ak4jel66ZVOv+uEvfdgo13IgqV0+NU6TZY52CKjGZQCF+RAFwBxMdXwIxI7PcSes +UbvR22maX+RWXEJ++ZJJ4N0rh1E+emgciwtcg6W4rGrIZEN26YoAy8xK5Qg41BXSaA+ KfGA== 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 u1si1335126otk.80.2019.12.09.21.01.25; Mon, 09 Dec 2019 21:01:38 -0800 (PST) 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 S1726018AbfLJFAc (ORCPT + 99 others); Tue, 10 Dec 2019 00:00:32 -0500 Received: from mga14.intel.com ([192.55.52.115]:28957 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725857AbfLJFAc (ORCPT ); Tue, 10 Dec 2019 00:00:32 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Dec 2019 21:00:31 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,298,1571727600"; d="scan'208";a="244709086" Received: from ingas-nuc1.sea.intel.com ([10.252.197.92]) by fmsmga002.fm.intel.com with ESMTP; 09 Dec 2019 21:00:30 -0800 From: Inga Stotland To: linux-bluetooth@vger.kernel.org Cc: brian.gix@intel.com, Inga Stotland Subject: [PATCH BlueZ] tools/mesh-cfgclient: Fix appkey/netkey commands Date: Mon, 9 Dec 2019 21:00:28 -0800 Message-Id: <20191210050028.25507-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 fixes the checks for the presence of AppKeys and Netkeys. Also disallow sending a non-existing key: prior to sending either NetKeyAdd or AppKeyAdd message to a remote node, check if the key exists locally. --- tools/mesh-cfgclient.c | 14 +++++++++++++- tools/mesh/keys.c | 33 ++++++++++++++++++++++++--------- tools/mesh/keys.h | 1 + tools/mesh/remote.c | 17 +++++++++++++---- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/tools/mesh-cfgclient.c b/tools/mesh-cfgclient.c index 200eb5b84..b4d76de93 100644 --- a/tools/mesh-cfgclient.c +++ b/tools/mesh-cfgclient.c @@ -316,8 +316,20 @@ static bool send_key(void *user_data, uint16_t dst, uint16_t key_idx, const char *method_name = (!is_appkey) ? "AddNetKey" : "AddAppKey"; net_idx = remote_get_subnet_idx(dst); - if (net_idx == NET_IDX_INVALID) + if (net_idx == NET_IDX_INVALID) { + bt_shell_printf("Node %4.4x not found\n", dst); return false; + } + + if (!is_appkey && !keys_subnet_exists(key_idx)) { + bt_shell_printf("Local NetKey %u not found\n", key_idx); + return false; + } + + if (is_appkey && (keys_get_bound_key(key_idx) == NET_IDX_INVALID)) { + bt_shell_printf("Local AppKey %u not found\n", key_idx); + return false; + } req = l_new(struct key_data, 1); req->ele_path = user_data; diff --git a/tools/mesh/keys.c b/tools/mesh/keys.c index 7d2058294..77b32da63 100644 --- a/tools/mesh/keys.c +++ b/tools/mesh/keys.c @@ -37,9 +37,18 @@ struct net_key { static struct l_queue *net_keys; -static bool simple_match(const void *a, const void *b) +static bool app_key_present(const struct net_key *key, uint16_t app_idx) { - return a == b; + const struct l_queue_entry *l; + + for (l = l_queue_get_entries(key->app_keys); l; l = l->next) { + uint16_t idx = L_PTR_TO_UINT(l->data); + + if (idx == app_idx) + return true; + } + + return false; } static bool net_idx_match(const void *a, const void *b) @@ -102,7 +111,7 @@ void keys_add_app_key(uint16_t net_idx, uint16_t app_idx) if (!key->app_keys) key->app_keys = l_queue_new(); - if (l_queue_find(key->app_keys, simple_match, L_UINT_TO_PTR(app_idx))) + if (app_key_present(key, app_idx)) return; l_queue_push_tail(key->app_keys, L_UINT_TO_PTR(app_idx)); @@ -121,8 +130,7 @@ void keys_del_app_key(uint16_t app_idx) if (!key->app_keys) continue; - if (l_queue_remove_if(key->app_keys, simple_match, - L_UINT_TO_PTR(app_idx))) + if (l_queue_remove(key->app_keys, L_UINT_TO_PTR(app_idx))) return; } } @@ -140,8 +148,7 @@ uint16_t keys_get_bound_key(uint16_t app_idx) if (!key->app_keys) continue; - if (l_queue_find(key->app_keys, simple_match, - L_UINT_TO_PTR(app_idx))) + if (app_key_present(key, app_idx)) return key->idx; } @@ -152,14 +159,14 @@ static void print_appkey(void *app_key, void *user_data) { uint16_t app_idx = L_PTR_TO_UINT(app_key); - bt_shell_printf("%3.3x, ", app_idx); + bt_shell_printf("0x%3.3x, ", app_idx); } static void print_netkey(void *net_key, void *user_data) { struct net_key *key = net_key; - bt_shell_printf(COLOR_YELLOW "NetKey: %3.3x\n" COLOR_OFF, key->idx); + bt_shell_printf(COLOR_YELLOW "NetKey: 0x%3.3x\n" COLOR_OFF, key->idx); if (!key->app_keys || l_queue_isempty(key->app_keys)) return; @@ -173,3 +180,11 @@ void keys_print_keys(void) { l_queue_foreach(net_keys, print_netkey, NULL); } + +bool keys_subnet_exists(uint16_t idx) +{ + if (!l_queue_find(net_keys, net_idx_match, L_UINT_TO_PTR(idx))) + return false; + + return true; +} diff --git a/tools/mesh/keys.h b/tools/mesh/keys.h index 2a9faede6..71c3bb390 100644 --- a/tools/mesh/keys.h +++ b/tools/mesh/keys.h @@ -23,4 +23,5 @@ void keys_del_net_key(uint16_t net_idx); void keys_add_app_key(uint16_t net_idx, uint16_t app_idx); void keys_del_app_key(uint16_t app_idx); uint16_t keys_get_bound_key(uint16_t app_idx); +bool keys_subnet_exists(uint16_t idx); void keys_print_keys(void); diff --git a/tools/mesh/remote.c b/tools/mesh/remote.c index 673c7b0fb..25e8d23f8 100644 --- a/tools/mesh/remote.c +++ b/tools/mesh/remote.c @@ -41,9 +41,18 @@ struct remote_node { static struct l_queue *nodes; -static bool simple_match(const void *a, const void *b) +static bool key_present(struct l_queue *keys, uint16_t app_idx) { - return a == b; + const struct l_queue_entry *l; + + for (l = l_queue_get_entries(keys); l; l = l->next) { + uint16_t idx = L_PTR_TO_UINT(l->data); + + if (idx == app_idx) + return true; + } + + return false; } static int compare_unicast(const void *a, const void *b, void *user_data) @@ -104,7 +113,7 @@ bool remote_add_net_key(uint16_t addr, uint16_t net_idx) if (!rmt) return false; - if (l_queue_find(rmt->net_keys, simple_match, L_UINT_TO_PTR(net_idx))) + if (key_present(rmt->net_keys, net_idx)) return false; l_queue_push_tail(rmt->net_keys, L_UINT_TO_PTR(net_idx)); @@ -146,7 +155,7 @@ bool remote_add_app_key(uint16_t addr, uint16_t app_idx) if (!rmt->app_keys) rmt->app_keys = l_queue_new(); - if (l_queue_find(rmt->app_keys, simple_match, L_UINT_TO_PTR(app_idx))) + if (key_present(rmt->app_keys, app_idx)) return false; l_queue_push_tail(rmt->app_keys, L_UINT_TO_PTR(app_idx)); -- 2.21.0