Netfilter/IPVS updates for net-next
Subsystems (5)
Pablo Neira Ayuso
maintainercover Netfilter/IPVS updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next:
1) Support for rejecting packets from the prerouting chain, from Laura Garcia Liebana.
2) Remove useless assignment in pipapo, from Stefano Brivio.
3) On demand hook registration in IPVS, from Julian Anastasov.
4) Expire IPVS connection from process context to not overload timers, also from Julian.
5) Fallback to conntrack TCP tracker to handle connection reuse in IPVS, from Julian Anastasov.
6) Several patches to support for chain bindings.
7) Expose enum nft_chain_flags through UAPI.
8) Reject unsupported chain flags from the netlink control plane.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thank you.
----------------------------------------------------------------
The following changes since commit 5fb62372a0207f1514fa6052c51991198c46ffe2:
Merge branch 'dpaa2-eth-send-a-scatter-gather-FD-instead-of-realloc-ing' (2020-06-29 17:42:48 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to c1f79a2eefdcc0aef5d7a911c27a3f75f1936ecd:
netfilter: nf_tables: reject unsupported chain flags (2020-07-04 02:51:28 +0200)
----------------------------------------------------------------
Julian Anastasov (3):
ipvs: register hooks only with services
ipvs: avoid expiring many connections from timer
ipvs: allow connection reuse for unconfirmed conntrack
Laura Garcia Liebana (1):
netfilter: introduce support for reject at prerouting stage
Pablo Neira Ayuso (7):
netfilter: nf_tables: add NFTA_CHAIN_ID attribute
netfilter: nf_tables: add NFTA_RULE_CHAIN_ID attribute
netfilter: nf_tables: add NFTA_VERDICT_CHAIN_ID attribute
netfilter: nf_tables: expose enum nft_chain_flags through UAPI
netfilter: nf_tables: add nft_chain_add()
netfilter: nf_tables: add NFT_CHAIN_BINDING
netfilter: nf_tables: reject unsupported chain flags
Stefano Brivio (1):
netfilter: nft_set_pipapo: Drop useless assignment of scratch map index on insert
include/net/ip_vs.h | 15 ++- include/net/netfilter/nf_tables.h | 23 ++-- include/uapi/linux/netfilter/nf_tables.h | 14 +++ net/ipv4/netfilter/nf_reject_ipv4.c | 21 ++++ net/ipv6/netfilter/nf_reject_ipv6.c | 26 +++++ net/netfilter/ipvs/ip_vs_conn.c | 53 ++++++--- net/netfilter/ipvs/ip_vs_core.c | 92 +++++++++++---- net/netfilter/ipvs/ip_vs_ctl.c | 29 ++++- net/netfilter/nf_tables_api.c | 188 +++++++++++++++++++++++++------ net/netfilter/nft_immediate.c | 51 +++++++++ net/netfilter/nft_reject.c | 3 +- net/netfilter/nft_set_pipapo.c | 2 - 12 files changed, 428 insertions(+), 89 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/12 netfilter: introduce support for reject at prerouting stage
From: Laura Garcia Liebana <[email protected]>
REJECT statement can be only used in INPUT, FORWARD and OUTPUT chains. This patch adds support of REJECT, both icmp and tcp reset, at PREROUTING stage.
The need for this patch comes from the requirement of some forwarding devices to reject traffic before the natting and routing decisions.
The main use case is to be able to send a graceful termination to legitimate clients that, under any circumstances, the NATed endpoints are not available. This option allows clients to decide either to perform a reconnection or manage the error in their side, instead of just dropping the connection and let them die due to timeout.
It is supported ipv4, ipv6 and inet families for nft infrastructure.
--- net/ipv4/netfilter/nf_reject_ipv4.c | 21 +++++++++++++++++++++ net/ipv6/netfilter/nf_reject_ipv6.c | 26 ++++++++++++++++++++++++++ net/netfilter/nft_reject.c | 3 ++- 3 files changed, 49 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nf_reject_ipv4.c +21 -0
@@ -96,6 +96,21 @@ void nf_reject_ip_tcphdr_put(struct sk_buff *nskb, const struct sk_buff *oldskb, }EXPORT_SYMBOL_GPL (nf_reject_ip_tcphdr_put); +static int nf_reject_fill_skb_dst (struct sk_buff *skb_in) +{ +struct dst_entry *dst =NULL ; +struct flowi fl; + +memset (&fl,0 ,sizeof (struct flowi )); + fl.u.ip4.daddr =ip_hdr (skb_in)->saddr; +nf_ip_route (dev_net (skb_in->dev), &dst, &fl, false); +if (!dst) +return -1 ; + +skb_dst_set (skb_in, dst); +return 0 ; +} +/* Send RST reply */ void nf_send_reset (struct net *net,struct sk_buff *oldskb,int hook) {
@@ -109,6 +124,9 @@ void nf_send_reset(struct net *net, struct sk_buff *oldskb, int hook)if (!oth)return ; +if (hook ==NF_INET_PRE_ROUTING &&nf_reject_fill_skb_dst (oldskb)) +return ; +if (skb_rtable (oldskb)->rt_flags & (RTCF_BROADCAST |RTCF_MULTICAST ))return ;
@@ -175,6 +193,9 @@ void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)if (iph->frag_off &htons (IP_OFFSET ))return ; +if (hook ==NF_INET_PRE_ROUTING &&nf_reject_fill_skb_dst (skb_in)) +return ; +if (skb_csum_unnecessary (skb_in) || !nf_reject_verify_csum (proto)) {icmp_send (skb_in,ICMP_DEST_UNREACH , code,0 );return ;
net/ipv6/netfilter/nf_reject_ipv6.c +26 -0
@@ -126,6 +126,21 @@ void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb, }EXPORT_SYMBOL_GPL (nf_reject_ip6_tcphdr_put); +static int nf_reject6_fill_skb_dst (struct sk_buff *skb_in) +{ +struct dst_entry *dst =NULL ; +struct flowi fl; + +memset (&fl,0 ,sizeof (struct flowi )); + fl.u.ip6.daddr =ipv6_hdr (skb_in)->saddr; +nf_ip6_route (dev_net (skb_in->dev), &dst, &fl, false); +if (!dst) +return -1 ; + +skb_dst_set (skb_in, dst); +return 0 ; +} +void nf_send_reset6 (struct net *net,struct sk_buff *oldskb,int hook) {struct net_device *br_indev__maybe_unused ;
@@ -154,6 +169,14 @@ void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook) fl6.daddr = oip6h->saddr; fl6.fl6_sport = otcph->dest; fl6.fl6_dport = otcph->source; + +if (hook ==NF_INET_PRE_ROUTING ) { +nf_ip6_route (net, &dst,flowi6_to_flowi (&fl6), false); +if (!dst) +return ; +skb_dst_set (oldskb, dst); + } + fl6.flowi6_oif =l3mdev_master_ifindex (skb_dst (oldskb)->dev); fl6.flowi6_mark =IP6_REPLY_MARK (net, oldskb->mark);security_skb_classify_flow (oldskb,flowi6_to_flowi (&fl6));
@@ -245,6 +268,9 @@ void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,if (hooknum ==NF_INET_LOCAL_OUT && skb_in->dev ==NULL ) skb_in->dev = net->loopback_dev; +if (hooknum ==NF_INET_PRE_ROUTING &&nf_reject6_fill_skb_dst (skb_in)) +return ; +icmpv6_send (skb_in,ICMPV6_DEST_UNREACH , code,0 ); }EXPORT_SYMBOL_GPL (nf_send_unreach6);
net/netfilter/nft_reject.c +2 -1
@@ -30,7 +30,8 @@ int nft_reject_validate(const struct nft_ctx *ctx,return nft_chain_validate_hooks (ctx->chain, (1 <<NF_INET_LOCAL_IN ) | (1 <<NF_INET_FORWARD ) | - (1 <<NF_INET_LOCAL_OUT )); + (1 <<NF_INET_LOCAL_OUT ) | + (1 <<NF_INET_PRE_ROUTING )); }EXPORT_SYMBOL_GPL (nft_reject_validate);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 2/12 netfilter: nft_set_pipapo: Drop useless assignment of scratch map index on insert
In reply to Pablo Neira AyusoFrom: Stefano Brivio <[email protected]>
In nft_pipapo_insert(), we need to reallocate scratch maps that will be used for matching by lookup functions, if they have never been allocated or if the bucket size changes as a result of the insertion.
As pipapo_realloc_scratch() provides a pair of fresh, zeroed out maps, there's no need to select a particular one after reallocation.
Other than being useless, the existing assignment was also troubled by the fact that the index was set only on the CPU performing the actual insertion, as spotted by Florian.
Simply drop the assignment.
--- net/netfilter/nft_set_pipapo.c | 2 -- 1 file changed, 2 deletions(-)
net/netfilter/nft_set_pipapo.c +0 -2
@@ -1249,8 +1249,6 @@ static int nft_pipapo_insert(const struct net *net, const struct nft_set *set,if (err)return err; -this_cpu_write (nft_pipapo_scratch_index, false); - m->bsize_max = bsize_max; }else {put_cpu_ptr (m->scratch);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 3/12 ipvs: register hooks only with services
In reply to Pablo Neira AyusoFrom: Julian Anastasov <[email protected]>
Keep the IPVS hooks registered in Netfilter only while there are configured virtual services. This saves CPU cycles while IPVS is loaded but not used.
--- include/net/ip_vs.h | 5 +++ net/netfilter/ipvs/ip_vs_core.c | 80 ++++++++++++++++++++++++++------- net/netfilter/ipvs/ip_vs_ctl.c | 23 +++++++++- 3 files changed, 89 insertions(+), 19 deletions(-)
include/net/ip_vs.h +5 -0
@@ -874,6 +874,7 @@ struct netns_ipvs {struct ip_vs_stats tot_stats;/* Statistics & est. */ int num_services;/* no of virtual services */ +int num_services6;/* IPv6 virtual services */ /* Trash for destinations */ struct list_head dest_trash;
@@ -960,6 +961,7 @@ struct netns_ipvs {* are not supported when synchronization is enabled. */ unsigned int mixed_address_family_dests; +unsigned int hooks_afmask;/* &1=AF_INET, &2=AF_INET6 */ };#define DEFAULT_SYNC_THRESHOLD 3
@@ -1670,6 +1672,9 @@ static inline void ip_vs_unregister_conntrack(struct ip_vs_service *svc)#endif } +int ip_vs_register_hooks (struct netns_ipvs *ipvs,unsigned int af); +void ip_vs_unregister_hooks (struct netns_ipvs *ipvs,unsigned int af); +static inline int ip_vs_dest_conn_overhead (struct ip_vs_dest *dest) {
net/netfilter/ipvs/ip_vs_core.c +63 -17
118 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_ctl.c +21 -2
@@ -1272,6 +1272,7 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,struct ip_vs_scheduler *sched =NULL ;struct ip_vs_pe *pe =NULL ;struct ip_vs_service *svc =NULL ; +int ret_hooks =-1 ;/* increase the module use count */ if (!ip_vs_use_count_inc ())
@@ -1313,6 +1314,14 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, }#endif +if ((u->af ==AF_INET && !ipvs->num_services) || + (u->af ==AF_INET6 && !ipvs->num_services6)) { + ret =ip_vs_register_hooks (ipvs, u->af); +if (ret <0 ) + goto out_err; + ret_hooks = ret; + } + svc =kzalloc (sizeof (struct ip_vs_service ),GFP_KERNEL );if (svc ==NULL ) {IP_VS_DBG (1 ,"%s(): no memory\n" , __func__);
@@ -1374,6 +1383,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,/* Count only IPv4 services for old get/setsockopt interface */ if (svc->af ==AF_INET ) ipvs->num_services++; +else if (svc->af ==AF_INET6 ) + ipvs->num_services6++;/* Hash the service into the service table */ ip_vs_svc_hash (svc);
@@ -1385,6 +1396,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, out_err: +if (ret_hooks >=0 ) +ip_vs_unregister_hooks (ipvs, u->af);if (svc !=NULL ) {ip_vs_unbind_scheduler (svc, sched);ip_vs_service_free (svc);
@@ -1500,9 +1513,15 @@ static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup)struct ip_vs_pe *old_pe;struct netns_ipvs *ipvs = svc->ipvs; -/* Count only IPv4 services for old get/setsockopt interface */ -if (svc->af ==AF_INET ) +if (svc->af ==AF_INET ) { ipvs->num_services--; +if (!ipvs->num_services) +ip_vs_unregister_hooks (ipvs, svc->af); + }else if (svc->af ==AF_INET6 ) { + ipvs->num_services6--; +if (!ipvs->num_services6) +ip_vs_unregister_hooks (ipvs, svc->af); + }ip_vs_stop_estimator (svc->ipvs, &svc->stats);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 4/12 ipvs: avoid expiring many connections from timer
In reply to Pablo Neira AyusoFrom: Julian Anastasov <[email protected]>
Add new functions ip_vs_conn_del() and ip_vs_conn_del_put() to release many IPVS connections in process context. They are suitable for connections found in table when we do not want to overload the timers.
Currently, the change is useful for the dropentry delayed work but it will be used also in following patch when flushing connections to failed destinations.
--- net/netfilter/ipvs/ip_vs_conn.c | 53 +++++++++++++++++++++++---------- net/netfilter/ipvs/ip_vs_ctl.c | 6 ++-- 2 files changed, 42 insertions(+), 17 deletions(-)
net/netfilter/ipvs/ip_vs_conn.c +38 -15
@@ -807,6 +807,31 @@ static void ip_vs_conn_rcu_free(struct rcu_head *head)kmem_cache_free (ip_vs_conn_cachep, cp); } +/* Try to delete connection while not holding reference */ +static void ip_vs_conn_del (struct ip_vs_conn *cp) +{ +if (del_timer (&cp->timer)) { +/* Drop cp->control chain too */ +if (cp->control) + cp->timeout =0 ; +ip_vs_conn_expire (&cp->timer); + } +} + +/* Try to delete connection while holding reference */ +static void ip_vs_conn_del_put (struct ip_vs_conn *cp) +{ +if (del_timer (&cp->timer)) { +/* Drop cp->control chain too */ +if (cp->control) + cp->timeout =0 ; +__ip_vs_conn_put (cp); +ip_vs_conn_expire (&cp->timer); + }else { +__ip_vs_conn_put (cp); + } +} +static void ip_vs_conn_expire (struct timer_list *t) {struct ip_vs_conn *cp =from_timer (cp, t, timer);
@@ -827,14 +852,17 @@ static void ip_vs_conn_expire(struct timer_list *t)/* does anybody control me? */ if (ct) { +bool has_ref = !cp->timeout &&__ip_vs_conn_get (ct); +ip_vs_control_del (cp);/* Drop CTL or non-assured TPL if not used anymore */ -if (!cp->timeout && !atomic_read (&ct->n_control) && +if (has_ref && !atomic_read (&ct->n_control) && (!(ct->flags &IP_VS_CONN_F_TEMPLATE ) || !(ct->state &IP_VS_CTPL_S_ASSURED ))) {IP_VS_DBG (4 ,"drop controlling connection\n" ); - ct->timeout =0 ; -ip_vs_conn_expire_now (ct); +ip_vs_conn_del_put (ct); + }else if (has_ref) { +__ip_vs_conn_put (ct); } }
@@ -1317,8 +1345,7 @@ void ip_vs_random_dropentry(struct netns_ipvs *ipvs) drop:IP_VS_DBG (4 ,"drop connection\n" ); - cp->timeout =0 ; -ip_vs_conn_expire_now (cp); +ip_vs_conn_del (cp); }cond_resched_rcu (); }
@@ -1341,19 +1368,15 @@ static void ip_vs_conn_flush(struct netns_ipvs *ipvs)hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[idx], c_list) {if (cp->ipvs != ipvs)continue ; -/* As timers are expired in LIFO order, restart -* the timer of controlling connection first, so -* that it is expired after us. -*/ +if (atomic_read (&cp->n_control)) +continue ; cp_c = cp->control; -/* cp->control is valid only with reference to cp */ -if (cp_c &&__ip_vs_conn_get (cp)) { +IP_VS_DBG (4 ,"del connection\n" ); +ip_vs_conn_del (cp); +if (cp_c && !atomic_read (&cp_c->n_control)) {IP_VS_DBG (4 ,"del controlling connection\n" ); -ip_vs_conn_expire_now (cp_c); -__ip_vs_conn_put (cp); +ip_vs_conn_del (cp_c); } -IP_VS_DBG (4 ,"del connection\n" ); -ip_vs_conn_expire_now (cp); }cond_resched_rcu (); }
net/netfilter/ipvs/ip_vs_ctl.c +4 -2
@@ -224,7 +224,8 @@ static void defense_work_handler(struct work_struct *work)update_defense_level (ipvs);if (atomic_read (&ipvs->dropentry))ip_vs_random_dropentry (ipvs); - schedule_delayed_work(&ipvs->defense_work, DEFENSE_TIMER_PERIOD); +queue_delayed_work (system_long_wq, &ipvs->defense_work, + DEFENSE_TIMER_PERIOD); }#endif
@@ -4082,7 +4083,8 @@ static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) ipvs->sysctl_tbl = tbl;/* Schedule defense work */ INIT_DELAYED_WORK (&ipvs->defense_work, defense_work_handler); -schedule_delayed_work (&ipvs->defense_work,DEFENSE_TIMER_PERIOD ); +queue_delayed_work (system_long_wq, &ipvs->defense_work, +DEFENSE_TIMER_PERIOD );return 0 ; }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 5/12 ipvs: allow connection reuse for unconfirmed conntrack
In reply to Pablo Neira AyusoFrom: Julian Anastasov <[email protected]>
YangYuxi is reporting that connection reuse is causing one-second delay when SYN hits existing connection in TIME_WAIT state. Such delay was added to give time to expire both the IPVS connection and the corresponding conntrack. This was considered a rare case at that time but it is causing problem for some environments such as Kubernetes.
As nf_conntrack_tcp_packet() can decide to release the conntrack in TIME_WAIT state and to replace it with a fresh NEW conntrack, we can use this to allow rescheduling just by tuning our check: if the conntrack is confirmed we can not schedule it to different real server and the one-second delay still applies but if new conntrack was created, we are free to select new real server without any delays.
YangYuxi lists some of the problem reports:
- One second connection delay in masquerading mode:
https://marc.info/?t=151683118100004&r=1&w=2
- IPVS low throughput #70747
https://github.com/kubernetes/kubernetes/issues/70747
- Apache Bench can fill up ipvs service proxy in seconds #544
https://github.com/cloudnativelabs/kube-router/issues/544
- Additional 1s latency in host -> service IP -> pod
https://github.com/kubernetes/kubernetes/issues/90854
--- include/net/ip_vs.h | 10 ++++------ net/netfilter/ipvs/ip_vs_core.c | 12 +++++++----- 2 files changed, 11 insertions(+), 11 deletions(-)
include/net/ip_vs.h +4 -6
@@ -1626,18 +1626,16 @@ static inline void ip_vs_conn_drop_conntrack(struct ip_vs_conn *cp) }#endif /* CONFIG_IP_VS_NFCT */ -/* Really using conntrack? */ -static inline bool ip_vs_conn_uses_conntrack (struct ip_vs_conn *cp, -struct sk_buff *skb) +/* Using old conntrack that can not be redirected to another real server? */ +static inline bool ip_vs_conn_uses_old_conntrack (struct ip_vs_conn *cp, +struct sk_buff *skb) {#ifdef CONFIG_IP_VS_NFCT enum ip_conntrack_info ctinfo;struct nf_conn *ct; -if (!(cp->flags &IP_VS_CONN_F_NFCT )) -return false; ct =nf_ct_get (skb, &ctinfo); -if (ct) +if (ct &&nf_ct_is_confirmed (ct))return true;#endif return false;
net/netfilter/ipvs/ip_vs_core.c +7 -5
@@ -2066,14 +2066,14 @@ ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int conn_reuse_mode =sysctl_conn_reuse_mode (ipvs);if (conn_reuse_mode && !iph.fragoffs &&is_new_conn (skb, &iph) && cp) { -bool uses_ct = false, resched = false; +bool old_ct = false, resched = false;if (unlikely (sysctl_expire_nodest_conn (ipvs)) && cp->dest &&unlikely (!atomic_read (&cp->dest->weight))) { resched = true; - uses_ct =ip_vs_conn_uses_conntrack (cp, skb); + old_ct =ip_vs_conn_uses_old_conntrack (cp, skb); }else if (is_new_conn_expected (cp, conn_reuse_mode)) { - uses_ct =ip_vs_conn_uses_conntrack (cp, skb); + old_ct =ip_vs_conn_uses_old_conntrack (cp, skb);if (!atomic_read (&cp->n_control)) { resched = true; }else {
@@ -2081,15 +2081,17 @@ ip_vs_in(struct netns_ipvs *ipvs, unsigned int hooknum, struct sk_buff *skb, int* that uses conntrack while it is still * referenced by controlled connection(s). */ - resched = !uses_ct; + resched = !old_ct; } }if (resched) { +if (!old_ct) + cp->flags &= ~IP_VS_CONN_F_NFCT ;if (!atomic_read (&cp->n_control))ip_vs_conn_expire_now (cp);__ip_vs_conn_put (cp); -if (uses_ct) +if (old_ct)return NF_DROP ; cp =NULL ; }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 6/12 netfilter: nf_tables: add NFTA_CHAIN_ID attribute
In reply to Pablo Neira AyusoThis netlink attribute allows you to refer to chains inside a transaction as an alternative to the name and the handle. The chain binding support requires this new chain ID approach.
--- include/net/netfilter/nf_tables.h | 3 +++ include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +3 -0
@@ -1433,6 +1433,7 @@ struct nft_trans_chain {char *name;struct nft_stats __percpu *stats;u8 policy; +u32 chain_id; };#define nft_trans_chain_update (trans) \
@@ -1443,6 +1444,8 @@ struct nft_trans_chain { (((struct nft_trans_chain *)trans->data)->stats)#define nft_trans_chain_policy (trans) \ (((struct nft_trans_chain *)trans->data)->policy) +#define nft_trans_chain_id (trans) \ + (((struct nft_trans_chain *)trans->data)->chain_id)struct nft_trans_table {bool update;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -196,6 +196,7 @@ enum nft_table_attributes {* @NFTA_CHAIN_TYPE: type name of the string (NLA_NUL_STRING) * @NFTA_CHAIN_COUNTERS: counter specification of the chain (NLA_NESTED: nft_counter_attributes) * @NFTA_CHAIN_FLAGS: chain flags +* @NFTA_CHAIN_ID: uniquely identifies a chain in a transaction (NLA_U32) */ enum nft_chain_attributes {NFTA_CHAIN_UNSPEC ,
@@ -209,6 +210,7 @@ enum nft_chain_attributes {NFTA_CHAIN_COUNTERS ,NFTA_CHAIN_PAD ,NFTA_CHAIN_FLAGS , +NFTA_CHAIN_ID , __NFTA_CHAIN_MAX };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)
net/netfilter/nf_tables_api.c +12 -3
@@ -280,9 +280,15 @@ static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type)if (trans ==NULL )return ERR_PTR (-ENOMEM ); -if (msg_type ==NFT_MSG_NEWCHAIN ) +if (msg_type ==NFT_MSG_NEWCHAIN ) {nft_activate_next (ctx->net, ctx->chain); +if (ctx->nla[NFTA_CHAIN_ID ]) { +nft_trans_chain_id (trans) = +ntohl (nla_get_be32 (ctx->nla[NFTA_CHAIN_ID ])); + } + } +list_add_tail (&trans->list, &ctx->net->nft.commit_list);return trans; }
@@ -1274,6 +1280,7 @@ static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { .len = NFT_MODULE_AUTOLOAD_LIMIT }, [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, + [NFTA_CHAIN_ID] = { .type = NLA_U32 }, };static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX +1 ] = {
@@ -2154,9 +2161,9 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,const struct nfgenmsg *nfmsg =nlmsg_data (nlh);u8 genmask =nft_genmask_next (net);int family = nfmsg->nfgen_family; +struct nft_chain *chain =NULL ;const struct nlattr *attr;struct nft_table *table; -struct nft_chain *chain;u8 policy =NF_ACCEPT ;struct nft_ctx ctx;u64 handle =0 ;
@@ -2181,7 +2188,7 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,return PTR_ERR (chain); } attr = nla[NFTA_CHAIN_HANDLE ]; - }else { + }else if (nla[NFTA_CHAIN_NAME ]) { chain =nft_chain_lookup (net, table, attr, genmask);if (IS_ERR (chain)) {if (PTR_ERR (chain) != -ENOENT ) {
@@ -2190,6 +2197,8 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk, } chain =NULL ; } + }else if (!nla[NFTA_CHAIN_ID ]) { +return -EINVAL ; }if (nla[NFTA_CHAIN_POLICY ]) {
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 7/12 netfilter: nf_tables: add NFTA_RULE_CHAIN_ID attribute
In reply to Pablo Neira AyusoThis new netlink attribute allows you to add rules to chains by the chain ID.
--- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nf_tables_api.c | 36 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -240,6 +240,7 @@ enum nft_rule_attributes {NFTA_RULE_PAD ,NFTA_RULE_ID ,NFTA_RULE_POSITION_ID , +NFTA_RULE_CHAIN_ID , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1)
net/netfilter/nf_tables_api.c +32 -4
@@ -2153,6 +2153,22 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy,return err; } +static struct nft_chain *nft_chain_lookup_byid (const struct net *net, +const struct nlattr *nla) +{ +u32 id =ntohl (nla_get_be32 (nla)); +struct nft_trans *trans; + +list_for_each_entry (trans, &net->nft.commit_list, list) { +struct nft_chain *chain = trans->ctx.chain; + +if (trans->msg_type ==NFT_MSG_NEWCHAIN && + id ==nft_trans_chain_id (trans)) +return chain; + } +return ERR_PTR (-ENOENT ); +} + static int nf_tables_newchain(struct net *net, struct sock *nlsk, struct sk_buff *skb, const struct nlmsghdr *nlh, const struct nlattr * const nla[],
@@ -2633,6 +2649,7 @@ static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { .len = NFT_USERDATA_MAXLEN }, [NFTA_RULE_ID] = { .type = NLA_U32 }, [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, + [NFTA_RULE_CHAIN_ID] = { .type = NLA_U32 }, }; static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
@@ -3039,10 +3056,21 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,return PTR_ERR (table); } - chain =nft_chain_lookup (net, table, nla[NFTA_RULE_CHAIN ], genmask); -if (IS_ERR (chain)) { -NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_CHAIN ]); -return PTR_ERR (chain); +if (nla[NFTA_RULE_CHAIN ]) { + chain =nft_chain_lookup (net, table, nla[NFTA_RULE_CHAIN ], + genmask); +if (IS_ERR (chain)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_CHAIN ]); +return PTR_ERR (chain); + } + }else if (nla[NFTA_RULE_CHAIN_ID ]) { + chain =nft_chain_lookup_byid (net, nla[NFTA_RULE_CHAIN_ID ]); +if (IS_ERR (chain)) { +NL_SET_BAD_ATTR (extack, nla[NFTA_RULE_CHAIN_ID ]); +return PTR_ERR (chain); + } + }else { +return -EINVAL ; }if (nla[NFTA_RULE_HANDLE ]) {
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 8/12 netfilter: nf_tables: add NFTA_VERDICT_CHAIN_ID attribute
In reply to Pablo Neira AyusoThis netlink attribute allows you to identify the chain to jump/goto by means of the chain ID.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -471,11 +471,13 @@ enum nft_data_attributes {* * @NFTA_VERDICT_CODE: nf_tables verdict (NLA_U32: enum nft_verdicts) * @NFTA_VERDICT_CHAIN: jump target chain name (NLA_STRING) +* @NFTA_VERDICT_CHAIN_ID: jump target chain ID (NLA_U32) */ enum nft_verdict_attributes {NFTA_VERDICT_UNSPEC ,NFTA_VERDICT_CODE ,NFTA_VERDICT_CHAIN , +NFTA_VERDICT_CHAIN_ID , __NFTA_VERDICT_MAX };#define NFTA_VERDICT_MAX (__NFTA_VERDICT_MAX - 1)
net/netfilter/nf_tables_api.c +13 -3
@@ -8242,6 +8242,7 @@ static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, [NFTA_VERDICT_CHAIN ] = { .type =NLA_STRING , .len = NFT_CHAIN_MAXNAMELEN - 1 }, + [NFTA_VERDICT_CHAIN_ID] = { .type = NLA_U32 }, }; static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
@@ -8278,10 +8279,19 @@ static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,break ;case NFT_JUMP :case NFT_GOTO : -if (!tb[NFTA_VERDICT_CHAIN ]) +if (tb[NFTA_VERDICT_CHAIN ]) { + chain =nft_chain_lookup (ctx->net, ctx->table, + tb[NFTA_VERDICT_CHAIN ], + genmask); + }else if (tb[NFTA_VERDICT_CHAIN_ID ]) { + chain =nft_chain_lookup_byid (ctx->net, + tb[NFTA_VERDICT_CHAIN_ID ]); +if (IS_ERR (chain)) +return PTR_ERR (chain); + }else {return -EINVAL ; - chain =nft_chain_lookup (ctx->net, ctx->table, - tb[NFTA_VERDICT_CHAIN ], genmask); + } +if (IS_ERR (chain))return PTR_ERR (chain);if (nft_is_base_chain (chain))
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 9/12 netfilter: nf_tables: expose enum nft_chain_flags through UAPI
In reply to Pablo Neira AyusoThis enum definition was never exposed through UAPI. Rename NFT_BASE_CHAIN to NFT_CHAIN_BASE for consistency.
--- include/net/netfilter/nf_tables.h | 7 +------ include/uapi/linux/netfilter/nf_tables.h | 5 +++++ net/netfilter/nf_tables_api.c | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-)
include/net/netfilter/nf_tables.h +1 -6
@@ -921,11 +921,6 @@ static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext, (expr) != (last); \ (expr) = nft_expr_next(expr)) -enum nft_chain_flags { -NFT_BASE_CHAIN =0x1 , -NFT_CHAIN_HW_OFFLOAD =0x2 , -}; -#define NFT_CHAIN_POLICY_UNSET U8_MAX/**
@@ -1036,7 +1031,7 @@ static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chaistatic inline bool nft_is_base_chain (const struct nft_chain *chain) { -return chain->flags &NFT_BASE_CHAIN ; +return chain->flags &NFT_CHAIN_BASE ; }int __nft_release_basechain (struct nft_ctx *ctx);
include/uapi/linux/netfilter/nf_tables.h +5 -0
@@ -184,6 +184,11 @@ enum nft_table_attributes { };#define NFTA_TABLE_MAX (__NFTA_TABLE_MAX - 1) +enum nft_chain_flags { +NFT_CHAIN_BASE = (1 <<0 ), +NFT_CHAIN_HW_OFFLOAD = (1 <<1 ), +}; +/** * enum nft_chain_attributes - nf_tables chain netlink attributes *
net/netfilter/nf_tables_api.c +2 -2
@@ -1903,7 +1903,7 @@ static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,nft_basechain_hook_init (&basechain->ops, family, hook, chain); } - chain->flags |=NFT_BASE_CHAIN | flags; + chain->flags |=NFT_CHAIN_BASE | flags; basechain->policy =NF_ACCEPT ;if (chain->flags &NFT_CHAIN_HW_OFFLOAD &&nft_chain_offload_priority (basechain) <0 )
@@ -2255,7 +2255,7 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,if (nlh->nlmsg_flags &NLM_F_REPLACE )return -EOPNOTSUPP ; - flags |= chain->flags &NFT_BASE_CHAIN ; + flags |= chain->flags &NFT_CHAIN_BASE ;return nf_tables_updchain (&ctx, genmask, policy, flags); }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 10/12 netfilter: nf_tables: add nft_chain_add()
In reply to Pablo Neira AyusoThis patch adds a helper function to add the chain to the hashtable and the chain list.
--- net/netfilter/nf_tables_api.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
net/netfilter/nf_tables_api.c +20 -8
@@ -1914,6 +1914,20 @@ static int nft_basechain_init(struct nft_base_chain *basechain, u8 family,return 0 ; } +static int nft_chain_add (struct nft_table *table,struct nft_chain *chain) +{ +int err; + + err =rhltable_insert_key (&table->chains_ht, chain->name, + &chain->rhlhead, nft_chain_ht_params); +if (err) +return err; + +list_add_tail_rcu (&chain->list, &table->chains); + +return 0 ; +} +static int nf_tables_addchain (struct nft_ctx *ctx,u8 family,u8 genmask,u8 policy,u32 flags) {
@@ -1991,16 +2005,9 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,if (err <0 ) goto err1; - err =rhltable_insert_key (&table->chains_ht, chain->name, - &chain->rhlhead, nft_chain_ht_params); -if (err) - goto err2; - trans =nft_trans_chain_add (ctx,NFT_MSG_NEWCHAIN );if (IS_ERR (trans)) { err =PTR_ERR (trans); -rhltable_remove (&table->chains_ht, &chain->rhlhead, - nft_chain_ht_params); goto err2; }
@@ -2008,8 +2015,13 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,if (nft_is_base_chain (chain))nft_trans_chain_policy (trans) = policy; + err =nft_chain_add (table, chain); +if (err <0 ) { +nft_trans_destroy (trans); + goto err2; + } + table->use++; -list_add_tail_rcu (&chain->list, &table->chains);return 0 ; err2:
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 11/12 netfilter: nf_tables: add NFT_CHAIN_BINDING
In reply to Pablo Neira AyusoThis new chain flag specifies that:
* the kernel dynamically allocates the chain name, if no chain name is specified.
* If the immediate expression that refers to this chain is removed, then this bound chain (and its content) is destroyed.
--- include/net/netfilter/nf_tables.h | 13 +++- include/uapi/linux/netfilter/nf_tables.h | 1 + net/netfilter/nf_tables_api.c | 86 ++++++++++++++++++++---- net/netfilter/nft_immediate.c | 51 ++++++++++++++ 4 files changed, 138 insertions(+), 13 deletions(-)
include/net/netfilter/nf_tables.h +12 -1
@@ -899,6 +899,8 @@ static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)return (void *)&rule->data[rule->dlen]; } +void nf_tables_rule_release (const struct nft_ctx *ctx,struct nft_rule *rule); +static inline void nft_set_elem_update_expr (const struct nft_set_ext *ext,struct nft_regs *regs,const struct nft_pktinfo *pkt)
@@ -944,7 +946,8 @@ struct nft_chain {struct nft_table *table;u64 handle;u32 use; - u8 flags:6, + u8 flags:5, + bound:1, genmask:2;char *name;
@@ -989,6 +992,14 @@ int nft_chain_validate_dependency(const struct nft_chain *chain,int nft_chain_validate_hooks (const struct nft_chain *chain,unsigned int hook_flags); +static inline bool nft_chain_is_bound (struct nft_chain *chain) +{ +return (chain->flags &NFT_CHAIN_BINDING ) && chain->bound; +} + +void nft_chain_del (struct nft_chain *chain); +void nf_tables_chain_destroy (struct nft_ctx *ctx); +struct nft_stats {u64 bytes;u64 pkts;
include/uapi/linux/netfilter/nf_tables.h +1 -0
@@ -187,6 +187,7 @@ enum nft_table_attributes {enum nft_chain_flags {NFT_CHAIN_BASE = (1 <<0 ),NFT_CHAIN_HW_OFFLOAD = (1 <<1 ), +NFT_CHAIN_BINDING = (1 <<2 ), };/**
net/netfilter/nf_tables_api.c +74 -12
194 lines not on this page · raw message
net/netfilter/nft_immediate.c +51 -0
69 lines not on this page · raw message
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 12/12 netfilter: nf_tables: reject unsupported chain flags
In reply to Pablo Neira AyusoBail out if userspace sends unsupported chain flags.
--- include/uapi/linux/netfilter/nf_tables.h | 3 +++ net/netfilter/nf_tables_api.c | 3 +++ 2 files changed, 6 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +3 -0
@@ -189,6 +189,9 @@ enum nft_chain_flags {NFT_CHAIN_HW_OFFLOAD = (1 <<1 ), NFT_CHAIN_BINDING = (1 << 2), }; +#define NFT_CHAIN_FLAGS (NFT_CHAIN_BASE | \ + NFT_CHAIN_HW_OFFLOAD | \ + NFT_CHAIN_BINDING)/** * enum nft_chain_attributes - nf_tables chain netlink attributes
net/netfilter/nf_tables_api.c +3 -0
@@ -2285,6 +2285,9 @@ static int nf_tables_newchain(struct net *net, struct sock *nlsk,else if (chain) flags = chain->flags; +if (flags & ~NFT_CHAIN_FLAGS ) +return -EOPNOTSUPP ; +nft_ctx_init (&ctx, net, skb, nlh, family, table, chain, nla);if (chain !=NULL ) {
Signature · 1 line
2.20.1
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Wed, 8 Jul 2020 19:45:57 +0200
Quoted 17 lines
The following patchset contains Netfilter updates for net-next:
1) Support for rejecting packets from the prerouting chain, from Laura Garcia Liebana.
2) Remove useless assignment in pipapo, from Stefano Brivio.
3) On demand hook registration in IPVS, from Julian Anastasov.
4) Expire IPVS connection from process context to not overload timers, also from Julian.
5) Fallback to conntrack TCP tracker to handle connection reuse in IPVS, from Julian Anastasov.
6) Several patches to support for chain bindings.
Quoted 7 lines
7) Expose enum
nft_chain_flagsthrough UAPI.8) Reject unsupported chain flags from the netlink control plane.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Pulled, thanks Pablo.