3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
10 * Apr 29 2003: physdev module support (bdschuym)
11 * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
12 * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Lennert dedicates this file to Kerstin Wurdinger.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
26 #include <linux/netdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_vlan.h>
30 #include <linux/netfilter_bridge.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/netfilter_arp.h>
33 #include <linux/in_route.h>
35 #include <asm/uaccess.h>
36 #include <asm/checksum.h>
37 #include "br_private.h"
39 #include <linux/sysctl.h>
43 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
44 (skb->nf_bridge->data))->daddr.ipv4)
45 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
46 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
48 #define has_bridge_parent(device) ((device)->br_port != NULL)
49 #define bridge_parent(device) ((device)->br_port->br->dev)
52 static struct ctl_table_header *brnf_sysctl_header;
53 static int brnf_call_iptables = 1;
54 static int brnf_call_arptables = 1;
55 static int brnf_filter_vlan_tagged = 1;
57 #define brnf_filter_vlan_tagged 1
60 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
61 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
62 brnf_filter_vlan_tagged)
63 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
64 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
65 brnf_filter_vlan_tagged)
67 /* We need these fake structures to make netfilter happy --
68 * lots of places assume that skb->dst != NULL, which isn't
69 * all that unreasonable.
71 * Currently, we fill in the PMTU entry because netfilter
72 * refragmentation needs it, and the rt_flags entry because
73 * ipt_REJECT needs it. Future netfilter modules might
74 * require us to fill additional fields.
76 static struct net_device __fake_net_device = {
77 .hard_header_len = ETH_HLEN
80 static struct rtable __fake_rtable = {
83 .__refcnt = ATOMIC_INIT(1),
84 .dev = &__fake_net_device,
85 .path = &__fake_rtable.u.dst,
86 .metrics = {[RTAX_MTU - 1] = 1500},
93 /* PF_BRIDGE/PRE_ROUTING *********************************************/
94 static void __br_dnat_complain(void)
96 static unsigned long last_complaint;
98 if (jiffies - last_complaint >= 5 * HZ) {
99 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
100 "forwarding to be enabled\n");
101 last_complaint = jiffies;
106 /* This requires some explaining. If DNAT has taken place,
107 * we will need to fix up the destination Ethernet address,
108 * and this is a tricky process.
110 * There are two cases to consider:
111 * 1. The packet was DNAT'ed to a device in the same bridge
112 * port group as it was received on. We can still bridge
114 * 2. The packet was DNAT'ed to a different device, either
115 * a non-bridged device or another bridge port group.
116 * The packet will need to be routed.
118 * The correct way of distinguishing between these two cases is to
119 * call ip_route_input() and to look at skb->dst->dev, which is
120 * changed to the destination device if ip_route_input() succeeds.
122 * Let us first consider the case that ip_route_input() succeeds:
124 * If skb->dst->dev equals the logical bridge device the packet
125 * came in on, we can consider this bridging. We then call
126 * skb->dst->output() which will make the packet enter br_nf_local_out()
127 * not much later. In that function it is assured that the iptables
128 * FORWARD chain is traversed for the packet.
130 * Otherwise, the packet is considered to be routed and we just
131 * change the destination MAC address so that the packet will
132 * later be passed up to the IP stack to be routed.
134 * Let us now consider the case that ip_route_input() fails:
136 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
137 * will fail, while __ip_route_output_key() will return success. The source
138 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
139 * thinks we're handling a locally generated packet and won't care
140 * if IP forwarding is allowed. We send a warning message to the users's
141 * log telling her to put IP forwarding on.
143 * ip_route_input() will also fail if there is no route available.
144 * In that case we just drop the packet.
146 * --Lennert, 20020411
147 * --Bart, 20020416 (updated)
148 * --Bart, 20021007 (updated)
151 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
153 #ifdef CONFIG_NETFILTER_DEBUG
154 skb->nf_debug |= (1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_FORWARD);
157 if (skb->pkt_type == PACKET_OTHERHOST) {
158 skb->pkt_type = PACKET_HOST;
159 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
161 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
163 skb->dev = bridge_parent(skb->dev);
164 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
165 skb_pull(skb, VLAN_HLEN);
166 skb->nh.raw += VLAN_HLEN;
168 skb->dst->output(skb);
172 static int br_nf_pre_routing_finish(struct sk_buff *skb)
174 struct net_device *dev = skb->dev;
175 struct iphdr *iph = skb->nh.iph;
176 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
178 #ifdef CONFIG_NETFILTER_DEBUG
179 skb->nf_debug ^= (1 << NF_BR_PRE_ROUTING);
182 if (nf_bridge->mask & BRNF_PKT_TYPE) {
183 skb->pkt_type = PACKET_OTHERHOST;
184 nf_bridge->mask ^= BRNF_PKT_TYPE;
186 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
188 if (dnat_took_place(skb)) {
189 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
192 struct flowi fl = { .nl_u =
193 { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
194 .tos = RT_TOS(iph->tos)} }, .proto = 0};
196 if (!ip_route_output_key(&rt, &fl)) {
197 /* Bridged-and-DNAT'ed traffic doesn't
198 * require ip_forwarding.
200 if (((struct dst_entry *)rt)->dev == dev) {
201 skb->dst = (struct dst_entry *)rt;
204 __br_dnat_complain();
205 dst_release((struct dst_entry *)rt);
210 if (skb->dst->dev == dev) {
212 /* Tell br_nf_local_out this is a
215 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
216 skb->dev = nf_bridge->physindev;
218 __constant_htons(ETH_P_8021Q)) {
219 skb_push(skb, VLAN_HLEN);
220 skb->nh.raw -= VLAN_HLEN;
222 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
224 br_nf_pre_routing_finish_bridge,
228 memcpy(skb->mac.ethernet->h_dest, dev->dev_addr,
230 skb->pkt_type = PACKET_HOST;
233 skb->dst = (struct dst_entry *)&__fake_rtable;
237 skb->dev = nf_bridge->physindev;
238 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
239 skb_push(skb, VLAN_HLEN);
240 skb->nh.raw -= VLAN_HLEN;
242 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
243 br_handle_frame_finish, 1);
248 /* Replicate the checks that IPv4 does on packet reception.
249 * Set skb->dev to the bridge device (i.e. parent of the
250 * receiving device) to make netfilter happy, the REDIRECT
251 * target in particular. Save the original destination IP
252 * address to be able to detect DNAT afterwards.
254 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
255 const struct net_device *in, const struct net_device *out,
256 int (*okfn)(struct sk_buff *))
260 struct sk_buff *skb = *pskb;
261 struct nf_bridge_info *nf_bridge;
264 if (!brnf_call_iptables)
268 if (skb->protocol != __constant_htons(ETH_P_IP)) {
269 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)
270 ((*pskb)->mac.ethernet);
274 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
276 skb_pull(*pskb, VLAN_HLEN);
277 (*pskb)->nh.raw += VLAN_HLEN;
278 } else if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
281 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
285 if (iph->ihl < 5 || iph->version != 4)
288 if (!pskb_may_pull(skb, 4*iph->ihl))
292 if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
295 len = ntohs(iph->tot_len);
296 if (skb->len < len || len < 4*iph->ihl)
299 if (skb->len > len) {
300 __pskb_trim(skb, len);
301 if (skb->ip_summed == CHECKSUM_HW)
302 skb->ip_summed = CHECKSUM_NONE;
305 #ifdef CONFIG_NETFILTER_DEBUG
306 skb->nf_debug ^= (1 << NF_IP_PRE_ROUTING);
308 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
311 if (skb->pkt_type == PACKET_OTHERHOST) {
312 skb->pkt_type = PACKET_HOST;
313 nf_bridge->mask |= BRNF_PKT_TYPE;
316 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
317 nf_bridge->physindev = skb->dev;
318 skb->dev = bridge_parent(skb->dev);
319 store_orig_dstaddr(skb);
321 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
322 br_nf_pre_routing_finish);
327 // IP_INC_STATS_BH(IpInHdrErrors);
333 /* PF_BRIDGE/LOCAL_IN ************************************************/
334 /* The packet is locally destined, which requires a real
335 * dst_entry, so detach the fake one. On the way up, the
336 * packet would pass through PRE_ROUTING again (which already
337 * took place when the packet entered the bridge), but we
338 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
339 * prevent this from happening.
341 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
342 const struct net_device *in, const struct net_device *out,
343 int (*okfn)(struct sk_buff *))
345 struct sk_buff *skb = *pskb;
347 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
348 dst_release(skb->dst);
356 /* PF_BRIDGE/FORWARD *************************************************/
357 static int br_nf_forward_finish(struct sk_buff *skb)
359 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
360 struct net_device *in;
361 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
363 #ifdef CONFIG_NETFILTER_DEBUG
364 skb->nf_debug ^= (1 << NF_BR_FORWARD);
367 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP) {
368 in = nf_bridge->physindev;
369 if (nf_bridge->mask & BRNF_PKT_TYPE) {
370 skb->pkt_type = PACKET_OTHERHOST;
371 nf_bridge->mask ^= BRNF_PKT_TYPE;
374 in = *((struct net_device **)(skb->cb));
376 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
377 skb_push(skb, VLAN_HLEN);
378 skb->nh.raw -= VLAN_HLEN;
380 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
381 skb->dev, br_forward_finish, 1);
385 /* This is the 'purely bridged' case. For IP, we pass the packet to
386 * netfilter with indev and outdev set to the bridge device,
387 * but we are still able to filter on the 'real' indev/outdev
388 * because of the ipt_physdev.c module. For ARP, indev and outdev are the
391 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
392 const struct net_device *in, const struct net_device *out,
393 int (*okfn)(struct sk_buff *))
395 struct sk_buff *skb = *pskb;
396 struct nf_bridge_info *nf_bridge;
397 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
404 if (skb->protocol != __constant_htons(ETH_P_IP)) {
407 skb_pull(*pskb, VLAN_HLEN);
408 (*pskb)->nh.raw += VLAN_HLEN;
411 #ifdef CONFIG_NETFILTER_DEBUG
412 skb->nf_debug ^= (1 << NF_BR_FORWARD);
414 nf_bridge = skb->nf_bridge;
415 if (skb->pkt_type == PACKET_OTHERHOST) {
416 skb->pkt_type = PACKET_HOST;
417 nf_bridge->mask |= BRNF_PKT_TYPE;
420 /* The physdev module checks on this */
421 nf_bridge->mask |= BRNF_BRIDGED;
422 nf_bridge->physoutdev = skb->dev;
424 NF_HOOK(PF_INET, NF_IP_FORWARD, skb, bridge_parent(in),
425 bridge_parent(out), br_nf_forward_finish);
430 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
431 const struct net_device *in, const struct net_device *out,
432 int (*okfn)(struct sk_buff *))
434 struct sk_buff *skb = *pskb;
435 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
436 struct net_device **d = (struct net_device **)(skb->cb);
439 if (!brnf_call_arptables)
443 if (skb->protocol != __constant_htons(ETH_P_ARP)) {
446 skb_pull(*pskb, VLAN_HLEN);
447 (*pskb)->nh.raw += VLAN_HLEN;
450 #ifdef CONFIG_NETFILTER_DEBUG
451 skb->nf_debug ^= (1 << NF_BR_FORWARD);
454 if (skb->nh.arph->ar_pln != 4) {
456 skb_push(*pskb, VLAN_HLEN);
457 (*pskb)->nh.raw -= VLAN_HLEN;
461 *d = (struct net_device *)in;
462 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
463 (struct net_device *)out, br_nf_forward_finish);
469 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
470 static int br_nf_local_out_finish(struct sk_buff *skb)
472 #ifdef CONFIG_NETFILTER_DEBUG
473 skb->nf_debug &= ~(1 << NF_BR_LOCAL_OUT);
475 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
476 skb_push(skb, VLAN_HLEN);
477 skb->nh.raw -= VLAN_HLEN;
480 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
481 br_forward_finish, NF_BR_PRI_FIRST + 1);
487 /* This function sees both locally originated IP packets and forwarded
488 * IP packets (in both cases the destination device is a bridge
489 * device). It also sees bridged-and-DNAT'ed packets.
490 * To be able to filter on the physical bridge devices (with the ipt_physdev.c
491 * module), we steal packets destined to a bridge device away from the
492 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
493 * when we have determined the real output device. This is done in here.
495 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
496 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
497 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
498 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
500 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
501 * this packet before, and so the packet was locally originated. We fake
502 * the PF_INET/LOCAL_OUT hook.
503 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
504 * so we fake the PF_INET/FORWARD hook. ipv4_sabotage_out() makes sure
505 * even routed packets that didn't arrive on a bridge interface have their
506 * nf_bridge->physindev set.
509 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
510 const struct net_device *in, const struct net_device *out,
511 int (*_okfn)(struct sk_buff *))
513 int (*okfn)(struct sk_buff *skb);
514 struct net_device *realindev;
515 struct sk_buff *skb = *pskb;
516 struct nf_bridge_info *nf_bridge;
517 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
524 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
527 /* Sometimes we get packets with NULL ->dst here (for example,
528 * running a dhcp client daemon triggers this).
530 if (skb->dst == NULL)
533 nf_bridge = skb->nf_bridge;
534 nf_bridge->physoutdev = skb->dev;
535 realindev = nf_bridge->physindev;
537 /* Bridged, take PF_BRIDGE/FORWARD.
538 * (see big note in front of br_nf_pre_routing_finish)
540 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
541 okfn = br_forward_finish;
543 if (nf_bridge->mask & BRNF_PKT_TYPE) {
544 skb->pkt_type = PACKET_OTHERHOST;
545 nf_bridge->mask ^= BRNF_PKT_TYPE;
547 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
548 skb_push(skb, VLAN_HLEN);
549 skb->nh.raw -= VLAN_HLEN;
552 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
555 struct net_device *realoutdev = bridge_parent(skb->dev);
557 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
558 /* iptables should match -o br0.x */
559 if (nf_bridge->netoutdev)
560 realoutdev = nf_bridge->netoutdev;
562 okfn = br_nf_local_out_finish;
563 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
564 skb_pull(skb, VLAN_HLEN);
565 (*pskb)->nh.raw += VLAN_HLEN;
567 /* IP forwarded traffic has a physindev, locally
568 * generated traffic hasn't.
570 if (realindev != NULL) {
571 if (((nf_bridge->mask & BRNF_DONT_TAKE_PARENT) == 0) &&
572 has_bridge_parent(realindev))
573 realindev = bridge_parent(realindev);
574 NF_HOOK_THRESH(PF_INET, NF_IP_FORWARD, skb, realindev,
576 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
578 #ifdef CONFIG_NETFILTER_DEBUG
579 skb->nf_debug ^= (1 << NF_IP_LOCAL_OUT);
582 NF_HOOK_THRESH(PF_INET, NF_IP_LOCAL_OUT, skb, realindev,
584 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
592 /* PF_BRIDGE/POST_ROUTING ********************************************/
593 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
594 const struct net_device *in, const struct net_device *out,
595 int (*okfn)(struct sk_buff *))
597 struct sk_buff *skb = *pskb;
598 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
599 struct vlan_ethhdr *hdr = (struct vlan_ethhdr *)(skb->mac.ethernet);
600 struct net_device *realoutdev = bridge_parent(skb->dev);
602 #ifdef CONFIG_NETFILTER_DEBUG
603 /* Be very paranoid. This probably won't happen anymore, but let's
604 * keep the check just to be sure... */
605 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
606 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
607 "bad mac.raw pointer.");
617 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
620 #ifdef CONFIG_NETFILTER_DEBUG
621 /* Sometimes we get packets with NULL ->dst here (for example,
622 * running a dhcp client daemon triggers this). This should now
623 * be fixed, but let's keep the check around.
625 if (skb->dst == NULL) {
626 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
630 skb->nf_debug ^= (1 << NF_IP_POST_ROUTING);
633 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
634 * about the value of skb->pkt_type.
636 if (skb->pkt_type == PACKET_OTHERHOST) {
637 skb->pkt_type = PACKET_HOST;
638 nf_bridge->mask |= BRNF_PKT_TYPE;
641 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
642 skb_pull(skb, VLAN_HLEN);
643 skb->nh.raw += VLAN_HLEN;
646 nf_bridge_save_header(skb);
648 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
649 if (nf_bridge->netoutdev)
650 realoutdev = nf_bridge->netoutdev;
652 NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL,
653 realoutdev, br_dev_queue_push_xmit);
657 #ifdef CONFIG_NETFILTER_DEBUG
659 if (skb->dev != NULL) {
660 printk("[%s]", skb->dev->name);
661 if (has_bridge_parent(skb->dev))
662 printk("[%s]", bridge_parent(skb->dev)->name);
664 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
671 /* IPv4/SABOTAGE *****************************************************/
673 /* Don't hand locally destined packets to PF_INET/PRE_ROUTING
674 * for the second time.
676 static unsigned int ipv4_sabotage_in(unsigned int hook, struct sk_buff **pskb,
677 const struct net_device *in, const struct net_device *out,
678 int (*okfn)(struct sk_buff *))
680 if ((*pskb)->nf_bridge &&
681 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
689 /* Postpone execution of PF_INET/FORWARD, PF_INET/LOCAL_OUT
690 * and PF_INET/POST_ROUTING until we have done the forwarding
691 * decision in the bridge code and have determined skb->physoutdev.
693 static unsigned int ipv4_sabotage_out(unsigned int hook, struct sk_buff **pskb,
694 const struct net_device *in, const struct net_device *out,
695 int (*okfn)(struct sk_buff *))
697 struct sk_buff *skb = *pskb;
700 if (!brnf_call_iptables && !skb->nf_bridge)
704 if ((out->hard_start_xmit == br_dev_xmit &&
705 okfn != br_nf_forward_finish &&
706 okfn != br_nf_local_out_finish &&
707 okfn != br_dev_queue_push_xmit)
708 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
709 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
710 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
713 struct nf_bridge_info *nf_bridge;
715 if (!skb->nf_bridge && !nf_bridge_alloc(skb))
718 nf_bridge = skb->nf_bridge;
720 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
721 * will need the indev then. For a brouter, the real indev
722 * can be a bridge port, so we make sure br_nf_local_out()
723 * doesn't use the bridge parent of the indev by using
724 * the BRNF_DONT_TAKE_PARENT mask.
726 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
727 nf_bridge->mask &= BRNF_DONT_TAKE_PARENT;
728 nf_bridge->physindev = (struct net_device *)in;
730 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
731 /* the iptables outdev is br0.x, not br0 */
732 if (out->priv_flags & IFF_802_1Q_VLAN)
733 nf_bridge->netoutdev = (struct net_device *)out;
742 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
743 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
744 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
745 * ip_refrag() can return NF_STOLEN.
747 static struct nf_hook_ops br_nf_ops[] = {
748 { .hook = br_nf_pre_routing,
749 .owner = THIS_MODULE,
751 .hooknum = NF_BR_PRE_ROUTING,
752 .priority = NF_BR_PRI_BRNF, },
753 { .hook = br_nf_local_in,
754 .owner = THIS_MODULE,
756 .hooknum = NF_BR_LOCAL_IN,
757 .priority = NF_BR_PRI_BRNF, },
758 { .hook = br_nf_forward_ip,
759 .owner = THIS_MODULE,
761 .hooknum = NF_BR_FORWARD,
762 .priority = NF_BR_PRI_BRNF - 1, },
763 { .hook = br_nf_forward_arp,
764 .owner = THIS_MODULE,
766 .hooknum = NF_BR_FORWARD,
767 .priority = NF_BR_PRI_BRNF, },
768 { .hook = br_nf_local_out,
769 .owner = THIS_MODULE,
771 .hooknum = NF_BR_LOCAL_OUT,
772 .priority = NF_BR_PRI_FIRST, },
773 { .hook = br_nf_post_routing,
774 .owner = THIS_MODULE,
776 .hooknum = NF_BR_POST_ROUTING,
777 .priority = NF_BR_PRI_LAST, },
778 { .hook = ipv4_sabotage_in,
779 .owner = THIS_MODULE,
781 .hooknum = NF_IP_PRE_ROUTING,
782 .priority = NF_IP_PRI_FIRST, },
783 { .hook = ipv4_sabotage_out,
784 .owner = THIS_MODULE,
786 .hooknum = NF_IP_FORWARD,
787 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
788 { .hook = ipv4_sabotage_out,
789 .owner = THIS_MODULE,
791 .hooknum = NF_IP_LOCAL_OUT,
792 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
793 { .hook = ipv4_sabotage_out,
794 .owner = THIS_MODULE,
796 .hooknum = NF_IP_POST_ROUTING,
797 .priority = NF_IP_PRI_FIRST, },
802 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
803 void *buffer, size_t *lenp)
807 ret = proc_dointvec(ctl, write, filp, buffer, lenp);
809 if (write && *(int *)(ctl->data))
810 *(int *)(ctl->data) = 1;
814 static ctl_table brnf_table[] = {
816 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
817 .procname = "bridge-nf-call-arptables",
818 .data = &brnf_call_arptables,
819 .maxlen = sizeof(int),
821 .proc_handler = &brnf_sysctl_call_tables,
824 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
825 .procname = "bridge-nf-call-iptables",
826 .data = &brnf_call_iptables,
827 .maxlen = sizeof(int),
829 .proc_handler = &brnf_sysctl_call_tables,
832 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
833 .procname = "bridge-nf-filter-vlan-tagged",
834 .data = &brnf_filter_vlan_tagged,
835 .maxlen = sizeof(int),
837 .proc_handler = &brnf_sysctl_call_tables,
842 static ctl_table brnf_bridge_table[] = {
844 .ctl_name = NET_BRIDGE,
845 .procname = "bridge",
852 static ctl_table brnf_net_table[] = {
857 .child = brnf_bridge_table,
863 int br_netfilter_init(void)
867 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
870 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
874 nf_unregister_hook(&br_nf_ops[i]);
880 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
881 if (brnf_sysctl_header == NULL) {
882 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
883 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
884 nf_unregister_hook(&br_nf_ops[i]);
889 printk(KERN_NOTICE "Bridge firewalling registered\n");
894 void br_netfilter_fini(void)
898 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
899 nf_unregister_hook(&br_nf_ops[i]);
901 unregister_sysctl_table(brnf_sysctl_header);