3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
7 * Bart De Schuymer (maintainer) <bdschuym@pandora.be>
10 * Apr 29 2003: physdev module support (bdschuym)
11 * Jun 19 2003: let arptables see bridged ARP traffic (bdschuym)
12 * Oct 06 2003: filter encapsulated IP/ARP VLAN traffic on untagged bridge
14 * Sep 01 2004: add IPv6 filtering (bdschuym)
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
21 * Lennert dedicates this file to Kerstin Wurdinger.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/netfilter_bridge.h>
33 #include <linux/netfilter_ipv4.h>
34 #include <linux/netfilter_ipv6.h>
35 #include <linux/netfilter_arp.h>
36 #include <linux/in_route.h>
40 #include <net/route.h>
42 #include <asm/uaccess.h>
43 #include <asm/checksum.h>
44 #include "br_private.h"
46 #include <linux/sysctl.h>
49 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
50 (skb->nf_bridge->data))->daddr.ipv4)
51 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
52 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
55 static struct ctl_table_header
*brnf_sysctl_header
;
56 static int brnf_call_iptables
= 1;
57 static int brnf_call_ip6tables
= 1;
58 static int brnf_call_arptables
= 1;
59 static int brnf_filter_vlan_tagged
= 1;
61 #define brnf_filter_vlan_tagged 1
64 static __be16
inline vlan_proto(const struct sk_buff
*skb
)
66 return vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
;
69 #define IS_VLAN_IP(skb) \
70 (skb->protocol == htons(ETH_P_8021Q) && \
71 vlan_proto(skb) == htons(ETH_P_IP) && \
72 brnf_filter_vlan_tagged)
74 #define IS_VLAN_IPV6(skb) \
75 (skb->protocol == htons(ETH_P_8021Q) && \
76 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
77 brnf_filter_vlan_tagged)
79 #define IS_VLAN_ARP(skb) \
80 (skb->protocol == htons(ETH_P_8021Q) && \
81 vlan_proto(skb) == htons(ETH_P_ARP) && \
82 brnf_filter_vlan_tagged)
84 /* We need these fake structures to make netfilter happy --
85 * lots of places assume that skb->dst != NULL, which isn't
86 * all that unreasonable.
88 * Currently, we fill in the PMTU entry because netfilter
89 * refragmentation needs it, and the rt_flags entry because
90 * ipt_REJECT needs it. Future netfilter modules might
91 * require us to fill additional fields. */
92 static struct net_device __fake_net_device
= {
93 .hard_header_len
= ETH_HLEN
96 static struct rtable __fake_rtable
= {
99 .__refcnt
= ATOMIC_INIT(1),
100 .dev
= &__fake_net_device
,
101 .path
= &__fake_rtable
.u
.dst
,
102 .metrics
= {[RTAX_MTU
- 1] = 1500},
109 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
111 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
113 return port
? port
->br
->dev
: NULL
;
116 static inline struct nf_bridge_info
*nf_bridge_alloc(struct sk_buff
*skb
)
118 skb
->nf_bridge
= kzalloc(sizeof(struct nf_bridge_info
), GFP_ATOMIC
);
119 if (likely(skb
->nf_bridge
))
120 atomic_set(&(skb
->nf_bridge
->use
), 1);
122 return skb
->nf_bridge
;
125 static inline void nf_bridge_save_header(struct sk_buff
*skb
)
127 int header_size
= 16;
129 if (skb
->protocol
== htons(ETH_P_8021Q
))
132 memcpy(skb
->nf_bridge
->data
, skb
->data
- header_size
, header_size
);
135 /* PF_BRIDGE/PRE_ROUTING *********************************************/
136 /* Undo the changes made for ip6tables PREROUTING and continue the
137 * bridge PRE_ROUTING hook. */
138 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
140 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
142 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
143 skb
->pkt_type
= PACKET_OTHERHOST
;
144 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
146 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
148 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
151 skb
->dev
= nf_bridge
->physindev
;
152 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
153 skb_push(skb
, VLAN_HLEN
);
154 skb
->nh
.raw
-= VLAN_HLEN
;
156 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
157 br_handle_frame_finish
, 1);
162 static void __br_dnat_complain(void)
164 static unsigned long last_complaint
;
166 if (jiffies
- last_complaint
>= 5 * HZ
) {
167 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
168 "forwarding to be enabled\n");
169 last_complaint
= jiffies
;
173 /* This requires some explaining. If DNAT has taken place,
174 * we will need to fix up the destination Ethernet address,
175 * and this is a tricky process.
177 * There are two cases to consider:
178 * 1. The packet was DNAT'ed to a device in the same bridge
179 * port group as it was received on. We can still bridge
181 * 2. The packet was DNAT'ed to a different device, either
182 * a non-bridged device or another bridge port group.
183 * The packet will need to be routed.
185 * The correct way of distinguishing between these two cases is to
186 * call ip_route_input() and to look at skb->dst->dev, which is
187 * changed to the destination device if ip_route_input() succeeds.
189 * Let us first consider the case that ip_route_input() succeeds:
191 * If skb->dst->dev equals the logical bridge device the packet
192 * came in on, we can consider this bridging. We then call
193 * skb->dst->output() which will make the packet enter br_nf_local_out()
194 * not much later. In that function it is assured that the iptables
195 * FORWARD chain is traversed for the packet.
197 * Otherwise, the packet is considered to be routed and we just
198 * change the destination MAC address so that the packet will
199 * later be passed up to the IP stack to be routed.
201 * Let us now consider the case that ip_route_input() fails:
203 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
204 * will fail, while __ip_route_output_key() will return success. The source
205 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
206 * thinks we're handling a locally generated packet and won't care
207 * if IP forwarding is allowed. We send a warning message to the users's
208 * log telling her to put IP forwarding on.
210 * ip_route_input() will also fail if there is no route available.
211 * In that case we just drop the packet.
213 * --Lennert, 20020411
214 * --Bart, 20020416 (updated)
215 * --Bart, 20021007 (updated) */
216 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
218 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
219 skb
->pkt_type
= PACKET_HOST
;
220 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
222 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
224 skb
->dev
= bridge_parent(skb
->dev
);
228 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
229 skb_pull(skb
, VLAN_HLEN
);
230 skb
->nh
.raw
+= VLAN_HLEN
;
232 skb
->dst
->output(skb
);
237 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
239 struct net_device
*dev
= skb
->dev
;
240 struct iphdr
*iph
= skb
->nh
.iph
;
241 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
243 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
244 skb
->pkt_type
= PACKET_OTHERHOST
;
245 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
247 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
249 if (dnat_took_place(skb
)) {
250 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
)) {
257 .tos
= RT_TOS(iph
->tos
) },
262 if (!ip_route_output_key(&rt
, &fl
)) {
263 /* - Bridged-and-DNAT'ed traffic doesn't
264 * require ip_forwarding.
265 * - Deal with redirected traffic. */
266 if (((struct dst_entry
*)rt
)->dev
== dev
||
267 rt
->rt_type
== RTN_LOCAL
) {
268 skb
->dst
= (struct dst_entry
*)rt
;
271 __br_dnat_complain();
272 dst_release((struct dst_entry
*)rt
);
277 if (skb
->dst
->dev
== dev
) {
279 /* Tell br_nf_local_out this is a
281 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
282 skb
->dev
= nf_bridge
->physindev
;
284 htons(ETH_P_8021Q
)) {
285 skb_push(skb
, VLAN_HLEN
);
286 skb
->nh
.raw
-= VLAN_HLEN
;
288 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
290 br_nf_pre_routing_finish_bridge
,
294 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
, ETH_ALEN
);
295 skb
->pkt_type
= PACKET_HOST
;
298 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
302 skb
->dev
= nf_bridge
->physindev
;
303 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
304 skb_push(skb
, VLAN_HLEN
);
305 skb
->nh
.raw
-= VLAN_HLEN
;
307 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
308 br_handle_frame_finish
, 1);
313 /* Some common code for IPv4/IPv6 */
314 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
316 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
318 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
319 skb
->pkt_type
= PACKET_HOST
;
320 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
323 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
324 nf_bridge
->physindev
= skb
->dev
;
325 skb
->dev
= bridge_parent(skb
->dev
);
330 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
331 static int check_hbh_len(struct sk_buff
*skb
)
333 unsigned char *raw
= (u8
*) (skb
->nh
.ipv6h
+ 1);
335 int off
= raw
- skb
->nh
.raw
;
336 int len
= (raw
[1] + 1) << 3;
338 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
345 int optlen
= skb
->nh
.raw
[off
+ 1] + 2;
347 switch (skb
->nh
.raw
[off
]) {
356 if (skb
->nh
.raw
[off
+ 1] != 4 || (off
& 3) != 2)
358 pkt_len
= ntohl(*(u32
*) (skb
->nh
.raw
+ off
+ 2));
359 if (pkt_len
<= IPV6_MAXPLEN
||
360 skb
->nh
.ipv6h
->payload_len
)
362 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
364 if (pskb_trim_rcsum(skb
,
365 pkt_len
+ sizeof(struct ipv6hdr
)))
383 /* Replicate the checks that IPv6 does on packet reception and pass the packet
384 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
385 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
387 const struct net_device
*in
,
388 const struct net_device
*out
,
389 int (*okfn
)(struct sk_buff
*))
394 if (skb
->len
< sizeof(struct ipv6hdr
))
397 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
402 if (hdr
->version
!= 6)
405 pkt_len
= ntohs(hdr
->payload_len
);
407 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
408 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
410 if (pskb_trim_rcsum(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
413 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
416 nf_bridge_put(skb
->nf_bridge
);
417 if (!nf_bridge_alloc(skb
))
419 if (!setup_pre_routing(skb
))
422 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
423 br_nf_pre_routing_finish_ipv6
);
431 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
432 * Replicate the checks that IPv4 does on packet reception.
433 * Set skb->dev to the bridge device (i.e. parent of the
434 * receiving device) to make netfilter happy, the REDIRECT
435 * target in particular. Save the original destination IP
436 * address to be able to detect DNAT afterwards. */
437 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
438 const struct net_device
*in
,
439 const struct net_device
*out
,
440 int (*okfn
)(struct sk_buff
*))
444 struct sk_buff
*skb
= *pskb
;
446 if (skb
->protocol
== htons(ETH_P_IPV6
) || IS_VLAN_IPV6(skb
)) {
448 if (!brnf_call_ip6tables
)
451 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
454 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
455 skb_pull_rcsum(skb
, VLAN_HLEN
);
456 skb
->nh
.raw
+= VLAN_HLEN
;
458 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
461 if (!brnf_call_iptables
)
465 if (skb
->protocol
!= htons(ETH_P_IP
) && !IS_VLAN_IP(skb
))
468 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
471 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
472 skb_pull_rcsum(skb
, VLAN_HLEN
);
473 skb
->nh
.raw
+= VLAN_HLEN
;
476 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
480 if (iph
->ihl
< 5 || iph
->version
!= 4)
483 if (!pskb_may_pull(skb
, 4 * iph
->ihl
))
487 if (ip_fast_csum((__u8
*) iph
, iph
->ihl
) != 0)
490 len
= ntohs(iph
->tot_len
);
491 if (skb
->len
< len
|| len
< 4 * iph
->ihl
)
494 pskb_trim_rcsum(skb
, len
);
496 nf_bridge_put(skb
->nf_bridge
);
497 if (!nf_bridge_alloc(skb
))
499 if (!setup_pre_routing(skb
))
501 store_orig_dstaddr(skb
);
503 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
504 br_nf_pre_routing_finish
);
509 // IP_INC_STATS_BH(IpInHdrErrors);
515 /* PF_BRIDGE/LOCAL_IN ************************************************/
516 /* The packet is locally destined, which requires a real
517 * dst_entry, so detach the fake one. On the way up, the
518 * packet would pass through PRE_ROUTING again (which already
519 * took place when the packet entered the bridge), but we
520 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
521 * prevent this from happening. */
522 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
523 const struct net_device
*in
,
524 const struct net_device
*out
,
525 int (*okfn
)(struct sk_buff
*))
527 struct sk_buff
*skb
= *pskb
;
529 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
530 dst_release(skb
->dst
);
537 /* PF_BRIDGE/FORWARD *************************************************/
538 static int br_nf_forward_finish(struct sk_buff
*skb
)
540 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
541 struct net_device
*in
;
543 if (skb
->protocol
!= htons(ETH_P_ARP
) && !IS_VLAN_ARP(skb
)) {
544 in
= nf_bridge
->physindev
;
545 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
546 skb
->pkt_type
= PACKET_OTHERHOST
;
547 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
550 in
= *((struct net_device
**)(skb
->cb
));
552 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
553 skb_push(skb
, VLAN_HLEN
);
554 skb
->nh
.raw
-= VLAN_HLEN
;
556 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
557 skb
->dev
, br_forward_finish
, 1);
561 /* This is the 'purely bridged' case. For IP, we pass the packet to
562 * netfilter with indev and outdev set to the bridge device,
563 * but we are still able to filter on the 'real' indev/outdev
564 * because of the physdev module. For ARP, indev and outdev are the
566 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
567 const struct net_device
*in
,
568 const struct net_device
*out
,
569 int (*okfn
)(struct sk_buff
*))
571 struct sk_buff
*skb
= *pskb
;
572 struct nf_bridge_info
*nf_bridge
;
573 struct net_device
*parent
;
579 parent
= bridge_parent(out
);
583 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
588 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
589 skb_pull(*pskb
, VLAN_HLEN
);
590 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
593 nf_bridge
= skb
->nf_bridge
;
594 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
595 skb
->pkt_type
= PACKET_HOST
;
596 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
599 /* The physdev module checks on this */
600 nf_bridge
->mask
|= BRNF_BRIDGED
;
601 nf_bridge
->physoutdev
= skb
->dev
;
603 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
604 br_nf_forward_finish
);
609 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
610 const struct net_device
*in
,
611 const struct net_device
*out
,
612 int (*okfn
)(struct sk_buff
*))
614 struct sk_buff
*skb
= *pskb
;
615 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
618 if (!brnf_call_arptables
)
622 if (skb
->protocol
!= htons(ETH_P_ARP
)) {
623 if (!IS_VLAN_ARP(skb
))
625 skb_pull(*pskb
, VLAN_HLEN
);
626 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
629 if (skb
->nh
.arph
->ar_pln
!= 4) {
630 if (IS_VLAN_ARP(skb
)) {
631 skb_push(*pskb
, VLAN_HLEN
);
632 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
636 *d
= (struct net_device
*)in
;
637 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
638 (struct net_device
*)out
, br_nf_forward_finish
);
643 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
644 static int br_nf_local_out_finish(struct sk_buff
*skb
)
646 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
647 skb_push(skb
, VLAN_HLEN
);
648 skb
->nh
.raw
-= VLAN_HLEN
;
651 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
652 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
657 /* This function sees both locally originated IP packets and forwarded
658 * IP packets (in both cases the destination device is a bridge
659 * device). It also sees bridged-and-DNAT'ed packets.
660 * To be able to filter on the physical bridge devices (with the physdev
661 * module), we steal packets destined to a bridge device away from the
662 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
663 * when we have determined the real output device. This is done in here.
665 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
666 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
667 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
668 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
670 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
671 * this packet before, and so the packet was locally originated. We fake
672 * the PF_INET/LOCAL_OUT hook.
673 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
674 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
675 * even routed packets that didn't arrive on a bridge interface have their
676 * nf_bridge->physindev set. */
677 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
678 const struct net_device
*in
,
679 const struct net_device
*out
,
680 int (*okfn
)(struct sk_buff
*))
682 struct net_device
*realindev
, *realoutdev
;
683 struct sk_buff
*skb
= *pskb
;
684 struct nf_bridge_info
*nf_bridge
;
690 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
695 #ifdef CONFIG_NETFILTER_DEBUG
696 /* Sometimes we get packets with NULL ->dst here (for example,
697 * running a dhcp client daemon triggers this). This should now
698 * be fixed, but let's keep the check around. */
699 if (skb
->dst
== NULL
) {
700 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
705 nf_bridge
= skb
->nf_bridge
;
706 nf_bridge
->physoutdev
= skb
->dev
;
707 realindev
= nf_bridge
->physindev
;
709 /* Bridged, take PF_BRIDGE/FORWARD.
710 * (see big note in front of br_nf_pre_routing_finish) */
711 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
712 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
713 skb
->pkt_type
= PACKET_OTHERHOST
;
714 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
716 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
717 skb_push(skb
, VLAN_HLEN
);
718 skb
->nh
.raw
-= VLAN_HLEN
;
721 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
722 skb
->dev
, br_forward_finish
);
725 realoutdev
= bridge_parent(skb
->dev
);
729 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
730 /* iptables should match -o br0.x */
731 if (nf_bridge
->netoutdev
)
732 realoutdev
= nf_bridge
->netoutdev
;
734 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
735 skb_pull(skb
, VLAN_HLEN
);
736 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
738 /* IP forwarded traffic has a physindev, locally
739 * generated traffic hasn't. */
740 if (realindev
!= NULL
) {
741 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
)) {
742 struct net_device
*parent
= bridge_parent(realindev
);
747 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
748 realoutdev
, br_nf_local_out_finish
,
749 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
751 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
752 realoutdev
, br_nf_local_out_finish
,
753 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
760 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
762 if (skb
->protocol
== htons(ETH_P_IP
) &&
763 skb
->len
> skb
->dev
->mtu
&&
764 !(skb_shinfo(skb
)->ufo_size
|| skb_shinfo(skb
)->tso_size
))
765 return ip_fragment(skb
, br_dev_queue_push_xmit
);
767 return br_dev_queue_push_xmit(skb
);
770 /* PF_BRIDGE/POST_ROUTING ********************************************/
771 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
772 const struct net_device
*in
,
773 const struct net_device
*out
,
774 int (*okfn
)(struct sk_buff
*))
776 struct sk_buff
*skb
= *pskb
;
777 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
778 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
781 #ifdef CONFIG_NETFILTER_DEBUG
782 /* Be very paranoid. This probably won't happen anymore, but let's
783 * keep the check just to be sure... */
784 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
785 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
786 "bad mac.raw pointer.");
797 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
802 #ifdef CONFIG_NETFILTER_DEBUG
803 if (skb
->dst
== NULL
) {
804 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
809 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
810 * about the value of skb->pkt_type. */
811 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
812 skb
->pkt_type
= PACKET_HOST
;
813 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
816 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
817 skb_pull(skb
, VLAN_HLEN
);
818 skb
->nh
.raw
+= VLAN_HLEN
;
821 nf_bridge_save_header(skb
);
823 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
824 if (nf_bridge
->netoutdev
)
825 realoutdev
= nf_bridge
->netoutdev
;
827 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
828 br_nf_dev_queue_xmit
);
832 #ifdef CONFIG_NETFILTER_DEBUG
834 if (skb
->dev
!= NULL
) {
835 printk("[%s]", skb
->dev
->name
);
837 printk("[%s]", realoutdev
->name
);
839 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
845 /* IP/SABOTAGE *****************************************************/
846 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
847 * for the second time. */
848 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
849 const struct net_device
*in
,
850 const struct net_device
*out
,
851 int (*okfn
)(struct sk_buff
*))
853 if ((*pskb
)->nf_bridge
&&
854 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
861 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
862 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
863 * decision in the bridge code and have determined nf_bridge->physoutdev. */
864 static unsigned int ip_sabotage_out(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 struct sk_buff
*skb
= *pskb
;
871 if ((out
->hard_start_xmit
== br_dev_xmit
&&
872 okfn
!= br_nf_forward_finish
&&
873 okfn
!= br_nf_local_out_finish
&& okfn
!= br_nf_dev_queue_xmit
)
874 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
875 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
876 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
879 struct nf_bridge_info
*nf_bridge
;
881 if (!skb
->nf_bridge
) {
883 /* This code is executed while in the IP(v6) stack,
884 the version should be 4 or 6. We can't use
885 skb->protocol because that isn't set on
886 PF_INET(6)/LOCAL_OUT. */
887 struct iphdr
*ip
= skb
->nh
.iph
;
889 if (ip
->version
== 4 && !brnf_call_iptables
)
891 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
894 if (hook
== NF_IP_POST_ROUTING
)
896 if (!nf_bridge_alloc(skb
))
900 nf_bridge
= skb
->nf_bridge
;
902 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
903 * will need the indev then. For a brouter, the real indev
904 * can be a bridge port, so we make sure br_nf_local_out()
905 * doesn't use the bridge parent of the indev by using
906 * the BRNF_DONT_TAKE_PARENT mask. */
907 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
908 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
909 nf_bridge
->physindev
= (struct net_device
*)in
;
911 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
912 /* the iptables outdev is br0.x, not br0 */
913 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
914 nf_bridge
->netoutdev
= (struct net_device
*)out
;
922 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
923 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
924 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
925 * ip_refrag() can return NF_STOLEN. */
926 static struct nf_hook_ops br_nf_ops
[] = {
927 { .hook
= br_nf_pre_routing
,
928 .owner
= THIS_MODULE
,
930 .hooknum
= NF_BR_PRE_ROUTING
,
931 .priority
= NF_BR_PRI_BRNF
, },
932 { .hook
= br_nf_local_in
,
933 .owner
= THIS_MODULE
,
935 .hooknum
= NF_BR_LOCAL_IN
,
936 .priority
= NF_BR_PRI_BRNF
, },
937 { .hook
= br_nf_forward_ip
,
938 .owner
= THIS_MODULE
,
940 .hooknum
= NF_BR_FORWARD
,
941 .priority
= NF_BR_PRI_BRNF
- 1, },
942 { .hook
= br_nf_forward_arp
,
943 .owner
= THIS_MODULE
,
945 .hooknum
= NF_BR_FORWARD
,
946 .priority
= NF_BR_PRI_BRNF
, },
947 { .hook
= br_nf_local_out
,
948 .owner
= THIS_MODULE
,
950 .hooknum
= NF_BR_LOCAL_OUT
,
951 .priority
= NF_BR_PRI_FIRST
, },
952 { .hook
= br_nf_post_routing
,
953 .owner
= THIS_MODULE
,
955 .hooknum
= NF_BR_POST_ROUTING
,
956 .priority
= NF_BR_PRI_LAST
, },
957 { .hook
= ip_sabotage_in
,
958 .owner
= THIS_MODULE
,
960 .hooknum
= NF_IP_PRE_ROUTING
,
961 .priority
= NF_IP_PRI_FIRST
, },
962 { .hook
= ip_sabotage_in
,
963 .owner
= THIS_MODULE
,
965 .hooknum
= NF_IP6_PRE_ROUTING
,
966 .priority
= NF_IP6_PRI_FIRST
, },
967 { .hook
= ip_sabotage_out
,
968 .owner
= THIS_MODULE
,
970 .hooknum
= NF_IP_FORWARD
,
971 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
972 { .hook
= ip_sabotage_out
,
973 .owner
= THIS_MODULE
,
975 .hooknum
= NF_IP6_FORWARD
,
976 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
977 { .hook
= ip_sabotage_out
,
978 .owner
= THIS_MODULE
,
980 .hooknum
= NF_IP_LOCAL_OUT
,
981 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
982 { .hook
= ip_sabotage_out
,
983 .owner
= THIS_MODULE
,
985 .hooknum
= NF_IP6_LOCAL_OUT
,
986 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
987 { .hook
= ip_sabotage_out
,
988 .owner
= THIS_MODULE
,
990 .hooknum
= NF_IP_POST_ROUTING
,
991 .priority
= NF_IP_PRI_FIRST
, },
992 { .hook
= ip_sabotage_out
,
993 .owner
= THIS_MODULE
,
995 .hooknum
= NF_IP6_POST_ROUTING
,
996 .priority
= NF_IP6_PRI_FIRST
, },
1001 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
1002 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
1006 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1008 if (write
&& *(int *)(ctl
->data
))
1009 *(int *)(ctl
->data
) = 1;
1013 static ctl_table brnf_table
[] = {
1015 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
1016 .procname
= "bridge-nf-call-arptables",
1017 .data
= &brnf_call_arptables
,
1018 .maxlen
= sizeof(int),
1020 .proc_handler
= &brnf_sysctl_call_tables
,
1023 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
1024 .procname
= "bridge-nf-call-iptables",
1025 .data
= &brnf_call_iptables
,
1026 .maxlen
= sizeof(int),
1028 .proc_handler
= &brnf_sysctl_call_tables
,
1031 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1032 .procname
= "bridge-nf-call-ip6tables",
1033 .data
= &brnf_call_ip6tables
,
1034 .maxlen
= sizeof(int),
1036 .proc_handler
= &brnf_sysctl_call_tables
,
1039 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1040 .procname
= "bridge-nf-filter-vlan-tagged",
1041 .data
= &brnf_filter_vlan_tagged
,
1042 .maxlen
= sizeof(int),
1044 .proc_handler
= &brnf_sysctl_call_tables
,
1049 static ctl_table brnf_bridge_table
[] = {
1051 .ctl_name
= NET_BRIDGE
,
1052 .procname
= "bridge",
1054 .child
= brnf_table
,
1059 static ctl_table brnf_net_table
[] = {
1061 .ctl_name
= CTL_NET
,
1064 .child
= brnf_bridge_table
,
1070 int br_netfilter_init(void)
1074 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1077 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1081 nf_unregister_hook(&br_nf_ops
[i
]);
1086 #ifdef CONFIG_SYSCTL
1087 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1088 if (brnf_sysctl_header
== NULL
) {
1090 "br_netfilter: can't register to sysctl.\n");
1091 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1092 nf_unregister_hook(&br_nf_ops
[i
]);
1097 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1102 void br_netfilter_fini(void)
1106 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1107 nf_unregister_hook(&br_nf_ops
[i
]);
1108 #ifdef CONFIG_SYSCTL
1109 unregister_sysctl_table(brnf_sysctl_header
);