History

Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next

Pablo Neira Ayuso says: ==================== Netfilter/IPVS updates for net-next The following patchset contains Netfilter updates for your net-next tree, they are: 1) Remove useless debug message when deleting IPVS service, from Yannick Brosseau. 2) Get rid of compilation warning when CONFIG_PROC_FS is unset in several spots of the IPVS code, from Arnd Bergmann. 3) Add prandom_u32 support to nft_meta, from Florian Westphal. 4) Remove unused variable in xt_osf, from Sudip Mukherjee. 5) Don't calculate IP checksum twice from netfilter ipv4 defrag hook since fixing af_packet defragmentation issues, from Joe Stringer. 6) On-demand hook registration for iptables from netns. Instead of registering the hooks for every available netns whenever we need one of the support tables, we register this on the specific netns that needs it, patchset from Florian Westphal. 7) Add missing port range selection to nf_tables masquerading support. BTW, just for the record, there is a typo in the description of 5f6c253ebe93b0 ("netfilter: bridge: register hooks only when bridge interface is added") that refers to the cluster match as deprecated, but it is actually the CLUSTERIP target (which registers hooks inconditionally) the one that is scheduled for removal. ====================

David S. Miller committed 10 years ago

Commit 4c38cd62 parents d24ad3f 8a6bf5d32 files · +626 −353Browse files

Unified · Split

32 files
  1. include/linux/netfilter.h +11 −18
  2. include/linux/netfilter/x_tables.h +4 −2
  3. include/linux/netfilter_arp/arp_tables.h +5 −4
  4. include/linux/netfilter_ipv4/ip_tables.h +5 −4
  5. include/linux/netfilter_ipv6/ip6_tables.h +5 −4
  6. include/net/netfilter/nft_masq.h +3 −1
  7. include/uapi/linux/netfilter/nf_tables.h +6 −0
  8. lib/random32.c +1 −0
  9. net/bridge/br_netfilter_hooks.c +65 −3
  10. net/ipv4/netfilter/arp_tables.c +41 −25
  11. net/ipv4/netfilter/arptable_filter.c +23 −17
  12. net/ipv4/netfilter/ip_tables.c +38 −25
  13. net/ipv4/netfilter/iptable_filter.c +29 −15
  14. net/ipv4/netfilter/iptable_mangle.c +28 −13
  15. net/ipv4/netfilter/iptable_nat.c +22 −19
  16. net/ipv4/netfilter/iptable_raw.c +26 −12
  17. net/ipv4/netfilter/iptable_security.c +28 −16
  18. net/ipv4/netfilter/nf_defrag_ipv4.c +1 −3
  19. net/ipv4/netfilter/nft_masq_ipv4.c +6 −1
  20. net/ipv6/netfilter/ip6_tables.c +40 −25
  21. net/ipv6/netfilter/ip6table_filter.c +29 −18
  22. net/ipv6/netfilter/ip6table_mangle.c +28 −18
  23. net/ipv6/netfilter/ip6table_nat.c +22 −19
  24. net/ipv6/netfilter/ip6table_raw.c +28 −18
  25. net/ipv6/netfilter/ip6table_security.c +27 −17
  26. net/ipv6/netfilter/nft_masq_ipv6.c +6 −1
  27. net/netfilter/ipvs/ip_vs_app.c +2 −6
  28. net/netfilter/ipvs/ip_vs_ctl.c +6 −11
  29. net/netfilter/nft_masq.c +40 −11
  30. net/netfilter/nft_meta.c +11 −0
  31. net/netfilter/x_tables.c +40 −25
  32. net/netfilter/xt_osf.c +0 −2
include/linux/netfilter.h+11−18
@@ -141,22 +141,6 @@ void nf_unregister_sockopt(struct nf_sockopt_ops *reg); · 140 unmodified lines
141141
142142 #ifdef HAVE_JUMP_LABEL
143143 extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
144−
145−static inline bool nf_hook_list_active(struct list_head *hook_list,
146− u_int8_t pf, unsigned int hook)
147−{
148− if (__builtin_constant_p(pf) &&
149− __builtin_constant_p(hook))
150− return static_key_false(&nf_hooks_needed[pf][hook]);
151−
152− return !list_empty(hook_list);
153−}
154−#else
155−static inline bool nf_hook_list_active(struct list_head *hook_list,
156− u_int8_t pf, unsigned int hook)
157−{
158− return !list_empty(hook_list);
159−}
160144 #endif
161145
162146 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state);
@@ -177,9 +161,18 @@ static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook, · 14 unmodified lines
177161 int (*okfn)(struct net *, struct sock *, struct sk_buff *),
178162 int thresh)
179163 {
180− struct list_head *hook_list = &net->nf.hooks[pf][hook];
164+ struct list_head *hook_list;
165+
166+#ifdef HAVE_JUMP_LABEL
167+ if (__builtin_constant_p(pf) &&
168+ __builtin_constant_p(hook) &&
169+ !static_key_false(&nf_hooks_needed[pf][hook]))
170+ return 1;
171+#endif
172+
173+ hook_list = &net->nf.hooks[pf][hook];
181174
182− if (nf_hook_list_active(hook_list, pf, hook)) {
175+ if (!list_empty(hook_list)) {
183176 struct nf_hook_state state;
184177
185178 nf_hook_state_init(&state, hook_list, hook, thresh,
include/linux/netfilter/x_tables.h+4−2
@@ -200,6 +200,9 @@ struct xt_table { · 199 unmodified lines
200200 u_int8_t af; /* address/protocol family */
201201 int priority; /* hook order */
202202
203+ /* called when table is needed in the given netns */
204+ int (*table_init)(struct net *net);
205+
203206 /* A unique name... */
204207 const char name[XT_TABLE_MAXNAMELEN];
205208 };
@@ -408,8 +411,7 @@ xt_get_per_cpu_counter(struct xt_counters *cnt, unsigned int cpu) · 202 unmodified lines
408411 return cnt;
409412 }
410413
411−struct nf_hook_ops *xt_hook_link(const struct xt_table *, nf_hookfn *);
412−void xt_hook_unlink(const struct xt_table *, struct nf_hook_ops *);
414+struct nf_hook_ops *xt_hook_ops_alloc(const struct xt_table *, nf_hookfn *);
413415
414416 #ifdef CONFIG_COMPAT
415417 #include <net/compat.h>
include/linux/netfilter_arp/arp_tables.h+5−4
@@ -48,10 +48,11 @@ struct arpt_error { · 47 unmodified lines
4848 }
4949
5050 extern void *arpt_alloc_initial_table(const struct xt_table *);
51−extern struct xt_table *arpt_register_table(struct net *net,
52− const struct xt_table *table,
53− const struct arpt_replace *repl);
54−extern void arpt_unregister_table(struct xt_table *table);
51+int arpt_register_table(struct net *net, const struct xt_table *table,
52+ const struct arpt_replace *repl,
53+ const struct nf_hook_ops *ops, struct xt_table **res);
54+void arpt_unregister_table(struct net *net, struct xt_table *table,
55+ const struct nf_hook_ops *ops);
5556 extern unsigned int arpt_do_table(struct sk_buff *skb,
5657 const struct nf_hook_state *state,
5758 struct xt_table *table);
include/linux/netfilter_ipv4/ip_tables.h+5−4
@@ -24,10 +24,11 @@ · 23 unmodified lines
2424
2525 extern void ipt_init(void) __init;
2626
27−extern struct xt_table *ipt_register_table(struct net *net,
28− const struct xt_table *table,
29− const struct ipt_replace *repl);
30−extern void ipt_unregister_table(struct net *net, struct xt_table *table);
27+int ipt_register_table(struct net *net, const struct xt_table *table,
28+ const struct ipt_replace *repl,
29+ const struct nf_hook_ops *ops, struct xt_table **res);
30+void ipt_unregister_table(struct net *net, struct xt_table *table,
31+ const struct nf_hook_ops *ops);
3132
3233 /* Standard entry. */
3334 struct ipt_standard {
include/linux/netfilter_ipv6/ip6_tables.h+5−4
@@ -25,10 +25,11 @@ · 24 unmodified lines
2525 extern void ip6t_init(void) __init;
2626
2727 extern void *ip6t_alloc_initial_table(const struct xt_table *);
28−extern struct xt_table *ip6t_register_table(struct net *net,
29− const struct xt_table *table,
30− const struct ip6t_replace *repl);
31−extern void ip6t_unregister_table(struct net *net, struct xt_table *table);
28+int ip6t_register_table(struct net *net, const struct xt_table *table,
29+ const struct ip6t_replace *repl,
30+ const struct nf_hook_ops *ops, struct xt_table **res);
31+void ip6t_unregister_table(struct net *net, struct xt_table *table,
32+ const struct nf_hook_ops *ops);
3233 extern unsigned int ip6t_do_table(struct sk_buff *skb,
3334 const struct nf_hook_state *state,
3435 struct xt_table *table);
include/net/netfilter/nft_masq.h+3−1
@@ -2,7 +2,9 @@ · 1 unmodified line
22 #define _NFT_MASQ_H_
33
44 struct nft_masq {
5− u32 flags;
5+ u32 flags;
6+ enum nft_registers sreg_proto_min:8;
7+ enum nft_registers sreg_proto_max:8;
68 };
79
810 extern const struct nla_policy nft_masq_policy[];
include/uapi/linux/netfilter/nf_tables.h+6−0
@@ -681,6 +681,7 @@ enum nft_exthdr_attributes { · 680 unmodified lines
681681 * @NFT_META_IIFGROUP: packet input interface group
682682 * @NFT_META_OIFGROUP: packet output interface group
683683 * @NFT_META_CGROUP: socket control group (skb->sk->sk_classid)
684+ * @NFT_META_PRANDOM: a 32bit pseudo-random number
684685 */
685686 enum nft_meta_keys {
686687 NFT_META_LEN,
@@ -707,6 +708,7 @@ enum nft_meta_keys { · 20 unmodified lines
707708 NFT_META_IIFGROUP,
708709 NFT_META_OIFGROUP,
709710 NFT_META_CGROUP,
711+ NFT_META_PRANDOM,
710712 };
711713
712714 /**
@@ -949,10 +951,14 @@ enum nft_nat_attributes { · 236 unmodified lines
949951 * enum nft_masq_attributes - nf_tables masquerade expression attributes
950952 *
951953 * @NFTA_MASQ_FLAGS: NAT flags (see NF_NAT_RANGE_* in linux/netfilter/nf_nat.h) (NLA_U32)
954+ * @NFTA_MASQ_REG_PROTO_MIN: source register of proto range start (NLA_U32: nft_registers)
955+ * @NFTA_MASQ_REG_PROTO_MAX: source register of proto range end (NLA_U32: nft_registers)
952956 */
953957 enum nft_masq_attributes {
954958 NFTA_MASQ_UNSPEC,
955959 NFTA_MASQ_FLAGS,
960+ NFTA_MASQ_REG_PROTO_MIN,
961+ NFTA_MASQ_REG_PROTO_MAX,
956962 __NFTA_MASQ_MAX
957963 };
958964 #define NFTA_MASQ_MAX (__NFTA_MASQ_MAX - 1)
lib/random32.c+1−0
@@ -255,6 +255,7 @@ void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state) · 254 unmodified lines
255255 prandom_warmup(state);
256256 }
257257 }
258+EXPORT_SYMBOL(prandom_seed_full_state);
258259
259260 /*
260261 * Generate better values after random number generator
net/bridge/br_netfilter_hooks.c+65−3
@@ -37,6 +37,7 @@ · 36 unmodified lines
3737 #include <net/addrconf.h>
3838 #include <net/route.h>
3939 #include <net/netfilter/br_netfilter.h>
40+#include <net/netns/generic.h>
4041
4142 #include <asm/uaccess.h>
4243 #include "br_private.h"
@@ -44,6 +45,12 @@ · 1 unmodified line
4445 #include <linux/sysctl.h>
4546 #endif
4647
48+static int brnf_net_id __read_mostly;
49+
50+struct brnf_net {
51+ bool enabled;
52+};
53+
4754 #ifdef CONFIG_SYSCTL
4855 static struct ctl_table_header *brnf_sysctl_header;
4956 static int brnf_call_iptables __read_mostly = 1;
@@ -938,6 +945,53 @@ static struct nf_hook_ops br_nf_ops[] __read_mostly = { · 888 unmodified lines
938945 },
939946 };
940947
948+static int brnf_device_event(struct notifier_block *unused, unsigned long event,
949+ void *ptr)
950+{
951+ struct net_device *dev = netdev_notifier_info_to_dev(ptr);
952+ struct brnf_net *brnet;
953+ struct net *net;
954+ int ret;
955+
956+ if (event != NETDEV_REGISTER || !(dev->priv_flags & IFF_EBRIDGE))
957+ return NOTIFY_DONE;
958+
959+ ASSERT_RTNL();
960+
961+ net = dev_net(dev);
962+ brnet = net_generic(net, brnf_net_id);
963+ if (brnet->enabled)
964+ return NOTIFY_OK;
965+
966+ ret = nf_register_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
967+ if (ret)
968+ return NOTIFY_BAD;
969+
970+ brnet->enabled = true;
971+ return NOTIFY_OK;
972+}
973+
974+static void __net_exit brnf_exit_net(struct net *net)
975+{
976+ struct brnf_net *brnet = net_generic(net, brnf_net_id);
977+
978+ if (!brnet->enabled)
979+ return;
980+
981+ nf_unregister_net_hooks(net, br_nf_ops, ARRAY_SIZE(br_nf_ops));
982+ brnet->enabled = false;
983+}
984+
985+static struct pernet_operations brnf_net_ops __read_mostly = {
986+ .exit = brnf_exit_net,
987+ .id = &brnf_net_id,
988+ .size = sizeof(struct brnf_net),
989+};
990+
991+static struct notifier_block brnf_notifier __read_mostly = {
992+ .notifier_call = brnf_device_event,
993+};
994+
941995 #ifdef CONFIG_SYSCTL
942996 static
943997 int brnf_sysctl_call_tables(struct ctl_table *ctl, int write,
@@ -1003,16 +1057,23 @@ static int __init br_netfilter_init(void) · 59 unmodified lines
10031057 {
10041058 int ret;
10051059
1006− ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1060+ ret = register_pernet_subsys(&brnf_net_ops);
10071061 if (ret < 0)
10081062 return ret;
10091063
1064+ ret = register_netdevice_notifier(&brnf_notifier);
1065+ if (ret < 0) {
1066+ unregister_pernet_subsys(&brnf_net_ops);
1067+ return ret;
1068+ }
1069+
10101070 #ifdef CONFIG_SYSCTL
10111071 brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table);
10121072 if (brnf_sysctl_header == NULL) {
10131073 printk(KERN_WARNING
10141074 "br_netfilter: can't register to sysctl.\n");
1015− nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1075+ unregister_netdevice_notifier(&brnf_notifier);
1076+ unregister_pernet_subsys(&brnf_net_ops);
10161077 return -ENOMEM;
10171078 }
10181079 #endif
@@ -1024,7 +1085,8 @@ static int __init br_netfilter_init(void) · 5 unmodified lines
10241085 static void __exit br_netfilter_fini(void)
10251086 {
10261087 RCU_INIT_POINTER(nf_br_ops, NULL);
1027− nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1088+ unregister_netdevice_notifier(&brnf_notifier);
1089+ unregister_pernet_subsys(&brnf_net_ops);
10281090 #ifdef CONFIG_SYSCTL
10291091 unregister_net_sysctl_table(brnf_sysctl_header);
10301092 #endif
net/ipv4/netfilter/arp_tables.c+41−25
@@ -1780,9 +1780,29 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len · 1779 unmodified lines
17801780 return ret;
17811781 }
17821782
1783−struct xt_table *arpt_register_table(struct net *net,
1784− const struct xt_table *table,
1785− const struct arpt_replace *repl)
1783+static void __arpt_unregister_table(struct xt_table *table)
1784+{
1785+ struct xt_table_info *private;
1786+ void *loc_cpu_entry;
1787+ struct module *table_owner = table->me;
1788+ struct arpt_entry *iter;
1789+
1790+ private = xt_unregister_table(table);
1791+
1792+ /* Decrease module usage counts and free resources */
1793+ loc_cpu_entry = private->entries;
1794+ xt_entry_foreach(iter, loc_cpu_entry, private->size)
1795+ cleanup_entry(iter);
1796+ if (private->number > private->initial_entries)
1797+ module_put(table_owner);
1798+ xt_free_table_info(private);
1799+}
1800+
1801+int arpt_register_table(struct net *net,
1802+ const struct xt_table *table,
1803+ const struct arpt_replace *repl,
1804+ const struct nf_hook_ops *ops,
1805+ struct xt_table **res)
17861806 {
17871807 int ret;
17881808 struct xt_table_info *newinfo;
@@ -1791,10 +1811,8 @@ struct xt_table *arpt_register_table(struct net *net, · 2 unmodified lines
17911811 struct xt_table *new_table;
17921812
17931813 newinfo = xt_alloc_table_info(repl->size);
1794− if (!newinfo) {
1795− ret = -ENOMEM;
1796− goto out;
1797− }
1814+ if (!newinfo)
1815+ return -ENOMEM;
17981816
17991817 loc_cpu_entry = newinfo->entries;
18001818 memcpy(loc_cpu_entry, repl->entries, repl->size);
@@ -1809,30 +1827,28 @@ struct xt_table *arpt_register_table(struct net *net, · 8 unmodified lines
18091827 ret = PTR_ERR(new_table);
18101828 goto out_free;
18111829 }
1812− return new_table;
1830+
1831+ /* set res now, will see skbs right after nf_register_net_hooks */
1832+ WRITE_ONCE(*res, new_table);
1833+
1834+ ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1835+ if (ret != 0) {
1836+ __arpt_unregister_table(new_table);
1837+ *res = NULL;
1838+ }
1839+
1840+ return ret;
18131841
18141842 out_free:
18151843 xt_free_table_info(newinfo);
1816−out:
1817− return ERR_PTR(ret);
1844+ return ret;
18181845 }
18191846
1820−void arpt_unregister_table(struct xt_table *table)
1847+void arpt_unregister_table(struct net *net, struct xt_table *table,
1848+ const struct nf_hook_ops *ops)
18211849 {
1822− struct xt_table_info *private;
1823− void *loc_cpu_entry;
1824− struct module *table_owner = table->me;
1825− struct arpt_entry *iter;
1826−
1827− private = xt_unregister_table(table);
1828−
1829− /* Decrease module usage counts and free resources */
1830− loc_cpu_entry = private->entries;
1831− xt_entry_foreach(iter, loc_cpu_entry, private->size)
1832− cleanup_entry(iter);
1833− if (private->number > private->initial_entries)
1834− module_put(table_owner);
1835− xt_free_table_info(private);
1850+ nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1851+ __arpt_unregister_table(table);
18361852 }
18371853
18381854 /* The built-in targets: standard (NULL) and error. */
net/ipv4/netfilter/arptable_filter.c+23−17
@@ -17,12 +17,15 @@ MODULE_DESCRIPTION("arptables filter table"); · 16 unmodified lines
1717 #define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
1818 (1 << NF_ARP_FORWARD))
1919
20+static int __net_init arptable_filter_table_init(struct net *net);
21+
2022 static const struct xt_table packet_filter = {
2123 .name = "filter",
2224 .valid_hooks = FILTER_VALID_HOOKS,
2325 .me = THIS_MODULE,
2426 .af = NFPROTO_ARP,
2527 .priority = NF_IP_PRI_FILTER,
28+ .table_init = arptable_filter_table_init,
2629 };
2730
2831 /* The work comes in here from netfilter.c */
@@ -35,26 +38,32 @@ arptable_filter_hook(void *priv, struct sk_buff *skb, · 6 unmodified lines
3538
3639 static struct nf_hook_ops *arpfilter_ops __read_mostly;
3740
38−static int __net_init arptable_filter_net_init(struct net *net)
41+static int __net_init arptable_filter_table_init(struct net *net)
3942 {
4043 struct arpt_replace *repl;
41−
44+ int err;
45+
46+ if (net->ipv4.arptable_filter)
47+ return 0;
48+
4249 repl = arpt_alloc_initial_table(&packet_filter);
4350 if (repl == NULL)
4451 return -ENOMEM;
45− net->ipv4.arptable_filter =
46− arpt_register_table(net, &packet_filter, repl);
52+ err = arpt_register_table(net, &packet_filter, repl, arpfilter_ops,
53+ &net->ipv4.arptable_filter);
4754 kfree(repl);
48− return PTR_ERR_OR_ZERO(net->ipv4.arptable_filter);
55+ return err;
4956 }
5057
5158 static void __net_exit arptable_filter_net_exit(struct net *net)
5259 {
53− arpt_unregister_table(net->ipv4.arptable_filter);
60+ if (!net->ipv4.arptable_filter)
61+ return;
62+ arpt_unregister_table(net, net->ipv4.arptable_filter, arpfilter_ops);
63+ net->ipv4.arptable_filter = NULL;
5464 }
5565
5666 static struct pernet_operations arptable_filter_net_ops = {
57− .init = arptable_filter_net_init,
5867 .exit = arptable_filter_net_exit,
5968 };
6069
@@ -62,26 +71,23 @@ static int __init arptable_filter_init(void) · 1 unmodified line
6271 {
6372 int ret;
6473
74+ arpfilter_ops = xt_hook_ops_alloc(&packet_filter, arptable_filter_hook);
75+ if (IS_ERR(arpfilter_ops))
76+ return PTR_ERR(arpfilter_ops);
77+
6578 ret = register_pernet_subsys(&arptable_filter_net_ops);
66− if (ret < 0)
79+ if (ret < 0) {
80+ kfree(arpfilter_ops);
6781 return ret;
68−
69− arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook);
70− if (IS_ERR(arpfilter_ops)) {
71− ret = PTR_ERR(arpfilter_ops);
72− goto cleanup_table;
7382 }
74− return ret;
7583
76−cleanup_table:
77− unregister_pernet_subsys(&arptable_filter_net_ops);
7884 return ret;
7985 }
8086
8187 static void __exit arptable_filter_fini(void)
8288 {
83− xt_hook_unlink(&packet_filter, arpfilter_ops);
8489 unregister_pernet_subsys(&arptable_filter_net_ops);
90+ kfree(arpfilter_ops);
8591 }
8692
8793 module_init(arptable_filter_init);
net/ipv4/netfilter/ip_tables.c+38−25
@@ -2062,9 +2062,27 @@ do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) · 2061 unmodified lines
20622062 return ret;
20632063 }
20642064
2065−struct xt_table *ipt_register_table(struct net *net,
2066− const struct xt_table *table,
2067− const struct ipt_replace *repl)
2065+static void __ipt_unregister_table(struct net *net, struct xt_table *table)
2066+{
2067+ struct xt_table_info *private;
2068+ void *loc_cpu_entry;
2069+ struct module *table_owner = table->me;
2070+ struct ipt_entry *iter;
2071+
2072+ private = xt_unregister_table(table);
2073+
2074+ /* Decrease module usage counts and free resources */
2075+ loc_cpu_entry = private->entries;
2076+ xt_entry_foreach(iter, loc_cpu_entry, private->size)
2077+ cleanup_entry(iter, net);
2078+ if (private->number > private->initial_entries)
2079+ module_put(table_owner);
2080+ xt_free_table_info(private);
2081+}
2082+
2083+int ipt_register_table(struct net *net, const struct xt_table *table,
2084+ const struct ipt_replace *repl,
2085+ const struct nf_hook_ops *ops, struct xt_table **res)
20682086 {
20692087 int ret;
20702088 struct xt_table_info *newinfo;
@@ -2073,10 +2091,8 @@ struct xt_table *ipt_register_table(struct net *net, · 2 unmodified lines
20732091 struct xt_table *new_table;
20742092
20752093 newinfo = xt_alloc_table_info(repl->size);
2076− if (!newinfo) {
2077− ret = -ENOMEM;
2078− goto out;
2079− }
2094+ if (!newinfo)
2095+ return -ENOMEM;
20802096
20812097 loc_cpu_entry = newinfo->entries;
20822098 memcpy(loc_cpu_entry, repl->entries, repl->size);
@@ -2091,30 +2107,27 @@ struct xt_table *ipt_register_table(struct net *net, · 8 unmodified lines
20912107 goto out_free;
20922108 }
20932109
2094− return new_table;
2110+ /* set res now, will see skbs right after nf_register_net_hooks */
2111+ WRITE_ONCE(*res, new_table);
2112+
2113+ ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
2114+ if (ret != 0) {
2115+ __ipt_unregister_table(net, new_table);
2116+ *res = NULL;
2117+ }
2118+
2119+ return ret;
20952120
20962121 out_free:
20972122 xt_free_table_info(newinfo);
2098−out:
2099− return ERR_PTR(ret);
2123+ return ret;
21002124 }
21012125
2102−void ipt_unregister_table(struct net *net, struct xt_table *table)
2126+void ipt_unregister_table(struct net *net, struct xt_table *table,
2127+ const struct nf_hook_ops *ops)
21032128 {
2104− struct xt_table_info *private;
2105− void *loc_cpu_entry;
2106− struct module *table_owner = table->me;
2107− struct ipt_entry *iter;
2108−
2109− private = xt_unregister_table(table);
2110−
2111− /* Decrease module usage counts and free resources */
2112− loc_cpu_entry = private->entries;
2113− xt_entry_foreach(iter, loc_cpu_entry, private->size)
2114− cleanup_entry(iter, net);
2115− if (private->number > private->initial_entries)
2116− module_put(table_owner);
2117− xt_free_table_info(private);
2129+ nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
2130+ __ipt_unregister_table(net, table);
21182131 }
21192132
21202133 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
net/ipv4/netfilter/iptable_filter.c+29−15
@@ -23,6 +23,7 @@ MODULE_DESCRIPTION("iptables filter table"); · 22 unmodified lines
2323 #define FILTER_VALID_HOOKS ((1 << NF_INET_LOCAL_IN) | \
2424 (1 << NF_INET_FORWARD) | \
2525 (1 << NF_INET_LOCAL_OUT))
26+static int __net_init iptable_filter_table_init(struct net *net);
2627
2728 static const struct xt_table packet_filter = {
2829 .name = "filter",
@@ -30,6 +31,7 @@ static const struct xt_table packet_filter = { · 1 unmodified line
3031 .me = THIS_MODULE,
3132 .af = NFPROTO_IPV4,
3233 .priority = NF_IP_PRI_FILTER,
34+ .table_init = iptable_filter_table_init,
3335 };
3436
3537 static unsigned int
@@ -48,12 +50,16 @@ iptable_filter_hook(void *priv, struct sk_buff *skb, · 12 unmodified lines
4850 static struct nf_hook_ops *filter_ops __read_mostly;
4951
5052 /* Default to forward because I got too much mail already. */
51−static bool forward = true;
53+static bool forward __read_mostly = true;
5254 module_param(forward, bool, 0000);
5355
54−static int __net_init iptable_filter_net_init(struct net *net)
56+static int __net_init iptable_filter_table_init(struct net *net)
5557 {
5658 struct ipt_replace *repl;
59+ int err;
60+
61+ if (net->ipv4.iptable_filter)
62+ return 0;
5763
5864 repl = ipt_alloc_initial_table(&packet_filter);
5965 if (repl == NULL)
@@ -62,15 +68,26 @@ static int __net_init iptable_filter_net_init(struct net *net) · 2 unmodified lines
6268 ((struct ipt_standard *)repl->entries)[1].target.verdict =
6369 forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
6470
65− net->ipv4.iptable_filter =
66− ipt_register_table(net, &packet_filter, repl);
71+ err = ipt_register_table(net, &packet_filter, repl, filter_ops,
72+ &net->ipv4.iptable_filter);
6773 kfree(repl);
68− return PTR_ERR_OR_ZERO(net->ipv4.iptable_filter);
74+ return err;
75+}
76+
77+static int __net_init iptable_filter_net_init(struct net *net)
78+{
79+ if (net == &init_net || !forward)
80+ return iptable_filter_table_init(net);
81+
82+ return 0;
6983 }
7084
7185 static void __net_exit iptable_filter_net_exit(struct net *net)
7286 {
73− ipt_unregister_table(net, net->ipv4.iptable_filter);
87+ if (!net->ipv4.iptable_filter)
88+ return;
89+ ipt_unregister_table(net, net->ipv4.iptable_filter, filter_ops);
90+ net->ipv4.iptable_filter = NULL;
7491 }
7592
7693 static struct pernet_operations iptable_filter_net_ops = {
@@ -82,24 +99,21 @@ static int __init iptable_filter_init(void) · 5 unmodified lines
8299 {
83100 int ret;
84101
102+ filter_ops = xt_hook_ops_alloc(&packet_filter, iptable_filter_hook);
103+ if (IS_ERR(filter_ops))
104+ return PTR_ERR(filter_ops);
105+
85106 ret = register_pernet_subsys(&iptable_filter_net_ops);
86107 if (ret < 0)
87− return ret;
88−
89− /* Register hooks */
90− filter_ops = xt_hook_link(&packet_filter, iptable_filter_hook);
91− if (IS_ERR(filter_ops)) {
92− ret = PTR_ERR(filter_ops);
93− unregister_pernet_subsys(&iptable_filter_net_ops);
94− }
108+ kfree(filter_ops);
95109
96110 return ret;
97111 }
98112
99113 static void __exit iptable_filter_fini(void)
100114 {
101− xt_hook_unlink(&packet_filter, filter_ops);
102115 unregister_pernet_subsys(&iptable_filter_net_ops);
116+ kfree(filter_ops);
103117 }
104118
105119 module_init(iptable_filter_init);
net/ipv4/netfilter/iptable_mangle.c+28−13
@@ -28,12 +28,15 @@ MODULE_DESCRIPTION("iptables mangle table"); · 27 unmodified lines
2828 (1 << NF_INET_LOCAL_OUT) | \
2929 (1 << NF_INET_POST_ROUTING))
3030
31+static int __net_init iptable_mangle_table_init(struct net *net);
32+
3133 static const struct xt_table packet_mangler = {
3234 .name = "mangle",
3335 .valid_hooks = MANGLE_VALID_HOOKS,
3436 .me = THIS_MODULE,
3537 .af = NFPROTO_IPV4,
3638 .priority = NF_IP_PRI_MANGLE,
39+ .table_init = iptable_mangle_table_init,
3740 };
3841
3942 static unsigned int
@@ -92,27 +95,32 @@ iptable_mangle_hook(void *priv, · 52 unmodified lines
9295 }
9396
9497 static struct nf_hook_ops *mangle_ops __read_mostly;
95−
96−static int __net_init iptable_mangle_net_init(struct net *net)
98+static int __net_init iptable_mangle_table_init(struct net *net)
9799 {
98100 struct ipt_replace *repl;
101+ int ret;
102+
103+ if (net->ipv4.iptable_mangle)
104+ return 0;
99105
100106 repl = ipt_alloc_initial_table(&packet_mangler);
101107 if (repl == NULL)
102108 return -ENOMEM;
103− net->ipv4.iptable_mangle =
104− ipt_register_table(net, &packet_mangler, repl);
109+ ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
110+ &net->ipv4.iptable_mangle);
105111 kfree(repl);
106− return PTR_ERR_OR_ZERO(net->ipv4.iptable_mangle);
112+ return ret;
107113 }
108114
109115 static void __net_exit iptable_mangle_net_exit(struct net *net)
110116 {
111− ipt_unregister_table(net, net->ipv4.iptable_mangle);
117+ if (!net->ipv4.iptable_mangle)
118+ return;
119+ ipt_unregister_table(net, net->ipv4.iptable_mangle, mangle_ops);
120+ net->ipv4.iptable_mangle = NULL;
112121 }
113122
114123 static struct pernet_operations iptable_mangle_net_ops = {
115− .init = iptable_mangle_net_init,
116124 .exit = iptable_mangle_net_exit,
117125 };
118126
@@ -120,15 +128,22 @@ static int __init iptable_mangle_init(void) · 1 unmodified line
120128 {
121129 int ret;
122130
131+ mangle_ops = xt_hook_ops_alloc(&packet_mangler, iptable_mangle_hook);
132+ if (IS_ERR(mangle_ops)) {
133+ ret = PTR_ERR(mangle_ops);
134+ return ret;
135+ }
136+
123137 ret = register_pernet_subsys(&iptable_mangle_net_ops);
124− if (ret < 0)
138+ if (ret < 0) {
139+ kfree(mangle_ops);
125140 return ret;
141+ }
126142
127− /* Register hooks */
128− mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
129− if (IS_ERR(mangle_ops)) {
130− ret = PTR_ERR(mangle_ops);
143+ ret = iptable_mangle_table_init(&init_net);
144+ if (ret) {
131145 unregister_pernet_subsys(&iptable_mangle_net_ops);
146+ kfree(mangle_ops);
132147 }
133148
134149 return ret;
@@ -136,8 +151,8 @@ static int __init iptable_mangle_init(void) · 1 unmodified line
136151
137152 static void __exit iptable_mangle_fini(void)
138153 {
139− xt_hook_unlink(&packet_mangler, mangle_ops);
140154 unregister_pernet_subsys(&iptable_mangle_net_ops);
155+ kfree(mangle_ops);
141156 }
142157
143158 module_init(iptable_mangle_init);
net/ipv4/netfilter/iptable_nat.c+22−19
@@ -18,6 +18,8 @@ · 17 unmodified lines
1818 #include <net/netfilter/nf_nat_core.h>
1919 #include <net/netfilter/nf_nat_l3proto.h>
2020
21+static int __net_init iptable_nat_table_init(struct net *net);
22+
2123 static const struct xt_table nf_nat_ipv4_table = {
2224 .name = "nat",
2325 .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
@@ -26,6 +28,7 @@ static const struct xt_table nf_nat_ipv4_table = { · 2 unmodified lines
2628 (1 << NF_INET_LOCAL_IN),
2729 .me = THIS_MODULE,
2830 .af = NFPROTO_IPV4,
31+ .table_init = iptable_nat_table_init,
2932 };
3033
3134 static unsigned int iptable_nat_do_chain(void *priv,
@@ -95,50 +98,50 @@ static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = { · 63 unmodified lines
9598 },
9699 };
97100
98−static int __net_init iptable_nat_net_init(struct net *net)
101+static int __net_init iptable_nat_table_init(struct net *net)
99102 {
100103 struct ipt_replace *repl;
104+ int ret;
105+
106+ if (net->ipv4.nat_table)
107+ return 0;
101108
102109 repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
103110 if (repl == NULL)
104111 return -ENOMEM;
105− net->ipv4.nat_table = ipt_register_table(net, &nf_nat_ipv4_table, repl);
112+ ret = ipt_register_table(net, &nf_nat_ipv4_table, repl,
113+ nf_nat_ipv4_ops, &net->ipv4.nat_table);
106114 kfree(repl);
107− return PTR_ERR_OR_ZERO(net->ipv4.nat_table);
115+ return ret;
108116 }
109117
110118 static void __net_exit iptable_nat_net_exit(struct net *net)
111119 {
112− ipt_unregister_table(net, net->ipv4.nat_table);
120+ if (!net->ipv4.nat_table)
121+ return;
122+ ipt_unregister_table(net, net->ipv4.nat_table, nf_nat_ipv4_ops);
123+ net->ipv4.nat_table = NULL;
113124 }
114125
115126 static struct pernet_operations iptable_nat_net_ops = {
116− .init = iptable_nat_net_init,
117127 .exit = iptable_nat_net_exit,
118128 };
119129
120130 static int __init iptable_nat_init(void)
121131 {
122− int err;
132+ int ret = register_pernet_subsys(&iptable_nat_net_ops);
123133
124− err = register_pernet_subsys(&iptable_nat_net_ops);
125− if (err < 0)
126− goto err1;
134+ if (ret)
135+ return ret;
127136
128− err = nf_register_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
129− if (err < 0)
130− goto err2;
131− return 0;
132−
133−err2:
134− unregister_pernet_subsys(&iptable_nat_net_ops);
135−err1:
136− return err;
137+ ret = iptable_nat_table_init(&init_net);
138+ if (ret)
139+ unregister_pernet_subsys(&iptable_nat_net_ops);
140+ return ret;
137141 }
138142
139143 static void __exit iptable_nat_exit(void)
140144 {
141− nf_unregister_hooks(nf_nat_ipv4_ops, ARRAY_SIZE(nf_nat_ipv4_ops));
142145 unregister_pernet_subsys(&iptable_nat_net_ops);
143146 }
144147
net/ipv4/netfilter/iptable_raw.c+26−12
@@ -10,12 +10,15 @@ · 9 unmodified lines
1010
1111 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
1212
13+static int __net_init iptable_raw_table_init(struct net *net);
14+
1315 static const struct xt_table packet_raw = {
1416 .name = "raw",
1517 .valid_hooks = RAW_VALID_HOOKS,
1618 .me = THIS_MODULE,
1719 .af = NFPROTO_IPV4,
1820 .priority = NF_IP_PRI_RAW,
21+ .table_init = iptable_raw_table_init,
1922 };
2023
2124 /* The work comes in here from netfilter.c. */
@@ -34,26 +37,32 @@ iptable_raw_hook(void *priv, struct sk_buff *skb, · 12 unmodified lines
3437
3538 static struct nf_hook_ops *rawtable_ops __read_mostly;
3639
37−static int __net_init iptable_raw_net_init(struct net *net)
40+static int __net_init iptable_raw_table_init(struct net *net)
3841 {
3942 struct ipt_replace *repl;
43+ int ret;
44+
45+ if (net->ipv4.iptable_raw)
46+ return 0;
4047
4148 repl = ipt_alloc_initial_table(&packet_raw);
4249 if (repl == NULL)
4350 return -ENOMEM;
44− net->ipv4.iptable_raw =
45− ipt_register_table(net, &packet_raw, repl);
51+ ret = ipt_register_table(net, &packet_raw, repl, rawtable_ops,
52+ &net->ipv4.iptable_raw);
4653 kfree(repl);
47− return PTR_ERR_OR_ZERO(net->ipv4.iptable_raw);
54+ return ret;
4855 }
4956
5057 static void __net_exit iptable_raw_net_exit(struct net *net)
5158 {
52− ipt_unregister_table(net, net->ipv4.iptable_raw);
59+ if (!net->ipv4.iptable_raw)
60+ return;
61+ ipt_unregister_table(net, net->ipv4.iptable_raw, rawtable_ops);
62+ net->ipv4.iptable_raw = NULL;
5363 }
5464
5565 static struct pernet_operations iptable_raw_net_ops = {
56− .init = iptable_raw_net_init,
5766 .exit = iptable_raw_net_exit,
5867 };
5968
@@ -61,15 +70,20 @@ static int __init iptable_raw_init(void) · 1 unmodified line
6170 {
6271 int ret;
6372
73+ rawtable_ops = xt_hook_ops_alloc(&packet_raw, iptable_raw_hook);
74+ if (IS_ERR(rawtable_ops))
75+ return PTR_ERR(rawtable_ops);
76+
6477 ret = register_pernet_subsys(&iptable_raw_net_ops);
65− if (ret < 0)
78+ if (ret < 0) {
79+ kfree(rawtable_ops);
6680 return ret;
81+ }
6782
68− /* Register hooks */
69− rawtable_ops = xt_hook_link(&packet_raw, iptable_raw_hook);
70− if (IS_ERR(rawtable_ops)) {
71− ret = PTR_ERR(rawtable_ops);
83+ ret = iptable_raw_table_init(&init_net);
84+ if (ret) {
7285 unregister_pernet_subsys(&iptable_raw_net_ops);
86+ kfree(rawtable_ops);
7387 }
7488
7589 return ret;
@@ -77,8 +91,8 @@ static int __init iptable_raw_init(void) · 1 unmodified line
7791
7892 static void __exit iptable_raw_fini(void)
7993 {
80− xt_hook_unlink(&packet_raw, rawtable_ops);
8194 unregister_pernet_subsys(&iptable_raw_net_ops);
95+ kfree(rawtable_ops);
8296 }
8397
8498 module_init(iptable_raw_init);
net/ipv4/netfilter/iptable_security.c+28−16
@@ -28,12 +28,15 @@ MODULE_DESCRIPTION("iptables security table, for MAC rules"); · 27 unmodified lines
2828 (1 << NF_INET_FORWARD) | \
2929 (1 << NF_INET_LOCAL_OUT)
3030
31+static int __net_init iptable_security_table_init(struct net *net);
32+
3133 static const struct xt_table security_table = {
3234 .name = "security",
3335 .valid_hooks = SECURITY_VALID_HOOKS,
3436 .me = THIS_MODULE,
3537 .af = NFPROTO_IPV4,
3638 .priority = NF_IP_PRI_SECURITY,
39+ .table_init = iptable_security_table_init,
3740 };
3841
3942 static unsigned int
@@ -51,26 +54,33 @@ iptable_security_hook(void *priv, struct sk_buff *skb, · 11 unmodified lines
5154
5255 static struct nf_hook_ops *sectbl_ops __read_mostly;
5356
54−static int __net_init iptable_security_net_init(struct net *net)
57+static int __net_init iptable_security_table_init(struct net *net)
5558 {
5659 struct ipt_replace *repl;
60+ int ret;
61+
62+ if (net->ipv4.iptable_security)
63+ return 0;
5764
5865 repl = ipt_alloc_initial_table(&security_table);
5966 if (repl == NULL)
6067 return -ENOMEM;
61− net->ipv4.iptable_security =
62− ipt_register_table(net, &security_table, repl);
68+ ret = ipt_register_table(net, &security_table, repl, sectbl_ops,
69+ &net->ipv4.iptable_security);
6370 kfree(repl);
64− return PTR_ERR_OR_ZERO(net->ipv4.iptable_security);
71+ return ret;
6572 }
6673
6774 static void __net_exit iptable_security_net_exit(struct net *net)
6875 {
69− ipt_unregister_table(net, net->ipv4.iptable_security);
76+ if (!net->ipv4.iptable_security)
77+ return;
78+
79+ ipt_unregister_table(net, net->ipv4.iptable_security, sectbl_ops);
80+ net->ipv4.iptable_security = NULL;
7081 }
7182
7283 static struct pernet_operations iptable_security_net_ops = {
73− .init = iptable_security_net_init,
7484 .exit = iptable_security_net_exit,
7585 };
7686
@@ -78,27 +88,29 @@ static int __init iptable_security_init(void) · 1 unmodified line
7888 {
7989 int ret;
8090
91+ sectbl_ops = xt_hook_ops_alloc(&security_table, iptable_security_hook);
92+ if (IS_ERR(sectbl_ops))
93+ return PTR_ERR(sectbl_ops);
94+
8195 ret = register_pernet_subsys(&iptable_security_net_ops);
82− if (ret < 0)
96+ if (ret < 0) {
97+ kfree(sectbl_ops);
8398 return ret;
84−
85− sectbl_ops = xt_hook_link(&security_table, iptable_security_hook);
86− if (IS_ERR(sectbl_ops)) {
87− ret = PTR_ERR(sectbl_ops);
88− goto cleanup_table;
8999 }
90100
91− return ret;
101+ ret = iptable_security_table_init(&init_net);
102+ if (ret) {
103+ unregister_pernet_subsys(&iptable_security_net_ops);
104+ kfree(sectbl_ops);
105+ }
92106
93−cleanup_table:
94− unregister_pernet_subsys(&iptable_security_net_ops);
95107 return ret;
96108 }
97109
98110 static void __exit iptable_security_fini(void)
99111 {
100− xt_hook_unlink(&security_table, sectbl_ops);
101112 unregister_pernet_subsys(&iptable_security_net_ops);
113+ kfree(sectbl_ops);
102114 }
103115
104116 module_init(iptable_security_init);
net/ipv4/netfilter/nf_defrag_ipv4.c+1−3
@@ -31,10 +31,8 @@ static int nf_ct_ipv4_gather_frags(struct net *net, struct sk_buff *skb, · 30 unmodified lines
3131 err = ip_defrag(net, skb, user);
3232 local_bh_enable();
3333
34− if (!err) {
35− ip_send_check(ip_hdr(skb));
34+ if (!err)
3635 skb->ignore_df = 1;
37− }
3836
3937 return err;
4038 }
net/ipv4/netfilter/nft_masq_ipv4.c+6−1
@@ -25,7 +25,12 @@ static void nft_masq_ipv4_eval(const struct nft_expr *expr, · 24 unmodified lines
2525
2626 memset(&range, 0, sizeof(range));
2727 range.flags = priv->flags;
28−
28+ if (priv->sreg_proto_min) {
29+ range.min_proto.all =
30+ *(__be16 *)&regs->data[priv->sreg_proto_min];
31+ range.max_proto.all =
32+ *(__be16 *)&regs->data[priv->sreg_proto_max];
33+ }
2934 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, pkt->hook,
3035 &range, pkt->out);
3136 }
net/ipv6/netfilter/ip6_tables.c+40−25
@@ -2071,9 +2071,28 @@ do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len) · 2070 unmodified lines
20712071 return ret;
20722072 }
20732073
2074−struct xt_table *ip6t_register_table(struct net *net,
2075− const struct xt_table *table,
2076− const struct ip6t_replace *repl)
2074+static void __ip6t_unregister_table(struct net *net, struct xt_table *table)
2075+{
2076+ struct xt_table_info *private;
2077+ void *loc_cpu_entry;
2078+ struct module *table_owner = table->me;
2079+ struct ip6t_entry *iter;
2080+
2081+ private = xt_unregister_table(table);
2082+
2083+ /* Decrease module usage counts and free resources */
2084+ loc_cpu_entry = private->entries;
2085+ xt_entry_foreach(iter, loc_cpu_entry, private->size)
2086+ cleanup_entry(iter, net);
2087+ if (private->number > private->initial_entries)
2088+ module_put(table_owner);
2089+ xt_free_table_info(private);
2090+}
2091+
2092+int ip6t_register_table(struct net *net, const struct xt_table *table,
2093+ const struct ip6t_replace *repl,
2094+ const struct nf_hook_ops *ops,
2095+ struct xt_table **res)
20772096 {
20782097 int ret;
20792098 struct xt_table_info *newinfo;
@@ -2082,10 +2101,8 @@ struct xt_table *ip6t_register_table(struct net *net, · 2 unmodified lines
20822101 struct xt_table *new_table;
20832102
20842103 newinfo = xt_alloc_table_info(repl->size);
2085− if (!newinfo) {
2086− ret = -ENOMEM;
2087− goto out;
2088− }
2104+ if (!newinfo)
2105+ return -ENOMEM;
20892106
20902107 loc_cpu_entry = newinfo->entries;
20912108 memcpy(loc_cpu_entry, repl->entries, repl->size);
@@ -2099,30 +2116,28 @@ struct xt_table *ip6t_register_table(struct net *net, · 7 unmodified lines
20992116 ret = PTR_ERR(new_table);
21002117 goto out_free;
21012118 }
2102− return new_table;
2119+
2120+ /* set res now, will see skbs right after nf_register_net_hooks */
2121+ WRITE_ONCE(*res, new_table);
2122+
2123+ ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
2124+ if (ret != 0) {
2125+ __ip6t_unregister_table(net, new_table);
2126+ *res = NULL;
2127+ }
2128+
2129+ return ret;
21032130
21042131 out_free:
21052132 xt_free_table_info(newinfo);
2106−out:
2107− return ERR_PTR(ret);
2133+ return ret;
21082134 }
21092135
2110−void ip6t_unregister_table(struct net *net, struct xt_table *table)
2136+void ip6t_unregister_table(struct net *net, struct xt_table *table,
2137+ const struct nf_hook_ops *ops)
21112138 {
2112− struct xt_table_info *private;
2113− void *loc_cpu_entry;
2114− struct module *table_owner = table->me;
2115− struct ip6t_entry *iter;
2116−
2117− private = xt_unregister_table(table);
2118−
2119− /* Decrease module usage counts and free resources */
2120− loc_cpu_entry = private->entries;
2121− xt_entry_foreach(iter, loc_cpu_entry, private->size)
2122− cleanup_entry(iter, net);
2123− if (private->number > private->initial_entries)
2124− module_put(table_owner);
2125− xt_free_table_info(private);
2139+ nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
2140+ __ip6t_unregister_table(net, table);
21262141 }
21272142
21282143 /* Returns 1 if the type and code is matched by the range, 0 otherwise */
net/ipv6/netfilter/ip6table_filter.c+29−18
@@ -22,12 +22,15 @@ MODULE_DESCRIPTION("ip6tables filter table"); · 21 unmodified lines
2222 (1 << NF_INET_FORWARD) | \
2323 (1 << NF_INET_LOCAL_OUT))
2424
25+static int __net_init ip6table_filter_table_init(struct net *net);
26+
2527 static const struct xt_table packet_filter = {
2628 .name = "filter",
2729 .valid_hooks = FILTER_VALID_HOOKS,
2830 .me = THIS_MODULE,
2931 .af = NFPROTO_IPV6,
3032 .priority = NF_IP6_PRI_FILTER,
33+ .table_init = ip6table_filter_table_init,
3134 };
3235
3336 /* The work comes in here from netfilter.c. */
@@ -44,9 +47,13 @@ static struct nf_hook_ops *filter_ops __read_mostly; · 10 unmodified lines
4447 static bool forward = true;
4548 module_param(forward, bool, 0000);
4649
47−static int __net_init ip6table_filter_net_init(struct net *net)
50+static int __net_init ip6table_filter_table_init(struct net *net)
4851 {
4952 struct ip6t_replace *repl;
53+ int err;
54+
55+ if (net->ipv6.ip6table_filter)
56+ return 0;
5057
5158 repl = ip6t_alloc_initial_table(&packet_filter);
5259 if (repl == NULL)
@@ -55,15 +62,26 @@ static int __net_init ip6table_filter_net_init(struct net *net) · 2 unmodified lines
5562 ((struct ip6t_standard *)repl->entries)[1].target.verdict =
5663 forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
5764
58− net->ipv6.ip6table_filter =
59− ip6t_register_table(net, &packet_filter, repl);
65+ err = ip6t_register_table(net, &packet_filter, repl, filter_ops,
66+ &net->ipv6.ip6table_filter);
6067 kfree(repl);
61− return PTR_ERR_OR_ZERO(net->ipv6.ip6table_filter);
68+ return err;
69+}
70+
71+static int __net_init ip6table_filter_net_init(struct net *net)
72+{
73+ if (net == &init_net || !forward)
74+ return ip6table_filter_table_init(net);
75+
76+ return 0;
6277 }
6378
6479 static void __net_exit ip6table_filter_net_exit(struct net *net)
6580 {
66− ip6t_unregister_table(net, net->ipv6.ip6table_filter);
81+ if (!net->ipv6.ip6table_filter)
82+ return;
83+ ip6t_unregister_table(net, net->ipv6.ip6table_filter, filter_ops);
84+ net->ipv6.ip6table_filter = NULL;
6785 }
6886
6987 static struct pernet_operations ip6table_filter_net_ops = {
@@ -75,28 +93,21 @@ static int __init ip6table_filter_init(void) · 5 unmodified lines
7593 {
7694 int ret;
7795
96+ filter_ops = xt_hook_ops_alloc(&packet_filter, ip6table_filter_hook);
97+ if (IS_ERR(filter_ops))
98+ return PTR_ERR(filter_ops);
99+
78100 ret = register_pernet_subsys(&ip6table_filter_net_ops);
79101 if (ret < 0)
80− return ret;
81−
82− /* Register hooks */
83− filter_ops = xt_hook_link(&packet_filter, ip6table_filter_hook);
84− if (IS_ERR(filter_ops)) {
85− ret = PTR_ERR(filter_ops);
86− goto cleanup_table;
87− }
102+ kfree(filter_ops);
88103
89104 return ret;
90−
91− cleanup_table:
92− unregister_pernet_subsys(&ip6table_filter_net_ops);
93− return ret;
94105 }
95106
96107 static void __exit ip6table_filter_fini(void)
97108 {
98− xt_hook_unlink(&packet_filter, filter_ops);
99109 unregister_pernet_subsys(&ip6table_filter_net_ops);
110+ kfree(filter_ops);
100111 }
101112
102113 module_init(ip6table_filter_init);
net/ipv6/netfilter/ip6table_mangle.c+28−18
@@ -23,12 +23,15 @@ MODULE_DESCRIPTION("ip6tables mangle table"); · 22 unmodified lines
2323 (1 << NF_INET_LOCAL_OUT) | \
2424 (1 << NF_INET_POST_ROUTING))
2525
26+static int __net_init ip6table_mangle_table_init(struct net *net);
27+
2628 static const struct xt_table packet_mangler = {
2729 .name = "mangle",
2830 .valid_hooks = MANGLE_VALID_HOOKS,
2931 .me = THIS_MODULE,
3032 .af = NFPROTO_IPV6,
3133 .priority = NF_IP6_PRI_MANGLE,
34+ .table_init = ip6table_mangle_table_init,
3235 };
3336
3437 static unsigned int
@@ -88,26 +91,33 @@ ip6table_mangle_hook(void *priv, struct sk_buff *skb, · 53 unmodified lines
8891 }
8992
9093 static struct nf_hook_ops *mangle_ops __read_mostly;
91−static int __net_init ip6table_mangle_net_init(struct net *net)
94+static int __net_init ip6table_mangle_table_init(struct net *net)
9295 {
9396 struct ip6t_replace *repl;
97+ int ret;
98+
99+ if (net->ipv6.ip6table_mangle)
100+ return 0;
94101
95102 repl = ip6t_alloc_initial_table(&packet_mangler);
96103 if (repl == NULL)
97104 return -ENOMEM;
98− net->ipv6.ip6table_mangle =
99− ip6t_register_table(net, &packet_mangler, repl);
105+ ret = ip6t_register_table(net, &packet_mangler, repl, mangle_ops,
106+ &net->ipv6.ip6table_mangle);
100107 kfree(repl);
101− return PTR_ERR_OR_ZERO(net->ipv6.ip6table_mangle);
108+ return ret;
102109 }
103110
104111 static void __net_exit ip6table_mangle_net_exit(struct net *net)
105112 {
106− ip6t_unregister_table(net, net->ipv6.ip6table_mangle);
113+ if (!net->ipv6.ip6table_mangle)
114+ return;
115+
116+ ip6t_unregister_table(net, net->ipv6.ip6table_mangle, mangle_ops);
117+ net->ipv6.ip6table_mangle = NULL;
107118 }
108119
109120 static struct pernet_operations ip6table_mangle_net_ops = {
110− .init = ip6table_mangle_net_init,
111121 .exit = ip6table_mangle_net_exit,
112122 };
113123
@@ -115,28 +125,28 @@ static int __init ip6table_mangle_init(void) · 1 unmodified line
115125 {
116126 int ret;
117127
128+ mangle_ops = xt_hook_ops_alloc(&packet_mangler, ip6table_mangle_hook);
129+ if (IS_ERR(mangle_ops))
130+ return PTR_ERR(mangle_ops);
131+
118132 ret = register_pernet_subsys(&ip6table_mangle_net_ops);
119− if (ret < 0)
133+ if (ret < 0) {
134+ kfree(mangle_ops);
120135 return ret;
121−
122− /* Register hooks */
123− mangle_ops = xt_hook_link(&packet_mangler, ip6table_mangle_hook);
124− if (IS_ERR(mangle_ops)) {
125− ret = PTR_ERR(mangle_ops);
126− goto cleanup_table;
127136 }
128137
129− return ret;
130−
131− cleanup_table:
132− unregister_pernet_subsys(&ip6table_mangle_net_ops);
138+ ret = ip6table_mangle_table_init(&init_net);
139+ if (ret) {
140+ unregister_pernet_subsys(&ip6table_mangle_net_ops);
141+ kfree(mangle_ops);
142+ }
133143 return ret;
134144 }
135145
136146 static void __exit ip6table_mangle_fini(void)
137147 {
138− xt_hook_unlink(&packet_mangler, mangle_ops);
139148 unregister_pernet_subsys(&ip6table_mangle_net_ops);
149+ kfree(mangle_ops);
140150 }
141151
142152 module_init(ip6table_mangle_init);
net/ipv6/netfilter/ip6table_nat.c+22−19
@@ -20,6 +20,8 @@ · 19 unmodified lines
2020 #include <net/netfilter/nf_nat_core.h>
2121 #include <net/netfilter/nf_nat_l3proto.h>
2222
23+static int __net_init ip6table_nat_table_init(struct net *net);
24+
2325 static const struct xt_table nf_nat_ipv6_table = {
2426 .name = "nat",
2527 .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
@@ -28,6 +30,7 @@ static const struct xt_table nf_nat_ipv6_table = { · 2 unmodified lines
2830 (1 << NF_INET_LOCAL_IN),
2931 .me = THIS_MODULE,
3032 .af = NFPROTO_IPV6,
33+ .table_init = ip6table_nat_table_init,
3134 };
3235
3336 static unsigned int ip6table_nat_do_chain(void *priv,
@@ -97,50 +100,50 @@ static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = { · 63 unmodified lines
97100 },
98101 };
99102
100−static int __net_init ip6table_nat_net_init(struct net *net)
103+static int __net_init ip6table_nat_table_init(struct net *net)
101104 {
102105 struct ip6t_replace *repl;
106+ int ret;
107+
108+ if (net->ipv6.ip6table_nat)
109+ return 0;
103110
104111 repl = ip6t_alloc_initial_table(&nf_nat_ipv6_table);
105112 if (repl == NULL)
106113 return -ENOMEM;
107− net->ipv6.ip6table_nat = ip6t_register_table(net, &nf_nat_ipv6_table, repl);
114+ ret = ip6t_register_table(net, &nf_nat_ipv6_table, repl,
115+ nf_nat_ipv6_ops, &net->ipv6.ip6table_nat);
108116 kfree(repl);
109− return PTR_ERR_OR_ZERO(net->ipv6.ip6table_nat);
117+ return ret;
110118 }
111119
112120 static void __net_exit ip6table_nat_net_exit(struct net *net)
113121 {
114− ip6t_unregister_table(net, net->ipv6.ip6table_nat);
122+ if (!net->ipv6.ip6table_nat)
123+ return;
124+ ip6t_unregister_table(net, net->ipv6.ip6table_nat, nf_nat_ipv6_ops);
125+ net->ipv6.ip6table_nat = NULL;
115126 }
116127
117128 static struct pernet_operations ip6table_nat_net_ops = {
118− .init = ip6table_nat_net_init,
119129 .exit = ip6table_nat_net_exit,
120130 };
121131
122132 static int __init ip6table_nat_init(void)
123133 {
124− int err;
134+ int ret = register_pernet_subsys(&ip6table_nat_net_ops);
125135
126− err = register_pernet_subsys(&ip6table_nat_net_ops);
127− if (err < 0)
128− goto err1;
136+ if (ret)
137+ return ret;
129138
130− err = nf_register_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
131− if (err < 0)
132− goto err2;
133− return 0;
134−
135−err2:
136− unregister_pernet_subsys(&ip6table_nat_net_ops);
137−err1:
138− return err;
139+ ret = ip6table_nat_table_init(&init_net);
140+ if (ret)
141+ unregister_pernet_subsys(&ip6table_nat_net_ops);
142+ return ret;
139143 }
140144
141145 static void __exit ip6table_nat_exit(void)
142146 {
143− nf_unregister_hooks(nf_nat_ipv6_ops, ARRAY_SIZE(nf_nat_ipv6_ops));
144147 unregister_pernet_subsys(&ip6table_nat_net_ops);
145148 }
146149
net/ipv6/netfilter/ip6table_raw.c+28−18
@@ -9,12 +9,15 @@ · 8 unmodified lines
99
1010 #define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
1111
12+static int __net_init ip6table_raw_table_init(struct net *net);
13+
1214 static const struct xt_table packet_raw = {
1315 .name = "raw",
1416 .valid_hooks = RAW_VALID_HOOKS,
1517 .me = THIS_MODULE,
1618 .af = NFPROTO_IPV6,
1719 .priority = NF_IP6_PRI_RAW,
20+ .table_init = ip6table_raw_table_init,
1821 };
1922
2023 /* The work comes in here from netfilter.c. */
@@ -27,26 +30,32 @@ ip6table_raw_hook(void *priv, struct sk_buff *skb, · 6 unmodified lines
2730
2831 static struct nf_hook_ops *rawtable_ops __read_mostly;
2932
30−static int __net_init ip6table_raw_net_init(struct net *net)
33+static int __net_init ip6table_raw_table_init(struct net *net)
3134 {
3235 struct ip6t_replace *repl;
36+ int ret;
37+
38+ if (net->ipv6.ip6table_raw)
39+ return 0;
3340
3441 repl = ip6t_alloc_initial_table(&packet_raw);
3542 if (repl == NULL)
3643 return -ENOMEM;
37− net->ipv6.ip6table_raw =
38− ip6t_register_table(net, &packet_raw, repl);
44+ ret = ip6t_register_table(net, &packet_raw, repl, rawtable_ops,
45+ &net->ipv6.ip6table_raw);
3946 kfree(repl);
40− return PTR_ERR_OR_ZERO(net->ipv6.ip6table_raw);
47+ return ret;
4148 }
4249
4350 static void __net_exit ip6table_raw_net_exit(struct net *net)
4451 {
45− ip6t_unregister_table(net, net->ipv6.ip6table_raw);
52+ if (!net->ipv6.ip6table_raw)
53+ return;
54+ ip6t_unregister_table(net, net->ipv6.ip6table_raw, rawtable_ops);
55+ net->ipv6.ip6table_raw = NULL;
4656 }
4757
4858 static struct pernet_operations ip6table_raw_net_ops = {
49− .init = ip6table_raw_net_init,
5059 .exit = ip6table_raw_net_exit,
5160 };
5261
@@ -54,28 +63,29 @@ static int __init ip6table_raw_init(void) · 1 unmodified line
5463 {
5564 int ret;
5665
66+ /* Register hooks */
67+ rawtable_ops = xt_hook_ops_alloc(&packet_raw, ip6table_raw_hook);
68+ if (IS_ERR(rawtable_ops))
69+ return PTR_ERR(rawtable_ops);
70+
5771 ret = register_pernet_subsys(&ip6table_raw_net_ops);
58− if (ret < 0)
72+ if (ret < 0) {
73+ kfree(rawtable_ops);
5974 return ret;
60−
61− /* Register hooks */
62− rawtable_ops = xt_hook_link(&packet_raw, ip6table_raw_hook);
63− if (IS_ERR(rawtable_ops)) {
64− ret = PTR_ERR(rawtable_ops);
65− goto cleanup_table;
6675 }
6776
68− return ret;
69−
70− cleanup_table:
71− unregister_pernet_subsys(&ip6table_raw_net_ops);
77+ ret = ip6table_raw_table_init(&init_net);
78+ if (ret) {
79+ unregister_pernet_subsys(&ip6table_raw_net_ops);
80+ kfree(rawtable_ops);
81+ }
7282 return ret;
7383 }
7484
7585 static void __exit ip6table_raw_fini(void)
7686 {
77− xt_hook_unlink(&packet_raw, rawtable_ops);
7887 unregister_pernet_subsys(&ip6table_raw_net_ops);
88+ kfree(rawtable_ops);
7989 }
8090
8191 module_init(ip6table_raw_init);
net/ipv6/netfilter/ip6table_security.c+27−17
@@ -27,12 +27,15 @@ MODULE_DESCRIPTION("ip6tables security table, for MAC rules"); · 26 unmodified lines
2727 (1 << NF_INET_FORWARD) | \
2828 (1 << NF_INET_LOCAL_OUT)
2929
30+static int __net_init ip6table_security_table_init(struct net *net);
31+
3032 static const struct xt_table security_table = {
3133 .name = "security",
3234 .valid_hooks = SECURITY_VALID_HOOKS,
3335 .me = THIS_MODULE,
3436 .af = NFPROTO_IPV6,
3537 .priority = NF_IP6_PRI_SECURITY,
38+ .table_init = ip6table_security_table_init,
3639 };
3740
3841 static unsigned int
@@ -44,26 +47,32 @@ ip6table_security_hook(void *priv, struct sk_buff *skb, · 5 unmodified lines
4447
4548 static struct nf_hook_ops *sectbl_ops __read_mostly;
4649
47−static int __net_init ip6table_security_net_init(struct net *net)
50+static int __net_init ip6table_security_table_init(struct net *net)
4851 {
4952 struct ip6t_replace *repl;
53+ int ret;
54+
55+ if (net->ipv6.ip6table_security)
56+ return 0;
5057
5158 repl = ip6t_alloc_initial_table(&security_table);
5259 if (repl == NULL)
5360 return -ENOMEM;
54− net->ipv6.ip6table_security =
55− ip6t_register_table(net, &security_table, repl);
61+ ret = ip6t_register_table(net, &security_table, repl, sectbl_ops,
62+ &net->ipv6.ip6table_security);
5663 kfree(repl);
57− return PTR_ERR_OR_ZERO(net->ipv6.ip6table_security);
64+ return ret;
5865 }
5966
6067 static void __net_exit ip6table_security_net_exit(struct net *net)
6168 {
62− ip6t_unregister_table(net, net->ipv6.ip6table_security);
69+ if (!net->ipv6.ip6table_security)
70+ return;
71+ ip6t_unregister_table(net, net->ipv6.ip6table_security, sectbl_ops);
72+ net->ipv6.ip6table_security = NULL;
6373 }
6474
6575 static struct pernet_operations ip6table_security_net_ops = {
66− .init = ip6table_security_net_init,
6776 .exit = ip6table_security_net_exit,
6877 };
6978
@@ -71,27 +80,28 @@ static int __init ip6table_security_init(void) · 1 unmodified line
7180 {
7281 int ret;
7382
83+ sectbl_ops = xt_hook_ops_alloc(&security_table, ip6table_security_hook);
84+ if (IS_ERR(sectbl_ops))
85+ return PTR_ERR(sectbl_ops);
86+
7487 ret = register_pernet_subsys(&ip6table_security_net_ops);
75− if (ret < 0)
88+ if (ret < 0) {
89+ kfree(sectbl_ops);
7690 return ret;
77−
78− sectbl_ops = xt_hook_link(&security_table, ip6table_security_hook);
79− if (IS_ERR(sectbl_ops)) {
80− ret = PTR_ERR(sectbl_ops);
81− goto cleanup_table;
8291 }
8392
84− return ret;
85−
86−cleanup_table:
87− unregister_pernet_subsys(&ip6table_security_net_ops);
93+ ret = ip6table_security_table_init(&init_net);
94+ if (ret) {
95+ unregister_pernet_subsys(&ip6table_security_net_ops);
96+ kfree(sectbl_ops);
97+ }
8898 return ret;
8999 }
90100
91101 static void __exit ip6table_security_fini(void)
92102 {
93− xt_hook_unlink(&security_table, sectbl_ops);
94103 unregister_pernet_subsys(&ip6table_security_net_ops);
104+ kfree(sectbl_ops);
95105 }
96106
97107 module_init(ip6table_security_init);
net/ipv6/netfilter/nft_masq_ipv6.c+6−1
@@ -26,7 +26,12 @@ static void nft_masq_ipv6_eval(const struct nft_expr *expr, · 25 unmodified lines
2626
2727 memset(&range, 0, sizeof(range));
2828 range.flags = priv->flags;
29−
29+ if (priv->sreg_proto_min) {
30+ range.min_proto.all =
31+ *(__be16 *)&regs->data[priv->sreg_proto_min];
32+ range.max_proto.all =
33+ *(__be16 *)&regs->data[priv->sreg_proto_max];
34+ }
3035 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range, pkt->out);
3136 }
3237
net/netfilter/ipvs/ip_vs_app.c+2−6
@@ -605,17 +605,13 @@ static const struct file_operations ip_vs_app_fops = { · 604 unmodified lines
605605
606606 int __net_init ip_vs_app_net_init(struct netns_ipvs *ipvs)
607607 {
608− struct net *net = ipvs->net;
609−
610608 INIT_LIST_HEAD(&ipvs->app_list);
611− proc_create("ip_vs_app", 0, net->proc_net, &ip_vs_app_fops);
609+ proc_create("ip_vs_app", 0, ipvs->net->proc_net, &ip_vs_app_fops);
612610 return 0;
613611 }
614612
615613 void __net_exit ip_vs_app_net_cleanup(struct netns_ipvs *ipvs)
616614 {
617− struct net *net = ipvs->net;
618−
619615 unregister_ip_vs_app(ipvs, NULL /* all */);
620− remove_proc_entry("ip_vs_app", net->proc_net);
616+ remove_proc_entry("ip_vs_app", ipvs->net->proc_net);
621617 }
net/netfilter/ipvs/ip_vs_ctl.c+6−11
@@ -1376,8 +1376,6 @@ static void __ip_vs_del_service(struct ip_vs_service *svc, bool cleanup) · 1375 unmodified lines
13761376 struct ip_vs_pe *old_pe;
13771377 struct netns_ipvs *ipvs = svc->ipvs;
13781378
1379− pr_info("%s: enter\n", __func__);
1380−
13811379 /* Count only IPv4 services for old get/setsockopt interface */
13821380 if (svc->af == AF_INET)
13831381 ipvs->num_services--;
@@ -3947,7 +3945,6 @@ static struct notifier_block ip_vs_dst_notifier = { · 2563 unmodified lines
39473945
39483946 int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs)
39493947 {
3950− struct net *net = ipvs->net;
39513948 int i, idx;
39523949
39533950 /* Initialize rs_table */
@@ -3974,9 +3971,9 @@ int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs) · 20 unmodified lines
39743971
39753972 spin_lock_init(&ipvs->tot_stats.lock);
39763973
3977− proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
3978− proc_create("ip_vs_stats", 0, net->proc_net, &ip_vs_stats_fops);
3979− proc_create("ip_vs_stats_percpu", 0, net->proc_net,
3974+ proc_create("ip_vs", 0, ipvs->net->proc_net, &ip_vs_info_fops);
3975+ proc_create("ip_vs_stats", 0, ipvs->net->proc_net, &ip_vs_stats_fops);
3976+ proc_create("ip_vs_stats_percpu", 0, ipvs->net->proc_net,
39803977 &ip_vs_stats_percpu_fops);
39813978
39823979 if (ip_vs_control_net_init_sysctl(ipvs))
@@ -3991,13 +3988,11 @@ err: · 8 unmodified lines
39913988
39923989 void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)
39933990 {
3994− struct net *net = ipvs->net;
3995−
39963991 ip_vs_trash_cleanup(ipvs);
39973992 ip_vs_control_net_cleanup_sysctl(ipvs);
3998− remove_proc_entry("ip_vs_stats_percpu", net->proc_net);
3999− remove_proc_entry("ip_vs_stats", net->proc_net);
4000− remove_proc_entry("ip_vs", net->proc_net);
3993+ remove_proc_entry("ip_vs_stats_percpu", ipvs->net->proc_net);
3994+ remove_proc_entry("ip_vs_stats", ipvs->net->proc_net);
3995+ remove_proc_entry("ip_vs", ipvs->net->proc_net);
40013996 free_percpu(ipvs->tot_stats.cpustats);
40023997 }
40033998
net/netfilter/nft_masq.c+40−11
@@ -17,7 +17,9 @@ · 16 unmodified lines
1717 #include <net/netfilter/nft_masq.h>
1818
1919 const struct nla_policy nft_masq_policy[NFTA_MASQ_MAX + 1] = {
20− [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
20+ [NFTA_MASQ_FLAGS] = { .type = NLA_U32 },
21+ [NFTA_MASQ_REG_PROTO_MIN] = { .type = NLA_U32 },
22+ [NFTA_MASQ_REG_PROTO_MAX] = { .type = NLA_U32 },
2123 };
2224 EXPORT_SYMBOL_GPL(nft_masq_policy);
2325
@@ -40,6 +42,7 @@ int nft_masq_init(const struct nft_ctx *ctx, · 16 unmodified lines
4042 const struct nft_expr *expr,
4143 const struct nlattr * const tb[])
4244 {
45+ u32 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
4346 struct nft_masq *priv = nft_expr_priv(expr);
4447 int err;
4548
@@ -47,12 +50,32 @@ int nft_masq_init(const struct nft_ctx *ctx, · 1 unmodified line
4750 if (err)
4851 return err;
4952
50− if (tb[NFTA_MASQ_FLAGS] == NULL)
51− return 0;
52−
53− priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
54− if (priv->flags & ~NF_NAT_RANGE_MASK)
55− return -EINVAL;
53+ if (tb[NFTA_MASQ_FLAGS]) {
54+ priv->flags = ntohl(nla_get_be32(tb[NFTA_MASQ_FLAGS]));
55+ if (priv->flags & ~NF_NAT_RANGE_MASK)
56+ return -EINVAL;
57+ }
58+
59+ if (tb[NFTA_MASQ_REG_PROTO_MIN]) {
60+ priv->sreg_proto_min =
61+ nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MIN]);
62+
63+ err = nft_validate_register_load(priv->sreg_proto_min, plen);
64+ if (err < 0)
65+ return err;
66+
67+ if (tb[NFTA_MASQ_REG_PROTO_MAX]) {
68+ priv->sreg_proto_max =
69+ nft_parse_register(tb[NFTA_MASQ_REG_PROTO_MAX]);
70+
71+ err = nft_validate_register_load(priv->sreg_proto_max,
72+ plen);
73+ if (err < 0)
74+ return err;
75+ } else {
76+ priv->sreg_proto_max = priv->sreg_proto_min;
77+ }
78+ }
5679
5780 return 0;
5881 }
@@ -62,12 +85,18 @@ int nft_masq_dump(struct sk_buff *skb, const struct nft_expr *expr) · 3 unmodified lines
6285 {
6386 const struct nft_masq *priv = nft_expr_priv(expr);
6487
65− if (priv->flags == 0)
66− return 0;
67−
68− if (nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
88+ if (priv->flags != 0 &&
89+ nla_put_be32(skb, NFTA_MASQ_FLAGS, htonl(priv->flags)))
6990 goto nla_put_failure;
7091
92+ if (priv->sreg_proto_min) {
93+ if (nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MIN,
94+ priv->sreg_proto_min) ||
95+ nft_dump_register(skb, NFTA_MASQ_REG_PROTO_MAX,
96+ priv->sreg_proto_max))
97+ goto nla_put_failure;
98+ }
99+
71100 return 0;
72101
73102 nla_put_failure:
net/netfilter/nft_meta.c+11−0
@@ -28,6 +28,8 @@ · 27 unmodified lines
2828
2929 #include <uapi/linux/netfilter_bridge.h> /* NF_BR_PRE_ROUTING */
3030
31+static DEFINE_PER_CPU(struct rnd_state, nft_prandom_state);
32+
3133 void nft_meta_get_eval(const struct nft_expr *expr,
3234 struct nft_regs *regs,
3335 const struct nft_pktinfo *pkt)
@@ -181,6 +183,11 @@ void nft_meta_get_eval(const struct nft_expr *expr, · 147 unmodified lines
181183 *dest = sock_cgroup_classid(&sk->sk_cgrp_data);
182184 break;
183185 #endif
186+ case NFT_META_PRANDOM: {
187+ struct rnd_state *state = this_cpu_ptr(&nft_prandom_state);
188+ *dest = prandom_u32_state(state);
189+ break;
190+ }
184191 default:
185192 WARN_ON(1);
186193 goto err;
@@ -277,6 +284,10 @@ int nft_meta_get_init(const struct nft_ctx *ctx, · 90 unmodified lines
277284 case NFT_META_OIFNAME:
278285 len = IFNAMSIZ;
279286 break;
287+ case NFT_META_PRANDOM:
288+ prandom_init_once(&nft_prandom_state);
289+ len = sizeof(u32);
290+ break;
280291 default:
281292 return -EOPNOTSUPP;
282293 }
net/netfilter/x_tables.c+40−25
@@ -694,12 +694,45 @@ EXPORT_SYMBOL(xt_free_table_info); · 693 unmodified lines
694694 struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
695695 const char *name)
696696 {
697− struct xt_table *t;
697+ struct xt_table *t, *found = NULL;
698698
699699 mutex_lock(&xt[af].mutex);
700700 list_for_each_entry(t, &net->xt.tables[af], list)
701701 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
702702 return t;
703+
704+ if (net == &init_net)
705+ goto out;
706+
707+ /* Table doesn't exist in this netns, re-try init */
708+ list_for_each_entry(t, &init_net.xt.tables[af], list) {
709+ if (strcmp(t->name, name))
710+ continue;
711+ if (!try_module_get(t->me))
712+ return NULL;
713+
714+ mutex_unlock(&xt[af].mutex);
715+ if (t->table_init(net) != 0) {
716+ module_put(t->me);
717+ return NULL;
718+ }
719+
720+ found = t;
721+
722+ mutex_lock(&xt[af].mutex);
723+ break;
724+ }
725+
726+ if (!found)
727+ goto out;
728+
729+ /* and once again: */
730+ list_for_each_entry(t, &net->xt.tables[af], list)
731+ if (strcmp(t->name, name) == 0)
732+ return t;
733+
734+ module_put(found->me);
735+ out:
703736 mutex_unlock(&xt[af].mutex);
704737 return NULL;
705738 }
@@ -1170,20 +1203,20 @@ static const struct file_operations xt_target_ops = { · 464 unmodified lines
11701203 #endif /* CONFIG_PROC_FS */
11711204
11721205 /**
1173− * xt_hook_link - set up hooks for a new table
1206+ * xt_hook_ops_alloc - set up hooks for a new table
11741207 * @table: table with metadata needed to set up hooks
11751208 * @fn: Hook function
11761209 *
1177− * This function will take care of creating and registering the necessary
1178− * Netfilter hooks for XT tables.
1210+ * This function will create the nf_hook_ops that the x_table needs
1211+ * to hand to xt_hook_link_net().
11791212 */
1180−struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn)
1213+struct nf_hook_ops *
1214+xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
11811215 {
11821216 unsigned int hook_mask = table->valid_hooks;
11831217 uint8_t i, num_hooks = hweight32(hook_mask);
11841218 uint8_t hooknum;
11851219 struct nf_hook_ops *ops;
1186− int ret;
11871220
11881221 ops = kmalloc(sizeof(*ops) * num_hooks, GFP_KERNEL);
11891222 if (ops == NULL)
@@ -1200,27 +1233,9 @@ struct nf_hook_ops *xt_hook_link(const struct xt_table *table, nf_hookfn *fn) · 10 unmodified lines
12001233 ++i;
12011234 }
12021235
1203− ret = nf_register_hooks(ops, num_hooks);
1204− if (ret < 0) {
1205− kfree(ops);
1206− return ERR_PTR(ret);
1207− }
1208−
12091236 return ops;
12101237 }
1211−EXPORT_SYMBOL_GPL(xt_hook_link);
1212−
1213−/**
1214− * xt_hook_unlink - remove hooks for a table
1215− * @ops: nf_hook_ops array as returned by nf_hook_link
1216− * @hook_mask: the very same mask that was passed to nf_hook_link
1217− */
1218−void xt_hook_unlink(const struct xt_table *table, struct nf_hook_ops *ops)
1219−{
1220− nf_unregister_hooks(ops, hweight32(table->valid_hooks));
1221− kfree(ops);
1222−}
1223−EXPORT_SYMBOL_GPL(xt_hook_unlink);
1238+EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
12241239
12251240 int xt_proto_init(struct net *net, u_int8_t af)
12261241 {
net/netfilter/xt_osf.c+0−2
@@ -262,7 +262,6 @@ xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p) · 261 unmodified lines
262262 if (f->opt[optnum].kind == (*optp)) {
263263 __u32 len = f->opt[optnum].length;
264264 const __u8 *optend = optp + len;
265− int loop_cont = 0;
266265
267266 fmatch = FMATCH_OK;
268267
@@ -275,7 +274,6 @@ xt_osf_match_packet(const struct sk_buff *skb, struct xt_action_param *p) · 6 unmodified lines
275274 mss = ntohs((__force __be16)mss);
276275 break;
277276 case OSFOPT_TS:
278− loop_cont = 1;
279277 break;
280278 }
281279

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t