diff mbox

[ovs-dev,net-next,v3,3/8] netfilter: Allow calling into nat helper without skb_dst.

Message ID 1448496501-109561-4-git-send-email-jarno@ovn.org
State Not Applicable
Headers show

Commit Message

Jarno Rajahalme Nov. 26, 2015, 12:08 a.m. UTC
NAT checksum recalculation code assumes existence of skb_dst, which
becomes a problem for a later patch in the series ("openvswitch:
Interface with NAT.").  Simplify this by removing the check on
skb_dst, as the checksum will be dealt with later in the stack.

Suggested-by: Pravin Shelar <pshelar@nicira.com>
Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
---
 net/ipv4/netfilter/nf_nat_l3proto_ipv4.c | 29 ++++++++---------------------
 net/ipv6/netfilter/nf_nat_l3proto_ipv6.c | 29 ++++++++---------------------
 2 files changed, 16 insertions(+), 42 deletions(-)

Comments

Pablo Neira Ayuso Dec. 1, 2015, 8:51 p.m. UTC | #1
On Wed, Nov 25, 2015 at 04:08:16PM -0800, Jarno Rajahalme wrote:
> NAT checksum recalculation code assumes existence of skb_dst, which
> becomes a problem for a later patch in the series ("openvswitch:
> Interface with NAT.").  Simplify this by removing the check on
> skb_dst, as the checksum will be dealt with later in the stack.
> 
> Suggested-by: Pravin Shelar <pshelar@nicira.com>
> Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
> ---
>  net/ipv4/netfilter/nf_nat_l3proto_ipv4.c | 29 ++++++++---------------------
>  net/ipv6/netfilter/nf_nat_l3proto_ipv6.c | 29 ++++++++---------------------
>  2 files changed, 16 insertions(+), 42 deletions(-)
> 
> diff --git a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
> index 5075b7e..f8aad03 100644
> --- a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
> +++ b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
> @@ -127,28 +127,15 @@ static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
>  				    u8 proto, void *data, __sum16 *check,
>  				    int datalen, int oldlen)
>  {
> -	const struct iphdr *iph = ip_hdr(skb);
> -	struct rtable *rt = skb_rtable(skb);
> -
>  	if (skb->ip_summed != CHECKSUM_PARTIAL) {
> -		if (!(rt->rt_flags & RTCF_LOCAL) &&
> -		    (!skb->dev || skb->dev->features & NETIF_F_V4_CSUM)) {
> -			skb->ip_summed = CHECKSUM_PARTIAL;
> -			skb->csum_start = skb_headroom(skb) +
> -					  skb_network_offset(skb) +
> -					  ip_hdrlen(skb);
> -			skb->csum_offset = (void *)check - data;
> -			*check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
> -						    datalen, proto, 0);
> -		} else {
> -			*check = 0;
> -			*check = csum_tcpudp_magic(iph->saddr, iph->daddr,
> -						   datalen, proto,
> -						   csum_partial(data, datalen,
> -								0));
> -			if (proto == IPPROTO_UDP && !*check)
> -				*check = CSUM_MANGLED_0;
> -		}
> +		const struct iphdr *iph = ip_hdr(skb);
> +
> +		skb->ip_summed = CHECKSUM_PARTIAL;
> +		skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
> +			ip_hdrlen(skb);
> +		skb->csum_offset = (void *)check - data;
> +		*check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
> +					    proto, 0);

Is this change going to work with traffic that is redirected to the
localhost?
Pravin B Shelar Dec. 4, 2015, 11:45 p.m. UTC | #2
On Tue, Dec 1, 2015 at 12:51 PM, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> On Wed, Nov 25, 2015 at 04:08:16PM -0800, Jarno Rajahalme wrote:
>> NAT checksum recalculation code assumes existence of skb_dst, which
>> becomes a problem for a later patch in the series ("openvswitch:
>> Interface with NAT.").  Simplify this by removing the check on
>> skb_dst, as the checksum will be dealt with later in the stack.
>>
>> Suggested-by: Pravin Shelar <pshelar@nicira.com>
>> Signed-off-by: Jarno Rajahalme <jarno@ovn.org>
>> ---
>>  net/ipv4/netfilter/nf_nat_l3proto_ipv4.c | 29 ++++++++---------------------
>>  net/ipv6/netfilter/nf_nat_l3proto_ipv6.c | 29 ++++++++---------------------
>>  2 files changed, 16 insertions(+), 42 deletions(-)
>>
>> diff --git a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
>> index 5075b7e..f8aad03 100644
>> --- a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
>> +++ b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
>> @@ -127,28 +127,15 @@ static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
>>                                   u8 proto, void *data, __sum16 *check,
>>                                   int datalen, int oldlen)
>>  {
>> -     const struct iphdr *iph = ip_hdr(skb);
>> -     struct rtable *rt = skb_rtable(skb);
>> -
>>       if (skb->ip_summed != CHECKSUM_PARTIAL) {
>> -             if (!(rt->rt_flags & RTCF_LOCAL) &&
>> -                 (!skb->dev || skb->dev->features & NETIF_F_V4_CSUM)) {
>> -                     skb->ip_summed = CHECKSUM_PARTIAL;
>> -                     skb->csum_start = skb_headroom(skb) +
>> -                                       skb_network_offset(skb) +
>> -                                       ip_hdrlen(skb);
>> -                     skb->csum_offset = (void *)check - data;
>> -                     *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
>> -                                                 datalen, proto, 0);
>> -             } else {
>> -                     *check = 0;
>> -                     *check = csum_tcpudp_magic(iph->saddr, iph->daddr,
>> -                                                datalen, proto,
>> -                                                csum_partial(data, datalen,
>> -                                                             0));
>> -                     if (proto == IPPROTO_UDP && !*check)
>> -                             *check = CSUM_MANGLED_0;
>> -             }
>> +             const struct iphdr *iph = ip_hdr(skb);
>> +
>> +             skb->ip_summed = CHECKSUM_PARTIAL;
>> +             skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
>> +                     ip_hdrlen(skb);
>> +             skb->csum_offset = (void *)check - data;
>> +             *check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
>> +                                         proto, 0);
>
> Is this change going to work with traffic that is redirected to the
> localhost?

localhost should handle CHECKSUM_PARTIAL just fine. So I do not see
problem with always converting skb to CHECKSUM_PARTIAL skb.
diff mbox

Patch

diff --git a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
index 5075b7e..f8aad03 100644
--- a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
+++ b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
@@ -127,28 +127,15 @@  static void nf_nat_ipv4_csum_recalc(struct sk_buff *skb,
 				    u8 proto, void *data, __sum16 *check,
 				    int datalen, int oldlen)
 {
-	const struct iphdr *iph = ip_hdr(skb);
-	struct rtable *rt = skb_rtable(skb);
-
 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
-		if (!(rt->rt_flags & RTCF_LOCAL) &&
-		    (!skb->dev || skb->dev->features & NETIF_F_V4_CSUM)) {
-			skb->ip_summed = CHECKSUM_PARTIAL;
-			skb->csum_start = skb_headroom(skb) +
-					  skb_network_offset(skb) +
-					  ip_hdrlen(skb);
-			skb->csum_offset = (void *)check - data;
-			*check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
-						    datalen, proto, 0);
-		} else {
-			*check = 0;
-			*check = csum_tcpudp_magic(iph->saddr, iph->daddr,
-						   datalen, proto,
-						   csum_partial(data, datalen,
-								0));
-			if (proto == IPPROTO_UDP && !*check)
-				*check = CSUM_MANGLED_0;
-		}
+		const struct iphdr *iph = ip_hdr(skb);
+
+		skb->ip_summed = CHECKSUM_PARTIAL;
+		skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
+			ip_hdrlen(skb);
+		skb->csum_offset = (void *)check - data;
+		*check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
+					    proto, 0);
 	} else
 		inet_proto_csum_replace2(check, skb,
 					 htons(oldlen), htons(datalen), true);
diff --git a/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c b/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
index 238e70c..e0be97e 100644
--- a/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
+++ b/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
@@ -131,28 +131,15 @@  static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
 				    u8 proto, void *data, __sum16 *check,
 				    int datalen, int oldlen)
 {
-	const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
-	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
-
 	if (skb->ip_summed != CHECKSUM_PARTIAL) {
-		if (!(rt->rt6i_flags & RTF_LOCAL) &&
-		    (!skb->dev || skb->dev->features & NETIF_F_V6_CSUM)) {
-			skb->ip_summed = CHECKSUM_PARTIAL;
-			skb->csum_start = skb_headroom(skb) +
-					  skb_network_offset(skb) +
-					  (data - (void *)skb->data);
-			skb->csum_offset = (void *)check - data;
-			*check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
-						  datalen, proto, 0);
-		} else {
-			*check = 0;
-			*check = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
-						 datalen, proto,
-						 csum_partial(data, datalen,
-							      0));
-			if (proto == IPPROTO_UDP && !*check)
-				*check = CSUM_MANGLED_0;
-		}
+		const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
+
+		skb->ip_summed = CHECKSUM_PARTIAL;
+		skb->csum_start = skb_headroom(skb) + skb_network_offset(skb) +
+			(data - (void *)skb->data);
+		skb->csum_offset = (void *)check - data;
+		*check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
+					  datalen, proto, 0);
 	} else
 		inet_proto_csum_replace2(check, skb,
 					 htons(oldlen), htons(datalen), true);