1 /** @file
2 * @brief Internal APIs for Bluetooth L2CAP BR/EDR 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 #include "l2cap_br_interface.h"
14
15 #define BT_L2CAP_CID_BR_SIG 0x0001
16 #define BT_L2CAP_CID_CONNLESS 0x0002
17 #define BT_L2CAP_CID_BR_SMP 0x0007
18 #define BT_L2CAP_PSM_RFCOMM 0x0003
19
20 struct bt_l2cap_hdr {
21 uint16_t len;
22 uint16_t cid;
23 } __packed;
24
25 struct bt_l2cap_sig_hdr {
26 uint8_t code;
27 uint8_t ident;
28 uint16_t len;
29 } __packed;
30
31 #define BT_L2CAP_REJ_NOT_UNDERSTOOD 0x0000
32 #define BT_L2CAP_REJ_MTU_EXCEEDED 0x0001
33 #define BT_L2CAP_REJ_INVALID_CID 0x0002
34
35 #define BT_L2CAP_CMD_REJECT 0x01
36 struct bt_l2cap_cmd_reject {
37 uint16_t reason;
38 uint8_t data[];
39 } __packed;
40
41 struct bt_l2cap_cmd_reject_cid_data {
42 uint16_t scid;
43 uint16_t dcid;
44 } __packed;
45
46 #define BT_L2CAP_CONN_REQ 0x02
47 struct bt_l2cap_conn_req {
48 uint16_t psm;
49 uint16_t scid;
50 } __packed;
51
52 /* command statuses in response */
53 #define BT_L2CAP_CS_NO_INFO 0x0000
54 #define BT_L2CAP_CS_AUTHEN_PEND 0x0001
55
56 /* valid results in conn response on BR/EDR */
57 #define BT_L2CAP_BR_SUCCESS 0x0000
58 #define BT_L2CAP_BR_PENDING 0x0001
59 #define BT_L2CAP_BR_ERR_PSM_NOT_SUPP 0x0002
60 #define BT_L2CAP_BR_ERR_SEC_BLOCK 0x0003
61 #define BT_L2CAP_BR_ERR_NO_RESOURCES 0x0004
62 #define BT_L2CAP_BR_ERR_INVALID_SCID 0x0006
63 #define BT_L2CAP_BR_ERR_SCID_IN_USE 0x0007
64
65 #define BT_L2CAP_CONN_RSP 0x03
66 struct bt_l2cap_conn_rsp {
67 uint16_t dcid;
68 uint16_t scid;
69 uint16_t result;
70 uint16_t status;
71 } __packed;
72
73 #define BT_L2CAP_CONF_SUCCESS 0x0000
74 #define BT_L2CAP_CONF_UNACCEPT 0x0001
75 #define BT_L2CAP_CONF_REJECT 0x0002
76 #define BT_L2CAP_CONF_UNKNOWN_OPT 0x0003
77 #define BT_L2CAP_CONF_PENDING 0x0004
78 #define BT_L2CAP_CONF_FLOW_SPEC_REJECT 0x0005
79
80 #define BT_L2CAP_CONF_FLAGS_C BIT(0)
81 #define BT_L2CAP_CONF_FLAGS_MASK BT_L2CAP_CONF_FLAGS_C
82
83 #define BT_L2CAP_CONF_REQ 0x04
84 struct bt_l2cap_conf_req {
85 uint16_t dcid;
86 uint16_t flags;
87 uint8_t data[];
88 } __packed;
89
90 #define BT_L2CAP_CONF_RSP 0x05
91 struct bt_l2cap_conf_rsp {
92 uint16_t scid;
93 uint16_t flags;
94 uint16_t result;
95 uint8_t data[];
96 } __packed;
97
98 /* Option type used by MTU config request data */
99 #define BT_L2CAP_CONF_OPT_MTU 0x01
100 #define BT_L2CAP_CONF_OPT_FLUSH_TIMEOUT 0x02
101 #define BT_L2CAP_CONF_OPT_QOS 0x03
102 #define BT_L2CAP_CONF_OPT_RET_FC 0x04
103 #define BT_L2CAP_CONF_OPT_FCS 0x05
104 #define BT_L2CAP_CONF_OPT_EXT_FLOW_SPEC 0x06
105 #define BT_L2CAP_CONF_OPT_EXT_WIN_SIZE 0x07
106
107 /* Options bits selecting most significant bit (hint) in type field */
108 #define BT_L2CAP_CONF_HINT 0x80
109 #define BT_L2CAP_CONF_MASK 0x7f
110
111 struct bt_l2cap_conf_opt {
112 uint8_t type;
113 uint8_t len;
114 uint8_t data[];
115 } __packed;
116
117 struct bt_l2cap_conf_opt_mtu {
118 uint16_t mtu;
119 } __packed;
120
121 struct bt_l2cap_conf_opt_flush_timeout {
122 uint16_t timeout;
123 } __packed;
124
125 #define BT_L2CAP_QOS_TYPE_NO_TRAFFIC 0x00
126 #define BT_L2CAP_QOS_TYPE_BEST_EFFORT 0x01
127 #define BT_L2CAP_QOS_TYPE_GUARANTEED 0x02
128 struct bt_l2cap_conf_opt_qos {
129 uint8_t flags;
130 uint8_t service_type;
131 uint32_t token_rate;
132 uint32_t token_bucket_size;
133 uint32_t peak_bandwidth;
134 uint32_t latency;
135 uint32_t delay_variation;
136 } __packed;
137
138 struct bt_l2cap_conf_opt_ret_fc {
139 uint8_t mode;
140 uint8_t tx_windows_size;
141 uint8_t max_transmit;
142 uint16_t retransmission_timeout;
143 uint16_t monitor_timeout;
144 uint16_t mps;
145 } __packed;
146
147 #define BT_L2CAP_FCS_TYPE_NO 0x00
148 #define BT_L2CAP_FCS_TYPE_16BIT 0x01
149 struct bt_l2cap_conf_opt_fcs {
150 uint8_t type;
151 } __packed;
152
153 struct bt_l2cap_conf_opt_ext_flow_spec {
154 uint8_t identifier;
155 uint8_t service_type;
156 uint16_t sdu;
157 uint32_t sdu_inter_arrival_time;
158 uint32_t access_latency;
159 uint32_t flush_timeout;
160 } __packed;
161
162 struct bt_l2cap_conf_opt_ext_win_size {
163 uint16_t max_windows_size;
164 } __packed;
165
166 #define BT_L2CAP_DISCONN_REQ 0x06
167 struct bt_l2cap_disconn_req {
168 uint16_t dcid;
169 uint16_t scid;
170 } __packed;
171
172 #define BT_L2CAP_DISCONN_RSP 0x07
173 struct bt_l2cap_disconn_rsp {
174 uint16_t dcid;
175 uint16_t scid;
176 } __packed;
177
178 #define BT_L2CAP_ECHO_REQ 0x08
179 struct bt_l2cap_echo_req {
180 uint8_t data[0];
181 } __packed;
182
183 #define BT_L2CAP_ECHO_RSP 0x09
184 struct bt_l2cap_echo_rsp {
185 uint8_t data[0];
186 } __packed;
187
188 #define BT_L2CAP_INFO_CONNLESS_MTU 0x0001
189 #define BT_L2CAP_INFO_FEAT_MASK 0x0002
190 #define BT_L2CAP_INFO_FIXED_CHAN 0x0003
191
192 #define BT_L2CAP_INFO_REQ 0x0a
193 struct bt_l2cap_info_req {
194 uint16_t type;
195 } __packed;
196
197 /* info result */
198 #define BT_L2CAP_INFO_SUCCESS 0x0000
199 #define BT_L2CAP_INFO_NOTSUPP 0x0001
200
201 #define BT_L2CAP_INFO_RSP 0x0b
202 struct bt_l2cap_info_rsp {
203 uint16_t type;
204 uint16_t result;
205 uint8_t data[];
206 } __packed;
207
208 /* I Frame Standard Control Field Format definition */
209 #define BT_L2CAP_I_FRAME_STD_CONTROL_GET_TYPE(control) ((control) & 0x01)
210 #define BT_L2CAP_I_FRAME_STD_CONTROL_GET_TX_SEQ(control) (((control) >> 0x01) & 0x3f)
211 #define BT_L2CAP_I_FRAME_STD_CONTROL_GET_R(control) (((control) >> 0x07) & 0x01)
212 #define BT_L2CAP_I_FRAME_STD_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x08) & 0x3f)
213 #define BT_L2CAP_I_FRAME_STD_CONTROL_GET_SAR(control) (((control) >> 0x0e) & 0x03)
214
215 #define BT_L2CAP_I_FRAME_STD_CONTROL_SET(tx_seq, r, req_seq, sar) \
216 ((((tx_seq) & 0x3f) << 0x01) | (((r) & 0x01) << 0x07) | (((req_seq) & 0x3f) << 0x08) | \
217 (((sar) & 0x03) << 0x0e))
218
219 /* I Frame Enhanced Control Field Format definition */
220 #define BT_L2CAP_I_FRAME_ENH_CONTROL_GET_TYPE(control) ((control) & 0x01)
221 #define BT_L2CAP_I_FRAME_ENH_CONTROL_GET_TX_SEQ(control) (((control) >> 0x01) & 0x3f)
222 #define BT_L2CAP_I_FRAME_ENH_CONTROL_GET_F(control) (((control) >> 0x07) & 0x01)
223 #define BT_L2CAP_I_FRAME_ENH_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x08) & 0x3f)
224 #define BT_L2CAP_I_FRAME_ENH_CONTROL_GET_SAR(control) (((control) >> 0x0e) & 0x03)
225
226 #define BT_L2CAP_I_FRAME_ENH_CONTROL_SET(tx_seq, f, req_seq, sar) \
227 ((((tx_seq) & 0x3f) << 0x01) | (((f) & 0x01) << 0x07) | (((req_seq) & 0x3f) << 0x08) | \
228 (((sar) & 0x03) << 0x0e))
229
230 /* I Frame Extended Control Field Format definition */
231 #define BT_L2CAP_I_FRAME_EXT_CONTROL_GET_TYPE(control) ((control) & 0x01)
232 #define BT_L2CAP_I_FRAME_EXT_CONTROL_GET_F(control) (((control) >> 0x01) & 0x01)
233 #define BT_L2CAP_I_FRAME_EXT_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x02) & 0x3fff)
234 #define BT_L2CAP_I_FRAME_EXT_CONTROL_GET_SAR(control) (((control) >> 0x10) & 0x03)
235 #define BT_L2CAP_I_FRAME_EXT_CONTROL_GET_TX_SEQ(control) (((control) >> 0x12) & 0x3fff)
236
237 #define BT_L2CAP_I_FRAME_EXT_CONTROL_SET(f, tx_seq, sar, req_seq) \
238 ((((f) & 0x01) << 0x01) | (((req_seq) & 0x3fff) << 0x02) | (((sar) & 0x03) << 0x10) | \
239 (((tx_seq) & 0x3fff) << 0x12))
240
241 /* S Frame Standard Control Field Format definition */
242 #define BT_L2CAP_S_FRAME_STD_CONTROL_GET_TYPE(control) ((control) & 0x01)
243 #define BT_L2CAP_S_FRAME_STD_CONTROL_GET_S(control) (((control) >> 0x02) & 0x03)
244 #define BT_L2CAP_S_FRAME_STD_CONTROL_GET_R(control) (((control) >> 0x07) & 0x01)
245 #define BT_L2CAP_S_FRAME_STD_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x08) & 0x3f)
246
247 #define BT_L2CAP_S_FRAME_STD_CONTROL_SET(s, r, req_seq) \
248 (((1) & 0x01) | (((s) & 0x03) << 0x02) | (((r) & 0x01) << 0x07) | \
249 (((req_seq) & 0x3f) << 0x08))
250
251 /* S Frame Enhanced Control Field Format definition */
252 #define BT_L2CAP_S_FRAME_ENH_CONTROL_GET_TYPE(control) ((control) & 0x01)
253 #define BT_L2CAP_S_FRAME_ENH_CONTROL_GET_S(control) (((control) >> 0x02) & 0x03)
254 #define BT_L2CAP_S_FRAME_ENH_CONTROL_GET_P(control) (((control) >> 0x04) & 0x01)
255 #define BT_L2CAP_S_FRAME_ENH_CONTROL_GET_F(control) (((control) >> 0x07) & 0x01)
256 #define BT_L2CAP_S_FRAME_ENH_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x08) & 0x3f)
257
258 #define BT_L2CAP_S_FRAME_ENH_CONTROL_SET(s, p, f, req_seq) \
259 (((1) & 0x01) | (((s) & 0x03) << 0x02) | (((p) & 0x01) << 0x04) | (((f) & 0x01) << 0x07) | \
260 (((req_seq) & 0x3f) << 0x08))
261
262 /* S Frame Extended Control Field Format definition */
263 #define BT_L2CAP_S_FRAME_EXT_CONTROL_GET_TYPE(control) ((control) & 0x01)
264 #define BT_L2CAP_S_FRAME_EXT_CONTROL_GET_F(control) (((control) >> 0x01) & 0x01)
265 #define BT_L2CAP_S_FRAME_EXT_CONTROL_GET_REQ_SEQ(control) (((control) >> 0x02) & 0x3fff)
266 #define BT_L2CAP_S_FRAME_EXT_CONTROL_GET_S(control) (((control) >> 0x10) & 0x03)
267 #define BT_L2CAP_S_FRAME_EXT_CONTROL_GET_P(control) (((control) >> 0x12) & 0x01)
268
269 #define BT_L2CAP_S_FRAME_EXT_CONTROL_SET(f, req_seq, s, p) \
270 (((1) & 0x01) | (((f) & 0x01) << 0x01) | (((req_seq) & 0x3fff) << 0x02) | \
271 (((s) & 0x03) << 0x10) | (((p) & 0x01) << 0x12))
272
273 #define BT_L2CAP_CONTROL_TYPE_I 0x00
274 #define BT_L2CAP_CONTROL_TYPE_S 0x01
275
276 #define BT_L2CAP_CONTROL_SEQ_MAX 0x40
277 #define BT_L2CAP_EXT_CONTROL_SEQ_MAX 0x4000
278
279 #define BT_L2CAP_CONTROL_SAR_UNSEG 0x00
280 #define BT_L2CAP_CONTROL_SAR_START 0x01
281 #define BT_L2CAP_CONTROL_SAR_END 0x02
282 #define BT_L2CAP_CONTROL_SAR_CONTI 0x03
283
284 #define BT_L2CAP_CONTROL_S_RR 0x00
285 #define BT_L2CAP_CONTROL_S_REJ 0x01
286 #define BT_L2CAP_CONTROL_S_RNR 0x02
287 #define BT_L2CAP_CONTROL_S_SREJ 0x03
288
289 #define BT_L2CAP_RT_FC_SDU_LEN_SIZE 2
290
291 #define BT_L2CAP_STD_CONTROL_SIZE 2
292 #define BT_L2CAP_ENH_CONTROL_SIZE 2
293 #define BT_L2CAP_EXT_CONTROL_SIZE 4
294
295 #define BT_L2CAP_FCS_SIZE 2
296
297 #if defined(CONFIG_BT_L2CAP_RET_FC)
298 /**
299 *
300 * @brief Helper to calculate L2CAP SDU header size.
301 * Useful for creating buffer pools.
302 *
303 * @param mtu Required BT_L2CAP_*_SDU.
304 *
305 * @return Header size of the L2CAP channel.
306 */
bt_l2cap_br_get_ret_fc_hdr_size(struct bt_l2cap_br_chan * chan)307 static inline size_t bt_l2cap_br_get_ret_fc_hdr_size(struct bt_l2cap_br_chan *chan)
308 {
309 if (chan->tx.mode != BT_L2CAP_BR_LINK_MODE_BASIC) {
310 if (chan->tx.extended_control) {
311 return BT_L2CAP_EXT_CONTROL_SIZE + BT_L2CAP_RT_FC_SDU_LEN_SIZE;
312 } else {
313 return BT_L2CAP_STD_CONTROL_SIZE + BT_L2CAP_RT_FC_SDU_LEN_SIZE;
314 }
315 }
316
317 return 0;
318 }
319
bt_l2cap_br_get_ret_fc_tail_size(struct bt_l2cap_br_chan * chan)320 static inline size_t bt_l2cap_br_get_ret_fc_tail_size(struct bt_l2cap_br_chan *chan)
321 {
322 if (chan->tx.mode != BT_L2CAP_BR_LINK_MODE_BASIC) {
323 if (chan->tx.fcs == BT_L2CAP_BR_FCS_16BIT) {
324 return BT_L2CAP_FCS_SIZE;
325 }
326 }
327
328 return 0;
329 }
330
331 /**
332 *
333 * @brief Helper to calculate L2CAP SDU header size.
334 * Useful for creating buffer pools.
335 *
336 * @param chan the BR channel object point to `struct bt_l2cap_br_chan`.
337 *
338 * @return Header size of the L2CAP channel.
339 */
340 #define BT_L2CAP_RT_FC_SDU_HDR_SIZE(chan) bt_l2cap_br_get_ret_fc_hdr_size(chan)
341
342 /**
343 *
344 * @brief Helper to calculate L2CAP SDU tail size.
345 * Useful for creating buffer pools.
346 *
347 * @param chan the BR channel object point to `struct bt_l2cap_br_chan`.
348 *
349 * @return Header size of the L2CAP channel.
350 */
351 #define BT_L2CAP_RT_FC_SDU_TAIL_SIZE(chan) bt_l2cap_br_get_ret_fc_tail_size(chan)
352
353 /**
354 *
355 * @brief Helper to calculate needed buffer size for L2CAP SDUs.
356 * Useful for creating buffer pools.
357 *
358 * @param chan the BR channel object point to `struct bt_l2cap_br_chan`.
359 * @param mtu Required BT_L2CAP_*_SDU.
360 *
361 * @return Needed buffer size to match the requested L2CAP SDU MTU.
362 */
363 #define BT_L2CAP_RT_FC_SDU_BUF_SIZE(chan, mtu) \
364 (BT_L2CAP_BUF_SIZE(BT_L2CAP_RT_FC_SDU_HDR_SIZE((chan)) + (mtu) + \
365 BT_L2CAP_RT_FC_SDU_TAIL_SIZE((chan))))
366
367 /**
368 *
369 * @brief Helper to calculate needed buffer size for L2CAP SDUs.
370 * Useful for creating buffer pools.
371 *
372 * @param mtu Required BT_L2CAP_*_SDU.
373 *
374 * @return Needed buffer size to match the requested L2CAP SDU MTU.
375 */
376 #define BT_L2CAP_RT_FC_MAX_SDU_BUF_SIZE(mtu) \
377 BT_L2CAP_BUF_SIZE((mtu) + BT_L2CAP_EXT_CONTROL_SIZE + BT_L2CAP_RT_FC_SDU_LEN_SIZE + \
378 BT_L2CAP_FCS_SIZE)
379
380 /**
381 * @brief Headroom needed for outgoing L2CAP PDUs if channel in one of
382 * following mode, including retransmission, flow control, enhance
383 * retransmission, and streaming.
384 *
385 * @param chan the BR channel object point to `struct bt_l2cap_br_chan`.
386 */
387 #define BT_L2CAP_RET_FC_SDU_CHAN_SEND_RESERVE(chan) (BT_L2CAP_RT_FC_SDU_HDR_SIZE(chan))
388 #endif /* CONFIG_BT_L2CAP_RET_FC */
389
390 #define BR_CHAN(_ch) CONTAINER_OF(_ch, struct bt_l2cap_br_chan, chan)
391
392 /* Add channel to the connection */
393 void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
394 bt_l2cap_chan_destroy_t destroy);
395
396 /* Remove channel from the connection */
397 void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
398
399 /* Delete channel */
400 void bt_l2cap_br_chan_del(struct bt_l2cap_chan *chan);
401
402 const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
403
404 #if defined(CONFIG_BT_L2CAP_LOG_LEVEL_DBG)
405 void bt_l2cap_br_chan_set_state_debug(struct bt_l2cap_chan *chan,
406 bt_l2cap_chan_state_t state,
407 const char *func, int line);
408 #define bt_l2cap_br_chan_set_state(_chan, _state) \
409 bt_l2cap_br_chan_set_state_debug(_chan, _state, __func__, __LINE__)
410 #else
411 void bt_l2cap_br_chan_set_state(struct bt_l2cap_chan *chan,
412 bt_l2cap_chan_state_t state);
413 #endif /* CONFIG_BT_L2CAP_LOG_LEVEL_DBG */
414
415 /* Prepare an L2CAP PDU to be sent over a connection */
416 struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
417 size_t reserve,
418 k_timeout_t timeout);
419
420 #define bt_l2cap_create_pdu(_pool, _reserve) \
421 bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
422
423 bt_security_t bt_l2cap_br_get_max_sec_level(void);
424