Skip to contentSkip to conversation

shared/bass: Add Set Broadcast_Code opcode handler

PATCH Applied Iulia Tanasescu 3 participants 6 messages Sep 7, 2023 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

cover shared/bass: Add Set Broadcast_Code opcode handler

This adds the Set Broadcast_Code opcode handler, enabling a BASS Server to sync to an encrypted BIG.

The BASS Server will detect if the Source is advertising an encrypted BIG by looking at the QoS of the PA sync socket, after PA sync has been established and a first BIGInfo report has been received.

If the BIG is encrypted, it will wait for the Client to provide the correct Broadcast Code, before issuing the BIG Create Sync command.

Iulia Tanasescu (3):
btio: Add support for accepting BIS after defer setup
monitor: Fix ATT decoding issue
shared/bass: Add Set Broadcast_Code opcode handler

 btio/btio.c       |  31 +++++++++
 btio/btio.h       |   4 ++
 monitor/packet.c  |   7 +-
 src/shared/bass.c | 159 +++++++++++++++++++++++++++++++++++++++++++---
 src/shared/bass.h |   1 +
 5 files changed, 191 insertions(+), 11 deletions(-)

base-commit: dc13da09d78b3df761d27ee8f4bf7089afe37788

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 btio: Add support for accepting BIS after defer setup

This adds btio support for accepting BIS connections when defer setup is enabled on a Broadcast Receiver socket.

---
 btio/btio.c | 31 +++++++++++++++++++++++++++++++
 btio/btio.h |  4 ++++
 2 files changed, 35 insertions(+)
btio/btio.c +31 -0
index 8d9959038..c6d056b89 100644
@@ -1789,6 +1789,37 @@ gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
 	return TRUE;
 }
 
+gboolean bt_io_bcast_accept(GIOChannel *io, BtIOConnect connect,
+				gpointer user_data, GDestroyNotify destroy,
+				GError * *err)
+{
+	int sock;
+	char c;
+	struct pollfd pfd;
+
+	sock = g_io_channel_unix_get_fd(io);
+
+	memset(&pfd, 0, sizeof(pfd));
+	pfd.fd = sock;
+	pfd.events = POLLOUT;
+
+	if (poll(&pfd, 1, 0) < 0) {
+		ERROR_FAILED(err, "poll", errno);
+		return FALSE;
+	}
+
+	if (!(pfd.revents & POLLOUT)) {
+		if (read(sock, &c, 1) < 0) {
+			ERROR_FAILED(err, "read", errno);
+			return FALSE;
+		}
+	}
+
+	server_add(io, connect, NULL, user_data, destroy);
+
+	return TRUE;
+}
+
 gboolean bt_io_set(GIOChannel *io, GError **err, BtIOOption opt1, ...)
 {
 	va_list args;
btio/btio.h +4 -0
index 642af2e22..3169bebf3 100644
@@ -75,6 +75,10 @@ typedef void (*BtIOConnect)(GIOChannel *io, GError *err, gpointer user_data);
 gboolean bt_io_accept(GIOChannel *io, BtIOConnect connect, gpointer user_data,
 					GDestroyNotify destroy, GError **err);
 
+gboolean bt_io_bcast_accept(GIOChannel *io, BtIOConnect connect,
+				gpointer user_data, GDestroyNotify destroy,
+				GError **err);
+
 gboolean bt_io_set(GIOChannel *io, GError **err, BtIOOption opt1, ...);
 
 gboolean bt_io_get(GIOChannel *io, GError **err, BtIOOption opt1, ...);
Signature · 1 line
2.34.1
#2 lore raw

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=782237

---Test result---

Test Summary:

CheckPatch                    PASS      1.80 seconds
GitLint                       PASS      2.37 seconds
BuildEll                      PASS      27.39 seconds
BluezMake                     PASS      845.83 seconds
MakeCheck                     PASS      11.51 seconds
MakeDistcheck                 PASS      162.80 seconds
CheckValgrind                 PASS      257.55 seconds
CheckSmatch                   WARNING   350.77 seconds
bluezmakeextell               PASS      105.80 seconds
IncrementalBuild              PASS      2088.23 seconds
ScanBuild                     PASS      1073.11 seconds

Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
monitor/packet.c: note: in included file:monitor/display.h:82:26: warning: Variable length array is used.monitor/packet.c:1859:26: warning: Variable length array is used.monitor/packet.c: note: in included file:monitor/bt.h:3606:52: warning: array of flexible structuresmonitor/bt.h:3594:40: warning: array of flexible structures

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH 2/3 monitor: Fix ATT decoding issue

In reply to Iulia Tanasescu

This fixes the way conn handles are marked as unused in the conn_list array, so that valid conn entries are not overwritten by new ones.

---
 monitor/packet.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
monitor/packet.c +5 -2
index 279f5408d..f88a11539 100644
@@ -172,7 +172,9 @@ static uint16_t get_format(uint32_t cookie)
 
 #define MAX_CONN 16
 
-static struct packet_conn_data conn_list[MAX_CONN];
+static struct packet_conn_data conn_list[MAX_CONN] = {
+	 [0 ... MAX_CONN - 1].handle = 0xffff
+};
 
 static struct packet_conn_data *lookup_parent(uint16_t handle)
 {
@@ -192,7 +194,7 @@ static void assign_handle(uint16_t index, uint16_t handle, uint8_t type,
 	int i;
 
 	for (i = 0; i < MAX_CONN; i++) {
-		if (conn_list[i].handle == 0x0000) {
+		if (conn_list[i].handle == 0xffff) {
 			hci_devba(index, (bdaddr_t *)conn_list[i].src);
 
 			conn_list[i].index = index;
@@ -236,6 +238,7 @@ static void release_handle(uint16_t handle)
 			queue_destroy(conn->tx_q, free);
 			queue_destroy(conn->chan_q, free);
 			memset(conn, 0, sizeof(*conn));
+			conn->handle = 0xffff;
 			break;
 		}
 	}
Signature · 1 line
2.34.1
#4 lore raw

PATCH 3/3 shared/bass: Add Set Broadcast_Code opcode handler

In reply to Iulia Tanasescu

This adds the Set Broadcast_Code opcode handler, enabling a BASS Server to sync to an encrypted BIG.

---
 src/shared/bass.c | 159 +++++++++++++++++++++++++++++++++++++++++++---
 src/shared/bass.h |   1 +
 2 files changed, 151 insertions(+), 9 deletions(-)
src/shared/bass.c +150 -9
index 1bb40877f..86dab03e3 100644
@@ -613,9 +613,6 @@ static void connect_cb(GIOChannel *io, GError *gerr,
 	int bis_idx;
 	int i;
 
-	if (bcast_src->sync_state == BT_BASS_NOT_SYNCHRONIZED_TO_PA)
-		bcast_src->sync_state = BT_BASS_SYNCHRONIZED_TO_PA;
-
 	/* Keep io reference */
 	g_io_channel_ref(io);
 	queue_push_tail(bcast_src->bises, io);
@@ -661,9 +658,26 @@ static void connect_cb(GIOChannel *io, GError *gerr,
 		g_io_channel_unref(bcast_src->listen_io);
 		bcast_src->listen_io = NULL;
 
+		/* Close pa sync io */
+		if (bcast_src->pa_sync_io) {
+			g_io_channel_shutdown(bcast_src->pa_sync_io,
+					TRUE, NULL);
+			g_io_channel_unref(bcast_src->pa_sync_io);
+			bcast_src->pa_sync_io = NULL;
+		}
+
 		for (i = 0; i < bcast_src->num_subgroups; i++)
 			bcast_src->subgroup_data[i].bis_sync =
 				BT_BASS_BIG_SYNC_FAILED_BITMASK;
+
+		/* If BIG sync failed because of an incorrect broadcast code,
+		 * inform client
+		 */
+		if (bcast_src->enc == BT_BASS_BIG_ENC_STATE_BCODE_REQ)
+			bcast_src->enc = BT_BASS_BIG_ENC_STATE_BAD_CODE;
+	} else {
+		if (bcast_src->enc == BT_BASS_BIG_ENC_STATE_BCODE_REQ)
+			bcast_src->enc = BT_BASS_BIG_ENC_STATE_DEC;
 	}
 
 	/* Send notification to client */
@@ -678,6 +692,66 @@ static void connect_cb(GIOChannel *io, GError *gerr,
 	free(notify_data);
 }
 
+static void confirm_cb(GIOChannel *io, gpointer user_data)
+{
+	struct bt_bcast_src *bcast_src = user_data;
+	int sk, err;
+	socklen_t len;
+	struct bt_iso_qos qos;
+	uint8_t *notify_data;
+	size_t notify_data_len;
+	GError *gerr = NULL;
+
+	if (check_io_err(io)) {
+		DBG(bcast_src->bass, "PA sync failed");
+
+		/* Mark PA sync as failed and notify client */
+		bcast_src->sync_state = BT_BASS_FAILED_TO_SYNCHRONIZE_TO_PA;
+		goto notify;
+	}
+
+	bcast_src->sync_state = BT_BASS_SYNCHRONIZED_TO_PA;
+	bcast_src->pa_sync_io = io;
+	g_io_channel_ref(bcast_src->pa_sync_io);
+
+	len = sizeof(qos);
+	memset(&qos, 0, len);
+
+	sk = g_io_channel_unix_get_fd(io);
+
+	err = getsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, &len);
+	if (err < 0) {
+		DBG(bcast_src->bass, "Failed to get iso qos");
+		return;
+	}
+
+	if (!qos.bcast.encryption) {
+		/* BIG is not encrypted. Try to synchronize */
+		bcast_src->enc = BT_BASS_BIG_ENC_STATE_NO_ENC;
+
+		if (!bt_io_bcast_accept(bcast_src->pa_sync_io,
+			connect_cb, bcast_src, NULL, &gerr)) {
+			DBG(bcast_src->bass, "bt_io_accept: %s", gerr->message);
+			g_error_free(gerr);
+		}
+		return;
+	}
+
+	/* BIG is encrypted. Wait for Client to provide the Broadcast_Code */
+	bcast_src->enc = BT_BASS_BIG_ENC_STATE_BCODE_REQ;
+
+notify:
+	notify_data = bass_build_notif_from_bcast_src(bcast_src,
+						&notify_data_len);
+
+	gatt_db_attribute_notify(bcast_src->attr,
+					(void *)notify_data,
+					notify_data_len,
+					bt_bass_get_att(bcast_src->bass));
+
+	free(notify_data);
+}
+
 static struct bt_bass *bass_get_session(struct bt_att *att, struct gatt_db *db,
 		const bdaddr_t *adapter_bdaddr)
 {
@@ -796,11 +870,6 @@ static void bass_handle_add_src_op(struct bt_bass *bass,
 	pa_sync = util_iov_pull_mem(iov, sizeof(*pa_sync));
 	bcast_src->sync_state = BT_BASS_NOT_SYNCHRONIZED_TO_PA;
 
-	/* TODO: Set the encryption field based on observed BIGInfo reports,
-	 * after PA sync establishment
-	 */
-	bcast_src->enc = BT_BASS_BIG_ENC_STATE_NO_ENC;
-
 	/* TODO: Use the pa_interval field for the sync transfer procedure */
 	util_iov_pull_mem(iov, sizeof(uint16_t));
 
@@ -852,7 +921,7 @@ static void bass_handle_add_src_op(struct bt_bass *bass,
 
 	if (pa_sync != PA_SYNC_NO_SYNC && num_bis > 0) {
 		/* If requested by client, try to synchronize to the source */
-		io = bt_io_listen(connect_cb, NULL, bcast_src, NULL, &err,
+		io = bt_io_listen(NULL, confirm_cb, bcast_src, NULL, &err,
 					BT_IO_OPT_SOURCE_BDADDR,
 					&bass->ldb->adapter_bdaddr,
 					BT_IO_OPT_DEST_BDADDR,
@@ -906,6 +975,71 @@ err:
 	free(bcast_src);
 }
 
+static void bass_handle_set_bcast_code_op(struct bt_bass *bass,
+					struct gatt_db_attribute *attrib,
+					uint8_t opcode,
+					unsigned int id,
+					struct iovec *iov,
+					struct bt_att *att)
+{
+	struct bt_bass_set_bcast_code_params *params;
+	struct bt_bcast_src *bcast_src;
+	int sk, err;
+	socklen_t len;
+	struct bt_iso_qos qos;
+	GError *gerr = NULL;
+
+	if (opcode == BT_ATT_OP_WRITE_REQ)
+		gatt_db_attribute_write_result(attrib, id, 0x00);
+
+	/* Get Set Broadcast Code command parameters */
+	params = util_iov_pull_mem(iov, sizeof(*params));
+
+	bcast_src = queue_find(bass->ldb->bcast_srcs,
+						bass_src_id_match,
+						&params->id);
+
+	if (!bcast_src) {
+		/* No source matches the written source id */
+		if (opcode == BT_ATT_OP_WRITE_REQ)
+			gatt_db_attribute_write_result(attrib, id,
+					BT_BASS_ERROR_INVALID_SOURCE_ID);
+
+		return;
+	}
+
+	/* Try to sync to the source using the
+	 * received broadcast code
+	 */
+	len = sizeof(qos);
+	memset(&qos, 0, len);
+
+	if (!bcast_src->pa_sync_io)
+		return;
+
+	sk = g_io_channel_unix_get_fd(bcast_src->pa_sync_io);
+
+	err = getsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, &len);
+	if (err < 0) {
+		DBG(bcast_src->bass, "Failed to get iso qos");
+		return;
+	}
+
+	/* Update socket QoS with Broadcast Code */
+	memcpy(qos.bcast.bcode, params->bcast_code, BT_BASS_BCAST_CODE_SIZE);
+
+	if (setsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos,
+				sizeof(qos)) < 0) {
+		DBG(bcast_src->bass, "Failed to set iso qos");
+		return;
+	}
+
+	if (!bt_io_bcast_accept(bcast_src->pa_sync_io, connect_cb,
+		bcast_src, NULL, &gerr)) {
+		DBG(bcast_src->bass, "bt_io_accept: %s", gerr->message);
+		g_error_free(gerr);
+	}
+}
 
 #define BASS_OP(_str, _op, _size, _func) \
 	{ \
@@ -934,6 +1068,8 @@ struct bass_op_handler {
 		0, bass_handle_remove_src_op),
 	BASS_OP("Add Source", BT_BASS_ADD_SRC,
 		0, bass_handle_add_src_op),
+	BASS_OP("Set Broadcast Code", BT_BASS_SET_BCAST_CODE,
+		0, bass_handle_set_bcast_code_op),
 	{}
 };
 
@@ -1092,6 +1228,11 @@ static void bass_bcast_src_free(void *data)
 		g_io_channel_unref(bcast_src->listen_io);
 	}
 
+	if (bcast_src->pa_sync_io) {
+		g_io_channel_shutdown(bcast_src->pa_sync_io, TRUE, NULL);
+		g_io_channel_unref(bcast_src->pa_sync_io);
+	}
+
 	queue_destroy(bcast_src->bises, bass_bis_unref);
 
 	free(bcast_src);
src/shared/bass.h +1 -0
index fb4b72d7d..b3f83b32e 100644
@@ -57,6 +57,7 @@ struct bt_bcast_src {
 	uint8_t num_subgroups;
 	struct bt_bass_subgroup_data *subgroup_data;
 	GIOChannel *listen_io;
+	GIOChannel *pa_sync_io;
 	struct queue *bises;
 };
 
Signature · 1 line
2.34.1
#5 lore raw
In reply to Iulia Tanasescu
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 8 lines · On Thu, 7 Sep 2023 18:12:26 +0300 you wrote:

This adds the Set Broadcast_Code opcode handler, enabling a BASS Server to sync to an encrypted BIG.

The BASS Server will detect if the Source is advertising an encrypted BIG by looking at the QoS of the PA sync socket, after PA sync has been established and a first BIGInfo report has been received.

[...]

Here is the summary with links:
- [BlueZ,1/3] btio: Add support for accepting BIS after defer setup

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ed0def339cca
  - [BlueZ,2/3] monitor: Fix ATT decoding issue
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=42681a7c15ab
  - [BlueZ,3/3] shared/bass: Add Set Broadcast_Code opcode handler
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=df658c6c4ab5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html