From patchwork Mon Oct 17 06:34:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Langrock X-Patchwork-Id: 3220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1303234wrs; Sun, 16 Oct 2022 23:51:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4lQZqED5K6M5WGQQxfjJGw2oo6vDAZaYyg+wTcL2s7YvpHp6DfFO+AqlqV7WeookCc8MKa X-Received: by 2002:aa7:9397:0:b0:562:cbf1:1186 with SMTP id t23-20020aa79397000000b00562cbf11186mr11213228pfe.5.1665989471375; Sun, 16 Oct 2022 23:51:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665989471; cv=none; d=google.com; s=arc-20160816; b=XBRfl49dVLF1RfYB/Nknxr133ZRWcPubqQwmEgJlYF0LT+y2fGGXFfc5PDYnXrbKnE ZED53mNazRCKGiekBMysScEASrOVPW27atguQGH1aAaeWcSFiHdHKgNYV6Zn1k3XqnUo yUVizVgUlZPkh6ns8HULHG/oSKtWNCVYOUC6du16PjZP2LdnVdO+Y/r7M7RCWjf6iI/R IbBK3kKyelwUBuft2xIxbJE9aeJBadnmUvfpvT8LU/awcUZYScD+bEQK5g8s9SqjQa0M dg3tEd4X3U2jBB8jeqi7Ej/J3YhFYIeakKZjg3H94Wwg0a7UEAnsd/XXrXXrXFLsRMF5 4Rgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=+EFnllj2iq+a1PORYUAvDGN3Ikqt4avWx9p7PMnnVyY=; b=n7YTKbIqgUC8lcSH/ngRrlc1YTD25FWovOWtwABECqlojBX2rzJQoTBQmB491cQ2NI BsVCbOi17eeTJu+85sT3e+FbkZSC5ztAYlrJoeZQg6Z2ObmgzavYAIvcqFIAO37+8Igb hYmHGVeRNHG8ufsi4FyL8zywhMa7tgVU81qeSYTz/BuKBzApY25X1ju91LoBf53tnjNU z9HYLdZHRxKrpt+zGUl3oIPPJvjOHSsdohSTcz8tvE5vnkkbf9aqWyjJwzLAnhKxVMUQ cmm5wvzDy5TsYH36Uj+zPE3qOVesuNYZLhHruL9GrcLV9yMLCXfL5UaH19I1JsnSux1c HxFQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l12-20020a056a0016cc00b005281e5606b2si11400670pfc.32.2022.10.16.23.50.58; Sun, 16 Oct 2022 23:51:11 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230001AbiJQGf3 (ORCPT + 99 others); Mon, 17 Oct 2022 02:35:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbiJQGf0 (ORCPT ); Mon, 17 Oct 2022 02:35:26 -0400 Received: from a.mx.secunet.com (a.mx.secunet.com [62.96.220.36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFC6E3499B; Sun, 16 Oct 2022 23:35:24 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by a.mx.secunet.com (Postfix) with ESMTP id BC62020527; Mon, 17 Oct 2022 08:35:22 +0200 (CEST) X-Virus-Scanned: by secunet Received: from a.mx.secunet.com ([127.0.0.1]) by localhost (a.mx.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eoMKP7F9VnJk; Mon, 17 Oct 2022 08:35:22 +0200 (CEST) Received: from mailout1.secunet.com (mailout1.secunet.com [62.96.220.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by a.mx.secunet.com (Postfix) with ESMTPS id 2727520096; Mon, 17 Oct 2022 08:35:22 +0200 (CEST) Received: from cas-essen-01.secunet.de (unknown [10.53.40.201]) by mailout1.secunet.com (Postfix) with ESMTP id 1749980004A; Mon, 17 Oct 2022 08:35:22 +0200 (CEST) Received: from mbx-essen-01.secunet.de (10.53.40.197) by cas-essen-01.secunet.de (10.53.40.201) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 08:35:21 +0200 Received: from christian-dev1.secunet.de (172.18.157.49) by mbx-essen-01.secunet.de (10.53.40.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 17 Oct 2022 08:35:15 +0200 From: Christian Langrock To: Steffen Klassert , Herbert Xu , "David S. Miller" , Hideaki YOSHIFUJI , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "open list:NETWORKING [IPSEC]" , open list CC: Christian Langrock Subject: [PATCH ipsec v7] xfrm: replay: Fix ESN wrap around for GSO Date: Mon, 17 Oct 2022 08:34:47 +0200 Message-ID: <20221017063447.1816366-1-christian.langrock@secunet.com> X-Mailer: git-send-email 2.37.1.223.g6a475b71f8 MIME-Version: 1.0 X-ClientProxiedBy: cas-essen-02.secunet.de (10.53.40.202) To mbx-essen-01.secunet.de (10.53.40.197) X-EXCLAIMER-MD-CONFIG: 2c86f778-e09b-4440-8b15-867914633a10 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746916576278346340?= X-GMAIL-MSGID: =?utf-8?q?1746916576278346340?= When using GSO it can happen that the wrong seq_hi is used for the last packets before the wrap around. This can lead to double usage of a sequence number. To avoid this, we should serialize this last GSO packet. Fixes: d7dbefc45cf5 ("xfrm: Add xfrm_replay_overflow functions for offloading") Co-developed-by: Steffen Klassert Signed-off-by: Christian Langrock --- Changes in v7: - Fix malformed mail Changes in v6: - move overflow check to offloading path to avoid locking issues Changes in v5: - Fix build Changes in v4: - move changelog within comment - add reviewer Changes in v3: - fix build - remove wrapper function Changes in v2: - switch to bool as return value - remove switch case in wrapper function --- net/ipv4/esp4_offload.c | 3 +++ net/ipv6/esp6_offload.c | 3 +++ net/xfrm/xfrm_device.c | 15 ++++++++++++++- net/xfrm/xfrm_replay.c | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/net/ipv4/esp4_offload.c b/net/ipv4/esp4_offload.c index 170152772d33..3969fa805679 100644 --- a/net/ipv4/esp4_offload.c +++ b/net/ipv4/esp4_offload.c @@ -314,6 +314,9 @@ static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_ xo->seq.low += skb_shinfo(skb)->gso_segs; } + if (xo->seq.low < seq) + xo->seq.hi++; + esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32)); ip_hdr(skb)->tot_len = htons(skb->len); diff --git a/net/ipv6/esp6_offload.c b/net/ipv6/esp6_offload.c index 79d43548279c..242f4295940e 100644 --- a/net/ipv6/esp6_offload.c +++ b/net/ipv6/esp6_offload.c @@ -346,6 +346,9 @@ static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features xo->seq.low += skb_shinfo(skb)->gso_segs; } + if (xo->seq.low < seq) + xo->seq.hi++; + esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32)); len = skb->len - sizeof(struct ipv6hdr); diff --git a/net/xfrm/xfrm_device.c b/net/xfrm/xfrm_device.c index 5f5aafd418af..21269e8f2db4 100644 --- a/net/xfrm/xfrm_device.c +++ b/net/xfrm/xfrm_device.c @@ -97,6 +97,18 @@ static void xfrm_outer_mode_prep(struct xfrm_state *x, struct sk_buff *skb) } } +static inline bool xmit_xfrm_check_overflow(struct sk_buff *skb) +{ + struct xfrm_offload *xo = xfrm_offload(skb); + __u32 seq = xo->seq.low; + + seq += skb_shinfo(skb)->gso_segs; + if (unlikely(seq < xo->seq.low)) + return true; + + return false; +} + struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features, bool *again) { int err; @@ -134,7 +146,8 @@ struct sk_buff *validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t featur return skb; } - if (skb_is_gso(skb) && unlikely(x->xso.dev != dev)) { + if (skb_is_gso(skb) && (unlikely(x->xso.dev != dev) || + unlikely(xmit_xfrm_check_overflow(skb)))) { struct sk_buff *segs; /* Packet got rerouted, fixup features and segment it. */ diff --git a/net/xfrm/xfrm_replay.c b/net/xfrm/xfrm_replay.c index 9f4d42eb090f..ce56d659c55a 100644 --- a/net/xfrm/xfrm_replay.c +++ b/net/xfrm/xfrm_replay.c @@ -714,7 +714,7 @@ static int xfrm_replay_overflow_offload_esn(struct xfrm_state *x, struct sk_buff oseq += skb_shinfo(skb)->gso_segs; } - if (unlikely(oseq < replay_esn->oseq)) { + if (unlikely(xo->seq.low < replay_esn->oseq)) { XFRM_SKB_CB(skb)->seq.output.hi = ++oseq_hi; xo->seq.hi = oseq_hi; replay_esn->oseq_hi = oseq_hi;