1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
4 Copyright 2023-2024 NXP
5
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
24 */
25
26 /* Bluetooth HCI event handling. */
27
28 #include <linux/unaligned.h>
29 #include <linux/crypto.h>
30 #include <crypto/algapi.h>
31
32 #include <net/bluetooth/bluetooth.h>
33 #include <net/bluetooth/hci_core.h>
34 #include <net/bluetooth/mgmt.h>
35
36 #include "hci_debugfs.h"
37 #include "hci_codec.h"
38 #include "smp.h"
39 #include "msft.h"
40 #include "eir.h"
41
42 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
43 "\x00\x00\x00\x00\x00\x00\x00\x00"
44
45 /* Handle HCI Event packets */
46
hci_ev_skb_pull(struct hci_dev * hdev,struct sk_buff * skb,u8 ev,size_t len)47 static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
48 u8 ev, size_t len)
49 {
50 void *data;
51
52 data = skb_pull_data(skb, len);
53 if (!data)
54 bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev);
55
56 return data;
57 }
58
hci_cc_skb_pull(struct hci_dev * hdev,struct sk_buff * skb,u16 op,size_t len)59 static void *hci_cc_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
60 u16 op, size_t len)
61 {
62 void *data;
63
64 data = skb_pull_data(skb, len);
65 if (!data)
66 bt_dev_err(hdev, "Malformed Command Complete: 0x%4.4x", op);
67
68 return data;
69 }
70
hci_le_ev_skb_pull(struct hci_dev * hdev,struct sk_buff * skb,u8 ev,size_t len)71 static void *hci_le_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
72 u8 ev, size_t len)
73 {
74 void *data;
75
76 data = skb_pull_data(skb, len);
77 if (!data)
78 bt_dev_err(hdev, "Malformed LE Event: 0x%2.2x", ev);
79
80 return data;
81 }
82
hci_cc_inquiry_cancel(struct hci_dev * hdev,void * data,struct sk_buff * skb)83 static u8 hci_cc_inquiry_cancel(struct hci_dev *hdev, void *data,
84 struct sk_buff *skb)
85 {
86 struct hci_ev_status *rp = data;
87
88 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
89
90 /* It is possible that we receive Inquiry Complete event right
91 * before we receive Inquiry Cancel Command Complete event, in
92 * which case the latter event should have status of Command
93 * Disallowed. This should not be treated as error, since
94 * we actually achieve what Inquiry Cancel wants to achieve,
95 * which is to end the last Inquiry session.
96 */
97 if (rp->status == HCI_ERROR_COMMAND_DISALLOWED && !test_bit(HCI_INQUIRY, &hdev->flags)) {
98 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
99 rp->status = 0x00;
100 }
101
102 if (rp->status)
103 return rp->status;
104
105 clear_bit(HCI_INQUIRY, &hdev->flags);
106 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
107 wake_up_bit(&hdev->flags, HCI_INQUIRY);
108
109 hci_dev_lock(hdev);
110 /* Set discovery state to stopped if we're not doing LE active
111 * scanning.
112 */
113 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
114 hdev->le_scan_type != LE_SCAN_ACTIVE)
115 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
116 hci_dev_unlock(hdev);
117
118 return rp->status;
119 }
120
hci_cc_periodic_inq(struct hci_dev * hdev,void * data,struct sk_buff * skb)121 static u8 hci_cc_periodic_inq(struct hci_dev *hdev, void *data,
122 struct sk_buff *skb)
123 {
124 struct hci_ev_status *rp = data;
125
126 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
127
128 if (rp->status)
129 return rp->status;
130
131 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
132
133 return rp->status;
134 }
135
hci_cc_exit_periodic_inq(struct hci_dev * hdev,void * data,struct sk_buff * skb)136 static u8 hci_cc_exit_periodic_inq(struct hci_dev *hdev, void *data,
137 struct sk_buff *skb)
138 {
139 struct hci_ev_status *rp = data;
140
141 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
142
143 if (rp->status)
144 return rp->status;
145
146 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
147
148 return rp->status;
149 }
150
hci_cc_remote_name_req_cancel(struct hci_dev * hdev,void * data,struct sk_buff * skb)151 static u8 hci_cc_remote_name_req_cancel(struct hci_dev *hdev, void *data,
152 struct sk_buff *skb)
153 {
154 struct hci_rp_remote_name_req_cancel *rp = data;
155
156 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
157
158 return rp->status;
159 }
160
hci_cc_role_discovery(struct hci_dev * hdev,void * data,struct sk_buff * skb)161 static u8 hci_cc_role_discovery(struct hci_dev *hdev, void *data,
162 struct sk_buff *skb)
163 {
164 struct hci_rp_role_discovery *rp = data;
165 struct hci_conn *conn;
166
167 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
168
169 if (rp->status)
170 return rp->status;
171
172 hci_dev_lock(hdev);
173
174 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
175 if (conn)
176 conn->role = rp->role;
177
178 hci_dev_unlock(hdev);
179
180 return rp->status;
181 }
182
hci_cc_read_link_policy(struct hci_dev * hdev,void * data,struct sk_buff * skb)183 static u8 hci_cc_read_link_policy(struct hci_dev *hdev, void *data,
184 struct sk_buff *skb)
185 {
186 struct hci_rp_read_link_policy *rp = data;
187 struct hci_conn *conn;
188
189 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
190
191 if (rp->status)
192 return rp->status;
193
194 hci_dev_lock(hdev);
195
196 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
197 if (conn)
198 conn->link_policy = __le16_to_cpu(rp->policy);
199
200 hci_dev_unlock(hdev);
201
202 return rp->status;
203 }
204
hci_cc_write_link_policy(struct hci_dev * hdev,void * data,struct sk_buff * skb)205 static u8 hci_cc_write_link_policy(struct hci_dev *hdev, void *data,
206 struct sk_buff *skb)
207 {
208 struct hci_rp_write_link_policy *rp = data;
209 struct hci_conn *conn;
210 void *sent;
211
212 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
213
214 if (rp->status)
215 return rp->status;
216
217 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
218 if (!sent)
219 return rp->status;
220
221 hci_dev_lock(hdev);
222
223 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
224 if (conn)
225 conn->link_policy = get_unaligned_le16(sent + 2);
226
227 hci_dev_unlock(hdev);
228
229 return rp->status;
230 }
231
hci_cc_read_def_link_policy(struct hci_dev * hdev,void * data,struct sk_buff * skb)232 static u8 hci_cc_read_def_link_policy(struct hci_dev *hdev, void *data,
233 struct sk_buff *skb)
234 {
235 struct hci_rp_read_def_link_policy *rp = data;
236
237 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
238
239 if (rp->status)
240 return rp->status;
241
242 hdev->link_policy = __le16_to_cpu(rp->policy);
243
244 return rp->status;
245 }
246
hci_cc_write_def_link_policy(struct hci_dev * hdev,void * data,struct sk_buff * skb)247 static u8 hci_cc_write_def_link_policy(struct hci_dev *hdev, void *data,
248 struct sk_buff *skb)
249 {
250 struct hci_ev_status *rp = data;
251 void *sent;
252
253 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
254
255 if (rp->status)
256 return rp->status;
257
258 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
259 if (!sent)
260 return rp->status;
261
262 hdev->link_policy = get_unaligned_le16(sent);
263
264 return rp->status;
265 }
266
hci_cc_reset(struct hci_dev * hdev,void * data,struct sk_buff * skb)267 static u8 hci_cc_reset(struct hci_dev *hdev, void *data, struct sk_buff *skb)
268 {
269 struct hci_ev_status *rp = data;
270
271 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
272
273 clear_bit(HCI_RESET, &hdev->flags);
274
275 if (rp->status)
276 return rp->status;
277
278 /* Reset all non-persistent flags */
279 hci_dev_clear_volatile_flags(hdev);
280
281 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
282
283 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
284 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
285
286 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
287 hdev->adv_data_len = 0;
288
289 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
290 hdev->scan_rsp_data_len = 0;
291
292 hdev->le_scan_type = LE_SCAN_PASSIVE;
293
294 hdev->ssp_debug_mode = 0;
295
296 hci_bdaddr_list_clear(&hdev->le_accept_list);
297 hci_bdaddr_list_clear(&hdev->le_resolv_list);
298
299 return rp->status;
300 }
301
hci_cc_read_stored_link_key(struct hci_dev * hdev,void * data,struct sk_buff * skb)302 static u8 hci_cc_read_stored_link_key(struct hci_dev *hdev, void *data,
303 struct sk_buff *skb)
304 {
305 struct hci_rp_read_stored_link_key *rp = data;
306 struct hci_cp_read_stored_link_key *sent;
307
308 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
309
310 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
311 if (!sent)
312 return rp->status;
313
314 if (!rp->status && sent->read_all == 0x01) {
315 hdev->stored_max_keys = le16_to_cpu(rp->max_keys);
316 hdev->stored_num_keys = le16_to_cpu(rp->num_keys);
317 }
318
319 return rp->status;
320 }
321
hci_cc_delete_stored_link_key(struct hci_dev * hdev,void * data,struct sk_buff * skb)322 static u8 hci_cc_delete_stored_link_key(struct hci_dev *hdev, void *data,
323 struct sk_buff *skb)
324 {
325 struct hci_rp_delete_stored_link_key *rp = data;
326 u16 num_keys;
327
328 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
329
330 if (rp->status)
331 return rp->status;
332
333 num_keys = le16_to_cpu(rp->num_keys);
334
335 if (num_keys <= hdev->stored_num_keys)
336 hdev->stored_num_keys -= num_keys;
337 else
338 hdev->stored_num_keys = 0;
339
340 return rp->status;
341 }
342
hci_cc_write_local_name(struct hci_dev * hdev,void * data,struct sk_buff * skb)343 static u8 hci_cc_write_local_name(struct hci_dev *hdev, void *data,
344 struct sk_buff *skb)
345 {
346 struct hci_ev_status *rp = data;
347 void *sent;
348
349 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
350
351 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
352 if (!sent)
353 return rp->status;
354
355 hci_dev_lock(hdev);
356
357 if (hci_dev_test_flag(hdev, HCI_MGMT))
358 mgmt_set_local_name_complete(hdev, sent, rp->status);
359 else if (!rp->status)
360 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
361
362 hci_dev_unlock(hdev);
363
364 return rp->status;
365 }
366
hci_cc_read_local_name(struct hci_dev * hdev,void * data,struct sk_buff * skb)367 static u8 hci_cc_read_local_name(struct hci_dev *hdev, void *data,
368 struct sk_buff *skb)
369 {
370 struct hci_rp_read_local_name *rp = data;
371
372 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
373
374 if (rp->status)
375 return rp->status;
376
377 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
378 hci_dev_test_flag(hdev, HCI_CONFIG))
379 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
380
381 return rp->status;
382 }
383
hci_cc_write_auth_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)384 static u8 hci_cc_write_auth_enable(struct hci_dev *hdev, void *data,
385 struct sk_buff *skb)
386 {
387 struct hci_ev_status *rp = data;
388 void *sent;
389
390 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
391
392 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
393 if (!sent)
394 return rp->status;
395
396 hci_dev_lock(hdev);
397
398 if (!rp->status) {
399 __u8 param = *((__u8 *) sent);
400
401 if (param == AUTH_ENABLED)
402 set_bit(HCI_AUTH, &hdev->flags);
403 else
404 clear_bit(HCI_AUTH, &hdev->flags);
405 }
406
407 if (hci_dev_test_flag(hdev, HCI_MGMT))
408 mgmt_auth_enable_complete(hdev, rp->status);
409
410 hci_dev_unlock(hdev);
411
412 return rp->status;
413 }
414
hci_cc_write_encrypt_mode(struct hci_dev * hdev,void * data,struct sk_buff * skb)415 static u8 hci_cc_write_encrypt_mode(struct hci_dev *hdev, void *data,
416 struct sk_buff *skb)
417 {
418 struct hci_ev_status *rp = data;
419 __u8 param;
420 void *sent;
421
422 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
423
424 if (rp->status)
425 return rp->status;
426
427 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
428 if (!sent)
429 return rp->status;
430
431 param = *((__u8 *) sent);
432
433 if (param)
434 set_bit(HCI_ENCRYPT, &hdev->flags);
435 else
436 clear_bit(HCI_ENCRYPT, &hdev->flags);
437
438 return rp->status;
439 }
440
hci_cc_write_scan_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)441 static u8 hci_cc_write_scan_enable(struct hci_dev *hdev, void *data,
442 struct sk_buff *skb)
443 {
444 struct hci_ev_status *rp = data;
445 __u8 param;
446 void *sent;
447
448 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
449
450 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
451 if (!sent)
452 return rp->status;
453
454 param = *((__u8 *) sent);
455
456 hci_dev_lock(hdev);
457
458 if (rp->status) {
459 hdev->discov_timeout = 0;
460 goto done;
461 }
462
463 if (param & SCAN_INQUIRY)
464 set_bit(HCI_ISCAN, &hdev->flags);
465 else
466 clear_bit(HCI_ISCAN, &hdev->flags);
467
468 if (param & SCAN_PAGE)
469 set_bit(HCI_PSCAN, &hdev->flags);
470 else
471 clear_bit(HCI_PSCAN, &hdev->flags);
472
473 done:
474 hci_dev_unlock(hdev);
475
476 return rp->status;
477 }
478
hci_cc_set_event_filter(struct hci_dev * hdev,void * data,struct sk_buff * skb)479 static u8 hci_cc_set_event_filter(struct hci_dev *hdev, void *data,
480 struct sk_buff *skb)
481 {
482 struct hci_ev_status *rp = data;
483 struct hci_cp_set_event_filter *cp;
484 void *sent;
485
486 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
487
488 if (rp->status)
489 return rp->status;
490
491 sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
492 if (!sent)
493 return rp->status;
494
495 cp = (struct hci_cp_set_event_filter *)sent;
496
497 if (cp->flt_type == HCI_FLT_CLEAR_ALL)
498 hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
499 else
500 hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
501
502 return rp->status;
503 }
504
hci_cc_read_class_of_dev(struct hci_dev * hdev,void * data,struct sk_buff * skb)505 static u8 hci_cc_read_class_of_dev(struct hci_dev *hdev, void *data,
506 struct sk_buff *skb)
507 {
508 struct hci_rp_read_class_of_dev *rp = data;
509
510 if (WARN_ON(!hdev))
511 return HCI_ERROR_UNSPECIFIED;
512
513 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
514
515 if (rp->status)
516 return rp->status;
517
518 memcpy(hdev->dev_class, rp->dev_class, 3);
519
520 bt_dev_dbg(hdev, "class 0x%.2x%.2x%.2x", hdev->dev_class[2],
521 hdev->dev_class[1], hdev->dev_class[0]);
522
523 return rp->status;
524 }
525
hci_cc_write_class_of_dev(struct hci_dev * hdev,void * data,struct sk_buff * skb)526 static u8 hci_cc_write_class_of_dev(struct hci_dev *hdev, void *data,
527 struct sk_buff *skb)
528 {
529 struct hci_ev_status *rp = data;
530 void *sent;
531
532 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
533
534 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
535 if (!sent)
536 return rp->status;
537
538 hci_dev_lock(hdev);
539
540 if (!rp->status)
541 memcpy(hdev->dev_class, sent, 3);
542
543 if (hci_dev_test_flag(hdev, HCI_MGMT))
544 mgmt_set_class_of_dev_complete(hdev, sent, rp->status);
545
546 hci_dev_unlock(hdev);
547
548 return rp->status;
549 }
550
hci_cc_read_voice_setting(struct hci_dev * hdev,void * data,struct sk_buff * skb)551 static u8 hci_cc_read_voice_setting(struct hci_dev *hdev, void *data,
552 struct sk_buff *skb)
553 {
554 struct hci_rp_read_voice_setting *rp = data;
555 __u16 setting;
556
557 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
558
559 if (rp->status)
560 return rp->status;
561
562 setting = __le16_to_cpu(rp->voice_setting);
563
564 if (hdev->voice_setting == setting)
565 return rp->status;
566
567 hdev->voice_setting = setting;
568
569 bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
570
571 if (hdev->notify)
572 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
573
574 return rp->status;
575 }
576
hci_cc_write_voice_setting(struct hci_dev * hdev,void * data,struct sk_buff * skb)577 static u8 hci_cc_write_voice_setting(struct hci_dev *hdev, void *data,
578 struct sk_buff *skb)
579 {
580 struct hci_ev_status *rp = data;
581 __u16 setting;
582 void *sent;
583
584 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
585
586 if (rp->status)
587 return rp->status;
588
589 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
590 if (!sent)
591 return rp->status;
592
593 setting = get_unaligned_le16(sent);
594
595 if (hdev->voice_setting == setting)
596 return rp->status;
597
598 hdev->voice_setting = setting;
599
600 bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
601
602 if (hdev->notify)
603 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
604
605 return rp->status;
606 }
607
hci_cc_read_num_supported_iac(struct hci_dev * hdev,void * data,struct sk_buff * skb)608 static u8 hci_cc_read_num_supported_iac(struct hci_dev *hdev, void *data,
609 struct sk_buff *skb)
610 {
611 struct hci_rp_read_num_supported_iac *rp = data;
612
613 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
614
615 if (rp->status)
616 return rp->status;
617
618 hdev->num_iac = rp->num_iac;
619
620 bt_dev_dbg(hdev, "num iac %d", hdev->num_iac);
621
622 return rp->status;
623 }
624
hci_cc_write_ssp_mode(struct hci_dev * hdev,void * data,struct sk_buff * skb)625 static u8 hci_cc_write_ssp_mode(struct hci_dev *hdev, void *data,
626 struct sk_buff *skb)
627 {
628 struct hci_ev_status *rp = data;
629 struct hci_cp_write_ssp_mode *sent;
630
631 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
632
633 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
634 if (!sent)
635 return rp->status;
636
637 hci_dev_lock(hdev);
638
639 if (!rp->status) {
640 if (sent->mode)
641 hdev->features[1][0] |= LMP_HOST_SSP;
642 else
643 hdev->features[1][0] &= ~LMP_HOST_SSP;
644 }
645
646 if (!rp->status) {
647 if (sent->mode)
648 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
649 else
650 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
651 }
652
653 hci_dev_unlock(hdev);
654
655 return rp->status;
656 }
657
hci_cc_write_sc_support(struct hci_dev * hdev,void * data,struct sk_buff * skb)658 static u8 hci_cc_write_sc_support(struct hci_dev *hdev, void *data,
659 struct sk_buff *skb)
660 {
661 struct hci_ev_status *rp = data;
662 struct hci_cp_write_sc_support *sent;
663
664 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
665
666 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
667 if (!sent)
668 return rp->status;
669
670 hci_dev_lock(hdev);
671
672 if (!rp->status) {
673 if (sent->support)
674 hdev->features[1][0] |= LMP_HOST_SC;
675 else
676 hdev->features[1][0] &= ~LMP_HOST_SC;
677 }
678
679 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !rp->status) {
680 if (sent->support)
681 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
682 else
683 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
684 }
685
686 hci_dev_unlock(hdev);
687
688 return rp->status;
689 }
690
hci_cc_read_local_version(struct hci_dev * hdev,void * data,struct sk_buff * skb)691 static u8 hci_cc_read_local_version(struct hci_dev *hdev, void *data,
692 struct sk_buff *skb)
693 {
694 struct hci_rp_read_local_version *rp = data;
695
696 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
697
698 if (rp->status)
699 return rp->status;
700
701 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
702 hci_dev_test_flag(hdev, HCI_CONFIG)) {
703 hdev->hci_ver = rp->hci_ver;
704 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
705 hdev->lmp_ver = rp->lmp_ver;
706 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
707 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
708 }
709
710 return rp->status;
711 }
712
hci_cc_read_enc_key_size(struct hci_dev * hdev,void * data,struct sk_buff * skb)713 static u8 hci_cc_read_enc_key_size(struct hci_dev *hdev, void *data,
714 struct sk_buff *skb)
715 {
716 struct hci_rp_read_enc_key_size *rp = data;
717 struct hci_conn *conn;
718 u16 handle;
719 u8 status = rp->status;
720
721 bt_dev_dbg(hdev, "status 0x%2.2x", status);
722
723 handle = le16_to_cpu(rp->handle);
724
725 hci_dev_lock(hdev);
726
727 conn = hci_conn_hash_lookup_handle(hdev, handle);
728 if (!conn) {
729 status = 0xFF;
730 goto done;
731 }
732
733 /* While unexpected, the read_enc_key_size command may fail. The most
734 * secure approach is to then assume the key size is 0 to force a
735 * disconnection.
736 */
737 if (status) {
738 bt_dev_err(hdev, "failed to read key size for handle %u",
739 handle);
740 conn->enc_key_size = 0;
741 } else {
742 conn->enc_key_size = rp->key_size;
743 status = 0;
744
745 if (conn->enc_key_size < hdev->min_enc_key_size) {
746 /* As slave role, the conn->state has been set to
747 * BT_CONNECTED and l2cap conn req might not be received
748 * yet, at this moment the l2cap layer almost does
749 * nothing with the non-zero status.
750 * So we also clear encrypt related bits, and then the
751 * handler of l2cap conn req will get the right secure
752 * state at a later time.
753 */
754 status = HCI_ERROR_AUTH_FAILURE;
755 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
756 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
757 }
758 }
759
760 hci_encrypt_cfm(conn, status);
761
762 done:
763 hci_dev_unlock(hdev);
764
765 return status;
766 }
767
hci_cc_read_local_commands(struct hci_dev * hdev,void * data,struct sk_buff * skb)768 static u8 hci_cc_read_local_commands(struct hci_dev *hdev, void *data,
769 struct sk_buff *skb)
770 {
771 struct hci_rp_read_local_commands *rp = data;
772
773 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
774
775 if (rp->status)
776 return rp->status;
777
778 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
779 hci_dev_test_flag(hdev, HCI_CONFIG))
780 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
781
782 return rp->status;
783 }
784
hci_cc_read_auth_payload_timeout(struct hci_dev * hdev,void * data,struct sk_buff * skb)785 static u8 hci_cc_read_auth_payload_timeout(struct hci_dev *hdev, void *data,
786 struct sk_buff *skb)
787 {
788 struct hci_rp_read_auth_payload_to *rp = data;
789 struct hci_conn *conn;
790
791 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
792
793 if (rp->status)
794 return rp->status;
795
796 hci_dev_lock(hdev);
797
798 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
799 if (conn)
800 conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
801
802 hci_dev_unlock(hdev);
803
804 return rp->status;
805 }
806
hci_cc_write_auth_payload_timeout(struct hci_dev * hdev,void * data,struct sk_buff * skb)807 static u8 hci_cc_write_auth_payload_timeout(struct hci_dev *hdev, void *data,
808 struct sk_buff *skb)
809 {
810 struct hci_rp_write_auth_payload_to *rp = data;
811 struct hci_conn *conn;
812 void *sent;
813
814 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
815
816 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
817 if (!sent)
818 return rp->status;
819
820 hci_dev_lock(hdev);
821
822 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
823 if (!conn) {
824 rp->status = 0xff;
825 goto unlock;
826 }
827
828 if (!rp->status)
829 conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
830
831 unlock:
832 hci_dev_unlock(hdev);
833
834 return rp->status;
835 }
836
hci_cc_read_local_features(struct hci_dev * hdev,void * data,struct sk_buff * skb)837 static u8 hci_cc_read_local_features(struct hci_dev *hdev, void *data,
838 struct sk_buff *skb)
839 {
840 struct hci_rp_read_local_features *rp = data;
841
842 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
843
844 if (rp->status)
845 return rp->status;
846
847 memcpy(hdev->features, rp->features, 8);
848
849 /* Adjust default settings according to features
850 * supported by device. */
851
852 if (hdev->features[0][0] & LMP_3SLOT)
853 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
854
855 if (hdev->features[0][0] & LMP_5SLOT)
856 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
857
858 if (hdev->features[0][1] & LMP_HV2) {
859 hdev->pkt_type |= (HCI_HV2);
860 hdev->esco_type |= (ESCO_HV2);
861 }
862
863 if (hdev->features[0][1] & LMP_HV3) {
864 hdev->pkt_type |= (HCI_HV3);
865 hdev->esco_type |= (ESCO_HV3);
866 }
867
868 if (lmp_esco_capable(hdev))
869 hdev->esco_type |= (ESCO_EV3);
870
871 if (hdev->features[0][4] & LMP_EV4)
872 hdev->esco_type |= (ESCO_EV4);
873
874 if (hdev->features[0][4] & LMP_EV5)
875 hdev->esco_type |= (ESCO_EV5);
876
877 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
878 hdev->esco_type |= (ESCO_2EV3);
879
880 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
881 hdev->esco_type |= (ESCO_3EV3);
882
883 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
884 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
885
886 return rp->status;
887 }
888
hci_cc_read_local_ext_features(struct hci_dev * hdev,void * data,struct sk_buff * skb)889 static u8 hci_cc_read_local_ext_features(struct hci_dev *hdev, void *data,
890 struct sk_buff *skb)
891 {
892 struct hci_rp_read_local_ext_features *rp = data;
893
894 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
895
896 if (rp->status)
897 return rp->status;
898
899 if (hdev->max_page < rp->max_page) {
900 if (test_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
901 &hdev->quirks))
902 bt_dev_warn(hdev, "broken local ext features page 2");
903 else
904 hdev->max_page = rp->max_page;
905 }
906
907 if (rp->page < HCI_MAX_PAGES)
908 memcpy(hdev->features[rp->page], rp->features, 8);
909
910 return rp->status;
911 }
912
hci_cc_read_buffer_size(struct hci_dev * hdev,void * data,struct sk_buff * skb)913 static u8 hci_cc_read_buffer_size(struct hci_dev *hdev, void *data,
914 struct sk_buff *skb)
915 {
916 struct hci_rp_read_buffer_size *rp = data;
917
918 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
919
920 if (rp->status)
921 return rp->status;
922
923 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
924 hdev->sco_mtu = rp->sco_mtu;
925 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
926 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
927
928 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
929 hdev->sco_mtu = 64;
930 hdev->sco_pkts = 8;
931 }
932
933 if (!read_voice_setting_capable(hdev))
934 hdev->sco_pkts = 0;
935
936 hdev->acl_cnt = hdev->acl_pkts;
937 hdev->sco_cnt = hdev->sco_pkts;
938
939 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
940 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
941
942 if (!hdev->acl_mtu || !hdev->acl_pkts)
943 return HCI_ERROR_INVALID_PARAMETERS;
944
945 return rp->status;
946 }
947
hci_cc_read_bd_addr(struct hci_dev * hdev,void * data,struct sk_buff * skb)948 static u8 hci_cc_read_bd_addr(struct hci_dev *hdev, void *data,
949 struct sk_buff *skb)
950 {
951 struct hci_rp_read_bd_addr *rp = data;
952
953 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
954
955 if (rp->status)
956 return rp->status;
957
958 if (test_bit(HCI_INIT, &hdev->flags))
959 bacpy(&hdev->bdaddr, &rp->bdaddr);
960
961 if (hci_dev_test_flag(hdev, HCI_SETUP))
962 bacpy(&hdev->setup_addr, &rp->bdaddr);
963
964 return rp->status;
965 }
966
hci_cc_read_local_pairing_opts(struct hci_dev * hdev,void * data,struct sk_buff * skb)967 static u8 hci_cc_read_local_pairing_opts(struct hci_dev *hdev, void *data,
968 struct sk_buff *skb)
969 {
970 struct hci_rp_read_local_pairing_opts *rp = data;
971
972 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
973
974 if (rp->status)
975 return rp->status;
976
977 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
978 hci_dev_test_flag(hdev, HCI_CONFIG)) {
979 hdev->pairing_opts = rp->pairing_opts;
980 hdev->max_enc_key_size = rp->max_key_size;
981 }
982
983 return rp->status;
984 }
985
hci_cc_read_page_scan_activity(struct hci_dev * hdev,void * data,struct sk_buff * skb)986 static u8 hci_cc_read_page_scan_activity(struct hci_dev *hdev, void *data,
987 struct sk_buff *skb)
988 {
989 struct hci_rp_read_page_scan_activity *rp = data;
990
991 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
992
993 if (rp->status)
994 return rp->status;
995
996 if (test_bit(HCI_INIT, &hdev->flags)) {
997 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
998 hdev->page_scan_window = __le16_to_cpu(rp->window);
999 }
1000
1001 return rp->status;
1002 }
1003
hci_cc_write_page_scan_activity(struct hci_dev * hdev,void * data,struct sk_buff * skb)1004 static u8 hci_cc_write_page_scan_activity(struct hci_dev *hdev, void *data,
1005 struct sk_buff *skb)
1006 {
1007 struct hci_ev_status *rp = data;
1008 struct hci_cp_write_page_scan_activity *sent;
1009
1010 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1011
1012 if (rp->status)
1013 return rp->status;
1014
1015 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
1016 if (!sent)
1017 return rp->status;
1018
1019 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
1020 hdev->page_scan_window = __le16_to_cpu(sent->window);
1021
1022 return rp->status;
1023 }
1024
hci_cc_read_page_scan_type(struct hci_dev * hdev,void * data,struct sk_buff * skb)1025 static u8 hci_cc_read_page_scan_type(struct hci_dev *hdev, void *data,
1026 struct sk_buff *skb)
1027 {
1028 struct hci_rp_read_page_scan_type *rp = data;
1029
1030 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1031
1032 if (rp->status)
1033 return rp->status;
1034
1035 if (test_bit(HCI_INIT, &hdev->flags))
1036 hdev->page_scan_type = rp->type;
1037
1038 return rp->status;
1039 }
1040
hci_cc_write_page_scan_type(struct hci_dev * hdev,void * data,struct sk_buff * skb)1041 static u8 hci_cc_write_page_scan_type(struct hci_dev *hdev, void *data,
1042 struct sk_buff *skb)
1043 {
1044 struct hci_ev_status *rp = data;
1045 u8 *type;
1046
1047 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1048
1049 if (rp->status)
1050 return rp->status;
1051
1052 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
1053 if (type)
1054 hdev->page_scan_type = *type;
1055
1056 return rp->status;
1057 }
1058
hci_cc_read_clock(struct hci_dev * hdev,void * data,struct sk_buff * skb)1059 static u8 hci_cc_read_clock(struct hci_dev *hdev, void *data,
1060 struct sk_buff *skb)
1061 {
1062 struct hci_rp_read_clock *rp = data;
1063 struct hci_cp_read_clock *cp;
1064 struct hci_conn *conn;
1065
1066 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1067
1068 if (rp->status)
1069 return rp->status;
1070
1071 hci_dev_lock(hdev);
1072
1073 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
1074 if (!cp)
1075 goto unlock;
1076
1077 if (cp->which == 0x00) {
1078 hdev->clock = le32_to_cpu(rp->clock);
1079 goto unlock;
1080 }
1081
1082 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1083 if (conn) {
1084 conn->clock = le32_to_cpu(rp->clock);
1085 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
1086 }
1087
1088 unlock:
1089 hci_dev_unlock(hdev);
1090 return rp->status;
1091 }
1092
hci_cc_read_inq_rsp_tx_power(struct hci_dev * hdev,void * data,struct sk_buff * skb)1093 static u8 hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev, void *data,
1094 struct sk_buff *skb)
1095 {
1096 struct hci_rp_read_inq_rsp_tx_power *rp = data;
1097
1098 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1099
1100 if (rp->status)
1101 return rp->status;
1102
1103 hdev->inq_tx_power = rp->tx_power;
1104
1105 return rp->status;
1106 }
1107
hci_cc_read_def_err_data_reporting(struct hci_dev * hdev,void * data,struct sk_buff * skb)1108 static u8 hci_cc_read_def_err_data_reporting(struct hci_dev *hdev, void *data,
1109 struct sk_buff *skb)
1110 {
1111 struct hci_rp_read_def_err_data_reporting *rp = data;
1112
1113 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1114
1115 if (rp->status)
1116 return rp->status;
1117
1118 hdev->err_data_reporting = rp->err_data_reporting;
1119
1120 return rp->status;
1121 }
1122
hci_cc_write_def_err_data_reporting(struct hci_dev * hdev,void * data,struct sk_buff * skb)1123 static u8 hci_cc_write_def_err_data_reporting(struct hci_dev *hdev, void *data,
1124 struct sk_buff *skb)
1125 {
1126 struct hci_ev_status *rp = data;
1127 struct hci_cp_write_def_err_data_reporting *cp;
1128
1129 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1130
1131 if (rp->status)
1132 return rp->status;
1133
1134 cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
1135 if (!cp)
1136 return rp->status;
1137
1138 hdev->err_data_reporting = cp->err_data_reporting;
1139
1140 return rp->status;
1141 }
1142
hci_cc_pin_code_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1143 static u8 hci_cc_pin_code_reply(struct hci_dev *hdev, void *data,
1144 struct sk_buff *skb)
1145 {
1146 struct hci_rp_pin_code_reply *rp = data;
1147 struct hci_cp_pin_code_reply *cp;
1148 struct hci_conn *conn;
1149
1150 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1151
1152 hci_dev_lock(hdev);
1153
1154 if (hci_dev_test_flag(hdev, HCI_MGMT))
1155 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
1156
1157 if (rp->status)
1158 goto unlock;
1159
1160 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1161 if (!cp)
1162 goto unlock;
1163
1164 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1165 if (conn)
1166 conn->pin_length = cp->pin_len;
1167
1168 unlock:
1169 hci_dev_unlock(hdev);
1170 return rp->status;
1171 }
1172
hci_cc_pin_code_neg_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1173 static u8 hci_cc_pin_code_neg_reply(struct hci_dev *hdev, void *data,
1174 struct sk_buff *skb)
1175 {
1176 struct hci_rp_pin_code_neg_reply *rp = data;
1177
1178 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1179
1180 hci_dev_lock(hdev);
1181
1182 if (hci_dev_test_flag(hdev, HCI_MGMT))
1183 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
1184 rp->status);
1185
1186 hci_dev_unlock(hdev);
1187
1188 return rp->status;
1189 }
1190
hci_cc_le_read_buffer_size(struct hci_dev * hdev,void * data,struct sk_buff * skb)1191 static u8 hci_cc_le_read_buffer_size(struct hci_dev *hdev, void *data,
1192 struct sk_buff *skb)
1193 {
1194 struct hci_rp_le_read_buffer_size *rp = data;
1195
1196 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1197
1198 if (rp->status)
1199 return rp->status;
1200
1201 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1202 hdev->le_pkts = rp->le_max_pkt;
1203
1204 hdev->le_cnt = hdev->le_pkts;
1205
1206 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
1207
1208 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
1209 return HCI_ERROR_INVALID_PARAMETERS;
1210
1211 return rp->status;
1212 }
1213
hci_cc_le_read_local_features(struct hci_dev * hdev,void * data,struct sk_buff * skb)1214 static u8 hci_cc_le_read_local_features(struct hci_dev *hdev, void *data,
1215 struct sk_buff *skb)
1216 {
1217 struct hci_rp_le_read_local_features *rp = data;
1218
1219 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1220
1221 if (rp->status)
1222 return rp->status;
1223
1224 memcpy(hdev->le_features, rp->features, 8);
1225
1226 return rp->status;
1227 }
1228
hci_cc_le_read_adv_tx_power(struct hci_dev * hdev,void * data,struct sk_buff * skb)1229 static u8 hci_cc_le_read_adv_tx_power(struct hci_dev *hdev, void *data,
1230 struct sk_buff *skb)
1231 {
1232 struct hci_rp_le_read_adv_tx_power *rp = data;
1233
1234 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1235
1236 if (rp->status)
1237 return rp->status;
1238
1239 hdev->adv_tx_power = rp->tx_power;
1240
1241 return rp->status;
1242 }
1243
hci_cc_user_confirm_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1244 static u8 hci_cc_user_confirm_reply(struct hci_dev *hdev, void *data,
1245 struct sk_buff *skb)
1246 {
1247 struct hci_rp_user_confirm_reply *rp = data;
1248
1249 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1250
1251 hci_dev_lock(hdev);
1252
1253 if (hci_dev_test_flag(hdev, HCI_MGMT))
1254 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1255 rp->status);
1256
1257 hci_dev_unlock(hdev);
1258
1259 return rp->status;
1260 }
1261
hci_cc_user_confirm_neg_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1262 static u8 hci_cc_user_confirm_neg_reply(struct hci_dev *hdev, void *data,
1263 struct sk_buff *skb)
1264 {
1265 struct hci_rp_user_confirm_reply *rp = data;
1266
1267 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1268
1269 hci_dev_lock(hdev);
1270
1271 if (hci_dev_test_flag(hdev, HCI_MGMT))
1272 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
1273 ACL_LINK, 0, rp->status);
1274
1275 hci_dev_unlock(hdev);
1276
1277 return rp->status;
1278 }
1279
hci_cc_user_passkey_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1280 static u8 hci_cc_user_passkey_reply(struct hci_dev *hdev, void *data,
1281 struct sk_buff *skb)
1282 {
1283 struct hci_rp_user_confirm_reply *rp = data;
1284
1285 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1286
1287 hci_dev_lock(hdev);
1288
1289 if (hci_dev_test_flag(hdev, HCI_MGMT))
1290 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
1291 0, rp->status);
1292
1293 hci_dev_unlock(hdev);
1294
1295 return rp->status;
1296 }
1297
hci_cc_user_passkey_neg_reply(struct hci_dev * hdev,void * data,struct sk_buff * skb)1298 static u8 hci_cc_user_passkey_neg_reply(struct hci_dev *hdev, void *data,
1299 struct sk_buff *skb)
1300 {
1301 struct hci_rp_user_confirm_reply *rp = data;
1302
1303 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1304
1305 hci_dev_lock(hdev);
1306
1307 if (hci_dev_test_flag(hdev, HCI_MGMT))
1308 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
1309 ACL_LINK, 0, rp->status);
1310
1311 hci_dev_unlock(hdev);
1312
1313 return rp->status;
1314 }
1315
hci_cc_read_local_oob_data(struct hci_dev * hdev,void * data,struct sk_buff * skb)1316 static u8 hci_cc_read_local_oob_data(struct hci_dev *hdev, void *data,
1317 struct sk_buff *skb)
1318 {
1319 struct hci_rp_read_local_oob_data *rp = data;
1320
1321 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1322
1323 return rp->status;
1324 }
1325
hci_cc_read_local_oob_ext_data(struct hci_dev * hdev,void * data,struct sk_buff * skb)1326 static u8 hci_cc_read_local_oob_ext_data(struct hci_dev *hdev, void *data,
1327 struct sk_buff *skb)
1328 {
1329 struct hci_rp_read_local_oob_ext_data *rp = data;
1330
1331 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1332
1333 return rp->status;
1334 }
1335
hci_cc_le_set_random_addr(struct hci_dev * hdev,void * data,struct sk_buff * skb)1336 static u8 hci_cc_le_set_random_addr(struct hci_dev *hdev, void *data,
1337 struct sk_buff *skb)
1338 {
1339 struct hci_ev_status *rp = data;
1340 bdaddr_t *sent;
1341
1342 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1343
1344 if (rp->status)
1345 return rp->status;
1346
1347 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1348 if (!sent)
1349 return rp->status;
1350
1351 hci_dev_lock(hdev);
1352
1353 bacpy(&hdev->random_addr, sent);
1354
1355 if (!bacmp(&hdev->rpa, sent)) {
1356 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
1357 queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
1358 secs_to_jiffies(hdev->rpa_timeout));
1359 }
1360
1361 hci_dev_unlock(hdev);
1362
1363 return rp->status;
1364 }
1365
hci_cc_le_set_default_phy(struct hci_dev * hdev,void * data,struct sk_buff * skb)1366 static u8 hci_cc_le_set_default_phy(struct hci_dev *hdev, void *data,
1367 struct sk_buff *skb)
1368 {
1369 struct hci_ev_status *rp = data;
1370 struct hci_cp_le_set_default_phy *cp;
1371
1372 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1373
1374 if (rp->status)
1375 return rp->status;
1376
1377 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1378 if (!cp)
1379 return rp->status;
1380
1381 hci_dev_lock(hdev);
1382
1383 hdev->le_tx_def_phys = cp->tx_phys;
1384 hdev->le_rx_def_phys = cp->rx_phys;
1385
1386 hci_dev_unlock(hdev);
1387
1388 return rp->status;
1389 }
1390
hci_cc_le_set_adv_set_random_addr(struct hci_dev * hdev,void * data,struct sk_buff * skb)1391 static u8 hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev, void *data,
1392 struct sk_buff *skb)
1393 {
1394 struct hci_ev_status *rp = data;
1395 struct hci_cp_le_set_adv_set_rand_addr *cp;
1396 struct adv_info *adv;
1397
1398 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1399
1400 if (rp->status)
1401 return rp->status;
1402
1403 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
1404 /* Update only in case the adv instance since handle 0x00 shall be using
1405 * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
1406 * non-extended adverting.
1407 */
1408 if (!cp || !cp->handle)
1409 return rp->status;
1410
1411 hci_dev_lock(hdev);
1412
1413 adv = hci_find_adv_instance(hdev, cp->handle);
1414 if (adv) {
1415 bacpy(&adv->random_addr, &cp->bdaddr);
1416 if (!bacmp(&hdev->rpa, &cp->bdaddr)) {
1417 adv->rpa_expired = false;
1418 queue_delayed_work(hdev->workqueue,
1419 &adv->rpa_expired_cb,
1420 secs_to_jiffies(hdev->rpa_timeout));
1421 }
1422 }
1423
1424 hci_dev_unlock(hdev);
1425
1426 return rp->status;
1427 }
1428
hci_cc_le_remove_adv_set(struct hci_dev * hdev,void * data,struct sk_buff * skb)1429 static u8 hci_cc_le_remove_adv_set(struct hci_dev *hdev, void *data,
1430 struct sk_buff *skb)
1431 {
1432 struct hci_ev_status *rp = data;
1433 u8 *instance;
1434 int err;
1435
1436 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1437
1438 if (rp->status)
1439 return rp->status;
1440
1441 instance = hci_sent_cmd_data(hdev, HCI_OP_LE_REMOVE_ADV_SET);
1442 if (!instance)
1443 return rp->status;
1444
1445 hci_dev_lock(hdev);
1446
1447 err = hci_remove_adv_instance(hdev, *instance);
1448 if (!err)
1449 mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd), hdev,
1450 *instance);
1451
1452 hci_dev_unlock(hdev);
1453
1454 return rp->status;
1455 }
1456
hci_cc_le_clear_adv_sets(struct hci_dev * hdev,void * data,struct sk_buff * skb)1457 static u8 hci_cc_le_clear_adv_sets(struct hci_dev *hdev, void *data,
1458 struct sk_buff *skb)
1459 {
1460 struct hci_ev_status *rp = data;
1461 struct adv_info *adv, *n;
1462 int err;
1463
1464 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1465
1466 if (rp->status)
1467 return rp->status;
1468
1469 if (!hci_sent_cmd_data(hdev, HCI_OP_LE_CLEAR_ADV_SETS))
1470 return rp->status;
1471
1472 hci_dev_lock(hdev);
1473
1474 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1475 u8 instance = adv->instance;
1476
1477 err = hci_remove_adv_instance(hdev, instance);
1478 if (!err)
1479 mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd),
1480 hdev, instance);
1481 }
1482
1483 hci_dev_unlock(hdev);
1484
1485 return rp->status;
1486 }
1487
hci_cc_le_read_transmit_power(struct hci_dev * hdev,void * data,struct sk_buff * skb)1488 static u8 hci_cc_le_read_transmit_power(struct hci_dev *hdev, void *data,
1489 struct sk_buff *skb)
1490 {
1491 struct hci_rp_le_read_transmit_power *rp = data;
1492
1493 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1494
1495 if (rp->status)
1496 return rp->status;
1497
1498 hdev->min_le_tx_power = rp->min_le_tx_power;
1499 hdev->max_le_tx_power = rp->max_le_tx_power;
1500
1501 return rp->status;
1502 }
1503
hci_cc_le_set_privacy_mode(struct hci_dev * hdev,void * data,struct sk_buff * skb)1504 static u8 hci_cc_le_set_privacy_mode(struct hci_dev *hdev, void *data,
1505 struct sk_buff *skb)
1506 {
1507 struct hci_ev_status *rp = data;
1508 struct hci_cp_le_set_privacy_mode *cp;
1509 struct hci_conn_params *params;
1510
1511 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1512
1513 if (rp->status)
1514 return rp->status;
1515
1516 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
1517 if (!cp)
1518 return rp->status;
1519
1520 hci_dev_lock(hdev);
1521
1522 params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
1523 if (params)
1524 WRITE_ONCE(params->privacy_mode, cp->mode);
1525
1526 hci_dev_unlock(hdev);
1527
1528 return rp->status;
1529 }
1530
hci_cc_le_set_adv_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)1531 static u8 hci_cc_le_set_adv_enable(struct hci_dev *hdev, void *data,
1532 struct sk_buff *skb)
1533 {
1534 struct hci_ev_status *rp = data;
1535 __u8 *sent;
1536
1537 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1538
1539 if (rp->status)
1540 return rp->status;
1541
1542 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1543 if (!sent)
1544 return rp->status;
1545
1546 hci_dev_lock(hdev);
1547
1548 /* If we're doing connection initiation as peripheral. Set a
1549 * timeout in case something goes wrong.
1550 */
1551 if (*sent) {
1552 struct hci_conn *conn;
1553
1554 hci_dev_set_flag(hdev, HCI_LE_ADV);
1555
1556 conn = hci_lookup_le_connect(hdev);
1557 if (conn)
1558 queue_delayed_work(hdev->workqueue,
1559 &conn->le_conn_timeout,
1560 conn->conn_timeout);
1561 } else {
1562 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1563 }
1564
1565 hci_dev_unlock(hdev);
1566
1567 return rp->status;
1568 }
1569
hci_cc_le_set_ext_adv_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)1570 static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,
1571 struct sk_buff *skb)
1572 {
1573 struct hci_cp_le_set_ext_adv_enable *cp;
1574 struct hci_cp_ext_adv_set *set;
1575 struct adv_info *adv = NULL, *n;
1576 struct hci_ev_status *rp = data;
1577
1578 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1579
1580 if (rp->status)
1581 return rp->status;
1582
1583 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1584 if (!cp)
1585 return rp->status;
1586
1587 set = (void *)cp->data;
1588
1589 hci_dev_lock(hdev);
1590
1591 if (cp->num_of_sets)
1592 adv = hci_find_adv_instance(hdev, set->handle);
1593
1594 if (cp->enable) {
1595 struct hci_conn *conn;
1596
1597 hci_dev_set_flag(hdev, HCI_LE_ADV);
1598
1599 if (adv && !adv->periodic)
1600 adv->enabled = true;
1601
1602 conn = hci_lookup_le_connect(hdev);
1603 if (conn)
1604 queue_delayed_work(hdev->workqueue,
1605 &conn->le_conn_timeout,
1606 conn->conn_timeout);
1607 } else {
1608 if (cp->num_of_sets) {
1609 if (adv)
1610 adv->enabled = false;
1611
1612 /* If just one instance was disabled check if there are
1613 * any other instance enabled before clearing HCI_LE_ADV
1614 */
1615 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1616 list) {
1617 if (adv->enabled)
1618 goto unlock;
1619 }
1620 } else {
1621 /* All instances shall be considered disabled */
1622 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1623 list)
1624 adv->enabled = false;
1625 }
1626
1627 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1628 }
1629
1630 unlock:
1631 hci_dev_unlock(hdev);
1632 return rp->status;
1633 }
1634
hci_cc_le_set_scan_param(struct hci_dev * hdev,void * data,struct sk_buff * skb)1635 static u8 hci_cc_le_set_scan_param(struct hci_dev *hdev, void *data,
1636 struct sk_buff *skb)
1637 {
1638 struct hci_cp_le_set_scan_param *cp;
1639 struct hci_ev_status *rp = data;
1640
1641 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1642
1643 if (rp->status)
1644 return rp->status;
1645
1646 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1647 if (!cp)
1648 return rp->status;
1649
1650 hci_dev_lock(hdev);
1651
1652 hdev->le_scan_type = cp->type;
1653
1654 hci_dev_unlock(hdev);
1655
1656 return rp->status;
1657 }
1658
hci_cc_le_set_ext_scan_param(struct hci_dev * hdev,void * data,struct sk_buff * skb)1659 static u8 hci_cc_le_set_ext_scan_param(struct hci_dev *hdev, void *data,
1660 struct sk_buff *skb)
1661 {
1662 struct hci_cp_le_set_ext_scan_params *cp;
1663 struct hci_ev_status *rp = data;
1664 struct hci_cp_le_scan_phy_params *phy_param;
1665
1666 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1667
1668 if (rp->status)
1669 return rp->status;
1670
1671 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1672 if (!cp)
1673 return rp->status;
1674
1675 phy_param = (void *)cp->data;
1676
1677 hci_dev_lock(hdev);
1678
1679 hdev->le_scan_type = phy_param->type;
1680
1681 hci_dev_unlock(hdev);
1682
1683 return rp->status;
1684 }
1685
has_pending_adv_report(struct hci_dev * hdev)1686 static bool has_pending_adv_report(struct hci_dev *hdev)
1687 {
1688 struct discovery_state *d = &hdev->discovery;
1689
1690 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1691 }
1692
clear_pending_adv_report(struct hci_dev * hdev)1693 static void clear_pending_adv_report(struct hci_dev *hdev)
1694 {
1695 struct discovery_state *d = &hdev->discovery;
1696
1697 bacpy(&d->last_adv_addr, BDADDR_ANY);
1698 d->last_adv_data_len = 0;
1699 }
1700
store_pending_adv_report(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 bdaddr_type,s8 rssi,u32 flags,u8 * data,u8 len)1701 static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
1702 u8 bdaddr_type, s8 rssi, u32 flags,
1703 u8 *data, u8 len)
1704 {
1705 struct discovery_state *d = &hdev->discovery;
1706
1707 if (len > max_adv_len(hdev))
1708 return;
1709
1710 bacpy(&d->last_adv_addr, bdaddr);
1711 d->last_adv_addr_type = bdaddr_type;
1712 d->last_adv_rssi = rssi;
1713 d->last_adv_flags = flags;
1714 memcpy(d->last_adv_data, data, len);
1715 d->last_adv_data_len = len;
1716 }
1717
le_set_scan_enable_complete(struct hci_dev * hdev,u8 enable)1718 static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
1719 {
1720 hci_dev_lock(hdev);
1721
1722 switch (enable) {
1723 case LE_SCAN_ENABLE:
1724 hci_dev_set_flag(hdev, HCI_LE_SCAN);
1725 if (hdev->le_scan_type == LE_SCAN_ACTIVE) {
1726 clear_pending_adv_report(hdev);
1727 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
1728 }
1729 break;
1730
1731 case LE_SCAN_DISABLE:
1732 /* We do this here instead of when setting DISCOVERY_STOPPED
1733 * since the latter would potentially require waiting for
1734 * inquiry to stop too.
1735 */
1736 if (has_pending_adv_report(hdev)) {
1737 struct discovery_state *d = &hdev->discovery;
1738
1739 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
1740 d->last_adv_addr_type, NULL,
1741 d->last_adv_rssi, d->last_adv_flags,
1742 d->last_adv_data,
1743 d->last_adv_data_len, NULL, 0, 0);
1744 }
1745
1746 /* Cancel this timer so that we don't try to disable scanning
1747 * when it's already disabled.
1748 */
1749 cancel_delayed_work(&hdev->le_scan_disable);
1750
1751 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
1752
1753 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1754 * interrupted scanning due to a connect request. Mark
1755 * therefore discovery as stopped.
1756 */
1757 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
1758 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1759 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
1760 hdev->discovery.state == DISCOVERY_FINDING)
1761 queue_work(hdev->workqueue, &hdev->reenable_adv_work);
1762
1763 break;
1764
1765 default:
1766 bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
1767 enable);
1768 break;
1769 }
1770
1771 hci_dev_unlock(hdev);
1772 }
1773
hci_cc_le_set_scan_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)1774 static u8 hci_cc_le_set_scan_enable(struct hci_dev *hdev, void *data,
1775 struct sk_buff *skb)
1776 {
1777 struct hci_cp_le_set_scan_enable *cp;
1778 struct hci_ev_status *rp = data;
1779
1780 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1781
1782 if (rp->status)
1783 return rp->status;
1784
1785 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1786 if (!cp)
1787 return rp->status;
1788
1789 le_set_scan_enable_complete(hdev, cp->enable);
1790
1791 return rp->status;
1792 }
1793
hci_cc_le_set_ext_scan_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)1794 static u8 hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev, void *data,
1795 struct sk_buff *skb)
1796 {
1797 struct hci_cp_le_set_ext_scan_enable *cp;
1798 struct hci_ev_status *rp = data;
1799
1800 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1801
1802 if (rp->status)
1803 return rp->status;
1804
1805 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1806 if (!cp)
1807 return rp->status;
1808
1809 le_set_scan_enable_complete(hdev, cp->enable);
1810
1811 return rp->status;
1812 }
1813
hci_cc_le_read_num_adv_sets(struct hci_dev * hdev,void * data,struct sk_buff * skb)1814 static u8 hci_cc_le_read_num_adv_sets(struct hci_dev *hdev, void *data,
1815 struct sk_buff *skb)
1816 {
1817 struct hci_rp_le_read_num_supported_adv_sets *rp = data;
1818
1819 bt_dev_dbg(hdev, "status 0x%2.2x No of Adv sets %u", rp->status,
1820 rp->num_of_sets);
1821
1822 if (rp->status)
1823 return rp->status;
1824
1825 hdev->le_num_of_adv_sets = rp->num_of_sets;
1826
1827 return rp->status;
1828 }
1829
hci_cc_le_read_accept_list_size(struct hci_dev * hdev,void * data,struct sk_buff * skb)1830 static u8 hci_cc_le_read_accept_list_size(struct hci_dev *hdev, void *data,
1831 struct sk_buff *skb)
1832 {
1833 struct hci_rp_le_read_accept_list_size *rp = data;
1834
1835 bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
1836
1837 if (rp->status)
1838 return rp->status;
1839
1840 hdev->le_accept_list_size = rp->size;
1841
1842 return rp->status;
1843 }
1844
hci_cc_le_clear_accept_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)1845 static u8 hci_cc_le_clear_accept_list(struct hci_dev *hdev, void *data,
1846 struct sk_buff *skb)
1847 {
1848 struct hci_ev_status *rp = data;
1849
1850 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1851
1852 if (rp->status)
1853 return rp->status;
1854
1855 hci_dev_lock(hdev);
1856 hci_bdaddr_list_clear(&hdev->le_accept_list);
1857 hci_dev_unlock(hdev);
1858
1859 return rp->status;
1860 }
1861
hci_cc_le_add_to_accept_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)1862 static u8 hci_cc_le_add_to_accept_list(struct hci_dev *hdev, void *data,
1863 struct sk_buff *skb)
1864 {
1865 struct hci_cp_le_add_to_accept_list *sent;
1866 struct hci_ev_status *rp = data;
1867
1868 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1869
1870 if (rp->status)
1871 return rp->status;
1872
1873 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
1874 if (!sent)
1875 return rp->status;
1876
1877 hci_dev_lock(hdev);
1878 hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
1879 sent->bdaddr_type);
1880 hci_dev_unlock(hdev);
1881
1882 return rp->status;
1883 }
1884
hci_cc_le_del_from_accept_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)1885 static u8 hci_cc_le_del_from_accept_list(struct hci_dev *hdev, void *data,
1886 struct sk_buff *skb)
1887 {
1888 struct hci_cp_le_del_from_accept_list *sent;
1889 struct hci_ev_status *rp = data;
1890
1891 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1892
1893 if (rp->status)
1894 return rp->status;
1895
1896 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
1897 if (!sent)
1898 return rp->status;
1899
1900 hci_dev_lock(hdev);
1901 hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
1902 sent->bdaddr_type);
1903 hci_dev_unlock(hdev);
1904
1905 return rp->status;
1906 }
1907
hci_cc_le_read_supported_states(struct hci_dev * hdev,void * data,struct sk_buff * skb)1908 static u8 hci_cc_le_read_supported_states(struct hci_dev *hdev, void *data,
1909 struct sk_buff *skb)
1910 {
1911 struct hci_rp_le_read_supported_states *rp = data;
1912
1913 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1914
1915 if (rp->status)
1916 return rp->status;
1917
1918 memcpy(hdev->le_states, rp->le_states, 8);
1919
1920 return rp->status;
1921 }
1922
hci_cc_le_read_def_data_len(struct hci_dev * hdev,void * data,struct sk_buff * skb)1923 static u8 hci_cc_le_read_def_data_len(struct hci_dev *hdev, void *data,
1924 struct sk_buff *skb)
1925 {
1926 struct hci_rp_le_read_def_data_len *rp = data;
1927
1928 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1929
1930 if (rp->status)
1931 return rp->status;
1932
1933 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1934 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1935
1936 return rp->status;
1937 }
1938
hci_cc_le_write_def_data_len(struct hci_dev * hdev,void * data,struct sk_buff * skb)1939 static u8 hci_cc_le_write_def_data_len(struct hci_dev *hdev, void *data,
1940 struct sk_buff *skb)
1941 {
1942 struct hci_cp_le_write_def_data_len *sent;
1943 struct hci_ev_status *rp = data;
1944
1945 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1946
1947 if (rp->status)
1948 return rp->status;
1949
1950 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1951 if (!sent)
1952 return rp->status;
1953
1954 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1955 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1956
1957 return rp->status;
1958 }
1959
hci_cc_le_add_to_resolv_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)1960 static u8 hci_cc_le_add_to_resolv_list(struct hci_dev *hdev, void *data,
1961 struct sk_buff *skb)
1962 {
1963 struct hci_cp_le_add_to_resolv_list *sent;
1964 struct hci_ev_status *rp = data;
1965
1966 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1967
1968 if (rp->status)
1969 return rp->status;
1970
1971 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
1972 if (!sent)
1973 return rp->status;
1974
1975 hci_dev_lock(hdev);
1976 hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1977 sent->bdaddr_type, sent->peer_irk,
1978 sent->local_irk);
1979 hci_dev_unlock(hdev);
1980
1981 return rp->status;
1982 }
1983
hci_cc_le_del_from_resolv_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)1984 static u8 hci_cc_le_del_from_resolv_list(struct hci_dev *hdev, void *data,
1985 struct sk_buff *skb)
1986 {
1987 struct hci_cp_le_del_from_resolv_list *sent;
1988 struct hci_ev_status *rp = data;
1989
1990 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1991
1992 if (rp->status)
1993 return rp->status;
1994
1995 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
1996 if (!sent)
1997 return rp->status;
1998
1999 hci_dev_lock(hdev);
2000 hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
2001 sent->bdaddr_type);
2002 hci_dev_unlock(hdev);
2003
2004 return rp->status;
2005 }
2006
hci_cc_le_clear_resolv_list(struct hci_dev * hdev,void * data,struct sk_buff * skb)2007 static u8 hci_cc_le_clear_resolv_list(struct hci_dev *hdev, void *data,
2008 struct sk_buff *skb)
2009 {
2010 struct hci_ev_status *rp = data;
2011
2012 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2013
2014 if (rp->status)
2015 return rp->status;
2016
2017 hci_dev_lock(hdev);
2018 hci_bdaddr_list_clear(&hdev->le_resolv_list);
2019 hci_dev_unlock(hdev);
2020
2021 return rp->status;
2022 }
2023
hci_cc_le_read_resolv_list_size(struct hci_dev * hdev,void * data,struct sk_buff * skb)2024 static u8 hci_cc_le_read_resolv_list_size(struct hci_dev *hdev, void *data,
2025 struct sk_buff *skb)
2026 {
2027 struct hci_rp_le_read_resolv_list_size *rp = data;
2028
2029 bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
2030
2031 if (rp->status)
2032 return rp->status;
2033
2034 hdev->le_resolv_list_size = rp->size;
2035
2036 return rp->status;
2037 }
2038
hci_cc_le_set_addr_resolution_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)2039 static u8 hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev, void *data,
2040 struct sk_buff *skb)
2041 {
2042 struct hci_ev_status *rp = data;
2043 __u8 *sent;
2044
2045 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2046
2047 if (rp->status)
2048 return rp->status;
2049
2050 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
2051 if (!sent)
2052 return rp->status;
2053
2054 hci_dev_lock(hdev);
2055
2056 if (*sent)
2057 hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
2058 else
2059 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
2060
2061 hci_dev_unlock(hdev);
2062
2063 return rp->status;
2064 }
2065
hci_cc_le_read_max_data_len(struct hci_dev * hdev,void * data,struct sk_buff * skb)2066 static u8 hci_cc_le_read_max_data_len(struct hci_dev *hdev, void *data,
2067 struct sk_buff *skb)
2068 {
2069 struct hci_rp_le_read_max_data_len *rp = data;
2070
2071 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2072
2073 if (rp->status)
2074 return rp->status;
2075
2076 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
2077 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
2078 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
2079 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
2080
2081 return rp->status;
2082 }
2083
hci_cc_write_le_host_supported(struct hci_dev * hdev,void * data,struct sk_buff * skb)2084 static u8 hci_cc_write_le_host_supported(struct hci_dev *hdev, void *data,
2085 struct sk_buff *skb)
2086 {
2087 struct hci_cp_write_le_host_supported *sent;
2088 struct hci_ev_status *rp = data;
2089
2090 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2091
2092 if (rp->status)
2093 return rp->status;
2094
2095 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
2096 if (!sent)
2097 return rp->status;
2098
2099 hci_dev_lock(hdev);
2100
2101 if (sent->le) {
2102 hdev->features[1][0] |= LMP_HOST_LE;
2103 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
2104 } else {
2105 hdev->features[1][0] &= ~LMP_HOST_LE;
2106 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
2107 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
2108 }
2109
2110 if (sent->simul)
2111 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
2112 else
2113 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
2114
2115 hci_dev_unlock(hdev);
2116
2117 return rp->status;
2118 }
2119
hci_cc_set_adv_param(struct hci_dev * hdev,void * data,struct sk_buff * skb)2120 static u8 hci_cc_set_adv_param(struct hci_dev *hdev, void *data,
2121 struct sk_buff *skb)
2122 {
2123 struct hci_cp_le_set_adv_param *cp;
2124 struct hci_ev_status *rp = data;
2125
2126 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2127
2128 if (rp->status)
2129 return rp->status;
2130
2131 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
2132 if (!cp)
2133 return rp->status;
2134
2135 hci_dev_lock(hdev);
2136 hdev->adv_addr_type = cp->own_address_type;
2137 hci_dev_unlock(hdev);
2138
2139 return rp->status;
2140 }
2141
hci_cc_set_ext_adv_param(struct hci_dev * hdev,void * data,struct sk_buff * skb)2142 static u8 hci_cc_set_ext_adv_param(struct hci_dev *hdev, void *data,
2143 struct sk_buff *skb)
2144 {
2145 struct hci_rp_le_set_ext_adv_params *rp = data;
2146 struct hci_cp_le_set_ext_adv_params *cp;
2147 struct adv_info *adv_instance;
2148
2149 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2150
2151 if (rp->status)
2152 return rp->status;
2153
2154 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
2155 if (!cp)
2156 return rp->status;
2157
2158 hci_dev_lock(hdev);
2159 hdev->adv_addr_type = cp->own_addr_type;
2160 if (!cp->handle) {
2161 /* Store in hdev for instance 0 */
2162 hdev->adv_tx_power = rp->tx_power;
2163 } else {
2164 adv_instance = hci_find_adv_instance(hdev, cp->handle);
2165 if (adv_instance)
2166 adv_instance->tx_power = rp->tx_power;
2167 }
2168 /* Update adv data as tx power is known now */
2169 hci_update_adv_data(hdev, cp->handle);
2170
2171 hci_dev_unlock(hdev);
2172
2173 return rp->status;
2174 }
2175
hci_cc_read_rssi(struct hci_dev * hdev,void * data,struct sk_buff * skb)2176 static u8 hci_cc_read_rssi(struct hci_dev *hdev, void *data,
2177 struct sk_buff *skb)
2178 {
2179 struct hci_rp_read_rssi *rp = data;
2180 struct hci_conn *conn;
2181
2182 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2183
2184 if (rp->status)
2185 return rp->status;
2186
2187 hci_dev_lock(hdev);
2188
2189 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
2190 if (conn)
2191 conn->rssi = rp->rssi;
2192
2193 hci_dev_unlock(hdev);
2194
2195 return rp->status;
2196 }
2197
hci_cc_read_tx_power(struct hci_dev * hdev,void * data,struct sk_buff * skb)2198 static u8 hci_cc_read_tx_power(struct hci_dev *hdev, void *data,
2199 struct sk_buff *skb)
2200 {
2201 struct hci_cp_read_tx_power *sent;
2202 struct hci_rp_read_tx_power *rp = data;
2203 struct hci_conn *conn;
2204
2205 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2206
2207 if (rp->status)
2208 return rp->status;
2209
2210 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
2211 if (!sent)
2212 return rp->status;
2213
2214 hci_dev_lock(hdev);
2215
2216 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
2217 if (!conn)
2218 goto unlock;
2219
2220 switch (sent->type) {
2221 case 0x00:
2222 conn->tx_power = rp->tx_power;
2223 break;
2224 case 0x01:
2225 conn->max_tx_power = rp->tx_power;
2226 break;
2227 }
2228
2229 unlock:
2230 hci_dev_unlock(hdev);
2231 return rp->status;
2232 }
2233
hci_cc_write_ssp_debug_mode(struct hci_dev * hdev,void * data,struct sk_buff * skb)2234 static u8 hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, void *data,
2235 struct sk_buff *skb)
2236 {
2237 struct hci_ev_status *rp = data;
2238 u8 *mode;
2239
2240 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2241
2242 if (rp->status)
2243 return rp->status;
2244
2245 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
2246 if (mode)
2247 hdev->ssp_debug_mode = *mode;
2248
2249 return rp->status;
2250 }
2251
hci_cs_inquiry(struct hci_dev * hdev,__u8 status)2252 static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
2253 {
2254 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2255
2256 if (status)
2257 return;
2258
2259 if (hci_sent_cmd_data(hdev, HCI_OP_INQUIRY))
2260 set_bit(HCI_INQUIRY, &hdev->flags);
2261 }
2262
hci_cs_create_conn(struct hci_dev * hdev,__u8 status)2263 static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
2264 {
2265 struct hci_cp_create_conn *cp;
2266 struct hci_conn *conn;
2267
2268 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2269
2270 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
2271 if (!cp)
2272 return;
2273
2274 hci_dev_lock(hdev);
2275
2276 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2277
2278 bt_dev_dbg(hdev, "bdaddr %pMR hcon %p", &cp->bdaddr, conn);
2279
2280 if (status) {
2281 if (conn && conn->state == BT_CONNECT) {
2282 conn->state = BT_CLOSED;
2283 hci_connect_cfm(conn, status);
2284 hci_conn_del(conn);
2285 }
2286 } else {
2287 if (!conn) {
2288 conn = hci_conn_add_unset(hdev, ACL_LINK, &cp->bdaddr,
2289 HCI_ROLE_MASTER);
2290 if (IS_ERR(conn))
2291 bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
2292 }
2293 }
2294
2295 hci_dev_unlock(hdev);
2296 }
2297
hci_cs_add_sco(struct hci_dev * hdev,__u8 status)2298 static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
2299 {
2300 struct hci_cp_add_sco *cp;
2301 struct hci_conn *acl;
2302 struct hci_link *link;
2303 __u16 handle;
2304
2305 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2306
2307 if (!status)
2308 return;
2309
2310 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
2311 if (!cp)
2312 return;
2313
2314 handle = __le16_to_cpu(cp->handle);
2315
2316 bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
2317
2318 hci_dev_lock(hdev);
2319
2320 acl = hci_conn_hash_lookup_handle(hdev, handle);
2321 if (acl) {
2322 link = list_first_entry_or_null(&acl->link_list,
2323 struct hci_link, list);
2324 if (link && link->conn) {
2325 link->conn->state = BT_CLOSED;
2326
2327 hci_connect_cfm(link->conn, status);
2328 hci_conn_del(link->conn);
2329 }
2330 }
2331
2332 hci_dev_unlock(hdev);
2333 }
2334
hci_cs_auth_requested(struct hci_dev * hdev,__u8 status)2335 static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
2336 {
2337 struct hci_cp_auth_requested *cp;
2338 struct hci_conn *conn;
2339
2340 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2341
2342 if (!status)
2343 return;
2344
2345 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
2346 if (!cp)
2347 return;
2348
2349 hci_dev_lock(hdev);
2350
2351 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2352 if (conn) {
2353 if (conn->state == BT_CONFIG) {
2354 hci_connect_cfm(conn, status);
2355 hci_conn_drop(conn);
2356 }
2357 }
2358
2359 hci_dev_unlock(hdev);
2360 }
2361
hci_cs_set_conn_encrypt(struct hci_dev * hdev,__u8 status)2362 static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
2363 {
2364 struct hci_cp_set_conn_encrypt *cp;
2365 struct hci_conn *conn;
2366
2367 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2368
2369 if (!status)
2370 return;
2371
2372 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
2373 if (!cp)
2374 return;
2375
2376 hci_dev_lock(hdev);
2377
2378 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2379 if (conn) {
2380 if (conn->state == BT_CONFIG) {
2381 hci_connect_cfm(conn, status);
2382 hci_conn_drop(conn);
2383 }
2384 }
2385
2386 hci_dev_unlock(hdev);
2387 }
2388
hci_outgoing_auth_needed(struct hci_dev * hdev,struct hci_conn * conn)2389 static int hci_outgoing_auth_needed(struct hci_dev *hdev,
2390 struct hci_conn *conn)
2391 {
2392 if (conn->state != BT_CONFIG || !conn->out)
2393 return 0;
2394
2395 if (conn->pending_sec_level == BT_SECURITY_SDP)
2396 return 0;
2397
2398 /* Only request authentication for SSP connections or non-SSP
2399 * devices with sec_level MEDIUM or HIGH or if MITM protection
2400 * is requested.
2401 */
2402 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
2403 conn->pending_sec_level != BT_SECURITY_FIPS &&
2404 conn->pending_sec_level != BT_SECURITY_HIGH &&
2405 conn->pending_sec_level != BT_SECURITY_MEDIUM)
2406 return 0;
2407
2408 return 1;
2409 }
2410
hci_resolve_name(struct hci_dev * hdev,struct inquiry_entry * e)2411 static int hci_resolve_name(struct hci_dev *hdev,
2412 struct inquiry_entry *e)
2413 {
2414 struct hci_cp_remote_name_req cp;
2415
2416 memset(&cp, 0, sizeof(cp));
2417
2418 bacpy(&cp.bdaddr, &e->data.bdaddr);
2419 cp.pscan_rep_mode = e->data.pscan_rep_mode;
2420 cp.pscan_mode = e->data.pscan_mode;
2421 cp.clock_offset = e->data.clock_offset;
2422
2423 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2424 }
2425
hci_resolve_next_name(struct hci_dev * hdev)2426 static bool hci_resolve_next_name(struct hci_dev *hdev)
2427 {
2428 struct discovery_state *discov = &hdev->discovery;
2429 struct inquiry_entry *e;
2430
2431 if (list_empty(&discov->resolve))
2432 return false;
2433
2434 /* We should stop if we already spent too much time resolving names. */
2435 if (time_after(jiffies, discov->name_resolve_timeout)) {
2436 bt_dev_warn_ratelimited(hdev, "Name resolve takes too long.");
2437 return false;
2438 }
2439
2440 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2441 if (!e)
2442 return false;
2443
2444 if (hci_resolve_name(hdev, e) == 0) {
2445 e->name_state = NAME_PENDING;
2446 return true;
2447 }
2448
2449 return false;
2450 }
2451
hci_check_pending_name(struct hci_dev * hdev,struct hci_conn * conn,bdaddr_t * bdaddr,u8 * name,u8 name_len)2452 static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
2453 bdaddr_t *bdaddr, u8 *name, u8 name_len)
2454 {
2455 struct discovery_state *discov = &hdev->discovery;
2456 struct inquiry_entry *e;
2457
2458 /* Update the mgmt connected state if necessary. Be careful with
2459 * conn objects that exist but are not (yet) connected however.
2460 * Only those in BT_CONFIG or BT_CONNECTED states can be
2461 * considered connected.
2462 */
2463 if (conn && (conn->state == BT_CONFIG || conn->state == BT_CONNECTED))
2464 mgmt_device_connected(hdev, conn, name, name_len);
2465
2466 if (discov->state == DISCOVERY_STOPPED)
2467 return;
2468
2469 if (discov->state == DISCOVERY_STOPPING)
2470 goto discov_complete;
2471
2472 if (discov->state != DISCOVERY_RESOLVING)
2473 return;
2474
2475 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
2476 /* If the device was not found in a list of found devices names of which
2477 * are pending. there is no need to continue resolving a next name as it
2478 * will be done upon receiving another Remote Name Request Complete
2479 * Event */
2480 if (!e)
2481 return;
2482
2483 list_del(&e->list);
2484
2485 e->name_state = name ? NAME_KNOWN : NAME_NOT_KNOWN;
2486 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00, e->data.rssi,
2487 name, name_len);
2488
2489 if (hci_resolve_next_name(hdev))
2490 return;
2491
2492 discov_complete:
2493 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2494 }
2495
hci_cs_remote_name_req(struct hci_dev * hdev,__u8 status)2496 static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2497 {
2498 struct hci_cp_remote_name_req *cp;
2499 struct hci_conn *conn;
2500
2501 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2502
2503 /* If successful wait for the name req complete event before
2504 * checking for the need to do authentication */
2505 if (!status)
2506 return;
2507
2508 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2509 if (!cp)
2510 return;
2511
2512 hci_dev_lock(hdev);
2513
2514 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2515
2516 if (hci_dev_test_flag(hdev, HCI_MGMT))
2517 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2518
2519 if (!conn)
2520 goto unlock;
2521
2522 if (!hci_outgoing_auth_needed(hdev, conn))
2523 goto unlock;
2524
2525 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
2526 struct hci_cp_auth_requested auth_cp;
2527
2528 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2529
2530 auth_cp.handle = __cpu_to_le16(conn->handle);
2531 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2532 sizeof(auth_cp), &auth_cp);
2533 }
2534
2535 unlock:
2536 hci_dev_unlock(hdev);
2537 }
2538
hci_cs_read_remote_features(struct hci_dev * hdev,__u8 status)2539 static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2540 {
2541 struct hci_cp_read_remote_features *cp;
2542 struct hci_conn *conn;
2543
2544 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2545
2546 if (!status)
2547 return;
2548
2549 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2550 if (!cp)
2551 return;
2552
2553 hci_dev_lock(hdev);
2554
2555 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2556 if (conn) {
2557 if (conn->state == BT_CONFIG) {
2558 hci_connect_cfm(conn, status);
2559 hci_conn_drop(conn);
2560 }
2561 }
2562
2563 hci_dev_unlock(hdev);
2564 }
2565
hci_cs_read_remote_ext_features(struct hci_dev * hdev,__u8 status)2566 static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2567 {
2568 struct hci_cp_read_remote_ext_features *cp;
2569 struct hci_conn *conn;
2570
2571 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2572
2573 if (!status)
2574 return;
2575
2576 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2577 if (!cp)
2578 return;
2579
2580 hci_dev_lock(hdev);
2581
2582 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2583 if (conn) {
2584 if (conn->state == BT_CONFIG) {
2585 hci_connect_cfm(conn, status);
2586 hci_conn_drop(conn);
2587 }
2588 }
2589
2590 hci_dev_unlock(hdev);
2591 }
2592
hci_setup_sync_conn_status(struct hci_dev * hdev,__u16 handle,__u8 status)2593 static void hci_setup_sync_conn_status(struct hci_dev *hdev, __u16 handle,
2594 __u8 status)
2595 {
2596 struct hci_conn *acl;
2597 struct hci_link *link;
2598
2599 bt_dev_dbg(hdev, "handle 0x%4.4x status 0x%2.2x", handle, status);
2600
2601 hci_dev_lock(hdev);
2602
2603 acl = hci_conn_hash_lookup_handle(hdev, handle);
2604 if (acl) {
2605 link = list_first_entry_or_null(&acl->link_list,
2606 struct hci_link, list);
2607 if (link && link->conn) {
2608 link->conn->state = BT_CLOSED;
2609
2610 hci_connect_cfm(link->conn, status);
2611 hci_conn_del(link->conn);
2612 }
2613 }
2614
2615 hci_dev_unlock(hdev);
2616 }
2617
hci_cs_setup_sync_conn(struct hci_dev * hdev,__u8 status)2618 static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2619 {
2620 struct hci_cp_setup_sync_conn *cp;
2621
2622 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2623
2624 if (!status)
2625 return;
2626
2627 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2628 if (!cp)
2629 return;
2630
2631 hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
2632 }
2633
hci_cs_enhanced_setup_sync_conn(struct hci_dev * hdev,__u8 status)2634 static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2635 {
2636 struct hci_cp_enhanced_setup_sync_conn *cp;
2637
2638 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2639
2640 if (!status)
2641 return;
2642
2643 cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
2644 if (!cp)
2645 return;
2646
2647 hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
2648 }
2649
hci_cs_sniff_mode(struct hci_dev * hdev,__u8 status)2650 static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2651 {
2652 struct hci_cp_sniff_mode *cp;
2653 struct hci_conn *conn;
2654
2655 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2656
2657 if (!status)
2658 return;
2659
2660 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2661 if (!cp)
2662 return;
2663
2664 hci_dev_lock(hdev);
2665
2666 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2667 if (conn) {
2668 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
2669
2670 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
2671 hci_sco_setup(conn, status);
2672 }
2673
2674 hci_dev_unlock(hdev);
2675 }
2676
hci_cs_exit_sniff_mode(struct hci_dev * hdev,__u8 status)2677 static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2678 {
2679 struct hci_cp_exit_sniff_mode *cp;
2680 struct hci_conn *conn;
2681
2682 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2683
2684 if (!status)
2685 return;
2686
2687 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2688 if (!cp)
2689 return;
2690
2691 hci_dev_lock(hdev);
2692
2693 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2694 if (conn) {
2695 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
2696
2697 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
2698 hci_sco_setup(conn, status);
2699 }
2700
2701 hci_dev_unlock(hdev);
2702 }
2703
hci_cs_disconnect(struct hci_dev * hdev,u8 status)2704 static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2705 {
2706 struct hci_cp_disconnect *cp;
2707 struct hci_conn_params *params;
2708 struct hci_conn *conn;
2709 bool mgmt_conn;
2710
2711 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2712
2713 /* Wait for HCI_EV_DISCONN_COMPLETE if status 0x00 and not suspended
2714 * otherwise cleanup the connection immediately.
2715 */
2716 if (!status && !hdev->suspended)
2717 return;
2718
2719 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2720 if (!cp)
2721 return;
2722
2723 hci_dev_lock(hdev);
2724
2725 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2726 if (!conn)
2727 goto unlock;
2728
2729 if (status) {
2730 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2731 conn->dst_type, status);
2732
2733 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
2734 hdev->cur_adv_instance = conn->adv_instance;
2735 hci_enable_advertising(hdev);
2736 }
2737
2738 /* Inform sockets conn is gone before we delete it */
2739 hci_disconn_cfm(conn, HCI_ERROR_UNSPECIFIED);
2740
2741 goto done;
2742 }
2743
2744 mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2745
2746 if (conn->type == ACL_LINK) {
2747 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2748 hci_remove_link_key(hdev, &conn->dst);
2749 }
2750
2751 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2752 if (params) {
2753 switch (params->auto_connect) {
2754 case HCI_AUTO_CONN_LINK_LOSS:
2755 if (cp->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2756 break;
2757 fallthrough;
2758
2759 case HCI_AUTO_CONN_DIRECT:
2760 case HCI_AUTO_CONN_ALWAYS:
2761 hci_pend_le_list_del_init(params);
2762 hci_pend_le_list_add(params, &hdev->pend_le_conns);
2763 break;
2764
2765 default:
2766 break;
2767 }
2768 }
2769
2770 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2771 cp->reason, mgmt_conn);
2772
2773 hci_disconn_cfm(conn, cp->reason);
2774
2775 done:
2776 /* If the disconnection failed for any reason, the upper layer
2777 * does not retry to disconnect in current implementation.
2778 * Hence, we need to do some basic cleanup here and re-enable
2779 * advertising if necessary.
2780 */
2781 hci_conn_del(conn);
2782 unlock:
2783 hci_dev_unlock(hdev);
2784 }
2785
ev_bdaddr_type(struct hci_dev * hdev,u8 type,bool * resolved)2786 static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
2787 {
2788 /* When using controller based address resolution, then the new
2789 * address types 0x02 and 0x03 are used. These types need to be
2790 * converted back into either public address or random address type
2791 */
2792 switch (type) {
2793 case ADDR_LE_DEV_PUBLIC_RESOLVED:
2794 if (resolved)
2795 *resolved = true;
2796 return ADDR_LE_DEV_PUBLIC;
2797 case ADDR_LE_DEV_RANDOM_RESOLVED:
2798 if (resolved)
2799 *resolved = true;
2800 return ADDR_LE_DEV_RANDOM;
2801 }
2802
2803 if (resolved)
2804 *resolved = false;
2805 return type;
2806 }
2807
cs_le_create_conn(struct hci_dev * hdev,bdaddr_t * peer_addr,u8 peer_addr_type,u8 own_address_type,u8 filter_policy)2808 static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2809 u8 peer_addr_type, u8 own_address_type,
2810 u8 filter_policy)
2811 {
2812 struct hci_conn *conn;
2813
2814 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2815 peer_addr_type);
2816 if (!conn)
2817 return;
2818
2819 own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
2820
2821 /* Store the initiator and responder address information which
2822 * is needed for SMP. These values will not change during the
2823 * lifetime of the connection.
2824 */
2825 conn->init_addr_type = own_address_type;
2826 if (own_address_type == ADDR_LE_DEV_RANDOM)
2827 bacpy(&conn->init_addr, &hdev->random_addr);
2828 else
2829 bacpy(&conn->init_addr, &hdev->bdaddr);
2830
2831 conn->resp_addr_type = peer_addr_type;
2832 bacpy(&conn->resp_addr, peer_addr);
2833 }
2834
hci_cs_le_create_conn(struct hci_dev * hdev,u8 status)2835 static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2836 {
2837 struct hci_cp_le_create_conn *cp;
2838
2839 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2840
2841 /* All connection failure handling is taken care of by the
2842 * hci_conn_failed function which is triggered by the HCI
2843 * request completion callbacks used for connecting.
2844 */
2845 if (status)
2846 return;
2847
2848 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2849 if (!cp)
2850 return;
2851
2852 hci_dev_lock(hdev);
2853
2854 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2855 cp->own_address_type, cp->filter_policy);
2856
2857 hci_dev_unlock(hdev);
2858 }
2859
hci_cs_le_ext_create_conn(struct hci_dev * hdev,u8 status)2860 static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2861 {
2862 struct hci_cp_le_ext_create_conn *cp;
2863
2864 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2865
2866 /* All connection failure handling is taken care of by the
2867 * hci_conn_failed function which is triggered by the HCI
2868 * request completion callbacks used for connecting.
2869 */
2870 if (status)
2871 return;
2872
2873 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2874 if (!cp)
2875 return;
2876
2877 hci_dev_lock(hdev);
2878
2879 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2880 cp->own_addr_type, cp->filter_policy);
2881
2882 hci_dev_unlock(hdev);
2883 }
2884
hci_cs_le_read_remote_features(struct hci_dev * hdev,u8 status)2885 static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2886 {
2887 struct hci_cp_le_read_remote_features *cp;
2888 struct hci_conn *conn;
2889
2890 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2891
2892 if (!status)
2893 return;
2894
2895 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2896 if (!cp)
2897 return;
2898
2899 hci_dev_lock(hdev);
2900
2901 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2902 if (conn) {
2903 if (conn->state == BT_CONFIG) {
2904 hci_connect_cfm(conn, status);
2905 hci_conn_drop(conn);
2906 }
2907 }
2908
2909 hci_dev_unlock(hdev);
2910 }
2911
hci_cs_le_start_enc(struct hci_dev * hdev,u8 status)2912 static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2913 {
2914 struct hci_cp_le_start_enc *cp;
2915 struct hci_conn *conn;
2916
2917 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2918
2919 if (!status)
2920 return;
2921
2922 hci_dev_lock(hdev);
2923
2924 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2925 if (!cp)
2926 goto unlock;
2927
2928 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2929 if (!conn)
2930 goto unlock;
2931
2932 if (conn->state != BT_CONNECTED)
2933 goto unlock;
2934
2935 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2936 hci_conn_drop(conn);
2937
2938 unlock:
2939 hci_dev_unlock(hdev);
2940 }
2941
hci_cs_switch_role(struct hci_dev * hdev,u8 status)2942 static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2943 {
2944 struct hci_cp_switch_role *cp;
2945 struct hci_conn *conn;
2946
2947 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2948
2949 if (!status)
2950 return;
2951
2952 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2953 if (!cp)
2954 return;
2955
2956 hci_dev_lock(hdev);
2957
2958 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2959 if (conn)
2960 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2961
2962 hci_dev_unlock(hdev);
2963 }
2964
hci_inquiry_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)2965 static void hci_inquiry_complete_evt(struct hci_dev *hdev, void *data,
2966 struct sk_buff *skb)
2967 {
2968 struct hci_ev_status *ev = data;
2969 struct discovery_state *discov = &hdev->discovery;
2970 struct inquiry_entry *e;
2971
2972 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
2973
2974 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2975 return;
2976
2977 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
2978 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2979
2980 if (!hci_dev_test_flag(hdev, HCI_MGMT))
2981 return;
2982
2983 hci_dev_lock(hdev);
2984
2985 if (discov->state != DISCOVERY_FINDING)
2986 goto unlock;
2987
2988 if (list_empty(&discov->resolve)) {
2989 /* When BR/EDR inquiry is active and no LE scanning is in
2990 * progress, then change discovery state to indicate completion.
2991 *
2992 * When running LE scanning and BR/EDR inquiry simultaneously
2993 * and the LE scan already finished, then change the discovery
2994 * state to indicate completion.
2995 */
2996 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2997 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2998 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2999 goto unlock;
3000 }
3001
3002 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
3003 if (e && hci_resolve_name(hdev, e) == 0) {
3004 e->name_state = NAME_PENDING;
3005 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
3006 discov->name_resolve_timeout = jiffies + NAME_RESOLVE_DURATION;
3007 } else {
3008 /* When BR/EDR inquiry is active and no LE scanning is in
3009 * progress, then change discovery state to indicate completion.
3010 *
3011 * When running LE scanning and BR/EDR inquiry simultaneously
3012 * and the LE scan already finished, then change the discovery
3013 * state to indicate completion.
3014 */
3015 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
3016 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
3017 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3018 }
3019
3020 unlock:
3021 hci_dev_unlock(hdev);
3022 }
3023
hci_inquiry_result_evt(struct hci_dev * hdev,void * edata,struct sk_buff * skb)3024 static void hci_inquiry_result_evt(struct hci_dev *hdev, void *edata,
3025 struct sk_buff *skb)
3026 {
3027 struct hci_ev_inquiry_result *ev = edata;
3028 struct inquiry_data data;
3029 int i;
3030
3031 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT,
3032 flex_array_size(ev, info, ev->num)))
3033 return;
3034
3035 bt_dev_dbg(hdev, "num %d", ev->num);
3036
3037 if (!ev->num)
3038 return;
3039
3040 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3041 return;
3042
3043 hci_dev_lock(hdev);
3044
3045 for (i = 0; i < ev->num; i++) {
3046 struct inquiry_info *info = &ev->info[i];
3047 u32 flags;
3048
3049 bacpy(&data.bdaddr, &info->bdaddr);
3050 data.pscan_rep_mode = info->pscan_rep_mode;
3051 data.pscan_period_mode = info->pscan_period_mode;
3052 data.pscan_mode = info->pscan_mode;
3053 memcpy(data.dev_class, info->dev_class, 3);
3054 data.clock_offset = info->clock_offset;
3055 data.rssi = HCI_RSSI_INVALID;
3056 data.ssp_mode = 0x00;
3057
3058 flags = hci_inquiry_cache_update(hdev, &data, false);
3059
3060 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
3061 info->dev_class, HCI_RSSI_INVALID,
3062 flags, NULL, 0, NULL, 0, 0);
3063 }
3064
3065 hci_dev_unlock(hdev);
3066 }
3067
hci_conn_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3068 static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
3069 struct sk_buff *skb)
3070 {
3071 struct hci_ev_conn_complete *ev = data;
3072 struct hci_conn *conn;
3073 u8 status = ev->status;
3074
3075 bt_dev_dbg(hdev, "status 0x%2.2x", status);
3076
3077 hci_dev_lock(hdev);
3078
3079 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
3080 if (!conn) {
3081 /* In case of error status and there is no connection pending
3082 * just unlock as there is nothing to cleanup.
3083 */
3084 if (ev->status)
3085 goto unlock;
3086
3087 /* Connection may not exist if auto-connected. Check the bredr
3088 * allowlist to see if this device is allowed to auto connect.
3089 * If link is an ACL type, create a connection class
3090 * automatically.
3091 *
3092 * Auto-connect will only occur if the event filter is
3093 * programmed with a given address. Right now, event filter is
3094 * only used during suspend.
3095 */
3096 if (ev->link_type == ACL_LINK &&
3097 hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
3098 &ev->bdaddr,
3099 BDADDR_BREDR)) {
3100 conn = hci_conn_add_unset(hdev, ev->link_type,
3101 &ev->bdaddr, HCI_ROLE_SLAVE);
3102 if (IS_ERR(conn)) {
3103 bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
3104 goto unlock;
3105 }
3106 } else {
3107 if (ev->link_type != SCO_LINK)
3108 goto unlock;
3109
3110 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
3111 &ev->bdaddr);
3112 if (!conn)
3113 goto unlock;
3114
3115 conn->type = SCO_LINK;
3116 }
3117 }
3118
3119 /* The HCI_Connection_Complete event is only sent once per connection.
3120 * Processing it more than once per connection can corrupt kernel memory.
3121 *
3122 * As the connection handle is set here for the first time, it indicates
3123 * whether the connection is already set up.
3124 */
3125 if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
3126 bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
3127 goto unlock;
3128 }
3129
3130 if (!status) {
3131 status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
3132 if (status)
3133 goto done;
3134
3135 if (conn->type == ACL_LINK) {
3136 conn->state = BT_CONFIG;
3137 hci_conn_hold(conn);
3138
3139 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
3140 !hci_find_link_key(hdev, &ev->bdaddr))
3141 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
3142 else
3143 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3144 } else
3145 conn->state = BT_CONNECTED;
3146
3147 hci_debugfs_create_conn(conn);
3148 hci_conn_add_sysfs(conn);
3149
3150 if (test_bit(HCI_AUTH, &hdev->flags))
3151 set_bit(HCI_CONN_AUTH, &conn->flags);
3152
3153 if (test_bit(HCI_ENCRYPT, &hdev->flags))
3154 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3155
3156 /* "Link key request" completed ahead of "connect request" completes */
3157 if (ev->encr_mode == 1 && !test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3158 ev->link_type == ACL_LINK) {
3159 struct link_key *key;
3160 struct hci_cp_read_enc_key_size cp;
3161
3162 key = hci_find_link_key(hdev, &ev->bdaddr);
3163 if (key) {
3164 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3165
3166 if (!read_key_size_capable(hdev)) {
3167 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3168 } else {
3169 cp.handle = cpu_to_le16(conn->handle);
3170 if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE,
3171 sizeof(cp), &cp)) {
3172 bt_dev_err(hdev, "sending read key size failed");
3173 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3174 }
3175 }
3176
3177 hci_encrypt_cfm(conn, ev->status);
3178 }
3179 }
3180
3181 /* Get remote features */
3182 if (conn->type == ACL_LINK) {
3183 struct hci_cp_read_remote_features cp;
3184 cp.handle = ev->handle;
3185 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
3186 sizeof(cp), &cp);
3187
3188 hci_update_scan(hdev);
3189 }
3190
3191 /* Set packet type for incoming connection */
3192 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
3193 struct hci_cp_change_conn_ptype cp;
3194 cp.handle = ev->handle;
3195 cp.pkt_type = cpu_to_le16(conn->pkt_type);
3196 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
3197 &cp);
3198 }
3199 }
3200
3201 if (conn->type == ACL_LINK)
3202 hci_sco_setup(conn, ev->status);
3203
3204 done:
3205 if (status) {
3206 hci_conn_failed(conn, status);
3207 } else if (ev->link_type == SCO_LINK) {
3208 switch (conn->setting & SCO_AIRMODE_MASK) {
3209 case SCO_AIRMODE_CVSD:
3210 if (hdev->notify)
3211 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
3212 break;
3213 }
3214
3215 hci_connect_cfm(conn, status);
3216 }
3217
3218 unlock:
3219 hci_dev_unlock(hdev);
3220 }
3221
hci_reject_conn(struct hci_dev * hdev,bdaddr_t * bdaddr)3222 static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
3223 {
3224 struct hci_cp_reject_conn_req cp;
3225
3226 bacpy(&cp.bdaddr, bdaddr);
3227 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
3228 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
3229 }
3230
hci_conn_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3231 static void hci_conn_request_evt(struct hci_dev *hdev, void *data,
3232 struct sk_buff *skb)
3233 {
3234 struct hci_ev_conn_request *ev = data;
3235 int mask = hdev->link_mode;
3236 struct inquiry_entry *ie;
3237 struct hci_conn *conn;
3238 __u8 flags = 0;
3239
3240 bt_dev_dbg(hdev, "bdaddr %pMR type 0x%x", &ev->bdaddr, ev->link_type);
3241
3242 /* Reject incoming connection from device with same BD ADDR against
3243 * CVE-2020-26555
3244 */
3245 if (hdev && !bacmp(&hdev->bdaddr, &ev->bdaddr)) {
3246 bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
3247 &ev->bdaddr);
3248 hci_reject_conn(hdev, &ev->bdaddr);
3249 return;
3250 }
3251
3252 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
3253 &flags);
3254
3255 if (!(mask & HCI_LM_ACCEPT)) {
3256 hci_reject_conn(hdev, &ev->bdaddr);
3257 return;
3258 }
3259
3260 hci_dev_lock(hdev);
3261
3262 if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
3263 BDADDR_BREDR)) {
3264 hci_reject_conn(hdev, &ev->bdaddr);
3265 goto unlock;
3266 }
3267
3268 /* Require HCI_CONNECTABLE or an accept list entry to accept the
3269 * connection. These features are only touched through mgmt so
3270 * only do the checks if HCI_MGMT is set.
3271 */
3272 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
3273 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
3274 !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
3275 BDADDR_BREDR)) {
3276 hci_reject_conn(hdev, &ev->bdaddr);
3277 goto unlock;
3278 }
3279
3280 /* Connection accepted */
3281
3282 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3283 if (ie)
3284 memcpy(ie->data.dev_class, ev->dev_class, 3);
3285
3286 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
3287 &ev->bdaddr);
3288 if (!conn) {
3289 conn = hci_conn_add_unset(hdev, ev->link_type, &ev->bdaddr,
3290 HCI_ROLE_SLAVE);
3291 if (IS_ERR(conn)) {
3292 bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
3293 goto unlock;
3294 }
3295 }
3296
3297 memcpy(conn->dev_class, ev->dev_class, 3);
3298
3299 hci_dev_unlock(hdev);
3300
3301 if (ev->link_type == ACL_LINK ||
3302 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
3303 struct hci_cp_accept_conn_req cp;
3304 conn->state = BT_CONNECT;
3305
3306 bacpy(&cp.bdaddr, &ev->bdaddr);
3307
3308 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
3309 cp.role = 0x00; /* Become central */
3310 else
3311 cp.role = 0x01; /* Remain peripheral */
3312
3313 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
3314 } else if (!(flags & HCI_PROTO_DEFER)) {
3315 struct hci_cp_accept_sync_conn_req cp;
3316 conn->state = BT_CONNECT;
3317
3318 bacpy(&cp.bdaddr, &ev->bdaddr);
3319 cp.pkt_type = cpu_to_le16(conn->pkt_type);
3320
3321 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
3322 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
3323 cp.max_latency = cpu_to_le16(0xffff);
3324 cp.content_format = cpu_to_le16(hdev->voice_setting);
3325 cp.retrans_effort = 0xff;
3326
3327 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
3328 &cp);
3329 } else {
3330 conn->state = BT_CONNECT2;
3331 hci_connect_cfm(conn, 0);
3332 }
3333
3334 return;
3335 unlock:
3336 hci_dev_unlock(hdev);
3337 }
3338
hci_to_mgmt_reason(u8 err)3339 static u8 hci_to_mgmt_reason(u8 err)
3340 {
3341 switch (err) {
3342 case HCI_ERROR_CONNECTION_TIMEOUT:
3343 return MGMT_DEV_DISCONN_TIMEOUT;
3344 case HCI_ERROR_REMOTE_USER_TERM:
3345 case HCI_ERROR_REMOTE_LOW_RESOURCES:
3346 case HCI_ERROR_REMOTE_POWER_OFF:
3347 return MGMT_DEV_DISCONN_REMOTE;
3348 case HCI_ERROR_LOCAL_HOST_TERM:
3349 return MGMT_DEV_DISCONN_LOCAL_HOST;
3350 default:
3351 return MGMT_DEV_DISCONN_UNKNOWN;
3352 }
3353 }
3354
hci_disconn_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3355 static void hci_disconn_complete_evt(struct hci_dev *hdev, void *data,
3356 struct sk_buff *skb)
3357 {
3358 struct hci_ev_disconn_complete *ev = data;
3359 u8 reason;
3360 struct hci_conn_params *params;
3361 struct hci_conn *conn;
3362 bool mgmt_connected;
3363
3364 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3365
3366 hci_dev_lock(hdev);
3367
3368 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3369 if (!conn)
3370 goto unlock;
3371
3372 if (ev->status) {
3373 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
3374 conn->dst_type, ev->status);
3375 goto unlock;
3376 }
3377
3378 conn->state = BT_CLOSED;
3379
3380 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
3381
3382 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
3383 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
3384 else
3385 reason = hci_to_mgmt_reason(ev->reason);
3386
3387 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
3388 reason, mgmt_connected);
3389
3390 if (conn->type == ACL_LINK) {
3391 if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
3392 hci_remove_link_key(hdev, &conn->dst);
3393
3394 hci_update_scan(hdev);
3395 }
3396
3397 /* Re-enable passive scanning if disconnected device is marked
3398 * as auto-connectable.
3399 */
3400 if (conn->type == LE_LINK) {
3401 params = hci_conn_params_lookup(hdev, &conn->dst,
3402 conn->dst_type);
3403 if (params) {
3404 switch (params->auto_connect) {
3405 case HCI_AUTO_CONN_LINK_LOSS:
3406 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
3407 break;
3408 fallthrough;
3409
3410 case HCI_AUTO_CONN_DIRECT:
3411 case HCI_AUTO_CONN_ALWAYS:
3412 hci_pend_le_list_del_init(params);
3413 hci_pend_le_list_add(params,
3414 &hdev->pend_le_conns);
3415 hci_update_passive_scan(hdev);
3416 break;
3417
3418 default:
3419 break;
3420 }
3421 }
3422 }
3423
3424 hci_disconn_cfm(conn, ev->reason);
3425
3426 /* Re-enable advertising if necessary, since it might
3427 * have been disabled by the connection. From the
3428 * HCI_LE_Set_Advertise_Enable command description in
3429 * the core specification (v4.0):
3430 * "The Controller shall continue advertising until the Host
3431 * issues an LE_Set_Advertise_Enable command with
3432 * Advertising_Enable set to 0x00 (Advertising is disabled)
3433 * or until a connection is created or until the Advertising
3434 * is timed out due to Directed Advertising."
3435 */
3436 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
3437 hdev->cur_adv_instance = conn->adv_instance;
3438 hci_enable_advertising(hdev);
3439 }
3440
3441 hci_conn_del(conn);
3442
3443 unlock:
3444 hci_dev_unlock(hdev);
3445 }
3446
hci_auth_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3447 static void hci_auth_complete_evt(struct hci_dev *hdev, void *data,
3448 struct sk_buff *skb)
3449 {
3450 struct hci_ev_auth_complete *ev = data;
3451 struct hci_conn *conn;
3452
3453 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3454
3455 hci_dev_lock(hdev);
3456
3457 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3458 if (!conn)
3459 goto unlock;
3460
3461 if (!ev->status) {
3462 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3463 set_bit(HCI_CONN_AUTH, &conn->flags);
3464 conn->sec_level = conn->pending_sec_level;
3465 } else {
3466 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3467 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3468
3469 mgmt_auth_failed(conn, ev->status);
3470 }
3471
3472 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3473
3474 if (conn->state == BT_CONFIG) {
3475 if (!ev->status && hci_conn_ssp_enabled(conn)) {
3476 struct hci_cp_set_conn_encrypt cp;
3477 cp.handle = ev->handle;
3478 cp.encrypt = 0x01;
3479 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
3480 &cp);
3481 } else {
3482 conn->state = BT_CONNECTED;
3483 hci_connect_cfm(conn, ev->status);
3484 hci_conn_drop(conn);
3485 }
3486 } else {
3487 hci_auth_cfm(conn, ev->status);
3488
3489 hci_conn_hold(conn);
3490 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3491 hci_conn_drop(conn);
3492 }
3493
3494 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
3495 if (!ev->status) {
3496 struct hci_cp_set_conn_encrypt cp;
3497 cp.handle = ev->handle;
3498 cp.encrypt = 0x01;
3499 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
3500 &cp);
3501 } else {
3502 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3503 hci_encrypt_cfm(conn, ev->status);
3504 }
3505 }
3506
3507 unlock:
3508 hci_dev_unlock(hdev);
3509 }
3510
hci_remote_name_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3511 static void hci_remote_name_evt(struct hci_dev *hdev, void *data,
3512 struct sk_buff *skb)
3513 {
3514 struct hci_ev_remote_name *ev = data;
3515 struct hci_conn *conn;
3516
3517 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3518
3519 hci_dev_lock(hdev);
3520
3521 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3522
3523 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3524 goto check_auth;
3525
3526 if (ev->status == 0)
3527 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
3528 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
3529 else
3530 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3531
3532 check_auth:
3533 if (!conn)
3534 goto unlock;
3535
3536 if (!hci_outgoing_auth_needed(hdev, conn))
3537 goto unlock;
3538
3539 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
3540 struct hci_cp_auth_requested cp;
3541
3542 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3543
3544 cp.handle = __cpu_to_le16(conn->handle);
3545 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3546 }
3547
3548 unlock:
3549 hci_dev_unlock(hdev);
3550 }
3551
hci_encrypt_change_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3552 static void hci_encrypt_change_evt(struct hci_dev *hdev, void *data,
3553 struct sk_buff *skb)
3554 {
3555 struct hci_ev_encrypt_change *ev = data;
3556 struct hci_conn *conn;
3557
3558 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3559
3560 hci_dev_lock(hdev);
3561
3562 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3563 if (!conn)
3564 goto unlock;
3565
3566 if (!ev->status) {
3567 if (ev->encrypt) {
3568 /* Encryption implies authentication */
3569 set_bit(HCI_CONN_AUTH, &conn->flags);
3570 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
3571 conn->sec_level = conn->pending_sec_level;
3572
3573 /* P-256 authentication key implies FIPS */
3574 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
3575 set_bit(HCI_CONN_FIPS, &conn->flags);
3576
3577 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3578 conn->type == LE_LINK)
3579 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3580 } else {
3581 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
3582 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3583 }
3584 }
3585
3586 /* We should disregard the current RPA and generate a new one
3587 * whenever the encryption procedure fails.
3588 */
3589 if (ev->status && conn->type == LE_LINK) {
3590 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
3591 hci_adv_instances_set_rpa_expired(hdev, true);
3592 }
3593
3594 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3595
3596 /* Check link security requirements are met */
3597 if (!hci_conn_check_link_mode(conn))
3598 ev->status = HCI_ERROR_AUTH_FAILURE;
3599
3600 if (ev->status && conn->state == BT_CONNECTED) {
3601 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3602 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3603
3604 /* Notify upper layers so they can cleanup before
3605 * disconnecting.
3606 */
3607 hci_encrypt_cfm(conn, ev->status);
3608 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3609 hci_conn_drop(conn);
3610 goto unlock;
3611 }
3612
3613 /* Try reading the encryption key size for encrypted ACL links */
3614 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3615 struct hci_cp_read_enc_key_size cp;
3616
3617 /* Only send HCI_Read_Encryption_Key_Size if the
3618 * controller really supports it. If it doesn't, assume
3619 * the default size (16).
3620 */
3621 if (!read_key_size_capable(hdev)) {
3622 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3623 goto notify;
3624 }
3625
3626 cp.handle = cpu_to_le16(conn->handle);
3627 if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE,
3628 sizeof(cp), &cp)) {
3629 bt_dev_err(hdev, "sending read key size failed");
3630 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3631 goto notify;
3632 }
3633
3634 goto unlock;
3635 }
3636
3637 /* We skip the WRITE_AUTH_PAYLOAD_TIMEOUT for ATS2851 based controllers
3638 * to avoid unexpected SMP command errors when pairing.
3639 */
3640 if (test_bit(HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT,
3641 &hdev->quirks))
3642 goto notify;
3643
3644 /* Set the default Authenticated Payload Timeout after
3645 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3646 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3647 * sent when the link is active and Encryption is enabled, the conn
3648 * type can be either LE or ACL and controller must support LMP Ping.
3649 * Ensure for AES-CCM encryption as well.
3650 */
3651 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3652 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3653 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3654 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3655 struct hci_cp_write_auth_payload_to cp;
3656
3657 cp.handle = cpu_to_le16(conn->handle);
3658 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3659 if (hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3660 sizeof(cp), &cp))
3661 bt_dev_err(hdev, "write auth payload timeout failed");
3662 }
3663
3664 notify:
3665 hci_encrypt_cfm(conn, ev->status);
3666
3667 unlock:
3668 hci_dev_unlock(hdev);
3669 }
3670
hci_change_link_key_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3671 static void hci_change_link_key_complete_evt(struct hci_dev *hdev, void *data,
3672 struct sk_buff *skb)
3673 {
3674 struct hci_ev_change_link_key_complete *ev = data;
3675 struct hci_conn *conn;
3676
3677 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3678
3679 hci_dev_lock(hdev);
3680
3681 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3682 if (conn) {
3683 if (!ev->status)
3684 set_bit(HCI_CONN_SECURE, &conn->flags);
3685
3686 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3687
3688 hci_key_change_cfm(conn, ev->status);
3689 }
3690
3691 hci_dev_unlock(hdev);
3692 }
3693
hci_remote_features_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)3694 static void hci_remote_features_evt(struct hci_dev *hdev, void *data,
3695 struct sk_buff *skb)
3696 {
3697 struct hci_ev_remote_features *ev = data;
3698 struct hci_conn *conn;
3699
3700 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
3701
3702 hci_dev_lock(hdev);
3703
3704 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3705 if (!conn)
3706 goto unlock;
3707
3708 if (!ev->status)
3709 memcpy(conn->features[0], ev->features, 8);
3710
3711 if (conn->state != BT_CONFIG)
3712 goto unlock;
3713
3714 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3715 lmp_ext_feat_capable(conn)) {
3716 struct hci_cp_read_remote_ext_features cp;
3717 cp.handle = ev->handle;
3718 cp.page = 0x01;
3719 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
3720 sizeof(cp), &cp);
3721 goto unlock;
3722 }
3723
3724 if (!ev->status) {
3725 struct hci_cp_remote_name_req cp;
3726 memset(&cp, 0, sizeof(cp));
3727 bacpy(&cp.bdaddr, &conn->dst);
3728 cp.pscan_rep_mode = 0x02;
3729 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
3730 } else {
3731 mgmt_device_connected(hdev, conn, NULL, 0);
3732 }
3733
3734 if (!hci_outgoing_auth_needed(hdev, conn)) {
3735 conn->state = BT_CONNECTED;
3736 hci_connect_cfm(conn, ev->status);
3737 hci_conn_drop(conn);
3738 }
3739
3740 unlock:
3741 hci_dev_unlock(hdev);
3742 }
3743
handle_cmd_cnt_and_timer(struct hci_dev * hdev,u8 ncmd)3744 static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
3745 {
3746 cancel_delayed_work(&hdev->cmd_timer);
3747
3748 rcu_read_lock();
3749 if (!test_bit(HCI_RESET, &hdev->flags)) {
3750 if (ncmd) {
3751 cancel_delayed_work(&hdev->ncmd_timer);
3752 atomic_set(&hdev->cmd_cnt, 1);
3753 } else {
3754 if (!hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE))
3755 queue_delayed_work(hdev->workqueue, &hdev->ncmd_timer,
3756 HCI_NCMD_TIMEOUT);
3757 }
3758 }
3759 rcu_read_unlock();
3760 }
3761
hci_cc_le_read_buffer_size_v2(struct hci_dev * hdev,void * data,struct sk_buff * skb)3762 static u8 hci_cc_le_read_buffer_size_v2(struct hci_dev *hdev, void *data,
3763 struct sk_buff *skb)
3764 {
3765 struct hci_rp_le_read_buffer_size_v2 *rp = data;
3766
3767 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3768
3769 if (rp->status)
3770 return rp->status;
3771
3772 hdev->le_mtu = __le16_to_cpu(rp->acl_mtu);
3773 hdev->le_pkts = rp->acl_max_pkt;
3774 hdev->iso_mtu = __le16_to_cpu(rp->iso_mtu);
3775 hdev->iso_pkts = rp->iso_max_pkt;
3776
3777 hdev->le_cnt = hdev->le_pkts;
3778 hdev->iso_cnt = hdev->iso_pkts;
3779
3780 BT_DBG("%s acl mtu %d:%d iso mtu %d:%d", hdev->name, hdev->acl_mtu,
3781 hdev->acl_pkts, hdev->iso_mtu, hdev->iso_pkts);
3782
3783 if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
3784 return HCI_ERROR_INVALID_PARAMETERS;
3785
3786 return rp->status;
3787 }
3788
hci_unbound_cis_failed(struct hci_dev * hdev,u8 cig,u8 status)3789 static void hci_unbound_cis_failed(struct hci_dev *hdev, u8 cig, u8 status)
3790 {
3791 struct hci_conn *conn, *tmp;
3792
3793 lockdep_assert_held(&hdev->lock);
3794
3795 list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
3796 if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY) ||
3797 conn->state == BT_OPEN || conn->iso_qos.ucast.cig != cig)
3798 continue;
3799
3800 if (HCI_CONN_HANDLE_UNSET(conn->handle))
3801 hci_conn_failed(conn, status);
3802 }
3803 }
3804
hci_cc_le_set_cig_params(struct hci_dev * hdev,void * data,struct sk_buff * skb)3805 static u8 hci_cc_le_set_cig_params(struct hci_dev *hdev, void *data,
3806 struct sk_buff *skb)
3807 {
3808 struct hci_rp_le_set_cig_params *rp = data;
3809 struct hci_cp_le_set_cig_params *cp;
3810 struct hci_conn *conn;
3811 u8 status = rp->status;
3812 bool pending = false;
3813 int i;
3814
3815 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3816
3817 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_CIG_PARAMS);
3818 if (!rp->status && (!cp || rp->num_handles != cp->num_cis ||
3819 rp->cig_id != cp->cig_id)) {
3820 bt_dev_err(hdev, "unexpected Set CIG Parameters response data");
3821 status = HCI_ERROR_UNSPECIFIED;
3822 }
3823
3824 hci_dev_lock(hdev);
3825
3826 /* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 4, Part E page 2554
3827 *
3828 * If the Status return parameter is non-zero, then the state of the CIG
3829 * and its CIS configurations shall not be changed by the command. If
3830 * the CIG did not already exist, it shall not be created.
3831 */
3832 if (status) {
3833 /* Keep current configuration, fail only the unbound CIS */
3834 hci_unbound_cis_failed(hdev, rp->cig_id, status);
3835 goto unlock;
3836 }
3837
3838 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2553
3839 *
3840 * If the Status return parameter is zero, then the Controller shall
3841 * set the Connection_Handle arrayed return parameter to the connection
3842 * handle(s) corresponding to the CIS configurations specified in
3843 * the CIS_IDs command parameter, in the same order.
3844 */
3845 for (i = 0; i < rp->num_handles; ++i) {
3846 conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, rp->cig_id,
3847 cp->cis[i].cis_id);
3848 if (!conn || !bacmp(&conn->dst, BDADDR_ANY))
3849 continue;
3850
3851 if (conn->state != BT_BOUND && conn->state != BT_CONNECT)
3852 continue;
3853
3854 if (hci_conn_set_handle(conn, __le16_to_cpu(rp->handle[i])))
3855 continue;
3856
3857 if (conn->state == BT_CONNECT)
3858 pending = true;
3859 }
3860
3861 unlock:
3862 if (pending)
3863 hci_le_create_cis_pending(hdev);
3864
3865 hci_dev_unlock(hdev);
3866
3867 return rp->status;
3868 }
3869
hci_cc_le_setup_iso_path(struct hci_dev * hdev,void * data,struct sk_buff * skb)3870 static u8 hci_cc_le_setup_iso_path(struct hci_dev *hdev, void *data,
3871 struct sk_buff *skb)
3872 {
3873 struct hci_rp_le_setup_iso_path *rp = data;
3874 struct hci_cp_le_setup_iso_path *cp;
3875 struct hci_conn *conn;
3876
3877 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3878
3879 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SETUP_ISO_PATH);
3880 if (!cp)
3881 return rp->status;
3882
3883 hci_dev_lock(hdev);
3884
3885 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
3886 if (!conn)
3887 goto unlock;
3888
3889 if (rp->status) {
3890 hci_connect_cfm(conn, rp->status);
3891 hci_conn_del(conn);
3892 goto unlock;
3893 }
3894
3895 switch (cp->direction) {
3896 /* Input (Host to Controller) */
3897 case 0x00:
3898 /* Only confirm connection if output only */
3899 if (conn->iso_qos.ucast.out.sdu && !conn->iso_qos.ucast.in.sdu)
3900 hci_connect_cfm(conn, rp->status);
3901 break;
3902 /* Output (Controller to Host) */
3903 case 0x01:
3904 /* Confirm connection since conn->iso_qos is always configured
3905 * last.
3906 */
3907 hci_connect_cfm(conn, rp->status);
3908
3909 /* Notify device connected in case it is a BIG Sync */
3910 if (!rp->status && test_bit(HCI_CONN_BIG_SYNC, &conn->flags))
3911 mgmt_device_connected(hdev, conn, NULL, 0);
3912
3913 break;
3914 }
3915
3916 unlock:
3917 hci_dev_unlock(hdev);
3918 return rp->status;
3919 }
3920
hci_cs_le_create_big(struct hci_dev * hdev,u8 status)3921 static void hci_cs_le_create_big(struct hci_dev *hdev, u8 status)
3922 {
3923 bt_dev_dbg(hdev, "status 0x%2.2x", status);
3924 }
3925
hci_cc_set_per_adv_param(struct hci_dev * hdev,void * data,struct sk_buff * skb)3926 static u8 hci_cc_set_per_adv_param(struct hci_dev *hdev, void *data,
3927 struct sk_buff *skb)
3928 {
3929 struct hci_ev_status *rp = data;
3930 struct hci_cp_le_set_per_adv_params *cp;
3931
3932 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3933
3934 if (rp->status)
3935 return rp->status;
3936
3937 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS);
3938 if (!cp)
3939 return rp->status;
3940
3941 /* TODO: set the conn state */
3942 return rp->status;
3943 }
3944
hci_cc_le_set_per_adv_enable(struct hci_dev * hdev,void * data,struct sk_buff * skb)3945 static u8 hci_cc_le_set_per_adv_enable(struct hci_dev *hdev, void *data,
3946 struct sk_buff *skb)
3947 {
3948 struct hci_ev_status *rp = data;
3949 struct hci_cp_le_set_per_adv_enable *cp;
3950 struct adv_info *adv = NULL, *n;
3951 u8 per_adv_cnt = 0;
3952
3953 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
3954
3955 if (rp->status)
3956 return rp->status;
3957
3958 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE);
3959 if (!cp)
3960 return rp->status;
3961
3962 hci_dev_lock(hdev);
3963
3964 adv = hci_find_adv_instance(hdev, cp->handle);
3965
3966 if (cp->enable) {
3967 hci_dev_set_flag(hdev, HCI_LE_PER_ADV);
3968
3969 if (adv)
3970 adv->enabled = true;
3971 } else {
3972 /* If just one instance was disabled check if there are
3973 * any other instance enabled before clearing HCI_LE_PER_ADV.
3974 * The current periodic adv instance will be marked as
3975 * disabled once extended advertising is also disabled.
3976 */
3977 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
3978 list) {
3979 if (adv->periodic && adv->enabled)
3980 per_adv_cnt++;
3981 }
3982
3983 if (per_adv_cnt > 1)
3984 goto unlock;
3985
3986 hci_dev_clear_flag(hdev, HCI_LE_PER_ADV);
3987 }
3988
3989 unlock:
3990 hci_dev_unlock(hdev);
3991
3992 return rp->status;
3993 }
3994
3995 #define HCI_CC_VL(_op, _func, _min, _max) \
3996 { \
3997 .op = _op, \
3998 .func = _func, \
3999 .min_len = _min, \
4000 .max_len = _max, \
4001 }
4002
4003 #define HCI_CC(_op, _func, _len) \
4004 HCI_CC_VL(_op, _func, _len, _len)
4005
4006 #define HCI_CC_STATUS(_op, _func) \
4007 HCI_CC(_op, _func, sizeof(struct hci_ev_status))
4008
4009 static const struct hci_cc {
4010 u16 op;
4011 u8 (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
4012 u16 min_len;
4013 u16 max_len;
4014 } hci_cc_table[] = {
4015 HCI_CC_STATUS(HCI_OP_INQUIRY_CANCEL, hci_cc_inquiry_cancel),
4016 HCI_CC_STATUS(HCI_OP_PERIODIC_INQ, hci_cc_periodic_inq),
4017 HCI_CC_STATUS(HCI_OP_EXIT_PERIODIC_INQ, hci_cc_exit_periodic_inq),
4018 HCI_CC(HCI_OP_REMOTE_NAME_REQ_CANCEL, hci_cc_remote_name_req_cancel,
4019 sizeof(struct hci_rp_remote_name_req_cancel)),
4020 HCI_CC(HCI_OP_ROLE_DISCOVERY, hci_cc_role_discovery,
4021 sizeof(struct hci_rp_role_discovery)),
4022 HCI_CC(HCI_OP_READ_LINK_POLICY, hci_cc_read_link_policy,
4023 sizeof(struct hci_rp_read_link_policy)),
4024 HCI_CC(HCI_OP_WRITE_LINK_POLICY, hci_cc_write_link_policy,
4025 sizeof(struct hci_rp_write_link_policy)),
4026 HCI_CC(HCI_OP_READ_DEF_LINK_POLICY, hci_cc_read_def_link_policy,
4027 sizeof(struct hci_rp_read_def_link_policy)),
4028 HCI_CC_STATUS(HCI_OP_WRITE_DEF_LINK_POLICY,
4029 hci_cc_write_def_link_policy),
4030 HCI_CC_STATUS(HCI_OP_RESET, hci_cc_reset),
4031 HCI_CC(HCI_OP_READ_STORED_LINK_KEY, hci_cc_read_stored_link_key,
4032 sizeof(struct hci_rp_read_stored_link_key)),
4033 HCI_CC(HCI_OP_DELETE_STORED_LINK_KEY, hci_cc_delete_stored_link_key,
4034 sizeof(struct hci_rp_delete_stored_link_key)),
4035 HCI_CC_STATUS(HCI_OP_WRITE_LOCAL_NAME, hci_cc_write_local_name),
4036 HCI_CC(HCI_OP_READ_LOCAL_NAME, hci_cc_read_local_name,
4037 sizeof(struct hci_rp_read_local_name)),
4038 HCI_CC_STATUS(HCI_OP_WRITE_AUTH_ENABLE, hci_cc_write_auth_enable),
4039 HCI_CC_STATUS(HCI_OP_WRITE_ENCRYPT_MODE, hci_cc_write_encrypt_mode),
4040 HCI_CC_STATUS(HCI_OP_WRITE_SCAN_ENABLE, hci_cc_write_scan_enable),
4041 HCI_CC_STATUS(HCI_OP_SET_EVENT_FLT, hci_cc_set_event_filter),
4042 HCI_CC(HCI_OP_READ_CLASS_OF_DEV, hci_cc_read_class_of_dev,
4043 sizeof(struct hci_rp_read_class_of_dev)),
4044 HCI_CC_STATUS(HCI_OP_WRITE_CLASS_OF_DEV, hci_cc_write_class_of_dev),
4045 HCI_CC(HCI_OP_READ_VOICE_SETTING, hci_cc_read_voice_setting,
4046 sizeof(struct hci_rp_read_voice_setting)),
4047 HCI_CC_STATUS(HCI_OP_WRITE_VOICE_SETTING, hci_cc_write_voice_setting),
4048 HCI_CC(HCI_OP_READ_NUM_SUPPORTED_IAC, hci_cc_read_num_supported_iac,
4049 sizeof(struct hci_rp_read_num_supported_iac)),
4050 HCI_CC_STATUS(HCI_OP_WRITE_SSP_MODE, hci_cc_write_ssp_mode),
4051 HCI_CC_STATUS(HCI_OP_WRITE_SC_SUPPORT, hci_cc_write_sc_support),
4052 HCI_CC(HCI_OP_READ_AUTH_PAYLOAD_TO, hci_cc_read_auth_payload_timeout,
4053 sizeof(struct hci_rp_read_auth_payload_to)),
4054 HCI_CC(HCI_OP_WRITE_AUTH_PAYLOAD_TO, hci_cc_write_auth_payload_timeout,
4055 sizeof(struct hci_rp_write_auth_payload_to)),
4056 HCI_CC(HCI_OP_READ_LOCAL_VERSION, hci_cc_read_local_version,
4057 sizeof(struct hci_rp_read_local_version)),
4058 HCI_CC(HCI_OP_READ_LOCAL_COMMANDS, hci_cc_read_local_commands,
4059 sizeof(struct hci_rp_read_local_commands)),
4060 HCI_CC(HCI_OP_READ_LOCAL_FEATURES, hci_cc_read_local_features,
4061 sizeof(struct hci_rp_read_local_features)),
4062 HCI_CC(HCI_OP_READ_LOCAL_EXT_FEATURES, hci_cc_read_local_ext_features,
4063 sizeof(struct hci_rp_read_local_ext_features)),
4064 HCI_CC(HCI_OP_READ_BUFFER_SIZE, hci_cc_read_buffer_size,
4065 sizeof(struct hci_rp_read_buffer_size)),
4066 HCI_CC(HCI_OP_READ_BD_ADDR, hci_cc_read_bd_addr,
4067 sizeof(struct hci_rp_read_bd_addr)),
4068 HCI_CC(HCI_OP_READ_LOCAL_PAIRING_OPTS, hci_cc_read_local_pairing_opts,
4069 sizeof(struct hci_rp_read_local_pairing_opts)),
4070 HCI_CC(HCI_OP_READ_PAGE_SCAN_ACTIVITY, hci_cc_read_page_scan_activity,
4071 sizeof(struct hci_rp_read_page_scan_activity)),
4072 HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
4073 hci_cc_write_page_scan_activity),
4074 HCI_CC(HCI_OP_READ_PAGE_SCAN_TYPE, hci_cc_read_page_scan_type,
4075 sizeof(struct hci_rp_read_page_scan_type)),
4076 HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_TYPE, hci_cc_write_page_scan_type),
4077 HCI_CC(HCI_OP_READ_CLOCK, hci_cc_read_clock,
4078 sizeof(struct hci_rp_read_clock)),
4079 HCI_CC(HCI_OP_READ_ENC_KEY_SIZE, hci_cc_read_enc_key_size,
4080 sizeof(struct hci_rp_read_enc_key_size)),
4081 HCI_CC(HCI_OP_READ_INQ_RSP_TX_POWER, hci_cc_read_inq_rsp_tx_power,
4082 sizeof(struct hci_rp_read_inq_rsp_tx_power)),
4083 HCI_CC(HCI_OP_READ_DEF_ERR_DATA_REPORTING,
4084 hci_cc_read_def_err_data_reporting,
4085 sizeof(struct hci_rp_read_def_err_data_reporting)),
4086 HCI_CC_STATUS(HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
4087 hci_cc_write_def_err_data_reporting),
4088 HCI_CC(HCI_OP_PIN_CODE_REPLY, hci_cc_pin_code_reply,
4089 sizeof(struct hci_rp_pin_code_reply)),
4090 HCI_CC(HCI_OP_PIN_CODE_NEG_REPLY, hci_cc_pin_code_neg_reply,
4091 sizeof(struct hci_rp_pin_code_neg_reply)),
4092 HCI_CC(HCI_OP_READ_LOCAL_OOB_DATA, hci_cc_read_local_oob_data,
4093 sizeof(struct hci_rp_read_local_oob_data)),
4094 HCI_CC(HCI_OP_READ_LOCAL_OOB_EXT_DATA, hci_cc_read_local_oob_ext_data,
4095 sizeof(struct hci_rp_read_local_oob_ext_data)),
4096 HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE, hci_cc_le_read_buffer_size,
4097 sizeof(struct hci_rp_le_read_buffer_size)),
4098 HCI_CC(HCI_OP_LE_READ_LOCAL_FEATURES, hci_cc_le_read_local_features,
4099 sizeof(struct hci_rp_le_read_local_features)),
4100 HCI_CC(HCI_OP_LE_READ_ADV_TX_POWER, hci_cc_le_read_adv_tx_power,
4101 sizeof(struct hci_rp_le_read_adv_tx_power)),
4102 HCI_CC(HCI_OP_USER_CONFIRM_REPLY, hci_cc_user_confirm_reply,
4103 sizeof(struct hci_rp_user_confirm_reply)),
4104 HCI_CC(HCI_OP_USER_CONFIRM_NEG_REPLY, hci_cc_user_confirm_neg_reply,
4105 sizeof(struct hci_rp_user_confirm_reply)),
4106 HCI_CC(HCI_OP_USER_PASSKEY_REPLY, hci_cc_user_passkey_reply,
4107 sizeof(struct hci_rp_user_confirm_reply)),
4108 HCI_CC(HCI_OP_USER_PASSKEY_NEG_REPLY, hci_cc_user_passkey_neg_reply,
4109 sizeof(struct hci_rp_user_confirm_reply)),
4110 HCI_CC_STATUS(HCI_OP_LE_SET_RANDOM_ADDR, hci_cc_le_set_random_addr),
4111 HCI_CC_STATUS(HCI_OP_LE_SET_ADV_ENABLE, hci_cc_le_set_adv_enable),
4112 HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_PARAM, hci_cc_le_set_scan_param),
4113 HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_ENABLE, hci_cc_le_set_scan_enable),
4114 HCI_CC(HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
4115 hci_cc_le_read_accept_list_size,
4116 sizeof(struct hci_rp_le_read_accept_list_size)),
4117 HCI_CC_STATUS(HCI_OP_LE_CLEAR_ACCEPT_LIST, hci_cc_le_clear_accept_list),
4118 HCI_CC_STATUS(HCI_OP_LE_ADD_TO_ACCEPT_LIST,
4119 hci_cc_le_add_to_accept_list),
4120 HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
4121 hci_cc_le_del_from_accept_list),
4122 HCI_CC(HCI_OP_LE_READ_SUPPORTED_STATES, hci_cc_le_read_supported_states,
4123 sizeof(struct hci_rp_le_read_supported_states)),
4124 HCI_CC(HCI_OP_LE_READ_DEF_DATA_LEN, hci_cc_le_read_def_data_len,
4125 sizeof(struct hci_rp_le_read_def_data_len)),
4126 HCI_CC_STATUS(HCI_OP_LE_WRITE_DEF_DATA_LEN,
4127 hci_cc_le_write_def_data_len),
4128 HCI_CC_STATUS(HCI_OP_LE_ADD_TO_RESOLV_LIST,
4129 hci_cc_le_add_to_resolv_list),
4130 HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_RESOLV_LIST,
4131 hci_cc_le_del_from_resolv_list),
4132 HCI_CC_STATUS(HCI_OP_LE_CLEAR_RESOLV_LIST,
4133 hci_cc_le_clear_resolv_list),
4134 HCI_CC(HCI_OP_LE_READ_RESOLV_LIST_SIZE, hci_cc_le_read_resolv_list_size,
4135 sizeof(struct hci_rp_le_read_resolv_list_size)),
4136 HCI_CC_STATUS(HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
4137 hci_cc_le_set_addr_resolution_enable),
4138 HCI_CC(HCI_OP_LE_READ_MAX_DATA_LEN, hci_cc_le_read_max_data_len,
4139 sizeof(struct hci_rp_le_read_max_data_len)),
4140 HCI_CC_STATUS(HCI_OP_WRITE_LE_HOST_SUPPORTED,
4141 hci_cc_write_le_host_supported),
4142 HCI_CC_STATUS(HCI_OP_LE_SET_ADV_PARAM, hci_cc_set_adv_param),
4143 HCI_CC(HCI_OP_READ_RSSI, hci_cc_read_rssi,
4144 sizeof(struct hci_rp_read_rssi)),
4145 HCI_CC(HCI_OP_READ_TX_POWER, hci_cc_read_tx_power,
4146 sizeof(struct hci_rp_read_tx_power)),
4147 HCI_CC_STATUS(HCI_OP_WRITE_SSP_DEBUG_MODE, hci_cc_write_ssp_debug_mode),
4148 HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_PARAMS,
4149 hci_cc_le_set_ext_scan_param),
4150 HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_ENABLE,
4151 hci_cc_le_set_ext_scan_enable),
4152 HCI_CC_STATUS(HCI_OP_LE_SET_DEFAULT_PHY, hci_cc_le_set_default_phy),
4153 HCI_CC(HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
4154 hci_cc_le_read_num_adv_sets,
4155 sizeof(struct hci_rp_le_read_num_supported_adv_sets)),
4156 HCI_CC(HCI_OP_LE_SET_EXT_ADV_PARAMS, hci_cc_set_ext_adv_param,
4157 sizeof(struct hci_rp_le_set_ext_adv_params)),
4158 HCI_CC_STATUS(HCI_OP_LE_SET_EXT_ADV_ENABLE,
4159 hci_cc_le_set_ext_adv_enable),
4160 HCI_CC_STATUS(HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
4161 hci_cc_le_set_adv_set_random_addr),
4162 HCI_CC_STATUS(HCI_OP_LE_REMOVE_ADV_SET, hci_cc_le_remove_adv_set),
4163 HCI_CC_STATUS(HCI_OP_LE_CLEAR_ADV_SETS, hci_cc_le_clear_adv_sets),
4164 HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_PARAMS, hci_cc_set_per_adv_param),
4165 HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_ENABLE,
4166 hci_cc_le_set_per_adv_enable),
4167 HCI_CC(HCI_OP_LE_READ_TRANSMIT_POWER, hci_cc_le_read_transmit_power,
4168 sizeof(struct hci_rp_le_read_transmit_power)),
4169 HCI_CC_STATUS(HCI_OP_LE_SET_PRIVACY_MODE, hci_cc_le_set_privacy_mode),
4170 HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE_V2, hci_cc_le_read_buffer_size_v2,
4171 sizeof(struct hci_rp_le_read_buffer_size_v2)),
4172 HCI_CC_VL(HCI_OP_LE_SET_CIG_PARAMS, hci_cc_le_set_cig_params,
4173 sizeof(struct hci_rp_le_set_cig_params), HCI_MAX_EVENT_SIZE),
4174 HCI_CC(HCI_OP_LE_SETUP_ISO_PATH, hci_cc_le_setup_iso_path,
4175 sizeof(struct hci_rp_le_setup_iso_path)),
4176 };
4177
hci_cc_func(struct hci_dev * hdev,const struct hci_cc * cc,struct sk_buff * skb)4178 static u8 hci_cc_func(struct hci_dev *hdev, const struct hci_cc *cc,
4179 struct sk_buff *skb)
4180 {
4181 void *data;
4182
4183 if (skb->len < cc->min_len) {
4184 bt_dev_err(hdev, "unexpected cc 0x%4.4x length: %u < %u",
4185 cc->op, skb->len, cc->min_len);
4186 return HCI_ERROR_UNSPECIFIED;
4187 }
4188
4189 /* Just warn if the length is over max_len size it still be possible to
4190 * partially parse the cc so leave to callback to decide if that is
4191 * acceptable.
4192 */
4193 if (skb->len > cc->max_len)
4194 bt_dev_warn(hdev, "unexpected cc 0x%4.4x length: %u > %u",
4195 cc->op, skb->len, cc->max_len);
4196
4197 data = hci_cc_skb_pull(hdev, skb, cc->op, cc->min_len);
4198 if (!data)
4199 return HCI_ERROR_UNSPECIFIED;
4200
4201 return cc->func(hdev, data, skb);
4202 }
4203
hci_cmd_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb,u16 * opcode,u8 * status,hci_req_complete_t * req_complete,hci_req_complete_skb_t * req_complete_skb)4204 static void hci_cmd_complete_evt(struct hci_dev *hdev, void *data,
4205 struct sk_buff *skb, u16 *opcode, u8 *status,
4206 hci_req_complete_t *req_complete,
4207 hci_req_complete_skb_t *req_complete_skb)
4208 {
4209 struct hci_ev_cmd_complete *ev = data;
4210 int i;
4211
4212 *opcode = __le16_to_cpu(ev->opcode);
4213
4214 bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
4215
4216 for (i = 0; i < ARRAY_SIZE(hci_cc_table); i++) {
4217 if (hci_cc_table[i].op == *opcode) {
4218 *status = hci_cc_func(hdev, &hci_cc_table[i], skb);
4219 break;
4220 }
4221 }
4222
4223 if (i == ARRAY_SIZE(hci_cc_table)) {
4224 /* Unknown opcode, assume byte 0 contains the status, so
4225 * that e.g. __hci_cmd_sync() properly returns errors
4226 * for vendor specific commands send by HCI drivers.
4227 * If a vendor doesn't actually follow this convention we may
4228 * need to introduce a vendor CC table in order to properly set
4229 * the status.
4230 */
4231 *status = skb->data[0];
4232 }
4233
4234 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
4235
4236 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
4237 req_complete_skb);
4238
4239 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4240 bt_dev_err(hdev,
4241 "unexpected event for opcode 0x%4.4x", *opcode);
4242 return;
4243 }
4244
4245 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4246 queue_work(hdev->workqueue, &hdev->cmd_work);
4247 }
4248
hci_cs_le_create_cis(struct hci_dev * hdev,u8 status)4249 static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
4250 {
4251 struct hci_cp_le_create_cis *cp;
4252 bool pending = false;
4253 int i;
4254
4255 bt_dev_dbg(hdev, "status 0x%2.2x", status);
4256
4257 if (!status)
4258 return;
4259
4260 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CIS);
4261 if (!cp)
4262 return;
4263
4264 hci_dev_lock(hdev);
4265
4266 /* Remove connection if command failed */
4267 for (i = 0; i < cp->num_cis; i++) {
4268 struct hci_conn *conn;
4269 u16 handle;
4270
4271 handle = __le16_to_cpu(cp->cis[i].cis_handle);
4272
4273 conn = hci_conn_hash_lookup_handle(hdev, handle);
4274 if (conn) {
4275 if (test_and_clear_bit(HCI_CONN_CREATE_CIS,
4276 &conn->flags))
4277 pending = true;
4278 conn->state = BT_CLOSED;
4279 hci_connect_cfm(conn, status);
4280 hci_conn_del(conn);
4281 }
4282 }
4283 cp->num_cis = 0;
4284
4285 if (pending)
4286 hci_le_create_cis_pending(hdev);
4287
4288 hci_dev_unlock(hdev);
4289 }
4290
4291 #define HCI_CS(_op, _func) \
4292 { \
4293 .op = _op, \
4294 .func = _func, \
4295 }
4296
4297 static const struct hci_cs {
4298 u16 op;
4299 void (*func)(struct hci_dev *hdev, __u8 status);
4300 } hci_cs_table[] = {
4301 HCI_CS(HCI_OP_INQUIRY, hci_cs_inquiry),
4302 HCI_CS(HCI_OP_CREATE_CONN, hci_cs_create_conn),
4303 HCI_CS(HCI_OP_DISCONNECT, hci_cs_disconnect),
4304 HCI_CS(HCI_OP_ADD_SCO, hci_cs_add_sco),
4305 HCI_CS(HCI_OP_AUTH_REQUESTED, hci_cs_auth_requested),
4306 HCI_CS(HCI_OP_SET_CONN_ENCRYPT, hci_cs_set_conn_encrypt),
4307 HCI_CS(HCI_OP_REMOTE_NAME_REQ, hci_cs_remote_name_req),
4308 HCI_CS(HCI_OP_READ_REMOTE_FEATURES, hci_cs_read_remote_features),
4309 HCI_CS(HCI_OP_READ_REMOTE_EXT_FEATURES,
4310 hci_cs_read_remote_ext_features),
4311 HCI_CS(HCI_OP_SETUP_SYNC_CONN, hci_cs_setup_sync_conn),
4312 HCI_CS(HCI_OP_ENHANCED_SETUP_SYNC_CONN,
4313 hci_cs_enhanced_setup_sync_conn),
4314 HCI_CS(HCI_OP_SNIFF_MODE, hci_cs_sniff_mode),
4315 HCI_CS(HCI_OP_EXIT_SNIFF_MODE, hci_cs_exit_sniff_mode),
4316 HCI_CS(HCI_OP_SWITCH_ROLE, hci_cs_switch_role),
4317 HCI_CS(HCI_OP_LE_CREATE_CONN, hci_cs_le_create_conn),
4318 HCI_CS(HCI_OP_LE_READ_REMOTE_FEATURES, hci_cs_le_read_remote_features),
4319 HCI_CS(HCI_OP_LE_START_ENC, hci_cs_le_start_enc),
4320 HCI_CS(HCI_OP_LE_EXT_CREATE_CONN, hci_cs_le_ext_create_conn),
4321 HCI_CS(HCI_OP_LE_CREATE_CIS, hci_cs_le_create_cis),
4322 HCI_CS(HCI_OP_LE_CREATE_BIG, hci_cs_le_create_big),
4323 };
4324
hci_cmd_status_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb,u16 * opcode,u8 * status,hci_req_complete_t * req_complete,hci_req_complete_skb_t * req_complete_skb)4325 static void hci_cmd_status_evt(struct hci_dev *hdev, void *data,
4326 struct sk_buff *skb, u16 *opcode, u8 *status,
4327 hci_req_complete_t *req_complete,
4328 hci_req_complete_skb_t *req_complete_skb)
4329 {
4330 struct hci_ev_cmd_status *ev = data;
4331 int i;
4332
4333 *opcode = __le16_to_cpu(ev->opcode);
4334 *status = ev->status;
4335
4336 bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
4337
4338 for (i = 0; i < ARRAY_SIZE(hci_cs_table); i++) {
4339 if (hci_cs_table[i].op == *opcode) {
4340 hci_cs_table[i].func(hdev, ev->status);
4341 break;
4342 }
4343 }
4344
4345 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
4346
4347 /* Indicate request completion if the command failed. Also, if
4348 * we're not waiting for a special event and we get a success
4349 * command status we should try to flag the request as completed
4350 * (since for this kind of commands there will not be a command
4351 * complete event).
4352 */
4353 if (ev->status || (hdev->req_skb && !hci_skb_event(hdev->req_skb))) {
4354 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
4355 req_complete_skb);
4356 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4357 bt_dev_err(hdev, "unexpected event for opcode 0x%4.4x",
4358 *opcode);
4359 return;
4360 }
4361 }
4362
4363 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4364 queue_work(hdev->workqueue, &hdev->cmd_work);
4365 }
4366
hci_hardware_error_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4367 static void hci_hardware_error_evt(struct hci_dev *hdev, void *data,
4368 struct sk_buff *skb)
4369 {
4370 struct hci_ev_hardware_error *ev = data;
4371
4372 bt_dev_dbg(hdev, "code 0x%2.2x", ev->code);
4373
4374 hdev->hw_error_code = ev->code;
4375
4376 queue_work(hdev->req_workqueue, &hdev->error_reset);
4377 }
4378
hci_role_change_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4379 static void hci_role_change_evt(struct hci_dev *hdev, void *data,
4380 struct sk_buff *skb)
4381 {
4382 struct hci_ev_role_change *ev = data;
4383 struct hci_conn *conn;
4384
4385 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4386
4387 hci_dev_lock(hdev);
4388
4389 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4390 if (conn) {
4391 if (!ev->status)
4392 conn->role = ev->role;
4393
4394 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
4395
4396 hci_role_switch_cfm(conn, ev->status, ev->role);
4397 }
4398
4399 hci_dev_unlock(hdev);
4400 }
4401
hci_num_comp_pkts_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4402 static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
4403 struct sk_buff *skb)
4404 {
4405 struct hci_ev_num_comp_pkts *ev = data;
4406 int i;
4407
4408 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_PKTS,
4409 flex_array_size(ev, handles, ev->num)))
4410 return;
4411
4412 bt_dev_dbg(hdev, "num %d", ev->num);
4413
4414 for (i = 0; i < ev->num; i++) {
4415 struct hci_comp_pkts_info *info = &ev->handles[i];
4416 struct hci_conn *conn;
4417 __u16 handle, count;
4418 unsigned int i;
4419
4420 handle = __le16_to_cpu(info->handle);
4421 count = __le16_to_cpu(info->count);
4422
4423 conn = hci_conn_hash_lookup_handle(hdev, handle);
4424 if (!conn)
4425 continue;
4426
4427 conn->sent -= count;
4428
4429 for (i = 0; i < count; ++i)
4430 hci_conn_tx_dequeue(conn);
4431
4432 switch (conn->type) {
4433 case ACL_LINK:
4434 hdev->acl_cnt += count;
4435 if (hdev->acl_cnt > hdev->acl_pkts)
4436 hdev->acl_cnt = hdev->acl_pkts;
4437 break;
4438
4439 case LE_LINK:
4440 if (hdev->le_pkts) {
4441 hdev->le_cnt += count;
4442 if (hdev->le_cnt > hdev->le_pkts)
4443 hdev->le_cnt = hdev->le_pkts;
4444 } else {
4445 hdev->acl_cnt += count;
4446 if (hdev->acl_cnt > hdev->acl_pkts)
4447 hdev->acl_cnt = hdev->acl_pkts;
4448 }
4449 break;
4450
4451 case SCO_LINK:
4452 case ESCO_LINK:
4453 hdev->sco_cnt += count;
4454 if (hdev->sco_cnt > hdev->sco_pkts)
4455 hdev->sco_cnt = hdev->sco_pkts;
4456
4457 break;
4458
4459 case ISO_LINK:
4460 if (hdev->iso_pkts) {
4461 hdev->iso_cnt += count;
4462 if (hdev->iso_cnt > hdev->iso_pkts)
4463 hdev->iso_cnt = hdev->iso_pkts;
4464 } else if (hdev->le_pkts) {
4465 hdev->le_cnt += count;
4466 if (hdev->le_cnt > hdev->le_pkts)
4467 hdev->le_cnt = hdev->le_pkts;
4468 } else {
4469 hdev->acl_cnt += count;
4470 if (hdev->acl_cnt > hdev->acl_pkts)
4471 hdev->acl_cnt = hdev->acl_pkts;
4472 }
4473 break;
4474
4475 default:
4476 bt_dev_err(hdev, "unknown type %d conn %p",
4477 conn->type, conn);
4478 break;
4479 }
4480 }
4481
4482 queue_work(hdev->workqueue, &hdev->tx_work);
4483 }
4484
hci_mode_change_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4485 static void hci_mode_change_evt(struct hci_dev *hdev, void *data,
4486 struct sk_buff *skb)
4487 {
4488 struct hci_ev_mode_change *ev = data;
4489 struct hci_conn *conn;
4490
4491 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4492
4493 hci_dev_lock(hdev);
4494
4495 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4496 if (conn) {
4497 conn->mode = ev->mode;
4498
4499 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
4500 &conn->flags)) {
4501 if (conn->mode == HCI_CM_ACTIVE)
4502 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
4503 else
4504 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
4505 }
4506
4507 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
4508 hci_sco_setup(conn, ev->status);
4509 }
4510
4511 hci_dev_unlock(hdev);
4512 }
4513
hci_pin_code_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4514 static void hci_pin_code_request_evt(struct hci_dev *hdev, void *data,
4515 struct sk_buff *skb)
4516 {
4517 struct hci_ev_pin_code_req *ev = data;
4518 struct hci_conn *conn;
4519
4520 bt_dev_dbg(hdev, "");
4521
4522 hci_dev_lock(hdev);
4523
4524 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4525 if (!conn)
4526 goto unlock;
4527
4528 if (conn->state == BT_CONNECTED) {
4529 hci_conn_hold(conn);
4530 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
4531 hci_conn_drop(conn);
4532 }
4533
4534 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
4535 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
4536 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
4537 sizeof(ev->bdaddr), &ev->bdaddr);
4538 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
4539 u8 secure;
4540
4541 if (conn->pending_sec_level == BT_SECURITY_HIGH)
4542 secure = 1;
4543 else
4544 secure = 0;
4545
4546 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
4547 }
4548
4549 unlock:
4550 hci_dev_unlock(hdev);
4551 }
4552
conn_set_key(struct hci_conn * conn,u8 key_type,u8 pin_len)4553 static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4554 {
4555 if (key_type == HCI_LK_CHANGED_COMBINATION)
4556 return;
4557
4558 conn->pin_length = pin_len;
4559 conn->key_type = key_type;
4560
4561 switch (key_type) {
4562 case HCI_LK_LOCAL_UNIT:
4563 case HCI_LK_REMOTE_UNIT:
4564 case HCI_LK_DEBUG_COMBINATION:
4565 return;
4566 case HCI_LK_COMBINATION:
4567 if (pin_len == 16)
4568 conn->pending_sec_level = BT_SECURITY_HIGH;
4569 else
4570 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4571 break;
4572 case HCI_LK_UNAUTH_COMBINATION_P192:
4573 case HCI_LK_UNAUTH_COMBINATION_P256:
4574 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4575 break;
4576 case HCI_LK_AUTH_COMBINATION_P192:
4577 conn->pending_sec_level = BT_SECURITY_HIGH;
4578 break;
4579 case HCI_LK_AUTH_COMBINATION_P256:
4580 conn->pending_sec_level = BT_SECURITY_FIPS;
4581 break;
4582 }
4583 }
4584
hci_link_key_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4585 static void hci_link_key_request_evt(struct hci_dev *hdev, void *data,
4586 struct sk_buff *skb)
4587 {
4588 struct hci_ev_link_key_req *ev = data;
4589 struct hci_cp_link_key_reply cp;
4590 struct hci_conn *conn;
4591 struct link_key *key;
4592
4593 bt_dev_dbg(hdev, "");
4594
4595 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4596 return;
4597
4598 hci_dev_lock(hdev);
4599
4600 key = hci_find_link_key(hdev, &ev->bdaddr);
4601 if (!key) {
4602 bt_dev_dbg(hdev, "link key not found for %pMR", &ev->bdaddr);
4603 goto not_found;
4604 }
4605
4606 bt_dev_dbg(hdev, "found key type %u for %pMR", key->type, &ev->bdaddr);
4607
4608 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4609 if (conn) {
4610 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4611
4612 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4613 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
4614 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
4615 bt_dev_dbg(hdev, "ignoring unauthenticated key");
4616 goto not_found;
4617 }
4618
4619 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
4620 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4621 conn->pending_sec_level == BT_SECURITY_FIPS)) {
4622 bt_dev_dbg(hdev, "ignoring key unauthenticated for high security");
4623 goto not_found;
4624 }
4625
4626 conn_set_key(conn, key->type, key->pin_len);
4627 }
4628
4629 bacpy(&cp.bdaddr, &ev->bdaddr);
4630 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
4631
4632 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4633
4634 hci_dev_unlock(hdev);
4635
4636 return;
4637
4638 not_found:
4639 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4640 hci_dev_unlock(hdev);
4641 }
4642
hci_link_key_notify_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4643 static void hci_link_key_notify_evt(struct hci_dev *hdev, void *data,
4644 struct sk_buff *skb)
4645 {
4646 struct hci_ev_link_key_notify *ev = data;
4647 struct hci_conn *conn;
4648 struct link_key *key;
4649 bool persistent;
4650 u8 pin_len = 0;
4651
4652 bt_dev_dbg(hdev, "");
4653
4654 hci_dev_lock(hdev);
4655
4656 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4657 if (!conn)
4658 goto unlock;
4659
4660 /* Ignore NULL link key against CVE-2020-26555 */
4661 if (!crypto_memneq(ev->link_key, ZERO_KEY, HCI_LINK_KEY_SIZE)) {
4662 bt_dev_dbg(hdev, "Ignore NULL link key (ZERO KEY) for %pMR",
4663 &ev->bdaddr);
4664 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
4665 hci_conn_drop(conn);
4666 goto unlock;
4667 }
4668
4669 hci_conn_hold(conn);
4670 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4671 hci_conn_drop(conn);
4672
4673 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4674 conn_set_key(conn, ev->key_type, conn->pin_length);
4675
4676 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4677 goto unlock;
4678
4679 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4680 ev->key_type, pin_len, &persistent);
4681 if (!key)
4682 goto unlock;
4683
4684 /* Update connection information since adding the key will have
4685 * fixed up the type in the case of changed combination keys.
4686 */
4687 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4688 conn_set_key(conn, key->type, key->pin_len);
4689
4690 mgmt_new_link_key(hdev, key, persistent);
4691
4692 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4693 * is set. If it's not set simply remove the key from the kernel
4694 * list (we've still notified user space about it but with
4695 * store_hint being 0).
4696 */
4697 if (key->type == HCI_LK_DEBUG_COMBINATION &&
4698 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
4699 list_del_rcu(&key->list);
4700 kfree_rcu(key, rcu);
4701 goto unlock;
4702 }
4703
4704 if (persistent)
4705 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4706 else
4707 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4708
4709 unlock:
4710 hci_dev_unlock(hdev);
4711 }
4712
hci_clock_offset_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4713 static void hci_clock_offset_evt(struct hci_dev *hdev, void *data,
4714 struct sk_buff *skb)
4715 {
4716 struct hci_ev_clock_offset *ev = data;
4717 struct hci_conn *conn;
4718
4719 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4720
4721 hci_dev_lock(hdev);
4722
4723 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4724 if (conn && !ev->status) {
4725 struct inquiry_entry *ie;
4726
4727 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4728 if (ie) {
4729 ie->data.clock_offset = ev->clock_offset;
4730 ie->timestamp = jiffies;
4731 }
4732 }
4733
4734 hci_dev_unlock(hdev);
4735 }
4736
hci_pkt_type_change_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4737 static void hci_pkt_type_change_evt(struct hci_dev *hdev, void *data,
4738 struct sk_buff *skb)
4739 {
4740 struct hci_ev_pkt_type_change *ev = data;
4741 struct hci_conn *conn;
4742
4743 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4744
4745 hci_dev_lock(hdev);
4746
4747 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4748 if (conn && !ev->status)
4749 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4750
4751 hci_dev_unlock(hdev);
4752 }
4753
hci_pscan_rep_mode_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4754 static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, void *data,
4755 struct sk_buff *skb)
4756 {
4757 struct hci_ev_pscan_rep_mode *ev = data;
4758 struct inquiry_entry *ie;
4759
4760 bt_dev_dbg(hdev, "");
4761
4762 hci_dev_lock(hdev);
4763
4764 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4765 if (ie) {
4766 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4767 ie->timestamp = jiffies;
4768 }
4769
4770 hci_dev_unlock(hdev);
4771 }
4772
hci_inquiry_result_with_rssi_evt(struct hci_dev * hdev,void * edata,struct sk_buff * skb)4773 static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, void *edata,
4774 struct sk_buff *skb)
4775 {
4776 struct hci_ev_inquiry_result_rssi *ev = edata;
4777 struct inquiry_data data;
4778 int i;
4779
4780 bt_dev_dbg(hdev, "num_rsp %d", ev->num);
4781
4782 if (!ev->num)
4783 return;
4784
4785 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
4786 return;
4787
4788 hci_dev_lock(hdev);
4789
4790 if (skb->len == array_size(ev->num,
4791 sizeof(struct inquiry_info_rssi_pscan))) {
4792 struct inquiry_info_rssi_pscan *info;
4793
4794 for (i = 0; i < ev->num; i++) {
4795 u32 flags;
4796
4797 info = hci_ev_skb_pull(hdev, skb,
4798 HCI_EV_INQUIRY_RESULT_WITH_RSSI,
4799 sizeof(*info));
4800 if (!info) {
4801 bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4802 HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4803 goto unlock;
4804 }
4805
4806 bacpy(&data.bdaddr, &info->bdaddr);
4807 data.pscan_rep_mode = info->pscan_rep_mode;
4808 data.pscan_period_mode = info->pscan_period_mode;
4809 data.pscan_mode = info->pscan_mode;
4810 memcpy(data.dev_class, info->dev_class, 3);
4811 data.clock_offset = info->clock_offset;
4812 data.rssi = info->rssi;
4813 data.ssp_mode = 0x00;
4814
4815 flags = hci_inquiry_cache_update(hdev, &data, false);
4816
4817 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
4818 info->dev_class, info->rssi,
4819 flags, NULL, 0, NULL, 0, 0);
4820 }
4821 } else if (skb->len == array_size(ev->num,
4822 sizeof(struct inquiry_info_rssi))) {
4823 struct inquiry_info_rssi *info;
4824
4825 for (i = 0; i < ev->num; i++) {
4826 u32 flags;
4827
4828 info = hci_ev_skb_pull(hdev, skb,
4829 HCI_EV_INQUIRY_RESULT_WITH_RSSI,
4830 sizeof(*info));
4831 if (!info) {
4832 bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4833 HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4834 goto unlock;
4835 }
4836
4837 bacpy(&data.bdaddr, &info->bdaddr);
4838 data.pscan_rep_mode = info->pscan_rep_mode;
4839 data.pscan_period_mode = info->pscan_period_mode;
4840 data.pscan_mode = 0x00;
4841 memcpy(data.dev_class, info->dev_class, 3);
4842 data.clock_offset = info->clock_offset;
4843 data.rssi = info->rssi;
4844 data.ssp_mode = 0x00;
4845
4846 flags = hci_inquiry_cache_update(hdev, &data, false);
4847
4848 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
4849 info->dev_class, info->rssi,
4850 flags, NULL, 0, NULL, 0, 0);
4851 }
4852 } else {
4853 bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4854 HCI_EV_INQUIRY_RESULT_WITH_RSSI);
4855 }
4856 unlock:
4857 hci_dev_unlock(hdev);
4858 }
4859
hci_remote_ext_features_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4860 static void hci_remote_ext_features_evt(struct hci_dev *hdev, void *data,
4861 struct sk_buff *skb)
4862 {
4863 struct hci_ev_remote_ext_features *ev = data;
4864 struct hci_conn *conn;
4865
4866 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
4867
4868 hci_dev_lock(hdev);
4869
4870 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4871 if (!conn)
4872 goto unlock;
4873
4874 if (ev->page < HCI_MAX_PAGES)
4875 memcpy(conn->features[ev->page], ev->features, 8);
4876
4877 if (!ev->status && ev->page == 0x01) {
4878 struct inquiry_entry *ie;
4879
4880 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4881 if (ie)
4882 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
4883
4884 if (ev->features[0] & LMP_HOST_SSP) {
4885 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4886 } else {
4887 /* It is mandatory by the Bluetooth specification that
4888 * Extended Inquiry Results are only used when Secure
4889 * Simple Pairing is enabled, but some devices violate
4890 * this.
4891 *
4892 * To make these devices work, the internal SSP
4893 * enabled flag needs to be cleared if the remote host
4894 * features do not indicate SSP support */
4895 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4896 }
4897
4898 if (ev->features[0] & LMP_HOST_SC)
4899 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
4900 }
4901
4902 if (conn->state != BT_CONFIG)
4903 goto unlock;
4904
4905 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
4906 struct hci_cp_remote_name_req cp;
4907 memset(&cp, 0, sizeof(cp));
4908 bacpy(&cp.bdaddr, &conn->dst);
4909 cp.pscan_rep_mode = 0x02;
4910 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
4911 } else {
4912 mgmt_device_connected(hdev, conn, NULL, 0);
4913 }
4914
4915 if (!hci_outgoing_auth_needed(hdev, conn)) {
4916 conn->state = BT_CONNECTED;
4917 hci_connect_cfm(conn, ev->status);
4918 hci_conn_drop(conn);
4919 }
4920
4921 unlock:
4922 hci_dev_unlock(hdev);
4923 }
4924
hci_sync_conn_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)4925 static void hci_sync_conn_complete_evt(struct hci_dev *hdev, void *data,
4926 struct sk_buff *skb)
4927 {
4928 struct hci_ev_sync_conn_complete *ev = data;
4929 struct hci_conn *conn;
4930 u8 status = ev->status;
4931
4932 switch (ev->link_type) {
4933 case SCO_LINK:
4934 case ESCO_LINK:
4935 break;
4936 default:
4937 /* As per Core 5.3 Vol 4 Part E 7.7.35 (p.2219), Link_Type
4938 * for HCI_Synchronous_Connection_Complete is limited to
4939 * either SCO or eSCO
4940 */
4941 bt_dev_err(hdev, "Ignoring connect complete event for invalid link type");
4942 return;
4943 }
4944
4945 bt_dev_dbg(hdev, "status 0x%2.2x", status);
4946
4947 hci_dev_lock(hdev);
4948
4949 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
4950 if (!conn) {
4951 if (ev->link_type == ESCO_LINK)
4952 goto unlock;
4953
4954 /* When the link type in the event indicates SCO connection
4955 * and lookup of the connection object fails, then check
4956 * if an eSCO connection object exists.
4957 *
4958 * The core limits the synchronous connections to either
4959 * SCO or eSCO. The eSCO connection is preferred and tried
4960 * to be setup first and until successfully established,
4961 * the link type will be hinted as eSCO.
4962 */
4963 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
4964 if (!conn)
4965 goto unlock;
4966 }
4967
4968 /* The HCI_Synchronous_Connection_Complete event is only sent once per connection.
4969 * Processing it more than once per connection can corrupt kernel memory.
4970 *
4971 * As the connection handle is set here for the first time, it indicates
4972 * whether the connection is already set up.
4973 */
4974 if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
4975 bt_dev_err(hdev, "Ignoring HCI_Sync_Conn_Complete event for existing connection");
4976 goto unlock;
4977 }
4978
4979 switch (status) {
4980 case 0x00:
4981 status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
4982 if (status) {
4983 conn->state = BT_CLOSED;
4984 break;
4985 }
4986
4987 conn->state = BT_CONNECTED;
4988 conn->type = ev->link_type;
4989
4990 hci_debugfs_create_conn(conn);
4991 hci_conn_add_sysfs(conn);
4992 break;
4993
4994 case 0x10: /* Connection Accept Timeout */
4995 case 0x0d: /* Connection Rejected due to Limited Resources */
4996 case 0x11: /* Unsupported Feature or Parameter Value */
4997 case 0x1c: /* SCO interval rejected */
4998 case 0x1a: /* Unsupported Remote Feature */
4999 case 0x1e: /* Invalid LMP Parameters */
5000 case 0x1f: /* Unspecified error */
5001 case 0x20: /* Unsupported LMP Parameter value */
5002 if (conn->out) {
5003 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
5004 (hdev->esco_type & EDR_ESCO_MASK);
5005 if (hci_setup_sync(conn, conn->parent->handle))
5006 goto unlock;
5007 }
5008 fallthrough;
5009
5010 default:
5011 conn->state = BT_CLOSED;
5012 break;
5013 }
5014
5015 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
5016 /* Notify only in case of SCO over HCI transport data path which
5017 * is zero and non-zero value shall be non-HCI transport data path
5018 */
5019 if (conn->codec.data_path == 0 && hdev->notify) {
5020 switch (ev->air_mode) {
5021 case 0x02:
5022 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
5023 break;
5024 case 0x03:
5025 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
5026 break;
5027 }
5028 }
5029
5030 hci_connect_cfm(conn, status);
5031 if (status)
5032 hci_conn_del(conn);
5033
5034 unlock:
5035 hci_dev_unlock(hdev);
5036 }
5037
eir_get_length(u8 * eir,size_t eir_len)5038 static inline size_t eir_get_length(u8 *eir, size_t eir_len)
5039 {
5040 size_t parsed = 0;
5041
5042 while (parsed < eir_len) {
5043 u8 field_len = eir[0];
5044
5045 if (field_len == 0)
5046 return parsed;
5047
5048 parsed += field_len + 1;
5049 eir += field_len + 1;
5050 }
5051
5052 return eir_len;
5053 }
5054
hci_extended_inquiry_result_evt(struct hci_dev * hdev,void * edata,struct sk_buff * skb)5055 static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, void *edata,
5056 struct sk_buff *skb)
5057 {
5058 struct hci_ev_ext_inquiry_result *ev = edata;
5059 struct inquiry_data data;
5060 size_t eir_len;
5061 int i;
5062
5063 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_EXTENDED_INQUIRY_RESULT,
5064 flex_array_size(ev, info, ev->num)))
5065 return;
5066
5067 bt_dev_dbg(hdev, "num %d", ev->num);
5068
5069 if (!ev->num)
5070 return;
5071
5072 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
5073 return;
5074
5075 hci_dev_lock(hdev);
5076
5077 for (i = 0; i < ev->num; i++) {
5078 struct extended_inquiry_info *info = &ev->info[i];
5079 u32 flags;
5080 bool name_known;
5081
5082 bacpy(&data.bdaddr, &info->bdaddr);
5083 data.pscan_rep_mode = info->pscan_rep_mode;
5084 data.pscan_period_mode = info->pscan_period_mode;
5085 data.pscan_mode = 0x00;
5086 memcpy(data.dev_class, info->dev_class, 3);
5087 data.clock_offset = info->clock_offset;
5088 data.rssi = info->rssi;
5089 data.ssp_mode = 0x01;
5090
5091 if (hci_dev_test_flag(hdev, HCI_MGMT))
5092 name_known = eir_get_data(info->data,
5093 sizeof(info->data),
5094 EIR_NAME_COMPLETE, NULL);
5095 else
5096 name_known = true;
5097
5098 flags = hci_inquiry_cache_update(hdev, &data, name_known);
5099
5100 eir_len = eir_get_length(info->data, sizeof(info->data));
5101
5102 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
5103 info->dev_class, info->rssi,
5104 flags, info->data, eir_len, NULL, 0, 0);
5105 }
5106
5107 hci_dev_unlock(hdev);
5108 }
5109
hci_key_refresh_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5110 static void hci_key_refresh_complete_evt(struct hci_dev *hdev, void *data,
5111 struct sk_buff *skb)
5112 {
5113 struct hci_ev_key_refresh_complete *ev = data;
5114 struct hci_conn *conn;
5115
5116 bt_dev_dbg(hdev, "status 0x%2.2x handle 0x%4.4x", ev->status,
5117 __le16_to_cpu(ev->handle));
5118
5119 hci_dev_lock(hdev);
5120
5121 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5122 if (!conn)
5123 goto unlock;
5124
5125 /* For BR/EDR the necessary steps are taken through the
5126 * auth_complete event.
5127 */
5128 if (conn->type != LE_LINK)
5129 goto unlock;
5130
5131 if (!ev->status)
5132 conn->sec_level = conn->pending_sec_level;
5133
5134 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
5135
5136 if (ev->status && conn->state == BT_CONNECTED) {
5137 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
5138 hci_conn_drop(conn);
5139 goto unlock;
5140 }
5141
5142 if (conn->state == BT_CONFIG) {
5143 if (!ev->status)
5144 conn->state = BT_CONNECTED;
5145
5146 hci_connect_cfm(conn, ev->status);
5147 hci_conn_drop(conn);
5148 } else {
5149 hci_auth_cfm(conn, ev->status);
5150
5151 hci_conn_hold(conn);
5152 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
5153 hci_conn_drop(conn);
5154 }
5155
5156 unlock:
5157 hci_dev_unlock(hdev);
5158 }
5159
hci_get_auth_req(struct hci_conn * conn)5160 static u8 hci_get_auth_req(struct hci_conn *conn)
5161 {
5162 /* If remote requests no-bonding follow that lead */
5163 if (conn->remote_auth == HCI_AT_NO_BONDING ||
5164 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
5165 return conn->remote_auth | (conn->auth_type & 0x01);
5166
5167 /* If both remote and local have enough IO capabilities, require
5168 * MITM protection
5169 */
5170 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
5171 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
5172 return conn->remote_auth | 0x01;
5173
5174 /* No MITM protection possible so ignore remote requirement */
5175 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
5176 }
5177
bredr_oob_data_present(struct hci_conn * conn)5178 static u8 bredr_oob_data_present(struct hci_conn *conn)
5179 {
5180 struct hci_dev *hdev = conn->hdev;
5181 struct oob_data *data;
5182
5183 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
5184 if (!data)
5185 return 0x00;
5186
5187 if (bredr_sc_enabled(hdev)) {
5188 /* When Secure Connections is enabled, then just
5189 * return the present value stored with the OOB
5190 * data. The stored value contains the right present
5191 * information. However it can only be trusted when
5192 * not in Secure Connection Only mode.
5193 */
5194 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
5195 return data->present;
5196
5197 /* When Secure Connections Only mode is enabled, then
5198 * the P-256 values are required. If they are not
5199 * available, then do not declare that OOB data is
5200 * present.
5201 */
5202 if (!crypto_memneq(data->rand256, ZERO_KEY, 16) ||
5203 !crypto_memneq(data->hash256, ZERO_KEY, 16))
5204 return 0x00;
5205
5206 return 0x02;
5207 }
5208
5209 /* When Secure Connections is not enabled or actually
5210 * not supported by the hardware, then check that if
5211 * P-192 data values are present.
5212 */
5213 if (!crypto_memneq(data->rand192, ZERO_KEY, 16) ||
5214 !crypto_memneq(data->hash192, ZERO_KEY, 16))
5215 return 0x00;
5216
5217 return 0x01;
5218 }
5219
hci_io_capa_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5220 static void hci_io_capa_request_evt(struct hci_dev *hdev, void *data,
5221 struct sk_buff *skb)
5222 {
5223 struct hci_ev_io_capa_request *ev = data;
5224 struct hci_conn *conn;
5225
5226 bt_dev_dbg(hdev, "");
5227
5228 hci_dev_lock(hdev);
5229
5230 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5231 if (!conn || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
5232 goto unlock;
5233
5234 /* Assume remote supports SSP since it has triggered this event */
5235 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
5236
5237 hci_conn_hold(conn);
5238
5239 if (!hci_dev_test_flag(hdev, HCI_MGMT))
5240 goto unlock;
5241
5242 /* Allow pairing if we're pairable, the initiators of the
5243 * pairing or if the remote is not requesting bonding.
5244 */
5245 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
5246 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
5247 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
5248 struct hci_cp_io_capability_reply cp;
5249
5250 bacpy(&cp.bdaddr, &ev->bdaddr);
5251 /* Change the IO capability from KeyboardDisplay
5252 * to DisplayYesNo as it is not supported by BT spec. */
5253 cp.capability = (conn->io_capability == 0x04) ?
5254 HCI_IO_DISPLAY_YESNO : conn->io_capability;
5255
5256 /* If we are initiators, there is no remote information yet */
5257 if (conn->remote_auth == 0xff) {
5258 /* Request MITM protection if our IO caps allow it
5259 * except for the no-bonding case.
5260 */
5261 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
5262 conn->auth_type != HCI_AT_NO_BONDING)
5263 conn->auth_type |= 0x01;
5264 } else {
5265 conn->auth_type = hci_get_auth_req(conn);
5266 }
5267
5268 /* If we're not bondable, force one of the non-bondable
5269 * authentication requirement values.
5270 */
5271 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
5272 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
5273
5274 cp.authentication = conn->auth_type;
5275 cp.oob_data = bredr_oob_data_present(conn);
5276
5277 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
5278 sizeof(cp), &cp);
5279 } else {
5280 struct hci_cp_io_capability_neg_reply cp;
5281
5282 bacpy(&cp.bdaddr, &ev->bdaddr);
5283 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
5284
5285 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
5286 sizeof(cp), &cp);
5287 }
5288
5289 unlock:
5290 hci_dev_unlock(hdev);
5291 }
5292
hci_io_capa_reply_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5293 static void hci_io_capa_reply_evt(struct hci_dev *hdev, void *data,
5294 struct sk_buff *skb)
5295 {
5296 struct hci_ev_io_capa_reply *ev = data;
5297 struct hci_conn *conn;
5298
5299 bt_dev_dbg(hdev, "");
5300
5301 hci_dev_lock(hdev);
5302
5303 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5304 if (!conn)
5305 goto unlock;
5306
5307 conn->remote_cap = ev->capability;
5308 conn->remote_auth = ev->authentication;
5309
5310 unlock:
5311 hci_dev_unlock(hdev);
5312 }
5313
hci_user_confirm_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5314 static void hci_user_confirm_request_evt(struct hci_dev *hdev, void *data,
5315 struct sk_buff *skb)
5316 {
5317 struct hci_ev_user_confirm_req *ev = data;
5318 int loc_mitm, rem_mitm, confirm_hint = 0;
5319 struct hci_conn *conn;
5320
5321 bt_dev_dbg(hdev, "");
5322
5323 hci_dev_lock(hdev);
5324
5325 if (!hci_dev_test_flag(hdev, HCI_MGMT))
5326 goto unlock;
5327
5328 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5329 if (!conn)
5330 goto unlock;
5331
5332 loc_mitm = (conn->auth_type & 0x01);
5333 rem_mitm = (conn->remote_auth & 0x01);
5334
5335 /* If we require MITM but the remote device can't provide that
5336 * (it has NoInputNoOutput) then reject the confirmation
5337 * request. We check the security level here since it doesn't
5338 * necessarily match conn->auth_type.
5339 */
5340 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
5341 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
5342 bt_dev_dbg(hdev, "Rejecting request: remote device can't provide MITM");
5343 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
5344 sizeof(ev->bdaddr), &ev->bdaddr);
5345 goto unlock;
5346 }
5347
5348 /* If no side requires MITM protection; use JUST_CFM method */
5349 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
5350 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
5351
5352 /* If we're not the initiator of request authorization and the
5353 * local IO capability is not NoInputNoOutput, use JUST_WORKS
5354 * method (mgmt_user_confirm with confirm_hint set to 1).
5355 */
5356 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
5357 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT) {
5358 bt_dev_dbg(hdev, "Confirming auto-accept as acceptor");
5359 confirm_hint = 1;
5360 goto confirm;
5361 }
5362
5363 /* If there already exists link key in local host, leave the
5364 * decision to user space since the remote device could be
5365 * legitimate or malicious.
5366 */
5367 if (hci_find_link_key(hdev, &ev->bdaddr)) {
5368 bt_dev_dbg(hdev, "Local host already has link key");
5369 confirm_hint = 1;
5370 goto confirm;
5371 }
5372
5373 BT_DBG("Auto-accept of user confirmation with %ums delay",
5374 hdev->auto_accept_delay);
5375
5376 if (hdev->auto_accept_delay > 0) {
5377 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
5378 queue_delayed_work(conn->hdev->workqueue,
5379 &conn->auto_accept_work, delay);
5380 goto unlock;
5381 }
5382
5383 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
5384 sizeof(ev->bdaddr), &ev->bdaddr);
5385 goto unlock;
5386 }
5387
5388 confirm:
5389 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
5390 le32_to_cpu(ev->passkey), confirm_hint);
5391
5392 unlock:
5393 hci_dev_unlock(hdev);
5394 }
5395
hci_user_passkey_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5396 static void hci_user_passkey_request_evt(struct hci_dev *hdev, void *data,
5397 struct sk_buff *skb)
5398 {
5399 struct hci_ev_user_passkey_req *ev = data;
5400
5401 bt_dev_dbg(hdev, "");
5402
5403 if (hci_dev_test_flag(hdev, HCI_MGMT))
5404 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
5405 }
5406
hci_user_passkey_notify_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5407 static void hci_user_passkey_notify_evt(struct hci_dev *hdev, void *data,
5408 struct sk_buff *skb)
5409 {
5410 struct hci_ev_user_passkey_notify *ev = data;
5411 struct hci_conn *conn;
5412
5413 bt_dev_dbg(hdev, "");
5414
5415 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5416 if (!conn)
5417 return;
5418
5419 conn->passkey_notify = __le32_to_cpu(ev->passkey);
5420 conn->passkey_entered = 0;
5421
5422 if (hci_dev_test_flag(hdev, HCI_MGMT))
5423 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5424 conn->dst_type, conn->passkey_notify,
5425 conn->passkey_entered);
5426 }
5427
hci_keypress_notify_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5428 static void hci_keypress_notify_evt(struct hci_dev *hdev, void *data,
5429 struct sk_buff *skb)
5430 {
5431 struct hci_ev_keypress_notify *ev = data;
5432 struct hci_conn *conn;
5433
5434 bt_dev_dbg(hdev, "");
5435
5436 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5437 if (!conn)
5438 return;
5439
5440 switch (ev->type) {
5441 case HCI_KEYPRESS_STARTED:
5442 conn->passkey_entered = 0;
5443 return;
5444
5445 case HCI_KEYPRESS_ENTERED:
5446 conn->passkey_entered++;
5447 break;
5448
5449 case HCI_KEYPRESS_ERASED:
5450 conn->passkey_entered--;
5451 break;
5452
5453 case HCI_KEYPRESS_CLEARED:
5454 conn->passkey_entered = 0;
5455 break;
5456
5457 case HCI_KEYPRESS_COMPLETED:
5458 return;
5459 }
5460
5461 if (hci_dev_test_flag(hdev, HCI_MGMT))
5462 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5463 conn->dst_type, conn->passkey_notify,
5464 conn->passkey_entered);
5465 }
5466
hci_simple_pair_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5467 static void hci_simple_pair_complete_evt(struct hci_dev *hdev, void *data,
5468 struct sk_buff *skb)
5469 {
5470 struct hci_ev_simple_pair_complete *ev = data;
5471 struct hci_conn *conn;
5472
5473 bt_dev_dbg(hdev, "");
5474
5475 hci_dev_lock(hdev);
5476
5477 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5478 if (!conn || !hci_conn_ssp_enabled(conn))
5479 goto unlock;
5480
5481 /* Reset the authentication requirement to unknown */
5482 conn->remote_auth = 0xff;
5483
5484 /* To avoid duplicate auth_failed events to user space we check
5485 * the HCI_CONN_AUTH_PEND flag which will be set if we
5486 * initiated the authentication. A traditional auth_complete
5487 * event gets always produced as initiator and is also mapped to
5488 * the mgmt_auth_failed event */
5489 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
5490 mgmt_auth_failed(conn, ev->status);
5491
5492 hci_conn_drop(conn);
5493
5494 unlock:
5495 hci_dev_unlock(hdev);
5496 }
5497
hci_remote_host_features_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5498 static void hci_remote_host_features_evt(struct hci_dev *hdev, void *data,
5499 struct sk_buff *skb)
5500 {
5501 struct hci_ev_remote_host_features *ev = data;
5502 struct inquiry_entry *ie;
5503 struct hci_conn *conn;
5504
5505 bt_dev_dbg(hdev, "");
5506
5507 hci_dev_lock(hdev);
5508
5509 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5510 if (conn)
5511 memcpy(conn->features[1], ev->features, 8);
5512
5513 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
5514 if (ie)
5515 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
5516
5517 hci_dev_unlock(hdev);
5518 }
5519
hci_remote_oob_data_request_evt(struct hci_dev * hdev,void * edata,struct sk_buff * skb)5520 static void hci_remote_oob_data_request_evt(struct hci_dev *hdev, void *edata,
5521 struct sk_buff *skb)
5522 {
5523 struct hci_ev_remote_oob_data_request *ev = edata;
5524 struct oob_data *data;
5525
5526 bt_dev_dbg(hdev, "");
5527
5528 hci_dev_lock(hdev);
5529
5530 if (!hci_dev_test_flag(hdev, HCI_MGMT))
5531 goto unlock;
5532
5533 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
5534 if (!data) {
5535 struct hci_cp_remote_oob_data_neg_reply cp;
5536
5537 bacpy(&cp.bdaddr, &ev->bdaddr);
5538 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
5539 sizeof(cp), &cp);
5540 goto unlock;
5541 }
5542
5543 if (bredr_sc_enabled(hdev)) {
5544 struct hci_cp_remote_oob_ext_data_reply cp;
5545
5546 bacpy(&cp.bdaddr, &ev->bdaddr);
5547 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
5548 memset(cp.hash192, 0, sizeof(cp.hash192));
5549 memset(cp.rand192, 0, sizeof(cp.rand192));
5550 } else {
5551 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
5552 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
5553 }
5554 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5555 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5556
5557 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5558 sizeof(cp), &cp);
5559 } else {
5560 struct hci_cp_remote_oob_data_reply cp;
5561
5562 bacpy(&cp.bdaddr, &ev->bdaddr);
5563 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5564 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5565
5566 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5567 sizeof(cp), &cp);
5568 }
5569
5570 unlock:
5571 hci_dev_unlock(hdev);
5572 }
5573
le_conn_update_addr(struct hci_conn * conn,bdaddr_t * bdaddr,u8 bdaddr_type,bdaddr_t * local_rpa)5574 static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5575 u8 bdaddr_type, bdaddr_t *local_rpa)
5576 {
5577 if (conn->out) {
5578 conn->dst_type = bdaddr_type;
5579 conn->resp_addr_type = bdaddr_type;
5580 bacpy(&conn->resp_addr, bdaddr);
5581
5582 /* Check if the controller has set a Local RPA then it must be
5583 * used instead or hdev->rpa.
5584 */
5585 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5586 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5587 bacpy(&conn->init_addr, local_rpa);
5588 } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5589 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5590 bacpy(&conn->init_addr, &conn->hdev->rpa);
5591 } else {
5592 hci_copy_identity_address(conn->hdev, &conn->init_addr,
5593 &conn->init_addr_type);
5594 }
5595 } else {
5596 conn->resp_addr_type = conn->hdev->adv_addr_type;
5597 /* Check if the controller has set a Local RPA then it must be
5598 * used instead or hdev->rpa.
5599 */
5600 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5601 conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5602 bacpy(&conn->resp_addr, local_rpa);
5603 } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5604 /* In case of ext adv, resp_addr will be updated in
5605 * Adv Terminated event.
5606 */
5607 if (!ext_adv_capable(conn->hdev))
5608 bacpy(&conn->resp_addr,
5609 &conn->hdev->random_addr);
5610 } else {
5611 bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5612 }
5613
5614 conn->init_addr_type = bdaddr_type;
5615 bacpy(&conn->init_addr, bdaddr);
5616
5617 /* For incoming connections, set the default minimum
5618 * and maximum connection interval. They will be used
5619 * to check if the parameters are in range and if not
5620 * trigger the connection update procedure.
5621 */
5622 conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5623 conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5624 }
5625 }
5626
le_conn_complete_evt(struct hci_dev * hdev,u8 status,bdaddr_t * bdaddr,u8 bdaddr_type,bdaddr_t * local_rpa,u8 role,u16 handle,u16 interval,u16 latency,u16 supervision_timeout)5627 static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
5628 bdaddr_t *bdaddr, u8 bdaddr_type,
5629 bdaddr_t *local_rpa, u8 role, u16 handle,
5630 u16 interval, u16 latency,
5631 u16 supervision_timeout)
5632 {
5633 struct hci_conn_params *params;
5634 struct hci_conn *conn;
5635 struct smp_irk *irk;
5636 u8 addr_type;
5637
5638 hci_dev_lock(hdev);
5639
5640 /* All controllers implicitly stop advertising in the event of a
5641 * connection, so ensure that the state bit is cleared.
5642 */
5643 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5644
5645 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, bdaddr);
5646 if (!conn) {
5647 /* In case of error status and there is no connection pending
5648 * just unlock as there is nothing to cleanup.
5649 */
5650 if (status)
5651 goto unlock;
5652
5653 conn = hci_conn_add_unset(hdev, LE_LINK, bdaddr, role);
5654 if (IS_ERR(conn)) {
5655 bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
5656 goto unlock;
5657 }
5658
5659 conn->dst_type = bdaddr_type;
5660
5661 /* If we didn't have a hci_conn object previously
5662 * but we're in central role this must be something
5663 * initiated using an accept list. Since accept list based
5664 * connections are not "first class citizens" we don't
5665 * have full tracking of them. Therefore, we go ahead
5666 * with a "best effort" approach of determining the
5667 * initiator address based on the HCI_PRIVACY flag.
5668 */
5669 if (conn->out) {
5670 conn->resp_addr_type = bdaddr_type;
5671 bacpy(&conn->resp_addr, bdaddr);
5672 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
5673 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5674 bacpy(&conn->init_addr, &hdev->rpa);
5675 } else {
5676 hci_copy_identity_address(hdev,
5677 &conn->init_addr,
5678 &conn->init_addr_type);
5679 }
5680 }
5681 } else {
5682 cancel_delayed_work(&conn->le_conn_timeout);
5683 }
5684
5685 /* The HCI_LE_Connection_Complete event is only sent once per connection.
5686 * Processing it more than once per connection can corrupt kernel memory.
5687 *
5688 * As the connection handle is set here for the first time, it indicates
5689 * whether the connection is already set up.
5690 */
5691 if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
5692 bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
5693 goto unlock;
5694 }
5695
5696 le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
5697
5698 /* Lookup the identity address from the stored connection
5699 * address and address type.
5700 *
5701 * When establishing connections to an identity address, the
5702 * connection procedure will store the resolvable random
5703 * address first. Now if it can be converted back into the
5704 * identity address, start using the identity address from
5705 * now on.
5706 */
5707 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
5708 if (irk) {
5709 bacpy(&conn->dst, &irk->bdaddr);
5710 conn->dst_type = irk->addr_type;
5711 }
5712
5713 conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
5714
5715 /* All connection failure handling is taken care of by the
5716 * hci_conn_failed function which is triggered by the HCI
5717 * request completion callbacks used for connecting.
5718 */
5719 if (status || hci_conn_set_handle(conn, handle))
5720 goto unlock;
5721
5722 /* Drop the connection if it has been aborted */
5723 if (test_bit(HCI_CONN_CANCEL, &conn->flags)) {
5724 hci_conn_drop(conn);
5725 goto unlock;
5726 }
5727
5728 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5729 addr_type = BDADDR_LE_PUBLIC;
5730 else
5731 addr_type = BDADDR_LE_RANDOM;
5732
5733 /* Drop the connection if the device is blocked */
5734 if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
5735 hci_conn_drop(conn);
5736 goto unlock;
5737 }
5738
5739 mgmt_device_connected(hdev, conn, NULL, 0);
5740
5741 conn->sec_level = BT_SECURITY_LOW;
5742 conn->state = BT_CONFIG;
5743
5744 /* Store current advertising instance as connection advertising instance
5745 * when sotfware rotation is in use so it can be re-enabled when
5746 * disconnected.
5747 */
5748 if (!ext_adv_capable(hdev))
5749 conn->adv_instance = hdev->cur_adv_instance;
5750
5751 conn->le_conn_interval = interval;
5752 conn->le_conn_latency = latency;
5753 conn->le_supv_timeout = supervision_timeout;
5754
5755 hci_debugfs_create_conn(conn);
5756 hci_conn_add_sysfs(conn);
5757
5758 /* The remote features procedure is defined for central
5759 * role only. So only in case of an initiated connection
5760 * request the remote features.
5761 *
5762 * If the local controller supports peripheral-initiated features
5763 * exchange, then requesting the remote features in peripheral
5764 * role is possible. Otherwise just transition into the
5765 * connected state without requesting the remote features.
5766 */
5767 if (conn->out ||
5768 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
5769 struct hci_cp_le_read_remote_features cp;
5770
5771 cp.handle = __cpu_to_le16(conn->handle);
5772
5773 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
5774 sizeof(cp), &cp);
5775
5776 hci_conn_hold(conn);
5777 } else {
5778 conn->state = BT_CONNECTED;
5779 hci_connect_cfm(conn, status);
5780 }
5781
5782 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5783 conn->dst_type);
5784 if (params) {
5785 hci_pend_le_list_del_init(params);
5786 if (params->conn) {
5787 hci_conn_drop(params->conn);
5788 hci_conn_put(params->conn);
5789 params->conn = NULL;
5790 }
5791 }
5792
5793 unlock:
5794 hci_update_passive_scan(hdev);
5795 hci_dev_unlock(hdev);
5796 }
5797
hci_le_conn_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5798 static void hci_le_conn_complete_evt(struct hci_dev *hdev, void *data,
5799 struct sk_buff *skb)
5800 {
5801 struct hci_ev_le_conn_complete *ev = data;
5802
5803 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5804
5805 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5806 NULL, ev->role, le16_to_cpu(ev->handle),
5807 le16_to_cpu(ev->interval),
5808 le16_to_cpu(ev->latency),
5809 le16_to_cpu(ev->supervision_timeout));
5810 }
5811
hci_le_enh_conn_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5812 static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev, void *data,
5813 struct sk_buff *skb)
5814 {
5815 struct hci_ev_le_enh_conn_complete *ev = data;
5816
5817 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5818
5819 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5820 &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
5821 le16_to_cpu(ev->interval),
5822 le16_to_cpu(ev->latency),
5823 le16_to_cpu(ev->supervision_timeout));
5824 }
5825
hci_le_ext_adv_term_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5826 static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data,
5827 struct sk_buff *skb)
5828 {
5829 struct hci_evt_le_ext_adv_set_term *ev = data;
5830 struct hci_conn *conn;
5831 struct adv_info *adv, *n;
5832
5833 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5834
5835 /* The Bluetooth Core 5.3 specification clearly states that this event
5836 * shall not be sent when the Host disables the advertising set. So in
5837 * case of HCI_ERROR_CANCELLED_BY_HOST, just ignore the event.
5838 *
5839 * When the Host disables an advertising set, all cleanup is done via
5840 * its command callback and not needed to be duplicated here.
5841 */
5842 if (ev->status == HCI_ERROR_CANCELLED_BY_HOST) {
5843 bt_dev_warn_ratelimited(hdev, "Unexpected advertising set terminated event");
5844 return;
5845 }
5846
5847 hci_dev_lock(hdev);
5848
5849 adv = hci_find_adv_instance(hdev, ev->handle);
5850
5851 if (ev->status) {
5852 if (!adv)
5853 goto unlock;
5854
5855 /* Remove advertising as it has been terminated */
5856 hci_remove_adv_instance(hdev, ev->handle);
5857 mgmt_advertising_removed(NULL, hdev, ev->handle);
5858
5859 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
5860 if (adv->enabled)
5861 goto unlock;
5862 }
5863
5864 /* We are no longer advertising, clear HCI_LE_ADV */
5865 hci_dev_clear_flag(hdev, HCI_LE_ADV);
5866 goto unlock;
5867 }
5868
5869 if (adv)
5870 adv->enabled = false;
5871
5872 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5873 if (conn) {
5874 /* Store handle in the connection so the correct advertising
5875 * instance can be re-enabled when disconnected.
5876 */
5877 conn->adv_instance = ev->handle;
5878
5879 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
5880 bacmp(&conn->resp_addr, BDADDR_ANY))
5881 goto unlock;
5882
5883 if (!ev->handle) {
5884 bacpy(&conn->resp_addr, &hdev->random_addr);
5885 goto unlock;
5886 }
5887
5888 if (adv)
5889 bacpy(&conn->resp_addr, &adv->random_addr);
5890 }
5891
5892 unlock:
5893 hci_dev_unlock(hdev);
5894 }
5895
hci_le_conn_update_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)5896 static void hci_le_conn_update_complete_evt(struct hci_dev *hdev, void *data,
5897 struct sk_buff *skb)
5898 {
5899 struct hci_ev_le_conn_update_complete *ev = data;
5900 struct hci_conn *conn;
5901
5902 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
5903
5904 if (ev->status)
5905 return;
5906
5907 hci_dev_lock(hdev);
5908
5909 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5910 if (conn) {
5911 conn->le_conn_interval = le16_to_cpu(ev->interval);
5912 conn->le_conn_latency = le16_to_cpu(ev->latency);
5913 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
5914 }
5915
5916 hci_dev_unlock(hdev);
5917 }
5918
5919 /* This function requires the caller holds hdev->lock */
check_pending_le_conn(struct hci_dev * hdev,bdaddr_t * addr,u8 addr_type,bool addr_resolved,u8 adv_type,u8 phy,u8 sec_phy)5920 static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
5921 bdaddr_t *addr,
5922 u8 addr_type, bool addr_resolved,
5923 u8 adv_type, u8 phy, u8 sec_phy)
5924 {
5925 struct hci_conn *conn;
5926 struct hci_conn_params *params;
5927
5928 /* If the event is not connectable don't proceed further */
5929 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
5930 return NULL;
5931
5932 /* Ignore if the device is blocked or hdev is suspended */
5933 if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type) ||
5934 hdev->suspended)
5935 return NULL;
5936
5937 /* Most controller will fail if we try to create new connections
5938 * while we have an existing one in peripheral role.
5939 */
5940 if (hdev->conn_hash.le_num_peripheral > 0 &&
5941 (test_bit(HCI_QUIRK_BROKEN_LE_STATES, &hdev->quirks) ||
5942 !(hdev->le_states[3] & 0x10)))
5943 return NULL;
5944
5945 /* If we're not connectable only connect devices that we have in
5946 * our pend_le_conns list.
5947 */
5948 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
5949 addr_type);
5950 if (!params)
5951 return NULL;
5952
5953 if (!params->explicit_connect) {
5954 switch (params->auto_connect) {
5955 case HCI_AUTO_CONN_DIRECT:
5956 /* Only devices advertising with ADV_DIRECT_IND are
5957 * triggering a connection attempt. This is allowing
5958 * incoming connections from peripheral devices.
5959 */
5960 if (adv_type != LE_ADV_DIRECT_IND)
5961 return NULL;
5962 break;
5963 case HCI_AUTO_CONN_ALWAYS:
5964 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5965 * are triggering a connection attempt. This means
5966 * that incoming connections from peripheral device are
5967 * accepted and also outgoing connections to peripheral
5968 * devices are established when found.
5969 */
5970 break;
5971 default:
5972 return NULL;
5973 }
5974 }
5975
5976 conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
5977 BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
5978 HCI_ROLE_MASTER, phy, sec_phy);
5979 if (!IS_ERR(conn)) {
5980 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5981 * by higher layer that tried to connect, if no then
5982 * store the pointer since we don't really have any
5983 * other owner of the object besides the params that
5984 * triggered it. This way we can abort the connection if
5985 * the parameters get removed and keep the reference
5986 * count consistent once the connection is established.
5987 */
5988
5989 if (!params->explicit_connect)
5990 params->conn = hci_conn_get(conn);
5991
5992 return conn;
5993 }
5994
5995 switch (PTR_ERR(conn)) {
5996 case -EBUSY:
5997 /* If hci_connect() returns -EBUSY it means there is already
5998 * an LE connection attempt going on. Since controllers don't
5999 * support more than one connection attempt at the time, we
6000 * don't consider this an error case.
6001 */
6002 break;
6003 default:
6004 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
6005 return NULL;
6006 }
6007
6008 return NULL;
6009 }
6010
process_adv_report(struct hci_dev * hdev,u8 type,bdaddr_t * bdaddr,u8 bdaddr_type,bdaddr_t * direct_addr,u8 direct_addr_type,u8 phy,u8 sec_phy,s8 rssi,u8 * data,u8 len,bool ext_adv,bool ctl_time,u64 instant)6011 static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
6012 u8 bdaddr_type, bdaddr_t *direct_addr,
6013 u8 direct_addr_type, u8 phy, u8 sec_phy, s8 rssi,
6014 u8 *data, u8 len, bool ext_adv, bool ctl_time,
6015 u64 instant)
6016 {
6017 struct discovery_state *d = &hdev->discovery;
6018 struct smp_irk *irk;
6019 struct hci_conn *conn;
6020 bool match, bdaddr_resolved;
6021 u32 flags;
6022 u8 *ptr;
6023
6024 switch (type) {
6025 case LE_ADV_IND:
6026 case LE_ADV_DIRECT_IND:
6027 case LE_ADV_SCAN_IND:
6028 case LE_ADV_NONCONN_IND:
6029 case LE_ADV_SCAN_RSP:
6030 break;
6031 default:
6032 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
6033 "type: 0x%02x", type);
6034 return;
6035 }
6036
6037 if (len > max_adv_len(hdev)) {
6038 bt_dev_err_ratelimited(hdev,
6039 "adv larger than maximum supported");
6040 return;
6041 }
6042
6043 /* Find the end of the data in case the report contains padded zero
6044 * bytes at the end causing an invalid length value.
6045 *
6046 * When data is NULL, len is 0 so there is no need for extra ptr
6047 * check as 'ptr < data + 0' is already false in such case.
6048 */
6049 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
6050 if (ptr + 1 + *ptr > data + len)
6051 break;
6052 }
6053
6054 /* Adjust for actual length. This handles the case when remote
6055 * device is advertising with incorrect data length.
6056 */
6057 len = ptr - data;
6058
6059 /* If the direct address is present, then this report is from
6060 * a LE Direct Advertising Report event. In that case it is
6061 * important to see if the address is matching the local
6062 * controller address.
6063 *
6064 * If local privacy is not enable the controller shall not be
6065 * generating such event since according to its documentation it is only
6066 * valid for filter_policy 0x02 and 0x03, but the fact that it did
6067 * generate LE Direct Advertising Report means it is probably broken and
6068 * won't generate any other event which can potentially break
6069 * auto-connect logic so in case local privacy is not enable this
6070 * ignores the direct_addr so it works as a regular report.
6071 */
6072 if (!hci_dev_test_flag(hdev, HCI_MESH) && direct_addr &&
6073 hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6074 direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
6075 &bdaddr_resolved);
6076
6077 /* Only resolvable random addresses are valid for these
6078 * kind of reports and others can be ignored.
6079 */
6080 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
6081 return;
6082
6083 /* If the local IRK of the controller does not match
6084 * with the resolvable random address provided, then
6085 * this report can be ignored.
6086 */
6087 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
6088 return;
6089 }
6090
6091 /* Check if we need to convert to identity address */
6092 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
6093 if (irk) {
6094 bdaddr = &irk->bdaddr;
6095 bdaddr_type = irk->addr_type;
6096 }
6097
6098 bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
6099
6100 /* Check if we have been requested to connect to this device.
6101 *
6102 * direct_addr is set only for directed advertising reports (it is NULL
6103 * for advertising reports) and is already verified to be RPA above.
6104 */
6105 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
6106 type, phy, sec_phy);
6107 if (!ext_adv && conn && type == LE_ADV_IND &&
6108 len <= max_adv_len(hdev)) {
6109 /* Store report for later inclusion by
6110 * mgmt_device_connected
6111 */
6112 memcpy(conn->le_adv_data, data, len);
6113 conn->le_adv_data_len = len;
6114 }
6115
6116 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
6117 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
6118 else
6119 flags = 0;
6120
6121 /* All scan results should be sent up for Mesh systems */
6122 if (hci_dev_test_flag(hdev, HCI_MESH)) {
6123 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6124 rssi, flags, data, len, NULL, 0, instant);
6125 return;
6126 }
6127
6128 /* Passive scanning shouldn't trigger any device found events,
6129 * except for devices marked as CONN_REPORT for which we do send
6130 * device found events, or advertisement monitoring requested.
6131 */
6132 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
6133 if (type == LE_ADV_DIRECT_IND)
6134 return;
6135
6136 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
6137 bdaddr, bdaddr_type) &&
6138 idr_is_empty(&hdev->adv_monitors_idr))
6139 return;
6140
6141 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6142 rssi, flags, data, len, NULL, 0, 0);
6143 return;
6144 }
6145
6146 /* When receiving a scan response, then there is no way to
6147 * know if the remote device is connectable or not. However
6148 * since scan responses are merged with a previously seen
6149 * advertising report, the flags field from that report
6150 * will be used.
6151 *
6152 * In the unlikely case that a controller just sends a scan
6153 * response event that doesn't match the pending report, then
6154 * it is marked as a standalone SCAN_RSP.
6155 */
6156 if (type == LE_ADV_SCAN_RSP)
6157 flags = MGMT_DEV_FOUND_SCAN_RSP;
6158
6159 /* If there's nothing pending either store the data from this
6160 * event or send an immediate device found event if the data
6161 * should not be stored for later.
6162 */
6163 if (!ext_adv && !has_pending_adv_report(hdev)) {
6164 /* If the report will trigger a SCAN_REQ store it for
6165 * later merging.
6166 */
6167 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
6168 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
6169 rssi, flags, data, len);
6170 return;
6171 }
6172
6173 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6174 rssi, flags, data, len, NULL, 0, 0);
6175 return;
6176 }
6177
6178 /* Check if the pending report is for the same device as the new one */
6179 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
6180 bdaddr_type == d->last_adv_addr_type);
6181
6182 /* If the pending data doesn't match this report or this isn't a
6183 * scan response (e.g. we got a duplicate ADV_IND) then force
6184 * sending of the pending data.
6185 */
6186 if (type != LE_ADV_SCAN_RSP || !match) {
6187 /* Send out whatever is in the cache, but skip duplicates */
6188 if (!match)
6189 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
6190 d->last_adv_addr_type, NULL,
6191 d->last_adv_rssi, d->last_adv_flags,
6192 d->last_adv_data,
6193 d->last_adv_data_len, NULL, 0, 0);
6194
6195 /* If the new report will trigger a SCAN_REQ store it for
6196 * later merging.
6197 */
6198 if (!ext_adv && (type == LE_ADV_IND ||
6199 type == LE_ADV_SCAN_IND)) {
6200 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
6201 rssi, flags, data, len);
6202 return;
6203 }
6204
6205 /* The advertising reports cannot be merged, so clear
6206 * the pending report and send out a device found event.
6207 */
6208 clear_pending_adv_report(hdev);
6209 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6210 rssi, flags, data, len, NULL, 0, 0);
6211 return;
6212 }
6213
6214 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
6215 * the new event is a SCAN_RSP. We can therefore proceed with
6216 * sending a merged device found event.
6217 */
6218 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
6219 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
6220 d->last_adv_data, d->last_adv_data_len, data, len, 0);
6221 clear_pending_adv_report(hdev);
6222 }
6223
hci_le_adv_report_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6224 static void hci_le_adv_report_evt(struct hci_dev *hdev, void *data,
6225 struct sk_buff *skb)
6226 {
6227 struct hci_ev_le_advertising_report *ev = data;
6228 u64 instant = jiffies;
6229
6230 if (!ev->num)
6231 return;
6232
6233 hci_dev_lock(hdev);
6234
6235 while (ev->num--) {
6236 struct hci_ev_le_advertising_info *info;
6237 s8 rssi;
6238
6239 info = hci_le_ev_skb_pull(hdev, skb,
6240 HCI_EV_LE_ADVERTISING_REPORT,
6241 sizeof(*info));
6242 if (!info)
6243 break;
6244
6245 if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_ADVERTISING_REPORT,
6246 info->length + 1))
6247 break;
6248
6249 if (info->length <= max_adv_len(hdev)) {
6250 rssi = info->data[info->length];
6251 process_adv_report(hdev, info->type, &info->bdaddr,
6252 info->bdaddr_type, NULL, 0,
6253 HCI_ADV_PHY_1M, 0, rssi,
6254 info->data, info->length, false,
6255 false, instant);
6256 } else {
6257 bt_dev_err(hdev, "Dropping invalid advertising data");
6258 }
6259 }
6260
6261 hci_dev_unlock(hdev);
6262 }
6263
ext_evt_type_to_legacy(struct hci_dev * hdev,u16 evt_type)6264 static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
6265 {
6266 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
6267 switch (evt_type) {
6268 case LE_LEGACY_ADV_IND:
6269 return LE_ADV_IND;
6270 case LE_LEGACY_ADV_DIRECT_IND:
6271 return LE_ADV_DIRECT_IND;
6272 case LE_LEGACY_ADV_SCAN_IND:
6273 return LE_ADV_SCAN_IND;
6274 case LE_LEGACY_NONCONN_IND:
6275 return LE_ADV_NONCONN_IND;
6276 case LE_LEGACY_SCAN_RSP_ADV:
6277 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
6278 return LE_ADV_SCAN_RSP;
6279 }
6280
6281 goto invalid;
6282 }
6283
6284 if (evt_type & LE_EXT_ADV_CONN_IND) {
6285 if (evt_type & LE_EXT_ADV_DIRECT_IND)
6286 return LE_ADV_DIRECT_IND;
6287
6288 return LE_ADV_IND;
6289 }
6290
6291 if (evt_type & LE_EXT_ADV_SCAN_RSP)
6292 return LE_ADV_SCAN_RSP;
6293
6294 if (evt_type & LE_EXT_ADV_SCAN_IND)
6295 return LE_ADV_SCAN_IND;
6296
6297 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
6298 evt_type & LE_EXT_ADV_DIRECT_IND)
6299 return LE_ADV_NONCONN_IND;
6300
6301 invalid:
6302 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
6303 evt_type);
6304
6305 return LE_ADV_INVALID;
6306 }
6307
hci_le_ext_adv_report_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6308 static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, void *data,
6309 struct sk_buff *skb)
6310 {
6311 struct hci_ev_le_ext_adv_report *ev = data;
6312 u64 instant = jiffies;
6313
6314 if (!ev->num)
6315 return;
6316
6317 hci_dev_lock(hdev);
6318
6319 while (ev->num--) {
6320 struct hci_ev_le_ext_adv_info *info;
6321 u8 legacy_evt_type;
6322 u16 evt_type;
6323
6324 info = hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
6325 sizeof(*info));
6326 if (!info)
6327 break;
6328
6329 if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
6330 info->length))
6331 break;
6332
6333 evt_type = __le16_to_cpu(info->type) & LE_EXT_ADV_EVT_TYPE_MASK;
6334 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
6335
6336 if (test_bit(HCI_QUIRK_FIXUP_LE_EXT_ADV_REPORT_PHY,
6337 &hdev->quirks)) {
6338 info->primary_phy &= 0x1f;
6339 info->secondary_phy &= 0x1f;
6340 }
6341
6342 if (legacy_evt_type != LE_ADV_INVALID) {
6343 process_adv_report(hdev, legacy_evt_type, &info->bdaddr,
6344 info->bdaddr_type, NULL, 0,
6345 info->primary_phy,
6346 info->secondary_phy,
6347 info->rssi, info->data, info->length,
6348 !(evt_type & LE_EXT_ADV_LEGACY_PDU),
6349 false, instant);
6350 }
6351 }
6352
6353 hci_dev_unlock(hdev);
6354 }
6355
hci_le_pa_term_sync(struct hci_dev * hdev,__le16 handle)6356 static int hci_le_pa_term_sync(struct hci_dev *hdev, __le16 handle)
6357 {
6358 struct hci_cp_le_pa_term_sync cp;
6359
6360 memset(&cp, 0, sizeof(cp));
6361 cp.handle = handle;
6362
6363 return hci_send_cmd(hdev, HCI_OP_LE_PA_TERM_SYNC, sizeof(cp), &cp);
6364 }
6365
hci_le_pa_sync_estabilished_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6366 static void hci_le_pa_sync_estabilished_evt(struct hci_dev *hdev, void *data,
6367 struct sk_buff *skb)
6368 {
6369 struct hci_ev_le_pa_sync_established *ev = data;
6370 int mask = hdev->link_mode;
6371 __u8 flags = 0;
6372 struct hci_conn *pa_sync, *conn;
6373
6374 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6375
6376 hci_dev_lock(hdev);
6377
6378 hci_dev_clear_flag(hdev, HCI_PA_SYNC);
6379
6380 conn = hci_conn_hash_lookup_sid(hdev, ev->sid, &ev->bdaddr,
6381 ev->bdaddr_type);
6382 if (!conn) {
6383 bt_dev_err(hdev,
6384 "Unable to find connection for dst %pMR sid 0x%2.2x",
6385 &ev->bdaddr, ev->sid);
6386 goto unlock;
6387 }
6388
6389 clear_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
6390
6391 conn->sync_handle = le16_to_cpu(ev->handle);
6392 conn->sid = HCI_SID_INVALID;
6393
6394 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ISO_LINK, &flags);
6395 if (!(mask & HCI_LM_ACCEPT)) {
6396 hci_le_pa_term_sync(hdev, ev->handle);
6397 goto unlock;
6398 }
6399
6400 if (!(flags & HCI_PROTO_DEFER))
6401 goto unlock;
6402
6403 /* Add connection to indicate PA sync event */
6404 pa_sync = hci_conn_add_unset(hdev, ISO_LINK, BDADDR_ANY,
6405 HCI_ROLE_SLAVE);
6406
6407 if (IS_ERR(pa_sync))
6408 goto unlock;
6409
6410 pa_sync->sync_handle = le16_to_cpu(ev->handle);
6411
6412 if (ev->status) {
6413 set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
6414
6415 /* Notify iso layer */
6416 hci_connect_cfm(pa_sync, ev->status);
6417 }
6418
6419 unlock:
6420 /* Handle any other pending PA sync command */
6421 hci_pa_create_sync_pending(hdev);
6422
6423 hci_dev_unlock(hdev);
6424 }
6425
hci_le_per_adv_report_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6426 static void hci_le_per_adv_report_evt(struct hci_dev *hdev, void *data,
6427 struct sk_buff *skb)
6428 {
6429 struct hci_ev_le_per_adv_report *ev = data;
6430 int mask = hdev->link_mode;
6431 __u8 flags = 0;
6432 struct hci_conn *pa_sync;
6433
6434 bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
6435
6436 hci_dev_lock(hdev);
6437
6438 mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, ISO_LINK, &flags);
6439 if (!(mask & HCI_LM_ACCEPT))
6440 goto unlock;
6441
6442 if (!(flags & HCI_PROTO_DEFER))
6443 goto unlock;
6444
6445 pa_sync = hci_conn_hash_lookup_pa_sync_handle
6446 (hdev,
6447 le16_to_cpu(ev->sync_handle));
6448
6449 if (!pa_sync)
6450 goto unlock;
6451
6452 if (ev->data_status == LE_PA_DATA_COMPLETE &&
6453 !test_and_set_bit(HCI_CONN_PA_SYNC, &pa_sync->flags)) {
6454 /* Notify iso layer */
6455 hci_connect_cfm(pa_sync, 0);
6456
6457 /* Notify MGMT layer */
6458 mgmt_device_connected(hdev, pa_sync, NULL, 0);
6459 }
6460
6461 unlock:
6462 hci_dev_unlock(hdev);
6463 }
6464
hci_le_remote_feat_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6465 static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev, void *data,
6466 struct sk_buff *skb)
6467 {
6468 struct hci_ev_le_remote_feat_complete *ev = data;
6469 struct hci_conn *conn;
6470
6471 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6472
6473 hci_dev_lock(hdev);
6474
6475 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6476 if (conn) {
6477 if (!ev->status)
6478 memcpy(conn->features[0], ev->features, 8);
6479
6480 if (conn->state == BT_CONFIG) {
6481 __u8 status;
6482
6483 /* If the local controller supports peripheral-initiated
6484 * features exchange, but the remote controller does
6485 * not, then it is possible that the error code 0x1a
6486 * for unsupported remote feature gets returned.
6487 *
6488 * In this specific case, allow the connection to
6489 * transition into connected state and mark it as
6490 * successful.
6491 */
6492 if (!conn->out && ev->status == HCI_ERROR_UNSUPPORTED_REMOTE_FEATURE &&
6493 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
6494 status = 0x00;
6495 else
6496 status = ev->status;
6497
6498 conn->state = BT_CONNECTED;
6499 hci_connect_cfm(conn, status);
6500 hci_conn_drop(conn);
6501 }
6502 }
6503
6504 hci_dev_unlock(hdev);
6505 }
6506
hci_le_ltk_request_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6507 static void hci_le_ltk_request_evt(struct hci_dev *hdev, void *data,
6508 struct sk_buff *skb)
6509 {
6510 struct hci_ev_le_ltk_req *ev = data;
6511 struct hci_cp_le_ltk_reply cp;
6512 struct hci_cp_le_ltk_neg_reply neg;
6513 struct hci_conn *conn;
6514 struct smp_ltk *ltk;
6515
6516 bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
6517
6518 hci_dev_lock(hdev);
6519
6520 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6521 if (conn == NULL)
6522 goto not_found;
6523
6524 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
6525 if (!ltk)
6526 goto not_found;
6527
6528 if (smp_ltk_is_sc(ltk)) {
6529 /* With SC both EDiv and Rand are set to zero */
6530 if (ev->ediv || ev->rand)
6531 goto not_found;
6532 } else {
6533 /* For non-SC keys check that EDiv and Rand match */
6534 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
6535 goto not_found;
6536 }
6537
6538 memcpy(cp.ltk, ltk->val, ltk->enc_size);
6539 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
6540 cp.handle = cpu_to_le16(conn->handle);
6541
6542 conn->pending_sec_level = smp_ltk_sec_level(ltk);
6543
6544 conn->enc_key_size = ltk->enc_size;
6545
6546 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
6547
6548 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
6549 * temporary key used to encrypt a connection following
6550 * pairing. It is used during the Encrypted Session Setup to
6551 * distribute the keys. Later, security can be re-established
6552 * using a distributed LTK.
6553 */
6554 if (ltk->type == SMP_STK) {
6555 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
6556 list_del_rcu(<k->list);
6557 kfree_rcu(ltk, rcu);
6558 } else {
6559 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
6560 }
6561
6562 hci_dev_unlock(hdev);
6563
6564 return;
6565
6566 not_found:
6567 neg.handle = ev->handle;
6568 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
6569 hci_dev_unlock(hdev);
6570 }
6571
send_conn_param_neg_reply(struct hci_dev * hdev,u16 handle,u8 reason)6572 static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
6573 u8 reason)
6574 {
6575 struct hci_cp_le_conn_param_req_neg_reply cp;
6576
6577 cp.handle = cpu_to_le16(handle);
6578 cp.reason = reason;
6579
6580 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
6581 &cp);
6582 }
6583
hci_le_remote_conn_param_req_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6584 static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev, void *data,
6585 struct sk_buff *skb)
6586 {
6587 struct hci_ev_le_remote_conn_param_req *ev = data;
6588 struct hci_cp_le_conn_param_req_reply cp;
6589 struct hci_conn *hcon;
6590 u16 handle, min, max, latency, timeout;
6591
6592 bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
6593
6594 handle = le16_to_cpu(ev->handle);
6595 min = le16_to_cpu(ev->interval_min);
6596 max = le16_to_cpu(ev->interval_max);
6597 latency = le16_to_cpu(ev->latency);
6598 timeout = le16_to_cpu(ev->timeout);
6599
6600 hcon = hci_conn_hash_lookup_handle(hdev, handle);
6601 if (!hcon || hcon->state != BT_CONNECTED)
6602 return send_conn_param_neg_reply(hdev, handle,
6603 HCI_ERROR_UNKNOWN_CONN_ID);
6604
6605 if (max > hcon->le_conn_max_interval)
6606 return send_conn_param_neg_reply(hdev, handle,
6607 HCI_ERROR_INVALID_LL_PARAMS);
6608
6609 if (hci_check_conn_params(min, max, latency, timeout))
6610 return send_conn_param_neg_reply(hdev, handle,
6611 HCI_ERROR_INVALID_LL_PARAMS);
6612
6613 if (hcon->role == HCI_ROLE_MASTER) {
6614 struct hci_conn_params *params;
6615 u8 store_hint;
6616
6617 hci_dev_lock(hdev);
6618
6619 params = hci_conn_params_lookup(hdev, &hcon->dst,
6620 hcon->dst_type);
6621 if (params) {
6622 params->conn_min_interval = min;
6623 params->conn_max_interval = max;
6624 params->conn_latency = latency;
6625 params->supervision_timeout = timeout;
6626 store_hint = 0x01;
6627 } else {
6628 store_hint = 0x00;
6629 }
6630
6631 hci_dev_unlock(hdev);
6632
6633 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6634 store_hint, min, max, latency, timeout);
6635 }
6636
6637 cp.handle = ev->handle;
6638 cp.interval_min = ev->interval_min;
6639 cp.interval_max = ev->interval_max;
6640 cp.latency = ev->latency;
6641 cp.timeout = ev->timeout;
6642 cp.min_ce_len = 0;
6643 cp.max_ce_len = 0;
6644
6645 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6646 }
6647
hci_le_direct_adv_report_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6648 static void hci_le_direct_adv_report_evt(struct hci_dev *hdev, void *data,
6649 struct sk_buff *skb)
6650 {
6651 struct hci_ev_le_direct_adv_report *ev = data;
6652 u64 instant = jiffies;
6653 int i;
6654
6655 if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_DIRECT_ADV_REPORT,
6656 flex_array_size(ev, info, ev->num)))
6657 return;
6658
6659 if (!ev->num)
6660 return;
6661
6662 hci_dev_lock(hdev);
6663
6664 for (i = 0; i < ev->num; i++) {
6665 struct hci_ev_le_direct_adv_info *info = &ev->info[i];
6666
6667 process_adv_report(hdev, info->type, &info->bdaddr,
6668 info->bdaddr_type, &info->direct_addr,
6669 info->direct_addr_type, HCI_ADV_PHY_1M, 0,
6670 info->rssi, NULL, 0, false, false, instant);
6671 }
6672
6673 hci_dev_unlock(hdev);
6674 }
6675
hci_le_phy_update_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6676 static void hci_le_phy_update_evt(struct hci_dev *hdev, void *data,
6677 struct sk_buff *skb)
6678 {
6679 struct hci_ev_le_phy_update_complete *ev = data;
6680 struct hci_conn *conn;
6681
6682 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6683
6684 if (ev->status)
6685 return;
6686
6687 hci_dev_lock(hdev);
6688
6689 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6690 if (!conn)
6691 goto unlock;
6692
6693 conn->le_tx_phy = ev->tx_phy;
6694 conn->le_rx_phy = ev->rx_phy;
6695
6696 unlock:
6697 hci_dev_unlock(hdev);
6698 }
6699
hci_le_cis_estabilished_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6700 static void hci_le_cis_estabilished_evt(struct hci_dev *hdev, void *data,
6701 struct sk_buff *skb)
6702 {
6703 struct hci_evt_le_cis_established *ev = data;
6704 struct hci_conn *conn;
6705 struct bt_iso_qos *qos;
6706 bool pending = false;
6707 u16 handle = __le16_to_cpu(ev->handle);
6708 u32 c_sdu_interval, p_sdu_interval;
6709
6710 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6711
6712 hci_dev_lock(hdev);
6713
6714 conn = hci_conn_hash_lookup_handle(hdev, handle);
6715 if (!conn) {
6716 bt_dev_err(hdev,
6717 "Unable to find connection with handle 0x%4.4x",
6718 handle);
6719 goto unlock;
6720 }
6721
6722 if (conn->type != ISO_LINK) {
6723 bt_dev_err(hdev,
6724 "Invalid connection link type handle 0x%4.4x",
6725 handle);
6726 goto unlock;
6727 }
6728
6729 qos = &conn->iso_qos;
6730
6731 pending = test_and_clear_bit(HCI_CONN_CREATE_CIS, &conn->flags);
6732
6733 /* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 6, Part G
6734 * page 3075:
6735 * Transport_Latency_C_To_P = CIG_Sync_Delay + (FT_C_To_P) ×
6736 * ISO_Interval + SDU_Interval_C_To_P
6737 * ...
6738 * SDU_Interval = (CIG_Sync_Delay + (FT) x ISO_Interval) -
6739 * Transport_Latency
6740 */
6741 c_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
6742 (ev->c_ft * le16_to_cpu(ev->interval) * 1250)) -
6743 get_unaligned_le24(ev->c_latency);
6744 p_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
6745 (ev->p_ft * le16_to_cpu(ev->interval) * 1250)) -
6746 get_unaligned_le24(ev->p_latency);
6747
6748 switch (conn->role) {
6749 case HCI_ROLE_SLAVE:
6750 qos->ucast.in.interval = c_sdu_interval;
6751 qos->ucast.out.interval = p_sdu_interval;
6752 /* Convert Transport Latency (us) to Latency (msec) */
6753 qos->ucast.in.latency =
6754 DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
6755 1000);
6756 qos->ucast.out.latency =
6757 DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
6758 1000);
6759 qos->ucast.in.sdu = le16_to_cpu(ev->c_mtu);
6760 qos->ucast.out.sdu = le16_to_cpu(ev->p_mtu);
6761 qos->ucast.in.phy = ev->c_phy;
6762 qos->ucast.out.phy = ev->p_phy;
6763 break;
6764 case HCI_ROLE_MASTER:
6765 qos->ucast.in.interval = p_sdu_interval;
6766 qos->ucast.out.interval = c_sdu_interval;
6767 /* Convert Transport Latency (us) to Latency (msec) */
6768 qos->ucast.out.latency =
6769 DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
6770 1000);
6771 qos->ucast.in.latency =
6772 DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
6773 1000);
6774 qos->ucast.out.sdu = le16_to_cpu(ev->c_mtu);
6775 qos->ucast.in.sdu = le16_to_cpu(ev->p_mtu);
6776 qos->ucast.out.phy = ev->c_phy;
6777 qos->ucast.in.phy = ev->p_phy;
6778 break;
6779 }
6780
6781 if (!ev->status) {
6782 conn->state = BT_CONNECTED;
6783 hci_debugfs_create_conn(conn);
6784 hci_conn_add_sysfs(conn);
6785 hci_iso_setup_path(conn);
6786 goto unlock;
6787 }
6788
6789 conn->state = BT_CLOSED;
6790 hci_connect_cfm(conn, ev->status);
6791 hci_conn_del(conn);
6792
6793 unlock:
6794 if (pending)
6795 hci_le_create_cis_pending(hdev);
6796
6797 hci_dev_unlock(hdev);
6798 }
6799
hci_le_reject_cis(struct hci_dev * hdev,__le16 handle)6800 static void hci_le_reject_cis(struct hci_dev *hdev, __le16 handle)
6801 {
6802 struct hci_cp_le_reject_cis cp;
6803
6804 memset(&cp, 0, sizeof(cp));
6805 cp.handle = handle;
6806 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
6807 hci_send_cmd(hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
6808 }
6809
hci_le_accept_cis(struct hci_dev * hdev,__le16 handle)6810 static void hci_le_accept_cis(struct hci_dev *hdev, __le16 handle)
6811 {
6812 struct hci_cp_le_accept_cis cp;
6813
6814 memset(&cp, 0, sizeof(cp));
6815 cp.handle = handle;
6816 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
6817 }
6818
hci_le_cis_req_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6819 static void hci_le_cis_req_evt(struct hci_dev *hdev, void *data,
6820 struct sk_buff *skb)
6821 {
6822 struct hci_evt_le_cis_req *ev = data;
6823 u16 acl_handle, cis_handle;
6824 struct hci_conn *acl, *cis;
6825 int mask;
6826 __u8 flags = 0;
6827
6828 acl_handle = __le16_to_cpu(ev->acl_handle);
6829 cis_handle = __le16_to_cpu(ev->cis_handle);
6830
6831 bt_dev_dbg(hdev, "acl 0x%4.4x handle 0x%4.4x cig 0x%2.2x cis 0x%2.2x",
6832 acl_handle, cis_handle, ev->cig_id, ev->cis_id);
6833
6834 hci_dev_lock(hdev);
6835
6836 acl = hci_conn_hash_lookup_handle(hdev, acl_handle);
6837 if (!acl)
6838 goto unlock;
6839
6840 mask = hci_proto_connect_ind(hdev, &acl->dst, ISO_LINK, &flags);
6841 if (!(mask & HCI_LM_ACCEPT)) {
6842 hci_le_reject_cis(hdev, ev->cis_handle);
6843 goto unlock;
6844 }
6845
6846 cis = hci_conn_hash_lookup_handle(hdev, cis_handle);
6847 if (!cis) {
6848 cis = hci_conn_add(hdev, ISO_LINK, &acl->dst, HCI_ROLE_SLAVE,
6849 cis_handle);
6850 if (IS_ERR(cis)) {
6851 hci_le_reject_cis(hdev, ev->cis_handle);
6852 goto unlock;
6853 }
6854 }
6855
6856 cis->iso_qos.ucast.cig = ev->cig_id;
6857 cis->iso_qos.ucast.cis = ev->cis_id;
6858
6859 if (!(flags & HCI_PROTO_DEFER)) {
6860 hci_le_accept_cis(hdev, ev->cis_handle);
6861 } else {
6862 cis->state = BT_CONNECT2;
6863 hci_connect_cfm(cis, 0);
6864 }
6865
6866 unlock:
6867 hci_dev_unlock(hdev);
6868 }
6869
hci_iso_term_big_sync(struct hci_dev * hdev,void * data)6870 static int hci_iso_term_big_sync(struct hci_dev *hdev, void *data)
6871 {
6872 u8 handle = PTR_UINT(data);
6873
6874 return hci_le_terminate_big_sync(hdev, handle,
6875 HCI_ERROR_LOCAL_HOST_TERM);
6876 }
6877
hci_le_create_big_complete_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6878 static void hci_le_create_big_complete_evt(struct hci_dev *hdev, void *data,
6879 struct sk_buff *skb)
6880 {
6881 struct hci_evt_le_create_big_complete *ev = data;
6882 struct hci_conn *conn;
6883 __u8 i = 0;
6884
6885 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6886
6887 if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_CREATE_BIG_COMPLETE,
6888 flex_array_size(ev, bis_handle, ev->num_bis)))
6889 return;
6890
6891 hci_dev_lock(hdev);
6892
6893 /* Connect all BISes that are bound to the BIG */
6894 while ((conn = hci_conn_hash_lookup_big_state(hdev, ev->handle,
6895 BT_BOUND))) {
6896 if (ev->status) {
6897 hci_connect_cfm(conn, ev->status);
6898 hci_conn_del(conn);
6899 continue;
6900 }
6901
6902 if (hci_conn_set_handle(conn,
6903 __le16_to_cpu(ev->bis_handle[i++])))
6904 continue;
6905
6906 conn->state = BT_CONNECTED;
6907 set_bit(HCI_CONN_BIG_CREATED, &conn->flags);
6908 hci_debugfs_create_conn(conn);
6909 hci_conn_add_sysfs(conn);
6910 hci_iso_setup_path(conn);
6911 }
6912
6913 if (!ev->status && !i)
6914 /* If no BISes have been connected for the BIG,
6915 * terminate. This is in case all bound connections
6916 * have been closed before the BIG creation
6917 * has completed.
6918 */
6919 hci_cmd_sync_queue(hdev, hci_iso_term_big_sync,
6920 UINT_PTR(ev->handle), NULL);
6921
6922 hci_dev_unlock(hdev);
6923 }
6924
hci_le_big_sync_established_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)6925 static void hci_le_big_sync_established_evt(struct hci_dev *hdev, void *data,
6926 struct sk_buff *skb)
6927 {
6928 struct hci_evt_le_big_sync_estabilished *ev = data;
6929 struct hci_conn *bis, *conn;
6930 int i;
6931
6932 bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
6933
6934 if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_BIG_SYNC_ESTABILISHED,
6935 flex_array_size(ev, bis, ev->num_bis)))
6936 return;
6937
6938 hci_dev_lock(hdev);
6939
6940 conn = hci_conn_hash_lookup_big_sync_pend(hdev, ev->handle,
6941 ev->num_bis);
6942 if (!conn) {
6943 bt_dev_err(hdev,
6944 "Unable to find connection for big 0x%2.2x",
6945 ev->handle);
6946 goto unlock;
6947 }
6948
6949 clear_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
6950
6951 conn->num_bis = 0;
6952 memset(conn->bis, 0, sizeof(conn->num_bis));
6953
6954 for (i = 0; i < ev->num_bis; i++) {
6955 u16 handle = le16_to_cpu(ev->bis[i]);
6956 __le32 interval;
6957
6958 bis = hci_conn_hash_lookup_handle(hdev, handle);
6959 if (!bis) {
6960 if (handle > HCI_CONN_HANDLE_MAX) {
6961 bt_dev_dbg(hdev, "ignore too large handle %u", handle);
6962 continue;
6963 }
6964 bis = hci_conn_add(hdev, ISO_LINK, BDADDR_ANY,
6965 HCI_ROLE_SLAVE, handle);
6966 if (IS_ERR(bis))
6967 continue;
6968 }
6969
6970 if (ev->status != 0x42)
6971 /* Mark PA sync as established */
6972 set_bit(HCI_CONN_PA_SYNC, &bis->flags);
6973
6974 bis->sync_handle = conn->sync_handle;
6975 bis->iso_qos.bcast.big = ev->handle;
6976 memset(&interval, 0, sizeof(interval));
6977 memcpy(&interval, ev->latency, sizeof(ev->latency));
6978 bis->iso_qos.bcast.in.interval = le32_to_cpu(interval);
6979 /* Convert ISO Interval (1.25 ms slots) to latency (ms) */
6980 bis->iso_qos.bcast.in.latency = le16_to_cpu(ev->interval) * 125 / 100;
6981 bis->iso_qos.bcast.in.sdu = le16_to_cpu(ev->max_pdu);
6982
6983 if (!ev->status) {
6984 set_bit(HCI_CONN_BIG_SYNC, &bis->flags);
6985 hci_iso_setup_path(bis);
6986 }
6987 }
6988
6989 /* In case BIG sync failed, notify each failed connection to
6990 * the user after all hci connections have been added
6991 */
6992 if (ev->status)
6993 for (i = 0; i < ev->num_bis; i++) {
6994 u16 handle = le16_to_cpu(ev->bis[i]);
6995
6996 bis = hci_conn_hash_lookup_handle(hdev, handle);
6997 if (!bis)
6998 continue;
6999
7000 set_bit(HCI_CONN_BIG_SYNC_FAILED, &bis->flags);
7001 hci_connect_cfm(bis, ev->status);
7002 }
7003
7004 unlock:
7005 /* Handle any other pending BIG sync command */
7006 hci_le_big_create_sync_pending(hdev);
7007
7008 hci_dev_unlock(hdev);
7009 }
7010
hci_le_big_info_adv_report_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb)7011 static void hci_le_big_info_adv_report_evt(struct hci_dev *hdev, void *data,
7012 struct sk_buff *skb)
7013 {
7014 struct hci_evt_le_big_info_adv_report *ev = data;
7015 int mask = hdev->link_mode;
7016 __u8 flags = 0;
7017 struct hci_conn *pa_sync;
7018
7019 bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
7020
7021 hci_dev_lock(hdev);
7022
7023 mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, ISO_LINK, &flags);
7024 if (!(mask & HCI_LM_ACCEPT))
7025 goto unlock;
7026
7027 if (!(flags & HCI_PROTO_DEFER))
7028 goto unlock;
7029
7030 pa_sync = hci_conn_hash_lookup_pa_sync_handle
7031 (hdev,
7032 le16_to_cpu(ev->sync_handle));
7033
7034 if (!pa_sync)
7035 goto unlock;
7036
7037 pa_sync->iso_qos.bcast.encryption = ev->encryption;
7038
7039 /* Notify iso layer */
7040 hci_connect_cfm(pa_sync, 0);
7041
7042 unlock:
7043 hci_dev_unlock(hdev);
7044 }
7045
7046 #define HCI_LE_EV_VL(_op, _func, _min_len, _max_len) \
7047 [_op] = { \
7048 .func = _func, \
7049 .min_len = _min_len, \
7050 .max_len = _max_len, \
7051 }
7052
7053 #define HCI_LE_EV(_op, _func, _len) \
7054 HCI_LE_EV_VL(_op, _func, _len, _len)
7055
7056 #define HCI_LE_EV_STATUS(_op, _func) \
7057 HCI_LE_EV(_op, _func, sizeof(struct hci_ev_status))
7058
7059 /* Entries in this table shall have their position according to the subevent
7060 * opcode they handle so the use of the macros above is recommend since it does
7061 * attempt to initialize at its proper index using Designated Initializers that
7062 * way events without a callback function can be ommited.
7063 */
7064 static const struct hci_le_ev {
7065 void (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
7066 u16 min_len;
7067 u16 max_len;
7068 } hci_le_ev_table[U8_MAX + 1] = {
7069 /* [0x01 = HCI_EV_LE_CONN_COMPLETE] */
7070 HCI_LE_EV(HCI_EV_LE_CONN_COMPLETE, hci_le_conn_complete_evt,
7071 sizeof(struct hci_ev_le_conn_complete)),
7072 /* [0x02 = HCI_EV_LE_ADVERTISING_REPORT] */
7073 HCI_LE_EV_VL(HCI_EV_LE_ADVERTISING_REPORT, hci_le_adv_report_evt,
7074 sizeof(struct hci_ev_le_advertising_report),
7075 HCI_MAX_EVENT_SIZE),
7076 /* [0x03 = HCI_EV_LE_CONN_UPDATE_COMPLETE] */
7077 HCI_LE_EV(HCI_EV_LE_CONN_UPDATE_COMPLETE,
7078 hci_le_conn_update_complete_evt,
7079 sizeof(struct hci_ev_le_conn_update_complete)),
7080 /* [0x04 = HCI_EV_LE_REMOTE_FEAT_COMPLETE] */
7081 HCI_LE_EV(HCI_EV_LE_REMOTE_FEAT_COMPLETE,
7082 hci_le_remote_feat_complete_evt,
7083 sizeof(struct hci_ev_le_remote_feat_complete)),
7084 /* [0x05 = HCI_EV_LE_LTK_REQ] */
7085 HCI_LE_EV(HCI_EV_LE_LTK_REQ, hci_le_ltk_request_evt,
7086 sizeof(struct hci_ev_le_ltk_req)),
7087 /* [0x06 = HCI_EV_LE_REMOTE_CONN_PARAM_REQ] */
7088 HCI_LE_EV(HCI_EV_LE_REMOTE_CONN_PARAM_REQ,
7089 hci_le_remote_conn_param_req_evt,
7090 sizeof(struct hci_ev_le_remote_conn_param_req)),
7091 /* [0x0a = HCI_EV_LE_ENHANCED_CONN_COMPLETE] */
7092 HCI_LE_EV(HCI_EV_LE_ENHANCED_CONN_COMPLETE,
7093 hci_le_enh_conn_complete_evt,
7094 sizeof(struct hci_ev_le_enh_conn_complete)),
7095 /* [0x0b = HCI_EV_LE_DIRECT_ADV_REPORT] */
7096 HCI_LE_EV_VL(HCI_EV_LE_DIRECT_ADV_REPORT, hci_le_direct_adv_report_evt,
7097 sizeof(struct hci_ev_le_direct_adv_report),
7098 HCI_MAX_EVENT_SIZE),
7099 /* [0x0c = HCI_EV_LE_PHY_UPDATE_COMPLETE] */
7100 HCI_LE_EV(HCI_EV_LE_PHY_UPDATE_COMPLETE, hci_le_phy_update_evt,
7101 sizeof(struct hci_ev_le_phy_update_complete)),
7102 /* [0x0d = HCI_EV_LE_EXT_ADV_REPORT] */
7103 HCI_LE_EV_VL(HCI_EV_LE_EXT_ADV_REPORT, hci_le_ext_adv_report_evt,
7104 sizeof(struct hci_ev_le_ext_adv_report),
7105 HCI_MAX_EVENT_SIZE),
7106 /* [0x0e = HCI_EV_LE_PA_SYNC_ESTABLISHED] */
7107 HCI_LE_EV(HCI_EV_LE_PA_SYNC_ESTABLISHED,
7108 hci_le_pa_sync_estabilished_evt,
7109 sizeof(struct hci_ev_le_pa_sync_established)),
7110 /* [0x0f = HCI_EV_LE_PER_ADV_REPORT] */
7111 HCI_LE_EV_VL(HCI_EV_LE_PER_ADV_REPORT,
7112 hci_le_per_adv_report_evt,
7113 sizeof(struct hci_ev_le_per_adv_report),
7114 HCI_MAX_EVENT_SIZE),
7115 /* [0x12 = HCI_EV_LE_EXT_ADV_SET_TERM] */
7116 HCI_LE_EV(HCI_EV_LE_EXT_ADV_SET_TERM, hci_le_ext_adv_term_evt,
7117 sizeof(struct hci_evt_le_ext_adv_set_term)),
7118 /* [0x19 = HCI_EVT_LE_CIS_ESTABLISHED] */
7119 HCI_LE_EV(HCI_EVT_LE_CIS_ESTABLISHED, hci_le_cis_estabilished_evt,
7120 sizeof(struct hci_evt_le_cis_established)),
7121 /* [0x1a = HCI_EVT_LE_CIS_REQ] */
7122 HCI_LE_EV(HCI_EVT_LE_CIS_REQ, hci_le_cis_req_evt,
7123 sizeof(struct hci_evt_le_cis_req)),
7124 /* [0x1b = HCI_EVT_LE_CREATE_BIG_COMPLETE] */
7125 HCI_LE_EV_VL(HCI_EVT_LE_CREATE_BIG_COMPLETE,
7126 hci_le_create_big_complete_evt,
7127 sizeof(struct hci_evt_le_create_big_complete),
7128 HCI_MAX_EVENT_SIZE),
7129 /* [0x1d = HCI_EV_LE_BIG_SYNC_ESTABILISHED] */
7130 HCI_LE_EV_VL(HCI_EVT_LE_BIG_SYNC_ESTABILISHED,
7131 hci_le_big_sync_established_evt,
7132 sizeof(struct hci_evt_le_big_sync_estabilished),
7133 HCI_MAX_EVENT_SIZE),
7134 /* [0x22 = HCI_EVT_LE_BIG_INFO_ADV_REPORT] */
7135 HCI_LE_EV_VL(HCI_EVT_LE_BIG_INFO_ADV_REPORT,
7136 hci_le_big_info_adv_report_evt,
7137 sizeof(struct hci_evt_le_big_info_adv_report),
7138 HCI_MAX_EVENT_SIZE),
7139 };
7140
hci_le_meta_evt(struct hci_dev * hdev,void * data,struct sk_buff * skb,u16 * opcode,u8 * status,hci_req_complete_t * req_complete,hci_req_complete_skb_t * req_complete_skb)7141 static void hci_le_meta_evt(struct hci_dev *hdev, void *data,
7142 struct sk_buff *skb, u16 *opcode, u8 *status,
7143 hci_req_complete_t *req_complete,
7144 hci_req_complete_skb_t *req_complete_skb)
7145 {
7146 struct hci_ev_le_meta *ev = data;
7147 const struct hci_le_ev *subev;
7148
7149 bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent);
7150
7151 /* Only match event if command OGF is for LE */
7152 if (hdev->req_skb &&
7153 hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) == 0x08 &&
7154 hci_skb_event(hdev->req_skb) == ev->subevent) {
7155 *opcode = hci_skb_opcode(hdev->req_skb);
7156 hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete,
7157 req_complete_skb);
7158 }
7159
7160 subev = &hci_le_ev_table[ev->subevent];
7161 if (!subev->func)
7162 return;
7163
7164 if (skb->len < subev->min_len) {
7165 bt_dev_err(hdev, "unexpected subevent 0x%2.2x length: %u < %u",
7166 ev->subevent, skb->len, subev->min_len);
7167 return;
7168 }
7169
7170 /* Just warn if the length is over max_len size it still be
7171 * possible to partially parse the event so leave to callback to
7172 * decide if that is acceptable.
7173 */
7174 if (skb->len > subev->max_len)
7175 bt_dev_warn(hdev, "unexpected subevent 0x%2.2x length: %u > %u",
7176 ev->subevent, skb->len, subev->max_len);
7177 data = hci_le_ev_skb_pull(hdev, skb, ev->subevent, subev->min_len);
7178 if (!data)
7179 return;
7180
7181 subev->func(hdev, data, skb);
7182 }
7183
hci_get_cmd_complete(struct hci_dev * hdev,u16 opcode,u8 event,struct sk_buff * skb)7184 static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
7185 u8 event, struct sk_buff *skb)
7186 {
7187 struct hci_ev_cmd_complete *ev;
7188 struct hci_event_hdr *hdr;
7189
7190 if (!skb)
7191 return false;
7192
7193 hdr = hci_ev_skb_pull(hdev, skb, event, sizeof(*hdr));
7194 if (!hdr)
7195 return false;
7196
7197 if (event) {
7198 if (hdr->evt != event)
7199 return false;
7200 return true;
7201 }
7202
7203 /* Check if request ended in Command Status - no way to retrieve
7204 * any extra parameters in this case.
7205 */
7206 if (hdr->evt == HCI_EV_CMD_STATUS)
7207 return false;
7208
7209 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
7210 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
7211 hdr->evt);
7212 return false;
7213 }
7214
7215 ev = hci_cc_skb_pull(hdev, skb, opcode, sizeof(*ev));
7216 if (!ev)
7217 return false;
7218
7219 if (opcode != __le16_to_cpu(ev->opcode)) {
7220 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
7221 __le16_to_cpu(ev->opcode));
7222 return false;
7223 }
7224
7225 return true;
7226 }
7227
hci_store_wake_reason(struct hci_dev * hdev,u8 event,struct sk_buff * skb)7228 static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
7229 struct sk_buff *skb)
7230 {
7231 struct hci_ev_le_advertising_info *adv;
7232 struct hci_ev_le_direct_adv_info *direct_adv;
7233 struct hci_ev_le_ext_adv_info *ext_adv;
7234 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
7235 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
7236
7237 hci_dev_lock(hdev);
7238
7239 /* If we are currently suspended and this is the first BT event seen,
7240 * save the wake reason associated with the event.
7241 */
7242 if (!hdev->suspended || hdev->wake_reason)
7243 goto unlock;
7244
7245 /* Default to remote wake. Values for wake_reason are documented in the
7246 * Bluez mgmt api docs.
7247 */
7248 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
7249
7250 /* Once configured for remote wakeup, we should only wake up for
7251 * reconnections. It's useful to see which device is waking us up so
7252 * keep track of the bdaddr of the connection event that woke us up.
7253 */
7254 if (event == HCI_EV_CONN_REQUEST) {
7255 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
7256 hdev->wake_addr_type = BDADDR_BREDR;
7257 } else if (event == HCI_EV_CONN_COMPLETE) {
7258 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
7259 hdev->wake_addr_type = BDADDR_BREDR;
7260 } else if (event == HCI_EV_LE_META) {
7261 struct hci_ev_le_meta *le_ev = (void *)skb->data;
7262 u8 subevent = le_ev->subevent;
7263 u8 *ptr = &skb->data[sizeof(*le_ev)];
7264 u8 num_reports = *ptr;
7265
7266 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
7267 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
7268 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
7269 num_reports) {
7270 adv = (void *)(ptr + 1);
7271 direct_adv = (void *)(ptr + 1);
7272 ext_adv = (void *)(ptr + 1);
7273
7274 switch (subevent) {
7275 case HCI_EV_LE_ADVERTISING_REPORT:
7276 bacpy(&hdev->wake_addr, &adv->bdaddr);
7277 hdev->wake_addr_type = adv->bdaddr_type;
7278 break;
7279 case HCI_EV_LE_DIRECT_ADV_REPORT:
7280 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
7281 hdev->wake_addr_type = direct_adv->bdaddr_type;
7282 break;
7283 case HCI_EV_LE_EXT_ADV_REPORT:
7284 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
7285 hdev->wake_addr_type = ext_adv->bdaddr_type;
7286 break;
7287 }
7288 }
7289 } else {
7290 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
7291 }
7292
7293 unlock:
7294 hci_dev_unlock(hdev);
7295 }
7296
7297 #define HCI_EV_VL(_op, _func, _min_len, _max_len) \
7298 [_op] = { \
7299 .req = false, \
7300 .func = _func, \
7301 .min_len = _min_len, \
7302 .max_len = _max_len, \
7303 }
7304
7305 #define HCI_EV(_op, _func, _len) \
7306 HCI_EV_VL(_op, _func, _len, _len)
7307
7308 #define HCI_EV_STATUS(_op, _func) \
7309 HCI_EV(_op, _func, sizeof(struct hci_ev_status))
7310
7311 #define HCI_EV_REQ_VL(_op, _func, _min_len, _max_len) \
7312 [_op] = { \
7313 .req = true, \
7314 .func_req = _func, \
7315 .min_len = _min_len, \
7316 .max_len = _max_len, \
7317 }
7318
7319 #define HCI_EV_REQ(_op, _func, _len) \
7320 HCI_EV_REQ_VL(_op, _func, _len, _len)
7321
7322 /* Entries in this table shall have their position according to the event opcode
7323 * they handle so the use of the macros above is recommend since it does attempt
7324 * to initialize at its proper index using Designated Initializers that way
7325 * events without a callback function don't have entered.
7326 */
7327 static const struct hci_ev {
7328 bool req;
7329 union {
7330 void (*func)(struct hci_dev *hdev, void *data,
7331 struct sk_buff *skb);
7332 void (*func_req)(struct hci_dev *hdev, void *data,
7333 struct sk_buff *skb, u16 *opcode, u8 *status,
7334 hci_req_complete_t *req_complete,
7335 hci_req_complete_skb_t *req_complete_skb);
7336 };
7337 u16 min_len;
7338 u16 max_len;
7339 } hci_ev_table[U8_MAX + 1] = {
7340 /* [0x01 = HCI_EV_INQUIRY_COMPLETE] */
7341 HCI_EV_STATUS(HCI_EV_INQUIRY_COMPLETE, hci_inquiry_complete_evt),
7342 /* [0x02 = HCI_EV_INQUIRY_RESULT] */
7343 HCI_EV_VL(HCI_EV_INQUIRY_RESULT, hci_inquiry_result_evt,
7344 sizeof(struct hci_ev_inquiry_result), HCI_MAX_EVENT_SIZE),
7345 /* [0x03 = HCI_EV_CONN_COMPLETE] */
7346 HCI_EV(HCI_EV_CONN_COMPLETE, hci_conn_complete_evt,
7347 sizeof(struct hci_ev_conn_complete)),
7348 /* [0x04 = HCI_EV_CONN_REQUEST] */
7349 HCI_EV(HCI_EV_CONN_REQUEST, hci_conn_request_evt,
7350 sizeof(struct hci_ev_conn_request)),
7351 /* [0x05 = HCI_EV_DISCONN_COMPLETE] */
7352 HCI_EV(HCI_EV_DISCONN_COMPLETE, hci_disconn_complete_evt,
7353 sizeof(struct hci_ev_disconn_complete)),
7354 /* [0x06 = HCI_EV_AUTH_COMPLETE] */
7355 HCI_EV(HCI_EV_AUTH_COMPLETE, hci_auth_complete_evt,
7356 sizeof(struct hci_ev_auth_complete)),
7357 /* [0x07 = HCI_EV_REMOTE_NAME] */
7358 HCI_EV(HCI_EV_REMOTE_NAME, hci_remote_name_evt,
7359 sizeof(struct hci_ev_remote_name)),
7360 /* [0x08 = HCI_EV_ENCRYPT_CHANGE] */
7361 HCI_EV(HCI_EV_ENCRYPT_CHANGE, hci_encrypt_change_evt,
7362 sizeof(struct hci_ev_encrypt_change)),
7363 /* [0x09 = HCI_EV_CHANGE_LINK_KEY_COMPLETE] */
7364 HCI_EV(HCI_EV_CHANGE_LINK_KEY_COMPLETE,
7365 hci_change_link_key_complete_evt,
7366 sizeof(struct hci_ev_change_link_key_complete)),
7367 /* [0x0b = HCI_EV_REMOTE_FEATURES] */
7368 HCI_EV(HCI_EV_REMOTE_FEATURES, hci_remote_features_evt,
7369 sizeof(struct hci_ev_remote_features)),
7370 /* [0x0e = HCI_EV_CMD_COMPLETE] */
7371 HCI_EV_REQ_VL(HCI_EV_CMD_COMPLETE, hci_cmd_complete_evt,
7372 sizeof(struct hci_ev_cmd_complete), HCI_MAX_EVENT_SIZE),
7373 /* [0x0f = HCI_EV_CMD_STATUS] */
7374 HCI_EV_REQ(HCI_EV_CMD_STATUS, hci_cmd_status_evt,
7375 sizeof(struct hci_ev_cmd_status)),
7376 /* [0x10 = HCI_EV_CMD_STATUS] */
7377 HCI_EV(HCI_EV_HARDWARE_ERROR, hci_hardware_error_evt,
7378 sizeof(struct hci_ev_hardware_error)),
7379 /* [0x12 = HCI_EV_ROLE_CHANGE] */
7380 HCI_EV(HCI_EV_ROLE_CHANGE, hci_role_change_evt,
7381 sizeof(struct hci_ev_role_change)),
7382 /* [0x13 = HCI_EV_NUM_COMP_PKTS] */
7383 HCI_EV_VL(HCI_EV_NUM_COMP_PKTS, hci_num_comp_pkts_evt,
7384 sizeof(struct hci_ev_num_comp_pkts), HCI_MAX_EVENT_SIZE),
7385 /* [0x14 = HCI_EV_MODE_CHANGE] */
7386 HCI_EV(HCI_EV_MODE_CHANGE, hci_mode_change_evt,
7387 sizeof(struct hci_ev_mode_change)),
7388 /* [0x16 = HCI_EV_PIN_CODE_REQ] */
7389 HCI_EV(HCI_EV_PIN_CODE_REQ, hci_pin_code_request_evt,
7390 sizeof(struct hci_ev_pin_code_req)),
7391 /* [0x17 = HCI_EV_LINK_KEY_REQ] */
7392 HCI_EV(HCI_EV_LINK_KEY_REQ, hci_link_key_request_evt,
7393 sizeof(struct hci_ev_link_key_req)),
7394 /* [0x18 = HCI_EV_LINK_KEY_NOTIFY] */
7395 HCI_EV(HCI_EV_LINK_KEY_NOTIFY, hci_link_key_notify_evt,
7396 sizeof(struct hci_ev_link_key_notify)),
7397 /* [0x1c = HCI_EV_CLOCK_OFFSET] */
7398 HCI_EV(HCI_EV_CLOCK_OFFSET, hci_clock_offset_evt,
7399 sizeof(struct hci_ev_clock_offset)),
7400 /* [0x1d = HCI_EV_PKT_TYPE_CHANGE] */
7401 HCI_EV(HCI_EV_PKT_TYPE_CHANGE, hci_pkt_type_change_evt,
7402 sizeof(struct hci_ev_pkt_type_change)),
7403 /* [0x20 = HCI_EV_PSCAN_REP_MODE] */
7404 HCI_EV(HCI_EV_PSCAN_REP_MODE, hci_pscan_rep_mode_evt,
7405 sizeof(struct hci_ev_pscan_rep_mode)),
7406 /* [0x22 = HCI_EV_INQUIRY_RESULT_WITH_RSSI] */
7407 HCI_EV_VL(HCI_EV_INQUIRY_RESULT_WITH_RSSI,
7408 hci_inquiry_result_with_rssi_evt,
7409 sizeof(struct hci_ev_inquiry_result_rssi),
7410 HCI_MAX_EVENT_SIZE),
7411 /* [0x23 = HCI_EV_REMOTE_EXT_FEATURES] */
7412 HCI_EV(HCI_EV_REMOTE_EXT_FEATURES, hci_remote_ext_features_evt,
7413 sizeof(struct hci_ev_remote_ext_features)),
7414 /* [0x2c = HCI_EV_SYNC_CONN_COMPLETE] */
7415 HCI_EV(HCI_EV_SYNC_CONN_COMPLETE, hci_sync_conn_complete_evt,
7416 sizeof(struct hci_ev_sync_conn_complete)),
7417 /* [0x2d = HCI_EV_EXTENDED_INQUIRY_RESULT] */
7418 HCI_EV_VL(HCI_EV_EXTENDED_INQUIRY_RESULT,
7419 hci_extended_inquiry_result_evt,
7420 sizeof(struct hci_ev_ext_inquiry_result), HCI_MAX_EVENT_SIZE),
7421 /* [0x30 = HCI_EV_KEY_REFRESH_COMPLETE] */
7422 HCI_EV(HCI_EV_KEY_REFRESH_COMPLETE, hci_key_refresh_complete_evt,
7423 sizeof(struct hci_ev_key_refresh_complete)),
7424 /* [0x31 = HCI_EV_IO_CAPA_REQUEST] */
7425 HCI_EV(HCI_EV_IO_CAPA_REQUEST, hci_io_capa_request_evt,
7426 sizeof(struct hci_ev_io_capa_request)),
7427 /* [0x32 = HCI_EV_IO_CAPA_REPLY] */
7428 HCI_EV(HCI_EV_IO_CAPA_REPLY, hci_io_capa_reply_evt,
7429 sizeof(struct hci_ev_io_capa_reply)),
7430 /* [0x33 = HCI_EV_USER_CONFIRM_REQUEST] */
7431 HCI_EV(HCI_EV_USER_CONFIRM_REQUEST, hci_user_confirm_request_evt,
7432 sizeof(struct hci_ev_user_confirm_req)),
7433 /* [0x34 = HCI_EV_USER_PASSKEY_REQUEST] */
7434 HCI_EV(HCI_EV_USER_PASSKEY_REQUEST, hci_user_passkey_request_evt,
7435 sizeof(struct hci_ev_user_passkey_req)),
7436 /* [0x35 = HCI_EV_REMOTE_OOB_DATA_REQUEST] */
7437 HCI_EV(HCI_EV_REMOTE_OOB_DATA_REQUEST, hci_remote_oob_data_request_evt,
7438 sizeof(struct hci_ev_remote_oob_data_request)),
7439 /* [0x36 = HCI_EV_SIMPLE_PAIR_COMPLETE] */
7440 HCI_EV(HCI_EV_SIMPLE_PAIR_COMPLETE, hci_simple_pair_complete_evt,
7441 sizeof(struct hci_ev_simple_pair_complete)),
7442 /* [0x3b = HCI_EV_USER_PASSKEY_NOTIFY] */
7443 HCI_EV(HCI_EV_USER_PASSKEY_NOTIFY, hci_user_passkey_notify_evt,
7444 sizeof(struct hci_ev_user_passkey_notify)),
7445 /* [0x3c = HCI_EV_KEYPRESS_NOTIFY] */
7446 HCI_EV(HCI_EV_KEYPRESS_NOTIFY, hci_keypress_notify_evt,
7447 sizeof(struct hci_ev_keypress_notify)),
7448 /* [0x3d = HCI_EV_REMOTE_HOST_FEATURES] */
7449 HCI_EV(HCI_EV_REMOTE_HOST_FEATURES, hci_remote_host_features_evt,
7450 sizeof(struct hci_ev_remote_host_features)),
7451 /* [0x3e = HCI_EV_LE_META] */
7452 HCI_EV_REQ_VL(HCI_EV_LE_META, hci_le_meta_evt,
7453 sizeof(struct hci_ev_le_meta), HCI_MAX_EVENT_SIZE),
7454 /* [0xff = HCI_EV_VENDOR] */
7455 HCI_EV_VL(HCI_EV_VENDOR, msft_vendor_evt, 0, HCI_MAX_EVENT_SIZE),
7456 };
7457
hci_event_func(struct hci_dev * hdev,u8 event,struct sk_buff * skb,u16 * opcode,u8 * status,hci_req_complete_t * req_complete,hci_req_complete_skb_t * req_complete_skb)7458 static void hci_event_func(struct hci_dev *hdev, u8 event, struct sk_buff *skb,
7459 u16 *opcode, u8 *status,
7460 hci_req_complete_t *req_complete,
7461 hci_req_complete_skb_t *req_complete_skb)
7462 {
7463 const struct hci_ev *ev = &hci_ev_table[event];
7464 void *data;
7465
7466 if (!ev->func)
7467 return;
7468
7469 if (skb->len < ev->min_len) {
7470 bt_dev_err(hdev, "unexpected event 0x%2.2x length: %u < %u",
7471 event, skb->len, ev->min_len);
7472 return;
7473 }
7474
7475 /* Just warn if the length is over max_len size it still be
7476 * possible to partially parse the event so leave to callback to
7477 * decide if that is acceptable.
7478 */
7479 if (skb->len > ev->max_len)
7480 bt_dev_warn_ratelimited(hdev,
7481 "unexpected event 0x%2.2x length: %u > %u",
7482 event, skb->len, ev->max_len);
7483
7484 data = hci_ev_skb_pull(hdev, skb, event, ev->min_len);
7485 if (!data)
7486 return;
7487
7488 if (ev->req)
7489 ev->func_req(hdev, data, skb, opcode, status, req_complete,
7490 req_complete_skb);
7491 else
7492 ev->func(hdev, data, skb);
7493 }
7494
hci_event_packet(struct hci_dev * hdev,struct sk_buff * skb)7495 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
7496 {
7497 struct hci_event_hdr *hdr = (void *) skb->data;
7498 hci_req_complete_t req_complete = NULL;
7499 hci_req_complete_skb_t req_complete_skb = NULL;
7500 struct sk_buff *orig_skb = NULL;
7501 u8 status = 0, event, req_evt = 0;
7502 u16 opcode = HCI_OP_NOP;
7503
7504 if (skb->len < sizeof(*hdr)) {
7505 bt_dev_err(hdev, "Malformed HCI Event");
7506 goto done;
7507 }
7508
7509 kfree_skb(hdev->recv_event);
7510 hdev->recv_event = skb_clone(skb, GFP_KERNEL);
7511
7512 event = hdr->evt;
7513 if (!event) {
7514 bt_dev_warn(hdev, "Received unexpected HCI Event 0x%2.2x",
7515 event);
7516 goto done;
7517 }
7518
7519 /* Only match event if command OGF is not for LE */
7520 if (hdev->req_skb &&
7521 hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) != 0x08 &&
7522 hci_skb_event(hdev->req_skb) == event) {
7523 hci_req_cmd_complete(hdev, hci_skb_opcode(hdev->req_skb),
7524 status, &req_complete, &req_complete_skb);
7525 req_evt = event;
7526 }
7527
7528 /* If it looks like we might end up having to call
7529 * req_complete_skb, store a pristine copy of the skb since the
7530 * various handlers may modify the original one through
7531 * skb_pull() calls, etc.
7532 */
7533 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
7534 event == HCI_EV_CMD_COMPLETE)
7535 orig_skb = skb_clone(skb, GFP_KERNEL);
7536
7537 skb_pull(skb, HCI_EVENT_HDR_SIZE);
7538
7539 /* Store wake reason if we're suspended */
7540 hci_store_wake_reason(hdev, event, skb);
7541
7542 bt_dev_dbg(hdev, "event 0x%2.2x", event);
7543
7544 hci_event_func(hdev, event, skb, &opcode, &status, &req_complete,
7545 &req_complete_skb);
7546
7547 if (req_complete) {
7548 req_complete(hdev, status, opcode);
7549 } else if (req_complete_skb) {
7550 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
7551 kfree_skb(orig_skb);
7552 orig_skb = NULL;
7553 }
7554 req_complete_skb(hdev, status, opcode, orig_skb);
7555 }
7556
7557 done:
7558 kfree_skb(orig_skb);
7559 kfree_skb(skb);
7560 hdev->stat.evt_rx++;
7561 }
7562