amp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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, bool out)
  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. BT_DBG("hcon %p dst %pMR", hcon, dst);
  93. hcon->state = BT_CONNECT;
  94. hcon->attempt++;
  95. hcon->handle = __next_handle(mgr);
  96. hcon->remote_id = remote_id;
  97. hcon->amp_mgr = amp_mgr_get(mgr);
  98. hcon->out = out;
  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. int ret = 0;
  105. struct crypto_shash *tfm;
  106. if (!ksize)
  107. return -EINVAL;
  108. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  109. if (IS_ERR(tfm)) {
  110. BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
  111. return PTR_ERR(tfm);
  112. }
  113. ret = crypto_shash_setkey(tfm, key, ksize);
  114. if (ret) {
  115. BT_DBG("crypto_ahash_setkey failed: err %d", ret);
  116. } else {
  117. struct {
  118. struct shash_desc shash;
  119. char ctx[crypto_shash_descsize(tfm)];
  120. } desc;
  121. desc.shash.tfm = tfm;
  122. desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  123. ret = crypto_shash_digest(&desc.shash, plaintext, psize,
  124. output);
  125. }
  126. crypto_free_shash(tfm);
  127. return ret;
  128. }
  129. int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
  130. {
  131. struct hci_dev *hdev = conn->hdev;
  132. struct link_key *key;
  133. u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
  134. u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
  135. int err;
  136. if (!hci_conn_check_link_mode(conn))
  137. return -EACCES;
  138. BT_DBG("conn %p key_type %d", conn, conn->key_type);
  139. /* Legacy key */
  140. if (conn->key_type < 3) {
  141. BT_ERR("Legacy key type %d", conn->key_type);
  142. return -EACCES;
  143. }
  144. *type = conn->key_type;
  145. *len = HCI_AMP_LINK_KEY_SIZE;
  146. key = hci_find_link_key(hdev, &conn->dst);
  147. if (!key) {
  148. BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst);
  149. return -EACCES;
  150. }
  151. /* BR/EDR Link Key concatenated together with itself */
  152. memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
  153. memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
  154. /* Derive Generic AMP Link Key (gamp) */
  155. err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
  156. if (err) {
  157. BT_ERR("Could not derive Generic AMP Key: err %d", err);
  158. return err;
  159. }
  160. if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
  161. BT_DBG("Use Generic AMP Key (gamp)");
  162. memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
  163. return err;
  164. }
  165. /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
  166. return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
  167. }
  168. void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
  169. {
  170. struct hci_cp_read_local_amp_assoc cp;
  171. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  172. BT_DBG("%s handle %d", hdev->name, phy_handle);
  173. cp.phy_handle = phy_handle;
  174. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  175. cp.len_so_far = cpu_to_le16(loc_assoc->offset);
  176. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  177. }
  178. void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
  179. {
  180. struct hci_cp_read_local_amp_assoc cp;
  181. memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
  182. memset(&cp, 0, sizeof(cp));
  183. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  184. mgr->state = READ_LOC_AMP_ASSOC;
  185. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  186. }
  187. void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
  188. struct hci_conn *hcon)
  189. {
  190. struct hci_cp_read_local_amp_assoc cp;
  191. struct amp_mgr *mgr = hcon->amp_mgr;
  192. cp.phy_handle = hcon->handle;
  193. cp.len_so_far = cpu_to_le16(0);
  194. cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
  195. mgr->state = READ_LOC_AMP_ASSOC_FINAL;
  196. /* Read Local AMP Assoc final link information data */
  197. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
  198. }
  199. /* Write AMP Assoc data fragments, returns true with last fragment written*/
  200. static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
  201. struct hci_conn *hcon)
  202. {
  203. struct hci_cp_write_remote_amp_assoc *cp;
  204. struct amp_mgr *mgr = hcon->amp_mgr;
  205. struct amp_ctrl *ctrl;
  206. u16 frag_len, len;
  207. ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
  208. if (!ctrl)
  209. return false;
  210. if (!ctrl->assoc_rem_len) {
  211. BT_DBG("all fragments are written");
  212. ctrl->assoc_rem_len = ctrl->assoc_len;
  213. ctrl->assoc_len_so_far = 0;
  214. amp_ctrl_put(ctrl);
  215. return true;
  216. }
  217. frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
  218. len = frag_len + sizeof(*cp);
  219. cp = kzalloc(len, GFP_KERNEL);
  220. if (!cp) {
  221. amp_ctrl_put(ctrl);
  222. return false;
  223. }
  224. BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
  225. hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
  226. cp->phy_handle = hcon->handle;
  227. cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
  228. cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
  229. memcpy(cp->frag, ctrl->assoc, frag_len);
  230. ctrl->assoc_len_so_far += frag_len;
  231. ctrl->assoc_rem_len -= frag_len;
  232. amp_ctrl_put(ctrl);
  233. hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
  234. kfree(cp);
  235. return false;
  236. }
  237. void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
  238. {
  239. struct hci_conn *hcon;
  240. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  241. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  242. if (!hcon)
  243. return;
  244. amp_write_rem_assoc_frag(hdev, hcon);
  245. }
  246. void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
  247. {
  248. struct hci_conn *hcon;
  249. BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
  250. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  251. if (!hcon)
  252. return;
  253. BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
  254. amp_write_rem_assoc_frag(hdev, hcon);
  255. }
  256. void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  257. struct hci_conn *hcon)
  258. {
  259. struct hci_cp_create_phy_link cp;
  260. cp.phy_handle = hcon->handle;
  261. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  262. hcon->handle);
  263. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  264. &cp.key_type)) {
  265. BT_DBG("Cannot create link key");
  266. return;
  267. }
  268. hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
  269. }
  270. void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
  271. struct hci_conn *hcon)
  272. {
  273. struct hci_cp_accept_phy_link cp;
  274. cp.phy_handle = hcon->handle;
  275. BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
  276. hcon->handle);
  277. if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
  278. &cp.key_type)) {
  279. BT_DBG("Cannot create link key");
  280. return;
  281. }
  282. hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
  283. }
  284. void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon)
  285. {
  286. struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev);
  287. struct amp_mgr *mgr = hs_hcon->amp_mgr;
  288. struct l2cap_chan *bredr_chan;
  289. BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr);
  290. if (!bredr_hdev || !mgr || !mgr->bredr_chan)
  291. return;
  292. bredr_chan = mgr->bredr_chan;
  293. l2cap_chan_lock(bredr_chan);
  294. set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags);
  295. bredr_chan->remote_amp_id = hs_hcon->remote_id;
  296. bredr_chan->local_amp_id = hs_hcon->hdev->id;
  297. bredr_chan->hs_hcon = hs_hcon;
  298. bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu;
  299. __l2cap_physical_cfm(bredr_chan, 0);
  300. l2cap_chan_unlock(bredr_chan);
  301. hci_dev_put(bredr_hdev);
  302. }
  303. void amp_create_logical_link(struct l2cap_chan *chan)
  304. {
  305. struct hci_cp_create_accept_logical_link cp;
  306. struct hci_conn *hcon;
  307. struct hci_dev *hdev;
  308. BT_DBG("chan %p", chan);
  309. if (!chan->hs_hcon)
  310. return;
  311. hdev = hci_dev_hold(chan->hs_hcon->hdev);
  312. if (!hdev)
  313. return;
  314. BT_DBG("chan %p dst %pMR", chan, chan->conn->dst);
  315. hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, chan->conn->dst);
  316. if (!hcon)
  317. goto done;
  318. cp.phy_handle = hcon->handle;
  319. cp.tx_flow_spec.id = chan->local_id;
  320. cp.tx_flow_spec.stype = chan->local_stype;
  321. cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu);
  322. cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
  323. cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat);
  324. cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to);
  325. cp.rx_flow_spec.id = chan->remote_id;
  326. cp.rx_flow_spec.stype = chan->remote_stype;
  327. cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu);
  328. cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime);
  329. cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat);
  330. cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to);
  331. if (hcon->out)
  332. hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp),
  333. &cp);
  334. else
  335. hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp),
  336. &cp);
  337. done:
  338. hci_dev_put(hdev);
  339. }
  340. void amp_disconnect_logical_link(struct hci_chan *hchan)
  341. {
  342. struct hci_conn *hcon = hchan->conn;
  343. struct hci_cp_disconn_logical_link cp;
  344. if (hcon->state != BT_CONNECTED) {
  345. BT_DBG("hchan %p not connected", hchan);
  346. return;
  347. }
  348. cp.log_handle = cpu_to_le16(hchan->handle);
  349. hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp);
  350. }
  351. void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason)
  352. {
  353. BT_DBG("hchan %p", hchan);
  354. hci_chan_del(hchan);
  355. }