1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * BlueZ - Bluetooth protocol stack for Linux
4 *
5 * Copyright (C) 2022 Intel Corporation
6 * Copyright 2023-2024 NXP
7 */
8
9 #include <linux/module.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
12 #include <linux/sched/signal.h>
13
14 #include <net/bluetooth/bluetooth.h>
15 #include <net/bluetooth/hci_core.h>
16 #include <net/bluetooth/iso.h>
17 #include "eir.h"
18
19 static const struct proto_ops iso_sock_ops;
20
21 static struct bt_sock_list iso_sk_list = {
22 .lock = __RW_LOCK_UNLOCKED(iso_sk_list.lock)
23 };
24
25 /* ---- ISO connections ---- */
26 struct iso_conn {
27 struct hci_conn *hcon;
28
29 /* @lock: spinlock protecting changes to iso_conn fields */
30 spinlock_t lock;
31 struct sock *sk;
32
33 struct delayed_work timeout_work;
34
35 struct sk_buff *rx_skb;
36 __u32 rx_len;
37 __u16 tx_sn;
38 };
39
40 #define iso_conn_lock(c) spin_lock(&(c)->lock)
41 #define iso_conn_unlock(c) spin_unlock(&(c)->lock)
42
43 static void iso_sock_close(struct sock *sk);
44 static void iso_sock_kill(struct sock *sk);
45
46 /* ----- ISO socket info ----- */
47 #define iso_pi(sk) ((struct iso_pinfo *)sk)
48
49 #define EIR_SERVICE_DATA_LENGTH 4
50 #define BASE_MAX_LENGTH (HCI_MAX_PER_AD_LENGTH - EIR_SERVICE_DATA_LENGTH)
51 #define EIR_BAA_SERVICE_UUID 0x1851
52
53 /* iso_pinfo flags values */
54 enum {
55 BT_SK_BIG_SYNC,
56 BT_SK_PA_SYNC,
57 };
58
59 struct iso_pinfo {
60 struct bt_sock bt;
61 bdaddr_t src;
62 __u8 src_type;
63 bdaddr_t dst;
64 __u8 dst_type;
65 __u8 bc_sid;
66 __u8 bc_num_bis;
67 __u8 bc_bis[ISO_MAX_NUM_BIS];
68 __u16 sync_handle;
69 unsigned long flags;
70 struct bt_iso_qos qos;
71 bool qos_user_set;
72 __u8 base_len;
73 __u8 base[BASE_MAX_LENGTH];
74 struct iso_conn *conn;
75 };
76
77 static struct bt_iso_qos default_qos;
78
79 static bool check_ucast_qos(struct bt_iso_qos *qos);
80 static bool check_bcast_qos(struct bt_iso_qos *qos);
81 static bool iso_match_sid(struct sock *sk, void *data);
82 static bool iso_match_sync_handle(struct sock *sk, void *data);
83 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data);
84 static void iso_sock_disconn(struct sock *sk);
85
86 typedef bool (*iso_sock_match_t)(struct sock *sk, void *data);
87
88 static struct sock *iso_get_sock(bdaddr_t *src, bdaddr_t *dst,
89 enum bt_sock_state state,
90 iso_sock_match_t match, void *data);
91
92 /* ---- ISO timers ---- */
93 #define ISO_CONN_TIMEOUT (HZ * 40)
94 #define ISO_DISCONN_TIMEOUT (HZ * 2)
95
iso_sock_hold(struct iso_conn * conn)96 static struct sock *iso_sock_hold(struct iso_conn *conn)
97 {
98 if (!conn || !bt_sock_linked(&iso_sk_list, conn->sk))
99 return NULL;
100
101 sock_hold(conn->sk);
102
103 return conn->sk;
104 }
105
iso_sock_timeout(struct work_struct * work)106 static void iso_sock_timeout(struct work_struct *work)
107 {
108 struct iso_conn *conn = container_of(work, struct iso_conn,
109 timeout_work.work);
110 struct sock *sk;
111
112 iso_conn_lock(conn);
113 sk = iso_sock_hold(conn);
114 iso_conn_unlock(conn);
115
116 if (!sk)
117 return;
118
119 BT_DBG("sock %p state %d", sk, sk->sk_state);
120
121 lock_sock(sk);
122 sk->sk_err = ETIMEDOUT;
123 sk->sk_state_change(sk);
124 release_sock(sk);
125 sock_put(sk);
126 }
127
iso_sock_set_timer(struct sock * sk,long timeout)128 static void iso_sock_set_timer(struct sock *sk, long timeout)
129 {
130 if (!iso_pi(sk)->conn)
131 return;
132
133 BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
134 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
135 schedule_delayed_work(&iso_pi(sk)->conn->timeout_work, timeout);
136 }
137
iso_sock_clear_timer(struct sock * sk)138 static void iso_sock_clear_timer(struct sock *sk)
139 {
140 if (!iso_pi(sk)->conn)
141 return;
142
143 BT_DBG("sock %p state %d", sk, sk->sk_state);
144 cancel_delayed_work(&iso_pi(sk)->conn->timeout_work);
145 }
146
147 /* ---- ISO connections ---- */
iso_conn_add(struct hci_conn * hcon)148 static struct iso_conn *iso_conn_add(struct hci_conn *hcon)
149 {
150 struct iso_conn *conn = hcon->iso_data;
151
152 if (conn) {
153 if (!conn->hcon)
154 conn->hcon = hcon;
155 return conn;
156 }
157
158 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
159 if (!conn)
160 return NULL;
161
162 spin_lock_init(&conn->lock);
163 INIT_DELAYED_WORK(&conn->timeout_work, iso_sock_timeout);
164
165 hcon->iso_data = conn;
166 conn->hcon = hcon;
167 conn->tx_sn = 0;
168
169 BT_DBG("hcon %p conn %p", hcon, conn);
170
171 return conn;
172 }
173
174 /* Delete channel. Must be called on the locked socket. */
iso_chan_del(struct sock * sk,int err)175 static void iso_chan_del(struct sock *sk, int err)
176 {
177 struct iso_conn *conn;
178 struct sock *parent;
179
180 conn = iso_pi(sk)->conn;
181
182 BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
183
184 if (conn) {
185 iso_conn_lock(conn);
186 conn->sk = NULL;
187 iso_pi(sk)->conn = NULL;
188 iso_conn_unlock(conn);
189
190 if (conn->hcon)
191 hci_conn_drop(conn->hcon);
192 }
193
194 sk->sk_state = BT_CLOSED;
195 sk->sk_err = err;
196
197 parent = bt_sk(sk)->parent;
198 if (parent) {
199 bt_accept_unlink(sk);
200 parent->sk_data_ready(parent);
201 } else {
202 sk->sk_state_change(sk);
203 }
204
205 sock_set_flag(sk, SOCK_ZAPPED);
206 }
207
iso_conn_del(struct hci_conn * hcon,int err)208 static void iso_conn_del(struct hci_conn *hcon, int err)
209 {
210 struct iso_conn *conn = hcon->iso_data;
211 struct sock *sk;
212
213 if (!conn)
214 return;
215
216 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
217
218 /* Kill socket */
219 iso_conn_lock(conn);
220 sk = iso_sock_hold(conn);
221 iso_conn_unlock(conn);
222
223 if (sk) {
224 lock_sock(sk);
225 iso_sock_clear_timer(sk);
226 iso_chan_del(sk, err);
227 release_sock(sk);
228 sock_put(sk);
229 }
230
231 /* Ensure no more work items will run before freeing conn. */
232 cancel_delayed_work_sync(&conn->timeout_work);
233
234 hcon->iso_data = NULL;
235 kfree(conn);
236 }
237
__iso_chan_add(struct iso_conn * conn,struct sock * sk,struct sock * parent)238 static int __iso_chan_add(struct iso_conn *conn, struct sock *sk,
239 struct sock *parent)
240 {
241 BT_DBG("conn %p", conn);
242
243 if (iso_pi(sk)->conn == conn && conn->sk == sk)
244 return 0;
245
246 if (conn->sk) {
247 BT_ERR("conn->sk already set");
248 return -EBUSY;
249 }
250
251 iso_pi(sk)->conn = conn;
252 conn->sk = sk;
253
254 if (parent)
255 bt_accept_enqueue(parent, sk, true);
256
257 return 0;
258 }
259
iso_chan_add(struct iso_conn * conn,struct sock * sk,struct sock * parent)260 static int iso_chan_add(struct iso_conn *conn, struct sock *sk,
261 struct sock *parent)
262 {
263 int err;
264
265 iso_conn_lock(conn);
266 err = __iso_chan_add(conn, sk, parent);
267 iso_conn_unlock(conn);
268
269 return err;
270 }
271
le_addr_type(u8 bdaddr_type)272 static inline u8 le_addr_type(u8 bdaddr_type)
273 {
274 if (bdaddr_type == BDADDR_LE_PUBLIC)
275 return ADDR_LE_DEV_PUBLIC;
276 else
277 return ADDR_LE_DEV_RANDOM;
278 }
279
iso_connect_bis(struct sock * sk)280 static int iso_connect_bis(struct sock *sk)
281 {
282 struct iso_conn *conn;
283 struct hci_conn *hcon;
284 struct hci_dev *hdev;
285 int err;
286
287 BT_DBG("%pMR", &iso_pi(sk)->src);
288
289 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
290 iso_pi(sk)->src_type);
291 if (!hdev)
292 return -EHOSTUNREACH;
293
294 hci_dev_lock(hdev);
295
296 if (!bis_capable(hdev)) {
297 err = -EOPNOTSUPP;
298 goto unlock;
299 }
300
301 /* Fail if user set invalid QoS */
302 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
303 iso_pi(sk)->qos = default_qos;
304 err = -EINVAL;
305 goto unlock;
306 }
307
308 /* Fail if out PHYs are marked as disabled */
309 if (!iso_pi(sk)->qos.bcast.out.phy) {
310 err = -EINVAL;
311 goto unlock;
312 }
313
314 /* Just bind if DEFER_SETUP has been set */
315 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
316 hcon = hci_bind_bis(hdev, &iso_pi(sk)->dst,
317 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
318 iso_pi(sk)->base);
319 if (IS_ERR(hcon)) {
320 err = PTR_ERR(hcon);
321 goto unlock;
322 }
323 } else {
324 hcon = hci_connect_bis(hdev, &iso_pi(sk)->dst,
325 le_addr_type(iso_pi(sk)->dst_type),
326 &iso_pi(sk)->qos, iso_pi(sk)->base_len,
327 iso_pi(sk)->base);
328 if (IS_ERR(hcon)) {
329 err = PTR_ERR(hcon);
330 goto unlock;
331 }
332 }
333
334 conn = iso_conn_add(hcon);
335 if (!conn) {
336 hci_conn_drop(hcon);
337 err = -ENOMEM;
338 goto unlock;
339 }
340
341 lock_sock(sk);
342
343 err = iso_chan_add(conn, sk, NULL);
344 if (err) {
345 release_sock(sk);
346 goto unlock;
347 }
348
349 /* Update source addr of the socket */
350 bacpy(&iso_pi(sk)->src, &hcon->src);
351
352 if (hcon->state == BT_CONNECTED) {
353 iso_sock_clear_timer(sk);
354 sk->sk_state = BT_CONNECTED;
355 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
356 iso_sock_clear_timer(sk);
357 sk->sk_state = BT_CONNECT;
358 } else {
359 sk->sk_state = BT_CONNECT;
360 iso_sock_set_timer(sk, sk->sk_sndtimeo);
361 }
362
363 release_sock(sk);
364
365 unlock:
366 hci_dev_unlock(hdev);
367 hci_dev_put(hdev);
368 return err;
369 }
370
iso_connect_cis(struct sock * sk)371 static int iso_connect_cis(struct sock *sk)
372 {
373 struct iso_conn *conn;
374 struct hci_conn *hcon;
375 struct hci_dev *hdev;
376 int err;
377
378 BT_DBG("%pMR -> %pMR", &iso_pi(sk)->src, &iso_pi(sk)->dst);
379
380 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
381 iso_pi(sk)->src_type);
382 if (!hdev)
383 return -EHOSTUNREACH;
384
385 hci_dev_lock(hdev);
386
387 if (!cis_central_capable(hdev)) {
388 err = -EOPNOTSUPP;
389 goto unlock;
390 }
391
392 /* Fail if user set invalid QoS */
393 if (iso_pi(sk)->qos_user_set && !check_ucast_qos(&iso_pi(sk)->qos)) {
394 iso_pi(sk)->qos = default_qos;
395 err = -EINVAL;
396 goto unlock;
397 }
398
399 /* Fail if either PHYs are marked as disabled */
400 if (!iso_pi(sk)->qos.ucast.in.phy && !iso_pi(sk)->qos.ucast.out.phy) {
401 err = -EINVAL;
402 goto unlock;
403 }
404
405 /* Just bind if DEFER_SETUP has been set */
406 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
407 hcon = hci_bind_cis(hdev, &iso_pi(sk)->dst,
408 le_addr_type(iso_pi(sk)->dst_type),
409 &iso_pi(sk)->qos);
410 if (IS_ERR(hcon)) {
411 err = PTR_ERR(hcon);
412 goto unlock;
413 }
414 } else {
415 hcon = hci_connect_cis(hdev, &iso_pi(sk)->dst,
416 le_addr_type(iso_pi(sk)->dst_type),
417 &iso_pi(sk)->qos);
418 if (IS_ERR(hcon)) {
419 err = PTR_ERR(hcon);
420 goto unlock;
421 }
422 }
423
424 conn = iso_conn_add(hcon);
425 if (!conn) {
426 hci_conn_drop(hcon);
427 err = -ENOMEM;
428 goto unlock;
429 }
430
431 lock_sock(sk);
432
433 err = iso_chan_add(conn, sk, NULL);
434 if (err) {
435 release_sock(sk);
436 goto unlock;
437 }
438
439 /* Update source addr of the socket */
440 bacpy(&iso_pi(sk)->src, &hcon->src);
441
442 if (hcon->state == BT_CONNECTED) {
443 iso_sock_clear_timer(sk);
444 sk->sk_state = BT_CONNECTED;
445 } else if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
446 iso_sock_clear_timer(sk);
447 sk->sk_state = BT_CONNECT;
448 } else {
449 sk->sk_state = BT_CONNECT;
450 iso_sock_set_timer(sk, sk->sk_sndtimeo);
451 }
452
453 release_sock(sk);
454
455 unlock:
456 hci_dev_unlock(hdev);
457 hci_dev_put(hdev);
458 return err;
459 }
460
iso_sock_get_qos(struct sock * sk)461 static struct bt_iso_qos *iso_sock_get_qos(struct sock *sk)
462 {
463 if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONNECT2)
464 return &iso_pi(sk)->conn->hcon->iso_qos;
465
466 return &iso_pi(sk)->qos;
467 }
468
iso_send_frame(struct sock * sk,struct sk_buff * skb)469 static int iso_send_frame(struct sock *sk, struct sk_buff *skb)
470 {
471 struct iso_conn *conn = iso_pi(sk)->conn;
472 struct bt_iso_qos *qos = iso_sock_get_qos(sk);
473 struct hci_iso_data_hdr *hdr;
474 int len = 0;
475
476 BT_DBG("sk %p len %d", sk, skb->len);
477
478 if (skb->len > qos->ucast.out.sdu)
479 return -EMSGSIZE;
480
481 len = skb->len;
482
483 /* Push ISO data header */
484 hdr = skb_push(skb, HCI_ISO_DATA_HDR_SIZE);
485 hdr->sn = cpu_to_le16(conn->tx_sn++);
486 hdr->slen = cpu_to_le16(hci_iso_data_len_pack(len,
487 HCI_ISO_STATUS_VALID));
488
489 if (sk->sk_state == BT_CONNECTED)
490 hci_send_iso(conn->hcon, skb);
491 else
492 len = -ENOTCONN;
493
494 return len;
495 }
496
iso_recv_frame(struct iso_conn * conn,struct sk_buff * skb)497 static void iso_recv_frame(struct iso_conn *conn, struct sk_buff *skb)
498 {
499 struct sock *sk;
500
501 iso_conn_lock(conn);
502 sk = conn->sk;
503 iso_conn_unlock(conn);
504
505 if (!sk)
506 goto drop;
507
508 BT_DBG("sk %p len %d", sk, skb->len);
509
510 if (sk->sk_state != BT_CONNECTED)
511 goto drop;
512
513 if (!sock_queue_rcv_skb(sk, skb))
514 return;
515
516 drop:
517 kfree_skb(skb);
518 }
519
520 /* -------- Socket interface ---------- */
__iso_get_sock_listen_by_addr(bdaddr_t * src,bdaddr_t * dst)521 static struct sock *__iso_get_sock_listen_by_addr(bdaddr_t *src, bdaddr_t *dst)
522 {
523 struct sock *sk;
524
525 sk_for_each(sk, &iso_sk_list.head) {
526 if (sk->sk_state != BT_LISTEN)
527 continue;
528
529 if (bacmp(&iso_pi(sk)->dst, dst))
530 continue;
531
532 if (!bacmp(&iso_pi(sk)->src, src))
533 return sk;
534 }
535
536 return NULL;
537 }
538
__iso_get_sock_listen_by_sid(bdaddr_t * ba,bdaddr_t * bc,__u8 sid)539 static struct sock *__iso_get_sock_listen_by_sid(bdaddr_t *ba, bdaddr_t *bc,
540 __u8 sid)
541 {
542 struct sock *sk;
543
544 sk_for_each(sk, &iso_sk_list.head) {
545 if (sk->sk_state != BT_LISTEN)
546 continue;
547
548 if (bacmp(&iso_pi(sk)->src, ba))
549 continue;
550
551 if (bacmp(&iso_pi(sk)->dst, bc))
552 continue;
553
554 if (iso_pi(sk)->bc_sid == sid)
555 return sk;
556 }
557
558 return NULL;
559 }
560
561 /* Find socket in given state:
562 * source bdaddr (Unicast)
563 * destination bdaddr (Broadcast only)
564 * match func - pass NULL to ignore
565 * match func data - pass -1 to ignore
566 * Returns closest match.
567 */
iso_get_sock(bdaddr_t * src,bdaddr_t * dst,enum bt_sock_state state,iso_sock_match_t match,void * data)568 static struct sock *iso_get_sock(bdaddr_t *src, bdaddr_t *dst,
569 enum bt_sock_state state,
570 iso_sock_match_t match, void *data)
571 {
572 struct sock *sk = NULL, *sk1 = NULL;
573
574 read_lock(&iso_sk_list.lock);
575
576 sk_for_each(sk, &iso_sk_list.head) {
577 if (sk->sk_state != state)
578 continue;
579
580 /* Match Broadcast destination */
581 if (bacmp(dst, BDADDR_ANY) && bacmp(&iso_pi(sk)->dst, dst))
582 continue;
583
584 /* Use Match function if provided */
585 if (match && !match(sk, data))
586 continue;
587
588 /* Exact match. */
589 if (!bacmp(&iso_pi(sk)->src, src)) {
590 sock_hold(sk);
591 break;
592 }
593
594 /* Closest match */
595 if (!bacmp(&iso_pi(sk)->src, BDADDR_ANY)) {
596 if (sk1)
597 sock_put(sk1);
598
599 sk1 = sk;
600 sock_hold(sk1);
601 }
602 }
603
604 if (sk && sk1)
605 sock_put(sk1);
606
607 read_unlock(&iso_sk_list.lock);
608
609 return sk ? sk : sk1;
610 }
611
iso_get_sock_big(struct sock * match_sk,bdaddr_t * src,bdaddr_t * dst,uint8_t big)612 static struct sock *iso_get_sock_big(struct sock *match_sk, bdaddr_t *src,
613 bdaddr_t *dst, uint8_t big)
614 {
615 struct sock *sk = NULL;
616
617 read_lock(&iso_sk_list.lock);
618
619 sk_for_each(sk, &iso_sk_list.head) {
620 if (match_sk == sk)
621 continue;
622
623 /* Look for sockets that have already been
624 * connected to the BIG
625 */
626 if (sk->sk_state != BT_CONNECTED &&
627 sk->sk_state != BT_CONNECT)
628 continue;
629
630 /* Match Broadcast destination */
631 if (bacmp(&iso_pi(sk)->dst, dst))
632 continue;
633
634 /* Match BIG handle */
635 if (iso_pi(sk)->qos.bcast.big != big)
636 continue;
637
638 /* Match source address */
639 if (bacmp(&iso_pi(sk)->src, src))
640 continue;
641
642 sock_hold(sk);
643 break;
644 }
645
646 read_unlock(&iso_sk_list.lock);
647
648 return sk;
649 }
650
iso_sock_destruct(struct sock * sk)651 static void iso_sock_destruct(struct sock *sk)
652 {
653 BT_DBG("sk %p", sk);
654
655 skb_queue_purge(&sk->sk_receive_queue);
656 skb_queue_purge(&sk->sk_write_queue);
657 }
658
iso_sock_cleanup_listen(struct sock * parent)659 static void iso_sock_cleanup_listen(struct sock *parent)
660 {
661 struct sock *sk;
662
663 BT_DBG("parent %p", parent);
664
665 /* Close not yet accepted channels */
666 while ((sk = bt_accept_dequeue(parent, NULL))) {
667 iso_sock_close(sk);
668 iso_sock_kill(sk);
669 }
670
671 /* If listening socket has a hcon, properly disconnect it */
672 if (iso_pi(parent)->conn && iso_pi(parent)->conn->hcon) {
673 iso_sock_disconn(parent);
674 return;
675 }
676
677 parent->sk_state = BT_CLOSED;
678 sock_set_flag(parent, SOCK_ZAPPED);
679 }
680
681 /* Kill socket (only if zapped and orphan)
682 * Must be called on unlocked socket.
683 */
iso_sock_kill(struct sock * sk)684 static void iso_sock_kill(struct sock *sk)
685 {
686 if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
687 sock_flag(sk, SOCK_DEAD))
688 return;
689
690 BT_DBG("sk %p state %d", sk, sk->sk_state);
691
692 /* Kill poor orphan */
693 bt_sock_unlink(&iso_sk_list, sk);
694 sock_set_flag(sk, SOCK_DEAD);
695 sock_put(sk);
696 }
697
iso_sock_disconn(struct sock * sk)698 static void iso_sock_disconn(struct sock *sk)
699 {
700 struct sock *bis_sk;
701 struct hci_conn *hcon = iso_pi(sk)->conn->hcon;
702
703 if (test_bit(HCI_CONN_BIG_CREATED, &hcon->flags)) {
704 bis_sk = iso_get_sock_big(sk, &iso_pi(sk)->src,
705 &iso_pi(sk)->dst,
706 iso_pi(sk)->qos.bcast.big);
707
708 /* If there are any other connected sockets for the
709 * same BIG, just delete the sk and leave the bis
710 * hcon active, in case later rebinding is needed.
711 */
712 if (bis_sk) {
713 hcon->state = BT_OPEN;
714 iso_pi(sk)->conn->hcon = NULL;
715 iso_sock_clear_timer(sk);
716 iso_chan_del(sk, bt_to_errno(hcon->abort_reason));
717 sock_put(bis_sk);
718 return;
719 }
720 }
721
722 sk->sk_state = BT_DISCONN;
723 iso_sock_set_timer(sk, ISO_DISCONN_TIMEOUT);
724 iso_conn_lock(iso_pi(sk)->conn);
725 hci_conn_drop(iso_pi(sk)->conn->hcon);
726 iso_pi(sk)->conn->hcon = NULL;
727 iso_conn_unlock(iso_pi(sk)->conn);
728 }
729
__iso_sock_close(struct sock * sk)730 static void __iso_sock_close(struct sock *sk)
731 {
732 BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
733
734 switch (sk->sk_state) {
735 case BT_LISTEN:
736 iso_sock_cleanup_listen(sk);
737 break;
738
739 case BT_CONNECT:
740 case BT_CONNECTED:
741 case BT_CONFIG:
742 if (iso_pi(sk)->conn->hcon)
743 iso_sock_disconn(sk);
744 else
745 iso_chan_del(sk, ECONNRESET);
746 break;
747
748 case BT_CONNECT2:
749 if (iso_pi(sk)->conn->hcon &&
750 (test_bit(HCI_CONN_PA_SYNC, &iso_pi(sk)->conn->hcon->flags) ||
751 test_bit(HCI_CONN_PA_SYNC_FAILED, &iso_pi(sk)->conn->hcon->flags)))
752 iso_sock_disconn(sk);
753 else
754 iso_chan_del(sk, ECONNRESET);
755 break;
756 case BT_DISCONN:
757 iso_chan_del(sk, ECONNRESET);
758 break;
759
760 default:
761 sock_set_flag(sk, SOCK_ZAPPED);
762 break;
763 }
764 }
765
766 /* Must be called on unlocked socket. */
iso_sock_close(struct sock * sk)767 static void iso_sock_close(struct sock *sk)
768 {
769 iso_sock_clear_timer(sk);
770 lock_sock(sk);
771 __iso_sock_close(sk);
772 release_sock(sk);
773 iso_sock_kill(sk);
774 }
775
iso_sock_init(struct sock * sk,struct sock * parent)776 static void iso_sock_init(struct sock *sk, struct sock *parent)
777 {
778 BT_DBG("sk %p", sk);
779
780 if (parent) {
781 sk->sk_type = parent->sk_type;
782 bt_sk(sk)->flags = bt_sk(parent)->flags;
783 security_sk_clone(parent, sk);
784 }
785 }
786
787 static struct proto iso_proto = {
788 .name = "ISO",
789 .owner = THIS_MODULE,
790 .obj_size = sizeof(struct iso_pinfo)
791 };
792
793 #define DEFAULT_IO_QOS \
794 { \
795 .interval = 10000u, \
796 .latency = 10u, \
797 .sdu = 40u, \
798 .phy = BT_ISO_PHY_2M, \
799 .rtn = 2u, \
800 }
801
802 static struct bt_iso_qos default_qos = {
803 .bcast = {
804 .big = BT_ISO_QOS_BIG_UNSET,
805 .bis = BT_ISO_QOS_BIS_UNSET,
806 .sync_factor = 0x01,
807 .packing = 0x00,
808 .framing = 0x00,
809 .in = DEFAULT_IO_QOS,
810 .out = DEFAULT_IO_QOS,
811 .encryption = 0x00,
812 .bcode = {0x00},
813 .options = 0x00,
814 .skip = 0x0000,
815 .sync_timeout = BT_ISO_SYNC_TIMEOUT,
816 .sync_cte_type = 0x00,
817 .mse = 0x00,
818 .timeout = BT_ISO_SYNC_TIMEOUT,
819 },
820 };
821
iso_sock_alloc(struct net * net,struct socket * sock,int proto,gfp_t prio,int kern)822 static struct sock *iso_sock_alloc(struct net *net, struct socket *sock,
823 int proto, gfp_t prio, int kern)
824 {
825 struct sock *sk;
826
827 sk = bt_sock_alloc(net, sock, &iso_proto, proto, prio, kern);
828 if (!sk)
829 return NULL;
830
831 sk->sk_destruct = iso_sock_destruct;
832 sk->sk_sndtimeo = ISO_CONN_TIMEOUT;
833
834 /* Set address type as public as default src address is BDADDR_ANY */
835 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
836
837 iso_pi(sk)->qos = default_qos;
838 iso_pi(sk)->sync_handle = -1;
839
840 bt_sock_link(&iso_sk_list, sk);
841 return sk;
842 }
843
iso_sock_create(struct net * net,struct socket * sock,int protocol,int kern)844 static int iso_sock_create(struct net *net, struct socket *sock, int protocol,
845 int kern)
846 {
847 struct sock *sk;
848
849 BT_DBG("sock %p", sock);
850
851 sock->state = SS_UNCONNECTED;
852
853 if (sock->type != SOCK_SEQPACKET)
854 return -ESOCKTNOSUPPORT;
855
856 sock->ops = &iso_sock_ops;
857
858 sk = iso_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
859 if (!sk)
860 return -ENOMEM;
861
862 iso_sock_init(sk, NULL);
863 return 0;
864 }
865
iso_sock_bind_bc(struct socket * sock,struct sockaddr * addr,int addr_len)866 static int iso_sock_bind_bc(struct socket *sock, struct sockaddr *addr,
867 int addr_len)
868 {
869 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
870 struct sock *sk = sock->sk;
871 int i;
872
873 BT_DBG("sk %p bc_sid %u bc_num_bis %u", sk, sa->iso_bc->bc_sid,
874 sa->iso_bc->bc_num_bis);
875
876 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc))
877 return -EINVAL;
878
879 bacpy(&iso_pi(sk)->dst, &sa->iso_bc->bc_bdaddr);
880
881 /* Check if the address type is of LE type */
882 if (!bdaddr_type_is_le(sa->iso_bc->bc_bdaddr_type))
883 return -EINVAL;
884
885 iso_pi(sk)->dst_type = sa->iso_bc->bc_bdaddr_type;
886
887 if (sa->iso_bc->bc_sid > 0x0f)
888 return -EINVAL;
889
890 iso_pi(sk)->bc_sid = sa->iso_bc->bc_sid;
891
892 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS)
893 return -EINVAL;
894
895 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
896
897 for (i = 0; i < iso_pi(sk)->bc_num_bis; i++)
898 if (sa->iso_bc->bc_bis[i] < 0x01 ||
899 sa->iso_bc->bc_bis[i] > 0x1f)
900 return -EINVAL;
901
902 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
903 iso_pi(sk)->bc_num_bis);
904
905 return 0;
906 }
907
iso_sock_bind_pa_sk(struct sock * sk,struct sockaddr_iso * sa,int addr_len)908 static int iso_sock_bind_pa_sk(struct sock *sk, struct sockaddr_iso *sa,
909 int addr_len)
910 {
911 int err = 0;
912
913 if (sk->sk_type != SOCK_SEQPACKET) {
914 err = -EINVAL;
915 goto done;
916 }
917
918 if (addr_len != sizeof(*sa) + sizeof(*sa->iso_bc)) {
919 err = -EINVAL;
920 goto done;
921 }
922
923 if (sa->iso_bc->bc_num_bis > ISO_MAX_NUM_BIS) {
924 err = -EINVAL;
925 goto done;
926 }
927
928 iso_pi(sk)->bc_num_bis = sa->iso_bc->bc_num_bis;
929
930 for (int i = 0; i < iso_pi(sk)->bc_num_bis; i++)
931 if (sa->iso_bc->bc_bis[i] < 0x01 ||
932 sa->iso_bc->bc_bis[i] > 0x1f) {
933 err = -EINVAL;
934 goto done;
935 }
936
937 memcpy(iso_pi(sk)->bc_bis, sa->iso_bc->bc_bis,
938 iso_pi(sk)->bc_num_bis);
939
940 done:
941 return err;
942 }
943
iso_sock_bind(struct socket * sock,struct sockaddr * addr,int addr_len)944 static int iso_sock_bind(struct socket *sock, struct sockaddr *addr,
945 int addr_len)
946 {
947 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
948 struct sock *sk = sock->sk;
949 int err = 0;
950
951 BT_DBG("sk %p %pMR type %u", sk, &sa->iso_bdaddr, sa->iso_bdaddr_type);
952
953 if (!addr || addr_len < sizeof(struct sockaddr_iso) ||
954 addr->sa_family != AF_BLUETOOTH)
955 return -EINVAL;
956
957 lock_sock(sk);
958
959 /* Allow the user to bind a PA sync socket to a number
960 * of BISes to sync to.
961 */
962 if ((sk->sk_state == BT_CONNECT2 ||
963 sk->sk_state == BT_CONNECTED) &&
964 test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) {
965 err = iso_sock_bind_pa_sk(sk, sa, addr_len);
966 goto done;
967 }
968
969 if (sk->sk_state != BT_OPEN) {
970 err = -EBADFD;
971 goto done;
972 }
973
974 if (sk->sk_type != SOCK_SEQPACKET) {
975 err = -EINVAL;
976 goto done;
977 }
978
979 /* Check if the address type is of LE type */
980 if (!bdaddr_type_is_le(sa->iso_bdaddr_type)) {
981 err = -EINVAL;
982 goto done;
983 }
984
985 bacpy(&iso_pi(sk)->src, &sa->iso_bdaddr);
986 iso_pi(sk)->src_type = sa->iso_bdaddr_type;
987
988 /* Check for Broadcast address */
989 if (addr_len > sizeof(*sa)) {
990 err = iso_sock_bind_bc(sock, addr, addr_len);
991 if (err)
992 goto done;
993 }
994
995 sk->sk_state = BT_BOUND;
996
997 done:
998 release_sock(sk);
999 return err;
1000 }
1001
iso_sock_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)1002 static int iso_sock_connect(struct socket *sock, struct sockaddr *addr,
1003 int alen, int flags)
1004 {
1005 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1006 struct sock *sk = sock->sk;
1007 int err;
1008
1009 BT_DBG("sk %p", sk);
1010
1011 if (alen < sizeof(struct sockaddr_iso) ||
1012 addr->sa_family != AF_BLUETOOTH)
1013 return -EINVAL;
1014
1015 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
1016 return -EBADFD;
1017
1018 if (sk->sk_type != SOCK_SEQPACKET)
1019 return -EINVAL;
1020
1021 /* Check if the address type is of LE type */
1022 if (!bdaddr_type_is_le(sa->iso_bdaddr_type))
1023 return -EINVAL;
1024
1025 lock_sock(sk);
1026
1027 bacpy(&iso_pi(sk)->dst, &sa->iso_bdaddr);
1028 iso_pi(sk)->dst_type = sa->iso_bdaddr_type;
1029
1030 release_sock(sk);
1031
1032 if (bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
1033 err = iso_connect_cis(sk);
1034 else
1035 err = iso_connect_bis(sk);
1036
1037 if (err)
1038 return err;
1039
1040 lock_sock(sk);
1041
1042 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1043 err = bt_sock_wait_state(sk, BT_CONNECTED,
1044 sock_sndtimeo(sk, flags & O_NONBLOCK));
1045 }
1046
1047 release_sock(sk);
1048 return err;
1049 }
1050
iso_listen_bis(struct sock * sk)1051 static int iso_listen_bis(struct sock *sk)
1052 {
1053 struct hci_dev *hdev;
1054 int err = 0;
1055 struct iso_conn *conn;
1056 struct hci_conn *hcon;
1057
1058 BT_DBG("%pMR -> %pMR (SID 0x%2.2x)", &iso_pi(sk)->src,
1059 &iso_pi(sk)->dst, iso_pi(sk)->bc_sid);
1060
1061 write_lock(&iso_sk_list.lock);
1062
1063 if (__iso_get_sock_listen_by_sid(&iso_pi(sk)->src, &iso_pi(sk)->dst,
1064 iso_pi(sk)->bc_sid))
1065 err = -EADDRINUSE;
1066
1067 write_unlock(&iso_sk_list.lock);
1068
1069 if (err)
1070 return err;
1071
1072 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
1073 iso_pi(sk)->src_type);
1074 if (!hdev)
1075 return -EHOSTUNREACH;
1076
1077 hci_dev_lock(hdev);
1078
1079 /* Fail if user set invalid QoS */
1080 if (iso_pi(sk)->qos_user_set && !check_bcast_qos(&iso_pi(sk)->qos)) {
1081 iso_pi(sk)->qos = default_qos;
1082 err = -EINVAL;
1083 goto unlock;
1084 }
1085
1086 hcon = hci_pa_create_sync(hdev, &iso_pi(sk)->dst,
1087 le_addr_type(iso_pi(sk)->dst_type),
1088 iso_pi(sk)->bc_sid, &iso_pi(sk)->qos);
1089 if (IS_ERR(hcon)) {
1090 err = PTR_ERR(hcon);
1091 goto unlock;
1092 }
1093
1094 conn = iso_conn_add(hcon);
1095 if (!conn) {
1096 hci_conn_drop(hcon);
1097 err = -ENOMEM;
1098 goto unlock;
1099 }
1100
1101 err = iso_chan_add(conn, sk, NULL);
1102 if (err) {
1103 hci_conn_drop(hcon);
1104 goto unlock;
1105 }
1106
1107 hci_dev_put(hdev);
1108
1109 unlock:
1110 hci_dev_unlock(hdev);
1111 return err;
1112 }
1113
iso_listen_cis(struct sock * sk)1114 static int iso_listen_cis(struct sock *sk)
1115 {
1116 int err = 0;
1117
1118 BT_DBG("%pMR", &iso_pi(sk)->src);
1119
1120 write_lock(&iso_sk_list.lock);
1121
1122 if (__iso_get_sock_listen_by_addr(&iso_pi(sk)->src, &iso_pi(sk)->dst))
1123 err = -EADDRINUSE;
1124
1125 write_unlock(&iso_sk_list.lock);
1126
1127 return err;
1128 }
1129
iso_sock_listen(struct socket * sock,int backlog)1130 static int iso_sock_listen(struct socket *sock, int backlog)
1131 {
1132 struct sock *sk = sock->sk;
1133 int err = 0;
1134
1135 BT_DBG("sk %p backlog %d", sk, backlog);
1136
1137 lock_sock(sk);
1138
1139 if (sk->sk_state != BT_BOUND) {
1140 err = -EBADFD;
1141 goto done;
1142 }
1143
1144 if (sk->sk_type != SOCK_SEQPACKET) {
1145 err = -EINVAL;
1146 goto done;
1147 }
1148
1149 if (!bacmp(&iso_pi(sk)->dst, BDADDR_ANY))
1150 err = iso_listen_cis(sk);
1151 else
1152 err = iso_listen_bis(sk);
1153
1154 if (err)
1155 goto done;
1156
1157 sk->sk_max_ack_backlog = backlog;
1158 sk->sk_ack_backlog = 0;
1159
1160 sk->sk_state = BT_LISTEN;
1161
1162 done:
1163 release_sock(sk);
1164 return err;
1165 }
1166
iso_sock_accept(struct socket * sock,struct socket * newsock,struct proto_accept_arg * arg)1167 static int iso_sock_accept(struct socket *sock, struct socket *newsock,
1168 struct proto_accept_arg *arg)
1169 {
1170 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1171 struct sock *sk = sock->sk, *ch;
1172 long timeo;
1173 int err = 0;
1174
1175 lock_sock(sk);
1176
1177 timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK);
1178
1179 BT_DBG("sk %p timeo %ld", sk, timeo);
1180
1181 /* Wait for an incoming connection. (wake-one). */
1182 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1183 while (1) {
1184 if (sk->sk_state != BT_LISTEN) {
1185 err = -EBADFD;
1186 break;
1187 }
1188
1189 ch = bt_accept_dequeue(sk, newsock);
1190 if (ch)
1191 break;
1192
1193 if (!timeo) {
1194 err = -EAGAIN;
1195 break;
1196 }
1197
1198 if (signal_pending(current)) {
1199 err = sock_intr_errno(timeo);
1200 break;
1201 }
1202
1203 release_sock(sk);
1204
1205 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1206 lock_sock(sk);
1207 }
1208 remove_wait_queue(sk_sleep(sk), &wait);
1209
1210 if (err)
1211 goto done;
1212
1213 newsock->state = SS_CONNECTED;
1214
1215 BT_DBG("new socket %p", ch);
1216
1217 done:
1218 release_sock(sk);
1219 return err;
1220 }
1221
iso_sock_getname(struct socket * sock,struct sockaddr * addr,int peer)1222 static int iso_sock_getname(struct socket *sock, struct sockaddr *addr,
1223 int peer)
1224 {
1225 struct sockaddr_iso *sa = (struct sockaddr_iso *)addr;
1226 struct sock *sk = sock->sk;
1227
1228 BT_DBG("sock %p, sk %p", sock, sk);
1229
1230 addr->sa_family = AF_BLUETOOTH;
1231
1232 if (peer) {
1233 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->dst);
1234 sa->iso_bdaddr_type = iso_pi(sk)->dst_type;
1235 } else {
1236 bacpy(&sa->iso_bdaddr, &iso_pi(sk)->src);
1237 sa->iso_bdaddr_type = iso_pi(sk)->src_type;
1238 }
1239
1240 return sizeof(struct sockaddr_iso);
1241 }
1242
iso_sock_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)1243 static int iso_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1244 size_t len)
1245 {
1246 struct sock *sk = sock->sk;
1247 struct sk_buff *skb, **frag;
1248 size_t mtu;
1249 int err;
1250
1251 BT_DBG("sock %p, sk %p", sock, sk);
1252
1253 err = sock_error(sk);
1254 if (err)
1255 return err;
1256
1257 if (msg->msg_flags & MSG_OOB)
1258 return -EOPNOTSUPP;
1259
1260 lock_sock(sk);
1261
1262 if (sk->sk_state != BT_CONNECTED) {
1263 release_sock(sk);
1264 return -ENOTCONN;
1265 }
1266
1267 mtu = iso_pi(sk)->conn->hcon->mtu;
1268
1269 release_sock(sk);
1270
1271 skb = bt_skb_sendmsg(sk, msg, len, mtu, HCI_ISO_DATA_HDR_SIZE, 0);
1272 if (IS_ERR(skb))
1273 return PTR_ERR(skb);
1274
1275 len -= skb->len;
1276
1277 BT_DBG("skb %p len %d", sk, skb->len);
1278
1279 /* Continuation fragments */
1280 frag = &skb_shinfo(skb)->frag_list;
1281 while (len) {
1282 struct sk_buff *tmp;
1283
1284 tmp = bt_skb_sendmsg(sk, msg, len, mtu, 0, 0);
1285 if (IS_ERR(tmp)) {
1286 kfree_skb(skb);
1287 return PTR_ERR(tmp);
1288 }
1289
1290 *frag = tmp;
1291
1292 len -= tmp->len;
1293
1294 skb->len += tmp->len;
1295 skb->data_len += tmp->len;
1296
1297 BT_DBG("frag %p len %d", *frag, tmp->len);
1298
1299 frag = &(*frag)->next;
1300 }
1301
1302 lock_sock(sk);
1303
1304 if (sk->sk_state == BT_CONNECTED)
1305 err = iso_send_frame(sk, skb);
1306 else
1307 err = -ENOTCONN;
1308
1309 release_sock(sk);
1310
1311 if (err < 0)
1312 kfree_skb(skb);
1313 return err;
1314 }
1315
iso_conn_defer_accept(struct hci_conn * conn)1316 static void iso_conn_defer_accept(struct hci_conn *conn)
1317 {
1318 struct hci_cp_le_accept_cis cp;
1319 struct hci_dev *hdev = conn->hdev;
1320
1321 BT_DBG("conn %p", conn);
1322
1323 conn->state = BT_CONFIG;
1324
1325 cp.handle = cpu_to_le16(conn->handle);
1326
1327 hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
1328 }
1329
iso_conn_big_sync(struct sock * sk)1330 static void iso_conn_big_sync(struct sock *sk)
1331 {
1332 int err;
1333 struct hci_dev *hdev;
1334
1335 hdev = hci_get_route(&iso_pi(sk)->dst, &iso_pi(sk)->src,
1336 iso_pi(sk)->src_type);
1337
1338 if (!hdev)
1339 return;
1340
1341 if (!test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) {
1342 err = hci_le_big_create_sync(hdev, iso_pi(sk)->conn->hcon,
1343 &iso_pi(sk)->qos,
1344 iso_pi(sk)->sync_handle,
1345 iso_pi(sk)->bc_num_bis,
1346 iso_pi(sk)->bc_bis);
1347 if (err)
1348 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1349 err);
1350 }
1351 }
1352
iso_sock_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)1353 static int iso_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1354 size_t len, int flags)
1355 {
1356 struct sock *sk = sock->sk;
1357 struct iso_pinfo *pi = iso_pi(sk);
1358
1359 BT_DBG("sk %p", sk);
1360
1361 if (test_and_clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
1362 lock_sock(sk);
1363 switch (sk->sk_state) {
1364 case BT_CONNECT2:
1365 if (test_bit(BT_SK_PA_SYNC, &pi->flags)) {
1366 iso_conn_big_sync(sk);
1367 sk->sk_state = BT_LISTEN;
1368 } else {
1369 iso_conn_defer_accept(pi->conn->hcon);
1370 sk->sk_state = BT_CONFIG;
1371 }
1372 release_sock(sk);
1373 return 0;
1374 case BT_CONNECTED:
1375 if (test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags)) {
1376 iso_conn_big_sync(sk);
1377 sk->sk_state = BT_LISTEN;
1378 release_sock(sk);
1379 return 0;
1380 }
1381
1382 release_sock(sk);
1383 break;
1384 case BT_CONNECT:
1385 release_sock(sk);
1386 return iso_connect_cis(sk);
1387 default:
1388 release_sock(sk);
1389 break;
1390 }
1391 }
1392
1393 return bt_sock_recvmsg(sock, msg, len, flags);
1394 }
1395
check_io_qos(struct bt_iso_io_qos * qos)1396 static bool check_io_qos(struct bt_iso_io_qos *qos)
1397 {
1398 /* If no PHY is enable SDU must be 0 */
1399 if (!qos->phy && qos->sdu)
1400 return false;
1401
1402 if (qos->interval && (qos->interval < 0xff || qos->interval > 0xfffff))
1403 return false;
1404
1405 if (qos->latency && (qos->latency < 0x05 || qos->latency > 0xfa0))
1406 return false;
1407
1408 if (qos->phy > BT_ISO_PHY_ANY)
1409 return false;
1410
1411 return true;
1412 }
1413
check_ucast_qos(struct bt_iso_qos * qos)1414 static bool check_ucast_qos(struct bt_iso_qos *qos)
1415 {
1416 if (qos->ucast.cig > 0xef && qos->ucast.cig != BT_ISO_QOS_CIG_UNSET)
1417 return false;
1418
1419 if (qos->ucast.cis > 0xef && qos->ucast.cis != BT_ISO_QOS_CIS_UNSET)
1420 return false;
1421
1422 if (qos->ucast.sca > 0x07)
1423 return false;
1424
1425 if (qos->ucast.packing > 0x01)
1426 return false;
1427
1428 if (qos->ucast.framing > 0x01)
1429 return false;
1430
1431 if (!check_io_qos(&qos->ucast.in))
1432 return false;
1433
1434 if (!check_io_qos(&qos->ucast.out))
1435 return false;
1436
1437 return true;
1438 }
1439
check_bcast_qos(struct bt_iso_qos * qos)1440 static bool check_bcast_qos(struct bt_iso_qos *qos)
1441 {
1442 if (!qos->bcast.sync_factor)
1443 qos->bcast.sync_factor = 0x01;
1444
1445 if (qos->bcast.packing > 0x01)
1446 return false;
1447
1448 if (qos->bcast.framing > 0x01)
1449 return false;
1450
1451 if (!check_io_qos(&qos->bcast.in))
1452 return false;
1453
1454 if (!check_io_qos(&qos->bcast.out))
1455 return false;
1456
1457 if (qos->bcast.encryption > 0x01)
1458 return false;
1459
1460 if (qos->bcast.options > 0x07)
1461 return false;
1462
1463 if (qos->bcast.skip > 0x01f3)
1464 return false;
1465
1466 if (!qos->bcast.sync_timeout)
1467 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT;
1468
1469 if (qos->bcast.sync_timeout < 0x000a || qos->bcast.sync_timeout > 0x4000)
1470 return false;
1471
1472 if (qos->bcast.sync_cte_type > 0x1f)
1473 return false;
1474
1475 if (qos->bcast.mse > 0x1f)
1476 return false;
1477
1478 if (!qos->bcast.timeout)
1479 qos->bcast.sync_timeout = BT_ISO_SYNC_TIMEOUT;
1480
1481 if (qos->bcast.timeout < 0x000a || qos->bcast.timeout > 0x4000)
1482 return false;
1483
1484 return true;
1485 }
1486
iso_sock_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)1487 static int iso_sock_setsockopt(struct socket *sock, int level, int optname,
1488 sockptr_t optval, unsigned int optlen)
1489 {
1490 struct sock *sk = sock->sk;
1491 int err = 0;
1492 struct bt_iso_qos qos = default_qos;
1493 u32 opt;
1494
1495 BT_DBG("sk %p", sk);
1496
1497 lock_sock(sk);
1498
1499 switch (optname) {
1500 case BT_DEFER_SETUP:
1501 if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
1502 err = -EINVAL;
1503 break;
1504 }
1505
1506 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen);
1507 if (err)
1508 break;
1509
1510 if (opt)
1511 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1512 else
1513 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
1514 break;
1515
1516 case BT_PKT_STATUS:
1517 err = bt_copy_from_sockptr(&opt, sizeof(opt), optval, optlen);
1518 if (err)
1519 break;
1520
1521 if (opt)
1522 set_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1523 else
1524 clear_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags);
1525 break;
1526
1527 case BT_ISO_QOS:
1528 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1529 sk->sk_state != BT_CONNECT2 &&
1530 (!test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags) ||
1531 sk->sk_state != BT_CONNECTED)) {
1532 err = -EINVAL;
1533 break;
1534 }
1535
1536 err = bt_copy_from_sockptr(&qos, sizeof(qos), optval, optlen);
1537 if (err)
1538 break;
1539
1540 iso_pi(sk)->qos = qos;
1541 iso_pi(sk)->qos_user_set = true;
1542
1543 break;
1544
1545 case BT_ISO_BASE:
1546 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
1547 sk->sk_state != BT_CONNECT2) {
1548 err = -EINVAL;
1549 break;
1550 }
1551
1552 if (optlen > sizeof(iso_pi(sk)->base)) {
1553 err = -EINVAL;
1554 break;
1555 }
1556
1557 err = bt_copy_from_sockptr(iso_pi(sk)->base, optlen, optval,
1558 optlen);
1559 if (err)
1560 break;
1561
1562 iso_pi(sk)->base_len = optlen;
1563
1564 break;
1565
1566 default:
1567 err = -ENOPROTOOPT;
1568 break;
1569 }
1570
1571 release_sock(sk);
1572 return err;
1573 }
1574
iso_sock_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)1575 static int iso_sock_getsockopt(struct socket *sock, int level, int optname,
1576 char __user *optval, int __user *optlen)
1577 {
1578 struct sock *sk = sock->sk;
1579 int len, err = 0;
1580 struct bt_iso_qos *qos;
1581 u8 base_len;
1582 u8 *base;
1583
1584 BT_DBG("sk %p", sk);
1585
1586 if (get_user(len, optlen))
1587 return -EFAULT;
1588
1589 lock_sock(sk);
1590
1591 switch (optname) {
1592 case BT_DEFER_SETUP:
1593 if (sk->sk_state == BT_CONNECTED) {
1594 err = -EINVAL;
1595 break;
1596 }
1597
1598 if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
1599 (u32 __user *)optval))
1600 err = -EFAULT;
1601
1602 break;
1603
1604 case BT_PKT_STATUS:
1605 if (put_user(test_bit(BT_SK_PKT_STATUS, &bt_sk(sk)->flags),
1606 (int __user *)optval))
1607 err = -EFAULT;
1608 break;
1609
1610 case BT_ISO_QOS:
1611 qos = iso_sock_get_qos(sk);
1612
1613 len = min_t(unsigned int, len, sizeof(*qos));
1614 if (copy_to_user(optval, qos, len))
1615 err = -EFAULT;
1616
1617 break;
1618
1619 case BT_ISO_BASE:
1620 if (sk->sk_state == BT_CONNECTED &&
1621 !bacmp(&iso_pi(sk)->dst, BDADDR_ANY)) {
1622 base_len = iso_pi(sk)->conn->hcon->le_per_adv_data_len;
1623 base = iso_pi(sk)->conn->hcon->le_per_adv_data;
1624 } else {
1625 base_len = iso_pi(sk)->base_len;
1626 base = iso_pi(sk)->base;
1627 }
1628
1629 len = min_t(unsigned int, len, base_len);
1630 if (copy_to_user(optval, base, len))
1631 err = -EFAULT;
1632 if (put_user(len, optlen))
1633 err = -EFAULT;
1634
1635 break;
1636
1637 default:
1638 err = -ENOPROTOOPT;
1639 break;
1640 }
1641
1642 release_sock(sk);
1643 return err;
1644 }
1645
iso_sock_shutdown(struct socket * sock,int how)1646 static int iso_sock_shutdown(struct socket *sock, int how)
1647 {
1648 struct sock *sk = sock->sk;
1649 int err = 0;
1650
1651 BT_DBG("sock %p, sk %p, how %d", sock, sk, how);
1652
1653 if (!sk)
1654 return 0;
1655
1656 sock_hold(sk);
1657 lock_sock(sk);
1658
1659 switch (how) {
1660 case SHUT_RD:
1661 if (sk->sk_shutdown & RCV_SHUTDOWN)
1662 goto unlock;
1663 sk->sk_shutdown |= RCV_SHUTDOWN;
1664 break;
1665 case SHUT_WR:
1666 if (sk->sk_shutdown & SEND_SHUTDOWN)
1667 goto unlock;
1668 sk->sk_shutdown |= SEND_SHUTDOWN;
1669 break;
1670 case SHUT_RDWR:
1671 if (sk->sk_shutdown & SHUTDOWN_MASK)
1672 goto unlock;
1673 sk->sk_shutdown |= SHUTDOWN_MASK;
1674 break;
1675 }
1676
1677 iso_sock_clear_timer(sk);
1678 __iso_sock_close(sk);
1679
1680 if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
1681 !(current->flags & PF_EXITING))
1682 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1683
1684 unlock:
1685 release_sock(sk);
1686 sock_put(sk);
1687
1688 return err;
1689 }
1690
iso_sock_release(struct socket * sock)1691 static int iso_sock_release(struct socket *sock)
1692 {
1693 struct sock *sk = sock->sk;
1694 int err = 0;
1695
1696 BT_DBG("sock %p, sk %p", sock, sk);
1697
1698 if (!sk)
1699 return 0;
1700
1701 iso_sock_close(sk);
1702
1703 if (sock_flag(sk, SOCK_LINGER) && READ_ONCE(sk->sk_lingertime) &&
1704 !(current->flags & PF_EXITING)) {
1705 lock_sock(sk);
1706 err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
1707 release_sock(sk);
1708 }
1709
1710 sock_orphan(sk);
1711 iso_sock_kill(sk);
1712 return err;
1713 }
1714
iso_sock_ready(struct sock * sk)1715 static void iso_sock_ready(struct sock *sk)
1716 {
1717 BT_DBG("sk %p", sk);
1718
1719 if (!sk)
1720 return;
1721
1722 lock_sock(sk);
1723 iso_sock_clear_timer(sk);
1724 sk->sk_state = BT_CONNECTED;
1725 sk->sk_state_change(sk);
1726 release_sock(sk);
1727 }
1728
iso_match_big(struct sock * sk,void * data)1729 static bool iso_match_big(struct sock *sk, void *data)
1730 {
1731 struct hci_evt_le_big_sync_estabilished *ev = data;
1732
1733 return ev->handle == iso_pi(sk)->qos.bcast.big;
1734 }
1735
iso_match_pa_sync_flag(struct sock * sk,void * data)1736 static bool iso_match_pa_sync_flag(struct sock *sk, void *data)
1737 {
1738 return test_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1739 }
1740
iso_conn_ready(struct iso_conn * conn)1741 static void iso_conn_ready(struct iso_conn *conn)
1742 {
1743 struct sock *parent = NULL;
1744 struct sock *sk = conn->sk;
1745 struct hci_ev_le_big_sync_estabilished *ev = NULL;
1746 struct hci_ev_le_pa_sync_established *ev2 = NULL;
1747 struct hci_ev_le_per_adv_report *ev3 = NULL;
1748 struct hci_conn *hcon;
1749
1750 BT_DBG("conn %p", conn);
1751
1752 if (sk) {
1753 iso_sock_ready(conn->sk);
1754 } else {
1755 hcon = conn->hcon;
1756 if (!hcon)
1757 return;
1758
1759 if (test_bit(HCI_CONN_BIG_SYNC, &hcon->flags) ||
1760 test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags)) {
1761 ev = hci_recv_event_data(hcon->hdev,
1762 HCI_EVT_LE_BIG_SYNC_ESTABILISHED);
1763
1764 /* Get reference to PA sync parent socket, if it exists */
1765 parent = iso_get_sock(&hcon->src, &hcon->dst,
1766 BT_LISTEN,
1767 iso_match_pa_sync_flag,
1768 NULL);
1769 if (!parent && ev)
1770 parent = iso_get_sock(&hcon->src,
1771 &hcon->dst,
1772 BT_LISTEN,
1773 iso_match_big, ev);
1774 } else if (test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
1775 ev2 = hci_recv_event_data(hcon->hdev,
1776 HCI_EV_LE_PA_SYNC_ESTABLISHED);
1777 if (ev2)
1778 parent = iso_get_sock(&hcon->src,
1779 &hcon->dst,
1780 BT_LISTEN,
1781 iso_match_sid, ev2);
1782 } else if (test_bit(HCI_CONN_PA_SYNC, &hcon->flags)) {
1783 ev3 = hci_recv_event_data(hcon->hdev,
1784 HCI_EV_LE_PER_ADV_REPORT);
1785 if (ev3)
1786 parent = iso_get_sock(&hcon->src,
1787 &hcon->dst,
1788 BT_LISTEN,
1789 iso_match_sync_handle_pa_report,
1790 ev3);
1791 }
1792
1793 if (!parent)
1794 parent = iso_get_sock(&hcon->src, BDADDR_ANY,
1795 BT_LISTEN, NULL, NULL);
1796
1797 if (!parent)
1798 return;
1799
1800 lock_sock(parent);
1801
1802 sk = iso_sock_alloc(sock_net(parent), NULL,
1803 BTPROTO_ISO, GFP_ATOMIC, 0);
1804 if (!sk) {
1805 release_sock(parent);
1806 return;
1807 }
1808
1809 iso_sock_init(sk, parent);
1810
1811 bacpy(&iso_pi(sk)->src, &hcon->src);
1812
1813 /* Convert from HCI to three-value type */
1814 if (hcon->src_type == ADDR_LE_DEV_PUBLIC)
1815 iso_pi(sk)->src_type = BDADDR_LE_PUBLIC;
1816 else
1817 iso_pi(sk)->src_type = BDADDR_LE_RANDOM;
1818
1819 /* If hcon has no destination address (BDADDR_ANY) it means it
1820 * was created by HCI_EV_LE_BIG_SYNC_ESTABILISHED or
1821 * HCI_EV_LE_PA_SYNC_ESTABLISHED so we need to initialize using
1822 * the parent socket destination address.
1823 */
1824 if (!bacmp(&hcon->dst, BDADDR_ANY)) {
1825 bacpy(&hcon->dst, &iso_pi(parent)->dst);
1826 hcon->dst_type = iso_pi(parent)->dst_type;
1827 hcon->sync_handle = iso_pi(parent)->sync_handle;
1828 }
1829
1830 if (ev3) {
1831 iso_pi(sk)->qos = iso_pi(parent)->qos;
1832 hcon->iso_qos = iso_pi(sk)->qos;
1833 iso_pi(sk)->bc_num_bis = iso_pi(parent)->bc_num_bis;
1834 memcpy(iso_pi(sk)->bc_bis, iso_pi(parent)->bc_bis, ISO_MAX_NUM_BIS);
1835 set_bit(BT_SK_PA_SYNC, &iso_pi(sk)->flags);
1836 }
1837
1838 bacpy(&iso_pi(sk)->dst, &hcon->dst);
1839 iso_pi(sk)->dst_type = hcon->dst_type;
1840 iso_pi(sk)->sync_handle = iso_pi(parent)->sync_handle;
1841 memcpy(iso_pi(sk)->base, iso_pi(parent)->base, iso_pi(parent)->base_len);
1842 iso_pi(sk)->base_len = iso_pi(parent)->base_len;
1843
1844 hci_conn_hold(hcon);
1845 iso_chan_add(conn, sk, parent);
1846
1847 if ((ev && ((struct hci_evt_le_big_sync_estabilished *)ev)->status) ||
1848 (ev2 && ev2->status)) {
1849 /* Trigger error signal on child socket */
1850 sk->sk_err = ECONNREFUSED;
1851 sk->sk_error_report(sk);
1852 }
1853
1854 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
1855 sk->sk_state = BT_CONNECT2;
1856 else
1857 sk->sk_state = BT_CONNECTED;
1858
1859 /* Wake up parent */
1860 parent->sk_data_ready(parent);
1861
1862 release_sock(parent);
1863 sock_put(parent);
1864 }
1865 }
1866
iso_match_sid(struct sock * sk,void * data)1867 static bool iso_match_sid(struct sock *sk, void *data)
1868 {
1869 struct hci_ev_le_pa_sync_established *ev = data;
1870
1871 return ev->sid == iso_pi(sk)->bc_sid;
1872 }
1873
iso_match_sync_handle(struct sock * sk,void * data)1874 static bool iso_match_sync_handle(struct sock *sk, void *data)
1875 {
1876 struct hci_evt_le_big_info_adv_report *ev = data;
1877
1878 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1879 }
1880
iso_match_sync_handle_pa_report(struct sock * sk,void * data)1881 static bool iso_match_sync_handle_pa_report(struct sock *sk, void *data)
1882 {
1883 struct hci_ev_le_per_adv_report *ev = data;
1884
1885 return le16_to_cpu(ev->sync_handle) == iso_pi(sk)->sync_handle;
1886 }
1887
1888 /* ----- ISO interface with lower layer (HCI) ----- */
1889
iso_connect_ind(struct hci_dev * hdev,bdaddr_t * bdaddr,__u8 * flags)1890 int iso_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1891 {
1892 struct hci_ev_le_pa_sync_established *ev1;
1893 struct hci_evt_le_big_info_adv_report *ev2;
1894 struct hci_ev_le_per_adv_report *ev3;
1895 struct sock *sk;
1896
1897 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
1898
1899 /* Broadcast receiver requires handling of some events before it can
1900 * proceed to establishing a BIG sync:
1901 *
1902 * 1. HCI_EV_LE_PA_SYNC_ESTABLISHED: The socket may specify a specific
1903 * SID to listen to and once sync is estabilished its handle needs to
1904 * be stored in iso_pi(sk)->sync_handle so it can be matched once
1905 * receiving the BIG Info.
1906 * 2. HCI_EVT_LE_BIG_INFO_ADV_REPORT: When connect_ind is triggered by a
1907 * a BIG Info it attempts to check if there any listening socket with
1908 * the same sync_handle and if it does then attempt to create a sync.
1909 * 3. HCI_EV_LE_PER_ADV_REPORT: When a PA report is received, it is stored
1910 * in iso_pi(sk)->base so it can be passed up to user, in the case of a
1911 * broadcast sink.
1912 */
1913 ev1 = hci_recv_event_data(hdev, HCI_EV_LE_PA_SYNC_ESTABLISHED);
1914 if (ev1) {
1915 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_LISTEN,
1916 iso_match_sid, ev1);
1917 if (sk && !ev1->status)
1918 iso_pi(sk)->sync_handle = le16_to_cpu(ev1->handle);
1919
1920 goto done;
1921 }
1922
1923 ev2 = hci_recv_event_data(hdev, HCI_EVT_LE_BIG_INFO_ADV_REPORT);
1924 if (ev2) {
1925 /* Check if BIGInfo report has already been handled */
1926 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_CONNECTED,
1927 iso_match_sync_handle, ev2);
1928 if (sk) {
1929 sock_put(sk);
1930 sk = NULL;
1931 goto done;
1932 }
1933
1934 /* Try to get PA sync socket, if it exists */
1935 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_CONNECT2,
1936 iso_match_sync_handle, ev2);
1937 if (!sk)
1938 sk = iso_get_sock(&hdev->bdaddr, bdaddr,
1939 BT_LISTEN,
1940 iso_match_sync_handle,
1941 ev2);
1942
1943 if (sk) {
1944 int err;
1945
1946 iso_pi(sk)->qos.bcast.encryption = ev2->encryption;
1947
1948 if (ev2->num_bis < iso_pi(sk)->bc_num_bis)
1949 iso_pi(sk)->bc_num_bis = ev2->num_bis;
1950
1951 if (!test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags) &&
1952 !test_and_set_bit(BT_SK_BIG_SYNC, &iso_pi(sk)->flags)) {
1953 err = hci_le_big_create_sync(hdev, NULL,
1954 &iso_pi(sk)->qos,
1955 iso_pi(sk)->sync_handle,
1956 iso_pi(sk)->bc_num_bis,
1957 iso_pi(sk)->bc_bis);
1958 if (err) {
1959 bt_dev_err(hdev, "hci_le_big_create_sync: %d",
1960 err);
1961 sock_put(sk);
1962 sk = NULL;
1963 }
1964 }
1965 }
1966
1967 goto done;
1968 }
1969
1970 ev3 = hci_recv_event_data(hdev, HCI_EV_LE_PER_ADV_REPORT);
1971 if (ev3) {
1972 size_t base_len = 0;
1973 u8 *base;
1974 struct hci_conn *hcon;
1975
1976 sk = iso_get_sock(&hdev->bdaddr, bdaddr, BT_LISTEN,
1977 iso_match_sync_handle_pa_report, ev3);
1978 if (!sk)
1979 goto done;
1980
1981 hcon = iso_pi(sk)->conn->hcon;
1982 if (!hcon)
1983 goto done;
1984
1985 if (ev3->data_status == LE_PA_DATA_TRUNCATED) {
1986 /* The controller was unable to retrieve PA data. */
1987 memset(hcon->le_per_adv_data, 0,
1988 HCI_MAX_PER_AD_TOT_LEN);
1989 hcon->le_per_adv_data_len = 0;
1990 hcon->le_per_adv_data_offset = 0;
1991 goto done;
1992 }
1993
1994 if (hcon->le_per_adv_data_offset + ev3->length >
1995 HCI_MAX_PER_AD_TOT_LEN)
1996 goto done;
1997
1998 memcpy(hcon->le_per_adv_data + hcon->le_per_adv_data_offset,
1999 ev3->data, ev3->length);
2000 hcon->le_per_adv_data_offset += ev3->length;
2001
2002 if (ev3->data_status == LE_PA_DATA_COMPLETE) {
2003 /* All PA data has been received. */
2004 hcon->le_per_adv_data_len =
2005 hcon->le_per_adv_data_offset;
2006 hcon->le_per_adv_data_offset = 0;
2007
2008 /* Extract BASE */
2009 base = eir_get_service_data(hcon->le_per_adv_data,
2010 hcon->le_per_adv_data_len,
2011 EIR_BAA_SERVICE_UUID,
2012 &base_len);
2013
2014 if (!base || base_len > BASE_MAX_LENGTH)
2015 goto done;
2016
2017 memcpy(iso_pi(sk)->base, base, base_len);
2018 iso_pi(sk)->base_len = base_len;
2019 } else {
2020 /* This is a PA data fragment. Keep pa_data_len set to 0
2021 * until all data has been reassembled.
2022 */
2023 hcon->le_per_adv_data_len = 0;
2024 }
2025 } else {
2026 sk = iso_get_sock(&hdev->bdaddr, BDADDR_ANY,
2027 BT_LISTEN, NULL, NULL);
2028 }
2029
2030 done:
2031 if (!sk)
2032 return 0;
2033
2034 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
2035 *flags |= HCI_PROTO_DEFER;
2036
2037 sock_put(sk);
2038
2039 return HCI_LM_ACCEPT;
2040 }
2041
iso_connect_cfm(struct hci_conn * hcon,__u8 status)2042 static void iso_connect_cfm(struct hci_conn *hcon, __u8 status)
2043 {
2044 if (hcon->type != ISO_LINK) {
2045 if (hcon->type != LE_LINK)
2046 return;
2047
2048 /* Check if LE link has failed */
2049 if (status) {
2050 struct hci_link *link, *t;
2051
2052 list_for_each_entry_safe(link, t, &hcon->link_list,
2053 list)
2054 iso_conn_del(link->conn, bt_to_errno(status));
2055
2056 return;
2057 }
2058
2059 /* Create CIS if pending */
2060 hci_le_create_cis_pending(hcon->hdev);
2061 return;
2062 }
2063
2064 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
2065
2066 /* Similar to the success case, if HCI_CONN_BIG_SYNC_FAILED or
2067 * HCI_CONN_PA_SYNC_FAILED is set, queue the failed connection
2068 * into the accept queue of the listening socket and wake up
2069 * userspace, to inform the user about the event.
2070 */
2071 if (!status || test_bit(HCI_CONN_BIG_SYNC_FAILED, &hcon->flags) ||
2072 test_bit(HCI_CONN_PA_SYNC_FAILED, &hcon->flags)) {
2073 struct iso_conn *conn;
2074
2075 conn = iso_conn_add(hcon);
2076 if (conn)
2077 iso_conn_ready(conn);
2078 } else {
2079 iso_conn_del(hcon, bt_to_errno(status));
2080 }
2081 }
2082
iso_disconn_cfm(struct hci_conn * hcon,__u8 reason)2083 static void iso_disconn_cfm(struct hci_conn *hcon, __u8 reason)
2084 {
2085 if (hcon->type != ISO_LINK)
2086 return;
2087
2088 BT_DBG("hcon %p reason %d", hcon, reason);
2089
2090 iso_conn_del(hcon, bt_to_errno(reason));
2091 }
2092
iso_recv(struct hci_conn * hcon,struct sk_buff * skb,u16 flags)2093 void iso_recv(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
2094 {
2095 struct iso_conn *conn = hcon->iso_data;
2096 __u16 pb, ts, len;
2097
2098 if (!conn)
2099 goto drop;
2100
2101 pb = hci_iso_flags_pb(flags);
2102 ts = hci_iso_flags_ts(flags);
2103
2104 BT_DBG("conn %p len %d pb 0x%x ts 0x%x", conn, skb->len, pb, ts);
2105
2106 switch (pb) {
2107 case ISO_START:
2108 case ISO_SINGLE:
2109 if (conn->rx_len) {
2110 BT_ERR("Unexpected start frame (len %d)", skb->len);
2111 kfree_skb(conn->rx_skb);
2112 conn->rx_skb = NULL;
2113 conn->rx_len = 0;
2114 }
2115
2116 if (ts) {
2117 struct hci_iso_ts_data_hdr *hdr;
2118
2119 /* TODO: add timestamp to the packet? */
2120 hdr = skb_pull_data(skb, HCI_ISO_TS_DATA_HDR_SIZE);
2121 if (!hdr) {
2122 BT_ERR("Frame is too short (len %d)", skb->len);
2123 goto drop;
2124 }
2125
2126 len = __le16_to_cpu(hdr->slen);
2127 } else {
2128 struct hci_iso_data_hdr *hdr;
2129
2130 hdr = skb_pull_data(skb, HCI_ISO_DATA_HDR_SIZE);
2131 if (!hdr) {
2132 BT_ERR("Frame is too short (len %d)", skb->len);
2133 goto drop;
2134 }
2135
2136 len = __le16_to_cpu(hdr->slen);
2137 }
2138
2139 flags = hci_iso_data_flags(len);
2140 len = hci_iso_data_len(len);
2141
2142 BT_DBG("Start: total len %d, frag len %d flags 0x%4.4x", len,
2143 skb->len, flags);
2144
2145 if (len == skb->len) {
2146 /* Complete frame received */
2147 hci_skb_pkt_status(skb) = flags & 0x03;
2148 iso_recv_frame(conn, skb);
2149 return;
2150 }
2151
2152 if (pb == ISO_SINGLE) {
2153 BT_ERR("Frame malformed (len %d, expected len %d)",
2154 skb->len, len);
2155 goto drop;
2156 }
2157
2158 if (skb->len > len) {
2159 BT_ERR("Frame is too long (len %d, expected len %d)",
2160 skb->len, len);
2161 goto drop;
2162 }
2163
2164 /* Allocate skb for the complete frame (with header) */
2165 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
2166 if (!conn->rx_skb)
2167 goto drop;
2168
2169 hci_skb_pkt_status(conn->rx_skb) = flags & 0x03;
2170 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2171 skb->len);
2172 conn->rx_len = len - skb->len;
2173 break;
2174
2175 case ISO_CONT:
2176 BT_DBG("Cont: frag len %d (expecting %d)", skb->len,
2177 conn->rx_len);
2178
2179 if (!conn->rx_len) {
2180 BT_ERR("Unexpected continuation frame (len %d)",
2181 skb->len);
2182 goto drop;
2183 }
2184
2185 if (skb->len > conn->rx_len) {
2186 BT_ERR("Fragment is too long (len %d, expected %d)",
2187 skb->len, conn->rx_len);
2188 kfree_skb(conn->rx_skb);
2189 conn->rx_skb = NULL;
2190 conn->rx_len = 0;
2191 goto drop;
2192 }
2193
2194 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2195 skb->len);
2196 conn->rx_len -= skb->len;
2197 return;
2198
2199 case ISO_END:
2200 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
2201 skb->len);
2202 conn->rx_len -= skb->len;
2203
2204 if (!conn->rx_len) {
2205 struct sk_buff *rx_skb = conn->rx_skb;
2206
2207 /* Complete frame received. iso_recv_frame
2208 * takes ownership of the skb so set the global
2209 * rx_skb pointer to NULL first.
2210 */
2211 conn->rx_skb = NULL;
2212 iso_recv_frame(conn, rx_skb);
2213 }
2214 break;
2215 }
2216
2217 drop:
2218 kfree_skb(skb);
2219 }
2220
2221 static struct hci_cb iso_cb = {
2222 .name = "ISO",
2223 .connect_cfm = iso_connect_cfm,
2224 .disconn_cfm = iso_disconn_cfm,
2225 };
2226
iso_debugfs_show(struct seq_file * f,void * p)2227 static int iso_debugfs_show(struct seq_file *f, void *p)
2228 {
2229 struct sock *sk;
2230
2231 read_lock(&iso_sk_list.lock);
2232
2233 sk_for_each(sk, &iso_sk_list.head) {
2234 seq_printf(f, "%pMR %pMR %d\n", &iso_pi(sk)->src,
2235 &iso_pi(sk)->dst, sk->sk_state);
2236 }
2237
2238 read_unlock(&iso_sk_list.lock);
2239
2240 return 0;
2241 }
2242
2243 DEFINE_SHOW_ATTRIBUTE(iso_debugfs);
2244
2245 static struct dentry *iso_debugfs;
2246
2247 static const struct proto_ops iso_sock_ops = {
2248 .family = PF_BLUETOOTH,
2249 .owner = THIS_MODULE,
2250 .release = iso_sock_release,
2251 .bind = iso_sock_bind,
2252 .connect = iso_sock_connect,
2253 .listen = iso_sock_listen,
2254 .accept = iso_sock_accept,
2255 .getname = iso_sock_getname,
2256 .sendmsg = iso_sock_sendmsg,
2257 .recvmsg = iso_sock_recvmsg,
2258 .poll = bt_sock_poll,
2259 .ioctl = bt_sock_ioctl,
2260 .mmap = sock_no_mmap,
2261 .socketpair = sock_no_socketpair,
2262 .shutdown = iso_sock_shutdown,
2263 .setsockopt = iso_sock_setsockopt,
2264 .getsockopt = iso_sock_getsockopt
2265 };
2266
2267 static const struct net_proto_family iso_sock_family_ops = {
2268 .family = PF_BLUETOOTH,
2269 .owner = THIS_MODULE,
2270 .create = iso_sock_create,
2271 };
2272
2273 static bool iso_inited;
2274
iso_enabled(void)2275 bool iso_enabled(void)
2276 {
2277 return iso_inited;
2278 }
2279
iso_init(void)2280 int iso_init(void)
2281 {
2282 int err;
2283
2284 BUILD_BUG_ON(sizeof(struct sockaddr_iso) > sizeof(struct sockaddr));
2285
2286 if (iso_inited)
2287 return -EALREADY;
2288
2289 err = proto_register(&iso_proto, 0);
2290 if (err < 0)
2291 return err;
2292
2293 err = bt_sock_register(BTPROTO_ISO, &iso_sock_family_ops);
2294 if (err < 0) {
2295 BT_ERR("ISO socket registration failed");
2296 goto error;
2297 }
2298
2299 err = bt_procfs_init(&init_net, "iso", &iso_sk_list, NULL);
2300 if (err < 0) {
2301 BT_ERR("Failed to create ISO proc file");
2302 bt_sock_unregister(BTPROTO_ISO);
2303 goto error;
2304 }
2305
2306 BT_INFO("ISO socket layer initialized");
2307
2308 hci_register_cb(&iso_cb);
2309
2310 if (!IS_ERR_OR_NULL(bt_debugfs))
2311 iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
2312 NULL, &iso_debugfs_fops);
2313
2314 iso_inited = true;
2315
2316 return 0;
2317
2318 error:
2319 proto_unregister(&iso_proto);
2320 return err;
2321 }
2322
iso_exit(void)2323 int iso_exit(void)
2324 {
2325 if (!iso_inited)
2326 return -EALREADY;
2327
2328 bt_procfs_cleanup(&init_net, "iso");
2329
2330 debugfs_remove(iso_debugfs);
2331 iso_debugfs = NULL;
2332
2333 hci_unregister_cb(&iso_cb);
2334
2335 bt_sock_unregister(BTPROTO_ISO);
2336
2337 proto_unregister(&iso_proto);
2338
2339 iso_inited = false;
2340
2341 return 0;
2342 }
2343