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