Skip to contentSkip to conversation

Bluetooth: L2CAP: Use DEFER_SETUP to group ECRED connections

PATCH In review Luiz Augusto von Dentz 2 participants 5 messages Mar 25, 2020

Subsystems (2)
lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v5 1/2 Bluetooth: L2CAP: Use DEFER_SETUP to group ECRED connections

From: Luiz Augusto von Dentz <[email protected]>

This uses the DEFER_SETUP flag to group channels with L2CAP_CREDIT_BASED_CONNECTION_REQ.

---
 include/net/bluetooth/l2cap.h |   4 +
 net/bluetooth/l2cap_core.c    | 137 +++++++++++++++++++++++++++++++---
 2 files changed, 131 insertions(+), 10 deletions(-)
include/net/bluetooth/l2cap.h +4 -0
index 2d7d28474d34..dada14d0622c 100644
@@ -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);
net/bluetooth/l2cap_core.c +127 -10

174 lines not on this page · raw message

Signature · 1 line
2.21.1
author maintainer
#1 lore raw

PATCH v5 2/2 Bluetooth: Add BT_MODE socket option

From: Luiz Augusto von Dentz <[email protected]>

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.

---
 include/net/bluetooth/bluetooth.h |   8 ++
 net/bluetooth/l2cap_sock.c        | 120 +++++++++++++++++++++++++++++-
 2 files changed, 127 insertions(+), 1 deletion(-)
include/net/bluetooth/bluetooth.h +8 -0
index 1576353a2773..3fa7b1e3c5d9 100644
@@ -139,6 +139,14 @@ 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
+
 __printf(1, 2)
 void bt_info(const char *fmt, ...);
 __printf(1, 2)
net/bluetooth/l2cap_sock.c +119 -1

156 lines not on this page · raw message

Signature · 1 line
2.21.1
maintainer
#2 lore raw

Re: [PATCH v5 2/2] Bluetooth: Add BT_MODE socket option

Hi Luiz,

Quoted 59 lines

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.

---
include/net/bluetooth/bluetooth.h | 8 ++
net/bluetooth/l2cap_sock.c | 120 +++++++++++++++++++++++++++++-

2 files changed, 127 insertions(+), 1 deletion(-)
include/net/bluetooth/bluetooth.h +8 -0
index 1576353a2773..3fa7b1e3c5d9 100644
@@ -139,6 +139,14 @@ 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
+
 __printf(1, 2)
 void bt_info(const char *fmt, ...);
 __printf(1, 2)
net/bluetooth/l2cap_sock.c +18 -0
index 117ba20ea194..07f8d60953f2 100644
@@ -395,6 +395,24 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
 	return sizeof(struct sockaddr_l2);
 }
 
+static int l2cap_get_mode(struct l2cap_chan *chan)
+{
+	switch (chan->mode) {
+	case L2CAP_MODE_BASIC:
+		return BT_MODE_BASIC;
+	case L2CAP_MODE_ERTM:
+		return BT_MODE_ERTM;
+	case L2CAP_MODE_STREAMING:
+		return BT_MODE_STREAMING;
+	case L2CAP_MODE_LE_FLOWCTL:
+		return BT_MODE_LE_FLOWCTL;
+	case L2CAP_MODE_EXT_FLOWCTL:
+		return BT_MODE_EXT_FLOWCTL;
+	}
+
+	return -EINVAL;
+}
+
 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
 				     char __user *optval, int __user *optlen)
 {

net/bluetooth/l2cap_sock.c static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, · line 449

Quoted 8 lines
net/bluetooth/l2cap_sock.c +5 -0
@@ -424,6 +442,13 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
 			break;
 		}
 
+		/* L2CAP_MODE_LE_FLOWCTL and L2CAP_MODE_EXT_FLOWCTL are not
+		 * supported by L2CAP_OPTIONS.
+		 */
+		if (chan->mode == L2CAP_MODE_LE_FLOWCTL ||
+				chan->mode == L2CAP_MODE_EXT_FLOWCTL)

this is the wrong kernel indentation.

Quoted 115 lines
net/bluetooth/l2cap_sock.c +86 -1
+			return -EINVAL;
+
 		memset(&opts, 0, sizeof(opts));
 		opts.imtu     = chan->imtu;
 		opts.omtu     = chan->omtu;
@@ -508,7 +533,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
 	struct bt_security sec;
 	struct bt_power pwr;
 	u32 phys;
-	int len, err = 0;
+	int len, mode, err = 0;
 
 	BT_DBG("sk %p", sk);
 
@@ -624,6 +649,27 @@ 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;
+		}
+
+		mode = l2cap_get_mode(chan);
+		if (mode < 0) {
+			err = mode;
+			break;
+		}
+
+		if (put_user(mode, (u8 __user *) optval))
+			err = -EFAULT;
+		break;
+
 	default:
 		err = -ENOPROTOOPT;
 		break;
@@ -763,6 +809,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 +1053,39 @@ 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;
+		}
+

net/bluetooth/l2cap_sock.c static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, · line 1088

Quoted 10 lines
net/bluetooth/l2cap_sock.c +10 -0
+		BT_DBG("opt %u", opt);
+
+		err = l2cap_set_mode(chan, opt);
+		if (err)
+			break;
+
+		BT_DBG("mode 0x%2.2x", chan->mode);
+
+		break;
+

So do we want BT_MODE as u8 or just simply as int?

Regards

Marcel

author maintainer
#3 lore raw

Re: [PATCH v5 2/2] Bluetooth: Add BT_MODE socket option

Hi Marcel,

Quoted 65 lines · Marcel Holtmann

Hi Luiz,

Quoted 62 lines

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.

---
include/net/bluetooth/bluetooth.h | 8 ++
net/bluetooth/l2cap_sock.c | 120 +++++++++++++++++++++++++++++-

2 files changed, 127 insertions(+), 1 deletion(-)
include/net/bluetooth/bluetooth.h +8 -0
index 1576353a2773..3fa7b1e3c5d9 100644
@@ -139,6 +139,14 @@ 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
+
 __printf(1, 2)
 void bt_info(const char *fmt, ...);
 __printf(1, 2)
net/bluetooth/l2cap_sock.c +18 -0
index 117ba20ea194..07f8d60953f2 100644
@@ -395,6 +395,24 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
      return sizeof(struct sockaddr_l2);
 }
 
+static int l2cap_get_mode(struct l2cap_chan *chan)
+{
+     switch (chan->mode) {
+     case L2CAP_MODE_BASIC:
+             return BT_MODE_BASIC;
+     case L2CAP_MODE_ERTM:
+             return BT_MODE_ERTM;
+     case L2CAP_MODE_STREAMING:
+             return BT_MODE_STREAMING;
+     case L2CAP_MODE_LE_FLOWCTL:
+             return BT_MODE_LE_FLOWCTL;
+     case L2CAP_MODE_EXT_FLOWCTL:
+             return BT_MODE_EXT_FLOWCTL;
+     }
+
+     return -EINVAL;
+}
+
 static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
                                   char __user *optval, int __user *optlen)
 {
@@ -424,6 +442,13 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
                      break;
              }
 
Quoted 7 lines

net/bluetooth/l2cap_sock.c static int l2cap_sock_getsockopt_old(struct socket *sock, int optname, · line 449

Quoted 5 lines
net/bluetooth/l2cap_sock.c +5 -0
+             /* L2CAP_MODE_LE_FLOWCTL and L2CAP_MODE_EXT_FLOWCTL are not
+              * supported by L2CAP_OPTIONS.
+              */
+             if (chan->mode == L2CAP_MODE_LE_FLOWCTL ||
+                             chan->mode == L2CAP_MODE_EXT_FLOWCTL)

this is the wrong kernel indentation.

Fixed

Quoted 119 lines
Quoted 119 lines
net/bluetooth/l2cap_sock.c +90 -1
+                     return -EINVAL;
+
              memset(&opts, 0, sizeof(opts));
              opts.imtu     = chan->imtu;
              opts.omtu     = chan->omtu;
@@ -508,7 +533,7 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
      struct bt_security sec;
      struct bt_power pwr;
      u32 phys;
-     int len, err = 0;
+     int len, mode, err = 0;
 
      BT_DBG("sk %p", sk);
 
@@ -624,6 +649,27 @@ 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;
+             }
+
+             mode = l2cap_get_mode(chan);
+             if (mode < 0) {
+                     err = mode;
+                     break;
+             }
+
+             if (put_user(mode, (u8 __user *) optval))
+                     err = -EFAULT;
+             break;
+
      default:
              err = -ENOPROTOOPT;
              break;
@@ -763,6 +809,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 +1053,39 @@ 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)
Quoted 8 lines

net/bluetooth/l2cap_sock.c static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname, · line 1088

Quoted 6 lines
net/bluetooth/l2cap_sock.c +6 -0
+                     break;
+
+             BT_DBG("mode 0x%2.2x", chan->mode);
+
+             break;
+

So do we want BT_MODE as u8 or just simply as int?

For the mode alone I guess it would be fine, the question is if we want to leave space to add other fields later but I guess we could do that with use of new options instead.

Quoted 4 lines

Regards

Marcel

Signature · 1 line
Luiz Augusto von Dentz