Skip to contentSkip to conversation

Bluetooth: mgmt: Fix powered checks for commands

PATCH In review [email protected] 2 participants 19 messages Feb 21, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/9 Bluetooth: mgmt: Fix powered checks for commands

From: Johan Hedberg <[email protected]>

Having the HCI_AUTO_OFF flag set means that from a mgmt interface perspective we're still not powered, so all tests for HCI_UP should also include a test for HCI_AUTO_OFF. This patch adds a convenience macro for it.

---
 net/bluetooth/mgmt.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)
net/bluetooth/mgmt.c +14 -12
index c25cb64..c27481c 100644
@@ -118,6 +118,9 @@ static const u16 mgmt_events[] = {
 
 #define SERVICE_CACHE_TIMEOUT (5 * 1000)
 
+#define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \
+				!test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
+
 struct pending_cmd {
 	struct list_head list;
 	u16 opcode;
@@ -733,7 +736,7 @@ static int set_powered(struct sock *sk, u16 index, void *data, u16 len)
 	struct mgmt_mode *cp = data;
 	struct hci_dev *hdev;
 	struct pending_cmd *cmd;
-	int err, up;
+	int err;
 
 	BT_DBG("request for hci%u", index);
 
@@ -758,8 +761,7 @@ static int set_powered(struct sock *sk, u16 index, void *data, u16 len)
 		}
 	}
 
-	up = test_bit(HCI_UP, &hdev->flags);
-	if ((cp->val && up) || (!cp->val && !up)) {
+	if (!!cp->val == hdev_is_powered(hdev)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
 		goto failed;
 	}
@@ -810,7 +812,7 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -877,7 +879,7 @@ static int set_connectable(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -1005,7 +1007,7 @@ static int set_link_security(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_SET_LINK_SECURITY,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -1064,7 +1066,7 @@ static int set_ssp(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_SET_SSP,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -1621,7 +1623,7 @@ static int pin_code_reply(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_PIN_CODE_REPLY,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -1690,7 +1692,7 @@ static int pin_code_neg_reply(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_PIN_CODE_NEG_REPLY,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
@@ -1933,7 +1935,7 @@ static int user_pairing_resp(struct sock *sk, u16 index, bdaddr_t *bdaddr,
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, mgmt_op, MGMT_STATUS_NOT_POWERED);
 		goto done;
 	}
@@ -2109,7 +2111,7 @@ static int read_local_oob_data(struct sock *sk, u16 index)
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_READ_LOCAL_OOB_DATA,
 						MGMT_STATUS_NOT_POWERED);
 		goto unlock;
@@ -2280,7 +2282,7 @@ static int start_discovery(struct sock *sk, u16 index,
 
 	hci_dev_lock(hdev);
 
-	if (!test_bit(HCI_UP, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
 		err = cmd_status(sk, index, MGMT_OP_START_DISCOVERY,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;
Signature · 1 line
1.7.9
#1 lore raw

PATCH 2/9 Bluetooth: mgmt: Fix set_local_name and set_dev_class powered checks

From: Johan Hedberg <[email protected]>

Both the Set Local Name and the Set Device Class commands should fail if the adapter is not powered on.

---
 net/bluetooth/mgmt.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
net/bluetooth/mgmt.c +13 -0
index c27481c..03a1384 100644
@@ -1272,6 +1272,12 @@ static int set_dev_class(struct sock *sk, u16 index, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
+	if (!hdev_is_powered(hdev)) {
+		err = cmd_status(sk, index, MGMT_OP_SET_DEV_CLASS,
+						MGMT_STATUS_NOT_POWERED);
+		goto unlock;
+	}
+
 	hdev->major_class = cp->major;
 	hdev->minor_class = cp->minor;
 
@@ -1288,6 +1294,7 @@ static int set_dev_class(struct sock *sk, u16 index, void *data, u16 len)
 		err = cmd_complete(sk, index, MGMT_OP_SET_DEV_CLASS, 0,
 								NULL, 0);
 
+unlock:
 	hci_dev_unlock(hdev);
 	hci_dev_put(hdev);
 
@@ -2076,6 +2083,12 @@ static int set_local_name(struct sock *sk, u16 index, void *data,
 
 	hci_dev_lock(hdev);
 
+	if (!hdev_is_powered(hdev)) {
+		err = cmd_status(sk, index, MGMT_OP_SET_LOCAL_NAME,
+						MGMT_STATUS_NOT_POWERED);
+		goto failed;
+	}
+
 	cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data,
 									len);
 	if (!cmd) {
Signature · 1 line
1.7.9
maintainer Acked-by on v1 2/9
#2 lore raw

Re: [PATCH 2/9] Bluetooth: mgmt: Fix set_local_name and set_dev_class powered checks

Hi Johan,

Quoted 7 lines

Both the Set Local Name and the Set Device Class commands should fail if the adapter is not powered on.

---
 net/bluetooth/mgmt.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

Regards

Marcel

#3 lore raw

PATCH 3/9 Bluetooth: mgmt: Allow connectable/discoverable changes in off state

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch makes it possible to toggle the connectable & discoverable settings when powered off. Two new hdev->dev_flags flags are added to track what the scan mode should be when the device is finally powered on.

---
 include/net/bluetooth/hci.h |    2 +
 net/bluetooth/hci_core.c    |    1 +
 net/bluetooth/mgmt.c        |   86 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 71 insertions(+), 18 deletions(-)
include/net/bluetooth/hci.h +2 -0
index ec37049..169d2f8 100644
@@ -96,6 +96,8 @@ enum {
 	HCI_LE_SCAN,
 	HCI_SSP_ENABLED,
 	HCI_HS_ENABLED,
+	HCI_CONNECTABLE,
+	HCI_DISCOVERABLE,
 };
 
 /* HCI ioctl defines */
net/bluetooth/hci_core.c +1 -0
index a787c9c..9d19949 100644
@@ -737,6 +737,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	if (hdev->discov_timeout > 0) {
 		cancel_delayed_work(&hdev->discov_off);
 		hdev->discov_timeout = 0;
+		clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
 	}
 
 	if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
net/bluetooth/mgmt.c +68 -18

163 lines not on this page · raw message

Signature · 1 line
1.7.9
maintainer
#4 lore raw

Re: [PATCH 3/9] Bluetooth: mgmt: Allow connectable/discoverable changes in off state

Hi Johan,

Quoted 76 lines

This patch makes it possible to toggle the connectable & discoverable settings when powered off. Two new hdev->dev_flags flags are added to track what the scan mode should be when the device is finally powered on.

---
 include/net/bluetooth/hci.h |    2 +
 net/bluetooth/hci_core.c    |    1 +
 net/bluetooth/mgmt.c        |   86 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 71 insertions(+), 18 deletions(-)
include/net/bluetooth/hci.h +2 -0
index ec37049..169d2f8 100644
@@ -96,6 +96,8 @@ enum {
 	HCI_LE_SCAN,
 	HCI_SSP_ENABLED,
 	HCI_HS_ENABLED,
+	HCI_CONNECTABLE,
+	HCI_DISCOVERABLE,
 };
 
 /* HCI ioctl defines */
net/bluetooth/hci_core.c +1 -0
index a787c9c..9d19949 100644
@@ -737,6 +737,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
 	if (hdev->discov_timeout > 0) {
 		cancel_delayed_work(&hdev->discov_off);
 		hdev->discov_timeout = 0;
+		clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
 	}
 
 	if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
net/bluetooth/mgmt.c +6 -3
index 03a1384..54df0c0 100644
@@ -398,10 +398,10 @@ static u32 get_current_settings(struct hci_dev *hdev)
 	if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
 		settings |= MGMT_SETTING_POWERED;
 
-	if (test_bit(HCI_PSCAN, &hdev->flags))
+	if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
 		settings |= MGMT_SETTING_CONNECTABLE;
 
-	if (test_bit(HCI_ISCAN, &hdev->flags))
+	if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
 		settings |= MGMT_SETTING_DISCOVERABLE;
 
 	if (test_bit(HCI_PAIRABLE, &hdev->dev_flags))
@@ -796,6 +796,7 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 	struct mgmt_cp_set_discoverable *cp = data;
 	struct hci_dev *hdev;
 	struct pending_cmd *cmd;
+	u16 timeout;
 	u8 scan;
 	int err;
 
@@ -810,9 +811,11 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 		return cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE,
 						MGMT_STATUS_INVALID_PARAMS);
 
+	timeout = get_unaligned_le16(&cp->timeout);
+
 	hci_dev_lock(hdev);
 
-	if (!hdev_is_powered(hdev)) {
+	if (!hdev_is_powered(hdev) && timeout > 0) {
 		err = cmd_status(sk, index, MGMT_OP_SET_DISCOVERABLE,
 						MGMT_STATUS_NOT_POWERED);
 		goto failed;

net/bluetooth/mgmt.c static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len) · line 834

Quoted 9 lines
net/bluetooth/mgmt.c +4 -2
@@ -825,8 +828,17 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 		goto failed;
 	}
 
-	if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) &&
-					test_bit(HCI_PSCAN, &hdev->flags)) {
+	if (!hdev_is_powered(hdev)) {
+		if (cp->val) {
+			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+			set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);

I think this is wrong. Even when powered off, we should only allow setting DISCOVERABLE when CONNECTABLE has been set previously.

No need to automatically enable CONNECTABLE. Just fail here.

Quoted 23 lines
net/bluetooth/mgmt.c +8 -1
+		} else
+			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
+		goto failed;
+	}
+
+	if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
 		goto failed;
 	}
@@ -849,7 +861,7 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 		mgmt_pending_remove(cmd);
 
 	if (cp->val)
-		hdev->discov_timeout = get_unaligned_le16(&cp->timeout);
+		hdev->discov_timeout = timeout;
 
 failed:
 	hci_dev_unlock(hdev);
@@ -880,8 +892,13 @@ static int set_connectable(struct sock *sk, u16 index, void *data, u16 len)
 	hci_dev_lock(hdev);
 
 	if (!hdev_is_powered(hdev)) {

net/bluetooth/mgmt.c static int set_connectable(struct sock *sk, u16 index, void *data, u16 len) · line 902

Quoted 10 lines
net/bluetooth/mgmt.c +7 -2
-		err = cmd_status(sk, index, MGMT_OP_SET_CONNECTABLE,
-						MGMT_STATUS_NOT_POWERED);
+		if (cp->val)
+			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+		else {
+			clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+		}
+		err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
 		goto failed;

This looks fine here.

However we need to handle an eventual discovery timeout.

Quoted 86 lines
net/bluetooth/mgmt.c +38 -9
 	}
 
@@ -892,7 +909,7 @@ static int set_connectable(struct sock *sk, u16 index, void *data, u16 len)
 		goto failed;
 	}
 
-	if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
+	if (!!cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
 		goto failed;
 	}
@@ -2866,9 +2883,22 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 	__le32 ev;
 	int err;
 
+	if (!test_bit(HCI_MGMT, &hdev->dev_flags))
+		return 0;
+
 	mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
 
-	if (!powered) {
+	if (powered) {
+		u8 scan = 0;
+
+		if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+			scan |= SCAN_PAGE;
+		if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
+			scan |= SCAN_INQUIRY;
+
+		if (scan)
+			hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
+	} else {
 		u8 status = ENETDOWN;
 		mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
 	}
@@ -2887,15 +2917,25 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 {
 	struct cmd_lookup match = { NULL, hdev };
-	__le32 ev;
-	int err;
+	bool changed = false;
+	int err = 0;
 
 	mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, &match);
 
-	ev = cpu_to_le32(get_current_settings(hdev));
+	if (discoverable) {
+		if (!test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
+			changed = true;
+	} else {
+		if (test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
+			changed = true;
+	}
 
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
+	if (changed) {
+		__le32 ev = cpu_to_le32(get_current_settings(hdev));
+		err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
 								match.sk);
+	}
+
 	if (match.sk)
 		sock_put(match.sk);
 
@@ -2904,16 +2944,26 @@ int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 
 int mgmt_connectable(struct hci_dev *hdev, u8 connectable)
 {
-	__le32 ev;
 	struct cmd_lookup match = { NULL, hdev };
-	int err;
+	bool changed = false;
+	int err = 0;
 
 	mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, settings_rsp,
 								&match);
 
-	ev = cpu_to_le32(get_current_settings(hdev));
+	if (connectable) {
+		if (!test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+			changed = true;
+	} else {
+		if (test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+			changed = true;
+	}

net/bluetooth/mgmt.c int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) · line 2969

Quoted 10 lines
net/bluetooth/mgmt.c +5 -1
 
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk);
+	if (changed) {
+		__le32 ev = cpu_to_le32(get_current_settings(hdev));
+		err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
+								match.sk);
+	}
 
 	if (match.sk)
 		sock_put(match.sk);

Regards

Marcel

#5 lore raw

PATCH 4/9 Bluetooth: mgmt: Fix current settings values when powered off

In reply to [email protected]

From: Johan Hedberg <[email protected]>

We should not stop iterating through the various settings if powered off since most may still be set even then.

---
 net/bluetooth/mgmt.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)
net/bluetooth/mgmt.c +1 -4
index 54df0c0..629bed1 100644
@@ -392,10 +392,7 @@ static u32 get_current_settings(struct hci_dev *hdev)
 {
 	u32 settings = 0;
 
-	if (!test_bit(HCI_UP, &hdev->flags))
-		return settings;
-
-	if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
+	if (hdev_is_powered(hdev))
 		settings |= MGMT_SETTING_POWERED;
 
 	if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
Signature · 1 line
1.7.9
maintainer Acked-by on v1 4/9
#6 lore raw

Re: [PATCH 4/9] Bluetooth: mgmt: Fix current settings values when powered off

Hi Johan,

Quoted 7 lines

We should not stop iterating through the various settings if powered off since most may still be set even then.

---
 net/bluetooth/mgmt.c |    5 +----
 1 files changed, 1 insertions(+), 4 deletions(-)

Regards

Marcel

#7 lore raw

PATCH 5/9 Bluetooth: mgmt: Add convenience function for sending New Settings

In reply to [email protected]

From: Johan Hedberg <[email protected]>

The New Settings event needs to be sent from quite many places so it makes sense to have a convenience function for it to simplify the code.

---
 net/bluetooth/mgmt.c |   96 +++++++++++++++++++++++---------------------------
 1 files changed, 44 insertions(+), 52 deletions(-)
net/bluetooth/mgmt.c +44 -52
index 629bed1..74b6db1 100644
@@ -788,6 +788,42 @@ failed:
 	return err;
 }
 
+static int mgmt_event(u16 event, struct hci_dev *hdev, void *data,
+					u16 data_len, struct sock *skip_sk)
+{
+	struct sk_buff *skb;
+	struct mgmt_hdr *hdr;
+
+	skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC);
+	if (!skb)
+		return -ENOMEM;
+
+	hdr = (void *) skb_put(skb, sizeof(*hdr));
+	hdr->opcode = cpu_to_le16(event);
+	if (hdev)
+		hdr->index = cpu_to_le16(hdev->id);
+	else
+		hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
+	hdr->len = cpu_to_le16(data_len);
+
+	if (data)
+		memcpy(skb_put(skb, data_len), data, data_len);
+
+	hci_send_to_control(skb, skip_sk);
+	kfree_skb(skb);
+
+	return 0;
+}
+
+static int new_settings(struct hci_dev *hdev, struct sock *skip)
+{
+	__le32 ev;
+
+	ev = cpu_to_le32(get_current_settings(hdev));
+
+	return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
+}
+
 static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 {
 	struct mgmt_cp_set_discoverable *cp = data;
@@ -933,38 +969,10 @@ failed:
 	return err;
 }
 
-static int mgmt_event(u16 event, struct hci_dev *hdev, void *data,
-					u16 data_len, struct sock *skip_sk)
-{
-	struct sk_buff *skb;
-	struct mgmt_hdr *hdr;
-
-	skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC);
-	if (!skb)
-		return -ENOMEM;
-
-	hdr = (void *) skb_put(skb, sizeof(*hdr));
-	hdr->opcode = cpu_to_le16(event);
-	if (hdev)
-		hdr->index = cpu_to_le16(hdev->id);
-	else
-		hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
-	hdr->len = cpu_to_le16(data_len);
-
-	if (data)
-		memcpy(skb_put(skb, data_len), data, data_len);
-
-	hci_send_to_control(skb, skip_sk);
-	kfree_skb(skb);
-
-	return 0;
-}
-
 static int set_pairable(struct sock *sk, u16 index, void *data, u16 len)
 {
 	struct mgmt_mode *cp = data;
 	struct hci_dev *hdev;
-	__le32 ev;
 	int err;
 
 	BT_DBG("request for hci%u", index);
@@ -989,9 +997,7 @@ static int set_pairable(struct sock *sk, u16 index, void *data, u16 len)
 	if (err < 0)
 		goto failed;
 
-	ev = cpu_to_le32(get_current_settings(hdev));
-
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), sk);
+	err = new_settings(hdev, sk);
 
 failed:
 	hci_dev_unlock(hdev);
@@ -2877,7 +2883,6 @@ static void settings_rsp(struct pending_cmd *cmd, void *data)
 int mgmt_powered(struct hci_dev *hdev, u8 powered)
 {
 	struct cmd_lookup match = { NULL, hdev };
-	__le32 ev;
 	int err;
 
 	if (!test_bit(HCI_MGMT, &hdev->dev_flags))
@@ -2900,10 +2905,7 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
 		mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
 	}
 
-	ev = cpu_to_le32(get_current_settings(hdev));
-
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
-								match.sk);
+	err = new_settings(hdev, match.sk);
 
 	if (match.sk)
 		sock_put(match.sk);
@@ -2927,11 +2929,8 @@ int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
 			changed = true;
 	}
 
-	if (changed) {
-		__le32 ev = cpu_to_le32(get_current_settings(hdev));
-		err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
-								match.sk);
-	}
+	if (changed)
+		err = new_settings(hdev, match.sk);
 
 	if (match.sk)
 		sock_put(match.sk);
@@ -2956,11 +2955,8 @@ int mgmt_connectable(struct hci_dev *hdev, u8 connectable)
 			changed = true;
 	}
 
-	if (changed) {
-		__le32 ev = cpu_to_le32(get_current_settings(hdev));
-		err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev),
-								match.sk);
-	}
+	if (changed)
+		err = new_settings(hdev, match.sk);
 
 	if (match.sk)
 		sock_put(match.sk);
@@ -3295,7 +3291,6 @@ int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
 int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
 {
 	struct cmd_lookup match = { NULL, hdev };
-	__le32 ev;
 	int err;
 
 	if (status) {
@@ -3308,8 +3303,7 @@ int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
 	mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
 								&match);
 
-	ev = cpu_to_le32(get_current_settings(hdev));
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk);
+	err = new_settings(hdev, match.sk);
 
 	if (match.sk)
 		sock_put(match.sk);
@@ -3332,7 +3326,6 @@ static int clear_eir(struct hci_dev *hdev)
 int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 status)
 {
 	struct cmd_lookup match = { NULL, hdev };
-	__le32 ev;
 	int err;
 
 	if (status) {
@@ -3344,8 +3337,7 @@ int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 status)
 
 	mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
 
-	ev = cpu_to_le32(get_current_settings(hdev));
-	err = mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), match.sk);
+	err = new_settings(hdev, match.sk);
 
 	if (match.sk) {
 		sock_put(match.sk);
Signature · 1 line
1.7.9
maintainer Acked-by on v1 5/9
#8 lore raw

Re: [PATCH 5/9] Bluetooth: mgmt: Add convenience function for sending New Settings

Hi Johan,

Quoted 7 lines

The New Settings event needs to be sent from quite many places so it makes sense to have a convenience function for it to simplify the code.

---
 net/bluetooth/mgmt.c |   96 +++++++++++++++++++++++---------------------------
 1 files changed, 44 insertions(+), 52 deletions(-)

Regards

Marcel

#9 lore raw

PATCH 6/9 Bluetooth: mgmt: Fix New Settings event for connectable/discoverable

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When powered off and doing changes to the Connectable or Discoverable setting we should also send an appropriate New Settings event in addition to the command response.

---
 net/bluetooth/mgmt.c |   41 ++++++++++++++++++++++++++++++++++-------
 1 files changed, 34 insertions(+), 7 deletions(-)
net/bluetooth/mgmt.c +34 -7
index 74b6db1..96386ff 100644
@@ -862,12 +862,26 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 	}
 
 	if (!hdev_is_powered(hdev)) {
+		bool changed = false;
+
 		if (cp->val) {
+			if (!test_and_set_bit(HCI_DISCOVERABLE,
+							&hdev->dev_flags))
+				changed = true;
 			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-			set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
-		} else
-			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+		} else {
+			if (test_and_clear_bit(HCI_DISCOVERABLE,
+							&hdev->dev_flags))
+				changed = true;
+		}
+
 		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
+		if (err < 0)
+			goto failed;
+
+		if (changed)
+			err = new_settings(hdev, sk);
+
 		goto failed;
 	}
 
@@ -925,13 +939,26 @@ static int set_connectable(struct sock *sk, u16 index, void *data, u16 len)
 	hci_dev_lock(hdev);
 
 	if (!hdev_is_powered(hdev)) {
-		if (cp->val)
-			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-		else {
-			clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+		bool changed = false;
+
+		if (cp->val) {
+			if (!test_and_set_bit(HCI_CONNECTABLE,
+							&hdev->dev_flags))
+				changed = true;
+		} else {
+			if (test_and_clear_bit(HCI_CONNECTABLE,
+							&hdev->dev_flags))
+				changed = true;
 			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
 		}
+
 		err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
+		if (err < 0)
+			goto failed;
+
+		if (changed)
+			err = new_settings(hdev, sk);
+
 		goto failed;
 	}
 
Signature · 1 line
1.7.9
maintainer
#10 lore raw

Re: [PATCH 6/9] Bluetooth: mgmt: Fix New Settings event for connectable/discoverable

Hi Johan,

Quoted 23 lines

When powered off and doing changes to the Connectable or Discoverable setting we should also send an appropriate New Settings event in addition to the command response.

---
 net/bluetooth/mgmt.c |   41 ++++++++++++++++++++++++++++++++++-------
 1 files changed, 34 insertions(+), 7 deletions(-)
net/bluetooth/mgmt.c +5 -0
index 74b6db1..96386ff 100644
@@ -862,12 +862,26 @@ static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len)
 	}
 
 	if (!hdev_is_powered(hdev)) {
+		bool changed = false;
+
 		if (cp->val) {
+			if (!test_and_set_bit(HCI_DISCOVERABLE,
+							&hdev->dev_flags))
+				changed = true;

net/bluetooth/mgmt.c static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len) · line 877

Quoted 10 lines
net/bluetooth/mgmt.c +6 -3
 			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-			set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
-		} else
-			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+		} else {
+			if (test_and_clear_bit(HCI_DISCOVERABLE,
+							&hdev->dev_flags))
+				changed = true;
+		}
+

does it really need to be this complicated? If we remove the set_bit for CONNECTABLE, then we can just send this one out. Since we know that something changed.

Quoted 21 lines
net/bluetooth/mgmt.c +9 -4
 		err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
+		if (err < 0)
+			goto failed;
+
+		if (changed)
+			err = new_settings(hdev, sk);
+
 		goto failed;
 	}
 
@@ -925,13 +939,26 @@ static int set_connectable(struct sock *sk, u16 index, void *data, u16 len)
 	hci_dev_lock(hdev);
 
 	if (!hdev_is_powered(hdev)) {
-		if (cp->val)
-			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-		else {
-			clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
+		bool changed = false;
+
+		if (cp->val) {

net/bluetooth/mgmt.c static int set_connectable(struct sock *sk, u16 index, void *data, u16 len) · line 954

Quoted 10 lines
net/bluetooth/mgmt.c +8 -0
+			if (!test_and_set_bit(HCI_CONNECTABLE,
+							&hdev->dev_flags))
+				changed = true;
+		} else {
+			if (test_and_clear_bit(HCI_CONNECTABLE,
+							&hdev->dev_flags))
+				changed = true;
 			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
 		}
+

Same here. Even with the clearing of DISCOVERABLE, we should just send the new settings. event.

net/bluetooth/mgmt.c static int set_connectable(struct sock *sk, u16 index, void *data, u16 len) · line 964

Quoted 10 lines
net/bluetooth/mgmt.c +6 -0
 		err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
+		if (err < 0)
+			goto failed;
+
+		if (changed)
+			err = new_settings(hdev, sk);
+
 		goto failed;
 	}
 

Regards

Marcel

#11 lore raw

Re: [PATCH 6/9] Bluetooth: mgmt: Fix New Settings event for connectable/discoverable

Hi Marcel,

Quoted 13 lines · Marcel Holtmann
Quoted 13 lines
net/bluetooth/mgmt.c +7 -3
 	if (!hdev_is_powered(hdev)) {
+		bool changed = false;
+
 		if (cp->val) {
+			if (!test_and_set_bit(HCI_DISCOVERABLE,
+							&hdev->dev_flags))
+				changed = true;
 			set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
-			set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
-		} else
-			clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
+		} else {
+			if (test_and_clear_bit(HCI_DISCOVERABLE,
Quoted 8 lines

net/bluetooth/mgmt.c static int set_discoverable(struct sock *sk, u16 index, void *data, u16 len) · line 877

Quoted 4 lines
net/bluetooth/mgmt.c +4 -0
+							&hdev->dev_flags))
+				changed = true;
+		}
+

does it really need to be this complicated? If we remove the set_bit for CONNECTABLE, then we can just send this one out. Since we know that something changed.

Since we don't treat trying to change a setting to a value it already has as an error it's not guaranteed that there will always be a change in settings. Therefore, we do always need to send the cmd_complete but not always the new_settings event. In the new set of patches I sent I've simplified this logic a bit but the check is still there.

Johan

#12 lore raw

PATCH 7/9 Bluetooth: mgmt: Fix set_fast_connectable error return

In reply to [email protected]

From: Johan Hedberg <[email protected]>

This patch ensures that Set Fast Connectable fails apropriately if we are not already in a connectable state (which is a pre-requisite for fast connectable).

---
 net/bluetooth/mgmt.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
net/bluetooth/mgmt.c +7 -0
index 96386ff..abad38c 100644
@@ -2607,6 +2607,13 @@ static int set_fast_connectable(struct sock *sk, u16 index,
 	if (!hdev)
 		return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
 						MGMT_STATUS_INVALID_PARAMS);
+	if (!hdev_is_powered(hdev))
+		return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
+						MGMT_STATUS_NOT_POWERED);
+
+	if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
+		return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
+							MGMT_STATUS_REJECTED);
 
 	hci_dev_lock(hdev);
 
Signature · 1 line
1.7.9
maintainer Acked-by on v1 7/9
#13 lore raw

Re: [PATCH 7/9] Bluetooth: mgmt: Fix set_fast_connectable error return

Hi Johan,

Quoted 8 lines

This patch ensures that Set Fast Connectable fails apropriately if we are not already in a connectable state (which is a pre-requisite for fast connectable).

---
 net/bluetooth/mgmt.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

Regards

Marcel

#14 lore raw

PATCH 8/9 Bluetooth: mgmt: Fix pairable setting upon initialization

In reply to [email protected]

From: Johan Hedberg <[email protected]>

When mgmt is not in use the HCI_PAIRABLE flag will get implicitly set so that pairing still works with old user space versions. However, as soon as mgmt comes into play we should clear this flag so that it can be properly set through the set_pairable command by user space.

---
 net/bluetooth/mgmt.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
net/bluetooth/mgmt.c +9 -1
index abad38c..ac6dcff 100644
@@ -601,9 +601,17 @@ static void service_cache_off(struct work_struct *work)
 
 static void mgmt_init_hdev(struct hci_dev *hdev)
 {
-	if (!test_and_set_bit(HCI_MGMT, &hdev->dev_flags))
+	if (!test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) {
 		INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
 
+		/* Non-mgmt controlled devices get this bit set
+		 * implicitly so that pairing works for them, however
+		 * for mgmt we require user-space to explicitly enable
+		 * it
+		 */
+		clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
+	}
+
 	if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
 		schedule_delayed_work(&hdev->service_cache,
 				msecs_to_jiffies(SERVICE_CACHE_TIMEOUT));
Signature · 1 line
1.7.9
maintainer Acked-by on v1 8/9
#15 lore raw

Re: [PATCH 8/9] Bluetooth: mgmt: Fix pairable setting upon initialization

Hi Johan,

Quoted 9 lines

When mgmt is not in use the HCI_PAIRABLE flag will get implicitly set so that pairing still works with old user space versions. However, as soon as mgmt comes into play we should clear this flag so that it can be properly set through the set_pairable command by user space.

---
 net/bluetooth/mgmt.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

Regards

Marcel

#16 lore raw

PATCH 9/9 Bluetooth: Fix clearing of persistent dev_flags

In reply to [email protected]

From: Johan Hedberg <[email protected]>

Now that most flags are persistent, only the LE_SCAN flag should be cleared after a reset.

---
 net/bluetooth/hci_event.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)
net/bluetooth/hci_event.c +2 -3
index 5d0f92a..2a5d05c 100644
@@ -192,9 +192,8 @@ static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
 
 	hci_req_complete(hdev, HCI_OP_RESET, status);
 
-	/* Reset all flags, except persistent ones */
-	hdev->dev_flags &= BIT(HCI_MGMT) | BIT(HCI_SETUP) | BIT(HCI_AUTO_OFF) |
-				BIT(HCI_LINK_KEYS) | BIT(HCI_DEBUG_KEYS);
+	/* Reset all non-persistent flags */
+	hdev->dev_flags &= ~(BIT(HCI_LE_SCAN));
 }
 
 static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
Signature · 1 line
1.7.9
maintainer Acked-by on v1 1/9
#18 lore raw
In reply to [email protected]

Hi Johan,

Quoted 9 lines

Having the HCI_AUTO_OFF flag set means that from a mgmt interface perspective we're still not powered, so all tests for HCI_UP should also include a test for HCI_AUTO_OFF. This patch adds a convenience macro for it.

---
 net/bluetooth/mgmt.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)

Regards

Marcel