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