Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 23 | #include <linux/crypto.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <crypto/b128ops.h> |
| 26 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 27 | #include <net/bluetooth/bluetooth.h> |
| 28 | #include <net/bluetooth/hci_core.h> |
| 29 | #include <net/bluetooth/l2cap.h> |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 30 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 31 | |
| 32 | #include "smp.h" |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 33 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 34 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 35 | |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 36 | #define AUTH_REQ_MASK 0x07 |
| 37 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 38 | static inline void swap128(u8 src[16], u8 dst[16]) |
| 39 | { |
| 40 | int i; |
| 41 | for (i = 0; i < 16; i++) |
| 42 | dst[15 - i] = src[i]; |
| 43 | } |
| 44 | |
| 45 | static inline void swap56(u8 src[7], u8 dst[7]) |
| 46 | { |
| 47 | int i; |
| 48 | for (i = 0; i < 7; i++) |
| 49 | dst[6 - i] = src[i]; |
| 50 | } |
| 51 | |
| 52 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) |
| 53 | { |
| 54 | struct blkcipher_desc desc; |
| 55 | struct scatterlist sg; |
Johan Hedberg | 201a592 | 2013-12-02 10:49:04 +0200 | [diff] [blame] | 56 | int err; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 57 | |
| 58 | if (tfm == NULL) { |
| 59 | BT_ERR("tfm %p", tfm); |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | |
| 63 | desc.tfm = tfm; |
| 64 | desc.flags = 0; |
| 65 | |
| 66 | err = crypto_blkcipher_setkey(tfm, k, 16); |
| 67 | if (err) { |
| 68 | BT_ERR("cipher setkey failed: %d", err); |
| 69 | return err; |
| 70 | } |
| 71 | |
| 72 | sg_init_one(&sg, r, 16); |
| 73 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 74 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
| 75 | if (err) |
| 76 | BT_ERR("Encrypt data error %d", err); |
| 77 | |
| 78 | return err; |
| 79 | } |
| 80 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 81 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
| 82 | { |
| 83 | u8 _res[16], k[16]; |
| 84 | int err; |
| 85 | |
| 86 | /* r' = padding || r */ |
| 87 | memset(_res, 0, 13); |
| 88 | _res[13] = r[2]; |
| 89 | _res[14] = r[1]; |
| 90 | _res[15] = r[0]; |
| 91 | |
| 92 | swap128(irk, k); |
| 93 | err = smp_e(tfm, k, _res); |
| 94 | if (err) { |
| 95 | BT_ERR("Encrypt error"); |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | /* The output of the random address function ah is: |
| 100 | * ah(h, r) = e(k, r') mod 2^24 |
| 101 | * The output of the security function e is then truncated to 24 bits |
| 102 | * by taking the least significant 24 bits of the output of e as the |
| 103 | * result of ah. |
| 104 | */ |
| 105 | res[0] = _res[15]; |
| 106 | res[1] = _res[14]; |
| 107 | res[2] = _res[13]; |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | bool smp_irk_matches(struct crypto_blkcipher *tfm, u8 irk[16], |
| 113 | bdaddr_t *bdaddr) |
| 114 | { |
| 115 | u8 hash[3]; |
| 116 | int err; |
| 117 | |
| 118 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
| 119 | |
| 120 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); |
| 121 | if (err) |
| 122 | return false; |
| 123 | |
| 124 | return !memcmp(bdaddr->b, hash, 3); |
| 125 | } |
| 126 | |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 127 | int smp_generate_rpa(struct crypto_blkcipher *tfm, u8 irk[16], bdaddr_t *rpa) |
| 128 | { |
| 129 | int err; |
| 130 | |
| 131 | get_random_bytes(&rpa->b[3], 3); |
| 132 | |
| 133 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ |
| 134 | rpa->b[5] |= 0x40; /* Set second most significant bit */ |
| 135 | |
| 136 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); |
| 137 | if (err < 0) |
| 138 | return err; |
| 139 | |
| 140 | BT_DBG("RPA %pMR", rpa); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 145 | static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 146 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, |
| 147 | u8 _rat, bdaddr_t *ra, u8 res[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 148 | { |
| 149 | u8 p1[16], p2[16]; |
| 150 | int err; |
| 151 | |
| 152 | memset(p1, 0, 16); |
| 153 | |
| 154 | /* p1 = pres || preq || _rat || _iat */ |
| 155 | swap56(pres, p1); |
| 156 | swap56(preq, p1 + 7); |
| 157 | p1[14] = _rat; |
| 158 | p1[15] = _iat; |
| 159 | |
| 160 | memset(p2, 0, 16); |
| 161 | |
| 162 | /* p2 = padding || ia || ra */ |
| 163 | baswap((bdaddr_t *) (p2 + 4), ia); |
| 164 | baswap((bdaddr_t *) (p2 + 10), ra); |
| 165 | |
| 166 | /* res = r XOR p1 */ |
| 167 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); |
| 168 | |
| 169 | /* res = e(k, res) */ |
| 170 | err = smp_e(tfm, k, res); |
| 171 | if (err) { |
| 172 | BT_ERR("Encrypt data error"); |
| 173 | return err; |
| 174 | } |
| 175 | |
| 176 | /* res = res XOR p2 */ |
| 177 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); |
| 178 | |
| 179 | /* res = e(k, res) */ |
| 180 | err = smp_e(tfm, k, res); |
| 181 | if (err) |
| 182 | BT_ERR("Encrypt data error"); |
| 183 | |
| 184 | return err; |
| 185 | } |
| 186 | |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 187 | static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16], |
| 188 | u8 r2[16], u8 _r[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 189 | { |
| 190 | int err; |
| 191 | |
| 192 | /* Just least significant octets from r1 and r2 are considered */ |
| 193 | memcpy(_r, r1 + 8, 8); |
| 194 | memcpy(_r + 8, r2 + 8, 8); |
| 195 | |
| 196 | err = smp_e(tfm, k, _r); |
| 197 | if (err) |
| 198 | BT_ERR("Encrypt data error"); |
| 199 | |
| 200 | return err; |
| 201 | } |
| 202 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 203 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 204 | u16 dlen, void *data) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 205 | { |
| 206 | struct sk_buff *skb; |
| 207 | struct l2cap_hdr *lh; |
| 208 | int len; |
| 209 | |
| 210 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; |
| 211 | |
| 212 | if (len > conn->mtu) |
| 213 | return NULL; |
| 214 | |
| 215 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 216 | if (!skb) |
| 217 | return NULL; |
| 218 | |
| 219 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 220 | lh->len = cpu_to_le16(sizeof(code) + dlen); |
Syam Sidhardhan | d8aece2 | 2012-10-10 22:09:28 +0530 | [diff] [blame] | 221 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SMP); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 222 | |
| 223 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); |
| 224 | |
| 225 | memcpy(skb_put(skb, dlen), data, dlen); |
| 226 | |
| 227 | return skb; |
| 228 | } |
| 229 | |
| 230 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
| 231 | { |
| 232 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); |
| 233 | |
| 234 | BT_DBG("code 0x%2.2x", code); |
| 235 | |
| 236 | if (!skb) |
| 237 | return; |
| 238 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 239 | skb->priority = HCI_PRIO_MAX; |
| 240 | hci_send_acl(conn->hchan, skb, 0); |
Vinicius Costa Gomes | e2dcd11 | 2011-08-19 21:06:50 -0300 | [diff] [blame] | 241 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 242 | cancel_delayed_work_sync(&conn->security_timer); |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 243 | schedule_delayed_work(&conn->security_timer, SMP_TIMEOUT); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 244 | } |
| 245 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 246 | static __u8 authreq_to_seclevel(__u8 authreq) |
| 247 | { |
| 248 | if (authreq & SMP_AUTH_MITM) |
| 249 | return BT_SECURITY_HIGH; |
| 250 | else |
| 251 | return BT_SECURITY_MEDIUM; |
| 252 | } |
| 253 | |
| 254 | static __u8 seclevel_to_authreq(__u8 sec_level) |
| 255 | { |
| 256 | switch (sec_level) { |
| 257 | case BT_SECURITY_HIGH: |
| 258 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; |
| 259 | case BT_SECURITY_MEDIUM: |
| 260 | return SMP_AUTH_BONDING; |
| 261 | default: |
| 262 | return SMP_AUTH_NONE; |
| 263 | } |
| 264 | } |
| 265 | |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 266 | static void build_pairing_cmd(struct l2cap_conn *conn, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 267 | struct smp_cmd_pairing *req, |
| 268 | struct smp_cmd_pairing *rsp, __u8 authreq) |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 269 | { |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 270 | struct smp_chan *smp = conn->smp_chan; |
| 271 | struct hci_conn *hcon = conn->hcon; |
| 272 | struct hci_dev *hdev = hcon->hdev; |
| 273 | u8 local_dist = 0, remote_dist = 0; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 274 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 275 | if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->dev_flags)) { |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 276 | local_dist = SMP_DIST_ENC_KEY; |
| 277 | remote_dist = SMP_DIST_ENC_KEY; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 278 | authreq |= SMP_AUTH_BONDING; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 279 | } else { |
| 280 | authreq &= ~SMP_AUTH_BONDING; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 281 | } |
| 282 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 283 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
| 284 | remote_dist |= SMP_DIST_ID_KEY; |
| 285 | |
Johan Hedberg | 863efaf | 2014-02-22 19:06:32 +0200 | [diff] [blame] | 286 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 287 | local_dist |= SMP_DIST_ID_KEY; |
| 288 | |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 289 | if (rsp == NULL) { |
| 290 | req->io_capability = conn->hcon->io_capability; |
| 291 | req->oob_flag = SMP_OOB_NOT_PRESENT; |
| 292 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 293 | req->init_key_dist = local_dist; |
| 294 | req->resp_key_dist = remote_dist; |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 295 | req->auth_req = (authreq & AUTH_REQ_MASK); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 296 | |
| 297 | smp->remote_key_dist = remote_dist; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
| 301 | rsp->io_capability = conn->hcon->io_capability; |
| 302 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; |
| 303 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 304 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
| 305 | rsp->resp_key_dist = req->resp_key_dist & local_dist; |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 306 | rsp->auth_req = (authreq & AUTH_REQ_MASK); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 307 | |
| 308 | smp->remote_key_dist = rsp->init_key_dist; |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 309 | } |
| 310 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 311 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
| 312 | { |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 313 | struct smp_chan *smp = conn->smp_chan; |
| 314 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 315 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 316 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 317 | return SMP_ENC_KEY_SIZE; |
| 318 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 319 | smp->enc_key_size = max_key_size; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 324 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 325 | { |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 326 | struct hci_conn *hcon = conn->hcon; |
| 327 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 328 | if (reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 329 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 330 | &reason); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 331 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 332 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
| 333 | mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type, |
| 334 | HCI_ERROR_AUTH_FAILURE); |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 335 | |
Andre Guedes | 61a0cfb | 2012-08-01 20:34:15 -0300 | [diff] [blame] | 336 | cancel_delayed_work_sync(&conn->security_timer); |
| 337 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 338 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 339 | smp_chan_destroy(conn); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 342 | #define JUST_WORKS 0x00 |
| 343 | #define JUST_CFM 0x01 |
| 344 | #define REQ_PASSKEY 0x02 |
| 345 | #define CFM_PASSKEY 0x03 |
| 346 | #define REQ_OOB 0x04 |
| 347 | #define OVERLAP 0xFF |
| 348 | |
| 349 | static const u8 gen_method[5][5] = { |
| 350 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 351 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 352 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 353 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, |
| 354 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, |
| 355 | }; |
| 356 | |
| 357 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
| 358 | u8 local_io, u8 remote_io) |
| 359 | { |
| 360 | struct hci_conn *hcon = conn->hcon; |
| 361 | struct smp_chan *smp = conn->smp_chan; |
| 362 | u8 method; |
| 363 | u32 passkey = 0; |
| 364 | int ret = 0; |
| 365 | |
| 366 | /* Initialize key for JUST WORKS */ |
| 367 | memset(smp->tk, 0, sizeof(smp->tk)); |
| 368 | clear_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); |
| 369 | |
| 370 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); |
| 371 | |
| 372 | /* If neither side wants MITM, use JUST WORKS */ |
| 373 | /* If either side has unknown io_caps, use JUST WORKS */ |
| 374 | /* Otherwise, look up method from the table */ |
| 375 | if (!(auth & SMP_AUTH_MITM) || |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 376 | local_io > SMP_IO_KEYBOARD_DISPLAY || |
| 377 | remote_io > SMP_IO_KEYBOARD_DISPLAY) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 378 | method = JUST_WORKS; |
| 379 | else |
Ido Yariv | b3ff53f | 2012-03-05 20:07:08 +0200 | [diff] [blame] | 380 | method = gen_method[remote_io][local_io]; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 381 | |
| 382 | /* If not bonding, don't ask user to confirm a Zero TK */ |
| 383 | if (!(auth & SMP_AUTH_BONDING) && method == JUST_CFM) |
| 384 | method = JUST_WORKS; |
| 385 | |
| 386 | /* If Just Works, Continue with Zero TK */ |
| 387 | if (method == JUST_WORKS) { |
| 388 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | /* Not Just Works/Confirm results in MITM Authentication */ |
| 393 | if (method != JUST_CFM) |
| 394 | set_bit(SMP_FLAG_MITM_AUTH, &smp->smp_flags); |
| 395 | |
| 396 | /* If both devices have Keyoard-Display I/O, the master |
| 397 | * Confirms and the slave Enters the passkey. |
| 398 | */ |
| 399 | if (method == OVERLAP) { |
| 400 | if (hcon->link_mode & HCI_LM_MASTER) |
| 401 | method = CFM_PASSKEY; |
| 402 | else |
| 403 | method = REQ_PASSKEY; |
| 404 | } |
| 405 | |
| 406 | /* Generate random passkey. Not valid until confirmed. */ |
| 407 | if (method == CFM_PASSKEY) { |
| 408 | u8 key[16]; |
| 409 | |
| 410 | memset(key, 0, sizeof(key)); |
| 411 | get_random_bytes(&passkey, sizeof(passkey)); |
| 412 | passkey %= 1000000; |
| 413 | put_unaligned_le32(passkey, key); |
| 414 | swap128(key, smp->tk); |
| 415 | BT_DBG("PassKey: %d", passkey); |
| 416 | } |
| 417 | |
| 418 | hci_dev_lock(hcon->hdev); |
| 419 | |
| 420 | if (method == REQ_PASSKEY) |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 421 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 422 | hcon->type, hcon->dst_type); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 423 | else |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 424 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 425 | hcon->type, hcon->dst_type, |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 426 | cpu_to_le32(passkey), 0); |
| 427 | |
| 428 | hci_dev_unlock(hcon->hdev); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 433 | static void confirm_work(struct work_struct *work) |
| 434 | { |
| 435 | struct smp_chan *smp = container_of(work, struct smp_chan, confirm); |
| 436 | struct l2cap_conn *conn = smp->conn; |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 437 | struct hci_dev *hdev = conn->hcon->hdev; |
| 438 | struct crypto_blkcipher *tfm = hdev->tfm_aes; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 439 | struct smp_cmd_pairing_confirm cp; |
| 440 | int ret; |
| 441 | u8 res[16], reason; |
| 442 | |
| 443 | BT_DBG("conn %p", conn); |
| 444 | |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 445 | /* Prevent mutual access to hdev->tfm_aes */ |
| 446 | hci_dev_lock(hdev); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 447 | |
| 448 | if (conn->hcon->out) |
Marcel Holtmann | c8462ca | 2013-10-13 05:24:02 -0700 | [diff] [blame] | 449 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, |
| 450 | conn->hcon->src_type, &conn->hcon->src, |
| 451 | conn->hcon->dst_type, &conn->hcon->dst, res); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 452 | else |
| 453 | ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, |
Marcel Holtmann | c8462ca | 2013-10-13 05:24:02 -0700 | [diff] [blame] | 454 | conn->hcon->dst_type, &conn->hcon->dst, |
| 455 | conn->hcon->src_type, &conn->hcon->src, res); |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 456 | |
| 457 | hci_dev_unlock(hdev); |
| 458 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 459 | if (ret) { |
| 460 | reason = SMP_UNSPECIFIED; |
| 461 | goto error; |
| 462 | } |
| 463 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 464 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
| 465 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 466 | swap128(res, cp.confirm_val); |
| 467 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
| 468 | |
| 469 | return; |
| 470 | |
| 471 | error: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 472 | smp_failure(conn, reason); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void random_work(struct work_struct *work) |
| 476 | { |
| 477 | struct smp_chan *smp = container_of(work, struct smp_chan, random); |
| 478 | struct l2cap_conn *conn = smp->conn; |
| 479 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 480 | struct hci_dev *hdev = hcon->hdev; |
| 481 | struct crypto_blkcipher *tfm = hdev->tfm_aes; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 482 | u8 reason, confirm[16], res[16], key[16]; |
| 483 | int ret; |
| 484 | |
| 485 | if (IS_ERR_OR_NULL(tfm)) { |
| 486 | reason = SMP_UNSPECIFIED; |
| 487 | goto error; |
| 488 | } |
| 489 | |
| 490 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 491 | |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 492 | /* Prevent mutual access to hdev->tfm_aes */ |
| 493 | hci_dev_lock(hdev); |
| 494 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 495 | if (hcon->out) |
Marcel Holtmann | c8462ca | 2013-10-13 05:24:02 -0700 | [diff] [blame] | 496 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
| 497 | hcon->src_type, &hcon->src, |
| 498 | hcon->dst_type, &hcon->dst, res); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 499 | else |
| 500 | ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
Marcel Holtmann | c8462ca | 2013-10-13 05:24:02 -0700 | [diff] [blame] | 501 | hcon->dst_type, &hcon->dst, |
| 502 | hcon->src_type, &hcon->src, res); |
Johan Hedberg | 893ce8b | 2014-02-18 21:41:31 +0200 | [diff] [blame] | 503 | |
| 504 | hci_dev_unlock(hdev); |
| 505 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 506 | if (ret) { |
| 507 | reason = SMP_UNSPECIFIED; |
| 508 | goto error; |
| 509 | } |
| 510 | |
| 511 | swap128(res, confirm); |
| 512 | |
| 513 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
| 514 | BT_ERR("Pairing failed (confirmation values mismatch)"); |
| 515 | reason = SMP_CONFIRM_FAILED; |
| 516 | goto error; |
| 517 | } |
| 518 | |
| 519 | if (hcon->out) { |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 520 | u8 stk[16]; |
| 521 | __le64 rand = 0; |
| 522 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 523 | |
| 524 | smp_s1(tfm, smp->tk, smp->rrnd, smp->prnd, key); |
| 525 | swap128(key, stk); |
| 526 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 527 | memset(stk + smp->enc_key_size, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 528 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 529 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 530 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 531 | reason = SMP_UNSPECIFIED; |
| 532 | goto error; |
| 533 | } |
| 534 | |
| 535 | hci_le_start_enc(hcon, ediv, rand, stk); |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 536 | hcon->enc_key_size = smp->enc_key_size; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 537 | } else { |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 538 | u8 stk[16], r[16]; |
| 539 | __le64 rand = 0; |
| 540 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 541 | |
| 542 | swap128(smp->prnd, r); |
| 543 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); |
| 544 | |
| 545 | smp_s1(tfm, smp->tk, smp->prnd, smp->rrnd, key); |
| 546 | swap128(key, stk); |
| 547 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 548 | memset(stk + smp->enc_key_size, 0, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 549 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 550 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 551 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 552 | HCI_SMP_STK_SLAVE, 0, stk, smp->enc_key_size, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 553 | ediv, rand); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return; |
| 557 | |
| 558 | error: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 559 | smp_failure(conn, reason); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
| 563 | { |
| 564 | struct smp_chan *smp; |
| 565 | |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 566 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 567 | if (!smp) |
| 568 | return NULL; |
| 569 | |
| 570 | INIT_WORK(&smp->confirm, confirm_work); |
| 571 | INIT_WORK(&smp->random, random_work); |
| 572 | |
| 573 | smp->conn = conn; |
| 574 | conn->smp_chan = smp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 575 | conn->hcon->smp_conn = conn; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 576 | |
| 577 | hci_conn_hold(conn->hcon); |
| 578 | |
| 579 | return smp; |
| 580 | } |
| 581 | |
| 582 | void smp_chan_destroy(struct l2cap_conn *conn) |
| 583 | { |
Brian Gix | c8eb969 | 2011-11-23 08:28:35 -0800 | [diff] [blame] | 584 | struct smp_chan *smp = conn->smp_chan; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 585 | bool complete; |
Brian Gix | c8eb969 | 2011-11-23 08:28:35 -0800 | [diff] [blame] | 586 | |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 587 | BUG_ON(!smp); |
Brian Gix | c8eb969 | 2011-11-23 08:28:35 -0800 | [diff] [blame] | 588 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 589 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->smp_flags); |
| 590 | mgmt_smp_complete(conn->hcon, complete); |
| 591 | |
Brian Gix | c8eb969 | 2011-11-23 08:28:35 -0800 | [diff] [blame] | 592 | kfree(smp); |
| 593 | conn->smp_chan = NULL; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 594 | conn->hcon->smp_conn = NULL; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 595 | hci_conn_drop(conn->hcon); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 596 | } |
| 597 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 598 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
| 599 | { |
| 600 | struct l2cap_conn *conn = hcon->smp_conn; |
| 601 | struct smp_chan *smp; |
| 602 | u32 value; |
| 603 | u8 key[16]; |
| 604 | |
| 605 | BT_DBG(""); |
| 606 | |
| 607 | if (!conn) |
| 608 | return -ENOTCONN; |
| 609 | |
| 610 | smp = conn->smp_chan; |
| 611 | |
| 612 | switch (mgmt_op) { |
| 613 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 614 | value = le32_to_cpu(passkey); |
| 615 | memset(key, 0, sizeof(key)); |
| 616 | BT_DBG("PassKey: %d", value); |
| 617 | put_unaligned_le32(value, key); |
| 618 | swap128(key, smp->tk); |
| 619 | /* Fall Through */ |
| 620 | case MGMT_OP_USER_CONFIRM_REPLY: |
| 621 | set_bit(SMP_FLAG_TK_VALID, &smp->smp_flags); |
| 622 | break; |
| 623 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 624 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 625 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 626 | return 0; |
| 627 | default: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 628 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 629 | return -EOPNOTSUPP; |
| 630 | } |
| 631 | |
| 632 | /* If it is our turn to send Pairing Confirm, do so now */ |
| 633 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags)) |
| 634 | queue_work(hcon->hdev->workqueue, &smp->confirm); |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 639 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 640 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 641 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 642 | struct smp_chan *smp; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 643 | u8 key_size; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 644 | u8 auth = SMP_AUTH_NONE; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 645 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 646 | |
| 647 | BT_DBG("conn %p", conn); |
| 648 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 649 | if (skb->len < sizeof(*req)) |
| 650 | return SMP_UNSPECIFIED; |
| 651 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 652 | if (conn->hcon->link_mode & HCI_LM_MASTER) |
| 653 | return SMP_CMD_NOTSUPP; |
| 654 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 655 | if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 656 | smp = smp_chan_create(conn); |
Andrei Emeltchenko | d08fd0e | 2012-07-19 17:03:43 +0300 | [diff] [blame] | 657 | else |
| 658 | smp = conn->smp_chan; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 659 | |
Andrei Emeltchenko | d08fd0e | 2012-07-19 17:03:43 +0300 | [diff] [blame] | 660 | if (!smp) |
| 661 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 662 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 663 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 664 | memcpy(&smp->preq[1], req, sizeof(*req)); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 665 | skb_pull(skb, sizeof(*req)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 666 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 667 | /* We didn't start the pairing, so match remote */ |
| 668 | if (req->auth_req & SMP_AUTH_BONDING) |
| 669 | auth = req->auth_req; |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 670 | |
Ido Yariv | fdde0a2 | 2012-03-05 20:09:38 +0200 | [diff] [blame] | 671 | conn->hcon->pending_sec_level = authreq_to_seclevel(auth); |
| 672 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 673 | build_pairing_cmd(conn, req, &rsp, auth); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 674 | |
| 675 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 676 | if (check_enc_key_size(conn, key_size)) |
| 677 | return SMP_ENC_KEY_SIZE; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 678 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 679 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 680 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 681 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 682 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 683 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 684 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 685 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 686 | /* Request setup of TK */ |
| 687 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); |
| 688 | if (ret) |
| 689 | return SMP_UNSPECIFIED; |
| 690 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 691 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 692 | } |
| 693 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 694 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 695 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 696 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 697 | struct smp_chan *smp = conn->smp_chan; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 698 | struct hci_dev *hdev = conn->hcon->hdev; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 699 | u8 key_size, auth = SMP_AUTH_NONE; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 700 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 701 | |
| 702 | BT_DBG("conn %p", conn); |
| 703 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 704 | if (skb->len < sizeof(*rsp)) |
| 705 | return SMP_UNSPECIFIED; |
| 706 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 707 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
| 708 | return SMP_CMD_NOTSUPP; |
| 709 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 710 | skb_pull(skb, sizeof(*rsp)); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 711 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 712 | req = (void *) &smp->preq[1]; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 713 | |
| 714 | key_size = min(req->max_key_size, rsp->max_key_size); |
| 715 | if (check_enc_key_size(conn, key_size)) |
| 716 | return SMP_ENC_KEY_SIZE; |
| 717 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 718 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 719 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 720 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 721 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 722 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 723 | if ((req->auth_req & SMP_AUTH_BONDING) && |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 724 | (rsp->auth_req & SMP_AUTH_BONDING)) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 725 | auth = SMP_AUTH_BONDING; |
| 726 | |
| 727 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; |
| 728 | |
Johan Hedberg | 476585e | 2012-06-06 18:54:15 +0800 | [diff] [blame] | 729 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 730 | if (ret) |
| 731 | return SMP_UNSPECIFIED; |
| 732 | |
| 733 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
| 734 | |
| 735 | /* Can't compose response until we have been confirmed */ |
| 736 | if (!test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) |
| 737 | return 0; |
| 738 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 739 | queue_work(hdev->workqueue, &smp->confirm); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 740 | |
| 741 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 742 | } |
| 743 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 744 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 745 | { |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 746 | struct smp_chan *smp = conn->smp_chan; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 747 | struct hci_dev *hdev = conn->hcon->hdev; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 748 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 749 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 750 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 751 | if (skb->len < sizeof(smp->pcnf)) |
| 752 | return SMP_UNSPECIFIED; |
| 753 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 754 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
| 755 | skb_pull(skb, sizeof(smp->pcnf)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 756 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 757 | if (conn->hcon->out) { |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 758 | u8 random[16]; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 759 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 760 | swap128(smp->prnd, random); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 761 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 762 | random); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 763 | } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 764 | queue_work(hdev->workqueue, &smp->confirm); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 765 | } else { |
| 766 | set_bit(SMP_FLAG_CFM_PENDING, &smp->smp_flags); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 767 | } |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 768 | |
| 769 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 770 | } |
| 771 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 772 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 773 | { |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 774 | struct smp_chan *smp = conn->smp_chan; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 775 | struct hci_dev *hdev = conn->hcon->hdev; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 776 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 777 | BT_DBG("conn %p", conn); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 778 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 779 | if (skb->len < sizeof(smp->rrnd)) |
| 780 | return SMP_UNSPECIFIED; |
| 781 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 782 | swap128(skb->data, smp->rrnd); |
| 783 | skb_pull(skb, sizeof(smp->rrnd)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 784 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 785 | queue_work(hdev->workqueue, &smp->random); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 786 | |
| 787 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 788 | } |
| 789 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 790 | static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 791 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 792 | struct smp_ltk *key; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 793 | struct hci_conn *hcon = conn->hcon; |
| 794 | |
Johan Hedberg | 98a0b84 | 2014-01-30 19:40:00 -0800 | [diff] [blame] | 795 | key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 796 | hcon->out); |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 797 | if (!key) |
| 798 | return 0; |
| 799 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 800 | if (sec_level > BT_SECURITY_MEDIUM && !key->authenticated) |
| 801 | return 0; |
| 802 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 803 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 804 | return 1; |
| 805 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 806 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
| 807 | hcon->enc_key_size = key->enc_size; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 808 | |
| 809 | return 1; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 810 | } |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 811 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 812 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 813 | { |
| 814 | struct smp_cmd_security_req *rp = (void *) skb->data; |
| 815 | struct smp_cmd_pairing cp; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 816 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 817 | struct smp_chan *smp; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 818 | |
| 819 | BT_DBG("conn %p", conn); |
| 820 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 821 | if (skb->len < sizeof(*rp)) |
| 822 | return SMP_UNSPECIFIED; |
| 823 | |
Johan Hedberg | 86ca9ea | 2013-11-05 11:30:39 +0200 | [diff] [blame] | 824 | if (!(conn->hcon->link_mode & HCI_LM_MASTER)) |
| 825 | return SMP_CMD_NOTSUPP; |
| 826 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 827 | hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); |
Vinicius Costa Gomes | feb45eb | 2011-08-25 20:02:35 -0300 | [diff] [blame] | 828 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 829 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 830 | return 0; |
| 831 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 832 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 833 | return 0; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 834 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 835 | smp = smp_chan_create(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 836 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 837 | skb_pull(skb, sizeof(*rp)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 838 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 839 | memset(&cp, 0, sizeof(cp)); |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 840 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 841 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 842 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 843 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 844 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 845 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 846 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 847 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 848 | } |
| 849 | |
Johan Hedberg | ad32a2f | 2013-05-14 18:05:12 +0300 | [diff] [blame] | 850 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level) |
| 851 | { |
| 852 | if (sec_level == BT_SECURITY_LOW) |
| 853 | return true; |
| 854 | |
| 855 | if (hcon->sec_level >= sec_level) |
| 856 | return true; |
| 857 | |
| 858 | return false; |
| 859 | } |
| 860 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 861 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 862 | { |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 863 | struct l2cap_conn *conn = hcon->l2cap_data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 864 | struct smp_chan *smp = conn->smp_chan; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 865 | __u8 authreq; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 866 | |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 867 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
| 868 | |
Johan Hedberg | 757aee0 | 2013-04-24 13:05:32 +0300 | [diff] [blame] | 869 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 870 | return 1; |
| 871 | |
Johan Hedberg | ad32a2f | 2013-05-14 18:05:12 +0300 | [diff] [blame] | 872 | if (smp_sufficient_security(hcon, sec_level)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 873 | return 1; |
| 874 | |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 875 | if (hcon->link_mode & HCI_LM_MASTER) |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 876 | if (smp_ltk_encrypt(conn, sec_level)) |
Vinicius Costa Gomes | 02bc745 | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 877 | goto done; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 878 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 879 | if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 880 | return 0; |
| 881 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 882 | smp = smp_chan_create(conn); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 883 | if (!smp) |
| 884 | return 1; |
| 885 | |
| 886 | authreq = seclevel_to_authreq(sec_level); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 887 | |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 888 | if (hcon->link_mode & HCI_LM_MASTER) { |
| 889 | struct smp_cmd_pairing cp; |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 890 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 891 | build_pairing_cmd(conn, &cp, NULL, authreq); |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 892 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 893 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 894 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 895 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
| 896 | } else { |
| 897 | struct smp_cmd_security_req cp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 898 | cp.auth_req = authreq; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 899 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
| 900 | } |
| 901 | |
Vinicius Costa Gomes | 02bc745 | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 902 | done: |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 903 | hcon->pending_sec_level = sec_level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 904 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 908 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 909 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 910 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 911 | struct smp_chan *smp = conn->smp_chan; |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 912 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 913 | BT_DBG("conn %p", conn); |
| 914 | |
| 915 | if (skb->len < sizeof(*rp)) |
| 916 | return SMP_UNSPECIFIED; |
| 917 | |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 918 | /* Ignore this PDU if it wasn't requested */ |
| 919 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) |
| 920 | return 0; |
| 921 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 922 | skb_pull(skb, sizeof(*rp)); |
| 923 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 924 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 925 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | |
| 929 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
| 930 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 931 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 932 | struct smp_chan *smp = conn->smp_chan; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 933 | struct hci_dev *hdev = conn->hcon->hdev; |
| 934 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 935 | struct smp_ltk *ltk; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 936 | u8 authenticated; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 937 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 938 | BT_DBG("conn %p", conn); |
| 939 | |
| 940 | if (skb->len < sizeof(*rp)) |
| 941 | return SMP_UNSPECIFIED; |
| 942 | |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 943 | /* Ignore this PDU if it wasn't requested */ |
| 944 | if (!(smp->remote_key_dist & SMP_DIST_ENC_KEY)) |
| 945 | return 0; |
| 946 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 947 | /* Mark the information as received */ |
| 948 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; |
| 949 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 950 | skb_pull(skb, sizeof(*rp)); |
| 951 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 952 | hci_dev_lock(hdev); |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 953 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 954 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 955 | authenticated, smp->tk, smp->enc_key_size, |
| 956 | rp->ediv, rp->rand); |
| 957 | smp->ltk = ltk; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 958 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 959 | smp_distribute_keys(conn); |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 960 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 965 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 966 | { |
| 967 | struct smp_cmd_ident_info *info = (void *) skb->data; |
| 968 | struct smp_chan *smp = conn->smp_chan; |
| 969 | |
| 970 | BT_DBG(""); |
| 971 | |
| 972 | if (skb->len < sizeof(*info)) |
| 973 | return SMP_UNSPECIFIED; |
| 974 | |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 975 | /* Ignore this PDU if it wasn't requested */ |
| 976 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
| 977 | return 0; |
| 978 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 979 | skb_pull(skb, sizeof(*info)); |
| 980 | |
| 981 | memcpy(smp->irk, info->irk, 16); |
| 982 | |
| 983 | return 0; |
| 984 | } |
| 985 | |
| 986 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, |
| 987 | struct sk_buff *skb) |
| 988 | { |
| 989 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; |
| 990 | struct smp_chan *smp = conn->smp_chan; |
| 991 | struct hci_conn *hcon = conn->hcon; |
| 992 | bdaddr_t rpa; |
| 993 | |
| 994 | BT_DBG(""); |
| 995 | |
| 996 | if (skb->len < sizeof(*info)) |
| 997 | return SMP_UNSPECIFIED; |
| 998 | |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 999 | /* Ignore this PDU if it wasn't requested */ |
| 1000 | if (!(smp->remote_key_dist & SMP_DIST_ID_KEY)) |
| 1001 | return 0; |
| 1002 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 1003 | /* Mark the information as received */ |
| 1004 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; |
| 1005 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1006 | skb_pull(skb, sizeof(*info)); |
| 1007 | |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1008 | /* Strictly speaking the Core Specification (4.1) allows sending |
| 1009 | * an empty address which would force us to rely on just the IRK |
| 1010 | * as "identity information". However, since such |
| 1011 | * implementations are not known of and in order to not over |
| 1012 | * complicate our implementation, simply pretend that we never |
| 1013 | * received an IRK for such a device. |
| 1014 | */ |
| 1015 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { |
| 1016 | BT_ERR("Ignoring IRK with no identity address"); |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 1017 | smp_distribute_keys(conn); |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1021 | bacpy(&smp->id_addr, &info->bdaddr); |
| 1022 | smp->id_addr_type = info->addr_type; |
| 1023 | |
| 1024 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) |
| 1025 | bacpy(&rpa, &hcon->dst); |
| 1026 | else |
| 1027 | bacpy(&rpa, BDADDR_ANY); |
| 1028 | |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1029 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
| 1030 | smp->id_addr_type, smp->irk, &rpa); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1031 | |
Johan Hedberg | 68d6f6d | 2014-02-18 21:41:32 +0200 | [diff] [blame] | 1032 | /* Track the connection based on the Identity Address from now on */ |
| 1033 | bacpy(&hcon->dst, &smp->id_addr); |
| 1034 | hcon->dst_type = smp->id_addr_type; |
| 1035 | |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 1036 | l2cap_conn_update_id_addr(hcon); |
| 1037 | |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 1038 | smp_distribute_keys(conn); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1043 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1044 | { |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1045 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1046 | __u8 code, reason; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1047 | int err = 0; |
| 1048 | |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1049 | if (hcon->type != LE_LINK) { |
| 1050 | kfree_skb(skb); |
Johan Hedberg | 3432711f | 2013-10-16 11:37:01 +0300 | [diff] [blame] | 1051 | return 0; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1054 | if (skb->len < 1) { |
| 1055 | kfree_skb(skb); |
| 1056 | return -EILSEQ; |
| 1057 | } |
| 1058 | |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 1059 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 1060 | err = -ENOTSUPP; |
| 1061 | reason = SMP_PAIRING_NOTSUPP; |
| 1062 | goto done; |
| 1063 | } |
| 1064 | |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1065 | code = skb->data[0]; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1066 | skb_pull(skb, sizeof(code)); |
| 1067 | |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 1068 | /* |
| 1069 | * The SMP context must be initialized for all other PDUs except |
| 1070 | * pairing and security requests. If we get any other PDU when |
| 1071 | * not initialized simply disconnect (done if this function |
| 1072 | * returns an error). |
| 1073 | */ |
| 1074 | if (code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ && |
| 1075 | !conn->smp_chan) { |
| 1076 | BT_ERR("Unexpected SMP command 0x%02x. Disconnecting.", code); |
| 1077 | kfree_skb(skb); |
| 1078 | return -ENOTSUPP; |
| 1079 | } |
| 1080 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1081 | switch (code) { |
| 1082 | case SMP_CMD_PAIRING_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1083 | reason = smp_cmd_pairing_req(conn, skb); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1084 | break; |
| 1085 | |
| 1086 | case SMP_CMD_PAIRING_FAIL: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1087 | smp_failure(conn, 0); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1088 | reason = 0; |
| 1089 | err = -EPERM; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1090 | break; |
| 1091 | |
| 1092 | case SMP_CMD_PAIRING_RSP: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1093 | reason = smp_cmd_pairing_rsp(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1094 | break; |
| 1095 | |
| 1096 | case SMP_CMD_SECURITY_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1097 | reason = smp_cmd_security_req(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1098 | break; |
| 1099 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1100 | case SMP_CMD_PAIRING_CONFIRM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1101 | reason = smp_cmd_pairing_confirm(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1102 | break; |
| 1103 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1104 | case SMP_CMD_PAIRING_RANDOM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1105 | reason = smp_cmd_pairing_random(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1106 | break; |
| 1107 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1108 | case SMP_CMD_ENCRYPT_INFO: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1109 | reason = smp_cmd_encrypt_info(conn, skb); |
| 1110 | break; |
| 1111 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1112 | case SMP_CMD_MASTER_IDENT: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1113 | reason = smp_cmd_master_ident(conn, skb); |
| 1114 | break; |
| 1115 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1116 | case SMP_CMD_IDENT_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1117 | reason = smp_cmd_ident_info(conn, skb); |
| 1118 | break; |
| 1119 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1120 | case SMP_CMD_IDENT_ADDR_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1121 | reason = smp_cmd_ident_addr_info(conn, skb); |
| 1122 | break; |
| 1123 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1124 | case SMP_CMD_SIGN_INFO: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1125 | /* Just ignored */ |
| 1126 | reason = 0; |
| 1127 | break; |
| 1128 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1129 | default: |
| 1130 | BT_DBG("Unknown command code 0x%2.2x", code); |
| 1131 | |
| 1132 | reason = SMP_CMD_NOTSUPP; |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1133 | err = -EOPNOTSUPP; |
| 1134 | goto done; |
| 1135 | } |
| 1136 | |
| 1137 | done: |
| 1138 | if (reason) |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1139 | smp_failure(conn, reason); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1140 | |
| 1141 | kfree_skb(skb); |
| 1142 | return err; |
| 1143 | } |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1144 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 1145 | static void smp_notify_keys(struct l2cap_conn *conn) |
| 1146 | { |
| 1147 | struct smp_chan *smp = conn->smp_chan; |
| 1148 | struct hci_conn *hcon = conn->hcon; |
| 1149 | struct hci_dev *hdev = hcon->hdev; |
| 1150 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 1151 | if (smp->remote_irk) |
| 1152 | mgmt_new_irk(hdev, smp->remote_irk); |
| 1153 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 1154 | if (smp->ltk) { |
| 1155 | smp->ltk->bdaddr_type = hcon->dst_type; |
| 1156 | bacpy(&smp->ltk->bdaddr, &hcon->dst); |
| 1157 | mgmt_new_ltk(hdev, smp->ltk); |
| 1158 | } |
| 1159 | |
| 1160 | if (smp->slave_ltk) { |
| 1161 | smp->slave_ltk->bdaddr_type = hcon->dst_type; |
| 1162 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); |
| 1163 | mgmt_new_ltk(hdev, smp->slave_ltk); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 1167 | int smp_distribute_keys(struct l2cap_conn *conn) |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1168 | { |
| 1169 | struct smp_cmd_pairing *req, *rsp; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1170 | struct smp_chan *smp = conn->smp_chan; |
Johan Hedberg | 524237c | 2014-02-22 19:06:31 +0200 | [diff] [blame] | 1171 | struct hci_conn *hcon = conn->hcon; |
| 1172 | struct hci_dev *hdev = hcon->hdev; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1173 | __u8 *keydist; |
| 1174 | |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 1175 | BT_DBG("conn %p", conn); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1176 | |
Johan Hedberg | 524237c | 2014-02-22 19:06:31 +0200 | [diff] [blame] | 1177 | if (!test_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1178 | return 0; |
| 1179 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1180 | rsp = (void *) &smp->prsp[1]; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1181 | |
| 1182 | /* The responder sends its keys first */ |
Johan Hedberg | efabba3 | 2014-02-26 23:33:44 +0200 | [diff] [blame] | 1183 | if (hcon->out && (smp->remote_key_dist & 0x07)) |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1184 | return 0; |
| 1185 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1186 | req = (void *) &smp->preq[1]; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1187 | |
Johan Hedberg | 524237c | 2014-02-22 19:06:31 +0200 | [diff] [blame] | 1188 | if (hcon->out) { |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1189 | keydist = &rsp->init_key_dist; |
| 1190 | *keydist &= req->init_key_dist; |
| 1191 | } else { |
| 1192 | keydist = &rsp->resp_key_dist; |
| 1193 | *keydist &= req->resp_key_dist; |
| 1194 | } |
| 1195 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1196 | BT_DBG("keydist 0x%x", *keydist); |
| 1197 | |
| 1198 | if (*keydist & SMP_DIST_ENC_KEY) { |
| 1199 | struct smp_cmd_encrypt_info enc; |
| 1200 | struct smp_cmd_master_ident ident; |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1201 | struct smp_ltk *ltk; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1202 | u8 authenticated; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1203 | __le16 ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 1204 | __le64 rand; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1205 | |
| 1206 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); |
| 1207 | get_random_bytes(&ediv, sizeof(ediv)); |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 1208 | get_random_bytes(&rand, sizeof(rand)); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1209 | |
| 1210 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); |
| 1211 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1212 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
Johan Hedberg | 524237c | 2014-02-22 19:06:31 +0200 | [diff] [blame] | 1213 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 1214 | HCI_SMP_LTK_SLAVE, authenticated, enc.ltk, |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 1215 | smp->enc_key_size, ediv, rand); |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1216 | smp->slave_ltk = ltk; |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1217 | |
Andrei Emeltchenko | 5811537 | 2012-03-12 12:13:06 +0200 | [diff] [blame] | 1218 | ident.ediv = ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame^] | 1219 | ident.rand = rand; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1220 | |
| 1221 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); |
| 1222 | |
| 1223 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 1224 | } |
| 1225 | |
| 1226 | if (*keydist & SMP_DIST_ID_KEY) { |
| 1227 | struct smp_cmd_ident_addr_info addrinfo; |
| 1228 | struct smp_cmd_ident_info idinfo; |
| 1229 | |
Johan Hedberg | 863efaf | 2014-02-22 19:06:32 +0200 | [diff] [blame] | 1230 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1231 | |
| 1232 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); |
| 1233 | |
Johan Hedberg | 82d4b35 | 2014-02-23 19:42:18 +0200 | [diff] [blame] | 1234 | /* The hci_conn contains the local identity address |
| 1235 | * after the connection has been established. |
| 1236 | * |
| 1237 | * This is true even when the connection has been |
| 1238 | * established using a resolvable random address. |
| 1239 | */ |
Johan Hedberg | 524237c | 2014-02-22 19:06:31 +0200 | [diff] [blame] | 1240 | bacpy(&addrinfo.bdaddr, &hcon->src); |
Johan Hedberg | 82d4b35 | 2014-02-23 19:42:18 +0200 | [diff] [blame] | 1241 | addrinfo.addr_type = hcon->src_type; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1242 | |
| 1243 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 1244 | &addrinfo); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1245 | |
| 1246 | *keydist &= ~SMP_DIST_ID_KEY; |
| 1247 | } |
| 1248 | |
| 1249 | if (*keydist & SMP_DIST_SIGN) { |
| 1250 | struct smp_cmd_sign_info sign; |
| 1251 | |
| 1252 | /* Send a dummy key */ |
| 1253 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
| 1254 | |
| 1255 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
| 1256 | |
| 1257 | *keydist &= ~SMP_DIST_SIGN; |
| 1258 | } |
| 1259 | |
Johan Hedberg | efabba3 | 2014-02-26 23:33:44 +0200 | [diff] [blame] | 1260 | /* If there are still keys to be received wait for them */ |
| 1261 | if ((smp->remote_key_dist & 0x07)) |
| 1262 | return 0; |
| 1263 | |
| 1264 | clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags); |
| 1265 | cancel_delayed_work_sync(&conn->security_timer); |
| 1266 | set_bit(SMP_FLAG_COMPLETE, &smp->smp_flags); |
| 1267 | smp_notify_keys(conn); |
| 1268 | |
| 1269 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1270 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |