Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6179801iog; Thu, 23 Jun 2022 13:02:38 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tk59a//WrfIiP2q5Mc83bPBsMEcAKnlPSP1EHWMP2iEyVs7Oqf8Ho4q6fS6a08mktEGRaq X-Received: by 2002:a63:9701:0:b0:40c:a588:b488 with SMTP id n1-20020a639701000000b0040ca588b488mr8772136pge.303.1656014557772; Thu, 23 Jun 2022 13:02:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656014557; cv=none; d=google.com; s=arc-20160816; b=s9ruulFkX8fjmGtySzUTMM5RpA/MwK96YJmopYu4E/R0eBf0SH3N2vHG7UxVxRAlPK ugIJVYeY6y7qIClwy8YUtVL4WIGUJzOMxbrBuUWN0M+UfyVZJwEy3uonLaq5xjk8WKQC lkgjgu6pegzahjQgiYw7rciHlieKngwk/Kan//E28igpSvz4SH3A+wGkahT6BC2bATGs 6ji4si4PiX0XsV10tzT1bL1ImkbfXuq5meWXYWUjC40CKsm6hjurx5wHCHNRNJbevi7T 1kaT/VAPA2G6urnQyg/N+QbCdG6ZbR6sf9j/FIEx+ElI1HyNJfKNnEdD5WCyj/a+MXGQ aVvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=4+DZBgQNJEW7Q6kXcVaIxW4C55KHor+7VOqv5Tm2iUs=; b=xw4zK3dCG4V0TQQGhL/rPhRH0sqTqoASTSUdP1Qv7dVzrPA342HXvc02LwBb1lUv2i k5jnPvnj3L6cpFWvIJK9ouT5b/PNNAxZUxvIdUa0q0AvE+nGfE0ozGafZ+jhl7OCSpEy znwA/thLVADflw34jYqYPHZq0MzKYqEfsm+4nnptz/j9JEC3L00lIa9peNsXIeC70Yeg m0WoLTfj5TdRdooxLa63GIrcG2JWDkhbI1yO9/hL4IbTuhIgQW5x3Gea9Jn/XwgblDd9 bc2c0aIl2zPpqKFETNmGtcsYbDG3LAwiWjLN0AevA1jZAm94AsRXp9w1ADoBmD4+7o2/ eiOg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x10-20020a056a00188a00b0051072f6d074si165614pfh.138.2022.06.23.13.02.24; Thu, 23 Jun 2022 13:02:37 -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 S231888AbiFWTom (ORCPT + 99 others); Thu, 23 Jun 2022 15:44:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbiFWToT (ORCPT ); Thu, 23 Jun 2022 15:44:19 -0400 Received: from mail.netfilter.org (mail.netfilter.org [217.70.188.207]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 83066262C; Thu, 23 Jun 2022 12:36:44 -0700 (PDT) Date: Thu, 23 Jun 2022 21:36:41 +0200 From: Pablo Neira Ayuso To: Wei Han Cc: kadlec@netfilter.org, fw@strlen.de, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-staging@lists.linux.dev Subject: Re: [PATCH] netfilter: xt_esp: add support for ESP match in NAT Traversal Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Thu, Jun 23, 2022 at 08:42:48PM +0800, Wei Han wrote: > 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)); this is not safe, skbuff might not be linear. > + } 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; > + } This is loose, the user does not have a way to restrict to either ESP over UDP or native ESP. I don't think this is going to look nice from iptables syntax perspective to restrict either one or another mode. > + } 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, > }, > }; > -- > 2.17.1 >