linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johan Hedberg <johan.hedberg@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH 08/31] Bluetooth: Add initial code for LE L2CAP Connect Request
Date: Wed,  4 Dec 2013 16:11:04 +0200	[thread overview]
Message-ID: <1386166287-13693-9-git-send-email-johan.hedberg@gmail.com> (raw)
In-Reply-To: <1386166287-13693-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

This patch adds the necessary code to send an LE L2CAP Connect Request
and handle its response when user space has provided us with an LE
socket with a PSM instead of a fixed CID.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 net/bluetooth/l2cap_core.c | 106 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 98 insertions(+), 8 deletions(-)

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index d250d8af7fd6..256662578f89 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1160,21 +1160,51 @@ static void l2cap_chan_ready(struct l2cap_chan *chan)
 	chan->ops->ready(chan);
 }
 
+static void l2cap_le_connect(struct l2cap_chan *chan)
+{
+	struct l2cap_conn *conn = chan->conn;
+	struct l2cap_le_conn_req req;
+
+	req.psm     = chan->psm;
+	req.scid    = cpu_to_le16(chan->scid);
+	req.mtu     = cpu_to_le16(chan->imtu);
+	req.mps     = __constant_cpu_to_le16(L2CAP_LE_DEFAULT_MPS);
+	req.credits = __constant_cpu_to_le16(L2CAP_LE_MAX_CREDITS);
+
+	chan->ident = l2cap_get_ident(conn);
+
+	l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
+		       sizeof(req), &req);
+}
+
+static void l2cap_le_start(struct l2cap_chan *chan)
+{
+	struct l2cap_conn *conn = chan->conn;
+
+	if (!smp_conn_security(conn->hcon, chan->sec_level))
+		return;
+
+	if (!chan->psm) {
+		l2cap_chan_ready(chan);
+		return;
+	}
+
+	if (chan->state == BT_CONNECT)
+		l2cap_le_connect(chan);
+}
+
 static void l2cap_start_connection(struct l2cap_chan *chan)
 {
 	if (__amp_capable(chan)) {
 		BT_DBG("chan %p AMP capable: discover AMPs", chan);
 		a2mp_discover_amp(chan);
+	} else if (chan->conn->hcon->type == LE_LINK) {
+		l2cap_le_start(chan);
 	} else {
 		l2cap_send_conn_req(chan);
 	}
 }
 
-static void l2cap_le_start(struct l2cap_chan *chan)
-{
-	l2cap_chan_ready(chan);
-}
-
 static void l2cap_do_start(struct l2cap_chan *chan)
 {
 	struct l2cap_conn *conn = chan->conn;
@@ -1438,9 +1468,7 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
 		}
 
 		if (hcon->type == LE_LINK) {
-			if (smp_conn_security(hcon, chan->sec_level))
-				l2cap_chan_ready(chan);
-
+			l2cap_le_start(chan);
 		} else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
 			l2cap_chan_ready(chan);
 
@@ -5210,6 +5238,64 @@ static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
 	return 0;
 }
 
+static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
+				struct l2cap_cmd_hdr *cmd, u16 cmd_len,
+				u8 *data)
+{
+	struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
+	u16 dcid, mtu, mps, credits, result;
+	struct l2cap_chan *chan;
+	int err;
+
+	if (cmd_len < sizeof(*rsp))
+		return -EPROTO;
+
+	dcid    = __le16_to_cpu(rsp->dcid);
+	mtu     = __le16_to_cpu(rsp->mtu);
+	mps     = __le16_to_cpu(rsp->mps);
+	credits = __le16_to_cpu(rsp->credits);
+	result  = __le16_to_cpu(rsp->result);
+
+	if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23))
+		return -EPROTO;
+
+	BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
+	       dcid, mtu, mps, credits, result);
+
+	mutex_lock(&conn->chan_lock);
+
+	chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
+	if (!chan) {
+		err = -EBADSLT;
+		goto unlock;
+	}
+
+	err = 0;
+
+	l2cap_chan_lock(chan);
+
+	switch (result) {
+	case L2CAP_CR_SUCCESS:
+		chan->ident = 0;
+		chan->dcid = dcid;
+		chan->omtu = mtu;
+		chan->remote_mps = mps;
+		l2cap_chan_ready(chan);
+		break;
+
+	default:
+		l2cap_chan_del(chan, ECONNREFUSED);
+		break;
+	}
+
+	l2cap_chan_unlock(chan);
+
+unlock:
+	mutex_unlock(&conn->chan_lock);
+
+	return err;
+}
+
 static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
 				      struct l2cap_cmd_hdr *cmd, u16 cmd_len,
 				      u8 *data)
@@ -5304,6 +5390,10 @@ static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
 	case L2CAP_CONN_PARAM_UPDATE_RSP:
 		return 0;
 
+	case L2CAP_LE_CONN_RSP:
+		l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
+		return 0;
+
 	default:
 		BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
 		return -EINVAL;
-- 
1.8.4.2


  parent reply	other threads:[~2013-12-04 14:11 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-04 14:10 [PATCH 00/31] Bluetooth: LE CoC support Johan Hedberg
2013-12-04 14:10 ` [PATCH 01/31] Bluetooth: Remove unnecessary braces from one-line if-statement Johan Hedberg
2013-12-04 14:10 ` [PATCH 02/31] Bluetooth: Add module parameter to enable LE CoC support Johan Hedberg
2013-12-04 14:10 ` [PATCH 03/31] Bluetooth: Update l2cap_global_chan_by_psm() to take a link type Johan Hedberg
2013-12-04 14:11 ` [PATCH 04/31] Bluetooth: Allow l2cap_chan_check_security() to be used for LE links Johan Hedberg
2013-12-04 14:11 ` [PATCH 05/31] Bluetooth: Pass command length to LE signaling channel handlers Johan Hedberg
2013-12-04 14:11 ` [PATCH 06/31] Bluetooth: Move LE L2CAP initiator procedure to its own function Johan Hedberg
2013-12-04 14:11 ` [PATCH 07/31] Bluetooth: Add definitions for LE connection oriented channels Johan Hedberg
2013-12-04 14:11 ` Johan Hedberg [this message]
2013-12-04 14:11 ` [PATCH 09/31] Bluetooth: Add smp_sufficient_security helper function Johan Hedberg
2013-12-04 14:11 ` [PATCH 10/31] Bluetooth: Refactor L2CAP connect rejection to its own function Johan Hedberg
2013-12-04 16:14   ` Vinicius Costa Gomes
2013-12-04 14:11 ` [PATCH 11/31] Bluetooth: Add basic LE L2CAP connect request receiving support Johan Hedberg
2013-12-04 14:11 ` [PATCH 12/31] Bluetooth: Fix L2CAP channel closing for LE connections Johan Hedberg
2013-12-04 14:11 ` [PATCH 13/31] Bluetooth: Add L2CAP Disconnect suppport for LE Johan Hedberg
2013-12-04 14:11 ` [PATCH 14/31] Bluetooth: Make l2cap_le_sig_cmd logic consistent Johan Hedberg
2013-12-04 14:11 ` [PATCH 15/31] Bluetooth: Add LE L2CAP flow control mode Johan Hedberg
2013-12-04 14:11 ` [PATCH 16/31] Bluetooth: Track LE L2CAP credits in l2cap_chan Johan Hedberg
2013-12-04 14:11 ` [PATCH 17/31] Bluetooth: Limit L2CAP_OPTIONS socket option usage with LE Johan Hedberg
2013-12-04 14:11 ` [PATCH 18/31] Bluetooth: Add new BT_SNDMTU and BT_RCVMTU socket options Johan Hedberg
2013-12-04 14:11 ` [PATCH 19/31] Bluetooth: Implement returning of LE L2CAP credits Johan Hedberg
2013-12-04 16:15   ` Vinicius Costa Gomes
2013-12-04 14:11 ` [PATCH 20/31] Bluetooth: Add LE flow control discipline Johan Hedberg
2013-12-04 14:11 ` [PATCH 21/31] Bluetooth: Reject LE CoC commands when the feature is not enabled Johan Hedberg
2013-12-04 14:11 ` [PATCH 22/31] Bluetooth: Introduce L2CAP channel callback for suspending Johan Hedberg
2013-12-04 14:11 ` [PATCH 23/31] Bluetooth: Add LE L2CAP segmentation support for outgoing data Johan Hedberg
2013-12-04 14:11 ` [PATCH 24/31] Bluetooth: Implement LE L2CAP reassembly Johan Hedberg
2013-12-04 14:11 ` [PATCH 25/31] Bluetooth: Fix LE L2CAP Connect Request handling together with SMP Johan Hedberg
2013-12-04 14:11 ` [PATCH 26/31] Bluetooth: Fix suspending the L2CAP socket if we start with 0 credits Johan Hedberg
2013-12-04 14:11 ` [PATCH 27/31] Bluetooth: Limit LE MPS to the MTU value Johan Hedberg
2013-12-04 14:11 ` [PATCH 28/31] Bluetooth: Fix clearing of chan->omtu for LE CoC channels Johan Hedberg
2013-12-04 14:11 ` [PATCH 29/31] Bluetooth: Fix CID ranges for LE CoC CID allocations Johan Hedberg
2013-12-04 14:11 ` [PATCH 30/31] Bluetooth: Fix validating LE PSM values Johan Hedberg
2013-12-04 14:11 ` [PATCH 31/31] Bluetooth: Add debugfs controls for LE CoC MPS and Credits Johan Hedberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1386166287-13693-9-git-send-email-johan.hedberg@gmail.com \
    --to=johan.hedberg@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).