netfilter: add NF_HOOK_OP_CONNTRACK and use it
Subsystems (3)
Pablo Neira Ayuso
maintainerPATCH v5 1/5 netfilter: add NF_HOOK_OP_CONNTRACK and use it
Add a new NF_HOOK_OP_CONNTRACK to identify conntrack hooks, this allows to identify these hooks from the kernel. This is not yet exposed through to userspace via nfnetlink_hook.
---
v5: handle new hook type as undefined in nfnetlink_hook.
include/linux/netfilter.h | 1 + net/bridge/netfilter/nf_conntrack_bridge.c | 3 +++ net/netfilter/nf_conntrack_proto.c | 8 ++++++++ net/netfilter/nfnetlink_hook.c | 1 + 4 files changed, 13 insertions(+)
include/linux/netfilter.h +1 -0
@@ -94,6 +94,7 @@ enum nf_hook_ops_type {NF_HOOK_OP_BPF ,NF_HOOK_OP_NFT_FT ,NF_HOOK_OP_NAT , + NF_HOOK_OP_CONNTRACK, }; struct nf_hook_ops {
net/bridge/netfilter/nf_conntrack_bridge.c +3 -0
@@ -418,18 +418,21 @@ static struct nf_hook_ops nf_ct_bridge_hook_ops[] __read_mostly = { .pf = NFPROTO_BRIDGE, .hooknum = NF_BR_PRE_ROUTING, .priority = NF_IP_PRI_CONNTRACK, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_ct_bridge_in, .pf = NFPROTO_BRIDGE, .hooknum = NF_BR_LOCAL_IN, .priority = NF_IP_PRI_CONNTRACK_CONFIRM, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_ct_bridge_post, .pf = NFPROTO_BRIDGE, .hooknum = NF_BR_POST_ROUTING, .priority = NF_IP_PRI_CONNTRACK_CONFIRM, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, };
net/netfilter/nf_conntrack_proto.c +8 -0
@@ -243,24 +243,28 @@ static const struct nf_hook_ops ipv4_conntrack_ops[] = { .pf = NFPROTO_IPV4, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP_PRI_CONNTRACK, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = ipv4_conntrack_local, .pf = NFPROTO_IPV4, .hooknum = NF_INET_LOCAL_OUT, .priority = NF_IP_PRI_CONNTRACK, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_confirm, .pf = NFPROTO_IPV4, .hooknum = NF_INET_POST_ROUTING, .priority = NF_IP_PRI_CONNTRACK_CONFIRM, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_confirm, .pf = NFPROTO_IPV4, .hooknum = NF_INET_LOCAL_IN, .priority = NF_IP_PRI_CONNTRACK_CONFIRM, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, };
@@ -400,24 +404,28 @@ static const struct nf_hook_ops ipv6_conntrack_ops[] = { .pf = NFPROTO_IPV6, .hooknum = NF_INET_PRE_ROUTING, .priority = NF_IP6_PRI_CONNTRACK, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = ipv6_conntrack_local, .pf = NFPROTO_IPV6, .hooknum = NF_INET_LOCAL_OUT, .priority = NF_IP6_PRI_CONNTRACK, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_confirm, .pf = NFPROTO_IPV6, .hooknum = NF_INET_POST_ROUTING, .priority = NF_IP6_PRI_LAST, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, { .hook = nf_confirm, .pf = NFPROTO_IPV6, .hooknum = NF_INET_LOCAL_IN, .priority = NF_IP6_PRI_LAST - 1, + .hook_ops_type = NF_HOOK_OP_CONNTRACK, }, }; #endif
net/netfilter/nfnetlink_hook.c +1 -0
@@ -257,6 +257,7 @@ static int nfnl_hook_dump_one(struct sk_buff *nlskb,case NF_HOOK_OP_NFT_FT : ret =nfnl_hook_put_nft_ft_info (nlskb, ctx, seq, ops->priv);break ; +case NF_HOOK_OP_CONNTRACK :case NF_HOOK_OP_UNDEFINED :break ;default :
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 2/5 netfilter: nfnetlink_queue: add helper function to retrieve hook entries
Add nf_queue_hook_entry() and nf_queue_hook_entries() helper functions to retrieve hook entries and use them.
This comes in preparation for a fix to restrict expectation creation to nfnetlink_cthelper.
No functional changes are intended.
---
v5: no changes
net/netfilter/nfnetlink_queue.c | 40 +++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-)
net/netfilter/nfnetlink_queue.c +31 -9
@@ -360,6 +360,27 @@ static int nf_reroute(struct sk_buff *skb, struct nf_queue_entry *entry)return ret; } +static const struct nf_hook_entries * +nf_queue_hook_entries (struct nf_queue_entry *entry) +{ +const struct net *net = entry->state.net; +u8 pf = entry->state.pf; + +return nf_hook_entries_head (net, pf, entry->state.hook); +} + +static const struct nf_hook_entry * +nf_queue_hook_entry (const struct nf_hook_entries *hooks, +struct nf_queue_entry *entry) +{ +unsigned int i = entry->hook_index; + +if (i >= hooks->num_hook_entries) +return NULL ; + +return &hooks->hooks[i]; +} +/* caller must hold rcu read-side lock */ static void nf_reinject (struct nf_queue_entry *entry,unsigned int verdict) {
@@ -367,10 +388,8 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)const struct nf_hook_entries *hooks;struct sk_buff *skb = entry->skb;bool old_in_nf_duplicate; -const struct net *net;unsigned int i;int err; -u8 pf; DEBUG_NET_WARN_ONCE(current->in_nf_duplicate && !entry->in_nf_duplicate,
@@ -379,20 +398,23 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)local_bh_disable (); old_in_nf_duplicate = current->in_nf_duplicate; - net = entry->state.net; - pf = entry->state.pf; - current->in_nf_duplicate = entry->in_nf_duplicate; - hooks =nf_hook_entries_head (net, pf, entry->state.hook); - i = entry->hook_index; -if (!hooks || i >= hooks->num_hook_entries) { + hooks =nf_queue_hook_entries (entry); +if (!hooks) {kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP );nf_queue_entry_free (entry); goto out; } - hook_entry = &hooks->hooks[i]; + hook_entry =nf_queue_hook_entry (hooks, entry); +if (!hook_entry) { +kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP ); +nf_queue_entry_free (entry); + goto out; + } + + i = entry->hook_index;/* Continue traversal iff userspace said ok... */ if (verdict ==NF_REPEAT )
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 3/5 netfilter: ctnetlink: restrict expectation creation glue to userspace helpers
In reply to Pablo Neira AyusoThe integration between ctnetlink and nfnetlink_queue that allows to create expectations is targeted at the userspace conntrack helpers.
---
v5: no changes
net/netfilter/nf_conntrack_netlink.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/netfilter/nf_conntrack_netlink.c +12 -5
@@ -2644,7 +2644,8 @@ static struct nf_conntrack_expect * ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct, const struct nf_conntrack_helper *assign_helper, struct nf_conntrack_tuple *tuple, - struct nf_conntrack_tuple *mask); + struct nf_conntrack_tuple *mask, + bool userspace);#ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT static size_t
@@ -2899,7 +2900,7 @@ ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct, } exp =ctnetlink_alloc_expect ((const struct nlattr *const *)cda, ct, - assign_helper, &tuple, &mask); + assign_helper, &tuple, &mask, true);if (IS_ERR (exp))return PTR_ERR (exp);
@@ -3534,14 +3535,14 @@ static struct nf_conntrack_expect * ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct, const struct nf_conntrack_helper *assign_helper, struct nf_conntrack_tuple *tuple, - struct nf_conntrack_tuple *mask) + struct nf_conntrack_tuple *mask, bool userspace) { struct net *net = read_pnet(&ct->ct_net);struct nf_conntrack_helper *helper;struct nf_conntrack_ecache *ecache;struct nf_conntrack_expect *exp;struct nf_conn_help *help; -u32 class =0 ; +u32 class =0 , flags;int err; help =nfct_help (ct);
@@ -3552,6 +3553,12 @@ ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,if (!helper)return ERR_PTR (-EOPNOTSUPP ); +if (userspace) { + flags =READ_ONCE (helper->flags); +if (!(flags &NF_CT_HELPER_F_USERSPACE )) +return ERR_PTR (-EOPNOTSUPP ); + } +if (cda[CTA_EXPECT_CLASS ]) { class =ntohl (nla_get_be32 (cda[CTA_EXPECT_CLASS ]));if (class > helper->expect_class_max)
@@ -3639,7 +3646,7 @@ ctnetlink_create_expect(struct net *net, ct =nf_ct_tuplehash_to_ctrack (h);rcu_read_lock (); - exp =ctnetlink_alloc_expect (cda, ct,NULL , &tuple, &mask); + exp =ctnetlink_alloc_expect (cda, ct,NULL , &tuple, &mask, false);if (IS_ERR (exp)) { err =PTR_ERR (exp); goto err_rcu;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 4/5 netfilter: nfnetlink_queue: restrict NFQA_EXP to userspace helpers
In reply to Pablo Neira AyusoAdding expectations to unconfirmed conntracks are problematic because of reallocations of the ct extension area can happen while expectations are placed in the ct extension expectation list.
NFQA_EXP was added with the purpose of supporting userspace conntrack helpers, which create expectations in confirmation hook.
On packet reinjection, hook updates could result in a stale hook_index that point to different hooks, but this is not an issue because, in such rare case, expectation will not be created, else it could be created on the confirmation hook, which is the only hook where userspace helpers are possible.
Use NF_HOOK_OP_CONNTRACK to check this is a conntrack hook, where the expectation creation for userspace helpers is allowed.
---
v4: remove bridge/input.
net/netfilter/nfnetlink_queue.c | 40 +++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-)
net/netfilter/nfnetlink_queue.c +36 -4
@@ -381,6 +381,19 @@ nf_queue_hook_entry(const struct nf_hook_entries *hooks,return &hooks->hooks[i]; } +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +static bool nfqnl_hook_exp_support (struct nf_hook_ops *ops) +{ +return (ops->hook_ops_type ==NF_HOOK_OP_CONNTRACK && + (((ops->pf ==NFPROTO_IPV4 || + ops->pf ==NFPROTO_IPV6 ) && + (ops->hooknum ==NF_INET_LOCAL_IN || + ops->hooknum ==NF_INET_POST_ROUTING )) || + (ops->pf ==NFPROTO_BRIDGE && + ops->hooknum ==NF_BR_POST_ROUTING ))); +} +#endif +/* caller must hold rcu read-side lock */ static void nf_reinject (struct nf_queue_entry *entry,unsigned int verdict) {
@@ -1816,10 +1829,29 @@ static struct nf_conn *nfqnl_ct_parse(const struct nfnl_ct_hook *nfnl_ct,if (nfnl_ct->parse (nfqa[NFQA_CT ], ct) <0 )return NULL ; -if (nfqa[NFQA_EXP ]) - nfnl_ct->attach_expect (nfqa[NFQA_EXP ], ct, -NETLINK_CB (entry->skb).portid, -nlmsg_report (nlh)); +if (nfqa[NFQA_EXP ]) { +const struct nf_hook_entries *hooks; +struct nf_hook_ops **ops; +unsigned int i; + + hooks =nf_queue_hook_entries (entry); +if (!hooks) +return NULL ; + + ops =nf_hook_entries_get_hook_ops (hooks); +if (!ops) +return NULL ; + + i = entry->hook_index; +if (i >= hooks->num_hook_entries) +return NULL ; + +if (nfqnl_hook_exp_support (ops[i])) { + nfnl_ct->attach_expect (nfqa[NFQA_EXP ], ct, +NETLINK_CB (entry->skb).portid, +nlmsg_report (nlh)); + } + }return ct;#else return NULL ;
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v5 5/5 netfilter: nfnetlink_queue: do not skip ct confirmation with userspace ct helpers
In reply to Pablo Neira AyusoThere are two more issues with the existing userspace ct helpers:
- A custom userspace ct helper could return an unexpected verdict could result in skipping the conntrack confirmation step.
- nf_conntrack_update() should only be called if this is the confirmation hook, there is a new helper function to check this, use it.
While at this, nfqnl_reinject() is already called from a rcu read lock section, remove the redundant rcu_read_{un,lock}().
---
v5: new in this series.
LLM is barfing at something else in this core, not pointing to the root cause, let's see if this is enough to address it.
net/netfilter/nfnetlink_queue.c | 58 ++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 23 deletions(-)
net/netfilter/nfnetlink_queue.c +35 -23
@@ -395,10 +395,11 @@ static bool nfqnl_hook_exp_support(struct nf_hook_ops *ops)#endif /* caller must hold rcu read-side lock */ -static void nf_reinject (struct nf_queue_entry *entry,unsigned int verdict) +static void nf_reinject (struct nf_queue_entry *entry, +const struct nf_hook_entries *hooks, +unsigned int verdict) {const struct nf_hook_entry *hook_entry; -const struct nf_hook_entries *hooks;struct sk_buff *skb = entry->skb;bool old_in_nf_duplicate;unsigned int i;
@@ -413,13 +414,6 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) current->in_nf_duplicate = entry->in_nf_duplicate; - hooks =nf_queue_hook_entries (entry); -if (!hooks) { -kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP ); -nf_queue_entry_free (entry); - goto out; - } - hook_entry =nf_queue_hook_entry (hooks, entry);if (!hook_entry) {kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP );
@@ -528,25 +522,39 @@ static bool nf_bridge_ports_valid(const struct nf_queue_entry *entry)return true; } -static void nfqnl_reinject (struct nf_queue_entry *entry,unsigned int verdict) +static void nfqnl_reinject (struct nf_queue_entry *entry, +unsigned int verdict) { +unsigned int verdict_mask = verdict &NF_VERDICT_MASK ; +const struct nf_hook_entries *hooks;const struct nf_ct_hook *ct_hook; +struct nf_hook_ops **ops; +unsigned int i; + + hooks =nf_queue_hook_entries (entry); +if (!hooks) + goto drop;if (!nf_bridge_ports_valid (entry)) verdict =NF_DROP ; -if (verdict ==NF_ACCEPT || - verdict ==NF_REPEAT || - verdict ==NF_STOP ) { -unsigned int ct_verdict = verdict; +#if IS_ENABLED (CONFIG_NF_CONNTRACK ) +if (verdict_mask ==NF_ACCEPT || + verdict_mask ==NF_REPEAT || + verdict_mask ==NF_STOP ) { + ops =nf_hook_entries_get_hook_ops (hooks); +if (!ops) + goto drop; + + i = entry->hook_index; +if (i >= hooks->num_hook_entries) + goto drop; -rcu_read_lock (); ct_hook =rcu_dereference (nf_ct_hook); -if (ct_hook) - ct_verdict = ct_hook->update (entry->state.net, entry->skb); -rcu_read_unlock (); +if (ct_hook &&nfqnl_hook_exp_support (ops[i])) + verdict_mask = ct_hook->update (entry->state.net, entry->skb); -switch (ct_verdict &NF_VERDICT_MASK ) { +switch (verdict_mask &NF_VERDICT_MASK ) {case NF_ACCEPT :/* follow userspace verdict, could be REPEAT */ break ;
@@ -554,12 +562,12 @@ static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)nf_queue_entry_free (entry);return ;default : - verdict = ct_verdict &NF_VERDICT_MASK ; + verdict = verdict_mask &NF_VERDICT_MASK ; break; } } - -if (verdict !=NF_DROP && entry->nf_ct_is_unconfirmed) { +#endif +if (verdict_mask !=NF_DROP && entry->nf_ct_is_unconfirmed) {/* If first queued segment was already reinjected then * there is a good chance the ct entry is now confirmed. *
@@ -575,8 +583,12 @@ static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)if (nf_ct_drop_unconfirmed (entry,NULL )) verdict =NF_DROP ; } +nf_reinject (entry, hooks, verdict); -nf_reinject (entry, verdict); +return ; +drop: +kfree_skb_reason (entry->skb,SKB_DROP_REASON_NETFILTER_DROP ); +nf_queue_entry_free (entry); } static void
Signature · 1 line
2.47.3