amp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 <net/bluetooth/a2mp.h>
  15. #include <net/bluetooth/amp.h>
  16. #include <crypto/hash.h>
  17. /* Remote AMP Controllers interface */
  18. static 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)
  38. {
  39. struct amp_ctrl *ctrl;
  40. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  41. if (!ctrl)
  42. return NULL;
  43. mutex_lock(&mgr->amp_ctrls_lock);
  44. list_add(&ctrl->list, &mgr->amp_ctrls);
  45. mutex_unlock(&mgr->amp_ctrls_lock);
  46. kref_init(&ctrl->kref);
  47. BT_DBG("mgr %p ctrl %p", mgr, ctrl);
  48. return ctrl;
  49. }
  50. void amp_ctrl_list_flush(struct amp_mgr *mgr)
  51. {
  52. struct amp_ctrl *ctrl, *n;
  53. BT_DBG("mgr %p", mgr);
  54. mutex_lock(&mgr->amp_ctrls_lock);
  55. list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
  56. list_del(&ctrl->list);
  57. amp_ctrl_put(ctrl);
  58. }
  59. mutex_unlock(&mgr->amp_ctrls_lock);
  60. }
  61. struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
  62. {
  63. struct amp_ctrl *ctrl;
  64. BT_DBG("mgr %p id %d", mgr, id);
  65. mutex_lock(&mgr->amp_ctrls_lock);
  66. list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
  67. if (ctrl->id == id) {
  68. amp_ctrl_get(ctrl);
  69. mutex_unlock(&mgr->amp_ctrls_lock);
  70. return ctrl;
  71. }
  72. }
  73. mutex_unlock(&mgr->amp_ctrls_lock);
  74. return NULL;
  75. }
  76. /* Physical Link interface */
  77. static u8 __next_handle(struct amp_mgr *mgr)
  78. {
  79. if (++mgr->handle == 0)
  80. mgr->handle = 1;
  81. return mgr->handle;
  82. }
  83. struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
  84. u8 remote_id)
  85. {
  86. bdaddr_t *dst = mgr->l2cap_conn->dst;
  87. struct hci_conn *hcon;
  88. hcon = hci_conn_add(hdev, AMP_LINK, dst);
  89. if (!hcon)
  90. return NULL;
  91. hcon->state = BT_CONNECT;
  92. hcon->out = true;
  93. hcon->attempt++;
  94. hcon->handle = __next_handle(mgr);
  95. hcon->remote_id = remote_id;
  96. hcon->amp_mgr = mgr;
  97. return hcon;
  98. }
  99. /* AMP crypto key generation interface */
  100. static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
  101. {
  102. int ret = 0;
  103. struct crypto_shash *tfm;
  104. if (!ksize)
  105. return -EINVAL;
  106. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  107. if (IS_ERR(tfm)) {
  108. BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
  109. return PTR_ERR(tfm);
  110. }
  111. ret = crypto_shash_setkey(tfm, key, ksize);
  112. if (ret) {
  113. BT_DBG("crypto_ahash_setkey failed: err %d", ret);
  114. } else {
  115. struct {
  116. struct shash_desc shash;
  117. char ctx[crypto_shash_descsize(tfm)];
  118. } desc;
  119. desc.shash.tfm = tfm;
  120. desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  121. ret = crypto_shash_digest(&desc.shash, plaintext, psize,
  122. output);
  123. }
  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_ERR("Legacy key type %d", 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. /* BR/EDR Link Key concatenated together with itself */
  146. memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
  147. memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
  148. /* Derive Generic AMP Link Key (gamp) */
  149. err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
  150. if (err) {
  151. BT_ERR("Could not derive Generic AMP Key: err %d", err);
  152. return err;
  153. }
  154. if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
  155. BT_DBG("Use Generic AMP Key (gamp)");
  156. memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
  157. return err;
  158. }
  159. /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
  160. return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
  161. }
  162. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  163. {
  164. struct hci_cp_read_local_amp_assoc cp;
  165. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  166. BT_DBG("%s handle %d", hdev->name, phy_handle);
  167. cp.phy_handle = phy_handle;
  168. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  169. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  170. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  171. }
  172. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  173. {
  174. struct hci_cp_read_local_amp_assoc cp;
  175. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  176. memset(&cp, 0, sizeof(cp));
  177. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  178. mgr->state = READ_LOC_AMP_ASSOC;
  179. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  180. }
  181. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  182. static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
  183. struct hci_conn *hcon)
  184. {
  185. struct hci_cp_write_remote_amp_assoc *cp;
  186. struct amp_mgr *mgr = hcon->amp_mgr;
  187. struct amp_ctrl *ctrl;
  188. u16 frag_len, len;
  189. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  190. if (!ctrl)
  191. return false;
  192. if (!ctrl->assoc_rem_len) {
  193. BT_DBG("all fragments are written");
  194. ctrl->assoc_rem_len = ctrl->assoc_len;
  195. ctrl->assoc_len_so_far = 0;
  196. amp_ctrl_put(ctrl);
  197. return true;
  198. }
  199. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  200. len = frag_len + sizeof(*cp);
  201. cp = kzalloc(len, GFP_KERNEL);
  202. if (!cp) {
  203. amp_ctrl_put(ctrl);
  204. return false;
  205. }
  206. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  207. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  208. cp->phy_handle = hcon->handle;
  209. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  210. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  211. memcpy(cp->frag, ctrl->assoc, frag_len);
  212. ctrl->assoc_len_so_far += frag_len;
  213. ctrl->assoc_rem_len -= frag_len;
  214. amp_ctrl_put(ctrl);
  215. hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  216. kfree(cp);
  217. return false;
  218. }
  219. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  220. {
  221. struct hci_conn *hcon;
  222. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  223. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  224. if (!hcon)
  225. return;
  226. amp_write_rem_assoc_frag(hdev, hcon);
  227. }
  228. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  229. {
  230. struct hci_conn *hcon;
  231. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  232. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  233. if (!hcon)
  234. return;
  235. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  236. amp_write_rem_assoc_frag(hdev, hcon);
  237. }
  238. void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  239. struct hci_conn *hcon)
  240. {
  241. struct hci_cp_create_phy_link cp;
  242. cp.phy_handle = hcon->handle;
  243. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  244. hcon->handle);
  245. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  246. &cp.key_type)) {
  247. BT_DBG("Cannot create link key");
  248. return;
  249. }
  250. hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  251. }