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