1 /*
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4    Copyright 2023 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 __BLUETOOTH_H
27 #define __BLUETOOTH_H
28 
29 #include <linux/poll.h>
30 #include <net/sock.h>
31 #include <linux/seq_file.h>
32 
33 #define BT_SUBSYS_VERSION	2
34 #define BT_SUBSYS_REVISION	22
35 
36 #ifndef AF_BLUETOOTH
37 #define AF_BLUETOOTH	31
38 #define PF_BLUETOOTH	AF_BLUETOOTH
39 #endif
40 
41 /* Bluetooth versions */
42 #define BLUETOOTH_VER_1_1	1
43 #define BLUETOOTH_VER_1_2	2
44 #define BLUETOOTH_VER_2_0	3
45 #define BLUETOOTH_VER_2_1	4
46 #define BLUETOOTH_VER_4_0	6
47 
48 /* Reserv for core and drivers use */
49 #define BT_SKB_RESERVE	8
50 
51 #define BTPROTO_L2CAP	0
52 #define BTPROTO_HCI	1
53 #define BTPROTO_SCO	2
54 #define BTPROTO_RFCOMM	3
55 #define BTPROTO_BNEP	4
56 #define BTPROTO_CMTP	5
57 #define BTPROTO_HIDP	6
58 #define BTPROTO_AVDTP	7
59 #define BTPROTO_ISO	8
60 #define BTPROTO_LAST	BTPROTO_ISO
61 
62 #define SOL_HCI		0
63 #define SOL_L2CAP	6
64 #define SOL_SCO		17
65 #define SOL_RFCOMM	18
66 
67 #define BT_SECURITY	4
68 struct bt_security {
69 	__u8 level;
70 	__u8 key_size;
71 };
72 #define BT_SECURITY_SDP		0
73 #define BT_SECURITY_LOW		1
74 #define BT_SECURITY_MEDIUM	2
75 #define BT_SECURITY_HIGH	3
76 #define BT_SECURITY_FIPS	4
77 
78 #define BT_DEFER_SETUP	7
79 
80 #define BT_FLUSHABLE	8
81 
82 #define BT_FLUSHABLE_OFF	0
83 #define BT_FLUSHABLE_ON		1
84 
85 #define BT_POWER	9
86 struct bt_power {
87 	__u8 force_active;
88 };
89 #define BT_POWER_FORCE_ACTIVE_OFF 0
90 #define BT_POWER_FORCE_ACTIVE_ON  1
91 
92 #define BT_CHANNEL_POLICY	10
93 
94 /* BR/EDR only (default policy)
95  *   AMP controllers cannot be used.
96  *   Channel move requests from the remote device are denied.
97  *   If the L2CAP channel is currently using AMP, move the channel to BR/EDR.
98  */
99 #define BT_CHANNEL_POLICY_BREDR_ONLY		0
100 
101 /* BR/EDR Preferred
102  *   Allow use of AMP controllers.
103  *   If the L2CAP channel is currently on AMP, move it to BR/EDR.
104  *   Channel move requests from the remote device are allowed.
105  */
106 #define BT_CHANNEL_POLICY_BREDR_PREFERRED	1
107 
108 /* AMP Preferred
109  *   Allow use of AMP controllers
110  *   If the L2CAP channel is currently on BR/EDR and AMP controller
111  *     resources are available, initiate a channel move to AMP.
112  *   Channel move requests from the remote device are allowed.
113  *   If the L2CAP socket has not been connected yet, try to create
114  *     and configure the channel directly on an AMP controller rather
115  *     than BR/EDR.
116  */
117 #define BT_CHANNEL_POLICY_AMP_PREFERRED		2
118 
119 #define BT_VOICE		11
120 struct bt_voice {
121 	__u16 setting;
122 };
123 
124 #define BT_VOICE_TRANSPARENT			0x0003
125 #define BT_VOICE_CVSD_16BIT			0x0060
126 
127 #define BT_SNDMTU		12
128 #define BT_RCVMTU		13
129 #define BT_PHY			14
130 
131 #define BT_PHY_BR_1M_1SLOT	0x00000001
132 #define BT_PHY_BR_1M_3SLOT	0x00000002
133 #define BT_PHY_BR_1M_5SLOT	0x00000004
134 #define BT_PHY_EDR_2M_1SLOT	0x00000008
135 #define BT_PHY_EDR_2M_3SLOT	0x00000010
136 #define BT_PHY_EDR_2M_5SLOT	0x00000020
137 #define BT_PHY_EDR_3M_1SLOT	0x00000040
138 #define BT_PHY_EDR_3M_3SLOT	0x00000080
139 #define BT_PHY_EDR_3M_5SLOT	0x00000100
140 #define BT_PHY_LE_1M_TX		0x00000200
141 #define BT_PHY_LE_1M_RX		0x00000400
142 #define BT_PHY_LE_2M_TX		0x00000800
143 #define BT_PHY_LE_2M_RX		0x00001000
144 #define BT_PHY_LE_CODED_TX	0x00002000
145 #define BT_PHY_LE_CODED_RX	0x00004000
146 
147 #define BT_MODE			15
148 
149 #define BT_MODE_BASIC		0x00
150 #define BT_MODE_ERTM		0x01
151 #define BT_MODE_STREAMING	0x02
152 #define BT_MODE_LE_FLOWCTL	0x03
153 #define BT_MODE_EXT_FLOWCTL	0x04
154 
155 #define BT_PKT_STATUS           16
156 
157 #define BT_SCM_PKT_STATUS	0x03
158 
159 #define BT_ISO_QOS		17
160 
161 #define BT_ISO_QOS_CIG_UNSET	0xff
162 #define BT_ISO_QOS_CIS_UNSET	0xff
163 
164 #define BT_ISO_QOS_BIG_UNSET	0xff
165 #define BT_ISO_QOS_BIS_UNSET	0xff
166 
167 struct bt_iso_io_qos {
168 	__u32 interval;
169 	__u16 latency;
170 	__u16 sdu;
171 	__u8  phy;
172 	__u8  rtn;
173 };
174 
175 struct bt_iso_ucast_qos {
176 	__u8  cig;
177 	__u8  cis;
178 	__u8  sca;
179 	__u8  packing;
180 	__u8  framing;
181 	struct bt_iso_io_qos in;
182 	struct bt_iso_io_qos out;
183 };
184 
185 struct bt_iso_bcast_qos {
186 	__u8  big;
187 	__u8  bis;
188 	__u8  sync_interval;
189 	__u8  packing;
190 	__u8  framing;
191 	struct bt_iso_io_qos in;
192 	struct bt_iso_io_qos out;
193 	__u8  encryption;
194 	__u8  bcode[16];
195 	__u8  options;
196 	__u16 skip;
197 	__u16 sync_timeout;
198 	__u8  sync_cte_type;
199 	__u8  mse;
200 	__u16 timeout;
201 };
202 
203 struct bt_iso_qos {
204 	union {
205 		struct bt_iso_ucast_qos ucast;
206 		struct bt_iso_bcast_qos bcast;
207 	};
208 };
209 
210 #define BT_ISO_PHY_1M		0x01
211 #define BT_ISO_PHY_2M		0x02
212 #define BT_ISO_PHY_CODED	0x04
213 #define BT_ISO_PHY_ANY		(BT_ISO_PHY_1M | BT_ISO_PHY_2M | \
214 				 BT_ISO_PHY_CODED)
215 
216 #define BT_CODEC	19
217 
218 struct	bt_codec_caps {
219 	__u8	len;
220 	__u8	data[];
221 } __packed;
222 
223 struct bt_codec {
224 	__u8	id;
225 	__u16	cid;
226 	__u16	vid;
227 	__u8	data_path;
228 	__u8	num_caps;
229 } __packed;
230 
231 struct bt_codecs {
232 	__u8		num_codecs;
233 	struct bt_codec	codecs[];
234 } __packed;
235 
236 #define BT_CODEC_CVSD		0x02
237 #define BT_CODEC_TRANSPARENT	0x03
238 #define BT_CODEC_MSBC		0x05
239 
240 #define BT_ISO_BASE		20
241 
242 __printf(1, 2)
243 void bt_info(const char *fmt, ...);
244 __printf(1, 2)
245 void bt_warn(const char *fmt, ...);
246 __printf(1, 2)
247 void bt_err(const char *fmt, ...);
248 #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
249 void bt_dbg_set(bool enable);
250 bool bt_dbg_get(void);
251 __printf(1, 2)
252 void bt_dbg(const char *fmt, ...);
253 #endif
254 __printf(1, 2)
255 void bt_warn_ratelimited(const char *fmt, ...);
256 __printf(1, 2)
257 void bt_err_ratelimited(const char *fmt, ...);
258 
259 #define BT_INFO(fmt, ...)	bt_info(fmt "\n", ##__VA_ARGS__)
260 #define BT_WARN(fmt, ...)	bt_warn(fmt "\n", ##__VA_ARGS__)
261 #define BT_ERR(fmt, ...)	bt_err(fmt "\n", ##__VA_ARGS__)
262 
263 #if IS_ENABLED(CONFIG_BT_FEATURE_DEBUG)
264 #define BT_DBG(fmt, ...)	bt_dbg(fmt "\n", ##__VA_ARGS__)
265 #else
266 #define BT_DBG(fmt, ...)	pr_debug(fmt "\n", ##__VA_ARGS__)
267 #endif
268 
269 #define bt_dev_name(hdev) ((hdev) ? (hdev)->name : "null")
270 
271 #define bt_dev_info(hdev, fmt, ...)				\
272 	BT_INFO("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
273 #define bt_dev_warn(hdev, fmt, ...)				\
274 	BT_WARN("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
275 #define bt_dev_err(hdev, fmt, ...)				\
276 	BT_ERR("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
277 #define bt_dev_dbg(hdev, fmt, ...)				\
278 	BT_DBG("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
279 
280 #define bt_dev_warn_ratelimited(hdev, fmt, ...)			\
281 	bt_warn_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
282 #define bt_dev_err_ratelimited(hdev, fmt, ...)			\
283 	bt_err_ratelimited("%s: " fmt, bt_dev_name(hdev), ##__VA_ARGS__)
284 
285 /* Connection and socket states */
286 enum {
287 	BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */
288 	BT_OPEN,
289 	BT_BOUND,
290 	BT_LISTEN,
291 	BT_CONNECT,
292 	BT_CONNECT2,
293 	BT_CONFIG,
294 	BT_DISCONN,
295 	BT_CLOSED
296 };
297 
298 /* If unused will be removed by compiler */
299 static inline const char *state_to_string(int state)
300 {
301 	switch (state) {
302 	case BT_CONNECTED:
303 		return "BT_CONNECTED";
304 	case BT_OPEN:
305 		return "BT_OPEN";
306 	case BT_BOUND:
307 		return "BT_BOUND";
308 	case BT_LISTEN:
309 		return "BT_LISTEN";
310 	case BT_CONNECT:
311 		return "BT_CONNECT";
312 	case BT_CONNECT2:
313 		return "BT_CONNECT2";
314 	case BT_CONFIG:
315 		return "BT_CONFIG";
316 	case BT_DISCONN:
317 		return "BT_DISCONN";
318 	case BT_CLOSED:
319 		return "BT_CLOSED";
320 	}
321 
322 	return "invalid state";
323 }
324 
325 /* BD Address */
326 typedef struct {
327 	__u8 b[6];
328 } __packed bdaddr_t;
329 
330 /* BD Address type */
331 #define BDADDR_BREDR		0x00
332 #define BDADDR_LE_PUBLIC	0x01
333 #define BDADDR_LE_RANDOM	0x02
334 
335 static inline bool bdaddr_type_is_valid(u8 type)
336 {
337 	switch (type) {
338 	case BDADDR_BREDR:
339 	case BDADDR_LE_PUBLIC:
340 	case BDADDR_LE_RANDOM:
341 		return true;
342 	}
343 
344 	return false;
345 }
346 
347 static inline bool bdaddr_type_is_le(u8 type)
348 {
349 	switch (type) {
350 	case BDADDR_LE_PUBLIC:
351 	case BDADDR_LE_RANDOM:
352 		return true;
353 	}
354 
355 	return false;
356 }
357 
358 #define BDADDR_ANY  (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
359 #define BDADDR_NONE (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
360 
361 /* Copy, swap, convert BD Address */
362 static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
363 {
364 	return memcmp(ba1, ba2, sizeof(bdaddr_t));
365 }
366 static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
367 {
368 	memcpy(dst, src, sizeof(bdaddr_t));
369 }
370 
371 void baswap(bdaddr_t *dst, const bdaddr_t *src);
372 
373 /* Common socket structures and functions */
374 
375 #define bt_sk(__sk) ((struct bt_sock *) __sk)
376 
377 struct bt_sock {
378 	struct sock sk;
379 	struct list_head accept_q;
380 	struct sock *parent;
381 	unsigned long flags;
382 	void (*skb_msg_name)(struct sk_buff *, void *, int *);
383 	void (*skb_put_cmsg)(struct sk_buff *, struct msghdr *, struct sock *);
384 };
385 
386 enum {
387 	BT_SK_DEFER_SETUP,
388 	BT_SK_SUSPEND,
389 };
390 
391 struct bt_sock_list {
392 	struct hlist_head head;
393 	rwlock_t          lock;
394 #ifdef CONFIG_PROC_FS
395         int (* custom_seq_show)(struct seq_file *, void *);
396 #endif
397 };
398 
399 int  bt_sock_register(int proto, const struct net_proto_family *ops);
400 void bt_sock_unregister(int proto);
401 void bt_sock_link(struct bt_sock_list *l, struct sock *s);
402 void bt_sock_unlink(struct bt_sock_list *l, struct sock *s);
403 int  bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
404 		     int flags);
405 int  bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
406 			    size_t len, int flags);
407 __poll_t bt_sock_poll(struct file *file, struct socket *sock, poll_table *wait);
408 int  bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
409 int  bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo);
410 int  bt_sock_wait_ready(struct sock *sk, unsigned int msg_flags);
411 
412 void bt_accept_enqueue(struct sock *parent, struct sock *sk, bool bh);
413 void bt_accept_unlink(struct sock *sk);
414 struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock);
415 
416 /* Skb helpers */
417 struct l2cap_ctrl {
418 	u8	sframe:1,
419 		poll:1,
420 		final:1,
421 		fcs:1,
422 		sar:2,
423 		super:2;
424 
425 	u16	reqseq;
426 	u16	txseq;
427 	u8	retries;
428 	__le16  psm;
429 	bdaddr_t bdaddr;
430 	struct l2cap_chan *chan;
431 };
432 
433 struct sco_ctrl {
434 	u8	pkt_status;
435 };
436 
437 struct hci_dev;
438 
439 typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
440 typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
441 				       u16 opcode, struct sk_buff *skb);
442 
443 #define HCI_REQ_START	BIT(0)
444 #define HCI_REQ_SKB	BIT(1)
445 
446 struct hci_ctrl {
447 	struct sock *sk;
448 	u16 opcode;
449 	u8 req_flags;
450 	u8 req_event;
451 	union {
452 		hci_req_complete_t req_complete;
453 		hci_req_complete_skb_t req_complete_skb;
454 	};
455 };
456 
457 struct mgmt_ctrl {
458 	struct hci_dev *hdev;
459 	u16 opcode;
460 };
461 
462 struct bt_skb_cb {
463 	u8 pkt_type;
464 	u8 force_active;
465 	u16 expect;
466 	u8 incoming:1;
467 	union {
468 		struct l2cap_ctrl l2cap;
469 		struct sco_ctrl sco;
470 		struct hci_ctrl hci;
471 		struct mgmt_ctrl mgmt;
472 	};
473 };
474 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
475 
476 #define hci_skb_pkt_type(skb) bt_cb((skb))->pkt_type
477 #define hci_skb_expect(skb) bt_cb((skb))->expect
478 #define hci_skb_opcode(skb) bt_cb((skb))->hci.opcode
479 #define hci_skb_event(skb) bt_cb((skb))->hci.req_event
480 #define hci_skb_sk(skb) bt_cb((skb))->hci.sk
481 
482 static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
483 {
484 	struct sk_buff *skb;
485 
486 	skb = alloc_skb(len + BT_SKB_RESERVE, how);
487 	if (skb)
488 		skb_reserve(skb, BT_SKB_RESERVE);
489 	return skb;
490 }
491 
492 static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
493 					unsigned long len, int nb, int *err)
494 {
495 	struct sk_buff *skb;
496 
497 	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
498 	if (skb)
499 		skb_reserve(skb, BT_SKB_RESERVE);
500 
501 	if (!skb && *err)
502 		return NULL;
503 
504 	*err = sock_error(sk);
505 	if (*err)
506 		goto out;
507 
508 	if (sk->sk_shutdown) {
509 		*err = -ECONNRESET;
510 		goto out;
511 	}
512 
513 	return skb;
514 
515 out:
516 	kfree_skb(skb);
517 	return NULL;
518 }
519 
520 /* Shall not be called with lock_sock held */
521 static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,
522 					     struct msghdr *msg,
523 					     size_t len, size_t mtu,
524 					     size_t headroom, size_t tailroom)
525 {
526 	struct sk_buff *skb;
527 	size_t size = min_t(size_t, len, mtu);
528 	int err;
529 
530 	skb = bt_skb_send_alloc(sk, size + headroom + tailroom,
531 				msg->msg_flags & MSG_DONTWAIT, &err);
532 	if (!skb)
533 		return ERR_PTR(err);
534 
535 	skb_reserve(skb, headroom);
536 	skb_tailroom_reserve(skb, mtu, tailroom);
537 
538 	if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {
539 		kfree_skb(skb);
540 		return ERR_PTR(-EFAULT);
541 	}
542 
543 	skb->priority = sk->sk_priority;
544 
545 	return skb;
546 }
547 
548 /* Similar to bt_skb_sendmsg but can split the msg into multiple fragments
549  * accourding to the MTU.
550  */
551 static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk,
552 					      struct msghdr *msg,
553 					      size_t len, size_t mtu,
554 					      size_t headroom, size_t tailroom)
555 {
556 	struct sk_buff *skb, **frag;
557 
558 	skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
559 	if (IS_ERR(skb))
560 		return skb;
561 
562 	len -= skb->len;
563 	if (!len)
564 		return skb;
565 
566 	/* Add remaining data over MTU as continuation fragments */
567 	frag = &skb_shinfo(skb)->frag_list;
568 	while (len) {
569 		struct sk_buff *tmp;
570 
571 		tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom);
572 		if (IS_ERR(tmp)) {
573 			return skb;
574 		}
575 
576 		len -= tmp->len;
577 
578 		*frag = tmp;
579 		frag = &(*frag)->next;
580 	}
581 
582 	return skb;
583 }
584 
585 int bt_to_errno(u16 code);
586 __u8 bt_status(int err);
587 
588 void hci_sock_set_flag(struct sock *sk, int nr);
589 void hci_sock_clear_flag(struct sock *sk, int nr);
590 int hci_sock_test_flag(struct sock *sk, int nr);
591 unsigned short hci_sock_get_channel(struct sock *sk);
592 u32 hci_sock_get_cookie(struct sock *sk);
593 
594 int hci_sock_init(void);
595 void hci_sock_cleanup(void);
596 
597 int bt_sysfs_init(void);
598 void bt_sysfs_cleanup(void);
599 
600 int bt_procfs_init(struct net *net, const char *name,
601 		   struct bt_sock_list *sk_list,
602 		   int (*seq_show)(struct seq_file *, void *));
603 void bt_procfs_cleanup(struct net *net, const char *name);
604 
605 extern struct dentry *bt_debugfs;
606 
607 int l2cap_init(void);
608 void l2cap_exit(void);
609 
610 #if IS_ENABLED(CONFIG_BT_BREDR)
611 int sco_init(void);
612 void sco_exit(void);
613 #else
614 static inline int sco_init(void)
615 {
616 	return 0;
617 }
618 
619 static inline void sco_exit(void)
620 {
621 }
622 #endif
623 
624 #if IS_ENABLED(CONFIG_BT_LE)
625 int iso_init(void);
626 int iso_exit(void);
627 bool iso_enabled(void);
628 #else
629 static inline int iso_init(void)
630 {
631 	return 0;
632 }
633 
634 static inline int iso_exit(void)
635 {
636 	return 0;
637 }
638 
639 static inline bool iso_enabled(void)
640 {
641 	return false;
642 }
643 #endif
644 
645 int mgmt_init(void);
646 void mgmt_exit(void);
647 void mgmt_cleanup(struct sock *sk);
648 
649 void bt_sock_reclassify_lock(struct sock *sk, int proto);
650 
651 #endif /* __BLUETOOTH_H */
652