Lines Matching +full:restore +full:- +full:keys
2 * Copyright (c) 2017-2024 Nordic Semiconductor ASA
3 * Copyright (c) 2015-2016 Intel Corporation
5 * SPDX-License-Identifier: Apache-2.0
28 #include "keys.h"
56 struct bt_keys *keys; in bt_lookup_id_addr() local
58 keys = bt_keys_find_irk(id, addr); in bt_lookup_id_addr()
59 if (keys) { in bt_lookup_id_addr()
60 LOG_DBG("Identity %s matched RPA %s", bt_addr_le_str(&keys->addr), in bt_lookup_id_addr()
62 return &keys->addr; in bt_lookup_id_addr()
78 if (check_data->id == adv->id) { in adv_id_check_func()
79 check_data->adv_enabled = true; in adv_id_check_func()
82 if (check_data->id == adv->id && in adv_id_check_func()
83 atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { in adv_id_check_func()
84 check_data->adv_enabled = true; in adv_id_check_func()
93 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in adv_is_private_enabled()
94 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) { in adv_is_private_enabled()
104 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in adv_is_limited_enabled()
105 atomic_test_bit(adv->flags, BT_ADV_LIMITED)) { in adv_is_limited_enabled()
112 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { in adv_pause_enabled()
113 atomic_set_bit(adv->flags, BT_ADV_PAUSED); in adv_pause_enabled()
120 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_PAUSED)) { in adv_unpause_enabled()
140 return -ENOBUFS; in set_random_address()
147 if (err == -EACCES) { in set_random_address()
172 return -EINVAL; in bt_id_set_adv_random_addr()
182 if (!atomic_test_bit(adv->flags, BT_ADV_PARAMS_SET)) { in bt_id_set_adv_random_addr()
183 bt_addr_copy(&adv->random_addr.a, addr); in bt_id_set_adv_random_addr()
184 adv->random_addr.type = BT_ADDR_LE_RANDOM; in bt_id_set_adv_random_addr()
185 atomic_set_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING); in bt_id_set_adv_random_addr()
192 return -ENOBUFS; in bt_id_set_adv_random_addr()
197 cp->handle = adv->handle; in bt_id_set_adv_random_addr()
198 bt_addr_copy(&cp->bdaddr, addr); in bt_id_set_adv_random_addr()
206 if (&adv->random_addr.a != addr) { in bt_id_set_adv_random_addr()
207 bt_addr_copy(&adv->random_addr.a, addr); in bt_id_set_adv_random_addr()
209 adv->random_addr.type = BT_ADDR_LE_RANDOM; in bt_id_set_adv_random_addr()
213 /* If rpa sharing is enabled, then rpa expired cb of adv-sets belonging
214 * to same id is verified to return true. If not, adv-sets will continue
222 if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID) && in adv_rpa_expired()
223 adv->cb && adv->cb->rpa_expired) { in adv_rpa_expired()
224 rpa_invalid = adv->cb->rpa_expired(adv); in adv_rpa_expired()
230 if (adv->id >= bt_dev.id_count) { in adv_rpa_expired()
236 rpa_invalid_set_ptr[adv->id] = false; in adv_rpa_expired()
240 atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID); in adv_rpa_expired()
250 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED) && in adv_rpa_invalidate()
251 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) { in adv_rpa_invalidate()
259 if (adv->id >= bt_dev.id_count) { in adv_rpa_clear_data()
264 if (rpa_invalid_set_ptr[adv->id]) { in adv_rpa_clear_data()
265 atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID); in adv_rpa_clear_data()
266 bt_addr_copy(&bt_dev.rpa[adv->id], BT_ADDR_NONE); in adv_rpa_clear_data()
268 LOG_WRN("Adv sets rpa expired cb with id %d returns false\n", adv->id); in adv_rpa_clear_data()
311 err = -ENOBUFS; in le_rpa_timeout_update()
316 cp->rpa_timeout = sys_cpu_to_le16(bt_dev.rpa_timeout); in le_rpa_timeout_update()
347 return -EINVAL; in bt_id_set_private_addr()
381 if (bt_addr_eq(&bt_dev.rpa[adv->id], BT_ADDR_NONE)) { in adv_rpa_get()
382 err = bt_rpa_create(bt_dev.irk[adv->id], &bt_dev.rpa[adv->id]); in adv_rpa_get()
388 bt_addr_copy(rpa, &bt_dev.rpa[adv->id]); in adv_rpa_get()
397 err = bt_rpa_create(bt_dev.irk[adv->id], rpa); in adv_rpa_get()
412 return -EINVAL; in bt_id_set_adv_private_addr()
416 (adv->options & BT_LE_ADV_OPT_USE_NRPA)) { in bt_id_set_adv_private_addr()
422 return -ENOSYS; in bt_id_set_adv_private_addr()
427 return bt_id_set_private_addr(adv->id); in bt_id_set_adv_private_addr()
431 if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID)) { in bt_id_set_adv_private_addr()
435 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) { in bt_id_set_adv_private_addr()
442 if (adv == bt_le_adv_lookup_legacy() && adv->id == BT_ID_DEFAULT) { in bt_id_set_adv_private_addr()
453 atomic_set_bit(adv->flags, BT_ADV_RPA_VALID); in bt_id_set_adv_private_addr()
463 atomic_set_bit(adv->flags, BT_ADV_RPA_VALID); in bt_id_set_adv_private_addr()
467 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) { in bt_id_set_adv_private_addr()
488 return -EINVAL; in bt_id_set_private_addr()
516 return -EINVAL; in bt_id_set_adv_private_addr()
544 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in adv_pause_rpa()
545 !atomic_test_bit(adv->flags, BT_ADV_LIMITED) && in adv_pause_rpa()
546 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) { in adv_pause_rpa()
554 atomic_set_bit(adv->flags, BT_ADV_RPA_UPDATE); in adv_pause_rpa()
579 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RPA_UPDATE)) { in adv_enable_rpa()
625 atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in le_update_private_addr()
626 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) { in le_update_private_addr()
628 id = adv->id; in le_update_private_addr()
725 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) { in bt_id_scan_random_addr_check()
738 if ((atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) && in bt_id_scan_random_addr_check()
739 bt_dev.id_addr[adv->id].type == BT_ADDR_LE_RANDOM) || in bt_id_scan_random_addr_check()
740 adv->id != BT_ID_DEFAULT) { in bt_id_scan_random_addr_check()
783 if (((param->options & BT_LE_ADV_OPT_USE_IDENTITY) && in bt_id_adv_random_addr_check()
784 bt_dev.id_addr[param->id].type == BT_ADDR_LE_RANDOM) || in bt_id_adv_random_addr_check()
785 param->id != BT_ID_DEFAULT) { in bt_id_adv_random_addr_check()
798 if (!(param->options & _BT_LE_ADV_OPT_CONNECTABLE) && in bt_id_adv_random_addr_check()
799 (param->options & BT_LE_ADV_OPT_USE_IDENTITY)) { in bt_id_adv_random_addr_check()
800 /* Attempt to set non-connectable NRPA */ in bt_id_adv_random_addr_check()
802 } else if (bt_dev.id_addr[param->id].type == in bt_id_adv_random_addr_check()
804 param->id != BT_ID_DEFAULT) { in bt_id_adv_random_addr_check()
805 /* Attempt to set connectable, or non-connectable with in bt_id_adv_random_addr_check()
845 return -ENOBUFS; in le_set_privacy_mode()
866 return -ENOBUFS; in addr_res_enable()
881 return -EINVAL; in hci_id_add()
888 return -ENOBUFS; in hci_id_add()
892 bt_addr_le_copy(&cp->peer_id_addr, addr); in hci_id_add()
893 memcpy(cp->peer_irk, peer_irk, 16); in hci_id_add()
896 (void)memcpy(cp->local_irk, &bt_dev.irk[id], 16); in hci_id_add()
898 (void)memset(cp->local_irk, 0, 16); in hci_id_add()
904 static void pending_id_update(struct bt_keys *keys, void *data) in pending_id_update() argument
906 if (keys->state & BT_KEYS_ID_PENDING_ADD) { in pending_id_update()
907 keys->state &= ~BT_KEYS_ID_PENDING_ADD; in pending_id_update()
908 bt_id_add(keys); in pending_id_update()
912 if (keys->state & BT_KEYS_ID_PENDING_DEL) { in pending_id_update()
913 keys->state &= ~BT_KEYS_ID_PENDING_DEL; in pending_id_update()
914 bt_id_del(keys); in pending_id_update()
919 static void bt_id_pending_keys_update_set(struct bt_keys *keys, uint8_t flag) in bt_id_pending_keys_update_set() argument
922 keys->state |= flag; in bt_id_pending_keys_update_set()
955 __ASSERT_NO_MSG(conflict->candidate != NULL); in find_rl_conflict()
958 __ASSERT_NO_MSG((conflict->candidate->state & BT_KEYS_ID_ADDED) == 0); in find_rl_conflict()
960 if (conflict->found) { in find_rl_conflict()
965 if ((resident->state & BT_KEYS_ID_ADDED) == 0) { in find_rl_conflict()
969 addr_conflict = bt_addr_le_eq(&conflict->candidate->addr, &resident->addr); in find_rl_conflict()
971 /* All-zero IRK is "no IRK", and does not conflict with other Zero-IRKs. */ in find_rl_conflict()
972 irk_conflict = (!bt_irk_eq(&conflict->candidate->irk, &(struct bt_irk){}) && in find_rl_conflict()
973 bt_irk_eq(&conflict->candidate->irk, &resident->irk)); in find_rl_conflict()
976 LOG_DBG("Resident : addr %s and IRK %s", bt_addr_le_str(&resident->addr), in find_rl_conflict()
977 bt_hex(resident->irk.val, sizeof(resident->irk.val))); in find_rl_conflict()
978 LOG_DBG("Candidate: addr %s and IRK %s", bt_addr_le_str(&conflict->candidate->addr), in find_rl_conflict()
979 bt_hex(conflict->candidate->irk.val, sizeof(conflict->candidate->irk.val))); in find_rl_conflict()
981 conflict->found = resident; in find_rl_conflict()
996 void bt_id_add(struct bt_keys *keys) in bt_id_add() argument
998 CHECKIF(keys == NULL) { in bt_id_add()
1005 LOG_DBG("addr %s", bt_addr_le_str(&keys->addr)); in bt_id_add()
1007 __ASSERT_NO_MSG(keys != NULL); in bt_id_add()
1008 /* We assume (and could assert) !bt_id_find_conflict(keys) here. */ in bt_id_add()
1010 /* Nothing to be done if host-side resolving is used */ in bt_id_add()
1013 keys->state |= BT_KEYS_ID_ADDED; in bt_id_add()
1019 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_ADD); in bt_id_add()
1030 bt_id_pending_keys_update_set(keys, in bt_id_add()
1041 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_ADD); in bt_id_add()
1074 keys->state |= BT_KEYS_ID_ADDED; in bt_id_add()
1079 err = hci_id_add(keys->id, &keys->addr, keys->irk.val); in bt_id_add()
1086 keys->state |= BT_KEYS_ID_ADDED; in bt_id_add()
1100 err = le_set_privacy_mode(&keys->addr, BT_HCI_LE_PRIVACY_MODE_DEVICE); in bt_id_add()
1120 static void keys_add_id(struct bt_keys *keys, void *data) in keys_add_id() argument
1122 if (keys->state & BT_KEYS_ID_ADDED) { in keys_add_id()
1123 hci_id_add(keys->id, &keys->addr, keys->irk.val); in keys_add_id()
1136 return -ENOBUFS; in hci_id_del()
1140 bt_addr_le_copy(&cp->peer_id_addr, addr); in hci_id_del()
1145 void bt_id_del(struct bt_keys *keys) in bt_id_del() argument
1150 CHECKIF(keys == NULL) { in bt_id_del()
1154 LOG_DBG("addr %s", bt_addr_le_str(&keys->addr)); in bt_id_del()
1160 bt_dev.le.rl_entries--; in bt_id_del()
1162 keys->state &= ~BT_KEYS_ID_ADDED; in bt_id_del()
1168 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL); in bt_id_del()
1179 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL); in bt_id_del()
1189 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL); in bt_id_del()
1211 bt_dev.le.rl_entries--; in bt_id_del()
1212 keys->state &= ~BT_KEYS_ID_ADDED; in bt_id_del()
1222 err = hci_id_del(&keys->addr); in bt_id_del()
1228 bt_dev.le.rl_entries--; in bt_id_del()
1229 keys->state &= ~BT_KEYS_ID_ADDED; in bt_id_del()
1232 /* Only re-enable if there are entries to do resolving with */ in bt_id_del()
1271 return -ENOENT; in id_find()
1340 return -EINVAL; in bt_id_create()
1345 return -EALREADY; in bt_id_create()
1348 if (addr->type == BT_ADDR_LE_PUBLIC && IS_ENABLED(CONFIG_BT_HCI_SET_PUBLIC_ADDR)) { in bt_id_create()
1351 return -EALREADY; in bt_id_create()
1356 } else if (addr->type != BT_ADDR_LE_RANDOM || !BT_ADDR_IS_STATIC(&addr->a)) { in bt_id_create()
1358 return -EINVAL; in bt_id_create()
1363 return -ENOMEM; in bt_id_create()
1371 return -EINVAL; in bt_id_create()
1376 return -EINVAL; in bt_id_create()
1383 bt_dev.id_count--; in bt_id_create()
1395 if (addr->type != BT_ADDR_LE_RANDOM || in bt_id_reset()
1396 !BT_ADDR_IS_STATIC(&addr->a)) { in bt_id_reset()
1398 return -EINVAL; in bt_id_reset()
1402 return -EALREADY; in bt_id_reset()
1407 return -EINVAL; in bt_id_reset()
1411 return -EINVAL; in bt_id_reset()
1422 return -EBUSY; in bt_id_reset()
1445 return -EINVAL; in bt_id_delete()
1449 return -EALREADY; in bt_id_delete()
1460 return -EBUSY; in bt_id_delete()
1478 if (id == bt_dev.id_count - 1) { in bt_id_delete()
1479 bt_dev.id_count--; in bt_id_delete()
1514 rsp->len != sizeof(struct bt_hci_rp_vs_read_key_hierarchy_roots)) { in bt_read_identity_root()
1520 rp = (void *)rsp->data; in bt_read_identity_root()
1521 memcpy(ir, rp->ir, 16); in bt_read_identity_root()
1546 rp = (void *)rsp->data; in bt_id_read_public_addr()
1548 if (bt_addr_eq(&rp->bdaddr, BT_ADDR_ANY) || in bt_id_read_public_addr()
1549 bt_addr_eq(&rp->bdaddr, BT_ADDR_NONE)) { in bt_id_read_public_addr()
1555 bt_addr_copy(&addr->a, &rp->bdaddr); in bt_id_read_public_addr()
1556 addr->type = BT_ADDR_LE_PUBLIC; in bt_id_read_public_addr()
1619 rsp->len < sizeof(struct bt_hci_rp_vs_read_static_addrs)) { in vs_read_static_addr()
1625 rp = (void *)rsp->data; in vs_read_static_addr()
1626 cnt = MIN(rp->num_addrs, size); in vs_read_static_addr()
1629 rsp->len != (sizeof(struct bt_hci_rp_vs_read_static_addrs) + in vs_read_static_addr()
1630 rp->num_addrs * in vs_read_static_addr()
1638 memcpy(&addrs[i], &rp->a[i], sizeof(struct bt_hci_vs_static_addr)); in vs_read_static_addr()
1725 return -EINVAL; in bt_id_set_create_conn_own_addr()
1750 * need to restore it before creating connection. Otherwise in bt_id_set_create_conn_own_addr()
1753 if (addr->type == BT_ADDR_LE_RANDOM) { in bt_id_set_create_conn_own_addr()
1754 err = set_random_address(&addr->a); in bt_id_set_create_conn_own_addr()
1789 return adv && atomic_test_bit(adv->flags, BT_ADV_ENABLED); in is_adv_using_rand_addr()
1797 return -EINVAL; in bt_id_set_scan_own_addr()
1809 if (err == -EACCES && (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) || in bt_id_set_scan_own_addr()
1865 return -EINVAL; in bt_id_set_adv_own_addr()
1869 id_addr = &bt_dev.id_addr[adv->id]; in bt_id_set_adv_own_addr()
1871 /* Short-circuit to force NRPA usage */ in bt_id_set_adv_own_addr()
1876 return -EINVAL; in bt_id_set_adv_own_addr()
1891 return -ENOTSUP; in bt_id_set_adv_own_addr()
1909 * address we need to restore it before advertising in bt_id_set_adv_own_addr()
1913 if (id_addr->type == BT_ADDR_LE_RANDOM) { in bt_id_set_adv_own_addr()
1914 err = bt_id_set_adv_random_addr(adv, &id_addr->a); in bt_id_set_adv_own_addr()
1920 } else if (id_addr->type == BT_ADDR_LE_PUBLIC) { in bt_id_set_adv_own_addr()
1930 if (id_addr->type == BT_ADDR_LE_RANDOM) { in bt_id_set_adv_own_addr()
1931 err = bt_id_set_adv_random_addr(adv, &id_addr->a); in bt_id_set_adv_own_addr()
1937 } else if (id_addr->type == BT_ADDR_LE_PUBLIC) { in bt_id_set_adv_own_addr()
1986 return -EINVAL; in bt_br_oob_get_local()
1989 bt_addr_copy(&oob->addr, &bt_dev.id_addr[0].a); in bt_br_oob_get_local()
2001 return -EINVAL; in bt_le_oob_get_local()
2005 return -EAGAIN; in bt_le_oob_get_local()
2009 return -EINVAL; in bt_le_oob_get_local()
2017 !(adv && adv->id == id && in bt_le_oob_get_local()
2018 atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in bt_le_oob_get_local()
2019 atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) && in bt_le_oob_get_local()
2032 return -EINVAL; in bt_le_oob_get_local()
2037 atomic_test_bit(adv->flags, BT_ADV_ENABLED) && in bt_le_oob_get_local()
2038 atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) && in bt_le_oob_get_local()
2044 return -EINVAL; in bt_le_oob_get_local()
2053 return -EINVAL; in bt_le_oob_get_local()
2058 bt_addr_le_copy(&oob->addr, &bt_dev.random_addr); in bt_le_oob_get_local()
2060 bt_addr_le_copy(&oob->addr, &bt_dev.id_addr[id]); in bt_le_oob_get_local()
2064 err = bt_smp_le_oob_generate_sc_data(&oob->le_sc_data); in bt_le_oob_get_local()
2065 if (err && err != -ENOTSUP) { in bt_le_oob_get_local()
2080 return -EINVAL; in bt_le_ext_adv_oob_get_local()
2084 return -EAGAIN; in bt_le_ext_adv_oob_get_local()
2088 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) { in bt_le_ext_adv_oob_get_local()
2094 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED) && in bt_le_ext_adv_oob_get_local()
2109 return -EINVAL; in bt_le_ext_adv_oob_get_local()
2116 bt_addr_le_copy(&oob->addr, &adv->random_addr); in bt_le_ext_adv_oob_get_local()
2118 bt_addr_le_copy(&oob->addr, &bt_dev.id_addr[adv->id]); in bt_le_ext_adv_oob_get_local()
2122 err = bt_smp_le_oob_generate_sc_data(&oob->le_sc_data); in bt_le_ext_adv_oob_get_local()
2123 if (err && err != -ENOTSUP) { in bt_le_ext_adv_oob_get_local()
2137 return -EINVAL; in bt_le_oob_set_legacy_tk()
2150 return -EINVAL; in bt_le_oob_set_sc_data()
2154 return -EAGAIN; in bt_le_oob_set_sc_data()
2165 return -EINVAL; in bt_le_oob_get_sc_data()
2169 return -EAGAIN; in bt_le_oob_get_sc_data()