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