amp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "a2mp.h"
  16. #include "amp.h"
  17. /* Remote AMP Controllers interface */
  18. void amp_ctrl_get(struct amp_ctrl *ctrl)
  19. {
  20. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  21. atomic_read(&ctrl->kref.refcount));
  22. kref_get(&ctrl->kref);
  23. }
  24. static void amp_ctrl_destroy(struct kref *kref)
  25. {
  26. struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
  27. BT_DBG("ctrl %p", ctrl);
  28. kfree(ctrl->assoc);
  29. kfree(ctrl);
  30. }
  31. int amp_ctrl_put(struct amp_ctrl *ctrl)
  32. {
  33. BT_DBG("ctrl %p orig refcnt %d", ctrl,
  34. atomic_read(&ctrl->kref.refcount));
  35. return kref_put(&ctrl->kref, &amp_ctrl_destroy);
  36. }
  37. struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id)
  38. {
  39. struct amp_ctrl *ctrl;
  40. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  41. if (!ctrl)
  42. return NULL;
  43. kref_init(&ctrl->kref);
  44. ctrl->id = id;
  45. mutex_lock(&mgr->amp_ctrls_lock);
  46. list_add(&ctrl->list, &mgr->amp_ctrls);
  47. mutex_unlock(&mgr->amp_ctrls_lock);
  48. BT_DBG("mgr %p ctrl %p", mgr, ctrl);
  49. return ctrl;
  50. }
  51. void amp_ctrl_list_flush(struct amp_mgr *mgr)
  52. {
  53. struct amp_ctrl *ctrl, *n;
  54. BT_DBG("mgr %p", mgr);
  55. mutex_lock(&mgr->amp_ctrls_lock);
  56. list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
  57. list_del(&ctrl->list);
  58. amp_ctrl_put(ctrl);
  59. }
  60. mutex_unlock(&mgr->amp_ctrls_lock);
  61. }
  62. struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
  63. {
  64. struct amp_ctrl *ctrl;
  65. BT_DBG("mgr %p id %d", mgr, id);
  66. mutex_lock(&mgr->amp_ctrls_lock);
  67. list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
  68. if (ctrl->id == id) {
  69. amp_ctrl_get(ctrl);
  70. mutex_unlock(&mgr->amp_ctrls_lock);
  71. return ctrl;
  72. }
  73. }
  74. mutex_unlock(&mgr->amp_ctrls_lock);
  75. return NULL;
  76. }
  77. /* Physical Link interface */
  78. static u8 __next_handle(struct amp_mgr *mgr)
  79. {
  80. if (++mgr->handle == 0)
  81. mgr->handle = 1;
  82. return mgr->handle;
  83. }
  84. struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
  85. u8 remote_id, bool out)
  86. {
  87. bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst;
  88. struct hci_conn *hcon;
  89. u8 role = out ? HCI_ROLE_MASTER : HCI_ROLE_SLAVE;
  90. hcon = hci_conn_add(hdev, AMP_LINK, dst, role);
  91. if (!hcon)
  92. return NULL;
  93. BT_DBG("hcon %p dst %pMR", hcon, dst);
  94. hcon->state = BT_CONNECT;
  95. hcon->attempt++;
  96. hcon->handle = __next_handle(mgr);
  97. hcon->remote_id = remote_id;
  98. hcon->amp_mgr = amp_mgr_get(mgr);
  99. return hcon;
  100. }
  101. /* AMP crypto key generation interface */
  102. static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
  103. {
  104. struct crypto_shash *tfm;
  105. struct shash_desc *shash;
  106. int ret;
  107. if (!ksize)
  108. return -EINVAL;
  109. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  110. if (IS_ERR(tfm)) {
  111. BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
  112. return PTR_ERR(tfm);
  113. }
  114. ret = crypto_shash_setkey(tfm, key, ksize);
  115. if (ret) {
  116. BT_DBG("crypto_ahash_setkey failed: err %d", ret);
  117. goto failed;
  118. }
  119. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  120. GFP_KERNEL);
  121. if (!shash) {
  122. ret = -ENOMEM;
  123. goto failed;
  124. }
  125. shash->tfm = tfm;
  126. shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  127. ret = crypto_shash_digest(shash, plaintext, psize, output);
  128. kfree(shash);
  129. failed:
  130. crypto_free_shash(tfm);
  131. return ret;
  132. }
  133. int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
  134. {
  135. struct hci_dev *hdev = conn->hdev;
  136. struct link_key *key;
  137. u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
  138. u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
  139. int err;
  140. if (!hci_conn_check_link_mode(conn))
  141. return -EACCES;
  142. BT_DBG("conn %p key_type %d", conn, conn->key_type);
  143. /* Legacy key */
  144. if (conn->key_type < 3) {
  145. BT_ERR("Legacy key type %d", conn->key_type);
  146. return -EACCES;
  147. }
  148. *type = conn->key_type;
  149. *len = HCI_AMP_LINK_KEY_SIZE;
  150. key = hci_find_link_key(hdev, &conn->dst);
  151. if (!key) {
  152. BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
  153. return -EACCES;
  154. }
  155. /* BR/EDR Link Key concatenated together with itself */
  156. memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
  157. memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
  158. /* Derive Generic AMP Link Key (gamp) */
  159. err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
  160. if (err) {
  161. BT_ERR("Could not derive Generic AMP Key: err %d", err);
  162. return err;
  163. }
  164. if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
  165. BT_DBG("Use Generic AMP Key (gamp)");
  166. memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
  167. return err;
  168. }
  169. /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
  170. return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
  171. }
  172. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  173. {
  174. struct hci_cp_read_local_amp_assoc cp;
  175. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  176. BT_DBG("%s handle %d", hdev->name, phy_handle);
  177. cp.phy_handle = phy_handle;
  178. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  179. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  180. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  181. }
  182. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  183. {
  184. struct hci_cp_read_local_amp_assoc cp;
  185. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  186. memset(&cp, 0, sizeof(cp));
  187. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  188. set_bit(READ_LOC_AMP_ASSOC, &mgr->state);
  189. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  190. }
  191. void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
  192. struct hci_conn *hcon)
  193. {
  194. struct hci_cp_read_local_amp_assoc cp;
  195. struct amp_mgr *mgr = hcon->amp_mgr;
  196. cp.phy_handle = hcon->handle;
  197. cp.len_so_far = cpu_to_le16(0);
  198. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  199. set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state);
  200. /* Read Local AMP Assoc final link information data */
  201. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  202. }
  203. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  204. static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
  205. struct hci_conn *hcon)
  206. {
  207. struct hci_cp_write_remote_amp_assoc *cp;
  208. struct amp_mgr *mgr = hcon->amp_mgr;
  209. struct amp_ctrl *ctrl;
  210. u16 frag_len, len;
  211. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  212. if (!ctrl)
  213. return false;
  214. if (!ctrl->assoc_rem_len) {
  215. BT_DBG("all fragments are written");
  216. ctrl->assoc_rem_len = ctrl->assoc_len;
  217. ctrl->assoc_len_so_far = 0;
  218. amp_ctrl_put(ctrl);
  219. return true;
  220. }
  221. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  222. len = frag_len + sizeof(*cp);
  223. cp = kzalloc(len, GFP_KERNEL);
  224. if (!cp) {
  225. amp_ctrl_put(ctrl);
  226. return false;
  227. }
  228. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  229. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  230. cp->phy_handle = hcon->handle;
  231. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  232. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  233. memcpy(cp->frag, ctrl->assoc, frag_len);
  234. ctrl->assoc_len_so_far += frag_len;
  235. ctrl->assoc_rem_len -= frag_len;
  236. amp_ctrl_put(ctrl);
  237. hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  238. kfree(cp);
  239. return false;
  240. }
  241. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  242. {
  243. struct hci_conn *hcon;
  244. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  245. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  246. if (!hcon)
  247. return;
  248. /* Send A2MP create phylink rsp when all fragments are written */
  249. if (amp_write_rem_assoc_frag(hdev, hcon))
  250. a2mp_send_create_phy_link_rsp(hdev, 0);
  251. }
  252. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  253. {
  254. struct hci_conn *hcon;
  255. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  256. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  257. if (!hcon)
  258. return;
  259. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  260. amp_write_rem_assoc_frag(hdev, hcon);
  261. }
  262. void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  263. struct hci_conn *hcon)
  264. {
  265. struct hci_cp_create_phy_link cp;
  266. cp.phy_handle = hcon->handle;
  267. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  268. hcon->handle);
  269. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  270. &cp.key_type)) {
  271. BT_DBG("Cannot create link key");
  272. return;
  273. }
  274. hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  275. }
  276. void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  277. struct hci_conn *hcon)
  278. {
  279. struct hci_cp_accept_phy_link cp;
  280. cp.phy_handle = hcon->handle;
  281. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  282. hcon->handle);
  283. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  284. &cp.key_type)) {
  285. BT_DBG("Cannot create link key");
  286. return;
  287. }
  288. hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
  289. }
  290. void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
  291. {
  292. struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
  293. struct amp_mgr *mgr = hs_hcon->amp_mgr;
  294. struct l2cap_chan *bredr_chan;
  295. BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
  296. if (!bredr_hdev || !mgr || !mgr->bredr_chan)
  297. return;
  298. bredr_chan = mgr->bredr_chan;
  299. l2cap_chan_lock(bredr_chan);
  300. set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
  301. bredr_chan->remote_amp_id = hs_hcon->remote_id;
  302. bredr_chan->local_amp_id = hs_hcon->hdev->id;
  303. bredr_chan->hs_hcon = hs_hcon;
  304. bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
  305. __l2cap_physical_cfm(bredr_chan, 0);
  306. l2cap_chan_unlock(bredr_chan);
  307. hci_dev_put(bredr_hdev);
  308. }
  309. void amp_create_logical_link(struct l2cap_chan *chan)
  310. {
  311. struct hci_conn *hs_hcon = chan->hs_hcon;
  312. struct hci_cp_create_accept_logical_link cp;
  313. struct hci_dev *hdev;
  314. BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
  315. &chan->conn->hcon->dst);
  316. if (!hs_hcon)
  317. return;
  318. hdev = hci_dev_hold(chan->hs_hcon->hdev);
  319. if (!hdev)
  320. return;
  321. cp.phy_handle = hs_hcon->handle;
  322. cp.tx_flow_spec.id = chan->local_id;
  323. cp.tx_flow_spec.stype = chan->local_stype;
  324. cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
  325. cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
  326. cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
  327. cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
  328. cp.rx_flow_spec.id = chan->remote_id;
  329. cp.rx_flow_spec.stype = chan->remote_stype;
  330. cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
  331. cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
  332. cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
  333. cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
  334. if (hs_hcon->out)
  335. hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
  336. &cp);
  337. else
  338. hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
  339. &cp);
  340. hci_dev_put(hdev);
  341. }
  342. void amp_disconnect_logical_link(struct hci_chan *hchan)
  343. {
  344. struct hci_conn *hcon = hchan->conn;
  345. struct hci_cp_disconn_logical_link cp;
  346. if (hcon->state != BT_CONNECTED) {
  347. BT_DBG("hchan %p not connected", hchan);
  348. return;
  349. }
  350. cp.log_handle = cpu_to_le16(hchan->handle);
  351. hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
  352. }
  353. void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
  354. {
  355. BT_DBG("hchan %p", hchan);
  356. hci_chan_del(hchan);
  357. }