use single timer for SM, stop timer on sm_done_for_handle

This commit is contained in:
matthias.ringwald@gmail.com 2015-02-27 14:17:45 +00:00
parent d213b6f740
commit d19b56e8be
2 changed files with 28 additions and 29 deletions

View File

@ -166,6 +166,8 @@ static sm_connection_t * sm_csrk_connection_source;
// data needed for security setup // data needed for security setup
typedef struct sm_setup_context { typedef struct sm_setup_context {
timer_source_t sm_timeout;
// used in all phases // used in all phases
uint8_t sm_pairing_failed_reason; uint8_t sm_pairing_failed_reason;
@ -290,28 +292,28 @@ static void sm_truncate_key(sm_key_t key, int max_encryption_size){
// Security Manager Channel. A new SM procedure shall only be performed when a new physical link has been // Security Manager Channel. A new SM procedure shall only be performed when a new physical link has been
// established. // established.
static void sm_2timeout_handler(timer_source_t * timer){ static void sm_timeout_handler(timer_source_t * timer){
log_info("SM timeout"); log_info("SM timeout");
sm_connection_t * sm_conn = (sm_connection_t *) linked_item_get_user((linked_item_t*) timer); sm_connection_t * sm_conn = (sm_connection_t *) linked_item_get_user((linked_item_t*) timer);
sm_done_for_handle(sm_conn->sm_handle);
sm_conn->sm_engine_state = SM_GENERAL_TIMEOUT; sm_conn->sm_engine_state = SM_GENERAL_TIMEOUT;
sm_done_for_handle(sm_conn->sm_handle);
// trigger handling of next ready connection // trigger handling of next ready connection
sm_run(); sm_run();
} }
static void sm_2timeout_start(sm_connection_t * sm_conn){ static void sm_timeout_start(sm_connection_t * sm_conn){
run_loop_remove_timer(&sm_conn->sm_timeout); run_loop_remove_timer(&setup->sm_timeout);
run_loop_set_timer_handler(&sm_conn->sm_timeout, sm_2timeout_handler); run_loop_set_timer_handler(&setup->sm_timeout, sm_timeout_handler);
run_loop_set_timer(&sm_conn->sm_timeout, 30000); // 30 seconds sm timeout run_loop_set_timer(&setup->sm_timeout, 30000); // 30 seconds sm timeout
linked_item_set_user((linked_item_t*) &sm_conn->sm_timeout, sm_conn); linked_item_set_user((linked_item_t*) &setup->sm_timeout, sm_conn);
run_loop_add_timer(&sm_conn->sm_timeout); run_loop_add_timer(&setup->sm_timeout);
} }
static void sm_2timeout_stop(sm_connection_t * sm_conn){ static void sm_timeout_stop(){
run_loop_remove_timer(&sm_conn->sm_timeout); run_loop_remove_timer(&setup->sm_timeout);
} }
static void sm_2timeout_reset(sm_connection_t * sm_conn){ static void sm_timeout_reset(sm_connection_t * sm_conn){
sm_2timeout_stop(sm_conn); sm_timeout_stop();
sm_2timeout_start(sm_conn); sm_timeout_start(sm_conn);
} }
// end of sm timeout // end of sm timeout
@ -754,8 +756,9 @@ static int sm_key_distribution_all_received(){
static void sm_done_for_handle(uint16_t handle){ static void sm_done_for_handle(uint16_t handle){
if (sm_active_connection == handle){ if (sm_active_connection == handle){
log_info("sm: connection 0x%x relesed setup context", sm_active_connection); sm_timeout_stop();
sm_active_connection = 0; sm_active_connection = 0;
log_info("sm: connection 0x%x released setup context", handle);
} }
} }
@ -821,7 +824,7 @@ static void sm_responder_setup(sm_connection_t * sm_conn){
sm_setup_key_distribution(setup->sm_m_preq.responder_key_distribution); sm_setup_key_distribution(setup->sm_m_preq.responder_key_distribution);
// start SM timeout // start SM timeout
sm_2timeout_start(sm_conn); sm_timeout_start(sm_conn);
// decide on STK generation method // decide on STK generation method
sm_setup_tk(); sm_setup_tk();
@ -1043,7 +1046,7 @@ static void sm_run(void){
setup->sm_m_preq.code = SM_CODE_PAIRING_REQUEST; setup->sm_m_preq.code = SM_CODE_PAIRING_REQUEST;
connection->sm_engine_state = SM_INITIATOR_PH1_W4_PAIRING_RESPONSE; connection->sm_engine_state = SM_INITIATOR_PH1_W4_PAIRING_RESPONSE;
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_m_preq, sizeof(sm_pairing_packet_t)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_m_preq, sizeof(sm_pairing_packet_t));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
break; break;
// responder side // responder side
@ -1065,7 +1068,7 @@ static void sm_run(void){
connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM; connection->sm_engine_state = SM_RESPONDER_PH1_W4_PAIRING_CONFIRM;
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) &setup->sm_s_pres, sizeof(sm_pairing_packet_t));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
sm_trigger_user_response(connection); sm_trigger_user_response(connection);
@ -1083,7 +1086,6 @@ static void sm_run(void){
buffer[1] = setup->sm_pairing_failed_reason; buffer[1] = setup->sm_pairing_failed_reason;
connection->sm_engine_state = SM_GENERAL_IDLE; connection->sm_engine_state = SM_GENERAL_IDLE;
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_stop(connection);
sm_done_for_handle(connection->sm_handle); sm_done_for_handle(connection->sm_handle);
break; break;
} }
@ -1098,7 +1100,7 @@ static void sm_run(void){
connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_RANDOM; connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_RANDOM;
} }
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
break; break;
} }
@ -1189,7 +1191,7 @@ static void sm_run(void){
connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_CONFIRM; connection->sm_engine_state = SM_INITIATOR_PH2_W4_PAIRING_CONFIRM;
} }
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
case SM_RESPONDER_PH2_SEND_LTK_REPLY: { case SM_RESPONDER_PH2_SEND_LTK_REPLY: {
@ -1230,7 +1232,7 @@ static void sm_run(void){
buffer[0] = SM_CODE_ENCRYPTION_INFORMATION; buffer[0] = SM_CODE_ENCRYPTION_INFORMATION;
swap128(setup->sm_ltk, &buffer[1]); swap128(setup->sm_ltk, &buffer[1]);
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION){ if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_MASTER_IDENTIFICATION){
@ -1240,7 +1242,7 @@ static void sm_run(void){
bt_store_16(buffer, 1, setup->sm_local_ediv); bt_store_16(buffer, 1, setup->sm_local_ediv);
swap64(setup->sm_local_rand, &buffer[3]); swap64(setup->sm_local_rand, &buffer[3]);
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){ if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_INFORMATION){
@ -1249,7 +1251,7 @@ static void sm_run(void){
buffer[0] = SM_CODE_IDENTITY_INFORMATION; buffer[0] = SM_CODE_IDENTITY_INFORMATION;
swap128(sm_persistent_irk, &buffer[1]); swap128(sm_persistent_irk, &buffer[1]);
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION){ if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_IDENTITY_ADDRESS_INFORMATION){
@ -1260,7 +1262,7 @@ static void sm_run(void){
hci_le_advertisement_address(&buffer[1], &local_address); hci_le_advertisement_address(&buffer[1], &local_address);
bt_flip_addr(&buffer[2], local_address); bt_flip_addr(&buffer[2], local_address);
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){ if (setup->sm_key_distribution_send_set & SM_KEYDIST_FLAG_SIGNING_IDENTIFICATION){
@ -1269,7 +1271,7 @@ static void sm_run(void){
buffer[0] = SM_CODE_SIGNING_INFORMATION; buffer[0] = SM_CODE_SIGNING_INFORMATION;
swap128(setup->sm_local_csrk, &buffer[1]); swap128(setup->sm_local_csrk, &buffer[1]);
l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer)); l2cap_send_connectionless(connection->sm_handle, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, (uint8_t*) buffer, sizeof(buffer));
sm_2timeout_reset(connection); sm_timeout_reset(connection);
return; return;
} }
@ -1279,7 +1281,6 @@ static void sm_run(void){
connection->sm_engine_state = SM_PH3_RECEIVE_KEYS; connection->sm_engine_state = SM_PH3_RECEIVE_KEYS;
} else { } else {
// master -> all done // master -> all done
sm_2timeout_stop(connection);
connection->sm_engine_state = SM_GENERAL_IDLE; connection->sm_engine_state = SM_GENERAL_IDLE;
sm_done_for_handle(connection->sm_handle); sm_done_for_handle(connection->sm_handle);
} }
@ -1773,7 +1774,7 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac
// identical to responder // identical to responder
// start SM timeout // start SM timeout
sm_2timeout_start(sm_conn); sm_timeout_start(sm_conn);
// decide on STK generation method // decide on STK generation method
sm_setup_tk(); sm_setup_tk();
@ -1920,7 +1921,6 @@ static void sm_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *pac
// done with key distribution? // done with key distribution?
if (sm_key_distribution_all_received()){ if (sm_key_distribution_all_received()){
if (sm_conn->sm_role){ if (sm_conn->sm_role){
sm_2timeout_stop(sm_conn);
sm_conn->sm_engine_state = SM_GENERAL_IDLE; sm_conn->sm_engine_state = SM_GENERAL_IDLE;
sm_done_for_handle(sm_conn->sm_handle); sm_done_for_handle(sm_conn->sm_handle);
} else { } else {

View File

@ -453,7 +453,6 @@ typedef struct sm_connection {
uint8_t sm_actual_encryption_key_size; uint8_t sm_actual_encryption_key_size;
sm_pairing_packet_t sm_m_preq; // only used during c1 sm_pairing_packet_t sm_m_preq; // only used during c1
authorization_state_t sm_connection_authorization_state; authorization_state_t sm_connection_authorization_state;
timer_source_t sm_timeout;
} sm_connection_t; } sm_connection_t;
typedef struct { typedef struct {