linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: johan.hedberg@gmail.com
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2 24/32] Bluetooth: Implement LE L2CAP reassembly
Date: Thu,  5 Dec 2013 15:11:22 +0200	[thread overview]
Message-ID: <1386249090-10236-25-git-send-email-johan.hedberg@gmail.com> (raw)
In-Reply-To: <1386249090-10236-1-git-send-email-johan.hedberg@gmail.com>

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

When receiving fragments over an LE Connection oriented Channel they
need to be collected up and eventually merged into a single SDU. This
patch adds the necessary code for collecting up the fragment skbs to the
channel context and passing them to the recv() callback when the entire
SDU has been received.

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

diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 9869faa2904d..ae90dd284c87 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -6816,18 +6816,91 @@ static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
 
 static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
 {
-	if (!chan->rx_credits)
+	int err;
+
+	if (!chan->rx_credits) {
+		BT_ERR("No credits to receive LE L2CAP data");
 		return -ENOBUFS;
+	}
 
-	if (chan->imtu < skb->len)
+	if (chan->imtu < skb->len) {
+		BT_ERR("Too big LE L2CAP PDU");
 		return -ENOBUFS;
+	}
 
 	chan->rx_credits--;
 	BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
 
 	l2cap_chan_le_send_credits(chan);
 
-	return chan->ops->recv(chan, skb);
+	err = 0;
+
+	if (!chan->sdu) {
+		u16 sdu_len;
+
+		sdu_len = get_unaligned_le16(skb->data);
+		skb_pull(skb, L2CAP_SDULEN_SIZE);
+
+		BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
+		       sdu_len, skb->len, chan->imtu);
+
+		if (sdu_len > chan->imtu) {
+			BT_ERR("Too big LE L2CAP SDU length received");
+			err = -EMSGSIZE;
+			goto failed;
+		}
+
+		if (skb->len > sdu_len) {
+			BT_ERR("Too much LE L2CAP data received");
+			err = -EINVAL;
+			goto failed;
+		}
+
+		if (skb->len == sdu_len)
+			return chan->ops->recv(chan, skb);
+
+		chan->sdu = skb;
+		chan->sdu_len = sdu_len;
+		chan->sdu_last_frag = skb;
+
+		return 0;
+	}
+
+	BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
+	       chan->sdu->len, skb->len, chan->sdu_len);
+
+	if (chan->sdu->len + skb->len > chan->sdu_len) {
+		BT_ERR("Too much LE L2CAP data received");
+		err = -EINVAL;
+		goto failed;
+	}
+
+	append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
+	skb = NULL;
+
+	if (chan->sdu->len == chan->sdu_len) {
+		err = chan->ops->recv(chan, chan->sdu);
+		if (!err) {
+			chan->sdu = NULL;
+			chan->sdu_last_frag = NULL;
+			chan->sdu_len = 0;
+		}
+	}
+
+failed:
+	if (err) {
+		kfree_skb(skb);
+		kfree_skb(chan->sdu);
+		chan->sdu = NULL;
+		chan->sdu_last_frag = NULL;
+		chan->sdu_len = 0;
+	}
+
+	/* We can't return an error here since we took care of the skb
+	 * freeing internally. An error return would cause the caller to
+	 * do a double-free of the skb.
+	 */
+	return 0;
 }
 
 static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
-- 
1.8.4.2


  parent reply	other threads:[~2013-12-05 13:11 UTC|newest]

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

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=1386249090-10236-25-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).