mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-15 08:44:14 +08:00
4e1a720d03
slub debug reported: [ 440.648642] ============================================================================= [ 440.648649] BUG kmalloc-1024 (Tainted: G BU O ): Poison overwritten [ 440.648651] ----------------------------------------------------------------------------- [ 440.648655] INFO: 0xe70f4bec-0xe70f4bec. First byte 0x6a instead of 0x6b [ 440.648665] INFO: Allocated in sk_prot_alloc+0x6b/0xc6 age=33155 cpu=1 pid=1047 [ 440.648671] ___slab_alloc.constprop.24+0x1fc/0x292 [ 440.648675] __slab_alloc.isra.18.constprop.23+0x1c/0x25 [ 440.648677] __kmalloc+0xb6/0x17f [ 440.648680] sk_prot_alloc+0x6b/0xc6 [ 440.648683] sk_alloc+0x1e/0xa1 [ 440.648700] sco_sock_alloc.constprop.6+0x26/0xaf [bluetooth] [ 440.648716] sco_connect_cfm+0x166/0x281 [bluetooth] [ 440.648731] hci_conn_request_evt.isra.53+0x258/0x281 [bluetooth] [ 440.648746] hci_event_packet+0x28b/0x2326 [bluetooth] [ 440.648759] hci_rx_work+0x161/0x291 [bluetooth] [ 440.648764] process_one_work+0x163/0x2b2 [ 440.648767] worker_thread+0x1a9/0x25c [ 440.648770] kthread+0xf8/0xfd [ 440.648774] ret_from_fork+0x2e/0x38 [ 440.648779] INFO: Freed in __sk_destruct+0xd3/0xdf age=3815 cpu=1 pid=1047 [ 440.648782] __slab_free+0x4b/0x27a [ 440.648784] kfree+0x12e/0x155 [ 440.648787] __sk_destruct+0xd3/0xdf [ 440.648790] sk_destruct+0x27/0x29 [ 440.648793] __sk_free+0x75/0x91 [ 440.648795] sk_free+0x1c/0x1e [ 440.648810] sco_sock_kill+0x5a/0x5f [bluetooth] [ 440.648825] sco_conn_del+0x8e/0xba [bluetooth] [ 440.648840] sco_disconn_cfm+0x3a/0x41 [bluetooth] [ 440.648855] hci_event_packet+0x45e/0x2326 [bluetooth] [ 440.648868] hci_rx_work+0x161/0x291 [bluetooth] [ 440.648872] process_one_work+0x163/0x2b2 [ 440.648875] worker_thread+0x1a9/0x25c [ 440.648877] kthread+0xf8/0xfd [ 440.648880] ret_from_fork+0x2e/0x38 [ 440.648884] INFO: Slab 0xf4718580 objects=27 used=27 fp=0x (null) flags=0x40008100 [ 440.648886] INFO: Object 0xe70f4b88 @offset=19336 fp=0xe70f54f8 When KASAN was enabled, it reported: [ 210.096613] ================================================================== [ 210.096634] BUG: KASAN: use-after-free in ex_handler_refcount+0x5b/0x127 [ 210.096641] Write of size 4 at addr ffff880107e17160 by task kworker/u9:1/2040 [ 210.096651] CPU: 1 PID: 2040 Comm: kworker/u9:1 Tainted: G U O 4.14.47-20180606+ #2 [ 210.096654] Hardware name: , BIOS 2017.01-00087-g43e04de 08/30/2017 [ 210.096693] Workqueue: hci0 hci_rx_work [bluetooth] [ 210.096698] Call Trace: [ 210.096711] dump_stack+0x46/0x59 [ 210.096722] print_address_description+0x6b/0x23b [ 210.096729] ? ex_handler_refcount+0x5b/0x127 [ 210.096736] kasan_report+0x220/0x246 [ 210.096744] ex_handler_refcount+0x5b/0x127 [ 210.096751] ? ex_handler_clear_fs+0x85/0x85 [ 210.096757] fixup_exception+0x8c/0x96 [ 210.096766] do_trap+0x66/0x2c1 [ 210.096773] do_error_trap+0x152/0x180 [ 210.096781] ? fixup_bug+0x78/0x78 [ 210.096817] ? hci_debugfs_create_conn+0x244/0x26a [bluetooth] [ 210.096824] ? __schedule+0x113b/0x1453 [ 210.096830] ? sysctl_net_exit+0xe/0xe [ 210.096837] ? __wake_up_common+0x343/0x343 [ 210.096843] ? insert_work+0x107/0x163 [ 210.096850] invalid_op+0x1b/0x40 [ 210.096888] RIP: 0010:hci_debugfs_create_conn+0x244/0x26a [bluetooth] [ 210.096892] RSP: 0018:ffff880094a0f970 EFLAGS: 00010296 [ 210.096898] RAX: 0000000000000000 RBX: ffff880107e170e8 RCX: ffff880107e17160 [ 210.096902] RDX: 000000000000002f RSI: ffff88013b80ed40 RDI: ffffffffa058b940 [ 210.096906] RBP: ffff88011b2b0578 R08: 00000000852f0ec9 R09: ffffffff81cfcf9b [ 210.096909] R10: 00000000d21bdad7 R11: 0000000000000001 R12: ffff8800967b0488 [ 210.096913] R13: ffff880107e17168 R14: 0000000000000068 R15: ffff8800949c0008 [ 210.096920] ? __sk_destruct+0x2c6/0x2d4 [ 210.096959] hci_event_packet+0xff5/0x7de2 [bluetooth] [ 210.096969] ? __local_bh_enable_ip+0x43/0x5b [ 210.097004] ? l2cap_sock_recv_cb+0x158/0x166 [bluetooth] [ 210.097039] ? hci_le_meta_evt+0x2bb3/0x2bb3 [bluetooth] [ 210.097075] ? l2cap_ertm_init+0x94e/0x94e [bluetooth] [ 210.097093] ? xhci_urb_enqueue+0xbd8/0xcf5 [xhci_hcd] [ 210.097102] ? __accumulate_pelt_segments+0x24/0x33 [ 210.097109] ? __accumulate_pelt_segments+0x24/0x33 [ 210.097115] ? __update_load_avg_se.isra.2+0x217/0x3a4 [ 210.097122] ? set_next_entity+0x7c3/0x12cd [ 210.097128] ? pick_next_entity+0x25e/0x26c [ 210.097135] ? pick_next_task_fair+0x2ca/0xc1a [ 210.097141] ? switch_mm_irqs_off+0x346/0xb4f [ 210.097147] ? __switch_to+0x769/0xbc4 [ 210.097153] ? compat_start_thread+0x66/0x66 [ 210.097188] ? hci_conn_check_link_mode+0x1cd/0x1cd [bluetooth] [ 210.097195] ? finish_task_switch+0x392/0x431 [ 210.097228] ? hci_rx_work+0x154/0x487 [bluetooth] [ 210.097260] hci_rx_work+0x154/0x487 [bluetooth] [ 210.097269] process_one_work+0x579/0x9e9 [ 210.097277] worker_thread+0x68f/0x804 [ 210.097285] kthread+0x31c/0x32b [ 210.097292] ? rescuer_thread+0x70c/0x70c [ 210.097299] ? kthread_create_on_node+0xa3/0xa3 [ 210.097306] ret_from_fork+0x35/0x40 [ 210.097314] Allocated by task 2040: [ 210.097323] kasan_kmalloc.part.1+0x51/0xc7 [ 210.097328] __kmalloc+0x17f/0x1b6 [ 210.097335] sk_prot_alloc+0xf2/0x1a3 [ 210.097340] sk_alloc+0x22/0x297 [ 210.097375] sco_sock_alloc.constprop.7+0x23/0x202 [bluetooth] [ 210.097410] sco_connect_cfm+0x2d0/0x566 [bluetooth] [ 210.097443] hci_conn_request_evt.isra.53+0x6d3/0x762 [bluetooth] [ 210.097476] hci_event_packet+0x85e/0x7de2 [bluetooth] [ 210.097507] hci_rx_work+0x154/0x487 [bluetooth] [ 210.097512] process_one_work+0x579/0x9e9 [ 210.097517] worker_thread+0x68f/0x804 [ 210.097523] kthread+0x31c/0x32b [ 210.097529] ret_from_fork+0x35/0x40 [ 210.097533] Freed by task 2040: [ 210.097539] kasan_slab_free+0xb3/0x15e [ 210.097544] kfree+0x103/0x1a9 [ 210.097549] __sk_destruct+0x2c6/0x2d4 [ 210.097584] sco_conn_del.isra.1+0xba/0x10e [bluetooth] [ 210.097617] hci_event_packet+0xff5/0x7de2 [bluetooth] [ 210.097648] hci_rx_work+0x154/0x487 [bluetooth] [ 210.097653] process_one_work+0x579/0x9e9 [ 210.097658] worker_thread+0x68f/0x804 [ 210.097663] kthread+0x31c/0x32b [ 210.097670] ret_from_fork+0x35/0x40 [ 210.097676] The buggy address belongs to the object at ffff880107e170e8 which belongs to the cache kmalloc-1024 of size 1024 [ 210.097681] The buggy address is located 120 bytes inside of 1024-byte region [ffff880107e170e8, ffff880107e174e8) [ 210.097683] The buggy address belongs to the page: [ 210.097689] page:ffffea00041f8400 count:1 mapcount:0 mapping: (null) index:0xffff880107e15b68 compound_mapcount: 0 [ 210.110194] flags: 0x8000000000008100(slab|head) [ 210.115441] raw: 8000000000008100 0000000000000000 ffff880107e15b68 0000000100170016 [ 210.115448] raw: ffffea0004a47620 ffffea0004b48e20 ffff88013b80ed40 0000000000000000 [ 210.115451] page dumped because: kasan: bad access detected [ 210.115454] Memory state around the buggy address: [ 210.115460] ffff880107e17000: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc [ 210.115465] ffff880107e17080: fc fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb [ 210.115469] >ffff880107e17100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 210.115472] ^ [ 210.115477] ffff880107e17180: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 210.115481] ffff880107e17200: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb [ 210.115483] ================================================================== And finally when BT_DBG() and ftrace was enabled it showed: <...>-14979 [001] .... 186.104191: sco_sock_kill <-sco_sock_close <...>-14979 [001] .... 186.104191: sco_sock_kill <-sco_sock_release <...>-14979 [001] .... 186.104192: sco_sock_kill: sk ef0497a0 state 9 <...>-14979 [001] .... 186.104193: bt_sock_unlink <-sco_sock_kill kworker/u9:2-792 [001] .... 186.104246: sco_sock_kill <-sco_conn_del kworker/u9:2-792 [001] .... 186.104248: sco_sock_kill: sk ef0497a0 state 9 kworker/u9:2-792 [001] .... 186.104249: bt_sock_unlink <-sco_sock_kill kworker/u9:2-792 [001] .... 186.104250: sco_sock_destruct <-__sk_destruct kworker/u9:2-792 [001] .... 186.104250: sco_sock_destruct: sk ef0497a0 kworker/u9:2-792 [001] .... 186.104860: hci_conn_del <-hci_event_packet kworker/u9:2-792 [001] .... 186.104864: hci_conn_del: hci0 hcon ef0484c0 handle 266 Only in the failed case, sco_sock_kill() gets called with the same sock pointer two times. Add a check for SOCK_DEAD to avoid continue killing a socket which has already been killed. Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1271 lines
25 KiB
C
1271 lines
25 KiB
C
/*
|
|
BlueZ - Bluetooth protocol stack for Linux
|
|
Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License version 2 as
|
|
published by the Free Software Foundation;
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
|
|
CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
|
|
COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
|
|
SOFTWARE IS DISCLAIMED.
|
|
*/
|
|
|
|
/* Bluetooth SCO sockets. */
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/debugfs.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/sched/signal.h>
|
|
|
|
#include <net/bluetooth/bluetooth.h>
|
|
#include <net/bluetooth/hci_core.h>
|
|
#include <net/bluetooth/sco.h>
|
|
|
|
static bool disable_esco;
|
|
|
|
static const struct proto_ops sco_sock_ops;
|
|
|
|
static struct bt_sock_list sco_sk_list = {
|
|
.lock = __RW_LOCK_UNLOCKED(sco_sk_list.lock)
|
|
};
|
|
|
|
/* ---- SCO connections ---- */
|
|
struct sco_conn {
|
|
struct hci_conn *hcon;
|
|
|
|
spinlock_t lock;
|
|
struct sock *sk;
|
|
|
|
unsigned int mtu;
|
|
};
|
|
|
|
#define sco_conn_lock(c) spin_lock(&c->lock);
|
|
#define sco_conn_unlock(c) spin_unlock(&c->lock);
|
|
|
|
static void sco_sock_close(struct sock *sk);
|
|
static void sco_sock_kill(struct sock *sk);
|
|
|
|
/* ----- SCO socket info ----- */
|
|
#define sco_pi(sk) ((struct sco_pinfo *) sk)
|
|
|
|
struct sco_pinfo {
|
|
struct bt_sock bt;
|
|
bdaddr_t src;
|
|
bdaddr_t dst;
|
|
__u32 flags;
|
|
__u16 setting;
|
|
struct sco_conn *conn;
|
|
};
|
|
|
|
/* ---- SCO timers ---- */
|
|
#define SCO_CONN_TIMEOUT (HZ * 40)
|
|
#define SCO_DISCONN_TIMEOUT (HZ * 2)
|
|
|
|
static void sco_sock_timeout(struct timer_list *t)
|
|
{
|
|
struct sock *sk = from_timer(sk, t, sk_timer);
|
|
|
|
BT_DBG("sock %p state %d", sk, sk->sk_state);
|
|
|
|
bh_lock_sock(sk);
|
|
sk->sk_err = ETIMEDOUT;
|
|
sk->sk_state_change(sk);
|
|
bh_unlock_sock(sk);
|
|
|
|
sco_sock_kill(sk);
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void sco_sock_set_timer(struct sock *sk, long timeout)
|
|
{
|
|
BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
|
|
sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
|
|
}
|
|
|
|
static void sco_sock_clear_timer(struct sock *sk)
|
|
{
|
|
BT_DBG("sock %p state %d", sk, sk->sk_state);
|
|
sk_stop_timer(sk, &sk->sk_timer);
|
|
}
|
|
|
|
/* ---- SCO connections ---- */
|
|
static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
|
|
{
|
|
struct hci_dev *hdev = hcon->hdev;
|
|
struct sco_conn *conn = hcon->sco_data;
|
|
|
|
if (conn)
|
|
return conn;
|
|
|
|
conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
|
|
if (!conn)
|
|
return NULL;
|
|
|
|
spin_lock_init(&conn->lock);
|
|
|
|
hcon->sco_data = conn;
|
|
conn->hcon = hcon;
|
|
|
|
if (hdev->sco_mtu > 0)
|
|
conn->mtu = hdev->sco_mtu;
|
|
else
|
|
conn->mtu = 60;
|
|
|
|
BT_DBG("hcon %p conn %p", hcon, conn);
|
|
|
|
return conn;
|
|
}
|
|
|
|
/* Delete channel.
|
|
* Must be called on the locked socket. */
|
|
static void sco_chan_del(struct sock *sk, int err)
|
|
{
|
|
struct sco_conn *conn;
|
|
|
|
conn = sco_pi(sk)->conn;
|
|
|
|
BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
|
|
|
|
if (conn) {
|
|
sco_conn_lock(conn);
|
|
conn->sk = NULL;
|
|
sco_pi(sk)->conn = NULL;
|
|
sco_conn_unlock(conn);
|
|
|
|
if (conn->hcon)
|
|
hci_conn_drop(conn->hcon);
|
|
}
|
|
|
|
sk->sk_state = BT_CLOSED;
|
|
sk->sk_err = err;
|
|
sk->sk_state_change(sk);
|
|
|
|
sock_set_flag(sk, SOCK_ZAPPED);
|
|
}
|
|
|
|
static void sco_conn_del(struct hci_conn *hcon, int err)
|
|
{
|
|
struct sco_conn *conn = hcon->sco_data;
|
|
struct sock *sk;
|
|
|
|
if (!conn)
|
|
return;
|
|
|
|
BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
|
|
|
|
/* Kill socket */
|
|
sco_conn_lock(conn);
|
|
sk = conn->sk;
|
|
sco_conn_unlock(conn);
|
|
|
|
if (sk) {
|
|
sock_hold(sk);
|
|
bh_lock_sock(sk);
|
|
sco_sock_clear_timer(sk);
|
|
sco_chan_del(sk, err);
|
|
bh_unlock_sock(sk);
|
|
sco_sock_kill(sk);
|
|
sock_put(sk);
|
|
}
|
|
|
|
hcon->sco_data = NULL;
|
|
kfree(conn);
|
|
}
|
|
|
|
static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
|
|
struct sock *parent)
|
|
{
|
|
BT_DBG("conn %p", conn);
|
|
|
|
sco_pi(sk)->conn = conn;
|
|
conn->sk = sk;
|
|
|
|
if (parent)
|
|
bt_accept_enqueue(parent, sk);
|
|
}
|
|
|
|
static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
|
|
struct sock *parent)
|
|
{
|
|
int err = 0;
|
|
|
|
sco_conn_lock(conn);
|
|
if (conn->sk)
|
|
err = -EBUSY;
|
|
else
|
|
__sco_chan_add(conn, sk, parent);
|
|
|
|
sco_conn_unlock(conn);
|
|
return err;
|
|
}
|
|
|
|
static int sco_connect(struct sock *sk)
|
|
{
|
|
struct sco_conn *conn;
|
|
struct hci_conn *hcon;
|
|
struct hci_dev *hdev;
|
|
int err, type;
|
|
|
|
BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
|
|
|
|
hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src, BDADDR_BREDR);
|
|
if (!hdev)
|
|
return -EHOSTUNREACH;
|
|
|
|
hci_dev_lock(hdev);
|
|
|
|
if (lmp_esco_capable(hdev) && !disable_esco)
|
|
type = ESCO_LINK;
|
|
else
|
|
type = SCO_LINK;
|
|
|
|
if (sco_pi(sk)->setting == BT_VOICE_TRANSPARENT &&
|
|
(!lmp_transp_capable(hdev) || !lmp_esco_capable(hdev))) {
|
|
err = -EOPNOTSUPP;
|
|
goto done;
|
|
}
|
|
|
|
hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
|
|
sco_pi(sk)->setting);
|
|
if (IS_ERR(hcon)) {
|
|
err = PTR_ERR(hcon);
|
|
goto done;
|
|
}
|
|
|
|
conn = sco_conn_add(hcon);
|
|
if (!conn) {
|
|
hci_conn_drop(hcon);
|
|
err = -ENOMEM;
|
|
goto done;
|
|
}
|
|
|
|
/* Update source addr of the socket */
|
|
bacpy(&sco_pi(sk)->src, &hcon->src);
|
|
|
|
err = sco_chan_add(conn, sk, NULL);
|
|
if (err)
|
|
goto done;
|
|
|
|
if (hcon->state == BT_CONNECTED) {
|
|
sco_sock_clear_timer(sk);
|
|
sk->sk_state = BT_CONNECTED;
|
|
} else {
|
|
sk->sk_state = BT_CONNECT;
|
|
sco_sock_set_timer(sk, sk->sk_sndtimeo);
|
|
}
|
|
|
|
done:
|
|
hci_dev_unlock(hdev);
|
|
hci_dev_put(hdev);
|
|
return err;
|
|
}
|
|
|
|
static int sco_send_frame(struct sock *sk, struct msghdr *msg, int len)
|
|
{
|
|
struct sco_conn *conn = sco_pi(sk)->conn;
|
|
struct sk_buff *skb;
|
|
int err;
|
|
|
|
/* Check outgoing MTU */
|
|
if (len > conn->mtu)
|
|
return -EINVAL;
|
|
|
|
BT_DBG("sk %p len %d", sk, len);
|
|
|
|
skb = bt_skb_send_alloc(sk, len, msg->msg_flags & MSG_DONTWAIT, &err);
|
|
if (!skb)
|
|
return err;
|
|
|
|
if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
|
|
kfree_skb(skb);
|
|
return -EFAULT;
|
|
}
|
|
|
|
hci_send_sco(conn->hcon, skb);
|
|
|
|
return len;
|
|
}
|
|
|
|
static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb)
|
|
{
|
|
struct sock *sk;
|
|
|
|
sco_conn_lock(conn);
|
|
sk = conn->sk;
|
|
sco_conn_unlock(conn);
|
|
|
|
if (!sk)
|
|
goto drop;
|
|
|
|
BT_DBG("sk %p len %d", sk, skb->len);
|
|
|
|
if (sk->sk_state != BT_CONNECTED)
|
|
goto drop;
|
|
|
|
if (!sock_queue_rcv_skb(sk, skb))
|
|
return;
|
|
|
|
drop:
|
|
kfree_skb(skb);
|
|
}
|
|
|
|
/* -------- Socket interface ---------- */
|
|
static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
|
|
{
|
|
struct sock *sk;
|
|
|
|
sk_for_each(sk, &sco_sk_list.head) {
|
|
if (sk->sk_state != BT_LISTEN)
|
|
continue;
|
|
|
|
if (!bacmp(&sco_pi(sk)->src, ba))
|
|
return sk;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
/* Find socket listening on source bdaddr.
|
|
* Returns closest match.
|
|
*/
|
|
static struct sock *sco_get_sock_listen(bdaddr_t *src)
|
|
{
|
|
struct sock *sk = NULL, *sk1 = NULL;
|
|
|
|
read_lock(&sco_sk_list.lock);
|
|
|
|
sk_for_each(sk, &sco_sk_list.head) {
|
|
if (sk->sk_state != BT_LISTEN)
|
|
continue;
|
|
|
|
/* Exact match. */
|
|
if (!bacmp(&sco_pi(sk)->src, src))
|
|
break;
|
|
|
|
/* Closest match */
|
|
if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
|
|
sk1 = sk;
|
|
}
|
|
|
|
read_unlock(&sco_sk_list.lock);
|
|
|
|
return sk ? sk : sk1;
|
|
}
|
|
|
|
static void sco_sock_destruct(struct sock *sk)
|
|
{
|
|
BT_DBG("sk %p", sk);
|
|
|
|
skb_queue_purge(&sk->sk_receive_queue);
|
|
skb_queue_purge(&sk->sk_write_queue);
|
|
}
|
|
|
|
static void sco_sock_cleanup_listen(struct sock *parent)
|
|
{
|
|
struct sock *sk;
|
|
|
|
BT_DBG("parent %p", parent);
|
|
|
|
/* Close not yet accepted channels */
|
|
while ((sk = bt_accept_dequeue(parent, NULL))) {
|
|
sco_sock_close(sk);
|
|
sco_sock_kill(sk);
|
|
}
|
|
|
|
parent->sk_state = BT_CLOSED;
|
|
sock_set_flag(parent, SOCK_ZAPPED);
|
|
}
|
|
|
|
/* Kill socket (only if zapped and orphan)
|
|
* Must be called on unlocked socket.
|
|
*/
|
|
static void sco_sock_kill(struct sock *sk)
|
|
{
|
|
if (!sock_flag(sk, SOCK_ZAPPED) || sk->sk_socket ||
|
|
sock_flag(sk, SOCK_DEAD))
|
|
return;
|
|
|
|
BT_DBG("sk %p state %d", sk, sk->sk_state);
|
|
|
|
/* Kill poor orphan */
|
|
bt_sock_unlink(&sco_sk_list, sk);
|
|
sock_set_flag(sk, SOCK_DEAD);
|
|
sock_put(sk);
|
|
}
|
|
|
|
static void __sco_sock_close(struct sock *sk)
|
|
{
|
|
BT_DBG("sk %p state %d socket %p", sk, sk->sk_state, sk->sk_socket);
|
|
|
|
switch (sk->sk_state) {
|
|
case BT_LISTEN:
|
|
sco_sock_cleanup_listen(sk);
|
|
break;
|
|
|
|
case BT_CONNECTED:
|
|
case BT_CONFIG:
|
|
if (sco_pi(sk)->conn->hcon) {
|
|
sk->sk_state = BT_DISCONN;
|
|
sco_sock_set_timer(sk, SCO_DISCONN_TIMEOUT);
|
|
sco_conn_lock(sco_pi(sk)->conn);
|
|
hci_conn_drop(sco_pi(sk)->conn->hcon);
|
|
sco_pi(sk)->conn->hcon = NULL;
|
|
sco_conn_unlock(sco_pi(sk)->conn);
|
|
} else
|
|
sco_chan_del(sk, ECONNRESET);
|
|
break;
|
|
|
|
case BT_CONNECT2:
|
|
case BT_CONNECT:
|
|
case BT_DISCONN:
|
|
sco_chan_del(sk, ECONNRESET);
|
|
break;
|
|
|
|
default:
|
|
sock_set_flag(sk, SOCK_ZAPPED);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Must be called on unlocked socket. */
|
|
static void sco_sock_close(struct sock *sk)
|
|
{
|
|
sco_sock_clear_timer(sk);
|
|
lock_sock(sk);
|
|
__sco_sock_close(sk);
|
|
release_sock(sk);
|
|
sco_sock_kill(sk);
|
|
}
|
|
|
|
static void sco_sock_init(struct sock *sk, struct sock *parent)
|
|
{
|
|
BT_DBG("sk %p", sk);
|
|
|
|
if (parent) {
|
|
sk->sk_type = parent->sk_type;
|
|
bt_sk(sk)->flags = bt_sk(parent)->flags;
|
|
security_sk_clone(parent, sk);
|
|
}
|
|
}
|
|
|
|
static struct proto sco_proto = {
|
|
.name = "SCO",
|
|
.owner = THIS_MODULE,
|
|
.obj_size = sizeof(struct sco_pinfo)
|
|
};
|
|
|
|
static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
|
|
int proto, gfp_t prio, int kern)
|
|
{
|
|
struct sock *sk;
|
|
|
|
sk = sk_alloc(net, PF_BLUETOOTH, prio, &sco_proto, kern);
|
|
if (!sk)
|
|
return NULL;
|
|
|
|
sock_init_data(sock, sk);
|
|
INIT_LIST_HEAD(&bt_sk(sk)->accept_q);
|
|
|
|
sk->sk_destruct = sco_sock_destruct;
|
|
sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
|
|
|
|
sock_reset_flag(sk, SOCK_ZAPPED);
|
|
|
|
sk->sk_protocol = proto;
|
|
sk->sk_state = BT_OPEN;
|
|
|
|
sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
|
|
|
|
timer_setup(&sk->sk_timer, sco_sock_timeout, 0);
|
|
|
|
bt_sock_link(&sco_sk_list, sk);
|
|
return sk;
|
|
}
|
|
|
|
static int sco_sock_create(struct net *net, struct socket *sock, int protocol,
|
|
int kern)
|
|
{
|
|
struct sock *sk;
|
|
|
|
BT_DBG("sock %p", sock);
|
|
|
|
sock->state = SS_UNCONNECTED;
|
|
|
|
if (sock->type != SOCK_SEQPACKET)
|
|
return -ESOCKTNOSUPPORT;
|
|
|
|
sock->ops = &sco_sock_ops;
|
|
|
|
sk = sco_sock_alloc(net, sock, protocol, GFP_ATOMIC, kern);
|
|
if (!sk)
|
|
return -ENOMEM;
|
|
|
|
sco_sock_init(sk, NULL);
|
|
return 0;
|
|
}
|
|
|
|
static int sco_sock_bind(struct socket *sock, struct sockaddr *addr,
|
|
int addr_len)
|
|
{
|
|
struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
|
|
struct sock *sk = sock->sk;
|
|
int err = 0;
|
|
|
|
BT_DBG("sk %p %pMR", sk, &sa->sco_bdaddr);
|
|
|
|
if (!addr || addr_len < sizeof(struct sockaddr_sco) ||
|
|
addr->sa_family != AF_BLUETOOTH)
|
|
return -EINVAL;
|
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state != BT_OPEN) {
|
|
err = -EBADFD;
|
|
goto done;
|
|
}
|
|
|
|
if (sk->sk_type != SOCK_SEQPACKET) {
|
|
err = -EINVAL;
|
|
goto done;
|
|
}
|
|
|
|
bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
|
|
|
|
sk->sk_state = BT_BOUND;
|
|
|
|
done:
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen, int flags)
|
|
{
|
|
struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
|
|
struct sock *sk = sock->sk;
|
|
int err;
|
|
|
|
BT_DBG("sk %p", sk);
|
|
|
|
if (alen < sizeof(struct sockaddr_sco) ||
|
|
addr->sa_family != AF_BLUETOOTH)
|
|
return -EINVAL;
|
|
|
|
if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND)
|
|
return -EBADFD;
|
|
|
|
if (sk->sk_type != SOCK_SEQPACKET)
|
|
return -EINVAL;
|
|
|
|
lock_sock(sk);
|
|
|
|
/* Set destination address and psm */
|
|
bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
|
|
|
|
err = sco_connect(sk);
|
|
if (err)
|
|
goto done;
|
|
|
|
err = bt_sock_wait_state(sk, BT_CONNECTED,
|
|
sock_sndtimeo(sk, flags & O_NONBLOCK));
|
|
|
|
done:
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_listen(struct socket *sock, int backlog)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
bdaddr_t *src = &sco_pi(sk)->src;
|
|
int err = 0;
|
|
|
|
BT_DBG("sk %p backlog %d", sk, backlog);
|
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state != BT_BOUND) {
|
|
err = -EBADFD;
|
|
goto done;
|
|
}
|
|
|
|
if (sk->sk_type != SOCK_SEQPACKET) {
|
|
err = -EINVAL;
|
|
goto done;
|
|
}
|
|
|
|
write_lock(&sco_sk_list.lock);
|
|
|
|
if (__sco_get_sock_listen_by_addr(src)) {
|
|
err = -EADDRINUSE;
|
|
goto unlock;
|
|
}
|
|
|
|
sk->sk_max_ack_backlog = backlog;
|
|
sk->sk_ack_backlog = 0;
|
|
|
|
sk->sk_state = BT_LISTEN;
|
|
|
|
unlock:
|
|
write_unlock(&sco_sk_list.lock);
|
|
|
|
done:
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_accept(struct socket *sock, struct socket *newsock,
|
|
int flags, bool kern)
|
|
{
|
|
DEFINE_WAIT_FUNC(wait, woken_wake_function);
|
|
struct sock *sk = sock->sk, *ch;
|
|
long timeo;
|
|
int err = 0;
|
|
|
|
lock_sock(sk);
|
|
|
|
timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
|
|
|
|
BT_DBG("sk %p timeo %ld", sk, timeo);
|
|
|
|
/* Wait for an incoming connection. (wake-one). */
|
|
add_wait_queue_exclusive(sk_sleep(sk), &wait);
|
|
while (1) {
|
|
if (sk->sk_state != BT_LISTEN) {
|
|
err = -EBADFD;
|
|
break;
|
|
}
|
|
|
|
ch = bt_accept_dequeue(sk, newsock);
|
|
if (ch)
|
|
break;
|
|
|
|
if (!timeo) {
|
|
err = -EAGAIN;
|
|
break;
|
|
}
|
|
|
|
if (signal_pending(current)) {
|
|
err = sock_intr_errno(timeo);
|
|
break;
|
|
}
|
|
|
|
release_sock(sk);
|
|
|
|
timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
|
|
lock_sock(sk);
|
|
}
|
|
remove_wait_queue(sk_sleep(sk), &wait);
|
|
|
|
if (err)
|
|
goto done;
|
|
|
|
newsock->state = SS_CONNECTED;
|
|
|
|
BT_DBG("new socket %p", ch);
|
|
|
|
done:
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_getname(struct socket *sock, struct sockaddr *addr,
|
|
int peer)
|
|
{
|
|
struct sockaddr_sco *sa = (struct sockaddr_sco *) addr;
|
|
struct sock *sk = sock->sk;
|
|
|
|
BT_DBG("sock %p, sk %p", sock, sk);
|
|
|
|
addr->sa_family = AF_BLUETOOTH;
|
|
|
|
if (peer)
|
|
bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
|
|
else
|
|
bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
|
|
|
|
return sizeof(struct sockaddr_sco);
|
|
}
|
|
|
|
static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
|
|
size_t len)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
int err;
|
|
|
|
BT_DBG("sock %p, sk %p", sock, sk);
|
|
|
|
err = sock_error(sk);
|
|
if (err)
|
|
return err;
|
|
|
|
if (msg->msg_flags & MSG_OOB)
|
|
return -EOPNOTSUPP;
|
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state == BT_CONNECTED)
|
|
err = sco_send_frame(sk, msg, len);
|
|
else
|
|
err = -ENOTCONN;
|
|
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
|
|
{
|
|
struct hci_dev *hdev = conn->hdev;
|
|
|
|
BT_DBG("conn %p", conn);
|
|
|
|
conn->state = BT_CONFIG;
|
|
|
|
if (!lmp_esco_capable(hdev)) {
|
|
struct hci_cp_accept_conn_req cp;
|
|
|
|
bacpy(&cp.bdaddr, &conn->dst);
|
|
cp.role = 0x00; /* Ignored */
|
|
|
|
hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
|
|
} else {
|
|
struct hci_cp_accept_sync_conn_req cp;
|
|
|
|
bacpy(&cp.bdaddr, &conn->dst);
|
|
cp.pkt_type = cpu_to_le16(conn->pkt_type);
|
|
|
|
cp.tx_bandwidth = cpu_to_le32(0x00001f40);
|
|
cp.rx_bandwidth = cpu_to_le32(0x00001f40);
|
|
cp.content_format = cpu_to_le16(setting);
|
|
|
|
switch (setting & SCO_AIRMODE_MASK) {
|
|
case SCO_AIRMODE_TRANSP:
|
|
if (conn->pkt_type & ESCO_2EV3)
|
|
cp.max_latency = cpu_to_le16(0x0008);
|
|
else
|
|
cp.max_latency = cpu_to_le16(0x000D);
|
|
cp.retrans_effort = 0x02;
|
|
break;
|
|
case SCO_AIRMODE_CVSD:
|
|
cp.max_latency = cpu_to_le16(0xffff);
|
|
cp.retrans_effort = 0xff;
|
|
break;
|
|
}
|
|
|
|
hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
|
|
sizeof(cp), &cp);
|
|
}
|
|
}
|
|
|
|
static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
|
|
size_t len, int flags)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
struct sco_pinfo *pi = sco_pi(sk);
|
|
|
|
lock_sock(sk);
|
|
|
|
if (sk->sk_state == BT_CONNECT2 &&
|
|
test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
|
|
sco_conn_defer_accept(pi->conn->hcon, pi->setting);
|
|
sk->sk_state = BT_CONFIG;
|
|
|
|
release_sock(sk);
|
|
return 0;
|
|
}
|
|
|
|
release_sock(sk);
|
|
|
|
return bt_sock_recvmsg(sock, msg, len, flags);
|
|
}
|
|
|
|
static int sco_sock_setsockopt(struct socket *sock, int level, int optname,
|
|
char __user *optval, unsigned int optlen)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
int len, err = 0;
|
|
struct bt_voice voice;
|
|
u32 opt;
|
|
|
|
BT_DBG("sk %p", sk);
|
|
|
|
lock_sock(sk);
|
|
|
|
switch (optname) {
|
|
|
|
case BT_DEFER_SETUP:
|
|
if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
|
|
err = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
if (get_user(opt, (u32 __user *) optval)) {
|
|
err = -EFAULT;
|
|
break;
|
|
}
|
|
|
|
if (opt)
|
|
set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
|
|
else
|
|
clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
|
|
break;
|
|
|
|
case BT_VOICE:
|
|
if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND &&
|
|
sk->sk_state != BT_CONNECT2) {
|
|
err = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
voice.setting = sco_pi(sk)->setting;
|
|
|
|
len = min_t(unsigned int, sizeof(voice), optlen);
|
|
if (copy_from_user((char *)&voice, optval, len)) {
|
|
err = -EFAULT;
|
|
break;
|
|
}
|
|
|
|
/* Explicitly check for these values */
|
|
if (voice.setting != BT_VOICE_TRANSPARENT &&
|
|
voice.setting != BT_VOICE_CVSD_16BIT) {
|
|
err = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
sco_pi(sk)->setting = voice.setting;
|
|
break;
|
|
|
|
default:
|
|
err = -ENOPROTOOPT;
|
|
break;
|
|
}
|
|
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_getsockopt_old(struct socket *sock, int optname,
|
|
char __user *optval, int __user *optlen)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
struct sco_options opts;
|
|
struct sco_conninfo cinfo;
|
|
int len, err = 0;
|
|
|
|
BT_DBG("sk %p", sk);
|
|
|
|
if (get_user(len, optlen))
|
|
return -EFAULT;
|
|
|
|
lock_sock(sk);
|
|
|
|
switch (optname) {
|
|
case SCO_OPTIONS:
|
|
if (sk->sk_state != BT_CONNECTED &&
|
|
!(sk->sk_state == BT_CONNECT2 &&
|
|
test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
|
|
err = -ENOTCONN;
|
|
break;
|
|
}
|
|
|
|
opts.mtu = sco_pi(sk)->conn->mtu;
|
|
|
|
BT_DBG("mtu %d", opts.mtu);
|
|
|
|
len = min_t(unsigned int, len, sizeof(opts));
|
|
if (copy_to_user(optval, (char *)&opts, len))
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
case SCO_CONNINFO:
|
|
if (sk->sk_state != BT_CONNECTED &&
|
|
!(sk->sk_state == BT_CONNECT2 &&
|
|
test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))) {
|
|
err = -ENOTCONN;
|
|
break;
|
|
}
|
|
|
|
memset(&cinfo, 0, sizeof(cinfo));
|
|
cinfo.hci_handle = sco_pi(sk)->conn->hcon->handle;
|
|
memcpy(cinfo.dev_class, sco_pi(sk)->conn->hcon->dev_class, 3);
|
|
|
|
len = min_t(unsigned int, len, sizeof(cinfo));
|
|
if (copy_to_user(optval, (char *)&cinfo, len))
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
default:
|
|
err = -ENOPROTOOPT;
|
|
break;
|
|
}
|
|
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_getsockopt(struct socket *sock, int level, int optname,
|
|
char __user *optval, int __user *optlen)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
int len, err = 0;
|
|
struct bt_voice voice;
|
|
|
|
BT_DBG("sk %p", sk);
|
|
|
|
if (level == SOL_SCO)
|
|
return sco_sock_getsockopt_old(sock, optname, optval, optlen);
|
|
|
|
if (get_user(len, optlen))
|
|
return -EFAULT;
|
|
|
|
lock_sock(sk);
|
|
|
|
switch (optname) {
|
|
|
|
case BT_DEFER_SETUP:
|
|
if (sk->sk_state != BT_BOUND && sk->sk_state != BT_LISTEN) {
|
|
err = -EINVAL;
|
|
break;
|
|
}
|
|
|
|
if (put_user(test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags),
|
|
(u32 __user *)optval))
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
case BT_VOICE:
|
|
voice.setting = sco_pi(sk)->setting;
|
|
|
|
len = min_t(unsigned int, len, sizeof(voice));
|
|
if (copy_to_user(optval, (char *)&voice, len))
|
|
err = -EFAULT;
|
|
|
|
break;
|
|
|
|
default:
|
|
err = -ENOPROTOOPT;
|
|
break;
|
|
}
|
|
|
|
release_sock(sk);
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_shutdown(struct socket *sock, int how)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
int err = 0;
|
|
|
|
BT_DBG("sock %p, sk %p", sock, sk);
|
|
|
|
if (!sk)
|
|
return 0;
|
|
|
|
sock_hold(sk);
|
|
lock_sock(sk);
|
|
|
|
if (!sk->sk_shutdown) {
|
|
sk->sk_shutdown = SHUTDOWN_MASK;
|
|
sco_sock_clear_timer(sk);
|
|
__sco_sock_close(sk);
|
|
|
|
if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
|
|
!(current->flags & PF_EXITING))
|
|
err = bt_sock_wait_state(sk, BT_CLOSED,
|
|
sk->sk_lingertime);
|
|
}
|
|
|
|
release_sock(sk);
|
|
sock_put(sk);
|
|
|
|
return err;
|
|
}
|
|
|
|
static int sco_sock_release(struct socket *sock)
|
|
{
|
|
struct sock *sk = sock->sk;
|
|
int err = 0;
|
|
|
|
BT_DBG("sock %p, sk %p", sock, sk);
|
|
|
|
if (!sk)
|
|
return 0;
|
|
|
|
sco_sock_close(sk);
|
|
|
|
if (sock_flag(sk, SOCK_LINGER) && sk->sk_lingertime &&
|
|
!(current->flags & PF_EXITING)) {
|
|
lock_sock(sk);
|
|
err = bt_sock_wait_state(sk, BT_CLOSED, sk->sk_lingertime);
|
|
release_sock(sk);
|
|
}
|
|
|
|
sock_orphan(sk);
|
|
sco_sock_kill(sk);
|
|
return err;
|
|
}
|
|
|
|
static void sco_conn_ready(struct sco_conn *conn)
|
|
{
|
|
struct sock *parent;
|
|
struct sock *sk = conn->sk;
|
|
|
|
BT_DBG("conn %p", conn);
|
|
|
|
if (sk) {
|
|
sco_sock_clear_timer(sk);
|
|
bh_lock_sock(sk);
|
|
sk->sk_state = BT_CONNECTED;
|
|
sk->sk_state_change(sk);
|
|
bh_unlock_sock(sk);
|
|
} else {
|
|
sco_conn_lock(conn);
|
|
|
|
if (!conn->hcon) {
|
|
sco_conn_unlock(conn);
|
|
return;
|
|
}
|
|
|
|
parent = sco_get_sock_listen(&conn->hcon->src);
|
|
if (!parent) {
|
|
sco_conn_unlock(conn);
|
|
return;
|
|
}
|
|
|
|
bh_lock_sock(parent);
|
|
|
|
sk = sco_sock_alloc(sock_net(parent), NULL,
|
|
BTPROTO_SCO, GFP_ATOMIC, 0);
|
|
if (!sk) {
|
|
bh_unlock_sock(parent);
|
|
sco_conn_unlock(conn);
|
|
return;
|
|
}
|
|
|
|
sco_sock_init(sk, parent);
|
|
|
|
bacpy(&sco_pi(sk)->src, &conn->hcon->src);
|
|
bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
|
|
|
|
hci_conn_hold(conn->hcon);
|
|
__sco_chan_add(conn, sk, parent);
|
|
|
|
if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
|
|
sk->sk_state = BT_CONNECT2;
|
|
else
|
|
sk->sk_state = BT_CONNECTED;
|
|
|
|
/* Wake up parent */
|
|
parent->sk_data_ready(parent);
|
|
|
|
bh_unlock_sock(parent);
|
|
|
|
sco_conn_unlock(conn);
|
|
}
|
|
}
|
|
|
|
/* ----- SCO interface with lower layer (HCI) ----- */
|
|
int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
|
|
{
|
|
struct sock *sk;
|
|
int lm = 0;
|
|
|
|
BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
|
|
|
|
/* Find listening sockets */
|
|
read_lock(&sco_sk_list.lock);
|
|
sk_for_each(sk, &sco_sk_list.head) {
|
|
if (sk->sk_state != BT_LISTEN)
|
|
continue;
|
|
|
|
if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
|
|
!bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
|
|
lm |= HCI_LM_ACCEPT;
|
|
|
|
if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
|
|
*flags |= HCI_PROTO_DEFER;
|
|
break;
|
|
}
|
|
}
|
|
read_unlock(&sco_sk_list.lock);
|
|
|
|
return lm;
|
|
}
|
|
|
|
static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
|
|
{
|
|
if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
|
|
return;
|
|
|
|
BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
|
|
|
|
if (!status) {
|
|
struct sco_conn *conn;
|
|
|
|
conn = sco_conn_add(hcon);
|
|
if (conn)
|
|
sco_conn_ready(conn);
|
|
} else
|
|
sco_conn_del(hcon, bt_to_errno(status));
|
|
}
|
|
|
|
static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
|
|
{
|
|
if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
|
|
return;
|
|
|
|
BT_DBG("hcon %p reason %d", hcon, reason);
|
|
|
|
sco_conn_del(hcon, bt_to_errno(reason));
|
|
}
|
|
|
|
void sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb)
|
|
{
|
|
struct sco_conn *conn = hcon->sco_data;
|
|
|
|
if (!conn)
|
|
goto drop;
|
|
|
|
BT_DBG("conn %p len %d", conn, skb->len);
|
|
|
|
if (skb->len) {
|
|
sco_recv_frame(conn, skb);
|
|
return;
|
|
}
|
|
|
|
drop:
|
|
kfree_skb(skb);
|
|
}
|
|
|
|
static struct hci_cb sco_cb = {
|
|
.name = "SCO",
|
|
.connect_cfm = sco_connect_cfm,
|
|
.disconn_cfm = sco_disconn_cfm,
|
|
};
|
|
|
|
static int sco_debugfs_show(struct seq_file *f, void *p)
|
|
{
|
|
struct sock *sk;
|
|
|
|
read_lock(&sco_sk_list.lock);
|
|
|
|
sk_for_each(sk, &sco_sk_list.head) {
|
|
seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
|
|
&sco_pi(sk)->dst, sk->sk_state);
|
|
}
|
|
|
|
read_unlock(&sco_sk_list.lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int sco_debugfs_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, sco_debugfs_show, inode->i_private);
|
|
}
|
|
|
|
static const struct file_operations sco_debugfs_fops = {
|
|
.open = sco_debugfs_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.release = single_release,
|
|
};
|
|
|
|
static struct dentry *sco_debugfs;
|
|
|
|
static const struct proto_ops sco_sock_ops = {
|
|
.family = PF_BLUETOOTH,
|
|
.owner = THIS_MODULE,
|
|
.release = sco_sock_release,
|
|
.bind = sco_sock_bind,
|
|
.connect = sco_sock_connect,
|
|
.listen = sco_sock_listen,
|
|
.accept = sco_sock_accept,
|
|
.getname = sco_sock_getname,
|
|
.sendmsg = sco_sock_sendmsg,
|
|
.recvmsg = sco_sock_recvmsg,
|
|
.poll = bt_sock_poll,
|
|
.ioctl = bt_sock_ioctl,
|
|
.mmap = sock_no_mmap,
|
|
.socketpair = sock_no_socketpair,
|
|
.shutdown = sco_sock_shutdown,
|
|
.setsockopt = sco_sock_setsockopt,
|
|
.getsockopt = sco_sock_getsockopt
|
|
};
|
|
|
|
static const struct net_proto_family sco_sock_family_ops = {
|
|
.family = PF_BLUETOOTH,
|
|
.owner = THIS_MODULE,
|
|
.create = sco_sock_create,
|
|
};
|
|
|
|
int __init sco_init(void)
|
|
{
|
|
int err;
|
|
|
|
BUILD_BUG_ON(sizeof(struct sockaddr_sco) > sizeof(struct sockaddr));
|
|
|
|
err = proto_register(&sco_proto, 0);
|
|
if (err < 0)
|
|
return err;
|
|
|
|
err = bt_sock_register(BTPROTO_SCO, &sco_sock_family_ops);
|
|
if (err < 0) {
|
|
BT_ERR("SCO socket registration failed");
|
|
goto error;
|
|
}
|
|
|
|
err = bt_procfs_init(&init_net, "sco", &sco_sk_list, NULL);
|
|
if (err < 0) {
|
|
BT_ERR("Failed to create SCO proc file");
|
|
bt_sock_unregister(BTPROTO_SCO);
|
|
goto error;
|
|
}
|
|
|
|
BT_INFO("SCO socket layer initialized");
|
|
|
|
hci_register_cb(&sco_cb);
|
|
|
|
if (IS_ERR_OR_NULL(bt_debugfs))
|
|
return 0;
|
|
|
|
sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
|
|
NULL, &sco_debugfs_fops);
|
|
|
|
return 0;
|
|
|
|
error:
|
|
proto_unregister(&sco_proto);
|
|
return err;
|
|
}
|
|
|
|
void sco_exit(void)
|
|
{
|
|
bt_procfs_cleanup(&init_net, "sco");
|
|
|
|
debugfs_remove(sco_debugfs);
|
|
|
|
hci_unregister_cb(&sco_cb);
|
|
|
|
bt_sock_unregister(BTPROTO_SCO);
|
|
|
|
proto_unregister(&sco_proto);
|
|
}
|
|
|
|
module_param(disable_esco, bool, 0644);
|
|
MODULE_PARM_DESC(disable_esco, "Disable eSCO connection creation");
|