amp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  128. {
  129. struct hci_cp_read_local_amp_assoc cp;
  130. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  131. BT_DBG("%s handle %d", hdev->name, phy_handle);
  132. cp.phy_handle = phy_handle;
  133. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  134. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  135. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  136. }
  137. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  138. {
  139. struct hci_cp_read_local_amp_assoc cp;
  140. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  141. memset(&cp, 0, sizeof(cp));
  142. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  143. mgr->state = READ_LOC_AMP_ASSOC;
  144. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  145. }