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 (pkt_len
+ sizeof(struct ipv6hdr
) < skb
->len
) {
411 if (__pskb_trim(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
413 if (skb
->ip_summed
== CHECKSUM_HW
)
414 skb
->ip_summed
= CHECKSUM_NONE
;
417 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
420 nf_bridge_put(skb
->nf_bridge
);
421 if (!nf_bridge_alloc(skb
))
423 if (!setup_pre_routing(skb
))
426 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
427 br_nf_pre_routing_finish_ipv6
);
435 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
436 * Replicate the checks that IPv4 does on packet reception.
437 * Set skb->dev to the bridge device (i.e. parent of the
438 * receiving device) to make netfilter happy, the REDIRECT
439 * target in particular. Save the original destination IP
440 * address to be able to detect DNAT afterwards. */
441 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
442 const struct net_device
*in
,
443 const struct net_device
*out
,
444 int (*okfn
)(struct sk_buff
*))
448 struct sk_buff
*skb
= *pskb
;
450 if (skb
->protocol
== htons(ETH_P_IPV6
) || IS_VLAN_IPV6(skb
)) {
452 if (!brnf_call_ip6tables
)
455 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
458 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
459 skb_pull_rcsum(skb
, VLAN_HLEN
);
460 skb
->nh
.raw
+= VLAN_HLEN
;
462 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
465 if (!brnf_call_iptables
)
469 if (skb
->protocol
!= htons(ETH_P_IP
) && !IS_VLAN_IP(skb
))
472 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
475 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
476 skb_pull_rcsum(skb
, VLAN_HLEN
);
477 skb
->nh
.raw
+= VLAN_HLEN
;
480 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
484 if (iph
->ihl
< 5 || iph
->version
!= 4)
487 if (!pskb_may_pull(skb
, 4 * iph
->ihl
))
491 if (ip_fast_csum((__u8
*) iph
, iph
->ihl
) != 0)
494 len
= ntohs(iph
->tot_len
);
495 if (skb
->len
< len
|| len
< 4 * iph
->ihl
)
498 if (skb
->len
> len
) {
499 __pskb_trim(skb
, len
);
500 if (skb
->ip_summed
== CHECKSUM_HW
)
501 skb
->ip_summed
= CHECKSUM_NONE
;
504 nf_bridge_put(skb
->nf_bridge
);
505 if (!nf_bridge_alloc(skb
))
507 if (!setup_pre_routing(skb
))
509 store_orig_dstaddr(skb
);
511 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
512 br_nf_pre_routing_finish
);
517 // IP_INC_STATS_BH(IpInHdrErrors);
523 /* PF_BRIDGE/LOCAL_IN ************************************************/
524 /* The packet is locally destined, which requires a real
525 * dst_entry, so detach the fake one. On the way up, the
526 * packet would pass through PRE_ROUTING again (which already
527 * took place when the packet entered the bridge), but we
528 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
529 * prevent this from happening. */
530 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
531 const struct net_device
*in
,
532 const struct net_device
*out
,
533 int (*okfn
)(struct sk_buff
*))
535 struct sk_buff
*skb
= *pskb
;
537 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
538 dst_release(skb
->dst
);
545 /* PF_BRIDGE/FORWARD *************************************************/
546 static int br_nf_forward_finish(struct sk_buff
*skb
)
548 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
549 struct net_device
*in
;
551 if (skb
->protocol
!= htons(ETH_P_ARP
) && !IS_VLAN_ARP(skb
)) {
552 in
= nf_bridge
->physindev
;
553 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
554 skb
->pkt_type
= PACKET_OTHERHOST
;
555 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
558 in
= *((struct net_device
**)(skb
->cb
));
560 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
561 skb_push(skb
, VLAN_HLEN
);
562 skb
->nh
.raw
-= VLAN_HLEN
;
564 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
565 skb
->dev
, br_forward_finish
, 1);
569 /* This is the 'purely bridged' case. For IP, we pass the packet to
570 * netfilter with indev and outdev set to the bridge device,
571 * but we are still able to filter on the 'real' indev/outdev
572 * because of the physdev module. For ARP, indev and outdev are the
574 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
575 const struct net_device
*in
,
576 const struct net_device
*out
,
577 int (*okfn
)(struct sk_buff
*))
579 struct sk_buff
*skb
= *pskb
;
580 struct nf_bridge_info
*nf_bridge
;
581 struct net_device
*parent
;
587 parent
= bridge_parent(out
);
591 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
596 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
597 skb_pull(*pskb
, VLAN_HLEN
);
598 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
601 nf_bridge
= skb
->nf_bridge
;
602 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
603 skb
->pkt_type
= PACKET_HOST
;
604 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
607 /* The physdev module checks on this */
608 nf_bridge
->mask
|= BRNF_BRIDGED
;
609 nf_bridge
->physoutdev
= skb
->dev
;
611 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
612 br_nf_forward_finish
);
617 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
618 const struct net_device
*in
,
619 const struct net_device
*out
,
620 int (*okfn
)(struct sk_buff
*))
622 struct sk_buff
*skb
= *pskb
;
623 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
626 if (!brnf_call_arptables
)
630 if (skb
->protocol
!= htons(ETH_P_ARP
)) {
631 if (!IS_VLAN_ARP(skb
))
633 skb_pull(*pskb
, VLAN_HLEN
);
634 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
637 if (skb
->nh
.arph
->ar_pln
!= 4) {
638 if (IS_VLAN_ARP(skb
)) {
639 skb_push(*pskb
, VLAN_HLEN
);
640 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
644 *d
= (struct net_device
*)in
;
645 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
646 (struct net_device
*)out
, br_nf_forward_finish
);
651 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
652 static int br_nf_local_out_finish(struct sk_buff
*skb
)
654 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
655 skb_push(skb
, VLAN_HLEN
);
656 skb
->nh
.raw
-= VLAN_HLEN
;
659 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
660 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
665 /* This function sees both locally originated IP packets and forwarded
666 * IP packets (in both cases the destination device is a bridge
667 * device). It also sees bridged-and-DNAT'ed packets.
668 * To be able to filter on the physical bridge devices (with the physdev
669 * module), we steal packets destined to a bridge device away from the
670 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
671 * when we have determined the real output device. This is done in here.
673 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
674 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
675 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
676 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
678 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
679 * this packet before, and so the packet was locally originated. We fake
680 * the PF_INET/LOCAL_OUT hook.
681 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
682 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
683 * even routed packets that didn't arrive on a bridge interface have their
684 * nf_bridge->physindev set. */
685 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
686 const struct net_device
*in
,
687 const struct net_device
*out
,
688 int (*okfn
)(struct sk_buff
*))
690 struct net_device
*realindev
, *realoutdev
;
691 struct sk_buff
*skb
= *pskb
;
692 struct nf_bridge_info
*nf_bridge
;
698 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
703 #ifdef CONFIG_NETFILTER_DEBUG
704 /* Sometimes we get packets with NULL ->dst here (for example,
705 * running a dhcp client daemon triggers this). This should now
706 * be fixed, but let's keep the check around. */
707 if (skb
->dst
== NULL
) {
708 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
713 nf_bridge
= skb
->nf_bridge
;
714 nf_bridge
->physoutdev
= skb
->dev
;
715 realindev
= nf_bridge
->physindev
;
717 /* Bridged, take PF_BRIDGE/FORWARD.
718 * (see big note in front of br_nf_pre_routing_finish) */
719 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
720 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
721 skb
->pkt_type
= PACKET_OTHERHOST
;
722 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
724 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
725 skb_push(skb
, VLAN_HLEN
);
726 skb
->nh
.raw
-= VLAN_HLEN
;
729 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
730 skb
->dev
, br_forward_finish
);
733 realoutdev
= bridge_parent(skb
->dev
);
737 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
738 /* iptables should match -o br0.x */
739 if (nf_bridge
->netoutdev
)
740 realoutdev
= nf_bridge
->netoutdev
;
742 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
743 skb_pull(skb
, VLAN_HLEN
);
744 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
746 /* IP forwarded traffic has a physindev, locally
747 * generated traffic hasn't. */
748 if (realindev
!= NULL
) {
749 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
)) {
750 struct net_device
*parent
= bridge_parent(realindev
);
755 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
756 realoutdev
, br_nf_local_out_finish
,
757 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
759 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
760 realoutdev
, br_nf_local_out_finish
,
761 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
768 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
770 if (skb
->protocol
== htons(ETH_P_IP
) &&
771 skb
->len
> skb
->dev
->mtu
&&
772 !(skb_shinfo(skb
)->ufo_size
|| skb_shinfo(skb
)->tso_size
))
773 return ip_fragment(skb
, br_dev_queue_push_xmit
);
775 return br_dev_queue_push_xmit(skb
);
778 /* PF_BRIDGE/POST_ROUTING ********************************************/
779 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
780 const struct net_device
*in
,
781 const struct net_device
*out
,
782 int (*okfn
)(struct sk_buff
*))
784 struct sk_buff
*skb
= *pskb
;
785 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
786 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
789 #ifdef CONFIG_NETFILTER_DEBUG
790 /* Be very paranoid. This probably won't happen anymore, but let's
791 * keep the check just to be sure... */
792 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
793 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
794 "bad mac.raw pointer.");
805 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
810 #ifdef CONFIG_NETFILTER_DEBUG
811 if (skb
->dst
== NULL
) {
812 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
817 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
818 * about the value of skb->pkt_type. */
819 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
820 skb
->pkt_type
= PACKET_HOST
;
821 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
824 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
825 skb_pull(skb
, VLAN_HLEN
);
826 skb
->nh
.raw
+= VLAN_HLEN
;
829 nf_bridge_save_header(skb
);
831 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
832 if (nf_bridge
->netoutdev
)
833 realoutdev
= nf_bridge
->netoutdev
;
835 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
836 br_nf_dev_queue_xmit
);
840 #ifdef CONFIG_NETFILTER_DEBUG
842 if (skb
->dev
!= NULL
) {
843 printk("[%s]", skb
->dev
->name
);
845 printk("[%s]", realoutdev
->name
);
847 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
853 /* IP/SABOTAGE *****************************************************/
854 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
855 * for the second time. */
856 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
857 const struct net_device
*in
,
858 const struct net_device
*out
,
859 int (*okfn
)(struct sk_buff
*))
861 if ((*pskb
)->nf_bridge
&&
862 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
869 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
870 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
871 * decision in the bridge code and have determined nf_bridge->physoutdev. */
872 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
873 const struct net_device
*in
,
874 const struct net_device
*out
,
875 int (*okfn
)(struct sk_buff
*))
877 struct sk_buff
*skb
= *pskb
;
879 if ((out
->hard_start_xmit
== br_dev_xmit
&&
880 okfn
!= br_nf_forward_finish
&&
881 okfn
!= br_nf_local_out_finish
&& okfn
!= br_nf_dev_queue_xmit
)
882 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
883 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
884 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
887 struct nf_bridge_info
*nf_bridge
;
889 if (!skb
->nf_bridge
) {
891 /* This code is executed while in the IP(v6) stack,
892 the version should be 4 or 6. We can't use
893 skb->protocol because that isn't set on
894 PF_INET(6)/LOCAL_OUT. */
895 struct iphdr
*ip
= skb
->nh
.iph
;
897 if (ip
->version
== 4 && !brnf_call_iptables
)
899 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
902 if (hook
== NF_IP_POST_ROUTING
)
904 if (!nf_bridge_alloc(skb
))
908 nf_bridge
= skb
->nf_bridge
;
910 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
911 * will need the indev then. For a brouter, the real indev
912 * can be a bridge port, so we make sure br_nf_local_out()
913 * doesn't use the bridge parent of the indev by using
914 * the BRNF_DONT_TAKE_PARENT mask. */
915 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
916 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
917 nf_bridge
->physindev
= (struct net_device
*)in
;
919 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
920 /* the iptables outdev is br0.x, not br0 */
921 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
922 nf_bridge
->netoutdev
= (struct net_device
*)out
;
930 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
931 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
932 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
933 * ip_refrag() can return NF_STOLEN. */
934 static struct nf_hook_ops br_nf_ops
[] = {
935 { .hook
= br_nf_pre_routing
,
936 .owner
= THIS_MODULE
,
938 .hooknum
= NF_BR_PRE_ROUTING
,
939 .priority
= NF_BR_PRI_BRNF
, },
940 { .hook
= br_nf_local_in
,
941 .owner
= THIS_MODULE
,
943 .hooknum
= NF_BR_LOCAL_IN
,
944 .priority
= NF_BR_PRI_BRNF
, },
945 { .hook
= br_nf_forward_ip
,
946 .owner
= THIS_MODULE
,
948 .hooknum
= NF_BR_FORWARD
,
949 .priority
= NF_BR_PRI_BRNF
- 1, },
950 { .hook
= br_nf_forward_arp
,
951 .owner
= THIS_MODULE
,
953 .hooknum
= NF_BR_FORWARD
,
954 .priority
= NF_BR_PRI_BRNF
, },
955 { .hook
= br_nf_local_out
,
956 .owner
= THIS_MODULE
,
958 .hooknum
= NF_BR_LOCAL_OUT
,
959 .priority
= NF_BR_PRI_FIRST
, },
960 { .hook
= br_nf_post_routing
,
961 .owner
= THIS_MODULE
,
963 .hooknum
= NF_BR_POST_ROUTING
,
964 .priority
= NF_BR_PRI_LAST
, },
965 { .hook
= ip_sabotage_in
,
966 .owner
= THIS_MODULE
,
968 .hooknum
= NF_IP_PRE_ROUTING
,
969 .priority
= NF_IP_PRI_FIRST
, },
970 { .hook
= ip_sabotage_in
,
971 .owner
= THIS_MODULE
,
973 .hooknum
= NF_IP6_PRE_ROUTING
,
974 .priority
= NF_IP6_PRI_FIRST
, },
975 { .hook
= ip_sabotage_out
,
976 .owner
= THIS_MODULE
,
978 .hooknum
= NF_IP_FORWARD
,
979 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
980 { .hook
= ip_sabotage_out
,
981 .owner
= THIS_MODULE
,
983 .hooknum
= NF_IP6_FORWARD
,
984 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
985 { .hook
= ip_sabotage_out
,
986 .owner
= THIS_MODULE
,
988 .hooknum
= NF_IP_LOCAL_OUT
,
989 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
990 { .hook
= ip_sabotage_out
,
991 .owner
= THIS_MODULE
,
993 .hooknum
= NF_IP6_LOCAL_OUT
,
994 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
995 { .hook
= ip_sabotage_out
,
996 .owner
= THIS_MODULE
,
998 .hooknum
= NF_IP_POST_ROUTING
,
999 .priority
= NF_IP_PRI_FIRST
, },
1000 { .hook
= ip_sabotage_out
,
1001 .owner
= THIS_MODULE
,
1003 .hooknum
= NF_IP6_POST_ROUTING
,
1004 .priority
= NF_IP6_PRI_FIRST
, },
1007 #ifdef CONFIG_SYSCTL
1009 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
1010 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
1014 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1016 if (write
&& *(int *)(ctl
->data
))
1017 *(int *)(ctl
->data
) = 1;
1021 static ctl_table brnf_table
[] = {
1023 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
1024 .procname
= "bridge-nf-call-arptables",
1025 .data
= &brnf_call_arptables
,
1026 .maxlen
= sizeof(int),
1028 .proc_handler
= &brnf_sysctl_call_tables
,
1031 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
1032 .procname
= "bridge-nf-call-iptables",
1033 .data
= &brnf_call_iptables
,
1034 .maxlen
= sizeof(int),
1036 .proc_handler
= &brnf_sysctl_call_tables
,
1039 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1040 .procname
= "bridge-nf-call-ip6tables",
1041 .data
= &brnf_call_ip6tables
,
1042 .maxlen
= sizeof(int),
1044 .proc_handler
= &brnf_sysctl_call_tables
,
1047 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1048 .procname
= "bridge-nf-filter-vlan-tagged",
1049 .data
= &brnf_filter_vlan_tagged
,
1050 .maxlen
= sizeof(int),
1052 .proc_handler
= &brnf_sysctl_call_tables
,
1057 static ctl_table brnf_bridge_table
[] = {
1059 .ctl_name
= NET_BRIDGE
,
1060 .procname
= "bridge",
1062 .child
= brnf_table
,
1067 static ctl_table brnf_net_table
[] = {
1069 .ctl_name
= CTL_NET
,
1072 .child
= brnf_bridge_table
,
1078 int br_netfilter_init(void)
1082 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1085 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1089 nf_unregister_hook(&br_nf_ops
[i
]);
1094 #ifdef CONFIG_SYSCTL
1095 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1096 if (brnf_sysctl_header
== NULL
) {
1098 "br_netfilter: can't register to sysctl.\n");
1099 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1100 nf_unregister_hook(&br_nf_ops
[i
]);
1105 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1110 void br_netfilter_fini(void)
1114 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1115 nf_unregister_hook(&br_nf_ops
[i
]);
1116 #ifdef CONFIG_SYSCTL
1117 unregister_sysctl_table(brnf_sysctl_header
);