Skip to contentSkip to conversation

netfilter: simplify nf_defrag_ipv6

PATCH In review Florian Westphal 2 participants 4 messages Nov 18, 2015 – Nov 23, 2015

Subsystems (3)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: simplify nf_defrag_ipv6

commit 6aafeef03b9d9ecf
("netfilter: push reasm skb through instead of original frag skbs") changed ipv6 defrag to not use the original skbs anymore.

However, we still clone fragment skbs and retain original ones just to free them soon after. Since reassembled skb is newly allocated, we also need to recursively re-invoke the netfilter hooks so they can work on the reassembled skb.

This removes those clone/free operations and also avoids the hook reinvocation by morphing the reassembled skb into the last skb (i.e. the skb that completes the frag queue), just like ipv4 defrag.

include/net/netfilter/ipv6/nf_defrag_ipv6.h |    3
net/ipv6/netfilter/nf_conntrack_reasm.c     |  168 +++++++++++-----------------
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c   |   20 ---
net/openvswitch/conntrack.c                 |   25 ----
4 files changed, 78 insertions(+), 138 deletions(-)
author maintainer
#1 lore raw

PATCH 1/2 netfilter: ipv6: nf_defrag: avoid/free clone operations

commit 6aafeef03b9d9ecf
("netfilter: push reasm skb through instead of original frag skbs") changed ipv6 defrag to not use the original skbs anymore.

So rather than keeping the original skbs around just to discard them afterwards just use the original skbs directly for the fraglist of the newly assembled skb and remove the extra clone/free operations.

The skb that completes the fragment queue is morphed into a the reassembled one instead, just like ipv4 defrag.

openvswitch doesn't need any additional skb_morph magic anymore to deal with this situation so just remove that.

A followup patch can then also remove the NF_HOOK (re)invocation in the ipv6 netfilter defrag hook.

---
 include/net/netfilter/ipv6/nf_defrag_ipv6.h |   1 -
 net/ipv6/netfilter/nf_conntrack_reasm.c     | 105 +++++++++++-----------------
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c   |   6 --
 net/openvswitch/conntrack.c                 |  14 ----
 4 files changed, 40 insertions(+), 86 deletions(-)
include/net/netfilter/ipv6/nf_defrag_ipv6.h +0 -1
index fb7da5b..fcd20cf 100644
@@ -6,7 +6,6 @@ void nf_defrag_ipv6_enable(void);
 int nf_ct_frag6_init(void);
 void nf_ct_frag6_cleanup(void);
 struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user);
-void nf_ct_frag6_consume_orig(struct sk_buff *skb);
 
 struct inet_frags_ctl;
 
net/ipv6/netfilter/nf_conntrack_reasm.c +40 -65

181 lines not on this page · raw message

net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +0 -6
index 4fdbed5e..fb96b10 100644
@@ -69,12 +69,6 @@ static unsigned int ipv6_defrag(void *priv,
 	if (reasm == NULL)
 		return NF_STOLEN;
 
-	/* error occurred or not fragmented */
-	if (reasm == skb)
-		return NF_ACCEPT;
-
-	nf_ct_frag6_consume_orig(reasm);
-
 	NF_HOOK_THRESH(NFPROTO_IPV6, state->hook, state->net, state->sk, reasm,
 		       state->in, state->out,
 		       state->okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
net/openvswitch/conntrack.c +0 -14
index c2cc111..cac2169 100644
@@ -321,21 +321,7 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
 		if (!reasm)
 			return -EINPROGRESS;
 
-		if (skb == reasm) {
-			kfree_skb(skb);
-			return -EINVAL;
-		}
-
-		/* Don't free 'skb' even though it is one of the original
-		 * fragments, as we're going to morph it into the head.
-		 */
-		skb_get(skb);
-		nf_ct_frag6_consume_orig(reasm);
-
 		key->ip.proto = ipv6_hdr(reasm)->nexthdr;
-		skb_morph(skb, reasm);
-		skb->next = reasm->next;
-		consume_skb(reasm);
 		ovs_cb.mru = IP6CB(skb)->frag_max_size;
 #endif
 	} else {
Signature · 1 line
2.4.10
author maintainer
#2 lore raw

PATCH 2/2 netfilter: ipv6: avoid nf_iterate recursion

In reply to Florian Westphal

The previous patch changed nf_ct_frag6_gather() to morph reassembled skb with the previous one.

This means that the return value is always NULL or the skb argument. So change it to an err value.

Instead of invoking NF_HOOK recursively with threshold to skip already-called hooks we can now just return NF_ACCEPT to move on to the next hook except for -EINPROGRESS (which means skb has been queued for reassembly), in which case we return NF_STOLEN.

---
 include/net/netfilter/ipv6/nf_defrag_ipv6.h |  2 +-
 net/ipv6/netfilter/nf_conntrack_reasm.c     | 71 +++++++++++++----------------
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c   | 14 ++----
 net/openvswitch/conntrack.c                 | 11 ++---
 4 files changed, 42 insertions(+), 56 deletions(-)
include/net/netfilter/ipv6/nf_defrag_ipv6.h +1 -1
index fcd20cf..ddf162f 100644
@@ -5,7 +5,7 @@ void nf_defrag_ipv6_enable(void);
 
 int nf_ct_frag6_init(void);
 void nf_ct_frag6_cleanup(void);
-struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user);
+int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user);
 
 struct inet_frags_ctl;
 
net/ipv6/netfilter/nf_conntrack_reasm.c +31 -40

151 lines not on this page · raw message

net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +5 -9
index fb96b10..f7aab5a 100644
@@ -55,7 +55,7 @@ static unsigned int ipv6_defrag(void *priv,
 				struct sk_buff *skb,
 				const struct nf_hook_state *state)
 {
-	struct sk_buff *reasm;
+	int err;
 
 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
 	/* Previously seen (loopback)?	*/
@@ -63,17 +63,13 @@ static unsigned int ipv6_defrag(void *priv,
 		return NF_ACCEPT;
 #endif
 
-	reasm = nf_ct_frag6_gather(state->net, skb,
-				   nf_ct6_defrag_user(state->hook, skb));
+	err = nf_ct_frag6_gather(state->net, skb,
+				 nf_ct6_defrag_user(state->hook, skb));
 	/* queued */
-	if (reasm == NULL)
+	if (err == -EINPROGRESS)
 		return NF_STOLEN;
 
-	NF_HOOK_THRESH(NFPROTO_IPV6, state->hook, state->net, state->sk, reasm,
-		       state->in, state->out,
-		       state->okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
-
-	return NF_STOLEN;
+	return NF_ACCEPT;
 }
 
 static struct nf_hook_ops ipv6_defrag_ops[] = {
net/openvswitch/conntrack.c +5 -6
index cac2169..0c68c8e 100644
@@ -300,10 +300,10 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
 			    u16 zone, struct sk_buff *skb)
 {
 	struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
+	int err;
 
 	if (key->eth.type == htons(ETH_P_IP)) {
 		enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
-		int err;
 
 		memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
 		err = ip_defrag(net, skb, user);
@@ -314,14 +314,13 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
 		enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
-		struct sk_buff *reasm;
 
 		memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
-		reasm = nf_ct_frag6_gather(net, skb, user);
-		if (!reasm)
-			return -EINPROGRESS;
+		err = nf_ct_frag6_gather(net, skb, user);
+		if (err)
+			return err;
 
-		key->ip.proto = ipv6_hdr(reasm)->nexthdr;
+		key->ip.proto = ipv6_hdr(skb)->nexthdr;
 		ovs_cb.mru = IP6CB(skb)->frag_max_size;
 #endif
 	} else {
Signature · 1 line
2.4.10
#3 lore raw
In reply to Florian Westphal
Quoted 4 lines · Florian Westphal

commit 6aafeef03b9d9ecf
("netfilter: push reasm skb through instead of original frag skbs") changed ipv6 defrag to not use the original skbs anymore.

Quoted 8 lines

However, we still clone fragment skbs and retain original ones just to free them soon after. Since reassembled skb is newly allocated, we also need to recursively re-invoke the netfilter hooks so they can work on the reassembled skb.

This removes those clone/free operations and also avoids the hook reinvocation by morphing the reassembled skb into the last skb (i.e. the skb that completes the frag queue), just like ipv4 defrag.

Series applied, thanks for your patience Florian.