1 /*
2 * Copyright (c) 2017-2021 Nordic Semiconductor ASA
3 * Copyright (c) 2015-2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <stdbool.h>
9
10 #include <zephyr/settings/settings.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/sys/check.h>
13
14 #include <zephyr/bluetooth/addr.h>
15 #include <zephyr/bluetooth/bluetooth.h>
16 #include <zephyr/bluetooth/hci_vs.h>
17 #include <zephyr/bluetooth/buf.h>
18 #include <zephyr/drivers/bluetooth/hci_driver.h>
19 #include <zephyr/sys/__assert.h>
20
21 #include "hci_core.h"
22 #include "id.h"
23 #include "scan.h"
24 #include "adv.h"
25 #include "smp.h"
26 #include "conn_internal.h"
27 #include "keys.h"
28 #include "common/rpa.h"
29
30 #include "settings.h"
31
32 #include <zephyr/bluetooth/hci.h>
33
34 #include "common/bt_str.h"
35
36 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
37 #include <zephyr/logging/log.h>
38 LOG_MODULE_REGISTER(bt_id);
39
40 struct bt_adv_id_check_data {
41 uint8_t id;
42 bool adv_enabled;
43 };
44
45 #if defined(CONFIG_BT_OBSERVER) || defined(CONFIG_BT_BROADCASTER)
bt_lookup_id_addr(uint8_t id,const bt_addr_le_t * addr)46 const bt_addr_le_t *bt_lookup_id_addr(uint8_t id, const bt_addr_le_t *addr)
47 {
48 CHECKIF(id >= CONFIG_BT_ID_MAX || addr == NULL) {
49 return NULL;
50 }
51
52 if (IS_ENABLED(CONFIG_BT_SMP)) {
53 struct bt_keys *keys;
54
55 keys = bt_keys_find_irk(id, addr);
56 if (keys) {
57 LOG_DBG("Identity %s matched RPA %s", bt_addr_le_str(&keys->addr),
58 bt_addr_le_str(addr));
59 return &keys->addr;
60 }
61 }
62
63 return addr;
64 }
65 #endif /* CONFIG_BT_OBSERVER || CONFIG_BT_CONN */
66
adv_id_check_func(struct bt_le_ext_adv * adv,void * data)67 static void adv_id_check_func(struct bt_le_ext_adv *adv, void *data)
68 {
69 struct bt_adv_id_check_data *check_data = data;
70
71 if (IS_ENABLED(CONFIG_BT_EXT_ADV)) {
72 /* Only check if the ID is in use, as the advertiser can be
73 * started and stopped without reconfiguring parameters.
74 */
75 if (check_data->id == adv->id) {
76 check_data->adv_enabled = true;
77 }
78 } else {
79 if (check_data->id == adv->id &&
80 atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
81 check_data->adv_enabled = true;
82 }
83 }
84 }
85
adv_is_private_enabled(struct bt_le_ext_adv * adv,void * data)86 static void adv_is_private_enabled(struct bt_le_ext_adv *adv, void *data)
87 {
88 bool *adv_enabled = data;
89
90 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
91 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
92 *adv_enabled = true;
93 }
94 }
95
96 #if defined(CONFIG_BT_SMP)
adv_is_limited_enabled(struct bt_le_ext_adv * adv,void * data)97 static void adv_is_limited_enabled(struct bt_le_ext_adv *adv, void *data)
98 {
99 bool *adv_enabled = data;
100
101 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
102 atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
103 *adv_enabled = true;
104 }
105 }
106
adv_pause_enabled(struct bt_le_ext_adv * adv,void * data)107 static void adv_pause_enabled(struct bt_le_ext_adv *adv, void *data)
108 {
109 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
110 atomic_set_bit(adv->flags, BT_ADV_PAUSED);
111 bt_le_adv_set_enable(adv, false);
112 }
113 }
114
adv_unpause_enabled(struct bt_le_ext_adv * adv,void * data)115 static void adv_unpause_enabled(struct bt_le_ext_adv *adv, void *data)
116 {
117 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_PAUSED)) {
118 bt_le_adv_set_enable(adv, true);
119 }
120 }
121 #endif /* defined(CONFIG_BT_SMP) */
122
set_random_address(const bt_addr_t * addr)123 static int set_random_address(const bt_addr_t *addr)
124 {
125 struct net_buf *buf;
126 int err;
127
128 LOG_DBG("%s", bt_addr_str(addr));
129
130 /* Do nothing if we already have the right address */
131 if (bt_addr_eq(addr, &bt_dev.random_addr.a)) {
132 return 0;
133 }
134
135 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(*addr));
136 if (!buf) {
137 return -ENOBUFS;
138 }
139
140 net_buf_add_mem(buf, addr, sizeof(*addr));
141
142 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL);
143 if (err) {
144 return err;
145 }
146
147 bt_addr_copy(&bt_dev.random_addr.a, addr);
148 bt_dev.random_addr.type = BT_ADDR_LE_RANDOM;
149 return 0;
150 }
151
bt_id_set_adv_random_addr(struct bt_le_ext_adv * adv,const bt_addr_t * addr)152 int bt_id_set_adv_random_addr(struct bt_le_ext_adv *adv,
153 const bt_addr_t *addr)
154 {
155 struct bt_hci_cp_le_set_adv_set_random_addr *cp;
156 struct net_buf *buf;
157 int err;
158
159 CHECKIF(adv == NULL || addr == NULL) {
160 return -EINVAL;
161 }
162
163 if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
164 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
165 return set_random_address(addr);
166 }
167
168 LOG_DBG("%s", bt_addr_str(addr));
169
170 if (!atomic_test_bit(adv->flags, BT_ADV_PARAMS_SET)) {
171 bt_addr_copy(&adv->random_addr.a, addr);
172 adv->random_addr.type = BT_ADDR_LE_RANDOM;
173 atomic_set_bit(adv->flags, BT_ADV_RANDOM_ADDR_PENDING);
174 return 0;
175 }
176
177 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_SET_RANDOM_ADDR,
178 sizeof(*cp));
179 if (!buf) {
180 return -ENOBUFS;
181 }
182
183 cp = net_buf_add(buf, sizeof(*cp));
184
185 cp->handle = adv->handle;
186 bt_addr_copy(&cp->bdaddr, addr);
187
188 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADV_SET_RANDOM_ADDR, buf,
189 NULL);
190 if (err) {
191 return err;
192 }
193
194 if (&adv->random_addr.a != addr) {
195 bt_addr_copy(&adv->random_addr.a, addr);
196 }
197 adv->random_addr.type = BT_ADDR_LE_RANDOM;
198 return 0;
199 }
200
adv_rpa_expired(struct bt_le_ext_adv * adv)201 static void adv_rpa_expired(struct bt_le_ext_adv *adv)
202 {
203 bool rpa_invalid = true;
204
205 #if defined(CONFIG_BT_EXT_ADV) && defined(CONFIG_BT_PRIVACY)
206 /* Notify the user about the RPA timeout and set the RPA validity. */
207 if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID) &&
208 adv->cb && adv->cb->rpa_expired) {
209 rpa_invalid = adv->cb->rpa_expired(adv);
210 }
211 #endif
212 if (rpa_invalid) {
213 atomic_clear_bit(adv->flags, BT_ADV_RPA_VALID);
214
215 #if defined(CONFIG_BT_RPA_SHARING)
216 bt_addr_copy(&bt_dev.rpa[adv->id], BT_ADDR_NONE);
217 #endif
218 }
219 }
220
adv_rpa_invalidate(struct bt_le_ext_adv * adv,void * data)221 static void adv_rpa_invalidate(struct bt_le_ext_adv *adv, void *data)
222 {
223 /* RPA of Advertisers limited by timeot or number of packets only expire
224 * when they are stopped.
225 */
226 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED) &&
227 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
228 adv_rpa_expired(adv);
229 }
230 }
231
le_rpa_invalidate(void)232 static void le_rpa_invalidate(void)
233 {
234 /* Invalidate RPA */
235 if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
236 atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED))) {
237 atomic_clear_bit(bt_dev.flags, BT_DEV_RPA_VALID);
238 }
239
240 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
241 bt_le_ext_adv_foreach(adv_rpa_invalidate, NULL);
242 }
243 }
244
245 #if defined(CONFIG_BT_PRIVACY)
246
247 #if defined(CONFIG_BT_RPA_TIMEOUT_DYNAMIC)
le_rpa_timeout_update(void)248 static void le_rpa_timeout_update(void)
249 {
250 int err = 0;
251
252 if (atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_RPA_TIMEOUT_CHANGED)) {
253 struct net_buf *buf;
254 struct bt_hci_cp_le_set_rpa_timeout *cp;
255
256 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RPA_TIMEOUT,
257 sizeof(*cp));
258 if (!buf) {
259 LOG_ERR("Failed to create HCI RPA timeout command");
260 err = -ENOBUFS;
261 goto submit;
262 }
263
264 cp = net_buf_add(buf, sizeof(*cp));
265 cp->rpa_timeout = sys_cpu_to_le16(bt_dev.rpa_timeout);
266 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_RPA_TIMEOUT, buf, NULL);
267 if (err) {
268 LOG_ERR("Failed to send HCI RPA timeout command");
269 goto submit;
270 }
271 }
272
273 submit:
274 if (err) {
275 atomic_set_bit(bt_dev.flags, BT_DEV_RPA_TIMEOUT_CHANGED);
276 }
277 }
278 #endif
279
le_rpa_timeout_submit(void)280 static void le_rpa_timeout_submit(void)
281 {
282 #if defined(CONFIG_BT_RPA_TIMEOUT_DYNAMIC)
283 le_rpa_timeout_update();
284 #endif
285
286 (void)k_work_schedule(&bt_dev.rpa_update, K_SECONDS(bt_dev.rpa_timeout));
287 }
288
289 /* this function sets new RPA only if current one is no longer valid */
bt_id_set_private_addr(uint8_t id)290 int bt_id_set_private_addr(uint8_t id)
291 {
292 bt_addr_t rpa;
293 int err;
294
295 CHECKIF(id >= CONFIG_BT_ID_MAX) {
296 return -EINVAL;
297 }
298
299 /* check if RPA is valid */
300 if (atomic_test_bit(bt_dev.flags, BT_DEV_RPA_VALID)) {
301 return 0;
302 }
303
304 err = bt_rpa_create(bt_dev.irk[id], &rpa);
305 if (!err) {
306 err = set_random_address(&rpa);
307 if (!err) {
308 atomic_set_bit(bt_dev.flags, BT_DEV_RPA_VALID);
309 }
310 }
311
312 le_rpa_timeout_submit();
313
314 if (err) {
315 return err;
316 }
317
318 if (IS_ENABLED(CONFIG_BT_LOG_SNIFFER_INFO)) {
319 LOG_INF("RPA: %s", bt_addr_str(&rpa));
320 }
321
322 return 0;
323 }
324
325 #if defined(CONFIG_BT_RPA_SHARING)
adv_rpa_get(struct bt_le_ext_adv * adv,bt_addr_t * rpa)326 static int adv_rpa_get(struct bt_le_ext_adv *adv, bt_addr_t *rpa)
327 {
328 int err;
329
330 if (bt_addr_eq(&bt_dev.rpa[adv->id], BT_ADDR_NONE)) {
331 err = bt_rpa_create(bt_dev.irk[adv->id], &bt_dev.rpa[adv->id]);
332 if (err) {
333 return err;
334 }
335 }
336
337 bt_addr_copy(rpa, &bt_dev.rpa[adv->id]);
338
339 return 0;
340 }
341 #else
adv_rpa_get(struct bt_le_ext_adv * adv,bt_addr_t * rpa)342 static int adv_rpa_get(struct bt_le_ext_adv *adv, bt_addr_t *rpa)
343 {
344 int err;
345
346 err = bt_rpa_create(bt_dev.irk[adv->id], rpa);
347 if (err) {
348 return err;
349 }
350
351 return 0;
352 }
353 #endif /* defined(CONFIG_BT_RPA_SHARING) */
354
bt_id_set_adv_private_addr(struct bt_le_ext_adv * adv)355 int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv)
356 {
357 bt_addr_t rpa;
358 int err;
359
360 CHECKIF(adv == NULL) {
361 return -EINVAL;
362 }
363
364 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
365 (adv->options & BT_LE_ADV_OPT_USE_NRPA)) {
366 /* The host doesn't support setting NRPAs when BT_PRIVACY=y.
367 * In that case you probably want to use an RPA anyway.
368 */
369 LOG_ERR("NRPA not supported when BT_PRIVACY=y");
370
371 return -ENOSYS;
372 }
373
374 if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
375 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
376 return bt_id_set_private_addr(adv->id);
377 }
378
379 /* check if RPA is valid */
380 if (atomic_test_bit(adv->flags, BT_ADV_RPA_VALID)) {
381 /* Schedule the RPA timer if it is not running.
382 * The RPA may be valid without the timer running.
383 */
384 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
385 le_rpa_timeout_submit();
386 }
387
388 return 0;
389 }
390
391 if (adv == bt_le_adv_lookup_legacy() && adv->id == BT_ID_DEFAULT) {
392 /* Make sure that a Legacy advertiser using default ID has same
393 * RPA address as scanner roles.
394 */
395 err = bt_id_set_private_addr(BT_ID_DEFAULT);
396 if (err) {
397 return err;
398 }
399
400 err = bt_id_set_adv_random_addr(adv, &bt_dev.random_addr.a);
401 if (!err) {
402 atomic_set_bit(adv->flags, BT_ADV_RPA_VALID);
403 }
404
405 return 0;
406 }
407
408 err = adv_rpa_get(adv, &rpa);
409 if (!err) {
410 err = bt_id_set_adv_random_addr(adv, &rpa);
411 if (!err) {
412 atomic_set_bit(adv->flags, BT_ADV_RPA_VALID);
413 }
414 }
415
416 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED)) {
417 le_rpa_timeout_submit();
418 }
419
420 if (err) {
421 return err;
422 }
423
424 if (IS_ENABLED(CONFIG_BT_LOG_SNIFFER_INFO)) {
425 LOG_INF("RPA: %s", bt_addr_str(&rpa));
426 }
427
428 return 0;
429 }
430 #else
bt_id_set_private_addr(uint8_t id)431 int bt_id_set_private_addr(uint8_t id)
432 {
433 bt_addr_t nrpa;
434 int err;
435
436 CHECKIF(id >= CONFIG_BT_ID_MAX) {
437 return -EINVAL;
438 }
439
440 err = bt_rand(nrpa.val, sizeof(nrpa.val));
441 if (err) {
442 return err;
443 }
444
445 BT_ADDR_SET_NRPA(&nrpa);
446
447 err = set_random_address(&nrpa);
448 if (err) {
449 return err;
450 }
451
452 if (IS_ENABLED(CONFIG_BT_LOG_SNIFFER_INFO)) {
453 LOG_INF("NRPA: %s", bt_addr_str(&nrpa));
454 }
455
456 return 0;
457 }
458
bt_id_set_adv_private_addr(struct bt_le_ext_adv * adv)459 int bt_id_set_adv_private_addr(struct bt_le_ext_adv *adv)
460 {
461 bt_addr_t nrpa;
462 int err;
463
464 CHECKIF(adv == NULL) {
465 return -EINVAL;
466 }
467
468 err = bt_rand(nrpa.val, sizeof(nrpa.val));
469 if (err) {
470 return err;
471 }
472
473 BT_ADDR_SET_NRPA(&nrpa);
474
475 err = bt_id_set_adv_random_addr(adv, &nrpa);
476 if (err) {
477 return err;
478 }
479
480 if (IS_ENABLED(CONFIG_BT_LOG_SNIFFER_INFO)) {
481 LOG_INF("NRPA: %s", bt_addr_str(&nrpa));
482 }
483
484 return 0;
485 }
486 #endif /* defined(CONFIG_BT_PRIVACY) */
487
adv_pause_rpa(struct bt_le_ext_adv * adv,void * data)488 static void adv_pause_rpa(struct bt_le_ext_adv *adv, void *data)
489 {
490 bool *adv_enabled = data;
491
492 /* Disable advertising sets to prepare them for RPA update. */
493 if (atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
494 !atomic_test_bit(adv->flags, BT_ADV_LIMITED) &&
495 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
496 int err;
497
498 err = bt_le_adv_set_enable_ext(adv, false, NULL);
499 if (err) {
500 LOG_ERR("Failed to disable advertising (err %d)", err);
501 }
502
503 atomic_set_bit(adv->flags, BT_ADV_RPA_UPDATE);
504 *adv_enabled = true;
505 }
506 }
507
le_adv_rpa_timeout(void)508 static bool le_adv_rpa_timeout(void)
509 {
510 bool adv_enabled = false;
511
512 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
513 if (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
514 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
515 /* Pause all advertising sets using RPAs */
516 bt_le_ext_adv_foreach(adv_pause_rpa, &adv_enabled);
517 } else {
518 /* Check if advertising set is enabled */
519 bt_le_ext_adv_foreach(adv_is_private_enabled, &adv_enabled);
520 }
521 }
522
523 return adv_enabled;
524 }
525
adv_enable_rpa(struct bt_le_ext_adv * adv,void * data)526 static void adv_enable_rpa(struct bt_le_ext_adv *adv, void *data)
527 {
528 if (atomic_test_and_clear_bit(adv->flags, BT_ADV_RPA_UPDATE)) {
529 int err;
530
531 err = bt_id_set_adv_private_addr(adv);
532 if (err) {
533 LOG_WRN("Failed to update advertiser RPA address (%d)", err);
534 }
535
536 err = bt_le_adv_set_enable_ext(adv, true, NULL);
537 if (err) {
538 LOG_ERR("Failed to enable advertising (err %d)", err);
539 }
540 }
541 }
542
le_update_private_addr(void)543 static void le_update_private_addr(void)
544 {
545 struct bt_le_ext_adv *adv = NULL;
546 bool adv_enabled = false;
547 uint8_t id = BT_ID_DEFAULT;
548 int err;
549
550 #if defined(CONFIG_BT_OBSERVER)
551 bool scan_enabled = false;
552
553 if (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) &&
554 !(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
555 atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED))) {
556 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
557 scan_enabled = true;
558 }
559 #endif
560 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
561 IS_ENABLED(CONFIG_BT_FILTER_ACCEPT_LIST) &&
562 atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
563 /* Canceled initiating procedure will be restarted by
564 * connection complete event.
565 */
566 bt_le_create_conn_cancel();
567 }
568
569 if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
570 !(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
571 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
572 adv = bt_le_adv_lookup_legacy();
573
574 if (adv &&
575 atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
576 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
577 adv_enabled = true;
578 id = adv->id;
579 bt_le_adv_set_enable_legacy(adv, false);
580 }
581 }
582
583 /* If both advertiser and scanner is running then the advertiser
584 * ID must be BT_ID_DEFAULT, this will update the RPA address
585 * for both roles.
586 */
587 err = bt_id_set_private_addr(id);
588 if (err) {
589 LOG_WRN("Failed to update RPA address (%d)", err);
590 return;
591 }
592
593 if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
594 IS_ENABLED(CONFIG_BT_EXT_ADV) &&
595 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features)) {
596 bt_le_ext_adv_foreach(adv_enable_rpa, NULL);
597 }
598
599 if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
600 adv && adv_enabled) {
601 bt_le_adv_set_enable_legacy(adv, true);
602 }
603
604 #if defined(CONFIG_BT_OBSERVER)
605 if (scan_enabled) {
606 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
607 }
608 #endif
609 }
610
le_force_rpa_timeout(void)611 static void le_force_rpa_timeout(void)
612 {
613 #if defined(CONFIG_BT_PRIVACY)
614 struct k_work_sync sync;
615
616 k_work_cancel_delayable_sync(&bt_dev.rpa_update, &sync);
617 #endif
618 (void)le_adv_rpa_timeout();
619 le_rpa_invalidate();
620 le_update_private_addr();
621 }
622
623 #if defined(CONFIG_BT_PRIVACY)
rpa_timeout(struct k_work * work)624 static void rpa_timeout(struct k_work *work)
625 {
626 bool adv_enabled;
627
628 LOG_DBG("");
629
630 if (IS_ENABLED(CONFIG_BT_CENTRAL)) {
631 struct bt_conn *conn =
632 bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
633 BT_CONN_CONNECTING_SCAN);
634
635 if (conn) {
636 bt_conn_unref(conn);
637 bt_le_create_conn_cancel();
638 }
639 }
640
641 adv_enabled = le_adv_rpa_timeout();
642 le_rpa_invalidate();
643
644 /* IF no roles using the RPA is running we can stop the RPA timer */
645 if (!(adv_enabled ||
646 atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING) ||
647 (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) &&
648 atomic_test_bit(bt_dev.flags, BT_DEV_ACTIVE_SCAN)))) {
649 return;
650 }
651
652 le_update_private_addr();
653 }
654 #endif /* CONFIG_BT_PRIVACY */
655
bt_id_scan_random_addr_check(void)656 bool bt_id_scan_random_addr_check(void)
657 {
658 struct bt_le_ext_adv *adv;
659
660 if (!IS_ENABLED(CONFIG_BT_BROADCASTER) ||
661 (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
662 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
663 /* Advertiser is not enabled or advertiser and scanner are using
664 * a different random address.
665 */
666 return true;
667 }
668
669 adv = bt_le_adv_lookup_legacy();
670 if (!adv) {
671 return true;
672 }
673
674 /* If the advertiser is not active there is no issue */
675 if (!atomic_test_bit(adv->flags, BT_ADV_ENABLED)) {
676 return true;
677 }
678
679 /* When privacy is enabled the random address will not be set
680 * immediately before starting the role, because the RPA might still be
681 * valid and only updated on RPA timeout.
682 */
683 if (IS_ENABLED(CONFIG_BT_PRIVACY)) {
684 /* Cannot start scanner or initiator if the random address is
685 * used by the advertiser for an RPA with a different identity
686 * or for a random static identity address.
687 */
688 if ((atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) &&
689 bt_dev.id_addr[adv->id].type == BT_ADDR_LE_RANDOM) ||
690 adv->id != BT_ID_DEFAULT) {
691 return false;
692 }
693 }
694
695 /* If privacy is not enabled then the random address will be attempted
696 * to be set before enabling the role. If another role is already using
697 * the random address then this command will fail, and should return
698 * the error code to the application.
699 */
700 return true;
701 }
702
bt_id_adv_random_addr_check(const struct bt_le_adv_param * param)703 bool bt_id_adv_random_addr_check(const struct bt_le_adv_param *param)
704 {
705 CHECKIF(param == NULL) {
706 return false;
707 }
708
709 if (!IS_ENABLED(CONFIG_BT_OBSERVER) ||
710 (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
711 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
712 /* If scanner roles are not enabled or advertiser and scanner
713 * are using a different random address.
714 */
715 return true;
716 }
717
718 /* If scanner roles are not active there is no issue. */
719 if (!(atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING) ||
720 atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING))) {
721 return true;
722 }
723
724 /* When privacy is enabled the random address will not be set
725 * immediately before starting the role, because the RPA might still be
726 * valid and only updated on RPA timeout.
727 */
728 if (IS_ENABLED(CONFIG_BT_PRIVACY)) {
729 /* Cannot start an advertiser with random static identity or
730 * using an RPA generated for a different identity than scanner
731 * roles.
732 */
733 if (((param->options & BT_LE_ADV_OPT_USE_IDENTITY) &&
734 bt_dev.id_addr[param->id].type == BT_ADDR_LE_RANDOM) ||
735 param->id != BT_ID_DEFAULT) {
736 return false;
737 }
738 } else if (IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
739 atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) &&
740 bt_dev.id_addr[BT_ID_DEFAULT].type == BT_ADDR_LE_RANDOM) {
741 /* Scanning with random static identity. Stop the advertiser
742 * from overwriting the passive scanner identity address.
743 * In this case the LE Set Random Address command does not
744 * protect us in the case of a passive scanner.
745 * Explicitly stop it here.
746 */
747
748 if (!(param->options & BT_LE_ADV_OPT_CONNECTABLE) &&
749 (param->options & BT_LE_ADV_OPT_USE_IDENTITY)) {
750 /* Attempt to set non-connectable NRPA */
751 return false;
752 } else if (bt_dev.id_addr[param->id].type ==
753 BT_ADDR_LE_RANDOM &&
754 param->id != BT_ID_DEFAULT) {
755 /* Attempt to set connectable, or non-connectable with
756 * identity different than scanner.
757 */
758 return false;
759 }
760 }
761
762 /* If privacy is not enabled then the random address will be attempted
763 * to be set before enabling the role. If another role is already using
764 * the random address then this command will fail, and should return
765 * the error code to the application.
766 */
767 return true;
768 }
769
bt_id_adv_limited_stopped(struct bt_le_ext_adv * adv)770 void bt_id_adv_limited_stopped(struct bt_le_ext_adv *adv)
771 {
772 adv_rpa_expired(adv);
773 }
774
775 #if defined(CONFIG_BT_SMP)
le_set_privacy_mode(const bt_addr_le_t * addr,uint8_t mode)776 static int le_set_privacy_mode(const bt_addr_le_t *addr, uint8_t mode)
777 {
778 struct bt_hci_cp_le_set_privacy_mode cp;
779 struct net_buf *buf;
780 int err;
781
782 /* Check if set privacy mode command is supported */
783 if (!BT_CMD_TEST(bt_dev.supported_commands, 39, 2)) {
784 LOG_WRN("Set privacy mode command is not supported");
785 return 0;
786 }
787
788 LOG_DBG("addr %s mode 0x%02x", bt_addr_le_str(addr), mode);
789
790 bt_addr_le_copy(&cp.id_addr, addr);
791 cp.mode = mode;
792
793 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_PRIVACY_MODE, sizeof(cp));
794 if (!buf) {
795 return -ENOBUFS;
796 }
797
798 net_buf_add_mem(buf, &cp, sizeof(cp));
799
800 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_PRIVACY_MODE, buf, NULL);
801 if (err) {
802 return err;
803 }
804
805 return 0;
806 }
807
addr_res_enable(uint8_t enable)808 static int addr_res_enable(uint8_t enable)
809 {
810 struct net_buf *buf;
811
812 LOG_DBG("%s", enable ? "enabled" : "disabled");
813
814 buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADDR_RES_ENABLE, 1);
815 if (!buf) {
816 return -ENOBUFS;
817 }
818
819 net_buf_add_u8(buf, enable);
820
821 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_SET_ADDR_RES_ENABLE,
822 buf, NULL);
823 }
824
hci_id_add(uint8_t id,const bt_addr_le_t * addr,uint8_t peer_irk[16])825 static int hci_id_add(uint8_t id, const bt_addr_le_t *addr, uint8_t peer_irk[16])
826 {
827 struct bt_hci_cp_le_add_dev_to_rl *cp;
828 struct net_buf *buf;
829
830 if (id >= CONFIG_BT_ID_MAX) {
831 return -EINVAL;
832 }
833
834 LOG_DBG("addr %s", bt_addr_le_str(addr));
835
836 buf = bt_hci_cmd_create(BT_HCI_OP_LE_ADD_DEV_TO_RL, sizeof(*cp));
837 if (!buf) {
838 return -ENOBUFS;
839 }
840
841 cp = net_buf_add(buf, sizeof(*cp));
842 bt_addr_le_copy(&cp->peer_id_addr, addr);
843 memcpy(cp->peer_irk, peer_irk, 16);
844
845 #if defined(CONFIG_BT_PRIVACY)
846 (void)memcpy(cp->local_irk, &bt_dev.irk[id], 16);
847 #else
848 (void)memset(cp->local_irk, 0, 16);
849 #endif
850
851 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_ADD_DEV_TO_RL, buf, NULL);
852 }
853
pending_id_update(struct bt_keys * keys,void * data)854 static void pending_id_update(struct bt_keys *keys, void *data)
855 {
856 if (keys->state & BT_KEYS_ID_PENDING_ADD) {
857 keys->state &= ~BT_KEYS_ID_PENDING_ADD;
858 bt_id_add(keys);
859 return;
860 }
861
862 if (keys->state & BT_KEYS_ID_PENDING_DEL) {
863 keys->state &= ~BT_KEYS_ID_PENDING_DEL;
864 bt_id_del(keys);
865 return;
866 }
867 }
868
bt_id_pending_keys_update_set(struct bt_keys * keys,uint8_t flag)869 static void bt_id_pending_keys_update_set(struct bt_keys *keys, uint8_t flag)
870 {
871 atomic_set_bit(bt_dev.flags, BT_DEV_ID_PENDING);
872 keys->state |= flag;
873 }
874
bt_id_pending_keys_update(void)875 void bt_id_pending_keys_update(void)
876 {
877 if (atomic_test_and_clear_bit(bt_dev.flags, BT_DEV_ID_PENDING)) {
878 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
879 IS_ENABLED(CONFIG_BT_PRIVACY)) {
880 bt_keys_foreach_type(BT_KEYS_ALL, pending_id_update, NULL);
881 } else {
882 bt_keys_foreach_type(BT_KEYS_IRK, pending_id_update, NULL);
883 }
884 }
885 }
886
887 struct bt_id_conflict {
888 struct bt_keys *candidate;
889 struct bt_keys *found;
890 };
891
892 /* The Controller Resolve List is constrained by 7.8.38 "LE Add Device To
893 * Resolving List command". The Host is designed with the assumption that all
894 * local bonds can be put in the resolve list if there is room. Therefore we
895 * must refuse bonds that conflict in the resolve list. Notably, this prevents
896 * multiple local identities to bond with the same remote identity.
897 */
find_rl_conflict(struct bt_keys * resident,void * user_data)898 void find_rl_conflict(struct bt_keys *resident, void *user_data)
899 {
900 struct bt_id_conflict *conflict = user_data;
901 bool addr_conflict;
902 bool irk_conflict;
903
904 __ASSERT_NO_MSG(conflict != NULL);
905 __ASSERT_NO_MSG(conflict->candidate != NULL);
906 __ASSERT_NO_MSG(resident != NULL);
907 /* Only uncommitted bonds can be in conflict with committed bonds. */
908 __ASSERT_NO_MSG((conflict->candidate->state & BT_KEYS_ID_ADDED) == 0);
909
910 if (conflict->found) {
911 return;
912 }
913
914 /* Test against committed bonds only. */
915 if ((resident->state & BT_KEYS_ID_ADDED) == 0) {
916 return;
917 }
918
919 addr_conflict = bt_addr_le_eq(&conflict->candidate->addr, &resident->addr);
920
921 /* All-zero IRK is "no IRK", and does not conflict with other Zero-IRKs. */
922 irk_conflict = (!bt_irk_eq(&conflict->candidate->irk, &(struct bt_irk){}) &&
923 bt_irk_eq(&conflict->candidate->irk, &resident->irk));
924
925 if (addr_conflict || irk_conflict) {
926 conflict->found = resident;
927 }
928 }
929
bt_id_find_conflict(struct bt_keys * candidate)930 struct bt_keys *bt_id_find_conflict(struct bt_keys *candidate)
931 {
932 struct bt_id_conflict conflict = {
933 .candidate = candidate,
934 };
935
936 bt_keys_foreach_type(BT_KEYS_IRK, find_rl_conflict, &conflict);
937
938 return conflict.found;
939 }
940
bt_id_add(struct bt_keys * keys)941 void bt_id_add(struct bt_keys *keys)
942 {
943 CHECKIF(keys == NULL) {
944 return;
945 }
946
947 struct bt_conn *conn;
948 int err;
949
950 LOG_DBG("addr %s", bt_addr_le_str(&keys->addr));
951
952 __ASSERT_NO_MSG(keys != NULL);
953 /* We assume (and could assert) !bt_id_find_conflict(keys) here. */
954
955 /* Nothing to be done if host-side resolving is used */
956 if (!bt_dev.le.rl_size || bt_dev.le.rl_entries > bt_dev.le.rl_size) {
957 bt_dev.le.rl_entries++;
958 keys->state |= BT_KEYS_ID_ADDED;
959 return;
960 }
961
962 conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL, BT_CONN_CONNECTING);
963 if (conn) {
964 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_ADD);
965 bt_conn_unref(conn);
966 return;
967 }
968
969 if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
970 IS_ENABLED(CONFIG_BT_EXT_ADV)) {
971 bool adv_enabled = false;
972
973 bt_le_ext_adv_foreach(adv_is_limited_enabled, &adv_enabled);
974 if (adv_enabled) {
975 bt_id_pending_keys_update_set(keys,
976 BT_KEYS_ID_PENDING_ADD);
977 return;
978 }
979 }
980
981 #if defined(CONFIG_BT_OBSERVER)
982 bool scan_enabled = atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING);
983
984 if (IS_ENABLED(CONFIG_BT_EXT_ADV) && scan_enabled &&
985 atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED)) {
986 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_ADD);
987 }
988 #endif
989
990 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
991 bt_le_ext_adv_foreach(adv_pause_enabled, NULL);
992 }
993
994 #if defined(CONFIG_BT_OBSERVER)
995 if (scan_enabled) {
996 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
997 }
998 #endif /* CONFIG_BT_OBSERVER */
999
1000 /* If there are any existing entries address resolution will be on */
1001 if (bt_dev.le.rl_entries) {
1002 err = addr_res_enable(BT_HCI_ADDR_RES_DISABLE);
1003 if (err) {
1004 LOG_WRN("Failed to disable address resolution");
1005 goto done;
1006 }
1007 }
1008
1009 if (bt_dev.le.rl_entries == bt_dev.le.rl_size) {
1010 LOG_WRN("Resolving list size exceeded. Switching to host.");
1011
1012 err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_CLEAR_RL, NULL, NULL);
1013 if (err) {
1014 LOG_ERR("Failed to clear resolution list");
1015 goto done;
1016 }
1017
1018 bt_dev.le.rl_entries++;
1019 keys->state |= BT_KEYS_ID_ADDED;
1020
1021 goto done;
1022 }
1023
1024 err = hci_id_add(keys->id, &keys->addr, keys->irk.val);
1025 if (err) {
1026 LOG_ERR("Failed to add IRK to controller");
1027 goto done;
1028 }
1029
1030 bt_dev.le.rl_entries++;
1031 keys->state |= BT_KEYS_ID_ADDED;
1032
1033 /*
1034 * According to Core Spec. 5.0 Vol 1, Part A 5.4.5 Privacy Feature
1035 *
1036 * By default, network privacy mode is used when private addresses are
1037 * resolved and generated by the Controller, so advertising packets from
1038 * peer devices that contain private addresses will only be accepted.
1039 * By changing to the device privacy mode device is only concerned about
1040 * its privacy and will accept advertising packets from peer devices
1041 * that contain their identity address as well as ones that contain
1042 * a private address, even if the peer device has distributed its IRK in
1043 * the past.
1044 */
1045 err = le_set_privacy_mode(&keys->addr, BT_HCI_LE_PRIVACY_MODE_DEVICE);
1046 if (err) {
1047 LOG_ERR("Failed to set privacy mode");
1048 goto done;
1049 }
1050
1051 done:
1052 addr_res_enable(BT_HCI_ADDR_RES_ENABLE);
1053
1054 #if defined(CONFIG_BT_OBSERVER)
1055 if (scan_enabled) {
1056 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1057 }
1058 #endif /* CONFIG_BT_OBSERVER */
1059
1060 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1061 bt_le_ext_adv_foreach(adv_unpause_enabled, NULL);
1062 }
1063 }
1064
keys_add_id(struct bt_keys * keys,void * data)1065 static void keys_add_id(struct bt_keys *keys, void *data)
1066 {
1067 if (keys->state & BT_KEYS_ID_ADDED) {
1068 hci_id_add(keys->id, &keys->addr, keys->irk.val);
1069 }
1070 }
1071
hci_id_del(const bt_addr_le_t * addr)1072 static int hci_id_del(const bt_addr_le_t *addr)
1073 {
1074 struct bt_hci_cp_le_rem_dev_from_rl *cp;
1075 struct net_buf *buf;
1076
1077 LOG_DBG("addr %s", bt_addr_le_str(addr));
1078
1079 buf = bt_hci_cmd_create(BT_HCI_OP_LE_REM_DEV_FROM_RL, sizeof(*cp));
1080 if (!buf) {
1081 return -ENOBUFS;
1082 }
1083
1084 cp = net_buf_add(buf, sizeof(*cp));
1085 bt_addr_le_copy(&cp->peer_id_addr, addr);
1086
1087 return bt_hci_cmd_send_sync(BT_HCI_OP_LE_REM_DEV_FROM_RL, buf, NULL);
1088 }
1089
bt_id_del(struct bt_keys * keys)1090 void bt_id_del(struct bt_keys *keys)
1091 {
1092 struct bt_conn *conn;
1093 int err;
1094
1095 CHECKIF(keys == NULL) {
1096 return;
1097 }
1098
1099 LOG_DBG("addr %s", bt_addr_le_str(&keys->addr));
1100
1101 if (!bt_dev.le.rl_size ||
1102 bt_dev.le.rl_entries > bt_dev.le.rl_size + 1) {
1103 __ASSERT_NO_MSG(bt_dev.le.rl_entries > 0);
1104 if (bt_dev.le.rl_entries > 0) {
1105 bt_dev.le.rl_entries--;
1106 }
1107 keys->state &= ~BT_KEYS_ID_ADDED;
1108 return;
1109 }
1110
1111 conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL, BT_CONN_CONNECTING);
1112 if (conn) {
1113 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL);
1114 bt_conn_unref(conn);
1115 return;
1116 }
1117
1118 if (IS_ENABLED(CONFIG_BT_BROADCASTER) &&
1119 IS_ENABLED(CONFIG_BT_EXT_ADV)) {
1120 bool adv_enabled = false;
1121
1122 bt_le_ext_adv_foreach(adv_is_limited_enabled, &adv_enabled);
1123 if (adv_enabled) {
1124 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL);
1125 return;
1126 }
1127 }
1128
1129 #if defined(CONFIG_BT_OBSERVER)
1130 bool scan_enabled = atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING);
1131
1132 if (IS_ENABLED(CONFIG_BT_EXT_ADV) && scan_enabled &&
1133 atomic_test_bit(bt_dev.flags, BT_DEV_SCAN_LIMITED)) {
1134 bt_id_pending_keys_update_set(keys, BT_KEYS_ID_PENDING_DEL);
1135 }
1136 #endif /* CONFIG_BT_OBSERVER */
1137
1138 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1139 bt_le_ext_adv_foreach(adv_pause_enabled, NULL);
1140 }
1141
1142 #if defined(CONFIG_BT_OBSERVER)
1143 if (scan_enabled) {
1144 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1145 }
1146 #endif /* CONFIG_BT_OBSERVER */
1147
1148 err = addr_res_enable(BT_HCI_ADDR_RES_DISABLE);
1149 if (err) {
1150 LOG_ERR("Disabling address resolution failed (err %d)", err);
1151 goto done;
1152 }
1153
1154 /* We checked size + 1 earlier, so here we know we can fit again */
1155 if (bt_dev.le.rl_entries > bt_dev.le.rl_size) {
1156 bt_dev.le.rl_entries--;
1157 keys->state &= ~BT_KEYS_ID_ADDED;
1158 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1159 IS_ENABLED(CONFIG_BT_PRIVACY)) {
1160 bt_keys_foreach_type(BT_KEYS_ALL, keys_add_id, NULL);
1161 } else {
1162 bt_keys_foreach_type(BT_KEYS_IRK, keys_add_id, NULL);
1163 }
1164 goto done;
1165 }
1166
1167 err = hci_id_del(&keys->addr);
1168 if (err) {
1169 LOG_ERR("Failed to remove IRK from controller");
1170 goto done;
1171 }
1172
1173 bt_dev.le.rl_entries--;
1174 keys->state &= ~BT_KEYS_ID_ADDED;
1175
1176 done:
1177 /* Only re-enable if there are entries to do resolving with */
1178 if (bt_dev.le.rl_entries) {
1179 addr_res_enable(BT_HCI_ADDR_RES_ENABLE);
1180 }
1181
1182 #if defined(CONFIG_BT_OBSERVER)
1183 if (scan_enabled) {
1184 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1185 }
1186 #endif /* CONFIG_BT_OBSERVER */
1187
1188 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1189 bt_le_ext_adv_foreach(adv_unpause_enabled, NULL);
1190 }
1191 }
1192 #endif /* defined(CONFIG_BT_SMP) */
1193
bt_id_get(bt_addr_le_t * addrs,size_t * count)1194 void bt_id_get(bt_addr_le_t *addrs, size_t *count)
1195 {
1196 if (addrs) {
1197 size_t to_copy = MIN(*count, bt_dev.id_count);
1198
1199 memcpy(addrs, bt_dev.id_addr, to_copy * sizeof(bt_addr_le_t));
1200 *count = to_copy;
1201 } else {
1202 *count = bt_dev.id_count;
1203 }
1204 }
1205
id_find(const bt_addr_le_t * addr)1206 static int id_find(const bt_addr_le_t *addr)
1207 {
1208 uint8_t id;
1209
1210 for (id = 0U; id < bt_dev.id_count; id++) {
1211 if (bt_addr_le_eq(addr, &bt_dev.id_addr[id])) {
1212 return id;
1213 }
1214 }
1215
1216 return -ENOENT;
1217 }
1218
id_create(uint8_t id,bt_addr_le_t * addr,uint8_t * irk)1219 static int id_create(uint8_t id, bt_addr_le_t *addr, uint8_t *irk)
1220 {
1221 if (addr && !bt_addr_le_eq(addr, BT_ADDR_LE_ANY)) {
1222 bt_addr_le_copy(&bt_dev.id_addr[id], addr);
1223 } else {
1224 bt_addr_le_t new_addr;
1225
1226 do {
1227 int err;
1228
1229 err = bt_addr_le_create_static(&new_addr);
1230 if (err) {
1231 return err;
1232 }
1233 /* Make sure we didn't generate a duplicate */
1234 } while (id_find(&new_addr) >= 0);
1235
1236 bt_addr_le_copy(&bt_dev.id_addr[id], &new_addr);
1237
1238 if (addr) {
1239 bt_addr_le_copy(addr, &bt_dev.id_addr[id]);
1240 }
1241 }
1242
1243 #if defined(CONFIG_BT_PRIVACY)
1244 {
1245 uint8_t zero_irk[16] = { 0 };
1246
1247 if (irk && memcmp(irk, zero_irk, 16)) {
1248 memcpy(&bt_dev.irk[id], irk, 16);
1249 } else {
1250 int err;
1251
1252 err = bt_rand(&bt_dev.irk[id], 16);
1253 if (err) {
1254 return err;
1255 }
1256
1257 if (irk) {
1258 memcpy(irk, &bt_dev.irk[id], 16);
1259 }
1260 }
1261
1262 #if defined(CONFIG_BT_RPA_SHARING)
1263 bt_addr_copy(&bt_dev.rpa[id], BT_ADDR_NONE);
1264 #endif
1265 }
1266 #endif
1267 /* Only store if stack was already initialized. Before initialization
1268 * we don't know the flash content, so it's potentially harmful to
1269 * try to write anything there.
1270 */
1271 if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
1272 atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1273 (void)bt_settings_store_id();
1274 (void)bt_settings_store_irk();
1275 }
1276
1277 return 0;
1278 }
1279
bt_id_create(bt_addr_le_t * addr,uint8_t * irk)1280 int bt_id_create(bt_addr_le_t *addr, uint8_t *irk)
1281 {
1282 int new_id, err;
1283
1284 if (!IS_ENABLED(CONFIG_BT_PRIVACY) && irk) {
1285 return -EINVAL;
1286 }
1287
1288 if (addr && !bt_addr_le_eq(addr, BT_ADDR_LE_ANY)) {
1289 if (id_find(addr) >= 0) {
1290 return -EALREADY;
1291 }
1292
1293 if (addr->type == BT_ADDR_LE_PUBLIC && IS_ENABLED(CONFIG_BT_HCI_SET_PUBLIC_ADDR)) {
1294 /* set the single public address */
1295 if (bt_dev.id_count != 0) {
1296 return -EALREADY;
1297 }
1298 bt_addr_le_copy(&bt_dev.id_addr[BT_ID_DEFAULT], addr);
1299 bt_dev.id_count++;
1300 return BT_ID_DEFAULT;
1301 } else if (addr->type != BT_ADDR_LE_RANDOM || !BT_ADDR_IS_STATIC(&addr->a)) {
1302 LOG_ERR("Only random static identity address supported");
1303 return -EINVAL;
1304 }
1305 }
1306
1307 if (bt_dev.id_count == ARRAY_SIZE(bt_dev.id_addr)) {
1308 return -ENOMEM;
1309 }
1310
1311 /* bt_rand is not available before Bluetooth enable has been called */
1312 if (!atomic_test_bit(bt_dev.flags, BT_DEV_ENABLE)) {
1313 uint8_t zero_irk[16] = { 0 };
1314
1315 if (!(addr && !bt_addr_le_eq(addr, BT_ADDR_LE_ANY))) {
1316 return -EINVAL;
1317 }
1318
1319 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1320 !(irk && memcmp(irk, zero_irk, 16))) {
1321 return -EINVAL;
1322 }
1323 }
1324
1325 new_id = bt_dev.id_count++;
1326 err = id_create(new_id, addr, irk);
1327 if (err) {
1328 bt_dev.id_count--;
1329 return err;
1330 }
1331
1332 return new_id;
1333 }
1334
bt_id_reset(uint8_t id,bt_addr_le_t * addr,uint8_t * irk)1335 int bt_id_reset(uint8_t id, bt_addr_le_t *addr, uint8_t *irk)
1336 {
1337 int err;
1338
1339 if (addr && !bt_addr_le_eq(addr, BT_ADDR_LE_ANY)) {
1340 if (addr->type != BT_ADDR_LE_RANDOM ||
1341 !BT_ADDR_IS_STATIC(&addr->a)) {
1342 LOG_ERR("Only static random identity address supported");
1343 return -EINVAL;
1344 }
1345
1346 if (id_find(addr) >= 0) {
1347 return -EALREADY;
1348 }
1349 }
1350
1351 if (!IS_ENABLED(CONFIG_BT_PRIVACY) && irk) {
1352 return -EINVAL;
1353 }
1354
1355 if (id == BT_ID_DEFAULT || id >= bt_dev.id_count) {
1356 return -EINVAL;
1357 }
1358
1359 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1360 struct bt_adv_id_check_data check_data = {
1361 .id = id,
1362 .adv_enabled = false,
1363 };
1364
1365 bt_le_ext_adv_foreach(adv_id_check_func, &check_data);
1366 if (check_data.adv_enabled) {
1367 return -EBUSY;
1368 }
1369 }
1370
1371 if (IS_ENABLED(CONFIG_BT_CONN) &&
1372 !bt_addr_le_eq(&bt_dev.id_addr[id], BT_ADDR_LE_ANY)) {
1373 err = bt_unpair(id, NULL);
1374 if (err) {
1375 return err;
1376 }
1377 }
1378
1379 err = id_create(id, addr, irk);
1380 if (err) {
1381 return err;
1382 }
1383
1384 return id;
1385 }
1386
bt_id_delete(uint8_t id)1387 int bt_id_delete(uint8_t id)
1388 {
1389 if (id == BT_ID_DEFAULT || id >= bt_dev.id_count) {
1390 return -EINVAL;
1391 }
1392
1393 if (bt_addr_le_eq(&bt_dev.id_addr[id], BT_ADDR_LE_ANY)) {
1394 return -EALREADY;
1395 }
1396
1397 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1398 struct bt_adv_id_check_data check_data = {
1399 .id = id,
1400 .adv_enabled = false,
1401 };
1402
1403 bt_le_ext_adv_foreach(adv_id_check_func, &check_data);
1404 if (check_data.adv_enabled) {
1405 return -EBUSY;
1406 }
1407 }
1408
1409 if (IS_ENABLED(CONFIG_BT_CONN)) {
1410 int err;
1411
1412 err = bt_unpair(id, NULL);
1413 if (err) {
1414 return err;
1415 }
1416 }
1417
1418 #if defined(CONFIG_BT_PRIVACY)
1419 (void)memset(bt_dev.irk[id], 0, 16);
1420 #endif
1421 bt_addr_le_copy(&bt_dev.id_addr[id], BT_ADDR_LE_ANY);
1422
1423 if (id == bt_dev.id_count - 1) {
1424 bt_dev.id_count--;
1425 }
1426
1427 if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
1428 atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1429 (void)bt_settings_store_id();
1430 (void)bt_settings_store_irk();
1431 }
1432
1433 return 0;
1434 }
1435
1436 #if defined(CONFIG_BT_PRIVACY)
bt_read_identity_root(uint8_t * ir)1437 static void bt_read_identity_root(uint8_t *ir)
1438 {
1439 /* Invalid IR */
1440 memset(ir, 0, 16);
1441
1442 #if defined(CONFIG_BT_HCI_VS_EXT)
1443 struct bt_hci_rp_vs_read_key_hierarchy_roots *rp;
1444 struct net_buf *rsp;
1445 int err;
1446
1447 if (!BT_VS_CMD_READ_KEY_ROOTS(bt_dev.vs_commands)) {
1448 return;
1449 }
1450
1451 err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_KEY_HIERARCHY_ROOTS, NULL,
1452 &rsp);
1453 if (err) {
1454 LOG_WRN("Failed to read identity root");
1455 return;
1456 }
1457
1458 if (IS_ENABLED(CONFIG_BT_HCI_VS_EXT_DETECT) &&
1459 rsp->len != sizeof(struct bt_hci_rp_vs_read_key_hierarchy_roots)) {
1460 LOG_WRN("Invalid Vendor HCI extensions");
1461 net_buf_unref(rsp);
1462 return;
1463 }
1464
1465 rp = (void *)rsp->data;
1466 memcpy(ir, rp->ir, 16);
1467
1468 net_buf_unref(rsp);
1469 #endif /* defined(CONFIG_BT_HCI_VS_EXT) */
1470 }
1471 #endif /* defined(CONFIG_BT_PRIVACY) */
1472
bt_id_read_public_addr(bt_addr_le_t * addr)1473 uint8_t bt_id_read_public_addr(bt_addr_le_t *addr)
1474 {
1475 struct bt_hci_rp_read_bd_addr *rp;
1476 struct net_buf *rsp;
1477 int err;
1478
1479 CHECKIF(addr == NULL) {
1480 LOG_WRN("Invalid input parameters");
1481 return 0U;
1482 }
1483
1484 /* Read Bluetooth Address */
1485 err = bt_hci_cmd_send_sync(BT_HCI_OP_READ_BD_ADDR, NULL, &rsp);
1486 if (err) {
1487 LOG_WRN("Failed to read public address");
1488 return 0U;
1489 }
1490
1491 rp = (void *)rsp->data;
1492
1493 if (bt_addr_eq(&rp->bdaddr, BT_ADDR_ANY) ||
1494 bt_addr_eq(&rp->bdaddr, BT_ADDR_NONE)) {
1495 LOG_DBG("Controller has no public address");
1496 net_buf_unref(rsp);
1497 return 0U;
1498 }
1499
1500 bt_addr_copy(&addr->a, &rp->bdaddr);
1501 addr->type = BT_ADDR_LE_PUBLIC;
1502
1503 net_buf_unref(rsp);
1504 return 1U;
1505 }
1506
bt_setup_public_id_addr(void)1507 int bt_setup_public_id_addr(void)
1508 {
1509 bt_addr_le_t addr;
1510 uint8_t *irk = NULL;
1511
1512 bt_dev.id_count = bt_id_read_public_addr(&addr);
1513
1514 if (!bt_dev.id_count) {
1515 return 0;
1516 }
1517
1518 #if defined(CONFIG_BT_PRIVACY)
1519 uint8_t ir_irk[16];
1520 uint8_t ir[16];
1521
1522 bt_read_identity_root(ir);
1523
1524 if (!IS_ENABLED(CONFIG_BT_PRIVACY_RANDOMIZE_IR)) {
1525 if (!bt_smp_irk_get(ir, ir_irk)) {
1526 irk = ir_irk;
1527 }
1528 }
1529 #endif /* defined(CONFIG_BT_PRIVACY) */
1530
1531 /* If true, `id_create` will randomize the IRK. */
1532 if (!irk && IS_ENABLED(CONFIG_BT_PRIVACY)) {
1533 /* `id_create` will not store the id when called before BT_DEV_READY.
1534 * But since part of the id will be randomized, it needs to be stored.
1535 */
1536 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1537 atomic_set_bit(bt_dev.flags, BT_DEV_STORE_ID);
1538 }
1539 }
1540
1541 return id_create(BT_ID_DEFAULT, &addr, irk);
1542 }
1543
1544 #if defined(CONFIG_BT_HCI_VS_EXT)
bt_read_static_addr(struct bt_hci_vs_static_addr addrs[],uint8_t size)1545 uint8_t bt_read_static_addr(struct bt_hci_vs_static_addr addrs[], uint8_t size)
1546 {
1547 struct bt_hci_rp_vs_read_static_addrs *rp;
1548 struct net_buf *rsp;
1549 int err, i;
1550 uint8_t cnt;
1551
1552 if (!BT_VS_CMD_READ_STATIC_ADDRS(bt_dev.vs_commands)) {
1553 LOG_WRN("Read Static Addresses command not available");
1554 return 0;
1555 }
1556
1557 err = bt_hci_cmd_send_sync(BT_HCI_OP_VS_READ_STATIC_ADDRS, NULL, &rsp);
1558 if (err) {
1559 LOG_WRN("Failed to read static addresses");
1560 return 0;
1561 }
1562
1563 if (IS_ENABLED(CONFIG_BT_HCI_VS_EXT_DETECT) &&
1564 rsp->len < sizeof(struct bt_hci_rp_vs_read_static_addrs)) {
1565 LOG_WRN("Invalid Vendor HCI extensions");
1566 net_buf_unref(rsp);
1567 return 0;
1568 }
1569
1570 rp = (void *)rsp->data;
1571 cnt = MIN(rp->num_addrs, size);
1572
1573 if (IS_ENABLED(CONFIG_BT_HCI_VS_EXT_DETECT) &&
1574 rsp->len != (sizeof(struct bt_hci_rp_vs_read_static_addrs) +
1575 rp->num_addrs *
1576 sizeof(struct bt_hci_vs_static_addr))) {
1577 LOG_WRN("Invalid Vendor HCI extensions");
1578 net_buf_unref(rsp);
1579 return 0;
1580 }
1581
1582 for (i = 0; i < cnt; i++) {
1583 memcpy(&addrs[i], &rp->a[i], sizeof(struct bt_hci_vs_static_addr));
1584 }
1585
1586 net_buf_unref(rsp);
1587 if (!cnt) {
1588 LOG_WRN("No static addresses stored in controller");
1589 }
1590
1591 return cnt;
1592 }
1593 #endif /* CONFIG_BT_HCI_VS_EXT */
1594
bt_setup_random_id_addr(void)1595 int bt_setup_random_id_addr(void)
1596 {
1597 #if defined(CONFIG_BT_HCI_VS_EXT) || defined(CONFIG_BT_CTLR)
1598 /* Only read the addresses if the user has not already configured one or
1599 * more identities (!bt_dev.id_count).
1600 */
1601 if (!bt_dev.id_count) {
1602 struct bt_hci_vs_static_addr addrs[CONFIG_BT_ID_MAX];
1603
1604 bt_dev.id_count = bt_read_static_addr(addrs, CONFIG_BT_ID_MAX);
1605
1606 if (bt_dev.id_count) {
1607 for (uint8_t i = 0; i < bt_dev.id_count; i++) {
1608 int err;
1609 bt_addr_le_t addr;
1610 uint8_t *irk = NULL;
1611 #if defined(CONFIG_BT_PRIVACY)
1612 uint8_t ir_irk[16];
1613
1614 if (!IS_ENABLED(CONFIG_BT_PRIVACY_RANDOMIZE_IR)) {
1615 if (!bt_smp_irk_get(addrs[i].ir, ir_irk)) {
1616 irk = ir_irk;
1617 }
1618 }
1619 #endif /* CONFIG_BT_PRIVACY */
1620
1621 /* If true, `id_create` will randomize the IRK. */
1622 if (!irk && IS_ENABLED(CONFIG_BT_PRIVACY)) {
1623 /* `id_create` will not store the id when called before
1624 * BT_DEV_READY. But since part of the id will be
1625 * randomized, it needs to be stored.
1626 */
1627 if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
1628 atomic_set_bit(bt_dev.flags, BT_DEV_STORE_ID);
1629 }
1630 }
1631
1632 bt_addr_copy(&addr.a, &addrs[i].bdaddr);
1633 addr.type = BT_ADDR_LE_RANDOM;
1634
1635 err = id_create(i, &addr, irk);
1636 if (err) {
1637 return err;
1638 }
1639 }
1640
1641 return 0;
1642 }
1643 }
1644 #endif /* defined(CONFIG_BT_HCI_VS_EXT) || defined(CONFIG_BT_CTLR) */
1645
1646 if (IS_ENABLED(CONFIG_BT_PRIVACY) && IS_ENABLED(CONFIG_BT_SETTINGS)) {
1647 atomic_set_bit(bt_dev.flags, BT_DEV_STORE_ID);
1648 }
1649
1650 return bt_id_create(NULL, NULL);
1651 }
1652
1653 #if defined(CONFIG_BT_CENTRAL)
rpa_timeout_valid_check(void)1654 static inline bool rpa_timeout_valid_check(void)
1655 {
1656 #if defined(CONFIG_BT_PRIVACY)
1657 uint32_t remaining_ms = k_ticks_to_ms_floor32(
1658 k_work_delayable_remaining_get(&bt_dev.rpa_update));
1659 /* Check if create conn timeout will happen before RPA timeout. */
1660 return remaining_ms > (10 * bt_dev.create_param.timeout);
1661 #else
1662 return true;
1663 #endif
1664 }
1665
bt_id_set_create_conn_own_addr(bool use_filter,uint8_t * own_addr_type)1666 int bt_id_set_create_conn_own_addr(bool use_filter, uint8_t *own_addr_type)
1667 {
1668 int err;
1669
1670 CHECKIF(own_addr_type == NULL) {
1671 return -EINVAL;
1672 }
1673
1674 if (IS_ENABLED(CONFIG_BT_PRIVACY)) {
1675 if (use_filter || rpa_timeout_valid_check()) {
1676 err = bt_id_set_private_addr(BT_ID_DEFAULT);
1677 if (err) {
1678 return err;
1679 }
1680 } else {
1681 /* Force new RPA timeout so that RPA timeout is not
1682 * triggered while direct initiator is active.
1683 */
1684 le_force_rpa_timeout();
1685 }
1686
1687 if (BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {
1688 *own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM;
1689 } else {
1690 *own_addr_type = BT_ADDR_LE_RANDOM;
1691 }
1692 } else {
1693 const bt_addr_le_t *addr = &bt_dev.id_addr[BT_ID_DEFAULT];
1694
1695 /* If Static Random address is used as Identity address we
1696 * need to restore it before creating connection. Otherwise
1697 * NRPA used for active scan could be used for connection.
1698 */
1699 if (addr->type == BT_ADDR_LE_RANDOM) {
1700 err = set_random_address(&addr->a);
1701 if (err) {
1702 return err;
1703 }
1704 }
1705
1706 *own_addr_type = addr->type;
1707 }
1708
1709 return 0;
1710 }
1711 #endif /* defined(CONFIG_BT_CENTRAL) */
1712
1713 #if defined(CONFIG_BT_OBSERVER)
is_adv_using_rand_addr(void)1714 static bool is_adv_using_rand_addr(void)
1715 {
1716 struct bt_le_ext_adv *adv;
1717
1718 if (!IS_ENABLED(CONFIG_BT_BROADCASTER) ||
1719 (IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1720 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
1721 /* When advertising is not enabled or is using extended
1722 * advertising HCI commands then only the scanner uses the set
1723 * random address command.
1724 */
1725 return false;
1726 }
1727
1728 adv = bt_le_adv_lookup_legacy();
1729
1730 return adv && atomic_test_bit(adv->flags, BT_ADV_ENABLED);
1731 }
1732
bt_id_set_scan_own_addr(bool active_scan,uint8_t * own_addr_type)1733 int bt_id_set_scan_own_addr(bool active_scan, uint8_t *own_addr_type)
1734 {
1735 int err;
1736
1737 CHECKIF(own_addr_type == NULL) {
1738 return -EINVAL;
1739 }
1740
1741 if (IS_ENABLED(CONFIG_BT_PRIVACY)) {
1742 err = bt_id_set_private_addr(BT_ID_DEFAULT);
1743 if (err) {
1744 return err;
1745 }
1746
1747 if (BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {
1748 *own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM;
1749 } else {
1750 *own_addr_type = BT_ADDR_LE_RANDOM;
1751 }
1752 } else {
1753 *own_addr_type = bt_dev.id_addr[0].type;
1754
1755 /* Use NRPA unless identity has been explicitly requested
1756 * (through Kconfig).
1757 * Use same RPA as legacy advertiser if advertising.
1758 */
1759 if (!IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
1760 !is_adv_using_rand_addr()) {
1761 err = bt_id_set_private_addr(BT_ID_DEFAULT);
1762 if (err) {
1763 if (active_scan || !is_adv_using_rand_addr()) {
1764 return err;
1765 }
1766
1767 LOG_WRN("Ignoring failure to set address for passive scan (%d)",
1768 err);
1769 }
1770
1771 *own_addr_type = BT_ADDR_LE_RANDOM;
1772 } else if (IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
1773 *own_addr_type == BT_ADDR_LE_RANDOM) {
1774 /* If scanning with Identity Address we must set the
1775 * random identity address for both active and passive
1776 * scanner in order to receive adv reports that are
1777 * directed towards this identity.
1778 */
1779 err = set_random_address(&bt_dev.id_addr[0].a);
1780 if (err) {
1781 return err;
1782 }
1783 } else {
1784 LOG_DBG("Not changing the address");
1785 }
1786 }
1787
1788 return 0;
1789 }
1790 #endif /* defined(CONFIG_BT_OBSERVER) */
1791
bt_id_set_adv_own_addr(struct bt_le_ext_adv * adv,uint32_t options,bool dir_adv,uint8_t * own_addr_type)1792 int bt_id_set_adv_own_addr(struct bt_le_ext_adv *adv, uint32_t options,
1793 bool dir_adv, uint8_t *own_addr_type)
1794 {
1795 const bt_addr_le_t *id_addr;
1796 int err = 0;
1797
1798 CHECKIF(adv == NULL || own_addr_type == NULL) {
1799 return -EINVAL;
1800 }
1801
1802 /* Set which local identity address we're advertising with */
1803 id_addr = &bt_dev.id_addr[adv->id];
1804
1805 /* Short-circuit to force NRPA usage */
1806 if (options & BT_LE_ADV_OPT_USE_NRPA) {
1807 if (options & BT_LE_ADV_OPT_USE_IDENTITY) {
1808 LOG_ERR("Can't set both IDENTITY & NRPA");
1809
1810 return -EINVAL;
1811 }
1812
1813 err = bt_id_set_adv_private_addr(adv);
1814 if (err) {
1815 return err;
1816 }
1817 *own_addr_type = BT_ADDR_LE_RANDOM;
1818
1819 return 0;
1820 }
1821
1822 if (options & BT_LE_ADV_OPT_CONNECTABLE) {
1823 if (dir_adv && (options & BT_LE_ADV_OPT_DIR_ADDR_RPA) &&
1824 !BT_FEAT_LE_PRIVACY(bt_dev.le.features)) {
1825 return -ENOTSUP;
1826 }
1827
1828 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1829 !(options & BT_LE_ADV_OPT_USE_IDENTITY)) {
1830 err = bt_id_set_adv_private_addr(adv);
1831 if (err) {
1832 return err;
1833 }
1834
1835 if (dir_adv && (options & BT_LE_ADV_OPT_DIR_ADDR_RPA)) {
1836 *own_addr_type = BT_HCI_OWN_ADDR_RPA_OR_RANDOM;
1837 } else {
1838 *own_addr_type = BT_ADDR_LE_RANDOM;
1839 }
1840 } else {
1841 /*
1842 * If Static Random address is used as Identity
1843 * address we need to restore it before advertising
1844 * is enabled. Otherwise NRPA used for active scan
1845 * could be used for advertising.
1846 */
1847 if (id_addr->type == BT_ADDR_LE_RANDOM) {
1848 err = bt_id_set_adv_random_addr(adv, &id_addr->a);
1849 if (err) {
1850 return err;
1851 }
1852 }
1853
1854 *own_addr_type = id_addr->type;
1855
1856 if (dir_adv && (options & BT_LE_ADV_OPT_DIR_ADDR_RPA)) {
1857 *own_addr_type |= BT_HCI_OWN_ADDR_RPA_MASK;
1858 }
1859 }
1860 } else {
1861 if (options & BT_LE_ADV_OPT_USE_IDENTITY) {
1862 if (id_addr->type == BT_ADDR_LE_RANDOM) {
1863 err = bt_id_set_adv_random_addr(adv, &id_addr->a);
1864 }
1865
1866 *own_addr_type = id_addr->type;
1867 } else if (!(IS_ENABLED(CONFIG_BT_EXT_ADV) &&
1868 BT_DEV_FEAT_LE_EXT_ADV(bt_dev.le.features))) {
1869 /* In case advertising set random address is not
1870 * available we must handle the shared random address
1871 * problem.
1872 */
1873 #if defined(CONFIG_BT_OBSERVER)
1874 bool scan_enabled = false;
1875
1876 /* If active scan with NRPA is ongoing refresh NRPA */
1877 if (!IS_ENABLED(CONFIG_BT_PRIVACY) &&
1878 !IS_ENABLED(CONFIG_BT_SCAN_WITH_IDENTITY) &&
1879 atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING)) {
1880 scan_enabled = true;
1881 bt_le_scan_set_enable(BT_HCI_LE_SCAN_DISABLE);
1882 }
1883 #endif /* defined(CONFIG_BT_OBSERVER) */
1884 err = bt_id_set_adv_private_addr(adv);
1885 *own_addr_type = BT_ADDR_LE_RANDOM;
1886
1887 #if defined(CONFIG_BT_OBSERVER)
1888 if (scan_enabled) {
1889 bt_le_scan_set_enable(BT_HCI_LE_SCAN_ENABLE);
1890 }
1891 #endif /* defined(CONFIG_BT_OBSERVER) */
1892 } else {
1893 err = bt_id_set_adv_private_addr(adv);
1894 *own_addr_type = BT_ADDR_LE_RANDOM;
1895 }
1896
1897 if (err) {
1898 return err;
1899 }
1900 }
1901
1902 return 0;
1903 }
1904
1905 #if defined(CONFIG_BT_BREDR)
bt_br_oob_get_local(struct bt_br_oob * oob)1906 int bt_br_oob_get_local(struct bt_br_oob *oob)
1907 {
1908 CHECKIF(oob == NULL) {
1909 return -EINVAL;
1910 }
1911
1912 bt_addr_copy(&oob->addr, &bt_dev.id_addr[0].a);
1913
1914 return 0;
1915 }
1916 #endif /* CONFIG_BT_BREDR */
1917
bt_le_oob_get_local(uint8_t id,struct bt_le_oob * oob)1918 int bt_le_oob_get_local(uint8_t id, struct bt_le_oob *oob)
1919 {
1920 struct bt_le_ext_adv *adv = NULL;
1921 int err;
1922
1923 CHECKIF(oob == NULL) {
1924 return -EINVAL;
1925 }
1926
1927 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
1928 return -EAGAIN;
1929 }
1930
1931 if (id >= CONFIG_BT_ID_MAX) {
1932 return -EINVAL;
1933 }
1934
1935 if (IS_ENABLED(CONFIG_BT_BROADCASTER)) {
1936 adv = bt_le_adv_lookup_legacy();
1937 }
1938
1939 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
1940 !(adv && adv->id == id &&
1941 atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
1942 atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) &&
1943 bt_dev.id_addr[id].type == BT_ADDR_LE_RANDOM)) {
1944 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
1945 atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
1946 struct bt_conn *conn;
1947
1948 conn = bt_conn_lookup_state_le(BT_ID_DEFAULT, NULL,
1949 BT_CONN_CONNECTING_SCAN);
1950 if (conn) {
1951 /* Cannot set new RPA while creating
1952 * connections.
1953 */
1954 bt_conn_unref(conn);
1955 return -EINVAL;
1956 }
1957 }
1958
1959 if (adv &&
1960 atomic_test_bit(adv->flags, BT_ADV_ENABLED) &&
1961 atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY) &&
1962 (bt_dev.id_addr[id].type == BT_ADDR_LE_RANDOM)) {
1963 /* Cannot set a new RPA address while advertising with
1964 * random static identity address for a different
1965 * identity.
1966 */
1967 return -EINVAL;
1968 }
1969
1970 if (IS_ENABLED(CONFIG_BT_OBSERVER) &&
1971 CONFIG_BT_ID_MAX > 1 &&
1972 id != BT_ID_DEFAULT &&
1973 (atomic_test_bit(bt_dev.flags, BT_DEV_SCANNING) ||
1974 atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING))) {
1975 /* Cannot switch identity of scanner or initiator */
1976 return -EINVAL;
1977 }
1978
1979 le_force_rpa_timeout();
1980
1981 bt_addr_le_copy(&oob->addr, &bt_dev.random_addr);
1982 } else {
1983 bt_addr_le_copy(&oob->addr, &bt_dev.id_addr[id]);
1984 }
1985
1986 if (IS_ENABLED(CONFIG_BT_SMP)) {
1987 err = bt_smp_le_oob_generate_sc_data(&oob->le_sc_data);
1988 if (err && err != -ENOTSUP) {
1989 return err;
1990 }
1991 }
1992
1993 return 0;
1994 }
1995
1996 #if defined(CONFIG_BT_EXT_ADV)
bt_le_ext_adv_oob_get_local(struct bt_le_ext_adv * adv,struct bt_le_oob * oob)1997 int bt_le_ext_adv_oob_get_local(struct bt_le_ext_adv *adv,
1998 struct bt_le_oob *oob)
1999 {
2000 int err;
2001
2002 CHECKIF(adv == NULL || oob == NULL) {
2003 return -EINVAL;
2004 }
2005
2006 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2007 return -EAGAIN;
2008 }
2009
2010 if (IS_ENABLED(CONFIG_BT_PRIVACY) &&
2011 !atomic_test_bit(adv->flags, BT_ADV_USE_IDENTITY)) {
2012 /* Don't refresh RPA addresses if the RPA is new.
2013 * This allows back to back calls to this function or
2014 * bt_le_oob_get_local to not invalidate the previously set
2015 * RPAs.
2016 */
2017 if (!atomic_test_bit(adv->flags, BT_ADV_LIMITED) &&
2018 !bt_id_rpa_is_new()) {
2019 if (IS_ENABLED(CONFIG_BT_CENTRAL) &&
2020 atomic_test_bit(bt_dev.flags, BT_DEV_INITIATING)) {
2021 struct bt_conn *conn;
2022
2023 conn = bt_conn_lookup_state_le(
2024 BT_ID_DEFAULT, NULL,
2025 BT_CONN_CONNECTING_SCAN);
2026
2027 if (conn) {
2028 /* Cannot set new RPA while creating
2029 * connections.
2030 */
2031 bt_conn_unref(conn);
2032 return -EINVAL;
2033 }
2034 }
2035
2036 le_force_rpa_timeout();
2037 }
2038
2039 bt_addr_le_copy(&oob->addr, &adv->random_addr);
2040 } else {
2041 bt_addr_le_copy(&oob->addr, &bt_dev.id_addr[adv->id]);
2042 }
2043
2044 if (IS_ENABLED(CONFIG_BT_SMP)) {
2045 err = bt_smp_le_oob_generate_sc_data(&oob->le_sc_data);
2046 if (err && err != -ENOTSUP) {
2047 return err;
2048 }
2049 }
2050
2051 return 0;
2052 }
2053 #endif /* defined(CONFIG_BT_EXT_ADV) */
2054
2055 #if defined(CONFIG_BT_SMP)
2056 #if !defined(CONFIG_BT_SMP_SC_PAIR_ONLY)
bt_le_oob_set_legacy_tk(struct bt_conn * conn,const uint8_t * tk)2057 int bt_le_oob_set_legacy_tk(struct bt_conn *conn, const uint8_t *tk)
2058 {
2059 CHECKIF(conn == NULL || tk == NULL) {
2060 return -EINVAL;
2061 }
2062
2063 return bt_smp_le_oob_set_tk(conn, tk);
2064 }
2065 #endif /* !defined(CONFIG_BT_SMP_SC_PAIR_ONLY) */
2066
2067 #if !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY)
bt_le_oob_set_sc_data(struct bt_conn * conn,const struct bt_le_oob_sc_data * oobd_local,const struct bt_le_oob_sc_data * oobd_remote)2068 int bt_le_oob_set_sc_data(struct bt_conn *conn,
2069 const struct bt_le_oob_sc_data *oobd_local,
2070 const struct bt_le_oob_sc_data *oobd_remote)
2071 {
2072 CHECKIF(conn == NULL) {
2073 return -EINVAL;
2074 }
2075
2076 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2077 return -EAGAIN;
2078 }
2079
2080 return bt_smp_le_oob_set_sc_data(conn, oobd_local, oobd_remote);
2081 }
2082
bt_le_oob_get_sc_data(struct bt_conn * conn,const struct bt_le_oob_sc_data ** oobd_local,const struct bt_le_oob_sc_data ** oobd_remote)2083 int bt_le_oob_get_sc_data(struct bt_conn *conn,
2084 const struct bt_le_oob_sc_data **oobd_local,
2085 const struct bt_le_oob_sc_data **oobd_remote)
2086 {
2087 CHECKIF(conn == NULL) {
2088 return -EINVAL;
2089 }
2090
2091 if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
2092 return -EAGAIN;
2093 }
2094
2095 return bt_smp_le_oob_get_sc_data(conn, oobd_local, oobd_remote);
2096 }
2097 #endif /* !defined(CONFIG_BT_SMP_OOB_LEGACY_PAIR_ONLY) */
2098 #endif /* defined(CONFIG_BT_SMP) */
2099
bt_id_init(void)2100 int bt_id_init(void)
2101 {
2102 int err;
2103
2104 #if defined(CONFIG_BT_PRIVACY)
2105 k_work_init_delayable(&bt_dev.rpa_update, rpa_timeout);
2106 #if defined(CONFIG_BT_RPA_SHARING)
2107 for (uint8_t id = 0U; id < ARRAY_SIZE(bt_dev.rpa); id++) {
2108 bt_addr_copy(&bt_dev.rpa[id], BT_ADDR_NONE);
2109 }
2110 #endif
2111 #endif
2112
2113 if (!IS_ENABLED(CONFIG_BT_SETTINGS) && !bt_dev.id_count) {
2114 LOG_DBG("No user identity. Trying to set public.");
2115
2116 err = bt_setup_public_id_addr();
2117 if (err) {
2118 LOG_ERR("Unable to set identity address");
2119 return err;
2120 }
2121 }
2122
2123 if (!IS_ENABLED(CONFIG_BT_SETTINGS) && !bt_dev.id_count) {
2124 LOG_DBG("No public address. Trying to set static random.");
2125
2126 err = bt_setup_random_id_addr();
2127 if (err) {
2128 LOG_ERR("Unable to set identity address");
2129 return err;
2130 }
2131
2132 /* The passive scanner just sends a dummy address type in the
2133 * command. If the first activity does this, and the dummy type
2134 * is a random address, it needs a valid value, even though it's
2135 * not actually used.
2136 */
2137 err = set_random_address(&bt_dev.id_addr[0].a);
2138 if (err) {
2139 LOG_ERR("Unable to set random address");
2140 return err;
2141 }
2142 }
2143
2144 return 0;
2145 }
2146