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