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