OSDN Git Service

9087e52c1a9bcb025794ffc352a9d6bad0f6959d
[android-x86/system-bt.git] / stack / btm / btm_dev.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 functions for the Bluetooth Device Manager
22  *
23  ******************************************************************************/
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <stddef.h>
29
30 #include "bt_types.h"
31 #include "device/include/controller.h"
32 #include "bt_common.h"
33 #include "hcimsgs.h"
34 #include "btu.h"
35 #include "btm_api.h"
36 #include "btm_int.h"
37 #include "hcidefs.h"
38 #include "l2c_api.h"
39
40 /*******************************************************************************
41 **
42 ** Function         BTM_SecAddDevice
43 **
44 ** Description      Add/modify device.  This function will be normally called
45 **                  during host startup to restore all required information
46 **                  stored in the NVRAM.
47 **
48 ** Parameters:      bd_addr          - BD address of the peer
49 **                  dev_class        - Device Class
50 **                  bd_name          - Name of the peer device.  NULL if unknown.
51 **                  features         - Remote device's features (up to 3 pages). NULL if not known
52 **                  trusted_mask     - Bitwise OR of services that do not
53 **                                     require authorization. (array of UINT32)
54 **                  link_key         - Connection link key. NULL if unknown.
55 **
56 ** Returns          TRUE if added OK, else FALSE
57 **
58 *******************************************************************************/
59 BOOLEAN BTM_SecAddDevice (BD_ADDR bd_addr, DEV_CLASS dev_class, BD_NAME bd_name,
60                           UINT8 *features, UINT32 trusted_mask[],
61                           LINK_KEY link_key, UINT8 key_type, tBTM_IO_CAP io_cap,
62                           UINT8 pin_length)
63 {
64     BTM_TRACE_API("%s: link key type:%x", __func__, key_type);
65
66     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev (bd_addr);
67     if (!p_dev_rec)
68     {
69         p_dev_rec = btm_sec_allocate_dev_rec();
70
71         memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
72         p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
73
74 #if BLE_INCLUDED == TRUE
75         /* use default value for background connection params */
76         /* update conn params, use default value for background connection params */
77         memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
78 #endif
79     } else {
80         /* "Bump" timestamp for existing record */
81         p_dev_rec->timestamp = btm_cb.dev_rec_count++;
82
83         /* TODO(eisenbach):
84          * Small refactor, but leaving original logic for now.
85          * On the surface, this does not make any sense at all. Why change the
86          * bond state for an existing device here? This logic should be verified
87          * as part of a larger refactor.
88          */
89         p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
90     }
91
92     if (dev_class)
93         memcpy (p_dev_rec->dev_class, dev_class, DEV_CLASS_LEN);
94
95     memset(p_dev_rec->sec_bd_name, 0, sizeof(tBTM_BD_NAME));
96
97     if (bd_name && bd_name[0])
98     {
99         p_dev_rec->sec_flags |= BTM_SEC_NAME_KNOWN;
100         strlcpy ((char *)p_dev_rec->sec_bd_name,
101                  (char *)bd_name, BTM_MAX_REM_BD_NAME_LEN);
102     }
103
104     p_dev_rec->num_read_pages = 0;
105     if (features)
106     {
107         BOOLEAN found = FALSE;
108         memcpy (p_dev_rec->features, features, sizeof (p_dev_rec->features));
109         for (int i = HCI_EXT_FEATURES_PAGE_MAX; !found && i >= 0; i--)
110         {
111             for (int j = 0; j < HCI_FEATURE_BYTES_PER_PAGE; j++)
112             {
113                 if (p_dev_rec->features[i][j] != 0)
114                 {
115                     found = TRUE;
116                     p_dev_rec->num_read_pages = i + 1;
117                     break;
118                 }
119             }
120         }
121     } else {
122         memset (p_dev_rec->features, 0, sizeof (p_dev_rec->features));
123     }
124
125     BTM_SEC_COPY_TRUSTED_DEVICE(trusted_mask, p_dev_rec->trusted_mask);
126
127     if (link_key)
128     {
129         BTM_TRACE_EVENT ("%s: BDA: %02x:%02x:%02x:%02x:%02x:%02x", __func__,
130                           bd_addr[0], bd_addr[1], bd_addr[2],
131                           bd_addr[3], bd_addr[4], bd_addr[5]);
132         p_dev_rec->sec_flags |= BTM_SEC_LINK_KEY_KNOWN;
133         memcpy (p_dev_rec->link_key, link_key, LINK_KEY_LEN);
134         p_dev_rec->link_key_type = key_type;
135         p_dev_rec->pin_code_length = pin_length;
136
137         if (pin_length >= 16 ||
138             key_type == BTM_LKEY_TYPE_AUTH_COMB ||
139             key_type == BTM_LKEY_TYPE_AUTH_COMB_P_256) {
140             // Set the flag if the link key was made by using either a 16 digit
141             // pin or MITM.
142             p_dev_rec->sec_flags |= BTM_SEC_16_DIGIT_PIN_AUTHED | BTM_SEC_LINK_KEY_AUTHED;
143         }
144     }
145
146 #if defined(BTIF_MIXED_MODE_INCLUDED) && (BTIF_MIXED_MODE_INCLUDED == TRUE)
147     if (key_type  < BTM_MAX_PRE_SM4_LKEY_TYPE)
148         p_dev_rec->sm4 = BTM_SM4_KNOWN;
149     else
150         p_dev_rec->sm4 = BTM_SM4_TRUE;
151 #endif
152
153     p_dev_rec->rmt_io_caps = io_cap;
154     p_dev_rec->device_type |= BT_DEVICE_TYPE_BREDR;
155
156     return(TRUE);
157 }
158
159
160 /*******************************************************************************
161 **
162 ** Function         BTM_SecDeleteDevice
163 **
164 ** Description      Free resources associated with the device.
165 **
166 ** Parameters:      bd_addr          - BD address of the peer
167 **
168 ** Returns          TRUE if removed OK, FALSE if not found or ACL link is active
169 **
170 *******************************************************************************/
171 BOOLEAN BTM_SecDeleteDevice (BD_ADDR bd_addr)
172 {
173     if (BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_LE) ||
174         BTM_IsAclConnectionUp(bd_addr, BT_TRANSPORT_BR_EDR))
175     {
176         BTM_TRACE_WARNING("%s FAILED: Cannot Delete when connection is active", __func__);
177         return FALSE;
178     }
179
180     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
181     if (p_dev_rec != NULL)
182     {
183         btm_sec_free_dev(p_dev_rec);
184         /* Tell controller to get rid of the link key, if it has one stored */
185         BTM_DeleteStoredLinkKey (p_dev_rec->bd_addr, NULL);
186     }
187
188     return TRUE;
189 }
190
191 /*******************************************************************************
192 **
193 ** Function         BTM_SecReadDevName
194 **
195 ** Description      Looks for the device name in the security database for the
196 **                  specified BD address.
197 **
198 ** Returns          Pointer to the name or NULL
199 **
200 *******************************************************************************/
201 char *BTM_SecReadDevName (BD_ADDR bd_addr)
202 {
203     char *p_name = NULL;
204     tBTM_SEC_DEV_REC *p_srec;
205
206     if ((p_srec = btm_find_dev(bd_addr)) != NULL)
207         p_name = (char *)p_srec->sec_bd_name;
208
209     return(p_name);
210 }
211
212 bool is_bd_addr_equal(void *data, void *context)
213 {
214     tBTM_SEC_DEV_REC *p_dev_rec = data;
215     BD_ADDR *bd_addr = context;
216
217     if (!memcmp(p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN))
218         return false;
219
220     return true;
221 }
222
223 /*******************************************************************************
224 **
225 ** Function         btm_sec_alloc_dev
226 **
227 ** Description      Look for the record in the device database for the record
228 **                  with specified address
229 **
230 ** Returns          Pointer to the record or NULL
231 **
232 *******************************************************************************/
233 tBTM_SEC_DEV_REC *btm_sec_alloc_dev (BD_ADDR bd_addr)
234 {
235     tBTM_INQ_INFO    *p_inq_info;
236     BTM_TRACE_EVENT ("btm_sec_alloc_dev");
237
238     tBTM_SEC_DEV_REC *p_dev_rec = btm_sec_allocate_dev_rec();
239
240     /* Check with the BT manager if details about remote device are known */
241     /* outgoing connection */
242     if ((p_inq_info = BTM_InqDbRead(bd_addr)) != NULL)
243     {
244         memcpy (p_dev_rec->dev_class, p_inq_info->results.dev_class, DEV_CLASS_LEN);
245
246 #if BLE_INCLUDED == TRUE
247         p_dev_rec->device_type = p_inq_info->results.device_type;
248         p_dev_rec->ble.ble_addr_type = p_inq_info->results.ble_addr_type;
249 #endif
250     }
251     else if (!memcmp (bd_addr, btm_cb.connecting_bda, BD_ADDR_LEN))
252             memcpy (p_dev_rec->dev_class, btm_cb.connecting_dc, DEV_CLASS_LEN);
253
254 #if BLE_INCLUDED == TRUE
255     /* update conn params, use default value for background connection params */
256     memset(&p_dev_rec->conn_params, 0xff, sizeof(tBTM_LE_CONN_PRAMS));
257 #endif
258
259     memcpy (p_dev_rec->bd_addr, bd_addr, BD_ADDR_LEN);
260
261 #if BLE_INCLUDED == TRUE
262     p_dev_rec->ble_hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_LE);
263 #endif
264     p_dev_rec->hci_handle = BTM_GetHCIConnHandle (bd_addr, BT_TRANSPORT_BR_EDR);
265
266     return(p_dev_rec);
267 }
268
269
270 /*******************************************************************************
271 **
272 ** Function         btm_sec_free_dev
273 **
274 ** Description      Mark device record as not used
275 **
276 *******************************************************************************/
277 void btm_sec_free_dev (tBTM_SEC_DEV_REC *p_dev_rec)
278 {
279 #if BLE_INCLUDED == TRUE
280     /* Clear out any saved BLE keys */
281     btm_sec_clear_ble_keys (p_dev_rec);
282 #endif
283     list_remove(btm_cb.sec_dev_rec, p_dev_rec);
284 }
285
286 /*******************************************************************************
287 **
288 ** Function         btm_dev_support_switch
289 **
290 ** Description      This function is called by the L2CAP to check if remote
291 **                  device supports role switch
292 **
293 ** Parameters:      bd_addr       - Address of the peer device
294 **
295 ** Returns          TRUE if device is known and role switch is supported
296 **
297 *******************************************************************************/
298 BOOLEAN btm_dev_support_switch (BD_ADDR bd_addr)
299 {
300     tBTM_SEC_DEV_REC  *p_dev_rec;
301     UINT8   xx;
302     BOOLEAN feature_empty = TRUE;
303
304 #if BTM_SCO_INCLUDED == TRUE
305     /* Role switch is not allowed if a SCO is up */
306     if (btm_is_sco_active_by_bdaddr(bd_addr))
307         return(FALSE);
308 #endif
309     p_dev_rec = btm_find_dev (bd_addr);
310     if (p_dev_rec && controller_get_interface()->supports_master_slave_role_switch())
311     {
312         if (HCI_SWITCH_SUPPORTED(p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0]))
313         {
314             BTM_TRACE_DEBUG("btm_dev_support_switch return TRUE (feature found)");
315             return (TRUE);
316         }
317
318         /* If the feature field is all zero, we never received them */
319         for (xx = 0 ; xx < BD_FEATURES_LEN ; xx++)
320         {
321             if (p_dev_rec->features[HCI_EXT_FEATURES_PAGE_0][xx] != 0x00)
322             {
323                 feature_empty = FALSE; /* at least one is != 0 */
324                 break;
325             }
326         }
327
328         /* If we don't know peer's capabilities, assume it supports Role-switch */
329         if (feature_empty)
330         {
331             BTM_TRACE_DEBUG("btm_dev_support_switch return TRUE (feature empty)");
332             return (TRUE);
333         }
334     }
335
336     BTM_TRACE_DEBUG("btm_dev_support_switch return FALSE");
337     return(FALSE);
338 }
339
340 bool is_handle_equal(void *data, void *context)
341 {
342     tBTM_SEC_DEV_REC *p_dev_rec = data;
343     UINT16 *handle = context;
344
345     if (p_dev_rec->hci_handle == *handle
346 #if BLE_INCLUDED == TRUE
347      || p_dev_rec->ble_hci_handle == *handle
348 #endif
349      )
350         return false;
351
352     return true;
353 }
354
355 /*******************************************************************************
356 **
357 ** Function         btm_find_dev_by_handle
358 **
359 ** Description      Look for the record in the device database for the record
360 **                  with specified handle
361 **
362 ** Returns          Pointer to the record or NULL
363 **
364 *******************************************************************************/
365 tBTM_SEC_DEV_REC *btm_find_dev_by_handle (UINT16 handle)
366 {
367     list_node_t *n = list_foreach(btm_cb.sec_dev_rec, is_handle_equal, &handle);
368     if (n)
369         return list_node(n);
370
371     return NULL;
372 }
373
374 bool is_address_equal(void *data, void *context)
375 {
376     tBTM_SEC_DEV_REC *p_dev_rec = data;
377     BD_ADDR *bd_addr = context;
378
379     if (!memcmp (p_dev_rec->bd_addr, *bd_addr, BD_ADDR_LEN))
380         return false;
381 #if BLE_INCLUDED == TRUE
382     // If a LE random address is looking for device record
383     if (!memcmp(p_dev_rec->ble.pseudo_addr, *bd_addr, BD_ADDR_LEN))
384         return false;
385
386     if (btm_ble_addr_resolvable(*bd_addr, p_dev_rec))
387         return false;
388 #endif
389     return true;
390 }
391
392 /*******************************************************************************
393 **
394 ** Function         btm_find_dev
395 **
396 ** Description      Look for the record in the device database for the record
397 **                  with specified BD address
398 **
399 ** Returns          Pointer to the record or NULL
400 **
401 *******************************************************************************/
402 tBTM_SEC_DEV_REC *btm_find_dev(BD_ADDR bd_addr)
403 {
404     if (!bd_addr)
405         return NULL;
406
407     list_node_t *n = list_foreach(btm_cb.sec_dev_rec, is_address_equal, bd_addr);
408     if (n)
409         return list_node(n);
410
411     return NULL;
412 }
413
414 /*******************************************************************************
415 **
416 ** Function         btm_consolidate_dev
417 5**
418 ** Description      combine security records if identified as same peer
419 **
420 ** Returns          none
421 **
422 *******************************************************************************/
423 void btm_consolidate_dev(tBTM_SEC_DEV_REC *p_target_rec)
424 {
425 #if BLE_INCLUDED == TRUE
426     tBTM_SEC_DEV_REC temp_rec = *p_target_rec;
427
428     BTM_TRACE_DEBUG("%s", __func__);
429
430     list_node_t *end = list_end(btm_cb.sec_dev_rec);
431     for (list_node_t *node = list_begin(btm_cb.sec_dev_rec); node != end; node = list_next(node)) {
432         tBTM_SEC_DEV_REC *p_dev_rec = list_node(node);
433
434         if (p_target_rec == p_dev_rec)
435             continue;
436
437         if (!memcmp (p_dev_rec->bd_addr, p_target_rec->bd_addr, BD_ADDR_LEN))
438         {
439             memcpy(p_target_rec, p_dev_rec, sizeof(tBTM_SEC_DEV_REC));
440             p_target_rec->ble = temp_rec.ble;
441             p_target_rec->ble_hci_handle = temp_rec.ble_hci_handle;
442             p_target_rec->enc_key_size = temp_rec.enc_key_size;
443             p_target_rec->conn_params = temp_rec.conn_params;
444             p_target_rec->device_type |= temp_rec.device_type;
445             p_target_rec->sec_flags |= temp_rec.sec_flags;
446
447             p_target_rec->new_encryption_key_is_p256 = temp_rec.new_encryption_key_is_p256;
448             p_target_rec->no_smp_on_br = temp_rec.no_smp_on_br;
449             p_target_rec->bond_type = temp_rec.bond_type;
450
451             /* remove the combined record */
452             list_remove(btm_cb.sec_dev_rec, p_dev_rec);
453             break;
454         }
455
456         /* an RPA device entry is a duplicate of the target record */
457         if (btm_ble_addr_resolvable(p_dev_rec->bd_addr, p_target_rec))
458         {
459             if (memcmp(p_target_rec->ble.pseudo_addr, p_dev_rec->bd_addr, BD_ADDR_LEN) == 0)
460             {
461                 p_target_rec->ble.ble_addr_type = p_dev_rec->ble.ble_addr_type;
462                 p_target_rec->device_type |= p_dev_rec->device_type;
463
464                 /* remove the combined record */
465                 list_remove(btm_cb.sec_dev_rec, p_dev_rec);
466             }
467             break;
468         }
469     }
470 #endif
471 }
472
473 /*******************************************************************************
474 **
475 ** Function         btm_find_or_alloc_dev
476 **
477 ** Description      Look for the record in the device database for the record
478 **                  with specified BD address
479 **
480 ** Returns          Pointer to the record or NULL
481 **
482 *******************************************************************************/
483 tBTM_SEC_DEV_REC *btm_find_or_alloc_dev (BD_ADDR bd_addr)
484 {
485     tBTM_SEC_DEV_REC *p_dev_rec;
486     BTM_TRACE_EVENT ("btm_find_or_alloc_dev");
487     if ((p_dev_rec = btm_find_dev (bd_addr)) == NULL)
488     {
489
490         /* Allocate a new device record or reuse the oldest one */
491         p_dev_rec = btm_sec_alloc_dev (bd_addr);
492     }
493     return(p_dev_rec);
494 }
495
496 /*******************************************************************************
497 **
498 ** Function         btm_find_oldest_dev_rec
499 **
500 ** Description      Locates the oldest device in use. It first looks for
501 **                  the oldest non-paired device.  If all devices are paired it
502 **                  returns the oldest paired device.
503 **
504 ** Returns          Pointer to the record or NULL
505 **
506 *******************************************************************************/
507 static tBTM_SEC_DEV_REC* btm_find_oldest_dev_rec (void)
508 {
509     tBTM_SEC_DEV_REC *p_oldest = NULL;
510     UINT32       ts_oldest = 0xFFFFFFFF;
511     tBTM_SEC_DEV_REC *p_oldest_paired = NULL;
512     UINT32       ts_oldest_paired = 0xFFFFFFFF;
513
514     list_node_t *end = list_end(btm_cb.sec_dev_rec);
515     for (list_node_t *node = list_begin(btm_cb.sec_dev_rec); node != end; node = list_next(node)) {
516         tBTM_SEC_DEV_REC *p_dev_rec = list_node(node);
517
518         if ((p_dev_rec->sec_flags & (BTM_SEC_LINK_KEY_KNOWN | BTM_SEC_LE_LINK_KEY_KNOWN)) == 0) {
519             // Device is not paired
520             if (p_dev_rec->timestamp < ts_oldest) {
521                 p_oldest = p_dev_rec;
522                 ts_oldest = p_dev_rec->timestamp;
523             }
524         } else {
525             // Paired device
526             if (p_dev_rec->timestamp < ts_oldest_paired) {
527                 p_oldest_paired = p_dev_rec;
528                 ts_oldest_paired = p_dev_rec->timestamp;
529             }
530         }
531     }
532
533     // If we did not find any non-paired devices, use the oldest paired one...
534     if (ts_oldest == 0xFFFFFFFF)
535         p_oldest = p_oldest_paired;
536
537     return p_oldest;
538 }
539
540 /*******************************************************************************
541 **
542 ** Function         btm_sec_allocate_dev_rec
543 **
544 ** Description      Attempts to allocate a new device record. If we have
545 **                  exceeded the maximum number of allowable records to
546 **                  allocate, the oldest record will be deleted to make room
547 **                  for the new record.
548 **
549 ** Returns          Pointer to the newly allocated record
550 **
551 *******************************************************************************/
552 tBTM_SEC_DEV_REC* btm_sec_allocate_dev_rec(void)
553 {
554     tBTM_SEC_DEV_REC *p_dev_rec = NULL;
555
556     if (list_length(btm_cb.sec_dev_rec) > BTM_SEC_MAX_DEVICE_RECORDS)
557     {
558         p_dev_rec = btm_find_oldest_dev_rec();
559         list_remove(btm_cb.sec_dev_rec, p_dev_rec);
560     }
561
562     p_dev_rec = osi_calloc(sizeof(tBTM_SEC_DEV_REC));
563     list_append(btm_cb.sec_dev_rec, p_dev_rec);
564
565     // Initialize defaults
566     p_dev_rec->sec_flags = BTM_SEC_IN_USE;
567     p_dev_rec->bond_type = BOND_TYPE_UNKNOWN;
568     p_dev_rec->timestamp = btm_cb.dev_rec_count++;
569
570     return p_dev_rec;
571 }
572
573 /*******************************************************************************
574 **
575 ** Function         btm_get_bond_type_dev
576 **
577 ** Description      Get the bond type for a device in the device database
578 **                  with specified BD address
579 **
580 ** Returns          The device bond type if known, otherwise BOND_TYPE_UNKNOWN
581 **
582 *******************************************************************************/
583 tBTM_BOND_TYPE btm_get_bond_type_dev(BD_ADDR bd_addr)
584 {
585     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
586
587     if (p_dev_rec == NULL)
588         return BOND_TYPE_UNKNOWN;
589
590     return p_dev_rec->bond_type;
591 }
592
593 /*******************************************************************************
594 **
595 ** Function         btm_set_bond_type_dev
596 **
597 ** Description      Set the bond type for a device in the device database
598 **                  with specified BD address
599 **
600 ** Returns          TRUE on success, otherwise FALSE
601 **
602 *******************************************************************************/
603 BOOLEAN btm_set_bond_type_dev(BD_ADDR bd_addr, tBTM_BOND_TYPE bond_type)
604 {
605     tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(bd_addr);
606
607     if (p_dev_rec == NULL)
608         return FALSE;
609
610     p_dev_rec->bond_type = bond_type;
611     return TRUE;
612 }