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
14 * Sep 01 2004: add IPv6 filtering (bdschuym)
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Lennert dedicates this file to Kerstin Wurdinger.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/netfilter_bridge.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/in_route.h>
40 #include <net/route.h>
42 #include <asm/uaccess.h>
43 #include <asm/checksum.h>
44 #include "br_private.h"
46 #include <linux/sysctl.h>
49 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
50 (skb->nf_bridge->data))->daddr.ipv4)
51 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
52 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
55 static struct ctl_table_header *brnf_sysctl_header;
56 static int brnf_call_iptables = 1;
57 static int brnf_call_ip6tables = 1;
58 static int brnf_call_arptables = 1;
59 static int brnf_filter_vlan_tagged = 1;
61 #define brnf_filter_vlan_tagged 1
64 static __be16 inline vlan_proto(const struct sk_buff *skb)
66 return vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
69 #define IS_VLAN_IP(skb) \
70 (skb->protocol == htons(ETH_P_8021Q) && \
71 vlan_proto(skb) == htons(ETH_P_IP) && \
72 brnf_filter_vlan_tagged)
74 #define IS_VLAN_IPV6(skb) \
75 (skb->protocol == htons(ETH_P_8021Q) && \
76 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
77 brnf_filter_vlan_tagged)
79 #define IS_VLAN_ARP(skb) \
80 (skb->protocol == htons(ETH_P_8021Q) && \
81 vlan_proto(skb) == htons(ETH_P_ARP) && \
82 brnf_filter_vlan_tagged)
84 /* We need these fake structures to make netfilter happy --
85 * lots of places assume that skb->dst != NULL, which isn't
86 * all that unreasonable.
88 * Currently, we fill in the PMTU entry because netfilter
89 * refragmentation needs it, and the rt_flags entry because
90 * ipt_REJECT needs it. Future netfilter modules might
91 * require us to fill additional fields. */
92 static struct net_device __fake_net_device = {
93 .hard_header_len = ETH_HLEN
96 static struct rtable __fake_rtable = {
99 .__refcnt = ATOMIC_INIT(1),
100 .dev = &__fake_net_device,
101 .path = &__fake_rtable.u.dst,
102 .metrics = {[RTAX_MTU - 1] = 1500},
109 static inline struct net_device *bridge_parent(const struct net_device *dev)
111 struct net_bridge_port *port = rcu_dereference(dev->br_port);
113 return port ? port->br->dev : NULL;
116 /* PF_BRIDGE/PRE_ROUTING *********************************************/
117 /* Undo the changes made for ip6tables PREROUTING and continue the
118 * bridge PRE_ROUTING hook. */
119 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
121 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
123 if (nf_bridge->mask & BRNF_PKT_TYPE) {
124 skb->pkt_type = PACKET_OTHERHOST;
125 nf_bridge->mask ^= BRNF_PKT_TYPE;
127 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
129 skb->dst = (struct dst_entry *)&__fake_rtable;
132 skb->dev = nf_bridge->physindev;
133 if (skb->protocol == htons(ETH_P_8021Q)) {
134 skb_push(skb, VLAN_HLEN);
135 skb->nh.raw -= VLAN_HLEN;
137 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
138 br_handle_frame_finish, 1);
143 static void __br_dnat_complain(void)
145 static unsigned long last_complaint;
147 if (jiffies - last_complaint >= 5 * HZ) {
148 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
149 "forwarding to be enabled\n");
150 last_complaint = jiffies;
154 /* This requires some explaining. If DNAT has taken place,
155 * we will need to fix up the destination Ethernet address,
156 * and this is a tricky process.
158 * There are two cases to consider:
159 * 1. The packet was DNAT'ed to a device in the same bridge
160 * port group as it was received on. We can still bridge
162 * 2. The packet was DNAT'ed to a different device, either
163 * a non-bridged device or another bridge port group.
164 * The packet will need to be routed.
166 * The correct way of distinguishing between these two cases is to
167 * call ip_route_input() and to look at skb->dst->dev, which is
168 * changed to the destination device if ip_route_input() succeeds.
170 * Let us first consider the case that ip_route_input() succeeds:
172 * If skb->dst->dev equals the logical bridge device the packet
173 * came in on, we can consider this bridging. We then call
174 * skb->dst->output() which will make the packet enter br_nf_local_out()
175 * not much later. In that function it is assured that the iptables
176 * FORWARD chain is traversed for the packet.
178 * Otherwise, the packet is considered to be routed and we just
179 * change the destination MAC address so that the packet will
180 * later be passed up to the IP stack to be routed.
182 * Let us now consider the case that ip_route_input() fails:
184 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
185 * will fail, while __ip_route_output_key() will return success. The source
186 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
187 * thinks we're handling a locally generated packet and won't care
188 * if IP forwarding is allowed. We send a warning message to the users's
189 * log telling her to put IP forwarding on.
191 * ip_route_input() will also fail if there is no route available.
192 * In that case we just drop the packet.
194 * --Lennert, 20020411
195 * --Bart, 20020416 (updated)
196 * --Bart, 20021007 (updated) */
197 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
199 if (skb->pkt_type == PACKET_OTHERHOST) {
200 skb->pkt_type = PACKET_HOST;
201 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
203 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
205 skb->dev = bridge_parent(skb->dev);
209 if (skb->protocol == htons(ETH_P_8021Q)) {
210 skb_pull(skb, VLAN_HLEN);
211 skb->nh.raw += VLAN_HLEN;
213 skb->dst->output(skb);
218 static int br_nf_pre_routing_finish(struct sk_buff *skb)
220 struct net_device *dev = skb->dev;
221 struct iphdr *iph = skb->nh.iph;
222 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
224 if (nf_bridge->mask & BRNF_PKT_TYPE) {
225 skb->pkt_type = PACKET_OTHERHOST;
226 nf_bridge->mask ^= BRNF_PKT_TYPE;
228 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
230 if (dnat_took_place(skb)) {
231 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev)) {
238 .tos = RT_TOS(iph->tos) },
243 if (!ip_route_output_key(&rt, &fl)) {
244 /* - Bridged-and-DNAT'ed traffic doesn't
245 * require ip_forwarding.
246 * - Deal with redirected traffic. */
247 if (((struct dst_entry *)rt)->dev == dev ||
248 rt->rt_type == RTN_LOCAL) {
249 skb->dst = (struct dst_entry *)rt;
252 __br_dnat_complain();
253 dst_release((struct dst_entry *)rt);
258 if (skb->dst->dev == dev) {
260 /* Tell br_nf_local_out this is a
262 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
263 skb->dev = nf_bridge->physindev;
265 htons(ETH_P_8021Q)) {
266 skb_push(skb, VLAN_HLEN);
267 skb->nh.raw -= VLAN_HLEN;
269 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
271 br_nf_pre_routing_finish_bridge,
275 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
276 skb->pkt_type = PACKET_HOST;
279 skb->dst = (struct dst_entry *)&__fake_rtable;
283 skb->dev = nf_bridge->physindev;
284 if (skb->protocol == htons(ETH_P_8021Q)) {
285 skb_push(skb, VLAN_HLEN);
286 skb->nh.raw -= VLAN_HLEN;
288 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
289 br_handle_frame_finish, 1);
294 /* Some common code for IPv4/IPv6 */
295 static struct net_device *setup_pre_routing(struct sk_buff *skb)
297 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
299 if (skb->pkt_type == PACKET_OTHERHOST) {
300 skb->pkt_type = PACKET_HOST;
301 nf_bridge->mask |= BRNF_PKT_TYPE;
304 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
305 nf_bridge->physindev = skb->dev;
306 skb->dev = bridge_parent(skb->dev);
311 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
312 static int check_hbh_len(struct sk_buff *skb)
314 unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1);
316 int off = raw - skb->nh.raw;
317 int len = (raw[1] + 1) << 3;
319 if ((raw + len) - skb->data > skb_headlen(skb))
326 int optlen = skb->nh.raw[off + 1] + 2;
328 switch (skb->nh.raw[off]) {
337 if (skb->nh.raw[off + 1] != 4 || (off & 3) != 2)
339 pkt_len = ntohl(*(u32 *) (skb->nh.raw + off + 2));
340 if (pkt_len <= IPV6_MAXPLEN ||
341 skb->nh.ipv6h->payload_len)
343 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
345 if (pskb_trim_rcsum(skb,
346 pkt_len + sizeof(struct ipv6hdr)))
364 /* Replicate the checks that IPv6 does on packet reception and pass the packet
365 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
366 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
368 const struct net_device *in,
369 const struct net_device *out,
370 int (*okfn)(struct sk_buff *))
374 struct nf_bridge_info *nf_bridge;
376 if (skb->len < sizeof(struct ipv6hdr))
379 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
384 if (hdr->version != 6)
387 pkt_len = ntohs(hdr->payload_len);
389 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
390 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
392 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
393 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
395 if (skb->ip_summed == CHECKSUM_HW)
396 skb->ip_summed = CHECKSUM_NONE;
399 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
402 nf_bridge_put(skb->nf_bridge);
403 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
405 if (!setup_pre_routing(skb))
408 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
409 br_nf_pre_routing_finish_ipv6);
417 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
418 * Replicate the checks that IPv4 does on packet reception.
419 * Set skb->dev to the bridge device (i.e. parent of the
420 * receiving device) to make netfilter happy, the REDIRECT
421 * target in particular. Save the original destination IP
422 * address to be able to detect DNAT afterwards. */
423 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
424 const struct net_device *in,
425 const struct net_device *out,
426 int (*okfn)(struct sk_buff *))
430 struct sk_buff *skb = *pskb;
431 struct nf_bridge_info *nf_bridge;
433 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6(skb)) {
435 if (!brnf_call_ip6tables)
438 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
441 if (skb->protocol == htons(ETH_P_8021Q)) {
442 skb_pull_rcsum(skb, VLAN_HLEN);
443 skb->nh.raw += VLAN_HLEN;
445 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
448 if (!brnf_call_iptables)
452 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP(skb))
455 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
458 if (skb->protocol == htons(ETH_P_8021Q)) {
459 skb_pull_rcsum(skb, VLAN_HLEN);
460 skb->nh.raw += VLAN_HLEN;
463 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
467 if (iph->ihl < 5 || iph->version != 4)
470 if (!pskb_may_pull(skb, 4 * iph->ihl))
474 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
477 len = ntohs(iph->tot_len);
478 if (skb->len < len || len < 4 * iph->ihl)
481 if (skb->len > len) {
482 __pskb_trim(skb, len);
483 if (skb->ip_summed == CHECKSUM_HW)
484 skb->ip_summed = CHECKSUM_NONE;
487 nf_bridge_put(skb->nf_bridge);
488 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
490 if (!setup_pre_routing(skb))
492 store_orig_dstaddr(skb);
494 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
495 br_nf_pre_routing_finish);
500 // IP_INC_STATS_BH(IpInHdrErrors);
506 /* PF_BRIDGE/LOCAL_IN ************************************************/
507 /* The packet is locally destined, which requires a real
508 * dst_entry, so detach the fake one. On the way up, the
509 * packet would pass through PRE_ROUTING again (which already
510 * took place when the packet entered the bridge), but we
511 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
512 * prevent this from happening. */
513 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
514 const struct net_device *in,
515 const struct net_device *out,
516 int (*okfn)(struct sk_buff *))
518 struct sk_buff *skb = *pskb;
520 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
521 dst_release(skb->dst);
528 /* PF_BRIDGE/FORWARD *************************************************/
529 static int br_nf_forward_finish(struct sk_buff *skb)
531 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
532 struct net_device *in;
534 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP(skb)) {
535 in = nf_bridge->physindev;
536 if (nf_bridge->mask & BRNF_PKT_TYPE) {
537 skb->pkt_type = PACKET_OTHERHOST;
538 nf_bridge->mask ^= BRNF_PKT_TYPE;
541 in = *((struct net_device **)(skb->cb));
543 if (skb->protocol == htons(ETH_P_8021Q)) {
544 skb_push(skb, VLAN_HLEN);
545 skb->nh.raw -= VLAN_HLEN;
547 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
548 skb->dev, br_forward_finish, 1);
552 /* This is the 'purely bridged' case. For IP, we pass the packet to
553 * netfilter with indev and outdev set to the bridge device,
554 * but we are still able to filter on the 'real' indev/outdev
555 * because of the physdev module. For ARP, indev and outdev are the
557 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
558 const struct net_device *in,
559 const struct net_device *out,
560 int (*okfn)(struct sk_buff *))
562 struct sk_buff *skb = *pskb;
563 struct nf_bridge_info *nf_bridge;
564 struct net_device *parent;
570 parent = bridge_parent(out);
574 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
579 if (skb->protocol == htons(ETH_P_8021Q)) {
580 skb_pull(*pskb, VLAN_HLEN);
581 (*pskb)->nh.raw += VLAN_HLEN;
584 nf_bridge = skb->nf_bridge;
585 if (skb->pkt_type == PACKET_OTHERHOST) {
586 skb->pkt_type = PACKET_HOST;
587 nf_bridge->mask |= BRNF_PKT_TYPE;
590 /* The physdev module checks on this */
591 nf_bridge->mask |= BRNF_BRIDGED;
592 nf_bridge->physoutdev = skb->dev;
594 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
595 br_nf_forward_finish);
600 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
601 const struct net_device *in,
602 const struct net_device *out,
603 int (*okfn)(struct sk_buff *))
605 struct sk_buff *skb = *pskb;
606 struct net_device **d = (struct net_device **)(skb->cb);
609 if (!brnf_call_arptables)
613 if (skb->protocol != htons(ETH_P_ARP)) {
614 if (!IS_VLAN_ARP(skb))
616 skb_pull(*pskb, VLAN_HLEN);
617 (*pskb)->nh.raw += VLAN_HLEN;
620 if (skb->nh.arph->ar_pln != 4) {
621 if (IS_VLAN_ARP(skb)) {
622 skb_push(*pskb, VLAN_HLEN);
623 (*pskb)->nh.raw -= VLAN_HLEN;
627 *d = (struct net_device *)in;
628 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
629 (struct net_device *)out, br_nf_forward_finish);
634 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
635 static int br_nf_local_out_finish(struct sk_buff *skb)
637 if (skb->protocol == htons(ETH_P_8021Q)) {
638 skb_push(skb, VLAN_HLEN);
639 skb->nh.raw -= VLAN_HLEN;
642 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
643 br_forward_finish, NF_BR_PRI_FIRST + 1);
648 /* This function sees both locally originated IP packets and forwarded
649 * IP packets (in both cases the destination device is a bridge
650 * device). It also sees bridged-and-DNAT'ed packets.
651 * To be able to filter on the physical bridge devices (with the physdev
652 * module), we steal packets destined to a bridge device away from the
653 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
654 * when we have determined the real output device. This is done in here.
656 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
657 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
658 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
659 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
661 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
662 * this packet before, and so the packet was locally originated. We fake
663 * the PF_INET/LOCAL_OUT hook.
664 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
665 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
666 * even routed packets that didn't arrive on a bridge interface have their
667 * nf_bridge->physindev set. */
668 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
669 const struct net_device *in,
670 const struct net_device *out,
671 int (*okfn)(struct sk_buff *))
673 struct net_device *realindev, *realoutdev;
674 struct sk_buff *skb = *pskb;
675 struct nf_bridge_info *nf_bridge;
681 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
686 #ifdef CONFIG_NETFILTER_DEBUG
687 /* Sometimes we get packets with NULL ->dst here (for example,
688 * running a dhcp client daemon triggers this). This should now
689 * be fixed, but let's keep the check around. */
690 if (skb->dst == NULL) {
691 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
696 nf_bridge = skb->nf_bridge;
697 nf_bridge->physoutdev = skb->dev;
698 realindev = nf_bridge->physindev;
700 /* Bridged, take PF_BRIDGE/FORWARD.
701 * (see big note in front of br_nf_pre_routing_finish) */
702 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
703 if (nf_bridge->mask & BRNF_PKT_TYPE) {
704 skb->pkt_type = PACKET_OTHERHOST;
705 nf_bridge->mask ^= BRNF_PKT_TYPE;
707 if (skb->protocol == htons(ETH_P_8021Q)) {
708 skb_push(skb, VLAN_HLEN);
709 skb->nh.raw -= VLAN_HLEN;
712 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
713 skb->dev, br_forward_finish);
716 realoutdev = bridge_parent(skb->dev);
720 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
721 /* iptables should match -o br0.x */
722 if (nf_bridge->netoutdev)
723 realoutdev = nf_bridge->netoutdev;
725 if (skb->protocol == htons(ETH_P_8021Q)) {
726 skb_pull(skb, VLAN_HLEN);
727 (*pskb)->nh.raw += VLAN_HLEN;
729 /* IP forwarded traffic has a physindev, locally
730 * generated traffic hasn't. */
731 if (realindev != NULL) {
732 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT)) {
733 struct net_device *parent = bridge_parent(realindev);
738 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
739 realoutdev, br_nf_local_out_finish,
740 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
742 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
743 realoutdev, br_nf_local_out_finish,
744 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
752 /* PF_BRIDGE/POST_ROUTING ********************************************/
753 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
754 const struct net_device *in,
755 const struct net_device *out,
756 int (*okfn)(struct sk_buff *))
758 struct sk_buff *skb = *pskb;
759 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
760 struct net_device *realoutdev = bridge_parent(skb->dev);
763 #ifdef CONFIG_NETFILTER_DEBUG
764 /* Be very paranoid. This probably won't happen anymore, but let's
765 * keep the check just to be sure... */
766 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
767 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
768 "bad mac.raw pointer.");
779 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP(skb))
784 #ifdef CONFIG_NETFILTER_DEBUG
785 if (skb->dst == NULL) {
786 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
791 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
792 * about the value of skb->pkt_type. */
793 if (skb->pkt_type == PACKET_OTHERHOST) {
794 skb->pkt_type = PACKET_HOST;
795 nf_bridge->mask |= BRNF_PKT_TYPE;
798 if (skb->protocol == htons(ETH_P_8021Q)) {
799 skb_pull(skb, VLAN_HLEN);
800 skb->nh.raw += VLAN_HLEN;
803 nf_bridge_save_header(skb);
805 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
806 if (nf_bridge->netoutdev)
807 realoutdev = nf_bridge->netoutdev;
809 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
810 br_dev_queue_push_xmit);
814 #ifdef CONFIG_NETFILTER_DEBUG
816 if (skb->dev != NULL) {
817 printk("[%s]", skb->dev->name);
819 printk("[%s]", realoutdev->name);
821 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
827 /* IP/SABOTAGE *****************************************************/
828 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
829 * for the second time. */
830 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
831 const struct net_device *in,
832 const struct net_device *out,
833 int (*okfn)(struct sk_buff *))
835 if ((*pskb)->nf_bridge &&
836 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
843 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
844 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
845 * decision in the bridge code and have determined nf_bridge->physoutdev. */
846 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
847 const struct net_device *in,
848 const struct net_device *out,
849 int (*okfn)(struct sk_buff *))
851 struct sk_buff *skb = *pskb;
853 if ((out->hard_start_xmit == br_dev_xmit &&
854 okfn != br_nf_forward_finish &&
855 okfn != br_nf_local_out_finish && okfn != br_dev_queue_push_xmit)
856 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
857 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
858 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
861 struct nf_bridge_info *nf_bridge;
863 if (!skb->nf_bridge) {
865 /* This code is executed while in the IP(v6) stack,
866 the version should be 4 or 6. We can't use
867 skb->protocol because that isn't set on
868 PF_INET(6)/LOCAL_OUT. */
869 struct iphdr *ip = skb->nh.iph;
871 if (ip->version == 4 && !brnf_call_iptables)
873 else if (ip->version == 6 && !brnf_call_ip6tables)
876 if (hook == NF_IP_POST_ROUTING)
878 if (!nf_bridge_alloc(skb))
882 nf_bridge = skb->nf_bridge;
884 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
885 * will need the indev then. For a brouter, the real indev
886 * can be a bridge port, so we make sure br_nf_local_out()
887 * doesn't use the bridge parent of the indev by using
888 * the BRNF_DONT_TAKE_PARENT mask. */
889 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
890 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
891 nf_bridge->physindev = (struct net_device *)in;
893 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
894 /* the iptables outdev is br0.x, not br0 */
895 if (out->priv_flags & IFF_802_1Q_VLAN)
896 nf_bridge->netoutdev = (struct net_device *)out;
904 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
905 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
906 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
907 * ip_refrag() can return NF_STOLEN. */
908 static struct nf_hook_ops br_nf_ops[] = {
909 { .hook = br_nf_pre_routing,
910 .owner = THIS_MODULE,
912 .hooknum = NF_BR_PRE_ROUTING,
913 .priority = NF_BR_PRI_BRNF, },
914 { .hook = br_nf_local_in,
915 .owner = THIS_MODULE,
917 .hooknum = NF_BR_LOCAL_IN,
918 .priority = NF_BR_PRI_BRNF, },
919 { .hook = br_nf_forward_ip,
920 .owner = THIS_MODULE,
922 .hooknum = NF_BR_FORWARD,
923 .priority = NF_BR_PRI_BRNF - 1, },
924 { .hook = br_nf_forward_arp,
925 .owner = THIS_MODULE,
927 .hooknum = NF_BR_FORWARD,
928 .priority = NF_BR_PRI_BRNF, },
929 { .hook = br_nf_local_out,
930 .owner = THIS_MODULE,
932 .hooknum = NF_BR_LOCAL_OUT,
933 .priority = NF_BR_PRI_FIRST, },
934 { .hook = br_nf_post_routing,
935 .owner = THIS_MODULE,
937 .hooknum = NF_BR_POST_ROUTING,
938 .priority = NF_BR_PRI_LAST, },
939 { .hook = ip_sabotage_in,
940 .owner = THIS_MODULE,
942 .hooknum = NF_IP_PRE_ROUTING,
943 .priority = NF_IP_PRI_FIRST, },
944 { .hook = ip_sabotage_in,
945 .owner = THIS_MODULE,
947 .hooknum = NF_IP6_PRE_ROUTING,
948 .priority = NF_IP6_PRI_FIRST, },
949 { .hook = ip_sabotage_out,
950 .owner = THIS_MODULE,
952 .hooknum = NF_IP_FORWARD,
953 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
954 { .hook = ip_sabotage_out,
955 .owner = THIS_MODULE,
957 .hooknum = NF_IP6_FORWARD,
958 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
959 { .hook = ip_sabotage_out,
960 .owner = THIS_MODULE,
962 .hooknum = NF_IP_LOCAL_OUT,
963 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
964 { .hook = ip_sabotage_out,
965 .owner = THIS_MODULE,
967 .hooknum = NF_IP6_LOCAL_OUT,
968 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
969 { .hook = ip_sabotage_out,
970 .owner = THIS_MODULE,
972 .hooknum = NF_IP_POST_ROUTING,
973 .priority = NF_IP_PRI_FIRST, },
974 { .hook = ip_sabotage_out,
975 .owner = THIS_MODULE,
977 .hooknum = NF_IP6_POST_ROUTING,
978 .priority = NF_IP6_PRI_FIRST, },
983 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
984 void __user * buffer, size_t * lenp, loff_t * ppos)
988 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
990 if (write && *(int *)(ctl->data))
991 *(int *)(ctl->data) = 1;
995 static ctl_table brnf_table[] = {
997 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
998 .procname = "bridge-nf-call-arptables",
999 .data = &brnf_call_arptables,
1000 .maxlen = sizeof(int),
1002 .proc_handler = &brnf_sysctl_call_tables,
1005 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
1006 .procname = "bridge-nf-call-iptables",
1007 .data = &brnf_call_iptables,
1008 .maxlen = sizeof(int),
1010 .proc_handler = &brnf_sysctl_call_tables,
1013 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
1014 .procname = "bridge-nf-call-ip6tables",
1015 .data = &brnf_call_ip6tables,
1016 .maxlen = sizeof(int),
1018 .proc_handler = &brnf_sysctl_call_tables,
1021 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1022 .procname = "bridge-nf-filter-vlan-tagged",
1023 .data = &brnf_filter_vlan_tagged,
1024 .maxlen = sizeof(int),
1026 .proc_handler = &brnf_sysctl_call_tables,
1031 static ctl_table brnf_bridge_table[] = {
1033 .ctl_name = NET_BRIDGE,
1034 .procname = "bridge",
1036 .child = brnf_table,
1041 static ctl_table brnf_net_table[] = {
1043 .ctl_name = CTL_NET,
1046 .child = brnf_bridge_table,
1052 int br_netfilter_init(void)
1056 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1059 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1063 nf_unregister_hook(&br_nf_ops[i]);
1068 #ifdef CONFIG_SYSCTL
1069 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1070 if (brnf_sysctl_header == NULL) {
1072 "br_netfilter: can't register to sysctl.\n");
1073 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1074 nf_unregister_hook(&br_nf_ops[i]);
1079 printk(KERN_NOTICE "Bridge firewalling registered\n");
1084 void br_netfilter_fini(void)
1088 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1089 nf_unregister_hook(&br_nf_ops[i]);
1090 #ifdef CONFIG_SYSCTL
1091 unregister_sysctl_table(brnf_sysctl_header);