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