Skip to contentSkip to conversation

sco: BT_DEFER_SETUP for SCO sockets

PATCH Superseded Frédéric Dalleau 2 participants 9 messages Nov 19, 2012 – Nov 20, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover sco: BT_DEFER_SETUP for SCO sockets

Hi,

This is a better view about what can be done to implement DEFER_SETUP on SCO sockets. hci layer get some changes since previous behavior was to accept all SCO connections.
The ugly hci_proto_defer has been removed and replaced by an additional flag parameter to hci_proto_connect_ind.
Regarding testing, I'm still stuck by a txt timeout issue however, if the delay between accept and recv is set to 0, then the connection is working fine. And I even managed to get the sco connection established correctly once. So I believed this emulator problem.
Now I'm trying without emulator.

Let me know what you think.

Best regards,
Frédéric

Frédéric Dalleau (2):
Bluetooth: Add BT_DEFER_SETUP option to sco socket
Bluetooth: Implement deferred sco socket setup

 include/net/bluetooth/hci_core.h |    9 +++--
 net/bluetooth/hci_event.c        |   53 +++++++++++++++++++++++++++---
 net/bluetooth/sco.c              |   67 ++++++++++++++++++++++++++++++++++++--
 3 files changed, 119 insertions(+), 10 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/2 Bluetooth: Add BT_DEFER_SETUP option to sco socket

This option will set the BT_SK_DEFER_SETUP bit in socket flags.

---
 net/bluetooth/sco.c |   32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
net/bluetooth/sco.c +32 -0
index 450cdcd..c6678f2 100644
@@ -666,12 +666,31 @@ static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char
 {
 	struct sock *sk = sock->sk;
 	int err = 0;
+	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;
+
 	default:
 		err = -ENOPROTOOPT;
 		break;
@@ -753,6 +772,19 @@ static int sco_sock_getsockopt(struct socket *sock, int level, int optname, char
 	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;
+
 	default:
 		err = -ENOPROTOOPT;
 		break;
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH 2/2 Bluetooth: Implement deferred sco socket setup

In reply to Frédéric Dalleau

In order to authenticate and configure an incoming SCO connection, the BT_DEFER_SETUP option was added. This option is intended to defer reply to Connect Request on SCO sockets.
When a connection is requested, the listening socket is unblocked but the effective connection setup happens only on first recv. Any send between accept and recv fails with -ENOTCONN.

---
 include/net/bluetooth/hci_core.h |    9 ++++---
 net/bluetooth/hci_event.c        |   53 +++++++++++++++++++++++++++++++++++---
 net/bluetooth/sco.c              |   35 ++++++++++++++++++++++---
 3 files changed, 87 insertions(+), 10 deletions(-)
include/net/bluetooth/hci_core.h +6 -3
index ef5b85d..8fceb35 100644
@@ -376,7 +376,7 @@ extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
 extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
 			      u16 flags);
 
-extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
+extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
 extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
 extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
 extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
@@ -577,6 +577,7 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
 int hci_conn_del(struct hci_conn *conn);
 void hci_conn_hash_flush(struct hci_dev *hdev);
 void hci_conn_check_pending(struct hci_dev *hdev);
+void hci_conn_accept(struct hci_conn *conn, int mask);
 
 struct hci_chan *hci_chan_create(struct hci_conn *conn);
 void hci_chan_del(struct hci_chan *chan);
@@ -779,8 +780,10 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define lmp_host_le_br_capable(dev) ((dev)->host_features[0] & LMP_HOST_LE_BREDR)
 
 /* ----- HCI protocols ----- */
+#define HCI_PROTO_DEFER             0x01
+
 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
-								__u8 type)
+						__u8 type, __u8 *flags)
 {
 	switch (type) {
 	case ACL_LINK:
@@ -788,7 +791,7 @@ static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
 
 	case SCO_LINK:
 	case ESCO_LINK:
-		return sco_connect_ind(hdev, bdaddr);
+		return sco_connect_ind(hdev, bdaddr, flags);
 
 	default:
 		BT_ERR("unknown link type %d", type);
net/bluetooth/hci_event.c +49 -4

90 lines not on this page · raw message

net/bluetooth/sco.c +32 -3
index c6678f2..77210bf 100644
@@ -397,6 +397,7 @@ static void sco_sock_init(struct sock *sk, struct sock *parent)
 
 	if (parent) {
 		sk->sk_type = parent->sk_type;
+		bt_sk(sk)->flags = bt_sk(parent)->flags;
 		security_sk_clone(parent, sk);
 	}
 }
@@ -662,6 +663,28 @@ static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
 	return err;
 }
 
+static int sco_sock_recvmsg(struct kiocb *iocb, 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)) {
+		hci_conn_accept(pi->conn->hcon, 0);
+		sk->sk_state = BT_CONFIG;
+
+		release_sock(sk);
+		return 0;
+	}
+
+	release_sock(sk);
+
+	return bt_sock_recvmsg(iocb, 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;
@@ -906,7 +929,10 @@ static void sco_conn_ready(struct sco_conn *conn)
 		hci_conn_hold(conn->hcon);
 		__sco_chan_add(conn, sk, parent);
 
-		sk->sk_state = BT_CONNECTED;
+		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, 1);
@@ -919,7 +945,7 @@ done:
 }
 
 /* ----- SCO interface with lower layer (HCI) ----- */
-int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
+int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
 {
 	struct sock *sk;
 	struct hlist_node *node;
@@ -936,6 +962,9 @@ int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
 		if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) ||
 		    !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
 			lm |= HCI_LM_ACCEPT;
+
+			if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
+				*flags |= HCI_PROTO_DEFER;
 			break;
 		}
 	}
@@ -1024,7 +1053,7 @@ static const struct proto_ops sco_sock_ops = {
 	.accept		= sco_sock_accept,
 	.getname	= sco_sock_getname,
 	.sendmsg	= sco_sock_sendmsg,
-	.recvmsg	= bt_sock_recvmsg,
+	.recvmsg	= sco_sock_recvmsg,
 	.poll		= bt_sock_poll,
 	.ioctl		= bt_sock_ioctl,
 	.mmap		= sock_no_mmap,
Signature · 1 line
1.7.9.5
#3 lore raw

Re: [PATCH 2/2] Bluetooth: Implement deferred sco socket setup

Hi Frédéric,

Quoted 34 lines · Frédéric Dalleau

In order to authenticate and configure an incoming SCO connection, the BT_DEFER_SETUP option was added. This option is intended to defer reply to Connect Request on SCO sockets.
When a connection is requested, the listening socket is unblocked but the effective connection setup happens only on first recv. Any send between accept and recv fails with -ENOTCONN.

---
 include/net/bluetooth/hci_core.h |    9 ++++---
 net/bluetooth/hci_event.c        |   53 +++++++++++++++++++++++++++++++++++---
 net/bluetooth/sco.c              |   35 ++++++++++++++++++++++---
 3 files changed, 87 insertions(+), 10 deletions(-)
include/net/bluetooth/hci_core.h +2 -1
index ef5b85d..8fceb35 100644
@@ -376,7 +376,7 @@ extern int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt);
 extern int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb,
 			      u16 flags);
 
-extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr);
+extern int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags);
 extern void sco_connect_cfm(struct hci_conn *hcon, __u8 status);
 extern void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason);
 extern int sco_recv_scodata(struct hci_conn *hcon, struct sk_buff *skb);
@@ -577,6 +577,7 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst);
 int hci_conn_del(struct hci_conn *conn);
 void hci_conn_hash_flush(struct hci_dev *hdev);
 void hci_conn_check_pending(struct hci_dev *hdev);
+void hci_conn_accept(struct hci_conn *conn, int mask);
 
 struct hci_chan *hci_chan_create(struct hci_conn *conn);
 void hci_chan_del(struct hci_chan *chan);

include/net/bluetooth/hci_core.h void hci_conn_del_sysfs(struct hci_conn *conn); · line 786

Quoted 8 lines
include/net/bluetooth/hci_core.h +3 -1
@@ -779,8 +780,10 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 #define lmp_host_le_br_capable(dev) ((dev)->host_features[0] & LMP_HOST_LE_BREDR)
 
 /* ----- HCI protocols ----- */
+#define HCI_PROTO_DEFER             0x01
+
 static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
-								__u8 type)
+						__u8 type, __u8 *flags)

Please fix the coding style here.

Quoted 29 lines
include/net/bluetooth/hci_core.h +1 -1
 {
 	switch (type) {
 	case ACL_LINK:
@@ -788,7 +791,7 @@ static inline int hci_proto_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr,
 
 	case SCO_LINK:
 	case ESCO_LINK:
-		return sco_connect_ind(hdev, bdaddr);
+		return sco_connect_ind(hdev, bdaddr, flags);
 
 	default:
 		BT_ERR("unknown link type %d", type);
net/bluetooth/hci_event.c +9 -0
index 9f5c5f2..f883ef5 100644
@@ -2047,15 +2047,54 @@ unlock:
 	hci_conn_check_pending(hdev);
 }
 
+void hci_conn_accept(struct hci_conn *conn, int mask)
+{
+	struct hci_dev *hdev = conn->hdev;
+
+	BT_DBG("conn %p", conn);
+
+	if (!lmp_esco_capable(hdev)) {
+		struct hci_cp_accept_conn_req cp;
+

net/bluetooth/hci_event.c unlock: · line 2068

Quoted 10 lines
net/bluetooth/hci_event.c +10 -0
+		conn->state = BT_CONFIG;
+		bacpy(&cp.bdaddr, &conn->dst);
+
+		if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
+			cp.role = 0x00; /* Become master */
+		else
+			cp.role = 0x01; /* Remain slave */
+
+		hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp),
+			     &cp);

I don't think you need to break a line for this hci_send_cmd(), it won't cross the column 80.

Quoted 87 lines
net/bluetooth/hci_event.c +30 -4
+	} else /* lmp_esco_capable(hdev)) */ {
+		struct hci_cp_accept_sync_conn_req cp;
+
+		conn->state = BT_CONFIG;
+		bacpy(&cp.bdaddr, &conn->dst);
+		cp.pkt_type = cpu_to_le16(conn->pkt_type);
+
+		cp.tx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
+		cp.rx_bandwidth   = __constant_cpu_to_le32(0x00001f40);
+		cp.max_latency    = __constant_cpu_to_le16(0xffff);
+		cp.content_format = cpu_to_le16(hdev->voice_setting);
+		cp.retrans_effort = 0xff;
+
+		hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
+			     sizeof(cp), &cp);
+	}
+}
+
 static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 {
 	struct hci_ev_conn_request *ev = (void *) skb->data;
 	int mask = hdev->link_mode;
+	__u8 flags = 0;
 
 	BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
 	       ev->link_type);
 
-	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type);
+	mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
+				      &flags);
 
 	if ((mask & HCI_LM_ACCEPT) &&
 	    !hci_blacklist_lookup(hdev, &ev->bdaddr)) {
@@ -2081,12 +2120,13 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		}
 
 		memcpy(conn->dev_class, ev->dev_class, 3);
-		conn->state = BT_CONNECT;
 
 		hci_dev_unlock(hdev);
 
-		if (ev->link_type == ACL_LINK || !lmp_esco_capable(hdev)) {
+		if (ev->link_type == ACL_LINK ||
+		    (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
 			struct hci_cp_accept_conn_req cp;
+			conn->state = BT_CONNECT;
 
 			bacpy(&cp.bdaddr, &ev->bdaddr);
 
@@ -2097,8 +2137,9 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 			hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp),
 				     &cp);
-		} else {
+		} else if (!(flags & HCI_PROTO_DEFER)) {
 			struct hci_cp_accept_sync_conn_req cp;
+			conn->state = BT_CONNECT;
 
 			bacpy(&cp.bdaddr, &ev->bdaddr);
 			cp.pkt_type = cpu_to_le16(conn->pkt_type);
@@ -2111,6 +2152,10 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
 
 			hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ,
 				     sizeof(cp), &cp);
+		} else {
+			conn->state = BT_CONNECT2;
+			hci_proto_connect_cfm(conn, 0);
+			hci_conn_put(conn);
 		}
 	} else {
 		/* Connection rejected */
net/bluetooth/sco.c +1 -0
index c6678f2..77210bf 100644
@@ -397,6 +397,7 @@ static void sco_sock_init(struct sock *sk, struct sock *parent)
 
 	if (parent) {
 		sk->sk_type = parent->sk_type;
+		bt_sk(sk)->flags = bt_sk(parent)->flags;
 		security_sk_clone(parent, sk);
 	}
 }
@@ -662,6 +663,28 @@ static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
 	return err;
 }
 

net/bluetooth/sco.c static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, · line 675

Quoted 10 lines
net/bluetooth/sco.c +10 -0
+static int sco_sock_recvmsg(struct kiocb *iocb, 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)) {

Wrong coding style here.

net/bluetooth/sco.c static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, · line 677

Quoted 2 lines
net/bluetooth/sco.c +2 -0
+		hci_conn_accept(pi->conn->hcon, 0);
+		sk->sk_state = BT_CONFIG;

Where do you set sk_state to BT_CONNECTED. Doesn't it need to be here?

Gustavo

#4 lore raw

Re: [PATCH 2/2] Bluetooth: Implement deferred sco socket setup

Hi Gustavo,

I'm taking your remarks into account asap

Quoted 1 line · Gustavo Padovan

Where do you set sk_state to BT_CONNECTED. Doesn't it need to be here?

The state is set to BT_CONNECTED in hci_sync_conn_complete_evt. ie when the connection is effectively created.

My understanding is that BT_CONFIG state indicate the configuration has been requested, in case recvmsg() is called twice.

Regards,
Frédéric

#5 lore raw

Re: [PATCH 2/2] Bluetooth: Implement deferred sco socket setup

Quoted 8 lines · Frédéric Dalleau

Hi Gustavo,

I'm taking your remarks into account asap

Quoted 1 line · Gustavo Padovan

Where do you set sk_state to BT_CONNECTED. Doesn't it need to be here?

The state is set to BT_CONNECTED in hci_sync_conn_complete_evt. ie when the connection is effectively created.

Are we both talking about the sk_state var? I failed to see how it is set to BT_CONNECTED.

Gustavo

#6 lore raw

Re: [PATCH 2/2] Bluetooth: Implement deferred sco socket setup

Hi,

Quoted 6 lines · Gustavo Padovan
Quoted 4 lines · Frédéric Dalleau

Hi Gustavo,

I'm taking your remarks into account asap

Quoted 7 lines
Quoted 4 lines · Gustavo Padovan
Quoted 1 line

Where do you set sk_state to BT_CONNECTED. Doesn't it need to be here?

The state is set to BT_CONNECTED in hci_sync_conn_complete_evt. ie when the connection is effectively created.

Are we both talking about the sk_state var? I failed to see how it is set to BT_CONNECTED.

Yes, hci_sync_conn_complete_evt > hci_proto_connect_cfm > sco_connect_cfm >

if there is no error, sco_conn_ready.
Since the socket is already created we get in first if branch.

Frédéric

#7 lore raw
In reply to Frédéric Dalleau

Hi Frédéric,

Quoted 11 lines · Frédéric Dalleau

Hi,

This is a better view about what can be done to implement DEFER_SETUP on SCO sockets. hci layer get some changes since previous behavior was to accept all SCO connections.
The ugly hci_proto_defer has been removed and replaced by an additional flag parameter to hci_proto_connect_ind.
Regarding testing, I'm still stuck by a txt timeout issue however, if the delay between accept and recv is set to 0, then the connection is working fine. And I even managed to get the sco connection established correctly once. So I believed this emulator problem.

You are telling me that your patch is not really working, I suggest you call this series RFC before you get everything sorted out. The approach in general is good, but I'd need confirmation that it works for actual defer delays, 3 seconds for example, and not 0.

Gustavo

#8 lore raw

Hi Gustavo,

Quoted 8 lines · Gustavo Padovan

Hi Frédéric,

Quoted 4 lines · Frédéric Dalleau

Regarding testing, I'm still stuck by a txt timeout issue however, if the delay between accept and recv is set to 0, then the connection is working fine. And I even managed to get the sco connection established correctly once. So I believed this emulator problem.

Quoted 4 lines

You are telling me that your patch is not really working, I suggest you call this series RFC before you get everything sorted out. The approach in general is good, but I'd need confirmation that it works for actual defer delays, 3 seconds for example, and not 0.

Sorry, I have been a bit premature, but for the reason explained
above I was confident.
So now I'm quite happy to say I successfully tested this series this morning, based on bluetooth-next, using delays from 0 to 35 secs several times in a row.

Regards,
Frédéric