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 "br_private.h"
45 #include <linux/sysctl.h>
48 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
49 (skb->nf_bridge->data))->daddr.ipv4)
50 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
51 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
54 static struct ctl_table_header
*brnf_sysctl_header
;
55 static int brnf_call_iptables __read_mostly
= 1;
56 static int brnf_call_ip6tables __read_mostly
= 1;
57 static int brnf_call_arptables __read_mostly
= 1;
58 static int brnf_filter_vlan_tagged __read_mostly
= 1;
60 #define brnf_filter_vlan_tagged 1
63 int brnf_deferred_hooks
;
64 EXPORT_SYMBOL_GPL(brnf_deferred_hooks
);
66 static __be16
inline vlan_proto(const struct sk_buff
*skb
)
68 return vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
;
71 #define IS_VLAN_IP(skb) \
72 (skb->protocol == htons(ETH_P_8021Q) && \
73 vlan_proto(skb) == htons(ETH_P_IP) && \
74 brnf_filter_vlan_tagged)
76 #define IS_VLAN_IPV6(skb) \
77 (skb->protocol == htons(ETH_P_8021Q) && \
78 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
79 brnf_filter_vlan_tagged)
81 #define IS_VLAN_ARP(skb) \
82 (skb->protocol == htons(ETH_P_8021Q) && \
83 vlan_proto(skb) == htons(ETH_P_ARP) && \
84 brnf_filter_vlan_tagged)
86 /* We need these fake structures to make netfilter happy --
87 * lots of places assume that skb->dst != NULL, which isn't
88 * all that unreasonable.
90 * Currently, we fill in the PMTU entry because netfilter
91 * refragmentation needs it, and the rt_flags entry because
92 * ipt_REJECT needs it. Future netfilter modules might
93 * require us to fill additional fields. */
94 static struct net_device __fake_net_device
= {
95 .hard_header_len
= ETH_HLEN
98 static struct rtable __fake_rtable
= {
101 .__refcnt
= ATOMIC_INIT(1),
102 .dev
= &__fake_net_device
,
103 .path
= &__fake_rtable
.u
.dst
,
104 .metrics
= {[RTAX_MTU
- 1] = 1500},
111 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
113 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
115 return port
? port
->br
->dev
: NULL
;
118 static inline struct nf_bridge_info
*nf_bridge_alloc(struct sk_buff
*skb
)
120 skb
->nf_bridge
= kzalloc(sizeof(struct nf_bridge_info
), GFP_ATOMIC
);
121 if (likely(skb
->nf_bridge
))
122 atomic_set(&(skb
->nf_bridge
->use
), 1);
124 return skb
->nf_bridge
;
127 static inline void nf_bridge_save_header(struct sk_buff
*skb
)
129 int header_size
= ETH_HLEN
;
131 if (skb
->protocol
== htons(ETH_P_8021Q
))
132 header_size
+= VLAN_HLEN
;
134 memcpy(skb
->nf_bridge
->data
, skb
->data
- header_size
, header_size
);
138 * When forwarding bridge frames, we save a copy of the original
139 * header before processing.
141 int nf_bridge_copy_header(struct sk_buff
*skb
)
144 int header_size
= ETH_HLEN
;
146 if (skb
->protocol
== htons(ETH_P_8021Q
))
147 header_size
+= VLAN_HLEN
;
149 err
= skb_cow(skb
, header_size
);
153 memcpy(skb
->data
- header_size
, skb
->nf_bridge
->data
, header_size
);
155 if (skb
->protocol
== htons(ETH_P_8021Q
))
156 __skb_push(skb
, VLAN_HLEN
);
160 /* PF_BRIDGE/PRE_ROUTING *********************************************/
161 /* Undo the changes made for ip6tables PREROUTING and continue the
162 * bridge PRE_ROUTING hook. */
163 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
165 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
167 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
168 skb
->pkt_type
= PACKET_OTHERHOST
;
169 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
171 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
173 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
176 skb
->dev
= nf_bridge
->physindev
;
177 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
178 skb_push(skb
, VLAN_HLEN
);
179 skb
->nh
.raw
-= VLAN_HLEN
;
181 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
182 br_handle_frame_finish
, 1);
187 static void __br_dnat_complain(void)
189 static unsigned long last_complaint
;
191 if (jiffies
- last_complaint
>= 5 * HZ
) {
192 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
193 "forwarding to be enabled\n");
194 last_complaint
= jiffies
;
198 /* This requires some explaining. If DNAT has taken place,
199 * we will need to fix up the destination Ethernet address,
200 * and this is a tricky process.
202 * There are two cases to consider:
203 * 1. The packet was DNAT'ed to a device in the same bridge
204 * port group as it was received on. We can still bridge
206 * 2. The packet was DNAT'ed to a different device, either
207 * a non-bridged device or another bridge port group.
208 * The packet will need to be routed.
210 * The correct way of distinguishing between these two cases is to
211 * call ip_route_input() and to look at skb->dst->dev, which is
212 * changed to the destination device if ip_route_input() succeeds.
214 * Let us first consider the case that ip_route_input() succeeds:
216 * If skb->dst->dev equals the logical bridge device the packet
217 * came in on, we can consider this bridging. We then call
218 * skb->dst->output() which will make the packet enter br_nf_local_out()
219 * not much later. In that function it is assured that the iptables
220 * FORWARD chain is traversed for the packet.
222 * Otherwise, the packet is considered to be routed and we just
223 * change the destination MAC address so that the packet will
224 * later be passed up to the IP stack to be routed.
226 * Let us now consider the case that ip_route_input() fails:
228 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
229 * will fail, while __ip_route_output_key() will return success. The source
230 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
231 * thinks we're handling a locally generated packet and won't care
232 * if IP forwarding is allowed. We send a warning message to the users's
233 * log telling her to put IP forwarding on.
235 * ip_route_input() will also fail if there is no route available.
236 * In that case we just drop the packet.
238 * --Lennert, 20020411
239 * --Bart, 20020416 (updated)
240 * --Bart, 20021007 (updated) */
241 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
243 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
244 skb
->pkt_type
= PACKET_HOST
;
245 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
247 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
249 skb
->dev
= bridge_parent(skb
->dev
);
253 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
254 skb_pull(skb
, VLAN_HLEN
);
255 skb
->nh
.raw
+= VLAN_HLEN
;
257 skb
->dst
->output(skb
);
262 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
264 struct net_device
*dev
= skb
->dev
;
265 struct iphdr
*iph
= skb
->nh
.iph
;
266 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
268 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
269 skb
->pkt_type
= PACKET_OTHERHOST
;
270 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
272 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
274 if (dnat_took_place(skb
)) {
275 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
)) {
282 .tos
= RT_TOS(iph
->tos
) },
287 if (!ip_route_output_key(&rt
, &fl
)) {
288 /* - Bridged-and-DNAT'ed traffic doesn't
289 * require ip_forwarding.
290 * - Deal with redirected traffic. */
291 if (((struct dst_entry
*)rt
)->dev
== dev
||
292 rt
->rt_type
== RTN_LOCAL
) {
293 skb
->dst
= (struct dst_entry
*)rt
;
296 __br_dnat_complain();
297 dst_release((struct dst_entry
*)rt
);
302 if (skb
->dst
->dev
== dev
) {
304 /* Tell br_nf_local_out this is a
306 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
307 skb
->dev
= nf_bridge
->physindev
;
309 htons(ETH_P_8021Q
)) {
310 skb_push(skb
, VLAN_HLEN
);
311 skb
->nh
.raw
-= VLAN_HLEN
;
313 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
315 br_nf_pre_routing_finish_bridge
,
319 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
, ETH_ALEN
);
320 skb
->pkt_type
= PACKET_HOST
;
323 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
327 skb
->dev
= nf_bridge
->physindev
;
328 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
329 skb_push(skb
, VLAN_HLEN
);
330 skb
->nh
.raw
-= VLAN_HLEN
;
332 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
333 br_handle_frame_finish
, 1);
338 /* Some common code for IPv4/IPv6 */
339 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
341 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
343 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
344 skb
->pkt_type
= PACKET_HOST
;
345 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
348 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
349 nf_bridge
->physindev
= skb
->dev
;
350 skb
->dev
= bridge_parent(skb
->dev
);
355 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
356 static int check_hbh_len(struct sk_buff
*skb
)
358 unsigned char *raw
= (u8
*) (skb
->nh
.ipv6h
+ 1);
360 int off
= raw
- skb
->nh
.raw
;
361 int len
= (raw
[1] + 1) << 3;
363 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
370 int optlen
= skb
->nh
.raw
[off
+ 1] + 2;
372 switch (skb
->nh
.raw
[off
]) {
381 if (skb
->nh
.raw
[off
+ 1] != 4 || (off
& 3) != 2)
383 pkt_len
= ntohl(*(__be32
*) (skb
->nh
.raw
+ off
+ 2));
384 if (pkt_len
<= IPV6_MAXPLEN
||
385 skb
->nh
.ipv6h
->payload_len
)
387 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
389 if (pskb_trim_rcsum(skb
,
390 pkt_len
+ sizeof(struct ipv6hdr
)))
408 /* Replicate the checks that IPv6 does on packet reception and pass the packet
409 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
410 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
412 const struct net_device
*in
,
413 const struct net_device
*out
,
414 int (*okfn
)(struct sk_buff
*))
419 if (skb
->len
< sizeof(struct ipv6hdr
))
422 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
427 if (hdr
->version
!= 6)
430 pkt_len
= ntohs(hdr
->payload_len
);
432 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
433 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
435 if (pskb_trim_rcsum(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
438 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
441 nf_bridge_put(skb
->nf_bridge
);
442 if (!nf_bridge_alloc(skb
))
444 if (!setup_pre_routing(skb
))
447 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
448 br_nf_pre_routing_finish_ipv6
);
456 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
457 * Replicate the checks that IPv4 does on packet reception.
458 * Set skb->dev to the bridge device (i.e. parent of the
459 * receiving device) to make netfilter happy, the REDIRECT
460 * target in particular. Save the original destination IP
461 * address to be able to detect DNAT afterwards. */
462 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
463 const struct net_device
*in
,
464 const struct net_device
*out
,
465 int (*okfn
)(struct sk_buff
*))
469 struct sk_buff
*skb
= *pskb
;
471 if (skb
->protocol
== htons(ETH_P_IPV6
) || IS_VLAN_IPV6(skb
)) {
473 if (!brnf_call_ip6tables
)
476 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
479 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
480 skb_pull_rcsum(skb
, VLAN_HLEN
);
481 skb
->nh
.raw
+= VLAN_HLEN
;
483 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
486 if (!brnf_call_iptables
)
490 if (skb
->protocol
!= htons(ETH_P_IP
) && !IS_VLAN_IP(skb
))
493 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
496 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
497 skb_pull_rcsum(skb
, VLAN_HLEN
);
498 skb
->nh
.raw
+= VLAN_HLEN
;
501 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
505 if (iph
->ihl
< 5 || iph
->version
!= 4)
508 if (!pskb_may_pull(skb
, 4 * iph
->ihl
))
512 if (ip_fast_csum((__u8
*) iph
, iph
->ihl
) != 0)
515 len
= ntohs(iph
->tot_len
);
516 if (skb
->len
< len
|| len
< 4 * iph
->ihl
)
519 pskb_trim_rcsum(skb
, len
);
521 nf_bridge_put(skb
->nf_bridge
);
522 if (!nf_bridge_alloc(skb
))
524 if (!setup_pre_routing(skb
))
526 store_orig_dstaddr(skb
);
528 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
529 br_nf_pre_routing_finish
);
534 // IP_INC_STATS_BH(IpInHdrErrors);
540 /* PF_BRIDGE/LOCAL_IN ************************************************/
541 /* The packet is locally destined, which requires a real
542 * dst_entry, so detach the fake one. On the way up, the
543 * packet would pass through PRE_ROUTING again (which already
544 * took place when the packet entered the bridge), but we
545 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
546 * prevent this from happening. */
547 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
548 const struct net_device
*in
,
549 const struct net_device
*out
,
550 int (*okfn
)(struct sk_buff
*))
552 struct sk_buff
*skb
= *pskb
;
554 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
555 dst_release(skb
->dst
);
562 /* PF_BRIDGE/FORWARD *************************************************/
563 static int br_nf_forward_finish(struct sk_buff
*skb
)
565 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
566 struct net_device
*in
;
568 if (skb
->protocol
!= htons(ETH_P_ARP
) && !IS_VLAN_ARP(skb
)) {
569 in
= nf_bridge
->physindev
;
570 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
571 skb
->pkt_type
= PACKET_OTHERHOST
;
572 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
575 in
= *((struct net_device
**)(skb
->cb
));
577 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
578 skb_push(skb
, VLAN_HLEN
);
579 skb
->nh
.raw
-= VLAN_HLEN
;
581 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
582 skb
->dev
, br_forward_finish
, 1);
586 /* This is the 'purely bridged' case. For IP, we pass the packet to
587 * netfilter with indev and outdev set to the bridge device,
588 * but we are still able to filter on the 'real' indev/outdev
589 * because of the physdev module. For ARP, indev and outdev are the
591 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
592 const struct net_device
*in
,
593 const struct net_device
*out
,
594 int (*okfn
)(struct sk_buff
*))
596 struct sk_buff
*skb
= *pskb
;
597 struct nf_bridge_info
*nf_bridge
;
598 struct net_device
*parent
;
604 parent
= bridge_parent(out
);
608 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
613 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
614 skb_pull(*pskb
, VLAN_HLEN
);
615 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
618 nf_bridge
= skb
->nf_bridge
;
619 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
620 skb
->pkt_type
= PACKET_HOST
;
621 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
624 /* The physdev module checks on this */
625 nf_bridge
->mask
|= BRNF_BRIDGED
;
626 nf_bridge
->physoutdev
= skb
->dev
;
628 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
629 br_nf_forward_finish
);
634 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
635 const struct net_device
*in
,
636 const struct net_device
*out
,
637 int (*okfn
)(struct sk_buff
*))
639 struct sk_buff
*skb
= *pskb
;
640 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
643 if (!brnf_call_arptables
)
647 if (skb
->protocol
!= htons(ETH_P_ARP
)) {
648 if (!IS_VLAN_ARP(skb
))
650 skb_pull(*pskb
, VLAN_HLEN
);
651 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
654 if (skb
->nh
.arph
->ar_pln
!= 4) {
655 if (IS_VLAN_ARP(skb
)) {
656 skb_push(*pskb
, VLAN_HLEN
);
657 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
661 *d
= (struct net_device
*)in
;
662 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
663 (struct net_device
*)out
, br_nf_forward_finish
);
668 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
669 static int br_nf_local_out_finish(struct sk_buff
*skb
)
671 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
672 skb_push(skb
, VLAN_HLEN
);
673 skb
->nh
.raw
-= VLAN_HLEN
;
676 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
677 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
682 /* This function sees both locally originated IP packets and forwarded
683 * IP packets (in both cases the destination device is a bridge
684 * device). It also sees bridged-and-DNAT'ed packets.
685 * To be able to filter on the physical bridge devices (with the physdev
686 * module), we steal packets destined to a bridge device away from the
687 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
688 * when we have determined the real output device. This is done in here.
690 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
691 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
692 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
693 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
695 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
696 * this packet before, and so the packet was locally originated. We fake
697 * the PF_INET/LOCAL_OUT hook.
698 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
699 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
700 * even routed packets that didn't arrive on a bridge interface have their
701 * nf_bridge->physindev set. */
702 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
703 const struct net_device
*in
,
704 const struct net_device
*out
,
705 int (*okfn
)(struct sk_buff
*))
707 struct net_device
*realindev
, *realoutdev
;
708 struct sk_buff
*skb
= *pskb
;
709 struct nf_bridge_info
*nf_bridge
;
715 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
720 nf_bridge
= skb
->nf_bridge
;
721 nf_bridge
->physoutdev
= skb
->dev
;
722 realindev
= nf_bridge
->physindev
;
724 /* Bridged, take PF_BRIDGE/FORWARD.
725 * (see big note in front of br_nf_pre_routing_finish) */
726 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
727 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
728 skb
->pkt_type
= PACKET_OTHERHOST
;
729 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
731 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
732 skb_push(skb
, VLAN_HLEN
);
733 skb
->nh
.raw
-= VLAN_HLEN
;
736 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
737 skb
->dev
, br_forward_finish
);
740 realoutdev
= bridge_parent(skb
->dev
);
744 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
745 /* iptables should match -o br0.x */
746 if (nf_bridge
->netoutdev
)
747 realoutdev
= nf_bridge
->netoutdev
;
749 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
750 skb_pull(skb
, VLAN_HLEN
);
751 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
753 /* IP forwarded traffic has a physindev, locally
754 * generated traffic hasn't. */
755 if (realindev
!= NULL
) {
756 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
)) {
757 struct net_device
*parent
= bridge_parent(realindev
);
762 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
763 realoutdev
, br_nf_local_out_finish
,
764 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
766 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
767 realoutdev
, br_nf_local_out_finish
,
768 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
775 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
777 if (skb
->protocol
== htons(ETH_P_IP
) &&
778 skb
->len
> skb
->dev
->mtu
&&
780 return ip_fragment(skb
, br_dev_queue_push_xmit
);
782 return br_dev_queue_push_xmit(skb
);
785 /* PF_BRIDGE/POST_ROUTING ********************************************/
786 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
787 const struct net_device
*in
,
788 const struct net_device
*out
,
789 int (*okfn
)(struct sk_buff
*))
791 struct sk_buff
*skb
= *pskb
;
792 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
793 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
796 #ifdef CONFIG_NETFILTER_DEBUG
797 /* Be very paranoid. This probably won't happen anymore, but let's
798 * keep the check just to be sure... */
799 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
800 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
801 "bad mac.raw pointer.\n");
812 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
817 #ifdef CONFIG_NETFILTER_DEBUG
818 if (skb
->dst
== NULL
) {
819 printk(KERN_INFO
"br_netfilter post_routing: skb->dst == NULL\n");
824 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
825 * about the value of skb->pkt_type. */
826 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
827 skb
->pkt_type
= PACKET_HOST
;
828 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
831 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
832 skb_pull(skb
, VLAN_HLEN
);
833 skb
->nh
.raw
+= VLAN_HLEN
;
836 nf_bridge_save_header(skb
);
838 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
839 if (nf_bridge
->netoutdev
)
840 realoutdev
= nf_bridge
->netoutdev
;
842 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
843 br_nf_dev_queue_xmit
);
847 #ifdef CONFIG_NETFILTER_DEBUG
849 if (skb
->dev
!= NULL
) {
850 printk("[%s]", skb
->dev
->name
);
852 printk("[%s]", realoutdev
->name
);
854 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
861 /* IP/SABOTAGE *****************************************************/
862 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
863 * for the second time. */
864 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
865 const struct net_device
*in
,
866 const struct net_device
*out
,
867 int (*okfn
)(struct sk_buff
*))
869 if ((*pskb
)->nf_bridge
&&
870 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
877 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
878 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
879 * decision in the bridge code and have determined nf_bridge->physoutdev. */
880 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
881 const struct net_device
*in
,
882 const struct net_device
*out
,
883 int (*okfn
)(struct sk_buff
*))
885 struct sk_buff
*skb
= *pskb
;
887 if ((out
->hard_start_xmit
== br_dev_xmit
&&
888 okfn
!= br_nf_forward_finish
&&
889 okfn
!= br_nf_local_out_finish
&& okfn
!= br_nf_dev_queue_xmit
)
890 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
891 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
892 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
895 struct nf_bridge_info
*nf_bridge
;
897 if (!skb
->nf_bridge
) {
899 /* This code is executed while in the IP(v6) stack,
900 the version should be 4 or 6. We can't use
901 skb->protocol because that isn't set on
902 PF_INET(6)/LOCAL_OUT. */
903 struct iphdr
*ip
= skb
->nh
.iph
;
905 if (ip
->version
== 4 && !brnf_call_iptables
)
907 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
909 else if (!brnf_deferred_hooks
)
912 if (hook
== NF_IP_POST_ROUTING
)
914 if (!nf_bridge_alloc(skb
))
918 nf_bridge
= skb
->nf_bridge
;
920 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
921 * will need the indev then. For a brouter, the real indev
922 * can be a bridge port, so we make sure br_nf_local_out()
923 * doesn't use the bridge parent of the indev by using
924 * the BRNF_DONT_TAKE_PARENT mask. */
925 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
926 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
927 nf_bridge
->physindev
= (struct net_device
*)in
;
929 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
930 /* the iptables outdev is br0.x, not br0 */
931 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
932 nf_bridge
->netoutdev
= (struct net_device
*)out
;
940 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
941 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
942 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
943 * ip_refrag() can return NF_STOLEN. */
944 static struct nf_hook_ops br_nf_ops
[] = {
945 { .hook
= br_nf_pre_routing
,
946 .owner
= THIS_MODULE
,
948 .hooknum
= NF_BR_PRE_ROUTING
,
949 .priority
= NF_BR_PRI_BRNF
, },
950 { .hook
= br_nf_local_in
,
951 .owner
= THIS_MODULE
,
953 .hooknum
= NF_BR_LOCAL_IN
,
954 .priority
= NF_BR_PRI_BRNF
, },
955 { .hook
= br_nf_forward_ip
,
956 .owner
= THIS_MODULE
,
958 .hooknum
= NF_BR_FORWARD
,
959 .priority
= NF_BR_PRI_BRNF
- 1, },
960 { .hook
= br_nf_forward_arp
,
961 .owner
= THIS_MODULE
,
963 .hooknum
= NF_BR_FORWARD
,
964 .priority
= NF_BR_PRI_BRNF
, },
965 { .hook
= br_nf_local_out
,
966 .owner
= THIS_MODULE
,
968 .hooknum
= NF_BR_LOCAL_OUT
,
969 .priority
= NF_BR_PRI_FIRST
, },
970 { .hook
= br_nf_post_routing
,
971 .owner
= THIS_MODULE
,
973 .hooknum
= NF_BR_POST_ROUTING
,
974 .priority
= NF_BR_PRI_LAST
, },
975 { .hook
= ip_sabotage_in
,
976 .owner
= THIS_MODULE
,
978 .hooknum
= NF_IP_PRE_ROUTING
,
979 .priority
= NF_IP_PRI_FIRST
, },
980 { .hook
= ip_sabotage_in
,
981 .owner
= THIS_MODULE
,
983 .hooknum
= NF_IP6_PRE_ROUTING
,
984 .priority
= NF_IP6_PRI_FIRST
, },
985 { .hook
= ip_sabotage_out
,
986 .owner
= THIS_MODULE
,
988 .hooknum
= NF_IP_FORWARD
,
989 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
990 { .hook
= ip_sabotage_out
,
991 .owner
= THIS_MODULE
,
993 .hooknum
= NF_IP6_FORWARD
,
994 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
995 { .hook
= ip_sabotage_out
,
996 .owner
= THIS_MODULE
,
998 .hooknum
= NF_IP_LOCAL_OUT
,
999 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
1000 { .hook
= ip_sabotage_out
,
1001 .owner
= THIS_MODULE
,
1003 .hooknum
= NF_IP6_LOCAL_OUT
,
1004 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
1005 { .hook
= ip_sabotage_out
,
1006 .owner
= THIS_MODULE
,
1008 .hooknum
= NF_IP_POST_ROUTING
,
1009 .priority
= NF_IP_PRI_FIRST
, },
1010 { .hook
= ip_sabotage_out
,
1011 .owner
= THIS_MODULE
,
1013 .hooknum
= NF_IP6_POST_ROUTING
,
1014 .priority
= NF_IP6_PRI_FIRST
, },
1017 #ifdef CONFIG_SYSCTL
1019 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
1020 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
1024 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1026 if (write
&& *(int *)(ctl
->data
))
1027 *(int *)(ctl
->data
) = 1;
1031 static ctl_table brnf_table
[] = {
1033 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
1034 .procname
= "bridge-nf-call-arptables",
1035 .data
= &brnf_call_arptables
,
1036 .maxlen
= sizeof(int),
1038 .proc_handler
= &brnf_sysctl_call_tables
,
1041 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
1042 .procname
= "bridge-nf-call-iptables",
1043 .data
= &brnf_call_iptables
,
1044 .maxlen
= sizeof(int),
1046 .proc_handler
= &brnf_sysctl_call_tables
,
1049 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1050 .procname
= "bridge-nf-call-ip6tables",
1051 .data
= &brnf_call_ip6tables
,
1052 .maxlen
= sizeof(int),
1054 .proc_handler
= &brnf_sysctl_call_tables
,
1057 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1058 .procname
= "bridge-nf-filter-vlan-tagged",
1059 .data
= &brnf_filter_vlan_tagged
,
1060 .maxlen
= sizeof(int),
1062 .proc_handler
= &brnf_sysctl_call_tables
,
1067 static ctl_table brnf_bridge_table
[] = {
1069 .ctl_name
= NET_BRIDGE
,
1070 .procname
= "bridge",
1072 .child
= brnf_table
,
1077 static ctl_table brnf_net_table
[] = {
1079 .ctl_name
= CTL_NET
,
1082 .child
= brnf_bridge_table
,
1088 int br_netfilter_init(void)
1092 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1095 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1099 nf_unregister_hook(&br_nf_ops
[i
]);
1104 #ifdef CONFIG_SYSCTL
1105 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1106 if (brnf_sysctl_header
== NULL
) {
1108 "br_netfilter: can't register to sysctl.\n");
1109 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1110 nf_unregister_hook(&br_nf_ops
[i
]);
1115 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1120 void br_netfilter_fini(void)
1124 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1125 nf_unregister_hook(&br_nf_ops
[i
]);
1126 #ifdef CONFIG_SYSCTL
1127 unregister_sysctl_table(brnf_sysctl_header
);