1 From: Felix Fietkau <nbd@openwrt.org>
2 Subject: [PATCH] bridge: optimize netfilter hooks
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
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
12 int br_forward_finish(struct sock *sk, struct sk_buff *skb)
14 - return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
15 + return BR_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
17 br_dev_queue_push_xmit);
19 @@ -76,7 +76,7 @@ static void __br_deliver(const struct ne
23 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
24 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
28 @@ -98,7 +98,7 @@ static void __br_forward(const struct ne
30 skb_forward_csum(skb);
32 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
33 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
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
43 - return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
44 + return BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
46 netif_receive_skb_sk);
48 @@ -284,7 +284,7 @@ rx_handler_result_t br_handle_frame(stru
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 */
57 @@ -299,7 +299,7 @@ forward:
58 if (ether_addr_equal(p->br->dev->dev_addr, dest))
59 skb->pkt_type = PACKET_HOST;
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))
66 @@ -321,7 +321,7 @@ forward:
67 if (ether_addr_equal(p->br->dev->dev_addr, dest))
68 skb->pkt_type = PACKET_HOST;
70 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb,
71 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb,
73 br_handle_frame_finish);
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
81 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
82 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
84 br_dev_queue_push_xmit);
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
90 (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP))
92 +int brnf_call_ebtables __read_mostly = 0;
93 +EXPORT_SYMBOL_GPL(brnf_call_ebtables);
95 +bool br_netfilter_run_hooks(void)
97 + return brnf_call_iptables | brnf_call_ip6tables | brnf_call_arptables |
101 static inline __be16 vlan_proto(const struct sk_buff *skb)
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)
110 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
111 +extern int brnf_call_ebtables;
112 +bool br_netfilter_run_hooks(void);
114 +#define br_netfilter_run_hooks() false
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 *))
122 + if (!br_netfilter_run_hooks())
123 + return okfn(sk, skb);
125 + return NF_HOOK(pf, hook, sk, skb, in, out, okfn);
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
135 skb_reset_mac_header(skb);
137 - NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
138 + BR_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
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)
147 printk(KERN_INFO "Ebtables v2.0 registered\n");
149 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
150 + brnf_call_ebtables = 1;
156 static void __exit ebtables_fini(void)
158 +#if IS_BUILTIN(CONFIG_BRIDGE_NETFILTER)
159 + brnf_call_ebtables = 0;
161 nf_unregister_sockopt(&ebt_sockopts);
162 xt_unregister_target(&ebt_standard_target);
163 printk(KERN_INFO "Ebtables v2.0 unregistered\n");
166 @@ -177,11 +177,11 @@ config NETFILTER_ADVANCED
169 config BRIDGE_NETFILTER
170 - tristate "Bridged IP/ARP packets filtering"
171 + bool "Bridged IP/ARP packets filtering"
173 depends on NETFILTER && INET
174 depends on NETFILTER_ADVANCED
178 Enabling this option will let arptables resp. iptables see bridged
179 ARP resp. IP traffic. If you want a bridging firewall, you probably