public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ 4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
Date: Wed, 22 Sep 2021 14:34:02 -0700	[thread overview]
Message-ID: <20210922213402.1978215-4-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20210922213402.1978215-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If MGMT MTU cannot accomodate all the existing LTKs only send the ones
that fit in the MTU and leave the remaining as unpaired.
---
 src/adapter.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 45c187a18..5846f0396 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -4163,8 +4163,9 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
 {
 	struct mgmt_cp_load_long_term_keys *cp;
 	struct mgmt_ltk_info *key;
-	size_t key_count, cp_size;
+	size_t key_count, max_key_count, cp_size;
 	GSList *l;
+	uint16_t mtu;
 
 	/*
 	 * If the controller does not support Low Energy operation,
@@ -4180,6 +4181,9 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
 		return;
 
 	key_count = g_slist_length(keys);
+	mtu = mgmt_get_mtu(adapter->mgmt);
+	max_key_count = (mtu - sizeof(*cp)) / sizeof(*key);
+	key_count = MIN(max_key_count, key_count);
 
 	DBG("hci%u keys %zu", adapter->dev_id, key_count);
 
@@ -4199,8 +4203,10 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
 	 */
 	cp->key_count = htobs(key_count);
 
-	for (l = keys, key = cp->keys; l != NULL; l = g_slist_next(l), key++) {
+	for (l = keys, key = cp->keys; l && key_count;
+			l = g_slist_next(l), key++, key_count--) {
 		struct smp_ltk_info *info = l->data;
+		struct btd_device *dev;
 
 		bacpy(&key->addr.bdaddr, &info->bdaddr);
 		key->addr.type = info->bdaddr_type;
@@ -4210,6 +4216,16 @@ static void load_ltks(struct btd_adapter *adapter, GSList *keys)
 		key->type = info->authenticated;
 		key->central = info->central;
 		key->enc_size = info->enc_size;
+
+		/* Mark device as paired as their LTKs can be loaded. */
+		dev = btd_adapter_find_device(adapter, &info->bdaddr,
+							info->bdaddr_type);
+		if (dev) {
+			device_set_paired(dev, info->bdaddr_type);
+			device_set_bonded(dev, info->bdaddr_type);
+			device_set_ltk_enc_size(dev, info->enc_size);
+			device_set_ltk_enc_size(dev, info->enc_size);
+		}
 	}
 
 	adapter->load_ltks_id = mgmt_send(adapter->mgmt,
@@ -4769,18 +4785,6 @@ device_exist:
 			device_set_bonded(device, BDADDR_BREDR);
 		}
 
-		if (ltk_info || peripheral_ltk_info) {
-			device_set_paired(device, bdaddr_type);
-			device_set_bonded(device, bdaddr_type);
-
-			if (ltk_info)
-				device_set_ltk_enc_size(device,
-							ltk_info->enc_size);
-			else if (peripheral_ltk_info)
-				device_set_ltk_enc_size(device,
-						peripheral_ltk_info->enc_size);
-		}
-
 free:
 		g_key_file_free(key_file);
 	}
-- 
2.31.1


  parent reply	other threads:[~2021-09-22 21:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22 21:33 [PATCH BlueZ 1/4] shared/mgmt: Set MTU to UINT16_MAX Luiz Augusto von Dentz
2021-09-22 21:34 ` [PATCH BlueZ 2/4] lib: Fix HCI_MAX_ACL_SIZE Luiz Augusto von Dentz
2021-09-22 21:34 ` [PATCH BlueZ 3/4] shared/mgmt: Add mgmt_get_mtu Luiz Augusto von Dentz
2021-09-22 21:34 ` Luiz Augusto von Dentz [this message]
2021-09-22 21:55 ` [BlueZ,1/4] shared/mgmt: Set MTU to UINT16_MAX bluez.test.bot
2021-09-22 22:35 ` bluez.test.bot
2021-09-23 21:25   ` Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210922213402.1978215-4-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox