xref: /openbmc/linux/net/bluetooth/iso.c (revision 06149746)
1ccf74f23SLuiz Augusto von Dentz // SPDX-License-Identifier: GPL-2.0
2ccf74f23SLuiz Augusto von Dentz /*
3ccf74f23SLuiz Augusto von Dentz  * BlueZ - Bluetooth protocol stack for Linux
4ccf74f23SLuiz Augusto von Dentz  *
5ccf74f23SLuiz Augusto von Dentz  * Copyright (C) 2022 Intel Corporation
60fe8c8d0SIulia Tanasescu  * Copyright 2023 NXP
7ccf74f23SLuiz Augusto von Dentz  */
8ccf74f23SLuiz Augusto von Dentz 
9ccf74f23SLuiz Augusto von Dentz #include <linux/module.h>
10ccf74f23SLuiz Augusto von Dentz #include <linux/debugfs.h>
11ccf74f23SLuiz Augusto von Dentz #include <linux/seq_file.h>
12ccf74f23SLuiz Augusto von Dentz #include <linux/sched/signal.h>
13ccf74f23SLuiz Augusto von Dentz 
14ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/bluetooth.h>
15ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/hci_core.h>
16ccf74f23SLuiz Augusto von Dentz #include <net/bluetooth/iso.h>
17ccf74f23SLuiz Augusto von Dentz 
18ccf74f23SLuiz Augusto von Dentz static const struct proto_ops iso_sock_ops;
19ccf74f23SLuiz Augusto von Dentz 
20ccf74f23SLuiz Augusto von Dentz static struct bt_sock_list iso_sk_list = {
21ccf74f23SLuiz Augusto von Dentz 	.lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
22ccf74f23SLuiz Augusto von Dentz };
23ccf74f23SLuiz Augusto von Dentz 
24ccf74f23SLuiz Augusto von Dentz /* ---- ISO connections ---- */
25ccf74f23SLuiz Augusto von Dentz struct iso_conn {
26ccf74f23SLuiz Augusto von Dentz 	struct hci_conn	*hcon;
27ccf74f23SLuiz Augusto von Dentz 
28ccf74f23SLuiz Augusto von Dentz 	/* @lock: spinlock protecting changes to iso_conn fields */
29ccf74f23SLuiz Augusto von Dentz 	spinlock_t	lock;
30ccf74f23SLuiz Augusto von Dentz 	struct sock	*sk;
31ccf74f23SLuiz Augusto von Dentz 
32ccf74f23SLuiz Augusto von Dentz 	struct delayed_work	timeout_work;
33ccf74f23SLuiz Augusto von Dentz 
34ccf74f23SLuiz Augusto von Dentz 	struct sk_buff	*rx_skb;
35ccf74f23SLuiz Augusto von Dentz 	__u32		rx_len;
36ccf74f23SLuiz Augusto von Dentz 	__u16		tx_sn;
37ccf74f23SLuiz Augusto von Dentz };
38ccf74f23SLuiz Augusto von Dentz 
39ccf74f23SLuiz Augusto von Dentz #define iso_conn_lock(c)	spin_lock(&(c)->lock)
40ccf74f23SLuiz Augusto von Dentz #define iso_conn_unlock(c)	spin_unlock(&(c)->lock)
41ccf74f23SLuiz Augusto von Dentz 
42ccf74f23SLuiz Augusto von Dentz static void iso_sock_close(struct sock *sk);
43ccf74f23SLuiz Augusto von Dentz static void iso_sock_kill(struct sock *sk);
44ccf74f23SLuiz Augusto von Dentz 
45ccf74f23SLuiz Augusto von Dentz /* ----- ISO socket info ----- */
46ccf74f23SLuiz Augusto von Dentz #define iso_pi(sk) ((struct iso_pinfo *)sk)
47ccf74f23SLuiz Augusto von Dentz 
48b4443423SLuiz Augusto von Dentz #define EIR_SERVICE_DATA_LENGTH 4
49b4443423SLuiz Augusto von Dentz #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
50b4443423SLuiz Augusto von Dentz 
51ccf74f23SLuiz Augusto von Dentz struct iso_pinfo {
52ccf74f23SLuiz Augusto von Dentz 	struct bt_sock		bt;
53ccf74f23SLuiz Augusto von Dentz 	bdaddr_t		src;
54ccf74f23SLuiz Augusto von Dentz 	__u8			src_type;
55ccf74f23SLuiz Augusto von Dentz 	bdaddr_t		dst;
56ccf74f23SLuiz Augusto von Dentz 	__u8			dst_type;
57f764a6c2SLuiz Augusto von Dentz 	__u8			bc_sid;
58f764a6c2SLuiz Augusto von Dentz 	__u8			bc_num_bis;
59f764a6c2SLuiz Augusto von Dentz 	__u8			bc_bis[ISO_MAX_NUM_BIS];
60f764a6c2SLuiz Augusto von Dentz 	__u16			sync_handle;
61ccf74f23SLuiz Augusto von Dentz 	__u32			flags;
62ccf74f23SLuiz Augusto von Dentz 	struct bt_iso_qos	qos;
630fe8c8d0SIulia Tanasescu 	bool			qos_user_set;
64f764a6c2SLuiz Augusto von Dentz 	__u8			base_len;
65b4443423SLuiz Augusto von Dentz 	__u8			base[BASE_MAX_LENGTH];
66ccf74f23SLuiz Augusto von Dentz 	struct iso_conn		*conn;
67ccf74f23SLuiz Augusto von Dentz };
68ccf74f23SLuiz Augusto von Dentz 
690fe8c8d0SIulia Tanasescu static struct bt_iso_qos default_qos;
700fe8c8d0SIulia Tanasescu 
710fe8c8d0SIulia Tanasescu static bool check_ucast_qos(struct bt_iso_qos *qos);
720fe8c8d0SIulia Tanasescu static bool check_bcast_qos(struct bt_iso_qos *qos);
730fe8c8d0SIulia Tanasescu 
74ccf74f23SLuiz Augusto von Dentz /* ---- ISO timers ---- */
75ccf74f23SLuiz Augusto von Dentz #define ISO_CONN_TIMEOUT	(HZ * 40)
76ccf74f23SLuiz Augusto von Dentz #define ISO_DISCONN_TIMEOUT	(HZ * 2)
77ccf74f23SLuiz Augusto von Dentz 
78ccf74f23SLuiz Augusto von Dentz static void iso_sock_timeout(struct work_struct *work)
79ccf74f23SLuiz Augusto von Dentz {
80ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = container_of(work, struct iso_conn,
81ccf74f23SLuiz Augusto von Dentz 					     timeout_work.work);
82ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
83ccf74f23SLuiz Augusto von Dentz 
84ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
85ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
86ccf74f23SLuiz Augusto von Dentz 	if (sk)
87ccf74f23SLuiz Augusto von Dentz 		sock_hold(sk);
88ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
89ccf74f23SLuiz Augusto von Dentz 
90ccf74f23SLuiz Augusto von Dentz 	if (!sk)
91ccf74f23SLuiz Augusto von Dentz 		return;
92ccf74f23SLuiz Augusto von Dentz 
93ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d", sk, sk->sk_state);
94ccf74f23SLuiz Augusto von Dentz 
95ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
96ccf74f23SLuiz Augusto von Dentz 	sk->sk_err = ETIMEDOUT;
97ccf74f23SLuiz Augusto von Dentz 	sk->sk_state_change(sk);
98ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
99ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
100ccf74f23SLuiz Augusto von Dentz }
101ccf74f23SLuiz Augusto von Dentz 
102ccf74f23SLuiz Augusto von Dentz static void iso_sock_set_timer(struct sock *sk, long timeout)
103ccf74f23SLuiz Augusto von Dentz {
104ccf74f23SLuiz Augusto von Dentz 	if (!iso_pi(sk)->conn)
105ccf74f23SLuiz Augusto von Dentz 		return;
106ccf74f23SLuiz Augusto von Dentz 
107ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
108ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
109ccf74f23SLuiz Augusto von Dentz 	schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
110ccf74f23SLuiz Augusto von Dentz }
111ccf74f23SLuiz Augusto von Dentz 
112ccf74f23SLuiz Augusto von Dentz static void iso_sock_clear_timer(struct sock *sk)
113ccf74f23SLuiz Augusto von Dentz {
114ccf74f23SLuiz Augusto von Dentz 	if (!iso_pi(sk)->conn)
115ccf74f23SLuiz Augusto von Dentz 		return;
116ccf74f23SLuiz Augusto von Dentz 
117ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p state %d", sk, sk->sk_state);
118ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
119ccf74f23SLuiz Augusto von Dentz }
120ccf74f23SLuiz Augusto von Dentz 
121ccf74f23SLuiz Augusto von Dentz /* ---- ISO connections ---- */
122ccf74f23SLuiz Augusto von Dentz static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
123ccf74f23SLuiz Augusto von Dentz {
124ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
125ccf74f23SLuiz Augusto von Dentz 
126ccf74f23SLuiz Augusto von Dentz 	if (conn)
127ccf74f23SLuiz Augusto von Dentz 		return conn;
128ccf74f23SLuiz Augusto von Dentz 
129ccf74f23SLuiz Augusto von Dentz 	conn = kzalloc(sizeof(*conn), GFP_KERNEL);
130ccf74f23SLuiz Augusto von Dentz 	if (!conn)
131ccf74f23SLuiz Augusto von Dentz 		return NULL;
132ccf74f23SLuiz Augusto von Dentz 
133ccf74f23SLuiz Augusto von Dentz 	spin_lock_init(&conn->lock);
134ccf74f23SLuiz Augusto von Dentz 	INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
135ccf74f23SLuiz Augusto von Dentz 
136ccf74f23SLuiz Augusto von Dentz 	hcon->iso_data = conn;
137ccf74f23SLuiz Augusto von Dentz 	conn->hcon = hcon;
138ccf74f23SLuiz Augusto von Dentz 	conn->tx_sn = 0;
139ccf74f23SLuiz Augusto von Dentz 
140ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p", hcon, conn);
141ccf74f23SLuiz Augusto von Dentz 
142ccf74f23SLuiz Augusto von Dentz 	return conn;
143ccf74f23SLuiz Augusto von Dentz }
144ccf74f23SLuiz Augusto von Dentz 
145ccf74f23SLuiz Augusto von Dentz /* Delete channel. Must be called on the locked socket. */
146ccf74f23SLuiz Augusto von Dentz static void iso_chan_del(struct sock *sk, int err)
147ccf74f23SLuiz Augusto von Dentz {
148ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn;
149f764a6c2SLuiz Augusto von Dentz 	struct sock *parent;
150ccf74f23SLuiz Augusto von Dentz 
151ccf74f23SLuiz Augusto von Dentz 	conn = iso_pi(sk)->conn;
152ccf74f23SLuiz Augusto von Dentz 
153ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
154ccf74f23SLuiz Augusto von Dentz 
155ccf74f23SLuiz Augusto von Dentz 	if (conn) {
156ccf74f23SLuiz Augusto von Dentz 		iso_conn_lock(conn);
157ccf74f23SLuiz Augusto von Dentz 		conn->sk = NULL;
158ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->conn = NULL;
159ccf74f23SLuiz Augusto von Dentz 		iso_conn_unlock(conn);
160ccf74f23SLuiz Augusto von Dentz 
161ccf74f23SLuiz Augusto von Dentz 		if (conn->hcon)
162ccf74f23SLuiz Augusto von Dentz 			hci_conn_drop(conn->hcon);
163ccf74f23SLuiz Augusto von Dentz 	}
164ccf74f23SLuiz Augusto von Dentz 
165ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_CLOSED;
166ccf74f23SLuiz Augusto von Dentz 	sk->sk_err   = err;
167f764a6c2SLuiz Augusto von Dentz 
168f764a6c2SLuiz Augusto von Dentz 	parent = bt_sk(sk)->parent;
169f764a6c2SLuiz Augusto von Dentz 	if (parent) {
170f764a6c2SLuiz Augusto von Dentz 		bt_accept_unlink(sk);
171f764a6c2SLuiz Augusto von Dentz 		parent->sk_data_ready(parent);
172f764a6c2SLuiz Augusto von Dentz 	} else {
173ccf74f23SLuiz Augusto von Dentz 		sk->sk_state_change(sk);
174f764a6c2SLuiz Augusto von Dentz 	}
175ccf74f23SLuiz Augusto von Dentz 
176ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(sk, SOCK_ZAPPED);
177ccf74f23SLuiz Augusto von Dentz }
178ccf74f23SLuiz Augusto von Dentz 
179ccf74f23SLuiz Augusto von Dentz static void iso_conn_del(struct hci_conn *hcon, int err)
180ccf74f23SLuiz Augusto von Dentz {
181ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
182ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
183ccf74f23SLuiz Augusto von Dentz 
184ccf74f23SLuiz Augusto von Dentz 	if (!conn)
185ccf74f23SLuiz Augusto von Dentz 		return;
186ccf74f23SLuiz Augusto von Dentz 
187ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
188ccf74f23SLuiz Augusto von Dentz 
189ccf74f23SLuiz Augusto von Dentz 	/* Kill socket */
190ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
191ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
192ccf74f23SLuiz Augusto von Dentz 	if (sk)
193ccf74f23SLuiz Augusto von Dentz 		sock_hold(sk);
194ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
195ccf74f23SLuiz Augusto von Dentz 
196ccf74f23SLuiz Augusto von Dentz 	if (sk) {
197ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
198ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
199ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, err);
200ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
201ccf74f23SLuiz Augusto von Dentz 		sock_put(sk);
202ccf74f23SLuiz Augusto von Dentz 	}
203ccf74f23SLuiz Augusto von Dentz 
204ccf74f23SLuiz Augusto von Dentz 	/* Ensure no more work items will run before freeing conn. */
205ccf74f23SLuiz Augusto von Dentz 	cancel_delayed_work_sync(&conn->timeout_work);
206ccf74f23SLuiz Augusto von Dentz 
207ccf74f23SLuiz Augusto von Dentz 	hcon->iso_data = NULL;
208ccf74f23SLuiz Augusto von Dentz 	kfree(conn);
209ccf74f23SLuiz Augusto von Dentz }
210ccf74f23SLuiz Augusto von Dentz 
211ccf74f23SLuiz Augusto von Dentz static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
212ccf74f23SLuiz Augusto von Dentz 			  struct sock *parent)
213ccf74f23SLuiz Augusto von Dentz {
214ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
215ccf74f23SLuiz Augusto von Dentz 
216ccf74f23SLuiz Augusto von Dentz 	if (iso_pi(sk)->conn == conn && conn->sk == sk)
217ccf74f23SLuiz Augusto von Dentz 		return 0;
218ccf74f23SLuiz Augusto von Dentz 
219ccf74f23SLuiz Augusto von Dentz 	if (conn->sk) {
220ccf74f23SLuiz Augusto von Dentz 		BT_ERR("conn->sk already set");
221ccf74f23SLuiz Augusto von Dentz 		return -EBUSY;
222ccf74f23SLuiz Augusto von Dentz 	}
223ccf74f23SLuiz Augusto von Dentz 
224ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->conn = conn;
225ccf74f23SLuiz Augusto von Dentz 	conn->sk = sk;
226ccf74f23SLuiz Augusto von Dentz 
227ccf74f23SLuiz Augusto von Dentz 	if (parent)
228ccf74f23SLuiz Augusto von Dentz 		bt_accept_enqueue(parent, sk, true);
229ccf74f23SLuiz Augusto von Dentz 
230ccf74f23SLuiz Augusto von Dentz 	return 0;
231ccf74f23SLuiz Augusto von Dentz }
232ccf74f23SLuiz Augusto von Dentz 
233ccf74f23SLuiz Augusto von Dentz static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
234ccf74f23SLuiz Augusto von Dentz 			struct sock *parent)
235ccf74f23SLuiz Augusto von Dentz {
236ccf74f23SLuiz Augusto von Dentz 	int err;
237ccf74f23SLuiz Augusto von Dentz 
238ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
239ccf74f23SLuiz Augusto von Dentz 	err = __iso_chan_add(conn, sk, parent);
240ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
241ccf74f23SLuiz Augusto von Dentz 
242ccf74f23SLuiz Augusto von Dentz 	return err;
243ccf74f23SLuiz Augusto von Dentz }
244ccf74f23SLuiz Augusto von Dentz 
245b36a234dSPauli Virtanen static inline u8 le_addr_type(u8 bdaddr_type)
246b36a234dSPauli Virtanen {
247b36a234dSPauli Virtanen 	if (bdaddr_type == BDADDR_LE_PUBLIC)
248b36a234dSPauli Virtanen 		return ADDR_LE_DEV_PUBLIC;
249b36a234dSPauli Virtanen 	else
250b36a234dSPauli Virtanen 		return ADDR_LE_DEV_RANDOM;
251b36a234dSPauli Virtanen }
252b36a234dSPauli Virtanen 
253f764a6c2SLuiz Augusto von Dentz static int iso_connect_bis(struct sock *sk)
254f764a6c2SLuiz Augusto von Dentz {
255f764a6c2SLuiz Augusto von Dentz 	struct iso_conn *conn;
256f764a6c2SLuiz Augusto von Dentz 	struct hci_conn *hcon;
257f764a6c2SLuiz Augusto von Dentz 	struct hci_dev  *hdev;
258f764a6c2SLuiz Augusto von Dentz 	int err;
259f764a6c2SLuiz Augusto von Dentz 
260f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR", &iso_pi(sk)->src);
261f764a6c2SLuiz Augusto von Dentz 
262f764a6c2SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
263f764a6c2SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
264f764a6c2SLuiz Augusto von Dentz 	if (!hdev)
265f764a6c2SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
266f764a6c2SLuiz Augusto von Dentz 
267f764a6c2SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
268f764a6c2SLuiz Augusto von Dentz 
269f764a6c2SLuiz Augusto von Dentz 	if (!bis_capable(hdev)) {
270f764a6c2SLuiz Augusto von Dentz 		err = -EOPNOTSUPP;
271241f5193SLuiz Augusto von Dentz 		goto unlock;
272f764a6c2SLuiz Augusto von Dentz 	}
273f764a6c2SLuiz Augusto von Dentz 
2740fe8c8d0SIulia Tanasescu 	/* Fail if user set invalid QoS */
2750fe8c8d0SIulia Tanasescu 	if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
2760fe8c8d0SIulia Tanasescu 		iso_pi(sk)->qos = default_qos;
2770fe8c8d0SIulia Tanasescu 		err = -EINVAL;
2780fe8c8d0SIulia Tanasescu 		goto unlock;
2790fe8c8d0SIulia Tanasescu 	}
2800fe8c8d0SIulia Tanasescu 
281f764a6c2SLuiz Augusto von Dentz 	/* Fail if out PHYs are marked as disabled */
2820fe8c8d0SIulia Tanasescu 	if (!iso_pi(sk)->qos.bcast.out.phy) {
283f764a6c2SLuiz Augusto von Dentz 		err = -EINVAL;
284241f5193SLuiz Augusto von Dentz 		goto unlock;
285f764a6c2SLuiz Augusto von Dentz 	}
286f764a6c2SLuiz Augusto von Dentz 
287d11ab690SPauli Virtanen 	hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst,
288d11ab690SPauli Virtanen 			       le_addr_type(iso_pi(sk)->dst_type),
289f764a6c2SLuiz Augusto von Dentz 			       &iso_pi(sk)->qos, iso_pi(sk)->base_len,
290f764a6c2SLuiz Augusto von Dentz 			       iso_pi(sk)->base);
291f764a6c2SLuiz Augusto von Dentz 	if (IS_ERR(hcon)) {
292f764a6c2SLuiz Augusto von Dentz 		err = PTR_ERR(hcon);
293241f5193SLuiz Augusto von Dentz 		goto unlock;
294f764a6c2SLuiz Augusto von Dentz 	}
295f764a6c2SLuiz Augusto von Dentz 
296f764a6c2SLuiz Augusto von Dentz 	conn = iso_conn_add(hcon);
297f764a6c2SLuiz Augusto von Dentz 	if (!conn) {
298f764a6c2SLuiz Augusto von Dentz 		hci_conn_drop(hcon);
299f764a6c2SLuiz Augusto von Dentz 		err = -ENOMEM;
300241f5193SLuiz Augusto von Dentz 		goto unlock;
301f764a6c2SLuiz Augusto von Dentz 	}
302f764a6c2SLuiz Augusto von Dentz 
303241f5193SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
304241f5193SLuiz Augusto von Dentz 	hci_dev_put(hdev);
305241f5193SLuiz Augusto von Dentz 
3066a5ad251SLuiz Augusto von Dentz 	err = iso_chan_add(conn, sk, NULL);
3076a5ad251SLuiz Augusto von Dentz 	if (err)
3086a5ad251SLuiz Augusto von Dentz 		return err;
3096a5ad251SLuiz Augusto von Dentz 
310241f5193SLuiz Augusto von Dentz 	lock_sock(sk);
311241f5193SLuiz Augusto von Dentz 
312f764a6c2SLuiz Augusto von Dentz 	/* Update source addr of the socket */
313f764a6c2SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &hcon->src);
314f764a6c2SLuiz Augusto von Dentz 
315f764a6c2SLuiz Augusto von Dentz 	if (hcon->state == BT_CONNECTED) {
316f764a6c2SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
317f764a6c2SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECTED;
318f764a6c2SLuiz Augusto von Dentz 	} else {
319f764a6c2SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
320f764a6c2SLuiz Augusto von Dentz 		iso_sock_set_timer(sk, sk->sk_sndtimeo);
321f764a6c2SLuiz Augusto von Dentz 	}
322f764a6c2SLuiz Augusto von Dentz 
323241f5193SLuiz Augusto von Dentz 	release_sock(sk);
324241f5193SLuiz Augusto von Dentz 	return err;
325241f5193SLuiz Augusto von Dentz 
326241f5193SLuiz Augusto von Dentz unlock:
327f764a6c2SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
328f764a6c2SLuiz Augusto von Dentz 	hci_dev_put(hdev);
329f764a6c2SLuiz Augusto von Dentz 	return err;
330f764a6c2SLuiz Augusto von Dentz }
331f764a6c2SLuiz Augusto von Dentz 
332f764a6c2SLuiz Augusto von Dentz static int iso_connect_cis(struct sock *sk)
333ccf74f23SLuiz Augusto von Dentz {
334ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn;
335ccf74f23SLuiz Augusto von Dentz 	struct hci_conn *hcon;
336ccf74f23SLuiz Augusto von Dentz 	struct hci_dev  *hdev;
337ccf74f23SLuiz Augusto von Dentz 	int err;
338ccf74f23SLuiz Augusto von Dentz 
339ccf74f23SLuiz Augusto von Dentz 	BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
340ccf74f23SLuiz Augusto von Dentz 
341ccf74f23SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
342ccf74f23SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
343ccf74f23SLuiz Augusto von Dentz 	if (!hdev)
344ccf74f23SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
345ccf74f23SLuiz Augusto von Dentz 
346ccf74f23SLuiz Augusto von Dentz 	hci_dev_lock(hdev);
347ccf74f23SLuiz Augusto von Dentz 
348ccf74f23SLuiz Augusto von Dentz 	if (!cis_central_capable(hdev)) {
349ccf74f23SLuiz Augusto von Dentz 		err = -EOPNOTSUPP;
350241f5193SLuiz Augusto von Dentz 		goto unlock;
351ccf74f23SLuiz Augusto von Dentz 	}
352ccf74f23SLuiz Augusto von Dentz 
3530fe8c8d0SIulia Tanasescu 	/* Fail if user set invalid QoS */
3540fe8c8d0SIulia Tanasescu 	if (iso_pi(sk)->qos_user_set && !check_ucast_qos(&iso_pi(sk)->qos)) {
3550fe8c8d0SIulia Tanasescu 		iso_pi(sk)->qos = default_qos;
3560fe8c8d0SIulia Tanasescu 		err = -EINVAL;
3570fe8c8d0SIulia Tanasescu 		goto unlock;
3580fe8c8d0SIulia Tanasescu 	}
3590fe8c8d0SIulia Tanasescu 
360ccf74f23SLuiz Augusto von Dentz 	/* Fail if either PHYs are marked as disabled */
3610fe8c8d0SIulia Tanasescu 	if (!iso_pi(sk)->qos.ucast.in.phy && !iso_pi(sk)->qos.ucast.out.phy) {
362ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
363241f5193SLuiz Augusto von Dentz 		goto unlock;
364ccf74f23SLuiz Augusto von Dentz 	}
365ccf74f23SLuiz Augusto von Dentz 
366ccf74f23SLuiz Augusto von Dentz 	/* Just bind if DEFER_SETUP has been set */
367ccf74f23SLuiz Augusto von Dentz 	if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
368ccf74f23SLuiz Augusto von Dentz 		hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
369b36a234dSPauli Virtanen 				    le_addr_type(iso_pi(sk)->dst_type),
370b36a234dSPauli Virtanen 				    &iso_pi(sk)->qos);
371ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(hcon)) {
372ccf74f23SLuiz Augusto von Dentz 			err = PTR_ERR(hcon);
373241f5193SLuiz Augusto von Dentz 			goto unlock;
374ccf74f23SLuiz Augusto von Dentz 		}
375ccf74f23SLuiz Augusto von Dentz 	} else {
376ccf74f23SLuiz Augusto von Dentz 		hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
377b36a234dSPauli Virtanen 				       le_addr_type(iso_pi(sk)->dst_type),
378b36a234dSPauli Virtanen 				       &iso_pi(sk)->qos);
379ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(hcon)) {
380ccf74f23SLuiz Augusto von Dentz 			err = PTR_ERR(hcon);
381241f5193SLuiz Augusto von Dentz 			goto unlock;
382ccf74f23SLuiz Augusto von Dentz 		}
383ccf74f23SLuiz Augusto von Dentz 	}
384ccf74f23SLuiz Augusto von Dentz 
385ccf74f23SLuiz Augusto von Dentz 	conn = iso_conn_add(hcon);
386ccf74f23SLuiz Augusto von Dentz 	if (!conn) {
387ccf74f23SLuiz Augusto von Dentz 		hci_conn_drop(hcon);
388ccf74f23SLuiz Augusto von Dentz 		err = -ENOMEM;
389241f5193SLuiz Augusto von Dentz 		goto unlock;
390ccf74f23SLuiz Augusto von Dentz 	}
391ccf74f23SLuiz Augusto von Dentz 
392241f5193SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
393241f5193SLuiz Augusto von Dentz 	hci_dev_put(hdev);
394241f5193SLuiz Augusto von Dentz 
3956a5ad251SLuiz Augusto von Dentz 	err = iso_chan_add(conn, sk, NULL);
3966a5ad251SLuiz Augusto von Dentz 	if (err)
3976a5ad251SLuiz Augusto von Dentz 		return err;
3986a5ad251SLuiz Augusto von Dentz 
399241f5193SLuiz Augusto von Dentz 	lock_sock(sk);
400241f5193SLuiz Augusto von Dentz 
401ccf74f23SLuiz Augusto von Dentz 	/* Update source addr of the socket */
402ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &hcon->src);
403ccf74f23SLuiz Augusto von Dentz 
404ccf74f23SLuiz Augusto von Dentz 	if (hcon->state == BT_CONNECTED) {
405ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
406ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECTED;
407ccf74f23SLuiz Augusto von Dentz 	} else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
408ccf74f23SLuiz Augusto von Dentz 		iso_sock_clear_timer(sk);
409ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
410ccf74f23SLuiz Augusto von Dentz 	} else {
411ccf74f23SLuiz Augusto von Dentz 		sk->sk_state = BT_CONNECT;
412ccf74f23SLuiz Augusto von Dentz 		iso_sock_set_timer(sk, sk->sk_sndtimeo);
413ccf74f23SLuiz Augusto von Dentz 	}
414ccf74f23SLuiz Augusto von Dentz 
415241f5193SLuiz Augusto von Dentz 	release_sock(sk);
416241f5193SLuiz Augusto von Dentz 	return err;
417241f5193SLuiz Augusto von Dentz 
418241f5193SLuiz Augusto von Dentz unlock:
419ccf74f23SLuiz Augusto von Dentz 	hci_dev_unlock(hdev);
420ccf74f23SLuiz Augusto von Dentz 	hci_dev_put(hdev);
421ccf74f23SLuiz Augusto von Dentz 	return err;
422ccf74f23SLuiz Augusto von Dentz }
423ccf74f23SLuiz Augusto von Dentz 
4241d1ab5d3SLuiz Augusto von Dentz static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
4251d1ab5d3SLuiz Augusto von Dentz {
4261d1ab5d3SLuiz Augusto von Dentz 	if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
4271d1ab5d3SLuiz Augusto von Dentz 		return &iso_pi(sk)->conn->hcon->iso_qos;
4281d1ab5d3SLuiz Augusto von Dentz 
4291d1ab5d3SLuiz Augusto von Dentz 	return &iso_pi(sk)->qos;
4301d1ab5d3SLuiz Augusto von Dentz }
4311d1ab5d3SLuiz Augusto von Dentz 
432ccf74f23SLuiz Augusto von Dentz static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
433ccf74f23SLuiz Augusto von Dentz {
434ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = iso_pi(sk)->conn;
4351d1ab5d3SLuiz Augusto von Dentz 	struct bt_iso_qos *qos = iso_sock_get_qos(sk);
436ccf74f23SLuiz Augusto von Dentz 	struct hci_iso_data_hdr *hdr;
437ccf74f23SLuiz Augusto von Dentz 	int len = 0;
438ccf74f23SLuiz Augusto von Dentz 
439ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p len %d", sk, skb->len);
440ccf74f23SLuiz Augusto von Dentz 
4410fe8c8d0SIulia Tanasescu 	if (skb->len > qos->ucast.out.sdu)
442ccf74f23SLuiz Augusto von Dentz 		return -EMSGSIZE;
443ccf74f23SLuiz Augusto von Dentz 
444ccf74f23SLuiz Augusto von Dentz 	len = skb->len;
445ccf74f23SLuiz Augusto von Dentz 
446ccf74f23SLuiz Augusto von Dentz 	/* Push ISO data header */
447ccf74f23SLuiz Augusto von Dentz 	hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
448ccf74f23SLuiz Augusto von Dentz 	hdr->sn = cpu_to_le16(conn->tx_sn++);
449ccf74f23SLuiz Augusto von Dentz 	hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
450ccf74f23SLuiz Augusto von Dentz 						      HCI_ISO_STATUS_VALID));
451ccf74f23SLuiz Augusto von Dentz 
452ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state == BT_CONNECTED)
453ccf74f23SLuiz Augusto von Dentz 		hci_send_iso(conn->hcon, skb);
454ccf74f23SLuiz Augusto von Dentz 	else
455ccf74f23SLuiz Augusto von Dentz 		len = -ENOTCONN;
456ccf74f23SLuiz Augusto von Dentz 
457ccf74f23SLuiz Augusto von Dentz 	return len;
458ccf74f23SLuiz Augusto von Dentz }
459ccf74f23SLuiz Augusto von Dentz 
460ccf74f23SLuiz Augusto von Dentz static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
461ccf74f23SLuiz Augusto von Dentz {
462ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
463ccf74f23SLuiz Augusto von Dentz 
464ccf74f23SLuiz Augusto von Dentz 	iso_conn_lock(conn);
465ccf74f23SLuiz Augusto von Dentz 	sk = conn->sk;
466ccf74f23SLuiz Augusto von Dentz 	iso_conn_unlock(conn);
467ccf74f23SLuiz Augusto von Dentz 
468ccf74f23SLuiz Augusto von Dentz 	if (!sk)
469ccf74f23SLuiz Augusto von Dentz 		goto drop;
470ccf74f23SLuiz Augusto von Dentz 
471ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p len %d", sk, skb->len);
472ccf74f23SLuiz Augusto von Dentz 
473ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_CONNECTED)
474ccf74f23SLuiz Augusto von Dentz 		goto drop;
475ccf74f23SLuiz Augusto von Dentz 
476ccf74f23SLuiz Augusto von Dentz 	if (!sock_queue_rcv_skb(sk, skb))
477ccf74f23SLuiz Augusto von Dentz 		return;
478ccf74f23SLuiz Augusto von Dentz 
479ccf74f23SLuiz Augusto von Dentz drop:
480ccf74f23SLuiz Augusto von Dentz 	kfree_skb(skb);
481ccf74f23SLuiz Augusto von Dentz }
482ccf74f23SLuiz Augusto von Dentz 
483ccf74f23SLuiz Augusto von Dentz /* -------- Socket interface ---------- */
484ccf74f23SLuiz Augusto von Dentz static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *ba)
485ccf74f23SLuiz Augusto von Dentz {
486ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
487ccf74f23SLuiz Augusto von Dentz 
488ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
489ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
490ccf74f23SLuiz Augusto von Dentz 			continue;
491ccf74f23SLuiz Augusto von Dentz 
492ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, ba))
493ccf74f23SLuiz Augusto von Dentz 			return sk;
494ccf74f23SLuiz Augusto von Dentz 	}
495ccf74f23SLuiz Augusto von Dentz 
496ccf74f23SLuiz Augusto von Dentz 	return NULL;
497ccf74f23SLuiz Augusto von Dentz }
498ccf74f23SLuiz Augusto von Dentz 
499f764a6c2SLuiz Augusto von Dentz static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
500f764a6c2SLuiz Augusto von Dentz 						 __u8 sid)
501f764a6c2SLuiz Augusto von Dentz {
502f764a6c2SLuiz Augusto von Dentz 	struct sock *sk;
503f764a6c2SLuiz Augusto von Dentz 
504f764a6c2SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
505f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
506f764a6c2SLuiz Augusto von Dentz 			continue;
507f764a6c2SLuiz Augusto von Dentz 
508f764a6c2SLuiz Augusto von Dentz 		if (bacmp(&iso_pi(sk)->src, ba))
509f764a6c2SLuiz Augusto von Dentz 			continue;
510f764a6c2SLuiz Augusto von Dentz 
511f764a6c2SLuiz Augusto von Dentz 		if (bacmp(&iso_pi(sk)->dst, bc))
512f764a6c2SLuiz Augusto von Dentz 			continue;
513f764a6c2SLuiz Augusto von Dentz 
514f764a6c2SLuiz Augusto von Dentz 		if (iso_pi(sk)->bc_sid == sid)
515f764a6c2SLuiz Augusto von Dentz 			return sk;
516f764a6c2SLuiz Augusto von Dentz 	}
517f764a6c2SLuiz Augusto von Dentz 
518f764a6c2SLuiz Augusto von Dentz 	return NULL;
519f764a6c2SLuiz Augusto von Dentz }
520f764a6c2SLuiz Augusto von Dentz 
521f764a6c2SLuiz Augusto von Dentz typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
522f764a6c2SLuiz Augusto von Dentz 
523f764a6c2SLuiz Augusto von Dentz /* Find socket listening:
524f764a6c2SLuiz Augusto von Dentz  * source bdaddr (Unicast)
525f764a6c2SLuiz Augusto von Dentz  * destination bdaddr (Broadcast only)
526f764a6c2SLuiz Augusto von Dentz  * match func - pass NULL to ignore
527f764a6c2SLuiz Augusto von Dentz  * match func data - pass -1 to ignore
528ccf74f23SLuiz Augusto von Dentz  * Returns closest match.
529ccf74f23SLuiz Augusto von Dentz  */
530f764a6c2SLuiz Augusto von Dentz static struct sock *iso_get_sock_listen(bdaddr_t *src, bdaddr_t *dst,
531f764a6c2SLuiz Augusto von Dentz 					iso_sock_match_t match, void *data)
532ccf74f23SLuiz Augusto von Dentz {
533ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = NULL, *sk1 = NULL;
534ccf74f23SLuiz Augusto von Dentz 
535ccf74f23SLuiz Augusto von Dentz 	read_lock(&iso_sk_list.lock);
536ccf74f23SLuiz Augusto von Dentz 
537ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
538ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN)
539ccf74f23SLuiz Augusto von Dentz 			continue;
540ccf74f23SLuiz Augusto von Dentz 
541f764a6c2SLuiz Augusto von Dentz 		/* Match Broadcast destination */
542f764a6c2SLuiz Augusto von Dentz 		if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
543f764a6c2SLuiz Augusto von Dentz 			continue;
544f764a6c2SLuiz Augusto von Dentz 
545f764a6c2SLuiz Augusto von Dentz 		/* Use Match function if provided */
546f764a6c2SLuiz Augusto von Dentz 		if (match && !match(sk, data))
547f764a6c2SLuiz Augusto von Dentz 			continue;
548f764a6c2SLuiz Augusto von Dentz 
549ccf74f23SLuiz Augusto von Dentz 		/* Exact match. */
550ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, src))
551ccf74f23SLuiz Augusto von Dentz 			break;
552ccf74f23SLuiz Augusto von Dentz 
553ccf74f23SLuiz Augusto von Dentz 		/* Closest match */
554ccf74f23SLuiz Augusto von Dentz 		if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY))
555ccf74f23SLuiz Augusto von Dentz 			sk1 = sk;
556ccf74f23SLuiz Augusto von Dentz 	}
557ccf74f23SLuiz Augusto von Dentz 
558ccf74f23SLuiz Augusto von Dentz 	read_unlock(&iso_sk_list.lock);
559ccf74f23SLuiz Augusto von Dentz 
560ccf74f23SLuiz Augusto von Dentz 	return sk ? sk : sk1;
561ccf74f23SLuiz Augusto von Dentz }
562ccf74f23SLuiz Augusto von Dentz 
563ccf74f23SLuiz Augusto von Dentz static void iso_sock_destruct(struct sock *sk)
564ccf74f23SLuiz Augusto von Dentz {
565ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
566ccf74f23SLuiz Augusto von Dentz 
567ccf74f23SLuiz Augusto von Dentz 	skb_queue_purge(&sk->sk_receive_queue);
568ccf74f23SLuiz Augusto von Dentz 	skb_queue_purge(&sk->sk_write_queue);
569ccf74f23SLuiz Augusto von Dentz }
570ccf74f23SLuiz Augusto von Dentz 
571ccf74f23SLuiz Augusto von Dentz static void iso_sock_cleanup_listen(struct sock *parent)
572ccf74f23SLuiz Augusto von Dentz {
573ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
574ccf74f23SLuiz Augusto von Dentz 
575ccf74f23SLuiz Augusto von Dentz 	BT_DBG("parent %p", parent);
576ccf74f23SLuiz Augusto von Dentz 
577ccf74f23SLuiz Augusto von Dentz 	/* Close not yet accepted channels */
578ccf74f23SLuiz Augusto von Dentz 	while ((sk = bt_accept_dequeue(parent, NULL))) {
579ccf74f23SLuiz Augusto von Dentz 		iso_sock_close(sk);
580ccf74f23SLuiz Augusto von Dentz 		iso_sock_kill(sk);
581ccf74f23SLuiz Augusto von Dentz 	}
582ccf74f23SLuiz Augusto von Dentz 
583ccf74f23SLuiz Augusto von Dentz 	parent->sk_state  = BT_CLOSED;
584ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(parent, SOCK_ZAPPED);
585ccf74f23SLuiz Augusto von Dentz }
586ccf74f23SLuiz Augusto von Dentz 
587ccf74f23SLuiz Augusto von Dentz /* Kill socket (only if zapped and orphan)
588ccf74f23SLuiz Augusto von Dentz  * Must be called on unlocked socket.
589ccf74f23SLuiz Augusto von Dentz  */
590ccf74f23SLuiz Augusto von Dentz static void iso_sock_kill(struct sock *sk)
591ccf74f23SLuiz Augusto von Dentz {
592ccf74f23SLuiz Augusto von Dentz 	if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
593ccf74f23SLuiz Augusto von Dentz 	    sock_flag(sk, SOCK_DEAD))
594ccf74f23SLuiz Augusto von Dentz 		return;
595ccf74f23SLuiz Augusto von Dentz 
596ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p state %d", sk, sk->sk_state);
597ccf74f23SLuiz Augusto von Dentz 
598ccf74f23SLuiz Augusto von Dentz 	/* Kill poor orphan */
599ccf74f23SLuiz Augusto von Dentz 	bt_sock_unlink(&iso_sk_list, sk);
600ccf74f23SLuiz Augusto von Dentz 	sock_set_flag(sk, SOCK_DEAD);
601ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
602ccf74f23SLuiz Augusto von Dentz }
603ccf74f23SLuiz Augusto von Dentz 
604ccf74f23SLuiz Augusto von Dentz static void iso_conn_defer_reject(struct hci_conn *conn)
605ccf74f23SLuiz Augusto von Dentz {
606ccf74f23SLuiz Augusto von Dentz 	struct hci_cp_le_reject_cis cp;
607ccf74f23SLuiz Augusto von Dentz 
608ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
609ccf74f23SLuiz Augusto von Dentz 
610ccf74f23SLuiz Augusto von Dentz 	memset(&cp, 0, sizeof(cp));
611ccf74f23SLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
612ccf74f23SLuiz Augusto von Dentz 	cp.reason = HCI_ERROR_REJ_BAD_ADDR;
613ccf74f23SLuiz Augusto von Dentz 	hci_send_cmd(conn->hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
614ccf74f23SLuiz Augusto von Dentz }
615ccf74f23SLuiz Augusto von Dentz 
616ccf74f23SLuiz Augusto von Dentz static void __iso_sock_close(struct sock *sk)
617ccf74f23SLuiz Augusto von Dentz {
618ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
619ccf74f23SLuiz Augusto von Dentz 
620ccf74f23SLuiz Augusto von Dentz 	switch (sk->sk_state) {
621ccf74f23SLuiz Augusto von Dentz 	case BT_LISTEN:
622ccf74f23SLuiz Augusto von Dentz 		iso_sock_cleanup_listen(sk);
623ccf74f23SLuiz Augusto von Dentz 		break;
624ccf74f23SLuiz Augusto von Dentz 
625ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECTED:
626ccf74f23SLuiz Augusto von Dentz 	case BT_CONFIG:
627ccf74f23SLuiz Augusto von Dentz 		if (iso_pi(sk)->conn->hcon) {
628ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_DISCONN;
629ccf74f23SLuiz Augusto von Dentz 			iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
630ccf74f23SLuiz Augusto von Dentz 			iso_conn_lock(iso_pi(sk)->conn);
631ccf74f23SLuiz Augusto von Dentz 			hci_conn_drop(iso_pi(sk)->conn->hcon);
632ccf74f23SLuiz Augusto von Dentz 			iso_pi(sk)->conn->hcon = NULL;
633ccf74f23SLuiz Augusto von Dentz 			iso_conn_unlock(iso_pi(sk)->conn);
634ccf74f23SLuiz Augusto von Dentz 		} else {
635ccf74f23SLuiz Augusto von Dentz 			iso_chan_del(sk, ECONNRESET);
636ccf74f23SLuiz Augusto von Dentz 		}
637ccf74f23SLuiz Augusto von Dentz 		break;
638ccf74f23SLuiz Augusto von Dentz 
639ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECT2:
640ccf74f23SLuiz Augusto von Dentz 		if (iso_pi(sk)->conn->hcon)
641ccf74f23SLuiz Augusto von Dentz 			iso_conn_defer_reject(iso_pi(sk)->conn->hcon);
642ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
643ccf74f23SLuiz Augusto von Dentz 		break;
644ccf74f23SLuiz Augusto von Dentz 	case BT_CONNECT:
645ccf74f23SLuiz Augusto von Dentz 		/* In case of DEFER_SETUP the hcon would be bound to CIG which
646ccf74f23SLuiz Augusto von Dentz 		 * needs to be removed so just call hci_conn_del so the cleanup
647ccf74f23SLuiz Augusto von Dentz 		 * callback do what is needed.
648ccf74f23SLuiz Augusto von Dentz 		 */
649ccf74f23SLuiz Augusto von Dentz 		if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
650ccf74f23SLuiz Augusto von Dentz 		    iso_pi(sk)->conn->hcon) {
651ccf74f23SLuiz Augusto von Dentz 			hci_conn_del(iso_pi(sk)->conn->hcon);
652ccf74f23SLuiz Augusto von Dentz 			iso_pi(sk)->conn->hcon = NULL;
653ccf74f23SLuiz Augusto von Dentz 		}
654ccf74f23SLuiz Augusto von Dentz 
655ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
656ccf74f23SLuiz Augusto von Dentz 		break;
657ccf74f23SLuiz Augusto von Dentz 	case BT_DISCONN:
658ccf74f23SLuiz Augusto von Dentz 		iso_chan_del(sk, ECONNRESET);
659ccf74f23SLuiz Augusto von Dentz 		break;
660ccf74f23SLuiz Augusto von Dentz 
661ccf74f23SLuiz Augusto von Dentz 	default:
662ccf74f23SLuiz Augusto von Dentz 		sock_set_flag(sk, SOCK_ZAPPED);
663ccf74f23SLuiz Augusto von Dentz 		break;
664ccf74f23SLuiz Augusto von Dentz 	}
665ccf74f23SLuiz Augusto von Dentz }
666ccf74f23SLuiz Augusto von Dentz 
667ccf74f23SLuiz Augusto von Dentz /* Must be called on unlocked socket. */
668ccf74f23SLuiz Augusto von Dentz static void iso_sock_close(struct sock *sk)
669ccf74f23SLuiz Augusto von Dentz {
670ccf74f23SLuiz Augusto von Dentz 	iso_sock_clear_timer(sk);
671ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
672ccf74f23SLuiz Augusto von Dentz 	__iso_sock_close(sk);
673ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
674ccf74f23SLuiz Augusto von Dentz 	iso_sock_kill(sk);
675ccf74f23SLuiz Augusto von Dentz }
676ccf74f23SLuiz Augusto von Dentz 
677ccf74f23SLuiz Augusto von Dentz static void iso_sock_init(struct sock *sk, struct sock *parent)
678ccf74f23SLuiz Augusto von Dentz {
679ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
680ccf74f23SLuiz Augusto von Dentz 
681ccf74f23SLuiz Augusto von Dentz 	if (parent) {
682ccf74f23SLuiz Augusto von Dentz 		sk->sk_type = parent->sk_type;
683ccf74f23SLuiz Augusto von Dentz 		bt_sk(sk)->flags = bt_sk(parent)->flags;
684ccf74f23SLuiz Augusto von Dentz 		security_sk_clone(parent, sk);
685ccf74f23SLuiz Augusto von Dentz 	}
686ccf74f23SLuiz Augusto von Dentz }
687ccf74f23SLuiz Augusto von Dentz 
688ccf74f23SLuiz Augusto von Dentz static struct proto iso_proto = {
689ccf74f23SLuiz Augusto von Dentz 	.name		= "ISO",
690ccf74f23SLuiz Augusto von Dentz 	.owner		= THIS_MODULE,
691ccf74f23SLuiz Augusto von Dentz 	.obj_size	= sizeof(struct iso_pinfo)
692ccf74f23SLuiz Augusto von Dentz };
693ccf74f23SLuiz Augusto von Dentz 
694ccf74f23SLuiz Augusto von Dentz #define DEFAULT_IO_QOS \
695ccf74f23SLuiz Augusto von Dentz { \
696ccf74f23SLuiz Augusto von Dentz 	.interval	= 10000u, \
697ccf74f23SLuiz Augusto von Dentz 	.latency	= 10u, \
698ccf74f23SLuiz Augusto von Dentz 	.sdu		= 40u, \
699ccf74f23SLuiz Augusto von Dentz 	.phy		= BT_ISO_PHY_2M, \
700ccf74f23SLuiz Augusto von Dentz 	.rtn		= 2u, \
701ccf74f23SLuiz Augusto von Dentz }
702ccf74f23SLuiz Augusto von Dentz 
703ccf74f23SLuiz Augusto von Dentz static struct bt_iso_qos default_qos = {
7040fe8c8d0SIulia Tanasescu 	.bcast = {
7050fe8c8d0SIulia Tanasescu 		.big			= BT_ISO_QOS_BIG_UNSET,
7060fe8c8d0SIulia Tanasescu 		.bis			= BT_ISO_QOS_BIS_UNSET,
7070fe8c8d0SIulia Tanasescu 		.sync_interval		= 0x00,
708ccf74f23SLuiz Augusto von Dentz 		.packing		= 0x00,
709ccf74f23SLuiz Augusto von Dentz 		.framing		= 0x00,
710ccf74f23SLuiz Augusto von Dentz 		.in			= DEFAULT_IO_QOS,
711ccf74f23SLuiz Augusto von Dentz 		.out			= DEFAULT_IO_QOS,
7120fe8c8d0SIulia Tanasescu 		.encryption		= 0x00,
7130fe8c8d0SIulia Tanasescu 		.bcode			= {0x00},
7140fe8c8d0SIulia Tanasescu 		.options		= 0x00,
7150fe8c8d0SIulia Tanasescu 		.skip			= 0x0000,
7160fe8c8d0SIulia Tanasescu 		.sync_timeout		= 0x4000,
7170fe8c8d0SIulia Tanasescu 		.sync_cte_type		= 0x00,
7180fe8c8d0SIulia Tanasescu 		.mse			= 0x00,
7190fe8c8d0SIulia Tanasescu 		.timeout		= 0x4000,
7200fe8c8d0SIulia Tanasescu 	},
721ccf74f23SLuiz Augusto von Dentz };
722ccf74f23SLuiz Augusto von Dentz 
723ccf74f23SLuiz Augusto von Dentz static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
724ccf74f23SLuiz Augusto von Dentz 				   int proto, gfp_t prio, int kern)
725ccf74f23SLuiz Augusto von Dentz {
726ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
727ccf74f23SLuiz Augusto von Dentz 
728ccf74f23SLuiz Augusto von Dentz 	sk = sk_alloc(net, PF_BLUETOOTH, prio, &iso_proto, kern);
729ccf74f23SLuiz Augusto von Dentz 	if (!sk)
730ccf74f23SLuiz Augusto von Dentz 		return NULL;
731ccf74f23SLuiz Augusto von Dentz 
732ccf74f23SLuiz Augusto von Dentz 	sock_init_data(sock, sk);
733ccf74f23SLuiz Augusto von Dentz 	INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
734ccf74f23SLuiz Augusto von Dentz 
735ccf74f23SLuiz Augusto von Dentz 	sk->sk_destruct = iso_sock_destruct;
736ccf74f23SLuiz Augusto von Dentz 	sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
737ccf74f23SLuiz Augusto von Dentz 
738ccf74f23SLuiz Augusto von Dentz 	sock_reset_flag(sk, SOCK_ZAPPED);
739ccf74f23SLuiz Augusto von Dentz 
740ccf74f23SLuiz Augusto von Dentz 	sk->sk_protocol = proto;
741ccf74f23SLuiz Augusto von Dentz 	sk->sk_state    = BT_OPEN;
742ccf74f23SLuiz Augusto von Dentz 
743ccf74f23SLuiz Augusto von Dentz 	/* Set address type as public as default src address is BDADDR_ANY */
744ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
745ccf74f23SLuiz Augusto von Dentz 
746ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->qos = default_qos;
747ccf74f23SLuiz Augusto von Dentz 
748ccf74f23SLuiz Augusto von Dentz 	bt_sock_link(&iso_sk_list, sk);
749ccf74f23SLuiz Augusto von Dentz 	return sk;
750ccf74f23SLuiz Augusto von Dentz }
751ccf74f23SLuiz Augusto von Dentz 
752ccf74f23SLuiz Augusto von Dentz static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
753ccf74f23SLuiz Augusto von Dentz 			   int kern)
754ccf74f23SLuiz Augusto von Dentz {
755ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
756ccf74f23SLuiz Augusto von Dentz 
757ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p", sock);
758ccf74f23SLuiz Augusto von Dentz 
759ccf74f23SLuiz Augusto von Dentz 	sock->state = SS_UNCONNECTED;
760ccf74f23SLuiz Augusto von Dentz 
761ccf74f23SLuiz Augusto von Dentz 	if (sock->type != SOCK_SEQPACKET)
762ccf74f23SLuiz Augusto von Dentz 		return -ESOCKTNOSUPPORT;
763ccf74f23SLuiz Augusto von Dentz 
764ccf74f23SLuiz Augusto von Dentz 	sock->ops = &iso_sock_ops;
765ccf74f23SLuiz Augusto von Dentz 
766ccf74f23SLuiz Augusto von Dentz 	sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
767ccf74f23SLuiz Augusto von Dentz 	if (!sk)
768ccf74f23SLuiz Augusto von Dentz 		return -ENOMEM;
769ccf74f23SLuiz Augusto von Dentz 
770ccf74f23SLuiz Augusto von Dentz 	iso_sock_init(sk, NULL);
771ccf74f23SLuiz Augusto von Dentz 	return 0;
772ccf74f23SLuiz Augusto von Dentz }
773ccf74f23SLuiz Augusto von Dentz 
774f764a6c2SLuiz Augusto von Dentz static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
775f764a6c2SLuiz Augusto von Dentz 			    int addr_len)
776f764a6c2SLuiz Augusto von Dentz {
777f764a6c2SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
778f764a6c2SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
779f764a6c2SLuiz Augusto von Dentz 	int i;
780f764a6c2SLuiz Augusto von Dentz 
781f764a6c2SLuiz Augusto von Dentz 	BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
782f764a6c2SLuiz Augusto von Dentz 	       sa->iso_bc->bc_num_bis);
783f764a6c2SLuiz Augusto von Dentz 
784f764a6c2SLuiz Augusto von Dentz 	if (addr_len > sizeof(*sa) + sizeof(*sa->iso_bc) ||
785f764a6c2SLuiz Augusto von Dentz 	    sa->iso_bc->bc_num_bis < 0x01 || sa->iso_bc->bc_num_bis > 0x1f)
786f764a6c2SLuiz Augusto von Dentz 		return -EINVAL;
787f764a6c2SLuiz Augusto von Dentz 
788f764a6c2SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
789f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
790f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->sync_handle = -1;
791f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
792f764a6c2SLuiz Augusto von Dentz 	iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
793f764a6c2SLuiz Augusto von Dentz 
794f764a6c2SLuiz Augusto von Dentz 	for (i = 0; i < iso_pi(sk)->bc_num_bis; i++) {
795f764a6c2SLuiz Augusto von Dentz 		if (sa->iso_bc->bc_bis[i] < 0x01 ||
796f764a6c2SLuiz Augusto von Dentz 		    sa->iso_bc->bc_bis[i] > 0x1f)
797f764a6c2SLuiz Augusto von Dentz 			return -EINVAL;
798f764a6c2SLuiz Augusto von Dentz 
799f764a6c2SLuiz Augusto von Dentz 		memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
800f764a6c2SLuiz Augusto von Dentz 		       iso_pi(sk)->bc_num_bis);
801f764a6c2SLuiz Augusto von Dentz 	}
802f764a6c2SLuiz Augusto von Dentz 
803f764a6c2SLuiz Augusto von Dentz 	return 0;
804f764a6c2SLuiz Augusto von Dentz }
805f764a6c2SLuiz Augusto von Dentz 
806ccf74f23SLuiz Augusto von Dentz static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
807ccf74f23SLuiz Augusto von Dentz 			 int addr_len)
808ccf74f23SLuiz Augusto von Dentz {
809ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
810ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
811ccf74f23SLuiz Augusto von Dentz 	int err = 0;
812ccf74f23SLuiz Augusto von Dentz 
813ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
814ccf74f23SLuiz Augusto von Dentz 
815ccf74f23SLuiz Augusto von Dentz 	if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
816ccf74f23SLuiz Augusto von Dentz 	    addr->sa_family != AF_BLUETOOTH)
817ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
818ccf74f23SLuiz Augusto von Dentz 
819ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
820ccf74f23SLuiz Augusto von Dentz 
821ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_OPEN) {
822ccf74f23SLuiz Augusto von Dentz 		err = -EBADFD;
823ccf74f23SLuiz Augusto von Dentz 		goto done;
824ccf74f23SLuiz Augusto von Dentz 	}
825ccf74f23SLuiz Augusto von Dentz 
826ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET) {
827ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
828ccf74f23SLuiz Augusto von Dentz 		goto done;
829ccf74f23SLuiz Augusto von Dentz 	}
830ccf74f23SLuiz Augusto von Dentz 
831ccf74f23SLuiz Augusto von Dentz 	/* Check if the address type is of LE type */
832ccf74f23SLuiz Augusto von Dentz 	if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
833ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
834ccf74f23SLuiz Augusto von Dentz 		goto done;
835ccf74f23SLuiz Augusto von Dentz 	}
836ccf74f23SLuiz Augusto von Dentz 
837ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
838ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->src_type = sa->iso_bdaddr_type;
839ccf74f23SLuiz Augusto von Dentz 
840f764a6c2SLuiz Augusto von Dentz 	/* Check for Broadcast address */
841f764a6c2SLuiz Augusto von Dentz 	if (addr_len > sizeof(*sa)) {
842f764a6c2SLuiz Augusto von Dentz 		err = iso_sock_bind_bc(sock, addr, addr_len);
843f764a6c2SLuiz Augusto von Dentz 		if (err)
844f764a6c2SLuiz Augusto von Dentz 			goto done;
845f764a6c2SLuiz Augusto von Dentz 	}
846f764a6c2SLuiz Augusto von Dentz 
847ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_BOUND;
848ccf74f23SLuiz Augusto von Dentz 
849ccf74f23SLuiz Augusto von Dentz done:
850ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
851ccf74f23SLuiz Augusto von Dentz 	return err;
852ccf74f23SLuiz Augusto von Dentz }
853ccf74f23SLuiz Augusto von Dentz 
854ccf74f23SLuiz Augusto von Dentz static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
855ccf74f23SLuiz Augusto von Dentz 			    int alen, int flags)
856ccf74f23SLuiz Augusto von Dentz {
857ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
858ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
859ccf74f23SLuiz Augusto von Dentz 	int err;
860ccf74f23SLuiz Augusto von Dentz 
861ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
862ccf74f23SLuiz Augusto von Dentz 
863ccf74f23SLuiz Augusto von Dentz 	if (alen < sizeof(struct sockaddr_iso) ||
864ccf74f23SLuiz Augusto von Dentz 	    addr->sa_family != AF_BLUETOOTH)
865ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
866ccf74f23SLuiz Augusto von Dentz 
867ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
868ccf74f23SLuiz Augusto von Dentz 		return -EBADFD;
869ccf74f23SLuiz Augusto von Dentz 
870ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET)
871ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
872ccf74f23SLuiz Augusto von Dentz 
873ccf74f23SLuiz Augusto von Dentz 	/* Check if the address type is of LE type */
874ccf74f23SLuiz Augusto von Dentz 	if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
875ccf74f23SLuiz Augusto von Dentz 		return -EINVAL;
876ccf74f23SLuiz Augusto von Dentz 
877ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
878ccf74f23SLuiz Augusto von Dentz 
879ccf74f23SLuiz Augusto von Dentz 	bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
880ccf74f23SLuiz Augusto von Dentz 	iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
881ccf74f23SLuiz Augusto von Dentz 
882241f5193SLuiz Augusto von Dentz 	release_sock(sk);
883241f5193SLuiz Augusto von Dentz 
884f764a6c2SLuiz Augusto von Dentz 	if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
885f764a6c2SLuiz Augusto von Dentz 		err = iso_connect_cis(sk);
886f764a6c2SLuiz Augusto von Dentz 	else
887f764a6c2SLuiz Augusto von Dentz 		err = iso_connect_bis(sk);
888f764a6c2SLuiz Augusto von Dentz 
889ccf74f23SLuiz Augusto von Dentz 	if (err)
890241f5193SLuiz Augusto von Dentz 		return err;
891241f5193SLuiz Augusto von Dentz 
892241f5193SLuiz Augusto von Dentz 	lock_sock(sk);
893ccf74f23SLuiz Augusto von Dentz 
894ccf74f23SLuiz Augusto von Dentz 	if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
895ccf74f23SLuiz Augusto von Dentz 		err = bt_sock_wait_state(sk, BT_CONNECTED,
896ccf74f23SLuiz Augusto von Dentz 					 sock_sndtimeo(sk, flags & O_NONBLOCK));
897ccf74f23SLuiz Augusto von Dentz 	}
898ccf74f23SLuiz Augusto von Dentz 
899ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
900ccf74f23SLuiz Augusto von Dentz 	return err;
901ccf74f23SLuiz Augusto von Dentz }
902ccf74f23SLuiz Augusto von Dentz 
903f764a6c2SLuiz Augusto von Dentz static int iso_listen_bis(struct sock *sk)
904f764a6c2SLuiz Augusto von Dentz {
905f764a6c2SLuiz Augusto von Dentz 	struct hci_dev *hdev;
906f764a6c2SLuiz Augusto von Dentz 	int err = 0;
907f764a6c2SLuiz Augusto von Dentz 
908f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
909f764a6c2SLuiz Augusto von Dentz 	       &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
910f764a6c2SLuiz Augusto von Dentz 
911f764a6c2SLuiz Augusto von Dentz 	write_lock(&iso_sk_list.lock);
912f764a6c2SLuiz Augusto von Dentz 
913f764a6c2SLuiz Augusto von Dentz 	if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
914f764a6c2SLuiz Augusto von Dentz 					 iso_pi(sk)->bc_sid))
915f764a6c2SLuiz Augusto von Dentz 		err = -EADDRINUSE;
916f764a6c2SLuiz Augusto von Dentz 
917f764a6c2SLuiz Augusto von Dentz 	write_unlock(&iso_sk_list.lock);
918f764a6c2SLuiz Augusto von Dentz 
919f764a6c2SLuiz Augusto von Dentz 	if (err)
920f764a6c2SLuiz Augusto von Dentz 		return err;
921f764a6c2SLuiz Augusto von Dentz 
922f764a6c2SLuiz Augusto von Dentz 	hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
923f764a6c2SLuiz Augusto von Dentz 			     iso_pi(sk)->src_type);
924f764a6c2SLuiz Augusto von Dentz 	if (!hdev)
925f764a6c2SLuiz Augusto von Dentz 		return -EHOSTUNREACH;
926f764a6c2SLuiz Augusto von Dentz 
9270fe8c8d0SIulia Tanasescu 	/* Fail if user set invalid QoS */
9280fe8c8d0SIulia Tanasescu 	if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
9290fe8c8d0SIulia Tanasescu 		iso_pi(sk)->qos = default_qos;
9300fe8c8d0SIulia Tanasescu 		return -EINVAL;
9310fe8c8d0SIulia Tanasescu 	}
9320fe8c8d0SIulia Tanasescu 
933d11ab690SPauli Virtanen 	err = hci_pa_create_sync(hdev, &iso_pi(sk)->dst,
934d11ab690SPauli Virtanen 				 le_addr_type(iso_pi(sk)->dst_type),
9350fe8c8d0SIulia Tanasescu 				 iso_pi(sk)->bc_sid, &iso_pi(sk)->qos);
936f764a6c2SLuiz Augusto von Dentz 
9377e7df2c1SWang ShaoBo 	hci_dev_put(hdev);
938f764a6c2SLuiz Augusto von Dentz 
939f764a6c2SLuiz Augusto von Dentz 	return err;
940f764a6c2SLuiz Augusto von Dentz }
941f764a6c2SLuiz Augusto von Dentz 
942f764a6c2SLuiz Augusto von Dentz static int iso_listen_cis(struct sock *sk)
943f764a6c2SLuiz Augusto von Dentz {
944f764a6c2SLuiz Augusto von Dentz 	int err = 0;
945f764a6c2SLuiz Augusto von Dentz 
946f764a6c2SLuiz Augusto von Dentz 	BT_DBG("%pMR", &iso_pi(sk)->src);
947f764a6c2SLuiz Augusto von Dentz 
948f764a6c2SLuiz Augusto von Dentz 	write_lock(&iso_sk_list.lock);
949f764a6c2SLuiz Augusto von Dentz 
950f764a6c2SLuiz Augusto von Dentz 	if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src))
951f764a6c2SLuiz Augusto von Dentz 		err = -EADDRINUSE;
952f764a6c2SLuiz Augusto von Dentz 
953f764a6c2SLuiz Augusto von Dentz 	write_unlock(&iso_sk_list.lock);
954f764a6c2SLuiz Augusto von Dentz 
955f764a6c2SLuiz Augusto von Dentz 	return err;
956f764a6c2SLuiz Augusto von Dentz }
957f764a6c2SLuiz Augusto von Dentz 
958ccf74f23SLuiz Augusto von Dentz static int iso_sock_listen(struct socket *sock, int backlog)
959ccf74f23SLuiz Augusto von Dentz {
960ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
961ccf74f23SLuiz Augusto von Dentz 	int err = 0;
962ccf74f23SLuiz Augusto von Dentz 
963ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p backlog %d", sk, backlog);
964ccf74f23SLuiz Augusto von Dentz 
965ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
966ccf74f23SLuiz Augusto von Dentz 
967ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_BOUND) {
968ccf74f23SLuiz Augusto von Dentz 		err = -EBADFD;
969ccf74f23SLuiz Augusto von Dentz 		goto done;
970ccf74f23SLuiz Augusto von Dentz 	}
971ccf74f23SLuiz Augusto von Dentz 
972ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_type != SOCK_SEQPACKET) {
973ccf74f23SLuiz Augusto von Dentz 		err = -EINVAL;
974ccf74f23SLuiz Augusto von Dentz 		goto done;
975ccf74f23SLuiz Augusto von Dentz 	}
976ccf74f23SLuiz Augusto von Dentz 
977f764a6c2SLuiz Augusto von Dentz 	if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
978f764a6c2SLuiz Augusto von Dentz 		err = iso_listen_cis(sk);
979f764a6c2SLuiz Augusto von Dentz 	else
980f764a6c2SLuiz Augusto von Dentz 		err = iso_listen_bis(sk);
981ccf74f23SLuiz Augusto von Dentz 
982f764a6c2SLuiz Augusto von Dentz 	if (err)
983f764a6c2SLuiz Augusto von Dentz 		goto done;
984ccf74f23SLuiz Augusto von Dentz 
985ccf74f23SLuiz Augusto von Dentz 	sk->sk_max_ack_backlog = backlog;
986ccf74f23SLuiz Augusto von Dentz 	sk->sk_ack_backlog = 0;
987ccf74f23SLuiz Augusto von Dentz 
988ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_LISTEN;
989ccf74f23SLuiz Augusto von Dentz 
990ccf74f23SLuiz Augusto von Dentz done:
991ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
992ccf74f23SLuiz Augusto von Dentz 	return err;
993ccf74f23SLuiz Augusto von Dentz }
994ccf74f23SLuiz Augusto von Dentz 
995ccf74f23SLuiz Augusto von Dentz static int iso_sock_accept(struct socket *sock, struct socket *newsock,
996ccf74f23SLuiz Augusto von Dentz 			   int flags, bool kern)
997ccf74f23SLuiz Augusto von Dentz {
998ccf74f23SLuiz Augusto von Dentz 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
999ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk, *ch;
1000ccf74f23SLuiz Augusto von Dentz 	long timeo;
1001ccf74f23SLuiz Augusto von Dentz 	int err = 0;
1002ccf74f23SLuiz Augusto von Dentz 
1003ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1004ccf74f23SLuiz Augusto von Dentz 
1005ccf74f23SLuiz Augusto von Dentz 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1006ccf74f23SLuiz Augusto von Dentz 
1007ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p timeo %ld", sk, timeo);
1008ccf74f23SLuiz Augusto von Dentz 
1009ccf74f23SLuiz Augusto von Dentz 	/* Wait for an incoming connection. (wake-one). */
1010ccf74f23SLuiz Augusto von Dentz 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
1011ccf74f23SLuiz Augusto von Dentz 	while (1) {
1012ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_LISTEN) {
1013ccf74f23SLuiz Augusto von Dentz 			err = -EBADFD;
1014ccf74f23SLuiz Augusto von Dentz 			break;
1015ccf74f23SLuiz Augusto von Dentz 		}
1016ccf74f23SLuiz Augusto von Dentz 
1017ccf74f23SLuiz Augusto von Dentz 		ch = bt_accept_dequeue(sk, newsock);
1018ccf74f23SLuiz Augusto von Dentz 		if (ch)
1019ccf74f23SLuiz Augusto von Dentz 			break;
1020ccf74f23SLuiz Augusto von Dentz 
1021ccf74f23SLuiz Augusto von Dentz 		if (!timeo) {
1022ccf74f23SLuiz Augusto von Dentz 			err = -EAGAIN;
1023ccf74f23SLuiz Augusto von Dentz 			break;
1024ccf74f23SLuiz Augusto von Dentz 		}
1025ccf74f23SLuiz Augusto von Dentz 
1026ccf74f23SLuiz Augusto von Dentz 		if (signal_pending(current)) {
1027ccf74f23SLuiz Augusto von Dentz 			err = sock_intr_errno(timeo);
1028ccf74f23SLuiz Augusto von Dentz 			break;
1029ccf74f23SLuiz Augusto von Dentz 		}
1030ccf74f23SLuiz Augusto von Dentz 
1031ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
1032ccf74f23SLuiz Augusto von Dentz 
1033ccf74f23SLuiz Augusto von Dentz 		timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1034ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
1035ccf74f23SLuiz Augusto von Dentz 	}
1036ccf74f23SLuiz Augusto von Dentz 	remove_wait_queue(sk_sleep(sk), &wait);
1037ccf74f23SLuiz Augusto von Dentz 
1038ccf74f23SLuiz Augusto von Dentz 	if (err)
1039ccf74f23SLuiz Augusto von Dentz 		goto done;
1040ccf74f23SLuiz Augusto von Dentz 
1041ccf74f23SLuiz Augusto von Dentz 	newsock->state = SS_CONNECTED;
1042ccf74f23SLuiz Augusto von Dentz 
1043ccf74f23SLuiz Augusto von Dentz 	BT_DBG("new socket %p", ch);
1044ccf74f23SLuiz Augusto von Dentz 
1045ccf74f23SLuiz Augusto von Dentz done:
1046ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1047ccf74f23SLuiz Augusto von Dentz 	return err;
1048ccf74f23SLuiz Augusto von Dentz }
1049ccf74f23SLuiz Augusto von Dentz 
1050ccf74f23SLuiz Augusto von Dentz static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1051ccf74f23SLuiz Augusto von Dentz 			    int peer)
1052ccf74f23SLuiz Augusto von Dentz {
1053ccf74f23SLuiz Augusto von Dentz 	struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1054ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1055ccf74f23SLuiz Augusto von Dentz 
1056ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1057ccf74f23SLuiz Augusto von Dentz 
1058ccf74f23SLuiz Augusto von Dentz 	addr->sa_family = AF_BLUETOOTH;
1059ccf74f23SLuiz Augusto von Dentz 
1060ccf74f23SLuiz Augusto von Dentz 	if (peer) {
1061ccf74f23SLuiz Augusto von Dentz 		bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1062ccf74f23SLuiz Augusto von Dentz 		sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1063ccf74f23SLuiz Augusto von Dentz 	} else {
1064ccf74f23SLuiz Augusto von Dentz 		bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1065ccf74f23SLuiz Augusto von Dentz 		sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1066ccf74f23SLuiz Augusto von Dentz 	}
1067ccf74f23SLuiz Augusto von Dentz 
1068ccf74f23SLuiz Augusto von Dentz 	return sizeof(struct sockaddr_iso);
1069ccf74f23SLuiz Augusto von Dentz }
1070ccf74f23SLuiz Augusto von Dentz 
1071ccf74f23SLuiz Augusto von Dentz static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1072ccf74f23SLuiz Augusto von Dentz 			    size_t len)
1073ccf74f23SLuiz Augusto von Dentz {
1074ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1075ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = iso_pi(sk)->conn;
1076ccf74f23SLuiz Augusto von Dentz 	struct sk_buff *skb, **frag;
1077ccf74f23SLuiz Augusto von Dentz 	int err;
1078ccf74f23SLuiz Augusto von Dentz 
1079ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1080ccf74f23SLuiz Augusto von Dentz 
1081ccf74f23SLuiz Augusto von Dentz 	err = sock_error(sk);
1082ccf74f23SLuiz Augusto von Dentz 	if (err)
1083ccf74f23SLuiz Augusto von Dentz 		return err;
1084ccf74f23SLuiz Augusto von Dentz 
1085ccf74f23SLuiz Augusto von Dentz 	if (msg->msg_flags & MSG_OOB)
1086ccf74f23SLuiz Augusto von Dentz 		return -EOPNOTSUPP;
1087ccf74f23SLuiz Augusto von Dentz 
1088ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state != BT_CONNECTED)
1089ccf74f23SLuiz Augusto von Dentz 		return -ENOTCONN;
1090ccf74f23SLuiz Augusto von Dentz 
1091ccf74f23SLuiz Augusto von Dentz 	skb = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1092ccf74f23SLuiz Augusto von Dentz 			     HCI_ISO_DATA_HDR_SIZE, 0);
1093ccf74f23SLuiz Augusto von Dentz 	if (IS_ERR(skb))
1094ccf74f23SLuiz Augusto von Dentz 		return PTR_ERR(skb);
1095ccf74f23SLuiz Augusto von Dentz 
1096ccf74f23SLuiz Augusto von Dentz 	len -= skb->len;
1097ccf74f23SLuiz Augusto von Dentz 
1098ccf74f23SLuiz Augusto von Dentz 	BT_DBG("skb %p len %d", sk, skb->len);
1099ccf74f23SLuiz Augusto von Dentz 
1100ccf74f23SLuiz Augusto von Dentz 	/* Continuation fragments */
1101ccf74f23SLuiz Augusto von Dentz 	frag = &skb_shinfo(skb)->frag_list;
1102ccf74f23SLuiz Augusto von Dentz 	while (len) {
1103ccf74f23SLuiz Augusto von Dentz 		struct sk_buff *tmp;
1104ccf74f23SLuiz Augusto von Dentz 
1105ccf74f23SLuiz Augusto von Dentz 		tmp = bt_skb_sendmsg(sk, msg, len, conn->hcon->hdev->iso_mtu,
1106ccf74f23SLuiz Augusto von Dentz 				     0, 0);
1107ccf74f23SLuiz Augusto von Dentz 		if (IS_ERR(tmp)) {
1108ccf74f23SLuiz Augusto von Dentz 			kfree_skb(skb);
1109ccf74f23SLuiz Augusto von Dentz 			return PTR_ERR(tmp);
1110ccf74f23SLuiz Augusto von Dentz 		}
1111ccf74f23SLuiz Augusto von Dentz 
1112ccf74f23SLuiz Augusto von Dentz 		*frag = tmp;
1113ccf74f23SLuiz Augusto von Dentz 
1114ccf74f23SLuiz Augusto von Dentz 		len  -= tmp->len;
1115ccf74f23SLuiz Augusto von Dentz 
1116ccf74f23SLuiz Augusto von Dentz 		skb->len += tmp->len;
1117ccf74f23SLuiz Augusto von Dentz 		skb->data_len += tmp->len;
1118ccf74f23SLuiz Augusto von Dentz 
1119ccf74f23SLuiz Augusto von Dentz 		BT_DBG("frag %p len %d", *frag, tmp->len);
1120ccf74f23SLuiz Augusto von Dentz 
1121ccf74f23SLuiz Augusto von Dentz 		frag = &(*frag)->next;
1122ccf74f23SLuiz Augusto von Dentz 	}
1123ccf74f23SLuiz Augusto von Dentz 
1124ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1125ccf74f23SLuiz Augusto von Dentz 
1126ccf74f23SLuiz Augusto von Dentz 	if (sk->sk_state == BT_CONNECTED)
1127ccf74f23SLuiz Augusto von Dentz 		err = iso_send_frame(sk, skb);
1128ccf74f23SLuiz Augusto von Dentz 	else
1129ccf74f23SLuiz Augusto von Dentz 		err = -ENOTCONN;
1130ccf74f23SLuiz Augusto von Dentz 
1131ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1132ccf74f23SLuiz Augusto von Dentz 
1133ccf74f23SLuiz Augusto von Dentz 	if (err < 0)
1134ccf74f23SLuiz Augusto von Dentz 		kfree_skb(skb);
1135ccf74f23SLuiz Augusto von Dentz 	return err;
1136ccf74f23SLuiz Augusto von Dentz }
1137ccf74f23SLuiz Augusto von Dentz 
1138ccf74f23SLuiz Augusto von Dentz static void iso_conn_defer_accept(struct hci_conn *conn)
1139ccf74f23SLuiz Augusto von Dentz {
1140ccf74f23SLuiz Augusto von Dentz 	struct hci_cp_le_accept_cis cp;
1141ccf74f23SLuiz Augusto von Dentz 	struct hci_dev *hdev = conn->hdev;
1142ccf74f23SLuiz Augusto von Dentz 
1143ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
1144ccf74f23SLuiz Augusto von Dentz 
1145ccf74f23SLuiz Augusto von Dentz 	conn->state = BT_CONFIG;
1146ccf74f23SLuiz Augusto von Dentz 
1147ccf74f23SLuiz Augusto von Dentz 	cp.handle = cpu_to_le16(conn->handle);
1148ccf74f23SLuiz Augusto von Dentz 
1149ccf74f23SLuiz Augusto von Dentz 	hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1150ccf74f23SLuiz Augusto von Dentz }
1151ccf74f23SLuiz Augusto von Dentz 
1152ccf74f23SLuiz Augusto von Dentz static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1153ccf74f23SLuiz Augusto von Dentz 			    size_t len, int flags)
1154ccf74f23SLuiz Augusto von Dentz {
1155ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1156ccf74f23SLuiz Augusto von Dentz 	struct iso_pinfo *pi = iso_pi(sk);
1157ccf74f23SLuiz Augusto von Dentz 
1158ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1159ccf74f23SLuiz Augusto von Dentz 
1160ccf74f23SLuiz Augusto von Dentz 	if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1161ccf74f23SLuiz Augusto von Dentz 		switch (sk->sk_state) {
1162ccf74f23SLuiz Augusto von Dentz 		case BT_CONNECT2:
1163241f5193SLuiz Augusto von Dentz 			lock_sock(sk);
1164ccf74f23SLuiz Augusto von Dentz 			iso_conn_defer_accept(pi->conn->hcon);
1165ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONFIG;
1166ccf74f23SLuiz Augusto von Dentz 			release_sock(sk);
1167ccf74f23SLuiz Augusto von Dentz 			return 0;
1168ccf74f23SLuiz Augusto von Dentz 		case BT_CONNECT:
1169241f5193SLuiz Augusto von Dentz 			return iso_connect_cis(sk);
1170ccf74f23SLuiz Augusto von Dentz 		}
1171ccf74f23SLuiz Augusto von Dentz 	}
1172ccf74f23SLuiz Augusto von Dentz 
1173ccf74f23SLuiz Augusto von Dentz 	return bt_sock_recvmsg(sock, msg, len, flags);
1174ccf74f23SLuiz Augusto von Dentz }
1175ccf74f23SLuiz Augusto von Dentz 
1176ccf74f23SLuiz Augusto von Dentz static bool check_io_qos(struct bt_iso_io_qos *qos)
1177ccf74f23SLuiz Augusto von Dentz {
1178ccf74f23SLuiz Augusto von Dentz 	/* If no PHY is enable SDU must be 0 */
1179ccf74f23SLuiz Augusto von Dentz 	if (!qos->phy && qos->sdu)
1180ccf74f23SLuiz Augusto von Dentz 		return false;
1181ccf74f23SLuiz Augusto von Dentz 
1182ccf74f23SLuiz Augusto von Dentz 	if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1183ccf74f23SLuiz Augusto von Dentz 		return false;
1184ccf74f23SLuiz Augusto von Dentz 
1185ccf74f23SLuiz Augusto von Dentz 	if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1186ccf74f23SLuiz Augusto von Dentz 		return false;
1187ccf74f23SLuiz Augusto von Dentz 
1188ccf74f23SLuiz Augusto von Dentz 	if (qos->phy > BT_ISO_PHY_ANY)
1189ccf74f23SLuiz Augusto von Dentz 		return false;
1190ccf74f23SLuiz Augusto von Dentz 
1191ccf74f23SLuiz Augusto von Dentz 	return true;
1192ccf74f23SLuiz Augusto von Dentz }
1193ccf74f23SLuiz Augusto von Dentz 
11940fe8c8d0SIulia Tanasescu static bool check_ucast_qos(struct bt_iso_qos *qos)
1195ccf74f23SLuiz Augusto von Dentz {
11960fe8c8d0SIulia Tanasescu 	if (qos->ucast.sca > 0x07)
1197ccf74f23SLuiz Augusto von Dentz 		return false;
1198ccf74f23SLuiz Augusto von Dentz 
11990fe8c8d0SIulia Tanasescu 	if (qos->ucast.packing > 0x01)
1200ccf74f23SLuiz Augusto von Dentz 		return false;
1201ccf74f23SLuiz Augusto von Dentz 
12020fe8c8d0SIulia Tanasescu 	if (qos->ucast.framing > 0x01)
1203ccf74f23SLuiz Augusto von Dentz 		return false;
1204ccf74f23SLuiz Augusto von Dentz 
12050fe8c8d0SIulia Tanasescu 	if (!check_io_qos(&qos->ucast.in))
1206ccf74f23SLuiz Augusto von Dentz 		return false;
1207ccf74f23SLuiz Augusto von Dentz 
12080fe8c8d0SIulia Tanasescu 	if (!check_io_qos(&qos->ucast.out))
12090fe8c8d0SIulia Tanasescu 		return false;
12100fe8c8d0SIulia Tanasescu 
12110fe8c8d0SIulia Tanasescu 	return true;
12120fe8c8d0SIulia Tanasescu }
12130fe8c8d0SIulia Tanasescu 
12140fe8c8d0SIulia Tanasescu static bool check_bcast_qos(struct bt_iso_qos *qos)
12150fe8c8d0SIulia Tanasescu {
12160fe8c8d0SIulia Tanasescu 	if (qos->bcast.sync_interval > 0x07)
12170fe8c8d0SIulia Tanasescu 		return false;
12180fe8c8d0SIulia Tanasescu 
12190fe8c8d0SIulia Tanasescu 	if (qos->bcast.packing > 0x01)
12200fe8c8d0SIulia Tanasescu 		return false;
12210fe8c8d0SIulia Tanasescu 
12220fe8c8d0SIulia Tanasescu 	if (qos->bcast.framing > 0x01)
12230fe8c8d0SIulia Tanasescu 		return false;
12240fe8c8d0SIulia Tanasescu 
12250fe8c8d0SIulia Tanasescu 	if (!check_io_qos(&qos->bcast.in))
12260fe8c8d0SIulia Tanasescu 		return false;
12270fe8c8d0SIulia Tanasescu 
12280fe8c8d0SIulia Tanasescu 	if (!check_io_qos(&qos->bcast.out))
12290fe8c8d0SIulia Tanasescu 		return false;
12300fe8c8d0SIulia Tanasescu 
12310fe8c8d0SIulia Tanasescu 	if (qos->bcast.encryption > 0x01)
12320fe8c8d0SIulia Tanasescu 		return false;
12330fe8c8d0SIulia Tanasescu 
12340fe8c8d0SIulia Tanasescu 	if (qos->bcast.options > 0x07)
12350fe8c8d0SIulia Tanasescu 		return false;
12360fe8c8d0SIulia Tanasescu 
12370fe8c8d0SIulia Tanasescu 	if (qos->bcast.skip > 0x01f3)
12380fe8c8d0SIulia Tanasescu 		return false;
12390fe8c8d0SIulia Tanasescu 
12400fe8c8d0SIulia Tanasescu 	if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000)
12410fe8c8d0SIulia Tanasescu 		return false;
12420fe8c8d0SIulia Tanasescu 
12430fe8c8d0SIulia Tanasescu 	if (qos->bcast.sync_cte_type > 0x1f)
12440fe8c8d0SIulia Tanasescu 		return false;
12450fe8c8d0SIulia Tanasescu 
12460fe8c8d0SIulia Tanasescu 	if (qos->bcast.mse > 0x1f)
12470fe8c8d0SIulia Tanasescu 		return false;
12480fe8c8d0SIulia Tanasescu 
12490fe8c8d0SIulia Tanasescu 	if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000)
1250ccf74f23SLuiz Augusto von Dentz 		return false;
1251ccf74f23SLuiz Augusto von Dentz 
1252ccf74f23SLuiz Augusto von Dentz 	return true;
1253ccf74f23SLuiz Augusto von Dentz }
1254ccf74f23SLuiz Augusto von Dentz 
1255ccf74f23SLuiz Augusto von Dentz static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1256ccf74f23SLuiz Augusto von Dentz 			       sockptr_t optval, unsigned int optlen)
1257ccf74f23SLuiz Augusto von Dentz {
1258ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1259ccf74f23SLuiz Augusto von Dentz 	int len, err = 0;
12600fe8c8d0SIulia Tanasescu 	struct bt_iso_qos qos = default_qos;
1261ccf74f23SLuiz Augusto von Dentz 	u32 opt;
1262ccf74f23SLuiz Augusto von Dentz 
1263ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1264ccf74f23SLuiz Augusto von Dentz 
1265ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1266ccf74f23SLuiz Augusto von Dentz 
1267ccf74f23SLuiz Augusto von Dentz 	switch (optname) {
1268ccf74f23SLuiz Augusto von Dentz 	case BT_DEFER_SETUP:
1269ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1270ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1271ccf74f23SLuiz Augusto von Dentz 			break;
1272ccf74f23SLuiz Augusto von Dentz 		}
1273ccf74f23SLuiz Augusto von Dentz 
1274ccf74f23SLuiz Augusto von Dentz 		if (copy_from_sockptr(&opt, optval, sizeof(u32))) {
1275ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1276ccf74f23SLuiz Augusto von Dentz 			break;
1277ccf74f23SLuiz Augusto von Dentz 		}
1278ccf74f23SLuiz Augusto von Dentz 
1279ccf74f23SLuiz Augusto von Dentz 		if (opt)
1280ccf74f23SLuiz Augusto von Dentz 			set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1281ccf74f23SLuiz Augusto von Dentz 		else
1282ccf74f23SLuiz Augusto von Dentz 			clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1283ccf74f23SLuiz Augusto von Dentz 		break;
1284ccf74f23SLuiz Augusto von Dentz 
1285ccf74f23SLuiz Augusto von Dentz 	case BT_ISO_QOS:
1286ccf74f23SLuiz Augusto von Dentz 		if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1287ccf74f23SLuiz Augusto von Dentz 		    sk->sk_state != BT_CONNECT2) {
1288ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1289ccf74f23SLuiz Augusto von Dentz 			break;
1290ccf74f23SLuiz Augusto von Dentz 		}
1291ccf74f23SLuiz Augusto von Dentz 
1292ccf74f23SLuiz Augusto von Dentz 		len = min_t(unsigned int, sizeof(qos), optlen);
1293ccf74f23SLuiz Augusto von Dentz 
1294ccf74f23SLuiz Augusto von Dentz 		if (copy_from_sockptr(&qos, optval, len)) {
1295ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1296ccf74f23SLuiz Augusto von Dentz 			break;
1297ccf74f23SLuiz Augusto von Dentz 		}
1298ccf74f23SLuiz Augusto von Dentz 
12990fe8c8d0SIulia Tanasescu 		if (len == sizeof(qos.ucast) && !check_ucast_qos(&qos)) {
1300ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1301ccf74f23SLuiz Augusto von Dentz 			break;
1302ccf74f23SLuiz Augusto von Dentz 		}
1303ccf74f23SLuiz Augusto von Dentz 
1304ccf74f23SLuiz Augusto von Dentz 		iso_pi(sk)->qos = qos;
13050fe8c8d0SIulia Tanasescu 		iso_pi(sk)->qos_user_set = true;
1306ccf74f23SLuiz Augusto von Dentz 
1307ccf74f23SLuiz Augusto von Dentz 		break;
1308ccf74f23SLuiz Augusto von Dentz 
1309f764a6c2SLuiz Augusto von Dentz 	case BT_ISO_BASE:
1310f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1311f764a6c2SLuiz Augusto von Dentz 		    sk->sk_state != BT_CONNECT2) {
1312f764a6c2SLuiz Augusto von Dentz 			err = -EINVAL;
1313f764a6c2SLuiz Augusto von Dentz 			break;
1314f764a6c2SLuiz Augusto von Dentz 		}
1315f764a6c2SLuiz Augusto von Dentz 
1316f764a6c2SLuiz Augusto von Dentz 		if (optlen > sizeof(iso_pi(sk)->base)) {
1317f764a6c2SLuiz Augusto von Dentz 			err = -EOVERFLOW;
1318f764a6c2SLuiz Augusto von Dentz 			break;
1319f764a6c2SLuiz Augusto von Dentz 		}
1320f764a6c2SLuiz Augusto von Dentz 
1321f764a6c2SLuiz Augusto von Dentz 		len = min_t(unsigned int, sizeof(iso_pi(sk)->base), optlen);
1322f764a6c2SLuiz Augusto von Dentz 
1323f764a6c2SLuiz Augusto von Dentz 		if (copy_from_sockptr(iso_pi(sk)->base, optval, len)) {
1324f764a6c2SLuiz Augusto von Dentz 			err = -EFAULT;
1325f764a6c2SLuiz Augusto von Dentz 			break;
1326f764a6c2SLuiz Augusto von Dentz 		}
1327f764a6c2SLuiz Augusto von Dentz 
1328f764a6c2SLuiz Augusto von Dentz 		iso_pi(sk)->base_len = len;
1329f764a6c2SLuiz Augusto von Dentz 
1330f764a6c2SLuiz Augusto von Dentz 		break;
1331f764a6c2SLuiz Augusto von Dentz 
1332ccf74f23SLuiz Augusto von Dentz 	default:
1333ccf74f23SLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
1334ccf74f23SLuiz Augusto von Dentz 		break;
1335ccf74f23SLuiz Augusto von Dentz 	}
1336ccf74f23SLuiz Augusto von Dentz 
1337ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1338ccf74f23SLuiz Augusto von Dentz 	return err;
1339ccf74f23SLuiz Augusto von Dentz }
1340ccf74f23SLuiz Augusto von Dentz 
1341ccf74f23SLuiz Augusto von Dentz static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1342ccf74f23SLuiz Augusto von Dentz 			       char __user *optval, int __user *optlen)
1343ccf74f23SLuiz Augusto von Dentz {
1344ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1345ccf74f23SLuiz Augusto von Dentz 	int len, err = 0;
13460eee4995SLuiz Augusto von Dentz 	struct bt_iso_qos *qos;
1347f764a6c2SLuiz Augusto von Dentz 	u8 base_len;
1348f764a6c2SLuiz Augusto von Dentz 	u8 *base;
1349ccf74f23SLuiz Augusto von Dentz 
1350ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1351ccf74f23SLuiz Augusto von Dentz 
1352ccf74f23SLuiz Augusto von Dentz 	if (get_user(len, optlen))
1353ccf74f23SLuiz Augusto von Dentz 		return -EFAULT;
1354ccf74f23SLuiz Augusto von Dentz 
1355ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1356ccf74f23SLuiz Augusto von Dentz 
1357ccf74f23SLuiz Augusto von Dentz 	switch (optname) {
1358ccf74f23SLuiz Augusto von Dentz 	case BT_DEFER_SETUP:
13599dfe1727SLuiz Augusto von Dentz 		if (sk->sk_state == BT_CONNECTED) {
1360ccf74f23SLuiz Augusto von Dentz 			err = -EINVAL;
1361ccf74f23SLuiz Augusto von Dentz 			break;
1362ccf74f23SLuiz Augusto von Dentz 		}
1363ccf74f23SLuiz Augusto von Dentz 
1364ccf74f23SLuiz Augusto von Dentz 		if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1365ccf74f23SLuiz Augusto von Dentz 			     (u32 __user *)optval))
1366ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1367ccf74f23SLuiz Augusto von Dentz 
1368ccf74f23SLuiz Augusto von Dentz 		break;
1369ccf74f23SLuiz Augusto von Dentz 
1370ccf74f23SLuiz Augusto von Dentz 	case BT_ISO_QOS:
13711d1ab5d3SLuiz Augusto von Dentz 		qos = iso_sock_get_qos(sk);
1372ccf74f23SLuiz Augusto von Dentz 
13730eee4995SLuiz Augusto von Dentz 		len = min_t(unsigned int, len, sizeof(*qos));
13740eee4995SLuiz Augusto von Dentz 		if (copy_to_user(optval, qos, len))
1375ccf74f23SLuiz Augusto von Dentz 			err = -EFAULT;
1376ccf74f23SLuiz Augusto von Dentz 
1377ccf74f23SLuiz Augusto von Dentz 		break;
1378ccf74f23SLuiz Augusto von Dentz 
1379f764a6c2SLuiz Augusto von Dentz 	case BT_ISO_BASE:
1380f764a6c2SLuiz Augusto von Dentz 		if (sk->sk_state == BT_CONNECTED) {
1381f764a6c2SLuiz Augusto von Dentz 			base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1382f764a6c2SLuiz Augusto von Dentz 			base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1383f764a6c2SLuiz Augusto von Dentz 		} else {
1384f764a6c2SLuiz Augusto von Dentz 			base_len = iso_pi(sk)->base_len;
1385f764a6c2SLuiz Augusto von Dentz 			base = iso_pi(sk)->base;
1386f764a6c2SLuiz Augusto von Dentz 		}
1387f764a6c2SLuiz Augusto von Dentz 
1388f764a6c2SLuiz Augusto von Dentz 		len = min_t(unsigned int, len, base_len);
1389f764a6c2SLuiz Augusto von Dentz 		if (copy_to_user(optval, base, len))
1390f764a6c2SLuiz Augusto von Dentz 			err = -EFAULT;
1391f764a6c2SLuiz Augusto von Dentz 
1392f764a6c2SLuiz Augusto von Dentz 		break;
1393f764a6c2SLuiz Augusto von Dentz 
1394ccf74f23SLuiz Augusto von Dentz 	default:
1395ccf74f23SLuiz Augusto von Dentz 		err = -ENOPROTOOPT;
1396ccf74f23SLuiz Augusto von Dentz 		break;
1397ccf74f23SLuiz Augusto von Dentz 	}
1398ccf74f23SLuiz Augusto von Dentz 
1399ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1400ccf74f23SLuiz Augusto von Dentz 	return err;
1401ccf74f23SLuiz Augusto von Dentz }
1402ccf74f23SLuiz Augusto von Dentz 
1403ccf74f23SLuiz Augusto von Dentz static int iso_sock_shutdown(struct socket *sock, int how)
1404ccf74f23SLuiz Augusto von Dentz {
1405ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1406ccf74f23SLuiz Augusto von Dentz 	int err = 0;
1407ccf74f23SLuiz Augusto von Dentz 
1408c5729093SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1409ccf74f23SLuiz Augusto von Dentz 
1410ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1411ccf74f23SLuiz Augusto von Dentz 		return 0;
1412ccf74f23SLuiz Augusto von Dentz 
1413ccf74f23SLuiz Augusto von Dentz 	sock_hold(sk);
1414ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1415ccf74f23SLuiz Augusto von Dentz 
1416c5729093SLuiz Augusto von Dentz 	switch (how) {
1417c5729093SLuiz Augusto von Dentz 	case SHUT_RD:
1418c5729093SLuiz Augusto von Dentz 		if (sk->sk_shutdown & RCV_SHUTDOWN)
1419c5729093SLuiz Augusto von Dentz 			goto unlock;
1420c5729093SLuiz Augusto von Dentz 		sk->sk_shutdown |= RCV_SHUTDOWN;
1421c5729093SLuiz Augusto von Dentz 		break;
1422c5729093SLuiz Augusto von Dentz 	case SHUT_WR:
1423c5729093SLuiz Augusto von Dentz 		if (sk->sk_shutdown & SEND_SHUTDOWN)
1424c5729093SLuiz Augusto von Dentz 			goto unlock;
1425c5729093SLuiz Augusto von Dentz 		sk->sk_shutdown |= SEND_SHUTDOWN;
1426c5729093SLuiz Augusto von Dentz 		break;
1427c5729093SLuiz Augusto von Dentz 	case SHUT_RDWR:
1428c5729093SLuiz Augusto von Dentz 		if (sk->sk_shutdown & SHUTDOWN_MASK)
1429c5729093SLuiz Augusto von Dentz 			goto unlock;
1430c5729093SLuiz Augusto von Dentz 		sk->sk_shutdown |= SHUTDOWN_MASK;
1431c5729093SLuiz Augusto von Dentz 		break;
1432c5729093SLuiz Augusto von Dentz 	}
1433c5729093SLuiz Augusto von Dentz 
1434ccf74f23SLuiz Augusto von Dentz 	iso_sock_clear_timer(sk);
1435ccf74f23SLuiz Augusto von Dentz 	__iso_sock_close(sk);
1436ccf74f23SLuiz Augusto von Dentz 
1437ccf74f23SLuiz Augusto von Dentz 	if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1438ccf74f23SLuiz Augusto von Dentz 	    !(current->flags & PF_EXITING))
1439c5729093SLuiz Augusto von Dentz 		err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1440ccf74f23SLuiz Augusto von Dentz 
1441c5729093SLuiz Augusto von Dentz unlock:
1442ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1443ccf74f23SLuiz Augusto von Dentz 	sock_put(sk);
1444ccf74f23SLuiz Augusto von Dentz 
1445ccf74f23SLuiz Augusto von Dentz 	return err;
1446ccf74f23SLuiz Augusto von Dentz }
1447ccf74f23SLuiz Augusto von Dentz 
1448ccf74f23SLuiz Augusto von Dentz static int iso_sock_release(struct socket *sock)
1449ccf74f23SLuiz Augusto von Dentz {
1450ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = sock->sk;
1451ccf74f23SLuiz Augusto von Dentz 	int err = 0;
1452ccf74f23SLuiz Augusto von Dentz 
1453ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sock %p, sk %p", sock, sk);
1454ccf74f23SLuiz Augusto von Dentz 
1455ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1456ccf74f23SLuiz Augusto von Dentz 		return 0;
1457ccf74f23SLuiz Augusto von Dentz 
1458ccf74f23SLuiz Augusto von Dentz 	iso_sock_close(sk);
1459ccf74f23SLuiz Augusto von Dentz 
1460ccf74f23SLuiz Augusto von Dentz 	if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1461ccf74f23SLuiz Augusto von Dentz 	    !(current->flags & PF_EXITING)) {
1462ccf74f23SLuiz Augusto von Dentz 		lock_sock(sk);
1463ccf74f23SLuiz Augusto von Dentz 		err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1464ccf74f23SLuiz Augusto von Dentz 		release_sock(sk);
1465ccf74f23SLuiz Augusto von Dentz 	}
1466ccf74f23SLuiz Augusto von Dentz 
1467ccf74f23SLuiz Augusto von Dentz 	sock_orphan(sk);
1468ccf74f23SLuiz Augusto von Dentz 	iso_sock_kill(sk);
1469ccf74f23SLuiz Augusto von Dentz 	return err;
1470ccf74f23SLuiz Augusto von Dentz }
1471ccf74f23SLuiz Augusto von Dentz 
1472ccf74f23SLuiz Augusto von Dentz static void iso_sock_ready(struct sock *sk)
1473ccf74f23SLuiz Augusto von Dentz {
1474ccf74f23SLuiz Augusto von Dentz 	BT_DBG("sk %p", sk);
1475ccf74f23SLuiz Augusto von Dentz 
1476ccf74f23SLuiz Augusto von Dentz 	if (!sk)
1477ccf74f23SLuiz Augusto von Dentz 		return;
1478ccf74f23SLuiz Augusto von Dentz 
1479ccf74f23SLuiz Augusto von Dentz 	lock_sock(sk);
1480ccf74f23SLuiz Augusto von Dentz 	iso_sock_clear_timer(sk);
1481ccf74f23SLuiz Augusto von Dentz 	sk->sk_state = BT_CONNECTED;
1482ccf74f23SLuiz Augusto von Dentz 	sk->sk_state_change(sk);
1483ccf74f23SLuiz Augusto von Dentz 	release_sock(sk);
1484ccf74f23SLuiz Augusto von Dentz }
1485ccf74f23SLuiz Augusto von Dentz 
1486ccf74f23SLuiz Augusto von Dentz struct iso_list_data {
1487ccf74f23SLuiz Augusto von Dentz 	struct hci_conn *hcon;
1488ccf74f23SLuiz Augusto von Dentz 	int count;
1489ccf74f23SLuiz Augusto von Dentz };
1490ccf74f23SLuiz Augusto von Dentz 
1491f764a6c2SLuiz Augusto von Dentz static bool iso_match_big(struct sock *sk, void *data)
1492f764a6c2SLuiz Augusto von Dentz {
1493f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_sync_estabilished *ev = data;
1494f764a6c2SLuiz Augusto von Dentz 
14950fe8c8d0SIulia Tanasescu 	return ev->handle == iso_pi(sk)->qos.bcast.big;
1496f764a6c2SLuiz Augusto von Dentz }
1497f764a6c2SLuiz Augusto von Dentz 
1498ccf74f23SLuiz Augusto von Dentz static void iso_conn_ready(struct iso_conn *conn)
1499ccf74f23SLuiz Augusto von Dentz {
1500ccf74f23SLuiz Augusto von Dentz 	struct sock *parent;
1501ccf74f23SLuiz Augusto von Dentz 	struct sock *sk = conn->sk;
1502f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_big_sync_estabilished *ev;
15036a5ad251SLuiz Augusto von Dentz 	struct hci_conn *hcon;
1504ccf74f23SLuiz Augusto von Dentz 
1505ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p", conn);
1506ccf74f23SLuiz Augusto von Dentz 
1507ccf74f23SLuiz Augusto von Dentz 	if (sk) {
1508ccf74f23SLuiz Augusto von Dentz 		iso_sock_ready(conn->sk);
1509ccf74f23SLuiz Augusto von Dentz 	} else {
15106a5ad251SLuiz Augusto von Dentz 		hcon = conn->hcon;
15116a5ad251SLuiz Augusto von Dentz 		if (!hcon)
1512ccf74f23SLuiz Augusto von Dentz 			return;
1513ccf74f23SLuiz Augusto von Dentz 
15146a5ad251SLuiz Augusto von Dentz 		ev = hci_recv_event_data(hcon->hdev,
1515f764a6c2SLuiz Augusto von Dentz 					 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1516f764a6c2SLuiz Augusto von Dentz 		if (ev)
15176a5ad251SLuiz Augusto von Dentz 			parent = iso_get_sock_listen(&hcon->src,
15186a5ad251SLuiz Augusto von Dentz 						     &hcon->dst,
1519f764a6c2SLuiz Augusto von Dentz 						     iso_match_big, ev);
1520f764a6c2SLuiz Augusto von Dentz 		else
15216a5ad251SLuiz Augusto von Dentz 			parent = iso_get_sock_listen(&hcon->src,
1522f764a6c2SLuiz Augusto von Dentz 						     BDADDR_ANY, NULL, NULL);
1523f764a6c2SLuiz Augusto von Dentz 
15246a5ad251SLuiz Augusto von Dentz 		if (!parent)
1525ccf74f23SLuiz Augusto von Dentz 			return;
1526ccf74f23SLuiz Augusto von Dentz 
1527ccf74f23SLuiz Augusto von Dentz 		lock_sock(parent);
1528ccf74f23SLuiz Augusto von Dentz 
1529ccf74f23SLuiz Augusto von Dentz 		sk = iso_sock_alloc(sock_net(parent), NULL,
1530ccf74f23SLuiz Augusto von Dentz 				    BTPROTO_ISO, GFP_ATOMIC, 0);
1531ccf74f23SLuiz Augusto von Dentz 		if (!sk) {
1532ccf74f23SLuiz Augusto von Dentz 			release_sock(parent);
1533ccf74f23SLuiz Augusto von Dentz 			return;
1534ccf74f23SLuiz Augusto von Dentz 		}
1535ccf74f23SLuiz Augusto von Dentz 
1536ccf74f23SLuiz Augusto von Dentz 		iso_sock_init(sk, parent);
1537ccf74f23SLuiz Augusto von Dentz 
15386a5ad251SLuiz Augusto von Dentz 		bacpy(&iso_pi(sk)->src, &hcon->src);
15396a5ad251SLuiz Augusto von Dentz 		iso_pi(sk)->src_type = hcon->src_type;
1540f764a6c2SLuiz Augusto von Dentz 
1541f764a6c2SLuiz Augusto von Dentz 		/* If hcon has no destination address (BDADDR_ANY) it means it
1542f764a6c2SLuiz Augusto von Dentz 		 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED so we need to
1543f764a6c2SLuiz Augusto von Dentz 		 * initialize using the parent socket destination address.
1544f764a6c2SLuiz Augusto von Dentz 		 */
15456a5ad251SLuiz Augusto von Dentz 		if (!bacmp(&hcon->dst, BDADDR_ANY)) {
15466a5ad251SLuiz Augusto von Dentz 			bacpy(&hcon->dst, &iso_pi(parent)->dst);
15476a5ad251SLuiz Augusto von Dentz 			hcon->dst_type = iso_pi(parent)->dst_type;
15486a5ad251SLuiz Augusto von Dentz 			hcon->sync_handle = iso_pi(parent)->sync_handle;
1549f764a6c2SLuiz Augusto von Dentz 		}
1550f764a6c2SLuiz Augusto von Dentz 
15516a5ad251SLuiz Augusto von Dentz 		bacpy(&iso_pi(sk)->dst, &hcon->dst);
15526a5ad251SLuiz Augusto von Dentz 		iso_pi(sk)->dst_type = hcon->dst_type;
1553ccf74f23SLuiz Augusto von Dentz 
15546a5ad251SLuiz Augusto von Dentz 		hci_conn_hold(hcon);
15556a5ad251SLuiz Augusto von Dentz 		iso_chan_add(conn, sk, parent);
1556ccf74f23SLuiz Augusto von Dentz 
1557ccf74f23SLuiz Augusto von Dentz 		if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1558ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONNECT2;
1559ccf74f23SLuiz Augusto von Dentz 		else
1560ccf74f23SLuiz Augusto von Dentz 			sk->sk_state = BT_CONNECTED;
1561ccf74f23SLuiz Augusto von Dentz 
1562ccf74f23SLuiz Augusto von Dentz 		/* Wake up parent */
1563ccf74f23SLuiz Augusto von Dentz 		parent->sk_data_ready(parent);
1564ccf74f23SLuiz Augusto von Dentz 
1565ccf74f23SLuiz Augusto von Dentz 		release_sock(parent);
1566ccf74f23SLuiz Augusto von Dentz 	}
1567ccf74f23SLuiz Augusto von Dentz }
1568ccf74f23SLuiz Augusto von Dentz 
1569f764a6c2SLuiz Augusto von Dentz static bool iso_match_sid(struct sock *sk, void *data)
1570f764a6c2SLuiz Augusto von Dentz {
1571f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_pa_sync_established *ev = data;
1572f764a6c2SLuiz Augusto von Dentz 
1573f764a6c2SLuiz Augusto von Dentz 	return ev->sid == iso_pi(sk)->bc_sid;
1574f764a6c2SLuiz Augusto von Dentz }
1575f764a6c2SLuiz Augusto von Dentz 
1576f764a6c2SLuiz Augusto von Dentz static bool iso_match_sync_handle(struct sock *sk, void *data)
1577f764a6c2SLuiz Augusto von Dentz {
1578f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_info_adv_report *ev = data;
1579f764a6c2SLuiz Augusto von Dentz 
1580f764a6c2SLuiz Augusto von Dentz 	return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1581f764a6c2SLuiz Augusto von Dentz }
1582f764a6c2SLuiz Augusto von Dentz 
1583ccf74f23SLuiz Augusto von Dentz /* ----- ISO interface with lower layer (HCI) ----- */
1584f764a6c2SLuiz Augusto von Dentz 
1585ccf74f23SLuiz Augusto von Dentz int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1586ccf74f23SLuiz Augusto von Dentz {
1587f764a6c2SLuiz Augusto von Dentz 	struct hci_ev_le_pa_sync_established *ev1;
1588f764a6c2SLuiz Augusto von Dentz 	struct hci_evt_le_big_info_adv_report *ev2;
1589ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
1590ccf74f23SLuiz Augusto von Dentz 	int lm = 0;
1591ccf74f23SLuiz Augusto von Dentz 
1592f764a6c2SLuiz Augusto von Dentz 	bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1593ccf74f23SLuiz Augusto von Dentz 
1594f764a6c2SLuiz Augusto von Dentz 	/* Broadcast receiver requires handling of some events before it can
1595f764a6c2SLuiz Augusto von Dentz 	 * proceed to establishing a BIG sync:
1596f764a6c2SLuiz Augusto von Dentz 	 *
1597f764a6c2SLuiz Augusto von Dentz 	 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1598f764a6c2SLuiz Augusto von Dentz 	 * SID to listen to and once sync is estabilished its handle needs to
1599f764a6c2SLuiz Augusto von Dentz 	 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1600f764a6c2SLuiz Augusto von Dentz 	 * receiving the BIG Info.
1601f764a6c2SLuiz Augusto von Dentz 	 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1602f764a6c2SLuiz Augusto von Dentz 	 * a BIG Info it attempts to check if there any listening socket with
1603f764a6c2SLuiz Augusto von Dentz 	 * the same sync_handle and if it does then attempt to create a sync.
1604f764a6c2SLuiz Augusto von Dentz 	 */
1605f764a6c2SLuiz Augusto von Dentz 	ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1606f764a6c2SLuiz Augusto von Dentz 	if (ev1) {
1607f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr, iso_match_sid,
1608f764a6c2SLuiz Augusto von Dentz 					 ev1);
1609f764a6c2SLuiz Augusto von Dentz 		if (sk)
1610f764a6c2SLuiz Augusto von Dentz 			iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1611ccf74f23SLuiz Augusto von Dentz 
1612f764a6c2SLuiz Augusto von Dentz 		goto done;
1613f764a6c2SLuiz Augusto von Dentz 	}
1614f764a6c2SLuiz Augusto von Dentz 
1615f764a6c2SLuiz Augusto von Dentz 	ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1616f764a6c2SLuiz Augusto von Dentz 	if (ev2) {
1617f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, bdaddr,
1618f764a6c2SLuiz Augusto von Dentz 					 iso_match_sync_handle, ev2);
1619f764a6c2SLuiz Augusto von Dentz 		if (sk) {
1620f764a6c2SLuiz Augusto von Dentz 			int err;
1621f764a6c2SLuiz Augusto von Dentz 
1622f764a6c2SLuiz Augusto von Dentz 			if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1623f764a6c2SLuiz Augusto von Dentz 				iso_pi(sk)->bc_num_bis = ev2->num_bis;
1624f764a6c2SLuiz Augusto von Dentz 
1625f764a6c2SLuiz Augusto von Dentz 			err = hci_le_big_create_sync(hdev,
1626f764a6c2SLuiz Augusto von Dentz 						     &iso_pi(sk)->qos,
1627f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->sync_handle,
1628f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->bc_num_bis,
1629f764a6c2SLuiz Augusto von Dentz 						     iso_pi(sk)->bc_bis);
1630f764a6c2SLuiz Augusto von Dentz 			if (err) {
1631f764a6c2SLuiz Augusto von Dentz 				bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1632f764a6c2SLuiz Augusto von Dentz 					   err);
1633f764a6c2SLuiz Augusto von Dentz 				sk = NULL;
1634f764a6c2SLuiz Augusto von Dentz 			}
1635f764a6c2SLuiz Augusto von Dentz 		}
1636f764a6c2SLuiz Augusto von Dentz 	} else {
1637f764a6c2SLuiz Augusto von Dentz 		sk = iso_get_sock_listen(&hdev->bdaddr, BDADDR_ANY, NULL, NULL);
1638f764a6c2SLuiz Augusto von Dentz 	}
1639f764a6c2SLuiz Augusto von Dentz 
1640f764a6c2SLuiz Augusto von Dentz done:
1641f764a6c2SLuiz Augusto von Dentz 	if (!sk)
1642f764a6c2SLuiz Augusto von Dentz 		return lm;
1643f764a6c2SLuiz Augusto von Dentz 
1644ccf74f23SLuiz Augusto von Dentz 	lm |= HCI_LM_ACCEPT;
1645ccf74f23SLuiz Augusto von Dentz 
1646ccf74f23SLuiz Augusto von Dentz 	if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
1647ccf74f23SLuiz Augusto von Dentz 		*flags |= HCI_PROTO_DEFER;
1648ccf74f23SLuiz Augusto von Dentz 
1649ccf74f23SLuiz Augusto von Dentz 	return lm;
1650ccf74f23SLuiz Augusto von Dentz }
1651ccf74f23SLuiz Augusto von Dentz 
1652ccf74f23SLuiz Augusto von Dentz static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
1653ccf74f23SLuiz Augusto von Dentz {
1654ccf74f23SLuiz Augusto von Dentz 	if (hcon->type != ISO_LINK) {
1655ccf74f23SLuiz Augusto von Dentz 		if (hcon->type != LE_LINK)
1656ccf74f23SLuiz Augusto von Dentz 			return;
1657ccf74f23SLuiz Augusto von Dentz 
1658ccf74f23SLuiz Augusto von Dentz 		/* Check if LE link has failed */
1659ccf74f23SLuiz Augusto von Dentz 		if (status) {
1660*06149746SLuiz Augusto von Dentz 			struct hci_link *link, *t;
1661*06149746SLuiz Augusto von Dentz 
1662*06149746SLuiz Augusto von Dentz 			list_for_each_entry_safe(link, t, &hcon->link_list,
1663*06149746SLuiz Augusto von Dentz 						 list)
1664*06149746SLuiz Augusto von Dentz 				iso_conn_del(link->conn, bt_to_errno(status));
1665*06149746SLuiz Augusto von Dentz 
1666ccf74f23SLuiz Augusto von Dentz 			return;
1667ccf74f23SLuiz Augusto von Dentz 		}
1668ccf74f23SLuiz Augusto von Dentz 
1669ccf74f23SLuiz Augusto von Dentz 		/* Create CIS if pending */
1670ccf74f23SLuiz Augusto von Dentz 		hci_le_create_cis(hcon);
1671ccf74f23SLuiz Augusto von Dentz 		return;
1672ccf74f23SLuiz Augusto von Dentz 	}
1673ccf74f23SLuiz Augusto von Dentz 
1674ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1675ccf74f23SLuiz Augusto von Dentz 
1676ccf74f23SLuiz Augusto von Dentz 	if (!status) {
1677ccf74f23SLuiz Augusto von Dentz 		struct iso_conn *conn;
1678ccf74f23SLuiz Augusto von Dentz 
1679ccf74f23SLuiz Augusto von Dentz 		conn = iso_conn_add(hcon);
1680ccf74f23SLuiz Augusto von Dentz 		if (conn)
1681ccf74f23SLuiz Augusto von Dentz 			iso_conn_ready(conn);
1682ccf74f23SLuiz Augusto von Dentz 	} else {
1683ccf74f23SLuiz Augusto von Dentz 		iso_conn_del(hcon, bt_to_errno(status));
1684ccf74f23SLuiz Augusto von Dentz 	}
1685ccf74f23SLuiz Augusto von Dentz }
1686ccf74f23SLuiz Augusto von Dentz 
1687ccf74f23SLuiz Augusto von Dentz static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1688ccf74f23SLuiz Augusto von Dentz {
1689ccf74f23SLuiz Augusto von Dentz 	if (hcon->type != ISO_LINK)
1690ccf74f23SLuiz Augusto von Dentz 		return;
1691ccf74f23SLuiz Augusto von Dentz 
1692ccf74f23SLuiz Augusto von Dentz 	BT_DBG("hcon %p reason %d", hcon, reason);
1693ccf74f23SLuiz Augusto von Dentz 
1694ccf74f23SLuiz Augusto von Dentz 	iso_conn_del(hcon, bt_to_errno(reason));
1695ccf74f23SLuiz Augusto von Dentz }
1696ccf74f23SLuiz Augusto von Dentz 
1697ccf74f23SLuiz Augusto von Dentz void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
1698ccf74f23SLuiz Augusto von Dentz {
1699ccf74f23SLuiz Augusto von Dentz 	struct iso_conn *conn = hcon->iso_data;
1700ccf74f23SLuiz Augusto von Dentz 	__u16 pb, ts, len;
1701ccf74f23SLuiz Augusto von Dentz 
1702ccf74f23SLuiz Augusto von Dentz 	if (!conn)
1703ccf74f23SLuiz Augusto von Dentz 		goto drop;
1704ccf74f23SLuiz Augusto von Dentz 
1705ccf74f23SLuiz Augusto von Dentz 	pb     = hci_iso_flags_pb(flags);
1706ccf74f23SLuiz Augusto von Dentz 	ts     = hci_iso_flags_ts(flags);
1707ccf74f23SLuiz Augusto von Dentz 
1708ccf74f23SLuiz Augusto von Dentz 	BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
1709ccf74f23SLuiz Augusto von Dentz 
1710ccf74f23SLuiz Augusto von Dentz 	switch (pb) {
1711ccf74f23SLuiz Augusto von Dentz 	case ISO_START:
1712ccf74f23SLuiz Augusto von Dentz 	case ISO_SINGLE:
1713ccf74f23SLuiz Augusto von Dentz 		if (conn->rx_len) {
1714ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Unexpected start frame (len %d)", skb->len);
1715ccf74f23SLuiz Augusto von Dentz 			kfree_skb(conn->rx_skb);
1716ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1717ccf74f23SLuiz Augusto von Dentz 			conn->rx_len = 0;
1718ccf74f23SLuiz Augusto von Dentz 		}
1719ccf74f23SLuiz Augusto von Dentz 
1720ccf74f23SLuiz Augusto von Dentz 		if (ts) {
17212f10e40aSPauli Virtanen 			struct hci_iso_ts_data_hdr *hdr;
17222f10e40aSPauli Virtanen 
1723ccf74f23SLuiz Augusto von Dentz 			/* TODO: add timestamp to the packet? */
1724ccf74f23SLuiz Augusto von Dentz 			hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
1725ccf74f23SLuiz Augusto von Dentz 			if (!hdr) {
1726ccf74f23SLuiz Augusto von Dentz 				BT_ERR("Frame is too short (len %d)", skb->len);
1727ccf74f23SLuiz Augusto von Dentz 				goto drop;
1728ccf74f23SLuiz Augusto von Dentz 			}
1729ccf74f23SLuiz Augusto von Dentz 
17302f10e40aSPauli Virtanen 			len = __le16_to_cpu(hdr->slen);
1731ccf74f23SLuiz Augusto von Dentz 		} else {
17322f10e40aSPauli Virtanen 			struct hci_iso_data_hdr *hdr;
17332f10e40aSPauli Virtanen 
1734ccf74f23SLuiz Augusto von Dentz 			hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
1735ccf74f23SLuiz Augusto von Dentz 			if (!hdr) {
1736ccf74f23SLuiz Augusto von Dentz 				BT_ERR("Frame is too short (len %d)", skb->len);
1737ccf74f23SLuiz Augusto von Dentz 				goto drop;
1738ccf74f23SLuiz Augusto von Dentz 			}
1739ccf74f23SLuiz Augusto von Dentz 
1740ccf74f23SLuiz Augusto von Dentz 			len = __le16_to_cpu(hdr->slen);
17412f10e40aSPauli Virtanen 		}
17422f10e40aSPauli Virtanen 
1743ccf74f23SLuiz Augusto von Dentz 		flags  = hci_iso_data_flags(len);
1744ccf74f23SLuiz Augusto von Dentz 		len    = hci_iso_data_len(len);
1745ccf74f23SLuiz Augusto von Dentz 
1746ccf74f23SLuiz Augusto von Dentz 		BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
1747ccf74f23SLuiz Augusto von Dentz 		       skb->len, flags);
1748ccf74f23SLuiz Augusto von Dentz 
1749ccf74f23SLuiz Augusto von Dentz 		if (len == skb->len) {
1750ccf74f23SLuiz Augusto von Dentz 			/* Complete frame received */
1751ccf74f23SLuiz Augusto von Dentz 			iso_recv_frame(conn, skb);
1752ccf74f23SLuiz Augusto von Dentz 			return;
1753ccf74f23SLuiz Augusto von Dentz 		}
1754ccf74f23SLuiz Augusto von Dentz 
1755ccf74f23SLuiz Augusto von Dentz 		if (pb == ISO_SINGLE) {
1756ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Frame malformed (len %d, expected len %d)",
1757ccf74f23SLuiz Augusto von Dentz 			       skb->len, len);
1758ccf74f23SLuiz Augusto von Dentz 			goto drop;
1759ccf74f23SLuiz Augusto von Dentz 		}
1760ccf74f23SLuiz Augusto von Dentz 
1761ccf74f23SLuiz Augusto von Dentz 		if (skb->len > len) {
1762ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Frame is too long (len %d, expected len %d)",
1763ccf74f23SLuiz Augusto von Dentz 			       skb->len, len);
1764ccf74f23SLuiz Augusto von Dentz 			goto drop;
1765ccf74f23SLuiz Augusto von Dentz 		}
1766ccf74f23SLuiz Augusto von Dentz 
1767ccf74f23SLuiz Augusto von Dentz 		/* Allocate skb for the complete frame (with header) */
1768ccf74f23SLuiz Augusto von Dentz 		conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
1769ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_skb)
1770ccf74f23SLuiz Augusto von Dentz 			goto drop;
1771ccf74f23SLuiz Augusto von Dentz 
1772ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1773ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1774ccf74f23SLuiz Augusto von Dentz 		conn->rx_len = len - skb->len;
1775ccf74f23SLuiz Augusto von Dentz 		break;
1776ccf74f23SLuiz Augusto von Dentz 
1777ccf74f23SLuiz Augusto von Dentz 	case ISO_CONT:
1778ccf74f23SLuiz Augusto von Dentz 		BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
1779ccf74f23SLuiz Augusto von Dentz 		       conn->rx_len);
1780ccf74f23SLuiz Augusto von Dentz 
1781ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_len) {
1782ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Unexpected continuation frame (len %d)",
1783ccf74f23SLuiz Augusto von Dentz 			       skb->len);
1784ccf74f23SLuiz Augusto von Dentz 			goto drop;
1785ccf74f23SLuiz Augusto von Dentz 		}
1786ccf74f23SLuiz Augusto von Dentz 
1787ccf74f23SLuiz Augusto von Dentz 		if (skb->len > conn->rx_len) {
1788ccf74f23SLuiz Augusto von Dentz 			BT_ERR("Fragment is too long (len %d, expected %d)",
1789ccf74f23SLuiz Augusto von Dentz 			       skb->len, conn->rx_len);
1790ccf74f23SLuiz Augusto von Dentz 			kfree_skb(conn->rx_skb);
1791ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1792ccf74f23SLuiz Augusto von Dentz 			conn->rx_len = 0;
1793ccf74f23SLuiz Augusto von Dentz 			goto drop;
1794ccf74f23SLuiz Augusto von Dentz 		}
1795ccf74f23SLuiz Augusto von Dentz 
1796ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1797ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1798ccf74f23SLuiz Augusto von Dentz 		conn->rx_len -= skb->len;
1799ccf74f23SLuiz Augusto von Dentz 		return;
1800ccf74f23SLuiz Augusto von Dentz 
1801ccf74f23SLuiz Augusto von Dentz 	case ISO_END:
1802ccf74f23SLuiz Augusto von Dentz 		skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
1803ccf74f23SLuiz Augusto von Dentz 					  skb->len);
1804ccf74f23SLuiz Augusto von Dentz 		conn->rx_len -= skb->len;
1805ccf74f23SLuiz Augusto von Dentz 
1806ccf74f23SLuiz Augusto von Dentz 		if (!conn->rx_len) {
1807ccf74f23SLuiz Augusto von Dentz 			struct sk_buff *rx_skb = conn->rx_skb;
1808ccf74f23SLuiz Augusto von Dentz 
1809ccf74f23SLuiz Augusto von Dentz 			/* Complete frame received. iso_recv_frame
1810ccf74f23SLuiz Augusto von Dentz 			 * takes ownership of the skb so set the global
1811ccf74f23SLuiz Augusto von Dentz 			 * rx_skb pointer to NULL first.
1812ccf74f23SLuiz Augusto von Dentz 			 */
1813ccf74f23SLuiz Augusto von Dentz 			conn->rx_skb = NULL;
1814ccf74f23SLuiz Augusto von Dentz 			iso_recv_frame(conn, rx_skb);
1815ccf74f23SLuiz Augusto von Dentz 		}
1816ccf74f23SLuiz Augusto von Dentz 		break;
1817ccf74f23SLuiz Augusto von Dentz 	}
1818ccf74f23SLuiz Augusto von Dentz 
1819ccf74f23SLuiz Augusto von Dentz drop:
1820ccf74f23SLuiz Augusto von Dentz 	kfree_skb(skb);
1821ccf74f23SLuiz Augusto von Dentz }
1822ccf74f23SLuiz Augusto von Dentz 
1823ccf74f23SLuiz Augusto von Dentz static struct hci_cb iso_cb = {
1824ccf74f23SLuiz Augusto von Dentz 	.name		= "ISO",
1825ccf74f23SLuiz Augusto von Dentz 	.connect_cfm	= iso_connect_cfm,
1826ccf74f23SLuiz Augusto von Dentz 	.disconn_cfm	= iso_disconn_cfm,
1827ccf74f23SLuiz Augusto von Dentz };
1828ccf74f23SLuiz Augusto von Dentz 
1829ccf74f23SLuiz Augusto von Dentz static int iso_debugfs_show(struct seq_file *f, void *p)
1830ccf74f23SLuiz Augusto von Dentz {
1831ccf74f23SLuiz Augusto von Dentz 	struct sock *sk;
1832ccf74f23SLuiz Augusto von Dentz 
1833ccf74f23SLuiz Augusto von Dentz 	read_lock(&iso_sk_list.lock);
1834ccf74f23SLuiz Augusto von Dentz 
1835ccf74f23SLuiz Augusto von Dentz 	sk_for_each(sk, &iso_sk_list.head) {
1836ccf74f23SLuiz Augusto von Dentz 		seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
1837ccf74f23SLuiz Augusto von Dentz 			   &iso_pi(sk)->dst, sk->sk_state);
1838ccf74f23SLuiz Augusto von Dentz 	}
1839ccf74f23SLuiz Augusto von Dentz 
1840ccf74f23SLuiz Augusto von Dentz 	read_unlock(&iso_sk_list.lock);
1841ccf74f23SLuiz Augusto von Dentz 
1842ccf74f23SLuiz Augusto von Dentz 	return 0;
1843ccf74f23SLuiz Augusto von Dentz }
1844ccf74f23SLuiz Augusto von Dentz 
1845ccf74f23SLuiz Augusto von Dentz DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
1846ccf74f23SLuiz Augusto von Dentz 
1847ccf74f23SLuiz Augusto von Dentz static struct dentry *iso_debugfs;
1848ccf74f23SLuiz Augusto von Dentz 
1849ccf74f23SLuiz Augusto von Dentz static const struct proto_ops iso_sock_ops = {
1850ccf74f23SLuiz Augusto von Dentz 	.family		= PF_BLUETOOTH,
1851ccf74f23SLuiz Augusto von Dentz 	.owner		= THIS_MODULE,
1852ccf74f23SLuiz Augusto von Dentz 	.release	= iso_sock_release,
1853ccf74f23SLuiz Augusto von Dentz 	.bind		= iso_sock_bind,
1854ccf74f23SLuiz Augusto von Dentz 	.connect	= iso_sock_connect,
1855ccf74f23SLuiz Augusto von Dentz 	.listen		= iso_sock_listen,
1856ccf74f23SLuiz Augusto von Dentz 	.accept		= iso_sock_accept,
1857ccf74f23SLuiz Augusto von Dentz 	.getname	= iso_sock_getname,
1858ccf74f23SLuiz Augusto von Dentz 	.sendmsg	= iso_sock_sendmsg,
1859ccf74f23SLuiz Augusto von Dentz 	.recvmsg	= iso_sock_recvmsg,
1860ccf74f23SLuiz Augusto von Dentz 	.poll		= bt_sock_poll,
1861ccf74f23SLuiz Augusto von Dentz 	.ioctl		= bt_sock_ioctl,
1862ccf74f23SLuiz Augusto von Dentz 	.mmap		= sock_no_mmap,
1863ccf74f23SLuiz Augusto von Dentz 	.socketpair	= sock_no_socketpair,
1864ccf74f23SLuiz Augusto von Dentz 	.shutdown	= iso_sock_shutdown,
1865ccf74f23SLuiz Augusto von Dentz 	.setsockopt	= iso_sock_setsockopt,
1866ccf74f23SLuiz Augusto von Dentz 	.getsockopt	= iso_sock_getsockopt
1867ccf74f23SLuiz Augusto von Dentz };
1868ccf74f23SLuiz Augusto von Dentz 
1869ccf74f23SLuiz Augusto von Dentz static const struct net_proto_family iso_sock_family_ops = {
1870ccf74f23SLuiz Augusto von Dentz 	.family	= PF_BLUETOOTH,
1871ccf74f23SLuiz Augusto von Dentz 	.owner	= THIS_MODULE,
1872ccf74f23SLuiz Augusto von Dentz 	.create	= iso_sock_create,
1873ccf74f23SLuiz Augusto von Dentz };
1874ccf74f23SLuiz Augusto von Dentz 
1875ccf74f23SLuiz Augusto von Dentz static bool iso_inited;
1876ccf74f23SLuiz Augusto von Dentz 
1877ccf74f23SLuiz Augusto von Dentz bool iso_enabled(void)
1878ccf74f23SLuiz Augusto von Dentz {
1879ccf74f23SLuiz Augusto von Dentz 	return iso_inited;
1880ccf74f23SLuiz Augusto von Dentz }
1881ccf74f23SLuiz Augusto von Dentz 
1882ccf74f23SLuiz Augusto von Dentz int iso_init(void)
1883ccf74f23SLuiz Augusto von Dentz {
1884ccf74f23SLuiz Augusto von Dentz 	int err;
1885ccf74f23SLuiz Augusto von Dentz 
1886ccf74f23SLuiz Augusto von Dentz 	BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
1887ccf74f23SLuiz Augusto von Dentz 
1888ccf74f23SLuiz Augusto von Dentz 	if (iso_inited)
1889ccf74f23SLuiz Augusto von Dentz 		return -EALREADY;
1890ccf74f23SLuiz Augusto von Dentz 
1891ccf74f23SLuiz Augusto von Dentz 	err = proto_register(&iso_proto, 0);
1892ccf74f23SLuiz Augusto von Dentz 	if (err < 0)
1893ccf74f23SLuiz Augusto von Dentz 		return err;
1894ccf74f23SLuiz Augusto von Dentz 
1895ccf74f23SLuiz Augusto von Dentz 	err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
1896ccf74f23SLuiz Augusto von Dentz 	if (err < 0) {
1897ccf74f23SLuiz Augusto von Dentz 		BT_ERR("ISO socket registration failed");
1898ccf74f23SLuiz Augusto von Dentz 		goto error;
1899ccf74f23SLuiz Augusto von Dentz 	}
1900ccf74f23SLuiz Augusto von Dentz 
1901ccf74f23SLuiz Augusto von Dentz 	err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
1902ccf74f23SLuiz Augusto von Dentz 	if (err < 0) {
1903ccf74f23SLuiz Augusto von Dentz 		BT_ERR("Failed to create ISO proc file");
1904ccf74f23SLuiz Augusto von Dentz 		bt_sock_unregister(BTPROTO_ISO);
1905ccf74f23SLuiz Augusto von Dentz 		goto error;
1906ccf74f23SLuiz Augusto von Dentz 	}
1907ccf74f23SLuiz Augusto von Dentz 
1908ccf74f23SLuiz Augusto von Dentz 	BT_INFO("ISO socket layer initialized");
1909ccf74f23SLuiz Augusto von Dentz 
1910ccf74f23SLuiz Augusto von Dentz 	hci_register_cb(&iso_cb);
1911ccf74f23SLuiz Augusto von Dentz 
1912ccf74f23SLuiz Augusto von Dentz 	if (IS_ERR_OR_NULL(bt_debugfs))
1913ccf74f23SLuiz Augusto von Dentz 		return 0;
1914ccf74f23SLuiz Augusto von Dentz 
1915ccf74f23SLuiz Augusto von Dentz 	if (!iso_debugfs) {
1916ccf74f23SLuiz Augusto von Dentz 		iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
1917ccf74f23SLuiz Augusto von Dentz 						  NULL, &iso_debugfs_fops);
1918ccf74f23SLuiz Augusto von Dentz 	}
1919ccf74f23SLuiz Augusto von Dentz 
1920ccf74f23SLuiz Augusto von Dentz 	iso_inited = true;
1921ccf74f23SLuiz Augusto von Dentz 
1922ccf74f23SLuiz Augusto von Dentz 	return 0;
1923ccf74f23SLuiz Augusto von Dentz 
1924ccf74f23SLuiz Augusto von Dentz error:
1925ccf74f23SLuiz Augusto von Dentz 	proto_unregister(&iso_proto);
1926ccf74f23SLuiz Augusto von Dentz 	return err;
1927ccf74f23SLuiz Augusto von Dentz }
1928ccf74f23SLuiz Augusto von Dentz 
1929ccf74f23SLuiz Augusto von Dentz int iso_exit(void)
1930ccf74f23SLuiz Augusto von Dentz {
1931ccf74f23SLuiz Augusto von Dentz 	if (!iso_inited)
1932ccf74f23SLuiz Augusto von Dentz 		return -EALREADY;
1933ccf74f23SLuiz Augusto von Dentz 
1934ccf74f23SLuiz Augusto von Dentz 	bt_procfs_cleanup(&init_net, "iso");
1935ccf74f23SLuiz Augusto von Dentz 
1936ccf74f23SLuiz Augusto von Dentz 	debugfs_remove(iso_debugfs);
1937ccf74f23SLuiz Augusto von Dentz 	iso_debugfs = NULL;
1938ccf74f23SLuiz Augusto von Dentz 
1939ccf74f23SLuiz Augusto von Dentz 	hci_unregister_cb(&iso_cb);
1940ccf74f23SLuiz Augusto von Dentz 
1941ccf74f23SLuiz Augusto von Dentz 	bt_sock_unregister(BTPROTO_ISO);
1942ccf74f23SLuiz Augusto von Dentz 
1943ccf74f23SLuiz Augusto von Dentz 	proto_unregister(&iso_proto);
1944ccf74f23SLuiz Augusto von Dentz 
1945ccf74f23SLuiz Augusto von Dentz 	iso_inited = false;
1946ccf74f23SLuiz Augusto von Dentz 
1947ccf74f23SLuiz Augusto von Dentz 	return 0;
1948ccf74f23SLuiz Augusto von Dentz }
1949