kernel: generic: Fix nftables inet table breakage
[openwrt/openwrt.git] / target / linux / generic / backport-4.14 / 294-v4.16-netfilter-reduce-hook-array-sizes-to-what-is-needed.patch
1 From ef57170bbfdd6958281011332b1fd237712f69f0 Mon Sep 17 00:00:00 2001
2 From: Florian Westphal <fw@strlen.de>
3 Date: Thu, 7 Dec 2017 16:28:24 +0100
4 Subject: [PATCH 06/11] netfilter: reduce hook array sizes to what is needed
5
6 Not all families share the same hook count, adjust sizes to what is
7 needed.
8
9 struct net before:
10 /* size: 6592, cachelines: 103, members: 46 */
11 after:
12 /* size: 5952, cachelines: 93, members: 46 */
13
14 Signed-off-by: Florian Westphal <fw@strlen.de>
15 Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
16 ---
17 include/net/netns/netfilter.h | 10 +++++-----
18 net/netfilter/core.c | 24 +++++++++++++++++-------
19 2 files changed, 22 insertions(+), 12 deletions(-)
20
21 --- a/include/net/netns/netfilter.h
22 +++ b/include/net/netns/netfilter.h
23 @@ -17,11 +17,11 @@ struct netns_nf {
24 #ifdef CONFIG_SYSCTL
25 struct ctl_table_header *nf_log_dir_header;
26 #endif
27 - struct nf_hook_entries __rcu *hooks_ipv4[NF_MAX_HOOKS];
28 - struct nf_hook_entries __rcu *hooks_ipv6[NF_MAX_HOOKS];
29 - struct nf_hook_entries __rcu *hooks_arp[NF_MAX_HOOKS];
30 - struct nf_hook_entries __rcu *hooks_bridge[NF_MAX_HOOKS];
31 - struct nf_hook_entries __rcu *hooks_decnet[NF_MAX_HOOKS];
32 + struct nf_hook_entries __rcu *hooks_ipv4[NF_INET_NUMHOOKS];
33 + struct nf_hook_entries __rcu *hooks_ipv6[NF_INET_NUMHOOKS];
34 + struct nf_hook_entries __rcu *hooks_arp[NF_ARP_NUMHOOKS];
35 + struct nf_hook_entries __rcu *hooks_bridge[NF_INET_NUMHOOKS];
36 + struct nf_hook_entries __rcu *hooks_decnet[NF_DN_NUMHOOKS];
37 #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
38 bool defrag_ipv4;
39 #endif
40 --- a/net/netfilter/core.c
41 +++ b/net/netfilter/core.c
42 @@ -268,14 +268,24 @@ static struct nf_hook_entries __rcu **nf
43 case NFPROTO_NETDEV:
44 break;
45 case NFPROTO_ARP:
46 + if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_arp) <= reg->hooknum))
47 + return NULL;
48 return net->nf.hooks_arp + reg->hooknum;
49 case NFPROTO_BRIDGE:
50 + if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_bridge) <= reg->hooknum))
51 + return NULL;
52 return net->nf.hooks_bridge + reg->hooknum;
53 case NFPROTO_IPV4:
54 + if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv4) <= reg->hooknum))
55 + return NULL;
56 return net->nf.hooks_ipv4 + reg->hooknum;
57 case NFPROTO_IPV6:
58 + if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_ipv6) <= reg->hooknum))
59 + return NULL;
60 return net->nf.hooks_ipv6 + reg->hooknum;
61 case NFPROTO_DECNET:
62 + if (WARN_ON_ONCE(ARRAY_SIZE(net->nf.hooks_decnet) <= reg->hooknum))
63 + return NULL;
64 return net->nf.hooks_decnet + reg->hooknum;
65 default:
66 WARN_ON_ONCE(1);
67 @@ -549,21 +559,21 @@ void (*nf_nat_decode_session_hook)(struc
68 EXPORT_SYMBOL(nf_nat_decode_session_hook);
69 #endif
70
71 -static void __net_init __netfilter_net_init(struct nf_hook_entries *e[NF_MAX_HOOKS])
72 +static void __net_init __netfilter_net_init(struct nf_hook_entries **e, int max)
73 {
74 int h;
75
76 - for (h = 0; h < NF_MAX_HOOKS; h++)
77 + for (h = 0; h < max; h++)
78 RCU_INIT_POINTER(e[h], NULL);
79 }
80
81 static int __net_init netfilter_net_init(struct net *net)
82 {
83 - __netfilter_net_init(net->nf.hooks_ipv4);
84 - __netfilter_net_init(net->nf.hooks_ipv6);
85 - __netfilter_net_init(net->nf.hooks_arp);
86 - __netfilter_net_init(net->nf.hooks_bridge);
87 - __netfilter_net_init(net->nf.hooks_decnet);
88 + __netfilter_net_init(net->nf.hooks_ipv4, ARRAY_SIZE(net->nf.hooks_ipv4));
89 + __netfilter_net_init(net->nf.hooks_ipv6, ARRAY_SIZE(net->nf.hooks_ipv6));
90 + __netfilter_net_init(net->nf.hooks_arp, ARRAY_SIZE(net->nf.hooks_arp));
91 + __netfilter_net_init(net->nf.hooks_bridge, ARRAY_SIZE(net->nf.hooks_bridge));
92 + __netfilter_net_init(net->nf.hooks_decnet, ARRAY_SIZE(net->nf.hooks_decnet));
93
94 #ifdef CONFIG_PROC_FS
95 net->nf.proc_netfilter = proc_net_mkdir(net, "netfilter",