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 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 32 | #include "ecc.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 33 | #include "smp.h" |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 34 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 35 | #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 36 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 37 | /* Keys which are not distributed with Secure Connections */ |
| 38 | #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY); |
| 39 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 40 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 41 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 42 | #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \ |
| 43 | 0x1f : 0x07) |
| 44 | #define KEY_DIST_MASK 0x07 |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 45 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 46 | /* Maximum message length that can be passed to aes_cmac */ |
| 47 | #define CMAC_MSG_MAX 80 |
| 48 | |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 49 | enum { |
| 50 | SMP_FLAG_TK_VALID, |
| 51 | SMP_FLAG_CFM_PENDING, |
| 52 | SMP_FLAG_MITM_AUTH, |
| 53 | SMP_FLAG_COMPLETE, |
| 54 | SMP_FLAG_INITIATOR, |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 55 | SMP_FLAG_SC, |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 56 | SMP_FLAG_REMOTE_PK, |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 57 | }; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 58 | |
| 59 | struct smp_chan { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 60 | struct l2cap_conn *conn; |
| 61 | struct delayed_work security_timer; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 62 | unsigned long allow_cmd; /* Bitmask of allowed commands */ |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 63 | |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 64 | u8 preq[7]; /* SMP Pairing Request */ |
| 65 | u8 prsp[7]; /* SMP Pairing Response */ |
| 66 | u8 prnd[16]; /* SMP Pairing Random (local) */ |
| 67 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ |
| 68 | u8 pcnf[16]; /* SMP Pairing Confirm */ |
| 69 | u8 tk[16]; /* SMP Temporary Key */ |
| 70 | u8 enc_key_size; |
| 71 | u8 remote_key_dist; |
| 72 | bdaddr_t id_addr; |
| 73 | u8 id_addr_type; |
| 74 | u8 irk[16]; |
| 75 | struct smp_csrk *csrk; |
| 76 | struct smp_csrk *slave_csrk; |
| 77 | struct smp_ltk *ltk; |
| 78 | struct smp_ltk *slave_ltk; |
| 79 | struct smp_irk *remote_irk; |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 80 | unsigned long flags; |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 81 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 82 | /* Secure Connections variables */ |
| 83 | u8 local_pk[64]; |
| 84 | u8 local_sk[32]; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 85 | u8 remote_pk[64]; |
| 86 | u8 dhkey[32]; |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 87 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 88 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 89 | struct crypto_hash *tfm_cmac; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 90 | }; |
| 91 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 92 | 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] | 93 | { |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 94 | size_t i; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 95 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 96 | for (i = 0; i < len; i++) |
| 97 | dst[len - 1 - i] = src[i]; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 98 | } |
| 99 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 100 | static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m, |
| 101 | size_t len, u8 mac[16]) |
| 102 | { |
| 103 | uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; |
| 104 | struct hash_desc desc; |
| 105 | struct scatterlist sg; |
| 106 | int err; |
| 107 | |
| 108 | if (len > CMAC_MSG_MAX) |
| 109 | return -EFBIG; |
| 110 | |
| 111 | if (!tfm) { |
| 112 | BT_ERR("tfm %p", tfm); |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | desc.tfm = tfm; |
| 117 | desc.flags = 0; |
| 118 | |
| 119 | crypto_hash_init(&desc); |
| 120 | |
| 121 | /* Swap key and message from LSB to MSB */ |
| 122 | swap_buf(k, tmp, 16); |
| 123 | swap_buf(m, msg_msb, len); |
| 124 | |
| 125 | BT_DBG("msg (len %zu) %*phN", len, (int) len, m); |
| 126 | BT_DBG("key %16phN", k); |
| 127 | |
| 128 | err = crypto_hash_setkey(tfm, tmp, 16); |
| 129 | if (err) { |
| 130 | BT_ERR("cipher setkey failed: %d", err); |
| 131 | return err; |
| 132 | } |
| 133 | |
| 134 | sg_init_one(&sg, msg_msb, len); |
| 135 | |
| 136 | err = crypto_hash_update(&desc, &sg, len); |
| 137 | if (err) { |
| 138 | BT_ERR("Hash update error %d", err); |
| 139 | return err; |
| 140 | } |
| 141 | |
| 142 | err = crypto_hash_final(&desc, mac_msb); |
| 143 | if (err) { |
| 144 | BT_ERR("Hash final error %d", err); |
| 145 | return err; |
| 146 | } |
| 147 | |
| 148 | swap_buf(mac_msb, mac, 16); |
| 149 | |
| 150 | BT_DBG("mac %16phN", mac); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], |
| 156 | const u8 x[16], u8 z, u8 res[16]) |
| 157 | { |
| 158 | u8 m[65]; |
| 159 | int err; |
| 160 | |
| 161 | BT_DBG("u %32phN", u); |
| 162 | BT_DBG("v %32phN", v); |
| 163 | BT_DBG("x %16phN z %02x", x, z); |
| 164 | |
| 165 | m[0] = z; |
| 166 | memcpy(m + 1, v, 32); |
| 167 | memcpy(m + 33, u, 32); |
| 168 | |
| 169 | err = aes_cmac(tfm_cmac, x, m, sizeof(m), res); |
| 170 | if (err) |
| 171 | return err; |
| 172 | |
| 173 | BT_DBG("res %16phN", res); |
| 174 | |
| 175 | return err; |
| 176 | } |
| 177 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 178 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) |
| 179 | { |
| 180 | struct blkcipher_desc desc; |
| 181 | struct scatterlist sg; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 182 | uint8_t tmp[16], data[16]; |
Johan Hedberg | 201a592 | 2013-12-02 10:49:04 +0200 | [diff] [blame] | 183 | int err; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 184 | |
| 185 | if (tfm == NULL) { |
| 186 | BT_ERR("tfm %p", tfm); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
| 190 | desc.tfm = tfm; |
| 191 | desc.flags = 0; |
| 192 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 193 | /* The most significant octet of key corresponds to k[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 194 | swap_buf(k, tmp, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 195 | |
| 196 | err = crypto_blkcipher_setkey(tfm, tmp, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 197 | if (err) { |
| 198 | BT_ERR("cipher setkey failed: %d", err); |
| 199 | return err; |
| 200 | } |
| 201 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 202 | /* Most significant octet of plaintextData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 203 | swap_buf(r, data, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 204 | |
| 205 | sg_init_one(&sg, data, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 206 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 207 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
| 208 | if (err) |
| 209 | BT_ERR("Encrypt data error %d", err); |
| 210 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 211 | /* Most significant octet of encryptedData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 212 | swap_buf(data, r, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 213 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 214 | return err; |
| 215 | } |
| 216 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 217 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
| 218 | { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 219 | u8 _res[16]; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 220 | int err; |
| 221 | |
| 222 | /* r' = padding || r */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 223 | memcpy(_res, r, 3); |
| 224 | memset(_res + 3, 0, 13); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 225 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 226 | err = smp_e(tfm, irk, _res); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 227 | if (err) { |
| 228 | BT_ERR("Encrypt error"); |
| 229 | return err; |
| 230 | } |
| 231 | |
| 232 | /* The output of the random address function ah is: |
| 233 | * ah(h, r) = e(k, r') mod 2^24 |
| 234 | * The output of the security function e is then truncated to 24 bits |
| 235 | * by taking the least significant 24 bits of the output of e as the |
| 236 | * result of ah. |
| 237 | */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 238 | memcpy(res, _res, 3); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 243 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 244 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 245 | struct l2cap_chan *chan = hdev->smp_data; |
| 246 | struct crypto_blkcipher *tfm; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 247 | u8 hash[3]; |
| 248 | int err; |
| 249 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 250 | if (!chan || !chan->data) |
| 251 | return false; |
| 252 | |
| 253 | tfm = chan->data; |
| 254 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 255 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
| 256 | |
| 257 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); |
| 258 | if (err) |
| 259 | return false; |
| 260 | |
| 261 | return !memcmp(bdaddr->b, hash, 3); |
| 262 | } |
| 263 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 264 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 265 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 266 | struct l2cap_chan *chan = hdev->smp_data; |
| 267 | struct crypto_blkcipher *tfm; |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 268 | int err; |
| 269 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 270 | if (!chan || !chan->data) |
| 271 | return -EOPNOTSUPP; |
| 272 | |
| 273 | tfm = chan->data; |
| 274 | |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 275 | get_random_bytes(&rpa->b[3], 3); |
| 276 | |
| 277 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ |
| 278 | rpa->b[5] |= 0x40; /* Set second most significant bit */ |
| 279 | |
| 280 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); |
| 281 | if (err < 0) |
| 282 | return err; |
| 283 | |
| 284 | BT_DBG("RPA %pMR", rpa); |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 289 | static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16], |
| 290 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, |
| 291 | bdaddr_t *ra, u8 res[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 292 | { |
| 293 | u8 p1[16], p2[16]; |
| 294 | int err; |
| 295 | |
| 296 | memset(p1, 0, 16); |
| 297 | |
| 298 | /* p1 = pres || preq || _rat || _iat */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 299 | p1[0] = _iat; |
| 300 | p1[1] = _rat; |
| 301 | memcpy(p1 + 2, preq, 7); |
| 302 | memcpy(p1 + 9, pres, 7); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 303 | |
| 304 | /* p2 = padding || ia || ra */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 305 | memcpy(p2, ra, 6); |
| 306 | memcpy(p2 + 6, ia, 6); |
| 307 | memset(p2 + 12, 0, 4); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 308 | |
| 309 | /* res = r XOR p1 */ |
| 310 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); |
| 311 | |
| 312 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 313 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 314 | if (err) { |
| 315 | BT_ERR("Encrypt data error"); |
| 316 | return err; |
| 317 | } |
| 318 | |
| 319 | /* res = res XOR p2 */ |
| 320 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); |
| 321 | |
| 322 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 323 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 324 | if (err) |
| 325 | BT_ERR("Encrypt data error"); |
| 326 | |
| 327 | return err; |
| 328 | } |
| 329 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 330 | static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16], |
| 331 | u8 r2[16], u8 _r[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 332 | { |
| 333 | int err; |
| 334 | |
| 335 | /* Just least significant octets from r1 and r2 are considered */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 336 | memcpy(_r, r2, 8); |
| 337 | memcpy(_r + 8, r1, 8); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 338 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 339 | err = smp_e(tfm_aes, k, _r); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 340 | if (err) |
| 341 | BT_ERR("Encrypt data error"); |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 346 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
| 347 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 348 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 349 | struct smp_chan *smp; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 350 | struct kvec iv[2]; |
| 351 | struct msghdr msg; |
| 352 | |
| 353 | if (!chan) |
| 354 | return; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 355 | |
| 356 | BT_DBG("code 0x%2.2x", code); |
| 357 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 358 | iv[0].iov_base = &code; |
| 359 | iv[0].iov_len = 1; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 360 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 361 | iv[1].iov_base = data; |
| 362 | iv[1].iov_len = len; |
| 363 | |
| 364 | memset(&msg, 0, sizeof(msg)); |
| 365 | |
| 366 | msg.msg_iov = (struct iovec *) &iv; |
| 367 | msg.msg_iovlen = 2; |
| 368 | |
| 369 | l2cap_chan_send(chan, &msg, 1 + len); |
Vinicius Costa Gomes | e2dcd11 | 2011-08-19 21:06:50 -0300 | [diff] [blame] | 370 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 371 | if (!chan->data) |
| 372 | return; |
| 373 | |
| 374 | smp = chan->data; |
| 375 | |
| 376 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 1b0921d | 2014-09-05 22:19:48 +0300 | [diff] [blame] | 377 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 378 | } |
| 379 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 380 | static u8 authreq_to_seclevel(u8 authreq) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 381 | { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 382 | if (authreq & SMP_AUTH_MITM) { |
| 383 | if (authreq & SMP_AUTH_SC) |
| 384 | return BT_SECURITY_FIPS; |
| 385 | else |
| 386 | return BT_SECURITY_HIGH; |
| 387 | } else { |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 388 | return BT_SECURITY_MEDIUM; |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 389 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static __u8 seclevel_to_authreq(__u8 sec_level) |
| 393 | { |
| 394 | switch (sec_level) { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 395 | case BT_SECURITY_FIPS: |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 396 | case BT_SECURITY_HIGH: |
| 397 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; |
| 398 | case BT_SECURITY_MEDIUM: |
| 399 | return SMP_AUTH_BONDING; |
| 400 | default: |
| 401 | return SMP_AUTH_NONE; |
| 402 | } |
| 403 | } |
| 404 | |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 405 | static void build_pairing_cmd(struct l2cap_conn *conn, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 406 | struct smp_cmd_pairing *req, |
| 407 | struct smp_cmd_pairing *rsp, __u8 authreq) |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 408 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 409 | struct l2cap_chan *chan = conn->smp; |
| 410 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 411 | struct hci_conn *hcon = conn->hcon; |
| 412 | struct hci_dev *hdev = hcon->hdev; |
| 413 | u8 local_dist = 0, remote_dist = 0; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 414 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 415 | if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 416 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
| 417 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 418 | authreq |= SMP_AUTH_BONDING; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 419 | } else { |
| 420 | authreq &= ~SMP_AUTH_BONDING; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 421 | } |
| 422 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 423 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
| 424 | remote_dist |= SMP_DIST_ID_KEY; |
| 425 | |
Johan Hedberg | 863efaf | 2014-02-22 19:06:32 +0200 | [diff] [blame] | 426 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 427 | local_dist |= SMP_DIST_ID_KEY; |
| 428 | |
Johan Hedberg | df8e1a4 | 2014-06-06 10:39:56 +0300 | [diff] [blame] | 429 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) { |
| 430 | if ((authreq & SMP_AUTH_SC) && |
| 431 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 432 | local_dist |= SMP_DIST_LINK_KEY; |
| 433 | remote_dist |= SMP_DIST_LINK_KEY; |
| 434 | } |
| 435 | } else { |
| 436 | authreq &= ~SMP_AUTH_SC; |
| 437 | } |
| 438 | |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 439 | if (rsp == NULL) { |
| 440 | req->io_capability = conn->hcon->io_capability; |
| 441 | req->oob_flag = SMP_OOB_NOT_PRESENT; |
| 442 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 443 | req->init_key_dist = local_dist; |
| 444 | req->resp_key_dist = remote_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 445 | req->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 446 | |
| 447 | smp->remote_key_dist = remote_dist; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
| 451 | rsp->io_capability = conn->hcon->io_capability; |
| 452 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; |
| 453 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 454 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
| 455 | rsp->resp_key_dist = req->resp_key_dist & local_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 456 | rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 457 | |
| 458 | smp->remote_key_dist = rsp->init_key_dist; |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 459 | } |
| 460 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 461 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
| 462 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 463 | struct l2cap_chan *chan = conn->smp; |
| 464 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 465 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 466 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 467 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 468 | return SMP_ENC_KEY_SIZE; |
| 469 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 470 | smp->enc_key_size = max_key_size; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 475 | static void smp_chan_destroy(struct l2cap_conn *conn) |
| 476 | { |
| 477 | struct l2cap_chan *chan = conn->smp; |
| 478 | struct smp_chan *smp = chan->data; |
| 479 | bool complete; |
| 480 | |
| 481 | BUG_ON(!smp); |
| 482 | |
| 483 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 484 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 485 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 486 | mgmt_smp_complete(conn->hcon, complete); |
| 487 | |
| 488 | kfree(smp->csrk); |
| 489 | kfree(smp->slave_csrk); |
| 490 | |
| 491 | crypto_free_blkcipher(smp->tfm_aes); |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 492 | crypto_free_hash(smp->tfm_cmac); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 493 | |
| 494 | /* If pairing failed clean up any keys we might have */ |
| 495 | if (!complete) { |
| 496 | if (smp->ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 497 | list_del_rcu(&smp->ltk->list); |
| 498 | kfree_rcu(smp->ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (smp->slave_ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 502 | list_del_rcu(&smp->slave_ltk->list); |
| 503 | kfree_rcu(smp->slave_ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (smp->remote_irk) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 507 | list_del_rcu(&smp->remote_irk->list); |
| 508 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | chan->data = NULL; |
| 513 | kfree(smp); |
| 514 | hci_conn_drop(conn->hcon); |
| 515 | } |
| 516 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 517 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 518 | { |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 519 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 520 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 521 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 522 | if (reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 523 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 524 | &reason); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 525 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 526 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 527 | mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 528 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 529 | if (chan->data) |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 530 | smp_chan_destroy(conn); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 533 | #define JUST_WORKS 0x00 |
| 534 | #define JUST_CFM 0x01 |
| 535 | #define REQ_PASSKEY 0x02 |
| 536 | #define CFM_PASSKEY 0x03 |
| 537 | #define REQ_OOB 0x04 |
| 538 | #define OVERLAP 0xFF |
| 539 | |
| 540 | static const u8 gen_method[5][5] = { |
| 541 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 542 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 543 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 544 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, |
| 545 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, |
| 546 | }; |
| 547 | |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 548 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
| 549 | { |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 550 | /* If either side has unknown io_caps, use JUST_CFM (which gets |
| 551 | * converted later to JUST_WORKS if we're initiators. |
| 552 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 553 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || |
| 554 | remote_io > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 555 | return JUST_CFM; |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 556 | |
| 557 | return gen_method[remote_io][local_io]; |
| 558 | } |
| 559 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 560 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
| 561 | u8 local_io, u8 remote_io) |
| 562 | { |
| 563 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 564 | struct l2cap_chan *chan = conn->smp; |
| 565 | struct smp_chan *smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 566 | u8 method; |
| 567 | u32 passkey = 0; |
| 568 | int ret = 0; |
| 569 | |
| 570 | /* Initialize key for JUST WORKS */ |
| 571 | memset(smp->tk, 0, sizeof(smp->tk)); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 572 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 573 | |
| 574 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); |
| 575 | |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 576 | /* If neither side wants MITM, either "just" confirm an incoming |
| 577 | * request or use just-works for outgoing ones. The JUST_CFM |
| 578 | * will be converted to JUST_WORKS if necessary later in this |
| 579 | * function. If either side has MITM look up the method from the |
| 580 | * table. |
| 581 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 582 | if (!(auth & SMP_AUTH_MITM)) |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 583 | method = JUST_CFM; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 584 | else |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 585 | method = get_auth_method(smp, local_io, remote_io); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 586 | |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 587 | /* Don't confirm locally initiated pairing attempts */ |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 588 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 589 | method = JUST_WORKS; |
| 590 | |
Johan Hedberg | 02f3e25 | 2014-07-16 15:09:13 +0300 | [diff] [blame] | 591 | /* Don't bother user space with no IO capabilities */ |
| 592 | if (method == JUST_CFM && hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
| 593 | method = JUST_WORKS; |
| 594 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 595 | /* If Just Works, Continue with Zero TK */ |
| 596 | if (method == JUST_WORKS) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 597 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* Not Just Works/Confirm results in MITM Authentication */ |
Johan Hedberg | 5eb596f | 2014-09-18 11:26:32 +0300 | [diff] [blame] | 602 | if (method != JUST_CFM) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 603 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
Johan Hedberg | 5eb596f | 2014-09-18 11:26:32 +0300 | [diff] [blame] | 604 | if (hcon->pending_sec_level < BT_SECURITY_HIGH) |
| 605 | hcon->pending_sec_level = BT_SECURITY_HIGH; |
| 606 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 607 | |
| 608 | /* If both devices have Keyoard-Display I/O, the master |
| 609 | * Confirms and the slave Enters the passkey. |
| 610 | */ |
| 611 | if (method == OVERLAP) { |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 612 | if (hcon->role == HCI_ROLE_MASTER) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 613 | method = CFM_PASSKEY; |
| 614 | else |
| 615 | method = REQ_PASSKEY; |
| 616 | } |
| 617 | |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 618 | /* Generate random passkey. */ |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 619 | if (method == CFM_PASSKEY) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 620 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 621 | get_random_bytes(&passkey, sizeof(passkey)); |
| 622 | passkey %= 1000000; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 623 | put_unaligned_le32(passkey, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 624 | BT_DBG("PassKey: %d", passkey); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 625 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 628 | if (method == REQ_PASSKEY) |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 629 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 630 | hcon->type, hcon->dst_type); |
Johan Hedberg | 4eb65e6 | 2014-03-24 14:39:05 +0200 | [diff] [blame] | 631 | else if (method == JUST_CFM) |
| 632 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
| 633 | hcon->type, hcon->dst_type, |
| 634 | passkey, 1); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 635 | else |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 636 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 637 | hcon->type, hcon->dst_type, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 638 | passkey, 0); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 639 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 640 | return ret; |
| 641 | } |
| 642 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 643 | static u8 smp_confirm(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 644 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 645 | struct l2cap_conn *conn = smp->conn; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 646 | struct smp_cmd_pairing_confirm cp; |
| 647 | int ret; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 648 | |
| 649 | BT_DBG("conn %p", conn); |
| 650 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 651 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 652 | conn->hcon->init_addr_type, &conn->hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 653 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
| 654 | cp.confirm_val); |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 655 | if (ret) |
| 656 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 657 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 658 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 659 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 660 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
| 661 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 662 | if (conn->hcon->out) |
| 663 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 664 | else |
| 665 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 666 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 667 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 668 | } |
| 669 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 670 | static u8 smp_random(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 671 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 672 | struct l2cap_conn *conn = smp->conn; |
| 673 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 674 | u8 confirm[16]; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 675 | int ret; |
| 676 | |
Johan Hedberg | ec70f36 | 2014-06-27 14:23:04 +0300 | [diff] [blame] | 677 | if (IS_ERR_OR_NULL(smp->tfm_aes)) |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 678 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 679 | |
| 680 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 681 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 682 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 683 | hcon->init_addr_type, &hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 684 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 685 | if (ret) |
| 686 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 687 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 688 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
| 689 | BT_ERR("Pairing failed (confirmation values mismatch)"); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 690 | return SMP_CONFIRM_FAILED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | if (hcon->out) { |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 694 | u8 stk[16]; |
| 695 | __le64 rand = 0; |
| 696 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 697 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 698 | smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 699 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 700 | memset(stk + smp->enc_key_size, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 701 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 702 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 703 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
| 704 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 705 | |
| 706 | hci_le_start_enc(hcon, ediv, rand, stk); |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 707 | hcon->enc_key_size = smp->enc_key_size; |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 708 | set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 709 | } else { |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 710 | u8 stk[16], auth; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 711 | __le64 rand = 0; |
| 712 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 713 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 714 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 715 | smp->prnd); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 716 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 717 | smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 718 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 719 | memset(stk + smp->enc_key_size, 0, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 720 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 721 | |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 722 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
| 723 | auth = 1; |
| 724 | else |
| 725 | auth = 0; |
| 726 | |
Johan Hedberg | 7d5843b | 2014-06-16 19:25:15 +0300 | [diff] [blame] | 727 | /* Even though there's no _SLAVE suffix this is the |
| 728 | * slave STK we're adding for later lookup (the master |
| 729 | * STK never needs to be stored). |
| 730 | */ |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 731 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 732 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 733 | } |
| 734 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 735 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 736 | } |
| 737 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 738 | static void smp_notify_keys(struct l2cap_conn *conn) |
| 739 | { |
| 740 | struct l2cap_chan *chan = conn->smp; |
| 741 | struct smp_chan *smp = chan->data; |
| 742 | struct hci_conn *hcon = conn->hcon; |
| 743 | struct hci_dev *hdev = hcon->hdev; |
| 744 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; |
| 745 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; |
| 746 | bool persistent; |
| 747 | |
| 748 | if (smp->remote_irk) { |
| 749 | mgmt_new_irk(hdev, smp->remote_irk); |
| 750 | /* Now that user space can be considered to know the |
| 751 | * identity address track the connection based on it |
| 752 | * from now on. |
| 753 | */ |
| 754 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); |
| 755 | hcon->dst_type = smp->remote_irk->addr_type; |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 756 | queue_work(hdev->workqueue, &conn->id_addr_update_work); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 757 | |
| 758 | /* When receiving an indentity resolving key for |
| 759 | * a remote device that does not use a resolvable |
| 760 | * private address, just remove the key so that |
| 761 | * it is possible to use the controller white |
| 762 | * list for scanning. |
| 763 | * |
| 764 | * Userspace will have been told to not store |
| 765 | * this key at this point. So it is safe to |
| 766 | * just remove it. |
| 767 | */ |
| 768 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 769 | list_del_rcu(&smp->remote_irk->list); |
| 770 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 771 | smp->remote_irk = NULL; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | /* The LTKs and CSRKs should be persistent only if both sides |
| 776 | * had the bonding bit set in their authentication requests. |
| 777 | */ |
| 778 | persistent = !!((req->auth_req & rsp->auth_req) & SMP_AUTH_BONDING); |
| 779 | |
| 780 | if (smp->csrk) { |
| 781 | smp->csrk->bdaddr_type = hcon->dst_type; |
| 782 | bacpy(&smp->csrk->bdaddr, &hcon->dst); |
| 783 | mgmt_new_csrk(hdev, smp->csrk, persistent); |
| 784 | } |
| 785 | |
| 786 | if (smp->slave_csrk) { |
| 787 | smp->slave_csrk->bdaddr_type = hcon->dst_type; |
| 788 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); |
| 789 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); |
| 790 | } |
| 791 | |
| 792 | if (smp->ltk) { |
| 793 | smp->ltk->bdaddr_type = hcon->dst_type; |
| 794 | bacpy(&smp->ltk->bdaddr, &hcon->dst); |
| 795 | mgmt_new_ltk(hdev, smp->ltk, persistent); |
| 796 | } |
| 797 | |
| 798 | if (smp->slave_ltk) { |
| 799 | smp->slave_ltk->bdaddr_type = hcon->dst_type; |
| 800 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); |
| 801 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); |
| 802 | } |
| 803 | } |
| 804 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 805 | static void smp_allow_key_dist(struct smp_chan *smp) |
| 806 | { |
| 807 | /* Allow the first expected phase 3 PDU. The rest of the PDUs |
| 808 | * will be allowed in each PDU handler to ensure we receive |
| 809 | * them in the correct order. |
| 810 | */ |
| 811 | if (smp->remote_key_dist & SMP_DIST_ENC_KEY) |
| 812 | SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); |
| 813 | else if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 814 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
| 815 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 816 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 817 | } |
| 818 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 819 | static void smp_distribute_keys(struct smp_chan *smp) |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 820 | { |
| 821 | struct smp_cmd_pairing *req, *rsp; |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 822 | struct l2cap_conn *conn = smp->conn; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 823 | struct hci_conn *hcon = conn->hcon; |
| 824 | struct hci_dev *hdev = hcon->hdev; |
| 825 | __u8 *keydist; |
| 826 | |
| 827 | BT_DBG("conn %p", conn); |
| 828 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 829 | rsp = (void *) &smp->prsp[1]; |
| 830 | |
| 831 | /* The responder sends its keys first */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 832 | if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) { |
| 833 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 834 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 835 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 836 | |
| 837 | req = (void *) &smp->preq[1]; |
| 838 | |
| 839 | if (hcon->out) { |
| 840 | keydist = &rsp->init_key_dist; |
| 841 | *keydist &= req->init_key_dist; |
| 842 | } else { |
| 843 | keydist = &rsp->resp_key_dist; |
| 844 | *keydist &= req->resp_key_dist; |
| 845 | } |
| 846 | |
| 847 | BT_DBG("keydist 0x%x", *keydist); |
| 848 | |
| 849 | if (*keydist & SMP_DIST_ENC_KEY) { |
| 850 | struct smp_cmd_encrypt_info enc; |
| 851 | struct smp_cmd_master_ident ident; |
| 852 | struct smp_ltk *ltk; |
| 853 | u8 authenticated; |
| 854 | __le16 ediv; |
| 855 | __le64 rand; |
| 856 | |
| 857 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); |
| 858 | get_random_bytes(&ediv, sizeof(ediv)); |
| 859 | get_random_bytes(&rand, sizeof(rand)); |
| 860 | |
| 861 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); |
| 862 | |
| 863 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
| 864 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, |
| 865 | SMP_LTK_SLAVE, authenticated, enc.ltk, |
| 866 | smp->enc_key_size, ediv, rand); |
| 867 | smp->slave_ltk = ltk; |
| 868 | |
| 869 | ident.ediv = ediv; |
| 870 | ident.rand = rand; |
| 871 | |
| 872 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); |
| 873 | |
| 874 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 875 | } |
| 876 | |
| 877 | if (*keydist & SMP_DIST_ID_KEY) { |
| 878 | struct smp_cmd_ident_addr_info addrinfo; |
| 879 | struct smp_cmd_ident_info idinfo; |
| 880 | |
| 881 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); |
| 882 | |
| 883 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); |
| 884 | |
| 885 | /* The hci_conn contains the local identity address |
| 886 | * after the connection has been established. |
| 887 | * |
| 888 | * This is true even when the connection has been |
| 889 | * established using a resolvable random address. |
| 890 | */ |
| 891 | bacpy(&addrinfo.bdaddr, &hcon->src); |
| 892 | addrinfo.addr_type = hcon->src_type; |
| 893 | |
| 894 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), |
| 895 | &addrinfo); |
| 896 | |
| 897 | *keydist &= ~SMP_DIST_ID_KEY; |
| 898 | } |
| 899 | |
| 900 | if (*keydist & SMP_DIST_SIGN) { |
| 901 | struct smp_cmd_sign_info sign; |
| 902 | struct smp_csrk *csrk; |
| 903 | |
| 904 | /* Generate a new random key */ |
| 905 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
| 906 | |
| 907 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 908 | if (csrk) { |
| 909 | csrk->master = 0x00; |
| 910 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); |
| 911 | } |
| 912 | smp->slave_csrk = csrk; |
| 913 | |
| 914 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
| 915 | |
| 916 | *keydist &= ~SMP_DIST_SIGN; |
| 917 | } |
| 918 | |
| 919 | /* If there are still keys to be received wait for them */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 920 | if (smp->remote_key_dist & KEY_DIST_MASK) { |
| 921 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 922 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 923 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 924 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 925 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 926 | smp_notify_keys(conn); |
| 927 | |
| 928 | smp_chan_destroy(conn); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 929 | } |
| 930 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 931 | static void smp_timeout(struct work_struct *work) |
| 932 | { |
| 933 | struct smp_chan *smp = container_of(work, struct smp_chan, |
| 934 | security_timer.work); |
| 935 | struct l2cap_conn *conn = smp->conn; |
| 936 | |
| 937 | BT_DBG("conn %p", conn); |
| 938 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 939 | hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 940 | } |
| 941 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 942 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
| 943 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 944 | struct l2cap_chan *chan = conn->smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 945 | struct smp_chan *smp; |
| 946 | |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 947 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 948 | if (!smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 949 | return NULL; |
| 950 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 951 | smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
| 952 | if (IS_ERR(smp->tfm_aes)) { |
| 953 | BT_ERR("Unable to create ECB crypto context"); |
| 954 | kfree(smp); |
| 955 | return NULL; |
| 956 | } |
| 957 | |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 958 | smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); |
| 959 | if (IS_ERR(smp->tfm_cmac)) { |
| 960 | BT_ERR("Unable to create CMAC crypto context"); |
| 961 | crypto_free_blkcipher(smp->tfm_aes); |
| 962 | kfree(smp); |
| 963 | return NULL; |
| 964 | } |
| 965 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 966 | smp->conn = conn; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 967 | chan->data = smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 968 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 969 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); |
| 970 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 971 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); |
| 972 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 973 | hci_conn_hold(conn->hcon); |
| 974 | |
| 975 | return smp; |
| 976 | } |
| 977 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 978 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
| 979 | { |
Johan Hedberg | b10e801 | 2014-06-27 14:23:07 +0300 | [diff] [blame] | 980 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 981 | struct l2cap_chan *chan; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 982 | struct smp_chan *smp; |
| 983 | u32 value; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 984 | int err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 985 | |
| 986 | BT_DBG(""); |
| 987 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 988 | if (!conn) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 989 | return -ENOTCONN; |
| 990 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 991 | chan = conn->smp; |
| 992 | if (!chan) |
| 993 | return -ENOTCONN; |
| 994 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 995 | l2cap_chan_lock(chan); |
| 996 | if (!chan->data) { |
| 997 | err = -ENOTCONN; |
| 998 | goto unlock; |
| 999 | } |
| 1000 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1001 | smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1002 | |
| 1003 | switch (mgmt_op) { |
| 1004 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 1005 | value = le32_to_cpu(passkey); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1006 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1007 | BT_DBG("PassKey: %d", value); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1008 | put_unaligned_le32(value, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1009 | /* Fall Through */ |
| 1010 | case MGMT_OP_USER_CONFIRM_REPLY: |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1011 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1012 | break; |
| 1013 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 1014 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1015 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1016 | err = 0; |
| 1017 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1018 | default: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1019 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1020 | err = -EOPNOTSUPP; |
| 1021 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1024 | err = 0; |
| 1025 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1026 | /* If it is our turn to send Pairing Confirm, do so now */ |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1027 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
| 1028 | u8 rsp = smp_confirm(smp); |
| 1029 | if (rsp) |
| 1030 | smp_failure(conn, rsp); |
| 1031 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1032 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1033 | unlock: |
| 1034 | l2cap_chan_unlock(chan); |
| 1035 | return err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1038 | 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] | 1039 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1040 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1041 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1042 | struct hci_dev *hdev = conn->hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1043 | struct smp_chan *smp; |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1044 | u8 key_size, auth, sec_level; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1045 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1046 | |
| 1047 | BT_DBG("conn %p", conn); |
| 1048 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1049 | if (skb->len < sizeof(*req)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1050 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1051 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1052 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1053 | return SMP_CMD_NOTSUPP; |
| 1054 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1055 | if (!chan->data) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1056 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1057 | else |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1058 | smp = chan->data; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1059 | |
Andrei Emeltchenko | d08fd0e | 2012-07-19 17:03:43 +0300 | [diff] [blame] | 1060 | if (!smp) |
| 1061 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1062 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1063 | /* We didn't start the pairing, so match remote */ |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1064 | auth = req->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1065 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1066 | if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1067 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1068 | return SMP_PAIRING_NOTSUPP; |
| 1069 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1070 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1071 | memcpy(&smp->preq[1], req, sizeof(*req)); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1072 | skb_pull(skb, sizeof(*req)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1073 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 1074 | if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 1075 | sec_level = BT_SECURITY_MEDIUM; |
| 1076 | else |
| 1077 | sec_level = authreq_to_seclevel(auth); |
| 1078 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1079 | if (sec_level > conn->hcon->pending_sec_level) |
| 1080 | conn->hcon->pending_sec_level = sec_level; |
Ido Yariv | fdde0a2 | 2012-03-05 20:09:38 +0200 | [diff] [blame] | 1081 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1082 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1083 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1084 | u8 method; |
| 1085 | |
| 1086 | method = get_auth_method(smp, conn->hcon->io_capability, |
| 1087 | req->io_capability); |
| 1088 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1089 | return SMP_AUTH_REQUIREMENTS; |
| 1090 | } |
| 1091 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1092 | build_pairing_cmd(conn, req, &rsp, auth); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1093 | |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1094 | if (rsp.auth_req & SMP_AUTH_SC) |
| 1095 | set_bit(SMP_FLAG_SC, &smp->flags); |
| 1096 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1097 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 1098 | if (check_enc_key_size(conn, key_size)) |
| 1099 | return SMP_ENC_KEY_SIZE; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1100 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1101 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1102 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1103 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1104 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1105 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1106 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1107 | |
| 1108 | clear_bit(SMP_FLAG_INITIATOR, &smp->flags); |
| 1109 | |
| 1110 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1111 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1112 | /* Clear bits which are generated but not distributed */ |
| 1113 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1114 | /* Wait for Public Key from Initiating Device */ |
| 1115 | return 0; |
| 1116 | } else { |
| 1117 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 1118 | } |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1119 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1120 | /* Request setup of TK */ |
| 1121 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); |
| 1122 | if (ret) |
| 1123 | return SMP_UNSPECIFIED; |
| 1124 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1125 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1126 | } |
| 1127 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1128 | static u8 sc_send_public_key(struct smp_chan *smp) |
| 1129 | { |
| 1130 | BT_DBG(""); |
| 1131 | |
| 1132 | /* Generate local key pair for Secure Connections */ |
| 1133 | if (!ecc_make_key(smp->local_pk, smp->local_sk)) |
| 1134 | return SMP_UNSPECIFIED; |
| 1135 | |
| 1136 | BT_DBG("Local Public Key X: %32phN", smp->local_pk); |
| 1137 | BT_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]); |
| 1138 | BT_DBG("Local Private Key: %32phN", smp->local_sk); |
| 1139 | |
| 1140 | smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); |
| 1141 | |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1145 | 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] | 1146 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1147 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1148 | struct l2cap_chan *chan = conn->smp; |
| 1149 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1150 | struct hci_dev *hdev = conn->hcon->hdev; |
Johan Hedberg | 3a7dbfb | 2014-09-10 17:37:41 -0700 | [diff] [blame] | 1151 | u8 key_size, auth; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1152 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1153 | |
| 1154 | BT_DBG("conn %p", conn); |
| 1155 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1156 | if (skb->len < sizeof(*rsp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1157 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1158 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1159 | if (conn->hcon->role != HCI_ROLE_MASTER) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1160 | return SMP_CMD_NOTSUPP; |
| 1161 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1162 | skb_pull(skb, sizeof(*rsp)); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1163 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1164 | req = (void *) &smp->preq[1]; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1165 | |
| 1166 | key_size = min(req->max_key_size, rsp->max_key_size); |
| 1167 | if (check_enc_key_size(conn, key_size)) |
| 1168 | return SMP_ENC_KEY_SIZE; |
| 1169 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1170 | auth = rsp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1171 | |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1172 | if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC)) |
| 1173 | set_bit(SMP_FLAG_SC, &smp->flags); |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 1174 | else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH) |
| 1175 | conn->hcon->pending_sec_level = BT_SECURITY_HIGH; |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1176 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1177 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1178 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1179 | u8 method; |
| 1180 | |
| 1181 | method = get_auth_method(smp, req->io_capability, |
| 1182 | rsp->io_capability); |
| 1183 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1184 | return SMP_AUTH_REQUIREMENTS; |
| 1185 | } |
| 1186 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1187 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1188 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1189 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1190 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1191 | |
Johan Hedberg | fdcc4be | 2014-03-14 10:53:50 +0200 | [diff] [blame] | 1192 | /* Update remote key distribution in case the remote cleared |
| 1193 | * some bits that we had enabled in our request. |
| 1194 | */ |
| 1195 | smp->remote_key_dist &= rsp->resp_key_dist; |
| 1196 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1197 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1198 | /* Clear bits which are generated but not distributed */ |
| 1199 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1200 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1201 | return sc_send_public_key(smp); |
| 1202 | } |
| 1203 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1204 | auth |= req->auth_req; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1205 | |
Johan Hedberg | 476585e | 2012-06-06 18:54:15 +0800 | [diff] [blame] | 1206 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1207 | if (ret) |
| 1208 | return SMP_UNSPECIFIED; |
| 1209 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1210 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1211 | |
| 1212 | /* Can't compose response until we have been confirmed */ |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1213 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1214 | return smp_confirm(smp); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1215 | |
| 1216 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1217 | } |
| 1218 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame^] | 1219 | static u8 sc_check_confirm(struct smp_chan *smp) |
| 1220 | { |
| 1221 | struct l2cap_conn *conn = smp->conn; |
| 1222 | |
| 1223 | BT_DBG(""); |
| 1224 | |
| 1225 | /* Public Key exchange must happen before any other steps */ |
| 1226 | if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags)) |
| 1227 | return SMP_UNSPECIFIED; |
| 1228 | |
| 1229 | if (conn->hcon->out) { |
| 1230 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1231 | smp->prnd); |
| 1232 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1233 | } |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1238 | 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] | 1239 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1240 | struct l2cap_chan *chan = conn->smp; |
| 1241 | struct smp_chan *smp = chan->data; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1242 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1243 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 1244 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1245 | if (skb->len < sizeof(smp->pcnf)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1246 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1247 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1248 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
| 1249 | skb_pull(skb, sizeof(smp->pcnf)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1250 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame^] | 1251 | if (test_bit(SMP_FLAG_SC, &smp->flags)) |
| 1252 | return sc_check_confirm(smp); |
| 1253 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1254 | if (conn->hcon->out) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1255 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1256 | smp->prnd); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1257 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1262 | return smp_confirm(smp); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1263 | else |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1264 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1265 | |
| 1266 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1267 | } |
| 1268 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1269 | 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] | 1270 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1271 | struct l2cap_chan *chan = conn->smp; |
| 1272 | struct smp_chan *smp = chan->data; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1273 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1274 | BT_DBG("conn %p", conn); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1275 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1276 | if (skb->len < sizeof(smp->rrnd)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1277 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1278 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1279 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1280 | skb_pull(skb, sizeof(smp->rrnd)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1281 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 1282 | return smp_random(smp); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1283 | } |
| 1284 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1285 | static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1286 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1287 | struct smp_ltk *key; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1288 | struct hci_conn *hcon = conn->hcon; |
| 1289 | |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 1290 | key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role); |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1291 | if (!key) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1292 | return false; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1293 | |
Johan Hedberg | a6f7833 | 2014-09-10 17:37:45 -0700 | [diff] [blame] | 1294 | if (smp_ltk_sec_level(key) < sec_level) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1295 | return false; |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 1296 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1297 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1298 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1299 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1300 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
| 1301 | hcon->enc_key_size = key->enc_size; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1302 | |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 1303 | /* We never store STKs for master role, so clear this flag */ |
| 1304 | clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
| 1305 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1306 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1307 | } |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 1308 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1309 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level, |
| 1310 | enum smp_key_pref key_pref) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1311 | { |
| 1312 | if (sec_level == BT_SECURITY_LOW) |
| 1313 | return true; |
| 1314 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1315 | /* If we're encrypted with an STK but the caller prefers using |
| 1316 | * LTK claim insufficient security. This way we allow the |
| 1317 | * connection to be re-encrypted with an LTK, even if the LTK |
| 1318 | * provides the same level of security. Only exception is if we |
| 1319 | * don't have an LTK (e.g. because of key distribution bits). |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 1320 | */ |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1321 | if (key_pref == SMP_USE_LTK && |
| 1322 | test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) && |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 1323 | hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role)) |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 1324 | return false; |
| 1325 | |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1326 | if (hcon->sec_level >= sec_level) |
| 1327 | return true; |
| 1328 | |
| 1329 | return false; |
| 1330 | } |
| 1331 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1332 | 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] | 1333 | { |
| 1334 | struct smp_cmd_security_req *rp = (void *) skb->data; |
| 1335 | struct smp_cmd_pairing cp; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1336 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1337 | struct hci_dev *hdev = hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1338 | struct smp_chan *smp; |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1339 | u8 sec_level, auth; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1340 | |
| 1341 | BT_DBG("conn %p", conn); |
| 1342 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1343 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1344 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1345 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1346 | if (hcon->role != HCI_ROLE_MASTER) |
Johan Hedberg | 86ca9ea | 2013-11-05 11:30:39 +0200 | [diff] [blame] | 1347 | return SMP_CMD_NOTSUPP; |
| 1348 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1349 | auth = rp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1350 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 1351 | if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 1352 | sec_level = BT_SECURITY_MEDIUM; |
| 1353 | else |
| 1354 | sec_level = authreq_to_seclevel(auth); |
| 1355 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1356 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 1357 | return 0; |
| 1358 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1359 | if (sec_level > hcon->pending_sec_level) |
| 1360 | hcon->pending_sec_level = sec_level; |
Vinicius Costa Gomes | feb45eb | 2011-08-25 20:02:35 -0300 | [diff] [blame] | 1361 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 1362 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1363 | return 0; |
| 1364 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1365 | smp = smp_chan_create(conn); |
Johan Hedberg | c29d244 | 2014-06-16 19:25:14 +0300 | [diff] [blame] | 1366 | if (!smp) |
| 1367 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1368 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1369 | if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1370 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | 616d55b | 2014-07-29 14:18:48 +0300 | [diff] [blame] | 1371 | return SMP_PAIRING_NOTSUPP; |
| 1372 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1373 | skb_pull(skb, sizeof(*rp)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1374 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1375 | memset(&cp, 0, sizeof(cp)); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1376 | build_pairing_cmd(conn, &cp, NULL, auth); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1377 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1378 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1379 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1380 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1381 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1382 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1383 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1384 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1385 | } |
| 1386 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1387 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1388 | { |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1389 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 1390 | struct l2cap_chan *chan; |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 1391 | struct smp_chan *smp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1392 | __u8 authreq; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1393 | int ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1394 | |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1395 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
| 1396 | |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 1397 | /* This may be NULL if there's an unexpected disconnection */ |
| 1398 | if (!conn) |
| 1399 | return 1; |
| 1400 | |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 1401 | chan = conn->smp; |
| 1402 | |
Johan Hedberg | 757aee0 | 2013-04-24 13:05:32 +0300 | [diff] [blame] | 1403 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 1404 | return 1; |
| 1405 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 1406 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 1407 | return 1; |
| 1408 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1409 | if (sec_level > hcon->pending_sec_level) |
| 1410 | hcon->pending_sec_level = sec_level; |
| 1411 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1412 | if (hcon->role == HCI_ROLE_MASTER) |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1413 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
| 1414 | return 0; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1415 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1416 | l2cap_chan_lock(chan); |
| 1417 | |
| 1418 | /* If SMP is already in progress ignore this request */ |
| 1419 | if (chan->data) { |
| 1420 | ret = 0; |
| 1421 | goto unlock; |
| 1422 | } |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1423 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1424 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1425 | if (!smp) { |
| 1426 | ret = 1; |
| 1427 | goto unlock; |
| 1428 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1429 | |
| 1430 | authreq = seclevel_to_authreq(sec_level); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1431 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 1432 | if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags)) |
| 1433 | authreq |= SMP_AUTH_SC; |
| 1434 | |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 1435 | /* Require MITM if IO Capability allows or the security level |
| 1436 | * requires it. |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 1437 | */ |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 1438 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1439 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 1440 | authreq |= SMP_AUTH_MITM; |
| 1441 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1442 | if (hcon->role == HCI_ROLE_MASTER) { |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1443 | struct smp_cmd_pairing cp; |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1444 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1445 | build_pairing_cmd(conn, &cp, NULL, authreq); |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1446 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1447 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1448 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1449 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1450 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1451 | } else { |
| 1452 | struct smp_cmd_security_req cp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1453 | cp.auth_req = authreq; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1454 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1455 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1456 | } |
| 1457 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1458 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1459 | ret = 0; |
Johan Hedberg | edca792 | 2014-03-24 15:54:11 +0200 | [diff] [blame] | 1460 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1461 | unlock: |
| 1462 | l2cap_chan_unlock(chan); |
| 1463 | return ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1464 | } |
| 1465 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1466 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1467 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1468 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1469 | struct l2cap_chan *chan = conn->smp; |
| 1470 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1471 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1472 | BT_DBG("conn %p", conn); |
| 1473 | |
| 1474 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1475 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1476 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1477 | SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 1478 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1479 | skb_pull(skb, sizeof(*rp)); |
| 1480 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1481 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1482 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1487 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1488 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1489 | struct l2cap_chan *chan = conn->smp; |
| 1490 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1491 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1492 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1493 | struct smp_ltk *ltk; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1494 | u8 authenticated; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1495 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1496 | BT_DBG("conn %p", conn); |
| 1497 | |
| 1498 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1499 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1500 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 1501 | /* Mark the information as received */ |
| 1502 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; |
| 1503 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1504 | if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 1505 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
Johan Hedberg | 196332f | 2014-09-09 16:21:46 -0700 | [diff] [blame] | 1506 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 1507 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1508 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 1509 | skb_pull(skb, sizeof(*rp)); |
| 1510 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 1511 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 1512 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1513 | authenticated, smp->tk, smp->enc_key_size, |
| 1514 | rp->ediv, rp->rand); |
| 1515 | smp->ltk = ltk; |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 1516 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1517 | smp_distribute_keys(smp); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1518 | |
| 1519 | return 0; |
| 1520 | } |
| 1521 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1522 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1523 | { |
| 1524 | struct smp_cmd_ident_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1525 | struct l2cap_chan *chan = conn->smp; |
| 1526 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1527 | |
| 1528 | BT_DBG(""); |
| 1529 | |
| 1530 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1531 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1532 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1533 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 1534 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1535 | skb_pull(skb, sizeof(*info)); |
| 1536 | |
| 1537 | memcpy(smp->irk, info->irk, 16); |
| 1538 | |
| 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, |
| 1543 | struct sk_buff *skb) |
| 1544 | { |
| 1545 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1546 | struct l2cap_chan *chan = conn->smp; |
| 1547 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1548 | struct hci_conn *hcon = conn->hcon; |
| 1549 | bdaddr_t rpa; |
| 1550 | |
| 1551 | BT_DBG(""); |
| 1552 | |
| 1553 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1554 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1555 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 1556 | /* Mark the information as received */ |
| 1557 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; |
| 1558 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1559 | if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 1560 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 1561 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1562 | skb_pull(skb, sizeof(*info)); |
| 1563 | |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1564 | /* Strictly speaking the Core Specification (4.1) allows sending |
| 1565 | * an empty address which would force us to rely on just the IRK |
| 1566 | * as "identity information". However, since such |
| 1567 | * implementations are not known of and in order to not over |
| 1568 | * complicate our implementation, simply pretend that we never |
| 1569 | * received an IRK for such a device. |
| 1570 | */ |
| 1571 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { |
| 1572 | BT_ERR("Ignoring IRK with no identity address"); |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 1573 | goto distribute; |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1576 | bacpy(&smp->id_addr, &info->bdaddr); |
| 1577 | smp->id_addr_type = info->addr_type; |
| 1578 | |
| 1579 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) |
| 1580 | bacpy(&rpa, &hcon->dst); |
| 1581 | else |
| 1582 | bacpy(&rpa, BDADDR_ANY); |
| 1583 | |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 1584 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
| 1585 | smp->id_addr_type, smp->irk, &rpa); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1586 | |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 1587 | distribute: |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 1588 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
| 1589 | smp_distribute_keys(smp); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1590 | |
| 1591 | return 0; |
| 1592 | } |
| 1593 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1594 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1595 | { |
| 1596 | struct smp_cmd_sign_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1597 | struct l2cap_chan *chan = conn->smp; |
| 1598 | struct smp_chan *smp = chan->data; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1599 | struct smp_csrk *csrk; |
| 1600 | |
| 1601 | BT_DBG("conn %p", conn); |
| 1602 | |
| 1603 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1604 | return SMP_INVALID_PARAMS; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1605 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1606 | /* Mark the information as received */ |
| 1607 | smp->remote_key_dist &= ~SMP_DIST_SIGN; |
| 1608 | |
| 1609 | skb_pull(skb, sizeof(*rp)); |
| 1610 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1611 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 1612 | if (csrk) { |
| 1613 | csrk->master = 0x01; |
| 1614 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); |
| 1615 | } |
| 1616 | smp->csrk = csrk; |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1617 | smp_distribute_keys(smp); |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1618 | |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1622 | static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1623 | { |
| 1624 | struct smp_cmd_public_key *key = (void *) skb->data; |
| 1625 | struct hci_conn *hcon = conn->hcon; |
| 1626 | struct l2cap_chan *chan = conn->smp; |
| 1627 | struct smp_chan *smp = chan->data; |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 1628 | struct smp_cmd_pairing_confirm cfm; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1629 | int err; |
| 1630 | |
| 1631 | BT_DBG("conn %p", conn); |
| 1632 | |
| 1633 | if (skb->len < sizeof(*key)) |
| 1634 | return SMP_INVALID_PARAMS; |
| 1635 | |
| 1636 | memcpy(smp->remote_pk, key, 64); |
| 1637 | |
| 1638 | /* Non-initiating device sends its public key after receiving |
| 1639 | * the key from the initiating device. |
| 1640 | */ |
| 1641 | if (!hcon->out) { |
| 1642 | err = sc_send_public_key(smp); |
| 1643 | if (err) |
| 1644 | return err; |
| 1645 | } |
| 1646 | |
| 1647 | BT_DBG("Remote Public Key X: %32phN", smp->remote_pk); |
| 1648 | BT_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]); |
| 1649 | |
| 1650 | if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey)) |
| 1651 | return SMP_UNSPECIFIED; |
| 1652 | |
| 1653 | BT_DBG("DHKey %32phN", smp->dhkey); |
| 1654 | |
| 1655 | set_bit(SMP_FLAG_REMOTE_PK, &smp->flags); |
| 1656 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 1657 | /* The Initiating device waits for the non-initiating device to |
| 1658 | * send the confirm value. |
| 1659 | */ |
| 1660 | if (conn->hcon->out) |
| 1661 | return 0; |
| 1662 | |
| 1663 | err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, |
| 1664 | 0, cfm.confirm_val); |
| 1665 | if (err) |
| 1666 | return SMP_UNSPECIFIED; |
| 1667 | |
| 1668 | smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); |
| 1669 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1670 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1674 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1675 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1676 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1677 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1678 | struct smp_chan *smp; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1679 | __u8 code, reason; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1680 | int err = 0; |
| 1681 | |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1682 | if (hcon->type != LE_LINK) { |
| 1683 | kfree_skb(skb); |
Johan Hedberg | 3432711f | 2013-10-16 11:37:01 +0300 | [diff] [blame] | 1684 | return 0; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 1687 | if (skb->len < 1) |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1688 | return -EILSEQ; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1689 | |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 1690 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 1691 | reason = SMP_PAIRING_NOTSUPP; |
| 1692 | goto done; |
| 1693 | } |
| 1694 | |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 1695 | code = skb->data[0]; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1696 | skb_pull(skb, sizeof(code)); |
| 1697 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1698 | smp = chan->data; |
| 1699 | |
| 1700 | if (code > SMP_CMD_MAX) |
| 1701 | goto drop; |
| 1702 | |
Johan Hedberg | 24bd0bd | 2014-09-10 17:37:43 -0700 | [diff] [blame] | 1703 | if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1704 | goto drop; |
| 1705 | |
| 1706 | /* If we don't have a context the only allowed commands are |
| 1707 | * pairing request and security request. |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 1708 | */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1709 | if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) |
| 1710 | goto drop; |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 1711 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1712 | switch (code) { |
| 1713 | case SMP_CMD_PAIRING_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1714 | reason = smp_cmd_pairing_req(conn, skb); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1715 | break; |
| 1716 | |
| 1717 | case SMP_CMD_PAIRING_FAIL: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1718 | smp_failure(conn, 0); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1719 | err = -EPERM; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1720 | break; |
| 1721 | |
| 1722 | case SMP_CMD_PAIRING_RSP: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1723 | reason = smp_cmd_pairing_rsp(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1724 | break; |
| 1725 | |
| 1726 | case SMP_CMD_SECURITY_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1727 | reason = smp_cmd_security_req(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1728 | break; |
| 1729 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1730 | case SMP_CMD_PAIRING_CONFIRM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1731 | reason = smp_cmd_pairing_confirm(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1732 | break; |
| 1733 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1734 | case SMP_CMD_PAIRING_RANDOM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1735 | reason = smp_cmd_pairing_random(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1736 | break; |
| 1737 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1738 | case SMP_CMD_ENCRYPT_INFO: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1739 | reason = smp_cmd_encrypt_info(conn, skb); |
| 1740 | break; |
| 1741 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1742 | case SMP_CMD_MASTER_IDENT: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1743 | reason = smp_cmd_master_ident(conn, skb); |
| 1744 | break; |
| 1745 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1746 | case SMP_CMD_IDENT_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1747 | reason = smp_cmd_ident_info(conn, skb); |
| 1748 | break; |
| 1749 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1750 | case SMP_CMD_IDENT_ADDR_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 1751 | reason = smp_cmd_ident_addr_info(conn, skb); |
| 1752 | break; |
| 1753 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1754 | case SMP_CMD_SIGN_INFO: |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 1755 | reason = smp_cmd_sign_info(conn, skb); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1756 | break; |
| 1757 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 1758 | case SMP_CMD_PUBLIC_KEY: |
| 1759 | reason = smp_cmd_public_key(conn, skb); |
| 1760 | break; |
| 1761 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1762 | default: |
| 1763 | BT_DBG("Unknown command code 0x%2.2x", code); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1764 | reason = SMP_CMD_NOTSUPP; |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 1765 | goto done; |
| 1766 | } |
| 1767 | |
| 1768 | done: |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 1769 | if (!err) { |
| 1770 | if (reason) |
| 1771 | smp_failure(conn, reason); |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 1772 | kfree_skb(skb); |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 1773 | } |
| 1774 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1775 | return err; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1776 | |
| 1777 | drop: |
| 1778 | BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name, |
| 1779 | code, &hcon->dst); |
| 1780 | kfree_skb(skb); |
| 1781 | return 0; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1782 | } |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1783 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1784 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
| 1785 | { |
| 1786 | struct l2cap_conn *conn = chan->conn; |
| 1787 | |
| 1788 | BT_DBG("chan %p", chan); |
| 1789 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1790 | if (chan->data) |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1791 | smp_chan_destroy(conn); |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1792 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1793 | conn->smp = NULL; |
| 1794 | l2cap_chan_put(chan); |
| 1795 | } |
| 1796 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1797 | static void smp_resume_cb(struct l2cap_chan *chan) |
| 1798 | { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1799 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1800 | struct l2cap_conn *conn = chan->conn; |
| 1801 | struct hci_conn *hcon = conn->hcon; |
| 1802 | |
| 1803 | BT_DBG("chan %p", chan); |
| 1804 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1805 | if (!smp) |
| 1806 | return; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1807 | |
Johan Hedberg | 84bc0db | 2014-09-05 22:19:49 +0300 | [diff] [blame] | 1808 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
| 1809 | return; |
| 1810 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1811 | cancel_delayed_work(&smp->security_timer); |
| 1812 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1813 | smp_distribute_keys(smp); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1814 | } |
| 1815 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1816 | static void smp_ready_cb(struct l2cap_chan *chan) |
| 1817 | { |
| 1818 | struct l2cap_conn *conn = chan->conn; |
| 1819 | |
| 1820 | BT_DBG("chan %p", chan); |
| 1821 | |
| 1822 | conn->smp = chan; |
| 1823 | l2cap_chan_hold(chan); |
| 1824 | } |
| 1825 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1826 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
| 1827 | { |
| 1828 | int err; |
| 1829 | |
| 1830 | BT_DBG("chan %p", chan); |
| 1831 | |
| 1832 | err = smp_sig_channel(chan, skb); |
| 1833 | if (err) { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1834 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1835 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1836 | if (smp) |
| 1837 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1838 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 1839 | hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | return err; |
| 1843 | } |
| 1844 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1845 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
| 1846 | unsigned long hdr_len, |
| 1847 | unsigned long len, int nb) |
| 1848 | { |
| 1849 | struct sk_buff *skb; |
| 1850 | |
| 1851 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); |
| 1852 | if (!skb) |
| 1853 | return ERR_PTR(-ENOMEM); |
| 1854 | |
| 1855 | skb->priority = HCI_PRIO_MAX; |
| 1856 | bt_cb(skb)->chan = chan; |
| 1857 | |
| 1858 | return skb; |
| 1859 | } |
| 1860 | |
| 1861 | static const struct l2cap_ops smp_chan_ops = { |
| 1862 | .name = "Security Manager", |
| 1863 | .ready = smp_ready_cb, |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1864 | .recv = smp_recv_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1865 | .alloc_skb = smp_alloc_skb_cb, |
| 1866 | .teardown = smp_teardown_cb, |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1867 | .resume = smp_resume_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1868 | |
| 1869 | .new_connection = l2cap_chan_no_new_connection, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1870 | .state_change = l2cap_chan_no_state_change, |
| 1871 | .close = l2cap_chan_no_close, |
| 1872 | .defer = l2cap_chan_no_defer, |
| 1873 | .suspend = l2cap_chan_no_suspend, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1874 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 1875 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 1876 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 1877 | }; |
| 1878 | |
| 1879 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) |
| 1880 | { |
| 1881 | struct l2cap_chan *chan; |
| 1882 | |
| 1883 | BT_DBG("pchan %p", pchan); |
| 1884 | |
| 1885 | chan = l2cap_chan_create(); |
| 1886 | if (!chan) |
| 1887 | return NULL; |
| 1888 | |
| 1889 | chan->chan_type = pchan->chan_type; |
| 1890 | chan->ops = &smp_chan_ops; |
| 1891 | chan->scid = pchan->scid; |
| 1892 | chan->dcid = chan->scid; |
| 1893 | chan->imtu = pchan->imtu; |
| 1894 | chan->omtu = pchan->omtu; |
| 1895 | chan->mode = pchan->mode; |
| 1896 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 1897 | /* Other L2CAP channels may request SMP routines in order to |
| 1898 | * change the security level. This means that the SMP channel |
| 1899 | * lock must be considered in its own category to avoid lockdep |
| 1900 | * warnings. |
| 1901 | */ |
| 1902 | atomic_set(&chan->nesting, L2CAP_NESTING_SMP); |
| 1903 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1904 | BT_DBG("created chan %p", chan); |
| 1905 | |
| 1906 | return chan; |
| 1907 | } |
| 1908 | |
| 1909 | static const struct l2cap_ops smp_root_chan_ops = { |
| 1910 | .name = "Security Manager Root", |
| 1911 | .new_connection = smp_new_conn_cb, |
| 1912 | |
| 1913 | /* None of these are implemented for the root channel */ |
| 1914 | .close = l2cap_chan_no_close, |
| 1915 | .alloc_skb = l2cap_chan_no_alloc_skb, |
| 1916 | .recv = l2cap_chan_no_recv, |
| 1917 | .state_change = l2cap_chan_no_state_change, |
| 1918 | .teardown = l2cap_chan_no_teardown, |
| 1919 | .ready = l2cap_chan_no_ready, |
| 1920 | .defer = l2cap_chan_no_defer, |
| 1921 | .suspend = l2cap_chan_no_suspend, |
| 1922 | .resume = l2cap_chan_no_resume, |
| 1923 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 1924 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 1925 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 1926 | }; |
| 1927 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1928 | int smp_register(struct hci_dev *hdev) |
| 1929 | { |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1930 | struct l2cap_chan *chan; |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1931 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1932 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1933 | BT_DBG("%s", hdev->name); |
| 1934 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 1935 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0); |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1936 | if (IS_ERR(tfm_aes)) { |
| 1937 | int err = PTR_ERR(tfm_aes); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1938 | BT_ERR("Unable to create crypto context"); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1939 | return err; |
| 1940 | } |
| 1941 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1942 | chan = l2cap_chan_create(); |
| 1943 | if (!chan) { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1944 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1945 | return -ENOMEM; |
| 1946 | } |
| 1947 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1948 | chan->data = tfm_aes; |
| 1949 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1950 | l2cap_add_scid(chan, L2CAP_CID_SMP); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1951 | |
| 1952 | l2cap_chan_set_defaults(chan); |
| 1953 | |
| 1954 | bacpy(&chan->src, &hdev->bdaddr); |
| 1955 | chan->src_type = BDADDR_LE_PUBLIC; |
| 1956 | chan->state = BT_LISTEN; |
| 1957 | chan->mode = L2CAP_MODE_BASIC; |
| 1958 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 1959 | chan->ops = &smp_root_chan_ops; |
| 1960 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 1961 | /* Set correct nesting level for a parent/listening channel */ |
| 1962 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
| 1963 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1964 | hdev->smp_data = chan; |
| 1965 | |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | void smp_unregister(struct hci_dev *hdev) |
| 1970 | { |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1971 | struct l2cap_chan *chan = hdev->smp_data; |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1972 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1973 | |
| 1974 | if (!chan) |
| 1975 | return; |
| 1976 | |
| 1977 | BT_DBG("%s chan %p", hdev->name, chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1978 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 1979 | tfm_aes = chan->data; |
| 1980 | if (tfm_aes) { |
| 1981 | chan->data = NULL; |
| 1982 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1983 | } |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 1984 | |
| 1985 | hdev->smp_data = NULL; |
| 1986 | l2cap_chan_put(chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 1987 | } |