1 // SPDX-License-Identifier: GPL-2.0-only
3 Copyright (c) 2011,2012 Intel Corp.
7 #include <net/bluetooth/bluetooth.h>
8 #include <net/bluetooth/hci.h>
9 #include <net/bluetooth/hci_core.h>
10 #include <crypto/hash.h>
12 #include "hci_request.h"
16 /* Remote AMP Controllers interface */
17 void amp_ctrl_get(struct amp_ctrl *ctrl)
19 BT_DBG("ctrl %p orig refcnt %d", ctrl,
20 kref_read(&ctrl->kref));
22 kref_get(&ctrl->kref);
25 static void amp_ctrl_destroy(struct kref *kref)
27 struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
29 BT_DBG("ctrl %p", ctrl);
35 int amp_ctrl_put(struct amp_ctrl *ctrl)
37 BT_DBG("ctrl %p orig refcnt %d", ctrl,
38 kref_read(&ctrl->kref));
40 return kref_put(&ctrl->kref, &_ctrl_destroy);
43 struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id)
45 struct amp_ctrl *ctrl;
47 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
51 kref_init(&ctrl->kref);
54 mutex_lock(&mgr->amp_ctrls_lock);
55 list_add(&ctrl->list, &mgr->amp_ctrls);
56 mutex_unlock(&mgr->amp_ctrls_lock);
58 BT_DBG("mgr %p ctrl %p", mgr, ctrl);
63 void amp_ctrl_list_flush(struct amp_mgr *mgr)
65 struct amp_ctrl *ctrl, *n;
67 BT_DBG("mgr %p", mgr);
69 mutex_lock(&mgr->amp_ctrls_lock);
70 list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
71 list_del(&ctrl->list);
74 mutex_unlock(&mgr->amp_ctrls_lock);
77 struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
79 struct amp_ctrl *ctrl;
81 BT_DBG("mgr %p id %u", mgr, id);
83 mutex_lock(&mgr->amp_ctrls_lock);
84 list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
87 mutex_unlock(&mgr->amp_ctrls_lock);
91 mutex_unlock(&mgr->amp_ctrls_lock);
96 /* Physical Link interface */
97 static u8 __next_handle(struct amp_mgr *mgr)
99 if (++mgr->handle == 0)
105 struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
106 u8 remote_id, bool out)
108 bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst;
109 struct hci_conn *hcon;
110 u8 role = out ? HCI_ROLE_MASTER : HCI_ROLE_SLAVE;
112 hcon = hci_conn_add(hdev, AMP_LINK, dst, role);
116 BT_DBG("hcon %p dst %pMR", hcon, dst);
118 hcon->state = BT_CONNECT;
120 hcon->handle = __next_handle(mgr);
121 hcon->remote_id = remote_id;
122 hcon->amp_mgr = amp_mgr_get(mgr);
127 /* AMP crypto key generation interface */
128 static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
130 struct crypto_shash *tfm;
131 struct shash_desc *shash;
137 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
139 BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
143 ret = crypto_shash_setkey(tfm, key, ksize);
145 BT_DBG("crypto_ahash_setkey failed: err %d", ret);
149 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
158 ret = crypto_shash_digest(shash, plaintext, psize, output);
163 crypto_free_shash(tfm);
167 int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
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];
175 if (!hci_conn_check_link_mode(conn))
178 BT_DBG("conn %p key_type %d", conn, conn->key_type);
181 if (conn->key_type < 3) {
182 bt_dev_err(hdev, "legacy key type %u", conn->key_type);
186 *type = conn->key_type;
187 *len = HCI_AMP_LINK_KEY_SIZE;
189 key = hci_find_link_key(hdev, &conn->dst);
191 BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
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);
199 /* Derive Generic AMP Link Key (gamp) */
200 err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
202 bt_dev_err(hdev, "could not derive Generic AMP Key: err %d", err);
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);
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);
216 static void read_local_amp_assoc_complete(struct hci_dev *hdev, u8 status,
217 u16 opcode, struct sk_buff *skb)
219 struct hci_rp_read_local_amp_assoc *rp = (void *)skb->data;
220 struct amp_assoc *assoc = &hdev->loc_assoc;
221 size_t rem_len, frag_len;
223 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
228 frag_len = skb->len - sizeof(*rp);
229 rem_len = __le16_to_cpu(rp->rem_len);
231 if (rem_len > frag_len) {
232 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
234 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
235 assoc->offset += frag_len;
237 /* Read other fragments */
238 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
243 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
244 assoc->len = assoc->offset + rem_len;
248 /* Send A2MP Rsp when all fragments are received */
249 a2mp_send_getampassoc_rsp(hdev, rp->status);
250 a2mp_send_create_phy_link_req(hdev, rp->status);
253 void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
255 struct hci_cp_read_local_amp_assoc cp;
256 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
257 struct hci_request req;
260 BT_DBG("%s handle %u", hdev->name, phy_handle);
262 cp.phy_handle = phy_handle;
263 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
264 cp.len_so_far = cpu_to_le16(loc_assoc->offset);
266 hci_req_init(&req, hdev);
267 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
268 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
270 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
273 void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
275 struct hci_cp_read_local_amp_assoc cp;
276 struct hci_request req;
279 memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
280 memset(&cp, 0, sizeof(cp));
282 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
284 set_bit(READ_LOC_AMP_ASSOC, &mgr->state);
285 hci_req_init(&req, hdev);
286 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
287 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
289 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
292 void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
293 struct hci_conn *hcon)
295 struct hci_cp_read_local_amp_assoc cp;
296 struct amp_mgr *mgr = hcon->amp_mgr;
297 struct hci_request req;
303 cp.phy_handle = hcon->handle;
304 cp.len_so_far = cpu_to_le16(0);
305 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
307 set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
309 /* Read Local AMP Assoc final link information data */
310 hci_req_init(&req, hdev);
311 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
312 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
314 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
317 static void write_remote_amp_assoc_complete(struct hci_dev *hdev, u8 status,
318 u16 opcode, struct sk_buff *skb)
320 struct hci_rp_write_remote_amp_assoc *rp = (void *)skb->data;
322 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
323 hdev->name, rp->status, rp->phy_handle);
328 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
331 /* Write AMP Assoc data fragments, returns true with last fragment written*/
332 static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
333 struct hci_conn *hcon)
335 struct hci_cp_write_remote_amp_assoc *cp;
336 struct amp_mgr *mgr = hcon->amp_mgr;
337 struct amp_ctrl *ctrl;
338 struct hci_request req;
341 ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
345 if (!ctrl->assoc_rem_len) {
346 BT_DBG("all fragments are written");
347 ctrl->assoc_rem_len = ctrl->assoc_len;
348 ctrl->assoc_len_so_far = 0;
354 frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
355 len = frag_len + sizeof(*cp);
357 cp = kzalloc(len, GFP_KERNEL);
363 BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
364 hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
366 cp->phy_handle = hcon->handle;
367 cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
368 cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
369 memcpy(cp->frag, ctrl->assoc, frag_len);
371 ctrl->assoc_len_so_far += frag_len;
372 ctrl->assoc_rem_len -= frag_len;
376 hci_req_init(&req, hdev);
377 hci_req_add(&req, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
378 hci_req_run_skb(&req, write_remote_amp_assoc_complete);
385 void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
387 struct hci_conn *hcon;
389 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
391 hcon = hci_conn_hash_lookup_handle(hdev, handle);
395 /* Send A2MP create phylink rsp when all fragments are written */
396 if (amp_write_rem_assoc_frag(hdev, hcon))
397 a2mp_send_create_phy_link_rsp(hdev, 0);
400 void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
402 struct hci_conn *hcon;
404 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
406 hcon = hci_conn_hash_lookup_handle(hdev, handle);
410 BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
412 amp_write_rem_assoc_frag(hdev, hcon);
415 static void create_phylink_complete(struct hci_dev *hdev, u8 status,
418 struct hci_cp_create_phy_link *cp;
420 BT_DBG("%s status 0x%2.2x", hdev->name, status);
422 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
429 struct hci_conn *hcon;
431 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
435 amp_write_remote_assoc(hdev, cp->phy_handle);
438 hci_dev_unlock(hdev);
441 void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
442 struct hci_conn *hcon)
444 struct hci_cp_create_phy_link cp;
445 struct hci_request req;
447 cp.phy_handle = hcon->handle;
449 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
452 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
454 BT_DBG("Cannot create link key");
458 hci_req_init(&req, hdev);
459 hci_req_add(&req, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
460 hci_req_run(&req, create_phylink_complete);
463 static void accept_phylink_complete(struct hci_dev *hdev, u8 status,
466 struct hci_cp_accept_phy_link *cp;
468 BT_DBG("%s status 0x%2.2x", hdev->name, status);
473 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
477 amp_write_remote_assoc(hdev, cp->phy_handle);
480 void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
481 struct hci_conn *hcon)
483 struct hci_cp_accept_phy_link cp;
484 struct hci_request req;
486 cp.phy_handle = hcon->handle;
488 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
491 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
493 BT_DBG("Cannot create link key");
497 hci_req_init(&req, hdev);
498 hci_req_add(&req, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
499 hci_req_run(&req, accept_phylink_complete);
502 void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
504 struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
505 struct amp_mgr *mgr = hs_hcon->amp_mgr;
506 struct l2cap_chan *bredr_chan;
508 BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
510 if (!bredr_hdev || !mgr || !mgr->bredr_chan)
513 bredr_chan = mgr->bredr_chan;
515 l2cap_chan_lock(bredr_chan);
517 set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
518 bredr_chan->remote_amp_id = hs_hcon->remote_id;
519 bredr_chan->local_amp_id = hs_hcon->hdev->id;
520 bredr_chan->hs_hcon = hs_hcon;
521 bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
523 __l2cap_physical_cfm(bredr_chan, 0);
525 l2cap_chan_unlock(bredr_chan);
527 hci_dev_put(bredr_hdev);
530 void amp_create_logical_link(struct l2cap_chan *chan)
532 struct hci_conn *hs_hcon = chan->hs_hcon;
533 struct hci_cp_create_accept_logical_link cp;
534 struct hci_dev *hdev;
536 BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
537 &chan->conn->hcon->dst);
542 hdev = hci_dev_hold(chan->hs_hcon->hdev);
546 cp.phy_handle = hs_hcon->handle;
548 cp.tx_flow_spec.id = chan->local_id;
549 cp.tx_flow_spec.stype = chan->local_stype;
550 cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
551 cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
552 cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
553 cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
555 cp.rx_flow_spec.id = chan->remote_id;
556 cp.rx_flow_spec.stype = chan->remote_stype;
557 cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
558 cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
559 cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
560 cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
563 hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
566 hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
572 void amp_disconnect_logical_link(struct hci_chan *hchan)
574 struct hci_conn *hcon = hchan->conn;
575 struct hci_cp_disconn_logical_link cp;
577 if (hcon->state != BT_CONNECTED) {
578 BT_DBG("hchan %p not connected", hchan);
582 cp.log_handle = cpu_to_le16(hchan->handle);
583 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
586 void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
588 BT_DBG("hchan %p", hchan);