1 /******************************************************************************
2  *
3  *  Copyright 2001-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains internally used BNEP definitions
22  *
23  ******************************************************************************/
24 
25 #ifndef BNEP_INT_H
26 #define BNEP_INT_H
27 
28 #include "bnep_api.h"
29 #include "internal_include/bt_target.h"
30 #include "osi/include/alarm.h"
31 #include "osi/include/fixed_queue.h"
32 #include "stack/include/bt_hdr.h"
33 #include "stack/include/l2cap_interface.h"
34 #include "types/bluetooth/uuid.h"
35 #include "types/raw_address.h"
36 
37 /* BNEP frame types
38  */
39 #define BNEP_FRAME_GENERAL_ETHERNET 0x00
40 #define BNEP_FRAME_CONTROL 0x01
41 #define BNEP_FRAME_COMPRESSED_ETHERNET 0x02
42 #define BNEP_FRAME_COMPRESSED_ETHERNET_SRC_ONLY 0x03
43 #define BNEP_FRAME_COMPRESSED_ETHERNET_DEST_ONLY 0x04
44 
45 /* BNEP filter control message types
46  */
47 #define BNEP_CONTROL_COMMAND_NOT_UNDERSTOOD 0x00
48 #define BNEP_SETUP_CONNECTION_REQUEST_MSG 0x01
49 #define BNEP_SETUP_CONNECTION_RESPONSE_MSG 0x02
50 #define BNEP_FILTER_NET_TYPE_SET_MSG 0x03
51 #define BNEP_FILTER_NET_TYPE_RESPONSE_MSG 0x04
52 #define BNEP_FILTER_MULTI_ADDR_SET_MSG 0x05
53 #define BNEP_FILTER_MULTI_ADDR_RESPONSE_MSG 0x06
54 
55 /* BNEP header extension types
56  */
57 #define BNEP_EXTENSION_FILTER_CONTROL 0x00
58 
59 /* BNEP Setup Connection response codes
60  */
61 #define BNEP_SETUP_CONN_OK 0x0000
62 #define BNEP_SETUP_INVALID_DEST_UUID 0x0001
63 #define BNEP_SETUP_INVALID_SRC_UUID 0x0002
64 #define BNEP_SETUP_INVALID_UUID_SIZE 0x0003
65 #define BNEP_SETUP_CONN_NOT_ALLOWED 0x0004
66 
67 /* BNEP filter control response codes
68  */
69 #define BNEP_FILTER_CRL_OK 0x0000
70 #define BNEP_FILTER_CRL_UNSUPPORTED 0x0001
71 #define BNEP_FILTER_CRL_BAD_RANGE 0x0002
72 #define BNEP_FILTER_CRL_MAX_REACHED 0x0003
73 #define BNEP_FILTER_CRL_SECURITY_ERR 0x0004
74 
75 /* 802.1p protocol packet will have actual protocol field in side the payload */
76 #define BNEP_802_1_P_PROTOCOL 0x8100
77 
78 /* Timeout definitions.  */
79 /* Connection related timeout */
80 #define BNEP_CONN_TIMEOUT_MS (20 * 1000)
81 /* host response timeout */
82 #define BNEP_HOST_TIMEOUT_MS (200 * 1000)
83 #define BNEP_FILTER_SET_TIMEOUT_MS (10 * 1000)
84 
85 #define BNEP_MAX_RETRANSMITS 3
86 
87 /* Define the BNEP Connection Control Block
88  */
89 typedef struct {
90 #define BNEP_STATE_IDLE 0
91 #define BNEP_STATE_CONN_START 1
92 #define BNEP_STATE_CFG_SETUP 2
93 #define BNEP_STATE_CONN_SETUP 3
94 #define BNEP_STATE_SEC_CHECKING 4
95 #define BNEP_STATE_SETUP_RCVD 5
96 #define BNEP_STATE_CONNECTED 6
97   uint8_t con_state;
98 
99 #define BNEP_FLAGS_IS_ORIG 0x01
100 #define BNEP_FLAGS_HIS_CFG_DONE 0x02
101 #define BNEP_FLAGS_MY_CFG_DONE 0x04
102 #define BNEP_FLAGS_L2CAP_CONGESTED 0x08
103 #define BNEP_FLAGS_FILTER_RESP_PEND 0x10
104 #define BNEP_FLAGS_MULTI_RESP_PEND 0x20
105 #define BNEP_FLAGS_SETUP_RCVD 0x40
106 #define BNEP_FLAGS_CONN_COMPLETED 0x80
107   uint8_t con_flags;
108   BT_HDR* p_pending_data;
109 
110   uint16_t l2cap_cid;
111   RawAddress rem_bda;
112   alarm_t* conn_timer;
113   fixed_queue_t* xmit_q;
114 
115   uint16_t sent_num_filters;
116   uint16_t sent_prot_filter_start[BNEP_MAX_PROT_FILTERS];
117   uint16_t sent_prot_filter_end[BNEP_MAX_PROT_FILTERS];
118 
119   uint16_t sent_mcast_filters;
120   RawAddress sent_mcast_filter_start[BNEP_MAX_MULTI_FILTERS];
121   RawAddress sent_mcast_filter_end[BNEP_MAX_MULTI_FILTERS];
122 
123   uint16_t rcvd_num_filters;
124   uint16_t rcvd_prot_filter_start[BNEP_MAX_PROT_FILTERS];
125   uint16_t rcvd_prot_filter_end[BNEP_MAX_PROT_FILTERS];
126 
127   uint16_t rcvd_mcast_filters;
128   RawAddress rcvd_mcast_filter_start[BNEP_MAX_MULTI_FILTERS];
129   RawAddress rcvd_mcast_filter_end[BNEP_MAX_MULTI_FILTERS];
130 
131   uint16_t bad_pkts_rcvd;
132   uint8_t re_transmits;
133   uint16_t handle;
134   bluetooth::Uuid prv_src_uuid;
135   bluetooth::Uuid prv_dst_uuid;
136   bluetooth::Uuid src_uuid;
137   bluetooth::Uuid dst_uuid;
138 } tBNEP_CONN;
139 
140 /*  The main BNEP control block
141  */
142 typedef struct {
143   tL2CAP_CFG_INFO l2cap_my_cfg; /* My L2CAP config     */
144   tBNEP_CONN bcb[BNEP_MAX_CONNECTIONS];
145 
146   tBNEP_CONNECT_IND_CB* p_conn_ind_cb;
147   tBNEP_CONN_STATE_CB* p_conn_state_cb;
148   tBNEP_DATA_IND_CB* p_data_ind_cb;
149   tBNEP_DATA_BUF_CB* p_data_buf_cb;
150   tBNEP_FILTER_IND_CB* p_filter_ind_cb;
151   tBNEP_MFILTER_IND_CB* p_mfilter_ind_cb;
152   tBNEP_TX_DATA_FLOW_CB* p_tx_data_flow_cb;
153 
154   tL2CAP_APPL_INFO reg_info;
155 
156   bool profile_registered; /* true when we got our BD addr */
157 } tBNEP_CB;
158 
159 /* Global BNEP data
160  */
161 extern tBNEP_CB bnep_cb;
162 
163 /* Functions provided by bnep_main.cc
164  */
165 tBNEP_RESULT bnep_register_with_l2cap(void);
166 void bnep_disconnect(tBNEP_CONN* p_bcb, uint16_t reason);
167 tBNEP_CONN* bnep_conn_originate(uint8_t* p_bd_addr);
168 void bnep_conn_timer_timeout(void* data);
169 void bnep_connected(tBNEP_CONN* p_bcb);
170 
171 /* Functions provided by bnep_utils.cc
172  */
173 tBNEP_CONN* bnepu_find_bcb_by_cid(uint16_t cid);
174 tBNEP_CONN* bnepu_find_bcb_by_bd_addr(const RawAddress& p_bda);
175 tBNEP_CONN* bnepu_allocate_bcb(const RawAddress& p_rem_bda);
176 void bnepu_release_bcb(tBNEP_CONN* p_bcb);
177 void bnepu_send_peer_our_filters(tBNEP_CONN* p_bcb);
178 void bnepu_send_peer_our_multi_filters(tBNEP_CONN* p_bcb);
179 bool bnepu_does_dest_support_prot(tBNEP_CONN* p_bcb, uint16_t protocol);
180 void bnepu_build_bnep_hdr(tBNEP_CONN* p_bcb, BT_HDR* p_buf, uint16_t protocol,
181                           const RawAddress& src_addr, const RawAddress& dest_addr, bool ext_bit);
182 void test_bnepu_build_bnep_hdr(tBNEP_CONN* p_bcb, BT_HDR* p_buf, uint16_t protocol,
183                                uint8_t* p_src_addr, uint8_t* p_dest_addr, uint8_t type);
184 
185 tBNEP_CONN* bnepu_get_route_to_dest(uint8_t* p_bda);
186 void bnepu_check_send_packet(tBNEP_CONN* p_bcb, BT_HDR* p_buf);
187 void bnep_send_command_not_understood(tBNEP_CONN* p_bcb, uint8_t cmd_code);
188 void bnepu_process_peer_filter_set(tBNEP_CONN* p_bcb, uint8_t* p_filters, uint16_t len);
189 void bnepu_process_peer_filter_rsp(tBNEP_CONN* p_bcb, uint8_t* p_data);
190 void bnepu_process_multicast_filter_rsp(tBNEP_CONN* p_bcb, uint8_t* p_data);
191 void bnep_send_conn_req(tBNEP_CONN* p_bcb);
192 void bnep_send_conn_response(tBNEP_CONN* p_bcb, uint16_t resp_code);
193 void bnep_process_setup_conn_req(tBNEP_CONN* p_bcb, uint8_t* p_setup, uint8_t len);
194 void bnep_process_setup_conn_response(tBNEP_CONN* p_bcb, uint8_t* p_setup);
195 uint8_t* bnep_process_control_packet(tBNEP_CONN* p_bcb, uint8_t* p, uint16_t* len, bool is_ext);
196 void bnep_sec_check_complete(const RawAddress* bd_addr, tBT_TRANSPORT transport, void* p_ref_data);
197 tBNEP_RESULT bnep_is_packet_allowed(tBNEP_CONN* p_bcb, const RawAddress& dest_addr,
198                                     uint16_t protocol, bool fw_ext_present, uint8_t* p_data,
199                                     uint16_t org_len);
200 
201 #endif
202