xref: /linux/include/net/bluetooth/hci_core.h (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
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 #ifndef __HCI_CORE_H
27 #define __HCI_CORE_H
28 
29 #include <linux/idr.h>
30 #include <linux/leds.h>
31 #include <linux/rculist.h>
32 #include <linux/spinlock.h>
33 #include <linux/srcu.h>
34 
35 #include <net/bluetooth/hci.h>
36 #include <net/bluetooth/hci_drv.h>
37 #include <net/bluetooth/hci_sync.h>
38 #include <net/bluetooth/hci_sock.h>
39 #include <net/bluetooth/coredump.h>
40 
41 /* HCI priority */
42 #define HCI_PRIO_MAX	7
43 
44 /* HCI maximum id value */
45 #define HCI_MAX_ID 10000
46 
47 /* HCI Core structures */
48 struct inquiry_data {
49 	bdaddr_t	bdaddr;
50 	__u8		pscan_rep_mode;
51 	__u8		pscan_period_mode;
52 	__u8		pscan_mode;
53 	__u8		dev_class[3];
54 	__le16		clock_offset;
55 	__s8		rssi;
56 	__u8		ssp_mode;
57 };
58 
59 struct inquiry_entry {
60 	struct list_head	all;		/* inq_cache.all */
61 	struct list_head	list;		/* unknown or resolve */
62 	enum {
63 		NAME_NOT_KNOWN,
64 		NAME_NEEDED,
65 		NAME_PENDING,
66 		NAME_KNOWN,
67 	} name_state;
68 	__u32			timestamp;
69 	struct inquiry_data	data;
70 };
71 
72 struct discovery_state {
73 	int			type;
74 	enum {
75 		DISCOVERY_STOPPED,
76 		DISCOVERY_STARTING,
77 		DISCOVERY_FINDING,
78 		DISCOVERY_RESOLVING,
79 		DISCOVERY_STOPPING,
80 	} state;
81 	struct list_head	all;	/* All devices found during inquiry */
82 	struct list_head	unknown;	/* Name state not known */
83 	struct list_head	resolve;	/* Name needs to be resolved */
84 	__u32			timestamp;
85 	bdaddr_t		last_adv_addr;
86 	u8			last_adv_addr_type;
87 	s8			last_adv_rssi;
88 	u32			last_adv_flags;
89 	u8			last_adv_data[HCI_MAX_EXT_AD_LENGTH];
90 	u8			last_adv_data_len;
91 	bool			report_invalid_rssi;
92 	bool			result_filtering;
93 	bool			limited;
94 	s8			rssi;
95 	u16			uuid_count;
96 	u8			(*uuids)[16];
97 	unsigned long		name_resolve_timeout;
98 	spinlock_t		lock;
99 };
100 
101 #define SUSPEND_NOTIFIER_TIMEOUT	msecs_to_jiffies(2000) /* 2 seconds */
102 
103 enum suspend_tasks {
104 	SUSPEND_PAUSE_DISCOVERY,
105 	SUSPEND_UNPAUSE_DISCOVERY,
106 
107 	SUSPEND_PAUSE_ADVERTISING,
108 	SUSPEND_UNPAUSE_ADVERTISING,
109 
110 	SUSPEND_SCAN_DISABLE,
111 	SUSPEND_SCAN_ENABLE,
112 	SUSPEND_DISCONNECTING,
113 
114 	SUSPEND_POWERING_DOWN,
115 
116 	SUSPEND_PREPARE_NOTIFIER,
117 
118 	SUSPEND_SET_ADV_FILTER,
119 	__SUSPEND_NUM_TASKS
120 };
121 
122 enum suspended_state {
123 	BT_RUNNING = 0,
124 	BT_SUSPEND_DISCONNECT,
125 	BT_SUSPEND_CONFIGURE_WAKE,
126 };
127 
128 struct hci_conn_hash {
129 	struct list_head list;
130 	unsigned int     acl_num;
131 	unsigned int     sco_num;
132 	unsigned int     cis_num;
133 	unsigned int     bis_num;
134 	unsigned int     pa_num;
135 	unsigned int     le_num;
136 	unsigned int     le_num_peripheral;
137 };
138 
139 struct bdaddr_list {
140 	struct list_head list;
141 	bdaddr_t bdaddr;
142 	u8 bdaddr_type;
143 };
144 
145 struct codec_list {
146 	struct list_head list;
147 	u8	id;
148 	__u16	cid;
149 	__u16	vid;
150 	u8	transport;
151 	u8	num_caps;
152 	u32	len;
153 	struct hci_codec_caps caps[];
154 };
155 
156 struct bdaddr_list_with_irk {
157 	struct list_head list;
158 	bdaddr_t bdaddr;
159 	u8 bdaddr_type;
160 	u8 peer_irk[16];
161 	u8 local_irk[16];
162 };
163 
164 /* Bitmask of connection flags */
165 enum hci_conn_flags {
166 	HCI_CONN_FLAG_REMOTE_WAKEUP = BIT(0),
167 	HCI_CONN_FLAG_DEVICE_PRIVACY = BIT(1),
168 	HCI_CONN_FLAG_ADDRESS_RESOLUTION = BIT(2),
169 	HCI_CONN_FLAG_PAST = BIT(3),
170 };
171 typedef u8 hci_conn_flags_t;
172 
173 struct bdaddr_list_with_flags {
174 	struct list_head list;
175 	bdaddr_t bdaddr;
176 	u8 bdaddr_type;
177 	hci_conn_flags_t flags;
178 };
179 
180 struct bt_uuid {
181 	struct list_head list;
182 	u8 uuid[16];
183 	u8 size;
184 	u8 svc_hint;
185 };
186 
187 struct blocked_key {
188 	struct list_head list;
189 	struct rcu_head rcu;
190 	u8 type;
191 	u8 val[16];
192 };
193 
194 struct smp_csrk {
195 	bdaddr_t bdaddr;
196 	u8 bdaddr_type;
197 	u8 type;
198 	u8 val[16];
199 };
200 
201 struct smp_ltk {
202 	struct list_head list;
203 	struct rcu_head rcu;
204 	bdaddr_t bdaddr;
205 	u8 bdaddr_type;
206 	u8 authenticated;
207 	u8 type;
208 	u8 enc_size;
209 	__le16 ediv;
210 	__le64 rand;
211 	u8 val[16];
212 };
213 
214 struct smp_irk {
215 	struct list_head list;
216 	struct rcu_head rcu;
217 	bdaddr_t rpa;
218 	bdaddr_t bdaddr;
219 	u8 addr_type;
220 	u8 val[16];
221 };
222 
223 struct link_key {
224 	struct list_head list;
225 	struct rcu_head rcu;
226 	bdaddr_t bdaddr;
227 	u8 type;
228 	u8 val[HCI_LINK_KEY_SIZE];
229 	u8 pin_len;
230 };
231 
232 struct oob_data {
233 	struct list_head list;
234 	bdaddr_t bdaddr;
235 	u8 bdaddr_type;
236 	u8 present;
237 	u8 hash192[16];
238 	u8 rand192[16];
239 	u8 hash256[16];
240 	u8 rand256[16];
241 };
242 
243 struct adv_info {
244 	struct list_head list;
245 	bool	enabled;
246 	bool	pending;
247 	bool	periodic;
248 	bool	periodic_enabled;
249 	__u8	mesh;
250 	__u8	instance;
251 	__u8	handle;
252 	__u8	sid;
253 	__u32	flags;
254 	__u16	timeout;
255 	__u16	remaining_time;
256 	__u16	duration;
257 	__u16	adv_data_len;
258 	__u8	adv_data[HCI_MAX_EXT_AD_LENGTH];
259 	bool	adv_data_changed;
260 	__u16	scan_rsp_len;
261 	__u8	scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
262 	bool	scan_rsp_changed;
263 	__u16	per_adv_data_len;
264 	__u8	per_adv_data[HCI_MAX_PER_AD_LENGTH];
265 	__s8	tx_power;
266 	__u32   min_interval;
267 	__u32   max_interval;
268 	bdaddr_t	random_addr;
269 	bool 		rpa_expired;
270 	struct delayed_work	rpa_expired_cb;
271 };
272 
273 struct tx_queue {
274 	struct sk_buff_head queue;
275 	unsigned int extra;
276 	unsigned int tracked;
277 };
278 
279 #define HCI_MAX_ADV_INSTANCES		5
280 #define HCI_DEFAULT_ADV_DURATION	2
281 
282 #define HCI_ADV_TX_POWER_NO_PREFERENCE 0x7F
283 
284 #define DATA_CMP(_d1, _l1, _d2, _l2) \
285 	(_l1 == _l2 ? memcmp(_d1, _d2, _l1) : _l1 - _l2)
286 
287 #define ADV_DATA_CMP(_adv, _data, _len) \
288 	DATA_CMP((_adv)->adv_data, (_adv)->adv_data_len, _data, _len)
289 
290 #define SCAN_RSP_CMP(_adv, _data, _len) \
291 	DATA_CMP((_adv)->scan_rsp_data, (_adv)->scan_rsp_len, _data, _len)
292 
293 struct monitored_device {
294 	struct list_head list;
295 
296 	bdaddr_t bdaddr;
297 	__u8     addr_type;
298 	__u16    handle;
299 	bool     notified;
300 };
301 
302 struct adv_pattern {
303 	struct list_head list;
304 	__u8 ad_type;
305 	__u8 offset;
306 	__u8 length;
307 	__u8 value[HCI_MAX_EXT_AD_LENGTH];
308 };
309 
310 struct adv_rssi_thresholds {
311 	__s8 low_threshold;
312 	__s8 high_threshold;
313 	__u16 low_threshold_timeout;
314 	__u16 high_threshold_timeout;
315 	__u8 sampling_period;
316 };
317 
318 struct adv_monitor {
319 	struct list_head patterns;
320 	struct adv_rssi_thresholds rssi;
321 	__u16		handle;
322 
323 	enum {
324 		ADV_MONITOR_STATE_NOT_REGISTERED,
325 		ADV_MONITOR_STATE_REGISTERED,
326 		ADV_MONITOR_STATE_OFFLOADED
327 	} state;
328 };
329 
330 #define HCI_MIN_ADV_MONITOR_HANDLE		1
331 #define HCI_MAX_ADV_MONITOR_NUM_HANDLES		32
332 #define HCI_MAX_ADV_MONITOR_NUM_PATTERNS	16
333 #define HCI_ADV_MONITOR_EXT_NONE		1
334 #define HCI_ADV_MONITOR_EXT_MSFT		2
335 
336 #define HCI_MAX_SHORT_NAME_LENGTH	10
337 
338 #define HCI_CONN_HANDLE_MAX		0x0eff
339 #define HCI_CONN_HANDLE_UNSET(_handle)	(_handle > HCI_CONN_HANDLE_MAX)
340 
341 /* Min encryption key size to match with SMP */
342 #define HCI_MIN_ENC_KEY_SIZE		7
343 
344 /* Default LE RPA expiry time, 15 minutes */
345 #define HCI_DEFAULT_RPA_TIMEOUT		(15 * 60)
346 
347 /* Default min/max age of connection information (1s/3s) */
348 #define DEFAULT_CONN_INFO_MIN_AGE	1000
349 #define DEFAULT_CONN_INFO_MAX_AGE	3000
350 /* Default authenticated payload timeout 30s */
351 #define DEFAULT_AUTH_PAYLOAD_TIMEOUT   0x0bb8
352 
353 #define HCI_MAX_PAGES	3
354 
355 struct hci_dev {
356 	struct list_head list;
357 	struct srcu_struct srcu;
358 	struct mutex	lock;
359 
360 	struct ida	unset_handle_ida;
361 
362 	const char	*name;
363 	unsigned long	flags;
364 	__u16		id;
365 	__u8		bus;
366 	bdaddr_t	bdaddr;
367 	bdaddr_t	setup_addr;
368 	bdaddr_t	public_addr;
369 	bdaddr_t	random_addr;
370 	bdaddr_t	static_addr;
371 	__u8		adv_addr_type;
372 	__u8		dev_name[HCI_MAX_NAME_LENGTH];
373 	__u8		short_name[HCI_MAX_SHORT_NAME_LENGTH];
374 	__u8		eir[HCI_MAX_EIR_LENGTH];
375 	__u16		appearance;
376 	__u8		dev_class[3];
377 	__u8		major_class;
378 	__u8		minor_class;
379 	__u8		max_page;
380 	__u8		features[HCI_MAX_PAGES][8];
381 	__u8		le_features[248];
382 	__u8		le_accept_list_size;
383 	__u8		le_resolv_list_size;
384 	__u8		le_num_of_adv_sets;
385 	__u8		le_states[8];
386 	__u8		mesh_ad_types[16];
387 	__u8		mesh_send_ref;
388 	__u8		commands[64];
389 	__u8		hci_ver;
390 	__u16		hci_rev;
391 	__u8		lmp_ver;
392 	__u16		manufacturer;
393 	__u16		lmp_subver;
394 	__u16		voice_setting;
395 	__u8		num_iac;
396 	__u16		stored_max_keys;
397 	__u16		stored_num_keys;
398 	__u8		io_capability;
399 	__s8		inq_tx_power;
400 	__u8		err_data_reporting;
401 	__u16		page_scan_interval;
402 	__u16		page_scan_window;
403 	__u8		page_scan_type;
404 	__u8		le_adv_channel_map;
405 	__u16		le_adv_min_interval;
406 	__u16		le_adv_max_interval;
407 	__u8		le_scan_type;
408 	__u16		le_scan_interval;
409 	__u16		le_scan_window;
410 	__u16		le_scan_int_suspend;
411 	__u16		le_scan_window_suspend;
412 	__u16		le_scan_int_discovery;
413 	__u16		le_scan_window_discovery;
414 	__u16		le_scan_int_adv_monitor;
415 	__u16		le_scan_window_adv_monitor;
416 	__u16		le_scan_int_connect;
417 	__u16		le_scan_window_connect;
418 	__u16		le_conn_min_interval;
419 	__u16		le_conn_max_interval;
420 	__u16		le_conn_latency;
421 	__u16		le_supv_timeout;
422 	__u16		le_def_tx_len;
423 	__u16		le_def_tx_time;
424 	__u16		le_max_tx_len;
425 	__u16		le_max_tx_time;
426 	__u16		le_max_rx_len;
427 	__u16		le_max_rx_time;
428 	__u8		le_max_key_size;
429 	__u8		le_min_key_size;
430 	__u16		discov_interleaved_timeout;
431 	__u16		conn_info_min_age;
432 	__u16		conn_info_max_age;
433 	__u16		auth_payload_timeout;
434 	__u8		min_enc_key_size;
435 	__u8		max_enc_key_size;
436 	__u8		pairing_opts;
437 	__u8		ssp_debug_mode;
438 	__u8		hw_error_code;
439 	__u32		clock;
440 	__u16		advmon_allowlist_duration;
441 	__u16		advmon_no_filter_duration;
442 	__u8		enable_advmon_interleave_scan;
443 
444 	__u16		devid_source;
445 	__u16		devid_vendor;
446 	__u16		devid_product;
447 	__u16		devid_version;
448 
449 	__u8		def_page_scan_type;
450 	__u16		def_page_scan_int;
451 	__u16		def_page_scan_window;
452 	__u8		def_inq_scan_type;
453 	__u16		def_inq_scan_int;
454 	__u16		def_inq_scan_window;
455 	__u16		def_br_lsto;
456 	__u16		def_page_timeout;
457 	__u16		def_multi_adv_rotation_duration;
458 	__u16		def_le_autoconnect_timeout;
459 	__s8		min_le_tx_power;
460 	__s8		max_le_tx_power;
461 
462 	__u16		pkt_type;
463 	__u16		esco_type;
464 	__u16		link_policy;
465 	__u16		link_mode;
466 
467 	__u32		idle_timeout;
468 	__u16		sniff_min_interval;
469 	__u16		sniff_max_interval;
470 
471 	unsigned int	auto_accept_delay;
472 
473 	DECLARE_BITMAP(quirk_flags, __HCI_NUM_QUIRKS);
474 
475 	atomic_t	cmd_cnt;
476 	unsigned int	acl_cnt;
477 	unsigned int	sco_cnt;
478 	unsigned int	le_cnt;
479 	unsigned int	iso_cnt;
480 
481 	unsigned int	acl_mtu;
482 	unsigned int	sco_mtu;
483 	unsigned int	le_mtu;
484 	unsigned int	iso_mtu;
485 	unsigned int	acl_pkts;
486 	unsigned int	sco_pkts;
487 	unsigned int	le_pkts;
488 	unsigned int	iso_pkts;
489 
490 	unsigned long	acl_last_tx;
491 	unsigned long	le_last_tx;
492 	unsigned long	iso_last_tx;
493 
494 	__u8		le_tx_def_phys;
495 	__u8		le_rx_def_phys;
496 
497 	struct workqueue_struct	*workqueue;
498 	struct workqueue_struct	*req_workqueue;
499 
500 	struct work_struct	power_on;
501 	struct delayed_work	power_off;
502 	struct work_struct	error_reset;
503 	struct work_struct	cmd_sync_work;
504 	struct list_head	cmd_sync_work_list;
505 	struct mutex		cmd_sync_work_lock;
506 	struct mutex		unregister_lock;
507 	struct work_struct	cmd_sync_cancel_work;
508 	struct work_struct	reenable_adv_work;
509 
510 	__u16			discov_timeout;
511 	struct delayed_work	discov_off;
512 
513 	struct delayed_work	service_cache;
514 
515 	struct delayed_work	cmd_timer;
516 	struct delayed_work	ncmd_timer;
517 
518 	struct work_struct	rx_work;
519 	struct work_struct	cmd_work;
520 	struct work_struct	tx_work;
521 
522 	struct delayed_work	le_scan_disable;
523 
524 	struct sk_buff_head	rx_q;
525 	struct sk_buff_head	raw_q;
526 	struct sk_buff_head	cmd_q;
527 
528 	struct sk_buff		*sent_cmd;
529 	struct sk_buff		*recv_event;
530 
531 	struct mutex		req_lock;
532 	wait_queue_head_t	req_wait_q;
533 	__u32			req_status;
534 	__u32			req_result;
535 	struct sk_buff		*req_skb;
536 	struct sk_buff		*req_rsp;
537 
538 	void			*smp_data;
539 	void			*smp_bredr_data;
540 
541 	struct discovery_state	discovery;
542 
543 	bool			discovery_paused;
544 	int			advertising_old_state;
545 	bool			advertising_paused;
546 
547 	struct notifier_block	suspend_notifier;
548 	enum suspended_state	suspend_state_next;
549 	enum suspended_state	suspend_state;
550 	bool			scanning_paused;
551 	bool			suspended;
552 	u8			wake_reason;
553 	bdaddr_t		wake_addr;
554 	u8			wake_addr_type;
555 
556 	struct hci_conn_hash	conn_hash;
557 
558 	struct list_head	mesh_pending;
559 	struct mutex		mgmt_pending_lock;
560 	struct list_head	mgmt_pending;
561 	struct list_head	reject_list;
562 	struct list_head	accept_list;
563 	struct list_head	uuids;
564 	struct list_head	link_keys;
565 	struct list_head	long_term_keys;
566 	struct list_head	identity_resolving_keys;
567 	struct list_head	remote_oob_data;
568 	struct list_head	le_accept_list;
569 	struct list_head	le_resolv_list;
570 	struct list_head	le_conn_params;
571 	struct list_head	pend_le_conns;
572 	struct list_head	pend_le_reports;
573 	struct list_head	blocked_keys;
574 	struct list_head	local_codecs;
575 
576 	struct hci_dev_stats	stat;
577 
578 	atomic_t		promisc;
579 
580 	const char		*hw_info;
581 	const char		*fw_info;
582 	struct dentry		*debugfs;
583 
584 	struct hci_devcoredump	dump;
585 
586 	struct device		dev;
587 
588 	struct rfkill		*rfkill;
589 
590 	DECLARE_BITMAP(dev_flags, __HCI_NUM_FLAGS);
591 	hci_conn_flags_t	conn_flags;
592 
593 	__s8			adv_tx_power;
594 	__u8			adv_data[HCI_MAX_EXT_AD_LENGTH];
595 	__u8			adv_data_len;
596 	__u8			scan_rsp_data[HCI_MAX_EXT_AD_LENGTH];
597 	__u8			scan_rsp_data_len;
598 	__u8			per_adv_data[HCI_MAX_PER_AD_LENGTH];
599 	__u8			per_adv_data_len;
600 
601 	struct list_head	adv_instances;
602 	unsigned int		adv_instance_cnt;
603 	__u8			cur_adv_instance;
604 	__u16			adv_instance_timeout;
605 	struct delayed_work	adv_instance_expire;
606 
607 	struct idr		adv_monitors_idr;
608 	unsigned int		adv_monitors_cnt;
609 
610 	__u8			irk[16];
611 	__u32			rpa_timeout;
612 	struct delayed_work	rpa_expired;
613 	bdaddr_t		rpa;
614 
615 	struct delayed_work	mesh_send_done;
616 
617 	enum {
618 		INTERLEAVE_SCAN_NONE,
619 		INTERLEAVE_SCAN_NO_FILTER,
620 		INTERLEAVE_SCAN_ALLOWLIST
621 	} interleave_scan_state;
622 
623 	struct delayed_work	interleave_scan;
624 
625 	struct list_head	monitored_devices;
626 	bool			advmon_pend_notify;
627 
628 	struct hci_drv		*hci_drv;
629 
630 #if IS_ENABLED(CONFIG_BT_LEDS)
631 	struct led_trigger	*power_led;
632 #endif
633 
634 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
635 	__u16			msft_opcode;
636 	void			*msft_data;
637 	bool			msft_curve_validity;
638 #endif
639 
640 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
641 	bool			aosp_capable;
642 	bool			aosp_quality_report;
643 #endif
644 
645 	int (*open)(struct hci_dev *hdev);
646 	int (*close)(struct hci_dev *hdev);
647 	int (*flush)(struct hci_dev *hdev);
648 	int (*setup)(struct hci_dev *hdev);
649 	int (*shutdown)(struct hci_dev *hdev);
650 	int (*send)(struct hci_dev *hdev, struct sk_buff *skb);
651 	void (*notify)(struct hci_dev *hdev, unsigned int evt);
652 	void (*hw_error)(struct hci_dev *hdev, u8 code);
653 	int (*post_init)(struct hci_dev *hdev);
654 	int (*set_diag)(struct hci_dev *hdev, bool enable);
655 	int (*set_bdaddr)(struct hci_dev *hdev, const bdaddr_t *bdaddr);
656 	void (*reset)(struct hci_dev *hdev);
657 	bool (*wakeup)(struct hci_dev *hdev);
658 	int (*set_quality_report)(struct hci_dev *hdev, bool enable);
659 	int (*get_data_path_id)(struct hci_dev *hdev, __u8 *data_path);
660 	int (*get_codec_config_data)(struct hci_dev *hdev, __u8 type,
661 				     struct bt_codec *codec, __u8 *vnd_len,
662 				     __u8 **vnd_data);
663 	u8 (*classify_pkt_type)(struct hci_dev *hdev, struct sk_buff *skb);
664 };
665 
666 #define hci_set_quirk(hdev, nr) set_bit((nr), (hdev)->quirk_flags)
667 #define hci_clear_quirk(hdev, nr) clear_bit((nr), (hdev)->quirk_flags)
668 #define hci_test_quirk(hdev, nr) test_bit((nr), (hdev)->quirk_flags)
669 
670 #define HCI_PHY_HANDLE(handle)	(handle & 0xff)
671 
672 enum conn_reasons {
673 	CONN_REASON_PAIR_DEVICE,
674 	CONN_REASON_L2CAP_CHAN,
675 	CONN_REASON_SCO_CONNECT,
676 	CONN_REASON_ISO_CONNECT,
677 };
678 
679 struct hci_conn {
680 	struct list_head list;
681 
682 	atomic_t	refcnt;
683 
684 	bdaddr_t	dst;
685 	__u8		dst_type;
686 	bdaddr_t	src;
687 	__u8		src_type;
688 	bdaddr_t	init_addr;
689 	__u8		init_addr_type;
690 	bdaddr_t	resp_addr;
691 	__u8		resp_addr_type;
692 	__u8		adv_instance;
693 	__u16		handle;
694 	__u16		sync_handle;
695 	__u8		sid;
696 	__u16		state;
697 	__u16		mtu;
698 	__u8		mode;
699 	__u8		type;
700 	__u8		role;
701 	bool		out;
702 	__u8		attempt;
703 	__u8		dev_class[3];
704 	__u8		features[HCI_MAX_PAGES][8];
705 	__u8		le_features[248];
706 	__u16		pkt_type;
707 	__u16		link_policy;
708 	__u8		key_type;
709 	__u8		auth_type;
710 	__u8		sec_level;
711 	__u8		pending_sec_level;
712 	__u8		pin_length;
713 	__u8		enc_key_size;
714 	__u8		io_capability;
715 	__u32		passkey_notify;
716 	__u8		passkey_entered;
717 	__u16		disc_timeout;
718 	__u16		conn_timeout;
719 	__u16		setting;
720 	__u16		auth_payload_timeout;
721 	__u16		le_conn_min_interval;
722 	__u16		le_conn_max_interval;
723 	__u16		le_conn_interval;
724 	__u16		le_conn_latency;
725 	__u16		le_supv_timeout;
726 	__u8		le_adv_data[HCI_MAX_EXT_AD_LENGTH];
727 	__u8		le_adv_data_len;
728 	__u8		le_per_adv_data[HCI_MAX_PER_AD_TOT_LEN];
729 	__u16		le_per_adv_data_len;
730 	__u16		le_per_adv_data_offset;
731 	__u8		le_adv_phy;
732 	__u8		le_adv_sec_phy;
733 	__u8		le_tx_phy;
734 	__u8		le_rx_phy;
735 	__s8		rssi;
736 	__s8		tx_power;
737 	__s8		max_tx_power;
738 	struct bt_iso_qos iso_qos;
739 	__u8		num_bis;
740 	__u8		bis[HCI_MAX_ISO_BIS];
741 
742 	unsigned long	flags;
743 
744 	enum conn_reasons conn_reason;
745 	__u8		abort_reason;
746 
747 	__u32		clock;
748 	__u16		clock_accuracy;
749 
750 	unsigned long	conn_info_timestamp;
751 
752 	__u8		remote_cap;
753 	__u8		remote_auth;
754 
755 	unsigned int	sent;
756 
757 	struct sk_buff_head data_q;
758 	struct list_head chan_list;
759 
760 	struct tx_queue tx_q;
761 
762 	struct delayed_work disc_work;
763 	struct delayed_work auto_accept_work;
764 	struct delayed_work idle_work;
765 	struct delayed_work le_conn_timeout;
766 
767 	struct device	dev;
768 	struct dentry	*debugfs;
769 
770 	struct hci_dev	*hdev;
771 	void		*l2cap_data;
772 	void		*sco_data;
773 	void		*iso_data;
774 
775 	struct list_head link_list;
776 	struct hci_conn	*parent;
777 	struct hci_link *link;
778 
779 	struct bt_codec codec;
780 
781 	void (*connect_cfm_cb)	(struct hci_conn *conn, u8 status);
782 	void (*security_cfm_cb)	(struct hci_conn *conn, u8 status);
783 	void (*disconn_cfm_cb)	(struct hci_conn *conn, u8 reason);
784 
785 	void (*cleanup)(struct hci_conn *conn);
786 };
787 
788 struct hci_link {
789 	struct list_head list;
790 	struct hci_conn *conn;
791 };
792 
793 struct hci_chan {
794 	struct list_head list;
795 	__u16 handle;
796 	struct hci_conn *conn;
797 	struct sk_buff_head data_q;
798 	unsigned int	sent;
799 	__u8		state;
800 };
801 
802 struct hci_conn_params {
803 	struct list_head list;
804 	struct list_head action;
805 
806 	bdaddr_t addr;
807 	u8 addr_type;
808 
809 	u16 conn_min_interval;
810 	u16 conn_max_interval;
811 	u16 conn_latency;
812 	u16 supervision_timeout;
813 
814 	enum {
815 		HCI_AUTO_CONN_DISABLED,
816 		HCI_AUTO_CONN_REPORT,
817 		HCI_AUTO_CONN_DIRECT,
818 		HCI_AUTO_CONN_ALWAYS,
819 		HCI_AUTO_CONN_LINK_LOSS,
820 		HCI_AUTO_CONN_EXPLICIT,
821 	} auto_connect;
822 
823 	struct hci_conn *conn;
824 	bool explicit_connect;
825 	/* Accessed without hdev->lock: */
826 	hci_conn_flags_t flags;
827 	u8  privacy_mode;
828 };
829 
830 extern struct list_head hci_dev_list;
831 extern struct list_head hci_cb_list;
832 extern rwlock_t hci_dev_list_lock;
833 extern struct mutex hci_cb_list_lock;
834 
835 #define hci_dev_set_flag(hdev, nr)             set_bit((nr), (hdev)->dev_flags)
836 #define hci_dev_clear_flag(hdev, nr)           clear_bit((nr), (hdev)->dev_flags)
837 #define hci_dev_change_flag(hdev, nr)          change_bit((nr), (hdev)->dev_flags)
838 #define hci_dev_test_flag(hdev, nr)            test_bit((nr), (hdev)->dev_flags)
839 #define hci_dev_test_and_set_flag(hdev, nr)    test_and_set_bit((nr), (hdev)->dev_flags)
840 #define hci_dev_test_and_clear_flag(hdev, nr)  test_and_clear_bit((nr), (hdev)->dev_flags)
841 #define hci_dev_test_and_change_flag(hdev, nr) test_and_change_bit((nr), (hdev)->dev_flags)
842 
843 #define hci_dev_clear_volatile_flags(hdev)				\
844 	do {								\
845 		hci_dev_clear_flag((hdev), HCI_LE_SCAN);		\
846 		hci_dev_clear_flag((hdev), HCI_LE_ADV);			\
847 		hci_dev_clear_flag((hdev), HCI_LL_RPA_RESOLUTION);	\
848 		hci_dev_clear_flag((hdev), HCI_PERIODIC_INQ);		\
849 		hci_dev_clear_flag((hdev), HCI_QUALITY_REPORT);		\
850 	} while (0)
851 
852 #define hci_dev_le_state_simultaneous(hdev) \
853 	(!hci_test_quirk((hdev), HCI_QUIRK_BROKEN_LE_STATES) && \
854 	 ((hdev)->le_states[4] & 0x08) &&	/* Central */ \
855 	 ((hdev)->le_states[4] & 0x40) &&	/* Peripheral */ \
856 	 ((hdev)->le_states[3] & 0x10))		/* Simultaneous */
857 
858 /* ----- HCI interface to upper protocols ----- */
859 int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
860 int l2cap_disconn_ind(struct hci_conn *hcon);
861 int l2cap_recv_acldata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
862 		       u16 flags);
863 
864 #if IS_ENABLED(CONFIG_BT_BREDR)
865 int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
866 int sco_recv_scodata(struct hci_dev *hdev, u16 handle, struct sk_buff *skb);
867 #else
sco_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 * flags)868 static inline int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
869 				  __u8 *flags)
870 {
871 	return 0;
872 }
873 
sco_recv_scodata(struct hci_dev * hdev,u16 handle,struct sk_buff * skb)874 static inline int sco_recv_scodata(struct hci_dev *hdev, u16 handle,
875 				   struct sk_buff *skb)
876 {
877 	kfree_skb(skb);
878 	return -ENOENT;
879 }
880 #endif
881 
882 #if IS_ENABLED(CONFIG_BT_LE)
883 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
884 int iso_recv(struct hci_dev *hdev, u16 handle, struct sk_buff *skb,
885 	     u16 flags);
886 #else
iso_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 * flags)887 static inline int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
888 				  __u8 *flags)
889 {
890 	return 0;
891 }
892 
iso_recv(struct hci_dev * hdev,u16 handle,struct sk_buff * skb,u16 flags)893 static inline int iso_recv(struct hci_dev *hdev, u16 handle,
894 			   struct sk_buff *skb, u16 flags)
895 {
896 	kfree_skb(skb);
897 	return -ENOENT;
898 }
899 #endif
900 
901 /* ----- Inquiry cache ----- */
902 #define INQUIRY_CACHE_AGE_MAX   (HZ*30)   /* 30 seconds */
903 #define INQUIRY_ENTRY_AGE_MAX   (HZ*60)   /* 60 seconds */
904 
discovery_init(struct hci_dev * hdev)905 static inline void discovery_init(struct hci_dev *hdev)
906 {
907 	spin_lock_init(&hdev->discovery.lock);
908 	hdev->discovery.state = DISCOVERY_STOPPED;
909 	INIT_LIST_HEAD(&hdev->discovery.all);
910 	INIT_LIST_HEAD(&hdev->discovery.unknown);
911 	INIT_LIST_HEAD(&hdev->discovery.resolve);
912 	hdev->discovery.report_invalid_rssi = true;
913 	hdev->discovery.rssi = HCI_RSSI_INVALID;
914 }
915 
hci_discovery_filter_clear(struct hci_dev * hdev)916 static inline void hci_discovery_filter_clear(struct hci_dev *hdev)
917 {
918 	hdev->discovery.result_filtering = false;
919 	hdev->discovery.report_invalid_rssi = true;
920 	hdev->discovery.rssi = HCI_RSSI_INVALID;
921 	hdev->discovery.uuid_count = 0;
922 
923 	spin_lock(&hdev->discovery.lock);
924 	kfree(hdev->discovery.uuids);
925 	hdev->discovery.uuids = NULL;
926 	spin_unlock(&hdev->discovery.lock);
927 }
928 
929 bool hci_discovery_active(struct hci_dev *hdev);
930 
931 void hci_discovery_set_state(struct hci_dev *hdev, int state);
932 
inquiry_cache_empty(struct hci_dev * hdev)933 static inline int inquiry_cache_empty(struct hci_dev *hdev)
934 {
935 	return list_empty(&hdev->discovery.all);
936 }
937 
inquiry_cache_age(struct hci_dev * hdev)938 static inline long inquiry_cache_age(struct hci_dev *hdev)
939 {
940 	struct discovery_state *c = &hdev->discovery;
941 	return jiffies - c->timestamp;
942 }
943 
inquiry_entry_age(struct inquiry_entry * e)944 static inline long inquiry_entry_age(struct inquiry_entry *e)
945 {
946 	return jiffies - e->timestamp;
947 }
948 
949 struct inquiry_entry *hci_inquiry_cache_lookup(struct hci_dev *hdev,
950 					       bdaddr_t *bdaddr);
951 struct inquiry_entry *hci_inquiry_cache_lookup_unknown(struct hci_dev *hdev,
952 						       bdaddr_t *bdaddr);
953 struct inquiry_entry *hci_inquiry_cache_lookup_resolve(struct hci_dev *hdev,
954 						       bdaddr_t *bdaddr,
955 						       int state);
956 void hci_inquiry_cache_update_resolve(struct hci_dev *hdev,
957 				      struct inquiry_entry *ie);
958 u32 hci_inquiry_cache_update(struct hci_dev *hdev, struct inquiry_data *data,
959 			     bool name_known);
960 void hci_inquiry_cache_flush(struct hci_dev *hdev);
961 
962 /* ----- HCI Connections ----- */
963 enum {
964 	HCI_CONN_AUTH_PEND,
965 	HCI_CONN_ENCRYPT_PEND,
966 	HCI_CONN_RSWITCH_PEND,
967 	HCI_CONN_MODE_CHANGE_PEND,
968 	HCI_CONN_SCO_SETUP_PEND,
969 	HCI_CONN_MGMT_CONNECTED,
970 	HCI_CONN_SSP_ENABLED,
971 	HCI_CONN_SC_ENABLED,
972 	HCI_CONN_AES_CCM,
973 	HCI_CONN_POWER_SAVE,
974 	HCI_CONN_FLUSH_KEY,
975 	HCI_CONN_ENCRYPT,
976 	HCI_CONN_AUTH,
977 	HCI_CONN_SECURE,
978 	HCI_CONN_FIPS,
979 	HCI_CONN_STK_ENCRYPT,
980 	HCI_CONN_AUTH_INITIATOR,
981 	HCI_CONN_DROP,
982 	HCI_CONN_CANCEL,
983 	HCI_CONN_PARAM_REMOVAL_PEND,
984 	HCI_CONN_NEW_LINK_KEY,
985 	HCI_CONN_SCANNING,
986 	HCI_CONN_AUTH_FAILURE,
987 	HCI_CONN_PER_ADV,
988 	HCI_CONN_BIG_CREATED,
989 	HCI_CONN_CREATE_CIS,
990 	HCI_CONN_CREATE_BIG_SYNC,
991 	HCI_CONN_BIG_SYNC,
992 	HCI_CONN_BIG_SYNC_FAILED,
993 	HCI_CONN_CREATE_PA_SYNC,
994 	HCI_CONN_PA_SYNC,
995 	HCI_CONN_PA_SYNC_FAILED,
996 };
997 
hci_conn_ssp_enabled(struct hci_conn * conn)998 static inline bool hci_conn_ssp_enabled(struct hci_conn *conn)
999 {
1000 	struct hci_dev *hdev = conn->hdev;
1001 	return hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
1002 	       test_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
1003 }
1004 
hci_conn_sc_enabled(struct hci_conn * conn)1005 static inline bool hci_conn_sc_enabled(struct hci_conn *conn)
1006 {
1007 	struct hci_dev *hdev = conn->hdev;
1008 	return hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
1009 	       test_bit(HCI_CONN_SC_ENABLED, &conn->flags);
1010 }
1011 
hci_conn_hash_add(struct hci_dev * hdev,struct hci_conn * c)1012 static inline void hci_conn_hash_add(struct hci_dev *hdev, struct hci_conn *c)
1013 {
1014 	struct hci_conn_hash *h = &hdev->conn_hash;
1015 	list_add_tail_rcu(&c->list, &h->list);
1016 	switch (c->type) {
1017 	case ACL_LINK:
1018 		h->acl_num++;
1019 		break;
1020 	case LE_LINK:
1021 		h->le_num++;
1022 		if (c->role == HCI_ROLE_SLAVE)
1023 			h->le_num_peripheral++;
1024 		break;
1025 	case SCO_LINK:
1026 	case ESCO_LINK:
1027 		h->sco_num++;
1028 		break;
1029 	case CIS_LINK:
1030 		h->cis_num++;
1031 		break;
1032 	case BIS_LINK:
1033 		h->bis_num++;
1034 		break;
1035 	case PA_LINK:
1036 		h->pa_num++;
1037 		break;
1038 	}
1039 }
1040 
hci_conn_hash_del(struct hci_dev * hdev,struct hci_conn * c)1041 static inline void hci_conn_hash_del(struct hci_dev *hdev, struct hci_conn *c)
1042 {
1043 	struct hci_conn_hash *h = &hdev->conn_hash;
1044 
1045 	list_del_rcu(&c->list);
1046 	synchronize_rcu();
1047 
1048 	switch (c->type) {
1049 	case ACL_LINK:
1050 		h->acl_num--;
1051 		break;
1052 	case LE_LINK:
1053 		h->le_num--;
1054 		if (c->role == HCI_ROLE_SLAVE)
1055 			h->le_num_peripheral--;
1056 		break;
1057 	case SCO_LINK:
1058 	case ESCO_LINK:
1059 		h->sco_num--;
1060 		break;
1061 	case CIS_LINK:
1062 		h->cis_num--;
1063 		break;
1064 	case BIS_LINK:
1065 		h->bis_num--;
1066 		break;
1067 	case PA_LINK:
1068 		h->pa_num--;
1069 		break;
1070 	}
1071 }
1072 
hci_conn_num(struct hci_dev * hdev,__u8 type)1073 static inline unsigned int hci_conn_num(struct hci_dev *hdev, __u8 type)
1074 {
1075 	struct hci_conn_hash *h = &hdev->conn_hash;
1076 	switch (type) {
1077 	case ACL_LINK:
1078 		return h->acl_num;
1079 	case LE_LINK:
1080 		return h->le_num;
1081 	case SCO_LINK:
1082 	case ESCO_LINK:
1083 		return h->sco_num;
1084 	case CIS_LINK:
1085 		return h->cis_num;
1086 	case BIS_LINK:
1087 		return h->bis_num;
1088 	case PA_LINK:
1089 		return h->pa_num;
1090 	default:
1091 		return 0;
1092 	}
1093 }
1094 
hci_conn_count(struct hci_dev * hdev)1095 static inline unsigned int hci_conn_count(struct hci_dev *hdev)
1096 {
1097 	struct hci_conn_hash *c = &hdev->conn_hash;
1098 
1099 	return c->acl_num + c->sco_num + c->le_num + c->cis_num + c->bis_num +
1100 		c->pa_num;
1101 }
1102 
hci_iso_count(struct hci_dev * hdev)1103 static inline unsigned int hci_iso_count(struct hci_dev *hdev)
1104 {
1105 	struct hci_conn_hash *c = &hdev->conn_hash;
1106 
1107 	return c->cis_num + c->bis_num;
1108 }
1109 
hci_conn_valid(struct hci_dev * hdev,struct hci_conn * conn)1110 static inline bool hci_conn_valid(struct hci_dev *hdev, struct hci_conn *conn)
1111 {
1112 	struct hci_conn_hash *h = &hdev->conn_hash;
1113 	struct hci_conn  *c;
1114 
1115 	rcu_read_lock();
1116 
1117 	list_for_each_entry_rcu(c, &h->list, list) {
1118 		if (c == conn) {
1119 			rcu_read_unlock();
1120 			return true;
1121 		}
1122 	}
1123 	rcu_read_unlock();
1124 
1125 	return false;
1126 }
1127 
hci_conn_lookup_type(struct hci_dev * hdev,__u16 handle)1128 static inline __u8 hci_conn_lookup_type(struct hci_dev *hdev, __u16 handle)
1129 {
1130 	struct hci_conn_hash *h = &hdev->conn_hash;
1131 	struct hci_conn *c;
1132 	__u8 type = INVALID_LINK;
1133 
1134 	rcu_read_lock();
1135 
1136 	list_for_each_entry_rcu(c, &h->list, list) {
1137 		if (c->handle == handle) {
1138 			type = c->type;
1139 			break;
1140 		}
1141 	}
1142 
1143 	rcu_read_unlock();
1144 
1145 	return type;
1146 }
1147 
hci_conn_hash_lookup_bis(struct hci_dev * hdev,bdaddr_t * ba,__u8 bis)1148 static inline struct hci_conn *hci_conn_hash_lookup_bis(struct hci_dev *hdev,
1149 							bdaddr_t *ba, __u8 bis)
1150 {
1151 	struct hci_conn_hash *h = &hdev->conn_hash;
1152 	struct hci_conn  *c;
1153 
1154 	rcu_read_lock();
1155 
1156 	list_for_each_entry_rcu(c, &h->list, list) {
1157 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK)
1158 			continue;
1159 
1160 		if (c->iso_qos.bcast.bis == bis) {
1161 			rcu_read_unlock();
1162 			return c;
1163 		}
1164 	}
1165 	rcu_read_unlock();
1166 
1167 	return NULL;
1168 }
1169 
1170 static inline struct hci_conn *
hci_conn_hash_lookup_create_pa_sync(struct hci_dev * hdev)1171 hci_conn_hash_lookup_create_pa_sync(struct hci_dev *hdev)
1172 {
1173 	struct hci_conn_hash *h = &hdev->conn_hash;
1174 	struct hci_conn  *c;
1175 
1176 	rcu_read_lock();
1177 
1178 	list_for_each_entry_rcu(c, &h->list, list) {
1179 		if (c->type != PA_LINK)
1180 			continue;
1181 
1182 		if (!test_bit(HCI_CONN_CREATE_PA_SYNC, &c->flags))
1183 			continue;
1184 
1185 		rcu_read_unlock();
1186 		return c;
1187 	}
1188 
1189 	rcu_read_unlock();
1190 
1191 	return NULL;
1192 }
1193 
1194 static inline struct hci_conn *
hci_conn_hash_lookup_per_adv_bis(struct hci_dev * hdev,bdaddr_t * ba,__u8 big,__u8 bis)1195 hci_conn_hash_lookup_per_adv_bis(struct hci_dev *hdev,
1196 				 bdaddr_t *ba,
1197 				 __u8 big, __u8 bis)
1198 {
1199 	struct hci_conn_hash *h = &hdev->conn_hash;
1200 	struct hci_conn  *c;
1201 
1202 	rcu_read_lock();
1203 
1204 	list_for_each_entry_rcu(c, &h->list, list) {
1205 		if (bacmp(&c->dst, ba) || c->type != BIS_LINK ||
1206 		    !test_bit(HCI_CONN_PER_ADV, &c->flags))
1207 			continue;
1208 
1209 		if (c->iso_qos.bcast.big == big &&
1210 		    c->iso_qos.bcast.bis == bis) {
1211 			rcu_read_unlock();
1212 			return c;
1213 		}
1214 	}
1215 	rcu_read_unlock();
1216 
1217 	return NULL;
1218 }
1219 
hci_conn_hash_lookup_handle(struct hci_dev * hdev,__u16 handle)1220 static inline struct hci_conn *hci_conn_hash_lookup_handle(struct hci_dev *hdev,
1221 								__u16 handle)
1222 {
1223 	struct hci_conn_hash *h = &hdev->conn_hash;
1224 	struct hci_conn  *c;
1225 
1226 	rcu_read_lock();
1227 
1228 	list_for_each_entry_rcu(c, &h->list, list) {
1229 		if (c->handle == handle) {
1230 			rcu_read_unlock();
1231 			return c;
1232 		}
1233 	}
1234 	rcu_read_unlock();
1235 
1236 	return NULL;
1237 }
1238 
hci_conn_hash_lookup_ba(struct hci_dev * hdev,__u8 type,bdaddr_t * ba)1239 static inline struct hci_conn *hci_conn_hash_lookup_ba(struct hci_dev *hdev,
1240 							__u8 type, bdaddr_t *ba)
1241 {
1242 	struct hci_conn_hash *h = &hdev->conn_hash;
1243 	struct hci_conn  *c;
1244 
1245 	rcu_read_lock();
1246 
1247 	list_for_each_entry_rcu(c, &h->list, list) {
1248 		if (c->type == type && !bacmp(&c->dst, ba)) {
1249 			rcu_read_unlock();
1250 			return c;
1251 		}
1252 	}
1253 
1254 	rcu_read_unlock();
1255 
1256 	return NULL;
1257 }
1258 
hci_conn_hash_lookup_role(struct hci_dev * hdev,__u8 type,__u8 role,bdaddr_t * ba)1259 static inline struct hci_conn *hci_conn_hash_lookup_role(struct hci_dev *hdev,
1260 							 __u8 type, __u8 role,
1261 							 bdaddr_t *ba)
1262 {
1263 	struct hci_conn_hash *h = &hdev->conn_hash;
1264 	struct hci_conn  *c;
1265 
1266 	rcu_read_lock();
1267 
1268 	list_for_each_entry_rcu(c, &h->list, list) {
1269 		if (c->type == type && c->role == role && !bacmp(&c->dst, ba)) {
1270 			rcu_read_unlock();
1271 			return c;
1272 		}
1273 	}
1274 
1275 	rcu_read_unlock();
1276 
1277 	return NULL;
1278 }
1279 
hci_conn_hash_lookup_le(struct hci_dev * hdev,bdaddr_t * ba,__u8 ba_type)1280 static inline struct hci_conn *hci_conn_hash_lookup_le(struct hci_dev *hdev,
1281 						       bdaddr_t *ba,
1282 						       __u8 ba_type)
1283 {
1284 	struct hci_conn_hash *h = &hdev->conn_hash;
1285 	struct hci_conn  *c;
1286 
1287 	rcu_read_lock();
1288 
1289 	list_for_each_entry_rcu(c, &h->list, list) {
1290 		if (c->type != LE_LINK)
1291 		       continue;
1292 
1293 		if (ba_type == c->dst_type && !bacmp(&c->dst, ba)) {
1294 			rcu_read_unlock();
1295 			return c;
1296 		}
1297 	}
1298 
1299 	rcu_read_unlock();
1300 
1301 	return NULL;
1302 }
1303 
hci_conn_hash_lookup_cis(struct hci_dev * hdev,bdaddr_t * ba,__u8 ba_type,__u8 cig,__u8 id)1304 static inline struct hci_conn *hci_conn_hash_lookup_cis(struct hci_dev *hdev,
1305 							bdaddr_t *ba,
1306 							__u8 ba_type,
1307 							__u8 cig,
1308 							__u8 id)
1309 {
1310 	struct hci_conn_hash *h = &hdev->conn_hash;
1311 	struct hci_conn  *c;
1312 
1313 	rcu_read_lock();
1314 
1315 	list_for_each_entry_rcu(c, &h->list, list) {
1316 		if (c->type != CIS_LINK)
1317 			continue;
1318 
1319 		/* Match CIG ID if set */
1320 		if (cig != c->iso_qos.ucast.cig)
1321 			continue;
1322 
1323 		/* Match CIS ID if set */
1324 		if (id != c->iso_qos.ucast.cis)
1325 			continue;
1326 
1327 		/* Match destination address if set */
1328 		if (!ba || (ba_type == c->dst_type && !bacmp(&c->dst, ba))) {
1329 			rcu_read_unlock();
1330 			return c;
1331 		}
1332 	}
1333 
1334 	rcu_read_unlock();
1335 
1336 	return NULL;
1337 }
1338 
hci_conn_hash_lookup_cig(struct hci_dev * hdev,__u8 handle)1339 static inline struct hci_conn *hci_conn_hash_lookup_cig(struct hci_dev *hdev,
1340 							__u8 handle)
1341 {
1342 	struct hci_conn_hash *h = &hdev->conn_hash;
1343 	struct hci_conn  *c;
1344 
1345 	rcu_read_lock();
1346 
1347 	list_for_each_entry_rcu(c, &h->list, list) {
1348 		if (c->type != CIS_LINK)
1349 			continue;
1350 
1351 		if (handle == c->iso_qos.ucast.cig) {
1352 			rcu_read_unlock();
1353 			return c;
1354 		}
1355 	}
1356 
1357 	rcu_read_unlock();
1358 
1359 	return NULL;
1360 }
1361 
hci_conn_hash_lookup_big(struct hci_dev * hdev,__u8 handle)1362 static inline struct hci_conn *hci_conn_hash_lookup_big(struct hci_dev *hdev,
1363 							__u8 handle)
1364 {
1365 	struct hci_conn_hash *h = &hdev->conn_hash;
1366 	struct hci_conn  *c;
1367 
1368 	rcu_read_lock();
1369 
1370 	list_for_each_entry_rcu(c, &h->list, list) {
1371 		if (c->type != BIS_LINK)
1372 			continue;
1373 
1374 		if (handle == c->iso_qos.bcast.big) {
1375 			rcu_read_unlock();
1376 			return c;
1377 		}
1378 	}
1379 
1380 	rcu_read_unlock();
1381 
1382 	return NULL;
1383 }
1384 
1385 static inline struct hci_conn *
hci_conn_hash_lookup_big_sync_pend(struct hci_dev * hdev,__u8 handle,__u8 num_bis)1386 hci_conn_hash_lookup_big_sync_pend(struct hci_dev *hdev,
1387 				   __u8 handle, __u8 num_bis)
1388 {
1389 	struct hci_conn_hash *h = &hdev->conn_hash;
1390 	struct hci_conn  *c;
1391 
1392 	rcu_read_lock();
1393 
1394 	list_for_each_entry_rcu(c, &h->list, list) {
1395 		if (c->type != PA_LINK)
1396 			continue;
1397 
1398 		if (handle == c->iso_qos.bcast.big && num_bis == c->num_bis) {
1399 			rcu_read_unlock();
1400 			return c;
1401 		}
1402 	}
1403 
1404 	rcu_read_unlock();
1405 
1406 	return NULL;
1407 }
1408 
1409 static inline struct hci_conn *
hci_conn_hash_lookup_big_state(struct hci_dev * hdev,__u8 handle,__u16 state,__u8 role)1410 hci_conn_hash_lookup_big_state(struct hci_dev *hdev, __u8 handle, __u16 state,
1411 			       __u8 role)
1412 {
1413 	struct hci_conn_hash *h = &hdev->conn_hash;
1414 	struct hci_conn  *c;
1415 
1416 	rcu_read_lock();
1417 
1418 	list_for_each_entry_rcu(c, &h->list, list) {
1419 		if (c->type != BIS_LINK || c->state != state || c->role != role)
1420 			continue;
1421 
1422 		if (handle == c->iso_qos.bcast.big) {
1423 			rcu_read_unlock();
1424 			return c;
1425 		}
1426 	}
1427 
1428 	rcu_read_unlock();
1429 
1430 	return NULL;
1431 }
1432 
1433 static inline struct hci_conn *
hci_conn_hash_lookup_pa_sync_big_handle(struct hci_dev * hdev,__u8 big)1434 hci_conn_hash_lookup_pa_sync_big_handle(struct hci_dev *hdev, __u8 big)
1435 {
1436 	struct hci_conn_hash *h = &hdev->conn_hash;
1437 	struct hci_conn  *c;
1438 
1439 	rcu_read_lock();
1440 
1441 	list_for_each_entry_rcu(c, &h->list, list) {
1442 		if (c->type != BIS_LINK ||
1443 		    !test_bit(HCI_CONN_PA_SYNC, &c->flags))
1444 			continue;
1445 
1446 		if (c->iso_qos.bcast.big == big) {
1447 			rcu_read_unlock();
1448 			return c;
1449 		}
1450 	}
1451 	rcu_read_unlock();
1452 
1453 	return NULL;
1454 }
1455 
1456 static inline struct hci_conn *
hci_conn_hash_lookup_pa_sync_handle(struct hci_dev * hdev,__u16 sync_handle)1457 hci_conn_hash_lookup_pa_sync_handle(struct hci_dev *hdev, __u16 sync_handle)
1458 {
1459 	struct hci_conn_hash *h = &hdev->conn_hash;
1460 	struct hci_conn  *c;
1461 
1462 	rcu_read_lock();
1463 
1464 	list_for_each_entry_rcu(c, &h->list, list) {
1465 		if (c->type != PA_LINK)
1466 			continue;
1467 
1468 		/* Ignore the listen hcon, we are looking
1469 		 * for the child hcon that was created as
1470 		 * a result of the PA sync established event.
1471 		 */
1472 		if (c->state == BT_LISTEN)
1473 			continue;
1474 
1475 		if (c->sync_handle == sync_handle) {
1476 			rcu_read_unlock();
1477 			return c;
1478 		}
1479 	}
1480 	rcu_read_unlock();
1481 
1482 	return NULL;
1483 }
1484 
1485 typedef void (*hci_conn_func_t)(struct hci_conn *conn, void *data);
hci_conn_hash_list_state(struct hci_dev * hdev,hci_conn_func_t func,__u8 type,__u16 state,void * data)1486 static inline void hci_conn_hash_list_state(struct hci_dev *hdev,
1487 					    hci_conn_func_t func, __u8 type,
1488 					    __u16 state, void *data)
1489 {
1490 	struct hci_conn_hash *h = &hdev->conn_hash;
1491 	struct hci_conn  *c;
1492 
1493 	if (!func)
1494 		return;
1495 
1496 	rcu_read_lock();
1497 
1498 	list_for_each_entry_rcu(c, &h->list, list) {
1499 		if (c->type == type && c->state == state)
1500 			func(c, data);
1501 	}
1502 
1503 	rcu_read_unlock();
1504 }
1505 
hci_conn_hash_list_flag(struct hci_dev * hdev,hci_conn_func_t func,__u8 type,__u8 flag,void * data)1506 static inline void hci_conn_hash_list_flag(struct hci_dev *hdev,
1507 					    hci_conn_func_t func, __u8 type,
1508 					    __u8 flag, void *data)
1509 {
1510 	struct hci_conn_hash *h = &hdev->conn_hash;
1511 	struct hci_conn  *c;
1512 
1513 	if (!func)
1514 		return;
1515 
1516 	rcu_read_lock();
1517 
1518 	list_for_each_entry_rcu(c, &h->list, list) {
1519 		if (c->type == type && test_bit(flag, &c->flags))
1520 			func(c, data);
1521 	}
1522 
1523 	rcu_read_unlock();
1524 }
1525 
hci_lookup_le_connect(struct hci_dev * hdev)1526 static inline struct hci_conn *hci_lookup_le_connect(struct hci_dev *hdev)
1527 {
1528 	struct hci_conn_hash *h = &hdev->conn_hash;
1529 	struct hci_conn  *c;
1530 
1531 	rcu_read_lock();
1532 
1533 	list_for_each_entry_rcu(c, &h->list, list) {
1534 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1535 		    !test_bit(HCI_CONN_SCANNING, &c->flags)) {
1536 			rcu_read_unlock();
1537 			return c;
1538 		}
1539 	}
1540 
1541 	rcu_read_unlock();
1542 
1543 	return NULL;
1544 }
1545 
1546 /* Returns true if an le connection is in the scanning state */
hci_is_le_conn_scanning(struct hci_dev * hdev)1547 static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
1548 {
1549 	struct hci_conn_hash *h = &hdev->conn_hash;
1550 	struct hci_conn  *c;
1551 
1552 	rcu_read_lock();
1553 
1554 	list_for_each_entry_rcu(c, &h->list, list) {
1555 		if (c->type == LE_LINK && c->state == BT_CONNECT &&
1556 		    test_bit(HCI_CONN_SCANNING, &c->flags)) {
1557 			rcu_read_unlock();
1558 			return true;
1559 		}
1560 	}
1561 
1562 	rcu_read_unlock();
1563 
1564 	return false;
1565 }
1566 
1567 int hci_disconnect(struct hci_conn *conn, __u8 reason);
1568 bool hci_setup_sync(struct hci_conn *conn, __u16 handle);
1569 void hci_sco_setup(struct hci_conn *conn, __u8 status);
1570 bool hci_iso_setup_path(struct hci_conn *conn);
1571 int hci_le_create_cis_pending(struct hci_dev *hdev);
1572 int hci_conn_check_create_cis(struct hci_conn *conn);
1573 
1574 struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst,
1575 			      u8 dst_type, u8 role, u16 handle);
1576 struct hci_conn *hci_conn_add_unset(struct hci_dev *hdev, int type,
1577 				    bdaddr_t *dst, u8 dst_type, u8 role);
1578 void hci_conn_del(struct hci_conn *conn);
1579 void hci_conn_hash_flush(struct hci_dev *hdev);
1580 
1581 struct hci_chan *hci_chan_create(struct hci_conn *conn);
1582 void hci_chan_del(struct hci_chan *chan);
1583 void hci_chan_list_flush(struct hci_conn *conn);
1584 struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle);
1585 
1586 struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst,
1587 				     u8 dst_type, u8 sec_level,
1588 				     u16 conn_timeout,
1589 				     enum conn_reasons conn_reason);
1590 struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
1591 				u8 dst_type, bool dst_resolved, u8 sec_level,
1592 				u16 conn_timeout, u8 role, u8 phy, u8 sec_phy);
1593 void hci_connect_le_scan_cleanup(struct hci_conn *conn, u8 status);
1594 struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
1595 				 u8 sec_level, u8 auth_type,
1596 				 enum conn_reasons conn_reason, u16 timeout);
1597 struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
1598 				 __u16 setting, struct bt_codec *codec,
1599 				 u16 timeout);
1600 struct hci_conn *hci_bind_cis(struct hci_dev *hdev, bdaddr_t *dst,
1601 			      __u8 dst_type, struct bt_iso_qos *qos,
1602 			      u16 timeout);
1603 struct hci_conn *hci_bind_bis(struct hci_dev *hdev, bdaddr_t *dst, __u8 sid,
1604 			      struct bt_iso_qos *qos,
1605 			      __u8 base_len, __u8 *base, u16 timeout);
1606 int hci_past_bis(struct hci_conn *conn, bdaddr_t *dst, __u8 dst_type);
1607 struct hci_conn *hci_connect_cis(struct hci_dev *hdev, bdaddr_t *dst,
1608 				 __u8 dst_type, struct bt_iso_qos *qos,
1609 				 u16 timeout);
1610 struct hci_conn *hci_connect_bis(struct hci_dev *hdev, bdaddr_t *dst,
1611 				 __u8 dst_type, __u8 sid,
1612 				 struct bt_iso_qos *qos,
1613 				 __u8 data_len, __u8 *data, u16 timeout);
1614 struct hci_conn *hci_pa_create_sync(struct hci_dev *hdev, bdaddr_t *dst,
1615 		       __u8 dst_type, __u8 sid, struct bt_iso_qos *qos);
1616 int hci_conn_big_create_sync(struct hci_dev *hdev, struct hci_conn *hcon,
1617 			     struct bt_iso_qos *qos, __u16 sync_handle,
1618 			     __u8 num_bis, __u8 bis[]);
1619 int hci_conn_check_link_mode(struct hci_conn *conn);
1620 int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level);
1621 int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
1622 		      bool initiator);
1623 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
1624 
1625 void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
1626 
1627 void hci_conn_failed(struct hci_conn *conn, u8 status);
1628 u8 hci_conn_set_handle(struct hci_conn *conn, u16 handle);
1629 
1630 void hci_conn_tx_queue(struct hci_conn *conn, struct sk_buff *skb);
1631 void hci_conn_tx_dequeue(struct hci_conn *conn);
1632 void hci_setup_tx_timestamp(struct sk_buff *skb, size_t key_offset,
1633 			    const struct sockcm_cookie *sockc);
1634 
hci_sockcm_init(struct sockcm_cookie * sockc,struct sock * sk)1635 static inline void hci_sockcm_init(struct sockcm_cookie *sockc, struct sock *sk)
1636 {
1637 	*sockc = (struct sockcm_cookie) {
1638 		.tsflags = READ_ONCE(sk->sk_tsflags),
1639 	};
1640 }
1641 
1642 /*
1643  * hci_conn_get() and hci_conn_put() are used to control the life-time of an
1644  * "hci_conn" object. They do not guarantee that the hci_conn object is running,
1645  * working or anything else. They just guarantee that the object is available
1646  * and can be dereferenced. So you can use its locks, local variables and any
1647  * other constant data.
1648  * Before accessing runtime data, you _must_ lock the object and then check that
1649  * it is still running. As soon as you release the locks, the connection might
1650  * get dropped, though.
1651  *
1652  * On the other hand, hci_conn_hold() and hci_conn_drop() are used to control
1653  * how long the underlying connection is held. So every channel that runs on the
1654  * hci_conn object calls this to prevent the connection from disappearing. As
1655  * long as you hold a device, you must also guarantee that you have a valid
1656  * reference to the device via hci_conn_get() (or the initial reference from
1657  * hci_conn_add()).
1658  * The hold()/drop() ref-count is known to drop below 0 sometimes, which doesn't
1659  * break because nobody cares for that. But this means, we cannot use
1660  * _get()/_drop() in it, but require the caller to have a valid ref (FIXME).
1661  */
1662 
hci_conn_get(struct hci_conn * conn)1663 static inline struct hci_conn *hci_conn_get(struct hci_conn *conn)
1664 {
1665 	get_device(&conn->dev);
1666 	return conn;
1667 }
1668 
hci_conn_put(struct hci_conn * conn)1669 static inline void hci_conn_put(struct hci_conn *conn)
1670 {
1671 	put_device(&conn->dev);
1672 }
1673 
hci_conn_hold(struct hci_conn * conn)1674 static inline struct hci_conn *hci_conn_hold(struct hci_conn *conn)
1675 {
1676 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1677 
1678 	atomic_inc(&conn->refcnt);
1679 	cancel_delayed_work(&conn->disc_work);
1680 
1681 	return conn;
1682 }
1683 
hci_conn_drop(struct hci_conn * conn)1684 static inline void hci_conn_drop(struct hci_conn *conn)
1685 {
1686 	BT_DBG("hcon %p orig refcnt %d", conn, atomic_read(&conn->refcnt));
1687 
1688 	if (atomic_dec_and_test(&conn->refcnt)) {
1689 		unsigned long timeo;
1690 
1691 		switch (conn->type) {
1692 		case ACL_LINK:
1693 		case LE_LINK:
1694 			cancel_delayed_work(&conn->idle_work);
1695 			if (conn->state == BT_CONNECTED) {
1696 				timeo = conn->disc_timeout;
1697 				if (!conn->out)
1698 					timeo *= 2;
1699 			} else {
1700 				timeo = 0;
1701 			}
1702 			break;
1703 
1704 		default:
1705 			timeo = 0;
1706 			break;
1707 		}
1708 
1709 		cancel_delayed_work(&conn->disc_work);
1710 		queue_delayed_work(conn->hdev->workqueue,
1711 				   &conn->disc_work, timeo);
1712 	}
1713 }
1714 
1715 /* ----- HCI Devices ----- */
hci_dev_put(struct hci_dev * d)1716 static inline void hci_dev_put(struct hci_dev *d)
1717 {
1718 	BT_DBG("%s orig refcnt %d", d->name,
1719 	       kref_read(&d->dev.kobj.kref));
1720 
1721 	put_device(&d->dev);
1722 }
1723 
hci_dev_hold(struct hci_dev * d)1724 static inline struct hci_dev *hci_dev_hold(struct hci_dev *d)
1725 {
1726 	BT_DBG("%s orig refcnt %d", d->name,
1727 	       kref_read(&d->dev.kobj.kref));
1728 
1729 	get_device(&d->dev);
1730 	return d;
1731 }
1732 
1733 #define hci_dev_lock(d)		mutex_lock(&d->lock)
1734 #define hci_dev_unlock(d)	mutex_unlock(&d->lock)
1735 
1736 #define to_hci_dev(d) container_of(d, struct hci_dev, dev)
1737 #define to_hci_conn(c) container_of(c, struct hci_conn, dev)
1738 
hci_get_drvdata(struct hci_dev * hdev)1739 static inline void *hci_get_drvdata(struct hci_dev *hdev)
1740 {
1741 	return dev_get_drvdata(&hdev->dev);
1742 }
1743 
hci_set_drvdata(struct hci_dev * hdev,void * data)1744 static inline void hci_set_drvdata(struct hci_dev *hdev, void *data)
1745 {
1746 	dev_set_drvdata(&hdev->dev, data);
1747 }
1748 
hci_get_priv(struct hci_dev * hdev)1749 static inline void *hci_get_priv(struct hci_dev *hdev)
1750 {
1751 	return (char *)hdev + sizeof(*hdev);
1752 }
1753 
1754 struct hci_dev *hci_dev_get(int index);
1755 struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, u8 src_type);
1756 
1757 struct hci_dev *hci_alloc_dev_priv(int sizeof_priv);
1758 
hci_alloc_dev(void)1759 static inline struct hci_dev *hci_alloc_dev(void)
1760 {
1761 	return hci_alloc_dev_priv(0);
1762 }
1763 
1764 void hci_free_dev(struct hci_dev *hdev);
1765 int hci_register_dev(struct hci_dev *hdev);
1766 void hci_unregister_dev(struct hci_dev *hdev);
1767 void hci_release_dev(struct hci_dev *hdev);
1768 int hci_register_suspend_notifier(struct hci_dev *hdev);
1769 int hci_unregister_suspend_notifier(struct hci_dev *hdev);
1770 int hci_suspend_dev(struct hci_dev *hdev);
1771 int hci_resume_dev(struct hci_dev *hdev);
1772 int hci_reset_dev(struct hci_dev *hdev);
1773 int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb);
1774 int hci_recv_diag(struct hci_dev *hdev, struct sk_buff *skb);
1775 __printf(2, 3) void hci_set_hw_info(struct hci_dev *hdev, const char *fmt, ...);
1776 __printf(2, 3) void hci_set_fw_info(struct hci_dev *hdev, const char *fmt, ...);
1777 
hci_set_msft_opcode(struct hci_dev * hdev,__u16 opcode)1778 static inline void hci_set_msft_opcode(struct hci_dev *hdev, __u16 opcode)
1779 {
1780 #if IS_ENABLED(CONFIG_BT_MSFTEXT)
1781 	hdev->msft_opcode = opcode;
1782 #endif
1783 }
1784 
hci_set_aosp_capable(struct hci_dev * hdev)1785 static inline void hci_set_aosp_capable(struct hci_dev *hdev)
1786 {
1787 #if IS_ENABLED(CONFIG_BT_AOSPEXT)
1788 	hdev->aosp_capable = true;
1789 #endif
1790 }
1791 
hci_devcd_setup(struct hci_dev * hdev)1792 static inline void hci_devcd_setup(struct hci_dev *hdev)
1793 {
1794 #ifdef CONFIG_DEV_COREDUMP
1795 	INIT_WORK(&hdev->dump.dump_rx, hci_devcd_rx);
1796 	INIT_DELAYED_WORK(&hdev->dump.dump_timeout, hci_devcd_timeout);
1797 	skb_queue_head_init(&hdev->dump.dump_q);
1798 #endif
1799 }
1800 
1801 int hci_dev_open(__u16 dev);
1802 int hci_dev_close(__u16 dev);
1803 int hci_dev_do_close(struct hci_dev *hdev);
1804 int hci_dev_reset(__u16 dev);
1805 int hci_dev_reset_stat(__u16 dev);
1806 int hci_dev_cmd(unsigned int cmd, void __user *arg);
1807 int hci_get_dev_list(void __user *arg);
1808 int hci_get_dev_info(void __user *arg);
1809 int hci_get_conn_list(void __user *arg);
1810 int hci_get_conn_info(struct hci_dev *hdev, void __user *arg);
1811 int hci_get_auth_info(struct hci_dev *hdev, void __user *arg);
1812 int hci_inquiry(void __user *arg);
1813 
1814 struct bdaddr_list *hci_bdaddr_list_lookup(struct list_head *list,
1815 					   bdaddr_t *bdaddr, u8 type);
1816 struct bdaddr_list_with_irk *hci_bdaddr_list_lookup_with_irk(
1817 				    struct list_head *list, bdaddr_t *bdaddr,
1818 				    u8 type);
1819 struct bdaddr_list_with_flags *
1820 hci_bdaddr_list_lookup_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1821 				  u8 type);
1822 int hci_bdaddr_list_add(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1823 int hci_bdaddr_list_add_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1824 				 u8 type, u8 *peer_irk, u8 *local_irk);
1825 int hci_bdaddr_list_add_with_flags(struct list_head *list, bdaddr_t *bdaddr,
1826 				   u8 type, u32 flags);
1827 int hci_bdaddr_list_del(struct list_head *list, bdaddr_t *bdaddr, u8 type);
1828 int hci_bdaddr_list_del_with_irk(struct list_head *list, bdaddr_t *bdaddr,
1829 				 u8 type);
1830 void hci_bdaddr_list_clear(struct list_head *list);
1831 
1832 struct hci_conn_params *hci_conn_params_lookup(struct hci_dev *hdev,
1833 					       bdaddr_t *addr, u8 addr_type);
1834 struct hci_conn_params *hci_conn_params_add(struct hci_dev *hdev,
1835 					    bdaddr_t *addr, u8 addr_type);
1836 void hci_conn_params_del(struct hci_dev *hdev, bdaddr_t *addr, u8 addr_type);
1837 void hci_conn_params_clear_disabled(struct hci_dev *hdev);
1838 void hci_conn_params_free(struct hci_conn_params *param);
1839 
1840 void hci_pend_le_list_del_init(struct hci_conn_params *param);
1841 void hci_pend_le_list_add(struct hci_conn_params *param,
1842 			  struct list_head *list);
1843 struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
1844 						  bdaddr_t *addr,
1845 						  u8 addr_type);
1846 
1847 void hci_uuids_clear(struct hci_dev *hdev);
1848 
1849 void hci_link_keys_clear(struct hci_dev *hdev);
1850 u8 *hci_conn_key_enc_size(struct hci_conn *conn);
1851 struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1852 struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
1853 				  bdaddr_t *bdaddr, u8 *val, u8 type,
1854 				  u8 pin_len, bool *persistent);
1855 struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1856 			    u8 addr_type, u8 type, u8 authenticated,
1857 			    u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand);
1858 struct smp_ltk *hci_find_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1859 			     u8 addr_type, u8 role);
1860 int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type);
1861 void hci_smp_ltks_clear(struct hci_dev *hdev);
1862 int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
1863 
1864 struct smp_irk *hci_find_irk_by_rpa(struct hci_dev *hdev, bdaddr_t *rpa);
1865 struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
1866 				     u8 addr_type);
1867 struct smp_irk *hci_add_irk(struct hci_dev *hdev, bdaddr_t *bdaddr,
1868 			    u8 addr_type, u8 val[16], bdaddr_t *rpa);
1869 void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type);
1870 bool hci_is_blocked_key(struct hci_dev *hdev, u8 type, u8 val[16]);
1871 void hci_blocked_keys_clear(struct hci_dev *hdev);
1872 void hci_smp_irks_clear(struct hci_dev *hdev);
1873 
1874 bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type);
1875 
1876 void hci_remote_oob_data_clear(struct hci_dev *hdev);
1877 struct oob_data *hci_find_remote_oob_data(struct hci_dev *hdev,
1878 					  bdaddr_t *bdaddr, u8 bdaddr_type);
1879 int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1880 			    u8 bdaddr_type, u8 *hash192, u8 *rand192,
1881 			    u8 *hash256, u8 *rand256);
1882 int hci_remove_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr,
1883 			       u8 bdaddr_type);
1884 
1885 void hci_adv_instances_clear(struct hci_dev *hdev);
1886 struct adv_info *hci_find_adv_instance(struct hci_dev *hdev, u8 instance);
1887 struct adv_info *hci_find_adv_sid(struct hci_dev *hdev, u8 sid);
1888 struct adv_info *hci_get_next_instance(struct hci_dev *hdev, u8 instance);
1889 struct adv_info *hci_add_adv_instance(struct hci_dev *hdev, u8 instance,
1890 				      u32 flags, u16 adv_data_len, u8 *adv_data,
1891 				      u16 scan_rsp_len, u8 *scan_rsp_data,
1892 				      u16 timeout, u16 duration, s8 tx_power,
1893 				      u32 min_interval, u32 max_interval,
1894 				      u8 mesh_handle);
1895 struct adv_info *hci_add_per_instance(struct hci_dev *hdev, u8 instance, u8 sid,
1896 				      u32 flags, u8 data_len, u8 *data,
1897 				      u32 min_interval, u32 max_interval);
1898 int hci_set_adv_instance_data(struct hci_dev *hdev, u8 instance,
1899 			 u16 adv_data_len, u8 *adv_data,
1900 			 u16 scan_rsp_len, u8 *scan_rsp_data);
1901 int hci_remove_adv_instance(struct hci_dev *hdev, u8 instance);
1902 void hci_adv_instances_set_rpa_expired(struct hci_dev *hdev, bool rpa_expired);
1903 u32 hci_adv_instance_flags(struct hci_dev *hdev, u8 instance);
1904 bool hci_adv_instance_is_scannable(struct hci_dev *hdev, u8 instance);
1905 
1906 void hci_adv_monitors_clear(struct hci_dev *hdev);
1907 void hci_free_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1908 int hci_add_adv_monitor(struct hci_dev *hdev, struct adv_monitor *monitor);
1909 int hci_remove_single_adv_monitor(struct hci_dev *hdev, u16 handle);
1910 int hci_remove_all_adv_monitor(struct hci_dev *hdev);
1911 bool hci_is_adv_monitoring(struct hci_dev *hdev);
1912 int hci_get_adv_monitor_offload_ext(struct hci_dev *hdev);
1913 
1914 void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb);
1915 
1916 void hci_init_sysfs(struct hci_dev *hdev);
1917 void hci_conn_init_sysfs(struct hci_conn *conn);
1918 void hci_conn_add_sysfs(struct hci_conn *conn);
1919 void hci_conn_del_sysfs(struct hci_conn *conn);
1920 
1921 #define SET_HCIDEV_DEV(hdev, pdev) ((hdev)->dev.parent = (pdev))
1922 #define GET_HCIDEV_DEV(hdev) ((hdev)->dev.parent)
1923 
1924 /* ----- LMP capabilities ----- */
1925 #define lmp_encrypt_capable(dev)   ((dev)->features[0][0] & LMP_ENCRYPT)
1926 #define lmp_rswitch_capable(dev)   ((dev)->features[0][0] & LMP_RSWITCH)
1927 #define lmp_hold_capable(dev)      ((dev)->features[0][0] & LMP_HOLD)
1928 #define lmp_sniff_capable(dev)     ((dev)->features[0][0] & LMP_SNIFF)
1929 #define lmp_park_capable(dev)      ((dev)->features[0][1] & LMP_PARK)
1930 #define lmp_sco_capable(dev)       ((dev)->features[0][1] & LMP_SCO)
1931 #define lmp_inq_rssi_capable(dev)  ((dev)->features[0][3] & LMP_RSSI_INQ)
1932 #define lmp_esco_capable(dev)      ((dev)->features[0][3] & LMP_ESCO)
1933 #define lmp_bredr_capable(dev)     (!((dev)->features[0][4] & LMP_NO_BREDR))
1934 #define lmp_le_capable(dev)        ((dev)->features[0][4] & LMP_LE)
1935 #define lmp_sniffsubr_capable(dev) ((dev)->features[0][5] & LMP_SNIFF_SUBR)
1936 #define lmp_pause_enc_capable(dev) ((dev)->features[0][5] & LMP_PAUSE_ENC)
1937 #define lmp_esco_2m_capable(dev)   ((dev)->features[0][5] & LMP_EDR_ESCO_2M)
1938 #define lmp_ext_inq_capable(dev)   ((dev)->features[0][6] & LMP_EXT_INQ)
1939 #define lmp_le_br_capable(dev)     (!!((dev)->features[0][6] & LMP_SIMUL_LE_BR))
1940 #define lmp_ssp_capable(dev)       ((dev)->features[0][6] & LMP_SIMPLE_PAIR)
1941 #define lmp_no_flush_capable(dev)  ((dev)->features[0][6] & LMP_NO_FLUSH)
1942 #define lmp_lsto_capable(dev)      ((dev)->features[0][7] & LMP_LSTO)
1943 #define lmp_inq_tx_pwr_capable(dev) ((dev)->features[0][7] & LMP_INQ_TX_PWR)
1944 #define lmp_ext_feat_capable(dev)  ((dev)->features[0][7] & LMP_EXTFEATURES)
1945 #define lmp_transp_capable(dev)    ((dev)->features[0][2] & LMP_TRANSPARENT)
1946 #define lmp_edr_2m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_2M)
1947 #define lmp_edr_3m_capable(dev)    ((dev)->features[0][3] & LMP_EDR_3M)
1948 #define lmp_edr_3slot_capable(dev) ((dev)->features[0][4] & LMP_EDR_3SLOT)
1949 #define lmp_edr_5slot_capable(dev) ((dev)->features[0][5] & LMP_EDR_5SLOT)
1950 
1951 /* ----- Extended LMP capabilities ----- */
1952 #define lmp_cpb_central_capable(dev) ((dev)->features[2][0] & LMP_CPB_CENTRAL)
1953 #define lmp_cpb_peripheral_capable(dev) ((dev)->features[2][0] & LMP_CPB_PERIPHERAL)
1954 #define lmp_sync_train_capable(dev) ((dev)->features[2][0] & LMP_SYNC_TRAIN)
1955 #define lmp_sync_scan_capable(dev)  ((dev)->features[2][0] & LMP_SYNC_SCAN)
1956 #define lmp_sc_capable(dev)         ((dev)->features[2][1] & LMP_SC)
1957 #define lmp_ping_capable(dev)       ((dev)->features[2][1] & LMP_PING)
1958 
1959 /* ----- Host capabilities ----- */
1960 #define lmp_host_ssp_capable(dev)  ((dev)->features[1][0] & LMP_HOST_SSP)
1961 #define lmp_host_sc_capable(dev)   ((dev)->features[1][0] & LMP_HOST_SC)
1962 #define lmp_host_le_capable(dev)   (!!((dev)->features[1][0] & LMP_HOST_LE))
1963 #define lmp_host_le_br_capable(dev) (!!((dev)->features[1][0] & LMP_HOST_LE_BREDR))
1964 
1965 #define hdev_is_powered(dev)   (test_bit(HCI_UP, &(dev)->flags) && \
1966 				!hci_dev_test_flag(dev, HCI_AUTO_OFF))
1967 #define bredr_sc_enabled(dev)  (lmp_sc_capable(dev) && \
1968 				hci_dev_test_flag(dev, HCI_SC_ENABLED))
1969 #define rpa_valid(dev)         (bacmp(&dev->rpa, BDADDR_ANY) && \
1970 				!hci_dev_test_flag(dev, HCI_RPA_EXPIRED))
1971 #define adv_rpa_valid(adv)     (bacmp(&adv->random_addr, BDADDR_ANY) && \
1972 				!adv->rpa_expired)
1973 #define le_enabled(dev)        (lmp_le_capable(dev) && \
1974 				hci_dev_test_flag(dev, HCI_LE_ENABLED))
1975 
1976 #define scan_1m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_1M) || \
1977 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_1M))
1978 
1979 #define le_2m_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_2M))
1980 
1981 #define scan_2m(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_2M) || \
1982 		      ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_2M))
1983 
1984 #define le_coded_capable(dev) (((dev)->le_features[1] & HCI_LE_PHY_CODED) && \
1985 			       !hci_test_quirk((dev), \
1986 					       HCI_QUIRK_BROKEN_LE_CODED))
1987 
1988 #define scan_coded(dev) (((dev)->le_tx_def_phys & HCI_LE_SET_PHY_CODED) || \
1989 			 ((dev)->le_rx_def_phys & HCI_LE_SET_PHY_CODED))
1990 
1991 #define ll_privacy_capable(dev) ((dev)->le_features[0] & HCI_LE_LL_PRIVACY)
1992 #define ll_privacy_enabled(dev) (le_enabled(dev) && ll_privacy_capable(dev))
1993 
1994 #define privacy_mode_capable(dev) (ll_privacy_capable(dev) && \
1995 				   ((dev)->commands[39] & 0x04))
1996 
1997 #define read_key_size_capable(dev) \
1998 	((dev)->commands[20] & 0x10 && \
1999 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE))
2000 
2001 #define read_voice_setting_capable(dev) \
2002 	((dev)->commands[9] & 0x04 && \
2003 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_READ_VOICE_SETTING))
2004 
2005 /* Use enhanced synchronous connection if command is supported and its quirk
2006  * has not been set.
2007  */
2008 #define enhanced_sync_conn_capable(dev) \
2009 	(((dev)->commands[29] & 0x08) && \
2010 	 !hci_test_quirk((dev), HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN))
2011 
2012 /* Use ext scanning if set ext scan param and ext scan enable is supported */
2013 #define use_ext_scan(dev) (((dev)->commands[37] & 0x20) && \
2014 			   ((dev)->commands[37] & 0x40) && \
2015 			   !hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_SCAN))
2016 
2017 /* Use ext create connection if command is supported */
2018 #define use_ext_conn(dev) (((dev)->commands[37] & 0x80) && \
2019 	!hci_test_quirk((dev), HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2020 /* Extended advertising support */
2021 #define ext_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_EXT_ADV))
2022 
2023 /* Maximum advertising length */
2024 #define max_adv_len(dev) \
2025 	(ext_adv_capable(dev) ? HCI_MAX_EXT_AD_LENGTH : HCI_MAX_AD_LENGTH)
2026 
2027 /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 1789:
2028  *
2029  * C24: Mandatory if the LE Controller supports Connection State and either
2030  * LE Feature (LL Privacy) or LE Feature (Extended Advertising) is supported
2031  */
2032 #define use_enhanced_conn_complete(dev) ((ll_privacy_capable(dev) || \
2033 					 ext_adv_capable(dev)) && \
2034 					 !hci_test_quirk((dev), \
2035 							 HCI_QUIRK_BROKEN_EXT_CREATE_CONN))
2036 
2037 /* Periodic advertising support */
2038 #define per_adv_capable(dev) (((dev)->le_features[1] & HCI_LE_PERIODIC_ADV))
2039 
2040 /* CIS Master/Slave and BIS support */
2041 #define iso_capable(dev) (cis_capable(dev) || bis_capable(dev))
2042 #define iso_enabled(dev) (le_enabled(dev) && iso_capable(dev))
2043 #define cis_capable(dev) \
2044 	(cis_central_capable(dev) || cis_peripheral_capable(dev))
2045 #define cis_enabled(dev) (le_enabled(dev) && cis_capable(dev))
2046 #define cis_central_capable(dev) \
2047 	((dev)->le_features[3] & HCI_LE_CIS_CENTRAL)
2048 #define cis_central_enabled(dev) \
2049 	(le_enabled(dev) && cis_central_capable(dev))
2050 #define cis_peripheral_capable(dev) \
2051 	((dev)->le_features[3] & HCI_LE_CIS_PERIPHERAL)
2052 #define cis_peripheral_enabled(dev) \
2053 	(le_enabled(dev) && cis_peripheral_capable(dev))
2054 #define bis_capable(dev) ((dev)->le_features[3] & HCI_LE_ISO_BROADCASTER)
2055 #define bis_enabled(dev) (le_enabled(dev) && bis_capable(dev))
2056 #define sync_recv_capable(dev) \
2057 	((dev)->le_features[3] & HCI_LE_ISO_SYNC_RECEIVER)
2058 #define sync_recv_enabled(dev) (le_enabled(dev) && sync_recv_capable(dev))
2059 #define past_sender_capable(dev) \
2060 	((dev)->le_features[3] & HCI_LE_PAST_SENDER)
2061 #define past_receiver_capable(dev) \
2062 	((dev)->le_features[3] & HCI_LE_PAST_RECEIVER)
2063 #define past_capable(dev) \
2064 	(past_sender_capable(dev) || past_receiver_capable(dev))
2065 #define past_sender_enabled(dev) \
2066 	(le_enabled(dev) && past_sender_capable(dev))
2067 #define past_receiver_enabled(dev) \
2068 	(le_enabled(dev) && past_receiver_capable(dev))
2069 #define past_enabled(dev) \
2070 	(past_sender_enabled(dev) || past_receiver_enabled(dev))
2071 #define ll_ext_feature_capable(dev) \
2072 	((dev)->le_features[7] & HCI_LE_LL_EXT_FEATURE)
2073 
2074 #define mws_transport_config_capable(dev) (((dev)->commands[30] & 0x08) && \
2075 	(!hci_test_quirk((dev), HCI_QUIRK_BROKEN_MWS_TRANSPORT_CONFIG)))
2076 
2077 /* ----- HCI protocols ----- */
2078 #define HCI_PROTO_DEFER             0x01
2079 
hci_proto_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 type,__u8 * flags)2080 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
2081 					__u8 type, __u8 *flags)
2082 {
2083 	switch (type) {
2084 	case ACL_LINK:
2085 		return l2cap_connect_ind(hdev, bdaddr);
2086 
2087 	case SCO_LINK:
2088 	case ESCO_LINK:
2089 		return sco_connect_ind(hdev, bdaddr, flags);
2090 
2091 	case CIS_LINK:
2092 	case BIS_LINK:
2093 	case PA_LINK:
2094 		return iso_connect_ind(hdev, bdaddr, flags);
2095 
2096 	default:
2097 		BT_ERR("unknown link type %d", type);
2098 		return -EINVAL;
2099 	}
2100 }
2101 
hci_proto_disconn_ind(struct hci_conn * conn)2102 static inline int hci_proto_disconn_ind(struct hci_conn *conn)
2103 {
2104 	if (conn->type != ACL_LINK && conn->type != LE_LINK)
2105 		return HCI_ERROR_REMOTE_USER_TERM;
2106 
2107 	return l2cap_disconn_ind(conn);
2108 }
2109 
2110 /* ----- HCI callbacks ----- */
2111 struct hci_cb {
2112 	struct list_head list;
2113 
2114 	char *name;
2115 
2116 	void (*connect_cfm)	(struct hci_conn *conn, __u8 status);
2117 	void (*disconn_cfm)	(struct hci_conn *conn, __u8 status);
2118 	void (*security_cfm)	(struct hci_conn *conn, __u8 status,
2119 								__u8 encrypt);
2120 	void (*key_change_cfm)	(struct hci_conn *conn, __u8 status);
2121 	void (*role_switch_cfm)	(struct hci_conn *conn, __u8 status, __u8 role);
2122 };
2123 
hci_connect_cfm(struct hci_conn * conn,__u8 status)2124 static inline void hci_connect_cfm(struct hci_conn *conn, __u8 status)
2125 {
2126 	struct hci_cb *cb;
2127 
2128 	mutex_lock(&hci_cb_list_lock);
2129 	list_for_each_entry(cb, &hci_cb_list, list) {
2130 		if (cb->connect_cfm)
2131 			cb->connect_cfm(conn, status);
2132 	}
2133 	mutex_unlock(&hci_cb_list_lock);
2134 
2135 	if (conn->connect_cfm_cb)
2136 		conn->connect_cfm_cb(conn, status);
2137 }
2138 
hci_disconn_cfm(struct hci_conn * conn,__u8 reason)2139 static inline void hci_disconn_cfm(struct hci_conn *conn, __u8 reason)
2140 {
2141 	struct hci_cb *cb;
2142 
2143 	mutex_lock(&hci_cb_list_lock);
2144 	list_for_each_entry(cb, &hci_cb_list, list) {
2145 		if (cb->disconn_cfm)
2146 			cb->disconn_cfm(conn, reason);
2147 	}
2148 	mutex_unlock(&hci_cb_list_lock);
2149 
2150 	if (conn->disconn_cfm_cb)
2151 		conn->disconn_cfm_cb(conn, reason);
2152 }
2153 
hci_auth_cfm(struct hci_conn * conn,__u8 status)2154 static inline void hci_auth_cfm(struct hci_conn *conn, __u8 status)
2155 {
2156 	struct hci_cb *cb;
2157 	__u8 encrypt;
2158 
2159 	if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags))
2160 		return;
2161 
2162 	encrypt = test_bit(HCI_CONN_ENCRYPT, &conn->flags) ? 0x01 : 0x00;
2163 
2164 	mutex_lock(&hci_cb_list_lock);
2165 	list_for_each_entry(cb, &hci_cb_list, list) {
2166 		if (cb->security_cfm)
2167 			cb->security_cfm(conn, status, encrypt);
2168 	}
2169 	mutex_unlock(&hci_cb_list_lock);
2170 
2171 	if (conn->security_cfm_cb)
2172 		conn->security_cfm_cb(conn, status);
2173 }
2174 
hci_encrypt_cfm(struct hci_conn * conn,__u8 status)2175 static inline void hci_encrypt_cfm(struct hci_conn *conn, __u8 status)
2176 {
2177 	struct hci_cb *cb;
2178 	__u8 encrypt;
2179 
2180 	if (conn->state == BT_CONFIG) {
2181 		if (!status)
2182 			conn->state = BT_CONNECTED;
2183 
2184 		hci_connect_cfm(conn, status);
2185 		hci_conn_drop(conn);
2186 		return;
2187 	}
2188 
2189 	if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
2190 		encrypt = 0x00;
2191 	else if (test_bit(HCI_CONN_AES_CCM, &conn->flags))
2192 		encrypt = 0x02;
2193 	else
2194 		encrypt = 0x01;
2195 
2196 	if (!status) {
2197 		if (conn->sec_level == BT_SECURITY_SDP)
2198 			conn->sec_level = BT_SECURITY_LOW;
2199 
2200 		if (conn->pending_sec_level > conn->sec_level)
2201 			conn->sec_level = conn->pending_sec_level;
2202 	}
2203 
2204 	mutex_lock(&hci_cb_list_lock);
2205 	list_for_each_entry(cb, &hci_cb_list, list) {
2206 		if (cb->security_cfm)
2207 			cb->security_cfm(conn, status, encrypt);
2208 	}
2209 	mutex_unlock(&hci_cb_list_lock);
2210 
2211 	if (conn->security_cfm_cb)
2212 		conn->security_cfm_cb(conn, status);
2213 }
2214 
hci_key_change_cfm(struct hci_conn * conn,__u8 status)2215 static inline void hci_key_change_cfm(struct hci_conn *conn, __u8 status)
2216 {
2217 	struct hci_cb *cb;
2218 
2219 	mutex_lock(&hci_cb_list_lock);
2220 	list_for_each_entry(cb, &hci_cb_list, list) {
2221 		if (cb->key_change_cfm)
2222 			cb->key_change_cfm(conn, status);
2223 	}
2224 	mutex_unlock(&hci_cb_list_lock);
2225 }
2226 
hci_role_switch_cfm(struct hci_conn * conn,__u8 status,__u8 role)2227 static inline void hci_role_switch_cfm(struct hci_conn *conn, __u8 status,
2228 								__u8 role)
2229 {
2230 	struct hci_cb *cb;
2231 
2232 	mutex_lock(&hci_cb_list_lock);
2233 	list_for_each_entry(cb, &hci_cb_list, list) {
2234 		if (cb->role_switch_cfm)
2235 			cb->role_switch_cfm(conn, status, role);
2236 	}
2237 	mutex_unlock(&hci_cb_list_lock);
2238 }
2239 
hci_bdaddr_is_rpa(bdaddr_t * bdaddr,u8 addr_type)2240 static inline bool hci_bdaddr_is_rpa(bdaddr_t *bdaddr, u8 addr_type)
2241 {
2242 	if (addr_type != ADDR_LE_DEV_RANDOM)
2243 		return false;
2244 
2245 	if ((bdaddr->b[5] & 0xc0) == 0x40)
2246 	       return true;
2247 
2248 	return false;
2249 }
2250 
hci_is_identity_address(bdaddr_t * addr,u8 addr_type)2251 static inline bool hci_is_identity_address(bdaddr_t *addr, u8 addr_type)
2252 {
2253 	if (addr_type == ADDR_LE_DEV_PUBLIC)
2254 		return true;
2255 
2256 	/* Check for Random Static address type */
2257 	if ((addr->b[5] & 0xc0) == 0xc0)
2258 		return true;
2259 
2260 	return false;
2261 }
2262 
hci_get_irk(struct hci_dev * hdev,bdaddr_t * bdaddr,u8 addr_type)2263 static inline struct smp_irk *hci_get_irk(struct hci_dev *hdev,
2264 					  bdaddr_t *bdaddr, u8 addr_type)
2265 {
2266 	if (!hci_bdaddr_is_rpa(bdaddr, addr_type))
2267 		return NULL;
2268 
2269 	return hci_find_irk_by_rpa(hdev, bdaddr);
2270 }
2271 
hci_check_conn_params(u16 min,u16 max,u16 latency,u16 to_multiplier)2272 static inline int hci_check_conn_params(u16 min, u16 max, u16 latency,
2273 					u16 to_multiplier)
2274 {
2275 	u16 max_latency;
2276 
2277 	if (min > max) {
2278 		BT_WARN("min %d > max %d", min, max);
2279 		return -EINVAL;
2280 	}
2281 
2282 	if (min < 6) {
2283 		BT_WARN("min %d < 6", min);
2284 		return -EINVAL;
2285 	}
2286 
2287 	if (max > 3200) {
2288 		BT_WARN("max %d > 3200", max);
2289 		return -EINVAL;
2290 	}
2291 
2292 	if (to_multiplier < 10) {
2293 		BT_WARN("to_multiplier %d < 10", to_multiplier);
2294 		return -EINVAL;
2295 	}
2296 
2297 	if (to_multiplier > 3200) {
2298 		BT_WARN("to_multiplier %d > 3200", to_multiplier);
2299 		return -EINVAL;
2300 	}
2301 
2302 	if (max >= to_multiplier * 8) {
2303 		BT_WARN("max %d >= to_multiplier %d * 8", max, to_multiplier);
2304 		return -EINVAL;
2305 	}
2306 
2307 	max_latency = (to_multiplier * 4 / max) - 1;
2308 	if (latency > 499) {
2309 		BT_WARN("latency %d > 499", latency);
2310 		return -EINVAL;
2311 	}
2312 
2313 	if (latency > max_latency) {
2314 		BT_WARN("latency %d > max_latency %d", latency, max_latency);
2315 		return -EINVAL;
2316 	}
2317 
2318 	return 0;
2319 }
2320 
2321 int hci_register_cb(struct hci_cb *hcb);
2322 int hci_unregister_cb(struct hci_cb *hcb);
2323 
2324 int __hci_cmd_send(struct hci_dev *hdev, u16 opcode, u32 plen,
2325 		   const void *param);
2326 
2327 int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
2328 		 const void *param);
2329 void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags);
2330 void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb);
2331 void hci_send_iso(struct hci_conn *conn, struct sk_buff *skb);
2332 
2333 void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode);
2334 void *hci_recv_event_data(struct hci_dev *hdev, __u8 event);
2335 
2336 u32 hci_conn_get_phy(struct hci_conn *conn);
2337 
2338 /* ----- HCI Sockets ----- */
2339 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
2340 void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
2341 			 int flag, struct sock *skip_sk);
2342 void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb);
2343 void hci_send_monitor_ctrl_event(struct hci_dev *hdev, u16 event,
2344 				 void *data, u16 data_len, ktime_t tstamp,
2345 				 int flag, struct sock *skip_sk);
2346 
2347 void hci_sock_dev_event(struct hci_dev *hdev, int event);
2348 
2349 #define HCI_MGMT_VAR_LEN	BIT(0)
2350 #define HCI_MGMT_NO_HDEV	BIT(1)
2351 #define HCI_MGMT_UNTRUSTED	BIT(2)
2352 #define HCI_MGMT_UNCONFIGURED	BIT(3)
2353 #define HCI_MGMT_HDEV_OPTIONAL	BIT(4)
2354 
2355 struct hci_mgmt_handler {
2356 	int (*func) (struct sock *sk, struct hci_dev *hdev, void *data,
2357 		     u16 data_len);
2358 	size_t data_len;
2359 	unsigned long flags;
2360 };
2361 
2362 struct hci_mgmt_chan {
2363 	struct list_head list;
2364 	unsigned short channel;
2365 	size_t handler_count;
2366 	const struct hci_mgmt_handler *handlers;
2367 	void (*hdev_init) (struct sock *sk, struct hci_dev *hdev);
2368 };
2369 
2370 int hci_mgmt_chan_register(struct hci_mgmt_chan *c);
2371 void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c);
2372 
2373 /* Management interface */
2374 #define DISCOV_TYPE_BREDR		(BIT(BDADDR_BREDR))
2375 #define DISCOV_TYPE_LE			(BIT(BDADDR_LE_PUBLIC) | \
2376 					 BIT(BDADDR_LE_RANDOM))
2377 #define DISCOV_TYPE_INTERLEAVED		(BIT(BDADDR_BREDR) | \
2378 					 BIT(BDADDR_LE_PUBLIC) | \
2379 					 BIT(BDADDR_LE_RANDOM))
2380 
2381 /* These LE scan and inquiry parameters were chosen according to LE General
2382  * Discovery Procedure specification.
2383  */
2384 #define DISCOV_LE_SCAN_WIN		0x0012 /* 11.25 msec */
2385 #define DISCOV_LE_SCAN_INT		0x0012 /* 11.25 msec */
2386 #define DISCOV_LE_SCAN_INT_FAST		0x0060 /* 60 msec */
2387 #define DISCOV_LE_SCAN_WIN_FAST		0x0030 /* 30 msec */
2388 #define DISCOV_LE_SCAN_INT_CONN		0x0060 /* 60 msec */
2389 #define DISCOV_LE_SCAN_WIN_CONN		0x0060 /* 60 msec */
2390 #define DISCOV_LE_SCAN_INT_SLOW1	0x0800 /* 1.28 sec */
2391 #define DISCOV_LE_SCAN_WIN_SLOW1	0x0012 /* 11.25 msec */
2392 #define DISCOV_LE_SCAN_INT_SLOW2	0x1000 /* 2.56 sec */
2393 #define DISCOV_LE_SCAN_WIN_SLOW2	0x0024 /* 22.5 msec */
2394 #define DISCOV_CODED_SCAN_INT_FAST	0x0120 /* 180 msec */
2395 #define DISCOV_CODED_SCAN_WIN_FAST	0x0090 /* 90 msec */
2396 #define DISCOV_CODED_SCAN_INT_SLOW1	0x1800 /* 3.84 sec */
2397 #define DISCOV_CODED_SCAN_WIN_SLOW1	0x0036 /* 33.75 msec */
2398 #define DISCOV_CODED_SCAN_INT_SLOW2	0x3000 /* 7.68 sec */
2399 #define DISCOV_CODED_SCAN_WIN_SLOW2	0x006c /* 67.5 msec */
2400 #define DISCOV_LE_TIMEOUT		10240	/* msec */
2401 #define DISCOV_INTERLEAVED_TIMEOUT	5120	/* msec */
2402 #define DISCOV_INTERLEAVED_INQUIRY_LEN	0x04
2403 #define DISCOV_BREDR_INQUIRY_LEN	0x08
2404 #define DISCOV_LE_RESTART_DELAY		msecs_to_jiffies(200)	/* msec */
2405 #define DISCOV_LE_FAST_ADV_INT_MIN	0x00A0	/* 100 msec */
2406 #define DISCOV_LE_FAST_ADV_INT_MAX	0x00F0	/* 150 msec */
2407 #define DISCOV_LE_PER_ADV_INT_MIN	0x00A0	/* 200 msec */
2408 #define DISCOV_LE_PER_ADV_INT_MAX	0x00A0	/* 200 msec */
2409 #define DISCOV_LE_ADV_MESH_MIN		0x00A0  /* 100 msec */
2410 #define DISCOV_LE_ADV_MESH_MAX		0x00A0  /* 100 msec */
2411 #define INTERVAL_TO_MS(x)		(((x) * 10) / 0x10)
2412 
2413 #define NAME_RESOLVE_DURATION		msecs_to_jiffies(10240)	/* 10.24 sec */
2414 
2415 void mgmt_fill_version_info(void *ver);
2416 int mgmt_new_settings(struct hci_dev *hdev);
2417 void mgmt_index_added(struct hci_dev *hdev);
2418 void mgmt_index_removed(struct hci_dev *hdev);
2419 void mgmt_set_powered_failed(struct hci_dev *hdev, int err);
2420 void mgmt_power_on(struct hci_dev *hdev, int err);
2421 void __mgmt_power_off(struct hci_dev *hdev);
2422 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
2423 		       bool persistent);
2424 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
2425 			   u8 *name, u8 name_len);
2426 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
2427 			      u8 link_type, u8 addr_type, u8 reason,
2428 			      bool mgmt_connected);
2429 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
2430 			    u8 link_type, u8 addr_type, u8 status);
2431 void mgmt_connect_failed(struct hci_dev *hdev, struct hci_conn *conn,
2432 			 u8 status);
2433 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure);
2434 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2435 				  u8 status);
2436 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2437 				      u8 status);
2438 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2439 			      u8 link_type, u8 addr_type, u32 value,
2440 			      u8 confirm_hint);
2441 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2442 				     u8 link_type, u8 addr_type, u8 status);
2443 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2444 					 u8 link_type, u8 addr_type, u8 status);
2445 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
2446 			      u8 link_type, u8 addr_type);
2447 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2448 				     u8 link_type, u8 addr_type, u8 status);
2449 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
2450 					 u8 link_type, u8 addr_type, u8 status);
2451 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
2452 			     u8 link_type, u8 addr_type, u32 passkey,
2453 			     u8 entered);
2454 void mgmt_auth_failed(struct hci_conn *conn, u8 status);
2455 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status);
2456 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
2457 				    u8 status);
2458 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status);
2459 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2460 		       u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
2461 		       u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
2462 		       u64 instant);
2463 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
2464 		      u8 addr_type, s8 rssi, u8 *name, u8 name_len);
2465 void mgmt_discovering(struct hci_dev *hdev, u8 discovering);
2466 void mgmt_suspending(struct hci_dev *hdev, u8 state);
2467 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
2468 		   u8 addr_type);
2469 bool mgmt_powering_down(struct hci_dev *hdev);
2470 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent);
2471 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent);
2472 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
2473 		   bool persistent);
2474 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
2475 			 u8 bdaddr_type, u8 store_hint, u16 min_interval,
2476 			 u16 max_interval, u16 latency, u16 timeout);
2477 void mgmt_smp_complete(struct hci_conn *conn, bool complete);
2478 bool mgmt_get_connectable(struct hci_dev *hdev);
2479 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev);
2480 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev,
2481 			    u8 instance);
2482 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
2483 			      u8 instance);
2484 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip);
2485 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
2486 				  bdaddr_t *bdaddr, u8 addr_type);
2487 
2488 int hci_abort_conn(struct hci_conn *conn, u8 reason);
2489 u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
2490 		      u16 to_multiplier);
2491 void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand,
2492 		      __u8 ltk[16], __u8 key_size);
2493 
2494 void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2495 			       u8 *bdaddr_type);
2496 
2497 #define SCO_AIRMODE_MASK       0x0003
2498 #define SCO_AIRMODE_CVSD       0x0000
2499 #define SCO_AIRMODE_TRANSP     0x0003
2500 
2501 #define LOCAL_CODEC_ACL_MASK	BIT(0)
2502 #define LOCAL_CODEC_SCO_MASK	BIT(1)
2503 
2504 #define TRANSPORT_TYPE_MAX	0x04
2505 
2506 #endif /* __HCI_CORE_H */
2507