Anderson Briglia | 133e14c | 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 | |
| 23 | #include <net/bluetooth/bluetooth.h> |
| 24 | #include <net/bluetooth/hci_core.h> |
| 25 | #include <net/bluetooth/l2cap.h> |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 26 | #include <net/bluetooth/mgmt.h> |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 27 | #include <net/bluetooth/smp.h> |
Anderson Briglia | 8e7a3c5 | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 28 | #include <linux/crypto.h> |
| 29 | #include <crypto/b128ops.h> |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 30 | #include <asm/unaligned.h> |
Anderson Briglia | 8e7a3c5 | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 31 | |
Vinicius Costa Gomes | b19d5ce | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 32 | #define SMP_TIMEOUT 30000 /* 30 seconds */ |
| 33 | |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 34 | #define SMP_MIN_CONN_INTERVAL 40 /* 50ms (40 * 1.25ms) */ |
| 35 | #define SMP_MAX_CONN_INTERVAL 56 /* 70ms (56 * 1.25ms) */ |
| 36 | #define SMP_MAX_CONN_LATENCY 0 /* 0ms (0 * 1.25ms) */ |
| 37 | #define SMP_SUPERVISION_TIMEOUT 500 /* 5 seconds (500 * 10ms) */ |
| 38 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 39 | #ifndef FALSE |
| 40 | #define FALSE 0 |
| 41 | #define TRUE (!FALSE) |
| 42 | #endif |
| 43 | |
| 44 | static int smp_distribute_keys(struct l2cap_conn *conn, __u8 force); |
| 45 | |
Anderson Briglia | 8e7a3c5 | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 46 | static inline void swap128(u8 src[16], u8 dst[16]) |
| 47 | { |
| 48 | int i; |
| 49 | for (i = 0; i < 16; i++) |
| 50 | dst[15 - i] = src[i]; |
| 51 | } |
| 52 | |
| 53 | static inline void swap56(u8 src[7], u8 dst[7]) |
| 54 | { |
| 55 | int i; |
| 56 | for (i = 0; i < 7; i++) |
| 57 | dst[6 - i] = src[i]; |
| 58 | } |
| 59 | |
| 60 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) |
| 61 | { |
| 62 | struct blkcipher_desc desc; |
| 63 | struct scatterlist sg; |
| 64 | int err, iv_len; |
| 65 | unsigned char iv[128]; |
| 66 | |
| 67 | if (tfm == NULL) { |
| 68 | BT_ERR("tfm %p", tfm); |
| 69 | return -EINVAL; |
| 70 | } |
| 71 | |
| 72 | desc.tfm = tfm; |
| 73 | desc.flags = 0; |
| 74 | |
| 75 | err = crypto_blkcipher_setkey(tfm, k, 16); |
| 76 | if (err) { |
| 77 | BT_ERR("cipher setkey failed: %d", err); |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | sg_init_one(&sg, r, 16); |
| 82 | |
| 83 | iv_len = crypto_blkcipher_ivsize(tfm); |
| 84 | if (iv_len) { |
| 85 | memset(&iv, 0xff, iv_len); |
| 86 | crypto_blkcipher_set_iv(tfm, iv, iv_len); |
| 87 | } |
| 88 | |
| 89 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
| 90 | if (err) |
| 91 | BT_ERR("Encrypt data error %d", err); |
| 92 | |
| 93 | return err; |
| 94 | } |
| 95 | |
| 96 | static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], |
| 97 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, |
| 98 | u8 _rat, bdaddr_t *ra, u8 res[16]) |
| 99 | { |
| 100 | u8 p1[16], p2[16]; |
| 101 | int err; |
| 102 | |
| 103 | memset(p1, 0, 16); |
| 104 | |
| 105 | /* p1 = pres || preq || _rat || _iat */ |
| 106 | swap56(pres, p1); |
| 107 | swap56(preq, p1 + 7); |
| 108 | p1[14] = _rat; |
| 109 | p1[15] = _iat; |
| 110 | |
| 111 | memset(p2, 0, 16); |
| 112 | |
| 113 | /* p2 = padding || ia || ra */ |
| 114 | baswap((bdaddr_t *) (p2 + 4), ia); |
| 115 | baswap((bdaddr_t *) (p2 + 10), ra); |
| 116 | |
| 117 | /* res = r XOR p1 */ |
| 118 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); |
| 119 | |
| 120 | /* res = e(k, res) */ |
| 121 | err = smp_e(tfm, k, res); |
| 122 | if (err) { |
| 123 | BT_ERR("Encrypt data error"); |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | /* res = res XOR p2 */ |
| 128 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); |
| 129 | |
| 130 | /* res = e(k, res) */ |
| 131 | err = smp_e(tfm, k, res); |
| 132 | if (err) |
| 133 | BT_ERR("Encrypt data error"); |
| 134 | |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], |
| 139 | u8 r1[16], u8 r2[16], u8 _r[16]) |
| 140 | { |
| 141 | int err; |
| 142 | |
| 143 | /* Just least significant octets from r1 and r2 are considered */ |
| 144 | memcpy(_r, r1 + 8, 8); |
| 145 | memcpy(_r + 8, r2 + 8, 8); |
| 146 | |
| 147 | err = smp_e(tfm, k, _r); |
| 148 | if (err) |
| 149 | BT_ERR("Encrypt data error"); |
| 150 | |
| 151 | return err; |
| 152 | } |
| 153 | |
| 154 | static int smp_rand(u8 *buf) |
| 155 | { |
| 156 | get_random_bytes(buf, 16); |
| 157 | |
| 158 | return 0; |
| 159 | } |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 160 | |
| 161 | static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 162 | u16 dlen, void *data) |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 163 | { |
| 164 | struct sk_buff *skb; |
| 165 | struct l2cap_hdr *lh; |
| 166 | int len; |
| 167 | |
| 168 | len = L2CAP_HDR_SIZE + sizeof(code) + dlen; |
| 169 | |
| 170 | if (len > conn->mtu) |
| 171 | return NULL; |
| 172 | |
| 173 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 174 | if (!skb) |
| 175 | return NULL; |
| 176 | |
| 177 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 178 | lh->len = cpu_to_le16(sizeof(code) + dlen); |
| 179 | lh->cid = cpu_to_le16(L2CAP_CID_SMP); |
| 180 | |
| 181 | memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code)); |
| 182 | |
| 183 | memcpy(skb_put(skb, dlen), data, dlen); |
| 184 | |
| 185 | return skb; |
| 186 | } |
| 187 | |
| 188 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
| 189 | { |
| 190 | struct sk_buff *skb = smp_build_cmd(conn, code, len, data); |
| 191 | |
| 192 | BT_DBG("code 0x%2.2x", code); |
| 193 | |
| 194 | if (!skb) |
| 195 | return; |
| 196 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | hci_send_acl(conn->hcon, NULL, skb, 0); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 198 | } |
| 199 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 200 | static __u8 authreq_to_seclevel(__u8 authreq) |
| 201 | { |
| 202 | if (authreq & SMP_AUTH_MITM) |
| 203 | return BT_SECURITY_HIGH; |
| 204 | else if (authreq & SMP_AUTH_BONDING) |
| 205 | return BT_SECURITY_MEDIUM; |
| 206 | else |
| 207 | return BT_SECURITY_LOW; |
| 208 | } |
| 209 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 210 | static __u8 seclevel_to_authreq(__u8 level) |
| 211 | { |
| 212 | switch (level) { |
| 213 | case BT_SECURITY_HIGH: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 214 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 215 | |
| 216 | default: |
| 217 | return SMP_AUTH_NONE; |
| 218 | } |
| 219 | } |
| 220 | |
Vinicius Costa Gomes | 901920a | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 221 | static void build_pairing_cmd(struct l2cap_conn *conn, |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 222 | struct smp_cmd_pairing *req, |
| 223 | struct smp_cmd_pairing *rsp, |
| 224 | __u8 authreq) |
Vinicius Costa Gomes | 901920a | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 225 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 226 | struct hci_conn *hcon = conn->hcon; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 227 | u8 all_keys = 0; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 228 | u8 dist_keys = 0; |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 229 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 230 | dist_keys = SMP_DIST_ENC_KEY; |
| 231 | authreq |= SMP_AUTH_BONDING; |
| 232 | |
| 233 | BT_DBG("conn->hcon->io_capability:%d", conn->hcon->io_capability); |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 234 | |
| 235 | if (rsp == NULL) { |
| 236 | req->io_capability = conn->hcon->io_capability; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 237 | req->oob_flag = hcon->oob ? SMP_OOB_PRESENT : |
| 238 | SMP_OOB_NOT_PRESENT; |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 239 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 240 | req->init_key_dist = all_keys; |
| 241 | req->resp_key_dist = dist_keys; |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 242 | req->auth_req = authreq; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 243 | BT_DBG("SMP_CMD_PAIRING_REQ %d %d %d %d %2.2x %2.2x", |
| 244 | req->io_capability, req->oob_flag, |
| 245 | req->auth_req, req->max_key_size, |
| 246 | req->init_key_dist, req->resp_key_dist); |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 250 | /* Only request OOB if remote AND we support it */ |
| 251 | if (req->oob_flag) |
| 252 | rsp->oob_flag = hcon->oob ? SMP_OOB_PRESENT : |
| 253 | SMP_OOB_NOT_PRESENT; |
| 254 | else |
| 255 | rsp->oob_flag = SMP_OOB_NOT_PRESENT; |
| 256 | |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 257 | rsp->io_capability = conn->hcon->io_capability; |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 258 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | rsp->init_key_dist = req->init_key_dist & all_keys; |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 260 | rsp->resp_key_dist = req->resp_key_dist & dist_keys; |
| 261 | rsp->auth_req = authreq; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 262 | BT_DBG("SMP_CMD_PAIRING_RSP %d %d %d %d %2.2x %2.2x", |
| 263 | req->io_capability, req->oob_flag, req->auth_req, |
| 264 | req->max_key_size, req->init_key_dist, |
| 265 | req->resp_key_dist); |
Vinicius Costa Gomes | 901920a | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 266 | } |
| 267 | |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 268 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
| 269 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 270 | struct hci_conn *hcon = conn->hcon; |
| 271 | |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 272 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
| 273 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
| 274 | return SMP_ENC_KEY_SIZE; |
| 275 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 276 | hcon->smp_key_size = max_key_size; |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 281 | #define JUST_WORKS SMP_JUST_WORKS |
| 282 | #define REQ_PASSKEY SMP_REQ_PASSKEY |
| 283 | #define CFM_PASSKEY SMP_CFM_PASSKEY |
| 284 | #define JUST_CFM SMP_JUST_CFM |
| 285 | #define OVERLAP SMP_OVERLAP |
| 286 | static const u8 gen_method[5][5] = { |
| 287 | {JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY}, |
| 288 | {JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY}, |
| 289 | {CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY}, |
| 290 | {JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM}, |
| 291 | {CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP} |
| 292 | }; |
| 293 | |
| 294 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
| 295 | u8 local_io, u8 remote_io) |
| 296 | { |
| 297 | struct hci_conn *hcon = conn->hcon; |
| 298 | u8 method; |
| 299 | u32 passkey = 0; |
Brian Gix | 3dd7017 | 2011-09-16 21:38:54 -0700 | [diff] [blame] | 300 | int ret = 0; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 301 | |
| 302 | /* Initialize key to JUST WORKS */ |
| 303 | memset(hcon->tk, 0, sizeof(hcon->tk)); |
| 304 | hcon->tk_valid = FALSE; |
| 305 | hcon->auth = auth; |
| 306 | |
| 307 | /* By definition, OOB data will be used if both sides have it available |
| 308 | */ |
| 309 | if (remote_oob && hcon->oob) { |
| 310 | method = SMP_REQ_OOB; |
| 311 | goto agent_request; |
| 312 | } |
| 313 | |
| 314 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); |
| 315 | |
| 316 | /* If neither side wants MITM, use JUST WORKS */ |
| 317 | /* If either side has unknown io_caps, use JUST_WORKS */ |
| 318 | if (!(auth & SMP_AUTH_MITM) || |
| 319 | local_io > SMP_IO_KEYBOARD_DISPLAY || |
| 320 | remote_io > SMP_IO_KEYBOARD_DISPLAY) { |
| 321 | hcon->auth &= ~SMP_AUTH_MITM; |
| 322 | hcon->tk_valid = TRUE; |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /* MITM is now officially requested, but not required */ |
| 327 | /* Determine what we need (if anything) from the agent */ |
| 328 | method = gen_method[local_io][remote_io]; |
| 329 | |
| 330 | BT_DBG("tk_method: %d", method); |
| 331 | |
| 332 | if (method == SMP_JUST_WORKS || method == SMP_JUST_CFM) |
| 333 | hcon->auth &= ~SMP_AUTH_MITM; |
| 334 | |
| 335 | /* Don't bother confirming unbonded JUST_WORKS */ |
| 336 | if (!(auth & SMP_AUTH_BONDING) && method == SMP_JUST_CFM) { |
| 337 | hcon->tk_valid = TRUE; |
| 338 | return 0; |
| 339 | } else if (method == SMP_JUST_WORKS) { |
| 340 | hcon->tk_valid = TRUE; |
| 341 | return 0; |
| 342 | } else if (method == SMP_OVERLAP) { |
| 343 | if (hcon->link_mode & HCI_LM_MASTER) |
| 344 | method = SMP_CFM_PASSKEY; |
| 345 | else |
| 346 | method = SMP_REQ_PASSKEY; |
| 347 | } |
| 348 | |
| 349 | BT_DBG("tk_method-2: %d", method); |
| 350 | |
| 351 | if (method == SMP_CFM_PASSKEY) { |
| 352 | u8 key[16]; |
| 353 | /* Generate a passkey for display. It is not valid until |
| 354 | * confirmed. |
| 355 | */ |
| 356 | memset(key, 0, sizeof(key)); |
| 357 | get_random_bytes(&passkey, sizeof(passkey)); |
| 358 | passkey %= 1000000; |
| 359 | put_unaligned_le32(passkey, key); |
| 360 | swap128(key, hcon->tk); |
| 361 | BT_DBG("PassKey: %d", passkey); |
| 362 | } |
| 363 | |
| 364 | agent_request: |
Brian Gix | 3dd7017 | 2011-09-16 21:38:54 -0700 | [diff] [blame] | 365 | hci_dev_lock(hcon->hdev); |
| 366 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 367 | switch (method) { |
| 368 | case SMP_REQ_PASSKEY: |
Brian Gix | 3dd7017 | 2011-09-16 21:38:54 -0700 | [diff] [blame] | 369 | ret = mgmt_user_confirm_request(hcon->hdev->id, |
| 370 | HCI_EV_USER_PASSKEY_REQUEST, conn->dst, 0); |
| 371 | break; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 372 | case SMP_CFM_PASSKEY: |
| 373 | default: |
Brian Gix | 3dd7017 | 2011-09-16 21:38:54 -0700 | [diff] [blame] | 374 | ret = mgmt_user_confirm_request(hcon->hdev->id, |
| 375 | HCI_EV_USER_CONFIRM_REQUEST, conn->dst, passkey); |
| 376 | break; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 377 | } |
Brian Gix | 3dd7017 | 2011-09-16 21:38:54 -0700 | [diff] [blame] | 378 | |
| 379 | hci_dev_unlock(hcon->hdev); |
| 380 | |
| 381 | return ret; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int send_pairing_confirm(struct l2cap_conn *conn) |
| 385 | { |
| 386 | struct hci_conn *hcon = conn->hcon; |
| 387 | struct crypto_blkcipher *tfm = hcon->hdev->tfm; |
| 388 | struct smp_cmd_pairing_confirm cp; |
| 389 | int ret; |
| 390 | u8 res[16]; |
| 391 | |
| 392 | if (conn->hcon->out) |
| 393 | ret = smp_c1(tfm, hcon->tk, hcon->prnd, hcon->preq, hcon->prsp, |
| 394 | 0, conn->src, hcon->dst_type, conn->dst, res); |
| 395 | else |
| 396 | ret = smp_c1(tfm, hcon->tk, hcon->prnd, hcon->preq, hcon->prsp, |
| 397 | hcon->dst_type, conn->dst, 0, conn->src, res); |
| 398 | |
| 399 | if (ret) |
| 400 | return SMP_CONFIRM_FAILED; |
| 401 | |
| 402 | swap128(res, cp.confirm_val); |
| 403 | |
| 404 | hcon->cfm_pending = FALSE; |
| 405 | |
| 406 | smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 411 | int le_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, void *cp) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 412 | { |
| 413 | struct mgmt_cp_user_passkey_reply *psk_reply = cp; |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 414 | struct l2cap_conn *conn = hcon->smp_conn; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 415 | u8 key[16]; |
| 416 | u8 reason = 0; |
| 417 | int ret = 0; |
| 418 | |
| 419 | BT_DBG(""); |
| 420 | |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 421 | hcon->tk_valid = TRUE; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 422 | |
| 423 | switch (mgmt_op) { |
| 424 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
| 425 | reason = SMP_CONFIRM_FAILED; |
| 426 | break; |
| 427 | case MGMT_OP_USER_CONFIRM_REPLY: |
| 428 | break; |
| 429 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 430 | memset(key, 0, sizeof(key)); |
| 431 | BT_DBG("PassKey: %d", psk_reply->passkey); |
| 432 | put_unaligned_le32(psk_reply->passkey, key); |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 433 | swap128(key, hcon->tk); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 434 | break; |
| 435 | default: |
| 436 | reason = SMP_CONFIRM_FAILED; |
| 437 | ret = -EOPNOTSUPP; |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | if (reason) { |
| 442 | BT_DBG("smp_send_cmd: SMP_CMD_PAIRING_FAIL"); |
| 443 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
| 444 | &reason); |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 445 | del_timer(&hcon->smp_timer); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 446 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Brian Gix | 58ba077 | 2011-09-23 13:40:16 -0700 | [diff] [blame] | 447 | mgmt_auth_failed(hcon->hdev->id, conn->dst, reason); |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 448 | hci_conn_put(hcon); |
| 449 | } else if (hcon->cfm_pending) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 450 | BT_DBG("send_pairing_confirm"); |
| 451 | ret = send_pairing_confirm(conn); |
| 452 | } |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 457 | static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 458 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 459 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 460 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
| 461 | u8 key_size; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 462 | u8 auth = SMP_AUTH_NONE; |
| 463 | int ret; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 464 | |
| 465 | BT_DBG("conn %p", conn); |
| 466 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 467 | hcon->preq[0] = SMP_CMD_PAIRING_REQ; |
| 468 | memcpy(&hcon->preq[1], req, sizeof(*req)); |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 469 | skb_pull(skb, sizeof(*req)); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 470 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 471 | if (req->oob_flag && hcon->oob) { |
| 472 | /* By definition, OOB data pairing will have MITM protection */ |
| 473 | auth = req->auth_req | SMP_AUTH_MITM; |
| 474 | } else if (req->auth_req & SMP_AUTH_BONDING) { |
| 475 | /* We will attempt MITM for all Bonding attempts */ |
| 476 | auth = SMP_AUTH_BONDING | SMP_AUTH_MITM; |
| 477 | } |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 478 | |
| 479 | /* We didn't start the pairing, so no requirements */ |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 480 | build_pairing_cmd(conn, req, &rsp, auth); |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 481 | |
| 482 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 483 | if (check_enc_key_size(conn, key_size)) |
| 484 | return SMP_ENC_KEY_SIZE; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 485 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 486 | ret = smp_rand(hcon->prnd); |
| 487 | if (ret) |
| 488 | return SMP_UNSPECIFIED; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 489 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 490 | /* Request setup of TK */ |
| 491 | ret = tk_request(conn, req->oob_flag, auth, rsp.io_capability, |
| 492 | req->io_capability); |
| 493 | if (ret) |
| 494 | return SMP_UNSPECIFIED; |
| 495 | |
| 496 | hcon->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 497 | memcpy(&hcon->prsp[1], &rsp, sizeof(rsp)); |
Anderson Briglia | c8e856e | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 498 | |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 499 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 500 | |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 501 | mod_timer(&hcon->smp_timer, jiffies + msecs_to_jiffies(SMP_TIMEOUT)); |
Vinicius Costa Gomes | b19d5ce | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 502 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 503 | return 0; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 504 | } |
| 505 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 506 | static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 507 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 508 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 509 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 510 | u8 key_size, auth = SMP_AUTH_NONE; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 511 | int ret; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 512 | |
| 513 | BT_DBG("conn %p", conn); |
| 514 | |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 515 | skb_pull(skb, sizeof(*rsp)); |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 516 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 517 | req = (void *) &hcon->preq[1]; |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 518 | |
| 519 | key_size = min(req->max_key_size, rsp->max_key_size); |
| 520 | if (check_enc_key_size(conn, key_size)) |
| 521 | return SMP_ENC_KEY_SIZE; |
| 522 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 523 | hcon->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 524 | memcpy(&hcon->prsp[1], rsp, sizeof(*rsp)); |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 525 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 526 | ret = smp_rand(hcon->prnd); |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 527 | if (ret) |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 528 | return SMP_UNSPECIFIED; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 529 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 530 | if ((req->auth_req & SMP_AUTH_BONDING) && |
| 531 | (rsp->auth_req & SMP_AUTH_BONDING)) |
| 532 | auth = SMP_AUTH_BONDING; |
| 533 | |
| 534 | auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; |
| 535 | |
| 536 | ret = tk_request(conn, req->oob_flag, auth, rsp->io_capability, |
| 537 | req->io_capability); |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 538 | if (ret) |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 539 | return SMP_UNSPECIFIED; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 540 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 541 | hcon->cfm_pending = TRUE; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 542 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 543 | /* Can't compose response until we have been confirmed */ |
| 544 | if (!hcon->tk_valid) |
| 545 | return 0; |
| 546 | |
| 547 | ret = send_pairing_confirm(conn); |
| 548 | if (ret) |
| 549 | return SMP_CONFIRM_FAILED; |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 550 | |
| 551 | return 0; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 552 | } |
| 553 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 554 | static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 555 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 556 | struct hci_conn *hcon = conn->hcon; |
| 557 | int ret; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 558 | |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 559 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 560 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 561 | memcpy(hcon->pcnf, skb->data, sizeof(hcon->pcnf)); |
| 562 | skb_pull(skb, sizeof(hcon->pcnf)); |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 563 | |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 564 | if (conn->hcon->out) { |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 565 | u8 random[16]; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 566 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 567 | swap128(hcon->prnd, random); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 568 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 569 | random); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 570 | } else if (hcon->tk_valid) { |
| 571 | ret = send_pairing_confirm(conn); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 572 | |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 573 | if (ret) |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 574 | return SMP_CONFIRM_FAILED; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 575 | } else |
| 576 | hcon->cfm_pending = TRUE; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 577 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 578 | |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 579 | mod_timer(&hcon->smp_timer, jiffies + msecs_to_jiffies(SMP_TIMEOUT)); |
Vinicius Costa Gomes | b19d5ce | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 580 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 581 | return 0; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 582 | } |
| 583 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 584 | static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 585 | { |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 586 | struct hci_conn *hcon = conn->hcon; |
| 587 | struct crypto_blkcipher *tfm = hcon->hdev->tfm; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 588 | int ret; |
Vinicius Costa Gomes | c34e25e | 2011-06-09 18:50:48 -0300 | [diff] [blame] | 589 | u8 key[16], res[16], random[16], confirm[16]; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 590 | |
| 591 | swap128(skb->data, random); |
| 592 | skb_pull(skb, sizeof(random)); |
| 593 | |
| 594 | if (conn->hcon->out) |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 595 | ret = smp_c1(tfm, hcon->tk, random, hcon->preq, hcon->prsp, 0, |
| 596 | conn->src, hcon->dst_type, conn->dst, |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 597 | res); |
| 598 | else |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 599 | ret = smp_c1(tfm, hcon->tk, random, hcon->preq, hcon->prsp, |
| 600 | hcon->dst_type, conn->dst, 0, conn->src, |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 601 | res); |
| 602 | if (ret) |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 603 | return SMP_UNSPECIFIED; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 604 | |
| 605 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 606 | |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 607 | swap128(res, confirm); |
| 608 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 609 | if (memcmp(hcon->pcnf, confirm, sizeof(hcon->pcnf)) != 0) { |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 610 | BT_ERR("Pairing failed (confirmation values mismatch)"); |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 611 | return SMP_CONFIRM_FAILED; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 612 | } |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 613 | |
| 614 | if (conn->hcon->out) { |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 615 | u8 stk[16], rand[8]; |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 616 | __le16 ediv; |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 617 | |
Vinicius Costa Gomes | 735038c | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 618 | memset(rand, 0, sizeof(rand)); |
| 619 | ediv = 0; |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 620 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 621 | smp_s1(tfm, hcon->tk, random, hcon->prnd, key); |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 622 | swap128(key, stk); |
| 623 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 624 | memset(stk + hcon->smp_key_size, 0, |
| 625 | SMP_MAX_ENC_KEY_SIZE - hcon->smp_key_size); |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 626 | |
| 627 | hci_le_start_enc(hcon, ediv, rand, stk); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 628 | hcon->enc_key_size = hcon->smp_key_size; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 629 | } else { |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 630 | u8 stk[16], r[16], rand[8]; |
| 631 | __le16 ediv; |
| 632 | |
| 633 | memset(rand, 0, sizeof(rand)); |
| 634 | ediv = 0; |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 635 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 636 | swap128(hcon->prnd, r); |
Anderson Briglia | 21b8a2b | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 637 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r); |
| 638 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 639 | smp_s1(tfm, hcon->tk, hcon->prnd, random, key); |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 640 | swap128(key, stk); |
Vinicius Costa Gomes | 397d9ae | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 641 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 642 | memset(stk + hcon->smp_key_size, 0, |
| 643 | SMP_MAX_ENC_KEY_SIZE - hcon->smp_key_size); |
Vinicius Costa Gomes | f1b45c5 | 2011-07-07 18:59:40 -0300 | [diff] [blame] | 644 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 645 | hci_add_ltk(conn->hcon->hdev, 0, conn->dst, hcon->dst_type, |
| 646 | hcon->smp_key_size, hcon->auth, ediv, rand, stk); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 647 | } |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 648 | |
| 649 | return 0; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 650 | } |
| 651 | |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 652 | static int smp_encrypt_link(struct hci_conn *hcon, struct link_key *key) |
| 653 | { |
| 654 | struct key_master_id *master; |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 655 | u8 sec_level; |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 656 | u8 zerobuf[8]; |
| 657 | |
| 658 | if (!hcon || !key || !key->data) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | memset(zerobuf, 0, sizeof(zerobuf)); |
| 662 | |
| 663 | master = (void *) key->data; |
| 664 | |
| 665 | if (!master->ediv && !memcmp(master->rand, zerobuf, sizeof(zerobuf))) |
| 666 | return -EINVAL; |
| 667 | |
| 668 | hcon->enc_key_size = key->pin_len; |
| 669 | hcon->sec_req = TRUE; |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 670 | sec_level = authreq_to_seclevel(key->auth); |
| 671 | |
| 672 | BT_DBG("cur %d, req: %d", hcon->sec_level, sec_level); |
| 673 | |
| 674 | if (sec_level > hcon->sec_level) |
| 675 | hcon->pending_sec_level = sec_level; |
| 676 | |
| 677 | |
| 678 | if (!(hcon->link_mode & HCI_LM_ENCRYPT)) |
| 679 | hci_conn_hold(hcon); |
| 680 | |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 681 | hci_le_start_enc(hcon, master->ediv, master->rand, key->val); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 686 | static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 687 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 688 | struct hci_conn *hcon = conn->hcon; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 689 | struct smp_cmd_security_req *rp = (void *) skb->data; |
| 690 | struct smp_cmd_pairing cp; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 691 | struct link_key *key; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 692 | |
| 693 | BT_DBG("conn %p", conn); |
| 694 | |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 695 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 696 | return 0; |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 697 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 698 | key = hci_find_link_key_type(hcon->hdev, conn->dst, KEY_TYPE_LTK); |
| 699 | if (key && ((key->auth & SMP_AUTH_MITM) || |
| 700 | !(rp->auth_req & SMP_AUTH_MITM))) { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 701 | |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 702 | if (smp_encrypt_link(hcon, key) < 0) |
| 703 | goto invalid_key; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 704 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 708 | invalid_key: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 709 | hcon->sec_req = FALSE; |
| 710 | |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 711 | /* Switch to Pairing Connection Parameters */ |
| 712 | hci_le_conn_update(hcon, SMP_MIN_CONN_INTERVAL, SMP_MAX_CONN_INTERVAL, |
| 713 | SMP_MAX_CONN_LATENCY, SMP_SUPERVISION_TIMEOUT); |
| 714 | |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 715 | skb_pull(skb, sizeof(*rp)); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 716 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 717 | memset(&cp, 0, sizeof(cp)); |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 718 | build_pairing_cmd(conn, &cp, NULL, rp->auth_req); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 719 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 720 | hcon->preq[0] = SMP_CMD_PAIRING_REQ; |
| 721 | memcpy(&hcon->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | c8e856e | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 722 | |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 723 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 724 | |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 725 | mod_timer(&hcon->smp_timer, jiffies + msecs_to_jiffies(SMP_TIMEOUT)); |
Vinicius Costa Gomes | b19d5ce | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 726 | |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 727 | set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 728 | |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 729 | hci_conn_hold(hcon); |
| 730 | |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 731 | return 0; |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 732 | } |
| 733 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 734 | int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level) |
| 735 | { |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 736 | struct hci_conn *hcon = conn->hcon; |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 737 | __u8 authreq; |
| 738 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 739 | BT_DBG("conn %p hcon %p %d req: %d", |
| 740 | conn, hcon, hcon->sec_level, sec_level); |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 741 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 742 | if (IS_ERR(hcon->hdev->tfm)) |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 743 | return 1; |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 744 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 745 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 746 | return -EINPROGRESS; |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 747 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 748 | if (sec_level == BT_SECURITY_LOW) |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 749 | return 1; |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 750 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 751 | |
| 752 | if (hcon->sec_level >= sec_level) |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 753 | return 1; |
| 754 | |
| 755 | authreq = seclevel_to_authreq(sec_level); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 756 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 757 | hcon->smp_conn = conn; |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 758 | hcon->pending_sec_level = sec_level; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 759 | |
| 760 | if ((hcon->link_mode & HCI_LM_MASTER) && !hcon->sec_req) { |
Vinicius Costa Gomes | 5d873ca | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 761 | struct link_key *key; |
| 762 | |
| 763 | key = hci_find_link_key_type(hcon->hdev, conn->dst, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 764 | KEY_TYPE_LTK); |
Vinicius Costa Gomes | 5d873ca | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 765 | |
Brian Gix | 372257b | 2011-10-25 09:06:30 -0700 | [diff] [blame] | 766 | if (smp_encrypt_link(hcon, key) == 0) |
Vinicius Costa Gomes | 5d873ca | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 767 | goto done; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 770 | hcon->sec_req = FALSE; |
| 771 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 772 | if (hcon->link_mode & HCI_LM_MASTER) { |
| 773 | struct smp_cmd_pairing cp; |
Anderson Briglia | c8e856e | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 774 | |
Brian Gix | 7f7e16c | 2011-11-01 16:27:25 -0700 | [diff] [blame] | 775 | /* Switch to Pairing Connection Parameters */ |
| 776 | hci_le_conn_update(hcon, SMP_MIN_CONN_INTERVAL, |
| 777 | SMP_MAX_CONN_INTERVAL, SMP_MAX_CONN_LATENCY, |
| 778 | SMP_SUPERVISION_TIMEOUT); |
| 779 | |
Vinicius Costa Gomes | 10daf97 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 780 | build_pairing_cmd(conn, &cp, NULL, authreq); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 781 | hcon->preq[0] = SMP_CMD_PAIRING_REQ; |
| 782 | memcpy(&hcon->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | c8e856e | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 783 | |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 784 | mod_timer(&hcon->smp_timer, jiffies + |
Vinicius Costa Gomes | b19d5ce | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 785 | msecs_to_jiffies(SMP_TIMEOUT)); |
| 786 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 787 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 788 | hci_conn_hold(hcon); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 789 | } else { |
| 790 | struct smp_cmd_security_req cp; |
| 791 | cp.auth_req = authreq; |
| 792 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
| 793 | } |
| 794 | |
Vinicius Costa Gomes | 5d873ca | 2011-07-07 18:59:41 -0300 | [diff] [blame] | 795 | done: |
Vinicius Costa Gomes | a5474a8 | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 796 | set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
| 797 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 798 | return 0; |
| 799 | } |
| 800 | |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 801 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 802 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 803 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 804 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 805 | u8 rand[8]; |
| 806 | int err; |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 807 | |
| 808 | skb_pull(skb, sizeof(*rp)); |
| 809 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | BT_DBG("conn %p", conn); |
| 811 | |
| 812 | memset(rand, 0, sizeof(rand)); |
| 813 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 814 | err = hci_add_ltk(hcon->hdev, 0, conn->dst, hcon->dst_type, |
| 815 | 0, 0, 0, rand, rp->ltk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 816 | if (err) |
| 817 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 818 | |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
| 823 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 824 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 825 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 826 | struct smp_cmd_pairing *paircmd = (void *) &hcon->prsp[1]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 827 | struct link_key *key; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 828 | u8 *keydist; |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 829 | |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 830 | skb_pull(skb, sizeof(*rp)); |
| 831 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 832 | key = hci_find_link_key_type(hcon->hdev, conn->dst, KEY_TYPE_LTK); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 833 | if (key == NULL) |
| 834 | return SMP_UNSPECIFIED; |
| 835 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 836 | if (hcon->out) |
| 837 | keydist = &paircmd->resp_key_dist; |
| 838 | else |
| 839 | keydist = &paircmd->init_key_dist; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 840 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 841 | BT_DBG("keydist 0x%x", *keydist); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 842 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 843 | hci_add_ltk(hcon->hdev, 1, conn->dst, hcon->dst_type, |
| 844 | hcon->smp_key_size, hcon->auth, rp->ediv, |
| 845 | rp->rand, key->val); |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 846 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 847 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 848 | if (hcon->out) { |
| 849 | if (!(*keydist)) |
| 850 | smp_distribute_keys(conn, 1); |
| 851 | } |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 856 | int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) |
| 857 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 858 | struct hci_conn *hcon = conn->hcon; |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 859 | __u8 code = skb->data[0]; |
| 860 | __u8 reason; |
| 861 | int err = 0; |
| 862 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 863 | if (IS_ERR(hcon->hdev->tfm)) { |
| 864 | err = PTR_ERR(hcon->hdev->tfm); |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 865 | reason = SMP_PAIRING_NOTSUPP; |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 866 | BT_ERR("SMP_PAIRING_NOTSUPP %p", hcon->hdev->tfm); |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 867 | goto done; |
| 868 | } |
| 869 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 870 | hcon->smp_conn = conn; |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 871 | skb_pull(skb, sizeof(code)); |
| 872 | |
| 873 | switch (code) { |
| 874 | case SMP_CMD_PAIRING_REQ: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 875 | reason = smp_cmd_pairing_req(conn, skb); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 876 | break; |
| 877 | |
| 878 | case SMP_CMD_PAIRING_FAIL: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 879 | reason = 0; |
| 880 | err = -EPERM; |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 881 | del_timer(&hcon->smp_timer); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 882 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Brian Gix | 570efc9 | 2011-10-13 15:59:51 -0700 | [diff] [blame] | 883 | mgmt_auth_failed(hcon->hdev->id, conn->dst, skb->data[0]); |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 884 | hci_conn_put(hcon); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 885 | break; |
| 886 | |
| 887 | case SMP_CMD_PAIRING_RSP: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 888 | reason = smp_cmd_pairing_rsp(conn, skb); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 889 | break; |
| 890 | |
| 891 | case SMP_CMD_SECURITY_REQ: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 892 | reason = smp_cmd_security_req(conn, skb); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 893 | break; |
| 894 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 895 | case SMP_CMD_PAIRING_CONFIRM: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 896 | reason = smp_cmd_pairing_confirm(conn, skb); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 897 | break; |
| 898 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 899 | case SMP_CMD_PAIRING_RANDOM: |
Vinicius Costa Gomes | 64532ec | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 900 | reason = smp_cmd_pairing_random(conn, skb); |
Anderson Briglia | 13b4839 | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 901 | break; |
| 902 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 903 | case SMP_CMD_ENCRYPT_INFO: |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 904 | reason = smp_cmd_encrypt_info(conn, skb); |
| 905 | break; |
| 906 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 907 | case SMP_CMD_MASTER_IDENT: |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 908 | reason = smp_cmd_master_ident(conn, skb); |
| 909 | break; |
| 910 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 911 | case SMP_CMD_IDENT_INFO: |
| 912 | case SMP_CMD_IDENT_ADDR_INFO: |
| 913 | case SMP_CMD_SIGN_INFO: |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 914 | /* Just ignored */ |
| 915 | reason = 0; |
| 916 | break; |
| 917 | |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 918 | default: |
| 919 | BT_DBG("Unknown command code 0x%2.2x", code); |
| 920 | |
| 921 | reason = SMP_CMD_NOTSUPP; |
Vinicius Costa Gomes | 09fabbc | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 922 | err = -EOPNOTSUPP; |
| 923 | goto done; |
| 924 | } |
| 925 | |
| 926 | done: |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 927 | if (reason) { |
| 928 | BT_ERR("SMP_CMD_PAIRING_FAIL: %d", reason); |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 929 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
| 930 | &reason); |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 931 | del_timer(&hcon->smp_timer); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 932 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Brian Gix | 58ba077 | 2011-09-23 13:40:16 -0700 | [diff] [blame] | 933 | mgmt_auth_failed(hcon->hdev->id, conn->dst, reason); |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 934 | hci_conn_put(hcon); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 935 | } |
Anderson Briglia | 133e14c | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 936 | |
| 937 | kfree_skb(skb); |
| 938 | return err; |
| 939 | } |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 940 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 941 | static int smp_distribute_keys(struct l2cap_conn *conn, __u8 force) |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 942 | { |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 943 | struct hci_conn *hcon = conn->hcon; |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 944 | struct smp_cmd_pairing *req, *rsp; |
| 945 | __u8 *keydist; |
| 946 | |
| 947 | BT_DBG("conn %p force %d", conn, force); |
| 948 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 949 | if (IS_ERR(hcon->hdev->tfm)) |
| 950 | return PTR_ERR(hcon->hdev->tfm); |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 951 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 952 | rsp = (void *) &hcon->prsp[1]; |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 953 | |
| 954 | /* The responder sends its keys first */ |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 955 | if (!force && hcon->out && (rsp->resp_key_dist & 0x07)) |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 956 | return 0; |
| 957 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 958 | req = (void *) &hcon->preq[1]; |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 959 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 960 | if (hcon->out) { |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 961 | keydist = &rsp->init_key_dist; |
| 962 | *keydist &= req->init_key_dist; |
| 963 | } else { |
| 964 | keydist = &rsp->resp_key_dist; |
| 965 | *keydist &= req->resp_key_dist; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | BT_DBG("keydist 0x%x", *keydist); |
| 970 | |
| 971 | if (*keydist & SMP_DIST_ENC_KEY) { |
| 972 | struct smp_cmd_encrypt_info enc; |
| 973 | struct smp_cmd_master_ident ident; |
| 974 | __le16 ediv; |
| 975 | |
| 976 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); |
| 977 | get_random_bytes(&ediv, sizeof(ediv)); |
| 978 | get_random_bytes(ident.rand, sizeof(ident.rand)); |
| 979 | |
| 980 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); |
| 981 | |
Brian Gix | cf95677 | 2011-10-20 15:18:51 -0700 | [diff] [blame] | 982 | hci_add_ltk(hcon->hdev, 1, conn->dst, hcon->dst_type, |
| 983 | hcon->smp_key_size, hcon->auth, ediv, |
| 984 | ident.rand, enc.ltk); |
Vinicius Costa Gomes | e56cede | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 985 | |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 986 | ident.ediv = cpu_to_le16(ediv); |
| 987 | |
| 988 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); |
| 989 | |
| 990 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 991 | } |
| 992 | |
| 993 | if (*keydist & SMP_DIST_ID_KEY) { |
| 994 | struct smp_cmd_ident_addr_info addrinfo; |
| 995 | struct smp_cmd_ident_info idinfo; |
| 996 | |
| 997 | /* Send a dummy key */ |
| 998 | get_random_bytes(idinfo.irk, sizeof(idinfo.irk)); |
| 999 | |
| 1000 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); |
| 1001 | |
| 1002 | /* Just public address */ |
| 1003 | memset(&addrinfo, 0, sizeof(addrinfo)); |
| 1004 | bacpy(&addrinfo.bdaddr, conn->src); |
| 1005 | |
| 1006 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), |
| 1007 | &addrinfo); |
| 1008 | |
| 1009 | *keydist &= ~SMP_DIST_ID_KEY; |
| 1010 | } |
| 1011 | |
| 1012 | if (*keydist & SMP_DIST_SIGN) { |
| 1013 | struct smp_cmd_sign_info sign; |
| 1014 | |
| 1015 | /* Send a dummy key */ |
| 1016 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
| 1017 | |
| 1018 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
| 1019 | |
| 1020 | *keydist &= ~SMP_DIST_SIGN; |
| 1021 | } |
| 1022 | |
Brian Gix | 2a33526 | 2011-11-17 12:59:23 -0800 | [diff] [blame] | 1023 | if (hcon->out) { |
Brian Gix | e57c167 | 2011-09-13 12:34:59 -0700 | [diff] [blame] | 1024 | if (hcon->disconn_cfm_cb) |
| 1025 | hcon->disconn_cfm_cb(hcon, 0); |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 1026 | del_timer(&hcon->smp_timer); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 1027 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Brian Gix | 2a33526 | 2011-11-17 12:59:23 -0800 | [diff] [blame] | 1028 | hci_conn_put(hcon); |
| 1029 | } else if (rsp->resp_key_dist) { |
| 1030 | if (hcon->disconn_cfm_cb) |
| 1031 | hcon->disconn_cfm_cb(hcon, SMP_UNSPECIFIED); |
| 1032 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
Brian Gix | 58ba077 | 2011-09-23 13:40:16 -0700 | [diff] [blame] | 1033 | mgmt_auth_failed(hcon->hdev->id, conn->dst, SMP_UNSPECIFIED); |
Brian Gix | 6d5fb8a | 2011-09-09 14:53:04 -0700 | [diff] [blame] | 1034 | hci_conn_put(hcon); |
| 1035 | } |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | int smp_link_encrypt_cmplt(struct l2cap_conn *conn, u8 status, u8 encrypt) |
| 1041 | { |
| 1042 | struct hci_conn *hcon = conn->hcon; |
| 1043 | |
| 1044 | BT_DBG("smp: %d %d %d", status, encrypt, hcon->sec_req); |
| 1045 | |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 1046 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend); |
| 1047 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 1048 | if (!status && encrypt && hcon->sec_level < hcon->pending_sec_level) |
| 1049 | hcon->sec_level = hcon->pending_sec_level; |
| 1050 | |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1051 | if (!status && encrypt && !hcon->sec_req) |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 1052 | return smp_distribute_keys(conn, 0); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* Fall back to Pairing request if failed a Link Security request */ |
| 1055 | else if (hcon->sec_req && (status || !encrypt)) |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 1056 | smp_conn_security(conn, hcon->pending_sec_level); |
Brian Gix | a68668b | 2011-08-11 15:49:36 -0700 | [diff] [blame] | 1057 | |
Brian Gix | 80fb3a9 | 2012-01-31 13:15:20 -0800 | [diff] [blame] | 1058 | hci_conn_put(hcon); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 1059 | |
Vinicius Costa Gomes | c1d5e1d | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 1060 | return 0; |
| 1061 | } |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 1062 | |
| 1063 | void smp_timeout(unsigned long arg) |
| 1064 | { |
| 1065 | struct l2cap_conn *conn = (void *) arg; |
| 1066 | u8 reason = SMP_UNSPECIFIED; |
| 1067 | |
| 1068 | BT_DBG("%p", conn); |
| 1069 | |
| 1070 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), &reason); |
Brian Gix | 8d0b7d6 | 2011-10-12 15:12:42 -0700 | [diff] [blame] | 1071 | clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->hcon->pend); |
Brian Gix | 58ba077 | 2011-09-23 13:40:16 -0700 | [diff] [blame] | 1072 | mgmt_auth_failed(conn->hcon->hdev->id, conn->dst, SMP_UNSPECIFIED); |
Brian Gix | e9ceb52 | 2011-09-22 10:46:35 -0700 | [diff] [blame] | 1073 | hci_conn_put(conn->hcon); |
| 1074 | } |