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