nimble/sm: Use packed structures for sec req
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/34e5a4b2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/34e5a4b2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/34e5a4b2 Branch: refs/heads/develop Commit: 34e5a4b24e5d8af23ee4cdab48692f585a03f4bc Parents: 14d869c Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:40 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:15 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 25 +++++++++++++++--------- net/nimble/host/src/ble_sm_cmd.c | 6 +++--- net/nimble/host/src/ble_sm_priv.h | 3 +-- net/nimble/host/test/src/ble_sm_test_util.c | 6 +++--- 4 files changed, 23 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34e5a4b2/net/nimble/host/src/ble_sm.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c index 7bc6428..3a812cf 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -1612,11 +1612,18 @@ static void ble_sm_sec_req_exec(struct ble_sm_proc *proc, struct ble_sm_result *res, void *arg) { - struct ble_sm_sec_req cmd; + struct ble_sm_sec_req *cmd; + struct os_mbuf *txom; int rc; - cmd.authreq = ble_sm_build_authreq(); - rc = ble_sm_sec_req_tx(proc->conn_handle, &cmd); + cmd = ble_sm_cmd_get(BLE_SM_OP_SEC_REQ, sizeof(*cmd), &txom); + if (!cmd) { + res->app_status = BLE_HS_ENOMEM; + return; + } + + cmd->authreq = ble_sm_build_authreq(); + rc = ble_sm_tx(proc->conn_handle, txom); if (rc != 0) { res->app_status = rc; return; @@ -1630,17 +1637,17 @@ ble_sm_sec_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_store_value_sec value_sec; struct ble_store_key_sec key_sec; struct ble_hs_conn_addrs addrs; - struct ble_sm_sec_req cmd; + struct ble_sm_sec_req *cmd; struct ble_hs_conn *conn; int authreq_mitm; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_SEC_REQ_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd)); if (res->app_status != 0) { return; } - ble_sm_sec_req_parse((*om)->om_data, (*om)->om_len, &cmd); - BLE_SM_LOG_CMD(0, "sec req", conn_handle, ble_sm_sec_req_log, &cmd); + cmd = (struct ble_sm_sec_req *)(*om)->om_data; + BLE_SM_LOG_CMD(0, "sec req", conn_handle, ble_sm_sec_req_log, cmd); /* XXX: Reject if: * o authreq-reserved flags set? @@ -1669,7 +1676,7 @@ ble_sm_sec_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, /* If the peer is requesting a bonded connection, query database for an * LTK corresponding to the sender. */ - if (cmd.authreq & BLE_SM_PAIR_AUTHREQ_BOND) { + if (cmd->authreq & BLE_SM_PAIR_AUTHREQ_BOND) { res->app_status = ble_store_read_peer_sec(&key_sec, &value_sec); } else { res->app_status = BLE_HS_ENOENT; @@ -1678,7 +1685,7 @@ ble_sm_sec_req_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, /* Found a key corresponding to this peer. Make sure it meets the * requested minimum authreq. */ - authreq_mitm = cmd.authreq & BLE_SM_PAIR_AUTHREQ_MITM; + authreq_mitm = cmd->authreq & BLE_SM_PAIR_AUTHREQ_MITM; if ((!authreq_mitm && value_sec.authenticated) || (authreq_mitm && !value_sec.authenticated)) { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34e5a4b2/net/nimble/host/src/ble_sm_cmd.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c index 6d2b5d7..b4c88ce 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -587,7 +587,7 @@ ble_sm_sec_req_parse(void *payload, int len, struct ble_sm_sec_req *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= BLE_SM_SEC_REQ_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_sec_req)); u8ptr = payload; cmd->authreq = *u8ptr; @@ -598,7 +598,7 @@ ble_sm_sec_req_write(void *payload, int len, struct ble_sm_sec_req *cmd) { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sec_req)); u8ptr = payload; @@ -612,7 +612,7 @@ ble_sm_sec_req_tx(uint16_t conn_handle, struct ble_sm_sec_req *cmd) struct os_mbuf *txom; int rc; - rc = ble_sm_init_req(BLE_SM_SEC_REQ_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_sec_req), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34e5a4b2/net/nimble/host/src/ble_sm_priv.h ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h index 1820592..57f45c9 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -167,10 +167,9 @@ struct ble_sm_sign_info { * | (Code=0x0B) | 1 | * | authreq | 1 | */ -#define BLE_SM_SEC_REQ_SZ 1 struct ble_sm_sec_req { uint8_t authreq; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/34e5a4b2/net/nimble/host/test/src/ble_sm_test_util.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/test/src/ble_sm_test_util.c b/net/nimble/host/test/src/ble_sm_test_util.c index 8ff0f1a..e1f00bf 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -380,12 +380,12 @@ ble_sm_test_util_rx_sec_req(uint16_t conn_handle, struct ble_sm_sec_req *cmd, hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR( 2, BLE_HCI_PB_FIRST_FLUSH, - BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sec_req)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_SEC_REQ_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_sec_req); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -814,7 +814,7 @@ ble_sm_test_util_verify_tx_sec_req(struct ble_sm_sec_req *exp_cmd) ble_hs_test_util_tx_all(); - om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SEC_REQ, BLE_SM_SEC_REQ_SZ); + om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SEC_REQ, sizeof(struct ble_sm_sec_req)); ble_sm_sec_req_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(cmd.authreq == exp_cmd->authreq);
