blob: 845e43073c40b6fb241dbb7644a5bc7f527820cc [file] [log] [blame]
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03001/*
2 Copyright (c) 2011,2012 Intel Corp.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*/
13
14#include <net/bluetooth/bluetooth.h>
15#include <net/bluetooth/hci.h>
16#include <net/bluetooth/hci_core.h>
17#include <net/bluetooth/a2mp.h>
18#include <net/bluetooth/amp.h>
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +030019#include <crypto/hash.h>
Andrei Emeltchenko903e4542012-09-27 17:26:09 +030020
Andrei Emeltchenko52c0d6e2012-09-27 17:26:12 +030021/* Remote AMP Controllers interface */
22static void amp_ctrl_get(struct amp_ctrl *ctrl)
23{
24 BT_DBG("ctrl %p orig refcnt %d", ctrl,
25 atomic_read(&ctrl->kref.refcount));
26
27 kref_get(&ctrl->kref);
28}
29
30static void amp_ctrl_destroy(struct kref *kref)
31{
32 struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref);
33
34 BT_DBG("ctrl %p", ctrl);
35
36 kfree(ctrl->assoc);
37 kfree(ctrl);
38}
39
40int amp_ctrl_put(struct amp_ctrl *ctrl)
41{
42 BT_DBG("ctrl %p orig refcnt %d", ctrl,
43 atomic_read(&ctrl->kref.refcount));
44
45 return kref_put(&ctrl->kref, &amp_ctrl_destroy);
46}
47
48struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr)
49{
50 struct amp_ctrl *ctrl;
51
52 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
53 if (!ctrl)
54 return NULL;
55
56 mutex_lock(&mgr->amp_ctrls_lock);
57 list_add(&ctrl->list, &mgr->amp_ctrls);
58 mutex_unlock(&mgr->amp_ctrls_lock);
59
60 kref_init(&ctrl->kref);
61
62 BT_DBG("mgr %p ctrl %p", mgr, ctrl);
63
64 return ctrl;
65}
66
67void amp_ctrl_list_flush(struct amp_mgr *mgr)
68{
69 struct amp_ctrl *ctrl, *n;
70
71 BT_DBG("mgr %p", mgr);
72
73 mutex_lock(&mgr->amp_ctrls_lock);
74 list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) {
75 list_del(&ctrl->list);
76 amp_ctrl_put(ctrl);
77 }
78 mutex_unlock(&mgr->amp_ctrls_lock);
79}
80
81struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id)
82{
83 struct amp_ctrl *ctrl;
84
85 BT_DBG("mgr %p id %d", mgr, id);
86
87 mutex_lock(&mgr->amp_ctrls_lock);
88 list_for_each_entry(ctrl, &mgr->amp_ctrls, list) {
89 if (ctrl->id == id) {
90 amp_ctrl_get(ctrl);
91 mutex_unlock(&mgr->amp_ctrls_lock);
92 return ctrl;
93 }
94 }
95 mutex_unlock(&mgr->amp_ctrls_lock);
96
97 return NULL;
98}
99
Andrei Emeltchenko3161ae12012-09-27 17:26:11 +0300100/* Physical Link interface */
101static u8 __next_handle(struct amp_mgr *mgr)
102{
103 if (++mgr->handle == 0)
104 mgr->handle = 1;
105
106 return mgr->handle;
107}
108
109struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
110 u8 remote_id)
111{
112 bdaddr_t *dst = mgr->l2cap_conn->dst;
113 struct hci_conn *hcon;
114
115 hcon = hci_conn_add(hdev, AMP_LINK, dst);
116 if (!hcon)
117 return NULL;
118
119 hcon->state = BT_CONNECT;
120 hcon->out = true;
121 hcon->attempt++;
122 hcon->handle = __next_handle(mgr);
123 hcon->remote_id = remote_id;
124 hcon->amp_mgr = mgr;
125
126 return hcon;
127}
128
Dmitry Kasatkinba221bb2012-09-27 17:26:17 +0300129/* AMP crypto key generation interface */
130static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output)
131{
132 int ret = 0;
133 struct crypto_shash *tfm;
134
135 if (!ksize)
136 return -EINVAL;
137
138 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
139 if (IS_ERR(tfm)) {
140 BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm));
141 return PTR_ERR(tfm);
142 }
143
144 ret = crypto_shash_setkey(tfm, key, ksize);
145 if (ret) {
146 BT_DBG("crypto_ahash_setkey failed: err %d", ret);
147 } else {
148 struct {
149 struct shash_desc shash;
150 char ctx[crypto_shash_descsize(tfm)];
151 } desc;
152
153 desc.shash.tfm = tfm;
154 desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
155
156 ret = crypto_shash_digest(&desc.shash, plaintext, psize,
157 output);
158 }
159
160 crypto_free_shash(tfm);
161 return ret;
162}
163
Andrei Emeltchenko5a349182012-09-27 17:26:18 +0300164int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type)
165{
166 struct hci_dev *hdev = conn->hdev;
167 struct link_key *key;
168 u8 keybuf[HCI_AMP_LINK_KEY_SIZE];
169 u8 gamp_key[HCI_AMP_LINK_KEY_SIZE];
170 int err;
171
172 if (!hci_conn_check_link_mode(conn))
173 return -EACCES;
174
175 BT_DBG("conn %p key_type %d", conn, conn->key_type);
176
177 /* Legacy key */
178 if (conn->key_type < 3) {
179 BT_ERR("Legacy key type %d", conn->key_type);
180 return -EACCES;
181 }
182
183 *type = conn->key_type;
184 *len = HCI_AMP_LINK_KEY_SIZE;
185
186 key = hci_find_link_key(hdev, &conn->dst);
187
188 /* BR/EDR Link Key concatenated together with itself */
189 memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE);
190 memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE);
191
192 /* Derive Generic AMP Link Key (gamp) */
193 err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key);
194 if (err) {
195 BT_ERR("Could not derive Generic AMP Key: err %d", err);
196 return err;
197 }
198
199 if (conn->key_type == HCI_LK_DEBUG_COMBINATION) {
200 BT_DBG("Use Generic AMP Key (gamp)");
201 memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE);
202 return err;
203 }
204
205 /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */
206 return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data);
207}
208
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300209void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle)
210{
211 struct hci_cp_read_local_amp_assoc cp;
212 struct amp_assoc *loc_assoc = &hdev->loc_assoc;
213
214 BT_DBG("%s handle %d", hdev->name, phy_handle);
215
216 cp.phy_handle = phy_handle;
217 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
218 cp.len_so_far = cpu_to_le16(loc_assoc->offset);
219
220 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
221}
222
223void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr)
224{
225 struct hci_cp_read_local_amp_assoc cp;
226
227 memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc));
228 memset(&cp, 0, sizeof(cp));
229
230 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
231
232 mgr->state = READ_LOC_AMP_ASSOC;
233 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
234}
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +0300235
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300236void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
237 struct hci_conn *hcon)
238{
239 struct hci_cp_read_local_amp_assoc cp;
240 struct amp_mgr *mgr = hcon->amp_mgr;
241
242 cp.phy_handle = hcon->handle;
243 cp.len_so_far = cpu_to_le16(0);
244 cp.max_len = cpu_to_le16(hdev->amp_assoc_size);
245
246 mgr->state = READ_LOC_AMP_ASSOC_FINAL;
247
248 /* Read Local AMP Assoc final link information data */
249 hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp);
250}
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +0300251
252/* Write AMP Assoc data fragments, returns true with last fragment written*/
253static bool amp_write_rem_assoc_frag(struct hci_dev *hdev,
254 struct hci_conn *hcon)
255{
256 struct hci_cp_write_remote_amp_assoc *cp;
257 struct amp_mgr *mgr = hcon->amp_mgr;
258 struct amp_ctrl *ctrl;
259 u16 frag_len, len;
260
261 ctrl = amp_ctrl_lookup(mgr, hcon->remote_id);
262 if (!ctrl)
263 return false;
264
265 if (!ctrl->assoc_rem_len) {
266 BT_DBG("all fragments are written");
267 ctrl->assoc_rem_len = ctrl->assoc_len;
268 ctrl->assoc_len_so_far = 0;
269
270 amp_ctrl_put(ctrl);
271 return true;
272 }
273
274 frag_len = min_t(u16, 248, ctrl->assoc_rem_len);
275 len = frag_len + sizeof(*cp);
276
277 cp = kzalloc(len, GFP_KERNEL);
278 if (!cp) {
279 amp_ctrl_put(ctrl);
280 return false;
281 }
282
283 BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u",
284 hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len);
285
286 cp->phy_handle = hcon->handle;
287 cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far);
288 cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len);
289 memcpy(cp->frag, ctrl->assoc, frag_len);
290
291 ctrl->assoc_len_so_far += frag_len;
292 ctrl->assoc_rem_len -= frag_len;
293
294 amp_ctrl_put(ctrl);
295
296 hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp);
297
298 kfree(cp);
299
300 return false;
301}
302
303void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle)
304{
305 struct hci_conn *hcon;
306
307 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
308
309 hcon = hci_conn_hash_lookup_handle(hdev, handle);
310 if (!hcon)
311 return;
312
313 amp_write_rem_assoc_frag(hdev, hcon);
314}
315
316void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle)
317{
318 struct hci_conn *hcon;
319
320 BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle);
321
322 hcon = hci_conn_hash_lookup_handle(hdev, handle);
323 if (!hcon)
324 return;
325
326 BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon);
327
328 amp_write_rem_assoc_frag(hdev, hcon);
329}
330
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +0300331void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
332 struct hci_conn *hcon)
333{
334 struct hci_cp_create_phy_link cp;
335
336 cp.phy_handle = hcon->handle;
337
338 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
339 hcon->handle);
340
341 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
342 &cp.key_type)) {
343 BT_DBG("Cannot create link key");
344 return;
345 }
346
347 hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp);
348}
Andrei Emeltchenkodffa3872012-09-27 17:26:23 +0300349
350void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
351 struct hci_conn *hcon)
352{
353 struct hci_cp_accept_phy_link cp;
354
355 cp.phy_handle = hcon->handle;
356
357 BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon,
358 hcon->handle);
359
360 if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len,
361 &cp.key_type)) {
362 BT_DBG("Cannot create link key");
363 return;
364 }
365
366 hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp);
367}