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