amp.c 14 KB

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