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