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