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 #define IS_VLAN_IP (skb->protocol == htons(ETH_P_8021Q) && \
65 hdr->h_vlan_encapsulated_proto == htons(ETH_P_IP) && \
66 brnf_filter_vlan_tagged)
67 #define IS_VLAN_IPV6 (skb->protocol == htons(ETH_P_8021Q) && \
68 hdr->h_vlan_encapsulated_proto == htons(ETH_P_IPV6) && \
69 brnf_filter_vlan_tagged)
70 #define IS_VLAN_ARP (skb->protocol == htons(ETH_P_8021Q) && \
71 hdr->h_vlan_encapsulated_proto == htons(ETH_P_ARP) && \
72 brnf_filter_vlan_tagged)
74 /* We need these fake structures to make netfilter happy --
75 * lots of places assume that skb->dst != NULL, which isn't
76 * all that unreasonable.
78 * Currently, we fill in the PMTU entry because netfilter
79 * refragmentation needs it, and the rt_flags entry because
80 * ipt_REJECT needs it. Future netfilter modules might
81 * require us to fill additional fields. */
82 static struct net_device __fake_net_device = {
83 .hard_header_len = ETH_HLEN
86 static struct rtable __fake_rtable = {
89 .__refcnt = ATOMIC_INIT(1),
90 .dev = &__fake_net_device,
91 .path = &__fake_rtable.u.dst,
92 .metrics = {[RTAX_MTU - 1] = 1500},
99 static inline struct net_device *bridge_parent(const struct net_device *dev)
101 struct net_bridge_port *port = rcu_dereference(dev->br_port);
103 return port ? port->br->dev : NULL;
106 /* PF_BRIDGE/PRE_ROUTING *********************************************/
107 /* Undo the changes made for ip6tables PREROUTING and continue the
108 * bridge PRE_ROUTING hook. */
109 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
111 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
113 if (nf_bridge->mask & BRNF_PKT_TYPE) {
114 skb->pkt_type = PACKET_OTHERHOST;
115 nf_bridge->mask ^= BRNF_PKT_TYPE;
117 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
119 skb->dst = (struct dst_entry *)&__fake_rtable;
122 skb->dev = nf_bridge->physindev;
123 if (skb->protocol == htons(ETH_P_8021Q)) {
124 skb_push(skb, VLAN_HLEN);
125 skb->nh.raw -= VLAN_HLEN;
127 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
128 br_handle_frame_finish, 1);
133 static void __br_dnat_complain(void)
135 static unsigned long last_complaint;
137 if (jiffies - last_complaint >= 5 * HZ) {
138 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
139 "forwarding to be enabled\n");
140 last_complaint = jiffies;
144 /* This requires some explaining. If DNAT has taken place,
145 * we will need to fix up the destination Ethernet address,
146 * and this is a tricky process.
148 * There are two cases to consider:
149 * 1. The packet was DNAT'ed to a device in the same bridge
150 * port group as it was received on. We can still bridge
152 * 2. The packet was DNAT'ed to a different device, either
153 * a non-bridged device or another bridge port group.
154 * The packet will need to be routed.
156 * The correct way of distinguishing between these two cases is to
157 * call ip_route_input() and to look at skb->dst->dev, which is
158 * changed to the destination device if ip_route_input() succeeds.
160 * Let us first consider the case that ip_route_input() succeeds:
162 * If skb->dst->dev equals the logical bridge device the packet
163 * came in on, we can consider this bridging. We then call
164 * skb->dst->output() which will make the packet enter br_nf_local_out()
165 * not much later. In that function it is assured that the iptables
166 * FORWARD chain is traversed for the packet.
168 * Otherwise, the packet is considered to be routed and we just
169 * change the destination MAC address so that the packet will
170 * later be passed up to the IP stack to be routed.
172 * Let us now consider the case that ip_route_input() fails:
174 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
175 * will fail, while __ip_route_output_key() will return success. The source
176 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
177 * thinks we're handling a locally generated packet and won't care
178 * if IP forwarding is allowed. We send a warning message to the users's
179 * log telling her to put IP forwarding on.
181 * ip_route_input() will also fail if there is no route available.
182 * In that case we just drop the packet.
184 * --Lennert, 20020411
185 * --Bart, 20020416 (updated)
186 * --Bart, 20021007 (updated) */
187 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
189 if (skb->pkt_type == PACKET_OTHERHOST) {
190 skb->pkt_type = PACKET_HOST;
191 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
193 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
195 skb->dev = bridge_parent(skb->dev);
199 if (skb->protocol == htons(ETH_P_8021Q)) {
200 skb_pull(skb, VLAN_HLEN);
201 skb->nh.raw += VLAN_HLEN;
203 skb->dst->output(skb);
208 static int br_nf_pre_routing_finish(struct sk_buff *skb)
210 struct net_device *dev = skb->dev;
211 struct iphdr *iph = skb->nh.iph;
212 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
214 if (nf_bridge->mask & BRNF_PKT_TYPE) {
215 skb->pkt_type = PACKET_OTHERHOST;
216 nf_bridge->mask ^= BRNF_PKT_TYPE;
218 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
220 if (dnat_took_place(skb)) {
221 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, dev)) {
228 .tos = RT_TOS(iph->tos) },
233 if (!ip_route_output_key(&rt, &fl)) {
234 /* - Bridged-and-DNAT'ed traffic doesn't
235 * require ip_forwarding.
236 * - Deal with redirected traffic. */
237 if (((struct dst_entry *)rt)->dev == dev ||
238 rt->rt_type == RTN_LOCAL) {
239 skb->dst = (struct dst_entry *)rt;
242 __br_dnat_complain();
243 dst_release((struct dst_entry *)rt);
248 if (skb->dst->dev == dev) {
250 /* Tell br_nf_local_out this is a
252 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
253 skb->dev = nf_bridge->physindev;
255 htons(ETH_P_8021Q)) {
256 skb_push(skb, VLAN_HLEN);
257 skb->nh.raw -= VLAN_HLEN;
259 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING,
261 br_nf_pre_routing_finish_bridge,
265 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr, ETH_ALEN);
266 skb->pkt_type = PACKET_HOST;
269 skb->dst = (struct dst_entry *)&__fake_rtable;
273 skb->dev = nf_bridge->physindev;
274 if (skb->protocol == htons(ETH_P_8021Q)) {
275 skb_push(skb, VLAN_HLEN);
276 skb->nh.raw -= VLAN_HLEN;
278 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
279 br_handle_frame_finish, 1);
284 /* Some common code for IPv4/IPv6 */
285 static struct net_device *setup_pre_routing(struct sk_buff *skb)
287 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
289 if (skb->pkt_type == PACKET_OTHERHOST) {
290 skb->pkt_type = PACKET_HOST;
291 nf_bridge->mask |= BRNF_PKT_TYPE;
294 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
295 nf_bridge->physindev = skb->dev;
296 skb->dev = bridge_parent(skb->dev);
301 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
302 static int check_hbh_len(struct sk_buff *skb)
304 unsigned char *raw = (u8 *) (skb->nh.ipv6h + 1);
306 int off = raw - skb->nh.raw;
307 int len = (raw[1] + 1) << 3;
309 if ((raw + len) - skb->data > skb_headlen(skb))
316 int optlen = skb->nh.raw[off + 1] + 2;
318 switch (skb->nh.raw[off]) {
327 if (skb->nh.raw[off + 1] != 4 || (off & 3) != 2)
329 pkt_len = ntohl(*(u32 *) (skb->nh.raw + off + 2));
330 if (pkt_len <= IPV6_MAXPLEN ||
331 skb->nh.ipv6h->payload_len)
333 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
335 if (pskb_trim_rcsum(skb,
336 pkt_len + sizeof(struct ipv6hdr)))
354 /* Replicate the checks that IPv6 does on packet reception and pass the packet
355 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
356 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
358 const struct net_device *in,
359 const struct net_device *out,
360 int (*okfn)(struct sk_buff *))
364 struct nf_bridge_info *nf_bridge;
366 if (skb->len < sizeof(struct ipv6hdr))
369 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
374 if (hdr->version != 6)
377 pkt_len = ntohs(hdr->payload_len);
379 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
380 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
382 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
383 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
385 if (skb->ip_summed == CHECKSUM_HW)
386 skb->ip_summed = CHECKSUM_NONE;
389 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
392 nf_bridge_put(skb->nf_bridge);
393 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
395 if (!setup_pre_routing(skb))
398 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
399 br_nf_pre_routing_finish_ipv6);
407 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
408 * Replicate the checks that IPv4 does on packet reception.
409 * Set skb->dev to the bridge device (i.e. parent of the
410 * receiving device) to make netfilter happy, the REDIRECT
411 * target in particular. Save the original destination IP
412 * address to be able to detect DNAT afterwards. */
413 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
414 const struct net_device *in,
415 const struct net_device *out,
416 int (*okfn)(struct sk_buff *))
420 struct sk_buff *skb = *pskb;
421 struct nf_bridge_info *nf_bridge;
422 struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
424 if (skb->protocol == htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
426 if (!brnf_call_ip6tables)
429 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
432 if (skb->protocol == htons(ETH_P_8021Q)) {
433 skb_pull_rcsum(skb, VLAN_HLEN);
434 skb->nh.raw += VLAN_HLEN;
436 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
439 if (!brnf_call_iptables)
443 if (skb->protocol != htons(ETH_P_IP) && !IS_VLAN_IP)
446 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
449 if (skb->protocol == htons(ETH_P_8021Q)) {
450 skb_pull_rcsum(skb, VLAN_HLEN);
451 skb->nh.raw += VLAN_HLEN;
454 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
458 if (iph->ihl < 5 || iph->version != 4)
461 if (!pskb_may_pull(skb, 4 * iph->ihl))
465 if (ip_fast_csum((__u8 *) iph, iph->ihl) != 0)
468 len = ntohs(iph->tot_len);
469 if (skb->len < len || len < 4 * iph->ihl)
472 if (skb->len > len) {
473 __pskb_trim(skb, len);
474 if (skb->ip_summed == CHECKSUM_HW)
475 skb->ip_summed = CHECKSUM_NONE;
478 nf_bridge_put(skb->nf_bridge);
479 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
481 if (!setup_pre_routing(skb))
483 store_orig_dstaddr(skb);
485 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
486 br_nf_pre_routing_finish);
491 // IP_INC_STATS_BH(IpInHdrErrors);
497 /* PF_BRIDGE/LOCAL_IN ************************************************/
498 /* The packet is locally destined, which requires a real
499 * dst_entry, so detach the fake one. On the way up, the
500 * packet would pass through PRE_ROUTING again (which already
501 * took place when the packet entered the bridge), but we
502 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
503 * prevent this from happening. */
504 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
505 const struct net_device *in,
506 const struct net_device *out,
507 int (*okfn)(struct sk_buff *))
509 struct sk_buff *skb = *pskb;
511 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
512 dst_release(skb->dst);
519 /* PF_BRIDGE/FORWARD *************************************************/
520 static int br_nf_forward_finish(struct sk_buff *skb)
522 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
523 struct net_device *in;
524 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
526 if (skb->protocol != htons(ETH_P_ARP) && !IS_VLAN_ARP) {
527 in = nf_bridge->physindev;
528 if (nf_bridge->mask & BRNF_PKT_TYPE) {
529 skb->pkt_type = PACKET_OTHERHOST;
530 nf_bridge->mask ^= BRNF_PKT_TYPE;
533 in = *((struct net_device **)(skb->cb));
535 if (skb->protocol == htons(ETH_P_8021Q)) {
536 skb_push(skb, VLAN_HLEN);
537 skb->nh.raw -= VLAN_HLEN;
539 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
540 skb->dev, br_forward_finish, 1);
544 /* This is the 'purely bridged' case. For IP, we pass the packet to
545 * netfilter with indev and outdev set to the bridge device,
546 * but we are still able to filter on the 'real' indev/outdev
547 * because of the physdev module. For ARP, indev and outdev are the
549 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
550 const struct net_device *in,
551 const struct net_device *out,
552 int (*okfn)(struct sk_buff *))
554 struct sk_buff *skb = *pskb;
555 struct nf_bridge_info *nf_bridge;
556 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
557 struct net_device *parent;
563 parent = bridge_parent(out);
567 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP)
572 if (skb->protocol == htons(ETH_P_8021Q)) {
573 skb_pull(*pskb, VLAN_HLEN);
574 (*pskb)->nh.raw += VLAN_HLEN;
577 nf_bridge = skb->nf_bridge;
578 if (skb->pkt_type == PACKET_OTHERHOST) {
579 skb->pkt_type = PACKET_HOST;
580 nf_bridge->mask |= BRNF_PKT_TYPE;
583 /* The physdev module checks on this */
584 nf_bridge->mask |= BRNF_BRIDGED;
585 nf_bridge->physoutdev = skb->dev;
587 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in), parent,
588 br_nf_forward_finish);
593 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
594 const struct net_device *in,
595 const struct net_device *out,
596 int (*okfn)(struct sk_buff *))
598 struct sk_buff *skb = *pskb;
599 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
600 struct net_device **d = (struct net_device **)(skb->cb);
603 if (!brnf_call_arptables)
607 if (skb->protocol != htons(ETH_P_ARP)) {
610 skb_pull(*pskb, VLAN_HLEN);
611 (*pskb)->nh.raw += VLAN_HLEN;
614 if (skb->nh.arph->ar_pln != 4) {
616 skb_push(*pskb, VLAN_HLEN);
617 (*pskb)->nh.raw -= VLAN_HLEN;
621 *d = (struct net_device *)in;
622 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
623 (struct net_device *)out, br_nf_forward_finish);
628 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
629 static int br_nf_local_out_finish(struct sk_buff *skb)
631 if (skb->protocol == htons(ETH_P_8021Q)) {
632 skb_push(skb, VLAN_HLEN);
633 skb->nh.raw -= VLAN_HLEN;
636 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
637 br_forward_finish, NF_BR_PRI_FIRST + 1);
642 /* This function sees both locally originated IP packets and forwarded
643 * IP packets (in both cases the destination device is a bridge
644 * device). It also sees bridged-and-DNAT'ed packets.
645 * To be able to filter on the physical bridge devices (with the physdev
646 * module), we steal packets destined to a bridge device away from the
647 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
648 * when we have determined the real output device. This is done in here.
650 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
651 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
652 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
653 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
655 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
656 * this packet before, and so the packet was locally originated. We fake
657 * the PF_INET/LOCAL_OUT hook.
658 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
659 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
660 * even routed packets that didn't arrive on a bridge interface have their
661 * nf_bridge->physindev set. */
662 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
663 const struct net_device *in,
664 const struct net_device *out,
665 int (*okfn)(struct sk_buff *))
667 struct net_device *realindev, *realoutdev;
668 struct sk_buff *skb = *pskb;
669 struct nf_bridge_info *nf_bridge;
670 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
676 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP)
681 #ifdef CONFIG_NETFILTER_DEBUG
682 /* Sometimes we get packets with NULL ->dst here (for example,
683 * running a dhcp client daemon triggers this). This should now
684 * be fixed, but let's keep the check around. */
685 if (skb->dst == NULL) {
686 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
691 nf_bridge = skb->nf_bridge;
692 nf_bridge->physoutdev = skb->dev;
693 realindev = nf_bridge->physindev;
695 /* Bridged, take PF_BRIDGE/FORWARD.
696 * (see big note in front of br_nf_pre_routing_finish) */
697 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
698 if (nf_bridge->mask & BRNF_PKT_TYPE) {
699 skb->pkt_type = PACKET_OTHERHOST;
700 nf_bridge->mask ^= BRNF_PKT_TYPE;
702 if (skb->protocol == htons(ETH_P_8021Q)) {
703 skb_push(skb, VLAN_HLEN);
704 skb->nh.raw -= VLAN_HLEN;
707 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
708 skb->dev, br_forward_finish);
711 realoutdev = bridge_parent(skb->dev);
715 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
716 /* iptables should match -o br0.x */
717 if (nf_bridge->netoutdev)
718 realoutdev = nf_bridge->netoutdev;
720 if (skb->protocol == htons(ETH_P_8021Q)) {
721 skb_pull(skb, VLAN_HLEN);
722 (*pskb)->nh.raw += VLAN_HLEN;
724 /* IP forwarded traffic has a physindev, locally
725 * generated traffic hasn't. */
726 if (realindev != NULL) {
727 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT)) {
728 struct net_device *parent = bridge_parent(realindev);
733 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
734 realoutdev, br_nf_local_out_finish,
735 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
737 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
738 realoutdev, br_nf_local_out_finish,
739 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
747 /* PF_BRIDGE/POST_ROUTING ********************************************/
748 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
749 const struct net_device *in,
750 const struct net_device *out,
751 int (*okfn)(struct sk_buff *))
753 struct sk_buff *skb = *pskb;
754 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
755 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
756 struct net_device *realoutdev = bridge_parent(skb->dev);
759 #ifdef CONFIG_NETFILTER_DEBUG
760 /* Be very paranoid. This probably won't happen anymore, but let's
761 * keep the check just to be sure... */
762 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
763 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
764 "bad mac.raw pointer.");
775 if (skb->protocol == htons(ETH_P_IP) || IS_VLAN_IP)
780 #ifdef CONFIG_NETFILTER_DEBUG
781 if (skb->dst == NULL) {
782 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
787 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
788 * about the value of skb->pkt_type. */
789 if (skb->pkt_type == PACKET_OTHERHOST) {
790 skb->pkt_type = PACKET_HOST;
791 nf_bridge->mask |= BRNF_PKT_TYPE;
794 if (skb->protocol == htons(ETH_P_8021Q)) {
795 skb_pull(skb, VLAN_HLEN);
796 skb->nh.raw += VLAN_HLEN;
799 nf_bridge_save_header(skb);
801 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
802 if (nf_bridge->netoutdev)
803 realoutdev = nf_bridge->netoutdev;
805 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
806 br_dev_queue_push_xmit);
810 #ifdef CONFIG_NETFILTER_DEBUG
812 if (skb->dev != NULL) {
813 printk("[%s]", skb->dev->name);
815 printk("[%s]", realoutdev->name);
817 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
823 /* IP/SABOTAGE *****************************************************/
824 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
825 * for the second time. */
826 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
827 const struct net_device *in,
828 const struct net_device *out,
829 int (*okfn)(struct sk_buff *))
831 if ((*pskb)->nf_bridge &&
832 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
839 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
840 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
841 * decision in the bridge code and have determined nf_bridge->physoutdev. */
842 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
843 const struct net_device *in,
844 const struct net_device *out,
845 int (*okfn)(struct sk_buff *))
847 struct sk_buff *skb = *pskb;
849 if ((out->hard_start_xmit == br_dev_xmit &&
850 okfn != br_nf_forward_finish &&
851 okfn != br_nf_local_out_finish && okfn != br_dev_queue_push_xmit)
852 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
853 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
854 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
857 struct nf_bridge_info *nf_bridge;
859 if (!skb->nf_bridge) {
861 /* This code is executed while in the IP(v6) stack,
862 the version should be 4 or 6. We can't use
863 skb->protocol because that isn't set on
864 PF_INET(6)/LOCAL_OUT. */
865 struct iphdr *ip = skb->nh.iph;
867 if (ip->version == 4 && !brnf_call_iptables)
869 else if (ip->version == 6 && !brnf_call_ip6tables)
872 if (hook == NF_IP_POST_ROUTING)
874 if (!nf_bridge_alloc(skb))
878 nf_bridge = skb->nf_bridge;
880 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
881 * will need the indev then. For a brouter, the real indev
882 * can be a bridge port, so we make sure br_nf_local_out()
883 * doesn't use the bridge parent of the indev by using
884 * the BRNF_DONT_TAKE_PARENT mask. */
885 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
886 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
887 nf_bridge->physindev = (struct net_device *)in;
889 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
890 /* the iptables outdev is br0.x, not br0 */
891 if (out->priv_flags & IFF_802_1Q_VLAN)
892 nf_bridge->netoutdev = (struct net_device *)out;
900 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
901 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
902 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
903 * ip_refrag() can return NF_STOLEN. */
904 static struct nf_hook_ops br_nf_ops[] = {
905 { .hook = br_nf_pre_routing,
906 .owner = THIS_MODULE,
908 .hooknum = NF_BR_PRE_ROUTING,
909 .priority = NF_BR_PRI_BRNF, },
910 { .hook = br_nf_local_in,
911 .owner = THIS_MODULE,
913 .hooknum = NF_BR_LOCAL_IN,
914 .priority = NF_BR_PRI_BRNF, },
915 { .hook = br_nf_forward_ip,
916 .owner = THIS_MODULE,
918 .hooknum = NF_BR_FORWARD,
919 .priority = NF_BR_PRI_BRNF - 1, },
920 { .hook = br_nf_forward_arp,
921 .owner = THIS_MODULE,
923 .hooknum = NF_BR_FORWARD,
924 .priority = NF_BR_PRI_BRNF, },
925 { .hook = br_nf_local_out,
926 .owner = THIS_MODULE,
928 .hooknum = NF_BR_LOCAL_OUT,
929 .priority = NF_BR_PRI_FIRST, },
930 { .hook = br_nf_post_routing,
931 .owner = THIS_MODULE,
933 .hooknum = NF_BR_POST_ROUTING,
934 .priority = NF_BR_PRI_LAST, },
935 { .hook = ip_sabotage_in,
936 .owner = THIS_MODULE,
938 .hooknum = NF_IP_PRE_ROUTING,
939 .priority = NF_IP_PRI_FIRST, },
940 { .hook = ip_sabotage_in,
941 .owner = THIS_MODULE,
943 .hooknum = NF_IP6_PRE_ROUTING,
944 .priority = NF_IP6_PRI_FIRST, },
945 { .hook = ip_sabotage_out,
946 .owner = THIS_MODULE,
948 .hooknum = NF_IP_FORWARD,
949 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
950 { .hook = ip_sabotage_out,
951 .owner = THIS_MODULE,
953 .hooknum = NF_IP6_FORWARD,
954 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
955 { .hook = ip_sabotage_out,
956 .owner = THIS_MODULE,
958 .hooknum = NF_IP_LOCAL_OUT,
959 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
960 { .hook = ip_sabotage_out,
961 .owner = THIS_MODULE,
963 .hooknum = NF_IP6_LOCAL_OUT,
964 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
965 { .hook = ip_sabotage_out,
966 .owner = THIS_MODULE,
968 .hooknum = NF_IP_POST_ROUTING,
969 .priority = NF_IP_PRI_FIRST, },
970 { .hook = ip_sabotage_out,
971 .owner = THIS_MODULE,
973 .hooknum = NF_IP6_POST_ROUTING,
974 .priority = NF_IP6_PRI_FIRST, },
979 int brnf_sysctl_call_tables(ctl_table * ctl, int write, struct file *filp,
980 void __user * buffer, size_t * lenp, loff_t * ppos)
984 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
986 if (write && *(int *)(ctl->data))
987 *(int *)(ctl->data) = 1;
991 static ctl_table brnf_table[] = {
993 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
994 .procname = "bridge-nf-call-arptables",
995 .data = &brnf_call_arptables,
996 .maxlen = sizeof(int),
998 .proc_handler = &brnf_sysctl_call_tables,
1001 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
1002 .procname = "bridge-nf-call-iptables",
1003 .data = &brnf_call_iptables,
1004 .maxlen = sizeof(int),
1006 .proc_handler = &brnf_sysctl_call_tables,
1009 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
1010 .procname = "bridge-nf-call-ip6tables",
1011 .data = &brnf_call_ip6tables,
1012 .maxlen = sizeof(int),
1014 .proc_handler = &brnf_sysctl_call_tables,
1017 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
1018 .procname = "bridge-nf-filter-vlan-tagged",
1019 .data = &brnf_filter_vlan_tagged,
1020 .maxlen = sizeof(int),
1022 .proc_handler = &brnf_sysctl_call_tables,
1027 static ctl_table brnf_bridge_table[] = {
1029 .ctl_name = NET_BRIDGE,
1030 .procname = "bridge",
1032 .child = brnf_table,
1037 static ctl_table brnf_net_table[] = {
1039 .ctl_name = CTL_NET,
1042 .child = brnf_bridge_table,
1048 int br_netfilter_init(void)
1052 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1055 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1059 nf_unregister_hook(&br_nf_ops[i]);
1064 #ifdef CONFIG_SYSCTL
1065 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1066 if (brnf_sysctl_header == NULL) {
1068 "br_netfilter: can't register to sysctl.\n");
1069 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1070 nf_unregister_hook(&br_nf_ops[i]);
1075 printk(KERN_NOTICE "Bridge firewalling registered\n");
1080 void br_netfilter_fini(void)
1084 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1085 nf_unregister_hook(&br_nf_ops[i]);
1086 #ifdef CONFIG_SYSCTL
1087 unregister_sysctl_table(brnf_sysctl_header);