bnep.c (ce8f182ecc1074daae9aa7dbf600423af63ab5af) bnep.c (f8fbdce0c5067e7e7edd3a29934b1f9b79c8ff2d)
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

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

77
78static void bnep_emit_open_channel_complete(bnep_channel_t *channel, uint8_t status)
79{
80 log_info("BNEP_EVENT_OPEN_CHANNEL_COMPLETE status 0x%02x bd_addr: %s", status, bd_addr_to_str(channel->remote_addr));
81 uint8_t event[3 + sizeof(bd_addr_t) + 3 * sizeof(uint16_t)];
82 event[0] = BNEP_EVENT_OPEN_CHANNEL_COMPLETE;
83 event[1] = sizeof(event) - 2;
84 event[2] = status;
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

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

77
78static void bnep_emit_open_channel_complete(bnep_channel_t *channel, uint8_t status)
79{
80 log_info("BNEP_EVENT_OPEN_CHANNEL_COMPLETE status 0x%02x bd_addr: %s", status, bd_addr_to_str(channel->remote_addr));
81 uint8_t event[3 + sizeof(bd_addr_t) + 3 * sizeof(uint16_t)];
82 event[0] = BNEP_EVENT_OPEN_CHANNEL_COMPLETE;
83 event[1] = sizeof(event) - 2;
84 event[2] = status;
85 bt_store_16(event, 3, channel->uuid_source);
86 bt_store_16(event, 5, channel->uuid_dest);
87 bt_store_16(event, 7, channel->max_frame_size);
85 little_endian_store_16(event, 3, channel->uuid_source);
86 little_endian_store_16(event, 5, channel->uuid_dest);
87 little_endian_store_16(event, 7, channel->max_frame_size);
88 BD_ADDR_COPY(&event[9], channel->remote_addr);
89 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
90 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
91}
92
93static void bnep_emit_channel_timeout(bnep_channel_t *channel)
94{
95 log_info("BNEP_EVENT_CHANNEL_TIMEOUT bd_addr: %s", bd_addr_to_str(channel->remote_addr));
96 uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t) + sizeof(uint8_t)];
97 event[0] = BNEP_EVENT_CHANNEL_TIMEOUT;
98 event[1] = sizeof(event) - 2;
88 BD_ADDR_COPY(&event[9], channel->remote_addr);
89 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
90 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
91}
92
93static void bnep_emit_channel_timeout(bnep_channel_t *channel)
94{
95 log_info("BNEP_EVENT_CHANNEL_TIMEOUT bd_addr: %s", bd_addr_to_str(channel->remote_addr));
96 uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t) + sizeof(uint8_t)];
97 event[0] = BNEP_EVENT_CHANNEL_TIMEOUT;
98 event[1] = sizeof(event) - 2;
99 bt_store_16(event, 2, channel->uuid_source);
100 bt_store_16(event, 4, channel->uuid_dest);
99 little_endian_store_16(event, 2, channel->uuid_source);
100 little_endian_store_16(event, 4, channel->uuid_dest);
101 BD_ADDR_COPY(&event[6], channel->remote_addr);
102 event[12] = channel->state;
103 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
104 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
105}
106
107static void bnep_emit_channel_closed(bnep_channel_t *channel)
108{
109 log_info("BNEP_EVENT_CHANNEL_CLOSED bd_addr: %s", bd_addr_to_str(channel->remote_addr));
110 uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t)];
111 event[0] = BNEP_EVENT_CHANNEL_CLOSED;
112 event[1] = sizeof(event) - 2;
101 BD_ADDR_COPY(&event[6], channel->remote_addr);
102 event[12] = channel->state;
103 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
104 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
105}
106
107static void bnep_emit_channel_closed(bnep_channel_t *channel)
108{
109 log_info("BNEP_EVENT_CHANNEL_CLOSED bd_addr: %s", bd_addr_to_str(channel->remote_addr));
110 uint8_t event[2 + sizeof(bd_addr_t) + 2 * sizeof(uint16_t)];
111 event[0] = BNEP_EVENT_CHANNEL_CLOSED;
112 event[1] = sizeof(event) - 2;
113 bt_store_16(event, 2, channel->uuid_source);
114 bt_store_16(event, 4, channel->uuid_dest);
113 little_endian_store_16(event, 2, channel->uuid_source);
114 little_endian_store_16(event, 4, channel->uuid_dest);
115 BD_ADDR_COPY(&event[6], channel->remote_addr);
116 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
117 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
118}
119
120static void bnep_emit_ready_to_send(bnep_channel_t *channel)
121{
122 uint8_t event[2];

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

173 /* Setup control packet type */
174 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
175 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_SETUP_CONNECTION_REQUEST;
176
177 /* Add UUID Size */
178 bnep_out_buffer[pos++] = 2;
179
180 /* Add dest and source UUID */
115 BD_ADDR_COPY(&event[6], channel->remote_addr);
116 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event));
117 (*app_packet_handler)(HCI_EVENT_PACKET, channel->l2cap_cid, (uint8_t *) event, sizeof(event));
118}
119
120static void bnep_emit_ready_to_send(bnep_channel_t *channel)
121{
122 uint8_t event[2];

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

173 /* Setup control packet type */
174 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
175 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_SETUP_CONNECTION_REQUEST;
176
177 /* Add UUID Size */
178 bnep_out_buffer[pos++] = 2;
179
180 /* Add dest and source UUID */
181 net_store_16(bnep_out_buffer, pos, uuid_dest);
181 big_endian_store_16(bnep_out_buffer, pos, uuid_dest);
182 pos += 2;
183
182 pos += 2;
183
184 net_store_16(bnep_out_buffer, pos, uuid_source);
184 big_endian_store_16(bnep_out_buffer, pos, uuid_source);
185 pos += 2;
186
187 err = l2cap_send_prepared(channel->l2cap_cid, pos);
188
189 if (err) {
190 // TODO: Log error
191 }
192 return err;

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

206 l2cap_reserve_packet_buffer();
207 bnep_out_buffer = l2cap_get_outgoing_buffer();
208
209 /* Setup control packet type */
210 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
211 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_SETUP_CONNECTION_RESPONSE;
212
213 /* Add response code */
185 pos += 2;
186
187 err = l2cap_send_prepared(channel->l2cap_cid, pos);
188
189 if (err) {
190 // TODO: Log error
191 }
192 return err;

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

206 l2cap_reserve_packet_buffer();
207 bnep_out_buffer = l2cap_get_outgoing_buffer();
208
209 /* Setup control packet type */
210 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
211 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_SETUP_CONNECTION_RESPONSE;
212
213 /* Add response code */
214 net_store_16(bnep_out_buffer, pos, response_code);
214 big_endian_store_16(bnep_out_buffer, pos, response_code);
215 pos += 2;
216
217 err = l2cap_send_prepared(channel->l2cap_cid, pos);
218
219 if (err) {
220 // TODO: Log error
221 }
222 return err;

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

236
237 l2cap_reserve_packet_buffer();
238 bnep_out_buffer = l2cap_get_outgoing_buffer();
239
240 /* Setup control packet type */
241 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
242 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET;
243
215 pos += 2;
216
217 err = l2cap_send_prepared(channel->l2cap_cid, pos);
218
219 if (err) {
220 // TODO: Log error
221 }
222 return err;

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

236
237 l2cap_reserve_packet_buffer();
238 bnep_out_buffer = l2cap_get_outgoing_buffer();
239
240 /* Setup control packet type */
241 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
242 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_NET_TYPE_SET;
243
244 net_store_16(bnep_out_buffer, pos, len * 2 * 2);
244 big_endian_store_16(bnep_out_buffer, pos, len * 2 * 2);
245 pos += 2;
246
247 for (i = 0; i < len; i ++) {
245 pos += 2;
246
247 for (i = 0; i < len; i ++) {
248 net_store_16(bnep_out_buffer, pos, filter[i].range_start);
248 big_endian_store_16(bnep_out_buffer, pos, filter[i].range_start);
249 pos += 2;
249 pos += 2;
250 net_store_16(bnep_out_buffer, pos, filter[i].range_end);
250 big_endian_store_16(bnep_out_buffer, pos, filter[i].range_end);
251 pos += 2;
252 }
253
254 err = l2cap_send_prepared(channel->l2cap_cid, pos);
255
256 if (err) {
257 // TODO: Log error
258 }

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

273 l2cap_reserve_packet_buffer();
274 bnep_out_buffer = l2cap_get_outgoing_buffer();
275
276 /* Setup control packet type */
277 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
278 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE;
279
280 /* Add response code */
251 pos += 2;
252 }
253
254 err = l2cap_send_prepared(channel->l2cap_cid, pos);
255
256 if (err) {
257 // TODO: Log error
258 }

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

273 l2cap_reserve_packet_buffer();
274 bnep_out_buffer = l2cap_get_outgoing_buffer();
275
276 /* Setup control packet type */
277 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
278 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_NET_TYPE_RESPONSE;
279
280 /* Add response code */
281 net_store_16(bnep_out_buffer, pos, response_code);
281 big_endian_store_16(bnep_out_buffer, pos, response_code);
282 pos += 2;
283
284 err = l2cap_send_prepared(channel->l2cap_cid, pos);
285
286 if (err) {
287 // TODO: Log error
288 }
289 return err;

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

304
305 l2cap_reserve_packet_buffer();
306 bnep_out_buffer = l2cap_get_outgoing_buffer();
307
308 /* Setup control packet type */
309 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
310 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET;
311
282 pos += 2;
283
284 err = l2cap_send_prepared(channel->l2cap_cid, pos);
285
286 if (err) {
287 // TODO: Log error
288 }
289 return err;

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

304
305 l2cap_reserve_packet_buffer();
306 bnep_out_buffer = l2cap_get_outgoing_buffer();
307
308 /* Setup control packet type */
309 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
310 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_SET;
311
312 net_store_16(bnep_out_buffer, pos, len * 2 * ETHER_ADDR_LEN);
312 big_endian_store_16(bnep_out_buffer, pos, len * 2 * ETHER_ADDR_LEN);
313 pos += 2;
314
315 for (i = 0; i < len; i ++) {
316 BD_ADDR_COPY(bnep_out_buffer + pos, filter[i].addr_start);
317 pos += ETHER_ADDR_LEN;
318 BD_ADDR_COPY(bnep_out_buffer + pos, filter[i].addr_end);
319 pos += ETHER_ADDR_LEN;
320 }

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

341 l2cap_reserve_packet_buffer();
342 bnep_out_buffer = l2cap_get_outgoing_buffer();
343
344 /* Setup control packet type */
345 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
346 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE;
347
348 /* Add response code */
313 pos += 2;
314
315 for (i = 0; i < len; i ++) {
316 BD_ADDR_COPY(bnep_out_buffer + pos, filter[i].addr_start);
317 pos += ETHER_ADDR_LEN;
318 BD_ADDR_COPY(bnep_out_buffer + pos, filter[i].addr_end);
319 pos += ETHER_ADDR_LEN;
320 }

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

341 l2cap_reserve_packet_buffer();
342 bnep_out_buffer = l2cap_get_outgoing_buffer();
343
344 /* Setup control packet type */
345 bnep_out_buffer[pos++] = BNEP_PKT_TYPE_CONTROL;
346 bnep_out_buffer[pos++] = BNEP_CONTROL_TYPE_FILTER_MULTI_ADDR_RESPONSE;
347
348 /* Add response code */
349 net_store_16(bnep_out_buffer, pos, response_code);
349 big_endian_store_16(bnep_out_buffer, pos, response_code);
350 pos += 2;
351
352 err = l2cap_send_prepared(channel->l2cap_cid, pos);
353
354 if (err) {
355 // TODO: Log error
356 }
357 return err;

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

447 }
448
449 /* Extract destination and source address from the ethernet packet */
450 pos = 0;
451 BD_ADDR_COPY(addr_dest, &packet[pos]);
452 pos += sizeof(bd_addr_t);
453 BD_ADDR_COPY(addr_source, &packet[pos]);
454 pos += sizeof(bd_addr_t);
350 pos += 2;
351
352 err = l2cap_send_prepared(channel->l2cap_cid, pos);
353
354 if (err) {
355 // TODO: Log error
356 }
357 return err;

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

447 }
448
449 /* Extract destination and source address from the ethernet packet */
450 pos = 0;
451 BD_ADDR_COPY(addr_dest, &packet[pos]);
452 pos += sizeof(bd_addr_t);
453 BD_ADDR_COPY(addr_source, &packet[pos]);
454 pos += sizeof(bd_addr_t);
455 network_protocol_type = READ_NET_16(packet, pos);
455 network_protocol_type = big_endian_read_16(packet, pos);
456 pos += sizeof(uint16_t);
457
458 payload_len = len - pos;
459
460 if (network_protocol_type == ETHERTYPE_VLAN) { /* IEEE 802.1Q tag header */
461 if (payload_len < 4) {
462 /* Omit this packet */
463 return 0;
464 }
465 /* The "real" network protocol type is 4 bytes ahead in a VLAN packet */
456 pos += sizeof(uint16_t);
457
458 payload_len = len - pos;
459
460 if (network_protocol_type == ETHERTYPE_VLAN) { /* IEEE 802.1Q tag header */
461 if (payload_len < 4) {
462 /* Omit this packet */
463 return 0;
464 }
465 /* The "real" network protocol type is 4 bytes ahead in a VLAN packet */
466 network_protocol_type = READ_NET_16(packet, pos + 2);
466 network_protocol_type = big_endian_read_16(packet, pos + 2);
467 }
468
469 /* Check network protocol and multicast filters before sending */
470 if (!bnep_filter_protocol(channel, network_protocol_type) ||
471 !bnep_filter_multicast(channel, addr_dest)) {
472 /* Packet did not pass filter... */
473 if ((network_protocol_type == ETHERTYPE_VLAN) &&
474 (payload_len >= 4)) {

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

521
522 /* Add the source address if needed */
523 if (has_source) {
524 BD_ADDR_COPY(bnep_out_buffer + pos_out, addr_source);
525 pos_out += sizeof(bd_addr_t);
526 }
527
528 /* Add protocol type */
467 }
468
469 /* Check network protocol and multicast filters before sending */
470 if (!bnep_filter_protocol(channel, network_protocol_type) ||
471 !bnep_filter_multicast(channel, addr_dest)) {
472 /* Packet did not pass filter... */
473 if ((network_protocol_type == ETHERTYPE_VLAN) &&
474 (payload_len >= 4)) {

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

521
522 /* Add the source address if needed */
523 if (has_source) {
524 BD_ADDR_COPY(bnep_out_buffer + pos_out, addr_source);
525 pos_out += sizeof(bd_addr_t);
526 }
527
528 /* Add protocol type */
529 net_store_16(bnep_out_buffer, pos_out, network_protocol_type);
529 big_endian_store_16(bnep_out_buffer, pos_out, network_protocol_type);
530 pos_out += 2;
531
532 /* TODO: Add extension headers, if we may support them at a later stage */
533 /* Add the payload and then send out the package */
534 memcpy(bnep_out_buffer + pos_out, packet + pos, payload_len);
535 pos_out += payload_len;
536
537 err = l2cap_send_prepared(channel->l2cap_cid, pos_out);

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

790 default:
791 log_error("BNEP_CONNECTION_REQUEST: Invalid UUID size %d, l2cap_cid: %d!", channel->state, channel->l2cap_cid);
792 response_code = BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE;
793 break;
794 }
795
796 /* Check source and destination UUIDs for valid combinations */
797 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
530 pos_out += 2;
531
532 /* TODO: Add extension headers, if we may support them at a later stage */
533 /* Add the payload and then send out the package */
534 memcpy(bnep_out_buffer + pos_out, packet + pos, payload_len);
535 pos_out += payload_len;
536
537 err = l2cap_send_prepared(channel->l2cap_cid, pos_out);

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

790 default:
791 log_error("BNEP_CONNECTION_REQUEST: Invalid UUID size %d, l2cap_cid: %d!", channel->state, channel->l2cap_cid);
792 response_code = BNEP_RESP_SETUP_INVALID_SERVICE_UUID_SIZE;
793 break;
794 }
795
796 /* Check source and destination UUIDs for valid combinations */
797 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
798 channel->uuid_dest = READ_NET_16(packet, 2 + uuid_offset);
799 channel->uuid_source = READ_NET_16(packet, 2 + uuid_offset + uuid_size);
798 channel->uuid_dest = big_endian_read_16(packet, 2 + uuid_offset);
799 channel->uuid_source = big_endian_read_16(packet, 2 + uuid_offset + uuid_size);
800
801 if ((channel->uuid_dest != SDP_PANU) &&
802 (channel->uuid_dest != SDP_NAP) &&
803 (channel->uuid_dest != SDP_GN)) {
804 log_error("BNEP_CONNECTION_REQUEST: Invalid destination service UUID: %04x", channel->uuid_dest);
805 channel->uuid_dest = 0;
806 }
807 if ((channel->uuid_source != SDP_PANU) &&

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

839 }
840
841 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
842 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
843 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
844 return 1 + 2;
845 }
846
800
801 if ((channel->uuid_dest != SDP_PANU) &&
802 (channel->uuid_dest != SDP_NAP) &&
803 (channel->uuid_dest != SDP_GN)) {
804 log_error("BNEP_CONNECTION_REQUEST: Invalid destination service UUID: %04x", channel->uuid_dest);
805 channel->uuid_dest = 0;
806 }
807 if ((channel->uuid_source != SDP_PANU) &&

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

839 }
840
841 if (channel->state != BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE) {
842 /* Ignore a connection response in any state but WAIT_FOR_CONNECTION_RESPONSE */
843 log_error("BNEP_CONNECTION_RESPONSE: Ignored in channel state %d", channel->state);
844 return 1 + 2;
845 }
846
847 response_code = READ_NET_16(packet, 1);
847 response_code = big_endian_read_16(packet, 1);
848
849 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
850 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
851 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
852 /* Stop timeout timer! */
853 bnep_channel_stop_timer(channel);
854 bnep_emit_open_channel_complete(channel, 0);
855 } else {

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

875 uint16_t list_length;
876 uint16_t response_code = BNEP_RESP_FILTER_SUCCESS;
877
878 /* Sanity check packet size */
879 if (size < 3) {
880 return 0;
881 }
882
848
849 if (response_code == BNEP_RESP_SETUP_SUCCESS) {
850 log_info("BNEP_CONNECTION_RESPONSE: Channel established to %s", bd_addr_to_str(channel->remote_addr));
851 channel->state = BNEP_CHANNEL_STATE_CONNECTED;
852 /* Stop timeout timer! */
853 bnep_channel_stop_timer(channel);
854 bnep_emit_open_channel_complete(channel, 0);
855 } else {

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

875 uint16_t list_length;
876 uint16_t response_code = BNEP_RESP_FILTER_SUCCESS;
877
878 /* Sanity check packet size */
879 if (size < 3) {
880 return 0;
881 }
882
883 list_length = READ_NET_16(packet, 1);
883 list_length = big_endian_read_16(packet, 1);
884 /* Sanity check packet size again with known package size */
885 if (size < 3 + list_length) {
886 return 0;
887 }
888
889 if (!bnep_can_handle_extensions(channel)){
890 log_error("BNEP_FILTER_NET_TYPE_SET: Ignored in channel state %d", channel->state);
891 return 3 + list_length;

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

896 log_info("BNEP_FILTER_NET_TYPE_SET: Too many filter");
897 response_code = BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS;
898 } else {
899 int i;
900 channel->net_filter_count = 0;
901 /* There is still enough space, copy the filters to our filter list */
902 /* There is still enough space, copy the filters to our filter list */
903 for (i = 0; i < list_length / (2 * 2); i ++) {
884 /* Sanity check packet size again with known package size */
885 if (size < 3 + list_length) {
886 return 0;
887 }
888
889 if (!bnep_can_handle_extensions(channel)){
890 log_error("BNEP_FILTER_NET_TYPE_SET: Ignored in channel state %d", channel->state);
891 return 3 + list_length;

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

896 log_info("BNEP_FILTER_NET_TYPE_SET: Too many filter");
897 response_code = BNEP_RESP_FILTER_ERR_TOO_MANY_FILTERS;
898 } else {
899 int i;
900 channel->net_filter_count = 0;
901 /* There is still enough space, copy the filters to our filter list */
902 /* There is still enough space, copy the filters to our filter list */
903 for (i = 0; i < list_length / (2 * 2); i ++) {
904 channel->net_filter[channel->net_filter_count].range_start = READ_NET_16(packet, 1 + 2 + i * 4);
905 channel->net_filter[channel->net_filter_count].range_end = READ_NET_16(packet, 1 + 2 + i * 4 + 2);
904 channel->net_filter[channel->net_filter_count].range_start = big_endian_read_16(packet, 1 + 2 + i * 4);
905 channel->net_filter[channel->net_filter_count].range_end = big_endian_read_16(packet, 1 + 2 + i * 4 + 2);
906 if (channel->net_filter[channel->net_filter_count].range_start > channel->net_filter[channel->net_filter_count].range_end) {
907 /* Invalid filter range, ignore this filter rule */
908 log_error("BNEP_FILTER_NET_TYPE_SET: Invalid filter: start: %d, end: %d",
909 channel->net_filter[channel->net_filter_count].range_start,
910 channel->net_filter[channel->net_filter_count].range_end);
911 response_code = BNEP_RESP_FILTER_ERR_INVALID_RANGE;
912 } else {
913 /* Valid filter, increase the filter count */

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

937 return 0;
938 }
939
940 if (!bnep_can_handle_extensions(channel)){
941 log_error("BNEP_FILTER_NET_TYPE_RESPONSE: Ignored in channel state %d", channel->state);
942 return 1 + 2;
943 }
944
906 if (channel->net_filter[channel->net_filter_count].range_start > channel->net_filter[channel->net_filter_count].range_end) {
907 /* Invalid filter range, ignore this filter rule */
908 log_error("BNEP_FILTER_NET_TYPE_SET: Invalid filter: start: %d, end: %d",
909 channel->net_filter[channel->net_filter_count].range_start,
910 channel->net_filter[channel->net_filter_count].range_end);
911 response_code = BNEP_RESP_FILTER_ERR_INVALID_RANGE;
912 } else {
913 /* Valid filter, increase the filter count */

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

937 return 0;
938 }
939
940 if (!bnep_can_handle_extensions(channel)){
941 log_error("BNEP_FILTER_NET_TYPE_RESPONSE: Ignored in channel state %d", channel->state);
942 return 1 + 2;
943 }
944
945 response_code = READ_NET_16(packet, 1);
945 response_code = big_endian_read_16(packet, 1);
946
947 if (response_code == BNEP_RESP_FILTER_SUCCESS) {
948 log_info("BNEP_FILTER_NET_TYPE_RESPONSE: Net filter set successfully for %s", bd_addr_to_str(channel->remote_addr));
949 } else {
950 log_error("BNEP_FILTER_NET_TYPE_RESPONSE: Net filter setting for %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
951 }
952
953 return 1 + 2;

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

958 uint16_t list_length;
959 uint16_t response_code = BNEP_RESP_FILTER_SUCCESS;
960
961 /* Sanity check packet size */
962 if (size < 3) {
963 return 0;
964 }
965
946
947 if (response_code == BNEP_RESP_FILTER_SUCCESS) {
948 log_info("BNEP_FILTER_NET_TYPE_RESPONSE: Net filter set successfully for %s", bd_addr_to_str(channel->remote_addr));
949 } else {
950 log_error("BNEP_FILTER_NET_TYPE_RESPONSE: Net filter setting for %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
951 }
952
953 return 1 + 2;

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

958 uint16_t list_length;
959 uint16_t response_code = BNEP_RESP_FILTER_SUCCESS;
960
961 /* Sanity check packet size */
962 if (size < 3) {
963 return 0;
964 }
965
966 list_length = READ_NET_16(packet, 1);
966 list_length = big_endian_read_16(packet, 1);
967 /* Sanity check packet size again with known package size */
968 if (size < 3 + list_length) {
969 return 0;
970 }
971
972 if (!bnep_can_handle_extensions(channel)){
973 log_error("BNEP_MULTI_ADDR_SET: Ignored in channel state %d", channel->state);
974 return 3 + list_length;

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

1022 return 0;
1023 }
1024
1025 if (!bnep_can_handle_extensions(channel)){
1026 log_error("BNEP_MULTI_ADDR_RESPONSE: Ignored in channel state %d", channel->state);
1027 return 1 + 2;
1028 }
1029
967 /* Sanity check packet size again with known package size */
968 if (size < 3 + list_length) {
969 return 0;
970 }
971
972 if (!bnep_can_handle_extensions(channel)){
973 log_error("BNEP_MULTI_ADDR_SET: Ignored in channel state %d", channel->state);
974 return 3 + list_length;

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

1022 return 0;
1023 }
1024
1025 if (!bnep_can_handle_extensions(channel)){
1026 log_error("BNEP_MULTI_ADDR_RESPONSE: Ignored in channel state %d", channel->state);
1027 return 1 + 2;
1028 }
1029
1030 response_code = READ_NET_16(packet, 1);
1030 response_code = big_endian_read_16(packet, 1);
1031
1032 if (response_code == BNEP_RESP_FILTER_SUCCESS) {
1033 log_info("BNEP_MULTI_ADDR_RESPONSE: Multicast address filter set successfully for %s", bd_addr_to_str(channel->remote_addr));
1034 } else {
1035 log_error("BNEP_MULTI_ADDR_RESPONSE: Multicast address filter setting for %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
1036 }
1037
1038 return 1 + 2;

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

1047 * WARNING: This modifies the data in front of the payload and may overwrite 14 bytes there!
1048 */
1049 uint8_t *ethernet_packet = payload - 2 * sizeof(bd_addr_t) - sizeof(uint16_t);
1050 /* Restore the ethernet packet header */
1051 BD_ADDR_COPY(ethernet_packet + pos, addr_dest);
1052 pos += sizeof(bd_addr_t);
1053 BD_ADDR_COPY(ethernet_packet + pos, addr_source);
1054 pos += sizeof(bd_addr_t);
1031
1032 if (response_code == BNEP_RESP_FILTER_SUCCESS) {
1033 log_info("BNEP_MULTI_ADDR_RESPONSE: Multicast address filter set successfully for %s", bd_addr_to_str(channel->remote_addr));
1034 } else {
1035 log_error("BNEP_MULTI_ADDR_RESPONSE: Multicast address filter setting for %s failed. Err: %d", bd_addr_to_str(channel->remote_addr), response_code);
1036 }
1037
1038 return 1 + 2;

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

1047 * WARNING: This modifies the data in front of the payload and may overwrite 14 bytes there!
1048 */
1049 uint8_t *ethernet_packet = payload - 2 * sizeof(bd_addr_t) - sizeof(uint16_t);
1050 /* Restore the ethernet packet header */
1051 BD_ADDR_COPY(ethernet_packet + pos, addr_dest);
1052 pos += sizeof(bd_addr_t);
1053 BD_ADDR_COPY(ethernet_packet + pos, addr_source);
1054 pos += sizeof(bd_addr_t);
1055 net_store_16(ethernet_packet, pos, network_protocol_type);
1055 big_endian_store_16(ethernet_packet, pos, network_protocol_type);
1056 /* Payload is just in place... */
1057#else
1058 /* Copy ethernet frame to statically allocated buffer. This solution is more
1059 * save, but needs an extra copy and more stack!
1060 */
1061 uint8_t ethernet_packet[BNEP_MTU_MIN];
1062
1063 /* Restore the ethernet packet header */
1064 BD_ADDR_COPY(ethernet_packet + pos, addr_dest);
1065 pos += sizeof(bd_addr_t);
1066 BD_ADDR_COPY(ethernet_packet + pos, addr_source);
1067 pos += sizeof(bd_addr_t);
1056 /* Payload is just in place... */
1057#else
1058 /* Copy ethernet frame to statically allocated buffer. This solution is more
1059 * save, but needs an extra copy and more stack!
1060 */
1061 uint8_t ethernet_packet[BNEP_MTU_MIN];
1062
1063 /* Restore the ethernet packet header */
1064 BD_ADDR_COPY(ethernet_packet + pos, addr_dest);
1065 pos += sizeof(bd_addr_t);
1066 BD_ADDR_COPY(ethernet_packet + pos, addr_source);
1067 pos += sizeof(bd_addr_t);
1068 net_store_16(ethernet_packet, pos, network_protocol_type);
1068 big_endian_store_16(ethernet_packet, pos, network_protocol_type);
1069 pos += 2;
1070 memcpy(ethernet_packet + pos, payload, size);
1071#endif
1072
1073 /* Notify application layer and deliver the ethernet packet */
1074 (*app_packet_handler)(BNEP_DATA_PACKET, channel->uuid_source,
1075 ethernet_packet, size + sizeof(uint16_t) + 2 * sizeof(bd_addr_t));
1076

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

1158 uint8_t status;
1159
1160 switch (packet[0]) {
1161
1162 /* Accept an incoming L2CAP connection on PSM_BNEP */
1163 case L2CAP_EVENT_INCOMING_CONNECTION:
1164 /* L2CAP event data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) */
1165 bt_flip_addr(event_addr, &packet[2]);
1069 pos += 2;
1070 memcpy(ethernet_packet + pos, payload, size);
1071#endif
1072
1073 /* Notify application layer and deliver the ethernet packet */
1074 (*app_packet_handler)(BNEP_DATA_PACKET, channel->uuid_source,
1075 ethernet_packet, size + sizeof(uint16_t) + 2 * sizeof(bd_addr_t));
1076

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

1158 uint8_t status;
1159
1160 switch (packet[0]) {
1161
1162 /* Accept an incoming L2CAP connection on PSM_BNEP */
1163 case L2CAP_EVENT_INCOMING_CONNECTION:
1164 /* L2CAP event data: event(8), len(8), address(48), handle (16), psm (16), source cid(16) dest cid(16) */
1165 bt_flip_addr(event_addr, &packet[2]);
1166 con_handle = READ_BT_16(packet, 8);
1167 psm = READ_BT_16(packet, 10);
1168 l2cap_cid = READ_BT_16(packet, 12);
1166 con_handle = little_endian_read_16(packet, 8);
1167 psm = little_endian_read_16(packet, 10);
1168 l2cap_cid = little_endian_read_16(packet, 12);
1169
1170 if (psm != PSM_BNEP) break;
1171
1172 channel = bnep_channel_for_addr(event_addr);
1173
1174 if (channel) {
1175 log_error("INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_BNEP => decline - channel already exists", l2cap_cid);
1176 l2cap_decline_connection(l2cap_cid, 0x04); // no resources available

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

1198
1199 log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_BNEP => accept", l2cap_cid);
1200 l2cap_accept_connection(l2cap_cid);
1201 return 1;
1202
1203 /* Outgoing L2CAP connection has been opened -> store l2cap_cid, remote_addr */
1204 case L2CAP_EVENT_CHANNEL_OPENED:
1205 /* Check if the l2cap channel has been opened for PSM_BNEP */
1169
1170 if (psm != PSM_BNEP) break;
1171
1172 channel = bnep_channel_for_addr(event_addr);
1173
1174 if (channel) {
1175 log_error("INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_BNEP => decline - channel already exists", l2cap_cid);
1176 l2cap_decline_connection(l2cap_cid, 0x04); // no resources available

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

1198
1199 log_info("L2CAP_EVENT_INCOMING_CONNECTION (l2cap_cid 0x%02x) for PSM_BNEP => accept", l2cap_cid);
1200 l2cap_accept_connection(l2cap_cid);
1201 return 1;
1202
1203 /* Outgoing L2CAP connection has been opened -> store l2cap_cid, remote_addr */
1204 case L2CAP_EVENT_CHANNEL_OPENED:
1205 /* Check if the l2cap channel has been opened for PSM_BNEP */
1206 if (READ_BT_16(packet, 11) != PSM_BNEP) {
1206 if (little_endian_read_16(packet, 11) != PSM_BNEP) {
1207 break;
1208 }
1209
1210 status = packet[2];
1211 log_info("L2CAP_EVENT_CHANNEL_OPENED for PSM_BNEP, status %u", status);
1212
1213 /* Get the bnep channel fpr remote address */
1207 break;
1208 }
1209
1210 status = packet[2];
1211 log_info("L2CAP_EVENT_CHANNEL_OPENED for PSM_BNEP, status %u", status);
1212
1213 /* Get the bnep channel fpr remote address */
1214 con_handle = READ_BT_16(packet, 9);
1215 l2cap_cid = READ_BT_16(packet, 13);
1214 con_handle = little_endian_read_16(packet, 9);
1215 l2cap_cid = little_endian_read_16(packet, 13);
1216 bt_flip_addr(event_addr, &packet[3]);
1217 channel = bnep_channel_for_addr(event_addr);
1218 if (!channel) {
1219 log_error("L2CAP_EVENT_CHANNEL_OPENED but no BNEP channel prepared");
1220 return 1;
1221 }
1222
1223 /* On L2CAP open error discard everything */

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

1238
1239 /* Assign connection handle and l2cap cid */
1240 channel->l2cap_cid = l2cap_cid;
1241 channel->con_handle = con_handle;
1242
1243 /* Initiate the connection request */
1244 channel->state = BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE;
1245 bnep_channel_state_add(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST);
1216 bt_flip_addr(event_addr, &packet[3]);
1217 channel = bnep_channel_for_addr(event_addr);
1218 if (!channel) {
1219 log_error("L2CAP_EVENT_CHANNEL_OPENED but no BNEP channel prepared");
1220 return 1;
1221 }
1222
1223 /* On L2CAP open error discard everything */

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

1238
1239 /* Assign connection handle and l2cap cid */
1240 channel->l2cap_cid = l2cap_cid;
1241 channel->con_handle = con_handle;
1242
1243 /* Initiate the connection request */
1244 channel->state = BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_RESPONSE;
1245 bnep_channel_state_add(channel, BNEP_CHANNEL_STATE_VAR_SND_CONNECTION_REQUEST);
1246 channel->max_frame_size = bnep_max_frame_size_for_l2cap_mtu(READ_BT_16(packet, 17));
1246 channel->max_frame_size = bnep_max_frame_size_for_l2cap_mtu(little_endian_read_16(packet, 17));
1247 bnep_run();
1248 break;
1249 case BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST:
1250 /* New information: channel mtu */
1247 bnep_run();
1248 break;
1249 case BNEP_CHANNEL_STATE_WAIT_FOR_CONNECTION_REQUEST:
1250 /* New information: channel mtu */
1251 channel->max_frame_size = bnep_max_frame_size_for_l2cap_mtu(READ_BT_16(packet, 17));
1251 channel->max_frame_size = bnep_max_frame_size_for_l2cap_mtu(little_endian_read_16(packet, 17));
1252 break;
1253 default:
1254 log_error("L2CAP_EVENT_CHANNEL_OPENED: Invalid state: %d", channel->state);
1255 break;
1256 }
1257 return 1;
1258
1259 case DAEMON_EVENT_HCI_PACKET_SENT:
1260 bnep_run();
1261 break;
1262
1263 case L2CAP_EVENT_CHANNEL_CLOSED:
1264 // data: event (8), len(8), channel (16)
1252 break;
1253 default:
1254 log_error("L2CAP_EVENT_CHANNEL_OPENED: Invalid state: %d", channel->state);
1255 break;
1256 }
1257 return 1;
1258
1259 case DAEMON_EVENT_HCI_PACKET_SENT:
1260 bnep_run();
1261 break;
1262
1263 case L2CAP_EVENT_CHANNEL_CLOSED:
1264 // data: event (8), len(8), channel (16)
1265 l2cap_cid = READ_BT_16(packet, 2);
1265 l2cap_cid = little_endian_read_16(packet, 2);
1266 channel = bnep_channel_for_l2cap_cid(l2cap_cid);
1267 log_info("L2CAP_EVENT_CHANNEL_CLOSED cid 0x%0x, channel %p", l2cap_cid, channel);
1268
1269 if (!channel) {
1270 break;
1271 }
1272
1273 log_info("L2CAP_EVENT_CHANNEL_CLOSED state %u", channel->state);

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

1316 pos ++;
1317
1318 switch(bnep_type) {
1319 case BNEP_PKT_TYPE_GENERAL_ETHERNET:
1320 BD_ADDR_COPY(addr_dest, &packet[pos]);
1321 pos += sizeof(bd_addr_t);
1322 BD_ADDR_COPY(addr_source, &packet[pos]);
1323 pos += sizeof(bd_addr_t);
1266 channel = bnep_channel_for_l2cap_cid(l2cap_cid);
1267 log_info("L2CAP_EVENT_CHANNEL_CLOSED cid 0x%0x, channel %p", l2cap_cid, channel);
1268
1269 if (!channel) {
1270 break;
1271 }
1272
1273 log_info("L2CAP_EVENT_CHANNEL_CLOSED state %u", channel->state);

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

1316 pos ++;
1317
1318 switch(bnep_type) {
1319 case BNEP_PKT_TYPE_GENERAL_ETHERNET:
1320 BD_ADDR_COPY(addr_dest, &packet[pos]);
1321 pos += sizeof(bd_addr_t);
1322 BD_ADDR_COPY(addr_source, &packet[pos]);
1323 pos += sizeof(bd_addr_t);
1324 network_protocol_type = READ_NET_16(packet, pos);
1324 network_protocol_type = big_endian_read_16(packet, pos);
1325 pos += 2;
1326 break;
1327 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET:
1328 BD_ADDR_COPY(addr_dest, channel->local_addr);
1329 BD_ADDR_COPY(addr_source, channel->remote_addr);
1325 pos += 2;
1326 break;
1327 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET:
1328 BD_ADDR_COPY(addr_dest, channel->local_addr);
1329 BD_ADDR_COPY(addr_source, channel->remote_addr);
1330 network_protocol_type = READ_NET_16(packet, pos);
1330 network_protocol_type = big_endian_read_16(packet, pos);
1331 pos += 2;
1332 break;
1333 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET_SOURCE_ONLY:
1334 BD_ADDR_COPY(addr_dest, channel->local_addr);
1335 BD_ADDR_COPY(addr_source, &packet[pos]);
1336 pos += sizeof(bd_addr_t);
1331 pos += 2;
1332 break;
1333 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET_SOURCE_ONLY:
1334 BD_ADDR_COPY(addr_dest, channel->local_addr);
1335 BD_ADDR_COPY(addr_source, &packet[pos]);
1336 pos += sizeof(bd_addr_t);
1337 network_protocol_type = READ_NET_16(packet, pos);
1337 network_protocol_type = big_endian_read_16(packet, pos);
1338 pos += 2;
1339 break;
1340 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET_DEST_ONLY:
1341 BD_ADDR_COPY(addr_dest, &packet[pos]);
1342 pos += sizeof(bd_addr_t);
1343 BD_ADDR_COPY(addr_source, channel->remote_addr);
1338 pos += 2;
1339 break;
1340 case BNEP_PKT_TYPE_COMPRESSED_ETHERNET_DEST_ONLY:
1341 BD_ADDR_COPY(addr_dest, &packet[pos]);
1342 pos += sizeof(bd_addr_t);
1343 BD_ADDR_COPY(addr_source, channel->remote_addr);
1344 network_protocol_type = READ_NET_16(packet, pos);
1344 network_protocol_type = big_endian_read_16(packet, pos);
1345 pos += 2;
1346 break;
1347 case BNEP_PKT_TYPE_CONTROL:
1348 rc = bnep_handle_control_packet(channel, packet + pos, size - pos, 0);
1349 pos += rc;
1350 break;
1351 default:
1352 break;

--- 278 unchanged lines hidden ---
1345 pos += 2;
1346 break;
1347 case BNEP_PKT_TYPE_CONTROL:
1348 rc = bnep_handle_control_packet(channel, packet + pos, size - pos, 0);
1349 pos += rc;
1350 break;
1351 default:
1352 break;

--- 278 unchanged lines hidden ---