Bluetooth: Update hci_add_link_key() to return pointer to key
Subsystems (2)
PATCH 1/6 Bluetooth: Update hci_add_link_key() to return pointer to key
From: Johan Hedberg <[email protected]>
By returning the added (or updated) key we pave the way for further refactoring (in subsequent patches) that allows moving the mgmt event sending out from this function (and thereby removal of the awkward new_key parameter).
--- include/net/bluetooth/hci_core.h | 5 +++-- net/bluetooth/hci_core.c | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-)
include/net/bluetooth/hci_core.h +3 -2
@@ -859,8 +859,9 @@ void hci_uuids_clear(struct hci_dev *hdev);void hci_link_keys_clear (struct hci_dev *hdev);struct link_key *hci_find_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr); -int hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn,int new_key, -bdaddr_t *bdaddr,u8 *val,u8 type,u8 pin_len); +struct link_key *hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn, +int new_key,bdaddr_t *bdaddr,u8 *val, +u8 type,u8 pin_len);struct smp_ltk *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,__le64 rand,bool master); struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
net/bluetooth/hci_core.c +6 -5
@@ -3001,8 +3001,9 @@ struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,return NULL ; } -int hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn,int new_key, -bdaddr_t *bdaddr,u8 *val,u8 type,u8 pin_len) +struct link_key *hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn, +int new_key,bdaddr_t *bdaddr,u8 *val, +u8 type,u8 pin_len) {struct link_key *key, *old_key;u8 old_key_type;
@@ -3016,7 +3017,7 @@ int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, old_key_type = conn ? conn->key_type :0xff ; key =kzalloc (sizeof (*key),GFP_KERNEL );if (!key) -return -ENOMEM ; +return NULL ;list_add (&key->list, &hdev->link_keys); }
@@ -3042,7 +3043,7 @@ int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key, key->type = type;if (!new_key) -return 0 ; +return key; persistent =hci_persistent_key (hdev, conn, type, old_key_type);
@@ -3051,7 +3052,7 @@ int hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, int new_key,if (conn) conn->flush_key = !persistent; -return 0 ; +return key; } struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
Signature · 1 line
1.9.3
PATCH 2/6 Bluetooth: Move mgmt event sending out from hci_add_link_key()
From: Johan Hedberg <[email protected]>
There are two callers of hci_add_link_key(). The first one is the HCI Link Key Notification event and the second one the mgmt code that receives a list of link keys from user space. Previously we've had the hci_add_link_key() function being responsible for also emitting a mgmt signal but for the latter use case this should not happen. Because of this a rather awkward new_key paramter has been passed to the function.
This patch moves the mgmt event sending out from the hci_add_link_key() function, thereby making the code a bit more understandable.
--- include/net/bluetooth/hci_core.h | 4 ++-- net/bluetooth/hci_core.c | 17 +++++------------ net/bluetooth/hci_event.c | 18 +++++++++++++++--- net/bluetooth/mgmt.c | 4 ++-- 4 files changed, 24 insertions(+), 19 deletions(-)
include/net/bluetooth/hci_core.h +2 -2
@@ -860,8 +860,8 @@ void hci_uuids_clear(struct hci_dev *hdev);void hci_link_keys_clear (struct hci_dev *hdev);struct link_key *hci_find_link_key (struct hci_dev *hdev,bdaddr_t *bdaddr);struct link_key *hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn, -int new_key,bdaddr_t *bdaddr,u8 *val, -u8 type,u8 pin_len); +bdaddr_t *bdaddr,u8 *val,u8 type, +u8 pin_len,bool *persistent);struct smp_ltk *hci_find_ltk (struct hci_dev *hdev,__le16 ediv,__le64 rand,bool master); struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
net/bluetooth/hci_core.c +5 -12
@@ -3002,12 +3002,11 @@ struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr, }struct link_key *hci_add_link_key (struct hci_dev *hdev,struct hci_conn *conn, -int new_key,bdaddr_t *bdaddr,u8 *val, -u8 type,u8 pin_len) +bdaddr_t *bdaddr,u8 *val,u8 type, +u8 pin_len,bool *persistent) {struct link_key *key, *old_key;u8 old_key_type; -bool persistent; old_key =hci_find_link_key (hdev, bdaddr);if (old_key) {
@@ -3042,15 +3041,9 @@ struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,else key->type = type; -if (!new_key) -return key; - - persistent =hci_persistent_key (hdev, conn, type, old_key_type); - -mgmt_new_link_key (hdev, key, persistent); - -if (conn) - conn->flush_key = !persistent; +if (persistent) + *persistent =hci_persistent_key (hdev, conn, type, + old_key_type);return key; }
net/bluetooth/hci_event.c +15 -3
@@ -3111,6 +3111,8 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb) {struct hci_ev_link_key_notify *ev = (void *) skb->data;struct hci_conn *conn; +struct link_key *key; +bool persistent;u8 pin_len =0 ;BT_DBG ("%s" , hdev->name);
@@ -3129,10 +3131,20 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)hci_conn_drop (conn); } -if (test_bit (HCI_MGMT , &hdev->dev_flags)) -hci_add_link_key (hdev, conn,1 , &ev->bdaddr, ev->link_key, - ev->key_type, pin_len); +if (!test_bit (HCI_MGMT , &hdev->dev_flags)) + goto unlock; + + key =hci_add_link_key (hdev, conn, &ev->bdaddr, ev->link_key, + ev->key_type, pin_len, &persistent); +if (!key) + goto unlock; + +mgmt_new_link_key (hdev, key, persistent); +if (conn) + conn->flush_key = !persistent; + +unlock:hci_dev_unlock (hdev); }
net/bluetooth/mgmt.c +2 -2
@@ -2424,8 +2424,8 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,for (i =0 ; i < key_count; i++) {struct mgmt_link_key_info *key = &cp->keys[i]; -hci_add_link_key (hdev,NULL ,0 , &key->addr.bdaddr, key->val, - key->type, key->pin_len); +hci_add_link_key (hdev,NULL , &key->addr.bdaddr, key->val, + key->type, key->pin_len,NULL ); }cmd_complete (sk, hdev->id,MGMT_OP_LOAD_LINK_KEYS ,0 ,NULL ,0 );
Signature · 1 line
1.9.3
PATCH 3/6 Bluetooth: Rename HCI_DEBUG_KEYS to HCI_KEEP_DEBUG_KEYS
In reply to [email protected]From: Johan Hedberg <[email protected]>
We're planning to add a flag to actively use debug keys in addition to simply just accepting them, which makes the current generically named DEBUG_KEYS flag a bit confusing. Since the flag in practice affects whether the kernel keeps debug keys around or not rename it to HCI_KEEP_DEBUG_KEYS.
--- include/net/bluetooth/hci.h | 2 +- net/bluetooth/hci_event.c | 2 +- net/bluetooth/mgmt.c | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-)
include/net/bluetooth/hci.h +1 -1
@@ -124,7 +124,7 @@ enum {HCI_MGMT ,HCI_PAIRABLE ,HCI_SERVICE_CACHE , -HCI_DEBUG_KEYS , +HCI_KEEP_DEBUG_KEYS ,HCI_UNREGISTER , HCI_USER_CHANNEL,
net/bluetooth/hci_event.c +1 -1
@@ -3066,7 +3066,7 @@ static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)BT_DBG ("%s found key type %u for %pMR" , hdev->name, key->type, &ev->bdaddr); -if (!test_bit (HCI_DEBUG_KEYS , &hdev->dev_flags) && +if (!test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->dev_flags) && key->type ==HCI_LK_DEBUG_COMBINATION ) {BT_DBG ("%s ignoring debug key" , hdev->name); goto not_found;
net/bluetooth/mgmt.c +9 -5
@@ -440,7 +440,7 @@ static u32 get_current_settings(struct hci_dev *hdev)if (test_bit (HCI_SC_ENABLED , &hdev->dev_flags)) settings |=MGMT_SETTING_SECURE_CONN ; -if (test_bit (HCI_DEBUG_KEYS , &hdev->dev_flags)) +if (test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->dev_flags)) settings |=MGMT_SETTING_DEBUG_KEYS ;if (test_bit (HCI_PRIVACY , &hdev->dev_flags))
@@ -2414,9 +2414,11 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,hci_link_keys_clear (hdev);if (cp->debug_keys) - changed = !test_and_set_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); + changed = !test_and_set_bit (HCI_KEEP_DEBUG_KEYS , + &hdev->dev_flags);else - changed =test_and_clear_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); + changed =test_and_clear_bit (HCI_KEEP_DEBUG_KEYS , + &hdev->dev_flags);if (changed)new_settings (hdev,NULL );
@@ -4349,9 +4351,11 @@ static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,hci_dev_lock (hdev);if (cp->val) - changed = !test_and_set_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); + changed = !test_and_set_bit (HCI_KEEP_DEBUG_KEYS , + &hdev->dev_flags);else - changed =test_and_clear_bit (HCI_DEBUG_KEYS , &hdev->dev_flags); + changed =test_and_clear_bit (HCI_KEEP_DEBUG_KEYS , + &hdev->dev_flags); err =send_settings_rsp (sk,MGMT_OP_SET_DEBUG_KEYS , hdev);if (err <0 )
Signature · 1 line
1.9.3
PATCH 4/6 Bluetooth: Don't store debug keys if flag for them is not set
In reply to [email protected]From: Johan Hedberg <[email protected]>
Instead of waiting for a disconnection to occur to remove a debug key simply never store it in the list to begin with. This means we can also remove the debug keys check when looking up keys in hci_link_key_request_evt().
--- net/bluetooth/hci_event.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
net/bluetooth/hci_event.c +11 -7
@@ -3066,12 +3066,6 @@ static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)BT_DBG ("%s found key type %u for %pMR" , hdev->name, key->type, &ev->bdaddr); -if (!test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->dev_flags) && - key->type ==HCI_LK_DEBUG_COMBINATION ) { -BT_DBG ("%s ignoring debug key" , hdev->name); - goto not_found; - } - conn =hci_conn_hash_lookup_ba (hdev,ACL_LINK , &ev->bdaddr);if (conn) { if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
@@ -3141,8 +3135,18 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)mgmt_new_link_key (hdev, key, persistent); -if (conn) +/* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag +* is set. If it's not set simply remove the key from the kernel +* list (we've still notified user space about it but with +* store_hint being 0). +*/ +if (key->type ==HCI_LK_DEBUG_COMBINATION && + !test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->dev_flags)) { +list_del (&key->list); +kfree (key); + }else if (conn) { conn->flush_key = !persistent; + } unlock:hci_dev_unlock (hdev);
Signature · 1 line
1.9.3
PATCH 5/6 Bluetooth: Fix ignoring debug keys in mgmt_load_link_keys
In reply to [email protected]From: Johan Hedberg <[email protected]>
If support for debug keys is not enabled we should simply ignore any debug keys fed to us from user space using the mgmt_load_link_keys command.
--- net/bluetooth/mgmt.c | 5 +++++ 1 file changed, 5 insertions(+)
net/bluetooth/mgmt.c +5 -0
@@ -2426,6 +2426,11 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,for (i =0 ; i < key_count; i++) {struct mgmt_link_key_info *key = &cp->keys[i]; +/* Ignore debug keys if support is not enabled */ +if (key->type ==HCI_LK_DEBUG_COMBINATION && + !test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->flags)) +continue ; +hci_add_link_key (hdev,NULL , &key->addr.bdaddr, key->val, key->type, key->pin_len,NULL ); }
Signature · 1 line
1.9.3
PATCH 6/6 Bluetooth: Convert hcon->flush_key to a proper flag
In reply to [email protected]From: Johan Hedberg <[email protected]>
There's no point in having boolean variables in the hci_conn struct since it already has a flags member. This patch converts the flush_key member into a proper flag.
--- include/net/bluetooth/hci_core.h | 2 +- net/bluetooth/hci_event.c | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-)
include/net/bluetooth/hci_core.h +1 -1
@@ -394,7 +394,6 @@ struct hci_conn {__u8 remote_cap;__u8 remote_auth;__u8 remote_id; -bool flush_key;unsigned int sent;
@@ -524,6 +523,7 @@ enum {HCI_CONN_AES_CCM ,HCI_CONN_POWER_SAVE ,HCI_CONN_REMOTE_OOB , + HCI_CONN_FLUSH_KEY, };static inline bool hci_conn_ssp_enabled (struct hci_conn *conn)
net/bluetooth/hci_event.c +6 -2
@@ -2159,7 +2159,8 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)mgmt_device_disconnected (hdev, &conn->dst, conn->type, conn->dst_type, reason, mgmt_connected); -if (conn->type ==ACL_LINK && conn->flush_key) +if (conn->type ==ACL_LINK && +test_bit (HCI_CONN_FLUSH_KEY , &conn->flags))hci_remove_link_key (hdev, &conn->dst); params =hci_conn_params_lookup (hdev, &conn->dst, conn->dst_type);
@@ -3145,7 +3146,10 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)list_del (&key->list);kfree (key); }else if (conn) { - conn->flush_key = !persistent; +if (persistent) +clear_bit (HCI_CONN_FLUSH_KEY , &conn->flags); +else +set_bit (HCI_CONN_FLUSH_KEY , &conn->flags); } unlock:
Signature · 1 line
1.9.3
Marcel Holtmann
maintainerRe: [PATCH 5/6] Bluetooth: Fix ignoring debug keys in mgmt_load_link_keys
In reply to [email protected]Hi Johan,
Quoted 14 lines
If support for debug keys is not enabled we should simply ignore any debug keys fed to us from user space using the
mgmt_load_link_keyscommand.Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/mgmt.c| 5 +++++1 file changed, 5 insertions(+)net/bluetooth/mgmt.c +0
-0
net/bluetooth/mgmt.c static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, · line 2433
Quoted 8 lines
net/bluetooth/mgmt.c +5
-0@@ -2426,6 +2426,11 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,for (i =0 ; i < key_count; i++) {struct mgmt_link_key_info *key = &cp->keys[i]; +/* Ignore debug keys if support is not enabled */ +if (key->type ==HCI_LK_DEBUG_COMBINATION && + !test_bit (HCI_KEEP_DEBUG_KEYS , &hdev->flags)) +continue ; +
I would just always ignore debug keys. It makes no sense to allow loading them via userspace.
The Load Link Keys command is normally only ever used once. However Set Debug Keys might change during lifetime. Which means that debug keys you load here will be removed when we switch the mode back to no debug keys allowed. Then you would need to load all keys again. That sounds like really really complicated.
So my take is that we should always ignore debug keys loaded into the kernel. If you want a debug key, then pair with the device again. At least this way we know that a power cycle will always fix this, no matter what the setting of debug keys mode is.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 6/6] Bluetooth: Convert hcon->flush_key to a proper flag
In reply to [email protected]Hi Johan,
Quoted 9 lines
There's no point in having boolean variables in the
hci_connstruct since it already has a flags member. This patch converts theflush_keymember into a proper flag.Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci_core.h| 2 +-net/bluetooth/hci_event.c| 8 ++++++--2 files changed, 7 insertions(+), 3 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 4/6] Bluetooth: Don't store debug keys if flag for them is not set
In reply to [email protected]Hi Johan,
Quoted 9 lines
Instead of waiting for a disconnection to occur to remove a debug key simply never store it in the list to begin with. This means we can also remove the debug keys check when looking up keys in
hci_link_key_request_evt().Signed-off-by Johan Hedberg <[email protected]>---
net/bluetooth/hci_event.c| 18 +++++++++++-------1 file changed, 11 insertions(+), 7 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 3/6] Bluetooth: Rename HCI_DEBUG_KEYS to HCI_KEEP_DEBUG_KEYS
In reply to [email protected]Hi Johan,
Quoted 6 lines
We're planning to add a flag to actively use debug keys in addition to simply just accepting them, which makes the current generically named
DEBUG_KEYSflag a bit confusing. Since the flag in practice affects whether the kernel keeps debug keys around or not rename it toHCI_KEEP_DEBUG_KEYS.
Quoted 6 lines
Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci.h | 2 +- net/bluetooth/hci_event.c | 2 +- net/bluetooth/mgmt.c | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 2/6] Bluetooth: Move mgmt event sending out from hci_add_link_key()
In reply to [email protected]Hi Johan,
Quoted 10 lines
There are two callers of
hci_add_link_key(). The first one is the HCI Link Key Notification event and the second one the mgmt code that receives a list of link keys from user space. Previously we've had thehci_add_link_key()function being responsible for also emitting a mgmt signal but for the latter use case this should not happen. Because of this a rather awkwardnew_keyparamter has been passed to the function.This patch moves the mgmt event sending out from the
hci_add_link_key()function, thereby making the code a bit more understandable.
Quoted 7 lines
Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci_core.h | 4 ++-- net/bluetooth/hci_core.c | 17 +++++------------ net/bluetooth/hci_event.c | 18 +++++++++++++++--- net/bluetooth/mgmt.c | 4 ++-- 4 files changed, 24 insertions(+), 19 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel
Marcel Holtmann
maintainerRe: [PATCH 6/6] Bluetooth: Convert hcon->flush_key to a proper flag
In reply to [email protected]Hi Johan,
Quoted 9 lines
There's no point in having boolean variables in the
hci_connstruct since it already has a flags member. This patch converts theflush_keymember into a proper flag.Signed-off-by Johan Hedberg <[email protected]>---
include/net/bluetooth/hci_core.h| 2 +-net/bluetooth/hci_event.c| 8 ++++++--2 files changed, 7 insertions(+), 3 deletions(-)
patch has been applied to bluetooth-next tree.
Regards
Marcel