All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v3 0/5] Enhance GATT to pass PTS tests
@ 2024-01-25 19:08 Frédéric Danis
  2024-01-25 19:08 ` [PATCH BlueZ v3 1/5] shared/gatt: Prevent security level change for PTS GATT tests Frédéric Danis
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: Frédéric Danis @ 2024-01-25 19:08 UTC (permalink / raw)
  To: linux-bluetooth

The two first patches  allow to prevent automatic security level change
to allow to display the security error when running GATT/CL/GAR/BI-04-C
using btgatt-client.

The other patches add commands to be able to call GATT discovery
functions from btgatt-client and get their results.

v1 -> v2: Re-use att_send_op->retry and make it possible to prevent
            security upgrade on a per operation basis
          Remove "btgatt-client: Add function to search descriptors"
            as GATT/CL/GAD/BV-06-C test is optional
          Fix command arguments check in btgatt-client
v2 -> v3: Split first commit in two, one for src/shared and the other
            for tools directories

Frédéric Danis (5):
  shared/gatt: Prevent security level change for PTS GATT tests
  btgatt-client: Add command to prevent security level change
  btgatt-client: Add function to search service based on UUID
  btgatt-client: Add function to search characteristics
  btgatt-client: Add function to search all primary services

 src/shared/att.c         |  26 ++++
 src/shared/att.h         |   1 +
 src/shared/gatt-client.c |  19 +++
 src/shared/gatt-client.h |   3 +
 tools/btgatt-client.c    | 251 +++++++++++++++++++++++++++++++++++++--
 5 files changed, 288 insertions(+), 12 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 10+ messages in thread
* [PATCH BlueZ v2 1/4] gatt: Prevent security level change for PTS GATT tests
@ 2024-01-23 14:31 Frédéric Danis
  2024-01-23 16:05 ` Enhance GATT to pass PTS tests bluez.test.bot
  0 siblings, 1 reply; 10+ messages in thread
From: Frédéric Danis @ 2024-01-23 14:31 UTC (permalink / raw)
  To: linux-bluetooth

Some PTS GATT tests like GATT/CL/GAR/BI-04-C request to be able to get the
security error and do not try to change the security level.

This commit adds the ability to prevent to change the security level for
an operation.
---
 src/shared/att.c         | 26 +++++++++++++
 src/shared/att.h         |  1 +
 src/shared/gatt-client.c | 19 +++++++++
 src/shared/gatt-client.h |  3 ++
 tools/btgatt-client.c    | 84 ++++++++++++++++++++++++++++++++++------
 5 files changed, 121 insertions(+), 12 deletions(-)

diff --git a/src/shared/att.c b/src/shared/att.c
index 62c884b65..485ef071b 100644
--- a/src/shared/att.c
+++ b/src/shared/att.c
@@ -2042,3 +2042,29 @@ bool bt_att_has_crypto(struct bt_att *att)
 
 	return att->crypto ? true : false;
 }
+
+bool bt_att_set_retry(struct bt_att *att, unsigned int id, bool retry)
+{
+	struct att_send_op *op;
+
+	if (!id)
+		return false;
+
+	op = queue_find(att->req_queue, match_op_id, UINT_TO_PTR(id));
+	if (op)
+		goto done;
+
+	op = queue_find(att->ind_queue, match_op_id, UINT_TO_PTR(id));
+	if (op)
+		goto done;
+
+	op = queue_find(att->write_queue, match_op_id, UINT_TO_PTR(id));
+
+done:
+	if (!op)
+		return false;
+
+	op->retry = !retry;
+
+	return true;
+}
diff --git a/src/shared/att.h b/src/shared/att.h
index 4aa3de87b..6fd78636e 100644
--- a/src/shared/att.h
+++ b/src/shared/att.h
@@ -110,3 +110,4 @@ bool bt_att_set_local_key(struct bt_att *att, uint8_t sign_key[16],
 bool bt_att_set_remote_key(struct bt_att *att, uint8_t sign_key[16],
 			bt_att_counter_func_t func, void *user_data);
 bool bt_att_has_crypto(struct bt_att *att);
+bool bt_att_set_retry(struct bt_att *att, unsigned int id, bool retry);
diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 5de679c9b..6340bcd85 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -3818,3 +3818,22 @@ bool bt_gatt_client_idle_unregister(struct bt_gatt_client *client,
 
 	return false;
 }
+
+bool bt_gatt_client_set_retry(struct bt_gatt_client *client,
+					unsigned int id,
+					bool retry)
+{
+	struct request *req;
+
+	if (!client || !id)
+		return false;
+
+	req = queue_find(client->pending_requests, match_req_id,
+							UINT_TO_PTR(id));
+	if (!req)
+		return false;
+
+	bt_att_set_retry(client->att, req->att_id, retry);
+
+	return true;
+}
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index bccd04a62..63cf99500 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -134,3 +134,6 @@ unsigned int bt_gatt_client_idle_register(struct bt_gatt_client *client,
 					bt_gatt_client_destroy_func_t destroy);
 bool bt_gatt_client_idle_unregister(struct bt_gatt_client *client,
 						unsigned int id);
+bool bt_gatt_client_set_retry(struct bt_gatt_client *client,
+					unsigned int id,
+					bool retry);
diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index 58a03bd48..3bcb7e1cf 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -57,6 +57,7 @@ struct client {
 	struct bt_gatt_client *gatt;
 
 	unsigned int reliable_session_id;
+	bool sec_retry;
 };
 
 static void print_prompt(void)
@@ -172,6 +173,7 @@ static struct client *client_create(int fd, uint16_t mtu)
 		fprintf(stderr, "Failed to allocate memory for client\n");
 		return NULL;
 	}
+	cli->sec_retry = true;
 
 	cli->att = bt_att_new(fd, false);
 	if (!cli->att) {
@@ -488,6 +490,7 @@ static void cmd_read_multiple(struct client *cli, char *cmd_str)
 	char *argv[512];
 	int i;
 	char *endptr = NULL;
+	unsigned int id;
 
 	if (!bt_gatt_client_is_ready(cli->gatt)) {
 		printf("GATT client not initialized\n");
@@ -514,9 +517,12 @@ static void cmd_read_multiple(struct client *cli, char *cmd_str)
 		}
 	}
 
-	if (!bt_gatt_client_read_multiple(cli->gatt, value, argc,
-						read_multiple_cb, NULL, NULL))
+	id = bt_gatt_client_read_multiple(cli->gatt, value, argc,
+						read_multiple_cb, NULL, NULL);
+	if (!id)
 		printf("Failed to initiate read multiple procedure\n");
+	else if (!cli->sec_retry)
+		bt_gatt_client_set_retry(cli->gatt, id, false);
 
 	free(value);
 }
@@ -558,6 +564,7 @@ static void cmd_read_value(struct client *cli, char *cmd_str)
 	int argc = 0;
 	uint16_t handle;
 	char *endptr = NULL;
+	unsigned int id;
 
 	if (!bt_gatt_client_is_ready(cli->gatt)) {
 		printf("GATT client not initialized\n");
@@ -575,9 +582,12 @@ static void cmd_read_value(struct client *cli, char *cmd_str)
 		return;
 	}
 
-	if (!bt_gatt_client_read_value(cli->gatt, handle, read_cb,
-								NULL, NULL))
+	id = bt_gatt_client_read_value(cli->gatt, handle, read_cb,
+					NULL, NULL);
+	if (!id)
 		printf("Failed to initiate read value procedure\n");
+	else if (!cli->sec_retry)
+		bt_gatt_client_set_retry(cli->gatt, id, false);
 }
 
 static void read_long_value_usage(void)
@@ -592,6 +602,7 @@ static void cmd_read_long_value(struct client *cli, char *cmd_str)
 	uint16_t handle;
 	uint16_t offset;
 	char *endptr = NULL;
+	unsigned int id;
 
 	if (!bt_gatt_client_is_ready(cli->gatt)) {
 		printf("GATT client not initialized\n");
@@ -616,9 +627,12 @@ static void cmd_read_long_value(struct client *cli, char *cmd_str)
 		return;
 	}
 
-	if (!bt_gatt_client_read_long_value(cli->gatt, handle, offset, read_cb,
-								NULL, NULL))
+	id = bt_gatt_client_read_long_value(cli->gatt, handle, offset, read_cb,
+								NULL, NULL);
+	if (!id)
 		printf("Failed to initiate read long value procedure\n");
+	else if (!cli->sec_retry)
+		bt_gatt_client_set_retry(cli->gatt, id, false);
 }
 
 static void write_value_usage(void)
@@ -659,6 +673,7 @@ static void cmd_write_value(struct client *cli, char *cmd_str)
 	uint8_t *value = NULL;
 	bool without_response = false;
 	bool signed_write = false;
+	unsigned int id;
 
 	if (!bt_gatt_client_is_ready(cli->gatt)) {
 		printf("GATT client not initialized\n");
@@ -740,10 +755,13 @@ static void cmd_write_value(struct client *cli, char *cmd_str)
 		goto done;
 	}
 
-	if (!bt_gatt_client_write_value(cli->gatt, handle, value, length,
+	id = bt_gatt_client_write_value(cli->gatt, handle, value, length,
 								write_cb,
-								NULL, NULL))
+								NULL, NULL);
+	if (!id)
 		printf("Failed to initiate write procedure\n");
+	else if (!cli->sec_retry)
+		bt_gatt_client_set_retry(cli->gatt, id, false);
 
 done:
 	free(value);
@@ -789,6 +807,7 @@ static void cmd_write_long_value(struct client *cli, char *cmd_str)
 	int length;
 	uint8_t *value = NULL;
 	bool reliable_writes = false;
+	unsigned int id;
 
 	if (!bt_gatt_client_is_ready(cli->gatt)) {
 		printf("GATT client not initialized\n");
@@ -863,11 +882,14 @@ static void cmd_write_long_value(struct client *cli, char *cmd_str)
 		}
 	}
 
-	if (!bt_gatt_client_write_long_value(cli->gatt, reliable_writes, handle,
+	id = bt_gatt_client_write_long_value(cli->gatt, reliable_writes, handle,
 							offset, value, length,
 							write_long_cb,
-							NULL, NULL))
+							NULL, NULL);
+	if (!id)
 		printf("Failed to initiate long write procedure\n");
+	else if (!cli->sec_retry)
+		bt_gatt_client_set_retry(cli->gatt, id, false);
 
 	free(value);
 }
@@ -999,12 +1021,18 @@ done:
 							value, length,
 							write_long_cb, NULL,
 							NULL);
-	if (!cli->reliable_session_id)
+	if (!cli->reliable_session_id) {
 		printf("Failed to proceed prepare write\n");
-	else
+	} else {
+		if (!cli->sec_retry)
+			bt_gatt_client_set_retry(cli->gatt,
+						cli->reliable_session_id,
+						false);
+
 		printf("Prepare write success.\n"
 				"Session id: %d to be used on next write\n",
 						cli->reliable_session_id);
+	}
 
 	free(value);
 }
@@ -1236,6 +1264,36 @@ static void cmd_get_security(struct client *cli, char *cmd_str)
 		printf("Security level: %u\n", level);
 }
 
+static void set_security_retry_usage(void)
+{
+	printf("Usage: set-security-retry <y/n>\n"
+		"e.g.:\n"
+		"\tset-security-retry n\n");
+}
+
+static void cmd_set_security_retry(struct client *cli, char *cmd_str)
+{
+	char *argv[2];
+	int argc = 0;
+
+	if (!bt_gatt_client_is_ready(cli->gatt)) {
+		printf("GATT client not initialized\n");
+		return;
+	}
+
+	if (!parse_args(cmd_str, 1, argv, &argc) || argc != 1) {
+		set_security_retry_usage();
+		return;
+	}
+
+	if (argv[0][0] == 'y')
+		cli->sec_retry = true;
+	else if (argv[0][0] == 'n')
+		cli->sec_retry = false;
+	else
+		printf("Invalid argument: %s\n", argv[0]);
+}
+
 static bool convert_sign_key(char *optarg, uint8_t key[16])
 {
 	int i;
@@ -1327,6 +1385,8 @@ static struct {
 				"\tSet security level on le connection"},
 	{ "get-security", cmd_get_security,
 				"\tGet security level on le connection"},
+	{ "set-security-retry", cmd_set_security_retry,
+			"\tSet retry on security error by elevating security"},
 	{ "set-sign-key", cmd_set_sign_key,
 				"\tSet signing key for signed write command"},
 	{ }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread
* [PATCH BlueZ 1/5] gatt: Prevent security level change for PTS GATT tests
@ 2024-01-22 16:49 Frédéric Danis
  2024-01-22 19:27 ` Enhance GATT to pass PTS tests bluez.test.bot
  0 siblings, 1 reply; 10+ messages in thread
From: Frédéric Danis @ 2024-01-22 16:49 UTC (permalink / raw)
  To: linux-bluetooth

Some PTS GATT tests like GATT/CL/GAR/BI-04-C request to be able to get the
security error and do not try to change the security level.

this commit adds a variable allowing to prevent to change the security
level.
---
 src/shared/att.c         | 14 ++++++++++++++
 src/shared/att.h         |  1 +
 src/shared/gatt-client.c |  9 +++++++++
 src/shared/gatt-client.h |  2 ++
 tools/btgatt-client.c    | 38 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 64 insertions(+)

diff --git a/src/shared/att.c b/src/shared/att.c
index 62c884b65..decc24314 100644
--- a/src/shared/att.c
+++ b/src/shared/att.c
@@ -87,6 +87,8 @@ struct bt_att {
 
 	struct sign_info *local_sign;
 	struct sign_info *remote_sign;
+
+	bool retry_on_sec_error;
 };
 
 struct sign_info {
@@ -786,6 +788,9 @@ static bool handle_error_rsp(struct bt_att_chan *chan, uint8_t *pdu,
 
 	*opcode = rsp->opcode;
 
+	if (!att->retry_on_sec_error)
+		return false;
+
 	/* If operation has already been marked as retry don't attempt to change
 	 * the security again.
 	 */
@@ -1262,6 +1267,7 @@ struct bt_att *bt_att_new(int fd, bool ext_signed)
 	att = new0(struct bt_att, 1);
 	att->chans = queue_new();
 	att->mtu = chan->mtu;
+	att->retry_on_sec_error = true;
 
 	/* crypto is optional, if not available leave it NULL */
 	if (!ext_signed)
@@ -2042,3 +2048,11 @@ bool bt_att_has_crypto(struct bt_att *att)
 
 	return att->crypto ? true : false;
 }
+
+void bt_att_set_retry_on_sec_error(struct bt_att *att, bool retry_on_sec_error)
+{
+	if (!att)
+		return;
+
+	att->retry_on_sec_error = retry_on_sec_error;
+}
diff --git a/src/shared/att.h b/src/shared/att.h
index 4aa3de87b..8ed89ba80 100644
--- a/src/shared/att.h
+++ b/src/shared/att.h
@@ -110,3 +110,4 @@ bool bt_att_set_local_key(struct bt_att *att, uint8_t sign_key[16],
 bool bt_att_set_remote_key(struct bt_att *att, uint8_t sign_key[16],
 			bt_att_counter_func_t func, void *user_data);
 bool bt_att_has_crypto(struct bt_att *att);
+void bt_att_set_retry_on_sec_error(struct bt_att *att, bool retry_on_sec_error);
diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 5de679c9b..b484db9db 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -3818,3 +3818,12 @@ bool bt_gatt_client_idle_unregister(struct bt_gatt_client *client,
 
 	return false;
 }
+
+void bt_gatt_client_set_retry_on_sec_error(struct bt_gatt_client *client,
+						bool retry_on_sec_error)
+{
+	if (!client)
+		return;
+
+	bt_att_set_retry_on_sec_error(client->att, retry_on_sec_error);
+}
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index bccd04a62..fdb841df0 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -134,3 +134,5 @@ unsigned int bt_gatt_client_idle_register(struct bt_gatt_client *client,
 					bt_gatt_client_destroy_func_t destroy);
 bool bt_gatt_client_idle_unregister(struct bt_gatt_client *client,
 						unsigned int id);
+void bt_gatt_client_set_retry_on_sec_error(struct bt_gatt_client *client,
+						bool retry_on_sec_error);
diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index 58a03bd48..76c74c7a8 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -1295,6 +1295,42 @@ static void cmd_set_sign_key(struct client *cli, char *cmd_str)
 		set_sign_key_usage();
 }
 
+static void set_retry_on_sec_error_usage(void)
+{
+	printf("Usage: set-retry-on-sec-error <y/n>\n"
+		"e.g.:\n"
+		"\tset-retry-on-sec-error n\n");
+}
+
+static void cmd_set_retry_on_sec_error(struct client *cli, char *cmd_str)
+{
+	char *argv[2];
+	int argc = 0;
+
+	if (!bt_gatt_client_is_ready(cli->gatt)) {
+		printf("GATT client not initialized\n");
+		return;
+	}
+
+	if (!parse_args(cmd_str, 1, argv, &argc)) {
+		printf("Too many arguments\n");
+		set_retry_on_sec_error_usage();
+		return;
+	}
+
+	if (argc < 1) {
+		set_retry_on_sec_error_usage();
+		return;
+	}
+
+	if (argv[0][0] == 'y')
+		bt_gatt_client_set_retry_on_sec_error(cli->gatt, true);
+	else if (argv[0][0] == 'n')
+		bt_gatt_client_set_retry_on_sec_error(cli->gatt, false);
+	else
+		printf("Invalid argument: %s\n", argv[0]);
+}
+
 static void cmd_help(struct client *cli, char *cmd_str);
 
 typedef void (*command_func_t)(struct client *cli, char *cmd_str);
@@ -1329,6 +1365,8 @@ static struct {
 				"\tGet security level on le connection"},
 	{ "set-sign-key", cmd_set_sign_key,
 				"\tSet signing key for signed write command"},
+	{ "set-retry-on-sec-error", cmd_set_retry_on_sec_error,
+			"\tSet retry on security error by elevating security"},
 	{ }
 };
 
-- 
2.34.1


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

end of thread, other threads:[~2024-01-25 22:20 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-25 19:08 [PATCH BlueZ v3 0/5] Enhance GATT to pass PTS tests Frédéric Danis
2024-01-25 19:08 ` [PATCH BlueZ v3 1/5] shared/gatt: Prevent security level change for PTS GATT tests Frédéric Danis
2024-01-25 21:13   ` Enhance GATT to pass PTS tests bluez.test.bot
2024-01-25 19:08 ` [PATCH BlueZ v3 2/5] btgatt-client: Add command to prevent security level change Frédéric Danis
2024-01-25 19:08 ` [PATCH BlueZ v3 3/5] btgatt-client: Add function to search service based on UUID Frédéric Danis
2024-01-25 19:08 ` [PATCH BlueZ v3 4/5] btgatt-client: Add function to search characteristics Frédéric Danis
2024-01-25 19:08 ` [PATCH BlueZ v3 5/5] btgatt-client: Add function to search all primary services Frédéric Danis
2024-01-25 22:20 ` [PATCH BlueZ v3 0/5] Enhance GATT to pass PTS tests patchwork-bot+bluetooth
  -- strict thread matches above, loose matches on Subject: below --
2024-01-23 14:31 [PATCH BlueZ v2 1/4] gatt: Prevent security level change for PTS GATT tests Frédéric Danis
2024-01-23 16:05 ` Enhance GATT to pass PTS tests bluez.test.bot
2024-01-22 16:49 [PATCH BlueZ 1/5] gatt: Prevent security level change for PTS GATT tests Frédéric Danis
2024-01-22 19:27 ` Enhance GATT to pass PTS tests bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.