Skip to content

Commit 9713c17

Browse files
holtmannjhedberg
authored andcommitted
Bluetooth: Add support for changing the public device address
This adds support for changing the public device address. This feature is required by controllers that do not provide a public address and have HCI_QUIRK_INVALID_BDADDR set. Even if a controller has a public device address, this is useful when an embedded system wants to use its own value. As long as the driver provides the set_bdaddr callback, this allows changing the device address before powering on the controller. Signed-off-by: Marcel Holtmann <marcel@holtmann.org> Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
1 parent d603b76 commit 9713c17

3 files changed

Lines changed: 70 additions & 7 deletions

File tree

‎include/net/bluetooth/mgmt.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ struct mgmt_cp_set_external_config {
489489
} __packed;
490490
#define MGMT_SET_EXTERNAL_CONFIG_SIZE 1
491491

492+
#define MGMT_OP_SET_PUBLIC_ADDRESS 0x0039
493+
struct mgmt_cp_set_public_address {
494+
bdaddr_t bdaddr;
495+
} __packed;
496+
#define MGMT_SET_PUBLIC_ADDRESS_SIZE 6
497+
492498
#define MGMT_EV_CMD_COMPLETE 0x0001
493499
struct mgmt_ev_cmd_complete {
494500
__le16 opcode;

‎net/bluetooth/hci_core.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,12 +2302,14 @@ static int hci_dev_do_open(struct hci_dev *hdev)
23022302
ret = __hci_unconf_init(hdev);
23032303
}
23042304

2305-
/* If public address change is configured, ensure that the
2306-
* address gets programmed. If the driver does not support
2307-
* changing the public address, fail the power on procedure.
2308-
*/
2309-
if (!ret && bacmp(&hdev->public_addr, BDADDR_ANY)) {
2310-
if (hdev->set_bdaddr)
2305+
if (test_bit(HCI_CONFIG, &hdev->dev_flags)) {
2306+
/* If public address change is configured, ensure that
2307+
* the address gets programmed. If the driver does not
2308+
* support changing the public address, fail the power
2309+
* on procedure.
2310+
*/
2311+
if (bacmp(&hdev->public_addr, BDADDR_ANY) &&
2312+
hdev->set_bdaddr)
23112313
ret = hdev->set_bdaddr(hdev, &hdev->public_addr);
23122314
else
23132315
ret = -EADDRNOTAVAIL;

‎net/bluetooth/mgmt.c‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static const u16 mgmt_commands[] = {
9292
MGMT_OP_READ_UNCONF_INDEX_LIST,
9393
MGMT_OP_READ_CONFIG_INFO,
9494
MGMT_OP_SET_EXTERNAL_CONFIG,
95+
MGMT_OP_SET_PUBLIC_ADDRESS,
9596
};
9697

9798
static const u16 mgmt_events[] = {
@@ -5459,6 +5460,58 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
54595460
return err;
54605461
}
54615462

5463+
static int set_public_address(struct sock *sk, struct hci_dev *hdev,
5464+
void *data, u16 len)
5465+
{
5466+
struct mgmt_cp_set_public_address *cp = data;
5467+
bool changed;
5468+
int err;
5469+
5470+
BT_DBG("%s", hdev->name);
5471+
5472+
if (hdev_is_powered(hdev))
5473+
return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5474+
MGMT_STATUS_REJECTED);
5475+
5476+
if (!bacmp(&cp->bdaddr, BDADDR_ANY))
5477+
return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5478+
MGMT_STATUS_INVALID_PARAMS);
5479+
5480+
if (!hdev->set_bdaddr)
5481+
return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
5482+
MGMT_STATUS_NOT_SUPPORTED);
5483+
5484+
hci_dev_lock(hdev);
5485+
5486+
changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
5487+
bacpy(&hdev->public_addr, &cp->bdaddr);
5488+
5489+
err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
5490+
if (err < 0)
5491+
goto unlock;
5492+
5493+
if (!changed)
5494+
goto unlock;
5495+
5496+
if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags))
5497+
err = new_options(hdev, sk);
5498+
5499+
if (is_configured(hdev)) {
5500+
mgmt_index_removed(hdev);
5501+
5502+
clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags);
5503+
5504+
set_bit(HCI_CONFIG, &hdev->dev_flags);
5505+
set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
5506+
5507+
queue_work(hdev->req_workqueue, &hdev->power_on);
5508+
}
5509+
5510+
unlock:
5511+
hci_dev_unlock(hdev);
5512+
return err;
5513+
}
5514+
54625515
static const struct mgmt_handler {
54635516
int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
54645517
u16 data_len);
@@ -5522,6 +5575,7 @@ static const struct mgmt_handler {
55225575
{ read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE },
55235576
{ read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE },
55245577
{ set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE },
5578+
{ set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE },
55255579
};
55265580

55275581
int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
@@ -5576,7 +5630,8 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
55765630

55775631
if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) &&
55785632
opcode != MGMT_OP_READ_CONFIG_INFO &&
5579-
opcode != MGMT_OP_SET_EXTERNAL_CONFIG) {
5633+
opcode != MGMT_OP_SET_EXTERNAL_CONFIG &&
5634+
opcode != MGMT_OP_SET_PUBLIC_ADDRESS) {
55805635
err = cmd_status(sk, index, opcode,
55815636
MGMT_STATUS_INVALID_INDEX);
55825637
goto done;

0 commit comments

Comments
 (0)