blob: 587b674bbcd648c869e8c3dd3686be88232ea07f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
27#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31#include <net/bluetooth/mgmt.h>
32
33#include "hci_request.h"
34#include "hci_debugfs.h"
35#include "a2mp.h"
36#include "amp.h"
37#include "smp.h"
38
39#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
40 "\x00\x00\x00\x00\x00\x00\x00\x00"
41
42/* Handle HCI Event packets */
43
44static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb,
45 u8 *new_status)
46{
47 __u8 status = *((__u8 *) skb->data);
48
49 BT_DBG("%s status 0x%2.2x", hdev->name, status);
50
51 /* It is possible that we receive Inquiry Complete event right
52 * before we receive Inquiry Cancel Command Complete event, in
53 * which case the latter event should have status of Command
54 * Disallowed (0x0c). This should not be treated as error, since
55 * we actually achieve what Inquiry Cancel wants to achieve,
56 * which is to end the last Inquiry session.
57 */
58 if (status == 0x0c && !test_bit(HCI_INQUIRY, &hdev->flags)) {
59 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
60 status = 0x00;
61 }
62
63 *new_status = status;
64
65 if (status)
66 return;
67
68 clear_bit(HCI_INQUIRY, &hdev->flags);
69 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
70 wake_up_bit(&hdev->flags, HCI_INQUIRY);
71
72 hci_dev_lock(hdev);
73 /* Set discovery state to stopped if we're not doing LE active
74 * scanning.
75 */
76 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
77 hdev->le_scan_type != LE_SCAN_ACTIVE)
78 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
79 hci_dev_unlock(hdev);
80
81 hci_conn_check_pending(hdev);
82}
83
84static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
85{
86 __u8 status = *((__u8 *) skb->data);
87
88 BT_DBG("%s status 0x%2.2x", hdev->name, status);
89
90 if (status)
91 return;
92
93 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
94}
95
96static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
97{
98 __u8 status = *((__u8 *) skb->data);
99
100 BT_DBG("%s status 0x%2.2x", hdev->name, status);
101
102 if (status)
103 return;
104
105 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
106
107 hci_conn_check_pending(hdev);
108}
109
110static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
111 struct sk_buff *skb)
112{
113 BT_DBG("%s", hdev->name);
114}
115
116static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
117{
118 struct hci_rp_role_discovery *rp = (void *) skb->data;
119 struct hci_conn *conn;
120
121 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
122
123 if (rp->status)
124 return;
125
126 hci_dev_lock(hdev);
127
128 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
129 if (conn)
130 conn->role = rp->role;
131
132 hci_dev_unlock(hdev);
133}
134
135static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
136{
137 struct hci_rp_read_link_policy *rp = (void *) skb->data;
138 struct hci_conn *conn;
139
140 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
141
142 if (rp->status)
143 return;
144
145 hci_dev_lock(hdev);
146
147 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
148 if (conn)
149 conn->link_policy = __le16_to_cpu(rp->policy);
150
151 hci_dev_unlock(hdev);
152}
153
154static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
155{
156 struct hci_rp_write_link_policy *rp = (void *) skb->data;
157 struct hci_conn *conn;
158 void *sent;
159
160 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
161
162 if (rp->status)
163 return;
164
165 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
166 if (!sent)
167 return;
168
169 hci_dev_lock(hdev);
170
171 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
172 if (conn)
173 conn->link_policy = get_unaligned_le16(sent + 2);
174
175 hci_dev_unlock(hdev);
176}
177
178static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
179 struct sk_buff *skb)
180{
181 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
182
183 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
184
185 if (rp->status)
186 return;
187
188 hdev->link_policy = __le16_to_cpu(rp->policy);
189}
190
191static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
192 struct sk_buff *skb)
193{
194 __u8 status = *((__u8 *) skb->data);
195 void *sent;
196
197 BT_DBG("%s status 0x%2.2x", hdev->name, status);
198
199 if (status)
200 return;
201
202 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
203 if (!sent)
204 return;
205
206 hdev->link_policy = get_unaligned_le16(sent);
207}
208
209static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
210{
211 __u8 status = *((__u8 *) skb->data);
212
213 BT_DBG("%s status 0x%2.2x", hdev->name, status);
214
215 clear_bit(HCI_RESET, &hdev->flags);
216
217 if (status)
218 return;
219
220 /* Reset all non-persistent flags */
221 hci_dev_clear_volatile_flags(hdev);
222
223 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
224
225 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
226 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
227
228 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
229 hdev->adv_data_len = 0;
230
231 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
232 hdev->scan_rsp_data_len = 0;
233
234 hdev->le_scan_type = LE_SCAN_PASSIVE;
235
236 hdev->ssp_debug_mode = 0;
237
238 hci_bdaddr_list_clear(&hdev->le_white_list);
239}
240
241static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
242 struct sk_buff *skb)
243{
244 struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
245 struct hci_cp_read_stored_link_key *sent;
246
247 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
248
249 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
250 if (!sent)
251 return;
252
253 if (!rp->status && sent->read_all == 0x01) {
254 hdev->stored_max_keys = rp->max_keys;
255 hdev->stored_num_keys = rp->num_keys;
256 }
257}
258
259static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
260 struct sk_buff *skb)
261{
262 struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
263
264 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
265
266 if (rp->status)
267 return;
268
269 if (rp->num_keys <= hdev->stored_num_keys)
270 hdev->stored_num_keys -= rp->num_keys;
271 else
272 hdev->stored_num_keys = 0;
273}
274
275static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
276{
277 __u8 status = *((__u8 *) skb->data);
278 void *sent;
279
280 BT_DBG("%s status 0x%2.2x", hdev->name, status);
281
282 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
283 if (!sent)
284 return;
285
286 hci_dev_lock(hdev);
287
288 if (hci_dev_test_flag(hdev, HCI_MGMT))
289 mgmt_set_local_name_complete(hdev, sent, status);
290 else if (!status)
291 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
292
293 hci_dev_unlock(hdev);
294}
295
296static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
297{
298 struct hci_rp_read_local_name *rp = (void *) skb->data;
299
300 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
301
302 if (rp->status)
303 return;
304
305 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
306 hci_dev_test_flag(hdev, HCI_CONFIG))
307 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
308}
309
310static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
311{
312 __u8 status = *((__u8 *) skb->data);
313 void *sent;
314
315 BT_DBG("%s status 0x%2.2x", hdev->name, status);
316
317 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
318 if (!sent)
319 return;
320
321 hci_dev_lock(hdev);
322
323 if (!status) {
324 __u8 param = *((__u8 *) sent);
325
326 if (param == AUTH_ENABLED)
327 set_bit(HCI_AUTH, &hdev->flags);
328 else
329 clear_bit(HCI_AUTH, &hdev->flags);
330 }
331
332 if (hci_dev_test_flag(hdev, HCI_MGMT))
333 mgmt_auth_enable_complete(hdev, status);
334
335 hci_dev_unlock(hdev);
336}
337
338static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
339{
340 __u8 status = *((__u8 *) skb->data);
341 __u8 param;
342 void *sent;
343
344 BT_DBG("%s status 0x%2.2x", hdev->name, status);
345
346 if (status)
347 return;
348
349 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
350 if (!sent)
351 return;
352
353 param = *((__u8 *) sent);
354
355 if (param)
356 set_bit(HCI_ENCRYPT, &hdev->flags);
357 else
358 clear_bit(HCI_ENCRYPT, &hdev->flags);
359}
360
361static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
362{
363 __u8 status = *((__u8 *) skb->data);
364 __u8 param;
365 void *sent;
366
367 BT_DBG("%s status 0x%2.2x", hdev->name, status);
368
369 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
370 if (!sent)
371 return;
372
373 param = *((__u8 *) sent);
374
375 hci_dev_lock(hdev);
376
377 if (status) {
378 hdev->discov_timeout = 0;
379 goto done;
380 }
381
382 if (param & SCAN_INQUIRY)
383 set_bit(HCI_ISCAN, &hdev->flags);
384 else
385 clear_bit(HCI_ISCAN, &hdev->flags);
386
387 if (param & SCAN_PAGE)
388 set_bit(HCI_PSCAN, &hdev->flags);
389 else
390 clear_bit(HCI_PSCAN, &hdev->flags);
391
392done:
393 hci_dev_unlock(hdev);
394}
395
396static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
397{
398 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
399
400 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
401
402 if (rp->status)
403 return;
404
405 memcpy(hdev->dev_class, rp->dev_class, 3);
406
407 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
408 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
409}
410
411static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
412{
413 __u8 status = *((__u8 *) skb->data);
414 void *sent;
415
416 BT_DBG("%s status 0x%2.2x", hdev->name, status);
417
418 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
419 if (!sent)
420 return;
421
422 hci_dev_lock(hdev);
423
424 if (status == 0)
425 memcpy(hdev->dev_class, sent, 3);
426
427 if (hci_dev_test_flag(hdev, HCI_MGMT))
428 mgmt_set_class_of_dev_complete(hdev, sent, status);
429
430 hci_dev_unlock(hdev);
431}
432
433static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
434{
435 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
436 __u16 setting;
437
438 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
439
440 if (rp->status)
441 return;
442
443 setting = __le16_to_cpu(rp->voice_setting);
444
445 if (hdev->voice_setting == setting)
446 return;
447
448 hdev->voice_setting = setting;
449
450 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
451
452 if (hdev->notify)
453 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
454}
455
456static void hci_cc_write_voice_setting(struct hci_dev *hdev,
457 struct sk_buff *skb)
458{
459 __u8 status = *((__u8 *) skb->data);
460 __u16 setting;
461 void *sent;
462
463 BT_DBG("%s status 0x%2.2x", hdev->name, status);
464
465 if (status)
466 return;
467
468 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
469 if (!sent)
470 return;
471
472 setting = get_unaligned_le16(sent);
473
474 if (hdev->voice_setting == setting)
475 return;
476
477 hdev->voice_setting = setting;
478
479 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
480
481 if (hdev->notify)
482 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
483}
484
485static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
486 struct sk_buff *skb)
487{
488 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
489
490 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
491
492 if (rp->status)
493 return;
494
495 hdev->num_iac = rp->num_iac;
496
497 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
498}
499
500static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
501{
502 __u8 status = *((__u8 *) skb->data);
503 struct hci_cp_write_ssp_mode *sent;
504
505 BT_DBG("%s status 0x%2.2x", hdev->name, status);
506
507 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
508 if (!sent)
509 return;
510
511 hci_dev_lock(hdev);
512
513 if (!status) {
514 if (sent->mode)
515 hdev->features[1][0] |= LMP_HOST_SSP;
516 else
517 hdev->features[1][0] &= ~LMP_HOST_SSP;
518 }
519
520 if (hci_dev_test_flag(hdev, HCI_MGMT))
521 mgmt_ssp_enable_complete(hdev, sent->mode, status);
522 else if (!status) {
523 if (sent->mode)
524 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
525 else
526 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
527 }
528
529 hci_dev_unlock(hdev);
530}
531
532static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
533{
534 u8 status = *((u8 *) skb->data);
535 struct hci_cp_write_sc_support *sent;
536
537 BT_DBG("%s status 0x%2.2x", hdev->name, status);
538
539 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
540 if (!sent)
541 return;
542
543 hci_dev_lock(hdev);
544
545 if (!status) {
546 if (sent->support)
547 hdev->features[1][0] |= LMP_HOST_SC;
548 else
549 hdev->features[1][0] &= ~LMP_HOST_SC;
550 }
551
552 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
553 if (sent->support)
554 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
555 else
556 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
557 }
558
559 hci_dev_unlock(hdev);
560}
561
562static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
563{
564 struct hci_rp_read_local_version *rp = (void *) skb->data;
565
566 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
567
568 if (rp->status)
569 return;
570
571 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
572 hci_dev_test_flag(hdev, HCI_CONFIG)) {
573 hdev->hci_ver = rp->hci_ver;
574 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
575 hdev->lmp_ver = rp->lmp_ver;
576 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
577 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
578 }
579}
580
581static void hci_cc_read_local_commands(struct hci_dev *hdev,
582 struct sk_buff *skb)
583{
584 struct hci_rp_read_local_commands *rp = (void *) skb->data;
585
586 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
587
588 if (rp->status)
589 return;
590
591 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
592 hci_dev_test_flag(hdev, HCI_CONFIG))
593 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
594}
595
596static void hci_cc_read_local_features(struct hci_dev *hdev,
597 struct sk_buff *skb)
598{
599 struct hci_rp_read_local_features *rp = (void *) skb->data;
600
601 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
602
603 if (rp->status)
604 return;
605
606 memcpy(hdev->features, rp->features, 8);
607
608 /* Adjust default settings according to features
609 * supported by device. */
610
611 if (hdev->features[0][0] & LMP_3SLOT)
612 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
613
614 if (hdev->features[0][0] & LMP_5SLOT)
615 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
616
617 if (hdev->features[0][1] & LMP_HV2) {
618 hdev->pkt_type |= (HCI_HV2);
619 hdev->esco_type |= (ESCO_HV2);
620 }
621
622 if (hdev->features[0][1] & LMP_HV3) {
623 hdev->pkt_type |= (HCI_HV3);
624 hdev->esco_type |= (ESCO_HV3);
625 }
626
627 if (lmp_esco_capable(hdev))
628 hdev->esco_type |= (ESCO_EV3);
629
630 if (hdev->features[0][4] & LMP_EV4)
631 hdev->esco_type |= (ESCO_EV4);
632
633 if (hdev->features[0][4] & LMP_EV5)
634 hdev->esco_type |= (ESCO_EV5);
635
636 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
637 hdev->esco_type |= (ESCO_2EV3);
638
639 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
640 hdev->esco_type |= (ESCO_3EV3);
641
642 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
643 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
644}
645
646static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
647 struct sk_buff *skb)
648{
649 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
650
651 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
652
653 if (rp->status)
654 return;
655
656 if (hdev->max_page < rp->max_page)
657 hdev->max_page = rp->max_page;
658
659 if (rp->page < HCI_MAX_PAGES)
660 memcpy(hdev->features[rp->page], rp->features, 8);
661}
662
663static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
664 struct sk_buff *skb)
665{
666 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
667
668 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
669
670 if (rp->status)
671 return;
672
673 hdev->flow_ctl_mode = rp->mode;
674}
675
676static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
677{
678 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
679
680 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
681
682 if (rp->status)
683 return;
684
685 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
686 hdev->sco_mtu = rp->sco_mtu;
687 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
688 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
689
690 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
691 hdev->sco_mtu = 64;
692 hdev->sco_pkts = 8;
693 }
694
695 hdev->acl_cnt = hdev->acl_pkts;
696 hdev->sco_cnt = hdev->sco_pkts;
697
698 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
699 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
700}
701
702static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
703{
704 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
705
706 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
707
708 if (rp->status)
709 return;
710
711 if (test_bit(HCI_INIT, &hdev->flags))
712 bacpy(&hdev->bdaddr, &rp->bdaddr);
713
714 if (hci_dev_test_flag(hdev, HCI_SETUP))
715 bacpy(&hdev->setup_addr, &rp->bdaddr);
716}
717
718static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
719 struct sk_buff *skb)
720{
721 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
722
723 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
724
725 if (rp->status)
726 return;
727
728 if (test_bit(HCI_INIT, &hdev->flags)) {
729 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
730 hdev->page_scan_window = __le16_to_cpu(rp->window);
731 }
732}
733
734static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
735 struct sk_buff *skb)
736{
737 u8 status = *((u8 *) skb->data);
738 struct hci_cp_write_page_scan_activity *sent;
739
740 BT_DBG("%s status 0x%2.2x", hdev->name, status);
741
742 if (status)
743 return;
744
745 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
746 if (!sent)
747 return;
748
749 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
750 hdev->page_scan_window = __le16_to_cpu(sent->window);
751}
752
753static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
754 struct sk_buff *skb)
755{
756 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
757
758 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
759
760 if (rp->status)
761 return;
762
763 if (test_bit(HCI_INIT, &hdev->flags))
764 hdev->page_scan_type = rp->type;
765}
766
767static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
768 struct sk_buff *skb)
769{
770 u8 status = *((u8 *) skb->data);
771 u8 *type;
772
773 BT_DBG("%s status 0x%2.2x", hdev->name, status);
774
775 if (status)
776 return;
777
778 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
779 if (type)
780 hdev->page_scan_type = *type;
781}
782
783static void hci_cc_read_data_block_size(struct hci_dev *hdev,
784 struct sk_buff *skb)
785{
786 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
787
788 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
789
790 if (rp->status)
791 return;
792
793 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
794 hdev->block_len = __le16_to_cpu(rp->block_len);
795 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
796
797 hdev->block_cnt = hdev->num_blocks;
798
799 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
800 hdev->block_cnt, hdev->block_len);
801}
802
803static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
804{
805 struct hci_rp_read_clock *rp = (void *) skb->data;
806 struct hci_cp_read_clock *cp;
807 struct hci_conn *conn;
808
809 BT_DBG("%s", hdev->name);
810
811 if (skb->len < sizeof(*rp))
812 return;
813
814 if (rp->status)
815 return;
816
817 hci_dev_lock(hdev);
818
819 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
820 if (!cp)
821 goto unlock;
822
823 if (cp->which == 0x00) {
824 hdev->clock = le32_to_cpu(rp->clock);
825 goto unlock;
826 }
827
828 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
829 if (conn) {
830 conn->clock = le32_to_cpu(rp->clock);
831 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
832 }
833
834unlock:
835 hci_dev_unlock(hdev);
836}
837
838static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
839 struct sk_buff *skb)
840{
841 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
842
843 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
844
845 if (rp->status)
846 return;
847
848 hdev->amp_status = rp->amp_status;
849 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
850 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
851 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
852 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
853 hdev->amp_type = rp->amp_type;
854 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
855 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
856 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
857 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
858}
859
860static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
861 struct sk_buff *skb)
862{
863 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
864
865 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
866
867 if (rp->status)
868 return;
869
870 hdev->inq_tx_power = rp->tx_power;
871}
872
873static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
874{
875 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
876 struct hci_cp_pin_code_reply *cp;
877 struct hci_conn *conn;
878
879 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
880
881 hci_dev_lock(hdev);
882
883 if (hci_dev_test_flag(hdev, HCI_MGMT))
884 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
885
886 if (rp->status)
887 goto unlock;
888
889 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
890 if (!cp)
891 goto unlock;
892
893 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
894 if (conn)
895 conn->pin_length = cp->pin_len;
896
897unlock:
898 hci_dev_unlock(hdev);
899}
900
901static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
902{
903 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
904
905 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
906
907 hci_dev_lock(hdev);
908
909 if (hci_dev_test_flag(hdev, HCI_MGMT))
910 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
911 rp->status);
912
913 hci_dev_unlock(hdev);
914}
915
916static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
917 struct sk_buff *skb)
918{
919 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
920
921 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
922
923 if (rp->status)
924 return;
925
926 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
927 hdev->le_pkts = rp->le_max_pkt;
928
929 hdev->le_cnt = hdev->le_pkts;
930
931 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
932}
933
934static void hci_cc_le_read_local_features(struct hci_dev *hdev,
935 struct sk_buff *skb)
936{
937 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
938
939 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
940
941 if (rp->status)
942 return;
943
944 memcpy(hdev->le_features, rp->features, 8);
945}
946
947static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
948 struct sk_buff *skb)
949{
950 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
951
952 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
953
954 if (rp->status)
955 return;
956
957 hdev->adv_tx_power = rp->tx_power;
958}
959
960static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
961{
962 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
963
964 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
965
966 hci_dev_lock(hdev);
967
968 if (hci_dev_test_flag(hdev, HCI_MGMT))
969 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
970 rp->status);
971
972 hci_dev_unlock(hdev);
973}
974
975static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
976 struct sk_buff *skb)
977{
978 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
979
980 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
981
982 hci_dev_lock(hdev);
983
984 if (hci_dev_test_flag(hdev, HCI_MGMT))
985 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
986 ACL_LINK, 0, rp->status);
987
988 hci_dev_unlock(hdev);
989}
990
991static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
992{
993 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
994
995 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
996
997 hci_dev_lock(hdev);
998
999 if (hci_dev_test_flag(hdev, HCI_MGMT))
1000 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
1001 0, rp->status);
1002
1003 hci_dev_unlock(hdev);
1004}
1005
1006static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
1007 struct sk_buff *skb)
1008{
1009 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1010
1011 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1012
1013 hci_dev_lock(hdev);
1014
1015 if (hci_dev_test_flag(hdev, HCI_MGMT))
1016 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
1017 ACL_LINK, 0, rp->status);
1018
1019 hci_dev_unlock(hdev);
1020}
1021
1022static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1023 struct sk_buff *skb)
1024{
1025 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1026
1027 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1028}
1029
1030static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1031 struct sk_buff *skb)
1032{
1033 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1034
1035 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1036}
1037
1038static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1039{
1040 __u8 status = *((__u8 *) skb->data);
1041 bdaddr_t *sent;
1042
1043 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1044
1045 if (status)
1046 return;
1047
1048 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1049 if (!sent)
1050 return;
1051
1052 hci_dev_lock(hdev);
1053
1054 bacpy(&hdev->random_addr, sent);
1055
1056 hci_dev_unlock(hdev);
1057}
1058
1059static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1060{
1061 __u8 *sent, status = *((__u8 *) skb->data);
1062
1063 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1064
1065 if (status)
1066 return;
1067
1068 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1069 if (!sent)
1070 return;
1071
1072 hci_dev_lock(hdev);
1073
1074 /* If we're doing connection initiation as peripheral. Set a
1075 * timeout in case something goes wrong.
1076 */
1077 if (*sent) {
1078 struct hci_conn *conn;
1079
1080 hci_dev_set_flag(hdev, HCI_LE_ADV);
1081
1082 conn = hci_lookup_le_connect(hdev);
1083 if (conn)
1084 queue_delayed_work(hdev->workqueue,
1085 &conn->le_conn_timeout,
1086 conn->conn_timeout);
1087 } else {
1088 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1089 }
1090
1091 hci_dev_unlock(hdev);
1092}
1093
1094static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1095{
1096 struct hci_cp_le_set_scan_param *cp;
1097 __u8 status = *((__u8 *) skb->data);
1098
1099 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1100
1101 if (status)
1102 return;
1103
1104 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1105 if (!cp)
1106 return;
1107
1108 hci_dev_lock(hdev);
1109
1110 hdev->le_scan_type = cp->type;
1111
1112 hci_dev_unlock(hdev);
1113}
1114
1115static bool has_pending_adv_report(struct hci_dev *hdev)
1116{
1117 struct discovery_state *d = &hdev->discovery;
1118
1119 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1120}
1121
1122static void clear_pending_adv_report(struct hci_dev *hdev)
1123{
1124 struct discovery_state *d = &hdev->discovery;
1125
1126 bacpy(&d->last_adv_addr, BDADDR_ANY);
1127 d->last_adv_data_len = 0;
1128}
1129
1130static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
1131 u8 bdaddr_type, s8 rssi, u32 flags,
1132 u8 *data, u8 len)
1133{
1134 struct discovery_state *d = &hdev->discovery;
1135
1136 bacpy(&d->last_adv_addr, bdaddr);
1137 d->last_adv_addr_type = bdaddr_type;
1138 d->last_adv_rssi = rssi;
1139 d->last_adv_flags = flags;
1140 memcpy(d->last_adv_data, data, len);
1141 d->last_adv_data_len = len;
1142}
1143
1144static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1145 struct sk_buff *skb)
1146{
1147 struct hci_cp_le_set_scan_enable *cp;
1148 __u8 status = *((__u8 *) skb->data);
1149
1150 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1151
1152 if (status)
1153 return;
1154
1155 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1156 if (!cp)
1157 return;
1158
1159 hci_dev_lock(hdev);
1160
1161 switch (cp->enable) {
1162 case LE_SCAN_ENABLE:
1163 hci_dev_set_flag(hdev, HCI_LE_SCAN);
1164 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1165 clear_pending_adv_report(hdev);
1166 break;
1167
1168 case LE_SCAN_DISABLE:
1169 /* We do this here instead of when setting DISCOVERY_STOPPED
1170 * since the latter would potentially require waiting for
1171 * inquiry to stop too.
1172 */
1173 if (has_pending_adv_report(hdev)) {
1174 struct discovery_state *d = &hdev->discovery;
1175
1176 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
1177 d->last_adv_addr_type, NULL,
1178 d->last_adv_rssi, d->last_adv_flags,
1179 d->last_adv_data,
1180 d->last_adv_data_len, NULL, 0);
1181 }
1182
1183 /* Cancel this timer so that we don't try to disable scanning
1184 * when it's already disabled.
1185 */
1186 cancel_delayed_work(&hdev->le_scan_disable);
1187
1188 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
1189
1190 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1191 * interrupted scanning due to a connect request. Mark
1192 * therefore discovery as stopped. If this was not
1193 * because of a connect request advertising might have
1194 * been disabled because of active scanning, so
1195 * re-enable it again if necessary.
1196 */
1197 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
1198 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1199 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
1200 hdev->discovery.state == DISCOVERY_FINDING)
1201 hci_req_reenable_advertising(hdev);
1202
1203 break;
1204
1205 default:
1206 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1207 break;
1208 }
1209
1210 hci_dev_unlock(hdev);
1211}
1212
1213static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1214 struct sk_buff *skb)
1215{
1216 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1217
1218 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1219
1220 if (rp->status)
1221 return;
1222
1223 hdev->le_white_list_size = rp->size;
1224}
1225
1226static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1227 struct sk_buff *skb)
1228{
1229 __u8 status = *((__u8 *) skb->data);
1230
1231 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1232
1233 if (status)
1234 return;
1235
1236 hci_bdaddr_list_clear(&hdev->le_white_list);
1237}
1238
1239static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1240 struct sk_buff *skb)
1241{
1242 struct hci_cp_le_add_to_white_list *sent;
1243 __u8 status = *((__u8 *) skb->data);
1244
1245 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1246
1247 if (status)
1248 return;
1249
1250 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1251 if (!sent)
1252 return;
1253
1254 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1255 sent->bdaddr_type);
1256}
1257
1258static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1259 struct sk_buff *skb)
1260{
1261 struct hci_cp_le_del_from_white_list *sent;
1262 __u8 status = *((__u8 *) skb->data);
1263
1264 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1265
1266 if (status)
1267 return;
1268
1269 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1270 if (!sent)
1271 return;
1272
1273 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1274 sent->bdaddr_type);
1275}
1276
1277static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1278 struct sk_buff *skb)
1279{
1280 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1281
1282 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1283
1284 if (rp->status)
1285 return;
1286
1287 memcpy(hdev->le_states, rp->le_states, 8);
1288}
1289
1290static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
1291 struct sk_buff *skb)
1292{
1293 struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
1294
1295 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1296
1297 if (rp->status)
1298 return;
1299
1300 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1301 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1302}
1303
1304static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
1305 struct sk_buff *skb)
1306{
1307 struct hci_cp_le_write_def_data_len *sent;
1308 __u8 status = *((__u8 *) skb->data);
1309
1310 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1311
1312 if (status)
1313 return;
1314
1315 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1316 if (!sent)
1317 return;
1318
1319 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1320 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1321}
1322
1323static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
1324 struct sk_buff *skb)
1325{
1326 struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
1327
1328 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1329
1330 if (rp->status)
1331 return;
1332
1333 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
1334 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
1335 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
1336 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
1337}
1338
1339static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1340 struct sk_buff *skb)
1341{
1342 struct hci_cp_write_le_host_supported *sent;
1343 __u8 status = *((__u8 *) skb->data);
1344
1345 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1346
1347 if (status)
1348 return;
1349
1350 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
1351 if (!sent)
1352 return;
1353
1354 hci_dev_lock(hdev);
1355
1356 if (sent->le) {
1357 hdev->features[1][0] |= LMP_HOST_LE;
1358 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
1359 } else {
1360 hdev->features[1][0] &= ~LMP_HOST_LE;
1361 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
1362 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
1363 }
1364
1365 if (sent->simul)
1366 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1367 else
1368 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
1369
1370 hci_dev_unlock(hdev);
1371}
1372
1373static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1374{
1375 struct hci_cp_le_set_adv_param *cp;
1376 u8 status = *((u8 *) skb->data);
1377
1378 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1379
1380 if (status)
1381 return;
1382
1383 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1384 if (!cp)
1385 return;
1386
1387 hci_dev_lock(hdev);
1388 hdev->adv_addr_type = cp->own_address_type;
1389 hci_dev_unlock(hdev);
1390}
1391
1392static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1393{
1394 struct hci_rp_read_rssi *rp = (void *) skb->data;
1395 struct hci_conn *conn;
1396
1397 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1398
1399 if (rp->status)
1400 return;
1401
1402 hci_dev_lock(hdev);
1403
1404 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1405 if (conn)
1406 conn->rssi = rp->rssi;
1407
1408 hci_dev_unlock(hdev);
1409}
1410
1411static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1412{
1413 struct hci_cp_read_tx_power *sent;
1414 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1415 struct hci_conn *conn;
1416
1417 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1418
1419 if (rp->status)
1420 return;
1421
1422 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1423 if (!sent)
1424 return;
1425
1426 hci_dev_lock(hdev);
1427
1428 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1429 if (!conn)
1430 goto unlock;
1431
1432 switch (sent->type) {
1433 case 0x00:
1434 conn->tx_power = rp->tx_power;
1435 break;
1436 case 0x01:
1437 conn->max_tx_power = rp->tx_power;
1438 break;
1439 }
1440
1441unlock:
1442 hci_dev_unlock(hdev);
1443}
1444
1445static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
1446{
1447 u8 status = *((u8 *) skb->data);
1448 u8 *mode;
1449
1450 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1451
1452 if (status)
1453 return;
1454
1455 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
1456 if (mode)
1457 hdev->ssp_debug_mode = *mode;
1458}
1459
1460static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
1461{
1462 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1463
1464 if (status) {
1465 hci_conn_check_pending(hdev);
1466 return;
1467 }
1468
1469 set_bit(HCI_INQUIRY, &hdev->flags);
1470}
1471
1472static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
1473{
1474 struct hci_cp_create_conn *cp;
1475 struct hci_conn *conn;
1476
1477 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1478
1479 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
1480 if (!cp)
1481 return;
1482
1483 hci_dev_lock(hdev);
1484
1485 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1486
1487 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
1488
1489 if (status) {
1490 if (conn && conn->state == BT_CONNECT) {
1491 if (status != 0x0c || conn->attempt > 2) {
1492 conn->state = BT_CLOSED;
1493 hci_connect_cfm(conn, status);
1494 hci_conn_del(conn);
1495 } else
1496 conn->state = BT_CONNECT2;
1497 }
1498 } else {
1499 if (!conn) {
1500 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1501 HCI_ROLE_MASTER);
1502 if (!conn)
1503 BT_ERR("No memory for new connection");
1504 }
1505 }
1506
1507 hci_dev_unlock(hdev);
1508}
1509
1510static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
1511{
1512 struct hci_cp_add_sco *cp;
1513 struct hci_conn *acl, *sco;
1514 __u16 handle;
1515
1516 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1517
1518 if (!status)
1519 return;
1520
1521 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1522 if (!cp)
1523 return;
1524
1525 handle = __le16_to_cpu(cp->handle);
1526
1527 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
1528
1529 hci_dev_lock(hdev);
1530
1531 acl = hci_conn_hash_lookup_handle(hdev, handle);
1532 if (acl) {
1533 sco = acl->link;
1534 if (sco) {
1535 sco->state = BT_CLOSED;
1536
1537 hci_connect_cfm(sco, status);
1538 hci_conn_del(sco);
1539 }
1540 }
1541
1542 hci_dev_unlock(hdev);
1543}
1544
1545static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1546{
1547 struct hci_cp_auth_requested *cp;
1548 struct hci_conn *conn;
1549
1550 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1551
1552 if (!status)
1553 return;
1554
1555 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1556 if (!cp)
1557 return;
1558
1559 hci_dev_lock(hdev);
1560
1561 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1562 if (conn) {
1563 if (conn->state == BT_CONFIG) {
1564 hci_connect_cfm(conn, status);
1565 hci_conn_drop(conn);
1566 }
1567 }
1568
1569 hci_dev_unlock(hdev);
1570}
1571
1572static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1573{
1574 struct hci_cp_set_conn_encrypt *cp;
1575 struct hci_conn *conn;
1576
1577 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1578
1579 if (!status)
1580 return;
1581
1582 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1583 if (!cp)
1584 return;
1585
1586 hci_dev_lock(hdev);
1587
1588 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1589 if (conn) {
1590 if (conn->state == BT_CONFIG) {
1591 hci_connect_cfm(conn, status);
1592 hci_conn_drop(conn);
1593 }
1594 }
1595
1596 hci_dev_unlock(hdev);
1597}
1598
1599static int hci_outgoing_auth_needed(struct hci_dev *hdev,
1600 struct hci_conn *conn)
1601{
1602 if (conn->state != BT_CONFIG || !conn->out)
1603 return 0;
1604
1605 if (conn->pending_sec_level == BT_SECURITY_SDP)
1606 return 0;
1607
1608 /* Only request authentication for SSP connections or non-SSP
1609 * devices with sec_level MEDIUM or HIGH or if MITM protection
1610 * is requested.
1611 */
1612 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
1613 conn->pending_sec_level != BT_SECURITY_FIPS &&
1614 conn->pending_sec_level != BT_SECURITY_HIGH &&
1615 conn->pending_sec_level != BT_SECURITY_MEDIUM)
1616 return 0;
1617
1618 return 1;
1619}
1620
1621static int hci_resolve_name(struct hci_dev *hdev,
1622 struct inquiry_entry *e)
1623{
1624 struct hci_cp_remote_name_req cp;
1625
1626 memset(&cp, 0, sizeof(cp));
1627
1628 bacpy(&cp.bdaddr, &e->data.bdaddr);
1629 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1630 cp.pscan_mode = e->data.pscan_mode;
1631 cp.clock_offset = e->data.clock_offset;
1632
1633 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1634}
1635
1636static bool hci_resolve_next_name(struct hci_dev *hdev)
1637{
1638 struct discovery_state *discov = &hdev->discovery;
1639 struct inquiry_entry *e;
1640
1641 if (list_empty(&discov->resolve))
1642 return false;
1643
1644 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1645 if (!e)
1646 return false;
1647
1648 if (hci_resolve_name(hdev, e) == 0) {
1649 e->name_state = NAME_PENDING;
1650 return true;
1651 }
1652
1653 return false;
1654}
1655
1656static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
1657 bdaddr_t *bdaddr, u8 *name, u8 name_len)
1658{
1659 struct discovery_state *discov = &hdev->discovery;
1660 struct inquiry_entry *e;
1661
1662 /* Update the mgmt connected state if necessary. Be careful with
1663 * conn objects that exist but are not (yet) connected however.
1664 * Only those in BT_CONFIG or BT_CONNECTED states can be
1665 * considered connected.
1666 */
1667 if (conn &&
1668 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
1669 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
1670 mgmt_device_connected(hdev, conn, 0, name, name_len);
1671
1672 if (discov->state == DISCOVERY_STOPPED)
1673 return;
1674
1675 if (discov->state == DISCOVERY_STOPPING)
1676 goto discov_complete;
1677
1678 if (discov->state != DISCOVERY_RESOLVING)
1679 return;
1680
1681 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
1682 /* If the device was not found in a list of found devices names of which
1683 * are pending. there is no need to continue resolving a next name as it
1684 * will be done upon receiving another Remote Name Request Complete
1685 * Event */
1686 if (!e)
1687 return;
1688
1689 list_del(&e->list);
1690 if (name) {
1691 e->name_state = NAME_KNOWN;
1692 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1693 e->data.rssi, name, name_len);
1694 } else {
1695 e->name_state = NAME_NOT_KNOWN;
1696 }
1697
1698 if (hci_resolve_next_name(hdev))
1699 return;
1700
1701discov_complete:
1702 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1703}
1704
1705static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1706{
1707 struct hci_cp_remote_name_req *cp;
1708 struct hci_conn *conn;
1709
1710 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1711
1712 /* If successful wait for the name req complete event before
1713 * checking for the need to do authentication */
1714 if (!status)
1715 return;
1716
1717 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1718 if (!cp)
1719 return;
1720
1721 hci_dev_lock(hdev);
1722
1723 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1724
1725 if (hci_dev_test_flag(hdev, HCI_MGMT))
1726 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1727
1728 if (!conn)
1729 goto unlock;
1730
1731 if (!hci_outgoing_auth_needed(hdev, conn))
1732 goto unlock;
1733
1734 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1735 struct hci_cp_auth_requested auth_cp;
1736
1737 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1738
1739 auth_cp.handle = __cpu_to_le16(conn->handle);
1740 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1741 sizeof(auth_cp), &auth_cp);
1742 }
1743
1744unlock:
1745 hci_dev_unlock(hdev);
1746}
1747
1748static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1749{
1750 struct hci_cp_read_remote_features *cp;
1751 struct hci_conn *conn;
1752
1753 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1754
1755 if (!status)
1756 return;
1757
1758 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1759 if (!cp)
1760 return;
1761
1762 hci_dev_lock(hdev);
1763
1764 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1765 if (conn) {
1766 if (conn->state == BT_CONFIG) {
1767 hci_connect_cfm(conn, status);
1768 hci_conn_drop(conn);
1769 }
1770 }
1771
1772 hci_dev_unlock(hdev);
1773}
1774
1775static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1776{
1777 struct hci_cp_read_remote_ext_features *cp;
1778 struct hci_conn *conn;
1779
1780 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1781
1782 if (!status)
1783 return;
1784
1785 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1786 if (!cp)
1787 return;
1788
1789 hci_dev_lock(hdev);
1790
1791 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1792 if (conn) {
1793 if (conn->state == BT_CONFIG) {
1794 hci_connect_cfm(conn, status);
1795 hci_conn_drop(conn);
1796 }
1797 }
1798
1799 hci_dev_unlock(hdev);
1800}
1801
1802static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1803{
1804 struct hci_cp_setup_sync_conn *cp;
1805 struct hci_conn *acl, *sco;
1806 __u16 handle;
1807
1808 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1809
1810 if (!status)
1811 return;
1812
1813 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1814 if (!cp)
1815 return;
1816
1817 handle = __le16_to_cpu(cp->handle);
1818
1819 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
1820
1821 hci_dev_lock(hdev);
1822
1823 acl = hci_conn_hash_lookup_handle(hdev, handle);
1824 if (acl) {
1825 sco = acl->link;
1826 if (sco) {
1827 sco->state = BT_CLOSED;
1828
1829 hci_connect_cfm(sco, status);
1830 hci_conn_del(sco);
1831 }
1832 }
1833
1834 hci_dev_unlock(hdev);
1835}
1836
1837static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1838{
1839 struct hci_cp_sniff_mode *cp;
1840 struct hci_conn *conn;
1841
1842 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1843
1844 if (!status)
1845 return;
1846
1847 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1848 if (!cp)
1849 return;
1850
1851 hci_dev_lock(hdev);
1852
1853 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1854 if (conn) {
1855 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
1856
1857 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
1858 hci_sco_setup(conn, status);
1859 }
1860
1861 hci_dev_unlock(hdev);
1862}
1863
1864static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1865{
1866 struct hci_cp_exit_sniff_mode *cp;
1867 struct hci_conn *conn;
1868
1869 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1870
1871 if (!status)
1872 return;
1873
1874 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1875 if (!cp)
1876 return;
1877
1878 hci_dev_lock(hdev);
1879
1880 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1881 if (conn) {
1882 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
1883
1884 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
1885 hci_sco_setup(conn, status);
1886 }
1887
1888 hci_dev_unlock(hdev);
1889}
1890
1891static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1892{
1893 struct hci_cp_disconnect *cp;
1894 struct hci_conn *conn;
1895
1896 if (!status)
1897 return;
1898
1899 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1900 if (!cp)
1901 return;
1902
1903 hci_dev_lock(hdev);
1904
1905 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1906 if (conn)
1907 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
1908 conn->dst_type, status);
1909
1910 hci_dev_unlock(hdev);
1911}
1912
1913static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1914{
1915 struct hci_cp_le_create_conn *cp;
1916 struct hci_conn *conn;
1917
1918 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1919
1920 /* All connection failure handling is taken care of by the
1921 * hci_le_conn_failed function which is triggered by the HCI
1922 * request completion callbacks used for connecting.
1923 */
1924 if (status)
1925 return;
1926
1927 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1928 if (!cp)
1929 return;
1930
1931 hci_dev_lock(hdev);
1932
1933 conn = hci_conn_hash_lookup_le(hdev, &cp->peer_addr,
1934 cp->peer_addr_type);
1935 if (!conn)
1936 goto unlock;
1937
1938 /* Store the initiator and responder address information which
1939 * is needed for SMP. These values will not change during the
1940 * lifetime of the connection.
1941 */
1942 conn->init_addr_type = cp->own_address_type;
1943 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1944 bacpy(&conn->init_addr, &hdev->random_addr);
1945 else
1946 bacpy(&conn->init_addr, &hdev->bdaddr);
1947
1948 conn->resp_addr_type = cp->peer_addr_type;
1949 bacpy(&conn->resp_addr, &cp->peer_addr);
1950
1951 /* We don't want the connection attempt to stick around
1952 * indefinitely since LE doesn't have a page timeout concept
1953 * like BR/EDR. Set a timer for any connection that doesn't use
1954 * the white list for connecting.
1955 */
1956 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1957 queue_delayed_work(conn->hdev->workqueue,
1958 &conn->le_conn_timeout,
1959 conn->conn_timeout);
1960
1961unlock:
1962 hci_dev_unlock(hdev);
1963}
1964
1965static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
1966{
1967 struct hci_cp_le_read_remote_features *cp;
1968 struct hci_conn *conn;
1969
1970 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1971
1972 if (!status)
1973 return;
1974
1975 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
1976 if (!cp)
1977 return;
1978
1979 hci_dev_lock(hdev);
1980
1981 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1982 if (conn) {
1983 if (conn->state == BT_CONFIG) {
1984 hci_connect_cfm(conn, status);
1985 hci_conn_drop(conn);
1986 }
1987 }
1988
1989 hci_dev_unlock(hdev);
1990}
1991
1992static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1993{
1994 struct hci_cp_le_start_enc *cp;
1995 struct hci_conn *conn;
1996
1997 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1998
1999 if (!status)
2000 return;
2001
2002 hci_dev_lock(hdev);
2003
2004 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2005 if (!cp)
2006 goto unlock;
2007
2008 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2009 if (!conn)
2010 goto unlock;
2011
2012 if (conn->state != BT_CONNECTED)
2013 goto unlock;
2014
2015 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2016 hci_conn_drop(conn);
2017
2018unlock:
2019 hci_dev_unlock(hdev);
2020}
2021
2022static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2023{
2024 struct hci_cp_switch_role *cp;
2025 struct hci_conn *conn;
2026
2027 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2028
2029 if (!status)
2030 return;
2031
2032 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2033 if (!cp)
2034 return;
2035
2036 hci_dev_lock(hdev);
2037
2038 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2039 if (conn)
2040 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2041
2042 hci_dev_unlock(hdev);
2043}
2044
2045static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2046{
2047 __u8 status = *((__u8 *) skb->data);
2048 struct discovery_state *discov = &hdev->discovery;
2049 struct inquiry_entry *e;
2050
2051 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2052
2053 hci_conn_check_pending(hdev);
2054
2055 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2056 return;
2057
2058 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
2059 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2060
2061 if (!hci_dev_test_flag(hdev, HCI_MGMT))
2062 return;
2063
2064 hci_dev_lock(hdev);
2065
2066 if (discov->state != DISCOVERY_FINDING)
2067 goto unlock;
2068
2069 if (list_empty(&discov->resolve)) {
2070 /* When BR/EDR inquiry is active and no LE scanning is in
2071 * progress, then change discovery state to indicate completion.
2072 *
2073 * When running LE scanning and BR/EDR inquiry simultaneously
2074 * and the LE scan already finished, then change the discovery
2075 * state to indicate completion.
2076 */
2077 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2078 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2079 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2080 goto unlock;
2081 }
2082
2083 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2084 if (e && hci_resolve_name(hdev, e) == 0) {
2085 e->name_state = NAME_PENDING;
2086 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2087 } else {
2088 /* When BR/EDR inquiry is active and no LE scanning is in
2089 * progress, then change discovery state to indicate completion.
2090 *
2091 * When running LE scanning and BR/EDR inquiry simultaneously
2092 * and the LE scan already finished, then change the discovery
2093 * state to indicate completion.
2094 */
2095 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2096 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2097 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2098 }
2099
2100unlock:
2101 hci_dev_unlock(hdev);
2102}
2103
2104static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2105{
2106 struct inquiry_data data;
2107 struct inquiry_info *info = (void *) (skb->data + 1);
2108 int num_rsp = *((__u8 *) skb->data);
2109
2110 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2111
2112 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
2113 return;
2114
2115 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
2116 return;
2117
2118 hci_dev_lock(hdev);
2119
2120 for (; num_rsp; num_rsp--, info++) {
2121 u32 flags;
2122
2123 bacpy(&data.bdaddr, &info->bdaddr);
2124 data.pscan_rep_mode = info->pscan_rep_mode;
2125 data.pscan_period_mode = info->pscan_period_mode;
2126 data.pscan_mode = info->pscan_mode;
2127 memcpy(data.dev_class, info->dev_class, 3);
2128 data.clock_offset = info->clock_offset;
2129 data.rssi = HCI_RSSI_INVALID;
2130 data.ssp_mode = 0x00;
2131
2132 flags = hci_inquiry_cache_update(hdev, &data, false);
2133
2134 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
2135 info->dev_class, HCI_RSSI_INVALID,
2136 flags, NULL, 0, NULL, 0);
2137 }
2138
2139 hci_dev_unlock(hdev);
2140}
2141
2142static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2143{
2144 struct hci_ev_conn_complete *ev = (void *) skb->data;
2145 struct hci_conn *conn;
2146
2147 BT_DBG("%s", hdev->name);
2148
2149 hci_dev_lock(hdev);
2150
2151 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
2152 if (!conn) {
2153 if (ev->link_type != SCO_LINK)
2154 goto unlock;
2155
2156 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2157 if (!conn)
2158 goto unlock;
2159
2160 conn->type = SCO_LINK;
2161 }
2162
2163 if (!ev->status) {
2164 conn->handle = __le16_to_cpu(ev->handle);
2165
2166 if (conn->type == ACL_LINK) {
2167 conn->state = BT_CONFIG;
2168 hci_conn_hold(conn);
2169
2170 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2171 !hci_find_link_key(hdev, &ev->bdaddr))
2172 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2173 else
2174 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
2175 } else
2176 conn->state = BT_CONNECTED;
2177
2178 hci_debugfs_create_conn(conn);
2179 hci_conn_add_sysfs(conn);
2180
2181 if (test_bit(HCI_AUTH, &hdev->flags))
2182 set_bit(HCI_CONN_AUTH, &conn->flags);
2183
2184 if (test_bit(HCI_ENCRYPT, &hdev->flags))
2185 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
2186
2187 /* Get remote features */
2188 if (conn->type == ACL_LINK) {
2189 struct hci_cp_read_remote_features cp;
2190 cp.handle = ev->handle;
2191 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
2192 sizeof(cp), &cp);
2193
2194 hci_req_update_scan(hdev);
2195 }
2196
2197 /* Set packet type for incoming connection */
2198 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
2199 struct hci_cp_change_conn_ptype cp;
2200 cp.handle = ev->handle;
2201 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2202 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2203 &cp);
2204 }
2205 } else {
2206 conn->state = BT_CLOSED;
2207 if (conn->type == ACL_LINK)
2208 mgmt_connect_failed(hdev, &conn->dst, conn->type,
2209 conn->dst_type, ev->status);
2210 }
2211
2212 if (conn->type == ACL_LINK)
2213 hci_sco_setup(conn, ev->status);
2214
2215 if (ev->status) {
2216 hci_connect_cfm(conn, ev->status);
2217 hci_conn_del(conn);
2218 } else if (ev->link_type != ACL_LINK)
2219 hci_connect_cfm(conn, ev->status);
2220
2221unlock:
2222 hci_dev_unlock(hdev);
2223
2224 hci_conn_check_pending(hdev);
2225}
2226
2227static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2228{
2229 struct hci_cp_reject_conn_req cp;
2230
2231 bacpy(&cp.bdaddr, bdaddr);
2232 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2233 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2234}
2235
2236static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2237{
2238 struct hci_ev_conn_request *ev = (void *) skb->data;
2239 int mask = hdev->link_mode;
2240 struct inquiry_entry *ie;
2241 struct hci_conn *conn;
2242 __u8 flags = 0;
2243
2244 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
2245 ev->link_type);
2246
2247 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2248 &flags);
2249
2250 if (!(mask & HCI_LM_ACCEPT)) {
2251 hci_reject_conn(hdev, &ev->bdaddr);
2252 return;
2253 }
2254
2255 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2256 BDADDR_BREDR)) {
2257 hci_reject_conn(hdev, &ev->bdaddr);
2258 return;
2259 }
2260
2261 /* Require HCI_CONNECTABLE or a whitelist entry to accept the
2262 * connection. These features are only touched through mgmt so
2263 * only do the checks if HCI_MGMT is set.
2264 */
2265 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2266 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
2267 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2268 BDADDR_BREDR)) {
2269 hci_reject_conn(hdev, &ev->bdaddr);
2270 return;
2271 }
2272
2273 /* Connection accepted */
2274
2275 hci_dev_lock(hdev);
2276
2277 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2278 if (ie)
2279 memcpy(ie->data.dev_class, ev->dev_class, 3);
2280
2281 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2282 &ev->bdaddr);
2283 if (!conn) {
2284 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2285 HCI_ROLE_SLAVE);
2286 if (!conn) {
2287 BT_ERR("No memory for new connection");
2288 hci_dev_unlock(hdev);
2289 return;
2290 }
2291 }
2292
2293 memcpy(conn->dev_class, ev->dev_class, 3);
2294
2295 hci_dev_unlock(hdev);
2296
2297 if (ev->link_type == ACL_LINK ||
2298 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2299 struct hci_cp_accept_conn_req cp;
2300 conn->state = BT_CONNECT;
2301
2302 bacpy(&cp.bdaddr, &ev->bdaddr);
2303
2304 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2305 cp.role = 0x00; /* Become master */
2306 else
2307 cp.role = 0x01; /* Remain slave */
2308
2309 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2310 } else if (!(flags & HCI_PROTO_DEFER)) {
2311 struct hci_cp_accept_sync_conn_req cp;
2312 conn->state = BT_CONNECT;
2313
2314 bacpy(&cp.bdaddr, &ev->bdaddr);
2315 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2316
2317 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2318 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2319 cp.max_latency = cpu_to_le16(0xffff);
2320 cp.content_format = cpu_to_le16(hdev->voice_setting);
2321 cp.retrans_effort = 0xff;
2322
2323 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2324 &cp);
2325 } else {
2326 conn->state = BT_CONNECT2;
2327 hci_connect_cfm(conn, 0);
2328 }
2329}
2330
2331static u8 hci_to_mgmt_reason(u8 err)
2332{
2333 switch (err) {
2334 case HCI_ERROR_CONNECTION_TIMEOUT:
2335 return MGMT_DEV_DISCONN_TIMEOUT;
2336 case HCI_ERROR_REMOTE_USER_TERM:
2337 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2338 case HCI_ERROR_REMOTE_POWER_OFF:
2339 return MGMT_DEV_DISCONN_REMOTE;
2340 case HCI_ERROR_LOCAL_HOST_TERM:
2341 return MGMT_DEV_DISCONN_LOCAL_HOST;
2342 default:
2343 return MGMT_DEV_DISCONN_UNKNOWN;
2344 }
2345}
2346
2347static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2348{
2349 struct hci_ev_disconn_complete *ev = (void *) skb->data;
2350 u8 reason;
2351 struct hci_conn_params *params;
2352 struct hci_conn *conn;
2353 bool mgmt_connected;
2354 u8 type;
2355
2356 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
2357
2358 hci_dev_lock(hdev);
2359
2360 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2361 if (!conn)
2362 goto unlock;
2363
2364 if (ev->status) {
2365 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2366 conn->dst_type, ev->status);
2367 goto unlock;
2368 }
2369
2370 conn->state = BT_CLOSED;
2371
2372 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2373
2374 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
2375 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
2376 else
2377 reason = hci_to_mgmt_reason(ev->reason);
2378
2379 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2380 reason, mgmt_connected);
2381
2382 if (conn->type == ACL_LINK) {
2383 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2384 hci_remove_link_key(hdev, &conn->dst);
2385
2386 hci_req_update_scan(hdev);
2387 }
2388
2389 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2390 if (params) {
2391 switch (params->auto_connect) {
2392 case HCI_AUTO_CONN_LINK_LOSS:
2393 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2394 break;
2395 /* Fall through */
2396
2397 case HCI_AUTO_CONN_DIRECT:
2398 case HCI_AUTO_CONN_ALWAYS:
2399 list_del_init(&params->action);
2400 list_add(&params->action, &hdev->pend_le_conns);
2401 hci_update_background_scan(hdev);
2402 break;
2403
2404 default:
2405 break;
2406 }
2407 }
2408
2409 type = conn->type;
2410
2411 hci_disconn_cfm(conn, ev->reason);
2412 hci_conn_del(conn);
2413
2414 /* Re-enable advertising if necessary, since it might
2415 * have been disabled by the connection. From the
2416 * HCI_LE_Set_Advertise_Enable command description in
2417 * the core specification (v4.0):
2418 * "The Controller shall continue advertising until the Host
2419 * issues an LE_Set_Advertise_Enable command with
2420 * Advertising_Enable set to 0x00 (Advertising is disabled)
2421 * or until a connection is created or until the Advertising
2422 * is timed out due to Directed Advertising."
2423 */
2424 if (type == LE_LINK)
2425 hci_req_reenable_advertising(hdev);
2426
2427unlock:
2428 hci_dev_unlock(hdev);
2429}
2430
2431static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2432{
2433 struct hci_ev_auth_complete *ev = (void *) skb->data;
2434 struct hci_conn *conn;
2435
2436 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
2437
2438 hci_dev_lock(hdev);
2439
2440 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2441 if (!conn)
2442 goto unlock;
2443
2444 if (!ev->status) {
2445 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2446
2447 if (!hci_conn_ssp_enabled(conn) &&
2448 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
2449 BT_INFO("re-auth of legacy device is not possible.");
2450 } else {
2451 set_bit(HCI_CONN_AUTH, &conn->flags);
2452 conn->sec_level = conn->pending_sec_level;
2453 }
2454 } else {
2455 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
2456 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2457
2458 mgmt_auth_failed(conn, ev->status);
2459 }
2460
2461 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2462 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
2463
2464 if (conn->state == BT_CONFIG) {
2465 if (!ev->status && hci_conn_ssp_enabled(conn)) {
2466 struct hci_cp_set_conn_encrypt cp;
2467 cp.handle = ev->handle;
2468 cp.encrypt = 0x01;
2469 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
2470 &cp);
2471 } else {
2472 conn->state = BT_CONNECTED;
2473 hci_connect_cfm(conn, ev->status);
2474 hci_conn_drop(conn);
2475 }
2476 } else {
2477 hci_auth_cfm(conn, ev->status);
2478
2479 hci_conn_hold(conn);
2480 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
2481 hci_conn_drop(conn);
2482 }
2483
2484 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
2485 if (!ev->status) {
2486 struct hci_cp_set_conn_encrypt cp;
2487 cp.handle = ev->handle;
2488 cp.encrypt = 0x01;
2489 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
2490 &cp);
2491 } else {
2492 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2493 hci_encrypt_cfm(conn, ev->status, 0x00);
2494 }
2495 }
2496
2497unlock:
2498 hci_dev_unlock(hdev);
2499}
2500
2501static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
2502{
2503 struct hci_ev_remote_name *ev = (void *) skb->data;
2504 struct hci_conn *conn;
2505
2506 BT_DBG("%s", hdev->name);
2507
2508 hci_conn_check_pending(hdev);
2509
2510 hci_dev_lock(hdev);
2511
2512 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2513
2514 if (!hci_dev_test_flag(hdev, HCI_MGMT))
2515 goto check_auth;
2516
2517 if (ev->status == 0)
2518 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
2519 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
2520 else
2521 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2522
2523check_auth:
2524 if (!conn)
2525 goto unlock;
2526
2527 if (!hci_outgoing_auth_needed(hdev, conn))
2528 goto unlock;
2529
2530 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
2531 struct hci_cp_auth_requested cp;
2532
2533 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2534
2535 cp.handle = __cpu_to_le16(conn->handle);
2536 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2537 }
2538
2539unlock:
2540 hci_dev_unlock(hdev);
2541}
2542
2543static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
2544 u16 opcode, struct sk_buff *skb)
2545{
2546 const struct hci_rp_read_enc_key_size *rp;
2547 struct hci_conn *conn;
2548 u16 handle;
2549
2550 BT_DBG("%s status 0x%02x", hdev->name, status);
2551
2552 if (!skb || skb->len < sizeof(*rp)) {
2553 BT_ERR("%s invalid HCI Read Encryption Key Size response",
2554 hdev->name);
2555 return;
2556 }
2557
2558 rp = (void *)skb->data;
2559 handle = le16_to_cpu(rp->handle);
2560
2561 hci_dev_lock(hdev);
2562
2563 conn = hci_conn_hash_lookup_handle(hdev, handle);
2564 if (!conn)
2565 goto unlock;
2566
2567 /* If we fail to read the encryption key size, assume maximum
2568 * (which is the same we do also when this HCI command isn't
2569 * supported.
2570 */
2571 if (rp->status) {
2572 BT_ERR("%s failed to read key size for handle %u", hdev->name,
2573 handle);
2574 conn->enc_key_size = HCI_LINK_KEY_SIZE;
2575 } else {
2576 conn->enc_key_size = rp->key_size;
2577 }
2578
2579 if (conn->state == BT_CONFIG) {
2580 conn->state = BT_CONNECTED;
2581 hci_connect_cfm(conn, 0);
2582 hci_conn_drop(conn);
2583 } else {
2584 u8 encrypt;
2585
2586 if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2587 encrypt = 0x00;
2588 else if (test_bit(HCI_CONN_AES_CCM, &conn->flags))
2589 encrypt = 0x02;
2590 else
2591 encrypt = 0x01;
2592
2593 hci_encrypt_cfm(conn, 0, encrypt);
2594 }
2595
2596unlock:
2597 hci_dev_unlock(hdev);
2598}
2599
2600static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2601{
2602 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2603 struct hci_conn *conn;
2604
2605 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
2606
2607 hci_dev_lock(hdev);
2608
2609 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2610 if (!conn)
2611 goto unlock;
2612
2613 if (!ev->status) {
2614 if (ev->encrypt) {
2615 /* Encryption implies authentication */
2616 set_bit(HCI_CONN_AUTH, &conn->flags);
2617 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
2618 conn->sec_level = conn->pending_sec_level;
2619
2620 /* P-256 authentication key implies FIPS */
2621 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
2622 set_bit(HCI_CONN_FIPS, &conn->flags);
2623
2624 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2625 conn->type == LE_LINK)
2626 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2627 } else {
2628 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
2629 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2630 }
2631 }
2632
2633 /* We should disregard the current RPA and generate a new one
2634 * whenever the encryption procedure fails.
2635 */
2636 if (ev->status && conn->type == LE_LINK)
2637 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
2638
2639 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2640
2641 if (ev->status && conn->state == BT_CONNECTED) {
2642 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
2643 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2644
2645 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2646 hci_conn_drop(conn);
2647 goto unlock;
2648 }
2649
2650 /* In Secure Connections Only mode, do not allow any connections
2651 * that are not encrypted with AES-CCM using a P-256 authenticated
2652 * combination key.
2653 */
2654 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) &&
2655 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2656 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2657 hci_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2658 hci_conn_drop(conn);
2659 goto unlock;
2660 }
2661
2662 /* Try reading the encryption key size for encrypted ACL links */
2663 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
2664 struct hci_cp_read_enc_key_size cp;
2665 struct hci_request req;
2666
2667 /* Only send HCI_Read_Encryption_Key_Size if the
2668 * controller really supports it. If it doesn't, assume
2669 * the default size (16).
2670 */
2671 if (!(hdev->commands[20] & 0x10)) {
2672 conn->enc_key_size = HCI_LINK_KEY_SIZE;
2673 goto notify;
2674 }
2675
2676 hci_req_init(&req, hdev);
2677
2678 cp.handle = cpu_to_le16(conn->handle);
2679 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
2680
2681 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
2682 BT_ERR("Sending HCI Read Encryption Key Size failed");
2683 conn->enc_key_size = HCI_LINK_KEY_SIZE;
2684 goto notify;
2685 }
2686
2687 goto unlock;
2688 }
2689
2690notify:
2691 if (conn->state == BT_CONFIG) {
2692 if (!ev->status)
2693 conn->state = BT_CONNECTED;
2694
2695 hci_connect_cfm(conn, ev->status);
2696 hci_conn_drop(conn);
2697 } else
2698 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2699
2700unlock:
2701 hci_dev_unlock(hdev);
2702}
2703
2704static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2705 struct sk_buff *skb)
2706{
2707 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2708 struct hci_conn *conn;
2709
2710 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
2711
2712 hci_dev_lock(hdev);
2713
2714 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2715 if (conn) {
2716 if (!ev->status)
2717 set_bit(HCI_CONN_SECURE, &conn->flags);
2718
2719 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2720
2721 hci_key_change_cfm(conn, ev->status);
2722 }
2723
2724 hci_dev_unlock(hdev);
2725}
2726
2727static void hci_remote_features_evt(struct hci_dev *hdev,
2728 struct sk_buff *skb)
2729{
2730 struct hci_ev_remote_features *ev = (void *) skb->data;
2731 struct hci_conn *conn;
2732
2733 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
2734
2735 hci_dev_lock(hdev);
2736
2737 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2738 if (!conn)
2739 goto unlock;
2740
2741 if (!ev->status)
2742 memcpy(conn->features[0], ev->features, 8);
2743
2744 if (conn->state != BT_CONFIG)
2745 goto unlock;
2746
2747 if (!ev->status && lmp_ext_feat_capable(hdev) &&
2748 lmp_ext_feat_capable(conn)) {
2749 struct hci_cp_read_remote_ext_features cp;
2750 cp.handle = ev->handle;
2751 cp.page = 0x01;
2752 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
2753 sizeof(cp), &cp);
2754 goto unlock;
2755 }
2756
2757 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
2758 struct hci_cp_remote_name_req cp;
2759 memset(&cp, 0, sizeof(cp));
2760 bacpy(&cp.bdaddr, &conn->dst);
2761 cp.pscan_rep_mode = 0x02;
2762 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2763 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2764 mgmt_device_connected(hdev, conn, 0, NULL, 0);
2765
2766 if (!hci_outgoing_auth_needed(hdev, conn)) {
2767 conn->state = BT_CONNECTED;
2768 hci_connect_cfm(conn, ev->status);
2769 hci_conn_drop(conn);
2770 }
2771
2772unlock:
2773 hci_dev_unlock(hdev);
2774}
2775
2776static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
2777 u16 *opcode, u8 *status,
2778 hci_req_complete_t *req_complete,
2779 hci_req_complete_skb_t *req_complete_skb)
2780{
2781 struct hci_ev_cmd_complete *ev = (void *) skb->data;
2782
2783 *opcode = __le16_to_cpu(ev->opcode);
2784 *status = skb->data[sizeof(*ev)];
2785
2786 skb_pull(skb, sizeof(*ev));
2787
2788 switch (*opcode) {
2789 case HCI_OP_INQUIRY_CANCEL:
2790 hci_cc_inquiry_cancel(hdev, skb, status);
2791 break;
2792
2793 case HCI_OP_PERIODIC_INQ:
2794 hci_cc_periodic_inq(hdev, skb);
2795 break;
2796
2797 case HCI_OP_EXIT_PERIODIC_INQ:
2798 hci_cc_exit_periodic_inq(hdev, skb);
2799 break;
2800
2801 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2802 hci_cc_remote_name_req_cancel(hdev, skb);
2803 break;
2804
2805 case HCI_OP_ROLE_DISCOVERY:
2806 hci_cc_role_discovery(hdev, skb);
2807 break;
2808
2809 case HCI_OP_READ_LINK_POLICY:
2810 hci_cc_read_link_policy(hdev, skb);
2811 break;
2812
2813 case HCI_OP_WRITE_LINK_POLICY:
2814 hci_cc_write_link_policy(hdev, skb);
2815 break;
2816
2817 case HCI_OP_READ_DEF_LINK_POLICY:
2818 hci_cc_read_def_link_policy(hdev, skb);
2819 break;
2820
2821 case HCI_OP_WRITE_DEF_LINK_POLICY:
2822 hci_cc_write_def_link_policy(hdev, skb);
2823 break;
2824
2825 case HCI_OP_RESET:
2826 hci_cc_reset(hdev, skb);
2827 break;
2828
2829 case HCI_OP_READ_STORED_LINK_KEY:
2830 hci_cc_read_stored_link_key(hdev, skb);
2831 break;
2832
2833 case HCI_OP_DELETE_STORED_LINK_KEY:
2834 hci_cc_delete_stored_link_key(hdev, skb);
2835 break;
2836
2837 case HCI_OP_WRITE_LOCAL_NAME:
2838 hci_cc_write_local_name(hdev, skb);
2839 break;
2840
2841 case HCI_OP_READ_LOCAL_NAME:
2842 hci_cc_read_local_name(hdev, skb);
2843 break;
2844
2845 case HCI_OP_WRITE_AUTH_ENABLE:
2846 hci_cc_write_auth_enable(hdev, skb);
2847 break;
2848
2849 case HCI_OP_WRITE_ENCRYPT_MODE:
2850 hci_cc_write_encrypt_mode(hdev, skb);
2851 break;
2852
2853 case HCI_OP_WRITE_SCAN_ENABLE:
2854 hci_cc_write_scan_enable(hdev, skb);
2855 break;
2856
2857 case HCI_OP_READ_CLASS_OF_DEV:
2858 hci_cc_read_class_of_dev(hdev, skb);
2859 break;
2860
2861 case HCI_OP_WRITE_CLASS_OF_DEV:
2862 hci_cc_write_class_of_dev(hdev, skb);
2863 break;
2864
2865 case HCI_OP_READ_VOICE_SETTING:
2866 hci_cc_read_voice_setting(hdev, skb);
2867 break;
2868
2869 case HCI_OP_WRITE_VOICE_SETTING:
2870 hci_cc_write_voice_setting(hdev, skb);
2871 break;
2872
2873 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2874 hci_cc_read_num_supported_iac(hdev, skb);
2875 break;
2876
2877 case HCI_OP_WRITE_SSP_MODE:
2878 hci_cc_write_ssp_mode(hdev, skb);
2879 break;
2880
2881 case HCI_OP_WRITE_SC_SUPPORT:
2882 hci_cc_write_sc_support(hdev, skb);
2883 break;
2884
2885 case HCI_OP_READ_LOCAL_VERSION:
2886 hci_cc_read_local_version(hdev, skb);
2887 break;
2888
2889 case HCI_OP_READ_LOCAL_COMMANDS:
2890 hci_cc_read_local_commands(hdev, skb);
2891 break;
2892
2893 case HCI_OP_READ_LOCAL_FEATURES:
2894 hci_cc_read_local_features(hdev, skb);
2895 break;
2896
2897 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2898 hci_cc_read_local_ext_features(hdev, skb);
2899 break;
2900
2901 case HCI_OP_READ_BUFFER_SIZE:
2902 hci_cc_read_buffer_size(hdev, skb);
2903 break;
2904
2905 case HCI_OP_READ_BD_ADDR:
2906 hci_cc_read_bd_addr(hdev, skb);
2907 break;
2908
2909 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2910 hci_cc_read_page_scan_activity(hdev, skb);
2911 break;
2912
2913 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2914 hci_cc_write_page_scan_activity(hdev, skb);
2915 break;
2916
2917 case HCI_OP_READ_PAGE_SCAN_TYPE:
2918 hci_cc_read_page_scan_type(hdev, skb);
2919 break;
2920
2921 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2922 hci_cc_write_page_scan_type(hdev, skb);
2923 break;
2924
2925 case HCI_OP_READ_DATA_BLOCK_SIZE:
2926 hci_cc_read_data_block_size(hdev, skb);
2927 break;
2928
2929 case HCI_OP_READ_FLOW_CONTROL_MODE:
2930 hci_cc_read_flow_control_mode(hdev, skb);
2931 break;
2932
2933 case HCI_OP_READ_LOCAL_AMP_INFO:
2934 hci_cc_read_local_amp_info(hdev, skb);
2935 break;
2936
2937 case HCI_OP_READ_CLOCK:
2938 hci_cc_read_clock(hdev, skb);
2939 break;
2940
2941 case HCI_OP_READ_INQ_RSP_TX_POWER:
2942 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2943 break;
2944
2945 case HCI_OP_PIN_CODE_REPLY:
2946 hci_cc_pin_code_reply(hdev, skb);
2947 break;
2948
2949 case HCI_OP_PIN_CODE_NEG_REPLY:
2950 hci_cc_pin_code_neg_reply(hdev, skb);
2951 break;
2952
2953 case HCI_OP_READ_LOCAL_OOB_DATA:
2954 hci_cc_read_local_oob_data(hdev, skb);
2955 break;
2956
2957 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2958 hci_cc_read_local_oob_ext_data(hdev, skb);
2959 break;
2960
2961 case HCI_OP_LE_READ_BUFFER_SIZE:
2962 hci_cc_le_read_buffer_size(hdev, skb);
2963 break;
2964
2965 case HCI_OP_LE_READ_LOCAL_FEATURES:
2966 hci_cc_le_read_local_features(hdev, skb);
2967 break;
2968
2969 case HCI_OP_LE_READ_ADV_TX_POWER:
2970 hci_cc_le_read_adv_tx_power(hdev, skb);
2971 break;
2972
2973 case HCI_OP_USER_CONFIRM_REPLY:
2974 hci_cc_user_confirm_reply(hdev, skb);
2975 break;
2976
2977 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2978 hci_cc_user_confirm_neg_reply(hdev, skb);
2979 break;
2980
2981 case HCI_OP_USER_PASSKEY_REPLY:
2982 hci_cc_user_passkey_reply(hdev, skb);
2983 break;
2984
2985 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2986 hci_cc_user_passkey_neg_reply(hdev, skb);
2987 break;
2988
2989 case HCI_OP_LE_SET_RANDOM_ADDR:
2990 hci_cc_le_set_random_addr(hdev, skb);
2991 break;
2992
2993 case HCI_OP_LE_SET_ADV_ENABLE:
2994 hci_cc_le_set_adv_enable(hdev, skb);
2995 break;
2996
2997 case HCI_OP_LE_SET_SCAN_PARAM:
2998 hci_cc_le_set_scan_param(hdev, skb);
2999 break;
3000
3001 case HCI_OP_LE_SET_SCAN_ENABLE:
3002 hci_cc_le_set_scan_enable(hdev, skb);
3003 break;
3004
3005 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
3006 hci_cc_le_read_white_list_size(hdev, skb);
3007 break;
3008
3009 case HCI_OP_LE_CLEAR_WHITE_LIST:
3010 hci_cc_le_clear_white_list(hdev, skb);
3011 break;
3012
3013 case HCI_OP_LE_ADD_TO_WHITE_LIST:
3014 hci_cc_le_add_to_white_list(hdev, skb);
3015 break;
3016
3017 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
3018 hci_cc_le_del_from_white_list(hdev, skb);
3019 break;
3020
3021 case HCI_OP_LE_READ_SUPPORTED_STATES:
3022 hci_cc_le_read_supported_states(hdev, skb);
3023 break;
3024
3025 case HCI_OP_LE_READ_DEF_DATA_LEN:
3026 hci_cc_le_read_def_data_len(hdev, skb);
3027 break;
3028
3029 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
3030 hci_cc_le_write_def_data_len(hdev, skb);
3031 break;
3032
3033 case HCI_OP_LE_READ_MAX_DATA_LEN:
3034 hci_cc_le_read_max_data_len(hdev, skb);
3035 break;
3036
3037 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
3038 hci_cc_write_le_host_supported(hdev, skb);
3039 break;
3040
3041 case HCI_OP_LE_SET_ADV_PARAM:
3042 hci_cc_set_adv_param(hdev, skb);
3043 break;
3044
3045 case HCI_OP_READ_RSSI:
3046 hci_cc_read_rssi(hdev, skb);
3047 break;
3048
3049 case HCI_OP_READ_TX_POWER:
3050 hci_cc_read_tx_power(hdev, skb);
3051 break;
3052
3053 case HCI_OP_WRITE_SSP_DEBUG_MODE:
3054 hci_cc_write_ssp_debug_mode(hdev, skb);
3055 break;
3056
3057 default:
3058 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
3059 break;
3060 }
3061
3062 if (*opcode != HCI_OP_NOP)
3063 cancel_delayed_work(&hdev->cmd_timer);
3064
3065 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3066 atomic_set(&hdev->cmd_cnt, 1);
3067
3068 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3069 req_complete_skb);
3070
3071 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3072 queue_work(hdev->workqueue, &hdev->cmd_work);
3073}
3074
3075static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3076 u16 *opcode, u8 *status,
3077 hci_req_complete_t *req_complete,
3078 hci_req_complete_skb_t *req_complete_skb)
3079{
3080 struct hci_ev_cmd_status *ev = (void *) skb->data;
3081
3082 skb_pull(skb, sizeof(*ev));
3083
3084 *opcode = __le16_to_cpu(ev->opcode);
3085 *status = ev->status;
3086
3087 switch (*opcode) {
3088 case HCI_OP_INQUIRY:
3089 hci_cs_inquiry(hdev, ev->status);
3090 break;
3091
3092 case HCI_OP_CREATE_CONN:
3093 hci_cs_create_conn(hdev, ev->status);
3094 break;
3095
3096 case HCI_OP_DISCONNECT:
3097 hci_cs_disconnect(hdev, ev->status);
3098 break;
3099
3100 case HCI_OP_ADD_SCO:
3101 hci_cs_add_sco(hdev, ev->status);
3102 break;
3103
3104 case HCI_OP_AUTH_REQUESTED:
3105 hci_cs_auth_requested(hdev, ev->status);
3106 break;
3107
3108 case HCI_OP_SET_CONN_ENCRYPT:
3109 hci_cs_set_conn_encrypt(hdev, ev->status);
3110 break;
3111
3112 case HCI_OP_REMOTE_NAME_REQ:
3113 hci_cs_remote_name_req(hdev, ev->status);
3114 break;
3115
3116 case HCI_OP_READ_REMOTE_FEATURES:
3117 hci_cs_read_remote_features(hdev, ev->status);
3118 break;
3119
3120 case HCI_OP_READ_REMOTE_EXT_FEATURES:
3121 hci_cs_read_remote_ext_features(hdev, ev->status);
3122 break;
3123
3124 case HCI_OP_SETUP_SYNC_CONN:
3125 hci_cs_setup_sync_conn(hdev, ev->status);
3126 break;
3127
3128 case HCI_OP_SNIFF_MODE:
3129 hci_cs_sniff_mode(hdev, ev->status);
3130 break;
3131
3132 case HCI_OP_EXIT_SNIFF_MODE:
3133 hci_cs_exit_sniff_mode(hdev, ev->status);
3134 break;
3135
3136 case HCI_OP_SWITCH_ROLE:
3137 hci_cs_switch_role(hdev, ev->status);
3138 break;
3139
3140 case HCI_OP_LE_CREATE_CONN:
3141 hci_cs_le_create_conn(hdev, ev->status);
3142 break;
3143
3144 case HCI_OP_LE_READ_REMOTE_FEATURES:
3145 hci_cs_le_read_remote_features(hdev, ev->status);
3146 break;
3147
3148 case HCI_OP_LE_START_ENC:
3149 hci_cs_le_start_enc(hdev, ev->status);
3150 break;
3151
3152 default:
3153 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
3154 break;
3155 }
3156
3157 if (*opcode != HCI_OP_NOP)
3158 cancel_delayed_work(&hdev->cmd_timer);
3159
3160 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3161 atomic_set(&hdev->cmd_cnt, 1);
3162
3163 /* Indicate request completion if the command failed. Also, if
3164 * we're not waiting for a special event and we get a success
3165 * command status we should try to flag the request as completed
3166 * (since for this kind of commands there will not be a command
3167 * complete event).
3168 */
3169 if (ev->status ||
3170 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
3171 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3172 req_complete_skb);
3173
3174 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3175 queue_work(hdev->workqueue, &hdev->cmd_work);
3176}
3177
3178static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3179{
3180 struct hci_ev_hardware_error *ev = (void *) skb->data;
3181
3182 hdev->hw_error_code = ev->code;
3183
3184 queue_work(hdev->req_workqueue, &hdev->error_reset);
3185}
3186
3187static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
3188{
3189 struct hci_ev_role_change *ev = (void *) skb->data;
3190 struct hci_conn *conn;
3191
3192 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3193
3194 hci_dev_lock(hdev);
3195
3196 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3197 if (conn) {
3198 if (!ev->status)
3199 conn->role = ev->role;
3200
3201 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
3202
3203 hci_role_switch_cfm(conn, ev->status, ev->role);
3204 }
3205
3206 hci_dev_unlock(hdev);
3207}
3208
3209static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
3210{
3211 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
3212 int i;
3213
3214 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
3215 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3216 return;
3217 }
3218
3219 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
3220 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
3221 BT_DBG("%s bad parameters", hdev->name);
3222 return;
3223 }
3224
3225 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3226
3227 for (i = 0; i < ev->num_hndl; i++) {
3228 struct hci_comp_pkts_info *info = &ev->handles[i];
3229 struct hci_conn *conn;
3230 __u16 handle, count;
3231
3232 handle = __le16_to_cpu(info->handle);
3233 count = __le16_to_cpu(info->count);
3234
3235 conn = hci_conn_hash_lookup_handle(hdev, handle);
3236 if (!conn)
3237 continue;
3238
3239 conn->sent -= count;
3240
3241 switch (conn->type) {
3242 case ACL_LINK:
3243 hdev->acl_cnt += count;
3244 if (hdev->acl_cnt > hdev->acl_pkts)
3245 hdev->acl_cnt = hdev->acl_pkts;
3246 break;
3247
3248 case LE_LINK:
3249 if (hdev->le_pkts) {
3250 hdev->le_cnt += count;
3251 if (hdev->le_cnt > hdev->le_pkts)
3252 hdev->le_cnt = hdev->le_pkts;
3253 } else {
3254 hdev->acl_cnt += count;
3255 if (hdev->acl_cnt > hdev->acl_pkts)
3256 hdev->acl_cnt = hdev->acl_pkts;
3257 }
3258 break;
3259
3260 case SCO_LINK:
3261 hdev->sco_cnt += count;
3262 if (hdev->sco_cnt > hdev->sco_pkts)
3263 hdev->sco_cnt = hdev->sco_pkts;
3264 break;
3265
3266 default:
3267 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3268 break;
3269 }
3270 }
3271
3272 queue_work(hdev->workqueue, &hdev->tx_work);
3273}
3274
3275static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3276 __u16 handle)
3277{
3278 struct hci_chan *chan;
3279
3280 switch (hdev->dev_type) {
3281 case HCI_PRIMARY:
3282 return hci_conn_hash_lookup_handle(hdev, handle);
3283 case HCI_AMP:
3284 chan = hci_chan_lookup_handle(hdev, handle);
3285 if (chan)
3286 return chan->conn;
3287 break;
3288 default:
3289 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3290 break;
3291 }
3292
3293 return NULL;
3294}
3295
3296static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
3297{
3298 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3299 int i;
3300
3301 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
3302 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3303 return;
3304 }
3305
3306 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
3307 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
3308 BT_DBG("%s bad parameters", hdev->name);
3309 return;
3310 }
3311
3312 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
3313 ev->num_hndl);
3314
3315 for (i = 0; i < ev->num_hndl; i++) {
3316 struct hci_comp_blocks_info *info = &ev->handles[i];
3317 struct hci_conn *conn = NULL;
3318 __u16 handle, block_count;
3319
3320 handle = __le16_to_cpu(info->handle);
3321 block_count = __le16_to_cpu(info->blocks);
3322
3323 conn = __hci_conn_lookup_handle(hdev, handle);
3324 if (!conn)
3325 continue;
3326
3327 conn->sent -= block_count;
3328
3329 switch (conn->type) {
3330 case ACL_LINK:
3331 case AMP_LINK:
3332 hdev->block_cnt += block_count;
3333 if (hdev->block_cnt > hdev->num_blocks)
3334 hdev->block_cnt = hdev->num_blocks;
3335 break;
3336
3337 default:
3338 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3339 break;
3340 }
3341 }
3342
3343 queue_work(hdev->workqueue, &hdev->tx_work);
3344}
3345
3346static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
3347{
3348 struct hci_ev_mode_change *ev = (void *) skb->data;
3349 struct hci_conn *conn;
3350
3351 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3352
3353 hci_dev_lock(hdev);
3354
3355 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3356 if (conn) {
3357 conn->mode = ev->mode;
3358
3359 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3360 &conn->flags)) {
3361 if (conn->mode == HCI_CM_ACTIVE)
3362 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
3363 else
3364 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
3365 }
3366
3367 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
3368 hci_sco_setup(conn, ev->status);
3369 }
3370
3371 hci_dev_unlock(hdev);
3372}
3373
3374static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3375{
3376 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3377 struct hci_conn *conn;
3378
3379 BT_DBG("%s", hdev->name);
3380
3381 hci_dev_lock(hdev);
3382
3383 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3384 if (!conn)
3385 goto unlock;
3386
3387 if (conn->state == BT_CONNECTED) {
3388 hci_conn_hold(conn);
3389 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
3390 hci_conn_drop(conn);
3391 }
3392
3393 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
3394 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
3395 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
3396 sizeof(ev->bdaddr), &ev->bdaddr);
3397 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
3398 u8 secure;
3399
3400 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3401 secure = 1;
3402 else
3403 secure = 0;
3404
3405 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
3406 }
3407
3408unlock:
3409 hci_dev_unlock(hdev);
3410}
3411
3412static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
3413{
3414 if (key_type == HCI_LK_CHANGED_COMBINATION)
3415 return;
3416
3417 conn->pin_length = pin_len;
3418 conn->key_type = key_type;
3419
3420 switch (key_type) {
3421 case HCI_LK_LOCAL_UNIT:
3422 case HCI_LK_REMOTE_UNIT:
3423 case HCI_LK_DEBUG_COMBINATION:
3424 return;
3425 case HCI_LK_COMBINATION:
3426 if (pin_len == 16)
3427 conn->pending_sec_level = BT_SECURITY_HIGH;
3428 else
3429 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3430 break;
3431 case HCI_LK_UNAUTH_COMBINATION_P192:
3432 case HCI_LK_UNAUTH_COMBINATION_P256:
3433 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3434 break;
3435 case HCI_LK_AUTH_COMBINATION_P192:
3436 conn->pending_sec_level = BT_SECURITY_HIGH;
3437 break;
3438 case HCI_LK_AUTH_COMBINATION_P256:
3439 conn->pending_sec_level = BT_SECURITY_FIPS;
3440 break;
3441 }
3442}
3443
3444static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3445{
3446 struct hci_ev_link_key_req *ev = (void *) skb->data;
3447 struct hci_cp_link_key_reply cp;
3448 struct hci_conn *conn;
3449 struct link_key *key;
3450
3451 BT_DBG("%s", hdev->name);
3452
3453 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3454 return;
3455
3456 hci_dev_lock(hdev);
3457
3458 key = hci_find_link_key(hdev, &ev->bdaddr);
3459 if (!key) {
3460 BT_DBG("%s link key not found for %pMR", hdev->name,
3461 &ev->bdaddr);
3462 goto not_found;
3463 }
3464
3465 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3466 &ev->bdaddr);
3467
3468 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3469 if (conn) {
3470 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
3471
3472 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3473 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
3474 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
3475 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3476 goto not_found;
3477 }
3478
3479 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
3480 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3481 conn->pending_sec_level == BT_SECURITY_FIPS)) {
3482 BT_DBG("%s ignoring key unauthenticated for high security",
3483 hdev->name);
3484 goto not_found;
3485 }
3486
3487 conn_set_key(conn, key->type, key->pin_len);
3488 }
3489
3490 bacpy(&cp.bdaddr, &ev->bdaddr);
3491 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
3492
3493 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3494
3495 hci_dev_unlock(hdev);
3496
3497 return;
3498
3499not_found:
3500 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3501 hci_dev_unlock(hdev);
3502}
3503
3504static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3505{
3506 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3507 struct hci_conn *conn;
3508 struct link_key *key;
3509 bool persistent;
3510 u8 pin_len = 0;
3511
3512 BT_DBG("%s", hdev->name);
3513
3514 hci_dev_lock(hdev);
3515
3516 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3517 if (!conn)
3518 goto unlock;
3519
3520 hci_conn_hold(conn);
3521 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3522 hci_conn_drop(conn);
3523
3524 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
3525 conn_set_key(conn, ev->key_type, conn->pin_length);
3526
3527 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3528 goto unlock;
3529
3530 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
3531 ev->key_type, pin_len, &persistent);
3532 if (!key)
3533 goto unlock;
3534
3535 /* Update connection information since adding the key will have
3536 * fixed up the type in the case of changed combination keys.
3537 */
3538 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
3539 conn_set_key(conn, key->type, key->pin_len);
3540
3541 mgmt_new_link_key(hdev, key, persistent);
3542
3543 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
3544 * is set. If it's not set simply remove the key from the kernel
3545 * list (we've still notified user space about it but with
3546 * store_hint being 0).
3547 */
3548 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3549 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
3550 list_del_rcu(&key->list);
3551 kfree_rcu(key, rcu);
3552 goto unlock;
3553 }
3554
3555 if (persistent)
3556 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3557 else
3558 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3559
3560unlock:
3561 hci_dev_unlock(hdev);
3562}
3563
3564static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
3565{
3566 struct hci_ev_clock_offset *ev = (void *) skb->data;
3567 struct hci_conn *conn;
3568
3569 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3570
3571 hci_dev_lock(hdev);
3572
3573 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3574 if (conn && !ev->status) {
3575 struct inquiry_entry *ie;
3576
3577 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3578 if (ie) {
3579 ie->data.clock_offset = ev->clock_offset;
3580 ie->timestamp = jiffies;
3581 }
3582 }
3583
3584 hci_dev_unlock(hdev);
3585}
3586
3587static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
3588{
3589 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3590 struct hci_conn *conn;
3591
3592 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3593
3594 hci_dev_lock(hdev);
3595
3596 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3597 if (conn && !ev->status)
3598 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3599
3600 hci_dev_unlock(hdev);
3601}
3602
3603static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
3604{
3605 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
3606 struct inquiry_entry *ie;
3607
3608 BT_DBG("%s", hdev->name);
3609
3610 hci_dev_lock(hdev);
3611
3612 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3613 if (ie) {
3614 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3615 ie->timestamp = jiffies;
3616 }
3617
3618 hci_dev_unlock(hdev);
3619}
3620
3621static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3622 struct sk_buff *skb)
3623{
3624 struct inquiry_data data;
3625 int num_rsp = *((__u8 *) skb->data);
3626
3627 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3628
3629 if (!num_rsp)
3630 return;
3631
3632 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3633 return;
3634
3635 hci_dev_lock(hdev);
3636
3637 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
3638 struct inquiry_info_with_rssi_and_pscan_mode *info;
3639 info = (void *) (skb->data + 1);
3640
3641 if (skb->len < num_rsp * sizeof(*info) + 1)
3642 goto unlock;
3643
3644 for (; num_rsp; num_rsp--, info++) {
3645 u32 flags;
3646
3647 bacpy(&data.bdaddr, &info->bdaddr);
3648 data.pscan_rep_mode = info->pscan_rep_mode;
3649 data.pscan_period_mode = info->pscan_period_mode;
3650 data.pscan_mode = info->pscan_mode;
3651 memcpy(data.dev_class, info->dev_class, 3);
3652 data.clock_offset = info->clock_offset;
3653 data.rssi = info->rssi;
3654 data.ssp_mode = 0x00;
3655
3656 flags = hci_inquiry_cache_update(hdev, &data, false);
3657
3658 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
3659 info->dev_class, info->rssi,
3660 flags, NULL, 0, NULL, 0);
3661 }
3662 } else {
3663 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3664
3665 if (skb->len < num_rsp * sizeof(*info) + 1)
3666 goto unlock;
3667
3668 for (; num_rsp; num_rsp--, info++) {
3669 u32 flags;
3670
3671 bacpy(&data.bdaddr, &info->bdaddr);
3672 data.pscan_rep_mode = info->pscan_rep_mode;
3673 data.pscan_period_mode = info->pscan_period_mode;
3674 data.pscan_mode = 0x00;
3675 memcpy(data.dev_class, info->dev_class, 3);
3676 data.clock_offset = info->clock_offset;
3677 data.rssi = info->rssi;
3678 data.ssp_mode = 0x00;
3679
3680 flags = hci_inquiry_cache_update(hdev, &data, false);
3681
3682 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
3683 info->dev_class, info->rssi,
3684 flags, NULL, 0, NULL, 0);
3685 }
3686 }
3687
3688unlock:
3689 hci_dev_unlock(hdev);
3690}
3691
3692static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3693 struct sk_buff *skb)
3694{
3695 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3696 struct hci_conn *conn;
3697
3698 BT_DBG("%s", hdev->name);
3699
3700 hci_dev_lock(hdev);
3701
3702 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3703 if (!conn)
3704 goto unlock;
3705
3706 if (ev->page < HCI_MAX_PAGES)
3707 memcpy(conn->features[ev->page], ev->features, 8);
3708
3709 if (!ev->status && ev->page == 0x01) {
3710 struct inquiry_entry *ie;
3711
3712 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3713 if (ie)
3714 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
3715
3716 if (ev->features[0] & LMP_HOST_SSP) {
3717 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3718 } else {
3719 /* It is mandatory by the Bluetooth specification that
3720 * Extended Inquiry Results are only used when Secure
3721 * Simple Pairing is enabled, but some devices violate
3722 * this.
3723 *
3724 * To make these devices work, the internal SSP
3725 * enabled flag needs to be cleared if the remote host
3726 * features do not indicate SSP support */
3727 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3728 }
3729
3730 if (ev->features[0] & LMP_HOST_SC)
3731 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
3732 }
3733
3734 if (conn->state != BT_CONFIG)
3735 goto unlock;
3736
3737 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
3738 struct hci_cp_remote_name_req cp;
3739 memset(&cp, 0, sizeof(cp));
3740 bacpy(&cp.bdaddr, &conn->dst);
3741 cp.pscan_rep_mode = 0x02;
3742 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
3743 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
3744 mgmt_device_connected(hdev, conn, 0, NULL, 0);
3745
3746 if (!hci_outgoing_auth_needed(hdev, conn)) {
3747 conn->state = BT_CONNECTED;
3748 hci_connect_cfm(conn, ev->status);
3749 hci_conn_drop(conn);
3750 }
3751
3752unlock:
3753 hci_dev_unlock(hdev);
3754}
3755
3756static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3757 struct sk_buff *skb)
3758{
3759 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3760 struct hci_conn *conn;
3761
3762 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
3763
3764 hci_dev_lock(hdev);
3765
3766 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
3767 if (!conn) {
3768 if (ev->link_type == ESCO_LINK)
3769 goto unlock;
3770
3771 /* When the link type in the event indicates SCO connection
3772 * and lookup of the connection object fails, then check
3773 * if an eSCO connection object exists.
3774 *
3775 * The core limits the synchronous connections to either
3776 * SCO or eSCO. The eSCO connection is preferred and tried
3777 * to be setup first and until successfully established,
3778 * the link type will be hinted as eSCO.
3779 */
3780 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3781 if (!conn)
3782 goto unlock;
3783 }
3784
3785 switch (ev->status) {
3786 case 0x00:
3787 conn->handle = __le16_to_cpu(ev->handle);
3788 conn->state = BT_CONNECTED;
3789 conn->type = ev->link_type;
3790
3791 hci_debugfs_create_conn(conn);
3792 hci_conn_add_sysfs(conn);
3793 break;
3794
3795 case 0x10: /* Connection Accept Timeout */
3796 case 0x0d: /* Connection Rejected due to Limited Resources */
3797 case 0x11: /* Unsupported Feature or Parameter Value */
3798 case 0x1c: /* SCO interval rejected */
3799 case 0x1a: /* Unsupported Remote Feature */
3800 case 0x1e: /* Invalid LMP Parameters */
3801 case 0x1f: /* Unspecified error */
3802 case 0x20: /* Unsupported LMP Parameter value */
3803 if (conn->out) {
3804 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3805 (hdev->esco_type & EDR_ESCO_MASK);
3806 if (hci_setup_sync(conn, conn->link->handle))
3807 goto unlock;
3808 }
3809 /* fall through */
3810
3811 default:
3812 conn->state = BT_CLOSED;
3813 break;
3814 }
3815
3816 hci_connect_cfm(conn, ev->status);
3817 if (ev->status)
3818 hci_conn_del(conn);
3819
3820unlock:
3821 hci_dev_unlock(hdev);
3822}
3823
3824static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3825{
3826 size_t parsed = 0;
3827
3828 while (parsed < eir_len) {
3829 u8 field_len = eir[0];
3830
3831 if (field_len == 0)
3832 return parsed;
3833
3834 parsed += field_len + 1;
3835 eir += field_len + 1;
3836 }
3837
3838 return eir_len;
3839}
3840
3841static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3842 struct sk_buff *skb)
3843{
3844 struct inquiry_data data;
3845 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3846 int num_rsp = *((__u8 *) skb->data);
3847 size_t eir_len;
3848
3849 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3850
3851 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
3852 return;
3853
3854 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3855 return;
3856
3857 hci_dev_lock(hdev);
3858
3859 for (; num_rsp; num_rsp--, info++) {
3860 u32 flags;
3861 bool name_known;
3862
3863 bacpy(&data.bdaddr, &info->bdaddr);
3864 data.pscan_rep_mode = info->pscan_rep_mode;
3865 data.pscan_period_mode = info->pscan_period_mode;
3866 data.pscan_mode = 0x00;
3867 memcpy(data.dev_class, info->dev_class, 3);
3868 data.clock_offset = info->clock_offset;
3869 data.rssi = info->rssi;
3870 data.ssp_mode = 0x01;
3871
3872 if (hci_dev_test_flag(hdev, HCI_MGMT))
3873 name_known = eir_get_data(info->data,
3874 sizeof(info->data),
3875 EIR_NAME_COMPLETE, NULL);
3876 else
3877 name_known = true;
3878
3879 flags = hci_inquiry_cache_update(hdev, &data, name_known);
3880
3881 eir_len = eir_get_length(info->data, sizeof(info->data));
3882
3883 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
3884 info->dev_class, info->rssi,
3885 flags, info->data, eir_len, NULL, 0);
3886 }
3887
3888 hci_dev_unlock(hdev);
3889}
3890
3891static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3892 struct sk_buff *skb)
3893{
3894 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3895 struct hci_conn *conn;
3896
3897 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
3898 __le16_to_cpu(ev->handle));
3899
3900 hci_dev_lock(hdev);
3901
3902 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3903 if (!conn)
3904 goto unlock;
3905
3906 /* For BR/EDR the necessary steps are taken through the
3907 * auth_complete event.
3908 */
3909 if (conn->type != LE_LINK)
3910 goto unlock;
3911
3912 if (!ev->status)
3913 conn->sec_level = conn->pending_sec_level;
3914
3915 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3916
3917 if (ev->status && conn->state == BT_CONNECTED) {
3918 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3919 hci_conn_drop(conn);
3920 goto unlock;
3921 }
3922
3923 if (conn->state == BT_CONFIG) {
3924 if (!ev->status)
3925 conn->state = BT_CONNECTED;
3926
3927 hci_connect_cfm(conn, ev->status);
3928 hci_conn_drop(conn);
3929 } else {
3930 hci_auth_cfm(conn, ev->status);
3931
3932 hci_conn_hold(conn);
3933 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3934 hci_conn_drop(conn);
3935 }
3936
3937unlock:
3938 hci_dev_unlock(hdev);
3939}
3940
3941static u8 hci_get_auth_req(struct hci_conn *conn)
3942{
3943 /* If remote requests no-bonding follow that lead */
3944 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3945 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
3946 return conn->remote_auth | (conn->auth_type & 0x01);
3947
3948 /* If both remote and local have enough IO capabilities, require
3949 * MITM protection
3950 */
3951 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3952 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3953 return conn->remote_auth | 0x01;
3954
3955 /* No MITM protection possible so ignore remote requirement */
3956 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
3957}
3958
3959static u8 bredr_oob_data_present(struct hci_conn *conn)
3960{
3961 struct hci_dev *hdev = conn->hdev;
3962 struct oob_data *data;
3963
3964 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
3965 if (!data)
3966 return 0x00;
3967
3968 if (bredr_sc_enabled(hdev)) {
3969 /* When Secure Connections is enabled, then just
3970 * return the present value stored with the OOB
3971 * data. The stored value contains the right present
3972 * information. However it can only be trusted when
3973 * not in Secure Connection Only mode.
3974 */
3975 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
3976 return data->present;
3977
3978 /* When Secure Connections Only mode is enabled, then
3979 * the P-256 values are required. If they are not
3980 * available, then do not declare that OOB data is
3981 * present.
3982 */
3983 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
3984 !memcmp(data->hash256, ZERO_KEY, 16))
3985 return 0x00;
3986
3987 return 0x02;
3988 }
3989
3990 /* When Secure Connections is not enabled or actually
3991 * not supported by the hardware, then check that if
3992 * P-192 data values are present.
3993 */
3994 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
3995 !memcmp(data->hash192, ZERO_KEY, 16))
3996 return 0x00;
3997
3998 return 0x01;
3999}
4000
4001static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
4002{
4003 struct hci_ev_io_capa_request *ev = (void *) skb->data;
4004 struct hci_conn *conn;
4005
4006 BT_DBG("%s", hdev->name);
4007
4008 hci_dev_lock(hdev);
4009
4010 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4011 if (!conn)
4012 goto unlock;
4013
4014 hci_conn_hold(conn);
4015
4016 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4017 goto unlock;
4018
4019 /* Allow pairing if we're pairable, the initiators of the
4020 * pairing or if the remote is not requesting bonding.
4021 */
4022 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
4023 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
4024 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
4025 struct hci_cp_io_capability_reply cp;
4026
4027 bacpy(&cp.bdaddr, &ev->bdaddr);
4028 /* Change the IO capability from KeyboardDisplay
4029 * to DisplayYesNo as it is not supported by BT spec. */
4030 cp.capability = (conn->io_capability == 0x04) ?
4031 HCI_IO_DISPLAY_YESNO : conn->io_capability;
4032
4033 /* If we are initiators, there is no remote information yet */
4034 if (conn->remote_auth == 0xff) {
4035 /* Request MITM protection if our IO caps allow it
4036 * except for the no-bonding case.
4037 */
4038 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
4039 conn->auth_type != HCI_AT_NO_BONDING)
4040 conn->auth_type |= 0x01;
4041 } else {
4042 conn->auth_type = hci_get_auth_req(conn);
4043 }
4044
4045 /* If we're not bondable, force one of the non-bondable
4046 * authentication requirement values.
4047 */
4048 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
4049 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
4050
4051 cp.authentication = conn->auth_type;
4052 cp.oob_data = bredr_oob_data_present(conn);
4053
4054 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
4055 sizeof(cp), &cp);
4056 } else {
4057 struct hci_cp_io_capability_neg_reply cp;
4058
4059 bacpy(&cp.bdaddr, &ev->bdaddr);
4060 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
4061
4062 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
4063 sizeof(cp), &cp);
4064 }
4065
4066unlock:
4067 hci_dev_unlock(hdev);
4068}
4069
4070static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
4071{
4072 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
4073 struct hci_conn *conn;
4074
4075 BT_DBG("%s", hdev->name);
4076
4077 hci_dev_lock(hdev);
4078
4079 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4080 if (!conn)
4081 goto unlock;
4082
4083 conn->remote_cap = ev->capability;
4084 conn->remote_auth = ev->authentication;
4085
4086unlock:
4087 hci_dev_unlock(hdev);
4088}
4089
4090static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4091 struct sk_buff *skb)
4092{
4093 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
4094 int loc_mitm, rem_mitm, confirm_hint = 0;
4095 struct hci_conn *conn;
4096
4097 BT_DBG("%s", hdev->name);
4098
4099 hci_dev_lock(hdev);
4100
4101 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4102 goto unlock;
4103
4104 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4105 if (!conn)
4106 goto unlock;
4107
4108 loc_mitm = (conn->auth_type & 0x01);
4109 rem_mitm = (conn->remote_auth & 0x01);
4110
4111 /* If we require MITM but the remote device can't provide that
4112 * (it has NoInputNoOutput) then reject the confirmation
4113 * request. We check the security level here since it doesn't
4114 * necessarily match conn->auth_type.
4115 */
4116 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
4117 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
4118 BT_DBG("Rejecting request: remote device can't provide MITM");
4119 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
4120 sizeof(ev->bdaddr), &ev->bdaddr);
4121 goto unlock;
4122 }
4123
4124 /* If no side requires MITM protection; auto-accept */
4125 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
4126 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
4127
4128 /* If we're not the initiators request authorization to
4129 * proceed from user space (mgmt_user_confirm with
4130 * confirm_hint set to 1). The exception is if neither
4131 * side had MITM or if the local IO capability is
4132 * NoInputNoOutput, in which case we do auto-accept
4133 */
4134 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
4135 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
4136 (loc_mitm || rem_mitm)) {
4137 BT_DBG("Confirming auto-accept as acceptor");
4138 confirm_hint = 1;
4139 goto confirm;
4140 }
4141
4142 BT_DBG("Auto-accept of user confirmation with %ums delay",
4143 hdev->auto_accept_delay);
4144
4145 if (hdev->auto_accept_delay > 0) {
4146 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
4147 queue_delayed_work(conn->hdev->workqueue,
4148 &conn->auto_accept_work, delay);
4149 goto unlock;
4150 }
4151
4152 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
4153 sizeof(ev->bdaddr), &ev->bdaddr);
4154 goto unlock;
4155 }
4156
4157confirm:
4158 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
4159 le32_to_cpu(ev->passkey), confirm_hint);
4160
4161unlock:
4162 hci_dev_unlock(hdev);
4163}
4164
4165static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4166 struct sk_buff *skb)
4167{
4168 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
4169
4170 BT_DBG("%s", hdev->name);
4171
4172 if (hci_dev_test_flag(hdev, HCI_MGMT))
4173 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
4174}
4175
4176static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4177 struct sk_buff *skb)
4178{
4179 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
4180 struct hci_conn *conn;
4181
4182 BT_DBG("%s", hdev->name);
4183
4184 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4185 if (!conn)
4186 return;
4187
4188 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4189 conn->passkey_entered = 0;
4190
4191 if (hci_dev_test_flag(hdev, HCI_MGMT))
4192 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4193 conn->dst_type, conn->passkey_notify,
4194 conn->passkey_entered);
4195}
4196
4197static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4198{
4199 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4200 struct hci_conn *conn;
4201
4202 BT_DBG("%s", hdev->name);
4203
4204 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4205 if (!conn)
4206 return;
4207
4208 switch (ev->type) {
4209 case HCI_KEYPRESS_STARTED:
4210 conn->passkey_entered = 0;
4211 return;
4212
4213 case HCI_KEYPRESS_ENTERED:
4214 conn->passkey_entered++;
4215 break;
4216
4217 case HCI_KEYPRESS_ERASED:
4218 conn->passkey_entered--;
4219 break;
4220
4221 case HCI_KEYPRESS_CLEARED:
4222 conn->passkey_entered = 0;
4223 break;
4224
4225 case HCI_KEYPRESS_COMPLETED:
4226 return;
4227 }
4228
4229 if (hci_dev_test_flag(hdev, HCI_MGMT))
4230 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4231 conn->dst_type, conn->passkey_notify,
4232 conn->passkey_entered);
4233}
4234
4235static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4236 struct sk_buff *skb)
4237{
4238 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4239 struct hci_conn *conn;
4240
4241 BT_DBG("%s", hdev->name);
4242
4243 hci_dev_lock(hdev);
4244
4245 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4246 if (!conn)
4247 goto unlock;
4248
4249 /* Reset the authentication requirement to unknown */
4250 conn->remote_auth = 0xff;
4251
4252 /* To avoid duplicate auth_failed events to user space we check
4253 * the HCI_CONN_AUTH_PEND flag which will be set if we
4254 * initiated the authentication. A traditional auth_complete
4255 * event gets always produced as initiator and is also mapped to
4256 * the mgmt_auth_failed event */
4257 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
4258 mgmt_auth_failed(conn, ev->status);
4259
4260 hci_conn_drop(conn);
4261
4262unlock:
4263 hci_dev_unlock(hdev);
4264}
4265
4266static void hci_remote_host_features_evt(struct hci_dev *hdev,
4267 struct sk_buff *skb)
4268{
4269 struct hci_ev_remote_host_features *ev = (void *) skb->data;
4270 struct inquiry_entry *ie;
4271 struct hci_conn *conn;
4272
4273 BT_DBG("%s", hdev->name);
4274
4275 hci_dev_lock(hdev);
4276
4277 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4278 if (conn)
4279 memcpy(conn->features[1], ev->features, 8);
4280
4281 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4282 if (ie)
4283 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
4284
4285 hci_dev_unlock(hdev);
4286}
4287
4288static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4289 struct sk_buff *skb)
4290{
4291 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
4292 struct oob_data *data;
4293
4294 BT_DBG("%s", hdev->name);
4295
4296 hci_dev_lock(hdev);
4297
4298 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4299 goto unlock;
4300
4301 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
4302 if (!data) {
4303 struct hci_cp_remote_oob_data_neg_reply cp;
4304
4305 bacpy(&cp.bdaddr, &ev->bdaddr);
4306 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
4307 sizeof(cp), &cp);
4308 goto unlock;
4309 }
4310
4311 if (bredr_sc_enabled(hdev)) {
4312 struct hci_cp_remote_oob_ext_data_reply cp;
4313
4314 bacpy(&cp.bdaddr, &ev->bdaddr);
4315 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
4316 memset(cp.hash192, 0, sizeof(cp.hash192));
4317 memset(cp.rand192, 0, sizeof(cp.rand192));
4318 } else {
4319 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
4320 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
4321 }
4322 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
4323 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
4324
4325 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
4326 sizeof(cp), &cp);
4327 } else {
4328 struct hci_cp_remote_oob_data_reply cp;
4329
4330 bacpy(&cp.bdaddr, &ev->bdaddr);
4331 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
4332 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
4333
4334 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
4335 sizeof(cp), &cp);
4336 }
4337
4338unlock:
4339 hci_dev_unlock(hdev);
4340}
4341
4342#if IS_ENABLED(CONFIG_BT_HS)
4343static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4344{
4345 struct hci_ev_channel_selected *ev = (void *)skb->data;
4346 struct hci_conn *hcon;
4347
4348 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4349
4350 skb_pull(skb, sizeof(*ev));
4351
4352 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4353 if (!hcon)
4354 return;
4355
4356 amp_read_loc_assoc_final_data(hdev, hcon);
4357}
4358
4359static void hci_phy_link_complete_evt(struct hci_dev *hdev,
4360 struct sk_buff *skb)
4361{
4362 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
4363 struct hci_conn *hcon, *bredr_hcon;
4364
4365 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
4366 ev->status);
4367
4368 hci_dev_lock(hdev);
4369
4370 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4371 if (!hcon) {
4372 hci_dev_unlock(hdev);
4373 return;
4374 }
4375
4376 if (ev->status) {
4377 hci_conn_del(hcon);
4378 hci_dev_unlock(hdev);
4379 return;
4380 }
4381
4382 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4383
4384 hcon->state = BT_CONNECTED;
4385 bacpy(&hcon->dst, &bredr_hcon->dst);
4386
4387 hci_conn_hold(hcon);
4388 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
4389 hci_conn_drop(hcon);
4390
4391 hci_debugfs_create_conn(hcon);
4392 hci_conn_add_sysfs(hcon);
4393
4394 amp_physical_cfm(bredr_hcon, hcon);
4395
4396 hci_dev_unlock(hdev);
4397}
4398
4399static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4400{
4401 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
4402 struct hci_conn *hcon;
4403 struct hci_chan *hchan;
4404 struct amp_mgr *mgr;
4405
4406 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
4407 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
4408 ev->status);
4409
4410 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4411 if (!hcon)
4412 return;
4413
4414 /* Create AMP hchan */
4415 hchan = hci_chan_create(hcon);
4416 if (!hchan)
4417 return;
4418
4419 hchan->handle = le16_to_cpu(ev->handle);
4420
4421 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
4422
4423 mgr = hcon->amp_mgr;
4424 if (mgr && mgr->bredr_chan) {
4425 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
4426
4427 l2cap_chan_lock(bredr_chan);
4428
4429 bredr_chan->conn->mtu = hdev->block_mtu;
4430 l2cap_logical_cfm(bredr_chan, hchan, 0);
4431 hci_conn_hold(hcon);
4432
4433 l2cap_chan_unlock(bredr_chan);
4434 }
4435}
4436
4437static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
4438 struct sk_buff *skb)
4439{
4440 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
4441 struct hci_chan *hchan;
4442
4443 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
4444 le16_to_cpu(ev->handle), ev->status);
4445
4446 if (ev->status)
4447 return;
4448
4449 hci_dev_lock(hdev);
4450
4451 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
4452 if (!hchan)
4453 goto unlock;
4454
4455 amp_destroy_logical_link(hchan, ev->reason);
4456
4457unlock:
4458 hci_dev_unlock(hdev);
4459}
4460
4461static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
4462 struct sk_buff *skb)
4463{
4464 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
4465 struct hci_conn *hcon;
4466
4467 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4468
4469 if (ev->status)
4470 return;
4471
4472 hci_dev_lock(hdev);
4473
4474 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4475 if (hcon) {
4476 hcon->state = BT_CLOSED;
4477 hci_conn_del(hcon);
4478 }
4479
4480 hci_dev_unlock(hdev);
4481}
4482#endif
4483
4484static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4485{
4486 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
4487 struct hci_conn_params *params;
4488 struct hci_conn *conn;
4489 struct smp_irk *irk;
4490 u8 addr_type;
4491
4492 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4493
4494 hci_dev_lock(hdev);
4495
4496 /* All controllers implicitly stop advertising in the event of a
4497 * connection, so ensure that the state bit is cleared.
4498 */
4499 hci_dev_clear_flag(hdev, HCI_LE_ADV);
4500
4501 conn = hci_lookup_le_connect(hdev);
4502 if (!conn) {
4503 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr, ev->role);
4504 if (!conn) {
4505 BT_ERR("No memory for new connection");
4506 goto unlock;
4507 }
4508
4509 conn->dst_type = ev->bdaddr_type;
4510
4511 /* If we didn't have a hci_conn object previously
4512 * but we're in master role this must be something
4513 * initiated using a white list. Since white list based
4514 * connections are not "first class citizens" we don't
4515 * have full tracking of them. Therefore, we go ahead
4516 * with a "best effort" approach of determining the
4517 * initiator address based on the HCI_PRIVACY flag.
4518 */
4519 if (conn->out) {
4520 conn->resp_addr_type = ev->bdaddr_type;
4521 bacpy(&conn->resp_addr, &ev->bdaddr);
4522 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
4523 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4524 bacpy(&conn->init_addr, &hdev->rpa);
4525 } else {
4526 hci_copy_identity_address(hdev,
4527 &conn->init_addr,
4528 &conn->init_addr_type);
4529 }
4530 }
4531 } else {
4532 cancel_delayed_work(&conn->le_conn_timeout);
4533 }
4534
4535 if (!conn->out) {
4536 /* Set the responder (our side) address type based on
4537 * the advertising address type.
4538 */
4539 conn->resp_addr_type = hdev->adv_addr_type;
4540 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4541 bacpy(&conn->resp_addr, &hdev->random_addr);
4542 else
4543 bacpy(&conn->resp_addr, &hdev->bdaddr);
4544
4545 conn->init_addr_type = ev->bdaddr_type;
4546 bacpy(&conn->init_addr, &ev->bdaddr);
4547
4548 /* For incoming connections, set the default minimum
4549 * and maximum connection interval. They will be used
4550 * to check if the parameters are in range and if not
4551 * trigger the connection update procedure.
4552 */
4553 conn->le_conn_min_interval = hdev->le_conn_min_interval;
4554 conn->le_conn_max_interval = hdev->le_conn_max_interval;
4555 }
4556
4557 /* Lookup the identity address from the stored connection
4558 * address and address type.
4559 *
4560 * When establishing connections to an identity address, the
4561 * connection procedure will store the resolvable random
4562 * address first. Now if it can be converted back into the
4563 * identity address, start using the identity address from
4564 * now on.
4565 */
4566 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
4567 if (irk) {
4568 bacpy(&conn->dst, &irk->bdaddr);
4569 conn->dst_type = irk->addr_type;
4570 }
4571
4572 if (ev->status) {
4573 hci_le_conn_failed(conn, ev->status);
4574 goto unlock;
4575 }
4576
4577 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
4578 addr_type = BDADDR_LE_PUBLIC;
4579 else
4580 addr_type = BDADDR_LE_RANDOM;
4581
4582 /* Drop the connection if the device is blocked */
4583 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
4584 hci_conn_drop(conn);
4585 goto unlock;
4586 }
4587
4588 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
4589 mgmt_device_connected(hdev, conn, 0, NULL, 0);
4590
4591 conn->sec_level = BT_SECURITY_LOW;
4592 conn->handle = __le16_to_cpu(ev->handle);
4593 conn->state = BT_CONFIG;
4594
4595 conn->le_conn_interval = le16_to_cpu(ev->interval);
4596 conn->le_conn_latency = le16_to_cpu(ev->latency);
4597 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4598
4599 hci_debugfs_create_conn(conn);
4600 hci_conn_add_sysfs(conn);
4601
4602 if (!ev->status) {
4603 /* The remote features procedure is defined for master
4604 * role only. So only in case of an initiated connection
4605 * request the remote features.
4606 *
4607 * If the local controller supports slave-initiated features
4608 * exchange, then requesting the remote features in slave
4609 * role is possible. Otherwise just transition into the
4610 * connected state without requesting the remote features.
4611 */
4612 if (conn->out ||
4613 (hdev->le_features[0] & HCI_LE_SLAVE_FEATURES)) {
4614 struct hci_cp_le_read_remote_features cp;
4615
4616 cp.handle = __cpu_to_le16(conn->handle);
4617
4618 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
4619 sizeof(cp), &cp);
4620
4621 hci_conn_hold(conn);
4622 } else {
4623 conn->state = BT_CONNECTED;
4624 hci_connect_cfm(conn, ev->status);
4625 }
4626 } else {
4627 hci_connect_cfm(conn, ev->status);
4628 }
4629
4630 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
4631 conn->dst_type);
4632 if (params) {
4633 list_del_init(&params->action);
4634 if (params->conn) {
4635 hci_conn_drop(params->conn);
4636 hci_conn_put(params->conn);
4637 params->conn = NULL;
4638 }
4639 }
4640
4641unlock:
4642 hci_update_background_scan(hdev);
4643 hci_dev_unlock(hdev);
4644}
4645
4646static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
4647 struct sk_buff *skb)
4648{
4649 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
4650 struct hci_conn *conn;
4651
4652 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4653
4654 if (ev->status)
4655 return;
4656
4657 hci_dev_lock(hdev);
4658
4659 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4660 if (conn) {
4661 conn->le_conn_interval = le16_to_cpu(ev->interval);
4662 conn->le_conn_latency = le16_to_cpu(ev->latency);
4663 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4664 }
4665
4666 hci_dev_unlock(hdev);
4667}
4668
4669/* This function requires the caller holds hdev->lock */
4670static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
4671 bdaddr_t *addr,
4672 u8 addr_type, u8 adv_type,
4673 bdaddr_t *direct_rpa)
4674{
4675 struct hci_conn *conn;
4676 struct hci_conn_params *params;
4677
4678 /* If the event is not connectable don't proceed further */
4679 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
4680 return NULL;
4681
4682 /* Ignore if the device is blocked */
4683 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
4684 return NULL;
4685
4686 /* Most controller will fail if we try to create new connections
4687 * while we have an existing one in slave role.
4688 */
4689 if (hdev->conn_hash.le_num_slave > 0)
4690 return NULL;
4691
4692 /* If we're not connectable only connect devices that we have in
4693 * our pend_le_conns list.
4694 */
4695 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
4696 addr_type);
4697 if (!params)
4698 return NULL;
4699
4700 if (!params->explicit_connect) {
4701 switch (params->auto_connect) {
4702 case HCI_AUTO_CONN_DIRECT:
4703 /* Only devices advertising with ADV_DIRECT_IND are
4704 * triggering a connection attempt. This is allowing
4705 * incoming connections from slave devices.
4706 */
4707 if (adv_type != LE_ADV_DIRECT_IND)
4708 return NULL;
4709 break;
4710 case HCI_AUTO_CONN_ALWAYS:
4711 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
4712 * are triggering a connection attempt. This means
4713 * that incoming connectioms from slave device are
4714 * accepted and also outgoing connections to slave
4715 * devices are established when found.
4716 */
4717 break;
4718 default:
4719 return NULL;
4720 }
4721 }
4722
4723 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
4724 HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER,
4725 direct_rpa);
4726 if (!IS_ERR(conn)) {
4727 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
4728 * by higher layer that tried to connect, if no then
4729 * store the pointer since we don't really have any
4730 * other owner of the object besides the params that
4731 * triggered it. This way we can abort the connection if
4732 * the parameters get removed and keep the reference
4733 * count consistent once the connection is established.
4734 */
4735
4736 if (!params->explicit_connect)
4737 params->conn = hci_conn_get(conn);
4738
4739 return conn;
4740 }
4741
4742 switch (PTR_ERR(conn)) {
4743 case -EBUSY:
4744 /* If hci_connect() returns -EBUSY it means there is already
4745 * an LE connection attempt going on. Since controllers don't
4746 * support more than one connection attempt at the time, we
4747 * don't consider this an error case.
4748 */
4749 break;
4750 default:
4751 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
4752 return NULL;
4753 }
4754
4755 return NULL;
4756}
4757
4758static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
4759 u8 bdaddr_type, bdaddr_t *direct_addr,
4760 u8 direct_addr_type, s8 rssi, u8 *data, u8 len)
4761{
4762 struct discovery_state *d = &hdev->discovery;
4763 struct smp_irk *irk;
4764 struct hci_conn *conn;
4765 bool match;
4766 u32 flags;
4767 u8 *ptr, real_len;
4768
4769 switch (type) {
4770 case LE_ADV_IND:
4771 case LE_ADV_DIRECT_IND:
4772 case LE_ADV_SCAN_IND:
4773 case LE_ADV_NONCONN_IND:
4774 case LE_ADV_SCAN_RSP:
4775 break;
4776 default:
4777 BT_ERR_RATELIMITED("Unknown advertising packet type: 0x%02x",
4778 type);
4779 return;
4780 }
4781
4782 /* Find the end of the data in case the report contains padded zero
4783 * bytes at the end causing an invalid length value.
4784 *
4785 * When data is NULL, len is 0 so there is no need for extra ptr
4786 * check as 'ptr < data + 0' is already false in such case.
4787 */
4788 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
4789 if (ptr + 1 + *ptr > data + len)
4790 break;
4791 }
4792
4793 real_len = ptr - data;
4794
4795 /* Adjust for actual length */
4796 if (len != real_len) {
4797 BT_ERR_RATELIMITED("%s advertising data length corrected",
4798 hdev->name);
4799 len = real_len;
4800 }
4801
4802 /* If the direct address is present, then this report is from
4803 * a LE Direct Advertising Report event. In that case it is
4804 * important to see if the address is matching the local
4805 * controller address.
4806 */
4807 if (direct_addr) {
4808 /* Only resolvable random addresses are valid for these
4809 * kind of reports and others can be ignored.
4810 */
4811 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
4812 return;
4813
4814 /* If the controller is not using resolvable random
4815 * addresses, then this report can be ignored.
4816 */
4817 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
4818 return;
4819
4820 /* If the local IRK of the controller does not match
4821 * with the resolvable random address provided, then
4822 * this report can be ignored.
4823 */
4824 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
4825 return;
4826 }
4827
4828 /* Check if we need to convert to identity address */
4829 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
4830 if (irk) {
4831 bdaddr = &irk->bdaddr;
4832 bdaddr_type = irk->addr_type;
4833 }
4834
4835 /* Check if we have been requested to connect to this device.
4836 *
4837 * direct_addr is set only for directed advertising reports (it is NULL
4838 * for advertising reports) and is already verified to be RPA above.
4839 */
4840 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type,
4841 direct_addr);
4842 if (conn && type == LE_ADV_IND) {
4843 /* Store report for later inclusion by
4844 * mgmt_device_connected
4845 */
4846 memcpy(conn->le_adv_data, data, len);
4847 conn->le_adv_data_len = len;
4848 }
4849
4850 /* Passive scanning shouldn't trigger any device found events,
4851 * except for devices marked as CONN_REPORT for which we do send
4852 * device found events.
4853 */
4854 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
4855 if (type == LE_ADV_DIRECT_IND)
4856 return;
4857
4858 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
4859 bdaddr, bdaddr_type))
4860 return;
4861
4862 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
4863 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4864 else
4865 flags = 0;
4866 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4867 rssi, flags, data, len, NULL, 0);
4868 return;
4869 }
4870
4871 /* When receiving non-connectable or scannable undirected
4872 * advertising reports, this means that the remote device is
4873 * not connectable and then clearly indicate this in the
4874 * device found event.
4875 *
4876 * When receiving a scan response, then there is no way to
4877 * know if the remote device is connectable or not. However
4878 * since scan responses are merged with a previously seen
4879 * advertising report, the flags field from that report
4880 * will be used.
4881 *
4882 * In the really unlikely case that a controller get confused
4883 * and just sends a scan response event, then it is marked as
4884 * not connectable as well.
4885 */
4886 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
4887 type == LE_ADV_SCAN_RSP)
4888 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4889 else
4890 flags = 0;
4891
4892 /* If there's nothing pending either store the data from this
4893 * event or send an immediate device found event if the data
4894 * should not be stored for later.
4895 */
4896 if (!has_pending_adv_report(hdev)) {
4897 /* If the report will trigger a SCAN_REQ store it for
4898 * later merging.
4899 */
4900 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4901 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
4902 rssi, flags, data, len);
4903 return;
4904 }
4905
4906 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4907 rssi, flags, data, len, NULL, 0);
4908 return;
4909 }
4910
4911 /* Check if the pending report is for the same device as the new one */
4912 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4913 bdaddr_type == d->last_adv_addr_type);
4914
4915 /* If the pending data doesn't match this report or this isn't a
4916 * scan response (e.g. we got a duplicate ADV_IND) then force
4917 * sending of the pending data.
4918 */
4919 if (type != LE_ADV_SCAN_RSP || !match) {
4920 /* Send out whatever is in the cache, but skip duplicates */
4921 if (!match)
4922 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
4923 d->last_adv_addr_type, NULL,
4924 d->last_adv_rssi, d->last_adv_flags,
4925 d->last_adv_data,
4926 d->last_adv_data_len, NULL, 0);
4927
4928 /* If the new report will trigger a SCAN_REQ store it for
4929 * later merging.
4930 */
4931 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4932 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
4933 rssi, flags, data, len);
4934 return;
4935 }
4936
4937 /* The advertising reports cannot be merged, so clear
4938 * the pending report and send out a device found event.
4939 */
4940 clear_pending_adv_report(hdev);
4941 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4942 rssi, flags, data, len, NULL, 0);
4943 return;
4944 }
4945
4946 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4947 * the new event is a SCAN_RSP. We can therefore proceed with
4948 * sending a merged device found event.
4949 */
4950 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
4951 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
4952 d->last_adv_data, d->last_adv_data_len, data, len);
4953 clear_pending_adv_report(hdev);
4954}
4955
4956static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
4957{
4958 u8 num_reports = skb->data[0];
4959 void *ptr = &skb->data[1];
4960
4961 hci_dev_lock(hdev);
4962
4963 while (num_reports--) {
4964 struct hci_ev_le_advertising_info *ev = ptr;
4965 s8 rssi;
4966
4967 rssi = ev->data[ev->length];
4968 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4969 ev->bdaddr_type, NULL, 0, rssi,
4970 ev->data, ev->length);
4971
4972 ptr += sizeof(*ev) + ev->length + 1;
4973 }
4974
4975 hci_dev_unlock(hdev);
4976}
4977
4978static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
4979 struct sk_buff *skb)
4980{
4981 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
4982 struct hci_conn *conn;
4983
4984 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4985
4986 hci_dev_lock(hdev);
4987
4988 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4989 if (conn) {
4990 if (!ev->status)
4991 memcpy(conn->features[0], ev->features, 8);
4992
4993 if (conn->state == BT_CONFIG) {
4994 __u8 status;
4995
4996 /* If the local controller supports slave-initiated
4997 * features exchange, but the remote controller does
4998 * not, then it is possible that the error code 0x1a
4999 * for unsupported remote feature gets returned.
5000 *
5001 * In this specific case, allow the connection to
5002 * transition into connected state and mark it as
5003 * successful.
5004 */
5005 if ((hdev->le_features[0] & HCI_LE_SLAVE_FEATURES) &&
5006 !conn->out && ev->status == 0x1a)
5007 status = 0x00;
5008 else
5009 status = ev->status;
5010
5011 conn->state = BT_CONNECTED;
5012 hci_connect_cfm(conn, status);
5013 hci_conn_drop(conn);
5014 }
5015 }
5016
5017 hci_dev_unlock(hdev);
5018}
5019
5020static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
5021{
5022 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
5023 struct hci_cp_le_ltk_reply cp;
5024 struct hci_cp_le_ltk_neg_reply neg;
5025 struct hci_conn *conn;
5026 struct smp_ltk *ltk;
5027
5028 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
5029
5030 hci_dev_lock(hdev);
5031
5032 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5033 if (conn == NULL)
5034 goto not_found;
5035
5036 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
5037 if (!ltk)
5038 goto not_found;
5039
5040 if (smp_ltk_is_sc(ltk)) {
5041 /* With SC both EDiv and Rand are set to zero */
5042 if (ev->ediv || ev->rand)
5043 goto not_found;
5044 } else {
5045 /* For non-SC keys check that EDiv and Rand match */
5046 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
5047 goto not_found;
5048 }
5049
5050 memcpy(cp.ltk, ltk->val, ltk->enc_size);
5051 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
5052 cp.handle = cpu_to_le16(conn->handle);
5053
5054 conn->pending_sec_level = smp_ltk_sec_level(ltk);
5055
5056 conn->enc_key_size = ltk->enc_size;
5057
5058 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
5059
5060 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5061 * temporary key used to encrypt a connection following
5062 * pairing. It is used during the Encrypted Session Setup to
5063 * distribute the keys. Later, security can be re-established
5064 * using a distributed LTK.
5065 */
5066 if (ltk->type == SMP_STK) {
5067 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
5068 list_del_rcu(&ltk->list);
5069 kfree_rcu(ltk, rcu);
5070 } else {
5071 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
5072 }
5073
5074 hci_dev_unlock(hdev);
5075
5076 return;
5077
5078not_found:
5079 neg.handle = ev->handle;
5080 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
5081 hci_dev_unlock(hdev);
5082}
5083
5084static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
5085 u8 reason)
5086{
5087 struct hci_cp_le_conn_param_req_neg_reply cp;
5088
5089 cp.handle = cpu_to_le16(handle);
5090 cp.reason = reason;
5091
5092 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
5093 &cp);
5094}
5095
5096static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
5097 struct sk_buff *skb)
5098{
5099 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
5100 struct hci_cp_le_conn_param_req_reply cp;
5101 struct hci_conn *hcon;
5102 u16 handle, min, max, latency, timeout;
5103
5104 handle = le16_to_cpu(ev->handle);
5105 min = le16_to_cpu(ev->interval_min);
5106 max = le16_to_cpu(ev->interval_max);
5107 latency = le16_to_cpu(ev->latency);
5108 timeout = le16_to_cpu(ev->timeout);
5109
5110 hcon = hci_conn_hash_lookup_handle(hdev, handle);
5111 if (!hcon || hcon->state != BT_CONNECTED)
5112 return send_conn_param_neg_reply(hdev, handle,
5113 HCI_ERROR_UNKNOWN_CONN_ID);
5114
5115 if (hci_check_conn_params(min, max, latency, timeout))
5116 return send_conn_param_neg_reply(hdev, handle,
5117 HCI_ERROR_INVALID_LL_PARAMS);
5118
5119 if (hcon->role == HCI_ROLE_MASTER) {
5120 struct hci_conn_params *params;
5121 u8 store_hint;
5122
5123 hci_dev_lock(hdev);
5124
5125 params = hci_conn_params_lookup(hdev, &hcon->dst,
5126 hcon->dst_type);
5127 if (params) {
5128 params->conn_min_interval = min;
5129 params->conn_max_interval = max;
5130 params->conn_latency = latency;
5131 params->supervision_timeout = timeout;
5132 store_hint = 0x01;
5133 } else{
5134 store_hint = 0x00;
5135 }
5136
5137 hci_dev_unlock(hdev);
5138
5139 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
5140 store_hint, min, max, latency, timeout);
5141 }
5142
5143 cp.handle = ev->handle;
5144 cp.interval_min = ev->interval_min;
5145 cp.interval_max = ev->interval_max;
5146 cp.latency = ev->latency;
5147 cp.timeout = ev->timeout;
5148 cp.min_ce_len = 0;
5149 cp.max_ce_len = 0;
5150
5151 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
5152}
5153
5154static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
5155 struct sk_buff *skb)
5156{
5157 u8 num_reports = skb->data[0];
5158 void *ptr = &skb->data[1];
5159
5160 hci_dev_lock(hdev);
5161
5162 while (num_reports--) {
5163 struct hci_ev_le_direct_adv_info *ev = ptr;
5164
5165 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5166 ev->bdaddr_type, &ev->direct_addr,
5167 ev->direct_addr_type, ev->rssi, NULL, 0);
5168
5169 ptr += sizeof(*ev);
5170 }
5171
5172 hci_dev_unlock(hdev);
5173}
5174
5175static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
5176{
5177 struct hci_ev_le_meta *le_ev = (void *) skb->data;
5178
5179 skb_pull(skb, sizeof(*le_ev));
5180
5181 switch (le_ev->subevent) {
5182 case HCI_EV_LE_CONN_COMPLETE:
5183 hci_le_conn_complete_evt(hdev, skb);
5184 break;
5185
5186 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
5187 hci_le_conn_update_complete_evt(hdev, skb);
5188 break;
5189
5190 case HCI_EV_LE_ADVERTISING_REPORT:
5191 hci_le_adv_report_evt(hdev, skb);
5192 break;
5193
5194 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
5195 hci_le_remote_feat_complete_evt(hdev, skb);
5196 break;
5197
5198 case HCI_EV_LE_LTK_REQ:
5199 hci_le_ltk_request_evt(hdev, skb);
5200 break;
5201
5202 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
5203 hci_le_remote_conn_param_req_evt(hdev, skb);
5204 break;
5205
5206 case HCI_EV_LE_DIRECT_ADV_REPORT:
5207 hci_le_direct_adv_report_evt(hdev, skb);
5208 break;
5209
5210 default:
5211 break;
5212 }
5213}
5214
5215static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
5216 u8 event, struct sk_buff *skb)
5217{
5218 struct hci_ev_cmd_complete *ev;
5219 struct hci_event_hdr *hdr;
5220
5221 if (!skb)
5222 return false;
5223
5224 if (skb->len < sizeof(*hdr)) {
5225 BT_ERR("Too short HCI event");
5226 return false;
5227 }
5228
5229 hdr = (void *) skb->data;
5230 skb_pull(skb, HCI_EVENT_HDR_SIZE);
5231
5232 if (event) {
5233 if (hdr->evt != event)
5234 return false;
5235 return true;
5236 }
5237
5238 /* Check if request ended in Command Status - no way to retreive
5239 * any extra parameters in this case.
5240 */
5241 if (hdr->evt == HCI_EV_CMD_STATUS)
5242 return false;
5243
5244 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
5245 BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
5246 return false;
5247 }
5248
5249 if (skb->len < sizeof(*ev)) {
5250 BT_ERR("Too short cmd_complete event");
5251 return false;
5252 }
5253
5254 ev = (void *) skb->data;
5255 skb_pull(skb, sizeof(*ev));
5256
5257 if (opcode != __le16_to_cpu(ev->opcode)) {
5258 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
5259 __le16_to_cpu(ev->opcode));
5260 return false;
5261 }
5262
5263 return true;
5264}
5265
5266void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
5267{
5268 struct hci_event_hdr *hdr = (void *) skb->data;
5269 hci_req_complete_t req_complete = NULL;
5270 hci_req_complete_skb_t req_complete_skb = NULL;
5271 struct sk_buff *orig_skb = NULL;
5272 u8 status = 0, event = hdr->evt, req_evt = 0;
5273 u16 opcode = HCI_OP_NOP;
5274
5275 if (!event) {
5276 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
5277 goto done;
5278 }
5279
5280 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
5281 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
5282 opcode = __le16_to_cpu(cmd_hdr->opcode);
5283 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
5284 &req_complete_skb);
5285 req_evt = event;
5286 }
5287
5288 /* If it looks like we might end up having to call
5289 * req_complete_skb, store a pristine copy of the skb since the
5290 * various handlers may modify the original one through
5291 * skb_pull() calls, etc.
5292 */
5293 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
5294 event == HCI_EV_CMD_COMPLETE)
5295 orig_skb = skb_clone(skb, GFP_KERNEL);
5296
5297 skb_pull(skb, HCI_EVENT_HDR_SIZE);
5298
5299 switch (event) {
5300 case HCI_EV_INQUIRY_COMPLETE:
5301 hci_inquiry_complete_evt(hdev, skb);
5302 break;
5303
5304 case HCI_EV_INQUIRY_RESULT:
5305 hci_inquiry_result_evt(hdev, skb);
5306 break;
5307
5308 case HCI_EV_CONN_COMPLETE:
5309 hci_conn_complete_evt(hdev, skb);
5310 break;
5311
5312 case HCI_EV_CONN_REQUEST:
5313 hci_conn_request_evt(hdev, skb);
5314 break;
5315
5316 case HCI_EV_DISCONN_COMPLETE:
5317 hci_disconn_complete_evt(hdev, skb);
5318 break;
5319
5320 case HCI_EV_AUTH_COMPLETE:
5321 hci_auth_complete_evt(hdev, skb);
5322 break;
5323
5324 case HCI_EV_REMOTE_NAME:
5325 hci_remote_name_evt(hdev, skb);
5326 break;
5327
5328 case HCI_EV_ENCRYPT_CHANGE:
5329 hci_encrypt_change_evt(hdev, skb);
5330 break;
5331
5332 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
5333 hci_change_link_key_complete_evt(hdev, skb);
5334 break;
5335
5336 case HCI_EV_REMOTE_FEATURES:
5337 hci_remote_features_evt(hdev, skb);
5338 break;
5339
5340 case HCI_EV_CMD_COMPLETE:
5341 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
5342 &req_complete, &req_complete_skb);
5343 break;
5344
5345 case HCI_EV_CMD_STATUS:
5346 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
5347 &req_complete_skb);
5348 break;
5349
5350 case HCI_EV_HARDWARE_ERROR:
5351 hci_hardware_error_evt(hdev, skb);
5352 break;
5353
5354 case HCI_EV_ROLE_CHANGE:
5355 hci_role_change_evt(hdev, skb);
5356 break;
5357
5358 case HCI_EV_NUM_COMP_PKTS:
5359 hci_num_comp_pkts_evt(hdev, skb);
5360 break;
5361
5362 case HCI_EV_MODE_CHANGE:
5363 hci_mode_change_evt(hdev, skb);
5364 break;
5365
5366 case HCI_EV_PIN_CODE_REQ:
5367 hci_pin_code_request_evt(hdev, skb);
5368 break;
5369
5370 case HCI_EV_LINK_KEY_REQ:
5371 hci_link_key_request_evt(hdev, skb);
5372 break;
5373
5374 case HCI_EV_LINK_KEY_NOTIFY:
5375 hci_link_key_notify_evt(hdev, skb);
5376 break;
5377
5378 case HCI_EV_CLOCK_OFFSET:
5379 hci_clock_offset_evt(hdev, skb);
5380 break;
5381
5382 case HCI_EV_PKT_TYPE_CHANGE:
5383 hci_pkt_type_change_evt(hdev, skb);
5384 break;
5385
5386 case HCI_EV_PSCAN_REP_MODE:
5387 hci_pscan_rep_mode_evt(hdev, skb);
5388 break;
5389
5390 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
5391 hci_inquiry_result_with_rssi_evt(hdev, skb);
5392 break;
5393
5394 case HCI_EV_REMOTE_EXT_FEATURES:
5395 hci_remote_ext_features_evt(hdev, skb);
5396 break;
5397
5398 case HCI_EV_SYNC_CONN_COMPLETE:
5399 hci_sync_conn_complete_evt(hdev, skb);
5400 break;
5401
5402 case HCI_EV_EXTENDED_INQUIRY_RESULT:
5403 hci_extended_inquiry_result_evt(hdev, skb);
5404 break;
5405
5406 case HCI_EV_KEY_REFRESH_COMPLETE:
5407 hci_key_refresh_complete_evt(hdev, skb);
5408 break;
5409
5410 case HCI_EV_IO_CAPA_REQUEST:
5411 hci_io_capa_request_evt(hdev, skb);
5412 break;
5413
5414 case HCI_EV_IO_CAPA_REPLY:
5415 hci_io_capa_reply_evt(hdev, skb);
5416 break;
5417
5418 case HCI_EV_USER_CONFIRM_REQUEST:
5419 hci_user_confirm_request_evt(hdev, skb);
5420 break;
5421
5422 case HCI_EV_USER_PASSKEY_REQUEST:
5423 hci_user_passkey_request_evt(hdev, skb);
5424 break;
5425
5426 case HCI_EV_USER_PASSKEY_NOTIFY:
5427 hci_user_passkey_notify_evt(hdev, skb);
5428 break;
5429
5430 case HCI_EV_KEYPRESS_NOTIFY:
5431 hci_keypress_notify_evt(hdev, skb);
5432 break;
5433
5434 case HCI_EV_SIMPLE_PAIR_COMPLETE:
5435 hci_simple_pair_complete_evt(hdev, skb);
5436 break;
5437
5438 case HCI_EV_REMOTE_HOST_FEATURES:
5439 hci_remote_host_features_evt(hdev, skb);
5440 break;
5441
5442 case HCI_EV_LE_META:
5443 hci_le_meta_evt(hdev, skb);
5444 break;
5445
5446 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
5447 hci_remote_oob_data_request_evt(hdev, skb);
5448 break;
5449
5450#if IS_ENABLED(CONFIG_BT_HS)
5451 case HCI_EV_CHANNEL_SELECTED:
5452 hci_chan_selected_evt(hdev, skb);
5453 break;
5454
5455 case HCI_EV_PHY_LINK_COMPLETE:
5456 hci_phy_link_complete_evt(hdev, skb);
5457 break;
5458
5459 case HCI_EV_LOGICAL_LINK_COMPLETE:
5460 hci_loglink_complete_evt(hdev, skb);
5461 break;
5462
5463 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
5464 hci_disconn_loglink_complete_evt(hdev, skb);
5465 break;
5466
5467 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
5468 hci_disconn_phylink_complete_evt(hdev, skb);
5469 break;
5470#endif
5471
5472 case HCI_EV_NUM_COMP_BLOCKS:
5473 hci_num_comp_blocks_evt(hdev, skb);
5474 break;
5475
5476 default:
5477 BT_DBG("%s event 0x%2.2x", hdev->name, event);
5478 break;
5479 }
5480
5481 if (req_complete) {
5482 req_complete(hdev, status, opcode);
5483 } else if (req_complete_skb) {
5484 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
5485 kfree_skb(orig_skb);
5486 orig_skb = NULL;
5487 }
5488 req_complete_skb(hdev, status, opcode, orig_skb);
5489 }
5490
5491done:
5492 kfree_skb(orig_skb);
5493 kfree_skb(skb);
5494 hdev->stat.evt_rx++;
5495}