Allow offloading of UDP NEW connections via act_ct
Subsystems (6)
v4 cover Allow offloading of UDP NEW connections via act_ct
Currently only bidirectional established connections can be offloaded via act_ct. Such approach allows to hardcode a lot of assumptions into act_ct, flow_table and flow_offload intermediate layer codes. In order to enabled offloading of unidirectional UDP NEW connections start with incrementally changing the following assumptions:
- Drivers assume that only established connections are offloaded and don't support updating existing connections. Extract ctinfo from meta action cookie and refuse offloading of new connections in the drivers.
- Fix flow_table offload fixup algorithm to calculate flow timeout according to current connection state instead of hardcoded "established" value.
- Add new flow_table flow flag that designates bidirectional connections instead of assuming it and hardcoding hardware offload of every flow in both directions.
- Add new flow_table flow "ext_data" field and use it in act_ct to track the ctinfo of offloaded flows instead of assuming that it is always "established".
With all the necessary infrastructure in place modify act_ct to offload UDP NEW as unidirectional connection. Pass reply direction traffic to CT and promote connection to bidirectional when UDP connection state changes to "assured". Rely on refresh mechanism to propagate connection state change to supporting drivers.
Note that early drop algorithm that is designed to free up some space in connection tracking table when it becomes full (by randomly deleting up to 5% of non-established connections) currently ignores connections marked as "offloaded". Now, with UDP NEW connections becoming "offloaded" it could allow malicious user to perform DoS attack by filling the table with non-droppable UDP NEW connections by sending just one packet in single direction. To prevent such scenario change early drop algorithm to also consider "offloaded" connections for deletion.
Vlad Buslov (7):
net: flow_offload: provision conntrack info in ct_metadata
netfilter: flowtable: fixup UDP timeout depending on ct state
netfilter: flowtable: allow unidirectional rules
netfilter: flowtable: save ctinfo in flow_offload net/sched: act_ct: set ctinfo in meta action depending on ct state net/sched: act_ct: offload UDP NEW connections
netfilter: nf_conntrack: allow early drop of offloaded UDP conns
.../ethernet/mellanox/mlx5/core/en/tc_ct.c | 4 + .../ethernet/netronome/nfp/flower/conntrack.c | 24 +++++ include/net/netfilter/nf_flow_table.h | 14 ++- net/netfilter/nf_conntrack_core.c | 11 +- net/netfilter/nf_flow_table_core.c | 40 ++++--- net/netfilter/nf_flow_table_inet.c | 2 +- net/netfilter/nf_flow_table_ip.c | 17 +-- net/netfilter/nf_flow_table_offload.c | 18 ++-- net/sched/act_ct.c | 100 ++++++++++++++---- 9 files changed, 175 insertions(+), 55 deletions(-)
Signature · 1 line
2.38.1
PATCH v4 1/7 net: flow_offload: provision conntrack info in ct_metadata
In order to offload connections in other states besides "established" the driver offload callbacks need to have access to connection conntrack info. Flow offload intermediate representation data structure already contains that data encoded in 'cookie' field, so just reuse it in the drivers.
Reject offloading IP_CT_NEW connections for now by returning an error in relevant driver callbacks based on value of ctinfo. Support for offloading such connections will need to be added to the drivers afterwards.
---
Notes:
Changes V3 -> V4:
- Only obtain ctinfo in mlx5 after checking the meta action pointer.
Changes V2 -> V3:
- Reuse existing meta action 'cookie' field to obtain ctinfo instead of introducing a new field as suggested by Marcelo.
Changes V1 -> V2:
- Add missing include that caused compilation errors on certain configs.
- Change naming in nfp driver as suggested by Simon and Baowen.
.../ethernet/mellanox/mlx5/core/en/tc_ct.c | 4 ++++ .../ethernet/netronome/nfp/flower/conntrack.c | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/en/tc_ct.c +4 -0
@@ -1073,12 +1073,16 @@ mlx5_tc_ct_block_flow_offload_add(struct mlx5_ct_ft *ft,struct mlx5_tc_ct_priv *ct_priv = ft->ct_priv;struct flow_action_entry *meta_action;unsigned long cookie = flow->cookie; +enum ip_conntrack_info ctinfo;struct mlx5_ct_entry *entry;int err; meta_action =mlx5_tc_ct_get_ct_metadata_action (flow_rule);if (!meta_action)return -EOPNOTSUPP ; + ctinfo = meta_action->ct_metadata.cookie &NFCT_INFOMASK ; +if (ctinfo ==IP_CT_NEW ) +return -EOPNOTSUPP ;spin_lock_bh (&ct_priv->ht_lock); entry =rhashtable_lookup_fast (&ft->ct_entries_ht, &cookie, cts_ht_params);
drivers/net/ethernet/netronome/nfp/flower/conntrack.c +24 -0
@@ -1964,6 +1964,27 @@ int nfp_fl_ct_stats(struct flow_cls_offload *flow,return 0 ; } +static bool +nfp_fl_ct_offload_nft_supported (struct flow_cls_offload *flow) +{ +struct flow_rule *flow_rule = flow->rule; +struct flow_action *flow_action = + &flow_rule->action; +struct flow_action_entry *act; +int i; + +flow_action_for_each (i, act, flow_action) { +if (act->id ==FLOW_ACTION_CT_METADATA ) { +enum ip_conntrack_info ctinfo = + act->ct_metadata.cookie &NFCT_INFOMASK ; + +return ctinfo !=IP_CT_NEW ; + } + } + +return false; +} +static int nfp_fl_ct_offload_nft_flow (struct nfp_fl_ct_zone_entry *zt,struct flow_cls_offload *flow) {
@@ -1976,6 +1997,9 @@ nfp_fl_ct_offload_nft_flow(struct nfp_fl_ct_zone_entry *zt, struct flow_cls_offl extack = flow->common.extack;switch (flow->command) {case FLOW_CLS_REPLACE : +if (!nfp_fl_ct_offload_nft_supported (flow)) +return -EOPNOTSUPP ; +/* Netfilter can request offload multiple times for the same * flow - protect against adding duplicates. */
Signature · 1 line
2.38.1
PATCH v4 2/7 netfilter: flowtable: fixup UDP timeout depending on ct state
In reply to Vlad BuslovCurrently flow_offload_fixup_ct() function assumes that only replied UDP connections can be offloaded and hardcodes UDP_CT_REPLIED timeout value. Allow users to modify timeout calculation by implementing new flowtable type callback 'timeout' and use the existing algorithm otherwise.
To enable UDP NEW connection offload in following patches implement 'timeout' callback in flowtable_ct of act_ct which extracts the actual connections state from ct->status and set the timeout according to it.
---
Notes:
Changes V3 -> V4:
- Rework the patch to decouple netfilter and act_ct timeout fixup algorithms.
include/net/netfilter/nf_flow_table.h | 6 +++- net/netfilter/nf_flow_table_core.c | 40 +++++++++++++++++++-------- net/netfilter/nf_flow_table_ip.c | 17 ++++++------ net/sched/act_ct.c | 35 ++++++++++++++++++++++- 4 files changed, 76 insertions(+), 22 deletions(-)
include/net/netfilter/nf_flow_table.h +5 -1
@@ -61,6 +61,9 @@ struct nf_flowtable_type {enum flow_offload_tuple_dir dir, struct nf_flow_rule *flow_rule);void (*free)(struct nf_flowtable *ft); +bool (*timeout)(struct nf_flowtable *ft, +struct flow_offload *flow, +s32 *val);nf_hookfn *hook;struct module *owner; };
@@ -278,7 +281,8 @@ void nf_flow_table_cleanup(struct net_device *dev);int nf_flow_table_init (struct nf_flowtable *flow_table);void nf_flow_table_free (struct nf_flowtable *flow_table); -void flow_offload_teardown (struct flow_offload *flow); +void flow_offload_teardown (struct nf_flowtable *flow_table, +struct flow_offload *flow); void nf_flow_snat_port(const struct flow_offload *flow, struct sk_buff *skb, unsigned int thoff,
net/netfilter/nf_flow_table_core.c +28 -12
@@ -178,28 +178,43 @@ static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp) tcp->seen[1 ].td_maxwin =0 ; } -static void flow_offload_fixup_ct (struct nf_conn *ct) +static bool flow_offload_timeout_default (struct nf_conn *ct,s32 *timeout) {struct net *net =nf_ct_net (ct);int l4num =nf_ct_protonum (ct); -s32 timeout;if (l4num ==IPPROTO_TCP ) {struct nf_tcp_net *tn =nf_tcp_pernet (net);flow_offload_fixup_tcp (&ct->proto.tcp); - timeout = tn->timeouts[ct->proto.tcp.state]; - timeout -= tn->offload_timeout; + *timeout = tn->timeouts[ct->proto.tcp.state]; + *timeout -= tn->offload_timeout; }else if (l4num ==IPPROTO_UDP ) {struct nf_udp_net *tn =nf_udp_pernet (net); - timeout = tn->timeouts[UDP_CT_REPLIED ]; - timeout -= tn->offload_timeout; + *timeout = tn->timeouts[UDP_CT_REPLIED ]; + *timeout -= tn->offload_timeout; }else { -return ; +return false; } +return true; +} + +static void flow_offload_fixup_ct (struct nf_flowtable *flow_table, +struct flow_offload *flow) +{ +struct nf_conn *ct = flow->ct; +bool needs_fixup; +s32 timeout; + + needs_fixup = flow_table->type->timeout ? + flow_table->type->timeout (flow_table, flow, &timeout) : +flow_offload_timeout_default (ct, &timeout); +if (!needs_fixup) +return ; +if (timeout <0 ) timeout =0 ;
@@ -348,11 +363,12 @@ static void flow_offload_del(struct nf_flowtable *flow_table,flow_offload_free (flow); } -void flow_offload_teardown (struct flow_offload *flow) +void flow_offload_teardown (struct nf_flowtable *flow_table, +struct flow_offload *flow) {clear_bit (IPS_OFFLOAD_BIT , &flow->ct->status);set_bit (NF_FLOW_TEARDOWN , &flow->flags); -flow_offload_fixup_ct (flow->ct); +flow_offload_fixup_ct (flow_table, flow); }EXPORT_SYMBOL_GPL (flow_offload_teardown);
@@ -421,7 +437,7 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table, {if (nf_flow_has_expired (flow) ||nf_ct_is_dying (flow->ct)) -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);if (test_bit (NF_FLOW_TEARDOWN , &flow->flags)) {if (test_bit (NF_FLOW_HW , &flow->flags)) {
@@ -569,14 +585,14 @@ static void nf_flow_table_do_cleanup(struct nf_flowtable *flow_table,struct net_device *dev = data;if (!dev) { -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);return ; }if (net_eq (nf_ct_net (flow->ct),dev_net (dev)) && (flow->tuplehash[0 ].tuple.iifidx == dev->ifindex || flow->tuplehash[1 ].tuple.iifidx == dev->ifindex)) -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow); } void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
net/netfilter/nf_flow_table_ip.c +9 -8
@@ -18,7 +18,8 @@#include <linux/tcp.h> #include <linux/udp.h> -static int nf_flow_state_check (struct flow_offload *flow,int proto, +static int nf_flow_state_check (struct nf_flowtable *flow_table, +struct flow_offload *flow,int proto,struct sk_buff *skb,unsigned int thoff) {struct tcphdr *tcph;
@@ -28,7 +29,7 @@ static int nf_flow_state_check(struct flow_offload *flow, int proto, tcph = (void *)(skb_network_header (skb) + thoff);if (unlikely (tcph->fin || tcph->rst)) { -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);return -1 ; }
@@ -373,11 +374,11 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, iph = (struct iphdr *)(skb_network_header (skb) + offset); thoff = (iph->ihl *4 ) + offset; -if (nf_flow_state_check (flow, iph->protocol, skb, thoff)) +if (nf_flow_state_check (flow_table, flow, iph->protocol, skb, thoff))return NF_ACCEPT ;if (!nf_flow_dst_check (&tuplehash->tuple)) { -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);return NF_ACCEPT ; }
@@ -419,7 +420,7 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,case FLOW_OFFLOAD_XMIT_DIRECT : ret =nf_flow_queue_xmit (state->net, skb, tuplehash,ETH_P_IP );if (ret ==NF_DROP ) -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);break ;default :WARN_ON_ONCE (1 );
@@ -639,11 +640,11 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb, ip6h = (struct ipv6hdr *)(skb_network_header (skb) + offset); thoff =sizeof (*ip6h) + offset; -if (nf_flow_state_check (flow, ip6h->nexthdr, skb, thoff)) +if (nf_flow_state_check (flow_table, flow, ip6h->nexthdr, skb, thoff))return NF_ACCEPT ;if (!nf_flow_dst_check (&tuplehash->tuple)) { -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);return NF_ACCEPT ; }
@@ -684,7 +685,7 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,case FLOW_OFFLOAD_XMIT_DIRECT : ret =nf_flow_queue_xmit (state->net, skb, tuplehash,ETH_P_IPV6 );if (ret ==NF_DROP ) -flow_offload_teardown (flow); +flow_offload_teardown (flow_table, flow);break ;default :WARN_ON_ONCE (1 );
net/sched/act_ct.c +34 -1
@@ -274,8 +274,41 @@ static int tcf_ct_flow_table_fill_actions(struct net *net,return err; } +static bool tcf_ct_flow_table_get_timeout (struct nf_flowtable *ft, +struct flow_offload *flow, +s32 *val) +{ +struct nf_conn *ct = flow->ct; +int l4num = +nf_ct_protonum (ct); +struct net *net = +nf_ct_net (ct); + +if (l4num ==IPPROTO_TCP ) { +struct nf_tcp_net *tn =nf_tcp_pernet (net); + + ct->proto.tcp.seen[0 ].td_maxwin =0 ; + ct->proto.tcp.seen[1 ].td_maxwin =0 ; + *val = tn->timeouts[ct->proto.tcp.state]; + *val -= tn->offload_timeout; + }else if (l4num ==IPPROTO_UDP ) { +struct nf_udp_net *tn =nf_udp_pernet (net); +enum udp_conntrack state = +test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +UDP_CT_REPLIED :UDP_CT_UNREPLIED ; + + *val = tn->timeouts[state]; + *val -= tn->offload_timeout; + }else { +return false; + } + +return true; +} +static struct nf_flowtable_type flowtable_ct = { .action = tcf_ct_flow_table_fill_actions, + .timeout = tcf_ct_flow_table_get_timeout, .owner =THIS_MODULE , };
@@ -622,7 +655,7 @@ static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, ct = flow->ct;if (tcph && (unlikely (tcph->fin || tcph->rst))) { -flow_offload_teardown (flow); +flow_offload_teardown (nf_ft, flow);return false; }
Signature · 1 line
2.38.1
PATCH v4 3/7 netfilter: flowtable: allow unidirectional rules
In reply to Vlad BuslovModify flow table offload to support unidirectional connections by extending enum nf_flow_flags with new "NF_FLOW_HW_BIDIRECTIONAL" flag. Only offload reply direction when the flag is set. This infrastructure change is necessary to support offloading UDP NEW connections in original direction in following patches in series.
---
Notes:
Changes V2 -> V3:
- Fix error in commit message (spotted by Marcelo).
include/net/netfilter/nf_flow_table.h | 1 + net/netfilter/nf_flow_table_offload.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +1 -0
@@ -167,6 +167,7 @@ enum nf_flow_flags {NF_FLOW_HW_DYING ,NF_FLOW_HW_DEAD ,NF_FLOW_HW_PENDING , + NF_FLOW_HW_BIDIRECTIONAL, }; enum flow_offload_type {
net/netfilter/nf_flow_table_offload.c +8 -4
@@ -895,8 +895,9 @@ static int flow_offload_rule_add(struct flow_offload_work *offload, ok_count +=flow_offload_tuple_add (offload, flow_rule[0 ],FLOW_OFFLOAD_DIR_ORIGINAL ); - ok_count +=flow_offload_tuple_add (offload, flow_rule[1 ], -FLOW_OFFLOAD_DIR_REPLY ); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) + ok_count +=flow_offload_tuple_add (offload, flow_rule[1 ], +FLOW_OFFLOAD_DIR_REPLY );if (ok_count ==0 )return -ENOENT ;
@@ -926,7 +927,8 @@ static void flow_offload_work_del(struct flow_offload_work *offload) {clear_bit (IPS_HW_OFFLOAD_BIT , &offload->flow->ct->status);flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_ORIGINAL ); -flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY ); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) +flow_offload_tuple_del (offload,FLOW_OFFLOAD_DIR_REPLY );set_bit (NF_FLOW_HW_DEAD , &offload->flow->flags); }
@@ -946,7 +948,9 @@ static void flow_offload_work_stats(struct flow_offload_work *offload)u64 lastused;flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_ORIGINAL , &stats[0 ]); -flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_REPLY , &stats[1 ]); +if (test_bit (NF_FLOW_HW_BIDIRECTIONAL , &offload->flow->flags)) +flow_offload_tuple_stats (offload,FLOW_OFFLOAD_DIR_REPLY , + &stats[1 ]); lastused =max_t (u64, stats[0 ].lastused, stats[1 ].lastused); offload->flow->timeout = max_t(u64, offload->flow->timeout,
Signature · 1 line
2.38.1
PATCH v4 4/7 netfilter: flowtable: save ctinfo in flow_offload
In reply to Vlad BuslovExtend struct flow_offload with generic 'ext_data' field. Use the field in act_ct to cache the last ctinfo value that was used to update the hardware offload when generating the actions. This is used to optimize the flow refresh algorithm in following patches.
---
Notes:
Changes V3 -> V4:
- New patch replaces gc async update that is no longer needed after refactoring of following act_ct patches.
include/net/netfilter/nf_flow_table.h | 7 ++++--- net/netfilter/nf_flow_table_inet.c | 2 +- net/netfilter/nf_flow_table_offload.c | 6 +++--- net/sched/act_ct.c | 12 +++++++----- 4 files changed, 15 insertions(+), 12 deletions(-)
include/net/netfilter/nf_flow_table.h +4 -3
@@ -57,7 +57,7 @@ struct nf_flowtable_type {struct net_device *dev, enum flow_block_command cmd);int (*action)(struct net *net, -const struct flow_offload *flow, +struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule);void (*free)(struct nf_flowtable *ft);
@@ -178,6 +178,7 @@ enum flow_offload_type {struct flow_offload {struct flow_offload_tuple_rhash tuplehash[FLOW_OFFLOAD_DIR_MAX ];struct nf_conn *ct; +void *ext_data;unsigned long flags;u16 type;u32 timeout;
@@ -317,10 +318,10 @@ void nf_flow_table_offload_flush_cleanup(struct nf_flowtable *flowtable);int nf_flow_table_offload_setup (struct nf_flowtable *flowtable,struct net_device *dev,enum flow_block_command cmd); -int nf_flow_rule_route_ipv4 (struct net *net,const struct flow_offload *flow, +int nf_flow_rule_route_ipv4 (struct net *net,struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule); -int nf_flow_rule_route_ipv6 (struct net *net,const struct flow_offload *flow, +int nf_flow_rule_route_ipv6 (struct net *net,struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule);
net/netfilter/nf_flow_table_inet.c +1 -1
@@ -39,7 +39,7 @@ nf_flow_offload_inet_hook(void *priv, struct sk_buff *skb, }static int nf_flow_rule_route_inet (struct net *net, -const struct flow_offload *flow, +struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule) {
net/netfilter/nf_flow_table_offload.c +3 -3
@@ -679,7 +679,7 @@ nf_flow_rule_route_common(struct net *net, const struct flow_offload *flow,return 0 ; } -int nf_flow_rule_route_ipv4 (struct net *net,const struct flow_offload *flow, +int nf_flow_rule_route_ipv4 (struct net *net,struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule) {
@@ -704,7 +704,7 @@ int nf_flow_rule_route_ipv4(struct net *net, const struct flow_offload *flow, }EXPORT_SYMBOL_GPL (nf_flow_rule_route_ipv4); -int nf_flow_rule_route_ipv6 (struct net *net,const struct flow_offload *flow, +int nf_flow_rule_route_ipv6 (struct net *net,struct flow_offload *flow,enum flow_offload_tuple_dir dir,struct nf_flow_rule *flow_rule) {
@@ -735,7 +735,7 @@ nf_flow_offload_rule_alloc(struct net *net, {const struct nf_flowtable *flowtable = offload->flowtable;const struct flow_offload_tuple *tuple, *other_tuple; -const struct flow_offload *flow = offload->flow; +struct flow_offload *flow = offload->flow;struct dst_entry *other_dst =NULL ;struct nf_flow_rule *flow_rule;int err = -ENOMEM ;
net/sched/act_ct.c +7 -5
@@ -170,11 +170,11 @@ tcf_ct_flow_table_add_action_nat_udp(const struct nf_conntrack_tuple *tuple,static void tcf_ct_flow_table_add_action_meta (struct nf_conn *ct,enum ip_conntrack_dir dir, +enum ip_conntrack_info ctinfo,struct flow_action *action) {struct nf_conn_labels *ct_labels;struct flow_action_entry *entry; -enum ip_conntrack_info ctinfo;u32 *act_ct_labels; entry =tcf_ct_flow_table_flow_action_get_next (action);
@@ -182,8 +182,6 @@ static void tcf_ct_flow_table_add_action_meta(struct nf_conn *ct,#if IS_ENABLED (CONFIG_NF_CONNTRACK_MARK ) entry->ct_metadata.mark =READ_ONCE (ct->mark);#endif - ctinfo = dir ==IP_CT_DIR_ORIGINAL ?IP_CT_ESTABLISHED : -IP_CT_ESTABLISHED_REPLY ;/* aligns with the CT reference on the SKB nf_ct_set */ entry->ct_metadata.cookie = (unsigned long )ct | ctinfo; entry->ct_metadata.orig_dir = dir ==IP_CT_DIR_ORIGINAL ;
@@ -237,22 +235,26 @@ static int tcf_ct_flow_table_add_action_nat(struct net *net, }static int tcf_ct_flow_table_fill_actions (struct net *net, -const struct flow_offload *flow, +struct flow_offload *flow,enum flow_offload_tuple_dir tdir,struct nf_flow_rule *flow_rule) {struct flow_action *action = &flow_rule->rule->action;int num_entries = action->num_entries;struct nf_conn *ct = flow->ct; +enum ip_conntrack_info ctinfo;enum ip_conntrack_dir dir;int i, err;switch (tdir) {case FLOW_OFFLOAD_DIR_ORIGINAL : dir =IP_CT_DIR_ORIGINAL ; + ctinfo =IP_CT_ESTABLISHED ; +WRITE_ONCE (flow->ext_data, (void *)ctinfo);break ;case FLOW_OFFLOAD_DIR_REPLY : dir =IP_CT_DIR_REPLY ; + ctinfo =IP_CT_ESTABLISHED_REPLY ;break ;default :return -EOPNOTSUPP ;
@@ -262,7 +264,7 @@ static int tcf_ct_flow_table_fill_actions(struct net *net,if (err) goto err_nat; -tcf_ct_flow_table_add_action_meta (ct, dir, action); +tcf_ct_flow_table_add_action_meta (ct, dir, ctinfo, action);return 0 ; err_nat:
Signature · 1 line
2.38.1
PATCH v4 5/7 net/sched: act_ct: set ctinfo in meta action depending on ct state
In reply to Vlad BuslovCurrently tcf_ct_flow_table_add_action_meta() function assumes that only established connections can be offloaded and always sets ctinfo to either IP_CT_ESTABLISHED or IP_CT_ESTABLISHED_REPLY strictly based on direction without checking actual connection state. To enable UDP NEW connection offload set the ctinfo and metadata cookie based on ct->status value.
--- net/sched/act_ct.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/sched/act_ct.c +2 -1
@@ -249,7 +249,8 @@ static int tcf_ct_flow_table_fill_actions(struct net *net,switch (tdir) {case FLOW_OFFLOAD_DIR_ORIGINAL : dir =IP_CT_DIR_ORIGINAL ; - ctinfo =IP_CT_ESTABLISHED ; + ctinfo =test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +IP_CT_ESTABLISHED :IP_CT_NEW ;WRITE_ONCE (flow->ext_data, (void *)ctinfo);break ;case FLOW_OFFLOAD_DIR_REPLY :
Signature · 1 line
2.38.1
PATCH v4 6/7 net/sched: act_ct: offload UDP NEW connections
In reply to Vlad BuslovModify the offload algorithm of UDP connections to the following:
- Offload NEW connection as unidirectional.
- When connection state changes to ESTABLISHED also update the hardware flow. However, in order to prevent act_ct from spamming offload add wq for every packet coming in reply direction in this state verify whether connection has already been updated to ESTABLISHED in the drivers. If that it the case, then skip flow_table and let conntrack handle such packets which will also allow conntrack to potentially promote the connection to ASSURED.
- When connection state changes to ASSURED set the flow_table flow NF_FLOW_HW_BIDIRECTIONAL flag which will cause refresh mechanism to offload the reply direction.
All other protocols have their offload algorithm preserved and are always offloaded as bidirectional.
Note that this change tries to minimize the load on flow_table add workqueue. First, it tracks the last ctinfo that was offloaded by using new flow 'ext_data' field and doesn't schedule the refresh for reply direction packets when the offloads have already been updated with current ctinfo. Second, when 'add' task executes on workqueue it always update the offload with current flow state (by checking 'bidirectional' flow flag and obtaining actual ctinfo/cookie through meta action instead of caching any of these from the moment of scheduling the 'add' work) preventing the need from scheduling more updates if state changed concurrently while the 'add' work was pending on workqueue.
---
Notes:
Changes V3 -> V4:
- Refactor the patch to leverage the refresh code and new flow 'ext_data' field in order to change the offload state instead of relying on async gc update.
net/sched/act_ct.c | 52 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 12 deletions(-)
net/sched/act_ct.c +40 -12
@@ -401,7 +401,7 @@ static void tcf_ct_flow_tc_ifidx(struct flow_offload *entry,static void tcf_ct_flow_table_add (struct tcf_ct_flow_table *ct_ft,struct nf_conn *ct, -bool tcp) +bool tcp,bool bidirectional) {struct nf_conn_act_ct_ext *act_ct_ext;struct flow_offload *entry;
@@ -420,6 +420,8 @@ static void tcf_ct_flow_table_add(struct tcf_ct_flow_table *ct_ft, ct->proto.tcp.seen[0 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; ct->proto.tcp.seen[1 ].flags |=IP_CT_TCP_FLAG_BE_LIBERAL ; } +if (bidirectional) +__set_bit (NF_FLOW_HW_BIDIRECTIONAL , &entry->flags); act_ct_ext =nf_conn_act_ct_ext_find (ct);if (act_ct_ext) {
@@ -443,26 +445,34 @@ static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft,struct nf_conn *ct, enum ip_conntrack_info ctinfo) { - bool tcp = false; - -if ((ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED_REPLY ) || - !test_bit (IPS_ASSURED_BIT , &ct->status)) -return ; + bool tcp = false, bidirectional = true;switch (nf_ct_protonum (ct)) {case IPPROTO_TCP : - tcp = true; -if (ct->proto.tcp.state !=TCP_CONNTRACK_ESTABLISHED ) +if ((ctinfo !=IP_CT_ESTABLISHED && + ctinfo !=IP_CT_ESTABLISHED_REPLY ) || + !test_bit (IPS_ASSURED_BIT , &ct->status) || + ct->proto.tcp.state !=TCP_CONNTRACK_ESTABLISHED )return ; + + tcp = true;break ;case IPPROTO_UDP : +if (!nf_ct_is_confirmed (ct)) +return ; +if (!test_bit (IPS_ASSURED_BIT , &ct->status)) + bidirectional = false;break ;#ifdef CONFIG_NF_CT_PROTO_GRE case IPPROTO_GRE : {struct nf_conntrack_tuple *tuple; -if (ct->status &IPS_NAT_MASK ) +if ((ctinfo !=IP_CT_ESTABLISHED && + ctinfo !=IP_CT_ESTABLISHED_REPLY ) || + !test_bit (IPS_ASSURED_BIT , &ct->status) || + ct->status &IPS_NAT_MASK )return ; + tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL ].tuple;/* No support for GRE v1 */ if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
@@ -478,7 +488,7 @@ static void tcf_ct_flow_table_process_conn(struct tcf_ct_flow_table *ct_ft, ct->status & IPS_SEQ_ADJUST)return ; -tcf_ct_flow_table_add (ct_ft, ct, tcp); +tcf_ct_flow_table_add (ct_ft, ct, tcp, bidirectional); } static bool
@@ -657,13 +667,31 @@ static bool tcf_ct_flow_table_lookup(struct tcf_ct_params *p, flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]); ct = flow->ct; +if (dir ==FLOW_OFFLOAD_DIR_REPLY && + !test_bit (NF_FLOW_HW_BIDIRECTIONAL , &flow->flags)) { +/* Only offload reply direction after connection became +* assured. +*/ +if (test_bit (IPS_ASSURED_BIT , &ct->status)) +set_bit (NF_FLOW_HW_BIDIRECTIONAL , &flow->flags); +else if ((enum ip_conntrack_info )READ_ONCE (flow->ext_data) == +IP_CT_ESTABLISHED ) +/* If flow_table flow has already been updated to the +* established state, then don't refresh. +*/ +return false; + } +if (tcph && (unlikely (tcph->fin || tcph->rst))) {flow_offload_teardown (nf_ft, flow);return false; } - ctinfo = dir ==FLOW_OFFLOAD_DIR_ORIGINAL ?IP_CT_ESTABLISHED : -IP_CT_ESTABLISHED_REPLY ; +if (dir ==FLOW_OFFLOAD_DIR_ORIGINAL ) + ctinfo =test_bit (IPS_SEEN_REPLY_BIT , &ct->status) ? +IP_CT_ESTABLISHED :IP_CT_NEW ; +else + ctinfo =IP_CT_ESTABLISHED_REPLY ;flow_offload_refresh (nf_ft, flow);nf_conntrack_get (&ct->ct_general);
Signature · 1 line
2.38.1
Jakub Kicinski
maintainerRe: [PATCH net-next v4 6/7] net/sched: act_ct: offload UDP NEW connections
Quoted 19 lines · On Tue, 24 Jan 2023 15:02:06 +0100 Vlad Buslov wrote:
Modify the offload algorithm of UDP connections to the following:
- Offload NEW connection as unidirectional.
- When connection state changes to ESTABLISHED also update the hardware flow. However, in order to prevent
act_ctfrom spamming offload add wq for every packet coming in reply direction in this state verify whether connection has already been updated to ESTABLISHED in the drivers. If that it the case, then skipflow_tableand let conntrack handle such packets which will also allow conntrack to potentially promote the connection to ASSURED.- When connection state changes to ASSURED set the
flow_tableflowNF_FLOW_HW_BIDIRECTIONALflag which will cause refresh mechanism to offload the reply direction.All other protocols have their offload algorithm preserved and are always offloaded as bidirectional.
Quoted 10 lines
Note that this change tries to minimize the load on
flow_tableadd workqueue. First, it tracks the last ctinfo that was offloaded by using new flow 'ext_data' field and doesn't schedule the refresh for reply direction packets when the offloads have already been updated with current ctinfo. Second, when 'add' task executes on workqueue it always update the offload with current flow state (by checking 'bidirectional' flow flag and obtaining actual ctinfo/cookie through meta action instead of caching any of these from the moment of scheduling the 'add' work) preventing the need from scheduling more updates if state changed concurrently while the 'add' work was pending on workqueue.
Clang is not happy:
net/sched/act_ct.c:677:12: warning: cast to smaller integer type 'enum ip_conntrack_info' from 'typeof (_Generic((flow->ext_data), char: (char)0, unsigned char: (unsigned char)0, signed char: (signed char)0, unsigned short: (unsigned short)0, short: (short)0, unsigned int: (unsigned int)0, int: (int)0, unsigned long: (unsigned long)0, long: (long)0, unsigned long long: (unsigned long long)0, long long: (long long)0, default: (flow->ext_data)))' (aka 'void *') [-Wvoid-pointer-to-enum-cast]
else if ((enum ip_conntrack_info)READ_ONCE(flow->ext_data) ==
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Re: [PATCH net-next v4 6/7] net/sched: act_ct: offload UDP NEW connections
Quoted 31 lines · Jakub Kicinski
Quoted 19 lines · On Tue, 24 Jan 2023 15:02:06 +0100 Vlad Buslov wrote:
Modify the offload algorithm of UDP connections to the following:
- Offload NEW connection as unidirectional.
- When connection state changes to ESTABLISHED also update the hardware flow. However, in order to prevent
act_ctfrom spamming offload add wq for every packet coming in reply direction in this state verify whether connection has already been updated to ESTABLISHED in the drivers. If that it the case, then skipflow_tableand let conntrack handle such packets which will also allow conntrack to potentially promote the connection to ASSURED.- When connection state changes to ASSURED set the
flow_tableflowNF_FLOW_HW_BIDIRECTIONALflag which will cause refresh mechanism to offload the reply direction.All other protocols have their offload algorithm preserved and are always offloaded as bidirectional.
Quoted 10 lines
Note that this change tries to minimize the load on
flow_tableadd workqueue. First, it tracks the last ctinfo that was offloaded by using new flow 'ext_data' field and doesn't schedule the refresh for reply direction packets when the offloads have already been updated with current ctinfo. Second, when 'add' task executes on workqueue it always update the offload with current flow state (by checking 'bidirectional' flow flag and obtaining actual ctinfo/cookie through meta action instead of caching any of these from the moment of scheduling the 'add' work) preventing the need from scheduling more updates if state changed concurrently while the 'add' work was pending on workqueue.
Quoted 5 lines
Clang is not happy:
net/sched/act_ct.c:677:12: warning: cast to smaller integer type 'enumip_conntrack_info' from 'typeof (_Generic((flow->ext_data), char: (char)0, unsigned char: (unsigned char)0, signed char: (signed char)0, unsigned short: (unsigned short)0, short: (short)0, unsigned int: (unsigned int)0, int: (int)0, unsigned long: (unsigned long)0, long: (long)0, unsigned long long: (unsigned long long)0, long long: (long long)0, default: (flow->ext_data)))' (aka 'void *') [-Wvoid-pointer-to-enum-cast]else if ((enum ip_conntrack_info)READ_ONCE(flow->ext_data) == ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks, will fix it in v5.
PATCH v4 7/7 netfilter: nf_conntrack: allow early drop of offloaded UDP conns
In reply to Vlad BuslovBoth synchronous early drop algorithm and asynchronous gc worker completely ignore connections with IPS_OFFLOAD_BIT status bit set. With new functionality that enabled UDP NEW connection offload in action CT malicious user can flood the conntrack table with offloaded UDP connections by just sending a single packet per 5tuple because such connections can no longer be deleted by early drop algorithm.
To mitigate the issue allow both early drop and gc to consider offloaded UDP connections for deletion.
--- net/netfilter/nf_conntrack_core.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_core.c +6 -5
@@ -1374,9 +1374,6 @@ static unsigned int early_drop_list(struct net *net,hlist_nulls_for_each_entry_rcu (h, n, head, hnnode) { tmp =nf_ct_tuplehash_to_ctrack (h); -if (test_bit (IPS_OFFLOAD_BIT , &tmp->status)) -continue ; -if (nf_ct_is_expired (tmp)) {nf_ct_gc_expired (tmp);continue ;
@@ -1446,11 +1443,14 @@ static bool gc_worker_skip_ct(const struct nf_conn *ct)static bool gc_worker_can_early_drop (const struct nf_conn *ct) {const struct nf_conntrack_l4proto *l4proto; +u8 protonum =nf_ct_protonum (ct); +if (test_bit (IPS_OFFLOAD_BIT , &ct->status) && protonum !=IPPROTO_UDP ) +return false;if (!test_bit (IPS_ASSURED_BIT , &ct->status))return true; - l4proto =nf_ct_l4proto_find (nf_ct_protonum (ct)); + l4proto =nf_ct_l4proto_find (protonum);if (l4proto->can_early_drop && l4proto->can_early_drop (ct))return true;
@@ -1507,7 +1507,8 @@ static void gc_worker(struct work_struct *work)if (test_bit (IPS_OFFLOAD_BIT , &tmp->status)) {nf_ct_offload_timeout (tmp); -continue ; +if (!nf_conntrack_max95) +continue ; }if (expired_count >GC_SCAN_EXPIRED_MAX ) {
Signature · 1 line
2.38.1