bnep.c (37e1b22048c5dc562e27aa1edc93fa4a6eda5cb4) bnep.c (fe3c1d9398e4313afad30ccb7708859e93c2c1f4)
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
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

--- 70 unchanged lines hidden (view full) ---

79#define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET 0x03
80#define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE 0x04
81#define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET 0x05
82#define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE 0x06
83
84/* BNEP extension header types */
85#define BNEP_EXT_HEADER_TYPE_EXTENSION_CONTROL 0x00
86
1/*
2 * Copyright (C) 2014 BlueKitchen GmbH
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

--- 70 unchanged lines hidden (view full) ---

79#define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET 0x03
80#define BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE 0x04
81#define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET 0x05
82#define BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE 0x06
83
84/* BNEP extension header types */
85#define BNEP_EXT_HEADER_TYPE_EXTENSION_CONTROL 0x00
86
87/* BNEP setup response codes */
88#define BNEP_RESP_SETUP_SUCCESS 0x0000
89#define BNEP_RESP_SETUP_INVALID_DEST_UUID 0x0001
90#define BNEP_RESP_SETUP_INVALID_SOURCE_UUID 0x0002
91#define BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE 0x0003
92#define BNEP_RESP_SETUP_CONNECTION_NOT_ALLOWED 0x0004
93
94/* BNEP filter response codes */
95#define BNEP_RESP_FILTER_SUCCESS 0x0000
96#define BNEP_RESP_FILTER_UNSUPPORTED_REQUEST 0x0001
97#define BNEP_RESP_FILTER_ERR_INVALID_RANGE 0x0002
98#define BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS 0x0003
99#define BNEP_RESP_FILTER_ERR_SECURITY 0x0004
100
101#define BNEP_CONNECTION_TIMEOUT_MS 10000

--- 717 unchanged lines hidden (view full) ---

819 l2cap_disconnect(l2cap_cid);
820}
821
822static int bnep_handle_connection_request(bnep_channel_t *channel, uint8_t *packet, uint16_t size)
823{
824 uint16_t uuid_size;
825 uint16_t uuid_offset = 0; // avoid "may be unitialized when used" in clang
826 uuid_size = packet[1];
87/* BNEP filter response codes */
88#define BNEP_RESP_FILTER_SUCCESS 0x0000
89#define BNEP_RESP_FILTER_UNSUPPORTED_REQUEST 0x0001
90#define BNEP_RESP_FILTER_ERR_INVALID_RANGE 0x0002
91#define BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS 0x0003
92#define BNEP_RESP_FILTER_ERR_SECURITY 0x0004
93
94#define BNEP_CONNECTION_TIMEOUT_MS 10000

--- 717 unchanged lines hidden (view full) ---

812 l2cap_disconnect(l2cap_cid);
813}
814
815static int bnep_handle_connection_request(bnep_channel_t *channel, uint8_t *packet, uint16_t size)
816{
817 uint16_t uuid_size;
818 uint16_t uuid_offset = 0; // avoid "may be unitialized when used" in clang
819 uuid_size = packet[1];
827 uint16_t response_code = BNEP_RESP_SETUP_SUCCESS;
820 uint16_t response_code = BNEP_SETUP_CONNECTION_RESPONSE_SUCCESS;
828 bnep_service_t * service;
829
830 /* Sanity check packet size */
831 if (size < (1 + 1 + (2 * uuid_size))) {
832 return 0;
833 }
834
835 if ((channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST) &&

--- 9 unchanged lines hidden (view full) ---

845 uuid_offset = 0;
846 break;
847 case 4: /* UUID32 */
848 case 16: /* UUID128 */
849 uuid_offset = 2;
850 break;
851 default:
852 log_error("BNEP_CONNECTION_REQUEST: Invalid UUID size %d, l2cap_cid: %d!", channel->state, channel->l2cap_cid);
821 bnep_service_t * service;
822
823 /* Sanity check packet size */
824 if (size < (1 + 1 + (2 * uuid_size))) {
825 return 0;
826 }
827
828 if ((channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST) &&

--- 9 unchanged lines hidden (view full) ---

838 uuid_offset = 0;
839 break;
840 case 4: /* UUID32 */
841 case 16: /* UUID128 */
842 uuid_offset = 2;
843 break;
844 default:
845 log_error("BNEP_CONNECTION_REQUEST: Invalid UUID size %d, l2cap_cid: %d!", channel->state, channel->l2cap_cid);
853 response_code = BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE;
846 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SERVICE_UUID_SIZE;
854 break;
855 }
856
857 /* Check bits 16-31 of UUID */
858 if (uuid_size > 2){
859 uint16_t dest_prefix = big_endian_read_16(packet, 2);
860 if (dest_prefix != 0){
847 break;
848 }
849
850 /* Check bits 16-31 of UUID */
851 if (uuid_size > 2){
852 uint16_t dest_prefix = big_endian_read_16(packet, 2);
853 if (dest_prefix != 0){
861 response_code = BNEP_RESP_SETUP_INVALID_DEST_UUID;
854 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_DEST_UUID;
862 }
863 uint16_t src_prefix = big_endian_read_16(packet, 2 + uuid_size);
864 if (src_prefix != 0){
855 }
856 uint16_t src_prefix = big_endian_read_16(packet, 2 + uuid_size);
857 if (src_prefix != 0){
865 response_code = BNEP_RESP_SETUP_INVALID_SOURCE_UUID;
858 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SOURCE_UUID;
866 }
867 }
868
869 /* check bits 32-127 of UUID */
870 if (uuid_size == 16){
871 if (uuid_has_bluetooth_prefix(&packet[2]) == false){
859 }
860 }
861
862 /* check bits 32-127 of UUID */
863 if (uuid_size == 16){
864 if (uuid_has_bluetooth_prefix(&packet[2]) == false){
872 response_code = BNEP_RESP_SETUP_INVALID_DEST_UUID;
865 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_DEST_UUID;
873 }
874 if (uuid_has_bluetooth_prefix(&packet[2+16]) == false){
866 }
867 if (uuid_has_bluetooth_prefix(&packet[2+16]) == false){
875 response_code = BNEP_RESP_SETUP_INVALID_SOURCE_UUID;
868 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SOURCE_UUID;
876 }
877 }
878
879 /* Check source and destination UUIDs for valid combinations */
869 }
870 }
871
872 /* Check source and destination UUIDs for valid combinations */
880 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
873 if (response_code == BNEP_SETUP_CONNECTION_RESPONSE_SUCCESS) {
881 channel->uuid_dest = big_endian_read_16(packet, 2 + uuid_offset);
882 channel->uuid_source = big_endian_read_16(packet, 2 + uuid_offset + uuid_size);
883
884 if ((channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_PANU) &&
885 (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_NAP) &&
886 (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_GN)) {
887 log_error("BNEP_CONNECTION_REQUEST: Invalid destination service UUID: %04x", channel->uuid_dest);
888 channel->uuid_dest = 0;
889 }
890 if ((channel->uuid_source != BLUETOOTH_SERVICE_CLASS_PANU) &&
891 (channel->uuid_source != BLUETOOTH_SERVICE_CLASS_NAP) &&
892 (channel->uuid_source != BLUETOOTH_SERVICE_CLASS_GN)) {
893 log_error("BNEP_CONNECTION_REQUEST: Invalid source service UUID: %04x", channel->uuid_source);
894 channel->uuid_source = 0;
895 }
896
897 /* Check if we have registered a service for the requested destination UUID */
898 service = bnep_service_for_uuid(channel->uuid_dest);
899 if (service == NULL) {
874 channel->uuid_dest = big_endian_read_16(packet, 2 + uuid_offset);
875 channel->uuid_source = big_endian_read_16(packet, 2 + uuid_offset + uuid_size);
876
877 if ((channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_PANU) &&
878 (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_NAP) &&
879 (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_GN)) {
880 log_error("BNEP_CONNECTION_REQUEST: Invalid destination service UUID: %04x", channel->uuid_dest);
881 channel->uuid_dest = 0;
882 }
883 if ((channel->uuid_source != BLUETOOTH_SERVICE_CLASS_PANU) &&
884 (channel->uuid_source != BLUETOOTH_SERVICE_CLASS_NAP) &&
885 (channel->uuid_source != BLUETOOTH_SERVICE_CLASS_GN)) {
886 log_error("BNEP_CONNECTION_REQUEST: Invalid source service UUID: %04x", channel->uuid_source);
887 channel->uuid_source = 0;
888 }
889
890 /* Check if we have registered a service for the requested destination UUID */
891 service = bnep_service_for_uuid(channel->uuid_dest);
892 if (service == NULL) {
900 response_code = BNEP_RESP_SETUP_INVALID_DEST_UUID;
893 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_DEST_UUID;
901 } else {
902 // use packet handler for service
903 channel->packet_handler = service->packet_handler;
904
905 if ((channel->uuid_source != BLUETOOTH_SERVICE_CLASS_PANU) && (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_PANU)) {
894 } else {
895 // use packet handler for service
896 channel->packet_handler = service->packet_handler;
897
898 if ((channel->uuid_source != BLUETOOTH_SERVICE_CLASS_PANU) && (channel->uuid_dest != BLUETOOTH_SERVICE_CLASS_PANU)) {
906 response_code = BNEP_RESP_SETUP_INVALID_SOURCE_UUID;
899 response_code = BNEP_SETUP_CONNECTION_RESPONSE_INVALID_SOURCE_UUID;
907 }
908 }
909 }
910
911 /* Set flag to send out the connection response on next statemachine cycle */
912 bnep_channel_state_add(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE);
913 channel->response_code = response_code;
914 l2cap_request_can_send_now_event(channel->l2cap_cid);

--- 13 unchanged lines hidden (view full) ---

928 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
929 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
930 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
931 return 1 + 2;
932 }
933
934 uint16_t response_code = big_endian_read_16(packet, 1);
935
900 }
901 }
902 }
903
904 /* Set flag to send out the connection response on next statemachine cycle */
905 bnep_channel_state_add(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_RESPONSE);
906 channel->response_code = response_code;
907 l2cap_request_can_send_now_event(channel->l2cap_cid);

--- 13 unchanged lines hidden (view full) ---

921 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
922 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
923 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
924 return 1 + 2;
925 }
926
927 uint16_t response_code = big_endian_read_16(packet, 1);
928
936 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
929 if (response_code == BNEP_SETUP_CONNECTION_RESPONSE_SUCCESS) {
937 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
938 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
939 /* Stop timeout timer! */
940 bnep_channel_stop_timer(channel);
941 bnep_emit_open_channel_complete(channel, ERROR_CODE_SUCCESS, response_code);
942 } else {
943 log_error("BNEP_CONNECTION_RESPONSE: Connection to %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
944 bnep_emit_open_channel_complete(channel, BNEP_SETUP_CONNECTION_ERROR, response_code);

--- 774 unchanged lines hidden ---
930 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
931 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
932 /* Stop timeout timer! */
933 bnep_channel_stop_timer(channel);
934 bnep_emit_open_channel_complete(channel, ERROR_CODE_SUCCESS, response_code);
935 } else {
936 log_error("BNEP_CONNECTION_RESPONSE: Connection to %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
937 bnep_emit_open_channel_complete(channel, BNEP_SETUP_CONNECTION_ERROR, response_code);

--- 774 unchanged lines hidden ---