amp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. Copyright (c) 2011,2012 Intel Corp.
  4. */
  5. #include <net/bluetooth/bluetooth.h>
  6. #include <net/bluetooth/hci.h>
  7. #include <net/bluetooth/hci_core.h>
  8. #include <crypto/hash.h>
  9. #include "hci_request.h"
  10. #include "a2mp.h"
  11. #include "amp.h"
  12. /* Remote AMP Controllers interface */
  13. void amp_ctrl_get(struct amp_ctrl *ctrl)
  14. {
  15. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  16. kref_read(&ctrl->kref));
  17. kref_get(&ctrl->kref);
  18. }
  19. static void amp_ctrl_destroy(struct kref *kref)
  20. {
  21. struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
  22. BT_DBG("ctrl %p", ctrl);
  23. kfree(ctrl->assoc);
  24. kfree(ctrl);
  25. }
  26. int amp_ctrl_put(struct amp_ctrl *ctrl)
  27. {
  28. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  29. kref_read(&ctrl->kref));
  30. return kref_put(&ctrl->kref, &amp_ctrl_destroy);
  31. }
  32. struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id)
  33. {
  34. struct amp_ctrl *ctrl;
  35. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  36. if (!ctrl)
  37. return NULL;
  38. kref_init(&ctrl->kref);
  39. ctrl->id = id;
  40. mutex_lock(&mgr->amp_ctrls_lock);
  41. list_add(&ctrl->list, &mgr->amp_ctrls);
  42. mutex_unlock(&mgr->amp_ctrls_lock);
  43. BT_DBG("mgr %p ctrl %p", mgr, ctrl);
  44. return ctrl;
  45. }
  46. void amp_ctrl_list_flush(struct amp_mgr *mgr)
  47. {
  48. struct amp_ctrl *ctrl, *n;
  49. BT_DBG("mgr %p", mgr);
  50. mutex_lock(&mgr->amp_ctrls_lock);
  51. list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
  52. list_del(&ctrl->list);
  53. amp_ctrl_put(ctrl);
  54. }
  55. mutex_unlock(&mgr->amp_ctrls_lock);
  56. }
  57. struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
  58. {
  59. struct amp_ctrl *ctrl;
  60. BT_DBG("mgr %p id %u", mgr, id);
  61. mutex_lock(&mgr->amp_ctrls_lock);
  62. list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
  63. if (ctrl->id == id) {
  64. amp_ctrl_get(ctrl);
  65. mutex_unlock(&mgr->amp_ctrls_lock);
  66. return ctrl;
  67. }
  68. }
  69. mutex_unlock(&mgr->amp_ctrls_lock);
  70. return NULL;
  71. }
  72. /* Physical Link interface */
  73. static u8 __next_handle(struct amp_mgr *mgr)
  74. {
  75. if (++mgr->handle == 0)
  76. mgr->handle = 1;
  77. return mgr->handle;
  78. }
  79. struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
  80. u8 remote_id, bool out)
  81. {
  82. bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst;
  83. struct hci_conn *hcon;
  84. u8 role = out ? HCI_ROLE_MASTER : HCI_ROLE_SLAVE;
  85. hcon = hci_conn_add(hdev, AMP_LINK, dst, role);
  86. if (!hcon)
  87. return NULL;
  88. BT_DBG("hcon %p dst %pMR", hcon, dst);
  89. hcon->state = BT_CONNECT;
  90. hcon->attempt++;
  91. hcon->handle = __next_handle(mgr);
  92. hcon->remote_id = remote_id;
  93. hcon->amp_mgr = amp_mgr_get(mgr);
  94. return hcon;
  95. }
  96. /* AMP crypto key generation interface */
  97. static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
  98. {
  99. struct crypto_shash *tfm;
  100. struct shash_desc *shash;
  101. int ret;
  102. if (!ksize)
  103. return -EINVAL;
  104. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  105. if (IS_ERR(tfm)) {
  106. BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
  107. return PTR_ERR(tfm);
  108. }
  109. ret = crypto_shash_setkey(tfm, key, ksize);
  110. if (ret) {
  111. BT_DBG("crypto_ahash_setkey failed: err %d", ret);
  112. goto failed;
  113. }
  114. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  115. GFP_KERNEL);
  116. if (!shash) {
  117. ret = -ENOMEM;
  118. goto failed;
  119. }
  120. shash->tfm = tfm;
  121. ret = crypto_shash_digest(shash, plaintext, psize, output);
  122. kfree(shash);
  123. failed:
  124. crypto_free_shash(tfm);
  125. return ret;
  126. }
  127. int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
  128. {
  129. struct hci_dev *hdev = conn->hdev;
  130. struct link_key *key;
  131. u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
  132. u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
  133. int err;
  134. if (!hci_conn_check_link_mode(conn))
  135. return -EACCES;
  136. BT_DBG("conn %p key_type %d", conn, conn->key_type);
  137. /* Legacy key */
  138. if (conn->key_type < 3) {
  139. bt_dev_err(hdev, "legacy key type %u", conn->key_type);
  140. return -EACCES;
  141. }
  142. *type = conn->key_type;
  143. *len = HCI_AMP_LINK_KEY_SIZE;
  144. key = hci_find_link_key(hdev, &conn->dst);
  145. if (!key) {
  146. BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
  147. return -EACCES;
  148. }
  149. /* BR/EDR Link Key concatenated together with itself */
  150. memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
  151. memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
  152. /* Derive Generic AMP Link Key (gamp) */
  153. err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
  154. if (err) {
  155. bt_dev_err(hdev, "could not derive Generic AMP Key: err %d", err);
  156. return err;
  157. }
  158. if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
  159. BT_DBG("Use Generic AMP Key (gamp)");
  160. memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
  161. return err;
  162. }
  163. /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
  164. return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
  165. }
  166. static void read_local_amp_assoc_complete(struct hci_dev *hdev, u8 status,
  167. u16 opcode, struct sk_buff *skb)
  168. {
  169. struct hci_rp_read_local_amp_assoc *rp = (void *)skb->data;
  170. struct amp_assoc *assoc = &hdev->loc_assoc;
  171. size_t rem_len, frag_len;
  172. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  173. if (rp->status)
  174. goto send_rsp;
  175. frag_len = skb->len - sizeof(*rp);
  176. rem_len = __le16_to_cpu(rp->rem_len);
  177. if (rem_len > frag_len) {
  178. BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
  179. memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
  180. assoc->offset += frag_len;
  181. /* Read other fragments */
  182. amp_read_loc_assoc_frag(hdev, rp->phy_handle);
  183. return;
  184. }
  185. memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
  186. assoc->len = assoc->offset + rem_len;
  187. assoc->offset = 0;
  188. send_rsp:
  189. /* Send A2MP Rsp when all fragments are received */
  190. a2mp_send_getampassoc_rsp(hdev, rp->status);
  191. a2mp_send_create_phy_link_req(hdev, rp->status);
  192. }
  193. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  194. {
  195. struct hci_cp_read_local_amp_assoc cp;
  196. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  197. struct hci_request req;
  198. int err;
  199. BT_DBG("%s handle %u", hdev->name, phy_handle);
  200. cp.phy_handle = phy_handle;
  201. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  202. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  203. hci_req_init(&req, hdev);
  204. hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  205. err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
  206. if (err < 0)
  207. a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
  208. }
  209. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  210. {
  211. struct hci_cp_read_local_amp_assoc cp;
  212. struct hci_request req;
  213. int err;
  214. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  215. memset(&cp, 0, sizeof(cp));
  216. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  217. set_bit(READ_LOC_AMP_ASSOC, &mgr->state);
  218. hci_req_init(&req, hdev);
  219. hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  220. err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
  221. if (err < 0)
  222. a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
  223. }
  224. void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
  225. struct hci_conn *hcon)
  226. {
  227. struct hci_cp_read_local_amp_assoc cp;
  228. struct amp_mgr *mgr = hcon->amp_mgr;
  229. struct hci_request req;
  230. int err;
  231. if (!mgr)
  232. return;
  233. cp.phy_handle = hcon->handle;
  234. cp.len_so_far = cpu_to_le16(0);
  235. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  236. set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
  237. /* Read Local AMP Assoc final link information data */
  238. hci_req_init(&req, hdev);
  239. hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  240. err = hci_req_run_skb(&req, read_local_amp_assoc_complete);
  241. if (err < 0)
  242. a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
  243. }
  244. static void write_remote_amp_assoc_complete(struct hci_dev *hdev, u8 status,
  245. u16 opcode, struct sk_buff *skb)
  246. {
  247. struct hci_rp_write_remote_amp_assoc *rp = (void *)skb->data;
  248. BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
  249. hdev->name, rp->status, rp->phy_handle);
  250. if (rp->status)
  251. return;
  252. amp_write_rem_assoc_continue(hdev, rp->phy_handle);
  253. }
  254. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  255. static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
  256. struct hci_conn *hcon)
  257. {
  258. struct hci_cp_write_remote_amp_assoc *cp;
  259. struct amp_mgr *mgr = hcon->amp_mgr;
  260. struct amp_ctrl *ctrl;
  261. struct hci_request req;
  262. u16 frag_len, len;
  263. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  264. if (!ctrl)
  265. return false;
  266. if (!ctrl->assoc_rem_len) {
  267. BT_DBG("all fragments are written");
  268. ctrl->assoc_rem_len = ctrl->assoc_len;
  269. ctrl->assoc_len_so_far = 0;
  270. amp_ctrl_put(ctrl);
  271. return true;
  272. }
  273. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  274. len = frag_len + sizeof(*cp);
  275. cp = kzalloc(len, GFP_KERNEL);
  276. if (!cp) {
  277. amp_ctrl_put(ctrl);
  278. return false;
  279. }
  280. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  281. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  282. cp->phy_handle = hcon->handle;
  283. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  284. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  285. memcpy(cp->frag, ctrl->assoc, frag_len);
  286. ctrl->assoc_len_so_far += frag_len;
  287. ctrl->assoc_rem_len -= frag_len;
  288. amp_ctrl_put(ctrl);
  289. hci_req_init(&req, hdev);
  290. hci_req_add(&req, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  291. hci_req_run_skb(&req, write_remote_amp_assoc_complete);
  292. kfree(cp);
  293. return false;
  294. }
  295. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  296. {
  297. struct hci_conn *hcon;
  298. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  299. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  300. if (!hcon)
  301. return;
  302. /* Send A2MP create phylink rsp when all fragments are written */
  303. if (amp_write_rem_assoc_frag(hdev, hcon))
  304. a2mp_send_create_phy_link_rsp(hdev, 0);
  305. }
  306. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  307. {
  308. struct hci_conn *hcon;
  309. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  310. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  311. if (!hcon)
  312. return;
  313. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  314. amp_write_rem_assoc_frag(hdev, hcon);
  315. }
  316. static void create_phylink_complete(struct hci_dev *hdev, u8 status,
  317. u16 opcode)
  318. {
  319. struct hci_cp_create_phy_link *cp;
  320. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  321. cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
  322. if (!cp)
  323. return;
  324. hci_dev_lock(hdev);
  325. if (status) {
  326. struct hci_conn *hcon;
  327. hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
  328. if (hcon)
  329. hci_conn_del(hcon);
  330. } else {
  331. amp_write_remote_assoc(hdev, cp->phy_handle);
  332. }
  333. hci_dev_unlock(hdev);
  334. }
  335. void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  336. struct hci_conn *hcon)
  337. {
  338. struct hci_cp_create_phy_link cp;
  339. struct hci_request req;
  340. cp.phy_handle = hcon->handle;
  341. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  342. hcon->handle);
  343. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  344. &cp.key_type)) {
  345. BT_DBG("Cannot create link key");
  346. return;
  347. }
  348. hci_req_init(&req, hdev);
  349. hci_req_add(&req, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  350. hci_req_run(&req, create_phylink_complete);
  351. }
  352. static void accept_phylink_complete(struct hci_dev *hdev, u8 status,
  353. u16 opcode)
  354. {
  355. struct hci_cp_accept_phy_link *cp;
  356. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  357. if (status)
  358. return;
  359. cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
  360. if (!cp)
  361. return;
  362. amp_write_remote_assoc(hdev, cp->phy_handle);
  363. }
  364. void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  365. struct hci_conn *hcon)
  366. {
  367. struct hci_cp_accept_phy_link cp;
  368. struct hci_request req;
  369. cp.phy_handle = hcon->handle;
  370. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  371. hcon->handle);
  372. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  373. &cp.key_type)) {
  374. BT_DBG("Cannot create link key");
  375. return;
  376. }
  377. hci_req_init(&req, hdev);
  378. hci_req_add(&req, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
  379. hci_req_run(&req, accept_phylink_complete);
  380. }
  381. void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
  382. {
  383. struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
  384. struct amp_mgr *mgr = hs_hcon->amp_mgr;
  385. struct l2cap_chan *bredr_chan;
  386. BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
  387. if (!bredr_hdev || !mgr || !mgr->bredr_chan)
  388. return;
  389. bredr_chan = mgr->bredr_chan;
  390. l2cap_chan_lock(bredr_chan);
  391. set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
  392. bredr_chan->remote_amp_id = hs_hcon->remote_id;
  393. bredr_chan->local_amp_id = hs_hcon->hdev->id;
  394. bredr_chan->hs_hcon = hs_hcon;
  395. bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
  396. __l2cap_physical_cfm(bredr_chan, 0);
  397. l2cap_chan_unlock(bredr_chan);
  398. hci_dev_put(bredr_hdev);
  399. }
  400. void amp_create_logical_link(struct l2cap_chan *chan)
  401. {
  402. struct hci_conn *hs_hcon = chan->hs_hcon;
  403. struct hci_cp_create_accept_logical_link cp;
  404. struct hci_dev *hdev;
  405. BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
  406. &chan->conn->hcon->dst);
  407. if (!hs_hcon)
  408. return;
  409. hdev = hci_dev_hold(chan->hs_hcon->hdev);
  410. if (!hdev)
  411. return;
  412. cp.phy_handle = hs_hcon->handle;
  413. cp.tx_flow_spec.id = chan->local_id;
  414. cp.tx_flow_spec.stype = chan->local_stype;
  415. cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
  416. cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
  417. cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
  418. cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
  419. cp.rx_flow_spec.id = chan->remote_id;
  420. cp.rx_flow_spec.stype = chan->remote_stype;
  421. cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
  422. cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
  423. cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
  424. cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
  425. if (hs_hcon->out)
  426. hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
  427. &cp);
  428. else
  429. hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
  430. &cp);
  431. hci_dev_put(hdev);
  432. }
  433. void amp_disconnect_logical_link(struct hci_chan *hchan)
  434. {
  435. struct hci_conn *hcon = hchan->conn;
  436. struct hci_cp_disconn_logical_link cp;
  437. if (hcon->state != BT_CONNECTED) {
  438. BT_DBG("hchan %p not connected", hchan);
  439. return;
  440. }
  441. cp.log_handle = cpu_to_le16(hchan->handle);
  442. hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
  443. }
  444. void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
  445. {
  446. BT_DBG("hchan %p", hchan);
  447. hci_chan_del(hchan);
  448. }