1 /******************************************************************************
2 *
3 * Copyright 1999-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 the main L2CAP entry points
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bt_l2c_main"
26
27 #include <string.h>
28
29 #include "bt_target.h"
30 #include "hcimsgs.h" // HCID_GET_
31 #include "main/shim/shim.h"
32 #include "osi/include/allocator.h"
33 #include "osi/include/log.h"
34 #include "osi/include/osi.h"
35 #include "stack/include/bt_hdr.h"
36 #include "stack/include/l2c_api.h"
37 #include "stack/include/l2cdefs.h"
38 #include "stack/l2cap/l2c_int.h"
39
40 /******************************************************************************/
41 /* L O C A L F U N C T I O N P R O T O T Y P E S */
42 /******************************************************************************/
43 static void process_l2cap_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len);
44
45 /******************************************************************************/
46 /* G L O B A L L 2 C A P D A T A */
47 /******************************************************************************/
48 tL2C_CB l2cb;
49
50 /*******************************************************************************
51 *
52 * Function l2c_rcv_acl_data
53 *
54 * Description This function is called from the HCI Interface when an ACL
55 * data packet is received.
56 *
57 * Returns void
58 *
59 ******************************************************************************/
l2c_rcv_acl_data(BT_HDR * p_msg)60 void l2c_rcv_acl_data(BT_HDR* p_msg) {
61 uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
62
63 /* Extract the handle */
64 uint16_t handle;
65 STREAM_TO_UINT16(handle, p);
66 uint8_t pkt_type = HCID_GET_EVENT(handle);
67 handle = HCID_GET_HANDLE(handle);
68
69 /* Since the HCI Transport is putting segmented packets back together, we */
70 /* should never get a valid packet with the type set to "continuation" */
71 if (pkt_type == L2CAP_PKT_CONTINUE) {
72 L2CAP_TRACE_WARNING("L2CAP - received packet continuation");
73 osi_free(p_msg);
74 return;
75 }
76
77 uint16_t hci_len;
78 STREAM_TO_UINT16(hci_len, p);
79 if (hci_len < L2CAP_PKT_OVERHEAD || hci_len != p_msg->len - 4) {
80 /* Remote-declared packet size must match HCI_ACL size - ACL header (4) */
81 L2CAP_TRACE_WARNING("L2CAP - got incorrect hci header");
82 osi_free(p_msg);
83 return;
84 }
85
86 uint16_t l2cap_len, rcv_cid;
87 STREAM_TO_UINT16(l2cap_len, p);
88 STREAM_TO_UINT16(rcv_cid, p);
89
90 /* Find the LCB based on the handle */
91 tL2C_LCB* p_lcb = l2cu_find_lcb_by_handle(handle);
92 if (!p_lcb) {
93 /* There is a slight possibility (specifically with USB) that we get an */
94 /* L2CAP connection request before we get the HCI connection complete. */
95 /* So for these types of messages, hold them for up to 2 seconds. */
96 if (l2cap_len == 0) {
97 L2CAP_TRACE_WARNING("received empty L2CAP packet");
98 osi_free(p_msg);
99 return;
100 }
101 uint8_t cmd_code;
102 STREAM_TO_UINT8(cmd_code, p);
103
104 if ((p_msg->layer_specific != 0) || (rcv_cid != L2CAP_SIGNALLING_CID) ||
105 (cmd_code != L2CAP_CMD_INFO_REQ && cmd_code != L2CAP_CMD_CONN_REQ)) {
106 bool qcom_debug_log = (handle == 3804 && ((rcv_cid & 0xff) == 0xff) &&
107 p_msg->layer_specific == 0);
108
109 if (!qcom_debug_log) {
110 L2CAP_TRACE_ERROR(
111 "L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur "
112 "count:%d",
113 handle, p_msg->layer_specific, rcv_cid, cmd_code,
114 list_length(l2cb.rcv_pending_q));
115 }
116
117 osi_free(p_msg);
118 return;
119 }
120
121 L2CAP_TRACE_WARNING(
122 "L2CAP - holding ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur "
123 "count:%d",
124 handle, p_msg->layer_specific, rcv_cid, cmd_code,
125 list_length(l2cb.rcv_pending_q));
126 p_msg->layer_specific = 2;
127 list_append(l2cb.rcv_pending_q, p_msg);
128
129 if (list_length(l2cb.rcv_pending_q) == 1) {
130 alarm_set_on_mloop(l2cb.receive_hold_timer, BT_1SEC_TIMEOUT_MS,
131 l2c_receive_hold_timer_timeout, NULL);
132 }
133 return;
134 }
135
136 /* Update the buffer header */
137 p_msg->offset += 4;
138
139 /* for BLE channel, always notify connection when ACL data received on the
140 * link */
141 if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE &&
142 p_lcb->link_state != LST_DISCONNECTING) {
143 /* only process fixed channel data as channel open indication when link is
144 * not in disconnecting mode */
145 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
146 }
147
148 /* Find the CCB for this CID */
149 tL2C_CCB* p_ccb = NULL;
150 if (rcv_cid >= L2CAP_BASE_APPL_CID) {
151 p_ccb = l2cu_find_ccb_by_cid(p_lcb, rcv_cid);
152 if (!p_ccb) {
153 L2CAP_TRACE_WARNING("L2CAP - unknown CID: 0x%04x", rcv_cid);
154 osi_free(p_msg);
155 return;
156 }
157 }
158
159 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
160 p_msg->offset += L2CAP_PKT_OVERHEAD;
161
162 if (l2cap_len != p_msg->len) {
163 L2CAP_TRACE_WARNING("L2CAP - bad length in pkt. Exp: %d Act: %d",
164 l2cap_len, p_msg->len);
165 osi_free(p_msg);
166 return;
167 }
168
169 /* Send the data through the channel state machine */
170 if (rcv_cid == L2CAP_SIGNALLING_CID) {
171 process_l2cap_cmd(p_lcb, p, l2cap_len);
172 osi_free(p_msg);
173 return;
174 }
175
176 if (rcv_cid == L2CAP_CONNECTIONLESS_CID) {
177 /* process_connectionless_data (p_lcb); */
178 osi_free(p_msg);
179 return;
180 }
181
182 if (rcv_cid == L2CAP_BLE_SIGNALLING_CID) {
183 l2cble_process_sig_cmd(p_lcb, p, l2cap_len);
184 osi_free(p_msg);
185 return;
186 }
187
188 if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) &&
189 (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
190 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb !=
191 NULL)) {
192 /* only process fixed channel data when link is open or wait for data
193 * indication */
194 if (!p_lcb || p_lcb->link_state == LST_DISCONNECTING ||
195 !l2cu_initialize_fixed_ccb(p_lcb, rcv_cid)) {
196 osi_free(p_msg);
197 return;
198 }
199
200 /* If no CCB for this channel, allocate one */
201 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
202 p_ccb->metrics.rx(p_msg->len);
203
204 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
205 l2c_fcr_proc_pdu(p_ccb, p_msg);
206 else
207 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)(
208 rcv_cid, p_lcb->remote_bd_addr, p_msg);
209 return;
210 }
211
212 if (!p_ccb) {
213 osi_free(p_msg);
214 return;
215 }
216
217 if (p_lcb->transport == BT_TRANSPORT_LE) {
218 l2c_lcc_proc_pdu(p_ccb, p_msg);
219
220 /* The remote device has one less credit left */
221 --p_ccb->remote_credit_count;
222
223 /* If the credits left on the remote device are getting low, send some */
224 if (p_ccb->remote_credit_count <= L2CA_LeCreditThreshold()) {
225 uint16_t credits = L2CA_LeCreditDefault() - p_ccb->remote_credit_count;
226 p_ccb->remote_credit_count = L2CA_LeCreditDefault();
227
228 /* Return back credits */
229 l2c_csm_execute(p_ccb, L2CEVT_L2CA_SEND_FLOW_CONTROL_CREDIT, &credits);
230 }
231 } else {
232 /* Basic mode packets go straight to the state machine */
233 if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
234 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_DATA, p_msg);
235 else {
236 /* eRTM or streaming mode, so we need to validate states first */
237 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG))
238 l2c_fcr_proc_pdu(p_ccb, p_msg);
239 else
240 osi_free(p_msg);
241 }
242 }
243 }
244
245 /*******************************************************************************
246 *
247 * Function process_l2cap_cmd
248 *
249 * Description This function is called when a packet is received on the
250 * L2CAP signalling CID
251 *
252 * Returns void
253 *
254 ******************************************************************************/
process_l2cap_cmd(tL2C_LCB * p_lcb,uint8_t * p,uint16_t pkt_len)255 static void process_l2cap_cmd(tL2C_LCB* p_lcb, uint8_t* p, uint16_t pkt_len) {
256 tL2C_CONN_INFO con_info;
257
258 /* if l2cap command received in CID 1 on top of an LE link, ignore this
259 * command */
260 if (p_lcb->transport == BT_TRANSPORT_LE) {
261 LOG_INFO("Dropping data on CID 1 for LE link");
262 return;
263 }
264
265 /* Reject the packet if it exceeds the default Signalling Channel MTU */
266 bool pkt_size_rej = false;
267 if (pkt_len > L2CAP_DEFAULT_MTU) {
268 /* Core Spec requires a single response to the first command found in a
269 * multi-command L2cap packet. If only responses in the packet, then it
270 * will be ignored. Here we simply mark the bad packet and decide which cmd
271 * ID to reject later */
272 pkt_size_rej = true;
273 LOG_WARN("Signaling pkt_len=%d exceeds MTU size %d", pkt_len,
274 L2CAP_DEFAULT_MTU);
275 }
276
277 uint8_t* p_next_cmd = p;
278 uint8_t* p_pkt_end = p + pkt_len;
279
280 tL2CAP_CFG_INFO cfg_info;
281 memset(&cfg_info, 0, sizeof(cfg_info));
282
283 /* An L2CAP packet may contain multiple commands */
284 while (true) {
285 /* Smallest command is 4 bytes */
286 p = p_next_cmd;
287 if (p > (p_pkt_end - 4)) break;
288
289 uint8_t cmd_code, id;
290 uint16_t cmd_len;
291 STREAM_TO_UINT8(cmd_code, p);
292 STREAM_TO_UINT8(id, p);
293 STREAM_TO_UINT16(cmd_len, p);
294
295 if (cmd_len > BT_SMALL_BUFFER_SIZE) {
296 LOG_WARN("Command size %u exceeds limit %d", cmd_len,
297 BT_SMALL_BUFFER_SIZE);
298 l2cu_send_peer_cmd_reject(p_lcb, L2CAP_CMD_REJ_MTU_EXCEEDED, id, 0, 0);
299 return;
300 }
301
302 /* Check command length does not exceed packet length */
303 p_next_cmd = p + cmd_len;
304 if (p_next_cmd > p_pkt_end) {
305 LOG_WARN("cmd_len > pkt_len, pkt_len=%d, cmd_len=%d, code=%d", pkt_len,
306 cmd_len, cmd_code);
307 break;
308 }
309
310 LOG_DEBUG("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
311
312 /* Bad L2CAP packet length, look for cmd to reject */
313 if (pkt_size_rej) {
314 /* If command found rejected it and we're done, otherwise keep looking */
315 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb)) {
316 LOG_WARN("Rejected command %d due to bad packet length", cmd_code);
317 return;
318 } else {
319 LOG_WARN("No need to reject command %d for bad packet len", cmd_code);
320 continue; /* Look for next cmd/response in current packet */
321 }
322 }
323
324 switch (cmd_code) {
325 case L2CAP_CMD_REJECT:
326 uint16_t rej_reason;
327 if (p + 2 > p_next_cmd) {
328 LOG_WARN("Not enough data for L2CAP_CMD_REJECT");
329 return;
330 }
331 STREAM_TO_UINT16(rej_reason, p);
332 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED) {
333 uint16_t rej_mtu;
334 if (p + 2 > p_next_cmd) {
335 LOG_WARN("Not enough data for L2CAP_CMD_REJ_MTU_EXCEEDED");
336 return;
337 }
338 STREAM_TO_UINT16(rej_mtu, p);
339 /* What to do with the MTU reject ? We have negotiated an MTU. For now
340 * we will ignore it and let a higher protocol timeout take care of it
341 */
342 LOG_WARN("MTU rej Handle: %d MTU: %d", p_lcb->Handle(), rej_mtu);
343 }
344 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID) {
345 uint16_t lcid, rcid;
346 if (p + 4 > p_next_cmd) {
347 LOG_WARN("Not enough data for L2CAP_CMD_REJ_INVALID_CID");
348 return;
349 }
350 STREAM_TO_UINT16(rcid, p);
351 STREAM_TO_UINT16(lcid, p);
352
353 LOG_WARN("Rejected due to invalid CID, LCID: 0x%04x RCID: 0x%04x",
354 lcid, rcid);
355
356 /* Remote CID invalid. Treat as a disconnect */
357 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
358 if ((p_ccb != NULL) && (p_ccb->remote_cid == rcid)) {
359 /* Fake link disconnect - no reply is generated */
360 LOG_WARN("Remote CID is invalid, treat as disconnected");
361 l2c_csm_execute(p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
362 }
363 }
364
365 /* SonyEricsson Info request Bug workaround (Continue connection) */
366 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD &&
367 p_lcb->w4_info_rsp) {
368 alarm_cancel(p_lcb->info_resp_timer);
369
370 p_lcb->w4_info_rsp = false;
371 tL2C_CONN_INFO ci;
372 ci.status = HCI_SUCCESS;
373 ci.bd_addr = p_lcb->remote_bd_addr;
374
375 /* For all channels, send the event through their FSMs */
376 for (tL2C_CCB* p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb;
377 p_ccb = p_ccb->p_next_ccb) {
378 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
379 }
380 }
381 break;
382
383 case L2CAP_CMD_CONN_REQ: {
384 uint16_t rcid;
385 if (p + 4 > p_next_cmd) {
386 LOG_WARN("Not enough data for L2CAP_CMD_CONN_REQ");
387 return;
388 }
389 STREAM_TO_UINT16(con_info.psm, p);
390 STREAM_TO_UINT16(rcid, p);
391 tL2C_RCB* p_rcb = l2cu_find_rcb_by_psm(con_info.psm);
392 if (!p_rcb) {
393 LOG_WARN("Rcvd conn req for unknown PSM: %d", con_info.psm);
394 l2cu_reject_connection(p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
395 break;
396 } else {
397 if (!p_rcb->api.pL2CA_ConnectInd_Cb) {
398 LOG_WARN("Rcvd conn req for outgoing-only connection PSM: %d",
399 con_info.psm);
400 l2cu_reject_connection(p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
401 break;
402 }
403 }
404 tL2C_CCB* p_ccb = l2cu_allocate_ccb(p_lcb, 0);
405 if (p_ccb == nullptr) {
406 LOG_ERROR("Unable to allocate CCB");
407 l2cu_reject_connection(p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
408 break;
409 }
410 p_ccb->remote_id = id;
411 p_ccb->p_rcb = p_rcb;
412 p_ccb->remote_cid = rcid;
413 p_ccb->connection_initiator = L2CAP_INITIATOR_REMOTE;
414
415 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
416 break;
417 }
418
419 case L2CAP_CMD_CONN_RSP: {
420 uint16_t lcid;
421 if (p + 8 > p_next_cmd) {
422 LOG_WARN("Not enough data for L2CAP_CMD_CONN_REQ");
423 return;
424 }
425 STREAM_TO_UINT16(con_info.remote_cid, p);
426 STREAM_TO_UINT16(lcid, p);
427 STREAM_TO_UINT16(con_info.l2cap_result, p);
428 STREAM_TO_UINT16(con_info.l2cap_status, p);
429
430 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
431 if (!p_ccb) {
432 LOG_WARN("no CCB for conn rsp, LCID: %d RCID: %d", lcid,
433 con_info.remote_cid);
434 break;
435 }
436 if (p_ccb->local_id != id) {
437 LOG_WARN("con rsp - bad ID. Exp: %d Got: %d", p_ccb->local_id, id);
438 break;
439 }
440
441 if (con_info.l2cap_result == L2CAP_CONN_OK)
442 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
443 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
444 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
445 else
446 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
447
448 break;
449 }
450
451 case L2CAP_CMD_CONFIG_REQ: {
452 uint8_t* p_cfg_end = p + cmd_len;
453 bool cfg_rej = false;
454 uint16_t cfg_rej_len = 0;
455
456 uint16_t lcid;
457 if (p + 4 > p_next_cmd) {
458 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_REQ");
459 return;
460 }
461 STREAM_TO_UINT16(lcid, p);
462 STREAM_TO_UINT16(cfg_info.flags, p);
463
464 uint8_t* p_cfg_start = p;
465
466 cfg_info.flush_to_present = cfg_info.mtu_present =
467 cfg_info.qos_present = cfg_info.fcr_present = cfg_info.fcs_present =
468 false;
469
470 while (p < p_cfg_end) {
471 uint8_t cfg_code, cfg_len;
472 if (p + 2 > p_next_cmd) {
473 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_REQ sub_event");
474 return;
475 }
476 STREAM_TO_UINT8(cfg_code, p);
477 STREAM_TO_UINT8(cfg_len, p);
478
479 switch (cfg_code & 0x7F) {
480 case L2CAP_CFG_TYPE_MTU:
481 cfg_info.mtu_present = true;
482 if (cfg_len != 2) {
483 return;
484 }
485 if (p + cfg_len > p_next_cmd) {
486 return;
487 }
488 STREAM_TO_UINT16(cfg_info.mtu, p);
489 break;
490
491 case L2CAP_CFG_TYPE_FLUSH_TOUT:
492 cfg_info.flush_to_present = true;
493 if (cfg_len != 2) {
494 return;
495 }
496 if (p + cfg_len > p_next_cmd) {
497 return;
498 }
499 STREAM_TO_UINT16(cfg_info.flush_to, p);
500 break;
501
502 case L2CAP_CFG_TYPE_QOS:
503 cfg_info.qos_present = true;
504 if (cfg_len != 2 + 5 * 4) {
505 return;
506 }
507 if (p + cfg_len > p_next_cmd) {
508 return;
509 }
510 STREAM_TO_UINT8(cfg_info.qos.qos_flags, p);
511 STREAM_TO_UINT8(cfg_info.qos.service_type, p);
512 STREAM_TO_UINT32(cfg_info.qos.token_rate, p);
513 STREAM_TO_UINT32(cfg_info.qos.token_bucket_size, p);
514 STREAM_TO_UINT32(cfg_info.qos.peak_bandwidth, p);
515 STREAM_TO_UINT32(cfg_info.qos.latency, p);
516 STREAM_TO_UINT32(cfg_info.qos.delay_variation, p);
517 break;
518
519 case L2CAP_CFG_TYPE_FCR:
520 cfg_info.fcr_present = true;
521 if (cfg_len != 3 + 3 * 2) {
522 return;
523 }
524 if (p + cfg_len > p_next_cmd) {
525 return;
526 }
527 STREAM_TO_UINT8(cfg_info.fcr.mode, p);
528 STREAM_TO_UINT8(cfg_info.fcr.tx_win_sz, p);
529 STREAM_TO_UINT8(cfg_info.fcr.max_transmit, p);
530 STREAM_TO_UINT16(cfg_info.fcr.rtrans_tout, p);
531 STREAM_TO_UINT16(cfg_info.fcr.mon_tout, p);
532 STREAM_TO_UINT16(cfg_info.fcr.mps, p);
533 break;
534
535 case L2CAP_CFG_TYPE_FCS:
536 cfg_info.fcs_present = true;
537 if (cfg_len != 1) {
538 return;
539 }
540 if (p + cfg_len > p_next_cmd) {
541 return;
542 }
543 STREAM_TO_UINT8(cfg_info.fcs, p);
544 break;
545
546 case L2CAP_CFG_TYPE_EXT_FLOW:
547 cfg_info.ext_flow_spec_present = true;
548 if (cfg_len != 2 + 2 + 3 * 4) {
549 return;
550 }
551 if (p + cfg_len > p_next_cmd) {
552 return;
553 }
554 STREAM_TO_UINT8(cfg_info.ext_flow_spec.id, p);
555 STREAM_TO_UINT8(cfg_info.ext_flow_spec.stype, p);
556 STREAM_TO_UINT16(cfg_info.ext_flow_spec.max_sdu_size, p);
557 STREAM_TO_UINT32(cfg_info.ext_flow_spec.sdu_inter_time, p);
558 STREAM_TO_UINT32(cfg_info.ext_flow_spec.access_latency, p);
559 STREAM_TO_UINT32(cfg_info.ext_flow_spec.flush_timeout, p);
560 break;
561
562 default:
563 /* sanity check option length */
564 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len) {
565 if (p + cfg_len > p_next_cmd) return;
566 p += cfg_len;
567 if ((cfg_code & 0x80) == 0) {
568 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
569 cfg_rej = true;
570 }
571 }
572 /* bad length; force loop exit */
573 else {
574 p = p_cfg_end;
575 cfg_rej = true;
576 }
577 break;
578 }
579 }
580
581 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
582 if (p_ccb) {
583 p_ccb->remote_id = id;
584 if (cfg_rej) {
585 l2cu_send_peer_config_rej(
586 p_ccb, p_cfg_start, (uint16_t)(cmd_len - L2CAP_CONFIG_REQ_LEN),
587 cfg_rej_len);
588 } else {
589 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
590 }
591 } else {
592 /* updated spec says send command reject on invalid cid */
593 l2cu_send_peer_cmd_reject(p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
594 }
595 break;
596 }
597
598 case L2CAP_CMD_CONFIG_RSP: {
599 uint8_t* p_cfg_end = p + cmd_len;
600 uint16_t lcid;
601 if (p + 6 > p_next_cmd) {
602 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_RSP");
603 return;
604 }
605 STREAM_TO_UINT16(lcid, p);
606 STREAM_TO_UINT16(cfg_info.flags, p);
607 STREAM_TO_UINT16(cfg_info.result, p);
608
609 cfg_info.flush_to_present = cfg_info.mtu_present =
610 cfg_info.qos_present = cfg_info.fcr_present = cfg_info.fcs_present =
611 false;
612
613 while (p < p_cfg_end) {
614 uint8_t cfg_code, cfg_len;
615 if (p + 2 > p_next_cmd) {
616 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_RSP sub_event");
617 return;
618 }
619 STREAM_TO_UINT8(cfg_code, p);
620 STREAM_TO_UINT8(cfg_len, p);
621
622 switch (cfg_code & 0x7F) {
623 case L2CAP_CFG_TYPE_MTU:
624 cfg_info.mtu_present = true;
625 if (p + 2 > p_next_cmd) {
626 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_MTU");
627 return;
628 }
629 STREAM_TO_UINT16(cfg_info.mtu, p);
630 break;
631
632 case L2CAP_CFG_TYPE_FLUSH_TOUT:
633 cfg_info.flush_to_present = true;
634 if (p + 2 > p_next_cmd) {
635 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FLUSH_TOUT");
636 return;
637 }
638 STREAM_TO_UINT16(cfg_info.flush_to, p);
639 break;
640
641 case L2CAP_CFG_TYPE_QOS:
642 cfg_info.qos_present = true;
643 if (p + 2 + 5 * 4 > p_next_cmd) {
644 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_QOS");
645 return;
646 }
647 STREAM_TO_UINT8(cfg_info.qos.qos_flags, p);
648 STREAM_TO_UINT8(cfg_info.qos.service_type, p);
649 STREAM_TO_UINT32(cfg_info.qos.token_rate, p);
650 STREAM_TO_UINT32(cfg_info.qos.token_bucket_size, p);
651 STREAM_TO_UINT32(cfg_info.qos.peak_bandwidth, p);
652 STREAM_TO_UINT32(cfg_info.qos.latency, p);
653 STREAM_TO_UINT32(cfg_info.qos.delay_variation, p);
654 break;
655
656 case L2CAP_CFG_TYPE_FCR:
657 cfg_info.fcr_present = true;
658 if (p + 3 + 3 * 2 > p_next_cmd) {
659 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FCR");
660 return;
661 }
662 STREAM_TO_UINT8(cfg_info.fcr.mode, p);
663 STREAM_TO_UINT8(cfg_info.fcr.tx_win_sz, p);
664 STREAM_TO_UINT8(cfg_info.fcr.max_transmit, p);
665 STREAM_TO_UINT16(cfg_info.fcr.rtrans_tout, p);
666 STREAM_TO_UINT16(cfg_info.fcr.mon_tout, p);
667 STREAM_TO_UINT16(cfg_info.fcr.mps, p);
668 break;
669
670 case L2CAP_CFG_TYPE_FCS:
671 cfg_info.fcs_present = true;
672 if (p + 1 > p_next_cmd) {
673 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FCS");
674 return;
675 }
676 STREAM_TO_UINT8(cfg_info.fcs, p);
677 break;
678
679 case L2CAP_CFG_TYPE_EXT_FLOW:
680 cfg_info.ext_flow_spec_present = true;
681 if (p + 2 + 2 + 3 * 4 > p_next_cmd) {
682 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_EXT_FLOW");
683 return;
684 }
685 STREAM_TO_UINT8(cfg_info.ext_flow_spec.id, p);
686 STREAM_TO_UINT8(cfg_info.ext_flow_spec.stype, p);
687 STREAM_TO_UINT16(cfg_info.ext_flow_spec.max_sdu_size, p);
688 STREAM_TO_UINT32(cfg_info.ext_flow_spec.sdu_inter_time, p);
689 STREAM_TO_UINT32(cfg_info.ext_flow_spec.access_latency, p);
690 STREAM_TO_UINT32(cfg_info.ext_flow_spec.flush_timeout, p);
691 break;
692 }
693 }
694
695 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
696 if (p_ccb) {
697 if (p_ccb->local_id != id) {
698 LOG_WARN("cfg rsp - bad ID. Exp: %d Got: %d", p_ccb->local_id, id);
699 break;
700 }
701 if (cfg_info.result == L2CAP_CFG_OK) {
702 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
703 } else {
704 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
705 }
706 } else {
707 LOG_WARN("Rcvd cfg rsp for unknown CID: 0x%04x", lcid);
708 }
709 break;
710 }
711
712 case L2CAP_CMD_DISC_REQ: {
713 uint16_t lcid, rcid;
714 if (p + 4 > p_next_cmd) {
715 LOG_WARN("Not enough data for L2CAP_CMD_DISC_REQ");
716 return;
717 }
718 STREAM_TO_UINT16(lcid, p);
719 STREAM_TO_UINT16(rcid, p);
720
721 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
722 if (p_ccb) {
723 if (p_ccb->remote_cid == rcid) {
724 p_ccb->remote_id = id;
725 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
726 }
727 } else
728 l2cu_send_peer_disc_rsp(p_lcb, id, lcid, rcid);
729
730 break;
731 }
732
733 case L2CAP_CMD_DISC_RSP: {
734 uint16_t lcid, rcid;
735 if (p + 4 > p_next_cmd) {
736 LOG_WARN("Not enough data for L2CAP_CMD_DISC_RSP");
737 return;
738 }
739 STREAM_TO_UINT16(rcid, p);
740 STREAM_TO_UINT16(lcid, p);
741
742 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
743 if (p_ccb) {
744 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id)) {
745 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
746 }
747 }
748 break;
749 }
750
751 case L2CAP_CMD_ECHO_REQ:
752 l2cu_send_peer_echo_rsp(p_lcb, id, p, cmd_len);
753 break;
754
755 case L2CAP_CMD_INFO_REQ: {
756 uint16_t info_type;
757 if (p + 2 > p_next_cmd) {
758 LOG_WARN("Not enough data for L2CAP_CMD_INFO_REQ");
759 return;
760 }
761 STREAM_TO_UINT16(info_type, p);
762 l2cu_send_peer_info_rsp(p_lcb, id, info_type);
763 break;
764 }
765
766 case L2CAP_CMD_INFO_RSP:
767 /* Stop the link connect timer if sent before L2CAP connection is up */
768 if (p_lcb->w4_info_rsp) {
769 alarm_cancel(p_lcb->info_resp_timer);
770 p_lcb->w4_info_rsp = false;
771 }
772
773 uint16_t info_type, result;
774 if (p + 4 > p_next_cmd) {
775 LOG_WARN("Not enough data for L2CAP_CMD_INFO_RSP");
776 return;
777 }
778 STREAM_TO_UINT16(info_type, p);
779 STREAM_TO_UINT16(result, p);
780
781 if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE) &&
782 (result == L2CAP_INFO_RESP_RESULT_SUCCESS)) {
783 if (p + 4 > p_next_cmd) {
784 LOG_WARN("Not enough data for L2CAP_CMD_INFO_RSP sub_event");
785 return;
786 }
787 STREAM_TO_UINT32(p_lcb->peer_ext_fea, p);
788
789 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS) {
790 l2cu_send_peer_info_req(p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
791 break;
792 } else {
793 l2cu_process_fixed_chnl_resp(p_lcb);
794 }
795 }
796
797 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
798 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
799 if (p + L2CAP_FIXED_CHNL_ARRAY_SIZE > p_next_cmd) {
800 return;
801 }
802 memcpy(p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
803 }
804
805 l2cu_process_fixed_chnl_resp(p_lcb);
806 }
807 {
808 tL2C_CONN_INFO ci;
809 ci.status = HCI_SUCCESS;
810 ci.bd_addr = p_lcb->remote_bd_addr;
811 for (tL2C_CCB* p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb;
812 p_ccb = p_ccb->p_next_ccb) {
813 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
814 }
815 }
816 break;
817
818 default:
819 LOG_WARN("Bad cmd code: %d", cmd_code);
820 l2cu_send_peer_cmd_reject(p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0,
821 0);
822 return;
823 }
824 }
825 }
826
827 /*******************************************************************************
828 *
829 * Function l2c_process_held_packets
830 *
831 * Description This function processes any L2CAP packets that arrived
832 * before the HCI connection complete arrived. It is a work
833 * around for badly behaved controllers.
834 *
835 * Returns void
836 *
837 ******************************************************************************/
l2c_process_held_packets(bool timed_out)838 void l2c_process_held_packets(bool timed_out) {
839 if (list_is_empty(l2cb.rcv_pending_q)) return;
840
841 if (!timed_out) {
842 alarm_cancel(l2cb.receive_hold_timer);
843 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
844 } else {
845 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
846 }
847
848 for (const list_node_t* node = list_begin(l2cb.rcv_pending_q);
849 node != list_end(l2cb.rcv_pending_q);) {
850 BT_HDR* p_buf = static_cast<BT_HDR*>(list_node(node));
851 node = list_next(node);
852 if (!timed_out || (!p_buf->layer_specific) ||
853 (--p_buf->layer_specific == 0)) {
854 list_remove(l2cb.rcv_pending_q, p_buf);
855 p_buf->layer_specific = 0xFFFF;
856 l2c_rcv_acl_data(p_buf);
857 }
858 }
859
860 /* If anyone still in the queue, restart the timeout */
861 if (!list_is_empty(l2cb.rcv_pending_q)) {
862 alarm_set_on_mloop(l2cb.receive_hold_timer, BT_1SEC_TIMEOUT_MS,
863 l2c_receive_hold_timer_timeout, NULL);
864 }
865 }
866
867 /*******************************************************************************
868 *
869 * Function l2c_init
870 *
871 * Description This function is called once at startup to initialize
872 * all the L2CAP structures
873 *
874 * Returns void
875 *
876 ******************************************************************************/
l2c_init(void)877 void l2c_init(void) {
878 if (bluetooth::shim::is_gd_l2cap_enabled()) {
879 // L2CAP init should be handled by GD stack manager
880 return;
881 }
882
883 int16_t xx;
884
885 memset(&l2cb, 0, sizeof(tL2C_CB));
886
887 /* the LE PSM is increased by 1 before being used */
888 l2cb.le_dyn_psm = LE_DYNAMIC_PSM_START - 1;
889
890 /* Put all the channel control blocks on the free queue */
891 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++) {
892 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
893 }
894
895 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
896 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
897
898 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
899 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
900
901 /* Set the default idle timeout */
902 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
903
904 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
905 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
906 #else
907 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
908 #endif
909
910 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
911 /* Conformance testing needs a dynamic response */
912 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
913 #endif
914
915 /* Number of ACL buffers to use for high priority channel */
916
917 l2cb.l2c_ble_fixed_chnls_mask = L2CAP_FIXED_CHNL_ATT_BIT |
918 L2CAP_FIXED_CHNL_BLE_SIG_BIT |
919 L2CAP_FIXED_CHNL_SMP_BIT;
920
921 l2cb.rcv_pending_q = list_new(NULL);
922 CHECK(l2cb.rcv_pending_q != NULL);
923
924 l2cb.receive_hold_timer = alarm_new("l2c.receive_hold_timer");
925 }
926
l2c_free(void)927 void l2c_free(void) {
928 if (bluetooth::shim::is_gd_l2cap_enabled()) {
929 // L2CAP cleanup should be handled by GD stack manager
930 return;
931 }
932
933 list_free(l2cb.rcv_pending_q);
934 l2cb.rcv_pending_q = NULL;
935 }
936
l2c_receive_hold_timer_timeout(UNUSED_ATTR void * data)937 void l2c_receive_hold_timer_timeout(UNUSED_ATTR void* data) {
938 /* Update the timeouts in the hold queue */
939 l2c_process_held_packets(true);
940 }
941
l2c_ccb_timer_timeout(void * data)942 void l2c_ccb_timer_timeout(void* data) {
943 tL2C_CCB* p_ccb = (tL2C_CCB*)data;
944
945 l2c_csm_execute(p_ccb, L2CEVT_TIMEOUT, NULL);
946 }
947
l2c_fcrb_ack_timer_timeout(void * data)948 void l2c_fcrb_ack_timer_timeout(void* data) {
949 tL2C_CCB* p_ccb = (tL2C_CCB*)data;
950
951 l2c_csm_execute(p_ccb, L2CEVT_ACK_TIMEOUT, NULL);
952 }
953
l2c_lcb_timer_timeout(void * data)954 void l2c_lcb_timer_timeout(void* data) {
955 tL2C_LCB* p_lcb = (tL2C_LCB*)data;
956
957 l2c_link_timeout(p_lcb);
958 }
959
960 /*******************************************************************************
961 *
962 * Function l2c_data_write
963 *
964 * Description API functions call this function to write data.
965 *
966 * Returns L2CAP_DW_SUCCESS, if data accepted, else false
967 * L2CAP_DW_CONGESTED, if data accepted and the channel is
968 * congested
969 * L2CAP_DW_FAILED, if error
970 *
971 ******************************************************************************/
l2c_data_write(uint16_t cid,BT_HDR * p_data,uint16_t flags)972 uint8_t l2c_data_write(uint16_t cid, BT_HDR* p_data, uint16_t flags) {
973 /* Find the channel control block. We don't know the link it is on. */
974 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
975 if (!p_ccb) {
976 L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
977 osi_free(p_data);
978 return (L2CAP_DW_FAILED);
979 }
980
981 /* Sending message bigger than mtu size of peer is a violation of protocol */
982 uint16_t mtu;
983
984 if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
985 mtu = p_ccb->peer_conn_cfg.mtu;
986 else
987 mtu = p_ccb->peer_cfg.mtu;
988
989 if (p_data->len > mtu) {
990 L2CAP_TRACE_WARNING(
991 "L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size: "
992 "len=%u mtu=%u",
993 cid, p_data->len, mtu);
994 osi_free(p_data);
995 return (L2CAP_DW_FAILED);
996 }
997
998 /* channel based, packet based flushable or non-flushable */
999 p_data->layer_specific = flags;
1000
1001 /* If already congested, do not accept any more packets */
1002 if (p_ccb->cong_sent) {
1003 L2CAP_TRACE_ERROR(
1004 "L2CAP - CID: 0x%04x cannot send, already congested "
1005 "xmit_hold_q.count: %u buff_quota: %u",
1006 p_ccb->local_cid, fixed_queue_length(p_ccb->xmit_hold_q),
1007 p_ccb->buff_quota);
1008
1009 osi_free(p_data);
1010 return (L2CAP_DW_FAILED);
1011 }
1012
1013 l2c_csm_execute(p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
1014
1015 if (p_ccb->cong_sent) return (L2CAP_DW_CONGESTED);
1016
1017 return (L2CAP_DW_SUCCESS);
1018 }
1019