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_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/netfilter_bridge.h>
32 #include <linux/netfilter_ipv4.h>
33 #include <linux/netfilter_ipv6.h>
34 #include <linux/netfilter_arp.h>
35 #include <linux/in_route.h>
38 #include <asm/uaccess.h>
39 #include <asm/checksum.h>
40 #include "br_private.h"
42 #include <linux/sysctl.h>
45 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
48 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
50 #define has_bridge_parent(device) ((device)->br_port != NULL)
51 #define bridge_parent(device) ((device)->br_port->br->dev)
54 static struct ctl_table_header *brnf_sysctl_header;
55 static int brnf_call_iptables = 1;
56 static int brnf_call_ip6tables = 1;
57 static int brnf_call_arptables = 1;
58 static int brnf_filter_vlan_tagged = 1;
60 #define brnf_filter_vlan_tagged 1
63 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
64 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
65 brnf_filter_vlan_tagged)
66 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
67 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
68 brnf_filter_vlan_tagged)
69 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
70 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
71 brnf_filter_vlan_tagged)
73 /* We need these fake structures to make netfilter happy --
74 * lots of places assume that skb->dst != NULL, which isn't
75 * all that unreasonable.
77 * Currently, we fill in the PMTU entry because netfilter
78 * refragmentation needs it, and the rt_flags entry because
79 * ipt_REJECT needs it. Future netfilter modules might
80 * require us to fill additional fields. */
81 static struct net_device __fake_net_device = {
82 .hard_header_len = ETH_HLEN
85 static struct rtable __fake_rtable = {
88 .__refcnt = ATOMIC_INIT(1),
89 .dev = &__fake_net_device,
90 .path = &__fake_rtable.u.dst,
91 .metrics = {[RTAX_MTU - 1] = 1500},
98 /* PF_BRIDGE/PRE_ROUTING *********************************************/
99 /* Undo the changes made for ip6tables PREROUTING and continue the
100 * bridge PRE_ROUTING hook. */
101 static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
103 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
105 if (nf_bridge->mask & BRNF_PKT_TYPE) {
106 skb->pkt_type = PACKET_OTHERHOST;
107 nf_bridge->mask ^= BRNF_PKT_TYPE;
109 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
111 skb->dst = (struct dst_entry *)&__fake_rtable;
114 skb->dev = nf_bridge->physindev;
115 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
116 skb_push(skb, VLAN_HLEN);
117 skb->nh.raw -= VLAN_HLEN;
119 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
120 br_handle_frame_finish, 1);
125 static void __br_dnat_complain(void)
127 static unsigned long last_complaint;
129 if (jiffies - last_complaint >= 5 * HZ) {
130 printk(KERN_WARNING "Performing cross-bridge DNAT requires IP "
131 "forwarding to be enabled\n");
132 last_complaint = jiffies;
136 /* This requires some explaining. If DNAT has taken place,
137 * we will need to fix up the destination Ethernet address,
138 * and this is a tricky process.
140 * There are two cases to consider:
141 * 1. The packet was DNAT'ed to a device in the same bridge
142 * port group as it was received on. We can still bridge
144 * 2. The packet was DNAT'ed to a different device, either
145 * a non-bridged device or another bridge port group.
146 * The packet will need to be routed.
148 * The correct way of distinguishing between these two cases is to
149 * call ip_route_input() and to look at skb->dst->dev, which is
150 * changed to the destination device if ip_route_input() succeeds.
152 * Let us first consider the case that ip_route_input() succeeds:
154 * If skb->dst->dev equals the logical bridge device the packet
155 * came in on, we can consider this bridging. We then call
156 * skb->dst->output() which will make the packet enter br_nf_local_out()
157 * not much later. In that function it is assured that the iptables
158 * FORWARD chain is traversed for the packet.
160 * Otherwise, the packet is considered to be routed and we just
161 * change the destination MAC address so that the packet will
162 * later be passed up to the IP stack to be routed.
164 * Let us now consider the case that ip_route_input() fails:
166 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
167 * will fail, while __ip_route_output_key() will return success. The source
168 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
169 * thinks we're handling a locally generated packet and won't care
170 * if IP forwarding is allowed. We send a warning message to the users's
171 * log telling her to put IP forwarding on.
173 * ip_route_input() will also fail if there is no route available.
174 * In that case we just drop the packet.
176 * --Lennert, 20020411
177 * --Bart, 20020416 (updated)
178 * --Bart, 20021007 (updated) */
179 static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
181 if (skb->pkt_type == PACKET_OTHERHOST) {
182 skb->pkt_type = PACKET_HOST;
183 skb->nf_bridge->mask |= BRNF_PKT_TYPE;
185 skb->nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
187 skb->dev = bridge_parent(skb->dev);
188 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
189 skb_pull(skb, VLAN_HLEN);
190 skb->nh.raw += VLAN_HLEN;
192 skb->dst->output(skb);
196 static int br_nf_pre_routing_finish(struct sk_buff *skb)
198 struct net_device *dev = skb->dev;
199 struct iphdr *iph = skb->nh.iph;
200 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
202 if (nf_bridge->mask & BRNF_PKT_TYPE) {
203 skb->pkt_type = PACKET_OTHERHOST;
204 nf_bridge->mask ^= BRNF_PKT_TYPE;
206 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
208 if (dnat_took_place(skb)) {
209 if (ip_route_input(skb, iph->daddr, iph->saddr, iph->tos,
212 struct flowi fl = { .nl_u =
213 { .ip4_u = { .daddr = iph->daddr, .saddr = 0 ,
214 .tos = RT_TOS(iph->tos)} }, .proto = 0};
216 if (!ip_route_output_key(&rt, &fl)) {
217 /* - Bridged-and-DNAT'ed traffic doesn't
218 * require ip_forwarding.
219 * - Deal with redirected traffic. */
220 if (((struct dst_entry *)rt)->dev == dev ||
221 rt->rt_type == RTN_LOCAL) {
222 skb->dst = (struct dst_entry *)rt;
225 __br_dnat_complain();
226 dst_release((struct dst_entry *)rt);
231 if (skb->dst->dev == dev) {
233 /* Tell br_nf_local_out this is a
235 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
236 skb->dev = nf_bridge->physindev;
238 __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,
244 br_nf_pre_routing_finish_bridge,
248 memcpy(eth_hdr(skb)->h_dest, dev->dev_addr,
250 skb->pkt_type = PACKET_HOST;
253 skb->dst = (struct dst_entry *)&__fake_rtable;
257 skb->dev = nf_bridge->physindev;
258 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
259 skb_push(skb, VLAN_HLEN);
260 skb->nh.raw -= VLAN_HLEN;
262 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
263 br_handle_frame_finish, 1);
268 /* Some common code for IPv4/IPv6 */
269 static void setup_pre_routing(struct sk_buff *skb)
271 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
273 if (skb->pkt_type == PACKET_OTHERHOST) {
274 skb->pkt_type = PACKET_HOST;
275 nf_bridge->mask |= BRNF_PKT_TYPE;
278 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
279 nf_bridge->physindev = skb->dev;
280 skb->dev = bridge_parent(skb->dev);
283 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
284 static int check_hbh_len(struct sk_buff *skb)
286 unsigned char *raw = (u8*)(skb->nh.ipv6h+1);
288 int off = raw - skb->nh.raw;
289 int len = (raw[1]+1)<<3;
291 if ((raw + len) - skb->data > skb_headlen(skb))
298 int optlen = raw[off+1]+2;
300 switch (skb->nh.raw[off]) {
309 if (skb->nh.raw[off+1] != 4 || (off&3) != 2)
312 pkt_len = ntohl(*(u32*)(skb->nh.raw+off+2));
314 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
316 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
318 pkt_len + sizeof(struct ipv6hdr)))
320 if (skb->ip_summed == CHECKSUM_HW)
321 skb->ip_summed = CHECKSUM_NONE;
339 /* Replicate the checks that IPv6 does on packet reception and pass the packet
340 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
341 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
342 struct sk_buff *skb, const struct net_device *in,
343 const struct net_device *out, int (*okfn)(struct sk_buff *))
347 struct nf_bridge_info *nf_bridge;
349 if (skb->len < sizeof(struct ipv6hdr))
352 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
357 if (hdr->version != 6)
360 pkt_len = ntohs(hdr->payload_len);
362 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
363 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
365 if (pkt_len + sizeof(struct ipv6hdr) < skb->len) {
366 if (__pskb_trim(skb, pkt_len + sizeof(struct ipv6hdr)))
368 if (skb->ip_summed == CHECKSUM_HW)
369 skb->ip_summed = CHECKSUM_NONE;
372 if (hdr->nexthdr == NEXTHDR_HOP && check_hbh_len(skb))
375 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
377 setup_pre_routing(skb);
379 NF_HOOK(PF_INET6, NF_IP6_PRE_ROUTING, skb, skb->dev, NULL,
380 br_nf_pre_routing_finish_ipv6);
388 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
389 * Replicate the checks that IPv4 does on packet reception.
390 * Set skb->dev to the bridge device (i.e. parent of the
391 * receiving device) to make netfilter happy, the REDIRECT
392 * target in particular. Save the original destination IP
393 * address to be able to detect DNAT afterwards. */
394 static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff **pskb,
395 const struct net_device *in, const struct net_device *out,
396 int (*okfn)(struct sk_buff *))
400 struct sk_buff *skb = *pskb;
401 struct nf_bridge_info *nf_bridge;
402 struct vlan_ethhdr *hdr = vlan_eth_hdr(*pskb);
404 if (skb->protocol == __constant_htons(ETH_P_IPV6) || IS_VLAN_IPV6) {
406 if (!brnf_call_ip6tables)
409 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
412 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
413 skb_pull(skb, VLAN_HLEN);
414 (skb)->nh.raw += VLAN_HLEN;
416 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn);
419 if (!brnf_call_iptables)
423 if (skb->protocol != __constant_htons(ETH_P_IP) && !IS_VLAN_IP)
426 if ((skb = skb_share_check(*pskb, GFP_ATOMIC)) == NULL)
429 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
430 skb_pull(skb, VLAN_HLEN);
431 (skb)->nh.raw += VLAN_HLEN;
434 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
438 if (iph->ihl < 5 || iph->version != 4)
441 if (!pskb_may_pull(skb, 4*iph->ihl))
445 if (ip_fast_csum((__u8 *)iph, iph->ihl) != 0)
448 len = ntohs(iph->tot_len);
449 if (skb->len < len || len < 4*iph->ihl)
452 if (skb->len > len) {
453 __pskb_trim(skb, len);
454 if (skb->ip_summed == CHECKSUM_HW)
455 skb->ip_summed = CHECKSUM_NONE;
458 if ((nf_bridge = nf_bridge_alloc(skb)) == NULL)
460 setup_pre_routing(skb);
461 store_orig_dstaddr(skb);
463 NF_HOOK(PF_INET, NF_IP_PRE_ROUTING, skb, skb->dev, NULL,
464 br_nf_pre_routing_finish);
469 // IP_INC_STATS_BH(IpInHdrErrors);
475 /* PF_BRIDGE/LOCAL_IN ************************************************/
476 /* The packet is locally destined, which requires a real
477 * dst_entry, so detach the fake one. On the way up, the
478 * packet would pass through PRE_ROUTING again (which already
479 * took place when the packet entered the bridge), but we
480 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
481 * prevent this from happening. */
482 static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff **pskb,
483 const struct net_device *in, const struct net_device *out,
484 int (*okfn)(struct sk_buff *))
486 struct sk_buff *skb = *pskb;
488 if (skb->dst == (struct dst_entry *)&__fake_rtable) {
489 dst_release(skb->dst);
497 /* PF_BRIDGE/FORWARD *************************************************/
498 static int br_nf_forward_finish(struct sk_buff *skb)
500 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
501 struct net_device *in;
502 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
504 if (skb->protocol != __constant_htons(ETH_P_ARP) && !IS_VLAN_ARP) {
505 in = nf_bridge->physindev;
506 if (nf_bridge->mask & BRNF_PKT_TYPE) {
507 skb->pkt_type = PACKET_OTHERHOST;
508 nf_bridge->mask ^= BRNF_PKT_TYPE;
511 in = *((struct net_device **)(skb->cb));
513 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
514 skb_push(skb, VLAN_HLEN);
515 skb->nh.raw -= VLAN_HLEN;
517 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_FORWARD, skb, in,
518 skb->dev, br_forward_finish, 1);
522 /* This is the 'purely bridged' case. For IP, we pass the packet to
523 * netfilter with indev and outdev set to the bridge device,
524 * but we are still able to filter on the 'real' indev/outdev
525 * because of the physdev module. For ARP, indev and outdev are the
527 static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff **pskb,
528 const struct net_device *in, const struct net_device *out,
529 int (*okfn)(struct sk_buff *))
531 struct sk_buff *skb = *pskb;
532 struct nf_bridge_info *nf_bridge;
533 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
539 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
544 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
545 skb_pull(*pskb, VLAN_HLEN);
546 (*pskb)->nh.raw += VLAN_HLEN;
549 nf_bridge = skb->nf_bridge;
550 if (skb->pkt_type == PACKET_OTHERHOST) {
551 skb->pkt_type = PACKET_HOST;
552 nf_bridge->mask |= BRNF_PKT_TYPE;
555 /* The physdev module checks on this */
556 nf_bridge->mask |= BRNF_BRIDGED;
557 nf_bridge->physoutdev = skb->dev;
559 NF_HOOK(pf, NF_IP_FORWARD, skb, bridge_parent(in),
560 bridge_parent(out), br_nf_forward_finish);
565 static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff **pskb,
566 const struct net_device *in, const struct net_device *out,
567 int (*okfn)(struct sk_buff *))
569 struct sk_buff *skb = *pskb;
570 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
571 struct net_device **d = (struct net_device **)(skb->cb);
574 if (!brnf_call_arptables)
578 if (skb->protocol != __constant_htons(ETH_P_ARP)) {
581 skb_pull(*pskb, VLAN_HLEN);
582 (*pskb)->nh.raw += VLAN_HLEN;
585 if (skb->nh.arph->ar_pln != 4) {
587 skb_push(*pskb, VLAN_HLEN);
588 (*pskb)->nh.raw -= VLAN_HLEN;
592 *d = (struct net_device *)in;
593 NF_HOOK(NF_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in,
594 (struct net_device *)out, br_nf_forward_finish);
600 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
601 static int br_nf_local_out_finish(struct sk_buff *skb)
603 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
604 skb_push(skb, VLAN_HLEN);
605 skb->nh.raw -= VLAN_HLEN;
608 NF_HOOK_THRESH(PF_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev,
609 br_forward_finish, NF_BR_PRI_FIRST + 1);
614 /* This function sees both locally originated IP packets and forwarded
615 * IP packets (in both cases the destination device is a bridge
616 * device). It also sees bridged-and-DNAT'ed packets.
617 * To be able to filter on the physical bridge devices (with the physdev
618 * module), we steal packets destined to a bridge device away from the
619 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
620 * when we have determined the real output device. This is done in here.
622 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
623 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
624 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
625 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
627 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
628 * this packet before, and so the packet was locally originated. We fake
629 * the PF_INET/LOCAL_OUT hook.
630 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
631 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
632 * even routed packets that didn't arrive on a bridge interface have their
633 * nf_bridge->physindev set. */
634 static unsigned int br_nf_local_out(unsigned int hook, struct sk_buff **pskb,
635 const struct net_device *in, const struct net_device *out,
636 int (*okfn)(struct sk_buff *))
638 struct net_device *realindev, *realoutdev;
639 struct sk_buff *skb = *pskb;
640 struct nf_bridge_info *nf_bridge;
641 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
647 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
652 #ifdef CONFIG_NETFILTER_DEBUG
653 /* Sometimes we get packets with NULL ->dst here (for example,
654 * running a dhcp client daemon triggers this). This should now
655 * be fixed, but let's keep the check around. */
656 if (skb->dst == NULL) {
657 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
662 nf_bridge = skb->nf_bridge;
663 nf_bridge->physoutdev = skb->dev;
664 realindev = nf_bridge->physindev;
666 /* Bridged, take PF_BRIDGE/FORWARD.
667 * (see big note in front of br_nf_pre_routing_finish) */
668 if (nf_bridge->mask & BRNF_BRIDGED_DNAT) {
669 if (nf_bridge->mask & BRNF_PKT_TYPE) {
670 skb->pkt_type = PACKET_OTHERHOST;
671 nf_bridge->mask ^= BRNF_PKT_TYPE;
673 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
674 skb_push(skb, VLAN_HLEN);
675 skb->nh.raw -= VLAN_HLEN;
678 NF_HOOK(PF_BRIDGE, NF_BR_FORWARD, skb, realindev,
679 skb->dev, br_forward_finish);
682 realoutdev = bridge_parent(skb->dev);
684 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
685 /* iptables should match -o br0.x */
686 if (nf_bridge->netoutdev)
687 realoutdev = nf_bridge->netoutdev;
689 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
690 skb_pull(skb, VLAN_HLEN);
691 (*pskb)->nh.raw += VLAN_HLEN;
693 /* IP forwarded traffic has a physindev, locally
694 * generated traffic hasn't. */
695 if (realindev != NULL) {
696 if (!(nf_bridge->mask & BRNF_DONT_TAKE_PARENT) &&
697 has_bridge_parent(realindev))
698 realindev = bridge_parent(realindev);
700 NF_HOOK_THRESH(pf, NF_IP_FORWARD, skb, realindev,
701 realoutdev, br_nf_local_out_finish,
702 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD + 1);
704 NF_HOOK_THRESH(pf, NF_IP_LOCAL_OUT, skb, realindev,
705 realoutdev, br_nf_local_out_finish,
706 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT + 1);
714 /* PF_BRIDGE/POST_ROUTING ********************************************/
715 static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff **pskb,
716 const struct net_device *in, const struct net_device *out,
717 int (*okfn)(struct sk_buff *))
719 struct sk_buff *skb = *pskb;
720 struct nf_bridge_info *nf_bridge = (*pskb)->nf_bridge;
721 struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
722 struct net_device *realoutdev = bridge_parent(skb->dev);
725 #ifdef CONFIG_NETFILTER_DEBUG
726 /* Be very paranoid. This probably won't happen anymore, but let's
727 * keep the check just to be sure... */
728 if (skb->mac.raw < skb->head || skb->mac.raw + ETH_HLEN > skb->data) {
729 printk(KERN_CRIT "br_netfilter: Argh!! br_nf_post_routing: "
730 "bad mac.raw pointer.");
738 if (skb->protocol == __constant_htons(ETH_P_IP) || IS_VLAN_IP)
743 #ifdef CONFIG_NETFILTER_DEBUG
744 if (skb->dst == NULL) {
745 printk(KERN_CRIT "br_netfilter: skb->dst == NULL.");
750 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
751 * about the value of skb->pkt_type. */
752 if (skb->pkt_type == PACKET_OTHERHOST) {
753 skb->pkt_type = PACKET_HOST;
754 nf_bridge->mask |= BRNF_PKT_TYPE;
757 if (skb->protocol == __constant_htons(ETH_P_8021Q)) {
758 skb_pull(skb, VLAN_HLEN);
759 skb->nh.raw += VLAN_HLEN;
762 nf_bridge_save_header(skb);
764 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
765 if (nf_bridge->netoutdev)
766 realoutdev = nf_bridge->netoutdev;
768 NF_HOOK(pf, NF_IP_POST_ROUTING, skb, NULL, realoutdev,
769 br_dev_queue_push_xmit);
773 #ifdef CONFIG_NETFILTER_DEBUG
775 if (skb->dev != NULL) {
776 printk("[%s]", skb->dev->name);
777 if (has_bridge_parent(skb->dev))
778 printk("[%s]", bridge_parent(skb->dev)->name);
780 printk(" head:%p, raw:%p, data:%p\n", skb->head, skb->mac.raw,
787 /* IP/SABOTAGE *****************************************************/
788 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
789 * for the second time. */
790 static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff **pskb,
791 const struct net_device *in, const struct net_device *out,
792 int (*okfn)(struct sk_buff *))
794 if ((*pskb)->nf_bridge &&
795 !((*pskb)->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
802 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
803 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
804 * decision in the bridge code and have determined nf_bridge->physoutdev. */
805 static unsigned int ip_sabotage_out(unsigned int hook, struct sk_buff **pskb,
806 const struct net_device *in, const struct net_device *out,
807 int (*okfn)(struct sk_buff *))
809 struct sk_buff *skb = *pskb;
811 if ((out->hard_start_xmit == br_dev_xmit &&
812 okfn != br_nf_forward_finish &&
813 okfn != br_nf_local_out_finish &&
814 okfn != br_dev_queue_push_xmit)
815 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
816 || ((out->priv_flags & IFF_802_1Q_VLAN) &&
817 VLAN_DEV_INFO(out)->real_dev->hard_start_xmit == br_dev_xmit)
820 struct nf_bridge_info *nf_bridge;
822 if (!skb->nf_bridge) {
824 /* This code is executed while in the IP(v6) stack,
825 the version should be 4 or 6. We can't use
826 skb->protocol because that isn't set on
827 PF_INET(6)/LOCAL_OUT. */
828 struct iphdr *ip = skb->nh.iph;
830 if (ip->version == 4 && !brnf_call_iptables)
832 else if (ip->version == 6 && !brnf_call_ip6tables)
835 if (hook == NF_IP_POST_ROUTING)
837 if (!nf_bridge_alloc(skb))
841 nf_bridge = skb->nf_bridge;
843 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
844 * will need the indev then. For a brouter, the real indev
845 * can be a bridge port, so we make sure br_nf_local_out()
846 * doesn't use the bridge parent of the indev by using
847 * the BRNF_DONT_TAKE_PARENT mask. */
848 if (hook == NF_IP_FORWARD && nf_bridge->physindev == NULL) {
849 nf_bridge->mask |= BRNF_DONT_TAKE_PARENT;
850 nf_bridge->physindev = (struct net_device *)in;
852 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
853 /* the iptables outdev is br0.x, not br0 */
854 if (out->priv_flags & IFF_802_1Q_VLAN)
855 nf_bridge->netoutdev = (struct net_device *)out;
863 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
864 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
865 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
866 * ip_refrag() can return NF_STOLEN. */
867 static struct nf_hook_ops br_nf_ops[] = {
868 { .hook = br_nf_pre_routing,
869 .owner = THIS_MODULE,
871 .hooknum = NF_BR_PRE_ROUTING,
872 .priority = NF_BR_PRI_BRNF, },
873 { .hook = br_nf_local_in,
874 .owner = THIS_MODULE,
876 .hooknum = NF_BR_LOCAL_IN,
877 .priority = NF_BR_PRI_BRNF, },
878 { .hook = br_nf_forward_ip,
879 .owner = THIS_MODULE,
881 .hooknum = NF_BR_FORWARD,
882 .priority = NF_BR_PRI_BRNF - 1, },
883 { .hook = br_nf_forward_arp,
884 .owner = THIS_MODULE,
886 .hooknum = NF_BR_FORWARD,
887 .priority = NF_BR_PRI_BRNF, },
888 { .hook = br_nf_local_out,
889 .owner = THIS_MODULE,
891 .hooknum = NF_BR_LOCAL_OUT,
892 .priority = NF_BR_PRI_FIRST, },
893 { .hook = br_nf_post_routing,
894 .owner = THIS_MODULE,
896 .hooknum = NF_BR_POST_ROUTING,
897 .priority = NF_BR_PRI_LAST, },
898 { .hook = ip_sabotage_in,
899 .owner = THIS_MODULE,
901 .hooknum = NF_IP_PRE_ROUTING,
902 .priority = NF_IP_PRI_FIRST, },
903 { .hook = ip_sabotage_in,
904 .owner = THIS_MODULE,
906 .hooknum = NF_IP6_PRE_ROUTING,
907 .priority = NF_IP6_PRI_FIRST, },
908 { .hook = ip_sabotage_out,
909 .owner = THIS_MODULE,
911 .hooknum = NF_IP_FORWARD,
912 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD, },
913 { .hook = ip_sabotage_out,
914 .owner = THIS_MODULE,
916 .hooknum = NF_IP6_FORWARD,
917 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD, },
918 { .hook = ip_sabotage_out,
919 .owner = THIS_MODULE,
921 .hooknum = NF_IP_LOCAL_OUT,
922 .priority = NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
923 { .hook = ip_sabotage_out,
924 .owner = THIS_MODULE,
926 .hooknum = NF_IP6_LOCAL_OUT,
927 .priority = NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT, },
928 { .hook = ip_sabotage_out,
929 .owner = THIS_MODULE,
931 .hooknum = NF_IP_POST_ROUTING,
932 .priority = NF_IP_PRI_FIRST, },
933 { .hook = ip_sabotage_out,
934 .owner = THIS_MODULE,
936 .hooknum = NF_IP6_POST_ROUTING,
937 .priority = NF_IP6_PRI_FIRST, },
942 int brnf_sysctl_call_tables(ctl_table *ctl, int write, struct file * filp,
943 void __user *buffer, size_t *lenp, loff_t *ppos)
947 ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
949 if (write && *(int *)(ctl->data))
950 *(int *)(ctl->data) = 1;
954 static ctl_table brnf_table[] = {
956 .ctl_name = NET_BRIDGE_NF_CALL_ARPTABLES,
957 .procname = "bridge-nf-call-arptables",
958 .data = &brnf_call_arptables,
959 .maxlen = sizeof(int),
961 .proc_handler = &brnf_sysctl_call_tables,
964 .ctl_name = NET_BRIDGE_NF_CALL_IPTABLES,
965 .procname = "bridge-nf-call-iptables",
966 .data = &brnf_call_iptables,
967 .maxlen = sizeof(int),
969 .proc_handler = &brnf_sysctl_call_tables,
972 .ctl_name = NET_BRIDGE_NF_CALL_IP6TABLES,
973 .procname = "bridge-nf-call-ip6tables",
974 .data = &brnf_call_ip6tables,
975 .maxlen = sizeof(int),
977 .proc_handler = &brnf_sysctl_call_tables,
980 .ctl_name = NET_BRIDGE_NF_FILTER_VLAN_TAGGED,
981 .procname = "bridge-nf-filter-vlan-tagged",
982 .data = &brnf_filter_vlan_tagged,
983 .maxlen = sizeof(int),
985 .proc_handler = &brnf_sysctl_call_tables,
990 static ctl_table brnf_bridge_table[] = {
992 .ctl_name = NET_BRIDGE,
993 .procname = "bridge",
1000 static ctl_table brnf_net_table[] = {
1002 .ctl_name = CTL_NET,
1005 .child = brnf_bridge_table,
1011 int br_netfilter_init(void)
1015 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++) {
1018 if ((ret = nf_register_hook(&br_nf_ops[i])) >= 0)
1022 nf_unregister_hook(&br_nf_ops[i]);
1027 #ifdef CONFIG_SYSCTL
1028 brnf_sysctl_header = register_sysctl_table(brnf_net_table, 0);
1029 if (brnf_sysctl_header == NULL) {
1030 printk(KERN_WARNING "br_netfilter: can't register to sysctl.\n");
1031 for (i = 0; i < ARRAY_SIZE(br_nf_ops); i++)
1032 nf_unregister_hook(&br_nf_ops[i]);
1037 printk(KERN_NOTICE "Bridge firewalling registered\n");
1042 void br_netfilter_fini(void)
1046 for (i = ARRAY_SIZE(br_nf_ops) - 1; i >= 0; i--)
1047 nf_unregister_hook(&br_nf_ops[i]);
1048 #ifdef CONFIG_SYSCTL
1049 unregister_sysctl_table(brnf_sysctl_header);