OSDN Git Service

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