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