aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth
diff options
context:
space:
mode:
authorJohn W. Linville <linville@tuxdriver.com>2011-09-30 14:52:16 -0400
committerJohn W. Linville <linville@tuxdriver.com>2011-09-30 14:52:29 -0400
commit8e00f5fbb4ecbc3431fa686cba60cd76a62604af (patch)
tree78aa3bdef892dbd1afc8969fc528e529cfc266f7 /net/bluetooth
parent56fd49e399ce1d82200fad5b8924d4e35a587809 (diff)
parent0874073570d9184a169a4ae2abbe8dbcb71878e9 (diff)
Merge branch 'master' of git://git.infradead.org/users/linville/wireless-next into for-davem
Conflicts: drivers/net/wireless/iwlwifi/iwl-pci.c drivers/net/wireless/wl12xx/main.c
Diffstat (limited to 'net/bluetooth')
-rw-r--r--net/bluetooth/hci_conn.c2
-rw-r--r--net/bluetooth/hci_core.c57
-rw-r--r--net/bluetooth/hci_event.c16
-rw-r--r--net/bluetooth/hci_sock.c18
-rw-r--r--net/bluetooth/hci_sysfs.c2
-rw-r--r--net/bluetooth/hidp/core.c3
-rw-r--r--net/bluetooth/l2cap_core.c18
-rw-r--r--net/bluetooth/mgmt.c212
-rw-r--r--net/bluetooth/smp.c421
9 files changed, 473 insertions, 276 deletions
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index ea7f031f3b04..c2df7bf1d374 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -218,7 +218,7 @@ void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
218 cp.handle = cpu_to_le16(conn->handle); 218 cp.handle = cpu_to_le16(conn->handle);
219 memcpy(cp.ltk, ltk, sizeof(cp.ltk)); 219 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
220 cp.ediv = ediv; 220 cp.ediv = ediv;
221 memcpy(cp.rand, rand, sizeof(rand)); 221 memcpy(cp.rand, rand, sizeof(cp.rand));
222 222
223 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 223 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
224} 224}
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 56943add45cc..b84458dcc226 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -1312,59 +1312,41 @@ int hci_blacklist_clear(struct hci_dev *hdev)
1312int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr) 1312int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr)
1313{ 1313{
1314 struct bdaddr_list *entry; 1314 struct bdaddr_list *entry;
1315 int err;
1316 1315
1317 if (bacmp(bdaddr, BDADDR_ANY) == 0) 1316 if (bacmp(bdaddr, BDADDR_ANY) == 0)
1318 return -EBADF; 1317 return -EBADF;
1319 1318
1320 hci_dev_lock_bh(hdev); 1319 if (hci_blacklist_lookup(hdev, bdaddr))
1321 1320 return -EEXIST;
1322 if (hci_blacklist_lookup(hdev, bdaddr)) {
1323 err = -EEXIST;
1324 goto err;
1325 }
1326 1321
1327 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); 1322 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
1328 if (!entry) { 1323 if (!entry)
1329 err = -ENOMEM; 1324 return -ENOMEM;
1330 goto err;
1331 }
1332 1325
1333 bacpy(&entry->bdaddr, bdaddr); 1326 bacpy(&entry->bdaddr, bdaddr);
1334 1327
1335 list_add(&entry->list, &hdev->blacklist); 1328 list_add(&entry->list, &hdev->blacklist);
1336 1329
1337 err = 0; 1330 return mgmt_device_blocked(hdev->id, bdaddr);
1338
1339err:
1340 hci_dev_unlock_bh(hdev);
1341 return err;
1342} 1331}
1343 1332
1344int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr) 1333int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr)
1345{ 1334{
1346 struct bdaddr_list *entry; 1335 struct bdaddr_list *entry;
1347 int err = 0;
1348
1349 hci_dev_lock_bh(hdev);
1350 1336
1351 if (bacmp(bdaddr, BDADDR_ANY) == 0) { 1337 if (bacmp(bdaddr, BDADDR_ANY) == 0) {
1352 hci_blacklist_clear(hdev); 1338 return hci_blacklist_clear(hdev);
1353 goto done;
1354 } 1339 }
1355 1340
1356 entry = hci_blacklist_lookup(hdev, bdaddr); 1341 entry = hci_blacklist_lookup(hdev, bdaddr);
1357 if (!entry) { 1342 if (!entry) {
1358 err = -ENOENT; 1343 return -ENOENT;
1359 goto done;
1360 } 1344 }
1361 1345
1362 list_del(&entry->list); 1346 list_del(&entry->list);
1363 kfree(entry); 1347 kfree(entry);
1364 1348
1365done: 1349 return mgmt_device_unblocked(hdev->id, bdaddr);
1366 hci_dev_unlock_bh(hdev);
1367 return err;
1368} 1350}
1369 1351
1370static void hci_clear_adv_cache(unsigned long arg) 1352static void hci_clear_adv_cache(unsigned long arg)
@@ -1523,11 +1505,6 @@ int hci_register_dev(struct hci_dev *hdev)
1523 if (!hdev->workqueue) 1505 if (!hdev->workqueue)
1524 goto nomem; 1506 goto nomem;
1525 1507
1526 hdev->tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1527 if (IS_ERR(hdev->tfm))
1528 BT_INFO("Failed to load transform for ecb(aes): %ld",
1529 PTR_ERR(hdev->tfm));
1530
1531 hci_register_sysfs(hdev); 1508 hci_register_sysfs(hdev);
1532 1509
1533 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev, 1510 hdev->rfkill = rfkill_alloc(hdev->name, &hdev->dev,
@@ -1576,9 +1553,6 @@ int hci_unregister_dev(struct hci_dev *hdev)
1576 !test_bit(HCI_SETUP, &hdev->flags)) 1553 !test_bit(HCI_SETUP, &hdev->flags))
1577 mgmt_index_removed(hdev->id); 1554 mgmt_index_removed(hdev->id);
1578 1555
1579 if (!IS_ERR(hdev->tfm))
1580 crypto_free_blkcipher(hdev->tfm);
1581
1582 hci_notify(hdev, HCI_DEV_UNREG); 1556 hci_notify(hdev, HCI_DEV_UNREG);
1583 1557
1584 if (hdev->rfkill) { 1558 if (hdev->rfkill) {
@@ -2074,6 +2048,9 @@ static inline struct hci_conn *hci_low_sent(struct hci_dev *hdev, __u8 type, int
2074 min = c->sent; 2048 min = c->sent;
2075 conn = c; 2049 conn = c;
2076 } 2050 }
2051
2052 if (hci_conn_num(hdev, type) == num)
2053 break;
2077 } 2054 }
2078 2055
2079 if (conn) { 2056 if (conn) {
@@ -2131,6 +2108,9 @@ static inline void hci_sched_acl(struct hci_dev *hdev)
2131 2108
2132 BT_DBG("%s", hdev->name); 2109 BT_DBG("%s", hdev->name);
2133 2110
2111 if (!hci_conn_num(hdev, ACL_LINK))
2112 return;
2113
2134 if (!test_bit(HCI_RAW, &hdev->flags)) { 2114 if (!test_bit(HCI_RAW, &hdev->flags)) {
2135 /* ACL tx timeout must be longer than maximum 2115 /* ACL tx timeout must be longer than maximum
2136 * link supervision timeout (40.9 seconds) */ 2116 * link supervision timeout (40.9 seconds) */
@@ -2162,6 +2142,9 @@ static inline void hci_sched_sco(struct hci_dev *hdev)
2162 2142
2163 BT_DBG("%s", hdev->name); 2143 BT_DBG("%s", hdev->name);
2164 2144
2145 if (!hci_conn_num(hdev, SCO_LINK))
2146 return;
2147
2165 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) { 2148 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
2166 while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 2149 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2167 BT_DBG("skb %p len %d", skb, skb->len); 2150 BT_DBG("skb %p len %d", skb, skb->len);
@@ -2182,6 +2165,9 @@ static inline void hci_sched_esco(struct hci_dev *hdev)
2182 2165
2183 BT_DBG("%s", hdev->name); 2166 BT_DBG("%s", hdev->name);
2184 2167
2168 if (!hci_conn_num(hdev, ESCO_LINK))
2169 return;
2170
2185 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) { 2171 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, ESCO_LINK, &quote))) {
2186 while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 2172 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
2187 BT_DBG("skb %p len %d", skb, skb->len); 2173 BT_DBG("skb %p len %d", skb, skb->len);
@@ -2202,6 +2188,9 @@ static inline void hci_sched_le(struct hci_dev *hdev)
2202 2188
2203 BT_DBG("%s", hdev->name); 2189 BT_DBG("%s", hdev->name);
2204 2190
2191 if (!hci_conn_num(hdev, LE_LINK))
2192 return;
2193
2205 if (!test_bit(HCI_RAW, &hdev->flags)) { 2194 if (!test_bit(HCI_RAW, &hdev->flags)) {
2206 /* LE tx timeout must be longer than maximum 2195 /* LE tx timeout must be longer than maximum
2207 * link supervision timeout (40.9 seconds) */ 2196 * link supervision timeout (40.9 seconds) */
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 7ef4eb4435fb..35083f2aa2ea 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -898,16 +898,15 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
898 if (!cp) 898 if (!cp)
899 return; 899 return;
900 900
901 hci_dev_lock(hdev);
902
903 if (cp->enable == 0x01) { 901 if (cp->enable == 0x01) {
904 del_timer(&hdev->adv_timer); 902 del_timer(&hdev->adv_timer);
903
904 hci_dev_lock(hdev);
905 hci_adv_entries_clear(hdev); 905 hci_adv_entries_clear(hdev);
906 hci_dev_unlock(hdev);
906 } else if (cp->enable == 0x00) { 907 } else if (cp->enable == 0x00) {
907 mod_timer(&hdev->adv_timer, jiffies + ADV_CLEAR_TIMEOUT); 908 mod_timer(&hdev->adv_timer, jiffies + ADV_CLEAR_TIMEOUT);
908 } 909 }
909
910 hci_dev_unlock(hdev);
911} 910}
912 911
913static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb) 912static void hci_cc_le_ltk_reply(struct hci_dev *hdev, struct sk_buff *skb)
@@ -1103,9 +1102,10 @@ static int hci_outgoing_auth_needed(struct hci_dev *hdev,
1103 return 0; 1102 return 0;
1104 1103
1105 /* Only request authentication for SSP connections or non-SSP 1104 /* Only request authentication for SSP connections or non-SSP
1106 * devices with sec_level HIGH */ 1105 * devices with sec_level HIGH or if MITM protection is requested */
1107 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) && 1106 if (!(hdev->ssp_mode > 0 && conn->ssp_mode > 0) &&
1108 conn->pending_sec_level != BT_SECURITY_HIGH) 1107 conn->pending_sec_level != BT_SECURITY_HIGH &&
1108 !(conn->auth_type & 0x01))
1109 return 0; 1109 return 0;
1110 1110
1111 return 1; 1111 return 1;
@@ -1412,7 +1412,7 @@ static inline void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *s
1412 conn->state = BT_CONFIG; 1412 conn->state = BT_CONFIG;
1413 hci_conn_hold(conn); 1413 hci_conn_hold(conn);
1414 conn->disc_timeout = HCI_DISCONN_TIMEOUT; 1414 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
1415 mgmt_connected(hdev->id, &ev->bdaddr); 1415 mgmt_connected(hdev->id, &ev->bdaddr, conn->type);
1416 } else 1416 } else
1417 conn->state = BT_CONNECTED; 1417 conn->state = BT_CONNECTED;
1418 1418
@@ -2816,7 +2816,7 @@ static inline void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff
2816 goto unlock; 2816 goto unlock;
2817 } 2817 }
2818 2818
2819 mgmt_connected(hdev->id, &ev->bdaddr); 2819 mgmt_connected(hdev->id, &ev->bdaddr, conn->type);
2820 2820
2821 conn->sec_level = BT_SECURITY_LOW; 2821 conn->sec_level = BT_SECURITY_LOW;
2822 conn->handle = __le16_to_cpu(ev->handle); 2822 conn->handle = __le16_to_cpu(ev->handle);
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index ff02cf5e77cc..f6afe3d76a66 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -183,21 +183,35 @@ static int hci_sock_release(struct socket *sock)
183static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg) 183static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
184{ 184{
185 bdaddr_t bdaddr; 185 bdaddr_t bdaddr;
186 int err;
186 187
187 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 188 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
188 return -EFAULT; 189 return -EFAULT;
189 190
190 return hci_blacklist_add(hdev, &bdaddr); 191 hci_dev_lock_bh(hdev);
192
193 err = hci_blacklist_add(hdev, &bdaddr);
194
195 hci_dev_unlock_bh(hdev);
196
197 return err;
191} 198}
192 199
193static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg) 200static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
194{ 201{
195 bdaddr_t bdaddr; 202 bdaddr_t bdaddr;
203 int err;
196 204
197 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr))) 205 if (copy_from_user(&bdaddr, arg, sizeof(bdaddr)))
198 return -EFAULT; 206 return -EFAULT;
199 207
200 return hci_blacklist_del(hdev, &bdaddr); 208 hci_dev_lock_bh(hdev);
209
210 err = hci_blacklist_del(hdev, &bdaddr);
211
212 hci_dev_unlock_bh(hdev);
213
214 return err;
201} 215}
202 216
203/* Ioctls that require bound socket */ 217/* Ioctls that require bound socket */
diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c
index a6c3aa8be1f7..22f1a6c87035 100644
--- a/net/bluetooth/hci_sysfs.c
+++ b/net/bluetooth/hci_sysfs.c
@@ -23,6 +23,8 @@ static inline char *link_typetostr(int type)
23 return "SCO"; 23 return "SCO";
24 case ESCO_LINK: 24 case ESCO_LINK:
25 return "eSCO"; 25 return "eSCO";
26 case LE_LINK:
27 return "LE";
26 default: 28 default:
27 return "UNKNOWN"; 29 return "UNKNOWN";
28 } 30 }
diff --git a/net/bluetooth/hidp/core.c b/net/bluetooth/hidp/core.c
index fb68f344c34a..b83979c548b2 100644
--- a/net/bluetooth/hidp/core.c
+++ b/net/bluetooth/hidp/core.c
@@ -872,6 +872,9 @@ static int hidp_start(struct hid_device *hid)
872 struct hidp_session *session = hid->driver_data; 872 struct hidp_session *session = hid->driver_data;
873 struct hid_report *report; 873 struct hid_report *report;
874 874
875 if (hid->quirks & HID_QUIRK_NO_INIT_REPORTS)
876 return 0;
877
875 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT]. 878 list_for_each_entry(report, &hid->report_enum[HID_INPUT_REPORT].
876 report_list, list) 879 report_list, list)
877 hidp_send_report(session, report); 880 hidp_send_report(session, report);
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index b3bdb482bbe6..1611b3544bb1 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -907,6 +907,9 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
907 if (!conn->hcon->out && conn->hcon->type == LE_LINK) 907 if (!conn->hcon->out && conn->hcon->type == LE_LINK)
908 l2cap_le_conn_ready(conn); 908 l2cap_le_conn_ready(conn);
909 909
910 if (conn->hcon->out && conn->hcon->type == LE_LINK)
911 smp_conn_security(conn, conn->hcon->pending_sec_level);
912
910 read_lock(&conn->chan_lock); 913 read_lock(&conn->chan_lock);
911 914
912 list_for_each_entry(chan, &conn->chan_l, list) { 915 list_for_each_entry(chan, &conn->chan_l, list) {
@@ -986,8 +989,10 @@ static void l2cap_conn_del(struct hci_conn *hcon, int err)
986 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 989 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
987 del_timer_sync(&conn->info_timer); 990 del_timer_sync(&conn->info_timer);
988 991
989 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) 992 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) {
990 del_timer(&conn->security_timer); 993 del_timer(&conn->security_timer);
994 smp_chan_destroy(conn);
995 }
991 996
992 hcon->l2cap_data = NULL; 997 hcon->l2cap_data = NULL;
993 kfree(conn); 998 kfree(conn);
@@ -1519,7 +1524,9 @@ struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *m
1519 return skb; 1524 return skb;
1520} 1525}
1521 1526
1522struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 1527static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
1528 struct msghdr *msg, size_t len,
1529 u16 control, u16 sdulen)
1523{ 1530{
1524 struct sock *sk = chan->sk; 1531 struct sock *sk = chan->sk;
1525 struct l2cap_conn *conn = chan->conn; 1532 struct l2cap_conn *conn = chan->conn;
@@ -4093,6 +4100,11 @@ static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
4093 4100
4094 BT_DBG("conn %p", conn); 4101 BT_DBG("conn %p", conn);
4095 4102
4103 if (hcon->type == LE_LINK) {
4104 smp_distribute_keys(conn, 0);
4105 del_timer(&conn->security_timer);
4106 }
4107
4096 read_lock(&conn->chan_lock); 4108 read_lock(&conn->chan_lock);
4097 4109
4098 list_for_each_entry(chan, &conn->chan_l, list) { 4110 list_for_each_entry(chan, &conn->chan_l, list) {
@@ -4105,9 +4117,7 @@ static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
4105 if (chan->scid == L2CAP_CID_LE_DATA) { 4117 if (chan->scid == L2CAP_CID_LE_DATA) {
4106 if (!status && encrypt) { 4118 if (!status && encrypt) {
4107 chan->sec_level = hcon->sec_level; 4119 chan->sec_level = hcon->sec_level;
4108 del_timer(&conn->security_timer);
4109 l2cap_chan_ready(sk); 4120 l2cap_chan_ready(sk);
4110 smp_distribute_keys(conn, 0);
4111 } 4121 }
4112 4122
4113 bh_unlock_sock(sk); 4123 bh_unlock_sock(sk);
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 53e109eb043e..5a94eec06caa 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -908,7 +908,7 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)
908 struct hci_dev *hdev; 908 struct hci_dev *hdev;
909 struct mgmt_cp_load_keys *cp; 909 struct mgmt_cp_load_keys *cp;
910 u16 key_count, expected_len; 910 u16 key_count, expected_len;
911 int i, err; 911 int i;
912 912
913 cp = (void *) data; 913 cp = (void *) data;
914 914
@@ -918,9 +918,9 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)
918 key_count = get_unaligned_le16(&cp->key_count); 918 key_count = get_unaligned_le16(&cp->key_count);
919 919
920 expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info); 920 expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info);
921 if (expected_len > len) { 921 if (expected_len != len) {
922 BT_ERR("load_keys: expected at least %u bytes, got %u bytes", 922 BT_ERR("load_keys: expected %u bytes, got %u bytes",
923 expected_len, len); 923 len, expected_len);
924 return -EINVAL; 924 return -EINVAL;
925 } 925 }
926 926
@@ -942,36 +942,17 @@ static int load_keys(struct sock *sk, u16 index, unsigned char *data, u16 len)
942 else 942 else
943 clear_bit(HCI_DEBUG_KEYS, &hdev->flags); 943 clear_bit(HCI_DEBUG_KEYS, &hdev->flags);
944 944
945 len -= sizeof(*cp); 945 for (i = 0; i < key_count; i++) {
946 i = 0; 946 struct mgmt_key_info *key = &cp->keys[i];
947
948 while (i < len) {
949 struct mgmt_key_info *key = (void *) cp->keys + i;
950
951 i += sizeof(*key) + key->dlen;
952
953 if (key->type == HCI_LK_SMP_LTK) {
954 struct key_master_id *id = (void *) key->data;
955
956 if (key->dlen != sizeof(struct key_master_id))
957 continue;
958
959 hci_add_ltk(hdev, 0, &key->bdaddr, key->pin_len,
960 id->ediv, id->rand, key->val);
961
962 continue;
963 }
964 947
965 hci_add_link_key(hdev, NULL, 0, &key->bdaddr, key->val, key->type, 948 hci_add_link_key(hdev, NULL, 0, &key->bdaddr, key->val, key->type,
966 key->pin_len); 949 key->pin_len);
967 } 950 }
968 951
969 err = cmd_complete(sk, index, MGMT_OP_LOAD_KEYS, NULL, 0);
970
971 hci_dev_unlock_bh(hdev); 952 hci_dev_unlock_bh(hdev);
972 hci_dev_put(hdev); 953 hci_dev_put(hdev);
973 954
974 return err; 955 return 0;
975} 956}
976 957
977static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len) 958static int remove_key(struct sock *sk, u16 index, unsigned char *data, u16 len)
@@ -1347,6 +1328,7 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len)
1347 struct hci_dev *hdev; 1328 struct hci_dev *hdev;
1348 struct mgmt_cp_pair_device *cp; 1329 struct mgmt_cp_pair_device *cp;
1349 struct pending_cmd *cmd; 1330 struct pending_cmd *cmd;
1331 struct adv_entry *entry;
1350 u8 sec_level, auth_type; 1332 u8 sec_level, auth_type;
1351 struct hci_conn *conn; 1333 struct hci_conn *conn;
1352 int err; 1334 int err;
@@ -1364,15 +1346,20 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len)
1364 1346
1365 hci_dev_lock_bh(hdev); 1347 hci_dev_lock_bh(hdev);
1366 1348
1367 if (cp->io_cap == 0x03) { 1349 sec_level = BT_SECURITY_MEDIUM;
1368 sec_level = BT_SECURITY_MEDIUM; 1350 if (cp->io_cap == 0x03)
1369 auth_type = HCI_AT_DEDICATED_BONDING; 1351 auth_type = HCI_AT_DEDICATED_BONDING;
1370 } else { 1352 else
1371 sec_level = BT_SECURITY_HIGH;
1372 auth_type = HCI_AT_DEDICATED_BONDING_MITM; 1353 auth_type = HCI_AT_DEDICATED_BONDING_MITM;
1373 }
1374 1354
1375 conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level, auth_type); 1355 entry = hci_find_adv_entry(hdev, &cp->bdaddr);
1356 if (entry)
1357 conn = hci_connect(hdev, LE_LINK, &cp->bdaddr, sec_level,
1358 auth_type);
1359 else
1360 conn = hci_connect(hdev, ACL_LINK, &cp->bdaddr, sec_level,
1361 auth_type);
1362
1376 if (IS_ERR(conn)) { 1363 if (IS_ERR(conn)) {
1377 err = PTR_ERR(conn); 1364 err = PTR_ERR(conn);
1378 goto unlock; 1365 goto unlock;
@@ -1391,7 +1378,10 @@ static int pair_device(struct sock *sk, u16 index, unsigned char *data, u16 len)
1391 goto unlock; 1378 goto unlock;
1392 } 1379 }
1393 1380
1394 conn->connect_cfm_cb = pairing_complete_cb; 1381 /* For LE, just connecting isn't a proof that the pairing finished */
1382 if (!entry)
1383 conn->connect_cfm_cb = pairing_complete_cb;
1384
1395 conn->security_cfm_cb = pairing_complete_cb; 1385 conn->security_cfm_cb = pairing_complete_cb;
1396 conn->disconn_cfm_cb = pairing_complete_cb; 1386 conn->disconn_cfm_cb = pairing_complete_cb;
1397 conn->io_capability = cp->io_cap; 1387 conn->io_capability = cp->io_cap;
@@ -1689,13 +1679,12 @@ static int block_device(struct sock *sk, u16 index, unsigned char *data,
1689 u16 len) 1679 u16 len)
1690{ 1680{
1691 struct hci_dev *hdev; 1681 struct hci_dev *hdev;
1692 struct mgmt_cp_block_device *cp; 1682 struct pending_cmd *cmd;
1683 struct mgmt_cp_block_device *cp = (void *) data;
1693 int err; 1684 int err;
1694 1685
1695 BT_DBG("hci%u", index); 1686 BT_DBG("hci%u", index);
1696 1687
1697 cp = (void *) data;
1698
1699 if (len != sizeof(*cp)) 1688 if (len != sizeof(*cp))
1700 return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, 1689 return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE,
1701 EINVAL); 1690 EINVAL);
@@ -1705,6 +1694,14 @@ static int block_device(struct sock *sk, u16 index, unsigned char *data,
1705 return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE, 1694 return cmd_status(sk, index, MGMT_OP_BLOCK_DEVICE,
1706 ENODEV); 1695 ENODEV);
1707 1696
1697 hci_dev_lock_bh(hdev);
1698
1699 cmd = mgmt_pending_add(sk, MGMT_OP_BLOCK_DEVICE, index, NULL, 0);
1700 if (!cmd) {
1701 err = -ENOMEM;
1702 goto failed;
1703 }
1704
1708 err = hci_blacklist_add(hdev, &cp->bdaddr); 1705 err = hci_blacklist_add(hdev, &cp->bdaddr);
1709 1706
1710 if (err < 0) 1707 if (err < 0)
@@ -1712,6 +1709,11 @@ static int block_device(struct sock *sk, u16 index, unsigned char *data,
1712 else 1709 else
1713 err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE, 1710 err = cmd_complete(sk, index, MGMT_OP_BLOCK_DEVICE,
1714 NULL, 0); 1711 NULL, 0);
1712
1713 mgmt_pending_remove(cmd);
1714
1715failed:
1716 hci_dev_unlock_bh(hdev);
1715 hci_dev_put(hdev); 1717 hci_dev_put(hdev);
1716 1718
1717 return err; 1719 return err;
@@ -1721,13 +1723,12 @@ static int unblock_device(struct sock *sk, u16 index, unsigned char *data,
1721 u16 len) 1723 u16 len)
1722{ 1724{
1723 struct hci_dev *hdev; 1725 struct hci_dev *hdev;
1724 struct mgmt_cp_unblock_device *cp; 1726 struct pending_cmd *cmd;
1727 struct mgmt_cp_unblock_device *cp = (void *) data;
1725 int err; 1728 int err;
1726 1729
1727 BT_DBG("hci%u", index); 1730 BT_DBG("hci%u", index);
1728 1731
1729 cp = (void *) data;
1730
1731 if (len != sizeof(*cp)) 1732 if (len != sizeof(*cp))
1732 return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, 1733 return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE,
1733 EINVAL); 1734 EINVAL);
@@ -1737,6 +1738,14 @@ static int unblock_device(struct sock *sk, u16 index, unsigned char *data,
1737 return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE, 1738 return cmd_status(sk, index, MGMT_OP_UNBLOCK_DEVICE,
1738 ENODEV); 1739 ENODEV);
1739 1740
1741 hci_dev_lock_bh(hdev);
1742
1743 cmd = mgmt_pending_add(sk, MGMT_OP_UNBLOCK_DEVICE, index, NULL, 0);
1744 if (!cmd) {
1745 err = -ENOMEM;
1746 goto failed;
1747 }
1748
1740 err = hci_blacklist_del(hdev, &cp->bdaddr); 1749 err = hci_blacklist_del(hdev, &cp->bdaddr);
1741 1750
1742 if (err < 0) 1751 if (err < 0)
@@ -1744,6 +1753,67 @@ static int unblock_device(struct sock *sk, u16 index, unsigned char *data,
1744 else 1753 else
1745 err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE, 1754 err = cmd_complete(sk, index, MGMT_OP_UNBLOCK_DEVICE,
1746 NULL, 0); 1755 NULL, 0);
1756
1757 mgmt_pending_remove(cmd);
1758
1759failed:
1760 hci_dev_unlock_bh(hdev);
1761 hci_dev_put(hdev);
1762
1763 return err;
1764}
1765
1766static int set_fast_connectable(struct sock *sk, u16 index,
1767 unsigned char *data, u16 len)
1768{
1769 struct hci_dev *hdev;
1770 struct mgmt_cp_set_fast_connectable *cp = (void *) data;
1771 struct hci_cp_write_page_scan_activity acp;
1772 u8 type;
1773 int err;
1774
1775 BT_DBG("hci%u", index);
1776
1777 if (len != sizeof(*cp))
1778 return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
1779 EINVAL);
1780
1781 hdev = hci_dev_get(index);
1782 if (!hdev)
1783 return cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
1784 ENODEV);
1785
1786 hci_dev_lock(hdev);
1787
1788 if (cp->enable) {
1789 type = PAGE_SCAN_TYPE_INTERLACED;
1790 acp.interval = 0x0024; /* 22.5 msec page scan interval */
1791 } else {
1792 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1793 acp.interval = 0x0800; /* default 1.28 sec page scan */
1794 }
1795
1796 acp.window = 0x0012; /* default 11.25 msec page scan window */
1797
1798 err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1799 sizeof(acp), &acp);
1800 if (err < 0) {
1801 err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
1802 -err);
1803 goto done;
1804 }
1805
1806 err = hci_send_cmd(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
1807 if (err < 0) {
1808 err = cmd_status(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
1809 -err);
1810 goto done;
1811 }
1812
1813 err = cmd_complete(sk, index, MGMT_OP_SET_FAST_CONNECTABLE,
1814 NULL, 0);
1815done:
1816 hci_dev_unlock(hdev);
1747 hci_dev_put(hdev); 1817 hci_dev_put(hdev);
1748 1818
1749 return err; 1819 return err;
@@ -1869,6 +1939,10 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
1869 case MGMT_OP_UNBLOCK_DEVICE: 1939 case MGMT_OP_UNBLOCK_DEVICE:
1870 err = unblock_device(sk, index, buf + sizeof(*hdr), len); 1940 err = unblock_device(sk, index, buf + sizeof(*hdr), len);
1871 break; 1941 break;
1942 case MGMT_OP_SET_FAST_CONNECTABLE:
1943 err = set_fast_connectable(sk, index, buf + sizeof(*hdr),
1944 len);
1945 break;
1872 default: 1946 default:
1873 BT_DBG("Unknown op %u", opcode); 1947 BT_DBG("Unknown op %u", opcode);
1874 err = cmd_status(sk, index, opcode, 0x01); 1948 err = cmd_status(sk, index, opcode, 0x01);
@@ -1977,35 +2051,25 @@ int mgmt_connectable(u16 index, u8 connectable)
1977 2051
1978int mgmt_new_key(u16 index, struct link_key *key, u8 persistent) 2052int mgmt_new_key(u16 index, struct link_key *key, u8 persistent)
1979{ 2053{
1980 struct mgmt_ev_new_key *ev; 2054 struct mgmt_ev_new_key ev;
1981 int err, total;
1982
1983 total = sizeof(struct mgmt_ev_new_key) + key->dlen;
1984 ev = kzalloc(total, GFP_ATOMIC);
1985 if (!ev)
1986 return -ENOMEM;
1987
1988 bacpy(&ev->key.bdaddr, &key->bdaddr);
1989 ev->key.type = key->type;
1990 memcpy(ev->key.val, key->val, 16);
1991 ev->key.pin_len = key->pin_len;
1992 ev->key.dlen = key->dlen;
1993 ev->store_hint = persistent;
1994 2055
1995 memcpy(ev->key.data, key->data, key->dlen); 2056 memset(&ev, 0, sizeof(ev));
1996
1997 err = mgmt_event(MGMT_EV_NEW_KEY, index, ev, total, NULL);
1998 2057
1999 kfree(ev); 2058 ev.store_hint = persistent;
2059 bacpy(&ev.key.bdaddr, &key->bdaddr);
2060 ev.key.type = key->type;
2061 memcpy(ev.key.val, key->val, 16);
2062 ev.key.pin_len = key->pin_len;
2000 2063
2001 return err; 2064 return mgmt_event(MGMT_EV_NEW_KEY, index, &ev, sizeof(ev), NULL);
2002} 2065}
2003 2066
2004int mgmt_connected(u16 index, bdaddr_t *bdaddr) 2067int mgmt_connected(u16 index, bdaddr_t *bdaddr, u8 link_type)
2005{ 2068{
2006 struct mgmt_ev_connected ev; 2069 struct mgmt_ev_connected ev;
2007 2070
2008 bacpy(&ev.bdaddr, bdaddr); 2071 bacpy(&ev.bdaddr, bdaddr);
2072 ev.link_type = link_type;
2009 2073
2010 return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL); 2074 return mgmt_event(MGMT_EV_CONNECTED, index, &ev, sizeof(ev), NULL);
2011} 2075}
@@ -2260,12 +2324,14 @@ int mgmt_device_found(u16 index, bdaddr_t *bdaddr, u8 *dev_class, s8 rssi,
2260 memset(&ev, 0, sizeof(ev)); 2324 memset(&ev, 0, sizeof(ev));
2261 2325
2262 bacpy(&ev.bdaddr, bdaddr); 2326 bacpy(&ev.bdaddr, bdaddr);
2263 memcpy(ev.dev_class, dev_class, sizeof(ev.dev_class));
2264 ev.rssi = rssi; 2327 ev.rssi = rssi;
2265 2328
2266 if (eir) 2329 if (eir)
2267 memcpy(ev.eir, eir, sizeof(ev.eir)); 2330 memcpy(ev.eir, eir, sizeof(ev.eir));
2268 2331
2332 if (dev_class)
2333 memcpy(ev.dev_class, dev_class, sizeof(ev.dev_class));
2334
2269 return mgmt_event(MGMT_EV_DEVICE_FOUND, index, &ev, sizeof(ev), NULL); 2335 return mgmt_event(MGMT_EV_DEVICE_FOUND, index, &ev, sizeof(ev), NULL);
2270} 2336}
2271 2337
@@ -2286,3 +2352,29 @@ int mgmt_discovering(u16 index, u8 discovering)
2286 return mgmt_event(MGMT_EV_DISCOVERING, index, &discovering, 2352 return mgmt_event(MGMT_EV_DISCOVERING, index, &discovering,
2287 sizeof(discovering), NULL); 2353 sizeof(discovering), NULL);
2288} 2354}
2355
2356int mgmt_device_blocked(u16 index, bdaddr_t *bdaddr)
2357{
2358 struct pending_cmd *cmd;
2359 struct mgmt_ev_device_blocked ev;
2360
2361 cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, index);
2362
2363 bacpy(&ev.bdaddr, bdaddr);
2364
2365 return mgmt_event(MGMT_EV_DEVICE_BLOCKED, index, &ev, sizeof(ev),
2366 cmd ? cmd->sk : NULL);
2367}
2368
2369int mgmt_device_unblocked(u16 index, bdaddr_t *bdaddr)
2370{
2371 struct pending_cmd *cmd;
2372 struct mgmt_ev_device_unblocked ev;
2373
2374 cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, index);
2375
2376 bacpy(&ev.bdaddr, bdaddr);
2377
2378 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, index, &ev, sizeof(ev),
2379 cmd ? cmd->sk : NULL);
2380}
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 391888b88a92..759b63572641 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -182,18 +182,9 @@ static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
182 return; 182 return;
183 183
184 hci_send_acl(conn->hcon, skb, 0); 184 hci_send_acl(conn->hcon, skb, 0);
185}
186
187static __u8 seclevel_to_authreq(__u8 level)
188{
189 switch (level) {
190 case BT_SECURITY_HIGH:
191 /* Right now we don't support bonding */
192 return SMP_AUTH_MITM;
193 185
194 default: 186 mod_timer(&conn->security_timer, jiffies +
195 return SMP_AUTH_NONE; 187 msecs_to_jiffies(SMP_TIMEOUT));
196 }
197} 188}
198 189
199static void build_pairing_cmd(struct l2cap_conn *conn, 190static void build_pairing_cmd(struct l2cap_conn *conn,
@@ -205,7 +196,7 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
205 196
206 dist_keys = 0; 197 dist_keys = 0;
207 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) { 198 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) {
208 dist_keys = SMP_DIST_ENC_KEY | SMP_DIST_ID_KEY | SMP_DIST_SIGN; 199 dist_keys = SMP_DIST_ENC_KEY;
209 authreq |= SMP_AUTH_BONDING; 200 authreq |= SMP_AUTH_BONDING;
210 } 201 }
211 202
@@ -229,24 +220,184 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
229 220
230static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) 221static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
231{ 222{
223 struct smp_chan *smp = conn->smp_chan;
224
232 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || 225 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
233 (max_key_size < SMP_MIN_ENC_KEY_SIZE)) 226 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
234 return SMP_ENC_KEY_SIZE; 227 return SMP_ENC_KEY_SIZE;
235 228
236 conn->smp_key_size = max_key_size; 229 smp->smp_key_size = max_key_size;
237 230
238 return 0; 231 return 0;
239} 232}
240 233
234static void confirm_work(struct work_struct *work)
235{
236 struct smp_chan *smp = container_of(work, struct smp_chan, confirm);
237 struct l2cap_conn *conn = smp->conn;
238 struct crypto_blkcipher *tfm;
239 struct smp_cmd_pairing_confirm cp;
240 int ret;
241 u8 res[16], reason;
242
243 BT_DBG("conn %p", conn);
244
245 tfm = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
246 if (IS_ERR(tfm)) {
247 reason = SMP_UNSPECIFIED;
248 goto error;
249 }
250
251 smp->tfm = tfm;
252
253 if (conn->hcon->out)
254 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0,
255 conn->src, conn->hcon->dst_type, conn->dst,
256 res);
257 else
258 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
259 conn->hcon->dst_type, conn->dst, 0, conn->src,
260 res);
261 if (ret) {
262 reason = SMP_UNSPECIFIED;
263 goto error;
264 }
265
266 swap128(res, cp.confirm_val);
267 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
268
269 return;
270
271error:
272 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), &reason);
273 smp_chan_destroy(conn);
274}
275
276static void random_work(struct work_struct *work)
277{
278 struct smp_chan *smp = container_of(work, struct smp_chan, random);
279 struct l2cap_conn *conn = smp->conn;
280 struct hci_conn *hcon = conn->hcon;
281 struct crypto_blkcipher *tfm = smp->tfm;
282 u8 reason, confirm[16], res[16], key[16];
283 int ret;
284
285 if (IS_ERR_OR_NULL(tfm)) {
286 reason = SMP_UNSPECIFIED;
287 goto error;
288 }
289
290 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
291
292 if (hcon->out)
293 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0,
294 conn->src, hcon->dst_type, conn->dst,
295 res);
296 else
297 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
298 hcon->dst_type, conn->dst, 0, conn->src,
299 res);
300 if (ret) {
301 reason = SMP_UNSPECIFIED;
302 goto error;
303 }
304
305 swap128(res, confirm);
306
307 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
308 BT_ERR("Pairing failed (confirmation values mismatch)");
309 reason = SMP_CONFIRM_FAILED;
310 goto error;
311 }
312
313 if (hcon->out) {
314 u8 stk[16], rand[8];
315 __le16 ediv;
316
317 memset(rand, 0, sizeof(rand));
318 ediv = 0;
319
320 smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key);
321 swap128(key, stk);
322
323 memset(stk + smp->smp_key_size, 0,
324 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
325
326 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) {
327 reason = SMP_UNSPECIFIED;
328 goto error;
329 }
330
331 hci_le_start_enc(hcon, ediv, rand, stk);
332 hcon->enc_key_size = smp->smp_key_size;
333 } else {
334 u8 stk[16], r[16], rand[8];
335 __le16 ediv;
336
337 memset(rand, 0, sizeof(rand));
338 ediv = 0;
339
340 swap128(smp->prnd, r);
341 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
342
343 smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key);
344 swap128(key, stk);
345
346 memset(stk + smp->smp_key_size, 0,
347 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
348
349 hci_add_ltk(hcon->hdev, 0, conn->dst, smp->smp_key_size,
350 ediv, rand, stk);
351 }
352
353 return;
354
355error:
356 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), &reason);
357 smp_chan_destroy(conn);
358}
359
360static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
361{
362 struct smp_chan *smp;
363
364 smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC);
365 if (!smp)
366 return NULL;
367
368 INIT_WORK(&smp->confirm, confirm_work);
369 INIT_WORK(&smp->random, random_work);
370
371 smp->conn = conn;
372 conn->smp_chan = smp;
373
374 hci_conn_hold(conn->hcon);
375
376 return smp;
377}
378
379void smp_chan_destroy(struct l2cap_conn *conn)
380{
381 kfree(conn->smp_chan);
382 hci_conn_put(conn->hcon);
383}
384
241static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) 385static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
242{ 386{
243 struct smp_cmd_pairing rsp, *req = (void *) skb->data; 387 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
388 struct smp_chan *smp;
244 u8 key_size; 389 u8 key_size;
390 int ret;
245 391
246 BT_DBG("conn %p", conn); 392 BT_DBG("conn %p", conn);
247 393
248 conn->preq[0] = SMP_CMD_PAIRING_REQ; 394 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
249 memcpy(&conn->preq[1], req, sizeof(*req)); 395 smp = smp_chan_create(conn);
396
397 smp = conn->smp_chan;
398
399 smp->preq[0] = SMP_CMD_PAIRING_REQ;
400 memcpy(&smp->preq[1], req, sizeof(*req));
250 skb_pull(skb, sizeof(*req)); 401 skb_pull(skb, sizeof(*req));
251 402
252 if (req->oob_flag) 403 if (req->oob_flag)
@@ -260,32 +411,33 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
260 return SMP_ENC_KEY_SIZE; 411 return SMP_ENC_KEY_SIZE;
261 412
262 /* Just works */ 413 /* Just works */
263 memset(conn->tk, 0, sizeof(conn->tk)); 414 memset(smp->tk, 0, sizeof(smp->tk));
415
416 ret = smp_rand(smp->prnd);
417 if (ret)
418 return SMP_UNSPECIFIED;
264 419
265 conn->prsp[0] = SMP_CMD_PAIRING_RSP; 420 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
266 memcpy(&conn->prsp[1], &rsp, sizeof(rsp)); 421 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
267 422
268 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); 423 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
269 424
270 mod_timer(&conn->security_timer, jiffies +
271 msecs_to_jiffies(SMP_TIMEOUT));
272
273 return 0; 425 return 0;
274} 426}
275 427
276static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) 428static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
277{ 429{
278 struct smp_cmd_pairing *req, *rsp = (void *) skb->data; 430 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
279 struct smp_cmd_pairing_confirm cp; 431 struct smp_chan *smp = conn->smp_chan;
280 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm; 432 struct hci_dev *hdev = conn->hcon->hdev;
433 u8 key_size;
281 int ret; 434 int ret;
282 u8 res[16], key_size;
283 435
284 BT_DBG("conn %p", conn); 436 BT_DBG("conn %p", conn);
285 437
286 skb_pull(skb, sizeof(*rsp)); 438 skb_pull(skb, sizeof(*rsp));
287 439
288 req = (void *) &conn->preq[1]; 440 req = (void *) &smp->preq[1];
289 441
290 key_size = min(req->max_key_size, rsp->max_key_size); 442 key_size = min(req->max_key_size, rsp->max_key_size);
291 if (check_enc_key_size(conn, key_size)) 443 if (check_enc_key_size(conn, key_size))
@@ -295,222 +447,154 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
295 return SMP_OOB_NOT_AVAIL; 447 return SMP_OOB_NOT_AVAIL;
296 448
297 /* Just works */ 449 /* Just works */
298 memset(conn->tk, 0, sizeof(conn->tk)); 450 memset(smp->tk, 0, sizeof(smp->tk));
299
300 conn->prsp[0] = SMP_CMD_PAIRING_RSP;
301 memcpy(&conn->prsp[1], rsp, sizeof(*rsp));
302
303 ret = smp_rand(conn->prnd);
304 if (ret)
305 return SMP_UNSPECIFIED;
306 451
307 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp, 0, 452 ret = smp_rand(smp->prnd);
308 conn->src, conn->hcon->dst_type, conn->dst, res);
309 if (ret) 453 if (ret)
310 return SMP_UNSPECIFIED; 454 return SMP_UNSPECIFIED;
311 455
312 swap128(res, cp.confirm_val); 456 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
457 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
313 458
314 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); 459 queue_work(hdev->workqueue, &smp->confirm);
315 460
316 return 0; 461 return 0;
317} 462}
318 463
319static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 464static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
320{ 465{
321 struct crypto_blkcipher *tfm = conn->hcon->hdev->tfm; 466 struct smp_chan *smp = conn->smp_chan;
467 struct hci_dev *hdev = conn->hcon->hdev;
322 468
323 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 469 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
324 470
325 memcpy(conn->pcnf, skb->data, sizeof(conn->pcnf)); 471 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
326 skb_pull(skb, sizeof(conn->pcnf)); 472 skb_pull(skb, sizeof(smp->pcnf));
327 473
328 if (conn->hcon->out) { 474 if (conn->hcon->out) {
329 u8 random[16]; 475 u8 random[16];
330 476
331 swap128(conn->prnd, random); 477 swap128(smp->prnd, random);
332 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), 478 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
333 random); 479 random);
334 } else { 480 } else {
335 struct smp_cmd_pairing_confirm cp; 481 queue_work(hdev->workqueue, &smp->confirm);
336 int ret;
337 u8 res[16];
338
339 ret = smp_rand(conn->prnd);
340 if (ret)
341 return SMP_UNSPECIFIED;
342
343 ret = smp_c1(tfm, conn->tk, conn->prnd, conn->preq, conn->prsp,
344 conn->hcon->dst_type, conn->dst,
345 0, conn->src, res);
346 if (ret)
347 return SMP_CONFIRM_FAILED;
348
349 swap128(res, cp.confirm_val);
350
351 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
352 } 482 }
353 483
354 mod_timer(&conn->security_timer, jiffies +
355 msecs_to_jiffies(SMP_TIMEOUT));
356
357 return 0; 484 return 0;
358} 485}
359 486
360static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) 487static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
361{ 488{
362 struct hci_conn *hcon = conn->hcon; 489 struct smp_chan *smp = conn->smp_chan;
363 struct crypto_blkcipher *tfm = hcon->hdev->tfm; 490 struct hci_dev *hdev = conn->hcon->hdev;
364 int ret;
365 u8 key[16], res[16], random[16], confirm[16];
366 491
367 swap128(skb->data, random); 492 BT_DBG("conn %p", conn);
368 skb_pull(skb, sizeof(random));
369
370 if (conn->hcon->out)
371 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp, 0,
372 conn->src, conn->hcon->dst_type, conn->dst,
373 res);
374 else
375 ret = smp_c1(tfm, conn->tk, random, conn->preq, conn->prsp,
376 conn->hcon->dst_type, conn->dst, 0, conn->src,
377 res);
378 if (ret)
379 return SMP_UNSPECIFIED;
380
381 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
382
383 swap128(res, confirm);
384
385 if (memcmp(conn->pcnf, confirm, sizeof(conn->pcnf)) != 0) {
386 BT_ERR("Pairing failed (confirmation values mismatch)");
387 return SMP_CONFIRM_FAILED;
388 }
389
390 if (conn->hcon->out) {
391 u8 stk[16], rand[8];
392 __le16 ediv;
393
394 memset(rand, 0, sizeof(rand));
395 ediv = 0;
396 493
397 smp_s1(tfm, conn->tk, random, conn->prnd, key); 494 swap128(skb->data, smp->rrnd);
398 swap128(key, stk); 495 skb_pull(skb, sizeof(smp->rrnd));
399 496
400 memset(stk + conn->smp_key_size, 0, 497 queue_work(hdev->workqueue, &smp->random);
401 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size);
402 498
403 hci_le_start_enc(hcon, ediv, rand, stk); 499 return 0;
404 hcon->enc_key_size = conn->smp_key_size; 500}
405 } else {
406 u8 stk[16], r[16], rand[8];
407 __le16 ediv;
408 501
409 memset(rand, 0, sizeof(rand)); 502static u8 smp_ltk_encrypt(struct l2cap_conn *conn)
410 ediv = 0; 503{
504 struct link_key *key;
505 struct key_master_id *master;
506 struct hci_conn *hcon = conn->hcon;
411 507
412 swap128(conn->prnd, r); 508 key = hci_find_link_key_type(hcon->hdev, conn->dst,
413 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); 509 HCI_LK_SMP_LTK);
510 if (!key)
511 return 0;
414 512
415 smp_s1(tfm, conn->tk, conn->prnd, random, key); 513 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND,
416 swap128(key, stk); 514 &hcon->pend))
515 return 1;
417 516
418 memset(stk + conn->smp_key_size, 0, 517 master = (void *) key->data;
419 SMP_MAX_ENC_KEY_SIZE - conn->smp_key_size); 518 hci_le_start_enc(hcon, master->ediv, master->rand,
519 key->val);
520 hcon->enc_key_size = key->pin_len;
420 521
421 hci_add_ltk(conn->hcon->hdev, 0, conn->dst, conn->smp_key_size, 522 return 1;
422 ediv, rand, stk);
423 }
424 523
425 return 0;
426} 524}
427
428static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) 525static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
429{ 526{
430 struct smp_cmd_security_req *rp = (void *) skb->data; 527 struct smp_cmd_security_req *rp = (void *) skb->data;
431 struct smp_cmd_pairing cp; 528 struct smp_cmd_pairing cp;
432 struct hci_conn *hcon = conn->hcon; 529 struct hci_conn *hcon = conn->hcon;
530 struct smp_chan *smp;
433 531
434 BT_DBG("conn %p", conn); 532 BT_DBG("conn %p", conn);
435 533
436 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) 534 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
535
536 if (smp_ltk_encrypt(conn))
437 return 0; 537 return 0;
438 538
539 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
540 return 0;
541
542 smp = smp_chan_create(conn);
543
439 skb_pull(skb, sizeof(*rp)); 544 skb_pull(skb, sizeof(*rp));
440 545
441 memset(&cp, 0, sizeof(cp)); 546 memset(&cp, 0, sizeof(cp));
442 build_pairing_cmd(conn, &cp, NULL, rp->auth_req); 547 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
443 548
444 conn->preq[0] = SMP_CMD_PAIRING_REQ; 549 smp->preq[0] = SMP_CMD_PAIRING_REQ;
445 memcpy(&conn->preq[1], &cp, sizeof(cp)); 550 memcpy(&smp->preq[1], &cp, sizeof(cp));
446 551
447 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 552 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
448 553
449 mod_timer(&conn->security_timer, jiffies +
450 msecs_to_jiffies(SMP_TIMEOUT));
451
452 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
453
454 return 0; 554 return 0;
455} 555}
456 556
457int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level) 557int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
458{ 558{
459 struct hci_conn *hcon = conn->hcon; 559 struct hci_conn *hcon = conn->hcon;
460 __u8 authreq; 560 struct smp_chan *smp = conn->smp_chan;
461 561
462 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); 562 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
463 563
464 if (!lmp_host_le_capable(hcon->hdev)) 564 if (!lmp_host_le_capable(hcon->hdev))
465 return 1; 565 return 1;
466 566
467 if (IS_ERR(hcon->hdev->tfm))
468 return 1;
469
470 if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
471 return 0;
472
473 if (sec_level == BT_SECURITY_LOW) 567 if (sec_level == BT_SECURITY_LOW)
474 return 1; 568 return 1;
475 569
476 if (hcon->sec_level >= sec_level) 570 if (hcon->sec_level >= sec_level)
477 return 1; 571 return 1;
478 572
479 authreq = seclevel_to_authreq(sec_level); 573 if (hcon->link_mode & HCI_LM_MASTER)
480 574 if (smp_ltk_encrypt(conn))
481 if (hcon->link_mode & HCI_LM_MASTER) { 575 goto done;
482 struct smp_cmd_pairing cp;
483 struct link_key *key;
484 576
485 key = hci_find_link_key_type(hcon->hdev, conn->dst, 577 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
486 HCI_LK_SMP_LTK); 578 return 0;
487 if (key) {
488 struct key_master_id *master = (void *) key->data;
489 579
490 hci_le_start_enc(hcon, master->ediv, master->rand, 580 smp = smp_chan_create(conn);
491 key->val);
492 hcon->enc_key_size = key->pin_len;
493 581
494 goto done; 582 if (hcon->link_mode & HCI_LM_MASTER) {
495 } 583 struct smp_cmd_pairing cp;
496
497 build_pairing_cmd(conn, &cp, NULL, authreq);
498 conn->preq[0] = SMP_CMD_PAIRING_REQ;
499 memcpy(&conn->preq[1], &cp, sizeof(cp));
500 584
501 mod_timer(&conn->security_timer, jiffies + 585 build_pairing_cmd(conn, &cp, NULL, SMP_AUTH_NONE);
502 msecs_to_jiffies(SMP_TIMEOUT)); 586 smp->preq[0] = SMP_CMD_PAIRING_REQ;
587 memcpy(&smp->preq[1], &cp, sizeof(cp));
503 588
504 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); 589 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
505 } else { 590 } else {
506 struct smp_cmd_security_req cp; 591 struct smp_cmd_security_req cp;
507 cp.auth_req = authreq; 592 cp.auth_req = SMP_AUTH_NONE;
508 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); 593 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
509 } 594 }
510 595
511done: 596done:
512 hcon->pending_sec_level = sec_level; 597 hcon->pending_sec_level = sec_level;
513 set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend);
514 598
515 return 0; 599 return 0;
516} 600}
@@ -518,10 +602,11 @@ done:
518static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) 602static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
519{ 603{
520 struct smp_cmd_encrypt_info *rp = (void *) skb->data; 604 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
605 struct smp_chan *smp = conn->smp_chan;
521 606
522 skb_pull(skb, sizeof(*rp)); 607 skb_pull(skb, sizeof(*rp));
523 608
524 memcpy(conn->tk, rp->ltk, sizeof(conn->tk)); 609 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
525 610
526 return 0; 611 return 0;
527} 612}
@@ -529,11 +614,12 @@ static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
529static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) 614static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
530{ 615{
531 struct smp_cmd_master_ident *rp = (void *) skb->data; 616 struct smp_cmd_master_ident *rp = (void *) skb->data;
617 struct smp_chan *smp = conn->smp_chan;
532 618
533 skb_pull(skb, sizeof(*rp)); 619 skb_pull(skb, sizeof(*rp));
534 620
535 hci_add_ltk(conn->hcon->hdev, 1, conn->src, conn->smp_key_size, 621 hci_add_ltk(conn->hcon->hdev, 1, conn->src, smp->smp_key_size,
536 rp->ediv, rp->rand, conn->tk); 622 rp->ediv, rp->rand, smp->tk);
537 623
538 smp_distribute_keys(conn, 1); 624 smp_distribute_keys(conn, 1);
539 625
@@ -552,12 +638,6 @@ int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
552 goto done; 638 goto done;
553 } 639 }
554 640
555 if (IS_ERR(conn->hcon->hdev->tfm)) {
556 err = PTR_ERR(conn->hcon->hdev->tfm);
557 reason = SMP_PAIRING_NOTSUPP;
558 goto done;
559 }
560
561 skb_pull(skb, sizeof(code)); 641 skb_pull(skb, sizeof(code));
562 642
563 switch (code) { 643 switch (code) {
@@ -621,20 +701,21 @@ done:
621int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) 701int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
622{ 702{
623 struct smp_cmd_pairing *req, *rsp; 703 struct smp_cmd_pairing *req, *rsp;
704 struct smp_chan *smp = conn->smp_chan;
624 __u8 *keydist; 705 __u8 *keydist;
625 706
626 BT_DBG("conn %p force %d", conn, force); 707 BT_DBG("conn %p force %d", conn, force);
627 708
628 if (IS_ERR(conn->hcon->hdev->tfm)) 709 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
629 return PTR_ERR(conn->hcon->hdev->tfm); 710 return 0;
630 711
631 rsp = (void *) &conn->prsp[1]; 712 rsp = (void *) &smp->prsp[1];
632 713
633 /* The responder sends its keys first */ 714 /* The responder sends its keys first */
634 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07)) 715 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
635 return 0; 716 return 0;
636 717
637 req = (void *) &conn->preq[1]; 718 req = (void *) &smp->preq[1];
638 719
639 if (conn->hcon->out) { 720 if (conn->hcon->out) {
640 keydist = &rsp->init_key_dist; 721 keydist = &rsp->init_key_dist;
@@ -658,7 +739,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
658 739
659 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 740 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
660 741
661 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, conn->smp_key_size, 742 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, smp->smp_key_size,
662 ediv, ident.rand, enc.ltk); 743 ediv, ident.rand, enc.ltk);
663 744
664 ident.ediv = cpu_to_le16(ediv); 745 ident.ediv = cpu_to_le16(ediv);
@@ -698,5 +779,11 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
698 *keydist &= ~SMP_DIST_SIGN; 779 *keydist &= ~SMP_DIST_SIGN;
699 } 780 }
700 781
782 if (conn->hcon->out || force) {
783 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend);
784 del_timer(&conn->security_timer);
785 smp_chan_destroy(conn);
786 }
787
701 return 0; 788 return 0;
702} 789}