Skip to content

Commit

Permalink
kernel: netfilter: Introduce NF_CONNTRACK_CHAIN_EVENTS (Backport from…
Browse files Browse the repository at this point in the history
… chromiumos 3.18 (114594778f3a8a22f1c98f70aef74b778329da3f) & lede patchs)
  • Loading branch information
Alexey authored and Yh793 committed Nov 28, 2020
1 parent c1d022c commit c7116ad
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 3 deletions.
26 changes: 24 additions & 2 deletions trunk/linux-3.4.x/include/net/netfilter/nf_conntrack_ecache.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,30 @@ struct nf_ct_event {
int report;
};

#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);
#else
struct nf_ct_event_notifier {
int (*fcn)(unsigned int events, struct nf_ct_event *item);
};

extern int nf_conntrack_register_notifier(struct net *net, struct nf_ct_event_notifier *nb);
extern void nf_conntrack_unregister_notifier(struct net *net, struct nf_ct_event_notifier *nb);
#endif

extern void nf_ct_deliver_cached_events(struct nf_conn *ct);

static inline void
nf_conntrack_event_cache(enum ip_conntrack_events event, struct nf_conn *ct)
{
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 (net->ct.nf_conntrack_event_cb == NULL)
return;
#endif

e = nf_ct_ecache_find(ct);
if (e == NULL)
Expand All @@ -95,19 +102,27 @@ nf_conntrack_eventmask_report(unsigned int eventmask,
u32 pid,
int report)
{
struct nf_conntrack_ecache *e;
#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
int ret = 0;
struct net *net = nf_ct_net(ct);
struct nf_ct_event_notifier *notify;
struct nf_conntrack_ecache *e;

rcu_read_lock();
notify = rcu_dereference(net->ct.nf_conntrack_event_cb);
if (notify == NULL)
goto out_unlock;
#else
struct net *net = nf_ct_net(ct);
#endif

e = nf_ct_ecache_find(ct);
if (e == NULL)
#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
goto out_unlock;
#else
return 0;
#endif

if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) {
struct nf_ct_event item = {
Expand All @@ -119,6 +134,7 @@ nf_conntrack_eventmask_report(unsigned int eventmask,
unsigned long missed = e->pid ? 0 : e->missed;

if (!((eventmask | missed) & e->ctmask))
#ifndef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
goto out_unlock;

ret = notify->fcn(eventmask | missed, &item);
Expand All @@ -141,6 +157,12 @@ nf_conntrack_eventmask_report(unsigned int eventmask,
out_unlock:
rcu_read_unlock();
return ret;
#else
return 0;
atomic_notifier_call_chain(&net->ct.nf_conntrack_chain, eventmask | missed, &item);
}
return 0;
#endif
}

static inline int
Expand Down
4 changes: 4 additions & 0 deletions trunk/linux-3.4.x/include/net/netns/conntrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ struct netns_ct {
struct hlist_nulls_head unconfirmed;
struct hlist_nulls_head dying;
struct ip_conntrack_stat __percpu *stat;
#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
struct atomic_notifier_head nf_conntrack_chain;
#else
struct nf_ct_event_notifier __rcu *nf_conntrack_event_cb;
#endif
struct nf_exp_event_notifier __rcu *nf_expect_event_cb;
int sysctl_events;
unsigned int sysctl_events_retry_timeout;
Expand Down
8 changes: 8 additions & 0 deletions trunk/linux-3.4.x/net/netfilter/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ config NF_CONNTRACK_EVENTS

If unsure, say `N'.

config NF_CONNTRACK_CHAIN_EVENTS
bool "Register multiple callbacks to ct events"
select NF_CONNTRACK_EVENTS
help
Support multiple registrations.

If unsure, say `N'.

config NF_CONNTRACK_TIMEOUT
bool 'Connection tracking timeout'
depends on NETFILTER_ADVANCED
Expand Down
3 changes: 3 additions & 0 deletions trunk/linux-3.4.x/net/netfilter/nf_conntrack_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,9 @@ static int nf_conntrack_init_net(struct net *net)
if (ret < 0)
goto err_timeout;

#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
ATOMIC_INIT_NOTIFIER_HEAD(&net->ct.nf_conntrack_chain);
#endif
return 0;

err_timeout:
Expand Down
59 changes: 59 additions & 0 deletions trunk/linux-3.4.x/net/netfilter/nf_conntrack_ecache.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include <linux/stddef.h>
#include <linux/err.h>
#include <linux/percpu.h>
#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
#include <linux/notifier.h>
#endif
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/slab.h>
Expand All @@ -29,6 +32,47 @@ static DEFINE_MUTEX(nf_ct_ecache_mutex);

/* 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);

e = nf_ct_ecache_find(ct);
if (e == NULL)
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.pid = 0;
item.report = 0;

atomic_notifier_call_chain(&net->ct.nf_conntrack_chain,
events | missed,
&item);

if (likely(!missed))
return;

spin_lock_bh(&ct->lock);
e->missed &= ~missed;
spin_unlock_bh(&ct->lock);
}
#else
void nf_ct_deliver_cached_events(struct nf_conn *ct)
{
struct net *net = nf_ct_net(ct);
Expand Down Expand Up @@ -79,8 +123,15 @@ void nf_ct_deliver_cached_events(struct nf_conn *ct)
out_unlock:
rcu_read_unlock();
}
#endif
EXPORT_SYMBOL_GPL(nf_ct_deliver_cached_events);

#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
int nf_conntrack_register_notifier(struct net *net,
struct nf_ct_event_notifier *new)
{
Expand All @@ -102,8 +153,15 @@ int nf_conntrack_register_notifier(struct net *net,
mutex_unlock(&nf_ct_ecache_mutex);
return ret;
}
#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,
struct nf_ct_event_notifier *new)
{
Expand All @@ -116,6 +174,7 @@ void nf_conntrack_unregister_notifier(struct net *net,
RCU_INIT_POINTER(net->ct.nf_conntrack_event_cb, NULL);
mutex_unlock(&nf_ct_ecache_mutex);
}
#endif
EXPORT_SYMBOL_GPL(nf_conntrack_unregister_notifier);

int nf_ct_expect_register_notifier(struct net *net,
Expand Down
21 changes: 20 additions & 1 deletion trunk/linux-3.4.x/net/netfilter/nf_conntrack_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include <linux/netlink.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>

#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
#include <linux/notifier.h>
#endif

#include <linux/slab.h>

#include <linux/netfilter.h>
Expand Down Expand Up @@ -559,13 +564,22 @@ ctnetlink_nlmsg_size(const struct nf_conn *ct)
;
}

#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, struct nf_ct_event *item)
#endif
{
struct net *net;
struct nlmsghdr *nlh;
struct nfgenmsg *nfmsg;
struct nlattr *nest_parms;
#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
struct nf_ct_event *item = ptr;
#endif
struct nf_conn *ct = item->ct;
struct sk_buff *skb;
unsigned int type;
Expand Down Expand Up @@ -2290,14 +2304,19 @@ ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
}

#ifdef CONFIG_NF_CONNTRACK_EVENTS
#ifdef CONFIG_NF_CONNTRACK_CHAIN_EVENTS
static struct notifier_block ctnl_notifier = {
.notifier_call = ctnetlink_conntrack_event,
#else /*CONFIG_NF_CONNTRACK_CHAIN_EVENTS*/
static struct nf_ct_event_notifier ctnl_notifier = {
.fcn = ctnetlink_conntrack_event,
#endif /*CONFIG_NF_CONNTRACK_CHAIN_EVENTS*/
};

static struct nf_exp_event_notifier ctnl_notifier_exp = {
.fcn = ctnetlink_expect_event,
};
#endif
#endif /*CONFIG_NF_CONNTRACK_EVENTS*/

static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
[IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
Expand Down

0 comments on commit c7116ad

Please sign in to comment.