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