From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.251.105.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796400264; b=AdK7TNx3gz/nsubh1YtJNrDgp21bk2Ko4aikbIvn3wDKkjPwLxwTkpEujL8nQODlnK7+ 0j0hlMO5eb15DuqrurtUCD+kriuqDYOWZXQVIUJhTx9++IjCRMMNrjjxFkE/rT0VirHhY U0SsDyQMehyRJdKtFFjpL+5Rybj3+JOuIEoWbv1AMDt2soSyilmTWdGW8ZEsdMUTX05s7 YkUvFg3RATeE42OY80nt1YOfP8MuCYrcPYPjZNcLz0uNNyuBOsEHDnz1sHHph7/nCgRF0 DvTtBfPRf0ogPH1jqKNJYtYP2bqBKQLwFcDiCUiXLteUdKqagerJAZ7JjqF2YwaU/zA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796400264; h=DMARC-Filter:DKIM-Signature:Received:From:To:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=oMw8BkYz5taqp/5gugI2kOc48WynbpqS6Zd13hzD/Uo=; b=RzfY89mGM2vN/9koqqzdpuQi7FIBBzM6y5X13XlbWhNIgd1L9ZxrPsROgmH8XKKgF+ze 3OMwikG13c3u/0f0YYihRVh86UC2wso4mqYgK8klHHDYkfGS6bZMq1qRS2TQRLHBIZ+ku ObhIq8PmGdMoELsmpMhX42AegtMIgtphsxtB2qbglAG4kwyp3j6ewLtMBDLjAqlfrQ0PC qL+ZZLf9UVpcNh4Hv5kBOAL65qUp/FWeYr2WwMCoErd5zb3WowbRtQZQDFes5X+NhW1QE ehHW8tp/97Yu5luCLi0EiD6voLBmup5bjqfIA74YLFoa9cPPbJ3VJavRNcA7TKLkTtQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=Wy9Tjj0G; arc=none smtp.remote-ip=148.251.105.195 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C585C606A32F for ; Fri, 4 Dec 2026 16:04:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C585C606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=mail header.b=Wy9Tjj0G DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1796400261; bh=/EAIrTDumLFo6f0vYlG92RhLi2tM6wVOG0aT5pcDl2A=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Wy9Tjj0G2/dpqxVZU4zKO14jDjRprfptZcrh6SGBiin95WqYOaI/acMXJS3Ml3hOe NGShsoKsALCcCpsIC0YRJaXPAYN44GHVhWonBCHhUvfdbtBFd5gIsauMZeLLmVgjNS lVGbzRKSIQT+Zg7b5f73hyI12BA2B4+xI7tXrCxnmjAer7gKCKCn8JUGKBgZTZRmba cIjNyB0PtW7MDu4BtT9Dw6k+ZMhJg79z5EHVudx/v/y+KNViCBf6cfcCZjdl1pACh8 POWr3c/tcrfWUXRVEChrH08tkR47Pwb8TnQkGY/lyeAcN27tQH/BHon7hyMm99T2Ar J1YDbGwsfEKdg== Received: from fdanis-ThinkPad-X1.. (unknown [100.64.1.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: fdanis) by bali.collaboradmins.com (Postfix) with ESMTPSA id 8730D17E09D8 for ; Fri, 04 Dec 2026 17:04:21 +0100 (CET) From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Danis?= To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v8 1/6] monitor: Strip trailing L2CAP FCS before upper layer dissectors Date: Fri, 4 Dec 2026 17:04:11 +0100 Message-ID: <20261204160416.1254097-2-frederic.danis@collabora.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204160416.1254097-1-frederic.danis@collabora.com> References: <20261204160416.1254097-1-frederic.danis@collabora.com> X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit From: Frédéric Danis ERTM and Streaming mode frames end with a 2-byte FCS, which was passed to the upper layer dissectors as part of the payload. Track the FCS option negotiated for each channel and remove the FCS from ERTM/Streaming mode frames before PSM dispatch. FCS is used unless both directions negotiate "No FCS", so the option is tracked per direction. An FCS option from a Config Request or Response only takes effect once the final Config Response fragment reports success, and is dropped on failure. Convert the Config Response result to host byte order before comparing it, as the comparisons were wrong on big-endian hosts. Assisted-by: Claude:claude-sonnet-5 --- v3->v4: - Track the FCS value requested by a Config Request and apply it on a successful Config Response that doesn't repeat it (a successful response need not repeat accepted options), instead of only reading the FCS option when the response itself carries it. v4->v5: - Discard a pending requested FCS value when its Config Request is rejected with Unknown Options (discard_req_fcs()), so a later, unrelated successful Config Response in the same renegotiation doesn't apply the rejected value. v5->v6: - Extend discard_req_fcs() to any final failure result (Unacceptable Parameters, Rejected, Flow Spec Rejected), not just Unknown Options, leaving the pending value alone while the result is Pending or the response is a non-final fragment. - Only apply a Config Response's FCS option (print_config_options()) on a definitively successful result, instead of on any response, so a failed negotiation doesn't change the channel's tracked FCS mode. v6->v7: - Compare the Config Response result against le16_to_cpu(pdu->result) instead of the raw wire value, so a big-endian host no longer misreads e.g. a Pending (0x0004) response as a final failure. - Stage a Config Response's own FCS option (rsp_fcs) instead of applying it as soon as a fragment's result reads Success, and commit it with the rest of the response's final result handling, so a non-final fragment reading Success no longer changes the channel's FCS mode ahead of a later fragment that actually rejects the configuration. v7->v8: - Track the FCS option per direction, as FCS is only disabled once both directions negotiate "No FCS". - Also discard the staged Config Response FCS option on Unknown Options, and discard both staged values once committed, so a later Config Response can't apply them again. - Trim comments and commit message. monitor/l2cap.c | 243 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 238 insertions(+), 5 deletions(-) diff --git a/monitor/l2cap.c b/monitor/l2cap.c index ddc2388d5..114037336 100644 --- a/monitor/l2cap.c +++ b/monitor/l2cap.c @@ -100,6 +100,10 @@ struct chan_data { uint8_t ext_ctrl; uint8_t seq_num; uint16_t sdu; + uint8_t fcs; + uint8_t fcs_dir[2]; + int8_t req_fcs[2]; + int8_t rsp_fcs[2]; struct packet_latency tx_l; }; @@ -158,6 +162,15 @@ static void assign_scid(const struct l2cap_frame *frame, uint16_t scid, chan_list[n].ctrlid = ctrlid; chan_list[n].mode = mode; + /* FCS is used unless both directions negotiate "No FCS" */ + chan_list[n].fcs = 1; + chan_list[n].fcs_dir[0] = 1; + chan_list[n].fcs_dir[1] = 1; + chan_list[n].req_fcs[0] = -1; + chan_list[n].req_fcs[1] = -1; + chan_list[n].rsp_fcs[0] = -1; + chan_list[n].rsp_fcs[1] = -1; + chan_list[n].seq_num = seq_num; } @@ -255,6 +268,189 @@ static void assign_mode(const struct l2cap_frame *frame, } } +/* A successful Config Response doesn't need to repeat the FCS option of + * the Request, so keep it until the final response, per direction. + */ +static void assign_req_fcs(const struct l2cap_frame *frame, + uint8_t fcs, uint16_t dcid) +{ + int i; + int dir = frame->in ? 1 : 0; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == dcid) { + chan_list[i].req_fcs[dir] = fcs; + break; + } + } else { + if (chan_list[i].dcid == dcid) { + chan_list[i].req_fcs[dir] = fcs; + break; + } + } + } +} + +/* Called with the Response frame, sent in the opposite direction of the + * Request. + */ +static void apply_req_fcs(const struct l2cap_frame *frame, uint16_t scid) +{ + int i; + int dir = frame->in ? 0 : 1; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == scid) { + if (chan_list[i].req_fcs[dir] >= 0) + chan_list[i].fcs_dir[dir] = + chan_list[i].req_fcs[dir]; + chan_list[i].fcs = chan_list[i].fcs_dir[0] || + chan_list[i].fcs_dir[1]; + break; + } + } else { + if (chan_list[i].dcid == scid) { + if (chan_list[i].req_fcs[dir] >= 0) + chan_list[i].fcs_dir[dir] = + chan_list[i].req_fcs[dir]; + chan_list[i].fcs = chan_list[i].fcs_dir[0] || + chan_list[i].fcs_dir[1]; + break; + } + } + } +} + +static void discard_req_fcs(const struct l2cap_frame *frame, uint16_t scid) +{ + int i; + int dir = frame->in ? 0 : 1; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == scid) { + chan_list[i].req_fcs[dir] = -1; + break; + } + } else { + if (chan_list[i].dcid == scid) { + chan_list[i].req_fcs[dir] = -1; + break; + } + } + } +} + +/* A Config Response option only takes effect once the final fragment + * reports success. + */ +static void assign_rsp_fcs(const struct l2cap_frame *frame, + uint8_t fcs, uint16_t dcid) +{ + int i; + int dir = frame->in ? 0 : 1; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == dcid) { + chan_list[i].rsp_fcs[dir] = fcs; + break; + } + } else { + if (chan_list[i].dcid == dcid) { + chan_list[i].rsp_fcs[dir] = fcs; + break; + } + } + } +} + +static void apply_rsp_fcs(const struct l2cap_frame *frame, uint16_t scid) +{ + int i; + int dir = frame->in ? 0 : 1; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == scid) { + if (chan_list[i].rsp_fcs[dir] >= 0) + chan_list[i].fcs_dir[dir] = + chan_list[i].rsp_fcs[dir]; + chan_list[i].fcs = chan_list[i].fcs_dir[0] || + chan_list[i].fcs_dir[1]; + break; + } + } else { + if (chan_list[i].dcid == scid) { + if (chan_list[i].rsp_fcs[dir] >= 0) + chan_list[i].fcs_dir[dir] = + chan_list[i].rsp_fcs[dir]; + chan_list[i].fcs = chan_list[i].fcs_dir[0] || + chan_list[i].fcs_dir[1]; + break; + } + } + } +} + +static void discard_rsp_fcs(const struct l2cap_frame *frame, uint16_t scid) +{ + int i; + int dir = frame->in ? 0 : 1; + + for (i = 0; i < MAX_CHAN; i++) { + if (chan_list[i].index != frame->index) + continue; + + if (chan_list[i].handle != frame->handle) + continue; + + if (frame->in) { + if (chan_list[i].scid == scid) { + chan_list[i].rsp_fcs[dir] = -1; + break; + } + } else { + if (chan_list[i].dcid == scid) { + chan_list[i].rsp_fcs[dir] = -1; + break; + } + } + } +} + static int get_chan_data_index(const struct l2cap_frame *frame) { int i; @@ -797,7 +993,8 @@ static void print_unknown_options(const struct l2cap_frame *source, } static void print_config_options(const struct l2cap_frame *frame, - uint8_t offset, uint16_t cid, bool response) + uint8_t offset, uint16_t cid, bool response, + bool success) { const uint8_t *data = frame->data + offset; uint16_t size = frame->size - offset; @@ -882,6 +1079,14 @@ static void print_config_options(const struct l2cap_frame *frame, get_le16(data + consumed + 9)); break; case 0x05: + if (response) { + if (success) + assign_rsp_fcs(frame, + data[consumed + 2], + cid); + } else + assign_req_fcs(frame, data[consumed + 2], cid); + switch (data[consumed + 2]) { case 0x00: str = "No FCS"; @@ -1178,20 +1383,43 @@ static void sig_config_req(const struct l2cap_frame *frame) print_cid("Destination", pdu->dcid); print_config_flags(pdu->flags); - print_config_options(frame, 4, le16_to_cpu(pdu->dcid), false); + print_config_options(frame, 4, le16_to_cpu(pdu->dcid), false, false); } static void sig_config_rsp(const struct l2cap_frame *frame) { const struct bt_l2cap_pdu_config_rsp *pdu = frame->data; + bool final = !(le16_to_cpu(pdu->flags) & 0x0001); + uint16_t result = le16_to_cpu(pdu->result); + bool success = result == 0x0000; print_cid("Source", pdu->scid); print_config_flags(pdu->flags); print_config_result(pdu->result); - if (pdu->result == 0x0003) + if (result == 0x0003) { + if (final) { + discard_req_fcs(frame, le16_to_cpu(pdu->scid)); + discard_rsp_fcs(frame, le16_to_cpu(pdu->scid)); + } print_unknown_options(frame, 6); - else - print_config_options(frame, 6, le16_to_cpu(pdu->scid), true); + } else { + print_config_options(frame, 6, le16_to_cpu(pdu->scid), true, + success); + + /* Pending (0x0004) isn't final */ + if (final) { + if (success) { + apply_req_fcs(frame, le16_to_cpu(pdu->scid)); + apply_rsp_fcs(frame, le16_to_cpu(pdu->scid)); + + discard_req_fcs(frame, le16_to_cpu(pdu->scid)); + discard_rsp_fcs(frame, le16_to_cpu(pdu->scid)); + } else if (result != 0x0004) { + discard_req_fcs(frame, le16_to_cpu(pdu->scid)); + discard_rsp_fcs(frame, le16_to_cpu(pdu->scid)); + } + } + } } static void sig_disconn_req(const struct l2cap_frame *frame) @@ -2740,6 +2968,11 @@ void l2cap_frame(uint16_t index, bool in, uint16_t handle, uint16_t cid, } display_printf("\n"); + + chan = get_chan(&frame); + if (chan && chan->fcs && frame.size >= 2) + frame.size -= 2; + break; } -- 2.43.0