Bluetooth: Move SMP fields to a separate structure
[deliverable/linux.git] / net / bluetooth / smp.c
CommitLineData
eb492e01
AB
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
23#include <net/bluetooth/bluetooth.h>
24#include <net/bluetooth/hci_core.h>
25#include <net/bluetooth/l2cap.h>
26#include <net/bluetooth/smp.h>
d22ef0bc 27#include <linux/crypto.h>
f70490e6 28#include <linux/scatterlist.h>
d22ef0bc
AB
29#include <crypto/b128ops.h>
30
5d3de7df
VCG
31#define SMP_TIMEOUT 30000 /* 30 seconds */
32
d22ef0bc
AB
33static inline void swap128(u8 src[16], u8 dst[16])
34{
35 int i;
36 for (i = 0; i < 16; i++)
37 dst[15 - i] = src[i];
38}
39
40static inline void swap56(u8 src[7], u8 dst[7])
41{
42 int i;
43 for (i = 0; i < 7; i++)
44 dst[6 - i] = src[i];
45}
46
47static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
48{
49 struct blkcipher_desc desc;
50 struct scatterlist sg;
51 int err, iv_len;
52 unsigned char iv[128];
53
54 if (tfm == NULL) {
55 BT_ERR("tfm %p", tfm);
56 return -EINVAL;
57 }
58
59 desc.tfm = tfm;
60 desc.flags = 0;
61
62 err = crypto_blkcipher_setkey(tfm, k, 16);
63 if (err) {
64 BT_ERR("cipher setkey failed: %d", err);
65 return err;
66 }
67
68 sg_init_one(&sg, r, 16);
69
70 iv_len = crypto_blkcipher_ivsize(tfm);
71 if (iv_len) {
72 memset(&iv, 0xff, iv_len);
73 crypto_blkcipher_set_iv(tfm, iv, iv_len);
74 }
75
76 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
77 if (err)
78 BT_ERR("Encrypt data error %d", err);
79
80 return err;
81}
82
83static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
84 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
85 u8 _rat, bdaddr_t *ra, u8 res[16])
86{
87 u8 p1[16], p2[16];
88 int err;
89
90 memset(p1, 0, 16);
91
92 /* p1 = pres || preq || _rat || _iat */
93 swap56(pres, p1);
94 swap56(preq, p1 + 7);
95 p1[14] = _rat;
96 p1[15] = _iat;
97
98 memset(p2, 0, 16);
99
100 /* p2 = padding || ia || ra */
101 baswap((bdaddr_t *) (p2 + 4), ia);
102 baswap((bdaddr_t *) (p2 + 10), ra);
103
104 /* res = r XOR p1 */
105 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
106
107 /* res = e(k, res) */
108 err = smp_e(tfm, k, res);
109 if (err) {
110 BT_ERR("Encrypt data error");
111 return err;
112 }
113
114 /* res = res XOR p2 */
115 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
116
117 /* res = e(k, res) */
118 err = smp_e(tfm, k, res);
119 if (err)
120 BT_ERR("Encrypt data error");
121
122 return err;
123}
124
125static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16],
126 u8 r1[16], u8 r2[16], u8 _r[16])
127{
128 int err;
129
130 /* Just least significant octets from r1 and r2 are considered */
131 memcpy(_r, r1 + 8, 8);
132 memcpy(_r + 8, r2 + 8, 8);
133
134 err = smp_e(tfm, k, _r);
135 if (err)
136 BT_ERR("Encrypt data error");
137
138 return err;
139}
140
141static int smp_rand(u8 *buf)
142{
143 get_random_bytes(buf, 16);
144
145 return 0;
146}
eb492e01
AB
147
148static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
149 u16 dlen, void *data)
150{
151 struct sk_buff *skb;
152 struct l2cap_hdr *lh;
153 int len;
154
155 len = L2CAP_HDR_SIZE + sizeof(code) + dlen;
156
157 if (len > conn->mtu)
158 return NULL;
159
160 skb = bt_skb_alloc(len, GFP_ATOMIC);
161 if (!skb)
162 return NULL;
163
164 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
165 lh->len = cpu_to_le16(sizeof(code) + dlen);
166 lh->cid = cpu_to_le16(L2CAP_CID_SMP);
167
168 memcpy(skb_put(skb, sizeof(code)), &code, sizeof(code));
169
170 memcpy(skb_put(skb, dlen), data, dlen);
171
172 return skb;
173}
174
175static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
176{
177 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
178
179 BT_DBG("code 0x%2.2x", code);
180
181 if (!skb)
182 return;
183
184 hci_send_acl(conn->hcon, skb, 0);
e2dcd113
VCG
185
186 mod_timer(&conn->security_timer, jiffies +
187 msecs_to_jiffies(SMP_TIMEOUT));
eb492e01
AB
188}
189
da85e5e5
VCG
190static __u8 seclevel_to_authreq(__u8 level)
191{
192 switch (level) {
193 case BT_SECURITY_HIGH:
194 /* Right now we don't support bonding */
195 return SMP_AUTH_MITM;
196
197 default:
198 return SMP_AUTH_NONE;
199 }
200}
201
b8e66eac 202static void build_pairing_cmd(struct l2cap_conn *conn,
54790f73
VCG
203 struct smp_cmd_pairing *req,
204 struct smp_cmd_pairing *rsp,
205 __u8 authreq)
b8e66eac 206{
54790f73
VCG
207 u8 dist_keys;
208
209 dist_keys = 0;
210 if (test_bit(HCI_PAIRABLE, &conn->hcon->hdev->flags)) {
211 dist_keys = SMP_DIST_ENC_KEY | SMP_DIST_ID_KEY | SMP_DIST_SIGN;
212 authreq |= SMP_AUTH_BONDING;
213 }
214
215 if (rsp == NULL) {
216 req->io_capability = conn->hcon->io_capability;
217 req->oob_flag = SMP_OOB_NOT_PRESENT;
218 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
219 req->init_key_dist = dist_keys;
220 req->resp_key_dist = dist_keys;
221 req->auth_req = authreq;
222 return;
223 }
224
225 rsp->io_capability = conn->hcon->io_capability;
226 rsp->oob_flag = SMP_OOB_NOT_PRESENT;
227 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
228 rsp->init_key_dist = req->init_key_dist & dist_keys;
229 rsp->resp_key_dist = req->resp_key_dist & dist_keys;
230 rsp->auth_req = authreq;
b8e66eac
VCG
231}
232
3158c50c
VCG
233static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
234{
1c1def09
VCG
235 struct smp_chan *smp = conn->smp_chan;
236
3158c50c
VCG
237 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
238 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
239 return SMP_ENC_KEY_SIZE;
240
1c1def09 241 smp->smp_key_size = max_key_size;
3158c50c
VCG
242
243 return 0;
244}
245
da85e5e5 246static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 247{
3158c50c 248 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
1c1def09 249 struct smp_chan *smp = conn->smp_chan;
3158c50c 250 u8 key_size;
88ba43b6
AB
251
252 BT_DBG("conn %p", conn);
253
d26a2345
VCG
254 if (!test_and_set_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
255 hci_conn_hold(conn->hcon);
256
1c1def09
VCG
257 smp->preq[0] = SMP_CMD_PAIRING_REQ;
258 memcpy(&smp->preq[1], req, sizeof(*req));
3158c50c 259 skb_pull(skb, sizeof(*req));
88ba43b6 260
3158c50c 261 if (req->oob_flag)
da85e5e5
VCG
262 return SMP_OOB_NOT_AVAIL;
263
264 /* We didn't start the pairing, so no requirements */
54790f73 265 build_pairing_cmd(conn, req, &rsp, SMP_AUTH_NONE);
3158c50c
VCG
266
267 key_size = min(req->max_key_size, rsp.max_key_size);
268 if (check_enc_key_size(conn, key_size))
269 return SMP_ENC_KEY_SIZE;
88ba43b6 270
7d24ddcc 271 /* Just works */
1c1def09 272 memset(smp->tk, 0, sizeof(smp->tk));
7d24ddcc 273
1c1def09
VCG
274 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
275 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
f01ead31 276
3158c50c 277 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
da85e5e5
VCG
278
279 return 0;
88ba43b6
AB
280}
281
da85e5e5 282static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 283{
3158c50c 284 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
88ba43b6 285 struct smp_cmd_pairing_confirm cp;
1c1def09
VCG
286 struct smp_chan *smp = conn->smp_chan;
287 struct crypto_blkcipher *tfm = smp->tfm;
288
7d24ddcc 289 int ret;
3158c50c 290 u8 res[16], key_size;
88ba43b6
AB
291
292 BT_DBG("conn %p", conn);
293
3158c50c
VCG
294 skb_pull(skb, sizeof(*rsp));
295
1c1def09 296 req = (void *) &smp->preq[1];
da85e5e5 297
3158c50c
VCG
298 key_size = min(req->max_key_size, rsp->max_key_size);
299 if (check_enc_key_size(conn, key_size))
300 return SMP_ENC_KEY_SIZE;
301
302 if (rsp->oob_flag)
da85e5e5
VCG
303 return SMP_OOB_NOT_AVAIL;
304
7d24ddcc 305 /* Just works */
1c1def09 306 memset(smp->tk, 0, sizeof(smp->tk));
88ba43b6 307
1c1def09
VCG
308 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
309 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
f01ead31 310
1c1def09 311 ret = smp_rand(smp->prnd);
7d24ddcc 312 if (ret)
da85e5e5 313 return SMP_UNSPECIFIED;
7d24ddcc 314
1c1def09 315 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0,
7d24ddcc
AB
316 conn->src, conn->hcon->dst_type, conn->dst, res);
317 if (ret)
da85e5e5 318 return SMP_UNSPECIFIED;
7d24ddcc
AB
319
320 swap128(res, cp.confirm_val);
321
88ba43b6 322 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
da85e5e5
VCG
323
324 return 0;
88ba43b6
AB
325}
326
da85e5e5 327static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 328{
1c1def09
VCG
329 struct smp_chan *smp = conn->smp_chan;
330 struct crypto_blkcipher *tfm = smp->tfm;
7d24ddcc 331
88ba43b6
AB
332 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
333
1c1def09
VCG
334 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
335 skb_pull(skb, sizeof(smp->pcnf));
88ba43b6 336
7d24ddcc
AB
337 if (conn->hcon->out) {
338 u8 random[16];
88ba43b6 339
1c1def09 340 swap128(smp->prnd, random);
88ba43b6 341 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
7d24ddcc 342 random);
88ba43b6 343 } else {
7d24ddcc
AB
344 struct smp_cmd_pairing_confirm cp;
345 int ret;
346 u8 res[16];
88ba43b6 347
1c1def09 348 ret = smp_rand(smp->prnd);
7d24ddcc 349 if (ret)
da85e5e5 350 return SMP_UNSPECIFIED;
88ba43b6 351
1c1def09 352 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
7d24ddcc
AB
353 conn->hcon->dst_type, conn->dst,
354 0, conn->src, res);
355 if (ret)
da85e5e5 356 return SMP_CONFIRM_FAILED;
7d24ddcc
AB
357
358 swap128(res, cp.confirm_val);
359
360 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
88ba43b6 361 }
da85e5e5
VCG
362
363 return 0;
88ba43b6
AB
364}
365
da85e5e5 366static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6 367{
a7a595f6 368 struct hci_conn *hcon = conn->hcon;
1c1def09
VCG
369 struct smp_chan *smp = conn->smp_chan;
370 struct crypto_blkcipher *tfm = smp->tfm;
7d24ddcc 371 int ret;
9b3d6740 372 u8 key[16], res[16], random[16], confirm[16];
7d24ddcc
AB
373
374 swap128(skb->data, random);
375 skb_pull(skb, sizeof(random));
376
377 if (conn->hcon->out)
1c1def09 378 ret = smp_c1(tfm, smp->tk, random, smp->preq, smp->prsp, 0,
7d24ddcc
AB
379 conn->src, conn->hcon->dst_type, conn->dst,
380 res);
381 else
1c1def09 382 ret = smp_c1(tfm, smp->tk, random, smp->preq, smp->prsp,
7d24ddcc
AB
383 conn->hcon->dst_type, conn->dst, 0, conn->src,
384 res);
385 if (ret)
da85e5e5 386 return SMP_UNSPECIFIED;
88ba43b6
AB
387
388 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
389
7d24ddcc
AB
390 swap128(res, confirm);
391
1c1def09 392 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
7d24ddcc 393 BT_ERR("Pairing failed (confirmation values mismatch)");
da85e5e5 394 return SMP_CONFIRM_FAILED;
7d24ddcc 395 }
88ba43b6
AB
396
397 if (conn->hcon->out) {
e7e62c85 398 u8 stk[16], rand[8];
a7a595f6 399 __le16 ediv;
e7e62c85
VCG
400
401 memset(rand, 0, sizeof(rand));
402 ediv = 0;
a7a595f6 403
1c1def09 404 smp_s1(tfm, smp->tk, random, smp->prnd, key);
e7e62c85 405 swap128(key, stk);
7d24ddcc 406
1c1def09
VCG
407 memset(stk + smp->smp_key_size, 0,
408 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
3158c50c 409
d26a2345
VCG
410 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->pend))
411 return SMP_UNSPECIFIED;
412
e7e62c85 413 hci_le_start_enc(hcon, ediv, rand, stk);
1c1def09 414 hcon->enc_key_size = smp->smp_key_size;
e7e62c85
VCG
415 } else {
416 u8 stk[16], r[16], rand[8];
417 __le16 ediv;
418
a7a595f6
VCG
419 memset(rand, 0, sizeof(rand));
420 ediv = 0;
7d24ddcc 421
1c1def09 422 swap128(smp->prnd, r);
7d24ddcc
AB
423 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(r), r);
424
1c1def09 425 smp_s1(tfm, smp->tk, smp->prnd, random, key);
e7e62c85 426 swap128(key, stk);
3158c50c 427
1c1def09
VCG
428 memset(stk + smp->smp_key_size, 0,
429 SMP_MAX_ENC_KEY_SIZE - smp->smp_key_size);
e7e62c85 430
1c1def09 431 hci_add_ltk(conn->hcon->hdev, 0, conn->dst, smp->smp_key_size,
726b4ffc 432 ediv, rand, stk);
88ba43b6 433 }
da85e5e5
VCG
434
435 return 0;
88ba43b6
AB
436}
437
da85e5e5 438static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
88ba43b6
AB
439{
440 struct smp_cmd_security_req *rp = (void *) skb->data;
441 struct smp_cmd_pairing cp;
f1cb9af5 442 struct hci_conn *hcon = conn->hcon;
1c1def09 443 struct smp_chan *smp = conn->smp_chan;
88ba43b6
AB
444
445 BT_DBG("conn %p", conn);
446
d26a2345 447 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
da85e5e5 448 return 0;
f1cb9af5 449
d26a2345
VCG
450 hci_conn_hold(hcon);
451
88ba43b6 452 skb_pull(skb, sizeof(*rp));
88ba43b6 453
da85e5e5 454 memset(&cp, 0, sizeof(cp));
54790f73 455 build_pairing_cmd(conn, &cp, NULL, rp->auth_req);
88ba43b6 456
1c1def09
VCG
457 smp->preq[0] = SMP_CMD_PAIRING_REQ;
458 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 459
88ba43b6 460 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
f1cb9af5 461
da85e5e5 462 return 0;
88ba43b6
AB
463}
464
eb492e01
AB
465int smp_conn_security(struct l2cap_conn *conn, __u8 sec_level)
466{
3a0259bb 467 struct hci_conn *hcon = conn->hcon;
1c1def09 468 struct smp_chan *smp = conn->smp_chan;
eb492e01
AB
469 __u8 authreq;
470
3a0259bb
VCG
471 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
472
2e65c9d2
AG
473 if (!lmp_host_le_capable(hcon->hdev))
474 return 1;
475
f1cb9af5
VCG
476 if (sec_level == BT_SECURITY_LOW)
477 return 1;
eb492e01 478
f1cb9af5 479 if (hcon->sec_level >= sec_level)
eb492e01 480 return 1;
f1cb9af5 481
3a0259bb 482 if (hcon->link_mode & HCI_LM_MASTER) {
02bc7455
VCG
483 struct link_key *key;
484
485 key = hci_find_link_key_type(hcon->hdev, conn->dst,
486 HCI_LK_SMP_LTK);
487 if (key) {
488 struct key_master_id *master = (void *) key->data;
489
d26a2345
VCG
490 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND,
491 &hcon->pend))
492 goto done;
493
02bc7455
VCG
494 hci_le_start_enc(hcon, master->ediv, master->rand,
495 key->val);
726b4ffc
VCG
496 hcon->enc_key_size = key->pin_len;
497
02bc7455
VCG
498 goto done;
499 }
d26a2345
VCG
500 }
501
502 if (test_and_set_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend))
503 return 0;
504
505 /* While SMP is going on */
506 hci_conn_hold(hcon);
507
508 authreq = seclevel_to_authreq(sec_level);
509
510 if (hcon->link_mode & HCI_LM_MASTER) {
511 struct smp_cmd_pairing cp;
f01ead31 512
54790f73 513 build_pairing_cmd(conn, &cp, NULL, authreq);
1c1def09
VCG
514 smp->preq[0] = SMP_CMD_PAIRING_REQ;
515 memcpy(&smp->preq[1], &cp, sizeof(cp));
f01ead31 516
eb492e01
AB
517 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
518 } else {
519 struct smp_cmd_security_req cp;
520 cp.auth_req = authreq;
521 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
522 }
523
02bc7455 524done:
f1cb9af5 525 hcon->pending_sec_level = sec_level;
f1cb9af5 526
eb492e01
AB
527 return 0;
528}
529
7034b911
VCG
530static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
531{
16b90839 532 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
1c1def09 533 struct smp_chan *smp = conn->smp_chan;
16b90839
VCG
534
535 skb_pull(skb, sizeof(*rp));
536
1c1def09 537 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
16b90839 538
7034b911
VCG
539 return 0;
540}
541
542static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
543{
16b90839 544 struct smp_cmd_master_ident *rp = (void *) skb->data;
1c1def09 545 struct smp_chan *smp = conn->smp_chan;
16b90839
VCG
546
547 skb_pull(skb, sizeof(*rp));
7034b911 548
1c1def09
VCG
549 hci_add_ltk(conn->hcon->hdev, 1, conn->src, smp->smp_key_size,
550 rp->ediv, rp->rand, smp->tk);
7034b911
VCG
551
552 smp_distribute_keys(conn, 1);
553
554 return 0;
555}
556
eb492e01
AB
557int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
558{
559 __u8 code = skb->data[0];
560 __u8 reason;
561 int err = 0;
562
2e65c9d2
AG
563 if (!lmp_host_le_capable(conn->hcon->hdev)) {
564 err = -ENOTSUPP;
565 reason = SMP_PAIRING_NOTSUPP;
566 goto done;
567 }
568
eb492e01
AB
569 skb_pull(skb, sizeof(code));
570
571 switch (code) {
572 case SMP_CMD_PAIRING_REQ:
da85e5e5 573 reason = smp_cmd_pairing_req(conn, skb);
eb492e01
AB
574 break;
575
576 case SMP_CMD_PAIRING_FAIL:
da85e5e5
VCG
577 reason = 0;
578 err = -EPERM;
eb492e01
AB
579 break;
580
581 case SMP_CMD_PAIRING_RSP:
da85e5e5 582 reason = smp_cmd_pairing_rsp(conn, skb);
88ba43b6
AB
583 break;
584
585 case SMP_CMD_SECURITY_REQ:
da85e5e5 586 reason = smp_cmd_security_req(conn, skb);
88ba43b6
AB
587 break;
588
eb492e01 589 case SMP_CMD_PAIRING_CONFIRM:
da85e5e5 590 reason = smp_cmd_pairing_confirm(conn, skb);
88ba43b6
AB
591 break;
592
eb492e01 593 case SMP_CMD_PAIRING_RANDOM:
da85e5e5 594 reason = smp_cmd_pairing_random(conn, skb);
88ba43b6
AB
595 break;
596
eb492e01 597 case SMP_CMD_ENCRYPT_INFO:
7034b911
VCG
598 reason = smp_cmd_encrypt_info(conn, skb);
599 break;
600
eb492e01 601 case SMP_CMD_MASTER_IDENT:
7034b911
VCG
602 reason = smp_cmd_master_ident(conn, skb);
603 break;
604
eb492e01
AB
605 case SMP_CMD_IDENT_INFO:
606 case SMP_CMD_IDENT_ADDR_INFO:
607 case SMP_CMD_SIGN_INFO:
7034b911
VCG
608 /* Just ignored */
609 reason = 0;
610 break;
611
eb492e01
AB
612 default:
613 BT_DBG("Unknown command code 0x%2.2x", code);
614
615 reason = SMP_CMD_NOTSUPP;
eb492e01 616 err = -EOPNOTSUPP;
3a0259bb 617 goto done;
eb492e01
AB
618 }
619
3a0259bb
VCG
620done:
621 if (reason)
622 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
623 &reason);
624
eb492e01
AB
625 kfree_skb(skb);
626 return err;
627}
7034b911
VCG
628
629int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
630{
631 struct smp_cmd_pairing *req, *rsp;
1c1def09 632 struct smp_chan *smp = conn->smp_chan;
7034b911
VCG
633 __u8 *keydist;
634
635 BT_DBG("conn %p force %d", conn, force);
636
d26a2345
VCG
637 if (!test_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend))
638 return 0;
639
1c1def09 640 rsp = (void *) &smp->prsp[1];
7034b911
VCG
641
642 /* The responder sends its keys first */
643 if (!force && conn->hcon->out && (rsp->resp_key_dist & 0x07))
644 return 0;
645
1c1def09 646 req = (void *) &smp->preq[1];
7034b911
VCG
647
648 if (conn->hcon->out) {
649 keydist = &rsp->init_key_dist;
650 *keydist &= req->init_key_dist;
651 } else {
652 keydist = &rsp->resp_key_dist;
653 *keydist &= req->resp_key_dist;
654 }
655
656
657 BT_DBG("keydist 0x%x", *keydist);
658
659 if (*keydist & SMP_DIST_ENC_KEY) {
660 struct smp_cmd_encrypt_info enc;
661 struct smp_cmd_master_ident ident;
662 __le16 ediv;
663
664 get_random_bytes(enc.ltk, sizeof(enc.ltk));
665 get_random_bytes(&ediv, sizeof(ediv));
666 get_random_bytes(ident.rand, sizeof(ident.rand));
667
668 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
669
1c1def09 670 hci_add_ltk(conn->hcon->hdev, 1, conn->dst, smp->smp_key_size,
726b4ffc 671 ediv, ident.rand, enc.ltk);
16b90839 672
7034b911
VCG
673 ident.ediv = cpu_to_le16(ediv);
674
675 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
676
677 *keydist &= ~SMP_DIST_ENC_KEY;
678 }
679
680 if (*keydist & SMP_DIST_ID_KEY) {
681 struct smp_cmd_ident_addr_info addrinfo;
682 struct smp_cmd_ident_info idinfo;
683
684 /* Send a dummy key */
685 get_random_bytes(idinfo.irk, sizeof(idinfo.irk));
686
687 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
688
689 /* Just public address */
690 memset(&addrinfo, 0, sizeof(addrinfo));
691 bacpy(&addrinfo.bdaddr, conn->src);
692
693 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
694 &addrinfo);
695
696 *keydist &= ~SMP_DIST_ID_KEY;
697 }
698
699 if (*keydist & SMP_DIST_SIGN) {
700 struct smp_cmd_sign_info sign;
701
702 /* Send a dummy key */
703 get_random_bytes(sign.csrk, sizeof(sign.csrk));
704
705 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
706
707 *keydist &= ~SMP_DIST_SIGN;
708 }
709
d26a2345
VCG
710 if (conn->hcon->out || force) {
711 clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->pend);
712 del_timer(&conn->security_timer);
713 hci_conn_put(conn->hcon);
714 }
715
7034b911
VCG
716 return 0;
717}
This page took 0.165992 seconds and 5 git commands to generate.