linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marcel Holtmann <marcel@holtmann.org>
To: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [RFC] Bluetooth: L2CAP: Fix to handling fragmented header
Date: Tue, 28 Jul 2020 09:22:46 +0200	[thread overview]
Message-ID: <80073DFD-564E-4B4E-9F23-02ED4075321D@holtmann.org> (raw)
In-Reply-To: <20200728070428.1754257-1-luiz.dentz@gmail.com>

Hi Luiz,

> Bluetooth Core Specification v5.2, Vol. 3, Part A, section 1.4, table
> 1.1:
> 
> 'Start Fragments always either begin with the first octet of the Basic
>  L2CAP header of a PDU or they have a length of zero (see [Vol 2] Part
>  B, Section 6.6.2).'
> 
> This text has been changed recently as it previously stated:
> 
> 'Start Fragments always begin with the Basic L2CAP header of a PDU.'
> 
> Apparently this was changed by the following errata:
> 
> https://www.bluetooth.org/tse/errata_view.cfm?errata_id=10216
> 
> In past this has not been a problem but it seems new controllers are
> apparently doing it as it has been reported in Zephyr:
> 
> https://github.com/zephyrproject-rtos/zephyr/issues/26900
> 
> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
> ---
> net/bluetooth/l2cap_core.c | 104 +++++++++++++++++++++++++++++--------
> 1 file changed, 83 insertions(+), 21 deletions(-)
> 
> diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
> index ade83e224567..193bea314222 100644
> --- a/net/bluetooth/l2cap_core.c
> +++ b/net/bluetooth/l2cap_core.c
> @@ -8269,6 +8269,63 @@ static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
> 	mutex_unlock(&conn->chan_lock);
> }
> 
> +/* Append fragment into frame respecting the maximum len of rx_skb */
> +static int l2cap_recv_frag(struct l2cap_conn *conn, struct sk_buff *skb,
> +			   u16 len)
> +{
> +	if (!conn->rx_skb) {
> +		/* Allocate skb for the complete frame (with header) */
> +		conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
> +		if (!conn->rx_skb)
> +			return -ENOMEM;
> +		/* Init rx_len */
> +		conn->rx_len = len;
> +	}
> +
> +	/* Copy as much as the rx_skb can hold */
> +	len = min_t(u16, len, skb->len);
> +	skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, len), len);
> +	skb_pull(skb, len);
> +	conn->rx_len -= len;
> +
> +	return len;
> +}
> +
> +static int l2cap_recv_header(struct l2cap_conn *conn, struct sk_buff *skb)
> +{
> +	struct l2cap_hdr *hdr;
> +	struct sk_buff *rx_skb;
> +	int len;
> +
> +	/* Append just enough to complete the header */
> +	len = l2cap_recv_frag(conn, skb, L2CAP_HDR_SIZE - conn->rx_skb->len);
> +
> +	/* If header could not be read just continue */
> +	if (len < 0 || conn->rx_skb->len < L2CAP_HDR_SIZE)
> +		return len;
> +
> +	rx_skb = conn->rx_skb;
> +	conn->rx_skb = NULL;
> +
> +	hdr = (struct l2cap_hdr *) rx_skb->data;

so I think it is pointless to insist on getting the complete header. We really just need the first 2 octets.

struct l2cap_hdr {                                                               
        __le16     len;                                                          
        __le16     cid;                                                          
} __packed;

Once we have received at least 2 octets, we can get_unaligned_le16(rx_skb->data) and then just continue.

Regards

Marcel


  reply	other threads:[~2020-07-28  7:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-28  7:04 [RFC] Bluetooth: L2CAP: Fix to handling fragmented header Luiz Augusto von Dentz
2020-07-28  7:22 ` Marcel Holtmann [this message]
2020-07-28 16:39   ` Luiz Augusto von Dentz

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=80073DFD-564E-4B4E-9F23-02ED4075321D@holtmann.org \
    --to=marcel@holtmann.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    /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).