linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ 1/4] shared/mgmt: Set MTU to UINT16_MAX
@ 2021-09-22 21:33 Luiz Augusto von Dentz
  2021-09-22 21:34 ` [PATCH BlueZ 2/4] lib: Fix HCI_MAX_ACL_SIZE Luiz Augusto von Dentz
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2021-09-22 21:33 UTC (permalink / raw)
  To: linux-bluetooth

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

This sets MTU of MGMT socket to UINT16_MAX since some commands may
require more than the default size (e.g. Load LTKs).

Fixes: https://github.com/bluez/bluez/issues/201
---
 src/shared/mgmt.c | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/shared/mgmt.c b/src/shared/mgmt.c
index b869fa6ef..cec8993e7 100644
--- a/src/shared/mgmt.c
+++ b/src/shared/mgmt.c
@@ -42,6 +42,7 @@ struct mgmt {
 	bool in_notify;
 	void *buf;
 	uint16_t len;
+	uint16_t mtu;
 	mgmt_debug_func_t debug_callback;
 	mgmt_destroy_func_t debug_destroy;
 	void *debug_data;
@@ -380,6 +381,32 @@ static bool can_read_data(struct io *io, void *user_data)
 	return true;
 }
 
+static void mgmt_set_mtu(struct mgmt *mgmt)
+{
+	socklen_t len = 0;
+
+	/* Check if kernel support BT_SNDMTU to read the current MTU set */
+	if (getsockopt(mgmt->fd, SOL_BLUETOOTH, BT_SNDMTU, &mgmt->mtu,
+							&len) < 0) {
+		/* If BT_SNDMTU is not supported then MTU cannot be changed and
+		 * MTU is fixed to HCI_MAX_ACL_SIZE.
+		 */
+		mgmt->mtu = HCI_MAX_ACL_SIZE;
+		return;
+	}
+
+	if (mgmt->mtu < UINT16_MAX) {
+		uint16_t mtu = UINT16_MAX;
+
+		/* Try increasing the MTU since some commands may go
+		 * over HCI_MAX_ACL_SIZE (1024)
+		 */
+		if (!setsockopt(mgmt->fd, SOL_BLUETOOTH, BT_SNDMTU, &mtu,
+							sizeof(mtu)))
+			mgmt->mtu = mtu;
+	}
+}
+
 struct mgmt *mgmt_new(int fd)
 {
 	struct mgmt *mgmt;
@@ -423,6 +450,8 @@ struct mgmt *mgmt_new(int fd)
 
 	mgmt->writer_active = false;
 
+	mgmt_set_mtu(mgmt);
+
 	return mgmt_ref(mgmt);
 }
 
@@ -534,9 +563,9 @@ bool mgmt_set_close_on_unref(struct mgmt *mgmt, bool do_close)
 	return true;
 }
 
-static struct mgmt_request *create_request(uint16_t opcode, uint16_t index,
-				uint16_t length, const void *param,
-				mgmt_request_func_t callback,
+static struct mgmt_request *create_request(struct mgmt *mgmt, uint16_t opcode,
+				uint16_t index, uint16_t length,
+				const void *param, mgmt_request_func_t callback,
 				void *user_data, mgmt_destroy_func_t destroy)
 {
 	struct mgmt_request *request;
@@ -548,6 +577,11 @@ static struct mgmt_request *create_request(uint16_t opcode, uint16_t index,
 	if (length > 0 && !param)
 		return NULL;
 
+	if (length > mgmt->mtu) {
+		printf("length %u > %u mgmt->mtu", length, mgmt->mtu);
+		return NULL;
+	}
+
 	request = new0(struct mgmt_request, 1);
 	request->len = length + MGMT_HDR_SIZE;
 	request->buf = malloc(request->len);
@@ -711,7 +745,7 @@ unsigned int mgmt_send(struct mgmt *mgmt, uint16_t opcode, uint16_t index,
 	if (!mgmt)
 		return 0;
 
-	request = create_request(opcode, index, length, param,
+	request = create_request(mgmt, opcode, index, length, param,
 					callback, user_data, destroy);
 	if (!request)
 		return 0;
@@ -742,7 +776,7 @@ unsigned int mgmt_send_nowait(struct mgmt *mgmt, uint16_t opcode, uint16_t index
 	if (!mgmt)
 		return 0;
 
-	request = create_request(opcode, index, length, param,
+	request = create_request(mgmt, opcode, index, length, param,
 					callback, user_data, destroy);
 	if (!request)
 		return 0;
@@ -768,7 +802,7 @@ unsigned int mgmt_reply(struct mgmt *mgmt, uint16_t opcode, uint16_t index,
 	if (!mgmt)
 		return 0;
 
-	request = create_request(opcode, index, length, param,
+	request = create_request(mgmt, opcode, index, length, param,
 					callback, user_data, destroy);
 	if (!request)
 		return 0;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH BlueZ 2/4] lib: Fix HCI_MAX_ACL_SIZE
  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 ` Luiz Augusto von Dentz
  2021-09-22 21:34 ` [PATCH BlueZ 3/4] shared/mgmt: Add mgmt_get_mtu Luiz Augusto von Dentz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2021-09-22 21:34 UTC (permalink / raw)
  To: linux-bluetooth

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

HCI_MAX_ACL_SIZE actually includes the maximum frames in AMP controller
so this changes it to properly define the max ACL frames on non-AMP
controllers and introduces a dedicated define for AMP controller as
HCI_MAX_AMP_SIZE.
---
 lib/hci.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/hci.h b/lib/hci.h
index a61568bce..50f385c1e 100644
--- a/lib/hci.h
+++ b/lib/hci.h
@@ -21,10 +21,11 @@ extern "C" {
 
 #define HCI_MAX_DEV	16
 
-#define HCI_MAX_ACL_SIZE	(1492 + 4)
+#define HCI_MAX_AMP_SIZE	(1492 + 4)
+#define HCI_MAX_ACL_SIZE	1024
 #define HCI_MAX_SCO_SIZE	255
 #define HCI_MAX_EVENT_SIZE	260
-#define HCI_MAX_FRAME_SIZE	(HCI_MAX_ACL_SIZE + 4)
+#define HCI_MAX_FRAME_SIZE	(HCI_MAX_AMP_SIZE + 4)
 
 /* HCI dev events */
 #define HCI_DEV_REG	1
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH BlueZ 3/4] shared/mgmt: Add mgmt_get_mtu
  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 ` Luiz Augusto von Dentz
  2021-09-22 21:34 ` [PATCH BlueZ 4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2021-09-22 21:34 UTC (permalink / raw)
  To: linux-bluetooth

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

This adds mgmt_get_mtu function which can be used to query the
transport MTU.
---
 src/shared/mgmt.c | 8 ++++++++
 src/shared/mgmt.h | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/src/shared/mgmt.c b/src/shared/mgmt.c
index cec8993e7..41457b430 100644
--- a/src/shared/mgmt.c
+++ b/src/shared/mgmt.c
@@ -963,3 +963,11 @@ bool mgmt_unregister_all(struct mgmt *mgmt)
 
 	return true;
 }
+
+uint16_t mgmt_get_mtu(struct mgmt *mgmt)
+{
+	if (!mgmt)
+		return 0;
+
+	return mgmt->mtu;
+}
diff --git a/src/shared/mgmt.h b/src/shared/mgmt.h
index 808bf4c7f..56add613d 100644
--- a/src/shared/mgmt.h
+++ b/src/shared/mgmt.h
@@ -76,3 +76,5 @@ unsigned int mgmt_register(struct mgmt *mgmt, uint16_t event, uint16_t index,
 bool mgmt_unregister(struct mgmt *mgmt, unsigned int id);
 bool mgmt_unregister_index(struct mgmt *mgmt, uint16_t index);
 bool mgmt_unregister_all(struct mgmt *mgmt);
+
+uint16_t mgmt_get_mtu(struct mgmt *mgmt);
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH BlueZ 4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
  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
  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
  4 siblings, 0 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2021-09-22 21:34 UTC (permalink / raw)
  To: linux-bluetooth

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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* RE: [BlueZ,1/4] shared/mgmt: Set MTU to UINT16_MAX
  2021-09-22 21:33 [PATCH BlueZ 1/4] shared/mgmt: Set MTU to UINT16_MAX Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2021-09-22 21:34 ` [PATCH BlueZ 4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU Luiz Augusto von Dentz
@ 2021-09-22 21:55 ` bluez.test.bot
  2021-09-22 22:35 ` bluez.test.bot
  4 siblings, 0 replies; 7+ messages in thread
From: bluez.test.bot @ 2021-09-22 21:55 UTC (permalink / raw)
  To: linux-bluetooth, luiz.dentz

[-- Attachment #1: Type: text/plain, Size: 3297 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=551265

---Test result---

Test Summary:
CheckPatch                    FAIL      5.51 seconds
GitLint                       FAIL      3.56 seconds
Prep - Setup ELL              PASS      50.85 seconds
Build - Prep                  PASS      0.47 seconds
Build - Configure             PASS      9.27 seconds
Build - Make                  PASS      215.58 seconds
Make Check                    PASS      10.15 seconds
Make Distcheck                PASS      256.89 seconds
Build w/ext ELL - Configure   PASS      9.66 seconds
Build w/ext ELL - Make        PASS      203.86 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf
Output:
[BlueZ,4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
WARNING:TYPO_SPELLING: 'accomodate' may be misspelled - perhaps 'accommodate'?
#91: 
If MGMT MTU cannot accomodate all the existing LTKs only send the ones
                   ^^^^^^^^^^

/github/workspace/src/12511495.patch total: 0 errors, 1 warnings, 64 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12511495.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.


##############################
Test: GitLint - FAIL
Desc: Run gitlint with rule in .gitlint
Output:
[BlueZ,1/4] shared/mgmt: Set MTU to UINT16_MAX
Config Error: Source contains parsing errors: '/.gitlint'
	[line 52]: '~                                                                                                                                               \n'
	[line 53]: '~                                             \n'

[BlueZ,2/4] lib: Fix HCI_MAX_ACL_SIZE
Config Error: Source contains parsing errors: '/.gitlint'
	[line 52]: '~                                                                                                                                               \n'
	[line 53]: '~                                             \n'

[BlueZ,3/4] shared/mgmt: Add mgmt_get_mtu
Config Error: Source contains parsing errors: '/.gitlint'
	[line 52]: '~                                                                                                                                               \n'
	[line 53]: '~                                             \n'

[BlueZ,4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
Config Error: Source contains parsing errors: '/.gitlint'
	[line 52]: '~                                                                                                                                               \n'
	[line 53]: '~                                             \n'




---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [BlueZ,1/4] shared/mgmt: Set MTU to UINT16_MAX
  2021-09-22 21:33 [PATCH BlueZ 1/4] shared/mgmt: Set MTU to UINT16_MAX Luiz Augusto von Dentz
                   ` (3 preceding siblings ...)
  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
  4 siblings, 1 reply; 7+ messages in thread
From: bluez.test.bot @ 2021-09-22 22:35 UTC (permalink / raw)
  To: linux-bluetooth, luiz.dentz

[-- Attachment #1: Type: text/plain, Size: 1867 bytes --]

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=551265

---Test result---

Test Summary:
CheckPatch                    FAIL      5.36 seconds
GitLint                       PASS      3.57 seconds
Prep - Setup ELL              PASS      40.85 seconds
Build - Prep                  PASS      0.48 seconds
Build - Configure             PASS      7.30 seconds
Build - Make                  PASS      173.82 seconds
Make Check                    PASS      8.95 seconds
Make Distcheck                PASS      204.82 seconds
Build w/ext ELL - Configure   PASS      7.29 seconds
Build w/ext ELL - Make        PASS      163.18 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script with rule in .checkpatch.conf
Output:
[BlueZ,4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
WARNING:TYPO_SPELLING: 'accomodate' may be misspelled - perhaps 'accommodate'?
#91: 
If MGMT MTU cannot accomodate all the existing LTKs only send the ones
                   ^^^^^^^^^^

/github/workspace/src/12511495.patch total: 0 errors, 1 warnings, 64 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/12511495.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.




---
Regards,
Linux Bluetooth


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [BlueZ,1/4] shared/mgmt: Set MTU to UINT16_MAX
  2021-09-22 22:35 ` bluez.test.bot
@ 2021-09-23 21:25   ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 7+ messages in thread
From: Luiz Augusto von Dentz @ 2021-09-23 21:25 UTC (permalink / raw)
  To: linux-bluetooth

Hi,

On Wed, Sep 22, 2021 at 3:35 PM <bluez.test.bot@gmail.com> wrote:
>
> This is automated email and please do not reply to this email!
>
> Dear submitter,
>
> Thank you for submitting the patches to the linux bluetooth mailing list.
> This is a CI test results with your patch series:
> PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=551265
>
> ---Test result---
>
> Test Summary:
> CheckPatch                    FAIL      5.36 seconds
> GitLint                       PASS      3.57 seconds
> Prep - Setup ELL              PASS      40.85 seconds
> Build - Prep                  PASS      0.48 seconds
> Build - Configure             PASS      7.30 seconds
> Build - Make                  PASS      173.82 seconds
> Make Check                    PASS      8.95 seconds
> Make Distcheck                PASS      204.82 seconds
> Build w/ext ELL - Configure   PASS      7.29 seconds
> Build w/ext ELL - Make        PASS      163.18 seconds
>
> Details
> ##############################
> Test: CheckPatch - FAIL
> Desc: Run checkpatch.pl script with rule in .checkpatch.conf
> Output:
> [BlueZ,4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU
> WARNING:TYPO_SPELLING: 'accomodate' may be misspelled - perhaps 'accommodate'?
> #91:
> If MGMT MTU cannot accomodate all the existing LTKs only send the ones
>                    ^^^^^^^^^^
>
> /github/workspace/src/12511495.patch total: 0 errors, 1 warnings, 64 lines checked
>
> NOTE: For some of the reported defects, checkpatch may be able to
>       mechanically convert to the typical style using --fix or --fix-inplace.
>
> /github/workspace/src/12511495.patch has style problems, please review.
>
> NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO
>
> NOTE: If any of the errors are false positives, please report
>       them to the maintainer, see CHECKPATCH in MAINTAINERS.
>
>
>
>
> ---
> Regards,
> Linux Bluetooth
>

Pushed.

-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-23 21:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH BlueZ 4/4] adapter: Truncate number of LTKs loaded if over MGMT MTU Luiz Augusto von Dentz
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).