Repository: incubator-mynewt-core Updated Branches: refs/heads/develop 73cd55b73 -> 5728339c9
nimble/sm: Use packed structures for confirm 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/bc0e7594 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/bc0e7594 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/bc0e7594 Branch: refs/heads/develop Commit: bc0e7594db3078ae7e9f4798aa746b119611d3f6 Parents: cb4de66 Author: Szymon Janc <[email protected]> Authored: Tue Jan 17 19:24:29 2017 +0100 Committer: Szymon Janc <[email protected]> Committed: Wed Jan 25 15:44:14 2017 +0100 ---------------------------------------------------------------------- net/nimble/host/src/ble_sm.c | 11 ++++++----- net/nimble/host/src/ble_sm_cmd.c | 6 +++--- net/nimble/host/src/ble_sm_lgcy.c | 17 ++++++++++++++--- net/nimble/host/src/ble_sm_priv.h | 4 ++-- net/nimble/host/src/ble_sm_sc.c | 17 ++++++++++++++--- net/nimble/host/test/src/ble_sm_test_util.c | 7 +++---- 6 files changed, 42 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/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 5ccd72a..974df60 100644 --- a/net/nimble/host/src/ble_sm.c +++ b/net/nimble/host/src/ble_sm.c @@ -1305,26 +1305,27 @@ static void ble_sm_confirm_rx(uint16_t conn_handle, uint8_t op, struct os_mbuf **om, struct ble_sm_result *res) { - struct ble_sm_pair_confirm cmd; + struct ble_sm_pair_confirm *cmd; struct ble_sm_proc *proc; uint8_t ioact; - res->app_status = ble_hs_mbuf_pullup_base(om, BLE_SM_PAIR_CONFIRM_SZ); + res->app_status = ble_hs_mbuf_pullup_base(om, sizeof(*cmd)); if (res->app_status != 0) { res->sm_err = BLE_SM_ERR_UNSPECIFIED; res->enc_cb = 1; return; } - ble_sm_pair_confirm_parse((*om)->om_data, (*om)->om_len, &cmd); - BLE_SM_LOG_CMD(0, "confirm", conn_handle, ble_sm_pair_confirm_log, &cmd); + cmd = (struct ble_sm_pair_confirm *)(*om)->om_data; + + BLE_SM_LOG_CMD(0, "confirm", conn_handle, ble_sm_pair_confirm_log, cmd); ble_hs_lock(); proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_CONFIRM, -1, NULL); if (proc == NULL) { res->app_status = BLE_HS_ENOENT; } else { - memcpy(proc->confirm_peer, cmd.value, 16); + memcpy(proc->confirm_peer, cmd->value, 16); if (proc->flags & BLE_SM_PROC_F_INITIATOR) { proc->state = BLE_SM_PROC_STATE_RANDOM; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/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 9b8f5d4..6ec7def 100644 --- a/net/nimble/host/src/ble_sm_cmd.c +++ b/net/nimble/host/src/ble_sm_cmd.c @@ -164,7 +164,7 @@ void ble_sm_pair_confirm_parse(void *payload, int len, struct ble_sm_pair_confirm *cmd) { - BLE_HS_DBG_ASSERT(len >= BLE_SM_PAIR_CONFIRM_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_pair_confirm)); memcpy(cmd->value, payload, sizeof cmd->value); } @@ -174,7 +174,7 @@ ble_sm_pair_confirm_write(void *payload, int len, { uint8_t *u8ptr; - BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_CONFIRM_SZ); + BLE_HS_DBG_ASSERT(len >= sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_confirm)); u8ptr = payload; @@ -188,7 +188,7 @@ ble_sm_pair_confirm_tx(uint16_t conn_handle, struct ble_sm_pair_confirm *cmd) struct os_mbuf *txom; int rc; - rc = ble_sm_init_req(BLE_SM_PAIR_CONFIRM_SZ, &txom); + rc = ble_sm_init_req(sizeof(struct ble_sm_pair_confirm), &txom); if (rc != 0) { return BLE_HS_ENOMEM; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/net/nimble/host/src/ble_sm_lgcy.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_lgcy.c b/net/nimble/host/src/ble_sm_lgcy.c index d4734e5..d94d945 100644 --- a/net/nimble/host/src/ble_sm_lgcy.c +++ b/net/nimble/host/src/ble_sm_lgcy.c @@ -112,22 +112,29 @@ ble_sm_lgcy_io_action(struct ble_sm_proc *proc) void ble_sm_lgcy_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) { - struct ble_sm_pair_confirm cmd; + struct ble_sm_pair_confirm *cmd; + struct os_mbuf *txom; uint8_t ia[6]; uint8_t ra[6]; uint8_t iat; uint8_t rat; int rc; + cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_CONFIRM, sizeof(*cmd), &txom); + if (cmd == NULL) { + rc = BLE_HS_ENOMEM; + goto err; + } + ble_sm_ia_ra(proc, &iat, ia, &rat, ra); rc = ble_sm_alg_c1(proc->tk, ble_sm_our_pair_rand(proc), proc->pair_req, - proc->pair_rsp, iat, rat, ia, ra, cmd.value); + proc->pair_rsp, iat, rat, ia, ra, cmd->value); if (rc != 0) { goto err; } - rc = ble_sm_pair_confirm_tx(proc->conn_handle, &cmd); + rc = ble_sm_tx(proc->conn_handle, txom); if (rc != 0) { goto err; } @@ -139,6 +146,10 @@ ble_sm_lgcy_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) return; err: + if (txom) { + os_mbuf_free_chain(txom); + } + res->app_status = rc; res->enc_cb = 1; res->sm_err = BLE_SM_ERR_UNSPECIFIED; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/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 c022e14..e9ecd90 100644 --- a/net/nimble/host/src/ble_sm_priv.h +++ b/net/nimble/host/src/ble_sm_priv.h @@ -82,10 +82,10 @@ struct ble_sm_pair_cmd { * | (Code=0x03) | 1 | * | Confirm Value | 16 | */ -#define BLE_SM_PAIR_CONFIRM_SZ 16 + struct ble_sm_pair_confirm { uint8_t value[16]; -}; +} __attribute__((packed)); /** * | Parameter | Size (octets) | http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/net/nimble/host/src/ble_sm_sc.c ---------------------------------------------------------------------- diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c index 26564c8..e440a85 100644 --- a/net/nimble/host/src/ble_sm_sc.c +++ b/net/nimble/host/src/ble_sm_sc.c @@ -279,7 +279,8 @@ ble_sm_sc_gen_ri(struct ble_sm_proc *proc) void ble_sm_sc_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) { - struct ble_sm_pair_confirm cmd; + struct ble_sm_pair_confirm *cmd; + struct os_mbuf *txom; int rc; rc = ble_sm_sc_gen_ri(proc); @@ -290,16 +291,26 @@ ble_sm_sc_confirm_exec(struct ble_sm_proc *proc, struct ble_sm_result *res) return; } + cmd = ble_sm_cmd_get(BLE_SM_OP_PAIR_CONFIRM, sizeof(*cmd), &txom); + if (cmd == NULL) { + rc = BLE_HS_ENOMEM; + res->app_status = rc; + res->enc_cb = 1; + res->sm_err = BLE_SM_ERR_UNSPECIFIED; + return; + } + rc = ble_sm_alg_f4(ble_sm_sc_pub_key.u8, proc->pub_key_peer.x, - ble_sm_our_pair_rand(proc), proc->ri, cmd.value); + ble_sm_our_pair_rand(proc), proc->ri, cmd->value); if (rc != 0) { + os_mbuf_free_chain(txom); res->app_status = rc; res->enc_cb = 1; res->sm_err = BLE_SM_ERR_UNSPECIFIED; return; } - rc = ble_sm_pair_confirm_tx(proc->conn_handle, &cmd); + rc = ble_sm_tx(proc->conn_handle, txom); if (rc != 0) { res->app_status = rc; res->enc_cb = 1; http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/bc0e7594/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 f73fb4b..2e3eecd 100644 --- a/net/nimble/host/test/src/ble_sm_test_util.c +++ b/net/nimble/host/test/src/ble_sm_test_util.c @@ -321,12 +321,12 @@ ble_sm_test_util_rx_confirm(uint16_t conn_handle, 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_PAIR_CONFIRM_SZ); + BLE_L2CAP_HDR_SZ + sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_confirm)); om = ble_hs_mbuf_l2cap_pkt(); TEST_ASSERT_FATAL(om != NULL); - payload_len = sizeof(struct ble_sm_hdr) + BLE_SM_PAIR_CONFIRM_SZ; + payload_len = sizeof(struct ble_sm_hdr) + sizeof(struct ble_sm_pair_confirm); v = os_mbuf_extend(om, payload_len); TEST_ASSERT_FATAL(v != NULL); @@ -665,8 +665,7 @@ ble_sm_test_util_verify_tx_pair_confirm( struct ble_sm_pair_confirm cmd; struct os_mbuf *om; - om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_CONFIRM, - BLE_SM_PAIR_CONFIRM_SZ); + om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_CONFIRM, sizeof(cmd)); ble_sm_pair_confirm_parse(om->om_data, om->om_len, &cmd); TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
