netfilter: add NF_HOOK_OP_CONNTRACK and use it
Subsystems (3)
Pablo Neira Ayuso
maintainerPATCH v8 1/7 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.
---
v8: no changes
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 v8 2/7 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.
---
v8: no changes
net/netfilter/nfnetlink_queue.c | 44 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-)
net/netfilter/nfnetlink_queue.c +33 -11
@@ -360,17 +360,36 @@ 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) { +const struct nf_hook_entries *hook_entries;const struct nf_hook_entry *hook_entry; -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) { + hook_entries =nf_queue_hook_entries (entry); +if (!hook_entries) {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 (hook_entries, 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 )
@@ -406,7 +428,7 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)if (verdict ==NF_ACCEPT ) { next_hook: ++i; - verdict =nf_iterate (skb, &entry->state, hooks, &i); + verdict =nf_iterate (skb, &entry->state, hook_entries, &i); }switch (verdict &NF_VERDICT_MASK ) {
Signature · 1 line
2.47.3
Pablo Neira Ayuso
maintainerPATCH v8 3/7 netfilter: nfnetlink_queue: consolidate access to hook entries on reinjection
In reply to Pablo Neira AyusoFetch struct nf_hook_entries and nf_hook_ops only once then pass it on to reinject path. Otherwise re-fetching the pointer breaks atomicity when checking for the conntrack hook type since hook (un)registration might occur while following nfqueue reinjection path.
This is a preparation patch, not behaviour change is intended.
---
v8: fetch nf_hook_ops only once not to break atomicity.
net/netfilter/nfnetlink_queue.c | 69 ++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 13 deletions(-)
net/netfilter/nfnetlink_queue.c +56 -13
@@ -382,9 +382,11 @@ nf_queue_hook_entry(const struct nf_hook_entries *hooks, }/* 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 *hook_entries, +const struct nf_hook_ops *hook, +unsigned int verdict) { -const struct nf_hook_entries *hook_entries;const struct nf_hook_entry *hook_entry;struct sk_buff *skb = entry->skb;bool old_in_nf_duplicate;
@@ -400,13 +402,6 @@ static void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) current->in_nf_duplicate = entry->in_nf_duplicate; - hook_entries =nf_queue_hook_entries (entry); -if (!hook_entries) { -kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP ); -nf_queue_entry_free (entry); - goto out; - } - hook_entry =nf_queue_hook_entry (hook_entries, entry);if (!hook_entry) {kfree_skb_reason (skb,SKB_DROP_REASON_NETFILTER_DROP );
@@ -515,7 +510,10 @@ 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, +const struct nf_hook_entries *hook_entries, +const struct nf_hook_ops *hook, +unsigned int verdict) {const struct nf_ct_hook *ct_hook;
@@ -562,8 +560,32 @@ 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, hook_entries, hook, verdict); +} + +static void nfqnl_reinject (struct nf_queue_entry *entry, +unsigned int verdict) +{ +const struct nf_hook_entries *hook_entries =nf_queue_hook_entries (entry); +struct nf_hook_ops **hooks, *hook; +unsigned int i; + +if (!hook_entries) + goto drop; + + hooks =nf_hook_entries_get_hook_ops (hook_entries); + + i = entry->hook_index; + hook =READ_ONCE (hooks[i]); + +if (i >= hook_entries->num_hook_entries) + goto drop; -nf_reinject (entry, verdict); +__nfqnl_reinject (entry, hook_entries, hook, verdict); +return ; +drop: +kfree_skb_reason (entry->skb,SKB_DROP_REASON_NETFILTER_DROP ); +nf_queue_entry_free (entry); } static void
@@ -1866,14 +1888,16 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info, {struct nfnl_queue_net *q =nfnl_queue_pernet (info->net);u_int16_t queue_num =ntohs (info->nfmsg->res_id); +const struct nf_hook_entries *hook_entries;const struct nfnl_ct_hook *nfnl_ct;struct nfqnl_msg_verdict_hdr *vhdr; +struct nf_hook_ops **hooks, *hook;enum ip_conntrack_info ctinfo;struct nfqnl_instance *queue;struct nf_queue_entry *entry;struct nf_conn *ct =NULL ;unsigned int verdict; -int err; +int err, i; queue =verdict_instance_lookup (q, queue_num,NETLINK_CB (skb).portid);
@@ -1890,6 +1914,24 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info,if (entry ==NULL )return -ENOENT ; + hook_entries =nf_queue_hook_entries (entry); +if (!hook_entries) { +kfree_skb_reason (entry->skb,SKB_DROP_REASON_NETFILTER_DROP ); +nf_queue_entry_free (entry); +return -ENOENT ; + } + + hooks =nf_hook_entries_get_hook_ops (hook_entries); + + i = entry->hook_index; + hook =READ_ONCE (hooks[i]); + +if (i >= hook_entries->num_hook_entries) { +kfree_skb_reason (entry->skb,SKB_DROP_REASON_NETFILTER_DROP ); +nf_queue_entry_free (entry); +return -ENOENT ; + } +/* rcu lock already held from nfnl->call_rcu. */ nfnl_ct =rcu_dereference (nfnl_ct_hook);
@@ -1924,7 +1966,8 @@ static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info,if (nfqa[NFQA_PRIORITY ]) entry->skb->priority =ntohl (nla_get_be32 (nfqa[NFQA_PRIORITY ])); -nfqnl_reinject (entry, verdict); +__nfqnl_reinject (entry, hook_entries, hook, verdict); +return 0 ; }
Signature · 1 line
2.47.3