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