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)
54 #define has_bridge_parent(device) ((device)->br_port != NULL)
55 #define bridge_parent(device) ((device)->br_port->br->dev)
58 static struct ctl_table_header
*brnf_sysctl_header
;
59 static int brnf_call_iptables
= 1;
60 static int brnf_call_ip6tables
= 1;
61 static int brnf_call_arptables
= 1;
62 static int brnf_filter_vlan_tagged
= 1;
64 #define brnf_filter_vlan_tagged 1
67 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
68 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
69 brnf_filter_vlan_tagged)
70 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
71 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
72 brnf_filter_vlan_tagged)
73 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
74 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
75 brnf_filter_vlan_tagged)
77 /* We need these fake structures to make netfilter happy --
78 * lots of places assume that skb->dst != NULL, which isn't
79 * all that unreasonable.
81 * Currently, we fill in the PMTU entry because netfilter
82 * refragmentation needs it, and the rt_flags entry because
83 * ipt_REJECT needs it. Future netfilter modules might
84 * require us to fill additional fields. */
85 static struct net_device __fake_net_device
= {
86 .hard_header_len
= ETH_HLEN
89 static struct rtable __fake_rtable
= {
92 .__refcnt
= ATOMIC_INIT(1),
93 .dev
= &__fake_net_device
,
94 .path
= &__fake_rtable
.u
.dst
,
95 .metrics
= {[RTAX_MTU
- 1] = 1500},
102 /* PF_BRIDGE/PRE_ROUTING *********************************************/
103 /* Undo the changes made for ip6tables PREROUTING and continue the
104 * bridge PRE_ROUTING hook. */
105 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
107 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
109 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
110 skb
->pkt_type
= PACKET_OTHERHOST
;
111 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
113 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
115 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
118 skb
->dev
= nf_bridge
->physindev
;
119 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
120 skb_push(skb
, VLAN_HLEN
);
121 skb
->nh
.raw
-= VLAN_HLEN
;
123 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
124 br_handle_frame_finish
, 1);
129 static void __br_dnat_complain(void)
131 static unsigned long last_complaint
;
133 if (jiffies
- last_complaint
>= 5 * HZ
) {
134 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
135 "forwarding to be enabled\n");
136 last_complaint
= jiffies
;
140 /* This requires some explaining. If DNAT has taken place,
141 * we will need to fix up the destination Ethernet address,
142 * and this is a tricky process.
144 * There are two cases to consider:
145 * 1. The packet was DNAT'ed to a device in the same bridge
146 * port group as it was received on. We can still bridge
148 * 2. The packet was DNAT'ed to a different device, either
149 * a non-bridged device or another bridge port group.
150 * The packet will need to be routed.
152 * The correct way of distinguishing between these two cases is to
153 * call ip_route_input() and to look at skb->dst->dev, which is
154 * changed to the destination device if ip_route_input() succeeds.
156 * Let us first consider the case that ip_route_input() succeeds:
158 * If skb->dst->dev equals the logical bridge device the packet
159 * came in on, we can consider this bridging. We then call
160 * skb->dst->output() which will make the packet enter br_nf_local_out()
161 * not much later. In that function it is assured that the iptables
162 * FORWARD chain is traversed for the packet.
164 * Otherwise, the packet is considered to be routed and we just
165 * change the destination MAC address so that the packet will
166 * later be passed up to the IP stack to be routed.
168 * Let us now consider the case that ip_route_input() fails:
170 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
171 * will fail, while __ip_route_output_key() will return success. The source
172 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
173 * thinks we're handling a locally generated packet and won't care
174 * if IP forwarding is allowed. We send a warning message to the users's
175 * log telling her to put IP forwarding on.
177 * ip_route_input() will also fail if there is no route available.
178 * In that case we just drop the packet.
180 * --Lennert, 20020411
181 * --Bart, 20020416 (updated)
182 * --Bart, 20021007 (updated) */
183 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
185 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
186 skb
->pkt_type
= PACKET_HOST
;
187 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
189 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
191 skb
->dev
= bridge_parent(skb
->dev
);
192 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
193 skb_pull(skb
, VLAN_HLEN
);
194 skb
->nh
.raw
+= VLAN_HLEN
;
196 skb
->dst
->output(skb
);
200 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
202 struct net_device
*dev
= skb
->dev
;
203 struct iphdr
*iph
= skb
->nh
.iph
;
204 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
206 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
207 skb
->pkt_type
= PACKET_OTHERHOST
;
208 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
210 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
212 if (dnat_took_place(skb
)) {
213 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
,
216 struct flowi fl
= { .nl_u
=
217 { .ip4_u
= { .daddr
= iph
->daddr
, .saddr
= 0 ,
218 .tos
= RT_TOS(iph
->tos
)} }, .proto
= 0};
220 if (!ip_route_output_key(&rt
, &fl
)) {
221 /* - Bridged-and-DNAT'ed traffic doesn't
222 * require ip_forwarding.
223 * - Deal with redirected traffic. */
224 if (((struct dst_entry
*)rt
)->dev
== dev
||
225 rt
->rt_type
== RTN_LOCAL
) {
226 skb
->dst
= (struct dst_entry
*)rt
;
229 __br_dnat_complain();
230 dst_release((struct dst_entry
*)rt
);
235 if (skb
->dst
->dev
== dev
) {
237 /* Tell br_nf_local_out this is a
239 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
240 skb
->dev
= nf_bridge
->physindev
;
242 __constant_htons(ETH_P_8021Q
)) {
243 skb_push(skb
, VLAN_HLEN
);
244 skb
->nh
.raw
-= VLAN_HLEN
;
246 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
248 br_nf_pre_routing_finish_bridge
,
252 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
,
254 skb
->pkt_type
= PACKET_HOST
;
257 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
261 skb
->dev
= nf_bridge
->physindev
;
262 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
263 skb_push(skb
, VLAN_HLEN
);
264 skb
->nh
.raw
-= VLAN_HLEN
;
266 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
267 br_handle_frame_finish
, 1);
272 /* Some common code for IPv4/IPv6 */
273 static void setup_pre_routing(struct sk_buff
*skb
)
275 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
277 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
278 skb
->pkt_type
= PACKET_HOST
;
279 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
282 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
283 nf_bridge
->physindev
= skb
->dev
;
284 skb
->dev
= bridge_parent(skb
->dev
);
287 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
288 static int check_hbh_len(struct sk_buff
*skb
)
290 unsigned char *raw
= (u8
*)(skb
->nh
.ipv6h
+1);
292 int off
= raw
- skb
->nh
.raw
;
293 int len
= (raw
[1]+1)<<3;
295 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
302 int optlen
= skb
->nh
.raw
[off
+1]+2;
304 switch (skb
->nh
.raw
[off
]) {
313 if (skb
->nh
.raw
[off
+1] != 4 || (off
&3) != 2)
315 pkt_len
= ntohl(*(u32
*)(skb
->nh
.raw
+off
+2));
316 if (pkt_len
<= IPV6_MAXPLEN
||
317 skb
->nh
.ipv6h
->payload_len
)
319 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
321 if (pskb_trim_rcsum(skb
,
322 pkt_len
+sizeof(struct ipv6hdr
)))
340 /* Replicate the checks that IPv6 does on packet reception and pass the packet
341 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
342 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
343 struct sk_buff
*skb
, const struct net_device
*in
,
344 const struct net_device
*out
, int (*okfn
)(struct sk_buff
*))
348 struct nf_bridge_info
*nf_bridge
;
350 if (skb
->len
< sizeof(struct ipv6hdr
))
353 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
358 if (hdr
->version
!= 6)
361 pkt_len
= ntohs(hdr
->payload_len
);
363 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
364 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
366 if (pkt_len
+ sizeof(struct ipv6hdr
) < skb
->len
) {
367 if (__pskb_trim(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
369 if (skb
->ip_summed
== CHECKSUM_HW
)
370 skb
->ip_summed
= CHECKSUM_NONE
;
373 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
376 nf_bridge_put(skb
->nf_bridge
);
377 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
379 setup_pre_routing(skb
);
381 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
382 br_nf_pre_routing_finish_ipv6
);
390 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
391 * Replicate the checks that IPv4 does on packet reception.
392 * Set skb->dev to the bridge device (i.e. parent of the
393 * receiving device) to make netfilter happy, the REDIRECT
394 * target in particular. Save the original destination IP
395 * address to be able to detect DNAT afterwards. */
396 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
397 const struct net_device
*in
,
398 const struct net_device
*out
,
399 int (*okfn
)(struct sk_buff
*))
403 struct sk_buff
*skb
= *pskb
;
404 struct nf_bridge_info
*nf_bridge
;
405 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(*pskb
);
407 if (skb
->protocol
== __constant_htons(ETH_P_IPV6
) || IS_VLAN_IPV6
) {
409 if (!brnf_call_ip6tables
)
412 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
415 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
416 u8
*vhdr
= skb
->data
;
417 skb_pull(skb
, VLAN_HLEN
);
418 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
419 skb
->nh
.raw
+= VLAN_HLEN
;
421 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
424 if (!brnf_call_iptables
)
428 if (skb
->protocol
!= __constant_htons(ETH_P_IP
) && !IS_VLAN_IP
)
431 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
434 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
435 u8
*vhdr
= skb
->data
;
436 skb_pull(skb
, VLAN_HLEN
);
437 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
438 skb
->nh
.raw
+= VLAN_HLEN
;
441 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
445 if (iph
->ihl
< 5 || iph
->version
!= 4)
448 if (!pskb_may_pull(skb
, 4*iph
->ihl
))
452 if (ip_fast_csum((__u8
*)iph
, iph
->ihl
) != 0)
455 len
= ntohs(iph
->tot_len
);
456 if (skb
->len
< len
|| len
< 4*iph
->ihl
)
459 if (skb
->len
> len
) {
460 __pskb_trim(skb
, len
);
461 if (skb
->ip_summed
== CHECKSUM_HW
)
462 skb
->ip_summed
= CHECKSUM_NONE
;
465 nf_bridge_put(skb
->nf_bridge
);
466 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
468 setup_pre_routing(skb
);
469 store_orig_dstaddr(skb
);
471 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
472 br_nf_pre_routing_finish
);
477 // IP_INC_STATS_BH(IpInHdrErrors);
483 /* PF_BRIDGE/LOCAL_IN ************************************************/
484 /* The packet is locally destined, which requires a real
485 * dst_entry, so detach the fake one. On the way up, the
486 * packet would pass through PRE_ROUTING again (which already
487 * took place when the packet entered the bridge), but we
488 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
489 * prevent this from happening. */
490 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
491 const struct net_device
*in
, const struct net_device
*out
,
492 int (*okfn
)(struct sk_buff
*))
494 struct sk_buff
*skb
= *pskb
;
496 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
497 dst_release(skb
->dst
);
505 /* PF_BRIDGE/FORWARD *************************************************/
506 static int br_nf_forward_finish(struct sk_buff
*skb
)
508 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
509 struct net_device
*in
;
510 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
512 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
) && !IS_VLAN_ARP
) {
513 in
= nf_bridge
->physindev
;
514 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
515 skb
->pkt_type
= PACKET_OTHERHOST
;
516 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
519 in
= *((struct net_device
**)(skb
->cb
));
521 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
522 skb_push(skb
, VLAN_HLEN
);
523 skb
->nh
.raw
-= VLAN_HLEN
;
525 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
526 skb
->dev
, br_forward_finish
, 1);
530 /* This is the 'purely bridged' case. For IP, we pass the packet to
531 * netfilter with indev and outdev set to the bridge device,
532 * but we are still able to filter on the 'real' indev/outdev
533 * because of the physdev module. For ARP, indev and outdev are the
535 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
536 const struct net_device
*in
, const struct net_device
*out
,
537 int (*okfn
)(struct sk_buff
*))
539 struct sk_buff
*skb
= *pskb
;
540 struct nf_bridge_info
*nf_bridge
;
541 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
547 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
552 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
553 skb_pull(*pskb
, VLAN_HLEN
);
554 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
557 nf_bridge
= skb
->nf_bridge
;
558 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
559 skb
->pkt_type
= PACKET_HOST
;
560 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
563 /* The physdev module checks on this */
564 nf_bridge
->mask
|= BRNF_BRIDGED
;
565 nf_bridge
->physoutdev
= skb
->dev
;
567 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
),
568 bridge_parent(out
), br_nf_forward_finish
);
573 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
574 const struct net_device
*in
, const struct net_device
*out
,
575 int (*okfn
)(struct sk_buff
*))
577 struct sk_buff
*skb
= *pskb
;
578 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
579 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
582 if (!brnf_call_arptables
)
586 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
)) {
589 skb_pull(*pskb
, VLAN_HLEN
);
590 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
593 if (skb
->nh
.arph
->ar_pln
!= 4) {
595 skb_push(*pskb
, VLAN_HLEN
);
596 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
600 *d
= (struct net_device
*)in
;
601 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
602 (struct net_device
*)out
, br_nf_forward_finish
);
608 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
609 static int br_nf_local_out_finish(struct sk_buff
*skb
)
611 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
612 skb_push(skb
, VLAN_HLEN
);
613 skb
->nh
.raw
-= VLAN_HLEN
;
616 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
617 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
622 /* This function sees both locally originated IP packets and forwarded
623 * IP packets (in both cases the destination device is a bridge
624 * device). It also sees bridged-and-DNAT'ed packets.
625 * To be able to filter on the physical bridge devices (with the physdev
626 * module), we steal packets destined to a bridge device away from the
627 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
628 * when we have determined the real output device. This is done in here.
630 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
631 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
632 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
633 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
635 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
636 * this packet before, and so the packet was locally originated. We fake
637 * the PF_INET/LOCAL_OUT hook.
638 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
639 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
640 * even routed packets that didn't arrive on a bridge interface have their
641 * nf_bridge->physindev set. */
642 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
643 const struct net_device
*in
, const struct net_device
*out
,
644 int (*okfn
)(struct sk_buff
*))
646 struct net_device
*realindev
, *realoutdev
;
647 struct sk_buff
*skb
= *pskb
;
648 struct nf_bridge_info
*nf_bridge
;
649 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
655 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
660 #ifdef CONFIG_NETFILTER_DEBUG
661 /* Sometimes we get packets with NULL ->dst here (for example,
662 * running a dhcp client daemon triggers this). This should now
663 * be fixed, but let's keep the check around. */
664 if (skb
->dst
== NULL
) {
665 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
670 nf_bridge
= skb
->nf_bridge
;
671 nf_bridge
->physoutdev
= skb
->dev
;
672 realindev
= nf_bridge
->physindev
;
674 /* Bridged, take PF_BRIDGE/FORWARD.
675 * (see big note in front of br_nf_pre_routing_finish) */
676 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
677 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
678 skb
->pkt_type
= PACKET_OTHERHOST
;
679 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
681 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
682 skb_push(skb
, VLAN_HLEN
);
683 skb
->nh
.raw
-= VLAN_HLEN
;
686 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
687 skb
->dev
, br_forward_finish
);
690 realoutdev
= bridge_parent(skb
->dev
);
692 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
693 /* iptables should match -o br0.x */
694 if (nf_bridge
->netoutdev
)
695 realoutdev
= nf_bridge
->netoutdev
;
697 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
698 skb_pull(skb
, VLAN_HLEN
);
699 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
701 /* IP forwarded traffic has a physindev, locally
702 * generated traffic hasn't. */
703 if (realindev
!= NULL
) {
704 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
) &&
705 has_bridge_parent(realindev
))
706 realindev
= bridge_parent(realindev
);
708 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
709 realoutdev
, br_nf_local_out_finish
,
710 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
712 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
713 realoutdev
, br_nf_local_out_finish
,
714 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
722 /* PF_BRIDGE/POST_ROUTING ********************************************/
723 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
724 const struct net_device
*in
, const struct net_device
*out
,
725 int (*okfn
)(struct sk_buff
*))
727 struct sk_buff
*skb
= *pskb
;
728 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
729 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
730 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
733 #ifdef CONFIG_NETFILTER_DEBUG
734 /* Be very paranoid. This probably won't happen anymore, but let's
735 * keep the check just to be sure... */
736 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
737 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
738 "bad mac.raw pointer.");
746 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
751 #ifdef CONFIG_NETFILTER_DEBUG
752 if (skb
->dst
== NULL
) {
753 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
758 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
759 * about the value of skb->pkt_type. */
760 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
761 skb
->pkt_type
= PACKET_HOST
;
762 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
765 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
766 skb_pull(skb
, VLAN_HLEN
);
767 skb
->nh
.raw
+= VLAN_HLEN
;
770 nf_bridge_save_header(skb
);
772 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
773 if (nf_bridge
->netoutdev
)
774 realoutdev
= nf_bridge
->netoutdev
;
776 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
777 br_dev_queue_push_xmit
);
781 #ifdef CONFIG_NETFILTER_DEBUG
783 if (skb
->dev
!= NULL
) {
784 printk("[%s]", skb
->dev
->name
);
785 if (has_bridge_parent(skb
->dev
))
786 printk("[%s]", bridge_parent(skb
->dev
)->name
);
788 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
795 /* IP/SABOTAGE *****************************************************/
796 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
797 * for the second time. */
798 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
799 const struct net_device
*in
, const struct net_device
*out
,
800 int (*okfn
)(struct sk_buff
*))
802 if ((*pskb
)->nf_bridge
&&
803 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
810 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
811 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
812 * decision in the bridge code and have determined nf_bridge->physoutdev. */
813 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
814 const struct net_device
*in
, const struct net_device
*out
,
815 int (*okfn
)(struct sk_buff
*))
817 struct sk_buff
*skb
= *pskb
;
819 if ((out
->hard_start_xmit
== br_dev_xmit
&&
820 okfn
!= br_nf_forward_finish
&&
821 okfn
!= br_nf_local_out_finish
&&
822 okfn
!= br_dev_queue_push_xmit
)
823 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
824 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
825 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
828 struct nf_bridge_info
*nf_bridge
;
830 if (!skb
->nf_bridge
) {
832 /* This code is executed while in the IP(v6) stack,
833 the version should be 4 or 6. We can't use
834 skb->protocol because that isn't set on
835 PF_INET(6)/LOCAL_OUT. */
836 struct iphdr
*ip
= skb
->nh
.iph
;
838 if (ip
->version
== 4 && !brnf_call_iptables
)
840 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
843 if (hook
== NF_IP_POST_ROUTING
)
845 if (!nf_bridge_alloc(skb
))
849 nf_bridge
= skb
->nf_bridge
;
851 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
852 * will need the indev then. For a brouter, the real indev
853 * can be a bridge port, so we make sure br_nf_local_out()
854 * doesn't use the bridge parent of the indev by using
855 * the BRNF_DONT_TAKE_PARENT mask. */
856 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
857 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
858 nf_bridge
->physindev
= (struct net_device
*)in
;
860 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
861 /* the iptables outdev is br0.x, not br0 */
862 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
863 nf_bridge
->netoutdev
= (struct net_device
*)out
;
871 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
872 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
873 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
874 * ip_refrag() can return NF_STOLEN. */
875 static struct nf_hook_ops br_nf_ops
[] = {
876 { .hook
= br_nf_pre_routing
,
877 .owner
= THIS_MODULE
,
879 .hooknum
= NF_BR_PRE_ROUTING
,
880 .priority
= NF_BR_PRI_BRNF
, },
881 { .hook
= br_nf_local_in
,
882 .owner
= THIS_MODULE
,
884 .hooknum
= NF_BR_LOCAL_IN
,
885 .priority
= NF_BR_PRI_BRNF
, },
886 { .hook
= br_nf_forward_ip
,
887 .owner
= THIS_MODULE
,
889 .hooknum
= NF_BR_FORWARD
,
890 .priority
= NF_BR_PRI_BRNF
- 1, },
891 { .hook
= br_nf_forward_arp
,
892 .owner
= THIS_MODULE
,
894 .hooknum
= NF_BR_FORWARD
,
895 .priority
= NF_BR_PRI_BRNF
, },
896 { .hook
= br_nf_local_out
,
897 .owner
= THIS_MODULE
,
899 .hooknum
= NF_BR_LOCAL_OUT
,
900 .priority
= NF_BR_PRI_FIRST
, },
901 { .hook
= br_nf_post_routing
,
902 .owner
= THIS_MODULE
,
904 .hooknum
= NF_BR_POST_ROUTING
,
905 .priority
= NF_BR_PRI_LAST
, },
906 { .hook
= ip_sabotage_in
,
907 .owner
= THIS_MODULE
,
909 .hooknum
= NF_IP_PRE_ROUTING
,
910 .priority
= NF_IP_PRI_FIRST
, },
911 { .hook
= ip_sabotage_in
,
912 .owner
= THIS_MODULE
,
914 .hooknum
= NF_IP6_PRE_ROUTING
,
915 .priority
= NF_IP6_PRI_FIRST
, },
916 { .hook
= ip_sabotage_out
,
917 .owner
= THIS_MODULE
,
919 .hooknum
= NF_IP_FORWARD
,
920 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
921 { .hook
= ip_sabotage_out
,
922 .owner
= THIS_MODULE
,
924 .hooknum
= NF_IP6_FORWARD
,
925 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
926 { .hook
= ip_sabotage_out
,
927 .owner
= THIS_MODULE
,
929 .hooknum
= NF_IP_LOCAL_OUT
,
930 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
931 { .hook
= ip_sabotage_out
,
932 .owner
= THIS_MODULE
,
934 .hooknum
= NF_IP6_LOCAL_OUT
,
935 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
936 { .hook
= ip_sabotage_out
,
937 .owner
= THIS_MODULE
,
939 .hooknum
= NF_IP_POST_ROUTING
,
940 .priority
= NF_IP_PRI_FIRST
, },
941 { .hook
= ip_sabotage_out
,
942 .owner
= THIS_MODULE
,
944 .hooknum
= NF_IP6_POST_ROUTING
,
945 .priority
= NF_IP6_PRI_FIRST
, },
950 int brnf_sysctl_call_tables(ctl_table
*ctl
, int write
, struct file
* filp
,
951 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
955 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
957 if (write
&& *(int *)(ctl
->data
))
958 *(int *)(ctl
->data
) = 1;
962 static ctl_table brnf_table
[] = {
964 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
965 .procname
= "bridge-nf-call-arptables",
966 .data
= &brnf_call_arptables
,
967 .maxlen
= sizeof(int),
969 .proc_handler
= &brnf_sysctl_call_tables
,
972 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
973 .procname
= "bridge-nf-call-iptables",
974 .data
= &brnf_call_iptables
,
975 .maxlen
= sizeof(int),
977 .proc_handler
= &brnf_sysctl_call_tables
,
980 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
981 .procname
= "bridge-nf-call-ip6tables",
982 .data
= &brnf_call_ip6tables
,
983 .maxlen
= sizeof(int),
985 .proc_handler
= &brnf_sysctl_call_tables
,
988 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
989 .procname
= "bridge-nf-filter-vlan-tagged",
990 .data
= &brnf_filter_vlan_tagged
,
991 .maxlen
= sizeof(int),
993 .proc_handler
= &brnf_sysctl_call_tables
,
998 static ctl_table brnf_bridge_table
[] = {
1000 .ctl_name
= NET_BRIDGE
,
1001 .procname
= "bridge",
1003 .child
= brnf_table
,
1008 static ctl_table brnf_net_table
[] = {
1010 .ctl_name
= CTL_NET
,
1013 .child
= brnf_bridge_table
,
1019 int br_netfilter_init(void)
1023 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1026 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1030 nf_unregister_hook(&br_nf_ops
[i
]);
1035 #ifdef CONFIG_SYSCTL
1036 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1037 if (brnf_sysctl_header
== NULL
) {
1038 printk(KERN_WARNING
"br_netfilter: can't register to sysctl.\n");
1039 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1040 nf_unregister_hook(&br_nf_ops
[i
]);
1045 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1050 void br_netfilter_fini(void)
1054 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1055 nf_unregister_hook(&br_nf_ops
[i
]);
1056 #ifdef CONFIG_SYSCTL
1057 unregister_sysctl_table(brnf_sysctl_header
);