Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License version 2 as |
| 7 | published by the Free Software Foundation; |
| 8 | |
| 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 10 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 12 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 13 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 14 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | |
| 18 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 19 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 20 | SOFTWARE IS DISCLAIMED. |
| 21 | */ |
| 22 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 23 | #include <linux/crypto.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <crypto/b128ops.h> |
| 26 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 27 | #include <net/bluetooth/bluetooth.h> |
| 28 | #include <net/bluetooth/hci_core.h> |
| 29 | #include <net/bluetooth/l2cap.h> |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 30 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 31 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 32 | #include "ecc.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 33 | #include "smp.h" |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 34 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 35 | /* Low-level debug macros to be used for stuff that we don't want |
| 36 | * accidentially in dmesg, i.e. the values of the various crypto keys |
| 37 | * and the inputs & outputs of crypto functions. |
| 38 | */ |
| 39 | #ifdef DEBUG |
| 40 | #define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \ |
| 41 | ##__VA_ARGS__) |
| 42 | #else |
| 43 | #define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \ |
| 44 | ##__VA_ARGS__) |
| 45 | #endif |
| 46 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 47 | #define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 48 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 49 | /* Keys which are not distributed with Secure Connections */ |
| 50 | #define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY); |
| 51 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 52 | #define SMP_TIMEOUT msecs_to_jiffies(30000) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 53 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 54 | #define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \ |
| 55 | 0x1f : 0x07) |
| 56 | #define KEY_DIST_MASK 0x07 |
Johan Hedberg | 065a13e | 2012-10-11 16:26:06 +0200 | [diff] [blame] | 57 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 58 | /* Maximum message length that can be passed to aes_cmac */ |
| 59 | #define CMAC_MSG_MAX 80 |
| 60 | |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 61 | enum { |
| 62 | SMP_FLAG_TK_VALID, |
| 63 | SMP_FLAG_CFM_PENDING, |
| 64 | SMP_FLAG_MITM_AUTH, |
| 65 | SMP_FLAG_COMPLETE, |
| 66 | SMP_FLAG_INITIATOR, |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 67 | SMP_FLAG_SC, |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 68 | SMP_FLAG_REMOTE_PK, |
Johan Hedberg | aeb7d46 | 2014-05-31 18:52:28 +0300 | [diff] [blame] | 69 | SMP_FLAG_DEBUG_KEY, |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 70 | SMP_FLAG_WAIT_USER, |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 71 | SMP_FLAG_DHKEY_PENDING, |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 72 | SMP_FLAG_OOB, |
Johan Hedberg | 533e35d | 2014-06-16 19:25:18 +0300 | [diff] [blame] | 73 | }; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 74 | |
| 75 | struct smp_chan { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 76 | struct l2cap_conn *conn; |
| 77 | struct delayed_work security_timer; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 78 | unsigned long allow_cmd; /* Bitmask of allowed commands */ |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 79 | |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 80 | u8 preq[7]; /* SMP Pairing Request */ |
| 81 | u8 prsp[7]; /* SMP Pairing Response */ |
| 82 | u8 prnd[16]; /* SMP Pairing Random (local) */ |
| 83 | u8 rrnd[16]; /* SMP Pairing Random (remote) */ |
| 84 | u8 pcnf[16]; /* SMP Pairing Confirm */ |
| 85 | u8 tk[16]; /* SMP Temporary Key */ |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 86 | u8 rr[16]; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 87 | u8 enc_key_size; |
| 88 | u8 remote_key_dist; |
| 89 | bdaddr_t id_addr; |
| 90 | u8 id_addr_type; |
| 91 | u8 irk[16]; |
| 92 | struct smp_csrk *csrk; |
| 93 | struct smp_csrk *slave_csrk; |
| 94 | struct smp_ltk *ltk; |
| 95 | struct smp_ltk *slave_ltk; |
| 96 | struct smp_irk *remote_irk; |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 97 | u8 *link_key; |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 98 | unsigned long flags; |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 99 | u8 method; |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 100 | u8 passkey_round; |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 101 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 102 | /* Secure Connections variables */ |
| 103 | u8 local_pk[64]; |
| 104 | u8 local_sk[32]; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 105 | u8 remote_pk[64]; |
| 106 | u8 dhkey[32]; |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 107 | u8 mackey[16]; |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 108 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 109 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 110 | struct crypto_hash *tfm_cmac; |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 111 | }; |
| 112 | |
Johan Hedberg | aeb7d46 | 2014-05-31 18:52:28 +0300 | [diff] [blame] | 113 | /* These debug key values are defined in the SMP section of the core |
| 114 | * specification. debug_pk is the public debug key and debug_sk the |
| 115 | * private debug key. |
| 116 | */ |
| 117 | static const u8 debug_pk[64] = { |
| 118 | 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, |
| 119 | 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, |
| 120 | 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, |
| 121 | 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20, |
| 122 | |
| 123 | 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74, |
| 124 | 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76, |
| 125 | 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63, |
| 126 | 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc, |
| 127 | }; |
| 128 | |
| 129 | static const u8 debug_sk[32] = { |
| 130 | 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58, |
| 131 | 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a, |
| 132 | 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74, |
| 133 | 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f, |
| 134 | }; |
| 135 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 136 | static inline void swap_buf(const u8 *src, u8 *dst, size_t len) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 137 | { |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 138 | size_t i; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 139 | |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 140 | for (i = 0; i < len; i++) |
| 141 | dst[len - 1 - i] = src[i]; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 142 | } |
| 143 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 144 | static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m, |
| 145 | size_t len, u8 mac[16]) |
| 146 | { |
| 147 | uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX]; |
| 148 | struct hash_desc desc; |
| 149 | struct scatterlist sg; |
| 150 | int err; |
| 151 | |
| 152 | if (len > CMAC_MSG_MAX) |
| 153 | return -EFBIG; |
| 154 | |
| 155 | if (!tfm) { |
| 156 | BT_ERR("tfm %p", tfm); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | desc.tfm = tfm; |
| 161 | desc.flags = 0; |
| 162 | |
| 163 | crypto_hash_init(&desc); |
| 164 | |
| 165 | /* Swap key and message from LSB to MSB */ |
| 166 | swap_buf(k, tmp, 16); |
| 167 | swap_buf(m, msg_msb, len); |
| 168 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 169 | SMP_DBG("msg (len %zu) %*phN", len, (int) len, m); |
| 170 | SMP_DBG("key %16phN", k); |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 171 | |
| 172 | err = crypto_hash_setkey(tfm, tmp, 16); |
| 173 | if (err) { |
| 174 | BT_ERR("cipher setkey failed: %d", err); |
| 175 | return err; |
| 176 | } |
| 177 | |
| 178 | sg_init_one(&sg, msg_msb, len); |
| 179 | |
| 180 | err = crypto_hash_update(&desc, &sg, len); |
| 181 | if (err) { |
| 182 | BT_ERR("Hash update error %d", err); |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | err = crypto_hash_final(&desc, mac_msb); |
| 187 | if (err) { |
| 188 | BT_ERR("Hash final error %d", err); |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | swap_buf(mac_msb, mac, 16); |
| 193 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 194 | SMP_DBG("mac %16phN", mac); |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], |
| 200 | const u8 x[16], u8 z, u8 res[16]) |
| 201 | { |
| 202 | u8 m[65]; |
| 203 | int err; |
| 204 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 205 | SMP_DBG("u %32phN", u); |
| 206 | SMP_DBG("v %32phN", v); |
| 207 | SMP_DBG("x %16phN z %02x", x, z); |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 208 | |
| 209 | m[0] = z; |
| 210 | memcpy(m + 1, v, 32); |
| 211 | memcpy(m + 33, u, 32); |
| 212 | |
| 213 | err = aes_cmac(tfm_cmac, x, m, sizeof(m), res); |
| 214 | if (err) |
| 215 | return err; |
| 216 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 217 | SMP_DBG("res %16phN", res); |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 218 | |
| 219 | return err; |
| 220 | } |
| 221 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 222 | static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16], |
| 223 | u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16]) |
| 224 | { |
| 225 | /* The btle, salt and length "magic" values are as defined in |
| 226 | * the SMP section of the Bluetooth core specification. In ASCII |
| 227 | * the btle value ends up being 'btle'. The salt is just a |
| 228 | * random number whereas length is the value 256 in little |
| 229 | * endian format. |
| 230 | */ |
| 231 | const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 }; |
| 232 | const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60, |
| 233 | 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c }; |
| 234 | const u8 length[2] = { 0x00, 0x01 }; |
| 235 | u8 m[53], t[16]; |
| 236 | int err; |
| 237 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 238 | SMP_DBG("w %32phN", w); |
| 239 | SMP_DBG("n1 %16phN n2 %16phN", n1, n2); |
| 240 | SMP_DBG("a1 %7phN a2 %7phN", a1, a2); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 241 | |
| 242 | err = aes_cmac(tfm_cmac, salt, w, 32, t); |
| 243 | if (err) |
| 244 | return err; |
| 245 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 246 | SMP_DBG("t %16phN", t); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 247 | |
| 248 | memcpy(m, length, 2); |
| 249 | memcpy(m + 2, a2, 7); |
| 250 | memcpy(m + 9, a1, 7); |
| 251 | memcpy(m + 16, n2, 16); |
| 252 | memcpy(m + 32, n1, 16); |
| 253 | memcpy(m + 48, btle, 4); |
| 254 | |
| 255 | m[52] = 0; /* Counter */ |
| 256 | |
| 257 | err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey); |
| 258 | if (err) |
| 259 | return err; |
| 260 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 261 | SMP_DBG("mackey %16phN", mackey); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 262 | |
| 263 | m[52] = 1; /* Counter */ |
| 264 | |
| 265 | err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk); |
| 266 | if (err) |
| 267 | return err; |
| 268 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 269 | SMP_DBG("ltk %16phN", ltk); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16], |
| 275 | const u8 n1[16], u8 n2[16], const u8 r[16], |
| 276 | const u8 io_cap[3], const u8 a1[7], const u8 a2[7], |
| 277 | u8 res[16]) |
| 278 | { |
| 279 | u8 m[65]; |
| 280 | int err; |
| 281 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 282 | SMP_DBG("w %16phN", w); |
| 283 | SMP_DBG("n1 %16phN n2 %16phN", n1, n2); |
| 284 | SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 285 | |
| 286 | memcpy(m, a2, 7); |
| 287 | memcpy(m + 7, a1, 7); |
| 288 | memcpy(m + 14, io_cap, 3); |
| 289 | memcpy(m + 17, r, 16); |
| 290 | memcpy(m + 33, n2, 16); |
| 291 | memcpy(m + 49, n1, 16); |
| 292 | |
| 293 | err = aes_cmac(tfm_cmac, w, m, sizeof(m), res); |
| 294 | if (err) |
| 295 | return err; |
| 296 | |
| 297 | BT_DBG("res %16phN", res); |
| 298 | |
| 299 | return err; |
| 300 | } |
| 301 | |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 302 | static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32], |
| 303 | const u8 x[16], const u8 y[16], u32 *val) |
| 304 | { |
| 305 | u8 m[80], tmp[16]; |
| 306 | int err; |
| 307 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 308 | SMP_DBG("u %32phN", u); |
| 309 | SMP_DBG("v %32phN", v); |
| 310 | SMP_DBG("x %16phN y %16phN", x, y); |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 311 | |
| 312 | memcpy(m, y, 16); |
| 313 | memcpy(m + 16, v, 32); |
| 314 | memcpy(m + 48, u, 32); |
| 315 | |
| 316 | err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp); |
| 317 | if (err) |
| 318 | return err; |
| 319 | |
| 320 | *val = get_unaligned_le32(tmp); |
| 321 | *val %= 1000000; |
| 322 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 323 | SMP_DBG("val %06u", *val); |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 328 | static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) |
| 329 | { |
| 330 | struct blkcipher_desc desc; |
| 331 | struct scatterlist sg; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 332 | uint8_t tmp[16], data[16]; |
Johan Hedberg | 201a592 | 2013-12-02 10:49:04 +0200 | [diff] [blame] | 333 | int err; |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 334 | |
| 335 | if (tfm == NULL) { |
| 336 | BT_ERR("tfm %p", tfm); |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
| 340 | desc.tfm = tfm; |
| 341 | desc.flags = 0; |
| 342 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 343 | /* The most significant octet of key corresponds to k[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 344 | swap_buf(k, tmp, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 345 | |
| 346 | err = crypto_blkcipher_setkey(tfm, tmp, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 347 | if (err) { |
| 348 | BT_ERR("cipher setkey failed: %d", err); |
| 349 | return err; |
| 350 | } |
| 351 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 352 | /* Most significant octet of plaintextData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 353 | swap_buf(r, data, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 354 | |
| 355 | sg_init_one(&sg, data, 16); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 356 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 357 | err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16); |
| 358 | if (err) |
| 359 | BT_ERR("Encrypt data error %d", err); |
| 360 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 361 | /* Most significant octet of encryptedData corresponds to data[0] */ |
Johan Hedberg | 8a2936f | 2014-06-16 19:25:19 +0300 | [diff] [blame] | 362 | swap_buf(data, r, 16); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 363 | |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 364 | return err; |
| 365 | } |
| 366 | |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 367 | static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16], |
| 368 | const u8 key_id[4], u8 res[16]) |
| 369 | { |
| 370 | int err; |
| 371 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 372 | SMP_DBG("w %16phN key_id %4phN", w, key_id); |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 373 | |
| 374 | err = aes_cmac(tfm_cmac, w, key_id, 4, res); |
| 375 | if (err) |
| 376 | return err; |
| 377 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 378 | SMP_DBG("res %16phN", res); |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 379 | |
| 380 | return err; |
| 381 | } |
| 382 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 383 | static int smp_ah(struct crypto_blkcipher *tfm, u8 irk[16], u8 r[3], u8 res[3]) |
| 384 | { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 385 | u8 _res[16]; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 386 | int err; |
| 387 | |
| 388 | /* r' = padding || r */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 389 | memcpy(_res, r, 3); |
| 390 | memset(_res + 3, 0, 13); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 391 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 392 | err = smp_e(tfm, irk, _res); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 393 | if (err) { |
| 394 | BT_ERR("Encrypt error"); |
| 395 | return err; |
| 396 | } |
| 397 | |
| 398 | /* The output of the random address function ah is: |
| 399 | * ah(h, r) = e(k, r') mod 2^24 |
| 400 | * The output of the security function e is then truncated to 24 bits |
| 401 | * by taking the least significant 24 bits of the output of e as the |
| 402 | * result of ah. |
| 403 | */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 404 | memcpy(res, _res, 3); |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 409 | bool smp_irk_matches(struct hci_dev *hdev, u8 irk[16], bdaddr_t *bdaddr) |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 410 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 411 | struct l2cap_chan *chan = hdev->smp_data; |
| 412 | struct crypto_blkcipher *tfm; |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 413 | u8 hash[3]; |
| 414 | int err; |
| 415 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 416 | if (!chan || !chan->data) |
| 417 | return false; |
| 418 | |
| 419 | tfm = chan->data; |
| 420 | |
Johan Hedberg | 6047805 | 2014-02-18 10:19:31 +0200 | [diff] [blame] | 421 | BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); |
| 422 | |
| 423 | err = smp_ah(tfm, irk, &bdaddr->b[3], hash); |
| 424 | if (err) |
| 425 | return false; |
| 426 | |
| 427 | return !memcmp(bdaddr->b, hash, 3); |
| 428 | } |
| 429 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 430 | int smp_generate_rpa(struct hci_dev *hdev, u8 irk[16], bdaddr_t *rpa) |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 431 | { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 432 | struct l2cap_chan *chan = hdev->smp_data; |
| 433 | struct crypto_blkcipher *tfm; |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 434 | int err; |
| 435 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 436 | if (!chan || !chan->data) |
| 437 | return -EOPNOTSUPP; |
| 438 | |
| 439 | tfm = chan->data; |
| 440 | |
Johan Hedberg | b1e2b3a | 2014-02-23 19:42:19 +0200 | [diff] [blame] | 441 | get_random_bytes(&rpa->b[3], 3); |
| 442 | |
| 443 | rpa->b[5] &= 0x3f; /* Clear two most significant bits */ |
| 444 | rpa->b[5] |= 0x40; /* Set second most significant bit */ |
| 445 | |
| 446 | err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); |
| 447 | if (err < 0) |
| 448 | return err; |
| 449 | |
| 450 | BT_DBG("RPA %pMR", rpa); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 455 | static int smp_c1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r[16], |
| 456 | u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, u8 _rat, |
| 457 | bdaddr_t *ra, u8 res[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 458 | { |
| 459 | u8 p1[16], p2[16]; |
| 460 | int err; |
| 461 | |
| 462 | memset(p1, 0, 16); |
| 463 | |
| 464 | /* p1 = pres || preq || _rat || _iat */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 465 | p1[0] = _iat; |
| 466 | p1[1] = _rat; |
| 467 | memcpy(p1 + 2, preq, 7); |
| 468 | memcpy(p1 + 9, pres, 7); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 469 | |
| 470 | /* p2 = padding || ia || ra */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 471 | memcpy(p2, ra, 6); |
| 472 | memcpy(p2 + 6, ia, 6); |
| 473 | memset(p2 + 12, 0, 4); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 474 | |
| 475 | /* res = r XOR p1 */ |
| 476 | u128_xor((u128 *) res, (u128 *) r, (u128 *) p1); |
| 477 | |
| 478 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 479 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 480 | if (err) { |
| 481 | BT_ERR("Encrypt data error"); |
| 482 | return err; |
| 483 | } |
| 484 | |
| 485 | /* res = res XOR p2 */ |
| 486 | u128_xor((u128 *) res, (u128 *) res, (u128 *) p2); |
| 487 | |
| 488 | /* res = e(k, res) */ |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 489 | err = smp_e(tfm_aes, k, res); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 490 | if (err) |
| 491 | BT_ERR("Encrypt data error"); |
| 492 | |
| 493 | return err; |
| 494 | } |
| 495 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 496 | static int smp_s1(struct crypto_blkcipher *tfm_aes, u8 k[16], u8 r1[16], |
| 497 | u8 r2[16], u8 _r[16]) |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 498 | { |
| 499 | int err; |
| 500 | |
| 501 | /* Just least significant octets from r1 and r2 are considered */ |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 502 | memcpy(_r, r2, 8); |
| 503 | memcpy(_r + 8, r1, 8); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 504 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 505 | err = smp_e(tfm_aes, k, _r); |
Anderson Briglia | d22ef0b | 2011-06-09 18:50:44 -0300 | [diff] [blame] | 506 | if (err) |
| 507 | BT_ERR("Encrypt data error"); |
| 508 | |
| 509 | return err; |
| 510 | } |
| 511 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 512 | static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) |
| 513 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 514 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 515 | struct smp_chan *smp; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 516 | struct kvec iv[2]; |
| 517 | struct msghdr msg; |
| 518 | |
| 519 | if (!chan) |
| 520 | return; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 521 | |
| 522 | BT_DBG("code 0x%2.2x", code); |
| 523 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 524 | iv[0].iov_base = &code; |
| 525 | iv[0].iov_len = 1; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 526 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 527 | iv[1].iov_base = data; |
| 528 | iv[1].iov_len = len; |
| 529 | |
| 530 | memset(&msg, 0, sizeof(msg)); |
| 531 | |
| 532 | msg.msg_iov = (struct iovec *) &iv; |
| 533 | msg.msg_iovlen = 2; |
| 534 | |
| 535 | l2cap_chan_send(chan, &msg, 1 + len); |
Vinicius Costa Gomes | e2dcd11 | 2011-08-19 21:06:50 -0300 | [diff] [blame] | 536 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 537 | if (!chan->data) |
| 538 | return; |
| 539 | |
| 540 | smp = chan->data; |
| 541 | |
| 542 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 1b0921d | 2014-09-05 22:19:48 +0300 | [diff] [blame] | 543 | schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 544 | } |
| 545 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 546 | static u8 authreq_to_seclevel(u8 authreq) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 547 | { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 548 | if (authreq & SMP_AUTH_MITM) { |
| 549 | if (authreq & SMP_AUTH_SC) |
| 550 | return BT_SECURITY_FIPS; |
| 551 | else |
| 552 | return BT_SECURITY_HIGH; |
| 553 | } else { |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 554 | return BT_SECURITY_MEDIUM; |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 555 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static __u8 seclevel_to_authreq(__u8 sec_level) |
| 559 | { |
| 560 | switch (sec_level) { |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 561 | case BT_SECURITY_FIPS: |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 562 | case BT_SECURITY_HIGH: |
| 563 | return SMP_AUTH_MITM | SMP_AUTH_BONDING; |
| 564 | case BT_SECURITY_MEDIUM: |
| 565 | return SMP_AUTH_BONDING; |
| 566 | default: |
| 567 | return SMP_AUTH_NONE; |
| 568 | } |
| 569 | } |
| 570 | |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 571 | static void build_pairing_cmd(struct l2cap_conn *conn, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 572 | struct smp_cmd_pairing *req, |
| 573 | struct smp_cmd_pairing *rsp, __u8 authreq) |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 574 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 575 | struct l2cap_chan *chan = conn->smp; |
| 576 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 577 | struct hci_conn *hcon = conn->hcon; |
| 578 | struct hci_dev *hdev = hcon->hdev; |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 579 | u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 580 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 581 | if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 582 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
| 583 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 584 | authreq |= SMP_AUTH_BONDING; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 585 | } else { |
| 586 | authreq &= ~SMP_AUTH_BONDING; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 587 | } |
| 588 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 589 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
| 590 | remote_dist |= SMP_DIST_ID_KEY; |
| 591 | |
Johan Hedberg | 863efaf | 2014-02-22 19:06:32 +0200 | [diff] [blame] | 592 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 593 | local_dist |= SMP_DIST_ID_KEY; |
| 594 | |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 595 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 596 | (authreq & SMP_AUTH_SC)) { |
| 597 | struct oob_data *oob_data; |
| 598 | u8 bdaddr_type; |
| 599 | |
| 600 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | df8e1a4 | 2014-06-06 10:39:56 +0300 | [diff] [blame] | 601 | local_dist |= SMP_DIST_LINK_KEY; |
| 602 | remote_dist |= SMP_DIST_LINK_KEY; |
| 603 | } |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 604 | |
| 605 | if (hcon->dst_type == ADDR_LE_DEV_PUBLIC) |
| 606 | bdaddr_type = BDADDR_LE_PUBLIC; |
| 607 | else |
| 608 | bdaddr_type = BDADDR_LE_RANDOM; |
| 609 | |
| 610 | oob_data = hci_find_remote_oob_data(hdev, &hcon->dst, |
| 611 | bdaddr_type); |
| 612 | if (oob_data) { |
| 613 | set_bit(SMP_FLAG_OOB, &smp->flags); |
| 614 | oob_flag = SMP_OOB_PRESENT; |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 615 | memcpy(smp->rr, oob_data->rand256, 16); |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 616 | memcpy(smp->pcnf, oob_data->hash256, 16); |
| 617 | } |
| 618 | |
Johan Hedberg | df8e1a4 | 2014-06-06 10:39:56 +0300 | [diff] [blame] | 619 | } else { |
| 620 | authreq &= ~SMP_AUTH_SC; |
| 621 | } |
| 622 | |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 623 | if (rsp == NULL) { |
| 624 | req->io_capability = conn->hcon->io_capability; |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 625 | req->oob_flag = oob_flag; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 626 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 627 | req->init_key_dist = local_dist; |
| 628 | req->resp_key_dist = remote_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 629 | req->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 630 | |
| 631 | smp->remote_key_dist = remote_dist; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 632 | return; |
| 633 | } |
| 634 | |
| 635 | rsp->io_capability = conn->hcon->io_capability; |
Johan Hedberg | 02b05bd | 2014-10-26 21:19:10 +0100 | [diff] [blame] | 636 | rsp->oob_flag = oob_flag; |
Vinicius Costa Gomes | 54790f7 | 2011-07-07 18:59:38 -0300 | [diff] [blame] | 637 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 638 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
| 639 | rsp->resp_key_dist = req->resp_key_dist & local_dist; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 640 | rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev)); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 641 | |
| 642 | smp->remote_key_dist = rsp->init_key_dist; |
Vinicius Costa Gomes | b8e66ea | 2011-06-09 18:50:52 -0300 | [diff] [blame] | 643 | } |
| 644 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 645 | static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size) |
| 646 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 647 | struct l2cap_chan *chan = conn->smp; |
| 648 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 649 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 650 | if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 651 | (max_key_size < SMP_MIN_ENC_KEY_SIZE)) |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 652 | return SMP_ENC_KEY_SIZE; |
| 653 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 654 | smp->enc_key_size = max_key_size; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 659 | static void smp_chan_destroy(struct l2cap_conn *conn) |
| 660 | { |
| 661 | struct l2cap_chan *chan = conn->smp; |
| 662 | struct smp_chan *smp = chan->data; |
| 663 | bool complete; |
| 664 | |
| 665 | BUG_ON(!smp); |
| 666 | |
| 667 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 668 | |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 669 | complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 670 | mgmt_smp_complete(conn->hcon, complete); |
| 671 | |
| 672 | kfree(smp->csrk); |
| 673 | kfree(smp->slave_csrk); |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 674 | kfree(smp->link_key); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 675 | |
| 676 | crypto_free_blkcipher(smp->tfm_aes); |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 677 | crypto_free_hash(smp->tfm_cmac); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 678 | |
| 679 | /* If pairing failed clean up any keys we might have */ |
| 680 | if (!complete) { |
| 681 | if (smp->ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 682 | list_del_rcu(&smp->ltk->list); |
| 683 | kfree_rcu(smp->ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | if (smp->slave_ltk) { |
Johan Hedberg | 970d0f1 | 2014-11-13 14:37:47 +0200 | [diff] [blame] | 687 | list_del_rcu(&smp->slave_ltk->list); |
| 688 | kfree_rcu(smp->slave_ltk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | if (smp->remote_irk) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 692 | list_del_rcu(&smp->remote_irk->list); |
| 693 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 6f48e26 | 2014-08-11 22:06:44 +0300 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
| 697 | chan->data = NULL; |
| 698 | kfree(smp); |
| 699 | hci_conn_drop(conn->hcon); |
| 700 | } |
| 701 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 702 | static void smp_failure(struct l2cap_conn *conn, u8 reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 703 | { |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 704 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 705 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 706 | |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 707 | if (reason) |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 708 | smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 709 | &reason); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 710 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 711 | clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags); |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 712 | mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE); |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 713 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 714 | if (chan->data) |
Vinicius Costa Gomes | f1c09c0 | 2012-02-01 18:27:56 -0300 | [diff] [blame] | 715 | smp_chan_destroy(conn); |
Brian Gix | 4f957a7 | 2011-11-23 08:28:36 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 718 | #define JUST_WORKS 0x00 |
| 719 | #define JUST_CFM 0x01 |
| 720 | #define REQ_PASSKEY 0x02 |
| 721 | #define CFM_PASSKEY 0x03 |
| 722 | #define REQ_OOB 0x04 |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 723 | #define DSP_PASSKEY 0x05 |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 724 | #define OVERLAP 0xFF |
| 725 | |
| 726 | static const u8 gen_method[5][5] = { |
| 727 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 728 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 729 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 730 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, |
| 731 | { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP }, |
| 732 | }; |
| 733 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 734 | static const u8 sc_method[5][5] = { |
| 735 | { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY }, |
| 736 | { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 737 | { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY }, |
| 738 | { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM }, |
| 739 | { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY }, |
| 740 | }; |
| 741 | |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 742 | static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io) |
| 743 | { |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 744 | /* If either side has unknown io_caps, use JUST_CFM (which gets |
| 745 | * converted later to JUST_WORKS if we're initiators. |
| 746 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 747 | if (local_io > SMP_IO_KEYBOARD_DISPLAY || |
| 748 | remote_io > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 749 | return JUST_CFM; |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 750 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 751 | if (test_bit(SMP_FLAG_SC, &smp->flags)) |
| 752 | return sc_method[remote_io][local_io]; |
| 753 | |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 754 | return gen_method[remote_io][local_io]; |
| 755 | } |
| 756 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 757 | static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth, |
| 758 | u8 local_io, u8 remote_io) |
| 759 | { |
| 760 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 761 | struct l2cap_chan *chan = conn->smp; |
| 762 | struct smp_chan *smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 763 | u32 passkey = 0; |
| 764 | int ret = 0; |
| 765 | |
| 766 | /* Initialize key for JUST WORKS */ |
| 767 | memset(smp->tk, 0, sizeof(smp->tk)); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 768 | clear_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 769 | |
| 770 | BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io); |
| 771 | |
Johan Hedberg | 2bcd400 | 2014-07-09 19:18:09 +0300 | [diff] [blame] | 772 | /* If neither side wants MITM, either "just" confirm an incoming |
| 773 | * request or use just-works for outgoing ones. The JUST_CFM |
| 774 | * will be converted to JUST_WORKS if necessary later in this |
| 775 | * function. If either side has MITM look up the method from the |
| 776 | * table. |
| 777 | */ |
Johan Hedberg | 581370c | 2014-06-17 13:07:38 +0300 | [diff] [blame] | 778 | if (!(auth & SMP_AUTH_MITM)) |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 779 | smp->method = JUST_CFM; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 780 | else |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 781 | smp->method = get_auth_method(smp, local_io, remote_io); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 782 | |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 783 | /* Don't confirm locally initiated pairing attempts */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 784 | if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, |
| 785 | &smp->flags)) |
| 786 | smp->method = JUST_WORKS; |
Johan Hedberg | a82505c | 2014-03-24 14:39:07 +0200 | [diff] [blame] | 787 | |
Johan Hedberg | 02f3e25 | 2014-07-16 15:09:13 +0300 | [diff] [blame] | 788 | /* Don't bother user space with no IO capabilities */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 789 | if (smp->method == JUST_CFM && |
| 790 | hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
| 791 | smp->method = JUST_WORKS; |
Johan Hedberg | 02f3e25 | 2014-07-16 15:09:13 +0300 | [diff] [blame] | 792 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 793 | /* If Just Works, Continue with Zero TK */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 794 | if (smp->method == JUST_WORKS) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 795 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /* Not Just Works/Confirm results in MITM Authentication */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 800 | if (smp->method != JUST_CFM) { |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 801 | set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); |
Johan Hedberg | 5eb596f | 2014-09-18 11:26:32 +0300 | [diff] [blame] | 802 | if (hcon->pending_sec_level < BT_SECURITY_HIGH) |
| 803 | hcon->pending_sec_level = BT_SECURITY_HIGH; |
| 804 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 805 | |
| 806 | /* If both devices have Keyoard-Display I/O, the master |
| 807 | * Confirms and the slave Enters the passkey. |
| 808 | */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 809 | if (smp->method == OVERLAP) { |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 810 | if (hcon->role == HCI_ROLE_MASTER) |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 811 | smp->method = CFM_PASSKEY; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 812 | else |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 813 | smp->method = REQ_PASSKEY; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 816 | /* Generate random passkey. */ |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 817 | if (smp->method == CFM_PASSKEY) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 818 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 819 | get_random_bytes(&passkey, sizeof(passkey)); |
| 820 | passkey %= 1000000; |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 821 | put_unaligned_le32(passkey, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 822 | BT_DBG("PassKey: %d", passkey); |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 823 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 826 | if (smp->method == REQ_PASSKEY) |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 827 | ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 828 | hcon->type, hcon->dst_type); |
Johan Hedberg | 783e057 | 2014-05-31 18:48:26 +0300 | [diff] [blame] | 829 | else if (smp->method == JUST_CFM) |
Johan Hedberg | 4eb65e6 | 2014-03-24 14:39:05 +0200 | [diff] [blame] | 830 | ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, |
| 831 | hcon->type, hcon->dst_type, |
| 832 | passkey, 1); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 833 | else |
Johan Hedberg | 01ad34d | 2014-03-19 14:14:53 +0200 | [diff] [blame] | 834 | ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst, |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 835 | hcon->type, hcon->dst_type, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 836 | passkey, 0); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 837 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 841 | static u8 smp_confirm(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 842 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 843 | struct l2cap_conn *conn = smp->conn; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 844 | struct smp_cmd_pairing_confirm cp; |
| 845 | int ret; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 846 | |
| 847 | BT_DBG("conn %p", conn); |
| 848 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 849 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 850 | conn->hcon->init_addr_type, &conn->hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 851 | conn->hcon->resp_addr_type, &conn->hcon->resp_addr, |
| 852 | cp.confirm_val); |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 853 | if (ret) |
| 854 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 855 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 856 | clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 857 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 858 | smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp); |
| 859 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 860 | if (conn->hcon->out) |
| 861 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 862 | else |
| 863 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 864 | |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 865 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 866 | } |
| 867 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 868 | static u8 smp_random(struct smp_chan *smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 869 | { |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 870 | struct l2cap_conn *conn = smp->conn; |
| 871 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 872 | u8 confirm[16]; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 873 | int ret; |
| 874 | |
Johan Hedberg | ec70f36 | 2014-06-27 14:23:04 +0300 | [diff] [blame] | 875 | if (IS_ERR_OR_NULL(smp->tfm_aes)) |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 876 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 877 | |
| 878 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 879 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 880 | ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp, |
Johan Hedberg | b1cd5fd | 2014-02-28 12:54:17 +0200 | [diff] [blame] | 881 | hcon->init_addr_type, &hcon->init_addr, |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 882 | hcon->resp_addr_type, &hcon->resp_addr, confirm); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 883 | if (ret) |
| 884 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 885 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 886 | if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) { |
| 887 | BT_ERR("Pairing failed (confirmation values mismatch)"); |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 888 | return SMP_CONFIRM_FAILED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if (hcon->out) { |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 892 | u8 stk[16]; |
| 893 | __le64 rand = 0; |
| 894 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 895 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 896 | smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 897 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 898 | memset(stk + smp->enc_key_size, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 899 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 900 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 901 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
| 902 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 903 | |
| 904 | hci_le_start_enc(hcon, ediv, rand, stk); |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 905 | hcon->enc_key_size = smp->enc_key_size; |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 906 | set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 907 | } else { |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 908 | u8 stk[16], auth; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 909 | __le64 rand = 0; |
| 910 | __le16 ediv = 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 911 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 912 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 913 | smp->prnd); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 914 | |
Johan Hedberg | e491eaf | 2014-10-25 21:15:37 +0200 | [diff] [blame] | 915 | smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 916 | |
Vinicius Costa Gomes | f7aa611 | 2012-01-30 19:29:12 -0300 | [diff] [blame] | 917 | memset(stk + smp->enc_key_size, 0, |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 918 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 919 | |
Johan Hedberg | fff3490 | 2014-06-10 15:19:50 +0300 | [diff] [blame] | 920 | if (hcon->pending_sec_level == BT_SECURITY_HIGH) |
| 921 | auth = 1; |
| 922 | else |
| 923 | auth = 0; |
| 924 | |
Johan Hedberg | 7d5843b | 2014-06-16 19:25:15 +0300 | [diff] [blame] | 925 | /* Even though there's no _SLAVE suffix this is the |
| 926 | * slave STK we're adding for later lookup (the master |
| 927 | * STK never needs to be stored). |
| 928 | */ |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 929 | hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 930 | SMP_STK, auth, stk, smp->enc_key_size, ediv, rand); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 931 | } |
| 932 | |
Johan Hedberg | 861580a | 2014-05-20 09:45:51 +0300 | [diff] [blame] | 933 | return 0; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 934 | } |
| 935 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 936 | static void smp_notify_keys(struct l2cap_conn *conn) |
| 937 | { |
| 938 | struct l2cap_chan *chan = conn->smp; |
| 939 | struct smp_chan *smp = chan->data; |
| 940 | struct hci_conn *hcon = conn->hcon; |
| 941 | struct hci_dev *hdev = hcon->hdev; |
| 942 | struct smp_cmd_pairing *req = (void *) &smp->preq[1]; |
| 943 | struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1]; |
| 944 | bool persistent; |
| 945 | |
| 946 | if (smp->remote_irk) { |
| 947 | mgmt_new_irk(hdev, smp->remote_irk); |
| 948 | /* Now that user space can be considered to know the |
| 949 | * identity address track the connection based on it |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 950 | * from now on (assuming this is an LE link). |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 951 | */ |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 952 | if (hcon->type == LE_LINK) { |
| 953 | bacpy(&hcon->dst, &smp->remote_irk->bdaddr); |
| 954 | hcon->dst_type = smp->remote_irk->addr_type; |
| 955 | queue_work(hdev->workqueue, &conn->id_addr_update_work); |
| 956 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 957 | |
| 958 | /* When receiving an indentity resolving key for |
| 959 | * a remote device that does not use a resolvable |
| 960 | * private address, just remove the key so that |
| 961 | * it is possible to use the controller white |
| 962 | * list for scanning. |
| 963 | * |
| 964 | * Userspace will have been told to not store |
| 965 | * this key at this point. So it is safe to |
| 966 | * just remove it. |
| 967 | */ |
| 968 | if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) { |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 969 | list_del_rcu(&smp->remote_irk->list); |
| 970 | kfree_rcu(smp->remote_irk, rcu); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 971 | smp->remote_irk = NULL; |
| 972 | } |
| 973 | } |
| 974 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 975 | if (hcon->type == ACL_LINK) { |
| 976 | if (hcon->key_type == HCI_LK_DEBUG_COMBINATION) |
| 977 | persistent = false; |
| 978 | else |
| 979 | persistent = !test_bit(HCI_CONN_FLUSH_KEY, |
| 980 | &hcon->flags); |
| 981 | } else { |
| 982 | /* The LTKs and CSRKs should be persistent only if both sides |
| 983 | * had the bonding bit set in their authentication requests. |
| 984 | */ |
| 985 | persistent = !!((req->auth_req & rsp->auth_req) & |
| 986 | SMP_AUTH_BONDING); |
| 987 | } |
| 988 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 989 | |
| 990 | if (smp->csrk) { |
| 991 | smp->csrk->bdaddr_type = hcon->dst_type; |
| 992 | bacpy(&smp->csrk->bdaddr, &hcon->dst); |
| 993 | mgmt_new_csrk(hdev, smp->csrk, persistent); |
| 994 | } |
| 995 | |
| 996 | if (smp->slave_csrk) { |
| 997 | smp->slave_csrk->bdaddr_type = hcon->dst_type; |
| 998 | bacpy(&smp->slave_csrk->bdaddr, &hcon->dst); |
| 999 | mgmt_new_csrk(hdev, smp->slave_csrk, persistent); |
| 1000 | } |
| 1001 | |
| 1002 | if (smp->ltk) { |
| 1003 | smp->ltk->bdaddr_type = hcon->dst_type; |
| 1004 | bacpy(&smp->ltk->bdaddr, &hcon->dst); |
| 1005 | mgmt_new_ltk(hdev, smp->ltk, persistent); |
| 1006 | } |
| 1007 | |
| 1008 | if (smp->slave_ltk) { |
| 1009 | smp->slave_ltk->bdaddr_type = hcon->dst_type; |
| 1010 | bacpy(&smp->slave_ltk->bdaddr, &hcon->dst); |
| 1011 | mgmt_new_ltk(hdev, smp->slave_ltk, persistent); |
| 1012 | } |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1013 | |
| 1014 | if (smp->link_key) { |
Johan Hedberg | e3befab | 2014-06-01 16:33:39 +0300 | [diff] [blame] | 1015 | struct link_key *key; |
| 1016 | u8 type; |
| 1017 | |
| 1018 | if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) |
| 1019 | type = HCI_LK_DEBUG_COMBINATION; |
| 1020 | else if (hcon->sec_level == BT_SECURITY_FIPS) |
| 1021 | type = HCI_LK_AUTH_COMBINATION_P256; |
| 1022 | else |
| 1023 | type = HCI_LK_UNAUTH_COMBINATION_P256; |
| 1024 | |
| 1025 | key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst, |
| 1026 | smp->link_key, type, 0, &persistent); |
| 1027 | if (key) { |
| 1028 | mgmt_new_link_key(hdev, key, persistent); |
| 1029 | |
| 1030 | /* Don't keep debug keys around if the relevant |
| 1031 | * flag is not set. |
| 1032 | */ |
| 1033 | if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) && |
| 1034 | key->type == HCI_LK_DEBUG_COMBINATION) { |
| 1035 | list_del_rcu(&key->list); |
| 1036 | kfree_rcu(key, rcu); |
| 1037 | } |
| 1038 | } |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 1042 | static void sc_add_ltk(struct smp_chan *smp) |
| 1043 | { |
| 1044 | struct hci_conn *hcon = smp->conn->hcon; |
| 1045 | u8 key_type, auth; |
| 1046 | |
| 1047 | if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags)) |
| 1048 | key_type = SMP_LTK_P256_DEBUG; |
| 1049 | else |
| 1050 | key_type = SMP_LTK_P256; |
| 1051 | |
| 1052 | if (hcon->pending_sec_level == BT_SECURITY_FIPS) |
| 1053 | auth = 1; |
| 1054 | else |
| 1055 | auth = 0; |
| 1056 | |
| 1057 | memset(smp->tk + smp->enc_key_size, 0, |
| 1058 | SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); |
| 1059 | |
| 1060 | smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 1061 | key_type, auth, smp->tk, smp->enc_key_size, |
| 1062 | 0, 0); |
| 1063 | } |
| 1064 | |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1065 | static void sc_generate_link_key(struct smp_chan *smp) |
| 1066 | { |
| 1067 | /* These constants are as specified in the core specification. |
| 1068 | * In ASCII they spell out to 'tmp1' and 'lebr'. |
| 1069 | */ |
| 1070 | const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 }; |
| 1071 | const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c }; |
| 1072 | |
| 1073 | smp->link_key = kzalloc(16, GFP_KERNEL); |
| 1074 | if (!smp->link_key) |
| 1075 | return; |
| 1076 | |
| 1077 | if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) { |
| 1078 | kfree(smp->link_key); |
| 1079 | smp->link_key = NULL; |
| 1080 | return; |
| 1081 | } |
| 1082 | |
| 1083 | if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) { |
| 1084 | kfree(smp->link_key); |
| 1085 | smp->link_key = NULL; |
| 1086 | return; |
| 1087 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1088 | } |
| 1089 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1090 | static void smp_allow_key_dist(struct smp_chan *smp) |
| 1091 | { |
| 1092 | /* Allow the first expected phase 3 PDU. The rest of the PDUs |
| 1093 | * will be allowed in each PDU handler to ensure we receive |
| 1094 | * them in the correct order. |
| 1095 | */ |
| 1096 | if (smp->remote_key_dist & SMP_DIST_ENC_KEY) |
| 1097 | SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO); |
| 1098 | else if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 1099 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
| 1100 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 1101 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 1102 | } |
| 1103 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1104 | static void sc_generate_ltk(struct smp_chan *smp) |
| 1105 | { |
| 1106 | /* These constants are as specified in the core specification. |
| 1107 | * In ASCII they spell out to 'tmp2' and 'brle'. |
| 1108 | */ |
| 1109 | const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 }; |
| 1110 | const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 }; |
| 1111 | struct hci_conn *hcon = smp->conn->hcon; |
| 1112 | struct hci_dev *hdev = hcon->hdev; |
| 1113 | struct link_key *key; |
| 1114 | |
| 1115 | key = hci_find_link_key(hdev, &hcon->dst); |
| 1116 | if (!key) { |
| 1117 | BT_ERR("%s No Link Key found to generate LTK", hdev->name); |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 1122 | set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); |
| 1123 | |
| 1124 | if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk)) |
| 1125 | return; |
| 1126 | |
| 1127 | if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk)) |
| 1128 | return; |
| 1129 | |
| 1130 | sc_add_ltk(smp); |
| 1131 | } |
| 1132 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 1133 | static void smp_distribute_keys(struct smp_chan *smp) |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1134 | { |
| 1135 | struct smp_cmd_pairing *req, *rsp; |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1136 | struct l2cap_conn *conn = smp->conn; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1137 | struct hci_conn *hcon = conn->hcon; |
| 1138 | struct hci_dev *hdev = hcon->hdev; |
| 1139 | __u8 *keydist; |
| 1140 | |
| 1141 | BT_DBG("conn %p", conn); |
| 1142 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1143 | rsp = (void *) &smp->prsp[1]; |
| 1144 | |
| 1145 | /* The responder sends its keys first */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1146 | if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) { |
| 1147 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1148 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1149 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1150 | |
| 1151 | req = (void *) &smp->preq[1]; |
| 1152 | |
| 1153 | if (hcon->out) { |
| 1154 | keydist = &rsp->init_key_dist; |
| 1155 | *keydist &= req->init_key_dist; |
| 1156 | } else { |
| 1157 | keydist = &rsp->resp_key_dist; |
| 1158 | *keydist &= req->resp_key_dist; |
| 1159 | } |
| 1160 | |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1161 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1162 | if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY)) |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1163 | sc_generate_link_key(smp); |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1164 | if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY)) |
| 1165 | sc_generate_ltk(smp); |
Johan Hedberg | 6a77083 | 2014-06-06 11:54:04 +0300 | [diff] [blame] | 1166 | |
| 1167 | /* Clear the keys which are generated but not distributed */ |
| 1168 | *keydist &= ~SMP_SC_NO_DIST; |
| 1169 | } |
| 1170 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1171 | BT_DBG("keydist 0x%x", *keydist); |
| 1172 | |
| 1173 | if (*keydist & SMP_DIST_ENC_KEY) { |
| 1174 | struct smp_cmd_encrypt_info enc; |
| 1175 | struct smp_cmd_master_ident ident; |
| 1176 | struct smp_ltk *ltk; |
| 1177 | u8 authenticated; |
| 1178 | __le16 ediv; |
| 1179 | __le64 rand; |
| 1180 | |
| 1181 | get_random_bytes(enc.ltk, sizeof(enc.ltk)); |
| 1182 | get_random_bytes(&ediv, sizeof(ediv)); |
| 1183 | get_random_bytes(&rand, sizeof(rand)); |
| 1184 | |
| 1185 | smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); |
| 1186 | |
| 1187 | authenticated = hcon->sec_level == BT_SECURITY_HIGH; |
| 1188 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, |
| 1189 | SMP_LTK_SLAVE, authenticated, enc.ltk, |
| 1190 | smp->enc_key_size, ediv, rand); |
| 1191 | smp->slave_ltk = ltk; |
| 1192 | |
| 1193 | ident.ediv = ediv; |
| 1194 | ident.rand = rand; |
| 1195 | |
| 1196 | smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident); |
| 1197 | |
| 1198 | *keydist &= ~SMP_DIST_ENC_KEY; |
| 1199 | } |
| 1200 | |
| 1201 | if (*keydist & SMP_DIST_ID_KEY) { |
| 1202 | struct smp_cmd_ident_addr_info addrinfo; |
| 1203 | struct smp_cmd_ident_info idinfo; |
| 1204 | |
| 1205 | memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk)); |
| 1206 | |
| 1207 | smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo); |
| 1208 | |
| 1209 | /* The hci_conn contains the local identity address |
| 1210 | * after the connection has been established. |
| 1211 | * |
| 1212 | * This is true even when the connection has been |
| 1213 | * established using a resolvable random address. |
| 1214 | */ |
| 1215 | bacpy(&addrinfo.bdaddr, &hcon->src); |
| 1216 | addrinfo.addr_type = hcon->src_type; |
| 1217 | |
| 1218 | smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), |
| 1219 | &addrinfo); |
| 1220 | |
| 1221 | *keydist &= ~SMP_DIST_ID_KEY; |
| 1222 | } |
| 1223 | |
| 1224 | if (*keydist & SMP_DIST_SIGN) { |
| 1225 | struct smp_cmd_sign_info sign; |
| 1226 | struct smp_csrk *csrk; |
| 1227 | |
| 1228 | /* Generate a new random key */ |
| 1229 | get_random_bytes(sign.csrk, sizeof(sign.csrk)); |
| 1230 | |
| 1231 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 1232 | if (csrk) { |
| 1233 | csrk->master = 0x00; |
| 1234 | memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); |
| 1235 | } |
| 1236 | smp->slave_csrk = csrk; |
| 1237 | |
| 1238 | smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign); |
| 1239 | |
| 1240 | *keydist &= ~SMP_DIST_SIGN; |
| 1241 | } |
| 1242 | |
| 1243 | /* If there are still keys to be received wait for them */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1244 | if (smp->remote_key_dist & KEY_DIST_MASK) { |
| 1245 | smp_allow_key_dist(smp); |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 1246 | return; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1247 | } |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1248 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1249 | set_bit(SMP_FLAG_COMPLETE, &smp->flags); |
| 1250 | smp_notify_keys(conn); |
| 1251 | |
| 1252 | smp_chan_destroy(conn); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 1253 | } |
| 1254 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1255 | static void smp_timeout(struct work_struct *work) |
| 1256 | { |
| 1257 | struct smp_chan *smp = container_of(work, struct smp_chan, |
| 1258 | security_timer.work); |
| 1259 | struct l2cap_conn *conn = smp->conn; |
| 1260 | |
| 1261 | BT_DBG("conn %p", conn); |
| 1262 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 1263 | hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1264 | } |
| 1265 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1266 | static struct smp_chan *smp_chan_create(struct l2cap_conn *conn) |
| 1267 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1268 | struct l2cap_chan *chan = conn->smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1269 | struct smp_chan *smp; |
| 1270 | |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 1271 | smp = kzalloc(sizeof(*smp), GFP_ATOMIC); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1272 | if (!smp) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1273 | return NULL; |
| 1274 | |
Johan Hedberg | 6a7bd10 | 2014-06-27 14:23:03 +0300 | [diff] [blame] | 1275 | smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); |
| 1276 | if (IS_ERR(smp->tfm_aes)) { |
| 1277 | BT_ERR("Unable to create ECB crypto context"); |
| 1278 | kfree(smp); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
Johan Hedberg | 407cecf | 2014-05-02 14:19:47 +0300 | [diff] [blame] | 1282 | smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC); |
| 1283 | if (IS_ERR(smp->tfm_cmac)) { |
| 1284 | BT_ERR("Unable to create CMAC crypto context"); |
| 1285 | crypto_free_blkcipher(smp->tfm_aes); |
| 1286 | kfree(smp); |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1290 | smp->conn = conn; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1291 | chan->data = smp; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1292 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1293 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL); |
| 1294 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 1295 | INIT_DELAYED_WORK(&smp->security_timer, smp_timeout); |
| 1296 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1297 | hci_conn_hold(conn->hcon); |
| 1298 | |
| 1299 | return smp; |
| 1300 | } |
| 1301 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1302 | static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16]) |
| 1303 | { |
| 1304 | struct hci_conn *hcon = smp->conn->hcon; |
| 1305 | u8 *na, *nb, a[7], b[7]; |
| 1306 | |
| 1307 | if (hcon->out) { |
| 1308 | na = smp->prnd; |
| 1309 | nb = smp->rrnd; |
| 1310 | } else { |
| 1311 | na = smp->rrnd; |
| 1312 | nb = smp->prnd; |
| 1313 | } |
| 1314 | |
| 1315 | memcpy(a, &hcon->init_addr, 6); |
| 1316 | memcpy(b, &hcon->resp_addr, 6); |
| 1317 | a[6] = hcon->init_addr_type; |
| 1318 | b[6] = hcon->resp_addr_type; |
| 1319 | |
| 1320 | return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk); |
| 1321 | } |
| 1322 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1323 | static void sc_dhkey_check(struct smp_chan *smp) |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1324 | { |
| 1325 | struct hci_conn *hcon = smp->conn->hcon; |
| 1326 | struct smp_cmd_dhkey_check check; |
| 1327 | u8 a[7], b[7], *local_addr, *remote_addr; |
| 1328 | u8 io_cap[3], r[16]; |
| 1329 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1330 | memcpy(a, &hcon->init_addr, 6); |
| 1331 | memcpy(b, &hcon->resp_addr, 6); |
| 1332 | a[6] = hcon->init_addr_type; |
| 1333 | b[6] = hcon->resp_addr_type; |
| 1334 | |
| 1335 | if (hcon->out) { |
| 1336 | local_addr = a; |
| 1337 | remote_addr = b; |
| 1338 | memcpy(io_cap, &smp->preq[1], 3); |
| 1339 | } else { |
| 1340 | local_addr = b; |
| 1341 | remote_addr = a; |
| 1342 | memcpy(io_cap, &smp->prsp[1], 3); |
| 1343 | } |
| 1344 | |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1345 | memset(r, 0, sizeof(r)); |
| 1346 | |
| 1347 | if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1348 | put_unaligned_le32(hcon->passkey_notify, r); |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1349 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 1350 | if (smp->method == REQ_OOB) |
| 1351 | memcpy(r, smp->rr, 16); |
| 1352 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1353 | smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap, |
| 1354 | local_addr, remote_addr, check.e); |
| 1355 | |
| 1356 | smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check); |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1357 | } |
| 1358 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1359 | static u8 sc_passkey_send_confirm(struct smp_chan *smp) |
| 1360 | { |
| 1361 | struct l2cap_conn *conn = smp->conn; |
| 1362 | struct hci_conn *hcon = conn->hcon; |
| 1363 | struct smp_cmd_pairing_confirm cfm; |
| 1364 | u8 r; |
| 1365 | |
| 1366 | r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); |
| 1367 | r |= 0x80; |
| 1368 | |
| 1369 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
| 1370 | |
| 1371 | if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r, |
| 1372 | cfm.confirm_val)) |
| 1373 | return SMP_UNSPECIFIED; |
| 1374 | |
| 1375 | smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); |
| 1376 | |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op) |
| 1381 | { |
| 1382 | struct l2cap_conn *conn = smp->conn; |
| 1383 | struct hci_conn *hcon = conn->hcon; |
| 1384 | struct hci_dev *hdev = hcon->hdev; |
| 1385 | u8 cfm[16], r; |
| 1386 | |
| 1387 | /* Ignore the PDU if we've already done 20 rounds (0 - 19) */ |
| 1388 | if (smp->passkey_round >= 20) |
| 1389 | return 0; |
| 1390 | |
| 1391 | switch (smp_op) { |
| 1392 | case SMP_CMD_PAIRING_RANDOM: |
| 1393 | r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01); |
| 1394 | r |= 0x80; |
| 1395 | |
| 1396 | if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk, |
| 1397 | smp->rrnd, r, cfm)) |
| 1398 | return SMP_UNSPECIFIED; |
| 1399 | |
| 1400 | if (memcmp(smp->pcnf, cfm, 16)) |
| 1401 | return SMP_CONFIRM_FAILED; |
| 1402 | |
| 1403 | smp->passkey_round++; |
| 1404 | |
| 1405 | if (smp->passkey_round == 20) { |
| 1406 | /* Generate MacKey and LTK */ |
| 1407 | if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk)) |
| 1408 | return SMP_UNSPECIFIED; |
| 1409 | } |
| 1410 | |
| 1411 | /* The round is only complete when the initiator |
| 1412 | * receives pairing random. |
| 1413 | */ |
| 1414 | if (!hcon->out) { |
| 1415 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, |
| 1416 | sizeof(smp->prnd), smp->prnd); |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 1417 | if (smp->passkey_round == 20) |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1418 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 1419 | else |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1420 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | /* Start the next round */ |
| 1425 | if (smp->passkey_round != 20) |
| 1426 | return sc_passkey_round(smp, 0); |
| 1427 | |
| 1428 | /* Passkey rounds are complete - start DHKey Check */ |
| 1429 | sc_dhkey_check(smp); |
| 1430 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1431 | |
| 1432 | break; |
| 1433 | |
| 1434 | case SMP_CMD_PAIRING_CONFIRM: |
| 1435 | if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { |
| 1436 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1441 | |
| 1442 | if (hcon->out) { |
| 1443 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, |
| 1444 | sizeof(smp->prnd), smp->prnd); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | return sc_passkey_send_confirm(smp); |
| 1449 | |
| 1450 | case SMP_CMD_PUBLIC_KEY: |
| 1451 | default: |
| 1452 | /* Initiating device starts the round */ |
| 1453 | if (!hcon->out) |
| 1454 | return 0; |
| 1455 | |
| 1456 | BT_DBG("%s Starting passkey round %u", hdev->name, |
| 1457 | smp->passkey_round + 1); |
| 1458 | |
| 1459 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 1460 | |
| 1461 | return sc_passkey_send_confirm(smp); |
| 1462 | } |
| 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1467 | static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey) |
| 1468 | { |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1469 | struct l2cap_conn *conn = smp->conn; |
| 1470 | struct hci_conn *hcon = conn->hcon; |
| 1471 | u8 smp_op; |
| 1472 | |
| 1473 | clear_bit(SMP_FLAG_WAIT_USER, &smp->flags); |
| 1474 | |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1475 | switch (mgmt_op) { |
| 1476 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 1477 | smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED); |
| 1478 | return 0; |
| 1479 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
| 1480 | smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED); |
| 1481 | return 0; |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1482 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 1483 | hcon->passkey_notify = le32_to_cpu(passkey); |
| 1484 | smp->passkey_round = 0; |
| 1485 | |
| 1486 | if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) |
| 1487 | smp_op = SMP_CMD_PAIRING_CONFIRM; |
| 1488 | else |
| 1489 | smp_op = 0; |
| 1490 | |
| 1491 | if (sc_passkey_round(smp, smp_op)) |
| 1492 | return -EIO; |
| 1493 | |
| 1494 | return 0; |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1495 | } |
| 1496 | |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 1497 | /* Initiator sends DHKey check first */ |
| 1498 | if (hcon->out) { |
| 1499 | sc_dhkey_check(smp); |
| 1500 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1501 | } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) { |
| 1502 | sc_dhkey_check(smp); |
| 1503 | sc_add_ltk(smp); |
| 1504 | } |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1509 | int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey) |
| 1510 | { |
Johan Hedberg | b10e801 | 2014-06-27 14:23:07 +0300 | [diff] [blame] | 1511 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1512 | struct l2cap_chan *chan; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1513 | struct smp_chan *smp; |
| 1514 | u32 value; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1515 | int err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1516 | |
| 1517 | BT_DBG(""); |
| 1518 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1519 | if (!conn) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1520 | return -ENOTCONN; |
| 1521 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1522 | chan = conn->smp; |
| 1523 | if (!chan) |
| 1524 | return -ENOTCONN; |
| 1525 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1526 | l2cap_chan_lock(chan); |
| 1527 | if (!chan->data) { |
| 1528 | err = -ENOTCONN; |
| 1529 | goto unlock; |
| 1530 | } |
| 1531 | |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1532 | smp = chan->data; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1533 | |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1534 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1535 | err = sc_user_reply(smp, mgmt_op, passkey); |
| 1536 | goto unlock; |
| 1537 | } |
| 1538 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1539 | switch (mgmt_op) { |
| 1540 | case MGMT_OP_USER_PASSKEY_REPLY: |
| 1541 | value = le32_to_cpu(passkey); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1542 | memset(smp->tk, 0, sizeof(smp->tk)); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1543 | BT_DBG("PassKey: %d", value); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1544 | put_unaligned_le32(value, smp->tk); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1545 | /* Fall Through */ |
| 1546 | case MGMT_OP_USER_CONFIRM_REPLY: |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1547 | set_bit(SMP_FLAG_TK_VALID, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1548 | break; |
| 1549 | case MGMT_OP_USER_PASSKEY_NEG_REPLY: |
| 1550 | case MGMT_OP_USER_CONFIRM_NEG_REPLY: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1551 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1552 | err = 0; |
| 1553 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1554 | default: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 1555 | smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1556 | err = -EOPNOTSUPP; |
| 1557 | goto unlock; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1560 | err = 0; |
| 1561 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1562 | /* If it is our turn to send Pairing Confirm, do so now */ |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1563 | if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) { |
| 1564 | u8 rsp = smp_confirm(smp); |
| 1565 | if (rsp) |
| 1566 | smp_failure(conn, rsp); |
| 1567 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1568 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1569 | unlock: |
| 1570 | l2cap_chan_unlock(chan); |
| 1571 | return err; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1574 | static void build_bredr_pairing_cmd(struct smp_chan *smp, |
| 1575 | struct smp_cmd_pairing *req, |
| 1576 | struct smp_cmd_pairing *rsp) |
| 1577 | { |
| 1578 | struct l2cap_conn *conn = smp->conn; |
| 1579 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1580 | u8 local_dist = 0, remote_dist = 0; |
| 1581 | |
| 1582 | if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) { |
| 1583 | local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
| 1584 | remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; |
| 1585 | } |
| 1586 | |
| 1587 | if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) |
| 1588 | remote_dist |= SMP_DIST_ID_KEY; |
| 1589 | |
| 1590 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 1591 | local_dist |= SMP_DIST_ID_KEY; |
| 1592 | |
| 1593 | if (!rsp) { |
| 1594 | memset(req, 0, sizeof(*req)); |
| 1595 | |
| 1596 | req->init_key_dist = local_dist; |
| 1597 | req->resp_key_dist = remote_dist; |
| 1598 | req->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
| 1599 | |
| 1600 | smp->remote_key_dist = remote_dist; |
| 1601 | |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | memset(rsp, 0, sizeof(*rsp)); |
| 1606 | |
| 1607 | rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE; |
| 1608 | rsp->init_key_dist = req->init_key_dist & remote_dist; |
| 1609 | rsp->resp_key_dist = req->resp_key_dist & local_dist; |
| 1610 | |
| 1611 | smp->remote_key_dist = rsp->init_key_dist; |
| 1612 | } |
| 1613 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1614 | 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] | 1615 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1616 | struct smp_cmd_pairing rsp, *req = (void *) skb->data; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1617 | struct l2cap_chan *chan = conn->smp; |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1618 | struct hci_dev *hdev = conn->hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1619 | struct smp_chan *smp; |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1620 | u8 key_size, auth, sec_level; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1621 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1622 | |
| 1623 | BT_DBG("conn %p", conn); |
| 1624 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1625 | if (skb->len < sizeof(*req)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1626 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1627 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1628 | if (conn->hcon->role != HCI_ROLE_SLAVE) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1629 | return SMP_CMD_NOTSUPP; |
| 1630 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1631 | if (!chan->data) |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1632 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 1633 | else |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1634 | smp = chan->data; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1635 | |
Andrei Emeltchenko | d08fd0e | 2012-07-19 17:03:43 +0300 | [diff] [blame] | 1636 | if (!smp) |
| 1637 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1638 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1639 | /* We didn't start the pairing, so match remote */ |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1640 | auth = req->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1641 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1642 | if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1643 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | b3c6410 | 2014-07-10 11:02:07 +0300 | [diff] [blame] | 1644 | return SMP_PAIRING_NOTSUPP; |
| 1645 | |
Johan Hedberg | 903b71c | 2014-09-08 16:59:18 -0700 | [diff] [blame] | 1646 | if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) |
| 1647 | return SMP_AUTH_REQUIREMENTS; |
| 1648 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1649 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 1650 | memcpy(&smp->preq[1], req, sizeof(*req)); |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1651 | skb_pull(skb, sizeof(*req)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1652 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1653 | /* SMP over BR/EDR requires special treatment */ |
| 1654 | if (conn->hcon->type == ACL_LINK) { |
| 1655 | /* We must have a BR/EDR SC link */ |
| 1656 | if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags)) |
| 1657 | return SMP_CROSS_TRANSP_NOT_ALLOWED; |
| 1658 | |
| 1659 | set_bit(SMP_FLAG_SC, &smp->flags); |
| 1660 | |
| 1661 | build_bredr_pairing_cmd(smp, req, &rsp); |
| 1662 | |
| 1663 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 1664 | if (check_enc_key_size(conn, key_size)) |
| 1665 | return SMP_ENC_KEY_SIZE; |
| 1666 | |
| 1667 | /* Clear bits which are generated but not distributed */ |
| 1668 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1669 | |
| 1670 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1671 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); |
| 1672 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
| 1673 | |
| 1674 | smp_distribute_keys(smp); |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 1678 | build_pairing_cmd(conn, req, &rsp, auth); |
| 1679 | |
| 1680 | if (rsp.auth_req & SMP_AUTH_SC) |
| 1681 | set_bit(SMP_FLAG_SC, &smp->flags); |
| 1682 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 1683 | if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 1684 | sec_level = BT_SECURITY_MEDIUM; |
| 1685 | else |
| 1686 | sec_level = authreq_to_seclevel(auth); |
| 1687 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 1688 | if (sec_level > conn->hcon->pending_sec_level) |
| 1689 | conn->hcon->pending_sec_level = sec_level; |
Ido Yariv | fdde0a2 | 2012-03-05 20:09:38 +0200 | [diff] [blame] | 1690 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1691 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1692 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1693 | u8 method; |
| 1694 | |
| 1695 | method = get_auth_method(smp, conn->hcon->io_capability, |
| 1696 | req->io_capability); |
| 1697 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1698 | return SMP_AUTH_REQUIREMENTS; |
| 1699 | } |
| 1700 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1701 | key_size = min(req->max_key_size, rsp.max_key_size); |
| 1702 | if (check_enc_key_size(conn, key_size)) |
| 1703 | return SMP_ENC_KEY_SIZE; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1704 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1705 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1706 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1707 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1708 | memcpy(&smp->prsp[1], &rsp, sizeof(rsp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 1709 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1710 | smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp); |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1711 | |
| 1712 | clear_bit(SMP_FLAG_INITIATOR, &smp->flags); |
| 1713 | |
| 1714 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1715 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1716 | /* Clear bits which are generated but not distributed */ |
| 1717 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1718 | /* Wait for Public Key from Initiating Device */ |
| 1719 | return 0; |
| 1720 | } else { |
| 1721 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 1722 | } |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1723 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1724 | /* Request setup of TK */ |
| 1725 | ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability); |
| 1726 | if (ret) |
| 1727 | return SMP_UNSPECIFIED; |
| 1728 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1729 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1730 | } |
| 1731 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1732 | static u8 sc_send_public_key(struct smp_chan *smp) |
| 1733 | { |
Johan Hedberg | 70157ef | 2014-06-24 15:22:59 +0300 | [diff] [blame] | 1734 | struct hci_dev *hdev = smp->conn->hcon->hdev; |
| 1735 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1736 | BT_DBG(""); |
| 1737 | |
Johan Hedberg | 70157ef | 2014-06-24 15:22:59 +0300 | [diff] [blame] | 1738 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) { |
| 1739 | BT_DBG("Using debug keys"); |
| 1740 | memcpy(smp->local_pk, debug_pk, 64); |
| 1741 | memcpy(smp->local_sk, debug_sk, 32); |
| 1742 | set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); |
| 1743 | } else { |
| 1744 | while (true) { |
| 1745 | /* Generate local key pair for Secure Connections */ |
| 1746 | if (!ecc_make_key(smp->local_pk, smp->local_sk)) |
| 1747 | return SMP_UNSPECIFIED; |
Johan Hedberg | 6c0dcc5 | 2014-06-06 15:33:30 +0300 | [diff] [blame] | 1748 | |
Johan Hedberg | 70157ef | 2014-06-24 15:22:59 +0300 | [diff] [blame] | 1749 | /* This is unlikely, but we need to check that |
| 1750 | * we didn't accidentially generate a debug key. |
| 1751 | */ |
| 1752 | if (memcmp(smp->local_sk, debug_sk, 32)) |
| 1753 | break; |
| 1754 | } |
Johan Hedberg | 6c0dcc5 | 2014-06-06 15:33:30 +0300 | [diff] [blame] | 1755 | } |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1756 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 1757 | SMP_DBG("Local Public Key X: %32phN", smp->local_pk); |
| 1758 | SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]); |
| 1759 | SMP_DBG("Local Private Key: %32phN", smp->local_sk); |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1760 | |
| 1761 | smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1766 | 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] | 1767 | { |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1768 | struct smp_cmd_pairing *req, *rsp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1769 | struct l2cap_chan *chan = conn->smp; |
| 1770 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1771 | struct hci_dev *hdev = conn->hcon->hdev; |
Johan Hedberg | 3a7dbfb | 2014-09-10 17:37:41 -0700 | [diff] [blame] | 1772 | u8 key_size, auth; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1773 | int ret; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1774 | |
| 1775 | BT_DBG("conn %p", conn); |
| 1776 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1777 | if (skb->len < sizeof(*rsp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1778 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1779 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1780 | if (conn->hcon->role != HCI_ROLE_MASTER) |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1781 | return SMP_CMD_NOTSUPP; |
| 1782 | |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1783 | skb_pull(skb, sizeof(*rsp)); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1784 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1785 | req = (void *) &smp->preq[1]; |
Vinicius Costa Gomes | 3158c50 | 2011-06-14 13:37:42 -0300 | [diff] [blame] | 1786 | |
| 1787 | key_size = min(req->max_key_size, rsp->max_key_size); |
| 1788 | if (check_enc_key_size(conn, key_size)) |
| 1789 | return SMP_ENC_KEY_SIZE; |
| 1790 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 1791 | auth = rsp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1792 | |
Johan Hedberg | 903b71c | 2014-09-08 16:59:18 -0700 | [diff] [blame] | 1793 | if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) |
| 1794 | return SMP_AUTH_REQUIREMENTS; |
| 1795 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 1796 | smp->prsp[0] = SMP_CMD_PAIRING_RSP; |
| 1797 | memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); |
| 1798 | |
| 1799 | /* Update remote key distribution in case the remote cleared |
| 1800 | * some bits that we had enabled in our request. |
| 1801 | */ |
| 1802 | smp->remote_key_dist &= rsp->resp_key_dist; |
| 1803 | |
| 1804 | /* For BR/EDR this means we're done and can start phase 3 */ |
| 1805 | if (conn->hcon->type == ACL_LINK) { |
| 1806 | /* Clear bits which are generated but not distributed */ |
| 1807 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1808 | smp_distribute_keys(smp); |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1812 | if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC)) |
| 1813 | set_bit(SMP_FLAG_SC, &smp->flags); |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 1814 | else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH) |
| 1815 | conn->hcon->pending_sec_level = BT_SECURITY_HIGH; |
Johan Hedberg | 6566877 | 2014-05-16 11:03:34 +0300 | [diff] [blame] | 1816 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 1817 | /* If we need MITM check that it can be achieved */ |
Johan Hedberg | 2ed8f65 | 2014-06-17 13:07:39 +0300 | [diff] [blame] | 1818 | if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) { |
| 1819 | u8 method; |
| 1820 | |
| 1821 | method = get_auth_method(smp, req->io_capability, |
| 1822 | rsp->io_capability); |
| 1823 | if (method == JUST_WORKS || method == JUST_CFM) |
| 1824 | return SMP_AUTH_REQUIREMENTS; |
| 1825 | } |
| 1826 | |
Johan Hedberg | e84a6b1 | 2013-12-02 10:49:03 +0200 | [diff] [blame] | 1827 | get_random_bytes(smp->prnd, sizeof(smp->prnd)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1828 | |
Johan Hedberg | fdcc4be | 2014-03-14 10:53:50 +0200 | [diff] [blame] | 1829 | /* Update remote key distribution in case the remote cleared |
| 1830 | * some bits that we had enabled in our request. |
| 1831 | */ |
| 1832 | smp->remote_key_dist &= rsp->resp_key_dist; |
| 1833 | |
Johan Hedberg | 3b19146 | 2014-06-06 10:50:15 +0300 | [diff] [blame] | 1834 | if (test_bit(SMP_FLAG_SC, &smp->flags)) { |
| 1835 | /* Clear bits which are generated but not distributed */ |
| 1836 | smp->remote_key_dist &= ~SMP_SC_NO_DIST; |
| 1837 | SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); |
| 1838 | return sc_send_public_key(smp); |
| 1839 | } |
| 1840 | |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 1841 | auth |= req->auth_req; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1842 | |
Johan Hedberg | 476585e | 2012-06-06 18:54:15 +0800 | [diff] [blame] | 1843 | ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1844 | if (ret) |
| 1845 | return SMP_UNSPECIFIED; |
| 1846 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1847 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 1848 | |
| 1849 | /* Can't compose response until we have been confirmed */ |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1850 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1851 | return smp_confirm(smp); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1852 | |
| 1853 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1854 | } |
| 1855 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame] | 1856 | static u8 sc_check_confirm(struct smp_chan *smp) |
| 1857 | { |
| 1858 | struct l2cap_conn *conn = smp->conn; |
| 1859 | |
| 1860 | BT_DBG(""); |
| 1861 | |
| 1862 | /* Public Key exchange must happen before any other steps */ |
| 1863 | if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags)) |
| 1864 | return SMP_UNSPECIFIED; |
| 1865 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1866 | if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) |
| 1867 | return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM); |
| 1868 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame] | 1869 | if (conn->hcon->out) { |
| 1870 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1871 | smp->prnd); |
| 1872 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1873 | } |
| 1874 | |
| 1875 | return 0; |
| 1876 | } |
| 1877 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1878 | 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] | 1879 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1880 | struct l2cap_chan *chan = conn->smp; |
| 1881 | struct smp_chan *smp = chan->data; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1882 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1883 | BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); |
| 1884 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1885 | if (skb->len < sizeof(smp->pcnf)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1886 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1887 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 1888 | memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); |
| 1889 | skb_pull(skb, sizeof(smp->pcnf)); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1890 | |
Johan Hedberg | dcee2b3 | 2014-06-06 11:36:38 +0300 | [diff] [blame] | 1891 | if (test_bit(SMP_FLAG_SC, &smp->flags)) |
| 1892 | return sc_check_confirm(smp); |
| 1893 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1894 | if (conn->hcon->out) { |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1895 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1896 | smp->prnd); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 1897 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) |
Johan Hedberg | 1cc6114 | 2014-05-20 09:45:52 +0300 | [diff] [blame] | 1902 | return smp_confirm(smp); |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1903 | else |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 1904 | set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1905 | |
| 1906 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1907 | } |
| 1908 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 1909 | 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] | 1910 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 1911 | struct l2cap_chan *chan = conn->smp; |
| 1912 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1913 | struct hci_conn *hcon = conn->hcon; |
| 1914 | u8 *pkax, *pkbx, *na, *nb; |
| 1915 | u32 passkey; |
| 1916 | int err; |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1917 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1918 | BT_DBG("conn %p", conn); |
Anderson Briglia | 7d24ddc | 2011-06-09 18:50:46 -0300 | [diff] [blame] | 1919 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1920 | if (skb->len < sizeof(smp->rrnd)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 1921 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 1922 | |
Johan Hedberg | 943a732 | 2014-03-18 12:58:24 +0200 | [diff] [blame] | 1923 | memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd)); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1924 | skb_pull(skb, sizeof(smp->rrnd)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1925 | |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1926 | if (!test_bit(SMP_FLAG_SC, &smp->flags)) |
| 1927 | return smp_random(smp); |
| 1928 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 1929 | if (smp->method == REQ_OOB) { |
| 1930 | if (!hcon->out) |
| 1931 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, |
| 1932 | sizeof(smp->prnd), smp->prnd); |
| 1933 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1934 | goto mackey_and_ltk; |
| 1935 | } |
| 1936 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1937 | /* Passkey entry has special treatment */ |
| 1938 | if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) |
| 1939 | return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM); |
| 1940 | |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1941 | if (hcon->out) { |
| 1942 | u8 cfm[16]; |
| 1943 | |
| 1944 | err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk, |
| 1945 | smp->rrnd, 0, cfm); |
| 1946 | if (err) |
| 1947 | return SMP_UNSPECIFIED; |
| 1948 | |
| 1949 | if (memcmp(smp->pcnf, cfm, 16)) |
| 1950 | return SMP_CONFIRM_FAILED; |
| 1951 | |
| 1952 | pkax = smp->local_pk; |
| 1953 | pkbx = smp->remote_pk; |
| 1954 | na = smp->prnd; |
| 1955 | nb = smp->rrnd; |
| 1956 | } else { |
| 1957 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), |
| 1958 | smp->prnd); |
| 1959 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1960 | |
| 1961 | pkax = smp->remote_pk; |
| 1962 | pkbx = smp->local_pk; |
| 1963 | na = smp->rrnd; |
| 1964 | nb = smp->prnd; |
| 1965 | } |
| 1966 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 1967 | mackey_and_ltk: |
Johan Hedberg | 760b018 | 2014-06-06 11:44:05 +0300 | [diff] [blame] | 1968 | /* Generate MacKey and LTK */ |
| 1969 | err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk); |
| 1970 | if (err) |
| 1971 | return SMP_UNSPECIFIED; |
| 1972 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 1973 | if (smp->method == JUST_WORKS || smp->method == REQ_OOB) { |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1974 | if (hcon->out) { |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1975 | sc_dhkey_check(smp); |
Johan Hedberg | dddd305 | 2014-06-01 15:38:09 +0300 | [diff] [blame] | 1976 | SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK); |
| 1977 | } |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1981 | err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey); |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1982 | if (err) |
| 1983 | return SMP_UNSPECIFIED; |
| 1984 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 1985 | err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type, |
| 1986 | hcon->dst_type, passkey, 0); |
| 1987 | if (err) |
| 1988 | return SMP_UNSPECIFIED; |
| 1989 | |
| 1990 | set_bit(SMP_FLAG_WAIT_USER, &smp->flags); |
| 1991 | |
Johan Hedberg | 191dc7fe2 | 2014-06-06 11:39:49 +0300 | [diff] [blame] | 1992 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 1993 | } |
| 1994 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 1995 | static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1996 | { |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 1997 | struct smp_ltk *key; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 1998 | struct hci_conn *hcon = conn->hcon; |
| 1999 | |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 2000 | key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role); |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2001 | if (!key) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 2002 | return false; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2003 | |
Johan Hedberg | a6f7833 | 2014-09-10 17:37:45 -0700 | [diff] [blame] | 2004 | if (smp_ltk_sec_level(key) < sec_level) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 2005 | return false; |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 2006 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 2007 | if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags)) |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 2008 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2009 | |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2010 | hci_le_start_enc(hcon, key->ediv, key->rand, key->val); |
| 2011 | hcon->enc_key_size = key->enc_size; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2012 | |
Johan Hedberg | fe59a05 | 2014-07-01 19:14:12 +0300 | [diff] [blame] | 2013 | /* We never store STKs for master role, so clear this flag */ |
| 2014 | clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags); |
| 2015 | |
Marcel Holtmann | f81cd82 | 2014-07-01 10:59:24 +0200 | [diff] [blame] | 2016 | return true; |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2017 | } |
Marcel Holtmann | f156046 | 2013-10-13 05:43:25 -0700 | [diff] [blame] | 2018 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 2019 | bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level, |
| 2020 | enum smp_key_pref key_pref) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 2021 | { |
| 2022 | if (sec_level == BT_SECURITY_LOW) |
| 2023 | return true; |
| 2024 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 2025 | /* If we're encrypted with an STK but the caller prefers using |
| 2026 | * LTK claim insufficient security. This way we allow the |
| 2027 | * connection to be re-encrypted with an LTK, even if the LTK |
| 2028 | * provides the same level of security. Only exception is if we |
| 2029 | * don't have an LTK (e.g. because of key distribution bits). |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 2030 | */ |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 2031 | if (key_pref == SMP_USE_LTK && |
| 2032 | test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) && |
Johan Hedberg | f3a73d9 | 2014-05-29 15:02:59 +0300 | [diff] [blame] | 2033 | hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role)) |
Johan Hedberg | 9ab65d60 | 2014-07-01 19:14:13 +0300 | [diff] [blame] | 2034 | return false; |
| 2035 | |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 2036 | if (hcon->sec_level >= sec_level) |
| 2037 | return true; |
| 2038 | |
| 2039 | return false; |
| 2040 | } |
| 2041 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2042 | 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] | 2043 | { |
| 2044 | struct smp_cmd_security_req *rp = (void *) skb->data; |
| 2045 | struct smp_cmd_pairing cp; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 2046 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 2047 | struct hci_dev *hdev = hcon->hdev; |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 2048 | struct smp_chan *smp; |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 2049 | u8 sec_level, auth; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2050 | |
| 2051 | BT_DBG("conn %p", conn); |
| 2052 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2053 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2054 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2055 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 2056 | if (hcon->role != HCI_ROLE_MASTER) |
Johan Hedberg | 86ca9ea | 2013-11-05 11:30:39 +0200 | [diff] [blame] | 2057 | return SMP_CMD_NOTSUPP; |
| 2058 | |
Johan Hedberg | 0edb14d | 2014-05-26 13:29:28 +0300 | [diff] [blame] | 2059 | auth = rp->auth_req & AUTH_REQ_MASK(hdev); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 2060 | |
Johan Hedberg | 903b71c | 2014-09-08 16:59:18 -0700 | [diff] [blame] | 2061 | if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) |
| 2062 | return SMP_AUTH_REQUIREMENTS; |
| 2063 | |
Johan Hedberg | 5be5e27 | 2014-09-10 17:58:54 -0700 | [diff] [blame] | 2064 | if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) |
Johan Hedberg | 1afc2a1 | 2014-09-10 17:37:44 -0700 | [diff] [blame] | 2065 | sec_level = BT_SECURITY_MEDIUM; |
| 2066 | else |
| 2067 | sec_level = authreq_to_seclevel(auth); |
| 2068 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 2069 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Johan Hedberg | 854f472 | 2014-07-01 18:40:20 +0300 | [diff] [blame] | 2070 | return 0; |
| 2071 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 2072 | if (sec_level > hcon->pending_sec_level) |
| 2073 | hcon->pending_sec_level = sec_level; |
Vinicius Costa Gomes | feb45eb | 2011-08-25 20:02:35 -0300 | [diff] [blame] | 2074 | |
Johan Hedberg | 4dab786 | 2012-06-07 14:58:37 +0800 | [diff] [blame] | 2075 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
Vinicius Costa Gomes | 988c599 | 2011-08-25 20:02:28 -0300 | [diff] [blame] | 2076 | return 0; |
| 2077 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 2078 | smp = smp_chan_create(conn); |
Johan Hedberg | c29d244 | 2014-06-16 19:25:14 +0300 | [diff] [blame] | 2079 | if (!smp) |
| 2080 | return SMP_UNSPECIFIED; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 2081 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 2082 | if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 2083 | (auth & SMP_AUTH_BONDING)) |
Johan Hedberg | 616d55b | 2014-07-29 14:18:48 +0300 | [diff] [blame] | 2084 | return SMP_PAIRING_NOTSUPP; |
| 2085 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2086 | skb_pull(skb, sizeof(*rp)); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2087 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2088 | memset(&cp, 0, sizeof(cp)); |
Johan Hedberg | c05b933 | 2014-09-10 17:37:42 -0700 | [diff] [blame] | 2089 | build_pairing_cmd(conn, &cp, NULL, auth); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2090 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 2091 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 2092 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 2093 | |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2094 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2095 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 2096 | |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2097 | return 0; |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2098 | } |
| 2099 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 2100 | int smp_conn_security(struct hci_conn *hcon, __u8 sec_level) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2101 | { |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 2102 | struct l2cap_conn *conn = hcon->l2cap_data; |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 2103 | struct l2cap_chan *chan; |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 2104 | struct smp_chan *smp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 2105 | __u8 authreq; |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2106 | int ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2107 | |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 2108 | BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level); |
| 2109 | |
Johan Hedberg | 0a66cf2 | 2014-03-24 14:39:03 +0200 | [diff] [blame] | 2110 | /* This may be NULL if there's an unexpected disconnection */ |
| 2111 | if (!conn) |
| 2112 | return 1; |
| 2113 | |
Johan Hedberg | c68b7f1 | 2014-09-06 06:59:10 +0300 | [diff] [blame] | 2114 | chan = conn->smp; |
| 2115 | |
Johan Hedberg | 757aee0 | 2013-04-24 13:05:32 +0300 | [diff] [blame] | 2116 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 2117 | return 1; |
| 2118 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 2119 | if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 2120 | return 1; |
| 2121 | |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 2122 | if (sec_level > hcon->pending_sec_level) |
| 2123 | hcon->pending_sec_level = sec_level; |
| 2124 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 2125 | if (hcon->role == HCI_ROLE_MASTER) |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 2126 | if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) |
| 2127 | return 0; |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 2128 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2129 | l2cap_chan_lock(chan); |
| 2130 | |
| 2131 | /* If SMP is already in progress ignore this request */ |
| 2132 | if (chan->data) { |
| 2133 | ret = 0; |
| 2134 | goto unlock; |
| 2135 | } |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 2136 | |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 2137 | smp = smp_chan_create(conn); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2138 | if (!smp) { |
| 2139 | ret = 1; |
| 2140 | goto unlock; |
| 2141 | } |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 2142 | |
| 2143 | authreq = seclevel_to_authreq(sec_level); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 2144 | |
Johan Hedberg | d2eb9e1 | 2014-05-16 10:59:06 +0300 | [diff] [blame] | 2145 | if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags)) |
| 2146 | authreq |= SMP_AUTH_SC; |
| 2147 | |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 2148 | /* Require MITM if IO Capability allows or the security level |
| 2149 | * requires it. |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 2150 | */ |
Johan Hedberg | 79897d2 | 2014-06-01 09:45:24 +0300 | [diff] [blame] | 2151 | if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT || |
Johan Hedberg | c7262e7 | 2014-06-17 13:07:37 +0300 | [diff] [blame] | 2152 | hcon->pending_sec_level > BT_SECURITY_MEDIUM) |
Johan Hedberg | 2e23364 | 2014-03-18 15:42:30 +0200 | [diff] [blame] | 2153 | authreq |= SMP_AUTH_MITM; |
| 2154 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 2155 | if (hcon->role == HCI_ROLE_MASTER) { |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 2156 | struct smp_cmd_pairing cp; |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 2157 | |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 2158 | build_pairing_cmd(conn, &cp, NULL, authreq); |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 2159 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 2160 | memcpy(&smp->preq[1], &cp, sizeof(cp)); |
Anderson Briglia | f01ead3 | 2011-06-09 18:50:45 -0300 | [diff] [blame] | 2161 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2162 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2163 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2164 | } else { |
| 2165 | struct smp_cmd_security_req cp; |
Brian Gix | 2b64d15 | 2011-12-21 16:12:12 -0800 | [diff] [blame] | 2166 | cp.auth_req = authreq; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2167 | smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2168 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2169 | } |
| 2170 | |
Johan Hedberg | 4a74d65 | 2014-05-20 09:45:50 +0300 | [diff] [blame] | 2171 | set_bit(SMP_FLAG_INITIATOR, &smp->flags); |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2172 | ret = 0; |
Johan Hedberg | edca792 | 2014-03-24 15:54:11 +0200 | [diff] [blame] | 2173 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2174 | unlock: |
| 2175 | l2cap_chan_unlock(chan); |
| 2176 | return ret; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2177 | } |
| 2178 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2179 | static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2180 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2181 | struct smp_cmd_encrypt_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2182 | struct l2cap_chan *chan = conn->smp; |
| 2183 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2184 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2185 | BT_DBG("conn %p", conn); |
| 2186 | |
| 2187 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2188 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2189 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2190 | SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 2191 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2192 | skb_pull(skb, sizeof(*rp)); |
| 2193 | |
Vinicius Costa Gomes | 1c1def0 | 2011-09-05 14:31:30 -0300 | [diff] [blame] | 2194 | memcpy(smp->tk, rp->ltk, sizeof(smp->tk)); |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2195 | |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2196 | return 0; |
| 2197 | } |
| 2198 | |
| 2199 | static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2200 | { |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2201 | struct smp_cmd_master_ident *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2202 | struct l2cap_chan *chan = conn->smp; |
| 2203 | struct smp_chan *smp = chan->data; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2204 | struct hci_dev *hdev = conn->hcon->hdev; |
| 2205 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 2206 | struct smp_ltk *ltk; |
Vinicius Costa Gomes | c9839a1 | 2012-02-02 21:08:01 -0300 | [diff] [blame] | 2207 | u8 authenticated; |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2208 | |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2209 | BT_DBG("conn %p", conn); |
| 2210 | |
| 2211 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2212 | return SMP_INVALID_PARAMS; |
Johan Hedberg | c46b98b | 2014-02-18 10:19:29 +0200 | [diff] [blame] | 2213 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 2214 | /* Mark the information as received */ |
| 2215 | smp->remote_key_dist &= ~SMP_DIST_ENC_KEY; |
| 2216 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2217 | if (smp->remote_key_dist & SMP_DIST_ID_KEY) |
| 2218 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO); |
Johan Hedberg | 196332f | 2014-09-09 16:21:46 -0700 | [diff] [blame] | 2219 | else if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 2220 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2221 | |
Vinicius Costa Gomes | 16b9083 | 2011-07-07 18:59:39 -0300 | [diff] [blame] | 2222 | skb_pull(skb, sizeof(*rp)); |
| 2223 | |
Marcel Holtmann | ce39fb4 | 2013-10-13 02:23:39 -0700 | [diff] [blame] | 2224 | authenticated = (hcon->sec_level == BT_SECURITY_HIGH); |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 2225 | ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK, |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 2226 | authenticated, smp->tk, smp->enc_key_size, |
| 2227 | rp->ediv, rp->rand); |
| 2228 | smp->ltk = ltk; |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 2229 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 2230 | smp_distribute_keys(smp); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2231 | |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2235 | static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2236 | { |
| 2237 | struct smp_cmd_ident_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2238 | struct l2cap_chan *chan = conn->smp; |
| 2239 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2240 | |
| 2241 | BT_DBG(""); |
| 2242 | |
| 2243 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2244 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2245 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2246 | SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO); |
Johan Hedberg | 6131ddc | 2014-02-18 10:19:37 +0200 | [diff] [blame] | 2247 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2248 | skb_pull(skb, sizeof(*info)); |
| 2249 | |
| 2250 | memcpy(smp->irk, info->irk, 16); |
| 2251 | |
| 2252 | return 0; |
| 2253 | } |
| 2254 | |
| 2255 | static int smp_cmd_ident_addr_info(struct l2cap_conn *conn, |
| 2256 | struct sk_buff *skb) |
| 2257 | { |
| 2258 | struct smp_cmd_ident_addr_info *info = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2259 | struct l2cap_chan *chan = conn->smp; |
| 2260 | struct smp_chan *smp = chan->data; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2261 | struct hci_conn *hcon = conn->hcon; |
| 2262 | bdaddr_t rpa; |
| 2263 | |
| 2264 | BT_DBG(""); |
| 2265 | |
| 2266 | if (skb->len < sizeof(*info)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2267 | return SMP_INVALID_PARAMS; |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2268 | |
Johan Hedberg | 9747a9f | 2014-02-26 23:33:43 +0200 | [diff] [blame] | 2269 | /* Mark the information as received */ |
| 2270 | smp->remote_key_dist &= ~SMP_DIST_ID_KEY; |
| 2271 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2272 | if (smp->remote_key_dist & SMP_DIST_SIGN) |
| 2273 | SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO); |
| 2274 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2275 | skb_pull(skb, sizeof(*info)); |
| 2276 | |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 2277 | /* Strictly speaking the Core Specification (4.1) allows sending |
| 2278 | * an empty address which would force us to rely on just the IRK |
| 2279 | * as "identity information". However, since such |
| 2280 | * implementations are not known of and in order to not over |
| 2281 | * complicate our implementation, simply pretend that we never |
| 2282 | * received an IRK for such a device. |
| 2283 | */ |
| 2284 | if (!bacmp(&info->bdaddr, BDADDR_ANY)) { |
| 2285 | BT_ERR("Ignoring IRK with no identity address"); |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 2286 | goto distribute; |
Johan Hedberg | a9a58f8 | 2014-02-25 22:24:37 +0200 | [diff] [blame] | 2287 | } |
| 2288 | |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2289 | bacpy(&smp->id_addr, &info->bdaddr); |
| 2290 | smp->id_addr_type = info->addr_type; |
| 2291 | |
| 2292 | if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type)) |
| 2293 | bacpy(&rpa, &hcon->dst); |
| 2294 | else |
| 2295 | bacpy(&rpa, BDADDR_ANY); |
| 2296 | |
Johan Hedberg | 23d0e12 | 2014-02-19 14:57:46 +0200 | [diff] [blame] | 2297 | smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr, |
| 2298 | smp->id_addr_type, smp->irk, &rpa); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2299 | |
Johan Hedberg | 31dd624 | 2014-06-27 14:23:02 +0300 | [diff] [blame] | 2300 | distribute: |
Johan Hedberg | c6e81e9 | 2014-09-05 22:19:54 +0300 | [diff] [blame] | 2301 | if (!(smp->remote_key_dist & KEY_DIST_MASK)) |
| 2302 | smp_distribute_keys(smp); |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2303 | |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2307 | static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2308 | { |
| 2309 | struct smp_cmd_sign_info *rp = (void *) skb->data; |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2310 | struct l2cap_chan *chan = conn->smp; |
| 2311 | struct smp_chan *smp = chan->data; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2312 | struct smp_csrk *csrk; |
| 2313 | |
| 2314 | BT_DBG("conn %p", conn); |
| 2315 | |
| 2316 | if (skb->len < sizeof(*rp)) |
Johan Hedberg | 38e4a91 | 2014-05-08 14:19:11 +0300 | [diff] [blame] | 2317 | return SMP_INVALID_PARAMS; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2318 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2319 | /* Mark the information as received */ |
| 2320 | smp->remote_key_dist &= ~SMP_DIST_SIGN; |
| 2321 | |
| 2322 | skb_pull(skb, sizeof(*rp)); |
| 2323 | |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2324 | csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); |
| 2325 | if (csrk) { |
| 2326 | csrk->master = 0x01; |
| 2327 | memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); |
| 2328 | } |
| 2329 | smp->csrk = csrk; |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 2330 | smp_distribute_keys(smp); |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2331 | |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 2335 | static u8 sc_select_method(struct smp_chan *smp) |
| 2336 | { |
| 2337 | struct l2cap_conn *conn = smp->conn; |
| 2338 | struct hci_conn *hcon = conn->hcon; |
| 2339 | struct smp_cmd_pairing *local, *remote; |
| 2340 | u8 local_mitm, remote_mitm, local_io, remote_io, method; |
| 2341 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 2342 | if (test_bit(SMP_FLAG_OOB, &smp->flags)) |
| 2343 | return REQ_OOB; |
| 2344 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 2345 | /* The preq/prsp contain the raw Pairing Request/Response PDUs |
| 2346 | * which are needed as inputs to some crypto functions. To get |
| 2347 | * the "struct smp_cmd_pairing" from them we need to skip the |
| 2348 | * first byte which contains the opcode. |
| 2349 | */ |
| 2350 | if (hcon->out) { |
| 2351 | local = (void *) &smp->preq[1]; |
| 2352 | remote = (void *) &smp->prsp[1]; |
| 2353 | } else { |
| 2354 | local = (void *) &smp->prsp[1]; |
| 2355 | remote = (void *) &smp->preq[1]; |
| 2356 | } |
| 2357 | |
| 2358 | local_io = local->io_capability; |
| 2359 | remote_io = remote->io_capability; |
| 2360 | |
| 2361 | local_mitm = (local->auth_req & SMP_AUTH_MITM); |
| 2362 | remote_mitm = (remote->auth_req & SMP_AUTH_MITM); |
| 2363 | |
| 2364 | /* If either side wants MITM, look up the method from the table, |
| 2365 | * otherwise use JUST WORKS. |
| 2366 | */ |
| 2367 | if (local_mitm || remote_mitm) |
| 2368 | method = get_auth_method(smp, local_io, remote_io); |
| 2369 | else |
| 2370 | method = JUST_WORKS; |
| 2371 | |
| 2372 | /* Don't confirm locally initiated pairing attempts */ |
| 2373 | if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags)) |
| 2374 | method = JUST_WORKS; |
| 2375 | |
| 2376 | return method; |
| 2377 | } |
| 2378 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2379 | static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2380 | { |
| 2381 | struct smp_cmd_public_key *key = (void *) skb->data; |
| 2382 | struct hci_conn *hcon = conn->hcon; |
| 2383 | struct l2cap_chan *chan = conn->smp; |
| 2384 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 2385 | struct hci_dev *hdev = hcon->hdev; |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 2386 | struct smp_cmd_pairing_confirm cfm; |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2387 | int err; |
| 2388 | |
| 2389 | BT_DBG("conn %p", conn); |
| 2390 | |
| 2391 | if (skb->len < sizeof(*key)) |
| 2392 | return SMP_INVALID_PARAMS; |
| 2393 | |
| 2394 | memcpy(smp->remote_pk, key, 64); |
| 2395 | |
| 2396 | /* Non-initiating device sends its public key after receiving |
| 2397 | * the key from the initiating device. |
| 2398 | */ |
| 2399 | if (!hcon->out) { |
| 2400 | err = sc_send_public_key(smp); |
| 2401 | if (err) |
| 2402 | return err; |
| 2403 | } |
| 2404 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 2405 | SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk); |
| 2406 | SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]); |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2407 | |
| 2408 | if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey)) |
| 2409 | return SMP_UNSPECIFIED; |
| 2410 | |
Johan Hedberg | c7a3d57 | 2014-12-01 22:03:16 +0200 | [diff] [blame^] | 2411 | SMP_DBG("DHKey %32phN", smp->dhkey); |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2412 | |
| 2413 | set_bit(SMP_FLAG_REMOTE_PK, &smp->flags); |
| 2414 | |
Johan Hedberg | 5e3d3d9 | 2014-05-31 18:51:02 +0300 | [diff] [blame] | 2415 | smp->method = sc_select_method(smp); |
| 2416 | |
| 2417 | BT_DBG("%s selected method 0x%02x", hdev->name, smp->method); |
| 2418 | |
| 2419 | /* JUST_WORKS and JUST_CFM result in an unauthenticated key */ |
| 2420 | if (smp->method == JUST_WORKS || smp->method == JUST_CFM) |
| 2421 | hcon->pending_sec_level = BT_SECURITY_MEDIUM; |
| 2422 | else |
| 2423 | hcon->pending_sec_level = BT_SECURITY_FIPS; |
| 2424 | |
Johan Hedberg | aeb7d46 | 2014-05-31 18:52:28 +0300 | [diff] [blame] | 2425 | if (!memcmp(debug_pk, smp->remote_pk, 64)) |
| 2426 | set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags); |
| 2427 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 2428 | if (smp->method == DSP_PASSKEY) { |
| 2429 | get_random_bytes(&hcon->passkey_notify, |
| 2430 | sizeof(hcon->passkey_notify)); |
| 2431 | hcon->passkey_notify %= 1000000; |
| 2432 | hcon->passkey_entered = 0; |
| 2433 | smp->passkey_round = 0; |
| 2434 | if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type, |
| 2435 | hcon->dst_type, |
| 2436 | hcon->passkey_notify, |
| 2437 | hcon->passkey_entered)) |
| 2438 | return SMP_UNSPECIFIED; |
| 2439 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 2440 | return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY); |
| 2441 | } |
| 2442 | |
Johan Hedberg | a29b073 | 2014-10-28 15:17:05 +0100 | [diff] [blame] | 2443 | if (smp->method == REQ_OOB) { |
| 2444 | err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk, |
| 2445 | smp->rr, 0, cfm.confirm_val); |
| 2446 | if (err) |
| 2447 | return SMP_UNSPECIFIED; |
| 2448 | |
| 2449 | if (memcmp(cfm.confirm_val, smp->pcnf, 16)) |
| 2450 | return SMP_CONFIRM_FAILED; |
| 2451 | |
| 2452 | if (hcon->out) |
| 2453 | smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, |
| 2454 | sizeof(smp->prnd), smp->prnd); |
| 2455 | |
| 2456 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 2457 | |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 2461 | if (hcon->out) |
| 2462 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 2463 | |
| 2464 | if (smp->method == REQ_PASSKEY) { |
| 2465 | if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type, |
| 2466 | hcon->dst_type)) |
| 2467 | return SMP_UNSPECIFIED; |
| 2468 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM); |
| 2469 | set_bit(SMP_FLAG_WAIT_USER, &smp->flags); |
| 2470 | return 0; |
| 2471 | } |
| 2472 | |
Johan Hedberg | cbbbe3e | 2014-06-06 11:30:08 +0300 | [diff] [blame] | 2473 | /* The Initiating device waits for the non-initiating device to |
| 2474 | * send the confirm value. |
| 2475 | */ |
| 2476 | if (conn->hcon->out) |
| 2477 | return 0; |
| 2478 | |
| 2479 | err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, |
| 2480 | 0, cfm.confirm_val); |
| 2481 | if (err) |
| 2482 | return SMP_UNSPECIFIED; |
| 2483 | |
| 2484 | smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm); |
| 2485 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM); |
| 2486 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2487 | return 0; |
| 2488 | } |
| 2489 | |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame] | 2490 | static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2491 | { |
| 2492 | struct smp_cmd_dhkey_check *check = (void *) skb->data; |
| 2493 | struct l2cap_chan *chan = conn->smp; |
| 2494 | struct hci_conn *hcon = conn->hcon; |
| 2495 | struct smp_chan *smp = chan->data; |
| 2496 | u8 a[7], b[7], *local_addr, *remote_addr; |
| 2497 | u8 io_cap[3], r[16], e[16]; |
| 2498 | int err; |
| 2499 | |
| 2500 | BT_DBG("conn %p", conn); |
| 2501 | |
| 2502 | if (skb->len < sizeof(*check)) |
| 2503 | return SMP_INVALID_PARAMS; |
| 2504 | |
| 2505 | memcpy(a, &hcon->init_addr, 6); |
| 2506 | memcpy(b, &hcon->resp_addr, 6); |
| 2507 | a[6] = hcon->init_addr_type; |
| 2508 | b[6] = hcon->resp_addr_type; |
| 2509 | |
| 2510 | if (hcon->out) { |
| 2511 | local_addr = a; |
| 2512 | remote_addr = b; |
| 2513 | memcpy(io_cap, &smp->prsp[1], 3); |
| 2514 | } else { |
| 2515 | local_addr = b; |
| 2516 | remote_addr = a; |
| 2517 | memcpy(io_cap, &smp->preq[1], 3); |
| 2518 | } |
| 2519 | |
| 2520 | memset(r, 0, sizeof(r)); |
| 2521 | |
Johan Hedberg | 38606f1 | 2014-06-25 11:10:28 +0300 | [diff] [blame] | 2522 | if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) |
| 2523 | put_unaligned_le32(hcon->passkey_notify, r); |
| 2524 | |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame] | 2525 | err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r, |
| 2526 | io_cap, remote_addr, local_addr, e); |
| 2527 | if (err) |
| 2528 | return SMP_UNSPECIFIED; |
| 2529 | |
| 2530 | if (memcmp(check->e, e, 16)) |
| 2531 | return SMP_DHKEY_CHECK_FAILED; |
| 2532 | |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 2533 | if (!hcon->out) { |
| 2534 | if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) { |
| 2535 | set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags); |
| 2536 | return 0; |
| 2537 | } |
Johan Hedberg | d378a2d | 2014-05-31 18:53:36 +0300 | [diff] [blame] | 2538 | |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 2539 | /* Slave sends DHKey check as response to master */ |
| 2540 | sc_dhkey_check(smp); |
| 2541 | } |
Johan Hedberg | d378a2d | 2014-05-31 18:53:36 +0300 | [diff] [blame] | 2542 | |
Johan Hedberg | d3e54a8 | 2014-06-04 11:07:40 +0300 | [diff] [blame] | 2543 | sc_add_ltk(smp); |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame] | 2544 | |
| 2545 | if (hcon->out) { |
| 2546 | hci_le_start_enc(hcon, 0, 0, smp->tk); |
| 2547 | hcon->enc_key_size = smp->enc_key_size; |
| 2548 | } |
| 2549 | |
| 2550 | return 0; |
| 2551 | } |
| 2552 | |
Johan Hedberg | 1408bb6 | 2014-06-04 22:45:57 +0300 | [diff] [blame] | 2553 | static int smp_cmd_keypress_notify(struct l2cap_conn *conn, |
| 2554 | struct sk_buff *skb) |
| 2555 | { |
| 2556 | struct smp_cmd_keypress_notify *kp = (void *) skb->data; |
| 2557 | |
| 2558 | BT_DBG("value 0x%02x", kp->value); |
| 2559 | |
| 2560 | return 0; |
| 2561 | } |
| 2562 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2563 | static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb) |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2564 | { |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2565 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 7b9899d | 2013-10-03 00:00:57 -0700 | [diff] [blame] | 2566 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2567 | struct smp_chan *smp; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 2568 | __u8 code, reason; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2569 | int err = 0; |
| 2570 | |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 2571 | if (skb->len < 1) |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 2572 | return -EILSEQ; |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 2573 | |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 2574 | if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { |
Andre Guedes | 2e65c9d | 2011-06-30 19:20:56 -0300 | [diff] [blame] | 2575 | reason = SMP_PAIRING_NOTSUPP; |
| 2576 | goto done; |
| 2577 | } |
| 2578 | |
Marcel Holtmann | 92381f5 | 2013-10-03 01:23:08 -0700 | [diff] [blame] | 2579 | code = skb->data[0]; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2580 | skb_pull(skb, sizeof(code)); |
| 2581 | |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2582 | smp = chan->data; |
| 2583 | |
| 2584 | if (code > SMP_CMD_MAX) |
| 2585 | goto drop; |
| 2586 | |
Johan Hedberg | 24bd0bd | 2014-09-10 17:37:43 -0700 | [diff] [blame] | 2587 | if (smp && !test_and_clear_bit(code, &smp->allow_cmd)) |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2588 | goto drop; |
| 2589 | |
| 2590 | /* If we don't have a context the only allowed commands are |
| 2591 | * pairing request and security request. |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 2592 | */ |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2593 | if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ) |
| 2594 | goto drop; |
Johan Hedberg | 8cf9fa1 | 2013-01-29 10:44:23 -0600 | [diff] [blame] | 2595 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2596 | switch (code) { |
| 2597 | case SMP_CMD_PAIRING_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2598 | reason = smp_cmd_pairing_req(conn, skb); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2599 | break; |
| 2600 | |
| 2601 | case SMP_CMD_PAIRING_FAIL: |
Johan Hedberg | 84794e1 | 2013-11-06 11:24:57 +0200 | [diff] [blame] | 2602 | smp_failure(conn, 0); |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2603 | err = -EPERM; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2604 | break; |
| 2605 | |
| 2606 | case SMP_CMD_PAIRING_RSP: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2607 | reason = smp_cmd_pairing_rsp(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2608 | break; |
| 2609 | |
| 2610 | case SMP_CMD_SECURITY_REQ: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2611 | reason = smp_cmd_security_req(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2612 | break; |
| 2613 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2614 | case SMP_CMD_PAIRING_CONFIRM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2615 | reason = smp_cmd_pairing_confirm(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2616 | break; |
| 2617 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2618 | case SMP_CMD_PAIRING_RANDOM: |
Vinicius Costa Gomes | da85e5e | 2011-06-09 18:50:53 -0300 | [diff] [blame] | 2619 | reason = smp_cmd_pairing_random(conn, skb); |
Anderson Briglia | 88ba43b | 2011-06-09 18:50:42 -0300 | [diff] [blame] | 2620 | break; |
| 2621 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2622 | case SMP_CMD_ENCRYPT_INFO: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2623 | reason = smp_cmd_encrypt_info(conn, skb); |
| 2624 | break; |
| 2625 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2626 | case SMP_CMD_MASTER_IDENT: |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2627 | reason = smp_cmd_master_ident(conn, skb); |
| 2628 | break; |
| 2629 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2630 | case SMP_CMD_IDENT_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2631 | reason = smp_cmd_ident_info(conn, skb); |
| 2632 | break; |
| 2633 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2634 | case SMP_CMD_IDENT_ADDR_INFO: |
Johan Hedberg | fd349c0 | 2014-02-18 10:19:36 +0200 | [diff] [blame] | 2635 | reason = smp_cmd_ident_addr_info(conn, skb); |
| 2636 | break; |
| 2637 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2638 | case SMP_CMD_SIGN_INFO: |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 2639 | reason = smp_cmd_sign_info(conn, skb); |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2640 | break; |
| 2641 | |
Johan Hedberg | d8f8edb | 2014-06-06 11:09:28 +0300 | [diff] [blame] | 2642 | case SMP_CMD_PUBLIC_KEY: |
| 2643 | reason = smp_cmd_public_key(conn, skb); |
| 2644 | break; |
| 2645 | |
Johan Hedberg | 6433a9a | 2014-06-06 11:47:30 +0300 | [diff] [blame] | 2646 | case SMP_CMD_DHKEY_CHECK: |
| 2647 | reason = smp_cmd_dhkey_check(conn, skb); |
| 2648 | break; |
| 2649 | |
Johan Hedberg | 1408bb6 | 2014-06-04 22:45:57 +0300 | [diff] [blame] | 2650 | case SMP_CMD_KEYPRESS_NOTIFY: |
| 2651 | reason = smp_cmd_keypress_notify(conn, skb); |
| 2652 | break; |
| 2653 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2654 | default: |
| 2655 | BT_DBG("Unknown command code 0x%2.2x", code); |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2656 | reason = SMP_CMD_NOTSUPP; |
Vinicius Costa Gomes | 3a0259b | 2011-06-09 18:50:43 -0300 | [diff] [blame] | 2657 | goto done; |
| 2658 | } |
| 2659 | |
| 2660 | done: |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 2661 | if (!err) { |
| 2662 | if (reason) |
| 2663 | smp_failure(conn, reason); |
Johan Hedberg | 8ae9b98 | 2014-08-11 22:06:39 +0300 | [diff] [blame] | 2664 | kfree_skb(skb); |
Johan Hedberg | 9b7b18e | 2014-08-18 20:33:31 +0300 | [diff] [blame] | 2665 | } |
| 2666 | |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2667 | return err; |
Johan Hedberg | b28b494 | 2014-09-05 22:19:55 +0300 | [diff] [blame] | 2668 | |
| 2669 | drop: |
| 2670 | BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name, |
| 2671 | code, &hcon->dst); |
| 2672 | kfree_skb(skb); |
| 2673 | return 0; |
Anderson Briglia | eb492e0 | 2011-06-09 18:50:40 -0300 | [diff] [blame] | 2674 | } |
Vinicius Costa Gomes | 7034b91 | 2011-07-07 18:59:34 -0300 | [diff] [blame] | 2675 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2676 | static void smp_teardown_cb(struct l2cap_chan *chan, int err) |
| 2677 | { |
| 2678 | struct l2cap_conn *conn = chan->conn; |
| 2679 | |
| 2680 | BT_DBG("chan %p", chan); |
| 2681 | |
Johan Hedberg | fc75cc8 | 2014-09-05 22:19:52 +0300 | [diff] [blame] | 2682 | if (chan->data) |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2683 | smp_chan_destroy(conn); |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2684 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2685 | conn->smp = NULL; |
| 2686 | l2cap_chan_put(chan); |
| 2687 | } |
| 2688 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 2689 | static void bredr_pairing(struct l2cap_chan *chan) |
| 2690 | { |
| 2691 | struct l2cap_conn *conn = chan->conn; |
| 2692 | struct hci_conn *hcon = conn->hcon; |
| 2693 | struct hci_dev *hdev = hcon->hdev; |
| 2694 | struct smp_cmd_pairing req; |
| 2695 | struct smp_chan *smp; |
| 2696 | |
| 2697 | BT_DBG("chan %p", chan); |
| 2698 | |
| 2699 | /* Only new pairings are interesting */ |
| 2700 | if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags)) |
| 2701 | return; |
| 2702 | |
| 2703 | /* Don't bother if we're not encrypted */ |
| 2704 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
| 2705 | return; |
| 2706 | |
| 2707 | /* Only master may initiate SMP over BR/EDR */ |
| 2708 | if (hcon->role != HCI_ROLE_MASTER) |
| 2709 | return; |
| 2710 | |
| 2711 | /* Secure Connections support must be enabled */ |
| 2712 | if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 2713 | return; |
| 2714 | |
| 2715 | /* BR/EDR must use Secure Connections for SMP */ |
| 2716 | if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) && |
| 2717 | !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags)) |
| 2718 | return; |
| 2719 | |
| 2720 | /* If our LE support is not enabled don't do anything */ |
| 2721 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 2722 | return; |
| 2723 | |
| 2724 | /* Don't bother if remote LE support is not enabled */ |
| 2725 | if (!lmp_host_le_capable(hcon)) |
| 2726 | return; |
| 2727 | |
| 2728 | /* Remote must support SMP fixed chan for BR/EDR */ |
| 2729 | if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR)) |
| 2730 | return; |
| 2731 | |
| 2732 | /* Don't bother if SMP is already ongoing */ |
| 2733 | if (chan->data) |
| 2734 | return; |
| 2735 | |
| 2736 | smp = smp_chan_create(conn); |
| 2737 | if (!smp) { |
| 2738 | BT_ERR("%s unable to create SMP context for BR/EDR", |
| 2739 | hdev->name); |
| 2740 | return; |
| 2741 | } |
| 2742 | |
| 2743 | set_bit(SMP_FLAG_SC, &smp->flags); |
| 2744 | |
| 2745 | BT_DBG("%s starting SMP over BR/EDR", hdev->name); |
| 2746 | |
| 2747 | /* Prepare and send the BR/EDR SMP Pairing Request */ |
| 2748 | build_bredr_pairing_cmd(smp, &req, NULL); |
| 2749 | |
| 2750 | smp->preq[0] = SMP_CMD_PAIRING_REQ; |
| 2751 | memcpy(&smp->preq[1], &req, sizeof(req)); |
| 2752 | |
| 2753 | smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req); |
| 2754 | SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP); |
| 2755 | } |
| 2756 | |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2757 | static void smp_resume_cb(struct l2cap_chan *chan) |
| 2758 | { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2759 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2760 | struct l2cap_conn *conn = chan->conn; |
| 2761 | struct hci_conn *hcon = conn->hcon; |
| 2762 | |
| 2763 | BT_DBG("chan %p", chan); |
| 2764 | |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 2765 | if (hcon->type == ACL_LINK) { |
| 2766 | bredr_pairing(chan); |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2767 | return; |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 2768 | } |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2769 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 2770 | if (!smp) |
| 2771 | return; |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2772 | |
Johan Hedberg | 84bc0db | 2014-09-05 22:19:49 +0300 | [diff] [blame] | 2773 | if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
| 2774 | return; |
| 2775 | |
Johan Hedberg | 86d1407 | 2014-08-11 22:06:43 +0300 | [diff] [blame] | 2776 | cancel_delayed_work(&smp->security_timer); |
| 2777 | |
Johan Hedberg | d6268e8 | 2014-09-05 22:19:51 +0300 | [diff] [blame] | 2778 | smp_distribute_keys(smp); |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2779 | } |
| 2780 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2781 | static void smp_ready_cb(struct l2cap_chan *chan) |
| 2782 | { |
| 2783 | struct l2cap_conn *conn = chan->conn; |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 2784 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2785 | |
| 2786 | BT_DBG("chan %p", chan); |
| 2787 | |
| 2788 | conn->smp = chan; |
| 2789 | l2cap_chan_hold(chan); |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 2790 | |
| 2791 | if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags)) |
| 2792 | bredr_pairing(chan); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2793 | } |
| 2794 | |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2795 | static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
| 2796 | { |
| 2797 | int err; |
| 2798 | |
| 2799 | BT_DBG("chan %p", chan); |
| 2800 | |
| 2801 | err = smp_sig_channel(chan, skb); |
| 2802 | if (err) { |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2803 | struct smp_chan *smp = chan->data; |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2804 | |
Johan Hedberg | b68fda6 | 2014-08-11 22:06:40 +0300 | [diff] [blame] | 2805 | if (smp) |
| 2806 | cancel_delayed_work_sync(&smp->security_timer); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2807 | |
Johan Hedberg | 1e91c29 | 2014-08-18 20:33:29 +0300 | [diff] [blame] | 2808 | hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE); |
Johan Hedberg | 4befb86 | 2014-08-11 22:06:38 +0300 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | return err; |
| 2812 | } |
| 2813 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2814 | static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan, |
| 2815 | unsigned long hdr_len, |
| 2816 | unsigned long len, int nb) |
| 2817 | { |
| 2818 | struct sk_buff *skb; |
| 2819 | |
| 2820 | skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL); |
| 2821 | if (!skb) |
| 2822 | return ERR_PTR(-ENOMEM); |
| 2823 | |
| 2824 | skb->priority = HCI_PRIO_MAX; |
| 2825 | bt_cb(skb)->chan = chan; |
| 2826 | |
| 2827 | return skb; |
| 2828 | } |
| 2829 | |
| 2830 | static const struct l2cap_ops smp_chan_ops = { |
| 2831 | .name = "Security Manager", |
| 2832 | .ready = smp_ready_cb, |
Johan Hedberg | 5d88cc7 | 2014-08-08 09:37:18 +0300 | [diff] [blame] | 2833 | .recv = smp_recv_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2834 | .alloc_skb = smp_alloc_skb_cb, |
| 2835 | .teardown = smp_teardown_cb, |
Johan Hedberg | 44f1a7a | 2014-08-11 22:06:36 +0300 | [diff] [blame] | 2836 | .resume = smp_resume_cb, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2837 | |
| 2838 | .new_connection = l2cap_chan_no_new_connection, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2839 | .state_change = l2cap_chan_no_state_change, |
| 2840 | .close = l2cap_chan_no_close, |
| 2841 | .defer = l2cap_chan_no_defer, |
| 2842 | .suspend = l2cap_chan_no_suspend, |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2843 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 2844 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 2845 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 2846 | }; |
| 2847 | |
| 2848 | static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan) |
| 2849 | { |
| 2850 | struct l2cap_chan *chan; |
| 2851 | |
| 2852 | BT_DBG("pchan %p", pchan); |
| 2853 | |
| 2854 | chan = l2cap_chan_create(); |
| 2855 | if (!chan) |
| 2856 | return NULL; |
| 2857 | |
| 2858 | chan->chan_type = pchan->chan_type; |
| 2859 | chan->ops = &smp_chan_ops; |
| 2860 | chan->scid = pchan->scid; |
| 2861 | chan->dcid = chan->scid; |
| 2862 | chan->imtu = pchan->imtu; |
| 2863 | chan->omtu = pchan->omtu; |
| 2864 | chan->mode = pchan->mode; |
| 2865 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 2866 | /* Other L2CAP channels may request SMP routines in order to |
| 2867 | * change the security level. This means that the SMP channel |
| 2868 | * lock must be considered in its own category to avoid lockdep |
| 2869 | * warnings. |
| 2870 | */ |
| 2871 | atomic_set(&chan->nesting, L2CAP_NESTING_SMP); |
| 2872 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2873 | BT_DBG("created chan %p", chan); |
| 2874 | |
| 2875 | return chan; |
| 2876 | } |
| 2877 | |
| 2878 | static const struct l2cap_ops smp_root_chan_ops = { |
| 2879 | .name = "Security Manager Root", |
| 2880 | .new_connection = smp_new_conn_cb, |
| 2881 | |
| 2882 | /* None of these are implemented for the root channel */ |
| 2883 | .close = l2cap_chan_no_close, |
| 2884 | .alloc_skb = l2cap_chan_no_alloc_skb, |
| 2885 | .recv = l2cap_chan_no_recv, |
| 2886 | .state_change = l2cap_chan_no_state_change, |
| 2887 | .teardown = l2cap_chan_no_teardown, |
| 2888 | .ready = l2cap_chan_no_ready, |
| 2889 | .defer = l2cap_chan_no_defer, |
| 2890 | .suspend = l2cap_chan_no_suspend, |
| 2891 | .resume = l2cap_chan_no_resume, |
| 2892 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 2893 | .get_sndtimeo = l2cap_chan_no_get_sndtimeo, |
| 2894 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 2895 | }; |
| 2896 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2897 | static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid) |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2898 | { |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2899 | struct l2cap_chan *chan; |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2900 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2901 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2902 | if (cid == L2CAP_CID_SMP_BREDR) { |
| 2903 | tfm_aes = NULL; |
| 2904 | goto create_chan; |
| 2905 | } |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2906 | |
Johan Hedberg | adae20c | 2014-11-13 14:37:48 +0200 | [diff] [blame] | 2907 | tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0); |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2908 | if (IS_ERR(tfm_aes)) { |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2909 | BT_ERR("Unable to create crypto context"); |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2910 | return ERR_PTR(PTR_ERR(tfm_aes)); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2911 | } |
| 2912 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2913 | create_chan: |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2914 | chan = l2cap_chan_create(); |
| 2915 | if (!chan) { |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2916 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2917 | return ERR_PTR(-ENOMEM); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2918 | } |
| 2919 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2920 | chan->data = tfm_aes; |
| 2921 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2922 | l2cap_add_scid(chan, cid); |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2923 | |
| 2924 | l2cap_chan_set_defaults(chan); |
| 2925 | |
| 2926 | bacpy(&chan->src, &hdev->bdaddr); |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2927 | if (cid == L2CAP_CID_SMP) |
| 2928 | chan->src_type = BDADDR_LE_PUBLIC; |
| 2929 | else |
| 2930 | chan->src_type = BDADDR_BREDR; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2931 | chan->state = BT_LISTEN; |
| 2932 | chan->mode = L2CAP_MODE_BASIC; |
| 2933 | chan->imtu = L2CAP_DEFAULT_MTU; |
| 2934 | chan->ops = &smp_root_chan_ops; |
| 2935 | |
Johan Hedberg | abe8490 | 2014-11-12 22:22:21 +0200 | [diff] [blame] | 2936 | /* Set correct nesting level for a parent/listening channel */ |
| 2937 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
| 2938 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2939 | return chan; |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2940 | } |
| 2941 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2942 | static void smp_del_chan(struct l2cap_chan *chan) |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2943 | { |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2944 | struct crypto_blkcipher *tfm_aes; |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2945 | |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2946 | BT_DBG("chan %p", chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2947 | |
Johan Hedberg | defce9e | 2014-08-08 09:37:17 +0300 | [diff] [blame] | 2948 | tfm_aes = chan->data; |
| 2949 | if (tfm_aes) { |
| 2950 | chan->data = NULL; |
| 2951 | crypto_free_blkcipher(tfm_aes); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2952 | } |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2953 | |
Johan Hedberg | 70db83c | 2014-08-08 09:37:16 +0300 | [diff] [blame] | 2954 | l2cap_chan_put(chan); |
Johan Hedberg | 711eafe | 2014-08-08 09:32:52 +0300 | [diff] [blame] | 2955 | } |
Johan Hedberg | ef8efe4 | 2014-08-13 15:12:32 +0300 | [diff] [blame] | 2956 | |
| 2957 | int smp_register(struct hci_dev *hdev) |
| 2958 | { |
| 2959 | struct l2cap_chan *chan; |
| 2960 | |
| 2961 | BT_DBG("%s", hdev->name); |
| 2962 | |
| 2963 | chan = smp_add_cid(hdev, L2CAP_CID_SMP); |
| 2964 | if (IS_ERR(chan)) |
| 2965 | return PTR_ERR(chan); |
| 2966 | |
| 2967 | hdev->smp_data = chan; |
| 2968 | |
| 2969 | if (!lmp_sc_capable(hdev) && |
| 2970 | !test_bit(HCI_FORCE_LESC, &hdev->dbg_flags)) |
| 2971 | return 0; |
| 2972 | |
| 2973 | chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR); |
| 2974 | if (IS_ERR(chan)) { |
| 2975 | int err = PTR_ERR(chan); |
| 2976 | chan = hdev->smp_data; |
| 2977 | hdev->smp_data = NULL; |
| 2978 | smp_del_chan(chan); |
| 2979 | return err; |
| 2980 | } |
| 2981 | |
| 2982 | hdev->smp_bredr_data = chan; |
| 2983 | |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | void smp_unregister(struct hci_dev *hdev) |
| 2988 | { |
| 2989 | struct l2cap_chan *chan; |
| 2990 | |
| 2991 | if (hdev->smp_bredr_data) { |
| 2992 | chan = hdev->smp_bredr_data; |
| 2993 | hdev->smp_bredr_data = NULL; |
| 2994 | smp_del_chan(chan); |
| 2995 | } |
| 2996 | |
| 2997 | if (hdev->smp_data) { |
| 2998 | chan = hdev->smp_data; |
| 2999 | hdev->smp_data = NULL; |
| 3000 | smp_del_chan(chan); |
| 3001 | } |
| 3002 | } |