l2cap.c (62f901df9794b0d1b972cdf6aeebec4b6f6ed719) | l2cap.c (df3354fc67b724e12e38312f95d99fe997e0d46e) |
---|---|
1/* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright --- 272 unchanged lines hidden (view full) --- 281static void l2cap_start_ertx(l2cap_channel_t * channel){ 282 log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 283 l2cap_stop_rtx(channel); 284 run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 285 run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 286 run_loop_add_timer(&channel->rtx); 287} 288 | 1/* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright --- 272 unchanged lines hidden (view full) --- 281static void l2cap_start_ertx(l2cap_channel_t * channel){ 282 log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid); 283 l2cap_stop_rtx(channel); 284 run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout); 285 run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS); 286 run_loop_add_timer(&channel->rtx); 287} 288 |
289static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){ 290 // return 0; // TESTING! 291 return psm != PSM_SDP; 292} |
|
289 290int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 291 292 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 293 log_info("l2cap_send_signaling_packet, cannot send\n"); 294 return BTSTACK_ACL_BUFFERS_FULL; 295 } 296 --- 193 unchanged lines hidden (view full) --- 490 491 l2cap_channel_t * channel = (l2cap_channel_t *) it; 492 493 // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 494 495 496 switch (channel->state){ 497 | 293 294int l2cap_send_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, uint8_t identifier, ...){ 295 296 if (!hci_can_send_packet_now(HCI_ACL_DATA_PACKET)){ 297 log_info("l2cap_send_signaling_packet, cannot send\n"); 298 return BTSTACK_ACL_BUFFERS_FULL; 299 } 300 --- 193 unchanged lines hidden (view full) --- 494 495 l2cap_channel_t * channel = (l2cap_channel_t *) it; 496 497 // log_info("l2cap_run: state %u, var 0x%02x\n", channel->state, channel->state_var); 498 499 500 switch (channel->state){ 501 |
498 case L2CAP_STATE_WAIT_SECURITY_LEVEL_UPDATE: | 502 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: |
499 case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 500 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 501 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 502 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 503 } 504 break; 505 506 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: --- 158 unchanged lines hidden (view full) --- 665 666static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 667 linked_item_t *it; 668 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 669 l2cap_channel_t * channel = (l2cap_channel_t *) it; 670 if ( ! BD_ADDR_CMP( channel->address, address) ){ 671 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 672 // success, start l2cap handshake | 503 case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT: 504 if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { 505 channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND); 506 l2cap_send_signaling_packet(channel->handle, CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid, 1, 0); 507 } 508 break; 509 510 case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION: --- 158 unchanged lines hidden (view full) --- 669 670static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){ 671 linked_item_t *it; 672 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 673 l2cap_channel_t * channel = (l2cap_channel_t *) it; 674 if ( ! BD_ADDR_CMP( channel->address, address) ){ 675 if (channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE || channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION) { 676 // success, start l2cap handshake |
673 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; | |
674 channel->handle = handle; 675 channel->local_cid = l2cap_next_local_cid(); | 677 channel->handle = handle; 678 channel->local_cid = l2cap_next_local_cid(); |
679 // check remote SSP feature first 680 channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES; |
|
676 } 677 } 678 } 679 // process 680 l2cap_run(); 681} 682 683void l2cap_event_handler( uint8_t *packet, uint16_t size ){ --- 82 unchanged lines hidden (view full) --- 766 if (attribute_protocol_packet_handler) { 767 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 768 } 769 if (security_protocol_packet_handler) { 770 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 771 } 772 break; 773 | 681 } 682 } 683 } 684 // process 685 l2cap_run(); 686} 687 688void l2cap_event_handler( uint8_t *packet, uint16_t size ){ --- 82 unchanged lines hidden (view full) --- 771 if (attribute_protocol_packet_handler) { 772 (*attribute_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 773 } 774 if (security_protocol_packet_handler) { 775 (*security_protocol_packet_handler)(HCI_EVENT_PACKET, 0, packet, size); 776 } 777 break; 778 |
779 case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 780 handle = READ_BT_16(packet, 3); 781 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 782 channel = (l2cap_channel_t *) it; 783 if (channel->handle != handle) continue; 784 if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) continue; 785 // we have been waiting for remote supported features, if both support SSP, 786 if (hci_ssp_supported_on_both_sides(channel->handle) && !l2cap_security_level_0_allowed_for_PSM(channel->psm)){ 787 // request security level 2 788 gap_request_security_level(channel->handle, LEVEL_2); 789 channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE; 790 break; 791 } 792 // fine, go ahead 793 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 794 break; 795 } 796 |
|
774 case GAP_SECURITY_LEVEL: 775 handle = READ_BT_16(packet, 2); | 797 case GAP_SECURITY_LEVEL: 798 handle = READ_BT_16(packet, 2); |
799 log_info("GAP_SECURITY_LEVEL"); |
|
776 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 777 channel = (l2cap_channel_t *) it; 778 gap_security_level_t actual_level = packet[4]; | 800 for (it = (linked_item_t *) l2cap_channels; it ; it = it->next){ 801 channel = (l2cap_channel_t *) it; 802 gap_security_level_t actual_level = packet[4]; |
779 log_info("GAP_SECURITY_LEVEL handle %x/%x level %u, state %u", handle, channel->handle, actual_level, channel->state); | |
780 if (channel->handle != handle) continue; | 803 if (channel->handle != handle) continue; |
781 log_info("handle ok"); 782 if (channel->state != L2CAP_STATE_WAIT_SECURITY_LEVEL_UPDATE) continue; 783 log_info("state ok"); 784 if (actual_level >= channel->required_security_level){ 785 log_info("level ok"); 786 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 787 l2cap_emit_connection_request(channel); 788 } else { 789 log_info("level nok"); 790 channel->reason = 0x03; // security block 791 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; | 804 switch (channel->state){ 805 case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE: 806 log_info("gap incoming"); 807 if (actual_level >= channel->required_security_level){ 808 channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT; 809 l2cap_emit_connection_request(channel); 810 } else { 811 channel->reason = 0x03; // security block 812 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE; 813 } 814 break; 815 816 case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE: 817 log_info("gap outgoing"); 818 if (actual_level >= channel->required_security_level){ 819 channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST; 820 } else { 821 // disconnnect, authentication not good enough 822 hci_disconnect_security_block(handle); 823 } 824 break; 825 826 default: 827 break; |
792 } 793 } 794 break; 795 796 default: 797 break; 798 } 799 --- 32 unchanged lines hidden (view full) --- 832 hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 833 if (!hci_connection) { 834 // 835 log_error("no hci_connection for handle %u\n", handle); 836 return; 837 } 838 839 // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP | 828 } 829 } 830 break; 831 832 default: 833 break; 834 } 835 --- 32 unchanged lines hidden (view full) --- 868 hci_connection_t * hci_connection = hci_connection_for_handle( handle ); 869 if (!hci_connection) { 870 // 871 log_error("no hci_connection for handle %u\n", handle); 872 return; 873 } 874 875 // reject connection (0x03 security block) and disconnect if both have SSP, connection is not encrypted and PSM != SDP |
840 if (psm != PSM_SDP 841 && hci_local_ssp_activated() 842 && hci_remote_ssp_supported(handle) 843 && gap_security_level(handle) == LEVEL_3){ | 876 if ( l2cap_security_level_0_allowed_for_PSM(psm) 877 && hci_ssp_supported_on_both_sides(handle) 878 && gap_security_level(handle) == LEVEL_0){ |
844 845 // 0x0003 Security Block 846 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 847 return; 848 } 849 850 851 // alloc structure --- 12 unchanged lines hidden (view full) --- 864 channel->connection = service->connection; 865 channel->packet_handler = service->packet_handler; 866 channel->local_cid = l2cap_next_local_cid(); 867 channel->remote_cid = source_cid; 868 channel->local_mtu = service->mtu; 869 channel->remote_mtu = L2CAP_DEFAULT_MTU; 870 channel->packets_granted = 0; 871 channel->remote_sig_id = sig_id; | 879 880 // 0x0003 Security Block 881 l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, 0x0003); 882 return; 883 } 884 885 886 // alloc structure --- 12 unchanged lines hidden (view full) --- 899 channel->connection = service->connection; 900 channel->packet_handler = service->packet_handler; 901 channel->local_cid = l2cap_next_local_cid(); 902 channel->remote_cid = source_cid; 903 channel->local_mtu = service->mtu; 904 channel->remote_mtu = L2CAP_DEFAULT_MTU; 905 channel->packets_granted = 0; 906 channel->remote_sig_id = sig_id; |
872 channel->required_security_level = LEVEL_0; // @TODO get from 'security database' | 907 channel->required_security_level = service->required_security_level; |
873 874 // limit local mtu to max acl packet length 875 if (channel->local_mtu > l2cap_max_mtu()) { 876 channel->local_mtu = l2cap_max_mtu(); 877 } 878 879 // set initial state | 908 909 // limit local mtu to max acl packet length 910 if (channel->local_mtu > l2cap_max_mtu()) { 911 channel->local_mtu = l2cap_max_mtu(); 912 } 913 914 // set initial state |
880 channel->state = L2CAP_STATE_WAIT_SECURITY_LEVEL_UPDATE; | 915 channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE; |
881 channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 882 883 // add to connections list 884 linked_list_add(&l2cap_channels, (linked_item_t *) channel); 885 886 // assert security requirements 887 gap_request_security_level(handle, channel->required_security_level); 888} --- 364 unchanged lines hidden (view full) --- 1253 return; 1254 } 1255 1256 // fill in 1257 service->psm = psm; 1258 service->mtu = mtu; 1259 service->connection = connection; 1260 service->packet_handler = packet_handler; | 916 channel->state_var = L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND; 917 918 // add to connections list 919 linked_list_add(&l2cap_channels, (linked_item_t *) channel); 920 921 // assert security requirements 922 gap_request_security_level(handle, channel->required_security_level); 923} --- 364 unchanged lines hidden (view full) --- 1288 return; 1289 } 1290 1291 // fill in 1292 service->psm = psm; 1293 service->mtu = mtu; 1294 service->connection = connection; 1295 service->packet_handler = packet_handler; |
1261 service->security_level = security_level; 1262 | 1296 service->required_security_level = security_level; 1297 |
1263 // add to services list 1264 linked_list_add(&l2cap_services, (linked_item_t *) service); 1265 1266 // enable page scan 1267 hci_connectable_control(1); 1268 1269 // done 1270 l2cap_emit_service_registered(connection, 0, psm); --- 71 unchanged lines hidden --- | 1298 // add to services list 1299 linked_list_add(&l2cap_services, (linked_item_t *) service); 1300 1301 // enable page scan 1302 hci_connectable_control(1); 1303 1304 // done 1305 l2cap_emit_service_registered(connection, 0, psm); --- 71 unchanged lines hidden --- |