Skip to content

Commit 3fe318e

Browse files
bgixVudentz
authored andcommitted
Bluetooth: move hci_get_random_address() to hci_sync
This function has no dependencies on the deprecated hci_request mechanism, so has been moved unchanged to hci_sync.c Signed-off-by: Brian Gix <brian.gix@intel.com> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
1 parent dd50a86 commit 3fe318e

File tree

4 files changed

+170
-172
lines changed

4 files changed

+170
-172
lines changed

include/net/bluetooth/hci_sync.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct hci_cmd_sync_work_entry {
1616
hci_cmd_sync_work_destroy_t destroy;
1717
};
1818

19+
struct adv_info;
1920
/* Function with sync suffix shall not be called with hdev->lock held as they
2021
* wait the command to complete and in the meantime an event could be received
2122
* which could attempt to acquire hdev->lock causing a deadlock.
@@ -51,6 +52,10 @@ int hci_update_class_sync(struct hci_dev *hdev);
5152
int hci_update_name_sync(struct hci_dev *hdev);
5253
int hci_write_ssp_mode_sync(struct hci_dev *hdev, u8 mode);
5354

55+
int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
56+
bool use_rpa, struct adv_info *adv_instance,
57+
u8 *own_addr_type, bdaddr_t *rand_addr);
58+
5459
int hci_update_random_address_sync(struct hci_dev *hdev, bool require_privacy,
5560
bool rpa, u8 *own_addr_type);
5661

net/bluetooth/hci_request.c

Lines changed: 92 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,96 @@ static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
650650
return false;
651651
}
652652

653+
static void set_random_addr(struct hci_request *req, bdaddr_t *rpa);
654+
static int hci_update_random_address(struct hci_request *req,
655+
bool require_privacy, bool use_rpa,
656+
u8 *own_addr_type)
657+
{
658+
struct hci_dev *hdev = req->hdev;
659+
int err;
660+
661+
/* If privacy is enabled use a resolvable private address. If
662+
* current RPA has expired or there is something else than
663+
* the current RPA in use, then generate a new one.
664+
*/
665+
if (use_rpa) {
666+
/* If Controller supports LL Privacy use own address type is
667+
* 0x03
668+
*/
669+
if (use_ll_privacy(hdev))
670+
*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
671+
else
672+
*own_addr_type = ADDR_LE_DEV_RANDOM;
673+
674+
if (rpa_valid(hdev))
675+
return 0;
676+
677+
err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
678+
if (err < 0) {
679+
bt_dev_err(hdev, "failed to generate new RPA");
680+
return err;
681+
}
682+
683+
set_random_addr(req, &hdev->rpa);
684+
685+
return 0;
686+
}
687+
688+
/* In case of required privacy without resolvable private address,
689+
* use an non-resolvable private address. This is useful for active
690+
* scanning and non-connectable advertising.
691+
*/
692+
if (require_privacy) {
693+
bdaddr_t nrpa;
694+
695+
while (true) {
696+
/* The non-resolvable private address is generated
697+
* from random six bytes with the two most significant
698+
* bits cleared.
699+
*/
700+
get_random_bytes(&nrpa, 6);
701+
nrpa.b[5] &= 0x3f;
702+
703+
/* The non-resolvable private address shall not be
704+
* equal to the public address.
705+
*/
706+
if (bacmp(&hdev->bdaddr, &nrpa))
707+
break;
708+
}
709+
710+
*own_addr_type = ADDR_LE_DEV_RANDOM;
711+
set_random_addr(req, &nrpa);
712+
return 0;
713+
}
714+
715+
/* If forcing static address is in use or there is no public
716+
* address use the static address as random address (but skip
717+
* the HCI command if the current random address is already the
718+
* static one.
719+
*
720+
* In case BR/EDR has been disabled on a dual-mode controller
721+
* and a static address has been configured, then use that
722+
* address instead of the public BR/EDR address.
723+
*/
724+
if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
725+
!bacmp(&hdev->bdaddr, BDADDR_ANY) ||
726+
(!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
727+
bacmp(&hdev->static_addr, BDADDR_ANY))) {
728+
*own_addr_type = ADDR_LE_DEV_RANDOM;
729+
if (bacmp(&hdev->static_addr, &hdev->random_addr))
730+
hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
731+
&hdev->static_addr);
732+
return 0;
733+
}
734+
735+
/* Neither privacy nor static address is being used so use a
736+
* public address.
737+
*/
738+
*own_addr_type = ADDR_LE_DEV_PUBLIC;
739+
740+
return 0;
741+
}
742+
653743
/* Ensure to call hci_req_add_le_scan_disable() first to disable the
654744
* controller based address resolution to be able to reconfigure
655745
* resolving list.
@@ -859,79 +949,6 @@ static void interleave_scan_work(struct work_struct *work)
859949
&hdev->interleave_scan, timeout);
860950
}
861951

862-
int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
863-
bool use_rpa, struct adv_info *adv_instance,
864-
u8 *own_addr_type, bdaddr_t *rand_addr)
865-
{
866-
int err;
867-
868-
bacpy(rand_addr, BDADDR_ANY);
869-
870-
/* If privacy is enabled use a resolvable private address. If
871-
* current RPA has expired then generate a new one.
872-
*/
873-
if (use_rpa) {
874-
/* If Controller supports LL Privacy use own address type is
875-
* 0x03
876-
*/
877-
if (use_ll_privacy(hdev))
878-
*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
879-
else
880-
*own_addr_type = ADDR_LE_DEV_RANDOM;
881-
882-
if (adv_instance) {
883-
if (adv_rpa_valid(adv_instance))
884-
return 0;
885-
} else {
886-
if (rpa_valid(hdev))
887-
return 0;
888-
}
889-
890-
err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
891-
if (err < 0) {
892-
bt_dev_err(hdev, "failed to generate new RPA");
893-
return err;
894-
}
895-
896-
bacpy(rand_addr, &hdev->rpa);
897-
898-
return 0;
899-
}
900-
901-
/* In case of required privacy without resolvable private address,
902-
* use an non-resolvable private address. This is useful for
903-
* non-connectable advertising.
904-
*/
905-
if (require_privacy) {
906-
bdaddr_t nrpa;
907-
908-
while (true) {
909-
/* The non-resolvable private address is generated
910-
* from random six bytes with the two most significant
911-
* bits cleared.
912-
*/
913-
get_random_bytes(&nrpa, 6);
914-
nrpa.b[5] &= 0x3f;
915-
916-
/* The non-resolvable private address shall not be
917-
* equal to the public address.
918-
*/
919-
if (bacmp(&hdev->bdaddr, &nrpa))
920-
break;
921-
}
922-
923-
*own_addr_type = ADDR_LE_DEV_RANDOM;
924-
bacpy(rand_addr, &nrpa);
925-
926-
return 0;
927-
}
928-
929-
/* No privacy so use a public address. */
930-
*own_addr_type = ADDR_LE_DEV_PUBLIC;
931-
932-
return 0;
933-
}
934-
935952
static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
936953
{
937954
struct hci_dev *hdev = req->hdev;
@@ -956,96 +973,8 @@ static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
956973
hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
957974
}
958975

959-
int hci_update_random_address(struct hci_request *req, bool require_privacy,
960-
bool use_rpa, u8 *own_addr_type)
961-
{
962-
struct hci_dev *hdev = req->hdev;
963-
int err;
964-
965-
/* If privacy is enabled use a resolvable private address. If
966-
* current RPA has expired or there is something else than
967-
* the current RPA in use, then generate a new one.
968-
*/
969-
if (use_rpa) {
970-
/* If Controller supports LL Privacy use own address type is
971-
* 0x03
972-
*/
973-
if (use_ll_privacy(hdev))
974-
*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
975-
else
976-
*own_addr_type = ADDR_LE_DEV_RANDOM;
977-
978-
if (rpa_valid(hdev))
979-
return 0;
980-
981-
err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
982-
if (err < 0) {
983-
bt_dev_err(hdev, "failed to generate new RPA");
984-
return err;
985-
}
986-
987-
set_random_addr(req, &hdev->rpa);
988-
989-
return 0;
990-
}
991-
992-
/* In case of required privacy without resolvable private address,
993-
* use an non-resolvable private address. This is useful for active
994-
* scanning and non-connectable advertising.
995-
*/
996-
if (require_privacy) {
997-
bdaddr_t nrpa;
998-
999-
while (true) {
1000-
/* The non-resolvable private address is generated
1001-
* from random six bytes with the two most significant
1002-
* bits cleared.
1003-
*/
1004-
get_random_bytes(&nrpa, 6);
1005-
nrpa.b[5] &= 0x3f;
1006-
1007-
/* The non-resolvable private address shall not be
1008-
* equal to the public address.
1009-
*/
1010-
if (bacmp(&hdev->bdaddr, &nrpa))
1011-
break;
1012-
}
1013-
1014-
*own_addr_type = ADDR_LE_DEV_RANDOM;
1015-
set_random_addr(req, &nrpa);
1016-
return 0;
1017-
}
1018-
1019-
/* If forcing static address is in use or there is no public
1020-
* address use the static address as random address (but skip
1021-
* the HCI command if the current random address is already the
1022-
* static one.
1023-
*
1024-
* In case BR/EDR has been disabled on a dual-mode controller
1025-
* and a static address has been configured, then use that
1026-
* address instead of the public BR/EDR address.
1027-
*/
1028-
if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
1029-
!bacmp(&hdev->bdaddr, BDADDR_ANY) ||
1030-
(!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
1031-
bacmp(&hdev->static_addr, BDADDR_ANY))) {
1032-
*own_addr_type = ADDR_LE_DEV_RANDOM;
1033-
if (bacmp(&hdev->static_addr, &hdev->random_addr))
1034-
hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
1035-
&hdev->static_addr);
1036-
return 0;
1037-
}
1038-
1039-
/* Neither privacy nor static address is being used so use a
1040-
* public address.
1041-
*/
1042-
*own_addr_type = ADDR_LE_DEV_PUBLIC;
1043-
1044-
return 0;
1045-
}
1046-
1047-
void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
1048-
u8 reason)
976+
static void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
977+
u8 reason)
1049978
{
1050979
switch (conn->state) {
1051980
case BT_CONNECTED:

net/bluetooth/hci_request.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,7 @@ void hci_req_add_le_passive_scan(struct hci_request *req);
7474
void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next);
7575

7676
int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance);
77-
int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
78-
bool use_rpa, struct adv_info *adv_instance,
79-
u8 *own_addr_type, bdaddr_t *rand_addr);
80-
81-
int hci_update_random_address(struct hci_request *req, bool require_privacy,
82-
bool use_rpa, u8 *own_addr_type);
8377

8478
int hci_abort_conn(struct hci_conn *conn, u8 reason);
85-
void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
86-
u8 reason);
87-
8879
void hci_request_setup(struct hci_dev *hdev);
8980
void hci_request_cancel_all(struct hci_dev *hdev);

net/bluetooth/hci_sync.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5992,3 +5992,76 @@ int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle)
59925992
return __hci_cmd_sync_status(hdev, HCI_OP_LE_PA_TERM_SYNC,
59935993
sizeof(cp), &cp, HCI_CMD_TIMEOUT);
59945994
}
5995+
5996+
int hci_get_random_address(struct hci_dev *hdev, bool require_privacy,
5997+
bool use_rpa, struct adv_info *adv_instance,
5998+
u8 *own_addr_type, bdaddr_t *rand_addr)
5999+
{
6000+
int err;
6001+
6002+
bacpy(rand_addr, BDADDR_ANY);
6003+
6004+
/* If privacy is enabled use a resolvable private address. If
6005+
* current RPA has expired then generate a new one.
6006+
*/
6007+
if (use_rpa) {
6008+
/* If Controller supports LL Privacy use own address type is
6009+
* 0x03
6010+
*/
6011+
if (use_ll_privacy(hdev))
6012+
*own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
6013+
else
6014+
*own_addr_type = ADDR_LE_DEV_RANDOM;
6015+
6016+
if (adv_instance) {
6017+
if (adv_rpa_valid(adv_instance))
6018+
return 0;
6019+
} else {
6020+
if (rpa_valid(hdev))
6021+
return 0;
6022+
}
6023+
6024+
err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
6025+
if (err < 0) {
6026+
bt_dev_err(hdev, "failed to generate new RPA");
6027+
return err;
6028+
}
6029+
6030+
bacpy(rand_addr, &hdev->rpa);
6031+
6032+
return 0;
6033+
}
6034+
6035+
/* In case of required privacy without resolvable private address,
6036+
* use an non-resolvable private address. This is useful for
6037+
* non-connectable advertising.
6038+
*/
6039+
if (require_privacy) {
6040+
bdaddr_t nrpa;
6041+
6042+
while (true) {
6043+
/* The non-resolvable private address is generated
6044+
* from random six bytes with the two most significant
6045+
* bits cleared.
6046+
*/
6047+
get_random_bytes(&nrpa, 6);
6048+
nrpa.b[5] &= 0x3f;
6049+
6050+
/* The non-resolvable private address shall not be
6051+
* equal to the public address.
6052+
*/
6053+
if (bacmp(&hdev->bdaddr, &nrpa))
6054+
break;
6055+
}
6056+
6057+
*own_addr_type = ADDR_LE_DEV_RANDOM;
6058+
bacpy(rand_addr, &nrpa);
6059+
6060+
return 0;
6061+
}
6062+
6063+
/* No privacy so use a public address. */
6064+
*own_addr_type = ADDR_LE_DEV_PUBLIC;
6065+
6066+
return 0;
6067+
}

0 commit comments

Comments
 (0)