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