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_common.h"
30 #include "bt_target.h"
31 #include "hci/include/btsnoop.h"
32 #include "hcimsgs.h"
33 #include "l2c_api.h"
34 #include "l2c_int.h"
35 #include "l2cdefs.h"
36 #include "main/shim/shim.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.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 <= L2CAP_LE_CREDIT_THRESHOLD) {
225 uint16_t credits = L2CAP_LE_CREDIT_DEFAULT - p_ccb->remote_credit_count;
226 p_ccb->remote_credit_count = L2CAP_LE_CREDIT_DEFAULT;
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 if (p_rcb->psm == BT_PSM_RFCOMM) {
416 btsnoop_get_interface()->add_rfc_l2c_channel(
417 p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid);
418 } else if (p_rcb->log_packets) {
419 btsnoop_get_interface()->allowlist_l2c_channel(
420 p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid);
421 }
422
423 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
424 break;
425 }
426
427 case L2CAP_CMD_CONN_RSP: {
428 uint16_t lcid;
429 if (p + 8 > p_next_cmd) {
430 LOG_WARN("Not enough data for L2CAP_CMD_CONN_REQ");
431 return;
432 }
433 STREAM_TO_UINT16(con_info.remote_cid, p);
434 STREAM_TO_UINT16(lcid, p);
435 STREAM_TO_UINT16(con_info.l2cap_result, p);
436 STREAM_TO_UINT16(con_info.l2cap_status, p);
437
438 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
439 if (!p_ccb) {
440 LOG_WARN("no CCB for conn rsp, LCID: %d RCID: %d", lcid,
441 con_info.remote_cid);
442 break;
443 }
444 if (p_ccb->local_id != id) {
445 LOG_WARN("con rsp - bad ID. Exp: %d Got: %d", p_ccb->local_id, id);
446 break;
447 }
448
449 if (con_info.l2cap_result == L2CAP_CONN_OK)
450 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
451 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
452 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
453 else
454 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
455
456 tL2C_RCB* p_rcb = p_ccb->p_rcb;
457 if (p_rcb->psm == BT_PSM_RFCOMM) {
458 btsnoop_get_interface()->add_rfc_l2c_channel(
459 p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid);
460 } else if (p_rcb->log_packets) {
461 btsnoop_get_interface()->allowlist_l2c_channel(
462 p_lcb->Handle(), p_ccb->local_cid, p_ccb->remote_cid);
463 }
464
465 break;
466 }
467
468 case L2CAP_CMD_CONFIG_REQ: {
469 uint8_t* p_cfg_end = p + cmd_len;
470 bool cfg_rej = false;
471 uint16_t cfg_rej_len = 0;
472
473 uint16_t lcid;
474 if (p + 4 > p_next_cmd) {
475 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_REQ");
476 return;
477 }
478 STREAM_TO_UINT16(lcid, p);
479 STREAM_TO_UINT16(cfg_info.flags, p);
480
481 uint8_t* p_cfg_start = p;
482
483 cfg_info.flush_to_present = cfg_info.mtu_present =
484 cfg_info.qos_present = cfg_info.fcr_present = cfg_info.fcs_present =
485 false;
486
487 while (p < p_cfg_end) {
488 uint8_t cfg_code, cfg_len;
489 if (p + 2 > p_next_cmd) {
490 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_REQ sub_event");
491 return;
492 }
493 STREAM_TO_UINT8(cfg_code, p);
494 STREAM_TO_UINT8(cfg_len, p);
495
496 switch (cfg_code & 0x7F) {
497 case L2CAP_CFG_TYPE_MTU:
498 cfg_info.mtu_present = true;
499 if (cfg_len != 2) {
500 android_errorWriteLog(0x534e4554, "119870451");
501 return;
502 }
503 if (p + cfg_len > p_next_cmd) {
504 android_errorWriteLog(0x534e4554, "74202041");
505 return;
506 }
507 STREAM_TO_UINT16(cfg_info.mtu, p);
508 break;
509
510 case L2CAP_CFG_TYPE_FLUSH_TOUT:
511 cfg_info.flush_to_present = true;
512 if (cfg_len != 2) {
513 android_errorWriteLog(0x534e4554, "119870451");
514 return;
515 }
516 if (p + cfg_len > p_next_cmd) {
517 android_errorWriteLog(0x534e4554, "74202041");
518 return;
519 }
520 STREAM_TO_UINT16(cfg_info.flush_to, p);
521 break;
522
523 case L2CAP_CFG_TYPE_QOS:
524 cfg_info.qos_present = true;
525 if (cfg_len != 2 + 5 * 4) {
526 android_errorWriteLog(0x534e4554, "119870451");
527 return;
528 }
529 if (p + cfg_len > p_next_cmd) {
530 android_errorWriteLog(0x534e4554, "74202041");
531 return;
532 }
533 STREAM_TO_UINT8(cfg_info.qos.qos_flags, p);
534 STREAM_TO_UINT8(cfg_info.qos.service_type, p);
535 STREAM_TO_UINT32(cfg_info.qos.token_rate, p);
536 STREAM_TO_UINT32(cfg_info.qos.token_bucket_size, p);
537 STREAM_TO_UINT32(cfg_info.qos.peak_bandwidth, p);
538 STREAM_TO_UINT32(cfg_info.qos.latency, p);
539 STREAM_TO_UINT32(cfg_info.qos.delay_variation, p);
540 break;
541
542 case L2CAP_CFG_TYPE_FCR:
543 cfg_info.fcr_present = true;
544 if (cfg_len != 3 + 3 * 2) {
545 android_errorWriteLog(0x534e4554, "119870451");
546 return;
547 }
548 if (p + cfg_len > p_next_cmd) {
549 android_errorWriteLog(0x534e4554, "74202041");
550 return;
551 }
552 STREAM_TO_UINT8(cfg_info.fcr.mode, p);
553 STREAM_TO_UINT8(cfg_info.fcr.tx_win_sz, p);
554 STREAM_TO_UINT8(cfg_info.fcr.max_transmit, p);
555 STREAM_TO_UINT16(cfg_info.fcr.rtrans_tout, p);
556 STREAM_TO_UINT16(cfg_info.fcr.mon_tout, p);
557 STREAM_TO_UINT16(cfg_info.fcr.mps, p);
558 break;
559
560 case L2CAP_CFG_TYPE_FCS:
561 cfg_info.fcs_present = true;
562 if (cfg_len != 1) {
563 android_errorWriteLog(0x534e4554, "119870451");
564 return;
565 }
566 if (p + cfg_len > p_next_cmd) {
567 android_errorWriteLog(0x534e4554, "74202041");
568 return;
569 }
570 STREAM_TO_UINT8(cfg_info.fcs, p);
571 break;
572
573 case L2CAP_CFG_TYPE_EXT_FLOW:
574 cfg_info.ext_flow_spec_present = true;
575 if (cfg_len != 2 + 2 + 3 * 4) {
576 android_errorWriteLog(0x534e4554, "119870451");
577 return;
578 }
579 if (p + cfg_len > p_next_cmd) {
580 android_errorWriteLog(0x534e4554, "74202041");
581 return;
582 }
583 STREAM_TO_UINT8(cfg_info.ext_flow_spec.id, p);
584 STREAM_TO_UINT8(cfg_info.ext_flow_spec.stype, p);
585 STREAM_TO_UINT16(cfg_info.ext_flow_spec.max_sdu_size, p);
586 STREAM_TO_UINT32(cfg_info.ext_flow_spec.sdu_inter_time, p);
587 STREAM_TO_UINT32(cfg_info.ext_flow_spec.access_latency, p);
588 STREAM_TO_UINT32(cfg_info.ext_flow_spec.flush_timeout, p);
589 break;
590
591 default:
592 /* sanity check option length */
593 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len) {
594 if (p + cfg_len > p_next_cmd) return;
595 p += cfg_len;
596 if ((cfg_code & 0x80) == 0) {
597 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
598 cfg_rej = true;
599 }
600 }
601 /* bad length; force loop exit */
602 else {
603 p = p_cfg_end;
604 cfg_rej = true;
605 }
606 break;
607 }
608 }
609
610 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
611 if (p_ccb) {
612 p_ccb->remote_id = id;
613 if (cfg_rej) {
614 l2cu_send_peer_config_rej(
615 p_ccb, p_cfg_start, (uint16_t)(cmd_len - L2CAP_CONFIG_REQ_LEN),
616 cfg_rej_len);
617 } else {
618 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
619 }
620 } else {
621 /* updated spec says send command reject on invalid cid */
622 l2cu_send_peer_cmd_reject(p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
623 }
624 break;
625 }
626
627 case L2CAP_CMD_CONFIG_RSP: {
628 uint8_t* p_cfg_end = p + cmd_len;
629 uint16_t lcid;
630 if (p + 6 > p_next_cmd) {
631 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_RSP");
632 return;
633 }
634 STREAM_TO_UINT16(lcid, p);
635 STREAM_TO_UINT16(cfg_info.flags, p);
636 STREAM_TO_UINT16(cfg_info.result, p);
637
638 cfg_info.flush_to_present = cfg_info.mtu_present =
639 cfg_info.qos_present = cfg_info.fcr_present = cfg_info.fcs_present =
640 false;
641
642 while (p < p_cfg_end) {
643 uint8_t cfg_code, cfg_len;
644 if (p + 2 > p_next_cmd) {
645 LOG_WARN("Not enough data for L2CAP_CMD_CONFIG_RSP sub_event");
646 return;
647 }
648 STREAM_TO_UINT8(cfg_code, p);
649 STREAM_TO_UINT8(cfg_len, p);
650
651 switch (cfg_code & 0x7F) {
652 case L2CAP_CFG_TYPE_MTU:
653 cfg_info.mtu_present = true;
654 if (p + 2 > p_next_cmd) {
655 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_MTU");
656 return;
657 }
658 STREAM_TO_UINT16(cfg_info.mtu, p);
659 break;
660
661 case L2CAP_CFG_TYPE_FLUSH_TOUT:
662 cfg_info.flush_to_present = true;
663 if (p + 2 > p_next_cmd) {
664 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FLUSH_TOUT");
665 return;
666 }
667 STREAM_TO_UINT16(cfg_info.flush_to, p);
668 break;
669
670 case L2CAP_CFG_TYPE_QOS:
671 cfg_info.qos_present = true;
672 if (p + 2 + 5 * 4 > p_next_cmd) {
673 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_QOS");
674 return;
675 }
676 STREAM_TO_UINT8(cfg_info.qos.qos_flags, p);
677 STREAM_TO_UINT8(cfg_info.qos.service_type, p);
678 STREAM_TO_UINT32(cfg_info.qos.token_rate, p);
679 STREAM_TO_UINT32(cfg_info.qos.token_bucket_size, p);
680 STREAM_TO_UINT32(cfg_info.qos.peak_bandwidth, p);
681 STREAM_TO_UINT32(cfg_info.qos.latency, p);
682 STREAM_TO_UINT32(cfg_info.qos.delay_variation, p);
683 break;
684
685 case L2CAP_CFG_TYPE_FCR:
686 cfg_info.fcr_present = true;
687 if (p + 3 + 3 * 2 > p_next_cmd) {
688 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FCR");
689 return;
690 }
691 STREAM_TO_UINT8(cfg_info.fcr.mode, p);
692 STREAM_TO_UINT8(cfg_info.fcr.tx_win_sz, p);
693 STREAM_TO_UINT8(cfg_info.fcr.max_transmit, p);
694 STREAM_TO_UINT16(cfg_info.fcr.rtrans_tout, p);
695 STREAM_TO_UINT16(cfg_info.fcr.mon_tout, p);
696 STREAM_TO_UINT16(cfg_info.fcr.mps, p);
697 break;
698
699 case L2CAP_CFG_TYPE_FCS:
700 cfg_info.fcs_present = true;
701 if (p + 1 > p_next_cmd) {
702 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_FCS");
703 return;
704 }
705 STREAM_TO_UINT8(cfg_info.fcs, p);
706 break;
707
708 case L2CAP_CFG_TYPE_EXT_FLOW:
709 cfg_info.ext_flow_spec_present = true;
710 if (p + 2 + 2 + 3 * 4 > p_next_cmd) {
711 LOG_WARN("Not enough data for L2CAP_CFG_TYPE_EXT_FLOW");
712 return;
713 }
714 STREAM_TO_UINT8(cfg_info.ext_flow_spec.id, p);
715 STREAM_TO_UINT8(cfg_info.ext_flow_spec.stype, p);
716 STREAM_TO_UINT16(cfg_info.ext_flow_spec.max_sdu_size, p);
717 STREAM_TO_UINT32(cfg_info.ext_flow_spec.sdu_inter_time, p);
718 STREAM_TO_UINT32(cfg_info.ext_flow_spec.access_latency, p);
719 STREAM_TO_UINT32(cfg_info.ext_flow_spec.flush_timeout, p);
720 break;
721 }
722 }
723
724 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
725 if (p_ccb) {
726 if (p_ccb->local_id != id) {
727 LOG_WARN("cfg rsp - bad ID. Exp: %d Got: %d", p_ccb->local_id, id);
728 break;
729 }
730 if (cfg_info.result == L2CAP_CFG_OK) {
731 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
732 } else {
733 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
734 }
735 } else {
736 LOG_WARN("Rcvd cfg rsp for unknown CID: 0x%04x", lcid);
737 }
738 break;
739 }
740
741 case L2CAP_CMD_DISC_REQ: {
742 uint16_t lcid, rcid;
743 if (p + 4 > p_next_cmd) {
744 LOG_WARN("Not enough data for L2CAP_CMD_DISC_REQ");
745 return;
746 }
747 STREAM_TO_UINT16(lcid, p);
748 STREAM_TO_UINT16(rcid, p);
749
750 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
751 if (p_ccb) {
752 if (p_ccb->remote_cid == rcid) {
753 p_ccb->remote_id = id;
754 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
755 }
756 } else
757 l2cu_send_peer_disc_rsp(p_lcb, id, lcid, rcid);
758
759 break;
760 }
761
762 case L2CAP_CMD_DISC_RSP: {
763 uint16_t lcid, rcid;
764 if (p + 4 > p_next_cmd) {
765 LOG_WARN("Not enough data for L2CAP_CMD_DISC_RSP");
766 return;
767 }
768 STREAM_TO_UINT16(rcid, p);
769 STREAM_TO_UINT16(lcid, p);
770
771 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
772 if (p_ccb) {
773 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id)) {
774 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
775 }
776 }
777 break;
778 }
779
780 case L2CAP_CMD_ECHO_REQ:
781 l2cu_send_peer_echo_rsp(p_lcb, id, p, cmd_len);
782 break;
783
784 case L2CAP_CMD_INFO_REQ: {
785 uint16_t info_type;
786 if (p + 2 > p_next_cmd) {
787 LOG_WARN("Not enough data for L2CAP_CMD_INFO_REQ");
788 return;
789 }
790 STREAM_TO_UINT16(info_type, p);
791 l2cu_send_peer_info_rsp(p_lcb, id, info_type);
792 break;
793 }
794
795 case L2CAP_CMD_INFO_RSP:
796 /* Stop the link connect timer if sent before L2CAP connection is up */
797 if (p_lcb->w4_info_rsp) {
798 alarm_cancel(p_lcb->info_resp_timer);
799 p_lcb->w4_info_rsp = false;
800 }
801
802 uint16_t info_type, result;
803 if (p + 4 > p_next_cmd) {
804 LOG_WARN("Not enough data for L2CAP_CMD_INFO_RSP");
805 return;
806 }
807 STREAM_TO_UINT16(info_type, p);
808 STREAM_TO_UINT16(result, p);
809
810 if ((info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE) &&
811 (result == L2CAP_INFO_RESP_RESULT_SUCCESS)) {
812 if (p + 4 > p_next_cmd) {
813 LOG_WARN("Not enough data for L2CAP_CMD_INFO_RSP sub_event");
814 return;
815 }
816 STREAM_TO_UINT32(p_lcb->peer_ext_fea, p);
817
818 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS) {
819 l2cu_send_peer_info_req(p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
820 break;
821 } else {
822 l2cu_process_fixed_chnl_resp(p_lcb);
823 }
824 }
825
826 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
827 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
828 if (p + L2CAP_FIXED_CHNL_ARRAY_SIZE > p_next_cmd) {
829 android_errorWriteLog(0x534e4554, "111215173");
830 return;
831 }
832 memcpy(p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
833 }
834
835 l2cu_process_fixed_chnl_resp(p_lcb);
836 }
837 {
838 tL2C_CONN_INFO ci;
839 ci.status = HCI_SUCCESS;
840 ci.bd_addr = p_lcb->remote_bd_addr;
841 for (tL2C_CCB* p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb;
842 p_ccb = p_ccb->p_next_ccb) {
843 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
844 }
845 }
846 break;
847
848 default:
849 LOG_WARN("Bad cmd code: %d", cmd_code);
850 l2cu_send_peer_cmd_reject(p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0,
851 0);
852 return;
853 }
854 }
855 }
856
857 /*******************************************************************************
858 *
859 * Function l2c_process_held_packets
860 *
861 * Description This function processes any L2CAP packets that arrived
862 * before the HCI connection complete arrived. It is a work
863 * around for badly behaved controllers.
864 *
865 * Returns void
866 *
867 ******************************************************************************/
l2c_process_held_packets(bool timed_out)868 void l2c_process_held_packets(bool timed_out) {
869 if (list_is_empty(l2cb.rcv_pending_q)) return;
870
871 if (!timed_out) {
872 alarm_cancel(l2cb.receive_hold_timer);
873 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
874 } else {
875 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
876 }
877
878 for (const list_node_t* node = list_begin(l2cb.rcv_pending_q);
879 node != list_end(l2cb.rcv_pending_q);) {
880 BT_HDR* p_buf = static_cast<BT_HDR*>(list_node(node));
881 node = list_next(node);
882 if (!timed_out || (!p_buf->layer_specific) ||
883 (--p_buf->layer_specific == 0)) {
884 list_remove(l2cb.rcv_pending_q, p_buf);
885 p_buf->layer_specific = 0xFFFF;
886 l2c_rcv_acl_data(p_buf);
887 }
888 }
889
890 /* If anyone still in the queue, restart the timeout */
891 if (!list_is_empty(l2cb.rcv_pending_q)) {
892 alarm_set_on_mloop(l2cb.receive_hold_timer, BT_1SEC_TIMEOUT_MS,
893 l2c_receive_hold_timer_timeout, NULL);
894 }
895 }
896
897 /*******************************************************************************
898 *
899 * Function l2c_init
900 *
901 * Description This function is called once at startup to initialize
902 * all the L2CAP structures
903 *
904 * Returns void
905 *
906 ******************************************************************************/
l2c_init(void)907 void l2c_init(void) {
908 if (bluetooth::shim::is_gd_l2cap_enabled()) {
909 // L2CAP init should be handled by GD stack manager
910 return;
911 }
912
913 int16_t xx;
914
915 memset(&l2cb, 0, sizeof(tL2C_CB));
916
917 /* the LE PSM is increased by 1 before being used */
918 l2cb.le_dyn_psm = LE_DYNAMIC_PSM_START - 1;
919
920 /* Put all the channel control blocks on the free queue */
921 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++) {
922 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
923 }
924
925 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
926 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
927
928 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
929 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
930
931 /* Set the default idle timeout */
932 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
933
934 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
935 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
936 #else
937 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
938 #endif
939
940 #if (L2CAP_CONFORMANCE_TESTING == TRUE)
941 /* Conformance testing needs a dynamic response */
942 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
943 #endif
944
945 /* Number of ACL buffers to use for high priority channel */
946
947 l2cb.l2c_ble_fixed_chnls_mask = L2CAP_FIXED_CHNL_ATT_BIT |
948 L2CAP_FIXED_CHNL_BLE_SIG_BIT |
949 L2CAP_FIXED_CHNL_SMP_BIT;
950
951 l2cb.rcv_pending_q = list_new(NULL);
952 CHECK(l2cb.rcv_pending_q != NULL);
953
954 l2cb.receive_hold_timer = alarm_new("l2c.receive_hold_timer");
955 }
956
l2c_free(void)957 void l2c_free(void) {
958 if (bluetooth::shim::is_gd_l2cap_enabled()) {
959 // L2CAP cleanup should be handled by GD stack manager
960 return;
961 }
962
963 list_free(l2cb.rcv_pending_q);
964 l2cb.rcv_pending_q = NULL;
965 }
966
l2c_receive_hold_timer_timeout(UNUSED_ATTR void * data)967 void l2c_receive_hold_timer_timeout(UNUSED_ATTR void* data) {
968 /* Update the timeouts in the hold queue */
969 l2c_process_held_packets(true);
970 }
971
l2c_ccb_timer_timeout(void * data)972 void l2c_ccb_timer_timeout(void* data) {
973 tL2C_CCB* p_ccb = (tL2C_CCB*)data;
974
975 l2c_csm_execute(p_ccb, L2CEVT_TIMEOUT, NULL);
976 }
977
l2c_fcrb_ack_timer_timeout(void * data)978 void l2c_fcrb_ack_timer_timeout(void* data) {
979 tL2C_CCB* p_ccb = (tL2C_CCB*)data;
980
981 l2c_csm_execute(p_ccb, L2CEVT_ACK_TIMEOUT, NULL);
982 }
983
l2c_lcb_timer_timeout(void * data)984 void l2c_lcb_timer_timeout(void* data) {
985 tL2C_LCB* p_lcb = (tL2C_LCB*)data;
986
987 l2c_link_timeout(p_lcb);
988 }
989
990 /*******************************************************************************
991 *
992 * Function l2c_data_write
993 *
994 * Description API functions call this function to write data.
995 *
996 * Returns L2CAP_DW_SUCCESS, if data accepted, else false
997 * L2CAP_DW_CONGESTED, if data accepted and the channel is
998 * congested
999 * L2CAP_DW_FAILED, if error
1000 *
1001 ******************************************************************************/
l2c_data_write(uint16_t cid,BT_HDR * p_data,uint16_t flags)1002 uint8_t l2c_data_write(uint16_t cid, BT_HDR* p_data, uint16_t flags) {
1003 /* Find the channel control block. We don't know the link it is on. */
1004 tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
1005 if (!p_ccb) {
1006 L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
1007 osi_free(p_data);
1008 return (L2CAP_DW_FAILED);
1009 }
1010
1011 /* Sending message bigger than mtu size of peer is a violation of protocol */
1012 uint16_t mtu;
1013
1014 if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE)
1015 mtu = p_ccb->peer_conn_cfg.mtu;
1016 else
1017 mtu = p_ccb->peer_cfg.mtu;
1018
1019 if (p_data->len > mtu) {
1020 L2CAP_TRACE_WARNING(
1021 "L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size: "
1022 "len=%u mtu=%u",
1023 cid, p_data->len, mtu);
1024 osi_free(p_data);
1025 return (L2CAP_DW_FAILED);
1026 }
1027
1028 /* channel based, packet based flushable or non-flushable */
1029 p_data->layer_specific = flags;
1030
1031 /* If already congested, do not accept any more packets */
1032 if (p_ccb->cong_sent) {
1033 L2CAP_TRACE_ERROR(
1034 "L2CAP - CID: 0x%04x cannot send, already congested "
1035 "xmit_hold_q.count: %u buff_quota: %u",
1036 p_ccb->local_cid, fixed_queue_length(p_ccb->xmit_hold_q),
1037 p_ccb->buff_quota);
1038
1039 osi_free(p_data);
1040 return (L2CAP_DW_FAILED);
1041 }
1042
1043 l2c_csm_execute(p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
1044
1045 if (p_ccb->cong_sent) return (L2CAP_DW_CONGESTED);
1046
1047 return (L2CAP_DW_SUCCESS);
1048 }
1049