Commit | Line | Data |
---|---|---|
903e4541 AE |
1 | /* |
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> | |
ba221bba | 17 | #include <crypto/hash.h> |
903e4541 | 18 | |
7024728e | 19 | #include "a2mp.h" |
7ef9fbf0 MH |
20 | #include "amp.h" |
21 | ||
52c0d6e5 | 22 | /* Remote AMP Controllers interface */ |
0b26ab9d | 23 | void amp_ctrl_get(struct amp_ctrl *ctrl) |
52c0d6e5 AE |
24 | { |
25 | BT_DBG("ctrl %p orig refcnt %d", ctrl, | |
26 | atomic_read(&ctrl->kref.refcount)); | |
27 | ||
28 | kref_get(&ctrl->kref); | |
29 | } | |
30 | ||
31 | static void amp_ctrl_destroy(struct kref *kref) | |
32 | { | |
33 | struct amp_ctrl *ctrl = container_of(kref, struct amp_ctrl, kref); | |
34 | ||
35 | BT_DBG("ctrl %p", ctrl); | |
36 | ||
37 | kfree(ctrl->assoc); | |
38 | kfree(ctrl); | |
39 | } | |
40 | ||
41 | int amp_ctrl_put(struct amp_ctrl *ctrl) | |
42 | { | |
43 | BT_DBG("ctrl %p orig refcnt %d", ctrl, | |
44 | atomic_read(&ctrl->kref.refcount)); | |
45 | ||
46 | return kref_put(&ctrl->kref, &_ctrl_destroy); | |
47 | } | |
48 | ||
fa4ebc66 | 49 | struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id) |
52c0d6e5 AE |
50 | { |
51 | struct amp_ctrl *ctrl; | |
52 | ||
53 | ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); | |
54 | if (!ctrl) | |
55 | return NULL; | |
56 | ||
fa4ebc66 AE |
57 | kref_init(&ctrl->kref); |
58 | ctrl->id = id; | |
59 | ||
52c0d6e5 AE |
60 | mutex_lock(&mgr->amp_ctrls_lock); |
61 | list_add(&ctrl->list, &mgr->amp_ctrls); | |
62 | mutex_unlock(&mgr->amp_ctrls_lock); | |
63 | ||
52c0d6e5 AE |
64 | BT_DBG("mgr %p ctrl %p", mgr, ctrl); |
65 | ||
66 | return ctrl; | |
67 | } | |
68 | ||
69 | void amp_ctrl_list_flush(struct amp_mgr *mgr) | |
70 | { | |
71 | struct amp_ctrl *ctrl, *n; | |
72 | ||
73 | BT_DBG("mgr %p", mgr); | |
74 | ||
75 | mutex_lock(&mgr->amp_ctrls_lock); | |
76 | list_for_each_entry_safe(ctrl, n, &mgr->amp_ctrls, list) { | |
77 | list_del(&ctrl->list); | |
78 | amp_ctrl_put(ctrl); | |
79 | } | |
80 | mutex_unlock(&mgr->amp_ctrls_lock); | |
81 | } | |
82 | ||
83 | struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id) | |
84 | { | |
85 | struct amp_ctrl *ctrl; | |
86 | ||
87 | BT_DBG("mgr %p id %d", mgr, id); | |
88 | ||
89 | mutex_lock(&mgr->amp_ctrls_lock); | |
90 | list_for_each_entry(ctrl, &mgr->amp_ctrls, list) { | |
91 | if (ctrl->id == id) { | |
92 | amp_ctrl_get(ctrl); | |
93 | mutex_unlock(&mgr->amp_ctrls_lock); | |
94 | return ctrl; | |
95 | } | |
96 | } | |
97 | mutex_unlock(&mgr->amp_ctrls_lock); | |
98 | ||
99 | return NULL; | |
100 | } | |
101 | ||
3161ae1c AE |
102 | /* Physical Link interface */ |
103 | static u8 __next_handle(struct amp_mgr *mgr) | |
104 | { | |
105 | if (++mgr->handle == 0) | |
106 | mgr->handle = 1; | |
107 | ||
108 | return mgr->handle; | |
109 | } | |
110 | ||
111 | struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr, | |
a0c234fe | 112 | u8 remote_id, bool out) |
3161ae1c | 113 | { |
bdc8ead2 | 114 | bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst; |
3161ae1c AE |
115 | struct hci_conn *hcon; |
116 | ||
117 | hcon = hci_conn_add(hdev, AMP_LINK, dst); | |
118 | if (!hcon) | |
119 | return NULL; | |
120 | ||
a0c234fe AE |
121 | BT_DBG("hcon %p dst %pMR", hcon, dst); |
122 | ||
3161ae1c | 123 | hcon->state = BT_CONNECT; |
3161ae1c AE |
124 | hcon->attempt++; |
125 | hcon->handle = __next_handle(mgr); | |
126 | hcon->remote_id = remote_id; | |
f706adfe | 127 | hcon->amp_mgr = amp_mgr_get(mgr); |
a0c234fe | 128 | hcon->out = out; |
3161ae1c AE |
129 | |
130 | return hcon; | |
131 | } | |
132 | ||
ba221bba DK |
133 | /* AMP crypto key generation interface */ |
134 | static int hmac_sha256(u8 *key, u8 ksize, char *plaintext, u8 psize, u8 *output) | |
135 | { | |
136 | int ret = 0; | |
137 | struct crypto_shash *tfm; | |
138 | ||
139 | if (!ksize) | |
140 | return -EINVAL; | |
141 | ||
142 | tfm = crypto_alloc_shash("hmac(sha256)", 0, 0); | |
143 | if (IS_ERR(tfm)) { | |
144 | BT_DBG("crypto_alloc_ahash failed: err %ld", PTR_ERR(tfm)); | |
145 | return PTR_ERR(tfm); | |
146 | } | |
147 | ||
148 | ret = crypto_shash_setkey(tfm, key, ksize); | |
149 | if (ret) { | |
150 | BT_DBG("crypto_ahash_setkey failed: err %d", ret); | |
151 | } else { | |
152 | struct { | |
153 | struct shash_desc shash; | |
154 | char ctx[crypto_shash_descsize(tfm)]; | |
155 | } desc; | |
156 | ||
157 | desc.shash.tfm = tfm; | |
158 | desc.shash.flags = CRYPTO_TFM_REQ_MAY_SLEEP; | |
159 | ||
160 | ret = crypto_shash_digest(&desc.shash, plaintext, psize, | |
161 | output); | |
162 | } | |
163 | ||
164 | crypto_free_shash(tfm); | |
165 | return ret; | |
166 | } | |
167 | ||
5a349186 AE |
168 | int phylink_gen_key(struct hci_conn *conn, u8 *data, u8 *len, u8 *type) |
169 | { | |
170 | struct hci_dev *hdev = conn->hdev; | |
171 | struct link_key *key; | |
172 | u8 keybuf[HCI_AMP_LINK_KEY_SIZE]; | |
173 | u8 gamp_key[HCI_AMP_LINK_KEY_SIZE]; | |
174 | int err; | |
175 | ||
176 | if (!hci_conn_check_link_mode(conn)) | |
177 | return -EACCES; | |
178 | ||
179 | BT_DBG("conn %p key_type %d", conn, conn->key_type); | |
180 | ||
181 | /* Legacy key */ | |
182 | if (conn->key_type < 3) { | |
183 | BT_ERR("Legacy key type %d", conn->key_type); | |
184 | return -EACCES; | |
185 | } | |
186 | ||
187 | *type = conn->key_type; | |
188 | *len = HCI_AMP_LINK_KEY_SIZE; | |
189 | ||
190 | key = hci_find_link_key(hdev, &conn->dst); | |
079db0c6 AE |
191 | if (!key) { |
192 | BT_DBG("No Link key for conn %p dst %pMR", conn, &conn->dst); | |
193 | return -EACCES; | |
194 | } | |
5a349186 AE |
195 | |
196 | /* BR/EDR Link Key concatenated together with itself */ | |
197 | memcpy(&keybuf[0], key->val, HCI_LINK_KEY_SIZE); | |
198 | memcpy(&keybuf[HCI_LINK_KEY_SIZE], key->val, HCI_LINK_KEY_SIZE); | |
199 | ||
200 | /* Derive Generic AMP Link Key (gamp) */ | |
201 | err = hmac_sha256(keybuf, HCI_AMP_LINK_KEY_SIZE, "gamp", 4, gamp_key); | |
202 | if (err) { | |
203 | BT_ERR("Could not derive Generic AMP Key: err %d", err); | |
204 | return err; | |
205 | } | |
206 | ||
207 | if (conn->key_type == HCI_LK_DEBUG_COMBINATION) { | |
208 | BT_DBG("Use Generic AMP Key (gamp)"); | |
209 | memcpy(data, gamp_key, HCI_AMP_LINK_KEY_SIZE); | |
210 | return err; | |
211 | } | |
212 | ||
213 | /* Derive Dedicated AMP Link Key: "802b" is 802.11 PAL keyID */ | |
214 | return hmac_sha256(gamp_key, HCI_AMP_LINK_KEY_SIZE, "802b", 4, data); | |
215 | } | |
216 | ||
903e4541 AE |
217 | void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle) |
218 | { | |
219 | struct hci_cp_read_local_amp_assoc cp; | |
220 | struct amp_assoc *loc_assoc = &hdev->loc_assoc; | |
221 | ||
222 | BT_DBG("%s handle %d", hdev->name, phy_handle); | |
223 | ||
224 | cp.phy_handle = phy_handle; | |
225 | cp.max_len = cpu_to_le16(hdev->amp_assoc_size); | |
226 | cp.len_so_far = cpu_to_le16(loc_assoc->offset); | |
227 | ||
228 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp); | |
229 | } | |
230 | ||
231 | void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr) | |
232 | { | |
233 | struct hci_cp_read_local_amp_assoc cp; | |
234 | ||
235 | memset(&hdev->loc_assoc, 0, sizeof(struct amp_assoc)); | |
236 | memset(&cp, 0, sizeof(cp)); | |
237 | ||
238 | cp.max_len = cpu_to_le16(hdev->amp_assoc_size); | |
239 | ||
cb6801c6 | 240 | set_bit(READ_LOC_AMP_ASSOC, &mgr->state); |
903e4541 AE |
241 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp); |
242 | } | |
a02226d6 | 243 | |
9495b2ee AE |
244 | void amp_read_loc_assoc_final_data(struct hci_dev *hdev, |
245 | struct hci_conn *hcon) | |
246 | { | |
247 | struct hci_cp_read_local_amp_assoc cp; | |
248 | struct amp_mgr *mgr = hcon->amp_mgr; | |
249 | ||
250 | cp.phy_handle = hcon->handle; | |
251 | cp.len_so_far = cpu_to_le16(0); | |
252 | cp.max_len = cpu_to_le16(hdev->amp_assoc_size); | |
253 | ||
cb6801c6 | 254 | set_bit(READ_LOC_AMP_ASSOC_FINAL, &mgr->state); |
9495b2ee AE |
255 | |
256 | /* Read Local AMP Assoc final link information data */ | |
257 | hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_ASSOC, sizeof(cp), &cp); | |
258 | } | |
93c284ee AE |
259 | |
260 | /* Write AMP Assoc data fragments, returns true with last fragment written*/ | |
261 | static bool amp_write_rem_assoc_frag(struct hci_dev *hdev, | |
262 | struct hci_conn *hcon) | |
263 | { | |
264 | struct hci_cp_write_remote_amp_assoc *cp; | |
265 | struct amp_mgr *mgr = hcon->amp_mgr; | |
266 | struct amp_ctrl *ctrl; | |
267 | u16 frag_len, len; | |
268 | ||
269 | ctrl = amp_ctrl_lookup(mgr, hcon->remote_id); | |
270 | if (!ctrl) | |
271 | return false; | |
272 | ||
273 | if (!ctrl->assoc_rem_len) { | |
274 | BT_DBG("all fragments are written"); | |
275 | ctrl->assoc_rem_len = ctrl->assoc_len; | |
276 | ctrl->assoc_len_so_far = 0; | |
277 | ||
278 | amp_ctrl_put(ctrl); | |
279 | return true; | |
280 | } | |
281 | ||
282 | frag_len = min_t(u16, 248, ctrl->assoc_rem_len); | |
283 | len = frag_len + sizeof(*cp); | |
284 | ||
285 | cp = kzalloc(len, GFP_KERNEL); | |
286 | if (!cp) { | |
287 | amp_ctrl_put(ctrl); | |
288 | return false; | |
289 | } | |
290 | ||
291 | BT_DBG("hcon %p ctrl %p frag_len %u assoc_len %u rem_len %u", | |
292 | hcon, ctrl, frag_len, ctrl->assoc_len, ctrl->assoc_rem_len); | |
293 | ||
294 | cp->phy_handle = hcon->handle; | |
295 | cp->len_so_far = cpu_to_le16(ctrl->assoc_len_so_far); | |
296 | cp->rem_len = cpu_to_le16(ctrl->assoc_rem_len); | |
297 | memcpy(cp->frag, ctrl->assoc, frag_len); | |
298 | ||
299 | ctrl->assoc_len_so_far += frag_len; | |
300 | ctrl->assoc_rem_len -= frag_len; | |
301 | ||
302 | amp_ctrl_put(ctrl); | |
303 | ||
304 | hci_send_cmd(hdev, HCI_OP_WRITE_REMOTE_AMP_ASSOC, len, cp); | |
305 | ||
306 | kfree(cp); | |
307 | ||
308 | return false; | |
309 | } | |
310 | ||
311 | void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle) | |
312 | { | |
313 | struct hci_conn *hcon; | |
314 | ||
315 | BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle); | |
316 | ||
317 | hcon = hci_conn_hash_lookup_handle(hdev, handle); | |
318 | if (!hcon) | |
319 | return; | |
320 | ||
8e05e3ba AE |
321 | /* Send A2MP create phylink rsp when all fragments are written */ |
322 | if (amp_write_rem_assoc_frag(hdev, hcon)) | |
323 | a2mp_send_create_phy_link_rsp(hdev, 0); | |
93c284ee AE |
324 | } |
325 | ||
326 | void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle) | |
327 | { | |
328 | struct hci_conn *hcon; | |
329 | ||
330 | BT_DBG("%s phy handle 0x%2.2x", hdev->name, handle); | |
331 | ||
332 | hcon = hci_conn_hash_lookup_handle(hdev, handle); | |
333 | if (!hcon) | |
334 | return; | |
335 | ||
336 | BT_DBG("%s phy handle 0x%2.2x hcon %p", hdev->name, handle, hcon); | |
337 | ||
338 | amp_write_rem_assoc_frag(hdev, hcon); | |
339 | } | |
340 | ||
a02226d6 AE |
341 | void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr, |
342 | struct hci_conn *hcon) | |
343 | { | |
344 | struct hci_cp_create_phy_link cp; | |
345 | ||
346 | cp.phy_handle = hcon->handle; | |
347 | ||
348 | BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon, | |
349 | hcon->handle); | |
350 | ||
351 | if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len, | |
352 | &cp.key_type)) { | |
353 | BT_DBG("Cannot create link key"); | |
354 | return; | |
355 | } | |
356 | ||
357 | hci_send_cmd(hdev, HCI_OP_CREATE_PHY_LINK, sizeof(cp), &cp); | |
358 | } | |
dffa3871 AE |
359 | |
360 | void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr, | |
361 | struct hci_conn *hcon) | |
362 | { | |
363 | struct hci_cp_accept_phy_link cp; | |
364 | ||
365 | cp.phy_handle = hcon->handle; | |
366 | ||
367 | BT_DBG("%s hcon %p phy handle 0x%2.2x", hdev->name, hcon, | |
368 | hcon->handle); | |
369 | ||
370 | if (phylink_gen_key(mgr->l2cap_conn->hcon, cp.key, &cp.key_len, | |
371 | &cp.key_type)) { | |
372 | BT_DBG("Cannot create link key"); | |
373 | return; | |
374 | } | |
375 | ||
376 | hci_send_cmd(hdev, HCI_OP_ACCEPT_PHY_LINK, sizeof(cp), &cp); | |
377 | } | |
5ce66b59 | 378 | |
cf70ff22 AE |
379 | void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon) |
380 | { | |
381 | struct hci_dev *bredr_hdev = hci_dev_hold(bredr_hcon->hdev); | |
382 | struct amp_mgr *mgr = hs_hcon->amp_mgr; | |
383 | struct l2cap_chan *bredr_chan; | |
384 | ||
385 | BT_DBG("bredr_hcon %p hs_hcon %p mgr %p", bredr_hcon, hs_hcon, mgr); | |
386 | ||
387 | if (!bredr_hdev || !mgr || !mgr->bredr_chan) | |
388 | return; | |
389 | ||
390 | bredr_chan = mgr->bredr_chan; | |
391 | ||
a514b17f AE |
392 | l2cap_chan_lock(bredr_chan); |
393 | ||
cf70ff22 | 394 | set_bit(FLAG_EFS_ENABLE, &bredr_chan->flags); |
fffadc08 | 395 | bredr_chan->remote_amp_id = hs_hcon->remote_id; |
37295996 | 396 | bredr_chan->local_amp_id = hs_hcon->hdev->id; |
cf70ff22 AE |
397 | bredr_chan->hs_hcon = hs_hcon; |
398 | bredr_chan->conn->mtu = hs_hcon->hdev->block_mtu; | |
cf70ff22 | 399 | |
a514b17f AE |
400 | __l2cap_physical_cfm(bredr_chan, 0); |
401 | ||
402 | l2cap_chan_unlock(bredr_chan); | |
cf70ff22 AE |
403 | |
404 | hci_dev_put(bredr_hdev); | |
405 | } | |
406 | ||
5ce66b59 AE |
407 | void amp_create_logical_link(struct l2cap_chan *chan) |
408 | { | |
7a9898c6 | 409 | struct hci_conn *hs_hcon = chan->hs_hcon; |
5ce66b59 | 410 | struct hci_cp_create_accept_logical_link cp; |
5ce66b59 AE |
411 | struct hci_dev *hdev; |
412 | ||
bdc8ead2 MH |
413 | BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon, |
414 | &chan->conn->hcon->dst); | |
5ce66b59 | 415 | |
7a9898c6 | 416 | if (!hs_hcon) |
5ce66b59 AE |
417 | return; |
418 | ||
419 | hdev = hci_dev_hold(chan->hs_hcon->hdev); | |
420 | if (!hdev) | |
421 | return; | |
422 | ||
7a9898c6 | 423 | cp.phy_handle = hs_hcon->handle; |
5ce66b59 AE |
424 | |
425 | cp.tx_flow_spec.id = chan->local_id; | |
426 | cp.tx_flow_spec.stype = chan->local_stype; | |
427 | cp.tx_flow_spec.msdu = cpu_to_le16(chan->local_msdu); | |
428 | cp.tx_flow_spec.sdu_itime = cpu_to_le32(chan->local_sdu_itime); | |
429 | cp.tx_flow_spec.acc_lat = cpu_to_le32(chan->local_acc_lat); | |
430 | cp.tx_flow_spec.flush_to = cpu_to_le32(chan->local_flush_to); | |
431 | ||
432 | cp.rx_flow_spec.id = chan->remote_id; | |
433 | cp.rx_flow_spec.stype = chan->remote_stype; | |
434 | cp.rx_flow_spec.msdu = cpu_to_le16(chan->remote_msdu); | |
435 | cp.rx_flow_spec.sdu_itime = cpu_to_le32(chan->remote_sdu_itime); | |
436 | cp.rx_flow_spec.acc_lat = cpu_to_le32(chan->remote_acc_lat); | |
437 | cp.rx_flow_spec.flush_to = cpu_to_le32(chan->remote_flush_to); | |
438 | ||
7a9898c6 | 439 | if (hs_hcon->out) |
5ce66b59 AE |
440 | hci_send_cmd(hdev, HCI_OP_CREATE_LOGICAL_LINK, sizeof(cp), |
441 | &cp); | |
442 | else | |
443 | hci_send_cmd(hdev, HCI_OP_ACCEPT_LOGICAL_LINK, sizeof(cp), | |
444 | &cp); | |
445 | ||
5ce66b59 AE |
446 | hci_dev_put(hdev); |
447 | } | |
606e2a10 | 448 | |
419e08c1 AE |
449 | void amp_disconnect_logical_link(struct hci_chan *hchan) |
450 | { | |
451 | struct hci_conn *hcon = hchan->conn; | |
452 | struct hci_cp_disconn_logical_link cp; | |
453 | ||
454 | if (hcon->state != BT_CONNECTED) { | |
455 | BT_DBG("hchan %p not connected", hchan); | |
456 | return; | |
457 | } | |
458 | ||
459 | cp.log_handle = cpu_to_le16(hchan->handle); | |
460 | hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK, sizeof(cp), &cp); | |
461 | } | |
462 | ||
606e2a10 AE |
463 | void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason) |
464 | { | |
465 | BT_DBG("hchan %p", hchan); | |
466 | ||
467 | hci_chan_del(hchan); | |
468 | } |