Skip to contentSkip to conversation

net: add helper support in tc act_ct for ovs offloading

PATCH In review Xin Long 3 participants 8 messages Oct 5, 2022 – Oct 6, 2022

Subsystems (5)
lore
Share & tools
lore

Xin Long

author
#0 lore raw

cover net: add helper support in tc act_ct for ovs offloading

Ilya reported an issue that FTP traffic would be broken when the OVS flow with ct(commit,alg=ftp) installed in the OVS kernel module, and it was caused by that TC didn't support the ftp helper offloaded from OVS.

This patchset is to add the helper support in act_ct for ovs offloading in kernel net/sched.

The 1st patch moves some code from openvswitch into nf_conntrack_helper so that it can be used by net/sched in the 3rd patch (Note there are still some other common code used in both OVS and TC and I will extract it in other patches). The 2nd patch extracts another function in net/ sched to make the 3rd patch easier to write.

The user space part will be added in another patch, and with it these OVS flows (FTP over SNAT) can be used to test this feature:

  table=0, in_port=veth1,tcp,tcp_dst=2121,ct_state=-trk \
    actions=ct(table=1, nat), normal
  table=0, in_port=veth2,tcp,ct_state=-trk actions=ct(table=1, nat)
  table=0, in_port=veth1,tcp,ct_state=-trk actions=ct(table=0, nat)
  table=0, in_port=veth1,tcp,ct_state=+trk+rel actions=ct(commit, nat),normal
  table=0, in_port=veth1,tcp,ct_state=+trk+est actions=veth2"

  table=1, in_port=veth1,tcp,tcp_dst=2121,ct_state=+trk+new \
    actions=ct(commit, nat(src=7.7.16.1), alg=ftp),normal"
  table=1, in_port=veth1,tcp,tcp_dst=2121,ct_state=+trk+est actions=veth2"
  table=1, in_port=veth2,tcp,ct_state=+trk+est actions=veth1"

Xin Long (3):
net: move the helper function to nf_conntrack_helper for ovs and tc
net: sched: call tcf_ct_params_free to free params in tcf_ct_init
net: sched: add helper support in act_ct

 include/net/netfilter/nf_conntrack_helper.h |   2 +
 include/net/tc_act/tc_ct.h                  |   1 +
 include/uapi/linux/tc_act/tc_ct.h           |   3 +
 net/netfilter/nf_conntrack_helper.c         |  71 ++++++++++
 net/openvswitch/conntrack.c                 |  61 +-------
 net/sched/act_ct.c                          | 148 +++++++++++++++++---
 6 files changed, 204 insertions(+), 82 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/3 net: move the helper function to nf_conntrack_helper for ovs and tc

Move ovs_ct_helper from openvswitch to nf_conntrack_helper and rename as nf_ct_helper so that it can be used in TC act_ct in the next patch. Note that it also adds the checks for the family and proto, as in TC act_ct, the packets with correct family and proto are not guaranteed.

---
 include/net/netfilter/nf_conntrack_helper.h |  2 +
 net/netfilter/nf_conntrack_helper.c         | 71 +++++++++++++++++++++
 net/openvswitch/conntrack.c                 | 61 +-----------------
 3 files changed, 74 insertions(+), 60 deletions(-)
include/net/netfilter/nf_conntrack_helper.h +2 -0
index 9939c366f720..6c32e59fc16f 100644
@@ -115,6 +115,8 @@ struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp);
 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
 			      gfp_t flags);
 
+int nf_ct_helper(struct sk_buff *skb, u16 proto);
+
 void nf_ct_helper_destroy(struct nf_conn *ct);
 
 static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct)
net/netfilter/nf_conntrack_helper.c +71 -0

84 lines not on this page · raw message

net/openvswitch/conntrack.c +1 -60

73 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#2 lore raw

PATCH 2/3 net: sched: call tcf_ct_params_free to free params in tcf_ct_init

In reply to Xin Long

This patch is to make the err path simple by calling tcf_ct_params_free(), so that it won't cause problems when more members are added into param and need freeing on the err path.

---
 net/sched/act_ct.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)
net/sched/act_ct.c +18 -17
index b38d91d6b249..193a460a9d7f 100644
@@ -345,11 +345,9 @@ static void tcf_ct_flow_table_cleanup_work(struct work_struct *work)
 	module_put(THIS_MODULE);
 }
 
-static void tcf_ct_flow_table_put(struct tcf_ct_params *params)
+static void tcf_ct_flow_table_put(struct tcf_ct_flow_table *ct_ft)
 {
-	struct tcf_ct_flow_table *ct_ft = params->ct_ft;
-
-	if (refcount_dec_and_test(&params->ct_ft->ref)) {
+	if (refcount_dec_and_test(&ct_ft->ref)) {
 		rhashtable_remove_fast(&zones_ht, &ct_ft->node, zones_params);
 		INIT_RCU_WORK(&ct_ft->rwork, tcf_ct_flow_table_cleanup_work);
 		queue_rcu_work(act_ct_wq, &ct_ft->rwork);
@@ -832,18 +830,23 @@ static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
 	return err;
 }
 
-static void tcf_ct_params_free(struct rcu_head *head)
+static void tcf_ct_params_free(struct tcf_ct_params *params)
 {
-	struct tcf_ct_params *params = container_of(head,
-						    struct tcf_ct_params, rcu);
-
-	tcf_ct_flow_table_put(params);
-
+	if (params->ct_ft)
+		tcf_ct_flow_table_put(params->ct_ft);
 	if (params->tmpl)
 		nf_ct_put(params->tmpl);
 	kfree(params);
 }
 
+static void tcf_ct_params_free_rcu(struct rcu_head *head)
+{
+	struct tcf_ct_params *params;
+
+	params = container_of(head, struct tcf_ct_params, rcu);
+	tcf_ct_params_free(params);
+}
+
 #if IS_ENABLED(CONFIG_NF_NAT)
 /* Modelled after nf_nat_ipv[46]_fn().
  * range is only used for new, uninitialized NAT state.
@@ -1390,7 +1393,7 @@ static int tcf_ct_init(struct net *net, struct nlattr *nla,
 
 	err = tcf_ct_flow_table_get(net, params);
 	if (err)
-		goto cleanup_params;
+		goto cleanup;
 
 	spin_lock_bh(&c->tcf_lock);
 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
@@ -1401,17 +1404,15 @@ static int tcf_ct_init(struct net *net, struct nlattr *nla,
 	if (goto_ch)
 		tcf_chain_put_by_act(goto_ch);
 	if (params)
-		call_rcu(&params->rcu, tcf_ct_params_free);
+		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
 
 	return res;
 
-cleanup_params:
-	if (params->tmpl)
-		nf_ct_put(params->tmpl);
 cleanup:
 	if (goto_ch)
 		tcf_chain_put_by_act(goto_ch);
-	kfree(params);
+	if (params)
+		tcf_ct_params_free(params);
 	tcf_idr_release(*a, bind);
 	return err;
 }
@@ -1423,7 +1424,7 @@ static void tcf_ct_cleanup(struct tc_action *a)
 
 	params = rcu_dereference_protected(c->params, 1);
 	if (params)
-		call_rcu(&params->rcu, tcf_ct_params_free);
+		call_rcu(&params->rcu, tcf_ct_params_free_rcu);
 }
 
 static int tcf_ct_dump_key_val(struct sk_buff *skb,
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH 3/3 net: sched: add helper support in act_ct

In reply to Xin Long

This patch is to add helper support in act_ct for OVS actions=ct(alg=xxx) offloading, which is corresponding to Commit cae3a2627520 ("openvswitch:
Allow attaching helpers to ct action") in OVS kernel part.

The difference is when adding TC actions family and proto cannot be got from the filter/match, other than helper name in tb[TCA_CT_HELPER_NAME], we also need to send the family in tb[TCA_CT_HELPER_FAMILY] and the proto in tb[TCA_CT_HELPER_PROTO] to kernel.

---
 include/net/tc_act/tc_ct.h        |   1 +
 include/uapi/linux/tc_act/tc_ct.h |   3 +
 net/sched/act_ct.c                | 113 ++++++++++++++++++++++++++++--
 3 files changed, 112 insertions(+), 5 deletions(-)
include/net/tc_act/tc_ct.h +1 -0
index 8250d6f0a462..b24ea2d9400b 100644
@@ -10,6 +10,7 @@
 #include <net/netfilter/nf_conntrack_labels.h>
 
 struct tcf_ct_params {
+	struct nf_conntrack_helper *helper;
 	struct nf_conn *tmpl;
 	u16 zone;
 
include/uapi/linux/tc_act/tc_ct.h +3 -0
index 5fb1d7ac1027..6c5200f0ed38 100644
@@ -22,6 +22,9 @@ enum {
 	TCA_CT_NAT_PORT_MIN,	/* be16 */
 	TCA_CT_NAT_PORT_MAX,	/* be16 */
 	TCA_CT_PAD,
+	TCA_CT_HELPER_NAME,	/* string */
+	TCA_CT_HELPER_FAMILY,	/* u8 */
+	TCA_CT_HELPER_PROTO,	/* u8 */
 	__TCA_CT_MAX
 };
 
net/sched/act_ct.c +108 -5

196 lines not on this page · raw message

Signature · 1 line
2.31.1
maintainer
#4 lore raw

Re: [PATCH net-next 3/3] net: sched: add helper support in act_ct

Quoted 81 lines · Xin Long

This patch is to add helper support in act_ct for OVS actions=ct(alg=xxx) offloading, which is corresponding to Commit cae3a2627520 ("openvswitch:
Allow attaching helpers to ct action") in OVS kernel part.

The difference is when adding TC actions family and proto cannot be got from the filter/match, other than helper name in tb[TCA_CT_HELPER_NAME], we also need to send the family in tb[TCA_CT_HELPER_FAMILY] and the proto in tb[TCA_CT_HELPER_PROTO] to kernel.

---
 include/net/tc_act/tc_ct.h        |   1 +
 include/uapi/linux/tc_act/tc_ct.h |   3 +
 net/sched/act_ct.c                | 113 ++++++++++++++++++++++++++++--
 3 files changed, 112 insertions(+), 5 deletions(-)
include/net/tc_act/tc_ct.h +1 -0
index 8250d6f0a462..b24ea2d9400b 100644
@@ -10,6 +10,7 @@
 #include <net/netfilter/nf_conntrack_labels.h>
 
 struct tcf_ct_params {
+	struct nf_conntrack_helper *helper;
 	struct nf_conn *tmpl;
 	u16 zone;
 
include/uapi/linux/tc_act/tc_ct.h +3 -0
index 5fb1d7ac1027..6c5200f0ed38 100644
@@ -22,6 +22,9 @@ enum {
 	TCA_CT_NAT_PORT_MIN,	/* be16 */
 	TCA_CT_NAT_PORT_MAX,	/* be16 */
 	TCA_CT_PAD,
+	TCA_CT_HELPER_NAME,	/* string */
+	TCA_CT_HELPER_FAMILY,	/* u8 */
+	TCA_CT_HELPER_PROTO,	/* u8 */
 	__TCA_CT_MAX
 };
 
net/sched/act_ct.c +10 -2
index 193a460a9d7f..f237c27079db 100644
@@ -33,6 +33,7 @@
 #include <net/netfilter/nf_conntrack_acct.h>
 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
 #include <net/netfilter/nf_conntrack_act_ct.h>
+#include <net/netfilter/nf_conntrack_seqadj.h>
 #include <uapi/linux/netfilter/nf_nat.h>
 
 static struct workqueue_struct *act_ct_wq;
@@ -655,7 +656,7 @@ struct tc_ct_action_net {
 
 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
-				   u16 zone_id, bool force)
+				   struct tcf_ct_params *p, bool force)
 {
 	enum ip_conntrack_info ctinfo;
 	struct nf_conn *ct;
@@ -665,8 +666,15 @@ static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
 		return false;
 	if (!net_eq(net, read_pnet(&ct->ct_net)))
 		goto drop_ct;
-	if (nf_ct_zone(ct)->id != zone_id)
+	if (nf_ct_zone(ct)->id != p->zone)
 		goto drop_ct;
+	if (p->helper) {
+		struct nf_conn_help *help;
+
+		help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
+		if (help && rcu_access_pointer(help->helper) != p->helper)
+			goto drop_ct;
+	}
 
 	/* Force conntrack entry direction. */
 	if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {

net/sched/act_ct.c static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, · line 849

Quoted 10 lines
net/sched/act_ct.c +7 -0
@@ -832,6 +840,13 @@ static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
 
 static void tcf_ct_params_free(struct tcf_ct_params *params)
 {
+	if (params->helper) {
+#if IS_ENABLED(CONFIG_NF_NAT)
+		if (params->ct_action & TCA_CT_ACT_NAT)
+			nf_nat_helper_put(params->helper);
+#endif
+		nf_conntrack_helper_put(params->helper);
+	}

There is exactly the same code chunk in __ovs_ct_free_action(), I guess you can extract a common helper here, too.

Quoted 22 lines
net/sched/act_ct.c +2 -1
 	if (params->ct_ft)
 		tcf_ct_flow_table_put(params->ct_ft);
 	if (params->tmpl)
@@ -1033,6 +1048,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
 	struct nf_hook_state state;
 	int nh_ofs, err, retval;
 	struct tcf_ct_params *p;
+	bool add_helper = false;
 	bool skip_add = false;
 	bool defrag = false;
 	struct nf_conn *ct;
@@ -1086,7 +1102,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
 	 * actually run the packet through conntrack twice unless it's for a
 	 * different zone.
 	 */
-	cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
+	cached = tcf_ct_skb_nfct_cached(net, skb, p, force);
 	if (!cached) {
 		if (tcf_ct_flow_table_lookup(p, skb, family)) {
 			skip_add = true;
@@ -1119,6 +1135,22 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
 	if (err != NF_ACCEPT)

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1145

Quoted 10 lines
net/sched/act_ct.c +8 -0
 		goto drop;
 
+	if (commit && p->helper && !nfct_help(ct)) {
+		err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
+		if (err)
+			goto drop;
+		add_helper = true;
+		if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
+			if (!nfct_seqadj_ext_add(ct))
+				return -EINVAL;

This return looks suspect/wrong. It will confuse the tc action mechanism. I guess you shold do

				goto drop;

even here.

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1151

Quoted 6 lines
net/sched/act_ct.c +6 -0
+		}
+	}
+
+	if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
+		if (nf_ct_helper(skb, family) != NF_ACCEPT)
+			goto drop;

With the above change, this chunk closely resamble

https://elixir.bootlin.com/linux/latest/source/net/openvswitch/conntrack.c#L1018
...
https://elixir.bootlin.com/linux/latest/source/net/openvswitch/conntrack.c#L1042

opening to an additional common helper;) Not strictly necessary, just nice to have :)

Thanks!

Paolo

author
#5 lore raw

Re: [PATCH net-next 3/3] net: sched: add helper support in act_ct

Quoted 132 lines · Paolo Abeni
Quoted 81 lines · Xin Long

This patch is to add helper support in act_ct for OVS actions=ct(alg=xxx) offloading, which is corresponding to Commit cae3a2627520 ("openvswitch:
Allow attaching helpers to ct action") in OVS kernel part.

The difference is when adding TC actions family and proto cannot be got from the filter/match, other than helper name in tb[TCA_CT_HELPER_NAME], we also need to send the family in tb[TCA_CT_HELPER_FAMILY] and the proto in tb[TCA_CT_HELPER_PROTO] to kernel.

---
 include/net/tc_act/tc_ct.h        |   1 +
 include/uapi/linux/tc_act/tc_ct.h |   3 +
 net/sched/act_ct.c                | 113 ++++++++++++++++++++++++++++--
 3 files changed, 112 insertions(+), 5 deletions(-)
include/net/tc_act/tc_ct.h +1 -0
index 8250d6f0a462..b24ea2d9400b 100644
@@ -10,6 +10,7 @@
 #include <net/netfilter/nf_conntrack_labels.h>
 
 struct tcf_ct_params {
+     struct nf_conntrack_helper *helper;
      struct nf_conn *tmpl;
      u16 zone;
 
include/uapi/linux/tc_act/tc_ct.h +3 -0
index 5fb1d7ac1027..6c5200f0ed38 100644
@@ -22,6 +22,9 @@ enum {
      TCA_CT_NAT_PORT_MIN,    /* be16 */
      TCA_CT_NAT_PORT_MAX,    /* be16 */
      TCA_CT_PAD,
+     TCA_CT_HELPER_NAME,     /* string */
+     TCA_CT_HELPER_FAMILY,   /* u8 */
+     TCA_CT_HELPER_PROTO,    /* u8 */
      __TCA_CT_MAX
 };
 
net/sched/act_ct.c +10 -2
index 193a460a9d7f..f237c27079db 100644
@@ -33,6 +33,7 @@
 #include <net/netfilter/nf_conntrack_acct.h>
 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
 #include <net/netfilter/nf_conntrack_act_ct.h>
+#include <net/netfilter/nf_conntrack_seqadj.h>
 #include <uapi/linux/netfilter/nf_nat.h>
 
 static struct workqueue_struct *act_ct_wq;
@@ -655,7 +656,7 @@ struct tc_ct_action_net {
 
 /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
 static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
-                                u16 zone_id, bool force)
+                                struct tcf_ct_params *p, bool force)
 {
      enum ip_conntrack_info ctinfo;
      struct nf_conn *ct;
@@ -665,8 +666,15 @@ static bool tcf_ct_skb_nfct_cached(struct net *net, struct sk_buff *skb,
              return false;
      if (!net_eq(net, read_pnet(&ct->ct_net)))
              goto drop_ct;
-     if (nf_ct_zone(ct)->id != zone_id)
+     if (nf_ct_zone(ct)->id != p->zone)
              goto drop_ct;
+     if (p->helper) {
+             struct nf_conn_help *help;
+
+             help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
+             if (help && rcu_access_pointer(help->helper) != p->helper)
+                     goto drop_ct;
+     }
 
      /* Force conntrack entry direction. */
      if (force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {

net/sched/act_ct.c static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb, · line 849

Quoted 10 lines
net/sched/act_ct.c +7 -0
@@ -832,6 +840,13 @@ static int tcf_ct_handle_fragments(struct net *net, struct sk_buff *skb,
 
 static void tcf_ct_params_free(struct tcf_ct_params *params)
 {
+     if (params->helper) {
+#if IS_ENABLED(CONFIG_NF_NAT)
+             if (params->ct_action & TCA_CT_ACT_NAT)
+                     nf_nat_helper_put(params->helper);
+#endif
+             nf_conntrack_helper_put(params->helper);
+     }

There is exactly the same code chunk in __ovs_ct_free_action(), I guess you can extract a common helper here, too.

Quoted 22 lines
net/sched/act_ct.c +2 -1
      if (params->ct_ft)
              tcf_ct_flow_table_put(params->ct_ft);
      if (params->tmpl)
@@ -1033,6 +1048,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
      struct nf_hook_state state;
      int nh_ofs, err, retval;
      struct tcf_ct_params *p;
+     bool add_helper = false;
      bool skip_add = false;
      bool defrag = false;
      struct nf_conn *ct;
@@ -1086,7 +1102,7 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
       * actually run the packet through conntrack twice unless it's for a
       * different zone.
       */
-     cached = tcf_ct_skb_nfct_cached(net, skb, p->zone, force);
+     cached = tcf_ct_skb_nfct_cached(net, skb, p, force);
      if (!cached) {
              if (tcf_ct_flow_table_lookup(p, skb, family)) {
                      skip_add = true;
@@ -1119,6 +1135,22 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
      if (err != NF_ACCEPT)

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1145

Quoted 10 lines
net/sched/act_ct.c +8 -0
              goto drop;
 
+     if (commit && p->helper && !nfct_help(ct)) {
+             err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
+             if (err)
+                     goto drop;
+             add_helper = true;
+             if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
+                     if (!nfct_seqadj_ext_add(ct))
+                             return -EINVAL;

This return looks suspect/wrong. It will confuse the tc action mechanism. I guess you shold do

Quoted 3 lines
                                goto drop;

even here.

You're right, will fix it.

Thanks.

Quoted 22 lines

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1151

Quoted 7 lines
net/sched/act_ct.c +6 -0
+             }
+     }
+
+     if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
+             if (nf_ct_helper(skb, family) != NF_ACCEPT)
+                     goto drop;

With the above change, this chunk closely resamble

https://elixir.bootlin.com/linux/latest/source/net/openvswitch/conntrack.c#L1018
...
https://elixir.bootlin.com/linux/latest/source/net/openvswitch/conntrack.c#L1042

opening to an additional common helper;) Not strictly necessary, just nice to have :)

Thanks!

Paolo

#6 lore raw

Re: [PATCH net-next 3/3] net: sched: add helper support in act_ct

In reply to Xin Long

On Tue, Oct 04, 2022 at 09:19:56PM -0400, Xin Long wrote:
[...]

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1144

Quoted 11 lines
net/sched/act_ct.c +7 -0
@@ -1119,6 +1135,22 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
 	if (err != NF_ACCEPT)
 		goto drop;
 
+	if (commit && p->helper && !nfct_help(ct)) {
+		err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
+		if (err)
+			goto drop;
+		add_helper = true;
+		if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
+			if (!nfct_seqadj_ext_add(ct))

You can only add ct extensions if ct is !nf_ct_is_confirmed(ct)), is this guaranteed in this codepath?

Quoted 12 lines
net/sched/act_ct.c +9 -0
+				return -EINVAL;
+		}
+	}
+
+	if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
+		if (nf_ct_helper(skb, family) != NF_ACCEPT)
+			goto drop;
+	}
+
 	if (commit) {
 		tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
 		tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);
author
#7 lore raw

Re: [PATCH net-next 3/3] net: sched: add helper support in act_ct

Quoted 7 lines · Pablo Neira Ayuso

On Tue, Oct 04, 2022 at 09:19:56PM -0400, Xin Long wrote:
[...]

Quoted 4 lines
net/sched/act_ct.c +0 -0
@@ -1119,6 +1135,22 @@ static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a,
      if (err != NF_ACCEPT)
              goto drop;
 
Quoted 10 lines

net/sched/act_ct.c static int tcf_ct_act(struct sk_buff *skb, const struct tc_action *a, · line 1144

Quoted 7 lines
net/sched/act_ct.c +7 -0
+     if (commit && p->helper && !nfct_help(ct)) {
+             err = __nf_ct_try_assign_helper(ct, p->tmpl, GFP_ATOMIC);
+             if (err)
+                     goto drop;
+             add_helper = true;
+             if (p->ct_action & TCA_CT_ACT_NAT && !nfct_seqadj(ct)) {
+                     if (!nfct_seqadj_ext_add(ct))

You can only add ct extensions if ct is !nf_ct_is_confirmed(ct)), is this guaranteed in this codepath?

This is a good catch, the same issue exists on __nf_ct_try_assign_helper(), and also in __ovs_ct_lookup().

I could trigger the warning on OVS conntrack with the flow:

table=0, in_port=veth1,tcp,tcp_dst=2121,ct_state=-trk actions=ct(commit, table=1)
table=1, in_port=veth1,tcp,tcp_dst=2121,ct_state=+trk+new actions=ct(commit, alg=ftp),normal

I will prepare a fix for ovs conntrack first.

Thanks.

Quoted 13 lines
Quoted 13 lines
net/sched/act_ct.c +9 -0
+                             return -EINVAL;
+             }
+     }
+
+     if (nf_ct_is_confirmed(ct) ? ((!cached && !skip_add) || add_helper) : commit) {
+             if (nf_ct_helper(skb, family) != NF_ACCEPT)
+                     goto drop;
+     }
+
      if (commit) {
              tcf_ct_act_set_mark(ct, p->mark, p->mark_mask);
              tcf_ct_act_set_labels(ct, p->labels, p->labels_mask);