blob: 2640d78f30b80080f5b240c8a25cf668392e2961 [file] [log] [blame]
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03001/*
2 Copyright (c) 2011,2012 Intel Corp.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*/
13
14#include <net/bluetooth/bluetooth.h>
15#include <net/bluetooth/hci.h>
16#include <net/bluetooth/hci_core.h>
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +030017#include <crypto/hash.h>
Andrei Emeltchenko903e4542012-09-27 17:26:09 +030018
Marcel Holtmann70247282013-10-10 14:54:15 -070019#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070020#include "amp.h"
21
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030022/* Remote AMP Controllers interface */
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +030023void amp_ctrl_get(struct amp_ctrl *ctrl)
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030024{
25 BT_DBG("ctrl %p orig refcnt %d", ctrl,
26 atomic_read(&ctrl->kref.refcount));
27
28 kref_get(&ctrl->kref);
29}
30
31static void amp_ctrl_destroy(struct kref *kref)
32{
33 struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
34
35 BT_DBG("ctrl %p", ctrl);
36
37 kfree(ctrl->assoc);
38 kfree(ctrl);
39}
40
41int amp_ctrl_put(struct amp_ctrl *ctrl)
42{
43 BT_DBG("ctrl %p orig refcnt %d", ctrl,
44 atomic_read(&ctrl->kref.refcount));
45
46 return kref_put(&ctrl->kref, &amp_ctrl_destroy);
47}
48
Andrei Emeltchenkofa4ebc62012-10-05 16:56:55 +030049struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id)
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030050{
51 struct amp_ctrl *ctrl;
52
53 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
54 if (!ctrl)
55 return NULL;
56
Andrei Emeltchenkofa4ebc62012-10-05 16:56:55 +030057 kref_init(&ctrl->kref);
58 ctrl->id = id;
59
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030060 mutex_lock(&mgr->amp_ctrls_lock);
61 list_add(&ctrl->list, &mgr->amp_ctrls);
62 mutex_unlock(&mgr->amp_ctrls_lock);
63
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030064 BT_DBG("mgr %p ctrl %p", mgr, ctrl);
65
66 return ctrl;
67}
68
69void amp_ctrl_list_flush(struct amp_mgr *mgr)
70{
71 struct amp_ctrl *ctrl, *n;
72
73 BT_DBG("mgr %p", mgr);
74
75 mutex_lock(&mgr->amp_ctrls_lock);
76 list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
77 list_del(&ctrl->list);
78 amp_ctrl_put(ctrl);
79 }
80 mutex_unlock(&mgr->amp_ctrls_lock);
81}
82
83struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
84{
85 struct amp_ctrl *ctrl;
86
87 BT_DBG("mgr %p id %d", mgr, id);
88
89 mutex_lock(&mgr->amp_ctrls_lock);
90 list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
91 if (ctrl->id == id) {
92 amp_ctrl_get(ctrl);
93 mutex_unlock(&mgr->amp_ctrls_lock);
94 return ctrl;
95 }
96 }
97 mutex_unlock(&mgr->amp_ctrls_lock);
98
99 return NULL;
100}
101
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300102/* Physical Link interface */
103static u8 __next_handle(struct amp_mgr *mgr)
104{
105 if (++mgr->handle == 0)
106 mgr->handle = 1;
107
108 return mgr->handle;
109}
110
111struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
Andrei Emeltchenkoa0c234f2012-10-05 16:56:56 +0300112 u8 remote_id, bool out)
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300113{
Marcel Holtmannbdc8ead22013-10-13 02:23:38 -0700114 bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst;
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300115 struct hci_conn *hcon;
Johan Hedberga5c4e302014-07-16 11:56:07 +0300116 u8 role = out ? HCI_ROLE_MASTER : HCI_ROLE_SLAVE;
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300117
Johan Hedberga5c4e302014-07-16 11:56:07 +0300118 hcon = hci_conn_add(hdev, AMP_LINK, dst, role);
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300119 if (!hcon)
120 return NULL;
121
Andrei Emeltchenkoa0c234f2012-10-05 16:56:56 +0300122 BT_DBG("hcon %p dst %pMR", hcon, dst);
123
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300124 hcon->state = BT_CONNECT;
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300125 hcon->attempt++;
126 hcon->handle = __next_handle(mgr);
127 hcon->remote_id = remote_id;
Andrei Emeltchenkof706adf2012-10-18 13:16:19 +0300128 hcon->amp_mgr = amp_mgr_get(mgr);
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300129
130 return hcon;
131}
132
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300133/* AMP crypto key generation interface */
134static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
135{
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300136 struct crypto_shash *tfm;
Marcel Holtmann0a961a42014-07-20 17:43:07 +0200137 int ret;
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300138
139 if (!ksize)
140 return -EINVAL;
141
142 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
143 if (IS_ERR(tfm)) {
144 BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
145 return PTR_ERR(tfm);
146 }
147
148 ret = crypto_shash_setkey(tfm, key, ksize);
149 if (ret) {
150 BT_DBG("crypto_ahash_setkey failed: err %d", ret);
151 } else {
Behan Webster9f06a8d2014-09-05 16:03:34 -0700152 char desc[sizeof(struct shash_desc) +
153 crypto_shash_descsize(tfm)] CRYPTO_MINALIGN_ATTR;
154 struct shash_desc *shash = (struct shash_desc *)desc;
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300155
Behan Webster9f06a8d2014-09-05 16:03:34 -0700156 shash->tfm = tfm;
157 shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300158
Behan Webster9f06a8d2014-09-05 16:03:34 -0700159 ret = crypto_shash_digest(shash, plaintext, psize,
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300160 output);
161 }
162
163 crypto_free_shash(tfm);
164 return ret;
165}
166
Andrei Emeltchenko5a349182012-09-27 17:26:18 +0300167int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
168{
169 struct hci_dev *hdev = conn->hdev;
170 struct link_key *key;
171 u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
172 u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
173 int err;
174
175 if (!hci_conn_check_link_mode(conn))
176 return -EACCES;
177
178 BT_DBG("conn %p key_type %d", conn, conn->key_type);
179
180 /* Legacy key */
181 if (conn->key_type < 3) {
182 BT_ERR("Legacy key type %d", conn->key_type);
183 return -EACCES;
184 }
185
186 *type = conn->key_type;
187 *len = HCI_AMP_LINK_KEY_SIZE;
188
189 key = hci_find_link_key(hdev, &conn->dst);
Andrei Emeltchenko079db0c2012-10-05 16:56:53 +0300190 if (!key) {
191 BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
192 return -EACCES;
193 }
Andrei Emeltchenko5a349182012-09-27 17:26:18 +0300194
195 /* BR/EDR Link Key concatenated together with itself */
196 memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
197 memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
198
199 /* Derive Generic AMP Link Key (gamp) */
200 err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
201 if (err) {
202 BT_ERR("Could not derive Generic AMP Key: err %d", err);
203 return err;
204 }
205
206 if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
207 BT_DBG("Use Generic AMP Key (gamp)");
208 memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
209 return err;
210 }
211
212 /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
213 return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
214}
215
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300216void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
217{
218 struct hci_cp_read_local_amp_assoc cp;
219 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
220
221 BT_DBG("%s handle %d", hdev->name, phy_handle);
222
223 cp.phy_handle = phy_handle;
224 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
225 cp.len_so_far = cpu_to_le16(loc_assoc->offset);
226
227 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
228}
229
230void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
231{
232 struct hci_cp_read_local_amp_assoc cp;
233
234 memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
235 memset(&cp, 0, sizeof(cp));
236
237 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
238
Andrei Emeltchenkocb6801c62012-12-07 14:59:08 +0200239 set_bit(READ_LOC_AMP_ASSOC, &mgr->state);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300240 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
241}
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +0300242
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300243void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
244 struct hci_conn *hcon)
245{
246 struct hci_cp_read_local_amp_assoc cp;
247 struct amp_mgr *mgr = hcon->amp_mgr;
248
249 cp.phy_handle = hcon->handle;
250 cp.len_so_far = cpu_to_le16(0);
251 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
252
Andrei Emeltchenkocb6801c62012-12-07 14:59:08 +0200253 set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300254
255 /* Read Local AMP Assoc final link information data */
256 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
257}
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +0300258
259/* Write AMP Assoc data fragments, returns true with last fragment written*/
260static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
261 struct hci_conn *hcon)
262{
263 struct hci_cp_write_remote_amp_assoc *cp;
264 struct amp_mgr *mgr = hcon->amp_mgr;
265 struct amp_ctrl *ctrl;
266 u16 frag_len, len;
267
268 ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
269 if (!ctrl)
270 return false;
271
272 if (!ctrl->assoc_rem_len) {
273 BT_DBG("all fragments are written");
274 ctrl->assoc_rem_len = ctrl->assoc_len;
275 ctrl->assoc_len_so_far = 0;
276
277 amp_ctrl_put(ctrl);
278 return true;
279 }
280
281 frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
282 len = frag_len + sizeof(*cp);
283
284 cp = kzalloc(len, GFP_KERNEL);
285 if (!cp) {
286 amp_ctrl_put(ctrl);
287 return false;
288 }
289
290 BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
291 hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
292
293 cp->phy_handle = hcon->handle;
294 cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
295 cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
296 memcpy(cp->frag, ctrl->assoc, frag_len);
297
298 ctrl->assoc_len_so_far += frag_len;
299 ctrl->assoc_rem_len -= frag_len;
300
301 amp_ctrl_put(ctrl);
302
303 hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
304
305 kfree(cp);
306
307 return false;
308}
309
310void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
311{
312 struct hci_conn *hcon;
313
314 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
315
316 hcon = hci_conn_hash_lookup_handle(hdev, handle);
317 if (!hcon)
318 return;
319
Andrei Emeltchenko8e05e3b2012-12-07 14:59:05 +0200320 /* Send A2MP create phylink rsp when all fragments are written */
321 if (amp_write_rem_assoc_frag(hdev, hcon))
322 a2mp_send_create_phy_link_rsp(hdev, 0);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +0300323}
324
325void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
326{
327 struct hci_conn *hcon;
328
329 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
330
331 hcon = hci_conn_hash_lookup_handle(hdev, handle);
332 if (!hcon)
333 return;
334
335 BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
336
337 amp_write_rem_assoc_frag(hdev, hcon);
338}
339
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +0300340void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
341 struct hci_conn *hcon)
342{
343 struct hci_cp_create_phy_link cp;
344
345 cp.phy_handle = hcon->handle;
346
347 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
348 hcon->handle);
349
350 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
351 &cp.key_type)) {
352 BT_DBG("Cannot create link key");
353 return;
354 }
355
356 hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
357}
Andrei Emeltchenkodffa3872012-09-27 17:26:23 +0300358
359void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
360 struct hci_conn *hcon)
361{
362 struct hci_cp_accept_phy_link cp;
363
364 cp.phy_handle = hcon->handle;
365
366 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
367 hcon->handle);
368
369 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
370 &cp.key_type)) {
371 BT_DBG("Cannot create link key");
372 return;
373 }
374
375 hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
376}
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200377
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +0200378void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
379{
380 struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
381 struct amp_mgr *mgr = hs_hcon->amp_mgr;
382 struct l2cap_chan *bredr_chan;
383
384 BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
385
386 if (!bredr_hdev || !mgr || !mgr->bredr_chan)
387 return;
388
389 bredr_chan = mgr->bredr_chan;
390
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +0200391 l2cap_chan_lock(bredr_chan);
392
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +0200393 set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +0200394 bredr_chan->remote_amp_id = hs_hcon->remote_id;
Andrei Emeltchenko37295992012-11-20 17:16:20 +0200395 bredr_chan->local_amp_id = hs_hcon->hdev->id;
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +0200396 bredr_chan->hs_hcon = hs_hcon;
397 bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +0200398
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +0200399 __l2cap_physical_cfm(bredr_chan, 0);
400
401 l2cap_chan_unlock(bredr_chan);
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +0200402
403 hci_dev_put(bredr_hdev);
404}
405
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200406void amp_create_logical_link(struct l2cap_chan *chan)
407{
Andrei Emeltchenko7a9898c2012-12-07 14:59:06 +0200408 struct hci_conn *hs_hcon = chan->hs_hcon;
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200409 struct hci_cp_create_accept_logical_link cp;
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200410 struct hci_dev *hdev;
411
Marcel Holtmannbdc8ead22013-10-13 02:23:38 -0700412 BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
413 &chan->conn->hcon->dst);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200414
Andrei Emeltchenko7a9898c2012-12-07 14:59:06 +0200415 if (!hs_hcon)
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200416 return;
417
418 hdev = hci_dev_hold(chan->hs_hcon->hdev);
419 if (!hdev)
420 return;
421
Andrei Emeltchenko7a9898c2012-12-07 14:59:06 +0200422 cp.phy_handle = hs_hcon->handle;
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200423
424 cp.tx_flow_spec.id = chan->local_id;
425 cp.tx_flow_spec.stype = chan->local_stype;
426 cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
427 cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
428 cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
429 cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
430
431 cp.rx_flow_spec.id = chan->remote_id;
432 cp.rx_flow_spec.stype = chan->remote_stype;
433 cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
434 cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
435 cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
436 cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
437
Andrei Emeltchenko7a9898c2012-12-07 14:59:06 +0200438 if (hs_hcon->out)
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200439 hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
440 &cp);
441 else
442 hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
443 &cp);
444
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +0200445 hci_dev_put(hdev);
446}
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +0200447
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200448void amp_disconnect_logical_link(struct hci_chan *hchan)
449{
450 struct hci_conn *hcon = hchan->conn;
451 struct hci_cp_disconn_logical_link cp;
452
453 if (hcon->state != BT_CONNECTED) {
454 BT_DBG("hchan %p not connected", hchan);
455 return;
456 }
457
458 cp.log_handle = cpu_to_le16(hchan->handle);
459 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
460}
461
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +0200462void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
463{
464 BT_DBG("hchan %p", hchan);
465
466 hci_chan_del(hchan);
467}