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, __next_handle(mgr));
116 BT_DBG("hcon %p dst %pMR", hcon, dst);
118 hcon->state = BT_CONNECT;
120 hcon->remote_id = remote_id;
121 hcon->amp_mgr = amp_mgr_get(mgr);
126 /* AMP crypto key generation interface */
127 static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
129 struct crypto_shash *tfm;
130 struct shash_desc *shash;
136 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
138 BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
142 ret = crypto_shash_setkey(tfm, key, ksize);
144 BT_DBG("crypto_ahash_setkey failed: err %d", ret);
148 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
157 ret = crypto_shash_digest(shash, plaintext, psize, output);
162 crypto_free_shash(tfm);
166 int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
168 struct hci_dev *hdev = conn->hdev;
169 struct link_key *key;
170 u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
171 u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
174 if (!hci_conn_check_link_mode(conn))
177 BT_DBG("conn %p key_type %d", conn, conn->key_type);
180 if (conn->key_type < 3) {
181 bt_dev_err(hdev, "legacy key type %u", conn->key_type);
185 *type = conn->key_type;
186 *len = HCI_AMP_LINK_KEY_SIZE;
188 key = hci_find_link_key(hdev, &conn->dst);
190 BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
194 /* BR/EDR Link Key concatenated together with itself */
195 memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
196 memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
198 /* Derive Generic AMP Link Key (gamp) */
199 err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
201 bt_dev_err(hdev, "could not derive Generic AMP Key: err %d", err);
205 if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
206 BT_DBG("Use Generic AMP Key (gamp)");
207 memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
211 /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
212 return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
215 static void read_local_amp_assoc_complete(struct hci_dev *hdev, u8 status,
216 u16 opcode, struct sk_buff *skb)
218 struct hci_rp_read_local_amp_assoc *rp = (void *)skb->data;
219 struct amp_assoc *assoc = &hdev->loc_assoc;
220 size_t rem_len, frag_len;
222 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
227 frag_len = skb->len - sizeof(*rp);
228 rem_len = __le16_to_cpu(rp->rem_len);
230 if (rem_len > frag_len) {
231 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
233 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
234 assoc->offset += frag_len;
236 /* Read other fragments */
237 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
242 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
243 assoc->len = assoc->offset + rem_len;
247 /* Send A2MP Rsp when all fragments are received */
248 a2mp_send_getampassoc_rsp(hdev, rp->status);
249 a2mp_send_create_phy_link_req(hdev, rp->status);
252 void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
254 struct hci_cp_read_local_amp_assoc cp;
255 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
256 struct hci_request req;
259 BT_DBG("%s handle %u", hdev->name, phy_handle);
261 cp.phy_handle = phy_handle;
262 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
263 cp.len_so_far = cpu_to_le16(loc_assoc->offset);
265 hci_req_init(&req, hdev);
266 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
267 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
269 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
272 void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
274 struct hci_cp_read_local_amp_assoc cp;
275 struct hci_request req;
278 memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
279 memset(&cp, 0, sizeof(cp));
281 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
283 set_bit(READ_LOC_AMP_ASSOC, &mgr->state);
284 hci_req_init(&req, hdev);
285 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
286 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
288 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
291 void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
292 struct hci_conn *hcon)
294 struct hci_cp_read_local_amp_assoc cp;
295 struct amp_mgr *mgr = hcon->amp_mgr;
296 struct hci_request req;
302 cp.phy_handle = hcon->handle;
303 cp.len_so_far = cpu_to_le16(0);
304 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
306 set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
308 /* Read Local AMP Assoc final link information data */
309 hci_req_init(&req, hdev);
310 hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
311 err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
313 a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
316 static void write_remote_amp_assoc_complete(struct hci_dev *hdev, u8 status,
317 u16 opcode, struct sk_buff *skb)
319 struct hci_rp_write_remote_amp_assoc *rp = (void *)skb->data;
321 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
322 hdev->name, rp->status, rp->phy_handle);
327 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
330 /* Write AMP Assoc data fragments, returns true with last fragment written*/
331 static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
332 struct hci_conn *hcon)
334 struct hci_cp_write_remote_amp_assoc *cp;
335 struct amp_mgr *mgr = hcon->amp_mgr;
336 struct amp_ctrl *ctrl;
337 struct hci_request req;
340 ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
344 if (!ctrl->assoc_rem_len) {
345 BT_DBG("all fragments are written");
346 ctrl->assoc_rem_len = ctrl->assoc_len;
347 ctrl->assoc_len_so_far = 0;
353 frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
354 len = frag_len + sizeof(*cp);
356 cp = kzalloc(len, GFP_KERNEL);
362 BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
363 hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
365 cp->phy_handle = hcon->handle;
366 cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
367 cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
368 memcpy(cp->frag, ctrl->assoc, frag_len);
370 ctrl->assoc_len_so_far += frag_len;
371 ctrl->assoc_rem_len -= frag_len;
375 hci_req_init(&req, hdev);
376 hci_req_add(&req, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
377 hci_req_run_skb(&req, write_remote_amp_assoc_complete);
384 void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
386 struct hci_conn *hcon;
388 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
390 hcon = hci_conn_hash_lookup_handle(hdev, handle);
394 /* Send A2MP create phylink rsp when all fragments are written */
395 if (amp_write_rem_assoc_frag(hdev, hcon))
396 a2mp_send_create_phy_link_rsp(hdev, 0);
399 void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
401 struct hci_conn *hcon;
403 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
405 hcon = hci_conn_hash_lookup_handle(hdev, handle);
409 BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
411 amp_write_rem_assoc_frag(hdev, hcon);
414 static void create_phylink_complete(struct hci_dev *hdev, u8 status,
417 struct hci_cp_create_phy_link *cp;
419 BT_DBG("%s status 0x%2.2x", hdev->name, status);
421 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
428 struct hci_conn *hcon;
430 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
434 amp_write_remote_assoc(hdev, cp->phy_handle);
437 hci_dev_unlock(hdev);
440 void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
441 struct hci_conn *hcon)
443 struct hci_cp_create_phy_link cp;
444 struct hci_request req;
446 cp.phy_handle = hcon->handle;
448 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
451 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
453 BT_DBG("Cannot create link key");
457 hci_req_init(&req, hdev);
458 hci_req_add(&req, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
459 hci_req_run(&req, create_phylink_complete);
462 static void accept_phylink_complete(struct hci_dev *hdev, u8 status,
465 struct hci_cp_accept_phy_link *cp;
467 BT_DBG("%s status 0x%2.2x", hdev->name, status);
472 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
476 amp_write_remote_assoc(hdev, cp->phy_handle);
479 void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
480 struct hci_conn *hcon)
482 struct hci_cp_accept_phy_link cp;
483 struct hci_request req;
485 cp.phy_handle = hcon->handle;
487 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
490 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
492 BT_DBG("Cannot create link key");
496 hci_req_init(&req, hdev);
497 hci_req_add(&req, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
498 hci_req_run(&req, accept_phylink_complete);
501 void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
503 struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
504 struct amp_mgr *mgr = hs_hcon->amp_mgr;
505 struct l2cap_chan *bredr_chan;
507 BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
509 if (!bredr_hdev || !mgr || !mgr->bredr_chan)
512 bredr_chan = mgr->bredr_chan;
514 l2cap_chan_lock(bredr_chan);
516 set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
517 bredr_chan->remote_amp_id = hs_hcon->remote_id;
518 bredr_chan->local_amp_id = hs_hcon->hdev->id;
519 bredr_chan->hs_hcon = hs_hcon;
520 bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
522 __l2cap_physical_cfm(bredr_chan, 0);
524 l2cap_chan_unlock(bredr_chan);
526 hci_dev_put(bredr_hdev);
529 void amp_create_logical_link(struct l2cap_chan *chan)
531 struct hci_conn *hs_hcon = chan->hs_hcon;
532 struct hci_cp_create_accept_logical_link cp;
533 struct hci_dev *hdev;
535 BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
536 &chan->conn->hcon->dst);
541 hdev = hci_dev_hold(chan->hs_hcon->hdev);
545 cp.phy_handle = hs_hcon->handle;
547 cp.tx_flow_spec.id = chan->local_id;
548 cp.tx_flow_spec.stype = chan->local_stype;
549 cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
550 cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
551 cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
552 cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
554 cp.rx_flow_spec.id = chan->remote_id;
555 cp.rx_flow_spec.stype = chan->remote_stype;
556 cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
557 cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
558 cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
559 cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
562 hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
565 hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
571 void amp_disconnect_logical_link(struct hci_chan *hchan)
573 struct hci_conn *hcon = hchan->conn;
574 struct hci_cp_disconn_logical_link cp;
576 if (hcon->state != BT_CONNECTED) {
577 BT_DBG("hchan %p not connected", hchan);
581 cp.log_handle = cpu_to_le16(hchan->handle);
582 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
585 void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
587 BT_DBG("hchan %p", hchan);