OSDN Git Service

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