1 /** @file
2  *  @brief Internal APIs for Bluetooth L2CAP handling.
3  */
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include <bluetooth/l2cap.h>
12 
13 enum l2cap_conn_list_action {
14 	BT_L2CAP_CHAN_LOOKUP,
15 	BT_L2CAP_CHAN_DETACH,
16 };
17 
18 #define BT_L2CAP_CID_BR_SIG             0x0001
19 #define BT_L2CAP_CID_ATT                0x0004
20 #define BT_L2CAP_CID_LE_SIG             0x0005
21 #define BT_L2CAP_CID_SMP                0x0006
22 #define BT_L2CAP_CID_BR_SMP             0x0007
23 
24 #define BT_L2CAP_PSM_RFCOMM             0x0003
25 
26 struct bt_l2cap_hdr {
27 	u16_t len;
28 	u16_t cid;
29 } __packed;
30 
31 struct bt_l2cap_sig_hdr {
32 	u8_t  code;
33 	u8_t  ident;
34 	u16_t len;
35 } __packed;
36 
37 #define BT_L2CAP_REJ_NOT_UNDERSTOOD     0x0000
38 #define BT_L2CAP_REJ_MTU_EXCEEDED       0x0001
39 #define BT_L2CAP_REJ_INVALID_CID        0x0002
40 
41 #define BT_L2CAP_CMD_REJECT             0x01
42 struct bt_l2cap_cmd_reject {
43 	u16_t reason;
44 	u8_t  data[0];
45 } __packed;
46 
47 struct bt_l2cap_cmd_reject_cid_data {
48 	u16_t scid;
49 	u16_t dcid;
50 } __packed;
51 
52 #define BT_L2CAP_CONN_REQ               0x02
53 struct bt_l2cap_conn_req {
54 	u16_t psm;
55 	u16_t scid;
56 } __packed;
57 
58 /* command statuses in reposnse */
59 #define BT_L2CAP_CS_NO_INFO             0x0000
60 #define BT_L2CAP_CS_AUTHEN_PEND         0x0001
61 
62 /* valid results in conn response on BR/EDR */
63 #define BT_L2CAP_BR_SUCCESS             0x0000
64 #define BT_L2CAP_BR_PENDING             0x0001
65 #define BT_L2CAP_BR_ERR_PSM_NOT_SUPP    0x0002
66 #define BT_L2CAP_BR_ERR_SEC_BLOCK       0x0003
67 #define BT_L2CAP_BR_ERR_NO_RESOURCES    0x0004
68 #define BT_L2CAP_BR_ERR_INVALID_SCID    0x0006
69 #define BT_L2CAP_BR_ERR_SCID_IN_USE     0x0007
70 
71 #define BT_L2CAP_CONN_RSP               0x03
72 struct bt_l2cap_conn_rsp {
73 	u16_t dcid;
74 	u16_t scid;
75 	u16_t result;
76 	u16_t status;
77 } __packed;
78 
79 #define BT_L2CAP_CONF_SUCCESS           0x0000
80 #define BT_L2CAP_CONF_UNACCEPT          0x0001
81 #define BT_L2CAP_CONF_REJECT            0x0002
82 
83 #define BT_L2CAP_CONF_REQ               0x04
84 struct bt_l2cap_conf_req {
85 	u16_t dcid;
86 	u16_t flags;
87 	u8_t  data[0];
88 } __packed;
89 
90 #define BT_L2CAP_CONF_RSP               0x05
91 struct bt_l2cap_conf_rsp {
92 	u16_t scid;
93 	u16_t flags;
94 	u16_t result;
95 	u8_t  data[0];
96 } __packed;
97 
98 /* Option type used by MTU config request data */
99 #define BT_L2CAP_CONF_OPT_MTU           0x01
100 /* Options bits selecting most significant bit (hint) in type field */
101 #define BT_L2CAP_CONF_HINT              0x80
102 #define BT_L2CAP_CONF_MASK              0x7f
103 
104 struct bt_l2cap_conf_opt {
105 	u8_t type;
106 	u8_t len;
107 	u8_t data[0];
108 } __packed;
109 
110 #define BT_L2CAP_DISCONN_REQ            0x06
111 struct bt_l2cap_disconn_req {
112 	u16_t dcid;
113 	u16_t scid;
114 } __packed;
115 
116 #define BT_L2CAP_DISCONN_RSP            0x07
117 struct bt_l2cap_disconn_rsp {
118 	u16_t dcid;
119 	u16_t scid;
120 } __packed;
121 
122 #define BT_L2CAP_INFO_FEAT_MASK         0x0002
123 #define BT_L2CAP_INFO_FIXED_CHAN        0x0003
124 
125 #define BT_L2CAP_INFO_REQ               0x0a
126 struct bt_l2cap_info_req {
127 	u16_t type;
128 } __packed;
129 
130 /* info result */
131 #define BT_L2CAP_INFO_SUCCESS           0x0000
132 #define BT_L2CAP_INFO_NOTSUPP           0x0001
133 
134 #define BT_L2CAP_INFO_RSP               0x0b
135 struct bt_l2cap_info_rsp {
136 	u16_t type;
137 	u16_t result;
138 	u8_t  data[0];
139 } __packed;
140 
141 #define BT_L2CAP_CONN_PARAM_REQ         0x12
142 struct bt_l2cap_conn_param_req {
143 	u16_t min_interval;
144 	u16_t max_interval;
145 	u16_t latency;
146 	u16_t timeout;
147 } __packed;
148 
149 #define BT_L2CAP_CONN_PARAM_ACCEPTED    0x0000
150 #define BT_L2CAP_CONN_PARAM_REJECTED    0x0001
151 
152 #define BT_L2CAP_CONN_PARAM_RSP         0x13
153 struct bt_l2cap_conn_param_rsp {
154 	u16_t result;
155 } __packed;
156 
157 #define BT_L2CAP_LE_CONN_REQ            0x14
158 struct bt_l2cap_le_conn_req {
159 	u16_t psm;
160 	u16_t scid;
161 	u16_t mtu;
162 	u16_t mps;
163 	u16_t credits;
164 } __packed;
165 
166 /* valid results in conn response on LE */
167 #define BT_L2CAP_LE_SUCCESS             0x0000
168 #define BT_L2CAP_LE_ERR_PSM_NOT_SUPP    0x0002
169 #define BT_L2CAP_LE_ERR_NO_RESOURCES    0x0004
170 #define BT_L2CAP_LE_ERR_AUTHENTICATION  0x0005
171 #define BT_L2CAP_LE_ERR_AUTHORIZATION   0x0006
172 #define BT_L2CAP_LE_ERR_KEY_SIZE        0x0007
173 #define BT_L2CAP_LE_ERR_ENCRYPTION      0x0008
174 #define BT_L2CAP_LE_ERR_INVALID_SCID    0x0009
175 #define BT_L2CAP_LE_ERR_SCID_IN_USE     0x000A
176 #define BT_L2CAP_LE_ERR_UNACCEPT_PARAMS 0x000B
177 #define BT_L2CAP_LE_ERR_INVALID_PARAMS  0x000C
178 
179 #define BT_L2CAP_LE_CONN_RSP            0x15
180 struct bt_l2cap_le_conn_rsp {
181 	u16_t dcid;
182 	u16_t mtu;
183 	u16_t mps;
184 	u16_t credits;
185 	u16_t result;
186 } __packed;
187 
188 #define BT_L2CAP_LE_CREDITS             0x16
189 struct bt_l2cap_le_credits {
190 	u16_t cid;
191 	u16_t credits;
192 } __packed;
193 
194 #define BT_L2CAP_ECRED_CONN_REQ         0x17
195 struct bt_l2cap_ecred_conn_req {
196 	u16_t psm;
197 	u16_t mtu;
198 	u16_t mps;
199 	u16_t credits;
200 	u16_t scid[0];
201 } __packed;
202 
203 #define BT_L2CAP_ECRED_CONN_RSP         0x18
204 struct bt_l2cap_ecred_conn_rsp {
205 	u16_t mtu;
206 	u16_t mps;
207 	u16_t credits;
208 	u16_t result;
209 	u16_t dcid[0];
210 } __packed;
211 
212 #define BT_L2CAP_ECRED_RECONF_REQ       0x19
213 struct bt_l2cap_ecred_reconf_req {
214 	u16_t mtu;
215 	u16_t mps;
216 	u16_t scid[0];
217 } __packed;
218 
219 #define BT_L2CAP_RECONF_SUCCESS         0x0000
220 #define BT_L2CAP_RECONF_INVALID_MTU     0x0001
221 #define BT_L2CAP_RECONF_INVALID_MPS     0x0002
222 
223 #define BT_L2CAP_ECRED_RECONF_RSP       0x1a
224 struct bt_l2cap_ecred_reconf_rsp {
225 	u16_t result;
226 } __packed;
227 
228 #define BT_L2CAP_SDU_HDR_LEN            2
229 
230 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
231 #define BT_L2CAP_RX_MTU CONFIG_BT_L2CAP_RX_MTU
232 #else
233 #define BT_L2CAP_RX_MTU (CONFIG_BT_RX_BUF_LEN - \
234 			 BT_HCI_ACL_HDR_SIZE - BT_L2CAP_HDR_SIZE)
235 #endif
236 
237 struct bt_l2cap_fixed_chan {
238 	u16_t		cid;
239 	int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
240 	bt_l2cap_chan_destroy_t destroy;
241 	sys_snode_t node;
242 };
243 
244 /* Register a fixed L2CAP channel for L2CAP */
245 void bt_l2cap_le_fixed_chan_register(struct bt_l2cap_fixed_chan *chan);
246 
247 #define BT_L2CAP_CHANNEL_DEFINE(_name, _cid, _accept, _destroy)         \
248 	static struct bt_l2cap_fixed_chan _name = { \
249 				.cid = _cid,                            \
250 				.accept = _accept,                      \
251 				.destroy = _destroy,                    \
252 			}
253 
254 /* Need a name different than bt_l2cap_fixed_chan for a different section */
255 struct bt_l2cap_br_fixed_chan {
256 	u16_t		cid;
257 	int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
258     sys_snode_t node;
259 };
260 
261 #define BT_L2CAP_BR_CHANNEL_DEFINE(_name, _cid, _accept)		\
262 	static struct bt_l2cap_br_fixed_chan _name = { \
263 				.cid = _cid,			\
264 				.accept = _accept,		\
265 			}
266 
267 /* Notify L2CAP channels of a new connection */
268 void bt_l2cap_connected(struct bt_conn *conn);
269 
270 /* Notify L2CAP channels of a disconnect event */
271 void bt_l2cap_disconnected(struct bt_conn *conn);
272 
273 /* Add channel to the connection */
274 void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
275 		       bt_l2cap_chan_destroy_t destroy);
276 
277 /* Remove channel from the connection */
278 void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
279 
280 /* Delete channel */
281 void bt_l2cap_chan_del(struct bt_l2cap_chan *chan);
282 
283 const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
284 
285 #if defined(CONFIG_BT_DEBUG_L2CAP)
286 void bt_l2cap_chan_set_state_debug(struct bt_l2cap_chan *chan,
287 				   bt_l2cap_chan_state_t state,
288 				   const char *func, int line);
289 #define bt_l2cap_chan_set_state(_chan, _state) \
290 	bt_l2cap_chan_set_state_debug(_chan, _state, __func__, __LINE__)
291 #else
292 void bt_l2cap_chan_set_state(struct bt_l2cap_chan *chan,
293 			     bt_l2cap_chan_state_t state);
294 #endif /* CONFIG_BT_DEBUG_L2CAP */
295 
296 /*
297  * Notify L2CAP channels of a change in encryption state passing additionally
298  * HCI status of performed security procedure.
299  */
300 void bt_l2cap_encrypt_change(struct bt_conn *conn, u8_t hci_status);
301 
302 /* Prepare an L2CAP PDU to be sent over a connection */
303 struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
304 					    size_t reserve,
305 					    k_timeout_t timeout);
306 
307 #define bt_l2cap_create_pdu(_pool, _reserve) \
308 	bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
309 
310 /* Prepare a L2CAP Response PDU to be sent over a connection */
311 struct net_buf *bt_l2cap_create_rsp(struct net_buf *buf, size_t reserve);
312 
313 /* Send L2CAP PDU over a connection
314  *
315  * Buffer ownership is transferred to stack so either in case of success
316  * or error the buffer will be unref internally.
317  *
318  * Calling this from RX thread is assumed to never fail so the return can be
319  * ignored.
320  */
321 int bt_l2cap_send_cb(struct bt_conn *conn, u16_t cid, struct net_buf *buf,
322 		     bt_conn_tx_cb_t cb, void *user_data);
323 
bt_l2cap_send(struct bt_conn * conn,u16_t cid,struct net_buf * buf)324 static inline void bt_l2cap_send(struct bt_conn *conn, u16_t cid,
325 				 struct net_buf *buf)
326 {
327 	bt_l2cap_send_cb(conn, cid, buf, NULL, NULL);
328 }
329 
330 /* Receive a new L2CAP PDU from a connection */
331 void bt_l2cap_recv(struct bt_conn *conn, struct net_buf *buf);
332 
333 /* Perform connection parameter update request */
334 int bt_l2cap_update_conn_param(struct bt_conn *conn,
335 			       const struct bt_le_conn_param *param);
336 
337 /* Initialize L2CAP and supported channels */
338 void bt_l2cap_init(void);
339 
340 /* Lookup channel by Transmission CID */
341 struct bt_l2cap_chan *bt_l2cap_le_lookup_tx_cid(struct bt_conn *conn,
342 						u16_t cid);
343 
344 /* Lookup channel by Receiver CID */
345 struct bt_l2cap_chan *bt_l2cap_le_lookup_rx_cid(struct bt_conn *conn,
346 						u16_t cid);
347 
348 /* Initialize BR/EDR L2CAP signal layer */
349 void bt_l2cap_br_init(void);
350 
351 /* Register fixed channel */
352 void bt_l2cap_br_fixed_chan_register(struct bt_l2cap_fixed_chan *chan);
353 
354 /* Notify BR/EDR L2CAP channels about established new ACL connection */
355 void bt_l2cap_br_connected(struct bt_conn *conn);
356 
357 /* Lookup BR/EDR L2CAP channel by Receiver CID */
358 struct bt_l2cap_chan *bt_l2cap_br_lookup_rx_cid(struct bt_conn *conn,
359 						u16_t cid);
360 
361 /* Disconnects dynamic channel */
362 int bt_l2cap_br_chan_disconnect(struct bt_l2cap_chan *chan);
363 
364 /* Make connection to peer psm server */
365 int bt_l2cap_br_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
366 			     u16_t psm);
367 
368 /* Send packet data to connected peer */
369 int bt_l2cap_br_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
370 
371 /*
372  * Handle security level changed on link passing HCI status of performed
373  * security procedure.
374  */
375 void l2cap_br_encrypt_change(struct bt_conn *conn, u8_t hci_status);
376 
377 /* Handle received data */
378 void bt_l2cap_br_recv(struct bt_conn *conn, struct net_buf *buf);
379