netfilter: ebtables: do not hook tables by default
Subsystems (3)
Florian Westphal
maintainerPATCH 1/1 netfilter: ebtables: do not hook tables by default
If any of these modules is loaded, hooks get registered in all netns:
Before: 'unshare -n nft list hooks' shows:
family bridge hook prerouting {
-2147483648 ebt_broute
-0000000300 ebt_nat_hook
}
family bridge hook input {
-0000000200 ebt_filter_hook
}
family bridge hook forward {
-0000000200 ebt_filter_hook
}
family bridge hook output {
+0000000100 ebt_nat_hook
+0000000200 ebt_filter_hook
}
family bridge hook postrouting {
+0000000300 ebt_nat_hook
}This adds 'template 'tables' for ebtables.
Each ebtable_foo registers the table as a template, with an init function that gets called once the first get/setsockopt call is made.
ebtables core then searches the (per netns) list of tables. If no table is found, it searches the list of templates instead. If a template entry exists, the init function is called which will enable the table and register the hooks (so packets are diverted to the table).
If no entry is found in the template list, request_module is called.
After this, hook registration is delayed until the 'ebtables' (set/getsockopt) request is made for a given table and will only happen in the specific namespace.
--- include/linux/netfilter_bridge/ebtables.h | 2 + net/bridge/netfilter/ebtable_broute.c | 17 +++- net/bridge/netfilter/ebtable_filter.c | 17 +++- net/bridge/netfilter/ebtable_nat.c | 17 +++- net/bridge/netfilter/ebtables.c | 109 +++++++++++++++++++--- 5 files changed, 139 insertions(+), 23 deletions(-)
include/linux/netfilter_bridge/ebtables.h +2 -0
@@ -127,4 +127,6 @@ static inline bool ebt_invalid_target(int target) return (target < -NUM_STANDARD_TARGETS || target >= 0); } +int ebt_register_template (const struct ebt_table *t,int (*table_init)(struct net *net)); +void ebt_unregister_template (const struct ebt_table *t);#endif
net/bridge/netfilter/ebtable_broute.c +14 -3
@@ -98,7 +98,7 @@ static const struct nf_hook_ops ebt_ops_broute = { .priority = NF_BR_PRI_FIRST, }; -static int __net_init broute_net_init (struct net *net) +static int broute_table_init (struct net *net) {return ebt_register_table (net, &broute_table, &ebt_ops_broute); }
@@ -114,19 +114,30 @@ static void __net_exit broute_net_exit(struct net *net) }static struct pernet_operations broute_net_ops = { - .init = broute_net_init, .exit = broute_net_exit, .pre_exit = broute_net_pre_exit, };static int __init ebtable_broute_init (void ) { -return register_pernet_subsys (&broute_net_ops); +int ret =ebt_register_template (&broute_table, broute_table_init); + +if (ret) +return ret; + + ret =register_pernet_subsys (&broute_net_ops); +if (ret) { +ebt_unregister_template (&broute_table); +return ret; + } + +return 0 ; } static void __exit ebtable_broute_fini(void) {unregister_pernet_subsys (&broute_net_ops); +ebt_unregister_template (&broute_table); }module_init (ebtable_broute_init);
net/bridge/netfilter/ebtable_filter.c +14 -3
@@ -86,7 +86,7 @@ static const struct nf_hook_ops ebt_ops_filter[] = { }, }; -static int __net_init frame_filter_net_init (struct net *net) +static int frame_filter_table_init (struct net *net) {return ebt_register_table (net, &frame_filter, ebt_ops_filter); }
@@ -102,19 +102,30 @@ static void __net_exit frame_filter_net_exit(struct net *net) }static struct pernet_operations frame_filter_net_ops = { - .init = frame_filter_net_init, .exit = frame_filter_net_exit, .pre_exit = frame_filter_net_pre_exit, };static int __init ebtable_filter_init (void ) { -return register_pernet_subsys (&frame_filter_net_ops); +int ret =ebt_register_template (&frame_filter, frame_filter_table_init); + +if (ret) +return ret; + + ret =register_pernet_subsys (&frame_filter_net_ops); +if (ret) { +ebt_unregister_template (&frame_filter); +return ret; + } + +return 0 ; } static void __exit ebtable_filter_fini(void) {unregister_pernet_subsys (&frame_filter_net_ops); +ebt_unregister_template (&frame_filter); }module_init (ebtable_filter_init);
net/bridge/netfilter/ebtable_nat.c +14 -3
@@ -85,7 +85,7 @@ static const struct nf_hook_ops ebt_ops_nat[] = { }, }; -static int __net_init frame_nat_net_init (struct net *net) +static int frame_nat_table_init (struct net *net) {return ebt_register_table (net, &frame_nat, ebt_ops_nat); }
@@ -101,19 +101,30 @@ static void __net_exit frame_nat_net_exit(struct net *net) }static struct pernet_operations frame_nat_net_ops = { - .init = frame_nat_net_init, .exit = frame_nat_net_exit, .pre_exit = frame_nat_net_pre_exit, };static int __init ebtable_nat_init (void ) { -return register_pernet_subsys (&frame_nat_net_ops); +int ret =ebt_register_template (&frame_nat, frame_nat_table_init); + +if (ret) +return ret; + + ret =register_pernet_subsys (&frame_nat_net_ops); +if (ret) { +ebt_unregister_template (&frame_nat); +return ret; + } + +return ret; } static void __exit ebtable_nat_fini(void) {unregister_pernet_subsys (&frame_nat_net_ops); +ebt_unregister_template (&frame_nat); }module_init (ebtable_nat_init);
net/bridge/netfilter/ebtables.c +95 -14
148 lines not on this page · raw message
Signature · 1 line
2.31.1
Quoted 32 lines · Florian Westphal
If any of these modules is loaded, hooks get registered in all netns:
Before: 'unshare -n nft list hooks' shows:
family bridge hook prerouting { -2147483648 ebt_broute -0000000300 ebt_nat_hook } family bridge hook input { -0000000200 ebt_filter_hook } family bridge hook forward { -0000000200 ebt_filter_hook } family bridge hook output { +0000000100 ebt_nat_hook +0000000200 ebt_filter_hook } family bridge hook postrouting { +0000000300 ebt_nat_hook }This adds 'template 'tables' for ebtables.
Each
ebtable_fooregisters the table as a template, with an init function that gets called once the first get/setsockopt call is made.ebtables core then searches the (per netns) list of tables. If no table is found, it searches the list of templates instead. If a template entry exists, the init function is called which will enable the table and register the hooks (so packets are diverted to the table).
Quoted 5 lines
If no entry is found in the template list,
request_moduleis called.After this, hook registration is delayed until the 'ebtables' (set/getsockopt) request is made for a given table and will only happen in the specific namespace.
Applied, thanks.