Skip to content

Commit 9abddac

Browse files
danobiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
netfilter: defrag: Add glue hooks for enabling/disabling defrag
We want to be able to enable/disable IP packet defrag from core bpf/netfilter code. In other words, execute code from core that could possibly be built as a module. To help avoid symbol resolution errors, use glue hooks that the modules will register callbacks with during module init. Signed-off-by: Daniel Xu <[email protected]> Reviewed-by: Florian Westphal <[email protected]> Link: https://lore.kernel.org/r/f6a8824052441b72afe5285acedbd634bd3384c1.1689970773.git.dxu@dxuuu.xyz Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent ee932bf commit 9abddac

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

include/linux/netfilter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/wait.h>
1212
#include <linux/list.h>
1313
#include <linux/static_key.h>
14+
#include <linux/module.h>
1415
#include <linux/netfilter_defs.h>
1516
#include <linux/netdevice.h>
1617
#include <linux/sockptr.h>
@@ -481,6 +482,15 @@ struct nfnl_ct_hook {
481482
};
482483
extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook;
483484

485+
struct nf_defrag_hook {
486+
struct module *owner;
487+
int (*enable)(struct net *net);
488+
void (*disable)(struct net *net);
489+
};
490+
491+
extern const struct nf_defrag_hook __rcu *nf_defrag_v4_hook;
492+
extern const struct nf_defrag_hook __rcu *nf_defrag_v6_hook;
493+
484494
/*
485495
* nf_skb_duplicated - TEE target has sent a packet
486496
*

net/ipv4/netfilter/nf_defrag_ipv4.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/ip.h>
88
#include <linux/netfilter.h>
99
#include <linux/module.h>
10+
#include <linux/rcupdate.h>
1011
#include <linux/skbuff.h>
1112
#include <net/netns/generic.h>
1213
#include <net/route.h>
@@ -113,17 +114,31 @@ static void __net_exit defrag4_net_exit(struct net *net)
113114
}
114115
}
115116

117+
static const struct nf_defrag_hook defrag_hook = {
118+
.owner = THIS_MODULE,
119+
.enable = nf_defrag_ipv4_enable,
120+
.disable = nf_defrag_ipv4_disable,
121+
};
122+
116123
static struct pernet_operations defrag4_net_ops = {
117124
.exit = defrag4_net_exit,
118125
};
119126

120127
static int __init nf_defrag_init(void)
121128
{
122-
return register_pernet_subsys(&defrag4_net_ops);
129+
int err;
130+
131+
err = register_pernet_subsys(&defrag4_net_ops);
132+
if (err)
133+
return err;
134+
135+
rcu_assign_pointer(nf_defrag_v4_hook, &defrag_hook);
136+
return err;
123137
}
124138

125139
static void __exit nf_defrag_fini(void)
126140
{
141+
rcu_assign_pointer(nf_defrag_v4_hook, NULL);
127142
unregister_pernet_subsys(&defrag4_net_ops);
128143
}
129144

net/ipv6/netfilter/nf_defrag_ipv6_hooks.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/module.h>
1111
#include <linux/skbuff.h>
1212
#include <linux/icmp.h>
13+
#include <linux/rcupdate.h>
1314
#include <linux/sysctl.h>
1415
#include <net/ipv6_frag.h>
1516

@@ -96,6 +97,12 @@ static void __net_exit defrag6_net_exit(struct net *net)
9697
}
9798
}
9899

100+
static const struct nf_defrag_hook defrag_hook = {
101+
.owner = THIS_MODULE,
102+
.enable = nf_defrag_ipv6_enable,
103+
.disable = nf_defrag_ipv6_disable,
104+
};
105+
99106
static struct pernet_operations defrag6_net_ops = {
100107
.exit = defrag6_net_exit,
101108
};
@@ -114,6 +121,9 @@ static int __init nf_defrag_init(void)
114121
pr_err("nf_defrag_ipv6: can't register pernet ops\n");
115122
goto cleanup_frag6;
116123
}
124+
125+
rcu_assign_pointer(nf_defrag_v6_hook, &defrag_hook);
126+
117127
return ret;
118128

119129
cleanup_frag6:
@@ -124,6 +134,7 @@ static int __init nf_defrag_init(void)
124134

125135
static void __exit nf_defrag_fini(void)
126136
{
137+
rcu_assign_pointer(nf_defrag_v6_hook, NULL);
127138
unregister_pernet_subsys(&defrag6_net_ops);
128139
nf_ct_frag6_cleanup();
129140
}

net/netfilter/core.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,12 @@ EXPORT_SYMBOL_GPL(nfnl_ct_hook);
680680
const struct nf_ct_hook __rcu *nf_ct_hook __read_mostly;
681681
EXPORT_SYMBOL_GPL(nf_ct_hook);
682682

683+
const struct nf_defrag_hook __rcu *nf_defrag_v4_hook __read_mostly;
684+
EXPORT_SYMBOL_GPL(nf_defrag_v4_hook);
685+
686+
const struct nf_defrag_hook __rcu *nf_defrag_v6_hook __read_mostly;
687+
EXPORT_SYMBOL_GPL(nf_defrag_v6_hook);
688+
683689
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
684690
u8 nf_ctnetlink_has_listener;
685691
EXPORT_SYMBOL_GPL(nf_ctnetlink_has_listener);

0 commit comments

Comments
 (0)