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