Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Handle firewalling | |
3 | * Linux ethernet bridge | |
4 | * | |
5 | * Authors: | |
8237908e BDS |
6 | * Lennert Buytenhek <buytenh@gnu.org> |
7 | * Bart De Schuymer <bdschuym@pandora.be> | |
1da177e4 LT |
8 | * |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License | |
11 | * as published by the Free Software Foundation; either version | |
12 | * 2 of the License, or (at your option) any later version. | |
13 | * | |
14 | * Lennert dedicates this file to Kerstin Wurdinger. | |
15 | */ | |
16 | ||
17 | #include <linux/module.h> | |
18 | #include <linux/kernel.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
1da177e4 LT |
20 | #include <linux/ip.h> |
21 | #include <linux/netdevice.h> | |
22 | #include <linux/skbuff.h> | |
14c85021 | 23 | #include <linux/if_arp.h> |
1da177e4 LT |
24 | #include <linux/if_ether.h> |
25 | #include <linux/if_vlan.h> | |
516299d2 MM |
26 | #include <linux/if_pppox.h> |
27 | #include <linux/ppp_defs.h> | |
1da177e4 LT |
28 | #include <linux/netfilter_bridge.h> |
29 | #include <linux/netfilter_ipv4.h> | |
30 | #include <linux/netfilter_ipv6.h> | |
31 | #include <linux/netfilter_arp.h> | |
32 | #include <linux/in_route.h> | |
f216f082 | 33 | #include <linux/inetdevice.h> |
14c85021 | 34 | |
1da177e4 LT |
35 | #include <net/ip.h> |
36 | #include <net/ipv6.h> | |
efb6de9b | 37 | #include <net/addrconf.h> |
14c85021 | 38 | #include <net/route.h> |
56768644 | 39 | #include <net/netfilter/br_netfilter.h> |
14c85021 | 40 | |
1da177e4 | 41 | #include <asm/uaccess.h> |
1da177e4 LT |
42 | #include "br_private.h" |
43 | #ifdef CONFIG_SYSCTL | |
44 | #include <linux/sysctl.h> | |
45 | #endif | |
46 | ||
1da177e4 LT |
47 | #ifdef CONFIG_SYSCTL |
48 | static struct ctl_table_header *brnf_sysctl_header; | |
9c1ea148 BH |
49 | static int brnf_call_iptables __read_mostly = 1; |
50 | static int brnf_call_ip6tables __read_mostly = 1; | |
51 | static int brnf_call_arptables __read_mostly = 1; | |
f4b3eee7 BT |
52 | static int brnf_filter_vlan_tagged __read_mostly; |
53 | static int brnf_filter_pppoe_tagged __read_mostly; | |
54 | static int brnf_pass_vlan_indev __read_mostly; | |
1da177e4 | 55 | #else |
4df53d8b PM |
56 | #define brnf_call_iptables 1 |
57 | #define brnf_call_ip6tables 1 | |
58 | #define brnf_call_arptables 1 | |
47e0e1ca HX |
59 | #define brnf_filter_vlan_tagged 0 |
60 | #define brnf_filter_pppoe_tagged 0 | |
4981682c | 61 | #define brnf_pass_vlan_indev 0 |
1da177e4 LT |
62 | #endif |
63 | ||
739e4505 | 64 | #define IS_IP(skb) \ |
df8a39de | 65 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IP)) |
739e4505 FW |
66 | |
67 | #define IS_IPV6(skb) \ | |
df8a39de | 68 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_IPV6)) |
739e4505 FW |
69 | |
70 | #define IS_ARP(skb) \ | |
df8a39de | 71 | (!skb_vlan_tag_present(skb) && skb->protocol == htons(ETH_P_ARP)) |
739e4505 | 72 | |
b6f99a21 | 73 | static inline __be16 vlan_proto(const struct sk_buff *skb) |
8b42ec39 | 74 | { |
df8a39de | 75 | if (skb_vlan_tag_present(skb)) |
13937911 JG |
76 | return skb->protocol; |
77 | else if (skb->protocol == htons(ETH_P_8021Q)) | |
78 | return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; | |
79 | else | |
80 | return 0; | |
8b42ec39 SH |
81 | } |
82 | ||
83 | #define IS_VLAN_IP(skb) \ | |
13937911 | 84 | (vlan_proto(skb) == htons(ETH_P_IP) && \ |
8b42ec39 SH |
85 | brnf_filter_vlan_tagged) |
86 | ||
87 | #define IS_VLAN_IPV6(skb) \ | |
13937911 | 88 | (vlan_proto(skb) == htons(ETH_P_IPV6) && \ |
8b42ec39 SH |
89 | brnf_filter_vlan_tagged) |
90 | ||
91 | #define IS_VLAN_ARP(skb) \ | |
13937911 | 92 | (vlan_proto(skb) == htons(ETH_P_ARP) && \ |
8b42ec39 | 93 | brnf_filter_vlan_tagged) |
1da177e4 | 94 | |
516299d2 MM |
95 | static inline __be16 pppoe_proto(const struct sk_buff *skb) |
96 | { | |
97 | return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN + | |
98 | sizeof(struct pppoe_hdr))); | |
99 | } | |
100 | ||
101 | #define IS_PPPOE_IP(skb) \ | |
102 | (skb->protocol == htons(ETH_P_PPP_SES) && \ | |
103 | pppoe_proto(skb) == htons(PPP_IP) && \ | |
104 | brnf_filter_pppoe_tagged) | |
105 | ||
106 | #define IS_PPPOE_IPV6(skb) \ | |
107 | (skb->protocol == htons(ETH_P_PPP_SES) && \ | |
108 | pppoe_proto(skb) == htons(PPP_IPV6) && \ | |
109 | brnf_filter_pppoe_tagged) | |
110 | ||
e70deecb FW |
111 | /* largest possible L2 header, see br_nf_dev_queue_xmit() */ |
112 | #define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN) | |
113 | ||
a1bc1b35 | 114 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
e70deecb FW |
115 | struct brnf_frag_data { |
116 | char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH]; | |
117 | u8 encap_size; | |
118 | u8 size; | |
d7b59742 FW |
119 | u16 vlan_tci; |
120 | __be16 vlan_proto; | |
e70deecb FW |
121 | }; |
122 | ||
123 | static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage); | |
124 | #endif | |
125 | ||
a9fcc6a4 FW |
126 | static void nf_bridge_info_free(struct sk_buff *skb) |
127 | { | |
128 | if (skb->nf_bridge) { | |
129 | nf_bridge_put(skb->nf_bridge); | |
130 | skb->nf_bridge = NULL; | |
131 | } | |
132 | } | |
133 | ||
5dce971a SH |
134 | static inline struct net_device *bridge_parent(const struct net_device *dev) |
135 | { | |
b5ed54e9 | 136 | struct net_bridge_port *port; |
5dce971a | 137 | |
b5ed54e9 | 138 | port = br_port_get_rcu(dev); |
139 | return port ? port->br->dev : NULL; | |
5dce971a | 140 | } |
1da177e4 | 141 | |
2dc2f207 PM |
142 | static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb) |
143 | { | |
144 | struct nf_bridge_info *nf_bridge = skb->nf_bridge; | |
145 | ||
146 | if (atomic_read(&nf_bridge->use) > 1) { | |
147 | struct nf_bridge_info *tmp = nf_bridge_alloc(skb); | |
148 | ||
149 | if (tmp) { | |
150 | memcpy(tmp, nf_bridge, sizeof(struct nf_bridge_info)); | |
151 | atomic_set(&tmp->use, 1); | |
2dc2f207 | 152 | } |
4c3a76ab | 153 | nf_bridge_put(nf_bridge); |
2dc2f207 PM |
154 | nf_bridge = tmp; |
155 | } | |
156 | return nf_bridge; | |
157 | } | |
158 | ||
230ac490 | 159 | unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb) |
8d045163 FW |
160 | { |
161 | switch (skb->protocol) { | |
162 | case __cpu_to_be16(ETH_P_8021Q): | |
163 | return VLAN_HLEN; | |
164 | case __cpu_to_be16(ETH_P_PPP_SES): | |
165 | return PPPOE_SES_HLEN; | |
166 | default: | |
167 | return 0; | |
168 | } | |
169 | } | |
170 | ||
fc38582d | 171 | static inline void nf_bridge_pull_encap_header(struct sk_buff *skb) |
fdeabdef | 172 | { |
fc38582d PM |
173 | unsigned int len = nf_bridge_encap_header_len(skb); |
174 | ||
175 | skb_pull(skb, len); | |
176 | skb->network_header += len; | |
177 | } | |
fdeabdef | 178 | |
fc38582d PM |
179 | static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb) |
180 | { | |
181 | unsigned int len = nf_bridge_encap_header_len(skb); | |
182 | ||
183 | skb_pull_rcsum(skb, len); | |
184 | skb->network_header += len; | |
185 | } | |
186 | ||
462fb2af BD |
187 | /* When handing a packet over to the IP layer |
188 | * check whether we have a skb that is in the | |
189 | * expected format | |
190 | */ | |
191 | ||
c1444c63 | 192 | static int br_validate_ipv4(struct net *net, struct sk_buff *skb) |
462fb2af | 193 | { |
b71d1d42 | 194 | const struct iphdr *iph; |
462fb2af BD |
195 | u32 len; |
196 | ||
6caab7b0 SB |
197 | if (!pskb_may_pull(skb, sizeof(struct iphdr))) |
198 | goto inhdr_error; | |
199 | ||
462fb2af | 200 | iph = ip_hdr(skb); |
462fb2af BD |
201 | |
202 | /* Basic sanity checks */ | |
203 | if (iph->ihl < 5 || iph->version != 4) | |
204 | goto inhdr_error; | |
205 | ||
206 | if (!pskb_may_pull(skb, iph->ihl*4)) | |
207 | goto inhdr_error; | |
208 | ||
209 | iph = ip_hdr(skb); | |
210 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) | |
211 | goto inhdr_error; | |
212 | ||
213 | len = ntohs(iph->tot_len); | |
214 | if (skb->len < len) { | |
c1444c63 | 215 | IP_INC_STATS_BH(net, IPSTATS_MIB_INTRUNCATEDPKTS); |
462fb2af BD |
216 | goto drop; |
217 | } else if (len < (iph->ihl*4)) | |
218 | goto inhdr_error; | |
219 | ||
220 | if (pskb_trim_rcsum(skb, len)) { | |
c1444c63 | 221 | IP_INC_STATS_BH(net, IPSTATS_MIB_INDISCARDS); |
462fb2af BD |
222 | goto drop; |
223 | } | |
224 | ||
f8e9881c | 225 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
7677e868 HX |
226 | /* We should really parse IP options here but until |
227 | * somebody who actually uses IP options complains to | |
228 | * us we'll just silently ignore the options because | |
229 | * we're lazy! | |
230 | */ | |
462fb2af BD |
231 | return 0; |
232 | ||
233 | inhdr_error: | |
c1444c63 | 234 | IP_INC_STATS_BH(net, IPSTATS_MIB_INHDRERRORS); |
462fb2af BD |
235 | drop: |
236 | return -1; | |
237 | } | |
238 | ||
230ac490 | 239 | void nf_bridge_update_protocol(struct sk_buff *skb) |
4a9d2f20 | 240 | { |
3eaf4025 FW |
241 | switch (skb->nf_bridge->orig_proto) { |
242 | case BRNF_PROTO_8021Q: | |
4a9d2f20 | 243 | skb->protocol = htons(ETH_P_8021Q); |
3eaf4025 FW |
244 | break; |
245 | case BRNF_PROTO_PPPOE: | |
4a9d2f20 | 246 | skb->protocol = htons(ETH_P_PPP_SES); |
3eaf4025 FW |
247 | break; |
248 | case BRNF_PROTO_UNCHANGED: | |
249 | break; | |
250 | } | |
4a9d2f20 FW |
251 | } |
252 | ||
e179e632 BDS |
253 | /* Obtain the correct destination MAC address, while preserving the original |
254 | * source MAC address. If we already know this address, we just copy it. If we | |
255 | * don't, we use the neighbour framework to find out. In both cases, we make | |
256 | * sure that br_handle_frame_finish() is called afterwards. | |
257 | */ | |
0c4b51f0 | 258 | int br_nf_pre_routing_finish_bridge(struct net *net, struct sock *sk, struct sk_buff *skb) |
e179e632 | 259 | { |
f6b72b62 | 260 | struct neighbour *neigh; |
e179e632 BDS |
261 | struct dst_entry *dst; |
262 | ||
263 | skb->dev = bridge_parent(skb->dev); | |
264 | if (!skb->dev) | |
265 | goto free_skb; | |
266 | dst = skb_dst(skb); | |
f9d75166 DM |
267 | neigh = dst_neigh_lookup_skb(dst, skb); |
268 | if (neigh) { | |
38330783 | 269 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
f9d75166 DM |
270 | int ret; |
271 | ||
272 | if (neigh->hh.hh_len) { | |
273 | neigh_hh_bridge(&neigh->hh, skb); | |
274 | skb->dev = nf_bridge->physindev; | |
0c4b51f0 | 275 | ret = br_handle_frame_finish(net, sk, skb); |
f9d75166 DM |
276 | } else { |
277 | /* the neighbour function below overwrites the complete | |
278 | * MAC header, so we save the Ethernet source address and | |
279 | * protocol number. | |
280 | */ | |
281 | skb_copy_from_linear_data_offset(skb, | |
282 | -(ETH_HLEN-ETH_ALEN), | |
e70deecb | 283 | nf_bridge->neigh_header, |
f9d75166 DM |
284 | ETH_HLEN-ETH_ALEN); |
285 | /* tell br_dev_xmit to continue with forwarding */ | |
72b1e5e4 | 286 | nf_bridge->bridged_dnat = 1; |
93fdd47e | 287 | /* FIXME Need to refragment */ |
f9d75166 DM |
288 | ret = neigh->output(neigh, skb); |
289 | } | |
290 | neigh_release(neigh); | |
291 | return ret; | |
e179e632 BDS |
292 | } |
293 | free_skb: | |
294 | kfree_skb(skb); | |
295 | return 0; | |
296 | } | |
297 | ||
230ac490 PNA |
298 | static inline bool |
299 | br_nf_ipv4_daddr_was_changed(const struct sk_buff *skb, | |
300 | const struct nf_bridge_info *nf_bridge) | |
8cae308d | 301 | { |
230ac490 | 302 | return ip_hdr(skb)->daddr != nf_bridge->ipv4_daddr; |
8cae308d BT |
303 | } |
304 | ||
1da177e4 | 305 | /* This requires some explaining. If DNAT has taken place, |
ea2d9b41 | 306 | * we will need to fix up the destination Ethernet address. |
faecbb45 | 307 | * This is also true when SNAT takes place (for the reply direction). |
1da177e4 LT |
308 | * |
309 | * There are two cases to consider: | |
310 | * 1. The packet was DNAT'ed to a device in the same bridge | |
311 | * port group as it was received on. We can still bridge | |
312 | * the packet. | |
313 | * 2. The packet was DNAT'ed to a different device, either | |
314 | * a non-bridged device or another bridge port group. | |
315 | * The packet will need to be routed. | |
316 | * | |
317 | * The correct way of distinguishing between these two cases is to | |
318 | * call ip_route_input() and to look at skb->dst->dev, which is | |
319 | * changed to the destination device if ip_route_input() succeeds. | |
320 | * | |
ea2d9b41 | 321 | * Let's first consider the case that ip_route_input() succeeds: |
1da177e4 | 322 | * |
ea2d9b41 BDS |
323 | * If the output device equals the logical bridge device the packet |
324 | * came in on, we can consider this bridging. The corresponding MAC | |
325 | * address will be obtained in br_nf_pre_routing_finish_bridge. | |
1da177e4 LT |
326 | * Otherwise, the packet is considered to be routed and we just |
327 | * change the destination MAC address so that the packet will | |
f216f082 BDS |
328 | * later be passed up to the IP stack to be routed. For a redirected |
329 | * packet, ip_route_input() will give back the localhost as output device, | |
330 | * which differs from the bridge device. | |
1da177e4 | 331 | * |
ea2d9b41 | 332 | * Let's now consider the case that ip_route_input() fails: |
1da177e4 | 333 | * |
f216f082 BDS |
334 | * This can be because the destination address is martian, in which case |
335 | * the packet will be dropped. | |
ea2d9b41 BDS |
336 | * If IP forwarding is disabled, ip_route_input() will fail, while |
337 | * ip_route_output_key() can return success. The source | |
338 | * address for ip_route_output_key() is set to zero, so ip_route_output_key() | |
1da177e4 | 339 | * thinks we're handling a locally generated packet and won't care |
ea2d9b41 BDS |
340 | * if IP forwarding is enabled. If the output device equals the logical bridge |
341 | * device, we proceed as if ip_route_input() succeeded. If it differs from the | |
342 | * logical bridge port or if ip_route_output_key() fails we drop the packet. | |
343 | */ | |
0c4b51f0 | 344 | static int br_nf_pre_routing_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 LT |
345 | { |
346 | struct net_device *dev = skb->dev; | |
eddc9ec5 | 347 | struct iphdr *iph = ip_hdr(skb); |
38330783 | 348 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
511c3f92 | 349 | struct rtable *rt; |
f216f082 | 350 | int err; |
93fdd47e | 351 | |
411ffb4f | 352 | nf_bridge->frag_max_size = IPCB(skb)->frag_max_size; |
1da177e4 | 353 | |
a1e67951 | 354 | if (nf_bridge->pkt_otherhost) { |
1da177e4 | 355 | skb->pkt_type = PACKET_OTHERHOST; |
a1e67951 | 356 | nf_bridge->pkt_otherhost = false; |
1da177e4 | 357 | } |
72b1e5e4 | 358 | nf_bridge->in_prerouting = 0; |
230ac490 | 359 | if (br_nf_ipv4_daddr_was_changed(skb, nf_bridge)) { |
f216f082 | 360 | if ((err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev))) { |
f3abc9b9 | 361 | struct in_device *in_dev = __in_dev_get_rcu(dev); |
f216f082 BDS |
362 | |
363 | /* If err equals -EHOSTUNREACH the error is due to a | |
364 | * martian destination or due to the fact that | |
365 | * forwarding is disabled. For most martian packets, | |
366 | * ip_route_output_key() will fail. It won't fail for 2 types of | |
367 | * martian destinations: loopback destinations and destination | |
368 | * 0.0.0.0. In both cases the packet will be dropped because the | |
369 | * destination is the loopback device and not the bridge. */ | |
370 | if (err != -EHOSTUNREACH || !in_dev || IN_DEV_FORWARD(in_dev)) | |
371 | goto free_skb; | |
1da177e4 | 372 | |
f2d74cf8 | 373 | rt = ip_route_output(net, iph->daddr, 0, |
78fbfd8a | 374 | RT_TOS(iph->tos), 0); |
b23dd4fe | 375 | if (!IS_ERR(rt)) { |
1c011bed | 376 | /* - Bridged-and-DNAT'ed traffic doesn't |
f216f082 | 377 | * require ip_forwarding. */ |
b23dd4fe DM |
378 | if (rt->dst.dev == dev) { |
379 | skb_dst_set(skb, &rt->dst); | |
1da177e4 LT |
380 | goto bridged_dnat; |
381 | } | |
b23dd4fe | 382 | ip_rt_put(rt); |
1da177e4 | 383 | } |
f216f082 | 384 | free_skb: |
1da177e4 LT |
385 | kfree_skb(skb); |
386 | return 0; | |
387 | } else { | |
adf30907 | 388 | if (skb_dst(skb)->dev == dev) { |
1da177e4 | 389 | bridged_dnat: |
1da177e4 | 390 | skb->dev = nf_bridge->physindev; |
e179e632 | 391 | nf_bridge_update_protocol(skb); |
fc38582d | 392 | nf_bridge_push_encap_header(skb); |
713aefa3 JE |
393 | NF_HOOK_THRESH(NFPROTO_BRIDGE, |
394 | NF_BR_PRE_ROUTING, | |
29a26a56 | 395 | net, sk, skb, skb->dev, NULL, |
1da177e4 LT |
396 | br_nf_pre_routing_finish_bridge, |
397 | 1); | |
398 | return 0; | |
399 | } | |
04091142 | 400 | ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr); |
1da177e4 LT |
401 | skb->pkt_type = PACKET_HOST; |
402 | } | |
403 | } else { | |
511c3f92 ED |
404 | rt = bridge_parent_rtable(nf_bridge->physindev); |
405 | if (!rt) { | |
4adf0af6 SW |
406 | kfree_skb(skb); |
407 | return 0; | |
408 | } | |
f9181f4f | 409 | skb_dst_set_noref(skb, &rt->dst); |
1da177e4 LT |
410 | } |
411 | ||
412 | skb->dev = nf_bridge->physindev; | |
e179e632 | 413 | nf_bridge_update_protocol(skb); |
fc38582d | 414 | nf_bridge_push_encap_header(skb); |
29a26a56 | 415 | NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, net, sk, skb, |
7026b1dd | 416 | skb->dev, NULL, |
1da177e4 LT |
417 | br_handle_frame_finish, 1); |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
4981682c PNA |
422 | static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct net_device *dev) |
423 | { | |
424 | struct net_device *vlan, *br; | |
425 | ||
426 | br = bridge_parent(dev); | |
df8a39de | 427 | if (brnf_pass_vlan_indev == 0 || !skb_vlan_tag_present(skb)) |
4981682c PNA |
428 | return br; |
429 | ||
f06c7f9f | 430 | vlan = __vlan_find_dev_deep_rcu(br, skb->vlan_proto, |
df8a39de | 431 | skb_vlan_tag_get(skb) & VLAN_VID_MASK); |
4981682c PNA |
432 | |
433 | return vlan ? vlan : br; | |
434 | } | |
435 | ||
1da177e4 | 436 | /* Some common code for IPv4/IPv6 */ |
230ac490 | 437 | struct net_device *setup_pre_routing(struct sk_buff *skb) |
1da177e4 | 438 | { |
38330783 | 439 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
1da177e4 LT |
440 | |
441 | if (skb->pkt_type == PACKET_OTHERHOST) { | |
442 | skb->pkt_type = PACKET_HOST; | |
a1e67951 | 443 | nf_bridge->pkt_otherhost = true; |
1da177e4 LT |
444 | } |
445 | ||
72b1e5e4 | 446 | nf_bridge->in_prerouting = 1; |
1da177e4 | 447 | nf_bridge->physindev = skb->dev; |
4981682c | 448 | skb->dev = brnf_get_logical_dev(skb, skb->dev); |
3eaf4025 | 449 | |
e179e632 | 450 | if (skb->protocol == htons(ETH_P_8021Q)) |
3eaf4025 | 451 | nf_bridge->orig_proto = BRNF_PROTO_8021Q; |
e179e632 | 452 | else if (skb->protocol == htons(ETH_P_PPP_SES)) |
3eaf4025 | 453 | nf_bridge->orig_proto = BRNF_PROTO_PPPOE; |
5dce971a | 454 | |
6b8dbcf2 FW |
455 | /* Must drop socket now because of tproxy. */ |
456 | skb_orphan(skb); | |
5dce971a | 457 | return skb->dev; |
1da177e4 LT |
458 | } |
459 | ||
1da177e4 LT |
460 | /* Direct IPv6 traffic to br_nf_pre_routing_ipv6. |
461 | * Replicate the checks that IPv4 does on packet reception. | |
462 | * Set skb->dev to the bridge device (i.e. parent of the | |
463 | * receiving device) to make netfilter happy, the REDIRECT | |
464 | * target in particular. Save the original destination IP | |
465 | * address to be able to detect DNAT afterwards. */ | |
06198b34 | 466 | static unsigned int br_nf_pre_routing(void *priv, |
795aa6ef | 467 | struct sk_buff *skb, |
238e54c9 | 468 | const struct nf_hook_state *state) |
1da177e4 | 469 | { |
faecbb45 | 470 | struct nf_bridge_info *nf_bridge; |
4df53d8b PM |
471 | struct net_bridge_port *p; |
472 | struct net_bridge *br; | |
e7c243c9 EP |
473 | __u32 len = nf_bridge_encap_header_len(skb); |
474 | ||
e7c243c9 | 475 | if (unlikely(!pskb_may_pull(skb, len))) |
deef4b52 | 476 | return NF_DROP; |
1da177e4 | 477 | |
238e54c9 | 478 | p = br_port_get_rcu(state->in); |
4df53d8b | 479 | if (p == NULL) |
deef4b52 | 480 | return NF_DROP; |
4df53d8b PM |
481 | br = p->br; |
482 | ||
739e4505 | 483 | if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) { |
4df53d8b | 484 | if (!brnf_call_ip6tables && !br->nf_call_ip6tables) |
1da177e4 | 485 | return NF_ACCEPT; |
4df53d8b | 486 | |
fc38582d | 487 | nf_bridge_pull_encap_header_rcsum(skb); |
06198b34 | 488 | return br_nf_pre_routing_ipv6(priv, skb, state); |
1da177e4 | 489 | } |
4df53d8b PM |
490 | |
491 | if (!brnf_call_iptables && !br->nf_call_iptables) | |
1da177e4 | 492 | return NF_ACCEPT; |
1da177e4 | 493 | |
739e4505 | 494 | if (!IS_IP(skb) && !IS_VLAN_IP(skb) && !IS_PPPOE_IP(skb)) |
1da177e4 LT |
495 | return NF_ACCEPT; |
496 | ||
fc38582d | 497 | nf_bridge_pull_encap_header_rcsum(skb); |
1da177e4 | 498 | |
c1444c63 | 499 | if (br_validate_ipv4(state->net, skb)) |
deef4b52 | 500 | return NF_DROP; |
17762060 | 501 | |
789bc3e5 | 502 | nf_bridge_put(skb->nf_bridge); |
fdeabdef | 503 | if (!nf_bridge_alloc(skb)) |
1da177e4 | 504 | return NF_DROP; |
5dce971a SH |
505 | if (!setup_pre_routing(skb)) |
506 | return NF_DROP; | |
c055d5b0 | 507 | |
faecbb45 FW |
508 | nf_bridge = nf_bridge_info_get(skb); |
509 | nf_bridge->ipv4_daddr = ip_hdr(skb)->daddr; | |
510 | ||
e179e632 | 511 | skb->protocol = htons(ETH_P_IP); |
1da177e4 | 512 | |
29a26a56 | 513 | NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->net, state->sk, skb, |
7026b1dd | 514 | skb->dev, NULL, |
1da177e4 LT |
515 | br_nf_pre_routing_finish); |
516 | ||
517 | return NF_STOLEN; | |
1da177e4 LT |
518 | } |
519 | ||
520 | ||
521 | /* PF_BRIDGE/LOCAL_IN ************************************************/ | |
522 | /* The packet is locally destined, which requires a real | |
523 | * dst_entry, so detach the fake one. On the way up, the | |
524 | * packet would pass through PRE_ROUTING again (which already | |
525 | * took place when the packet entered the bridge), but we | |
526 | * register an IPv4 PRE_ROUTING 'sabotage' hook that will | |
527 | * prevent this from happening. */ | |
06198b34 | 528 | static unsigned int br_nf_local_in(void *priv, |
795aa6ef | 529 | struct sk_buff *skb, |
238e54c9 | 530 | const struct nf_hook_state *state) |
1da177e4 | 531 | { |
a881e963 | 532 | br_drop_fake_rtable(skb); |
1da177e4 LT |
533 | return NF_ACCEPT; |
534 | } | |
535 | ||
1da177e4 | 536 | /* PF_BRIDGE/FORWARD *************************************************/ |
0c4b51f0 | 537 | static int br_nf_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
1da177e4 | 538 | { |
38330783 | 539 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
1da177e4 | 540 | struct net_device *in; |
1da177e4 | 541 | |
739e4505 | 542 | if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) { |
0b67c43c | 543 | |
efb6de9b | 544 | if (skb->protocol == htons(ETH_P_IP)) |
411ffb4f | 545 | nf_bridge->frag_max_size = IPCB(skb)->frag_max_size; |
efb6de9b BT |
546 | |
547 | if (skb->protocol == htons(ETH_P_IPV6)) | |
548 | nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size; | |
0b67c43c | 549 | |
1da177e4 | 550 | in = nf_bridge->physindev; |
a1e67951 | 551 | if (nf_bridge->pkt_otherhost) { |
1da177e4 | 552 | skb->pkt_type = PACKET_OTHERHOST; |
a1e67951 | 553 | nf_bridge->pkt_otherhost = false; |
1da177e4 | 554 | } |
e94c6743 | 555 | nf_bridge_update_protocol(skb); |
1da177e4 LT |
556 | } else { |
557 | in = *((struct net_device **)(skb->cb)); | |
558 | } | |
fc38582d | 559 | nf_bridge_push_encap_header(skb); |
e179e632 | 560 | |
29a26a56 | 561 | NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, net, sk, skb, |
7026b1dd | 562 | in, skb->dev, br_forward_finish, 1); |
1da177e4 LT |
563 | return 0; |
564 | } | |
565 | ||
739e4505 | 566 | |
1da177e4 LT |
567 | /* This is the 'purely bridged' case. For IP, we pass the packet to |
568 | * netfilter with indev and outdev set to the bridge device, | |
569 | * but we are still able to filter on the 'real' indev/outdev | |
570 | * because of the physdev module. For ARP, indev and outdev are the | |
571 | * bridge ports. */ | |
06198b34 | 572 | static unsigned int br_nf_forward_ip(void *priv, |
795aa6ef | 573 | struct sk_buff *skb, |
238e54c9 | 574 | const struct nf_hook_state *state) |
1da177e4 | 575 | { |
1da177e4 | 576 | struct nf_bridge_info *nf_bridge; |
5dce971a | 577 | struct net_device *parent; |
76108cea | 578 | u_int8_t pf; |
1da177e4 LT |
579 | |
580 | if (!skb->nf_bridge) | |
581 | return NF_ACCEPT; | |
582 | ||
2dc2f207 PM |
583 | /* Need exclusive nf_bridge_info since we might have multiple |
584 | * different physoutdevs. */ | |
585 | if (!nf_bridge_unshare(skb)) | |
586 | return NF_DROP; | |
587 | ||
38330783 FW |
588 | nf_bridge = nf_bridge_info_get(skb); |
589 | if (!nf_bridge) | |
590 | return NF_DROP; | |
591 | ||
238e54c9 | 592 | parent = bridge_parent(state->out); |
5dce971a SH |
593 | if (!parent) |
594 | return NF_DROP; | |
595 | ||
739e4505 | 596 | if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb)) |
aa740f46 | 597 | pf = NFPROTO_IPV4; |
739e4505 | 598 | else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) |
aa740f46 | 599 | pf = NFPROTO_IPV6; |
a2bd40ad HX |
600 | else |
601 | return NF_ACCEPT; | |
1da177e4 | 602 | |
3db05fea | 603 | nf_bridge_pull_encap_header(skb); |
1da177e4 | 604 | |
1da177e4 LT |
605 | if (skb->pkt_type == PACKET_OTHERHOST) { |
606 | skb->pkt_type = PACKET_HOST; | |
a1e67951 | 607 | nf_bridge->pkt_otherhost = true; |
1da177e4 LT |
608 | } |
609 | ||
0b67c43c | 610 | if (pf == NFPROTO_IPV4) { |
c1444c63 | 611 | if (br_validate_ipv4(state->net, skb)) |
0b67c43c | 612 | return NF_DROP; |
411ffb4f | 613 | IPCB(skb)->frag_max_size = nf_bridge->frag_max_size; |
0b67c43c | 614 | } |
6b1e960f | 615 | |
efb6de9b | 616 | if (pf == NFPROTO_IPV6) { |
c1444c63 | 617 | if (br_validate_ipv6(state->net, skb)) |
efb6de9b BT |
618 | return NF_DROP; |
619 | IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size; | |
620 | } | |
621 | ||
1da177e4 | 622 | nf_bridge->physoutdev = skb->dev; |
aa740f46 | 623 | if (pf == NFPROTO_IPV4) |
e179e632 BDS |
624 | skb->protocol = htons(ETH_P_IP); |
625 | else | |
626 | skb->protocol = htons(ETH_P_IPV6); | |
1da177e4 | 627 | |
29a26a56 | 628 | NF_HOOK(pf, NF_INET_FORWARD, state->net, NULL, skb, |
7026b1dd | 629 | brnf_get_logical_dev(skb, state->in), |
238e54c9 | 630 | parent, br_nf_forward_finish); |
1da177e4 LT |
631 | |
632 | return NF_STOLEN; | |
633 | } | |
634 | ||
06198b34 | 635 | static unsigned int br_nf_forward_arp(void *priv, |
795aa6ef | 636 | struct sk_buff *skb, |
238e54c9 | 637 | const struct nf_hook_state *state) |
1da177e4 | 638 | { |
4df53d8b PM |
639 | struct net_bridge_port *p; |
640 | struct net_bridge *br; | |
1da177e4 LT |
641 | struct net_device **d = (struct net_device **)(skb->cb); |
642 | ||
238e54c9 | 643 | p = br_port_get_rcu(state->out); |
4df53d8b PM |
644 | if (p == NULL) |
645 | return NF_ACCEPT; | |
646 | br = p->br; | |
647 | ||
648 | if (!brnf_call_arptables && !br->nf_call_arptables) | |
1da177e4 | 649 | return NF_ACCEPT; |
1da177e4 | 650 | |
739e4505 | 651 | if (!IS_ARP(skb)) { |
8b42ec39 | 652 | if (!IS_VLAN_ARP(skb)) |
1da177e4 | 653 | return NF_ACCEPT; |
3db05fea | 654 | nf_bridge_pull_encap_header(skb); |
1da177e4 LT |
655 | } |
656 | ||
d0a92be0 | 657 | if (arp_hdr(skb)->ar_pln != 4) { |
fc38582d | 658 | if (IS_VLAN_ARP(skb)) |
3db05fea | 659 | nf_bridge_push_encap_header(skb); |
1da177e4 LT |
660 | return NF_ACCEPT; |
661 | } | |
238e54c9 | 662 | *d = state->in; |
29a26a56 | 663 | NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->net, state->sk, skb, |
7026b1dd | 664 | state->in, state->out, br_nf_forward_finish); |
1da177e4 LT |
665 | |
666 | return NF_STOLEN; | |
667 | } | |
668 | ||
efb6de9b | 669 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) || IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
6532948b | 670 | static int br_nf_push_frag_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
8bd63cf1 | 671 | { |
e70deecb | 672 | struct brnf_frag_data *data; |
8bd63cf1 | 673 | int err; |
8bd63cf1 | 674 | |
e70deecb FW |
675 | data = this_cpu_ptr(&brnf_frag_data_storage); |
676 | err = skb_cow_head(skb, data->size); | |
8bd63cf1 | 677 | |
e70deecb | 678 | if (err) { |
8bd63cf1 FW |
679 | kfree_skb(skb); |
680 | return 0; | |
681 | } | |
682 | ||
d7b59742 FW |
683 | if (data->vlan_tci) { |
684 | skb->vlan_tci = data->vlan_tci; | |
685 | skb->vlan_proto = data->vlan_proto; | |
686 | } | |
687 | ||
e70deecb FW |
688 | skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size); |
689 | __skb_push(skb, data->encap_size); | |
690 | ||
a9fcc6a4 | 691 | nf_bridge_info_free(skb); |
0c4b51f0 | 692 | return br_dev_queue_push_xmit(net, sk, skb); |
8bd63cf1 | 693 | } |
6532948b EB |
694 | static int br_nf_push_frag_xmit_sk(struct sock *sk, struct sk_buff *skb) |
695 | { | |
696 | struct net *net = dev_net(skb_dst(skb)->dev); | |
697 | return br_nf_push_frag_xmit(net, sk, skb); | |
698 | } | |
efb6de9b | 699 | #endif |
8bd63cf1 | 700 | |
a1bc1b35 | 701 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) |
8d4df0b9 EB |
702 | static int |
703 | br_nf_ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb, | |
694869b3 | 704 | int (*output)(struct net *, struct sock *, struct sk_buff *)) |
49d16b23 AZ |
705 | { |
706 | unsigned int mtu = ip_skb_dst_mtu(skb); | |
707 | struct iphdr *iph = ip_hdr(skb); | |
49d16b23 AZ |
708 | |
709 | if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) || | |
710 | (IPCB(skb)->frag_max_size && | |
711 | IPCB(skb)->frag_max_size > mtu))) { | |
8d4df0b9 | 712 | IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); |
49d16b23 AZ |
713 | kfree_skb(skb); |
714 | return -EMSGSIZE; | |
715 | } | |
716 | ||
694869b3 | 717 | return ip_do_fragment(net, sk, skb, output); |
49d16b23 | 718 | } |
a1bc1b35 | 719 | #endif |
49d16b23 | 720 | |
33b1f313 FW |
721 | static unsigned int nf_bridge_mtu_reduction(const struct sk_buff *skb) |
722 | { | |
723 | if (skb->nf_bridge->orig_proto == BRNF_PROTO_PPPOE) | |
724 | return PPPOE_SES_HLEN; | |
725 | return 0; | |
726 | } | |
727 | ||
0c4b51f0 | 728 | static int br_nf_dev_queue_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
2e2f7aef | 729 | { |
411ffb4f | 730 | struct nf_bridge_info *nf_bridge; |
7a8d831d | 731 | unsigned int mtu_reserved; |
462fb2af | 732 | |
efb6de9b BT |
733 | mtu_reserved = nf_bridge_mtu_reduction(skb); |
734 | ||
735 | if (skb_is_gso(skb) || skb->len + mtu_reserved <= skb->dev->mtu) { | |
a9fcc6a4 | 736 | nf_bridge_info_free(skb); |
0c4b51f0 | 737 | return br_dev_queue_push_xmit(net, sk, skb); |
a9fcc6a4 | 738 | } |
7a8d831d | 739 | |
411ffb4f | 740 | nf_bridge = nf_bridge_info_get(skb); |
efb6de9b BT |
741 | |
742 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) | |
93fdd47e HX |
743 | /* This is wrong! We should preserve the original fragment |
744 | * boundaries by preserving frag_list rather than refragmenting. | |
745 | */ | |
efb6de9b | 746 | if (skb->protocol == htons(ETH_P_IP)) { |
e70deecb FW |
747 | struct brnf_frag_data *data; |
748 | ||
c1444c63 | 749 | if (br_validate_ipv4(net, skb)) |
dd302b59 | 750 | goto drop; |
411ffb4f BT |
751 | |
752 | IPCB(skb)->frag_max_size = nf_bridge->frag_max_size; | |
e70deecb FW |
753 | |
754 | nf_bridge_update_protocol(skb); | |
755 | ||
756 | data = this_cpu_ptr(&brnf_frag_data_storage); | |
d7b59742 FW |
757 | |
758 | data->vlan_tci = skb->vlan_tci; | |
759 | data->vlan_proto = skb->vlan_proto; | |
e70deecb FW |
760 | data->encap_size = nf_bridge_encap_header_len(skb); |
761 | data->size = ETH_HLEN + data->encap_size; | |
762 | ||
763 | skb_copy_from_linear_data_offset(skb, -data->size, data->mac, | |
764 | data->size); | |
765 | ||
694869b3 | 766 | return br_nf_ip_fragment(net, sk, skb, br_nf_push_frag_xmit); |
e70deecb | 767 | } |
efb6de9b BT |
768 | #endif |
769 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) | |
770 | if (skb->protocol == htons(ETH_P_IPV6)) { | |
771 | const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops(); | |
772 | struct brnf_frag_data *data; | |
462fb2af | 773 | |
c1444c63 | 774 | if (br_validate_ipv6(net, skb)) |
dd302b59 | 775 | goto drop; |
efb6de9b BT |
776 | |
777 | IP6CB(skb)->frag_max_size = nf_bridge->frag_max_size; | |
778 | ||
779 | nf_bridge_update_protocol(skb); | |
780 | ||
781 | data = this_cpu_ptr(&brnf_frag_data_storage); | |
782 | data->encap_size = nf_bridge_encap_header_len(skb); | |
783 | data->size = ETH_HLEN + data->encap_size; | |
784 | ||
785 | skb_copy_from_linear_data_offset(skb, -data->size, data->mac, | |
786 | data->size); | |
787 | ||
788 | if (v6ops) | |
7d8c6e39 | 789 | return v6ops->fragment(net, sk, skb, br_nf_push_frag_xmit); |
dd302b59 FW |
790 | |
791 | kfree_skb(skb); | |
792 | return -EMSGSIZE; | |
efb6de9b BT |
793 | } |
794 | #endif | |
a9fcc6a4 | 795 | nf_bridge_info_free(skb); |
0c4b51f0 | 796 | return br_dev_queue_push_xmit(net, sk, skb); |
dd302b59 FW |
797 | drop: |
798 | kfree_skb(skb); | |
799 | return 0; | |
c197facc | 800 | } |
1da177e4 LT |
801 | |
802 | /* PF_BRIDGE/POST_ROUTING ********************************************/ | |
06198b34 | 803 | static unsigned int br_nf_post_routing(void *priv, |
795aa6ef | 804 | struct sk_buff *skb, |
238e54c9 | 805 | const struct nf_hook_state *state) |
1da177e4 | 806 | { |
38330783 | 807 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
1da177e4 | 808 | struct net_device *realoutdev = bridge_parent(skb->dev); |
76108cea | 809 | u_int8_t pf; |
1da177e4 | 810 | |
e4bb9bcb FW |
811 | /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in |
812 | * on a bridge, but was delivered locally and is now being routed: | |
813 | * | |
814 | * POST_ROUTING was already invoked from the ip stack. | |
815 | */ | |
816 | if (!nf_bridge || !nf_bridge->physoutdev) | |
81d9ddae PM |
817 | return NF_ACCEPT; |
818 | ||
5dce971a SH |
819 | if (!realoutdev) |
820 | return NF_DROP; | |
821 | ||
739e4505 | 822 | if (IS_IP(skb) || IS_VLAN_IP(skb) || IS_PPPOE_IP(skb)) |
aa740f46 | 823 | pf = NFPROTO_IPV4; |
739e4505 | 824 | else if (IS_IPV6(skb) || IS_VLAN_IPV6(skb) || IS_PPPOE_IPV6(skb)) |
aa740f46 | 825 | pf = NFPROTO_IPV6; |
a2bd40ad HX |
826 | else |
827 | return NF_ACCEPT; | |
1da177e4 | 828 | |
1da177e4 LT |
829 | /* We assume any code from br_dev_queue_push_xmit onwards doesn't care |
830 | * about the value of skb->pkt_type. */ | |
831 | if (skb->pkt_type == PACKET_OTHERHOST) { | |
832 | skb->pkt_type = PACKET_HOST; | |
a1e67951 | 833 | nf_bridge->pkt_otherhost = true; |
1da177e4 LT |
834 | } |
835 | ||
fc38582d | 836 | nf_bridge_pull_encap_header(skb); |
aa740f46 | 837 | if (pf == NFPROTO_IPV4) |
e179e632 BDS |
838 | skb->protocol = htons(ETH_P_IP); |
839 | else | |
840 | skb->protocol = htons(ETH_P_IPV6); | |
1da177e4 | 841 | |
29a26a56 | 842 | NF_HOOK(pf, NF_INET_POST_ROUTING, state->net, state->sk, skb, |
7026b1dd | 843 | NULL, realoutdev, |
2e2f7aef | 844 | br_nf_dev_queue_xmit); |
1da177e4 LT |
845 | |
846 | return NF_STOLEN; | |
1da177e4 LT |
847 | } |
848 | ||
1da177e4 LT |
849 | /* IP/SABOTAGE *****************************************************/ |
850 | /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING | |
851 | * for the second time. */ | |
06198b34 | 852 | static unsigned int ip_sabotage_in(void *priv, |
795aa6ef | 853 | struct sk_buff *skb, |
238e54c9 | 854 | const struct nf_hook_state *state) |
1da177e4 | 855 | { |
72b1e5e4 | 856 | if (skb->nf_bridge && !skb->nf_bridge->in_prerouting) |
1da177e4 | 857 | return NF_STOP; |
1da177e4 LT |
858 | |
859 | return NF_ACCEPT; | |
860 | } | |
861 | ||
e5de75bf PNA |
862 | /* This is called when br_netfilter has called into iptables/netfilter, |
863 | * and DNAT has taken place on a bridge-forwarded packet. | |
864 | * | |
865 | * neigh->output has created a new MAC header, with local br0 MAC | |
866 | * as saddr. | |
867 | * | |
868 | * This restores the original MAC saddr of the bridged packet | |
869 | * before invoking bridge forward logic to transmit the packet. | |
870 | */ | |
871 | static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb) | |
872 | { | |
38330783 | 873 | struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb); |
e5de75bf PNA |
874 | |
875 | skb_pull(skb, ETH_HLEN); | |
72b1e5e4 | 876 | nf_bridge->bridged_dnat = 0; |
e5de75bf | 877 | |
e70deecb FW |
878 | BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN)); |
879 | ||
880 | skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN), | |
881 | nf_bridge->neigh_header, | |
882 | ETH_HLEN - ETH_ALEN); | |
e5de75bf | 883 | skb->dev = nf_bridge->physindev; |
7fb48c5b FW |
884 | |
885 | nf_bridge->physoutdev = NULL; | |
0c4b51f0 | 886 | br_handle_frame_finish(dev_net(skb->dev), NULL, skb); |
e5de75bf PNA |
887 | } |
888 | ||
1a4ba64d | 889 | static int br_nf_dev_xmit(struct sk_buff *skb) |
e5de75bf | 890 | { |
72b1e5e4 | 891 | if (skb->nf_bridge && skb->nf_bridge->bridged_dnat) { |
e5de75bf PNA |
892 | br_nf_pre_routing_finish_bridge_slow(skb); |
893 | return 1; | |
894 | } | |
895 | return 0; | |
896 | } | |
1a4ba64d PNA |
897 | |
898 | static const struct nf_br_ops br_ops = { | |
899 | .br_dev_xmit_hook = br_nf_dev_xmit, | |
900 | }; | |
e5de75bf | 901 | |
4b7fd5d9 PNA |
902 | void br_netfilter_enable(void) |
903 | { | |
904 | } | |
905 | EXPORT_SYMBOL_GPL(br_netfilter_enable); | |
906 | ||
ea2d9b41 BDS |
907 | /* For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because |
908 | * br_dev_queue_push_xmit is called afterwards */ | |
1999414a | 909 | static struct nf_hook_ops br_nf_ops[] __read_mostly = { |
1490fd89 CG |
910 | { |
911 | .hook = br_nf_pre_routing, | |
912 | .owner = THIS_MODULE, | |
aa740f46 | 913 | .pf = NFPROTO_BRIDGE, |
1490fd89 CG |
914 | .hooknum = NF_BR_PRE_ROUTING, |
915 | .priority = NF_BR_PRI_BRNF, | |
916 | }, | |
917 | { | |
918 | .hook = br_nf_local_in, | |
919 | .owner = THIS_MODULE, | |
aa740f46 | 920 | .pf = NFPROTO_BRIDGE, |
1490fd89 CG |
921 | .hooknum = NF_BR_LOCAL_IN, |
922 | .priority = NF_BR_PRI_BRNF, | |
923 | }, | |
924 | { | |
925 | .hook = br_nf_forward_ip, | |
926 | .owner = THIS_MODULE, | |
aa740f46 | 927 | .pf = NFPROTO_BRIDGE, |
1490fd89 CG |
928 | .hooknum = NF_BR_FORWARD, |
929 | .priority = NF_BR_PRI_BRNF - 1, | |
930 | }, | |
931 | { | |
932 | .hook = br_nf_forward_arp, | |
933 | .owner = THIS_MODULE, | |
aa740f46 | 934 | .pf = NFPROTO_BRIDGE, |
1490fd89 CG |
935 | .hooknum = NF_BR_FORWARD, |
936 | .priority = NF_BR_PRI_BRNF, | |
937 | }, | |
1490fd89 CG |
938 | { |
939 | .hook = br_nf_post_routing, | |
940 | .owner = THIS_MODULE, | |
aa740f46 | 941 | .pf = NFPROTO_BRIDGE, |
1490fd89 CG |
942 | .hooknum = NF_BR_POST_ROUTING, |
943 | .priority = NF_BR_PRI_LAST, | |
944 | }, | |
945 | { | |
946 | .hook = ip_sabotage_in, | |
947 | .owner = THIS_MODULE, | |
aa740f46 | 948 | .pf = NFPROTO_IPV4, |
1490fd89 CG |
949 | .hooknum = NF_INET_PRE_ROUTING, |
950 | .priority = NF_IP_PRI_FIRST, | |
951 | }, | |
952 | { | |
953 | .hook = ip_sabotage_in, | |
954 | .owner = THIS_MODULE, | |
aa740f46 | 955 | .pf = NFPROTO_IPV6, |
1490fd89 CG |
956 | .hooknum = NF_INET_PRE_ROUTING, |
957 | .priority = NF_IP6_PRI_FIRST, | |
958 | }, | |
1da177e4 LT |
959 | }; |
960 | ||
961 | #ifdef CONFIG_SYSCTL | |
962 | static | |
fe2c6338 | 963 | int brnf_sysctl_call_tables(struct ctl_table *ctl, int write, |
56b148eb | 964 | void __user *buffer, size_t *lenp, loff_t *ppos) |
1da177e4 LT |
965 | { |
966 | int ret; | |
967 | ||
8d65af78 | 968 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
1da177e4 LT |
969 | |
970 | if (write && *(int *)(ctl->data)) | |
971 | *(int *)(ctl->data) = 1; | |
972 | return ret; | |
973 | } | |
974 | ||
fe2c6338 | 975 | static struct ctl_table brnf_table[] = { |
1da177e4 | 976 | { |
1da177e4 LT |
977 | .procname = "bridge-nf-call-arptables", |
978 | .data = &brnf_call_arptables, | |
979 | .maxlen = sizeof(int), | |
980 | .mode = 0644, | |
6d9f239a | 981 | .proc_handler = brnf_sysctl_call_tables, |
1da177e4 LT |
982 | }, |
983 | { | |
1da177e4 LT |
984 | .procname = "bridge-nf-call-iptables", |
985 | .data = &brnf_call_iptables, | |
986 | .maxlen = sizeof(int), | |
987 | .mode = 0644, | |
6d9f239a | 988 | .proc_handler = brnf_sysctl_call_tables, |
1da177e4 LT |
989 | }, |
990 | { | |
1da177e4 LT |
991 | .procname = "bridge-nf-call-ip6tables", |
992 | .data = &brnf_call_ip6tables, | |
993 | .maxlen = sizeof(int), | |
994 | .mode = 0644, | |
6d9f239a | 995 | .proc_handler = brnf_sysctl_call_tables, |
1da177e4 LT |
996 | }, |
997 | { | |
1da177e4 LT |
998 | .procname = "bridge-nf-filter-vlan-tagged", |
999 | .data = &brnf_filter_vlan_tagged, | |
1000 | .maxlen = sizeof(int), | |
1001 | .mode = 0644, | |
6d9f239a | 1002 | .proc_handler = brnf_sysctl_call_tables, |
516299d2 MM |
1003 | }, |
1004 | { | |
516299d2 MM |
1005 | .procname = "bridge-nf-filter-pppoe-tagged", |
1006 | .data = &brnf_filter_pppoe_tagged, | |
1007 | .maxlen = sizeof(int), | |
1008 | .mode = 0644, | |
6d9f239a | 1009 | .proc_handler = brnf_sysctl_call_tables, |
1da177e4 | 1010 | }, |
4981682c PNA |
1011 | { |
1012 | .procname = "bridge-nf-pass-vlan-input-dev", | |
1013 | .data = &brnf_pass_vlan_indev, | |
1014 | .maxlen = sizeof(int), | |
1015 | .mode = 0644, | |
1016 | .proc_handler = brnf_sysctl_call_tables, | |
1017 | }, | |
f8572d8f | 1018 | { } |
1da177e4 | 1019 | }; |
1da177e4 LT |
1020 | #endif |
1021 | ||
34666d46 | 1022 | static int __init br_netfilter_init(void) |
1da177e4 | 1023 | { |
5eb87f45 | 1024 | int ret; |
1da177e4 | 1025 | |
34666d46 | 1026 | ret = nf_register_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops)); |
5eb87f45 | 1027 | if (ret < 0) |
1da177e4 | 1028 | return ret; |
fc66f95c | 1029 | |
1da177e4 | 1030 | #ifdef CONFIG_SYSCTL |
ec8f23ce | 1031 | brnf_sysctl_header = register_net_sysctl(&init_net, "net/bridge", brnf_table); |
1da177e4 | 1032 | if (brnf_sysctl_header == NULL) { |
789bc3e5 SH |
1033 | printk(KERN_WARNING |
1034 | "br_netfilter: can't register to sysctl.\n"); | |
96727239 GU |
1035 | nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops)); |
1036 | return -ENOMEM; | |
1da177e4 LT |
1037 | } |
1038 | #endif | |
1a4ba64d | 1039 | RCU_INIT_POINTER(nf_br_ops, &br_ops); |
1da177e4 | 1040 | printk(KERN_NOTICE "Bridge firewalling registered\n"); |
1da177e4 LT |
1041 | return 0; |
1042 | } | |
1043 | ||
34666d46 | 1044 | static void __exit br_netfilter_fini(void) |
1da177e4 | 1045 | { |
1a4ba64d | 1046 | RCU_INIT_POINTER(nf_br_ops, NULL); |
5eb87f45 | 1047 | nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops)); |
1da177e4 | 1048 | #ifdef CONFIG_SYSCTL |
5dd3df10 | 1049 | unregister_net_sysctl_table(brnf_sysctl_header); |
1da177e4 LT |
1050 | #endif |
1051 | } | |
34666d46 PNA |
1052 | |
1053 | module_init(br_netfilter_init); | |
1054 | module_exit(br_netfilter_fini); | |
1055 | ||
1056 | MODULE_LICENSE("GPL"); | |
1057 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@gnu.org>"); | |
1058 | MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>"); | |
1059 | MODULE_DESCRIPTION("Linux ethernet netfilter firewall bridge"); |