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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of linux/include/net/bluetooth/bluetooth.h