From 0d05596ba18a44aca87082b75989bb4114f896d8 Mon Sep 17 00:00:00 2001 From: Murat Sezgin Date: Tue, 18 Apr 2017 15:48:01 -0700 Subject: [PATCH 353/500] net: conntrack events, support multiple registrant Change-Id: Iebfb254590fb594f5baf232f849d1b7ae45ef757 Signed-off-by: Zhi Chen Signed-off-by: Murat Sezgin --- include/net/netfilter/nf_conntrack_ecache.h | 45 +++++++++++++- include/net/netns/conntrack.h | 3 + net/netfilter/Kconfig | 8 +++ net/netfilter/nf_conntrack_core.c | 3 + net/netfilter/nf_conntrack_ecache.c | 67 +++++++++++++++++++++ net/netfilter/nf_conntrack_netlink.c | 25 +++++++- 6 files changed, 148 insertions(+), 3 deletions(-) --- a/include/net/netfilter/nf_conntrack_ecache.h +++ b/include/net/netfilter/nf_conntrack_ecache.h @@ -65,13 +65,52 @@ struct nf_ct_event_notifier { int (*exp_event)(unsigned int events, const struct nf_exp_event *item); }; +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +extern int nf_conntrack_register_notifier(struct net *net, + struct notifier_block *nb); +extern int nf_conntrack_unregister_notifier(struct net *net, + struct notifier_block *nb); +static inline int +nf_conntrack_eventmask_report(unsigned int eventmask, + struct nf_conn *ct, + u32 portid, + int report) +{ + struct nf_conntrack_ecache *e; + struct net *net = nf_ct_net(ct); + + e = nf_ct_ecache_find(ct); + if (e == NULL) + return 0; + + if (nf_ct_is_confirmed(ct)) { + struct nf_ct_event item = { + .ct = ct, + .portid = e->portid ? e->portid : portid, + .report = report + }; + /* This is a resent of a destroy event? If so, skip missed */ + unsigned long missed = e->portid ? 0 : e->missed; + + if (!((eventmask | missed) & e->ctmask)) + return 0; + + atomic_notifier_call_chain(&net->ct.nf_conntrack_chain, + eventmask | missed, &item); + } + + return 0; +} +#else void nf_conntrack_register_notifier(struct net *net, const struct nf_ct_event_notifier *nb); void nf_conntrack_unregister_notifier(struct net *net); -void nf_ct_deliver_cached_events(struct nf_conn *ct); int nf_conntrack_eventmask_report(unsigned int eventmask, struct nf_conn *ct, u32 portid, int report); +#endif + +void nf_ct_deliver_cached_events(struct nf_conn *ct); bool nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp); #else @@ -98,11 +137,13 @@ static inline void nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct) { #ifdef CONFIG_NF_CONNTRACK_EVENTS - struct net *net = nf_ct_net(ct); struct nf_conntrack_ecache *e; +#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS + struct net *net = nf_ct_net(ct); if (!rcu_access_pointer(net->ct.nf_conntrack_event_cb)) return; +#endif e = nf_ct_ecache_find(ct); if (e == NULL) --- a/include/net/netns/conntrack.h +++ b/include/net/netns/conntrack.h @@ -105,6 +105,9 @@ struct netns_ct { u8 sysctl_checksum; struct ip_conntrack_stat __percpu *stat; +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS + struct atomic_notifier_head nf_conntrack_chain; +#endif struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb; struct nf_ip_net nf_ct_proto; #if defined(CONFIG_NF_CONNTRACK_LABELS) --- a/net/netfilter/Kconfig +++ b/net/netfilter/Kconfig @@ -181,6 +181,14 @@ config NF_CONNTRACK_DSCPREMARK_EXT This option enables support for connection tracking extension for dscp remark. +config NF_CONNTRACK_CHAIN_EVENTS + bool "Register multiple callbacks to ct events" + depends on NF_CONNTRACK_EVENTS + help + Support multiple registrations. + + If unsure, say `N'. + config NF_CONNTRACK_TIMESTAMP bool 'Connection tracking timestamping' depends on NETFILTER_ADVANCED --- a/net/netfilter/nf_conntrack_core.c +++ b/net/netfilter/nf_conntrack_core.c @@ -2810,6 +2810,9 @@ int nf_conntrack_init_net(struct net *ne nf_conntrack_ecache_pernet_init(net); nf_conntrack_proto_pernet_init(net); +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS + ATOMIC_INIT_NOTIFIER_HEAD(&net->ct.nf_conntrack_chain); +#endif return 0; err_expect: --- a/net/netfilter/nf_conntrack_ecache.c +++ b/net/netfilter/nf_conntrack_ecache.c @@ -16,6 +16,9 @@ #include #include #include +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +#include +#endif #include #include #include @@ -124,6 +127,7 @@ static void ecache_work(struct work_stru schedule_delayed_work(&cnet->ecache.dwork, delay); } +#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS static int __nf_conntrack_eventmask_report(struct nf_conntrack_ecache *e, const u32 events, const u32 missed, @@ -198,9 +202,56 @@ int nf_conntrack_eventmask_report(unsign return ret; } EXPORT_SYMBOL_GPL(nf_conntrack_eventmask_report); +#endif /* deliver cached events and clear cache entry - must be called with locally * disabled softirqs */ +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +void nf_ct_deliver_cached_events(struct nf_conn *ct) +{ + unsigned long events, missed; + struct nf_conntrack_ecache *e; + struct nf_ct_event item; + struct net *net = nf_ct_net(ct); + int ret = 0; + + e = nf_ct_ecache_find(ct); + if (!e) + return; + + events = xchg(&e->cache, 0); + + if (!nf_ct_is_confirmed(ct) || nf_ct_is_dying(ct) || !events) + return; + + /* + * We make a copy of the missed event cache without taking + * the lock, thus we may send missed events twice. However, + * this does not harm and it happens very rarely. + */ + missed = e->missed; + + if (!((events | missed) & e->ctmask)) + return; + + item.ct = ct; + item.portid = 0; + item.report = 0; + + atomic_notifier_call_chain(&net->ct.nf_conntrack_chain, + events | missed, &item); + + if (likely(ret >= 0 && !missed)) + return; + + spin_lock_bh(&ct->lock); + if (ret < 0) + e->missed |= events; + else + e->missed &= ~missed; + spin_unlock_bh(&ct->lock); +} +#else void nf_ct_deliver_cached_events(struct nf_conn *ct) { struct nf_conntrack_ecache *e; @@ -226,6 +277,7 @@ void nf_ct_deliver_cached_events(struct */ __nf_conntrack_eventmask_report(e, events, e->missed, &item); } +#endif EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events); void nf_ct_expect_event_report(enum ip_conntrack_expect_events event, @@ -258,6 +310,12 @@ out_unlock: rcu_read_unlock(); } +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +int nf_conntrack_register_notifier(struct net *net, struct notifier_block *nb) +{ + return atomic_notifier_chain_register(&net->ct.nf_conntrack_chain, nb); +} +#else void nf_conntrack_register_notifier(struct net *net, const struct nf_ct_event_notifier *new) { @@ -269,8 +327,16 @@ void nf_conntrack_register_notifier(stru rcu_assign_pointer(net->ct.nf_conntrack_event_cb, new); mutex_unlock(&nf_ct_ecache_mutex); } +#endif EXPORT_SYMBOL_GPL(nf_conntrack_register_notifier); +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +int nf_conntrack_unregister_notifier(struct net *net, struct notifier_block *nb) +{ + return atomic_notifier_chain_unregister(&net->ct.nf_conntrack_chain, + nb); +} +#else void nf_conntrack_unregister_notifier(struct net *net) { mutex_lock(&nf_ct_ecache_mutex); @@ -278,6 +344,7 @@ void nf_conntrack_unregister_notifier(st mutex_unlock(&nf_ct_ecache_mutex); /* synchronize_rcu() is called after netns pre_exit */ } +#endif EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier); void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state) --- a/net/netfilter/nf_conntrack_netlink.c +++ b/net/netfilter/nf_conntrack_netlink.c @@ -28,6 +28,9 @@ #include #include #include +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +#include +#endif #include #include @@ -719,18 +722,26 @@ static size_t ctnetlink_nlmsg_size(const ; } +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +static int ctnetlink_conntrack_event(struct notifier_block *this, + unsigned long events, void *ptr) +#else static int ctnetlink_conntrack_event(unsigned int events, const struct nf_ct_event *item) +#endif { const struct nf_conntrack_zone *zone; struct net *net; struct nlmsghdr *nlh; struct nlattr *nest_parms; - struct nf_conn *ct = item->ct; struct sk_buff *skb; unsigned int type; unsigned int flags = 0, group; int err; +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS + struct nf_ct_event *item = (struct nf_ct_event *)ptr; +#endif + struct nf_conn *ct = item->ct; if (events & (1 << IPCT_DESTROY)) { type = IPCTNL_MSG_CT_DELETE; @@ -3089,6 +3100,7 @@ nla_put_failure: } #ifdef CONFIG_NF_CONNTRACK_EVENTS +#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS static int ctnetlink_expect_event(unsigned int events, const struct nf_exp_event *item) { @@ -3138,6 +3150,7 @@ errout: return 0; } #endif +#endif static int ctnetlink_exp_done(struct netlink_callback *cb) { if (cb->args[1]) @@ -3748,11 +3761,17 @@ static int ctnetlink_stat_exp_cpu(struct } #ifdef CONFIG_NF_CONNTRACK_EVENTS +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS +static struct notifier_block ctnl_notifier = { + .notifier_call = ctnetlink_conntrack_event, +}; +#else static struct nf_ct_event_notifier ctnl_notifier = { .ct_event = ctnetlink_conntrack_event, .exp_event = ctnetlink_expect_event, }; #endif +#endif static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { [IPCTNL_MSG_CT_NEW] = { @@ -3851,8 +3870,12 @@ static int __net_init ctnetlink_net_init static void ctnetlink_net_pre_exit(struct net *net) { #ifdef CONFIG_NF_CONNTRACK_EVENTS +#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS + nf_conntrack_unregister_notifier(net, &ctnl_notifier); +#else nf_conntrack_unregister_notifier(net); #endif +#endif } static struct pernet_operations ctnetlink_net_ops = {