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