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