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 uint16_t len;
28 uint16_t cid;
29 } __packed;
30
31 struct bt_l2cap_sig_hdr {
32 uint8_t code;
33 uint8_t ident;
34 uint16_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 uint16_t reason;
44 uint8_t data[0];
45 } __packed;
46
47 struct bt_l2cap_cmd_reject_cid_data {
48 uint16_t scid;
49 uint16_t dcid;
50 } __packed;
51
52 #define BT_L2CAP_CONN_REQ 0x02
53 struct bt_l2cap_conn_req {
54 uint16_t psm;
55 uint16_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 uint16_t dcid;
74 uint16_t scid;
75 uint16_t result;
76 uint16_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 uint16_t dcid;
86 uint16_t flags;
87 uint8_t data[0];
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[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 uint8_t type;
106 uint8_t len;
107 uint8_t data[0];
108 } __packed;
109
110 #define BT_L2CAP_DISCONN_REQ 0x06
111 struct bt_l2cap_disconn_req {
112 uint16_t dcid;
113 uint16_t scid;
114 } __packed;
115
116 #define BT_L2CAP_DISCONN_RSP 0x07
117 struct bt_l2cap_disconn_rsp {
118 uint16_t dcid;
119 uint16_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 uint16_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 uint16_t type;
137 uint16_t result;
138 uint8_t data[0];
139 } __packed;
140
141 #define BT_L2CAP_CONN_PARAM_REQ 0x12
142 struct bt_l2cap_conn_param_req {
143 uint16_t min_interval;
144 uint16_t max_interval;
145 uint16_t latency;
146 uint16_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 uint16_t result;
155 } __packed;
156
157 #define BT_L2CAP_LE_CONN_REQ 0x14
158 struct bt_l2cap_le_conn_req {
159 uint16_t psm;
160 uint16_t scid;
161 uint16_t mtu;
162 uint16_t mps;
163 uint16_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 uint16_t dcid;
182 uint16_t mtu;
183 uint16_t mps;
184 uint16_t credits;
185 uint16_t result;
186 } __packed;
187
188 #define BT_L2CAP_LE_CREDITS 0x16
189 struct bt_l2cap_le_credits {
190 uint16_t cid;
191 uint16_t credits;
192 } __packed;
193
194 #define BT_L2CAP_ECRED_CONN_REQ 0x17
195 struct bt_l2cap_ecred_conn_req {
196 uint16_t psm;
197 uint16_t mtu;
198 uint16_t mps;
199 uint16_t credits;
200 uint16_t scid[0];
201 } __packed;
202
203 #define BT_L2CAP_ECRED_CONN_RSP 0x18
204 struct bt_l2cap_ecred_conn_rsp {
205 uint16_t mtu;
206 uint16_t mps;
207 uint16_t credits;
208 uint16_t result;
209 uint16_t dcid[0];
210 } __packed;
211
212 #define BT_L2CAP_ECRED_RECONF_REQ 0x19
213 struct bt_l2cap_ecred_reconf_req {
214 uint16_t mtu;
215 uint16_t mps;
216 uint16_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 #define BT_L2CAP_RECONF_INVALID_CID 0x0003
223 #define BT_L2CAP_RECONF_OTHER_UNACCEPT 0x0004
224
225 #define BT_L2CAP_ECRED_RECONF_RSP 0x1a
226 struct bt_l2cap_ecred_reconf_rsp {
227 uint16_t result;
228 } __packed;
229
230 struct bt_l2cap_fixed_chan {
231 uint16_t cid;
232 int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
233 bt_l2cap_chan_destroy_t destroy;
234 };
235
236 #define BT_L2CAP_CHANNEL_DEFINE(_name, _cid, _accept, _destroy) \
237 const STRUCT_SECTION_ITERABLE(bt_l2cap_fixed_chan, _name) = { \
238 .cid = _cid, \
239 .accept = _accept, \
240 .destroy = _destroy, \
241 }
242
243 /* Need a name different than bt_l2cap_fixed_chan for a different section */
244 struct bt_l2cap_br_fixed_chan {
245 uint16_t cid;
246 int (*accept)(struct bt_conn *conn, struct bt_l2cap_chan **chan);
247 };
248
249 #define BT_L2CAP_BR_CHANNEL_DEFINE(_name, _cid, _accept) \
250 const STRUCT_SECTION_ITERABLE(bt_l2cap_br_fixed_chan, _name) = { \
251 .cid = _cid, \
252 .accept = _accept, \
253 }
254
255 /* Notify L2CAP channels of a new connection */
256 void bt_l2cap_connected(struct bt_conn *conn);
257
258 /* Notify L2CAP channels of a disconnect event */
259 void bt_l2cap_disconnected(struct bt_conn *conn);
260
261 /* Add channel to the connection */
262 void bt_l2cap_chan_add(struct bt_conn *conn, struct bt_l2cap_chan *chan,
263 bt_l2cap_chan_destroy_t destroy);
264
265 /* Remove channel from the connection */
266 void bt_l2cap_chan_remove(struct bt_conn *conn, struct bt_l2cap_chan *chan);
267
268 /* Delete channel */
269 void bt_l2cap_chan_del(struct bt_l2cap_chan *chan);
270
271 const char *bt_l2cap_chan_state_str(bt_l2cap_chan_state_t state);
272
273 #if defined(CONFIG_BT_DEBUG_L2CAP)
274 void bt_l2cap_chan_set_state_debug(struct bt_l2cap_chan *chan,
275 bt_l2cap_chan_state_t state,
276 const char *func, int line);
277 #define bt_l2cap_chan_set_state(_chan, _state) \
278 bt_l2cap_chan_set_state_debug(_chan, _state, __func__, __LINE__)
279 #else
280 void bt_l2cap_chan_set_state(struct bt_l2cap_chan *chan,
281 bt_l2cap_chan_state_t state);
282 #endif /* CONFIG_BT_DEBUG_L2CAP */
283
284 /*
285 * Notify L2CAP channels of a change in encryption state passing additionally
286 * HCI status of performed security procedure.
287 */
288 void bt_l2cap_security_changed(struct bt_conn *conn, uint8_t hci_status);
289
290 /* Prepare an L2CAP PDU to be sent over a connection */
291 struct net_buf *bt_l2cap_create_pdu_timeout(struct net_buf_pool *pool,
292 size_t reserve,
293 k_timeout_t timeout);
294
295 #define bt_l2cap_create_pdu(_pool, _reserve) \
296 bt_l2cap_create_pdu_timeout(_pool, _reserve, K_FOREVER)
297
298 /* Prepare a L2CAP Response PDU to be sent over a connection */
299 struct net_buf *bt_l2cap_create_rsp(struct net_buf *buf, size_t reserve);
300
301 /* Send L2CAP PDU over a connection
302 *
303 * Buffer ownership is transferred to stack in case of success.
304 */
305 int bt_l2cap_send_cb(struct bt_conn *conn, uint16_t cid, struct net_buf *buf,
306 bt_conn_tx_cb_t cb, void *user_data);
307
bt_l2cap_send(struct bt_conn * conn,uint16_t cid,struct net_buf * buf)308 static inline int bt_l2cap_send(struct bt_conn *conn, uint16_t cid,
309 struct net_buf *buf)
310 {
311 return bt_l2cap_send_cb(conn, cid, buf, NULL, NULL);
312 }
313
314 /* Receive a new L2CAP PDU from a connection */
315 void bt_l2cap_recv(struct bt_conn *conn, struct net_buf *buf, bool complete);
316
317 /* Perform connection parameter update request */
318 int bt_l2cap_update_conn_param(struct bt_conn *conn,
319 const struct bt_le_conn_param *param);
320
321 /* Initialize L2CAP and supported channels */
322 void bt_l2cap_init(void);
323
324 /* Lookup channel by Transmission CID */
325 struct bt_l2cap_chan *bt_l2cap_le_lookup_tx_cid(struct bt_conn *conn,
326 uint16_t cid);
327
328 /* Lookup channel by Receiver CID */
329 struct bt_l2cap_chan *bt_l2cap_le_lookup_rx_cid(struct bt_conn *conn,
330 uint16_t cid);
331
332 /* Initialize BR/EDR L2CAP signal layer */
333 void bt_l2cap_br_init(void);
334
335 /* Register fixed channel */
336 void bt_l2cap_br_fixed_chan_register(struct bt_l2cap_fixed_chan *chan);
337
338 /* Notify BR/EDR L2CAP channels about established new ACL connection */
339 void bt_l2cap_br_connected(struct bt_conn *conn);
340
341 /* Lookup BR/EDR L2CAP channel by Receiver CID */
342 struct bt_l2cap_chan *bt_l2cap_br_lookup_rx_cid(struct bt_conn *conn,
343 uint16_t cid);
344
345 /* Disconnects dynamic channel */
346 int bt_l2cap_br_chan_disconnect(struct bt_l2cap_chan *chan);
347
348 /* Make connection to peer psm server */
349 int bt_l2cap_br_chan_connect(struct bt_conn *conn, struct bt_l2cap_chan *chan,
350 uint16_t psm);
351
352 /* Send packet data to connected peer */
353 int bt_l2cap_br_chan_send(struct bt_l2cap_chan *chan, struct net_buf *buf);
354
355 /*
356 * Handle security level changed on link passing HCI status of performed
357 * security procedure.
358 */
359 void l2cap_br_encrypt_change(struct bt_conn *conn, uint8_t hci_status);
360
361 /* Handle received data */
362 void bt_l2cap_br_recv(struct bt_conn *conn, struct net_buf *buf);
363