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