From patchwork Thu Jun 23 12:42:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Han X-Patchwork-Id: 12892333 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02CAAC43334 for ; Thu, 23 Jun 2022 12:51:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231570AbiFWMvK (ORCPT ); Thu, 23 Jun 2022 08:51:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231564AbiFWMvJ (ORCPT ); Thu, 23 Jun 2022 08:51:09 -0400 X-Greylist: delayed 248 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 23 Jun 2022 05:51:03 PDT Received: from out203-205-221-202.mail.qq.com (out203-205-221-202.mail.qq.com [203.205.221.202]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C8C645AF7; Thu, 23 Jun 2022 05:51:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1655988661; bh=WkPDR7AEUWbovUynFMyUZJb/WXXDveepQJRcNB/88sY=; h=Date:From:To:Cc:Subject; b=eThYzUm9Qka+IrJHeUkUmW1NCcRfGySimih5DaiBE6qcjMX4/P6POR84Ur8Met9Ao zU7IWvVj5Bsp3t+a+zBO5oyy+brRI+06ixl5zKsgf1OxDWqG8KsIGR3gsUfoSPXRXU /V5gpispz8eQWo6gLRdnCIHAzRmmp0CSwGf2jOcw= Received: from wh-VirtualBox ([117.175.169.40]) by newxmesmtplogicsvrsza5.qq.com (NewEsmtp) with SMTP id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8mAhvHuJoXe5NZRe/1lyt+6wSIvxUzyGq7 Date: Thu, 23 Jun 2022 20:42:48 +0800 From: Wei Han To: pablo@netfilter.org, kadlec@netfilter.org, fw@strlen.de, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com Cc: netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, lailitty@foxmail.com, linux-staging@lists.linux.dev Subject: [PATCH] netfilter: xt_esp: add support for ESP match in NAT Traversal X-OQ-MSGID: <20220623124248.GA10755@wh-VirtualBox> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org when the ESP packets traversing Network Address Translators, which are encapsulated and decapsulated inside UDP packets, so we need to get ESP data in UDP. Signed-off-by: Wei Han --- net/netfilter/xt_esp.c | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/net/netfilter/xt_esp.c b/net/netfilter/xt_esp.c index 2a1c0ad0ff07..c3feb79a830a 100644 --- a/net/netfilter/xt_esp.c +++ b/net/netfilter/xt_esp.c @@ -8,12 +8,14 @@ #include #include #include +#include #include #include #include #include +#include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Yon Uriarte "); @@ -39,17 +41,53 @@ static bool esp_mt(const struct sk_buff *skb, struct xt_action_param *par) struct ip_esp_hdr _esp; const struct xt_esp *espinfo = par->matchinfo; + const struct iphdr *iph = NULL; + const struct ipv6hdr *ip6h = NULL; + const struct udphdr *udph = NULL; + struct udphdr _udph; + int proto = -1; + /* Must not be a fragment. */ if (par->fragoff != 0) return false; - eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); - if (eh == NULL) { - /* We've been asked to examine this packet, and we - * can't. Hence, no choice but to drop. - */ - pr_debug("Dropping evil ESP tinygram.\n"); - par->hotdrop = true; + if (xt_family(par) == NFPROTO_IPV6) { + ip6h = ipv6_hdr(skb); + if (!ip6h) + return false; + proto = ip6h->nexthdr; + } else { + iph = ip_hdr(skb); + if (!iph) + return false; + proto = iph->protocol; + } + + if (proto == IPPROTO_UDP) { + //for NAT-T + udph = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph); + if (udph && (udph->source == htons(4500) || udph->dest == htons(4500))) { + /* Not deal with above data it don't conflict with SPI + * 1.IKE Header Format for Port 4500(Non-ESP Marker 0x00000000) + * 2.NAT-Keepalive Packet Format(0xFF) + */ + eh = (struct ip_esp_hdr *)((char *)udph + sizeof(struct udphdr)); + } else { + return false; + } + } else if (proto == IPPROTO_ESP) { + //not NAT-T + eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp); + if (!eh) { + /* We've been asked to examine this packet, and we + * can't. Hence, no choice but to drop. + */ + pr_debug("Dropping evil ESP tinygram.\n"); + par->hotdrop = true; + return false; + } + } else { + //not esp data return false; } @@ -76,7 +114,6 @@ static struct xt_match esp_mt_reg[] __read_mostly = { .checkentry = esp_mt_check, .match = esp_mt, .matchsize = sizeof(struct xt_esp), - .proto = IPPROTO_ESP, .me = THIS_MODULE, }, { @@ -85,7 +122,6 @@ static struct xt_match esp_mt_reg[] __read_mostly = { .checkentry = esp_mt_check, .match = esp_mt, .matchsize = sizeof(struct xt_esp), - .proto = IPPROTO_ESP, .me = THIS_MODULE, }, };