Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1508886pxb; Thu, 14 Apr 2022 07:41:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcjQhi09Sz/2eANzB8H+jmY7teeT77slEvnwUVSpL9ahK+qDWJgEawV/rt2abL5479/0lO X-Received: by 2002:aa7:df92:0:b0:41d:84fd:5d92 with SMTP id b18-20020aa7df92000000b0041d84fd5d92mr3371336edy.337.1649947277775; Thu, 14 Apr 2022 07:41:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649947277; cv=none; d=google.com; s=arc-20160816; b=usA/YJZInTehZl7x5EBp2YabRiIjX0MCvWN8caoxZFK+n5W//o1umMbf3qhOPDw3A7 6q/efdOmLBnrRrEgqDcC9coMue4N3a4OpKUyAdT6piU/POjYv/aDtRrjVxLFkWn1w1WK E+NOr+itsDeZeOPs12UDP4mdfPTGnF+YXr4lukPsbKgHh8NPxahZf4qT+em5I/tFI810 oUVydHs3YQxtE88Nahckl9buON/71O06MNlce1kQ0HXZc95WpI8zbyFJwFkzRYKc2SxO OnELnv37WjEdLR3oAV/PKoDgbFpOqf+tpHUN06u4OQEZXB8ixmu2+0hzW8BFM0YE+glA ObJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MyuV2ejM3EZJoNjQSkskelEubJjJKEoQ8hVNMMGTKWg=; b=K5CVsY9bi/hOGW+BBjx+9ybhFLoXknz+8Wae8kdZO7zU26qHHqng3EYcRfsF7u619o W/4+7r/ijV8eKSWitS9mmuYQucjq82sCSpQlqwBaJopX9HTkl3tlQpPSTKrvWgLtIVbw UNKEz2ZGN072ryLkwtf9lJRuVm/C34/dLaUtm0rRRt1012bDQQPNCuGik6ACCCYBCalo BoQAcXauzMnOg1gb7gOan+dKTPXGq7V+ZbYKbVhE8wnFT4lt8uvFxCCm/HzKAY0KPn4N 6qFCB6ZL1FU/cxrvwbCZTnm2YK24xlNORG6oNPjRhXza1fKk2uuOHiWqZFkxfh4Dv3so pi0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=R4BJf9+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u19-20020a50c053000000b00418c2b5bd29si3757078edd.11.2022.04.14.07.40.52; Thu, 14 Apr 2022 07:41:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@siemens.com header.s=fm1 header.b=R4BJf9+O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=siemens.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241979AbiDNJqo (ORCPT + 99 others); Thu, 14 Apr 2022 05:46:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241904AbiDNJqP (ORCPT ); Thu, 14 Apr 2022 05:46:15 -0400 X-Greylist: delayed 61 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 14 Apr 2022 02:43:51 PDT Received: from mta-64-225.siemens.flowmailer.net (mta-64-225.siemens.flowmailer.net [185.136.64.225]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED8C670CD9 for ; Thu, 14 Apr 2022 02:43:51 -0700 (PDT) Received: by mta-64-225.siemens.flowmailer.net with ESMTPSA id 20220414094243d9d98a6f0992892ece for ; Thu, 14 Apr 2022 11:42:48 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=daniel.starke@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc:References:In-Reply-To; bh=MyuV2ejM3EZJoNjQSkskelEubJjJKEoQ8hVNMMGTKWg=; b=R4BJf9+OOBsvdZoC5eSSAb9myKqgdbBVM2mNVyTAE9CWEeWsHKhcDG8XBsDHByrCaKt7RE u2HzNV62ZaamvMTbNRsc7oPOK9Mcyr0epyOEV2//i0pWxRT0PDWysZfCEpxjzCPMAYt2dtzd WgMUM+jy6jQqtM9j98hDTePHbQx7E=; From: "D. Starke" To: linux-serial@vger.kernel.org, gregkh@linuxfoundation.org, jirislaby@kernel.org Cc: linux-kernel@vger.kernel.org, Daniel Starke Subject: [PATCH 06/20] tty: n_gsm: fix frame reception handling Date: Thu, 14 Apr 2022 02:42:11 -0700 Message-Id: <20220414094225.4527-6-daniel.starke@siemens.com> In-Reply-To: <20220414094225.4527-1-daniel.starke@siemens.com> References: <20220414094225.4527-1-daniel.starke@siemens.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-314044:519-21489:flowmailer X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Daniel Starke The frame checksum (FCS) is currently handled in gsm_queue() after reception of a frame. However, this breaks layering. A workaround with 'received_fcs' was implemented so far. Furthermore, frames are handled as such even if no end flag was received. Move FCS calculation from gsm_queue() to gsm0_receive() and gsm1_receive(). Also delay gsm_queue() call there until a full frame was received to fix both points. Fixes: e1eaea46bb40 ("tty: n_gsm line discipline") Cc: stable@vger.kernel.org Signed-off-by: Daniel Starke --- drivers/tty/n_gsm.c | 53 +++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c index 3ba2505908e3..4ce18b42c37a 100644 --- a/drivers/tty/n_gsm.c +++ b/drivers/tty/n_gsm.c @@ -219,7 +219,6 @@ struct gsm_mux { int encoding; u8 control; u8 fcs; - u8 received_fcs; u8 *txframe; /* TX framing buffer */ /* Method for the receiver side */ @@ -1794,18 +1793,7 @@ static void gsm_queue(struct gsm_mux *gsm) u8 cr; int address; int i, j, k, address_tmp; - /* We have to sneak a look at the packet body to do the FCS. - A somewhat layering violation in the spec */ - if ((gsm->control & ~PF) == UI) - gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len); - if (gsm->encoding == 0) { - /* WARNING: gsm->received_fcs is used for - gsm->encoding = 0 only. - In this case it contain the last piece of data - required to generate final CRC */ - gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs); - } if (gsm->fcs != GOOD_FCS) { gsm->bad_fcs++; if (debug & 4) @@ -1993,19 +1981,25 @@ static void gsm0_receive(struct gsm_mux *gsm, unsigned char c) break; case GSM_DATA: /* Data */ gsm->buf[gsm->count++] = c; - if (gsm->count == gsm->len) + if (gsm->count == gsm->len) { + /* Calculate final FCS for UI frames over all data */ + if ((gsm->control & ~PF) != UIH) { + gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, + gsm->count); + } gsm->state = GSM_FCS; + } break; case GSM_FCS: /* FCS follows the packet */ - gsm->received_fcs = c; - gsm_queue(gsm); + gsm->fcs = gsm_fcs_add(gsm->fcs, c); gsm->state = GSM_SSOF; break; case GSM_SSOF: - if (c == GSM0_SOF) { - gsm->state = GSM_SEARCH; - break; - } + gsm->state = GSM_SEARCH; + if (c == GSM0_SOF) + gsm_queue(gsm); + else + gsm->bad_size++; break; default: pr_debug("%s: unhandled state: %d\n", __func__, gsm->state); @@ -2024,11 +2018,24 @@ static void gsm0_receive(struct gsm_mux *gsm, unsigned char c) static void gsm1_receive(struct gsm_mux *gsm, unsigned char c) { if (c == GSM1_SOF) { - /* EOF is only valid in frame if we have got to the data state - and received at least one byte (the FCS) */ - if (gsm->state == GSM_DATA && gsm->count) { - /* Extract the FCS */ + /* EOF is only valid in frame if we have got to the data state */ + if (gsm->state == GSM_DATA) { + if (gsm->count < 1) { + /* Missing FSC */ + gsm->malformed++; + gsm->state = GSM_START; + return; + } + /* Remove the FCS from data */ gsm->count--; + if ((gsm->control & ~PF) != UIH) { + /* Calculate final FCS for UI frames over all + * data but FCS + */ + gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, + gsm->count); + } + /* Add the FCS itself to test against GOOD_FCS */ gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]); gsm->len = gsm->count; gsm_queue(gsm); -- 2.25.1