From patchwork Fri Mar 20 20:40:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 11450321 X-Patchwork-Delegate: marcel@holtmann.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 21851139A for ; Fri, 20 Mar 2020 20:40:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EBF4120724 for ; Fri, 20 Mar 2020 20:40:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="m3VTDZJ6" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726925AbgCTUka (ORCPT ); Fri, 20 Mar 2020 16:40:30 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:44831 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726855AbgCTUka (ORCPT ); Fri, 20 Mar 2020 16:40:30 -0400 Received: by mail-pf1-f195.google.com with SMTP id b72so3877478pfb.11 for ; Fri, 20 Mar 2020 13:40:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=VVQ7vhvCR/dzXjhnVWZBKnsMGVGyca6I+XsQHNIPA+E=; b=m3VTDZJ6PK19hrjGMIBs7ROldDRFJlouDJppD1SF38tEmFyEa4cTpFsMyPLqleu3qa 8ultG6Mfb4yyAOdODPWtzgqA1cPEOitMvNqgPPYq84SEzMB3DJP8uBL1qmNXW63E8d1n P0Md09fUAOmI4f+c9EWLXCGyG+iJXXi0PAi/b1V6MQ2UO/kAyenjNVlHCzL2eqhCR0YM 8tYduzyZt3pFvTsLb8TmvI5Rcwk1f4qMVakifgwIWHSYfNuCJYYFMoeyj9efxQdO2upf uLfT+N3d2IRYYjQ878y9GeO7BWWOECnYYAThG4DLRSrTq/ru1S+AZHYAau6wONHhhZo/ qKxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VVQ7vhvCR/dzXjhnVWZBKnsMGVGyca6I+XsQHNIPA+E=; b=ix30S2msBJJNXQ1JzLJ7PZg9Y6uNNcttixH+Rjlm4om+dFBxRoUXVvOjJ70tIUoDrc b0B/XrldHLu4r+nhjv3HtuYdOkXgNq2B382Issklzjy7DW+VsvUy541/0lYYiphFRN8f LuDYF8QF9RDUwKsVn8gWR9zQmUFq/TR321WfkyJD36DHlEkiyiXPdyed+uNmy/+nRxey rKE60kl3phY8zTKc+e4lqbTmy4TYbWTLOhh5DYHWvf5QwlVVRCypw6hivZCSN0RS6yY6 pm+gxjWT0bwKmksIbpwaAyZq+VWMJ1lrW4wcK3FcRf7qnUhR9IB3vYjM1cdq+8bS9b29 h3eQ== X-Gm-Message-State: ANhLgQ20MlrWwXiVO3fq3TyawLVO0zvSJO4MsAX7Tm+pauFka4c97sfi z30ZGY4ndp18W2wiwNJhVjd1H8Sh X-Google-Smtp-Source: ADFU+vv5gvTBAix8T5aybqJqrxgZStr9JtX7Pi6saf6CrQqBiwY/ubamOkQ9ytDxvD0aQfmk2dTkFw== X-Received: by 2002:a63:fd0f:: with SMTP id d15mr10208547pgh.26.1584736828454; Fri, 20 Mar 2020 13:40:28 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id e187sm6048744pfe.143.2020.03.20.13.40.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2020 13:40:27 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v4 1/3] Bluetooth: L2CAP: Add get_peer_pid callback Date: Fri, 20 Mar 2020 13:40:24 -0700 Message-Id: <20200320204026.11680-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200320204026.11680-1-luiz.dentz@gmail.com> References: <20200320204026.11680-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This adds a callback to read the socket pid. Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_sock.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 537aaead259f..2d7d28474d34 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -660,6 +660,7 @@ struct l2cap_ops { void (*suspend) (struct l2cap_chan *chan); void (*set_shutdown) (struct l2cap_chan *chan); long (*get_sndtimeo) (struct l2cap_chan *chan); + struct pid *(*get_peer_pid) (struct l2cap_chan *chan); struct sk_buff *(*alloc_skb) (struct l2cap_chan *chan, unsigned long hdr_len, unsigned long len, int nb); diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index 40fb10b591bd..117ba20ea194 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -1504,6 +1504,13 @@ static long l2cap_sock_get_sndtimeo_cb(struct l2cap_chan *chan) return sk->sk_sndtimeo; } +static struct pid *l2cap_sock_get_peer_pid_cb(struct l2cap_chan *chan) +{ + struct sock *sk = chan->data; + + return sk->sk_peer_pid; +} + static void l2cap_sock_suspend_cb(struct l2cap_chan *chan) { struct sock *sk = chan->data; @@ -1525,6 +1532,7 @@ static const struct l2cap_ops l2cap_chan_ops = { .suspend = l2cap_sock_suspend_cb, .set_shutdown = l2cap_sock_set_shutdown_cb, .get_sndtimeo = l2cap_sock_get_sndtimeo_cb, + .get_peer_pid = l2cap_sock_get_peer_pid_cb, .alloc_skb = l2cap_sock_alloc_skb_cb, }; From patchwork Fri Mar 20 20:40:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 11450325 X-Patchwork-Delegate: marcel@holtmann.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9A3B7139A for ; Fri, 20 Mar 2020 20:40:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6D8BD20724 for ; Fri, 20 Mar 2020 20:40:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TcUwwa1Z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727175AbgCTUkc (ORCPT ); Fri, 20 Mar 2020 16:40:32 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:46360 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726983AbgCTUkb (ORCPT ); Fri, 20 Mar 2020 16:40:31 -0400 Received: by mail-pl1-f196.google.com with SMTP id r3so2984227pls.13 for ; Fri, 20 Mar 2020 13:40:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ax6LJvZifxs+Jz4PFhlO8B8liSrqW0rQiTisJ5Q7W3U=; b=TcUwwa1Znb7bXLElra4QUed/8QCtlUHGCippcD3EBdxJzWmqlmN2dne4IGl/XHRj3t mvykCC3IkDZzrIrhW8DxrcED+hIdzApS/WIP8+ViYLBlJMzFPQ/iBbYyR3EUxffkCUIE 27Q9lDqNbTNt1lF18jzeFsLZ4x24PvUDK5DgxB705QXcP9sOiMI9MDzqNR3K+XooSXvz qNGazZH2yPzaG8//TiaKHvuDbYlJtV9PK7LOQUxIPVBCNvXOXKKVbGeWjlBmy4xUIZ/N PmIOekOvFA4xi0OhjJo8XwnIOWnp1tZqinBviCR2gxmnxSw27Fov7hXIyWA20ZV/Yikr ZKEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ax6LJvZifxs+Jz4PFhlO8B8liSrqW0rQiTisJ5Q7W3U=; b=f4vBkJHi51XCOmSVRpFZiscxWx4Z4Ega8tDLj7RmggPiOgzaUC9C16SL70zfsVd2V4 o4o7DAXYhl+S1xLr8hywT3dRzUYDYUZKzctMXKNDA/4H8m+hEQDyPauRtNvadQ0tq4In fOf8A7KK3eV/STqHqIcOuTBVqrcrA3/Xny1FFRSJ4SNt/J6Yv3Nd9vQVsqorb6aY/Nq/ kmpSTJEPaD64VHrqH9FVnJT6CkcuG9oxN8+H04xlb1d3aktP0p8sbmI4A/JAnu9kwiZ6 ky9Q/QpBxQbRPGxz1xuhc/Xo04k5T7LxXH8qNFNKbX3liGt6nDGM4Kzw9V5qfpOHiofP XZ1w== X-Gm-Message-State: ANhLgQ2iS/bGmC2CUzOviiCPNs7AEkFdqmO+m6j08OUvoxACDTvU+aTv 2UEBdVkEjZ3gvIMPpi+jht+OjX2D X-Google-Smtp-Source: ADFU+vvgWqHxGFwKJ7gELI8IwmjMNAa+laf1jJguhBhNKqcoIiAVx2TsMNkgpKlG5n1hWXoHmcA4SA== X-Received: by 2002:a17:902:7c0c:: with SMTP id x12mr7831116pll.196.1584736829592; Fri, 20 Mar 2020 13:40:29 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id e187sm6048744pfe.143.2020.03.20.13.40.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2020 13:40:28 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v4 2/3] Bluetooth: L2CAP: Use DEFER_SETUP to group ECRED connections Date: Fri, 20 Mar 2020 13:40:25 -0700 Message-Id: <20200320204026.11680-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200320204026.11680-1-luiz.dentz@gmail.com> References: <20200320204026.11680-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This uses the DEFER_SETUP flag to group channels with L2CAP_CREDIT_BASED_CONNECTION_REQ. Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/l2cap.h | 4 + net/bluetooth/l2cap_core.c | 136 +++++++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 10 deletions(-) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index 2d7d28474d34..dada14d0622c 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -47,6 +47,7 @@ #define L2CAP_DEFAULT_ACC_LAT 0xFFFFFFFF #define L2CAP_BREDR_MAX_PAYLOAD 1019 /* 3-DH5 packet */ #define L2CAP_LE_MIN_MTU 23 +#define L2CAP_ECRED_CONN_SCID_MAX 5 #define L2CAP_DISC_TIMEOUT msecs_to_jiffies(100) #define L2CAP_DISC_REJ_TIMEOUT msecs_to_jiffies(5000) @@ -984,6 +985,9 @@ void l2cap_chan_set_defaults(struct l2cap_chan *chan); int l2cap_ertm_init(struct l2cap_chan *chan); void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan); +typedef void (*l2cap_chan_func_t)(struct l2cap_chan *chan, void *data); +void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func, + void *data); void l2cap_chan_del(struct l2cap_chan *chan, int err); void l2cap_send_conn_req(struct l2cap_chan *chan); void l2cap_move_start(struct l2cap_chan *chan); diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c index 5e6e35ab44dd..9f699d09d973 100644 --- a/net/bluetooth/l2cap_core.c +++ b/net/bluetooth/l2cap_core.c @@ -678,6 +678,29 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err) } EXPORT_SYMBOL_GPL(l2cap_chan_del); +static void __l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func, + void *data) +{ + struct l2cap_chan *chan; + + list_for_each_entry(chan, &conn->chan_l, list) { + func(chan, data); + } +} + +void l2cap_chan_list(struct l2cap_conn *conn, l2cap_chan_func_t func, + void *data) +{ + if (!conn) + return; + + mutex_lock(&conn->chan_lock); + __l2cap_chan_list(conn, func, data); + mutex_unlock(&conn->chan_lock); +} + +EXPORT_SYMBOL_GPL(l2cap_chan_list); + static void l2cap_conn_update_id_addr(struct work_struct *work) { struct l2cap_conn *conn = container_of(work, struct l2cap_conn, @@ -1356,29 +1379,79 @@ static void l2cap_le_connect(struct l2cap_chan *chan) sizeof(req), &req); } -static void l2cap_ecred_connect(struct l2cap_chan *chan) -{ - struct l2cap_conn *conn = chan->conn; +struct l2cap_ecred_conn_data { struct { struct l2cap_ecred_conn_req req; - __le16 scid; + __le16 scid[5]; } __packed pdu; + struct l2cap_chan *chan; + struct pid *pid; + int count; +}; + +static void l2cap_ecred_defer_connect(struct l2cap_chan *chan, void *data) +{ + struct l2cap_ecred_conn_data *conn = data; + struct pid *pid; + + if (chan == conn->chan) + return; + + if (!test_and_clear_bit(FLAG_DEFER_SETUP, &chan->flags)) + return; + + pid = chan->ops->get_peer_pid(chan); + + /* Only add deferred channels with the same PID/PSM */ + if (conn->pid != pid || chan->psm != conn->chan->psm || chan->ident || + chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT) + return; + + if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) + return; + + l2cap_ecred_init(chan, 0); + + /* Set the same ident so we can match on the rsp */ + chan->ident = conn->chan->ident; + + /* Include all channels deferred */ + conn->pdu.scid[conn->count] = cpu_to_le16(chan->scid); + + conn->count++; +} + +static void l2cap_ecred_connect(struct l2cap_chan *chan) +{ + struct l2cap_conn *conn = chan->conn; + struct l2cap_ecred_conn_data data; + + if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) + return; if (test_and_set_bit(FLAG_ECRED_CONN_REQ_SENT, &chan->flags)) return; l2cap_ecred_init(chan, 0); - pdu.req.psm = chan->psm; - pdu.req.mtu = cpu_to_le16(chan->imtu); - pdu.req.mps = cpu_to_le16(chan->mps); - pdu.req.credits = cpu_to_le16(chan->rx_credits); - pdu.scid = cpu_to_le16(chan->scid); + data.pdu.req.psm = chan->psm; + data.pdu.req.mtu = cpu_to_le16(chan->imtu); + data.pdu.req.mps = cpu_to_le16(chan->mps); + data.pdu.req.credits = cpu_to_le16(chan->rx_credits); + data.pdu.scid[0] = cpu_to_le16(chan->scid); chan->ident = l2cap_get_ident(conn); + data.pid = chan->ops->get_peer_pid(chan); + + data.count = 1; + data.chan = chan; + data.pid = chan->ops->get_peer_pid(chan); + + __l2cap_chan_list(conn, l2cap_ecred_defer_connect, &data); l2cap_send_cmd(conn, chan->ident, L2CAP_ECRED_CONN_REQ, - sizeof(pdu), &pdu); + sizeof(data.pdu.req) + data.count * sizeof(__le16), + &data.pdu); } static void l2cap_le_start(struct l2cap_chan *chan) @@ -7694,6 +7767,33 @@ static bool is_valid_psm(u16 psm, u8 dst_type) { return ((psm & 0x0101) == 0x0001); } +struct l2cap_chan_data { + struct l2cap_chan *chan; + struct pid *pid; + int count; +}; + +static void l2cap_chan_by_pid(struct l2cap_chan *chan, void *data) +{ + struct l2cap_chan_data *d = data; + struct pid *pid; + + if (chan == d->chan) + return; + + if (!test_bit(FLAG_DEFER_SETUP, &chan->flags)) + return; + + pid = chan->ops->get_peer_pid(chan); + + /* Only count deferred channels with the same PID/PSM */ + if (d->pid != pid || chan->psm != d->chan->psm || chan->ident || + chan->mode != L2CAP_MODE_EXT_FLOWCTL || chan->state != BT_CONNECT) + return; + + d->count++; +} + int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst, u8 dst_type) { @@ -7813,6 +7913,22 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, goto done; } + if (chan->mode == L2CAP_MODE_EXT_FLOWCTL) { + struct l2cap_chan_data data; + + data.chan = chan; + data.pid = chan->ops->get_peer_pid(chan); + data.count = 0; + + l2cap_chan_list(conn, l2cap_chan_by_pid, &data); + /* Check if there isn't too many channels being connected */ + if (!(data.count < L2CAP_ECRED_CONN_SCID_MAX - 1)) { + hci_conn_drop(hcon); + err = -EPROTO; + goto done; + } + } + mutex_lock(&conn->chan_lock); l2cap_chan_lock(chan); From patchwork Fri Mar 20 20:40:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 11450327 X-Patchwork-Delegate: marcel@holtmann.org Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6725C139A for ; Fri, 20 Mar 2020 20:40:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 46B6220775 for ; Fri, 20 Mar 2020 20:40:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Pbwy+ehT" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726983AbgCTUkc (ORCPT ); Fri, 20 Mar 2020 16:40:32 -0400 Received: from mail-pj1-f68.google.com ([209.85.216.68]:38115 "EHLO mail-pj1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726829AbgCTUkc (ORCPT ); Fri, 20 Mar 2020 16:40:32 -0400 Received: by mail-pj1-f68.google.com with SMTP id m15so2992695pje.3 for ; Fri, 20 Mar 2020 13:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=GkNcLk5if4krhXBmuI6AKGR91mSf4kdJ65783r5tpkE=; b=Pbwy+ehTubqCEWox6ekNmHhGwam9JzO9zOXRJ6Nd1dZ/SnDypubuslgrwnREoaXYpQ /kUfLYh92NCmVCwEzivFfYv4Y+NYuKr0T7XjJVkbYQqznGf6rKPxoleZ0GxYg8p+S7rl 717T9nRfXr7zlf3r5EZo2/md3XOvEqpeKpTXfgT2nkZH7YVIM6Ih/vYJvh1rNerHaTvz Cdb5mNCYr9NDSeQfYX2yISvMGDZSwXUrrf5GSXDzSmAwwONYX23z5GI5S9jEL/Uc8nc0 HtDxGakUS0x516a65ew6VSKwORxR5cM8O+YBPE/f7CK8vlFQSzyyO8nPP0uCBwg3nR8p ez1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GkNcLk5if4krhXBmuI6AKGR91mSf4kdJ65783r5tpkE=; b=kKO4X4TGTUXUWSP975Y8jzyEB1icOSvHCwUVobNJpr5+7deQPc/Gxfr0k6xv9Gpx4l teXK2FM9+MMRJ0/bry3qPZGKPCTmzx6yoUzGkQk8Mr2QY9I9Ascb99ux62GYop+4sH5S adnRa0KxQuABvK/Bxk7mG0cfRHIl6iCsDL+ABnR2S4ZdMqSWBkt9MwCciVCN5y+DsMkN wGbH1e95arIujW6YXsJTHgkYuw/G4F0DLpuotGxnT292R+pJZ1Bv0yND83qnAtZYCizI 1hczZUR3oLflsQVeCyD7JDRNEdmu0r15F7WbTYIGazh3LGwYkEECL/vdvfcic1sdzYW2 xPCg== X-Gm-Message-State: ANhLgQ15lz7B4rCVpNf0ZM4LeLpejjorr+3oCHL76WAtmELNci97l0KC qMBRafrDUv1ySAKvDX3b9WDvEEJC X-Google-Smtp-Source: ADFU+vse0e+qdbk1s3UgUTmIidWUq8QgEqHC4Y5hZs5GctV00jIIBhkBIxF/cZvn39ev9aRJRqMKaw== X-Received: by 2002:a17:902:b713:: with SMTP id d19mr10543796pls.270.1584736830914; Fri, 20 Mar 2020 13:40:30 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id e187sm6048744pfe.143.2020.03.20.13.40.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Mar 2020 13:40:30 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v4 3/3] Bluetooth: Add BT_MODE socket option Date: Fri, 20 Mar 2020 13:40:26 -0700 Message-Id: <20200320204026.11680-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200320204026.11680-1-luiz.dentz@gmail.com> References: <20200320204026.11680-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This adds BT_MODE socket option which can be used to set L2CAP modes, including modes only supported over LE which were not supported using the L2CAP_OPTIONS. Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/bluetooth.h | 11 ++++ include/net/bluetooth/l2cap.h | 1 + net/bluetooth/l2cap_sock.c | 99 +++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h index 1576353a2773..61694be4ade5 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -139,6 +139,17 @@ struct bt_voice { #define BT_PHY_LE_CODED_TX 0x00002000 #define BT_PHY_LE_CODED_RX 0x00004000 +#define BT_MODE 15 + +#define BT_MODE_BASIC 0x00 +#define BT_MODE_ERTM 0x01 +#define BT_MODE_STREAMING 0x02 +#define BT_MODE_LE_FLOWCTL 0x03 +#define BT_MODE_EXT_FLOWCTL 0x04 + +/* Only used internally to detect if L2CAP_OPTIONS was used */ +#define BT_MODE_L2CAP_OPTIONS 0xff + __printf(1, 2) void bt_info(const char *fmt, ...); __printf(1, 2) diff --git a/include/net/bluetooth/l2cap.h b/include/net/bluetooth/l2cap.h index dada14d0622c..27a398f7a28d 100644 --- a/include/net/bluetooth/l2cap.h +++ b/include/net/bluetooth/l2cap.h @@ -723,6 +723,7 @@ struct l2cap_user { struct l2cap_pinfo { struct bt_sock bt; struct l2cap_chan *chan; + u8 mode; struct sk_buff *rx_busy_skb; }; diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c index 117ba20ea194..d687392812a0 100644 --- a/net/bluetooth/l2cap_sock.c +++ b/net/bluetooth/l2cap_sock.c @@ -624,6 +624,29 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname, err = -EFAULT; break; + case BT_MODE: + if (!enable_ecred) { + err = -ENOPROTOOPT; + break; + } + + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { + err = -EINVAL; + break; + } + + /* Check if old sockopt was used which is not allowed since + * not all modes are not compatible. + */ + if (l2cap_pi(sk)->mode == BT_MODE_L2CAP_OPTIONS) { + err = -EINVAL; + break; + } + + if (put_user(l2cap_pi(sk)->mode, (u8 __user *) optval)) + err = -EFAULT; + break; + default: err = -ENOPROTOOPT; break; @@ -715,6 +738,8 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, break; } + l2cap_pi(sk)->mode = BT_MODE_L2CAP_OPTIONS; + BT_DBG("mode 0x%2.2x", chan->mode); chan->imtu = opts.imtu; @@ -763,6 +788,45 @@ static int l2cap_sock_setsockopt_old(struct socket *sock, int optname, return err; } +static int l2cap_set_mode(struct l2cap_chan *chan, u8 mode) +{ + switch (mode) { + case BT_MODE_BASIC: + if (bdaddr_type_is_le(chan->src_type)) + return -EINVAL; + mode = L2CAP_MODE_BASIC; + clear_bit(CONF_STATE2_DEVICE, &chan->conf_state); + break; + case BT_MODE_ERTM: + if (!disable_ertm || bdaddr_type_is_le(chan->src_type)) + return -EINVAL; + mode = L2CAP_MODE_ERTM; + break; + case BT_MODE_STREAMING: + if (!disable_ertm || bdaddr_type_is_le(chan->src_type)) + return -EINVAL; + mode = L2CAP_MODE_STREAMING; + break; + case BT_MODE_LE_FLOWCTL: + if (!bdaddr_type_is_le(chan->src_type)) + return -EINVAL; + mode = L2CAP_MODE_LE_FLOWCTL; + break; + case BT_MODE_EXT_FLOWCTL: + /* TODO: Add support for ECRED PDUs to BR/EDR */ + if (!bdaddr_type_is_le(chan->src_type)) + return -EINVAL; + mode = L2CAP_MODE_EXT_FLOWCTL; + break; + default: + return -EINVAL; + } + + chan->mode = mode; + + return 0; +} + static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) { @@ -968,6 +1032,41 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, break; + case BT_MODE: + if (!enable_ecred) { + err = -ENOPROTOOPT; + break; + } + + BT_DBG("sk->sk_state %u", sk->sk_state); + + if (sk->sk_state != BT_BOUND) { + err = -EINVAL; + break; + } + + if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { + err = -EINVAL; + break; + } + + if (get_user(opt, (u8 __user *) optval)) { + err = -EFAULT; + break; + } + + BT_DBG("opt %u", opt); + + err = l2cap_set_mode(chan, opt); + if (err) + break; + + l2cap_pi(sk)->mode = opt; + + BT_DBG("mode 0x%2.2x", chan->mode); + + break; + default: err = -ENOPROTOOPT; break;