Skip to contentSkip to conversation

shared/att: Auto retry if request fails

PATCH Applied Luiz Augusto von Dentz 1 participant 12 messages Jun 23, 2015 – Jun 24, 2015 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/11 shared/att: Auto retry if request fails

From: Luiz Augusto von Dentz <[email protected]>

This adds auto retry logic to requests when a security error happen, it will first attempt to elevate the security to match the requirement and then retry sending the request once again.

---
 src/shared/att.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 4 deletions(-)
src/shared/att.c +54 -4
index f9a5817..e8be4cc 100644
@@ -576,6 +576,55 @@ static bool disconnect_cb(struct io *io, void *user_data)
 	return false;
 }
 
+static bool change_security(struct bt_att *att, uint8_t ecode)
+{
+	int security;
+
+	security = bt_att_get_security(att);
+	if (ecode == BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION &&
+					security < BT_ATT_SECURITY_MEDIUM)
+		security = BT_ATT_SECURITY_MEDIUM;
+	else if (ecode == BT_ATT_ERROR_AUTHENTICATION &&
+					security < BT_ATT_SECURITY_HIGH)
+		security = BT_ATT_SECURITY_HIGH;
+	else
+		return false;
+
+	return bt_att_set_security(att, security);
+}
+
+static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu,
+					ssize_t pdu_len, uint8_t *opcode)
+{
+	const struct bt_att_pdu_error_rsp *rsp;
+	struct att_send_op *op = att->pending_req;
+
+	if (pdu_len != sizeof(*rsp)) {
+		*opcode = 0;
+		return false;
+	}
+
+	rsp = (void *) pdu;
+
+	*opcode = rsp->opcode;
+
+	/* Only try to change security on L2CAP */
+	if (!att->io_on_l2cap)
+		return false;
+
+	/* Attempt to change security */
+	if (!change_security(att, rsp->ecode))
+		return false;
+
+	util_debug(att->debug_callback, att->debug_data,
+						"Retrying operation %p", op);
+
+	att->pending_req = NULL;
+
+	/* Push operation back to request queue */
+	return queue_push_head(att->req_queue, op);
+}
+
 static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
 								ssize_t pdu_len)
 {
@@ -601,10 +650,11 @@ static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
 	 * the request is malformed, end the current request with failure.
 	 */
 	if (opcode == BT_ATT_OP_ERROR_RSP) {
-		if (pdu_len != 4)
-			goto fail;
-
-		req_opcode = pdu[0];
+		/* Return if error response cause a retry */
+		if (handle_error_rsp(att, pdu, pdu_len, &req_opcode)) {
+			wakeup_writer(att);
+			return;
+		}
 	} else if (!(req_opcode = get_req_opcode(opcode)))
 		goto fail;
 
Signature · 1 line
2.1.0
author maintainer
#1 lore raw

PATCH 2/11 shared/att: Add BT_ATT_SECURITY_AUTO

From: Luiz Augusto von Dentz <[email protected]>

This security level enables auto elevation (on by default), this would be useful for unit tests since it can now control this behavior (default for non L2CAP transport is BT_ATT_SECURITY_LOW).

---
 src/shared/att-types.h |  2 +-
 src/shared/att.c       | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
src/shared/att-types.h +1 -1
index ee20992..d474495 100644
@@ -27,7 +27,7 @@
 #define __packed __attribute__((packed))
 #endif
 
-#define BT_ATT_SECURITY_NONE	0
+#define BT_ATT_SECURITY_AUTO	0
 #define BT_ATT_SECURITY_LOW	1
 #define BT_ATT_SECURITY_MEDIUM	2
 #define BT_ATT_SECURITY_HIGH	3
src/shared/att.c +5 -5
index e8be4cc..70f5d72 100644
@@ -581,6 +581,9 @@ static bool change_security(struct bt_att *att, uint8_t ecode)
 	int security;
 
 	security = bt_att_get_security(att);
+	if (security != BT_ATT_SECURITY_AUTO)
+		return false;
+
 	if (ecode == BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION &&
 					security < BT_ATT_SECURITY_MEDIUM)
 		security = BT_ATT_SECURITY_MEDIUM;
@@ -608,10 +611,6 @@ static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu,
 
 	*opcode = rsp->opcode;
 
-	/* Only try to change security on L2CAP */
-	if (!att->io_on_l2cap)
-		return false;
-
 	/* Attempt to change security */
 	if (!change_security(att, rsp->ecode))
 		return false;
@@ -1416,7 +1415,8 @@ bool bt_att_set_security(struct bt_att *att, int level)
 {
 	struct bt_security sec;
 
-	if (!att || level < BT_ATT_SECURITY_LOW || level > BT_ATT_SECURITY_HIGH)
+	if (!att || level < BT_ATT_SECURITY_AUTO ||
+						level > BT_ATT_SECURITY_HIGH)
 		return false;
 
 	if (!att->io_on_l2cap) {
Signature · 1 line
2.1.0
author maintainer
#2 lore raw

PATCH 3/11 unit/test-gatt: Use auto security

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

If the test don't expect any errors to happen use set security to auto so bt_att will take care of elevating the security whenever needed.

---
 unit/test-gatt.c | 4 ++++
 1 file changed, 4 insertions(+)
unit/test-gatt.c +4 -0
index bb6a270..9a9fa60 100644
@@ -630,6 +630,10 @@ static void client_ready_cb(bool success, uint8_t att_ecode, void *user_data)
 	if (context->data->step) {
 		const struct test_step *step = context->data->step;
 
+		/* Auto elevate security for test that don't expect error */
+		if (!step->expected_att_ecode)
+			bt_att_set_security(context->att, BT_ATT_SECURITY_AUTO);
+
 		step->func(context);
 		return;
 	}
Signature · 1 line
2.1.0
author maintainer
#3 lore raw

PATCH 4/11 unit/test-gatt: Add /TP/GAR/CL/BI-04-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAR/CL/BI-04-C/auto is similar to TP/GAR/CL/BI-04-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 9a9fa60..2d9128e 100644
@@ -2691,6 +2691,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x0a, 0x03, 0x00),
 			raw_pdu(0x01, 0x0a, 0x03, 0x00, 0x05));
 
+	define_test_client("/TP/GAR/CL/BI-04-C/auto", test_client, service_db_1,
+			&test_read_1,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x0a, 0x03, 0x00),
+			raw_pdu(0x01, 0x0a, 0x03, 0x00, 0x05),
+			raw_pdu(0x0a, 0x03, 0x00),
+			raw_pdu(0x0b, 0x01, 0x02, 0x03));
+
 	define_test_client("/TP/GAR/CL/BI-05-C", test_client, service_db_1,
 			&test_read_6,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#4 lore raw

PATCH 5/11 unit/test-gatt: Add /TP/GAR/CL/BI-16-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAR/CL/BI-16-C/auto is similar to /TP/GAR/CL/BI-16-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 2d9128e..5d1650f 100644
@@ -3041,6 +3041,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x0c, 0x03, 0x00, 0x00, 0x00),
 			raw_pdu(0x01, 0x0c, 0x03, 0x00, 0x05));
 
+	define_test_client("/TP/GAR/CL/BI-16-C/auto", test_client, service_db_1,
+			&test_long_read_1,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x0c, 0x03, 0x00, 0x00, 0x00),
+			raw_pdu(0x01, 0x0c, 0x03, 0x00, 0x05),
+			raw_pdu(0x0c, 0x03, 0x00, 0x00, 0x00),
+			raw_pdu(0x0d, 0x01, 0x02, 0x03));
+
 	define_test_client("/TP/GAR/CL/BI-17-C", test_client, service_db_1,
 			&test_long_read_8,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#5 lore raw

PATCH 6/11 unit/test-gatt: Add /TP/GAR/CL/BI-21-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAR/CL/BI-21-C/auto is similar to /TP/GAR/CL/BI-21-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 5d1650f..a131501 100644
@@ -3079,6 +3079,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x0e, 0x03, 0x00, 0x07, 0x00),
 			raw_pdu(0x01, 0x0e, 0x03, 0x00, 0x05));
 
+	define_test_client("/TP/GAR/CL/BI-21-C/auto", test_client, service_db_1,
+			&test_multiple_read_1,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x0e, 0x03, 0x00, 0x07, 0x00),
+			raw_pdu(0x01, 0x0e, 0x03, 0x00, 0x05),
+			raw_pdu(0x0e, 0x03, 0x00, 0x07, 0x00),
+			raw_pdu(0x0f, 0x01, 0x02, 0x03));
+
 	define_test_client("/TP/GAR/CL/BI-22-C", test_client, service_db_1,
 			&test_multiple_read_6,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#6 lore raw

PATCH 7/11 unit/test-gatt: Add /TP/GAR/CL/BI-26-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAR/CL/BI-26-C/auto is similar to /TP/GAR/CL/BI-26-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index a131501..69dcbf9 100644
@@ -3282,6 +3282,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x0a, 0x04, 0x00),
 			raw_pdu(0x01, 0x0a, 0x04, 0x00, 0x05));
 
+	define_test_client("/TP/GAR/CL/BI-26-C/auto", test_client, service_db_1,
+			&test_read_7,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x0a, 0x04, 0x00),
+			raw_pdu(0x01, 0x0a, 0x04, 0x00, 0x05),
+			raw_pdu(0x0a, 0x04, 0x00),
+			raw_pdu(0x0b, 0x01, 0x02, 0x03));
+
 	define_test_client("/TP/GAR/CL/BI-27-C", test_client, service_db_1,
 			&test_read_11,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#7 lore raw

PATCH 8/11 unit/test-gatt: Add /TP/GAR/CL/BI-32-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAR/CL/BI-32-C/auto is similar to /TP/GAR/CL/BI-32-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 69dcbf9..2737bd4 100644
@@ -3442,6 +3442,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x0c, 0x04, 0x00, 0x00, 0x00),
 			raw_pdu(0x01, 0x0c, 0x04, 0x00, 0x05));
 
+	define_test_client("/TP/GAR/CL/BI-32-C/auto", test_client, service_db_1,
+			&test_long_read_9,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x0c, 0x04, 0x00, 0x00, 0x00),
+			raw_pdu(0x01, 0x0c, 0x04, 0x00, 0x05),
+			raw_pdu(0x0c, 0x04, 0x00, 0x00, 0x00),
+			raw_pdu(0x0d, 0x01, 0x02, 0x03));
+
 	define_test_client("/TP/GAR/CL/BI-33-C", test_client, service_db_1,
 			&test_long_read_15,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#8 lore raw

PATCH 9/11 unit/test-gatt: Add /TP/GAW/CL/BI-05-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAW/CL/BI-05-C/auto is similar to /TP/GAW/CL/BI-05-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 2737bd4..6df6342 100644
@@ -3515,6 +3515,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x12, 0x07, 0x00, 0x01, 0x02, 0x03),
 			raw_pdu(0x01, 0x12, 0x07, 0x00, 0x05));
 
+	define_test_client("/TP/GAW/CL/BI-05-C/auto", test_client, service_db_1,
+			&test_write_1,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x12, 0x07, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x01, 0x12, 0x07, 0x00, 0x05),
+			raw_pdu(0x12, 0x07, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x13));
+
 	define_test_client("/TP/GAW/CL/BI-06-C", test_client, service_db_1,
 			&test_write_6,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#9 lore raw

PATCH 10/11 unit/test-gatt: Add /TP/GAW/CL/BI-18-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAW/CL/BI-18-C/auto is similar to /TP/GAW/CL/BI-18-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
unit/test-gatt.c +10 -0
index 6df6342..82b6576 100644
@@ -3943,6 +3943,16 @@ int main(int argc, char *argv[])
 			raw_pdu(0x18, 0x00),
 			raw_pdu(0x19));
 
+	define_test_client("/TP/GAW/CL/BI-18-C/auto", test_client, service_db_1,
+			&test_reliable_write_1,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x16, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x01, 0x16, 0x07, 0x00, 0x05),
+			raw_pdu(0x16, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x17, 0x07, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x18, 0x01),
+			raw_pdu(0x19));
+
 	define_test_client("/TP/GAW/CL/BI-19-C", test_client, service_db_1,
 			&test_reliable_write_6,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer
#10 lore raw

PATCH 11/11 unit/test-gatt: Add /TP/GAW/CL/BI-23-C/auto test

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

/TP/GAW/CL/BI-23-C/auto is similar to /TP/GAW/CL/BI-23-C but automatically elevates the security level and retry.

---
 unit/test-gatt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
unit/test-gatt.c +8 -0
index 82b6576..326a32c 100644
@@ -4059,6 +4059,14 @@ int main(int argc, char *argv[])
 			raw_pdu(0x12, 0x08, 0x00, 0x01, 0x02, 0x03),
 			raw_pdu(0x01, 0x12, 0x08, 0x00, 0x05));
 
+	define_test_client("/TP/GAW/CL/BI-23-C/auto", test_client, service_db_1,
+			&test_write_7,
+			SERVICE_DATA_1_PDUS,
+			raw_pdu(0x12, 0x08, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x01, 0x12, 0x08, 0x00, 0x05),
+			raw_pdu(0x12, 0x08, 0x00, 0x01, 0x02, 0x03),
+			raw_pdu(0x13));
+
 	define_test_client("/TP/GAW/CL/BI-24-C", test_client, service_db_1,
 			&test_write_12,
 			SERVICE_DATA_1_PDUS,
Signature · 1 line
2.1.0
author maintainer Applied
#11 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 86 lines · Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

This adds auto retry logic to requests when a security error happen, it will first attempt to elevate the security to match the requirement and then retry sending the request once again.

---
 src/shared/att.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 54 insertions(+), 4 deletions(-)
src/shared/att.c +54 -5
index f9a5817..e8be4cc 100644
@@ -576,6 +576,55 @@ static bool disconnect_cb(struct io *io, void *user_data)
        return false;
 }
 
+static bool change_security(struct bt_att *att, uint8_t ecode)
+{
+       int security;
+
+       security = bt_att_get_security(att);
+       if (ecode == BT_ATT_ERROR_INSUFFICIENT_ENCRYPTION &&
+                                       security < BT_ATT_SECURITY_MEDIUM)
+               security = BT_ATT_SECURITY_MEDIUM;
+       else if (ecode == BT_ATT_ERROR_AUTHENTICATION &&
+                                       security < BT_ATT_SECURITY_HIGH)
+               security = BT_ATT_SECURITY_HIGH;
+       else
+               return false;
+
+       return bt_att_set_security(att, security);
+}
+
+static bool handle_error_rsp(struct bt_att *att, uint8_t *pdu,
+                                       ssize_t pdu_len, uint8_t *opcode)
+{
+       const struct bt_att_pdu_error_rsp *rsp;
+       struct att_send_op *op = att->pending_req;
+
+       if (pdu_len != sizeof(*rsp)) {
+               *opcode = 0;
+               return false;
+       }
+
+       rsp = (void *) pdu;
+
+       *opcode = rsp->opcode;
+
+       /* Only try to change security on L2CAP */
+       if (!att->io_on_l2cap)
+               return false;
+
+       /* Attempt to change security */
+       if (!change_security(att, rsp->ecode))
+               return false;
+
+       util_debug(att->debug_callback, att->debug_data,
+                                               "Retrying operation %p", op);
+
+       att->pending_req = NULL;
+
+       /* Push operation back to request queue */
+       return queue_push_head(att->req_queue, op);
+}
+
 static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
                                                                ssize_t pdu_len)
 {
@@ -601,10 +650,11 @@ static void handle_rsp(struct bt_att *att, uint8_t opcode, uint8_t *pdu,
         * the request is malformed, end the current request with failure.
         */
        if (opcode == BT_ATT_OP_ERROR_RSP) {
-               if (pdu_len != 4)
-                       goto fail;
-
-               req_opcode = pdu[0];
+               /* Return if error response cause a retry */
+               if (handle_error_rsp(att, pdu, pdu_len, &req_opcode)) {
+                       wakeup_writer(att);
+                       return;
+               }
        } else if (!(req_opcode = get_req_opcode(opcode)))
                goto fail;
 
--
Quoted 1 line

2.1.0

Applied.

Signature · 1 line
Luiz Augusto von Dentz