amp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. atomic_read(&ctrl->kref.refcount));
  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. atomic_read(&ctrl->kref.refcount));
  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_ERR("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_ERR("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 = 0;
  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 = 0;
  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. 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 = 0;
  238. cp.phy_handle = hcon->handle;
  239. cp.len_so_far = cpu_to_le16(0);
  240. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  241. set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
  242. /* Read Local AMP Assoc final link information data */
  243. hci_req_init(&req, hdev);
  244. hci_req_add(&req, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  245. hci_req_run_skb(&req, read_local_amp_assoc_complete);
  246. if (err < 0)
  247. a2mp_send_getampassoc_rsp(hdev, A2MP_STATUS_INVALID_CTRL_ID);
  248. }
  249. static void write_remote_amp_assoc_complete(struct hci_dev *hdev, u8 status,
  250. u16 opcode, struct sk_buff *skb)
  251. {
  252. struct hci_rp_write_remote_amp_assoc *rp = (void *)skb->data;
  253. BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
  254. hdev->name, rp->status, rp->phy_handle);
  255. if (rp->status)
  256. return;
  257. amp_write_rem_assoc_continue(hdev, rp->phy_handle);
  258. }
  259. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  260. static 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. struct hci_request req;
  267. u16 frag_len, len;
  268. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  269. if (!ctrl)
  270. return false;
  271. if (!ctrl->assoc_rem_len) {
  272. BT_DBG("all fragments are written");
  273. ctrl->assoc_rem_len = ctrl->assoc_len;
  274. ctrl->assoc_len_so_far = 0;
  275. amp_ctrl_put(ctrl);
  276. return true;
  277. }
  278. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  279. len = frag_len + sizeof(*cp);
  280. cp = kzalloc(len, GFP_KERNEL);
  281. if (!cp) {
  282. amp_ctrl_put(ctrl);
  283. return false;
  284. }
  285. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  286. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  287. cp->phy_handle = hcon->handle;
  288. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  289. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  290. memcpy(cp->frag, ctrl->assoc, frag_len);
  291. ctrl->assoc_len_so_far += frag_len;
  292. ctrl->assoc_rem_len -= frag_len;
  293. amp_ctrl_put(ctrl);
  294. hci_req_init(&req, hdev);
  295. hci_req_add(&req, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  296. hci_req_run_skb(&req, write_remote_amp_assoc_complete);
  297. kfree(cp);
  298. return false;
  299. }
  300. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  301. {
  302. struct hci_conn *hcon;
  303. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  304. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  305. if (!hcon)
  306. return;
  307. /* Send A2MP create phylink rsp when all fragments are written */
  308. if (amp_write_rem_assoc_frag(hdev, hcon))
  309. a2mp_send_create_phy_link_rsp(hdev, 0);
  310. }
  311. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  312. {
  313. struct hci_conn *hcon;
  314. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  315. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  316. if (!hcon)
  317. return;
  318. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  319. amp_write_rem_assoc_frag(hdev, hcon);
  320. }
  321. static void create_phylink_complete(struct hci_dev *hdev, u8 status,
  322. u16 opcode)
  323. {
  324. struct hci_cp_create_phy_link *cp;
  325. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  326. cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
  327. if (!cp)
  328. return;
  329. hci_dev_lock(hdev);
  330. if (status) {
  331. struct hci_conn *hcon;
  332. hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
  333. if (hcon)
  334. hci_conn_del(hcon);
  335. } else {
  336. amp_write_remote_assoc(hdev, cp->phy_handle);
  337. }
  338. hci_dev_unlock(hdev);
  339. }
  340. void 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. struct hci_request req;
  345. cp.phy_handle = hcon->handle;
  346. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  347. hcon->handle);
  348. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  349. &cp.key_type)) {
  350. BT_DBG("Cannot create link key");
  351. return;
  352. }
  353. hci_req_init(&req, hdev);
  354. hci_req_add(&req, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  355. hci_req_run(&req, create_phylink_complete);
  356. }
  357. static void accept_phylink_complete(struct hci_dev *hdev, u8 status,
  358. u16 opcode)
  359. {
  360. struct hci_cp_accept_phy_link *cp;
  361. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  362. if (status)
  363. return;
  364. cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
  365. if (!cp)
  366. return;
  367. amp_write_remote_assoc(hdev, cp->phy_handle);
  368. }
  369. void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  370. struct hci_conn *hcon)
  371. {
  372. struct hci_cp_accept_phy_link cp;
  373. struct hci_request req;
  374. cp.phy_handle = hcon->handle;
  375. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  376. hcon->handle);
  377. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  378. &cp.key_type)) {
  379. BT_DBG("Cannot create link key");
  380. return;
  381. }
  382. hci_req_init(&req, hdev);
  383. hci_req_add(&req, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
  384. hci_req_run(&req, accept_phylink_complete);
  385. }
  386. void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
  387. {
  388. struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
  389. struct amp_mgr *mgr = hs_hcon->amp_mgr;
  390. struct l2cap_chan *bredr_chan;
  391. BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
  392. if (!bredr_hdev || !mgr || !mgr->bredr_chan)
  393. return;
  394. bredr_chan = mgr->bredr_chan;
  395. l2cap_chan_lock(bredr_chan);
  396. set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
  397. bredr_chan->remote_amp_id = hs_hcon->remote_id;
  398. bredr_chan->local_amp_id = hs_hcon->hdev->id;
  399. bredr_chan->hs_hcon = hs_hcon;
  400. bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
  401. __l2cap_physical_cfm(bredr_chan, 0);
  402. l2cap_chan_unlock(bredr_chan);
  403. hci_dev_put(bredr_hdev);
  404. }
  405. void amp_create_logical_link(struct l2cap_chan *chan)
  406. {
  407. struct hci_conn *hs_hcon = chan->hs_hcon;
  408. struct hci_cp_create_accept_logical_link cp;
  409. struct hci_dev *hdev;
  410. BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
  411. &chan->conn->hcon->dst);
  412. if (!hs_hcon)
  413. return;
  414. hdev = hci_dev_hold(chan->hs_hcon->hdev);
  415. if (!hdev)
  416. return;
  417. cp.phy_handle = hs_hcon->handle;
  418. cp.tx_flow_spec.id = chan->local_id;
  419. cp.tx_flow_spec.stype = chan->local_stype;
  420. cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
  421. cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
  422. cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
  423. cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
  424. cp.rx_flow_spec.id = chan->remote_id;
  425. cp.rx_flow_spec.stype = chan->remote_stype;
  426. cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
  427. cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
  428. cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
  429. cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
  430. if (hs_hcon->out)
  431. hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
  432. &cp);
  433. else
  434. hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
  435. &cp);
  436. hci_dev_put(hdev);
  437. }
  438. void amp_disconnect_logical_link(struct hci_chan *hchan)
  439. {
  440. struct hci_conn *hcon = hchan->conn;
  441. struct hci_cp_disconn_logical_link cp;
  442. if (hcon->state != BT_CONNECTED) {
  443. BT_DBG("hchan %p not connected", hchan);
  444. return;
  445. }
  446. cp.log_handle = cpu_to_le16(hchan->handle);
  447. hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
  448. }
  449. void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
  450. {
  451. BT_DBG("hchan %p", hchan);
  452. hci_chan_del(hchan);
  453. }