kernel: describe bridge patch "optimize netfilter hooks"
[openwrt/openwrt.git] / target / linux / generic / patches-4.1 / 644-bridge_optimize_netfilter_hooks.patch
1 From: Felix Fietkau <nbd@openwrt.org>
2 Subject: [PATCH] bridge: optimize netfilter hooks
3
4 Bypass all netfilter hooks if the sysctls for that functionality have
5 been disabled - eliminates the overhead of enabling
6 CONFIG_BRIDGE_NETFILTER in the kernel config
7 ---
8 --- a/net/bridge/br_forward.c
9 +++ b/net/bridge/br_forward.c
10 @@ -51,7 +51,7 @@ EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit
11
12 int br_forward_finish(struct sock *sk, struct sk_buff *skb)
13 {
14 - return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
15 + return BR_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
16 NULL, skb->dev,
17 br_dev_queue_push_xmit);
18
19 @@ -76,7 +76,7 @@ static void __br_deliver(const struct ne
20 return;
21 }
22
23 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
24 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
25 NULL, skb->dev,
26 br_forward_finish);
27 }
28 @@ -98,7 +98,7 @@ static void __br_forward(const struct ne
29 skb->dev = to->dev;
30 skb_forward_csum(skb);
31
32 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
33 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
34 indev, skb->dev,
35 br_forward_finish);
36 }
37 --- a/net/bridge/br_input.c
38 +++ b/net/bridge/br_input.c
39 @@ -55,7 +55,7 @@ static int br_pass_frame_up(struct sk_bu
40 if (!skb)
41 return NET_RX_DROP;
42
43 - return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
44 + return BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
45 indev, NULL,
46 netif_receive_skb_sk);
47 }
48 @@ -284,7 +284,7 @@ rx_handler_result_t br_handle_frame(stru
49 }
50
51 /* Deliver packet to local host only */
52 - if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
53 + if (BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
54 skb->dev, NULL, br_handle_local_finish)) {
55 return RX_HANDLER_CONSUMED; /* consumed by filter */
56 } else {
57 @@ -299,7 +299,7 @@ forward:
58 if (ether_addr_equal(p->br->dev->dev_addr, dest))
59 skb->pkt_type = PACKET_HOST;
60
61 - if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb, skb->dev, NULL,
62 + if (BR_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb, skb->dev, NULL,
63 br_handle_local_finish))
64 break;
65
66 @@ -321,7 +321,7 @@ forward:
67 if (ether_addr_equal(p->br->dev->dev_addr, dest))
68 skb->pkt_type = PACKET_HOST;
69
70 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb,
71 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb,
72 skb->dev, NULL,
73 br_handle_frame_finish);
74 break;
75 --- a/net/bridge/br_multicast.c
76 +++ b/net/bridge/br_multicast.c
77 @@ -814,7 +814,7 @@ static void __br_multicast_send_query(st
78
79 if (port) {
80 skb->dev = port->dev;
81 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
82 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
83 NULL, skb->dev,
84 br_dev_queue_push_xmit);
85 } else {
86 --- a/net/bridge/br_netfilter.c
87 +++ b/net/bridge/br_netfilter.c
88 @@ -69,6 +69,15 @@ static int brnf_pass_vlan_indev __read_m
89 #define IS_ARP(skb) \
90 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
91
92 +int brnf_call_ebtables __read_mostly = 0;
93 +EXPORT_SYMBOL_GPL(brnf_call_ebtables);
94 +
95 +bool br_netfilter_run_hooks(void)
96 +{
97 + return brnf_call_iptables | brnf_call_ip6tables | brnf_call_arptables |
98 + brnf_call_ebtables;
99 +}
100 +
101 static inline __be16 vlan_proto(const struct sk_buff *skb)
102 {
103 if (skb_vlan_tag_present(skb))
104 --- a/net/bridge/br_private.h
105 +++ b/net/bridge/br_private.h
106 @@ -779,6 +779,24 @@ static inline void br_nf_core_fini(void)
107 #define br_netfilter_rtable_init(x)
108 #endif
109
110 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
111 +extern int brnf_call_ebtables;
112 +bool br_netfilter_run_hooks(void);
113 +#else
114 +#define br_netfilter_run_hooks() false
115 +#endif
116 +
117 +static inline int
118 +BR_HOOK(uint8_t pf, unsigned int hook, struct sock *sk, struct sk_buff *skb,
119 + struct net_device *in, struct net_device *out,
120 + int (*okfn)(struct sock *, struct sk_buff *))
121 +{
122 + if (!br_netfilter_run_hooks())
123 + return okfn(sk, skb);
124 +
125 + return NF_HOOK(pf, hook, sk, skb, in, out, okfn);
126 +}
127 +
128 /* br_stp.c */
129 void br_log_state(const struct net_bridge_port *p);
130 void br_set_state(struct net_bridge_port *p, unsigned int state);
131 --- a/net/bridge/br_stp_bpdu.c
132 +++ b/net/bridge/br_stp_bpdu.c
133 @@ -54,7 +54,7 @@ static void br_send_bpdu(struct net_brid
134
135 skb_reset_mac_header(skb);
136
137 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
138 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
139 NULL, skb->dev,
140 dev_queue_xmit_sk);
141 }
142 --- a/net/bridge/netfilter/ebtables.c
143 +++ b/net/bridge/netfilter/ebtables.c
144 @@ -2414,11 +2414,19 @@ static int __init ebtables_init(void)
145 }
146
147 printk(KERN_INFO "Ebtables v2.0 registered\n");
148 +
149 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
150 + brnf_call_ebtables = 1;
151 +#endif
152 +
153 return 0;
154 }
155
156 static void __exit ebtables_fini(void)
157 {
158 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
159 + brnf_call_ebtables = 0;
160 +#endif
161 nf_unregister_sockopt(&ebt_sockopts);
162 xt_unregister_target(&ebt_standard_target);
163 printk(KERN_INFO "Ebtables v2.0 unregistered\n");
164 --- a/net/Kconfig
165 +++ b/net/Kconfig
166 @@ -177,11 +177,11 @@ config NETFILTER_ADVANCED
167 If unsure, say Y.
168
169 config BRIDGE_NETFILTER
170 - tristate "Bridged IP/ARP packets filtering"
171 + bool "Bridged IP/ARP packets filtering"
172 depends on BRIDGE
173 depends on NETFILTER && INET
174 depends on NETFILTER_ADVANCED
175 - default m
176 + default y
177 ---help---
178 Enabling this option will let arptables resp. iptables see bridged
179 ARP resp. IP traffic. If you want a bridging firewall, you probably