amp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. 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)
  86. {
  87. bdaddr_t *dst = mgr->l2cap_conn->dst;
  88. struct hci_conn *hcon;
  89. hcon = hci_conn_add(hdev, AMP_LINK, dst);
  90. if (!hcon)
  91. return NULL;
  92. hcon->state = BT_CONNECT;
  93. hcon->out = true;
  94. hcon->attempt++;
  95. hcon->handle = __next_handle(mgr);
  96. hcon->remote_id = remote_id;
  97. hcon->amp_mgr = mgr;
  98. return hcon;
  99. }
  100. /* AMP crypto key generation interface */
  101. static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
  102. {
  103. int ret = 0;
  104. struct crypto_shash *tfm;
  105. if (!ksize)
  106. return -EINVAL;
  107. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  108. if (IS_ERR(tfm)) {
  109. BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
  110. return PTR_ERR(tfm);
  111. }
  112. ret = crypto_shash_setkey(tfm, key, ksize);
  113. if (ret) {
  114. BT_DBG("crypto_ahash_setkey failed: err %d", ret);
  115. } else {
  116. struct {
  117. struct shash_desc shash;
  118. char ctx[crypto_shash_descsize(tfm)];
  119. } desc;
  120. desc.shash.tfm = tfm;
  121. desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  122. ret = crypto_shash_digest(&desc.shash, plaintext, psize,
  123. output);
  124. }
  125. crypto_free_shash(tfm);
  126. return ret;
  127. }
  128. int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
  129. {
  130. struct hci_dev *hdev = conn->hdev;
  131. struct link_key *key;
  132. u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
  133. u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
  134. int err;
  135. if (!hci_conn_check_link_mode(conn))
  136. return -EACCES;
  137. BT_DBG("conn %p key_type %d", conn, conn->key_type);
  138. /* Legacy key */
  139. if (conn->key_type < 3) {
  140. BT_ERR("Legacy key type %d", conn->key_type);
  141. return -EACCES;
  142. }
  143. *type = conn->key_type;
  144. *len = HCI_AMP_LINK_KEY_SIZE;
  145. key = hci_find_link_key(hdev, &conn->dst);
  146. if (!key) {
  147. BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
  148. return -EACCES;
  149. }
  150. /* BR/EDR Link Key concatenated together with itself */
  151. memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
  152. memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
  153. /* Derive Generic AMP Link Key (gamp) */
  154. err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
  155. if (err) {
  156. BT_ERR("Could not derive Generic AMP Key: err %d", err);
  157. return err;
  158. }
  159. if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
  160. BT_DBG("Use Generic AMP Key (gamp)");
  161. memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
  162. return err;
  163. }
  164. /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
  165. return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
  166. }
  167. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  168. {
  169. struct hci_cp_read_local_amp_assoc cp;
  170. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  171. BT_DBG("%s handle %d", hdev->name, phy_handle);
  172. cp.phy_handle = phy_handle;
  173. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  174. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  175. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  176. }
  177. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  178. {
  179. struct hci_cp_read_local_amp_assoc cp;
  180. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  181. memset(&cp, 0, sizeof(cp));
  182. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  183. mgr->state = READ_LOC_AMP_ASSOC;
  184. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  185. }
  186. void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
  187. struct hci_conn *hcon)
  188. {
  189. struct hci_cp_read_local_amp_assoc cp;
  190. struct amp_mgr *mgr = hcon->amp_mgr;
  191. cp.phy_handle = hcon->handle;
  192. cp.len_so_far = cpu_to_le16(0);
  193. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  194. mgr->state = READ_LOC_AMP_ASSOC_FINAL;
  195. /* Read Local AMP Assoc final link information data */
  196. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  197. }
  198. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  199. static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
  200. struct hci_conn *hcon)
  201. {
  202. struct hci_cp_write_remote_amp_assoc *cp;
  203. struct amp_mgr *mgr = hcon->amp_mgr;
  204. struct amp_ctrl *ctrl;
  205. u16 frag_len, len;
  206. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  207. if (!ctrl)
  208. return false;
  209. if (!ctrl->assoc_rem_len) {
  210. BT_DBG("all fragments are written");
  211. ctrl->assoc_rem_len = ctrl->assoc_len;
  212. ctrl->assoc_len_so_far = 0;
  213. amp_ctrl_put(ctrl);
  214. return true;
  215. }
  216. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  217. len = frag_len + sizeof(*cp);
  218. cp = kzalloc(len, GFP_KERNEL);
  219. if (!cp) {
  220. amp_ctrl_put(ctrl);
  221. return false;
  222. }
  223. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  224. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  225. cp->phy_handle = hcon->handle;
  226. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  227. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  228. memcpy(cp->frag, ctrl->assoc, frag_len);
  229. ctrl->assoc_len_so_far += frag_len;
  230. ctrl->assoc_rem_len -= frag_len;
  231. amp_ctrl_put(ctrl);
  232. hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  233. kfree(cp);
  234. return false;
  235. }
  236. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  237. {
  238. struct hci_conn *hcon;
  239. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  240. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  241. if (!hcon)
  242. return;
  243. amp_write_rem_assoc_frag(hdev, hcon);
  244. }
  245. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  246. {
  247. struct hci_conn *hcon;
  248. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  249. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  250. if (!hcon)
  251. return;
  252. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  253. amp_write_rem_assoc_frag(hdev, hcon);
  254. }
  255. void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  256. struct hci_conn *hcon)
  257. {
  258. struct hci_cp_create_phy_link cp;
  259. cp.phy_handle = hcon->handle;
  260. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  261. hcon->handle);
  262. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  263. &cp.key_type)) {
  264. BT_DBG("Cannot create link key");
  265. return;
  266. }
  267. hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  268. }
  269. void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  270. struct hci_conn *hcon)
  271. {
  272. struct hci_cp_accept_phy_link cp;
  273. cp.phy_handle = hcon->handle;
  274. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  275. hcon->handle);
  276. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  277. &cp.key_type)) {
  278. BT_DBG("Cannot create link key");
  279. return;
  280. }
  281. hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
  282. }