Skip to contentSkip to conversation

netfilter: rework netfilter ipv6 defrag

PATCH In review Florian Westphal 4 participants 18 messages Oct 17, 2015 – Oct 21, 2015

Subsystems (6)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: rework netfilter ipv6 defrag

[ CC netdev since patch #2 isn't nf-specific. Dave, if you want
I can resubmit that one after the next nf-pull request; let me know if you would prefer that ].

Openvswitch seems broken wrt. to defragmentation, it doesn't call nf_ct_frag6_consume_orig to free the original fragments.

Moreover, openvswitch design seems to require that it can reuse current skb rather than work with a new skb pointer (it uses skb_morph for this).

Instead of OVS-side fix this series tries to alter netfilter ipv6 defrag accordingly.

1. nf_ct_frag6_consume_orig is removed, since
commit 6aafeef03b9d9ecf ("netfilter: push reasm skb through instead of original frag skbs") nothing needs the original fragments so there is no reason why we need to clone+store original skb -- just stash original skbs in the frag_list.

2. Use skb_morph to make the last skb processed (not necessarily last fragment) the reassembled one.

3. remove the no-longer needed recursion into nf_iterate, we can now just return ACCEPT/STOLEN as needed instead of NF_HOOK_THRESH()+NF_STOLEN.

Tested with flood-ping6+ fault-injection framework 'failslab' type.

author maintainer
#1 lore raw

PATCH 1/4 netfilter: ipv6: remove extra clone/free 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 assemled skb.

---
 include/net/netfilter/ipv6/nf_defrag_ipv6.h |  1 -
 net/ipv6/netfilter/nf_conntrack_reasm.c     | 71 ++++-------------------------
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c   |  2 -
 3 files changed, 9 insertions(+), 65 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 +9 -62

139 lines not on this page · raw message

net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +0 -2
index 4fdbed5e..313c1d0 100644
@@ -73,8 +73,6 @@ static unsigned int ipv6_defrag(void *priv,
 	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);
Signature · 1 line
2.0.5
author maintainer
#2 lore raw

PATCH 2/4 inet: kill obsolete skb_free op

In reply to Florian Westphal

The only user was removed in preceeding commit.

---
 include/net/inet_frag.h             |  1 -
 net/ieee802154/6lowpan/reassembly.c |  1 -
 net/ipv4/inet_fragment.c            | 10 +---------
 net/ipv4/ip_fragment.c              |  1 -
 net/ipv6/reassembly.c               |  1 -
 5 files changed, 1 insertion(+), 13 deletions(-)
include/net/inet_frag.h +0 -1
index 53eead2..9f9aa4d 100644
@@ -99,7 +99,6 @@ struct inet_frags {
 	void			(*constructor)(struct inet_frag_queue *q,
 					       const void *arg);
 	void			(*destructor)(struct inet_frag_queue *);
-	void			(*skb_free)(struct sk_buff *);
 	void			(*frag_expire)(unsigned long data);
 	struct kmem_cache	*frags_cachep;
 	const char		*frags_cache_name;
net/ieee802154/6lowpan/reassembly.c +0 -1
index 12e8cf4..f85b08b 100644
@@ -619,7 +619,6 @@ int __init lowpan_net_frag_init(void)
 	lowpan_frags.hashfn = lowpan_hashfn;
 	lowpan_frags.constructor = lowpan_frag_init;
 	lowpan_frags.destructor = NULL;
-	lowpan_frags.skb_free = NULL;
 	lowpan_frags.qsize = sizeof(struct frag_queue);
 	lowpan_frags.match = lowpan_frag_match;
 	lowpan_frags.frag_expire = lowpan_frag_expire;
net/ipv4/inet_fragment.c +1 -9
index d0a7c03..bf63ea4 100644
@@ -291,14 +291,6 @@ void inet_frag_kill(struct inet_frag_queue *fq, struct inet_frags *f)
 }
 EXPORT_SYMBOL(inet_frag_kill);
 
-static inline void frag_kfree_skb(struct netns_frags *nf, struct inet_frags *f,
-				  struct sk_buff *skb)
-{
-	if (f->skb_free)
-		f->skb_free(skb);
-	kfree_skb(skb);
-}
-
 void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f)
 {
 	struct sk_buff *fp;
@@ -315,7 +307,7 @@ void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f)
 		struct sk_buff *xp = fp->next;
 
 		sum_truesize += fp->truesize;
-		frag_kfree_skb(nf, f, fp);
+		kfree_skb(fp);
 		fp = xp;
 	}
 	sum = sum_truesize + f->qsize;
net/ipv4/ip_fragment.c +0 -1
index 5482745..325106d 100644
@@ -885,7 +885,6 @@ void __init ipfrag_init(void)
 	ip4_frags.hashfn = ip4_hashfn;
 	ip4_frags.constructor = ip4_frag_init;
 	ip4_frags.destructor = ip4_frag_free;
-	ip4_frags.skb_free = NULL;
 	ip4_frags.qsize = sizeof(struct ipq);
 	ip4_frags.match = ip4_frag_match;
 	ip4_frags.frag_expire = ip_expire;
net/ipv6/reassembly.c +0 -1
index f1159bb..10bbcd1 100644
@@ -745,7 +745,6 @@ int __init ipv6_frag_init(void)
 	ip6_frags.hashfn = ip6_hashfn;
 	ip6_frags.constructor = ip6_frag_init;
 	ip6_frags.destructor = NULL;
-	ip6_frags.skb_free = NULL;
 	ip6_frags.qsize = sizeof(struct frag_queue);
 	ip6_frags.match = ip6_frag_match;
 	ip6_frags.frag_expire = ip6_frag_expire;
Signature · 1 line
2.0.5
author maintainer
#3 lore raw

PATCH 3/4 netfilter: ipv6: in-place replacement of last skb

In reply to Florian Westphal

openvswitch attempts to morph the reassembled skb with the currently processed one. But this looks broken -- the currently processed skb is part of the reassembled skbs frag_list.

IOW, we morph an element of reasms frag_list into reasm itself, then free said frag_list element.

This allows callers to process skb as intended by openvswitch: we either return NULL (skb queued for reassembly), or turn the provided skb into a reassembled one.

A followup patch will change nf_defrag to avoid the NF_HOOK recursion which is now no longer needed.

---
 net/ipv6/netfilter/nf_conntrack_reasm.c   | 33 +++++++++++++++++++++++++++++--
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c |  4 ----
 net/openvswitch/conntrack.c               |  5 -----
 3 files changed, 31 insertions(+), 11 deletions(-)
net/ipv6/netfilter/nf_conntrack_reasm.c +31 -2
index 1b1a851..72ac916 100644
@@ -369,7 +369,7 @@ err:
  *	the last and the first frames arrived and all the bits are here.
  */
 static struct sk_buff *
-nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
+nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *prev,  struct net_device *dev)
 {
 	struct sk_buff *fp, *head = fq->q.fragments;
 	int    payload_len;
@@ -425,6 +425,35 @@ nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
 		add_frag_mem_limit(fq->q.net, clone->truesize);
 	}
 
+	/* morph head into last received skb: prev.
+	 *
+	 * This allows callers of ipv6 conntrack defrag to continue
+	 * to use the last skb(frag) passed into the reasm engine.
+	 * The last skb frag 'silently' turns into the full reassembled skb.
+	 *
+	 * Since prev is also part of q->fragments we have to clone it first.
+	 */
+	if (head != prev) {
+		struct sk_buff *iter;
+
+		fp = skb_clone(prev, GFP_ATOMIC);
+		if (!fp)
+			goto out_oom;
+
+		fp->next = prev->next;
+		skb_queue_walk(head, iter) {
+			if (iter->next != prev)
+				continue;
+			iter->next = fp;
+			break;
+		}
+
+		skb_morph(prev, head);
+		prev->next = head->next;
+		consume_skb(head);
+		head = prev;
+	}
+
 	/* We have to remove fragment header from datagram and to relocate
 	 * header in order to calculate ICV correctly. */
 	skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
@@ -582,7 +611,7 @@ struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 use
 
 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
 	    fq->q.meat == fq->q.len) {
-		ret_skb = nf_ct_frag6_reasm(fq, dev);
+		ret_skb = nf_ct_frag6_reasm(fq, skb, dev);
 		if (ret_skb == NULL)
 			pr_debug("Can't reassemble fragmented packets\n");
 	}
net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +0 -4
index 313c1d0..fb96b10 100644
@@ -69,10 +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_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 -5
index ad61426..30ece1d 100644
@@ -319,12 +319,7 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
 		if (!reasm)
 			return -EINPROGRESS;
 
-		if (skb == reasm)
-			return -EINVAL;
-
 		key->ip.proto = ipv6_hdr(reasm)->nexthdr;
-		skb_morph(skb, reasm);
-		consume_skb(reasm);
 		ovs_cb.mru = IP6CB(skb)->frag_max_size;
 #else
 		return -EPFNOSUPPORT;
Signature · 1 line
2.0.5
author maintainer
#6 lore raw

PATCH 4/4 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     | 68 ++++++++++++++---------------
 net/ipv6/netfilter/nf_defrag_ipv6_hooks.c   | 14 +++---
 net/openvswitch/conntrack.c                 | 11 +++--
 4 files changed, 43 insertions(+), 52 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 +32 -36

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 30ece1d..b42630d 100644
@@ -298,10 +298,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);
@@ -312,14 +312,13 @@ static int handle_fragments(struct net *net, struct sw_flow_key *key,
 	} else if (key->eth.type == htons(ETH_P_IPV6)) {
 #if IS_ENABLED(CONFIG_NF_DEFRAG_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;
 #else
 		return -EPFNOSUPPORT;
Signature · 1 line
2.0.5
#9 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal

[ CC netdev since patch #2 isn't nf-specific. Dave, if you want
I can resubmit that one after the next nf-pull request; let me know if you would prefer that ].

Openvswitch seems broken wrt. to defragmentation, it doesn't call nf_ct_frag6_consume_orig to free the original fragments.

This will need to be fixed for 'net' as well, do you have a path in mind for that?

Quoted 12 lines

Moreover, openvswitch design seems to require that it can reuse current skb rather than work with a new skb pointer (it uses skb_morph for this).

Instead of OVS-side fix this series tries to alter netfilter ipv6 defrag accordingly.

1. nf_ct_frag6_consume_orig is removed, since
commit 6aafeef03b9d9ecf ("netfilter: push reasm skb through instead of original frag skbs") nothing needs the original fragments so there is no reason why we need to clone+store original skb -- just stash original skbs in the frag_list.

Quoted 7 lines

2. Use skb_morph to make the last skb processed (not necessarily last fragment) the reassembled one.

3. remove the no-longer needed recursion into nf_iterate, we can now just return ACCEPT/STOLEN as needed instead of NF_HOOK_THRESH()+NF_STOLEN.

Tested with flood-ping6+ fault-injection framework 'failslab' type.

Overall, I like it in that it seems to make the IPv6 code more consistent with IPv4, and it's a net negative LoC, simplifying the code.

Patch 3 when taken independently from patch 4 hides user-visible error codes on the OVS side. The OVS conntrack action hides -EINPROGRESS from userspace, treating it as a successful execution. All other errors are returned up. With that patch, all errors will be hidden. I see that it's fixed in Patch 4, so maybe it's not a biggie but those two patches should be tightly coupled.

#7 lore raw

Re: [PATCH nf-next 4/4] netfilter: ipv6: avoid nf_iterate recursion

In reply to Florian Westphal
Quoted 19 lines · Florian Westphal
(quoted diff) +6 -6
@@ -606,19 +599,22 @@ struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 use
                spin_unlock_bh(&fq->q.lock);
                pr_debug("Can't insert skb to queue\n");
                inet_frag_put(&fq->q, &nf_frags);
-               return skb;
+               return -EINVAL;
        }
 
+       /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
+        * must be returned.
+        */
+       ret = -EINPROGRESS;
        if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
-           fq->q.meat == fq->q.len) {
-               ret_skb = nf_ct_frag6_reasm(fq, skb, dev);
-               if (ret_skb == NULL)
-                       pr_debug("Can't reassemble fragmented packets\n");
-       }
+           fq->q.meat == fq->q.len &&

Review struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 use · line 619

Quoted 10 lines
(quoted diff) +4 -1
+           nf_ct_frag6_reasm(fq, skb, dev))
+               ret = 0;
+
        spin_unlock_bh(&fq->q.lock);
 
        inet_frag_put(&fq->q, &nf_frags);
-       return ret_skb;
+       return ret;
 }
 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);

Minor nit below, feel free to disregard if it misses some code style guideline. The lock/unlock reads easier to me if the unlocking/frag_put is grouped together, something like the following incremental:

(quoted diff) +0 -0

b/net/ipv6/netfilter/nf_conntrack_reasm.c
index b87dd75967d0..edfd290792f5 100644

net/ipv6/netfilter/nf_conntrack_reasm.c +0 -0
@@ -596,10 +596,8 @@ int nf_ct_frag6_gather(struct net *net, struct

sk_buff *skb, u32 user)

        spin_lock_bh(&fq->q.lock);

        if (nf_ct_frag6_queue(fq, skb, fhdr, nhoff) < 0) {
-               spin_unlock_bh(&fq->q.lock);
-               pr_debug("Can't insert skb to queue\n");
-               inet_frag_put(&fq->q, &nf_frags);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto unlock;
        }

        /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
@@ -611,9 +609,11 @@ int nf_ct_frag6_gather(struct net *net, struct
sk_buff *skb, u32 user)
            nf_ct_frag6_reasm(fq, skb, dev))
                ret = 0;

+unlock:

        spin_unlock_bh(&fq->q.lock);
-
        inet_frag_put(&fq->q, &nf_frags);
+       if (unlikely(ret == -EINVAL))
+               pr_debug("Can't insert skb to queue\n");
        return ret;

}
EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);

author maintainer
#10 lore raw
In reply to Joe Stringer
Quoted 10 lines · Joe Stringer
Quoted 6 lines · Florian Westphal

[ CC netdev since patch #2 isn't nf-specific. Dave, if you want
I can resubmit that one after the next nf-pull request; let me know if you would prefer that ].

Openvswitch seems broken wrt. to defragmentation, it doesn't call nf_ct_frag6_consume_orig to free the original fragments.

This will need to be fixed for 'net' as well, do you have a path in mind for that?

Good point. No, I don't. Any suggestions?
I can try to just re-target -nf tree (sans patch #2). Pablo?

ipv4 side seems broken as well (ip_defrag frees skb on errors other than -EINPROGRESS, so it looks like we will double-free in do_execute_actions)

Quoted 6 lines

Patch 3 when taken independently from patch 4 hides user-visible error codes on the OVS side. The OVS conntrack action hides -EINPROGRESS from userspace, treating it as a successful execution. All other errors are returned up. With that patch, all errors will be hidden. I see that it's fixed in Patch 4, so maybe it's not a biggie but those two patches should be tightly coupled.

You're right, we can't signal "skb unchanged". I guess one could just test wheter skb is a fragment and -EINVAL if it is, not sure if its worth doing given that such test would be removed again by the very next patch?

author maintainer
#8 lore raw

Re: [PATCH nf-next 4/4] netfilter: ipv6: avoid nf_iterate recursion

In reply to Joe Stringer
Quoted 25 lines · Joe Stringer
Quoted 24 lines · Florian Westphal
(quoted diff) +9 -6
@@ -606,19 +599,22 @@ struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 use
                spin_unlock_bh(&fq->q.lock);
                pr_debug("Can't insert skb to queue\n");
                inet_frag_put(&fq->q, &nf_frags);
-               return skb;
+               return -EINVAL;
        }
 
+       /* after queue has assumed skb ownership, only 0 or -EINPROGRESS
+        * must be returned.
+        */
+       ret = -EINPROGRESS;
        if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
-           fq->q.meat == fq->q.len) {
-               ret_skb = nf_ct_frag6_reasm(fq, skb, dev);
-               if (ret_skb == NULL)
-                       pr_debug("Can't reassemble fragmented packets\n");
-       }
+           fq->q.meat == fq->q.len &&
+           nf_ct_frag6_reasm(fq, skb, dev))
+               ret = 0;
+
        spin_unlock_bh(&fq->q.lock);
 
Quoted 10 lines

Review struct sk_buff *nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 use · line 619

Quoted 5 lines
(quoted diff) +1 -1
        inet_frag_put(&fq->q, &nf_frags);
-       return ret_skb;
+       return ret;
 }
 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);

Minor nit below, feel free to disregard if it misses some code style guideline. The lock/unlock reads easier to me if the unlocking/frag_put is grouped together, something like the following incremental:

I like it, will change it for v2.

Thanks!

#4 lore raw

Re: [PATCH nf-next 3/4] netfilter: ipv6: in-place replacement of last skb

In reply to Florian Westphal
Quoted 26 lines · Florian Westphal
net/ipv6/netfilter/nf_conntrack_reasm.c +22 -0
@@ -425,6 +425,35 @@ nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
                add_frag_mem_limit(fq->q.net, clone->truesize);
        }
 
+       /* morph head into last received skb: prev.
+        *
+        * This allows callers of ipv6 conntrack defrag to continue
+        * to use the last skb(frag) passed into the reasm engine.
+        * The last skb frag 'silently' turns into the full reassembled skb.
+        *
+        * Since prev is also part of q->fragments we have to clone it first.
+        */
+       if (head != prev) {
+               struct sk_buff *iter;
+
+               fp = skb_clone(prev, GFP_ATOMIC);
+               if (!fp)
+                       goto out_oom;
+
+               fp->next = prev->next;
+               skb_queue_walk(head, iter) {
+                       if (iter->next != prev)
+                               continue;
+                       iter->next = fp;
+                       break;
+               }

net/ipv6/netfilter/nf_conntrack_reasm.c nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev) · line 459

Quoted 10 lines
net/ipv6/netfilter/nf_conntrack_reasm.c +7 -0
+
+               skb_morph(prev, head);
+               prev->next = head->next;
+               consume_skb(head);
+               head = prev;
+       }
+
        /* We have to remove fragment header from datagram and to relocate
         * header in order to calculate ICV correctly. */
        skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];

This hunk looks very similar to the logic in ip_frag_reasm(). Did you consider refactoring to share it?

#11 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Joe Stringer
Quoted 4 lines · Florian Westphal

[ CC netdev since patch #2 isn't nf-specific. Dave, if you want
I can resubmit that one after the next nf-pull request; let me know if you would prefer that ].

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Openvswitch seems broken wrt. to defragmentation, it doesn't call nf_ct_frag6_consume_orig to free the original fragments.

This will need to be fixed for 'net' as well, do you have a path in mind for that?

Good point. No, I don't. Any suggestions?
I can try to just re-target -nf tree (sans patch #2). Pablo?

The smallest change seems to be adding the nf_ct_frag6_consume_orig() call to OVS, plus the morph logic from patch 3. Alternatively if Pablo is fine with having the series re-targeted, then that sounds reasonable to me too.

Quoted 3 lines

ipv4 side seems broken as well (ip_defrag frees skb on errors other than -EINPROGRESS, so it looks like we will double-free in do_execute_actions)

Oh dear. Thanks for the report. I propose wrapping the ip_defrag() with an skb_get()/skb_consume() as this seems to require the least invasive changes:

net/openvswitch/conntrack.c +0 -0
index a5ec34f8502f..0d2d24c99fd5 100644
@@ -303,7 +303,11 @@ static int handle_fragments(struct net *net,

struct sw_flow_key *key,

                int err;

                memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
+
+               skb_get(skb);
                err = ip_defrag(skb, user);
+               if (!err || err == -EINPROGRESS)
+                       consume_skb(skb);
                if (err)
                        return err;

I don't have a test environment to verify this at the moment though. Let me know if you'll take this with your patch series or if you'd like me to send it out.

Quoted 11 lines
Quoted 6 lines

Patch 3 when taken independently from patch 4 hides user-visible error codes on the OVS side. The OVS conntrack action hides -EINPROGRESS from userspace, treating it as a successful execution. All other errors are returned up. With that patch, all errors will be hidden. I see that it's fixed in Patch 4, so maybe it's not a biggie but those two patches should be tightly coupled.

You're right, we can't signal "skb unchanged". I guess one could just test wheter skb is a fragment and -EINVAL if it is, not sure if its worth doing given that such test would be removed again by the very next patch?

On second thought, this would only affect 'net', and if we're considering the solution for that tree too then it really doesn't matter.

author maintainer
#5 lore raw

Re: [PATCH nf-next 3/4] netfilter: ipv6: in-place replacement of last skb

In reply to Joe Stringer
Quoted 2 lines · Joe Stringer

This hunk looks very similar to the logic in ip_frag_reasm(). Did you consider refactoring to share it?

Could be done but I did not plan to do that.

author maintainer
#12 lore raw
In reply to Joe Stringer
Quoted 7 lines · Joe Stringer
Quoted 2 lines

Good point. No, I don't. Any suggestions?
I can try to just re-target -nf tree (sans patch #2). Pablo?

The smallest change seems to be adding the nf_ct_frag6_consume_orig() call to OVS, plus the morph logic from patch 3. Alternatively if Pablo is fine with having the series re-targeted, then that sounds reasonable to me too.

Pablo, your call.

I would suggest to re-target patches #1 and #3 to nf tree, I can do this, just let me know.

Alternative is to just add the nf_ct_frag6_consume_orig call to openvswitch and handle that via net tree.

I can then wait for that change to pop up in nf-next and just resend this series (which will then undo that change).

Let me know, thanks!

Quoted 14 lines
Quoted 3 lines

ipv4 side seems broken as well (ip_defrag frees skb on errors other than -EINPROGRESS, so it looks like we will double-free in do_execute_actions)

Oh dear. Thanks for the report. I propose wrapping the ip_defrag() with an skb_get()/skb_consume() as this seems to require the least invasive changes:

net/openvswitch/conntrack.c +0 -0
index a5ec34f8502f..0d2d24c99fd5 100644
@@ -303,7 +303,11 @@ static int handle_fragments(struct net *net,
 struct sw_flow_key *key,

net/openvswitch/conntrack.c static int handle_fragments(struct net *net, · line 313

Quoted 10 lines
net/openvswitch/conntrack.c +4 -0
                int err;
 
                memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
+
+               skb_get(skb);
                err = ip_defrag(skb, user);
+               if (!err || err == -EINPROGRESS)
+                       consume_skb(skb);
                if (err)
                        return err;

Indeed, that seems like the least invasive change.

Feel free to submit this to -net, there is no dependency on any of the other changes.

#13 lore raw
Quoted 2 lines · Florian Westphal

Feel free to submit this to -net, there is no dependency on any of the other changes.

OK, I'll follow up on that patch.

#14 lore raw
In reply to Florian Westphal
Quoted 9 lines · Florian Westphal
Quoted 7 lines · Joe Stringer
Quoted 2 lines

Good point. No, I don't. Any suggestions?
I can try to just re-target -nf tree (sans patch #2). Pablo?

The smallest change seems to be adding the nf_ct_frag6_consume_orig() call to OVS, plus the morph logic from patch 3. Alternatively if Pablo is fine with having the series re-targeted, then that sounds reasonable to me too.

Quoted 4 lines

Pablo, your call.

I would suggest to re-target patches #1 and #3 to nf tree, I can do this, just let me know.

It's fairly late, we're on -rc6 so I don't think it's a good idea to submit a large rework to -nf at this stage.

Quoted 5 lines

Alternative is to just add the nf_ct_frag6_consume_orig call to openvswitch and handle that via net tree.

I can then wait for that change to pop up in nf-next and just resend this series (which will then undo that change).

I'd rather get things fixes for the existing code. This would also allow simple passing back to -stable, then we can move forward discuss and review your rework with sufficient time.

Let me know, thanks!

maintainer
#17 lore raw
In reply to Florian Westphal

From: Florian Westphal <[email protected]>
Date: Sat, 17 Oct 2015 22:14:21 +0200

Quoted 3 lines

[ CC netdev since patch #2 isn't nf-specific. Dave, if you want
I can resubmit that one after the next nf-pull request; let me know if you would prefer that ].

No objections to you merging patch #2 however you wish, it's perfectly fine.

Thanks.

author maintainer
#15 lore raw
In reply to Pablo Neira Ayuso
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines

I can then wait for that change to pop up in nf-next and just resend this series (which will then undo that change).

I'd rather get things fixes for the existing code. This would also allow simple passing back to -stable, then we can move forward discuss and review your rework with sufficient time.

Joe, could you take care of this and submit a OVS fix to net tree?

(just add that call to nf_ct_frag6_consume_orig and take
the morph change directly into the OVS callpath)

I will then resubmit all of this at some later point.

Thanks.

#16 lore raw
Quoted 8 lines · Florian Westphal
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines

I can then wait for that change to pop up in nf-next and just resend this series (which will then undo that change).

I'd rather get things fixes for the existing code. This would also allow simple passing back to -stable, then we can move forward discuss and review your rework with sufficient time.

Quoted 8 lines

Joe, could you take care of this and submit a OVS fix to net tree?

(just add that call to nf_ct_frag6_consume_orig and take
the morph change directly into the OVS callpath)

I will then resubmit all of this at some later point.

Thanks.

Sure thing.