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>
37 #include <linux/inetdevice.h>
41 #include <net/route.h>
43 #include <asm/uaccess.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 __read_mostly
= 1;
57 static int brnf_call_ip6tables __read_mostly
= 1;
58 static int brnf_call_arptables __read_mostly
= 1;
59 static int brnf_filter_vlan_tagged __read_mostly
= 1;
61 #define brnf_filter_vlan_tagged 1
64 int brnf_deferred_hooks
;
65 EXPORT_SYMBOL_GPL(brnf_deferred_hooks
);
67 static __be16
inline vlan_proto(const struct sk_buff
*skb
)
69 return vlan_eth_hdr(skb
)->h_vlan_encapsulated_proto
;
72 #define IS_VLAN_IP(skb) \
73 (skb->protocol == htons(ETH_P_8021Q) && \
74 vlan_proto(skb) == htons(ETH_P_IP) && \
75 brnf_filter_vlan_tagged)
77 #define IS_VLAN_IPV6(skb) \
78 (skb->protocol == htons(ETH_P_8021Q) && \
79 vlan_proto(skb) == htons(ETH_P_IPV6) &&\
80 brnf_filter_vlan_tagged)
82 #define IS_VLAN_ARP(skb) \
83 (skb->protocol == htons(ETH_P_8021Q) && \
84 vlan_proto(skb) == htons(ETH_P_ARP) && \
85 brnf_filter_vlan_tagged)
87 /* We need these fake structures to make netfilter happy --
88 * lots of places assume that skb->dst != NULL, which isn't
89 * all that unreasonable.
91 * Currently, we fill in the PMTU entry because netfilter
92 * refragmentation needs it, and the rt_flags entry because
93 * ipt_REJECT needs it. Future netfilter modules might
94 * require us to fill additional fields. */
95 static struct net_device __fake_net_device
= {
96 .hard_header_len
= ETH_HLEN
99 static struct rtable __fake_rtable
= {
102 .__refcnt
= ATOMIC_INIT(1),
103 .dev
= &__fake_net_device
,
104 .path
= &__fake_rtable
.u
.dst
,
105 .metrics
= {[RTAX_MTU
- 1] = 1500},
112 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
114 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
116 return port
? port
->br
->dev
: NULL
;
119 static inline struct nf_bridge_info
*nf_bridge_alloc(struct sk_buff
*skb
)
121 skb
->nf_bridge
= kzalloc(sizeof(struct nf_bridge_info
), GFP_ATOMIC
);
122 if (likely(skb
->nf_bridge
))
123 atomic_set(&(skb
->nf_bridge
->use
), 1);
125 return skb
->nf_bridge
;
128 static inline void nf_bridge_save_header(struct sk_buff
*skb
)
130 int header_size
= ETH_HLEN
;
132 if (skb
->protocol
== htons(ETH_P_8021Q
))
133 header_size
+= VLAN_HLEN
;
135 memcpy(skb
->nf_bridge
->data
, skb
->data
- header_size
, header_size
);
139 * When forwarding bridge frames, we save a copy of the original
140 * header before processing.
142 int nf_bridge_copy_header(struct sk_buff
*skb
)
145 int header_size
= ETH_HLEN
;
147 if (skb
->protocol
== htons(ETH_P_8021Q
))
148 header_size
+= VLAN_HLEN
;
150 err
= skb_cow(skb
, header_size
);
154 memcpy(skb
->data
- header_size
, skb
->nf_bridge
->data
, header_size
);
156 if (skb
->protocol
== htons(ETH_P_8021Q
))
157 __skb_push(skb
, VLAN_HLEN
);
161 /* PF_BRIDGE/PRE_ROUTING *********************************************/
162 /* Undo the changes made for ip6tables PREROUTING and continue the
163 * bridge PRE_ROUTING hook. */
164 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
166 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
168 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
169 skb
->pkt_type
= PACKET_OTHERHOST
;
170 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
172 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
174 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
177 skb
->dev
= nf_bridge
->physindev
;
178 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
179 skb_push(skb
, VLAN_HLEN
);
180 skb
->nh
.raw
-= VLAN_HLEN
;
182 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
183 br_handle_frame_finish
, 1);
188 static void __br_dnat_complain(void)
190 static unsigned long last_complaint
;
192 if (jiffies
- last_complaint
>= 5 * HZ
) {
193 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
194 "forwarding to be enabled\n");
195 last_complaint
= jiffies
;
199 /* This requires some explaining. If DNAT has taken place,
200 * we will need to fix up the destination Ethernet address,
201 * and this is a tricky process.
203 * There are two cases to consider:
204 * 1. The packet was DNAT'ed to a device in the same bridge
205 * port group as it was received on. We can still bridge
207 * 2. The packet was DNAT'ed to a different device, either
208 * a non-bridged device or another bridge port group.
209 * The packet will need to be routed.
211 * The correct way of distinguishing between these two cases is to
212 * call ip_route_input() and to look at skb->dst->dev, which is
213 * changed to the destination device if ip_route_input() succeeds.
215 * Let us first consider the case that ip_route_input() succeeds:
217 * If skb->dst->dev equals the logical bridge device the packet
218 * came in on, we can consider this bridging. We then call
219 * skb->dst->output() which will make the packet enter br_nf_local_out()
220 * not much later. In that function it is assured that the iptables
221 * FORWARD chain is traversed for the packet.
223 * Otherwise, the packet is considered to be routed and we just
224 * change the destination MAC address so that the packet will
225 * later be passed up to the IP stack to be routed. For a redirected
226 * packet, ip_route_input() will give back the localhost as output device,
227 * which differs from the bridge device.
229 * Let us now consider the case that ip_route_input() fails:
231 * This can be because the destination address is martian, in which case
232 * the packet will be dropped.
233 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
234 * will fail, while __ip_route_output_key() will return success. The source
235 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
236 * thinks we're handling a locally generated packet and won't care
237 * if IP forwarding is allowed. We send a warning message to the users's
238 * log telling her to put IP forwarding on.
240 * ip_route_input() will also fail if there is no route available.
241 * In that case we just drop the packet.
243 * --Lennert, 20020411
244 * --Bart, 20020416 (updated)
245 * --Bart, 20021007 (updated)
246 * --Bart, 20062711 (updated) */
247 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
249 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
250 skb
->pkt_type
= PACKET_HOST
;
251 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
253 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
255 skb
->dev
= bridge_parent(skb
->dev
);
259 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
260 skb_pull(skb
, VLAN_HLEN
);
261 skb
->nh
.raw
+= VLAN_HLEN
;
263 skb
->dst
->output(skb
);
268 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
270 struct net_device
*dev
= skb
->dev
;
271 struct iphdr
*iph
= skb
->nh
.iph
;
272 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
275 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
276 skb
->pkt_type
= PACKET_OTHERHOST
;
277 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
279 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
280 if (dnat_took_place(skb
)) {
281 if ((err
= ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
, dev
))) {
288 .tos
= RT_TOS(iph
->tos
) },
292 struct in_device
*in_dev
= in_dev_get(dev
);
294 /* If err equals -EHOSTUNREACH the error is due to a
295 * martian destination or due to the fact that
296 * forwarding is disabled. For most martian packets,
297 * ip_route_output_key() will fail. It won't fail for 2 types of
298 * martian destinations: loopback destinations and destination
299 * 0.0.0.0. In both cases the packet will be dropped because the
300 * destination is the loopback device and not the bridge. */
301 if (err
!= -EHOSTUNREACH
|| !in_dev
|| IN_DEV_FORWARD(in_dev
))
304 if (!ip_route_output_key(&rt
, &fl
)) {
305 /* - Bridged-and-DNAT'ed traffic doesn't
306 * require ip_forwarding. */
307 if (((struct dst_entry
*)rt
)->dev
== dev
) {
308 skb
->dst
= (struct dst_entry
*)rt
;
311 /* we are sure that forwarding is disabled, so printing
312 * this message is no problem. Note that the packet could
313 * still have a martian destination address, in which case
314 * the packet could be dropped even if forwarding were enabled */
315 __br_dnat_complain();
316 dst_release((struct dst_entry
*)rt
);
322 if (skb
->dst
->dev
== dev
) {
324 /* Tell br_nf_local_out this is a
326 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
327 skb
->dev
= nf_bridge
->physindev
;
329 htons(ETH_P_8021Q
)) {
330 skb_push(skb
, VLAN_HLEN
);
331 skb
->nh
.raw
-= VLAN_HLEN
;
333 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
335 br_nf_pre_routing_finish_bridge
,
339 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
, ETH_ALEN
);
340 skb
->pkt_type
= PACKET_HOST
;
343 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
347 skb
->dev
= nf_bridge
->physindev
;
348 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
349 skb_push(skb
, VLAN_HLEN
);
350 skb
->nh
.raw
-= VLAN_HLEN
;
352 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
353 br_handle_frame_finish
, 1);
358 /* Some common code for IPv4/IPv6 */
359 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
361 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
363 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
364 skb
->pkt_type
= PACKET_HOST
;
365 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
368 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
369 nf_bridge
->physindev
= skb
->dev
;
370 skb
->dev
= bridge_parent(skb
->dev
);
375 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
376 static int check_hbh_len(struct sk_buff
*skb
)
378 unsigned char *raw
= (u8
*) (skb
->nh
.ipv6h
+ 1);
380 int off
= raw
- skb
->nh
.raw
;
381 int len
= (raw
[1] + 1) << 3;
383 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
390 int optlen
= skb
->nh
.raw
[off
+ 1] + 2;
392 switch (skb
->nh
.raw
[off
]) {
401 if (skb
->nh
.raw
[off
+ 1] != 4 || (off
& 3) != 2)
403 pkt_len
= ntohl(*(__be32
*) (skb
->nh
.raw
+ off
+ 2));
404 if (pkt_len
<= IPV6_MAXPLEN
||
405 skb
->nh
.ipv6h
->payload_len
)
407 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
409 if (pskb_trim_rcsum(skb
,
410 pkt_len
+ sizeof(struct ipv6hdr
)))
428 /* Replicate the checks that IPv6 does on packet reception and pass the packet
429 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
430 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
432 const struct net_device
*in
,
433 const struct net_device
*out
,
434 int (*okfn
)(struct sk_buff
*))
439 if (skb
->len
< sizeof(struct ipv6hdr
))
442 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
447 if (hdr
->version
!= 6)
450 pkt_len
= ntohs(hdr
->payload_len
);
452 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
453 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
455 if (pskb_trim_rcsum(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
458 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
461 nf_bridge_put(skb
->nf_bridge
);
462 if (!nf_bridge_alloc(skb
))
464 if (!setup_pre_routing(skb
))
467 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
468 br_nf_pre_routing_finish_ipv6
);
476 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
477 * Replicate the checks that IPv4 does on packet reception.
478 * Set skb->dev to the bridge device (i.e. parent of the
479 * receiving device) to make netfilter happy, the REDIRECT
480 * target in particular. Save the original destination IP
481 * address to be able to detect DNAT afterwards. */
482 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
483 const struct net_device
*in
,
484 const struct net_device
*out
,
485 int (*okfn
)(struct sk_buff
*))
489 struct sk_buff
*skb
= *pskb
;
491 if (skb
->protocol
== htons(ETH_P_IPV6
) || IS_VLAN_IPV6(skb
)) {
493 if (!brnf_call_ip6tables
)
496 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
499 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
500 skb_pull_rcsum(skb
, VLAN_HLEN
);
501 skb
->nh
.raw
+= VLAN_HLEN
;
503 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
506 if (!brnf_call_iptables
)
510 if (skb
->protocol
!= htons(ETH_P_IP
) && !IS_VLAN_IP(skb
))
513 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
516 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
517 skb_pull_rcsum(skb
, VLAN_HLEN
);
518 skb
->nh
.raw
+= VLAN_HLEN
;
521 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
525 if (iph
->ihl
< 5 || iph
->version
!= 4)
528 if (!pskb_may_pull(skb
, 4 * iph
->ihl
))
532 if (ip_fast_csum((__u8
*) iph
, iph
->ihl
) != 0)
535 len
= ntohs(iph
->tot_len
);
536 if (skb
->len
< len
|| len
< 4 * iph
->ihl
)
539 pskb_trim_rcsum(skb
, len
);
541 nf_bridge_put(skb
->nf_bridge
);
542 if (!nf_bridge_alloc(skb
))
544 if (!setup_pre_routing(skb
))
546 store_orig_dstaddr(skb
);
548 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
549 br_nf_pre_routing_finish
);
554 // IP_INC_STATS_BH(IpInHdrErrors);
560 /* PF_BRIDGE/LOCAL_IN ************************************************/
561 /* The packet is locally destined, which requires a real
562 * dst_entry, so detach the fake one. On the way up, the
563 * packet would pass through PRE_ROUTING again (which already
564 * took place when the packet entered the bridge), but we
565 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
566 * prevent this from happening. */
567 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
568 const struct net_device
*in
,
569 const struct net_device
*out
,
570 int (*okfn
)(struct sk_buff
*))
572 struct sk_buff
*skb
= *pskb
;
574 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
575 dst_release(skb
->dst
);
582 /* PF_BRIDGE/FORWARD *************************************************/
583 static int br_nf_forward_finish(struct sk_buff
*skb
)
585 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
586 struct net_device
*in
;
588 if (skb
->protocol
!= htons(ETH_P_ARP
) && !IS_VLAN_ARP(skb
)) {
589 in
= nf_bridge
->physindev
;
590 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
591 skb
->pkt_type
= PACKET_OTHERHOST
;
592 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
595 in
= *((struct net_device
**)(skb
->cb
));
597 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
598 skb_push(skb
, VLAN_HLEN
);
599 skb
->nh
.raw
-= VLAN_HLEN
;
601 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
602 skb
->dev
, br_forward_finish
, 1);
606 /* This is the 'purely bridged' case. For IP, we pass the packet to
607 * netfilter with indev and outdev set to the bridge device,
608 * but we are still able to filter on the 'real' indev/outdev
609 * because of the physdev module. For ARP, indev and outdev are the
611 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
612 const struct net_device
*in
,
613 const struct net_device
*out
,
614 int (*okfn
)(struct sk_buff
*))
616 struct sk_buff
*skb
= *pskb
;
617 struct nf_bridge_info
*nf_bridge
;
618 struct net_device
*parent
;
624 parent
= bridge_parent(out
);
628 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
633 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
634 skb_pull(*pskb
, VLAN_HLEN
);
635 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
638 nf_bridge
= skb
->nf_bridge
;
639 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
640 skb
->pkt_type
= PACKET_HOST
;
641 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
644 /* The physdev module checks on this */
645 nf_bridge
->mask
|= BRNF_BRIDGED
;
646 nf_bridge
->physoutdev
= skb
->dev
;
648 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
649 br_nf_forward_finish
);
654 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
655 const struct net_device
*in
,
656 const struct net_device
*out
,
657 int (*okfn
)(struct sk_buff
*))
659 struct sk_buff
*skb
= *pskb
;
660 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
663 if (!brnf_call_arptables
)
667 if (skb
->protocol
!= htons(ETH_P_ARP
)) {
668 if (!IS_VLAN_ARP(skb
))
670 skb_pull(*pskb
, VLAN_HLEN
);
671 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
674 if (skb
->nh
.arph
->ar_pln
!= 4) {
675 if (IS_VLAN_ARP(skb
)) {
676 skb_push(*pskb
, VLAN_HLEN
);
677 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
681 *d
= (struct net_device
*)in
;
682 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
683 (struct net_device
*)out
, br_nf_forward_finish
);
688 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
689 static int br_nf_local_out_finish(struct sk_buff
*skb
)
691 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
692 skb_push(skb
, VLAN_HLEN
);
693 skb
->nh
.raw
-= VLAN_HLEN
;
696 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
697 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
702 /* This function sees both locally originated IP packets and forwarded
703 * IP packets (in both cases the destination device is a bridge
704 * device). It also sees bridged-and-DNAT'ed packets.
705 * To be able to filter on the physical bridge devices (with the physdev
706 * module), we steal packets destined to a bridge device away from the
707 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
708 * when we have determined the real output device. This is done in here.
710 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
711 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
712 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
713 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
715 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
716 * this packet before, and so the packet was locally originated. We fake
717 * the PF_INET/LOCAL_OUT hook.
718 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
719 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
720 * even routed packets that didn't arrive on a bridge interface have their
721 * nf_bridge->physindev set. */
722 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
723 const struct net_device
*in
,
724 const struct net_device
*out
,
725 int (*okfn
)(struct sk_buff
*))
727 struct net_device
*realindev
, *realoutdev
;
728 struct sk_buff
*skb
= *pskb
;
729 struct nf_bridge_info
*nf_bridge
;
735 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
740 nf_bridge
= skb
->nf_bridge
;
741 nf_bridge
->physoutdev
= skb
->dev
;
742 realindev
= nf_bridge
->physindev
;
744 /* Bridged, take PF_BRIDGE/FORWARD.
745 * (see big note in front of br_nf_pre_routing_finish) */
746 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
747 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
748 skb
->pkt_type
= PACKET_OTHERHOST
;
749 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
751 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
752 skb_push(skb
, VLAN_HLEN
);
753 skb
->nh
.raw
-= VLAN_HLEN
;
756 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
757 skb
->dev
, br_forward_finish
);
760 realoutdev
= bridge_parent(skb
->dev
);
764 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
765 /* iptables should match -o br0.x */
766 if (nf_bridge
->netoutdev
)
767 realoutdev
= nf_bridge
->netoutdev
;
769 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
770 skb_pull(skb
, VLAN_HLEN
);
771 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
773 /* IP forwarded traffic has a physindev, locally
774 * generated traffic hasn't. */
775 if (realindev
!= NULL
) {
776 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
)) {
777 struct net_device
*parent
= bridge_parent(realindev
);
782 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
783 realoutdev
, br_nf_local_out_finish
,
784 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
786 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
787 realoutdev
, br_nf_local_out_finish
,
788 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
795 static int br_nf_dev_queue_xmit(struct sk_buff
*skb
)
797 if (skb
->protocol
== htons(ETH_P_IP
) &&
798 skb
->len
> skb
->dev
->mtu
&&
800 return ip_fragment(skb
, br_dev_queue_push_xmit
);
802 return br_dev_queue_push_xmit(skb
);
805 /* PF_BRIDGE/POST_ROUTING ********************************************/
806 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
807 const struct net_device
*in
,
808 const struct net_device
*out
,
809 int (*okfn
)(struct sk_buff
*))
811 struct sk_buff
*skb
= *pskb
;
812 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
813 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
816 #ifdef CONFIG_NETFILTER_DEBUG
817 /* Be very paranoid. This probably won't happen anymore, but let's
818 * keep the check just to be sure... */
819 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
820 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
821 "bad mac.raw pointer.\n");
832 if (skb
->protocol
== htons(ETH_P_IP
) || IS_VLAN_IP(skb
))
837 #ifdef CONFIG_NETFILTER_DEBUG
838 if (skb
->dst
== NULL
) {
839 printk(KERN_INFO
"br_netfilter post_routing: skb->dst == NULL\n");
844 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
845 * about the value of skb->pkt_type. */
846 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
847 skb
->pkt_type
= PACKET_HOST
;
848 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
851 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
852 skb_pull(skb
, VLAN_HLEN
);
853 skb
->nh
.raw
+= VLAN_HLEN
;
856 nf_bridge_save_header(skb
);
858 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
859 if (nf_bridge
->netoutdev
)
860 realoutdev
= nf_bridge
->netoutdev
;
862 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
863 br_nf_dev_queue_xmit
);
867 #ifdef CONFIG_NETFILTER_DEBUG
869 if (skb
->dev
!= NULL
) {
870 printk("[%s]", skb
->dev
->name
);
872 printk("[%s]", realoutdev
->name
);
874 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
881 /* IP/SABOTAGE *****************************************************/
882 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
883 * for the second time. */
884 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
885 const struct net_device
*in
,
886 const struct net_device
*out
,
887 int (*okfn
)(struct sk_buff
*))
889 if ((*pskb
)->nf_bridge
&&
890 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
897 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
898 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
899 * decision in the bridge code and have determined nf_bridge->physoutdev. */
900 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
901 const struct net_device
*in
,
902 const struct net_device
*out
,
903 int (*okfn
)(struct sk_buff
*))
905 struct sk_buff
*skb
= *pskb
;
907 if ((out
->hard_start_xmit
== br_dev_xmit
&&
908 okfn
!= br_nf_forward_finish
&&
909 okfn
!= br_nf_local_out_finish
&& okfn
!= br_nf_dev_queue_xmit
)
910 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
911 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
912 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
915 struct nf_bridge_info
*nf_bridge
;
917 if (!skb
->nf_bridge
) {
919 /* This code is executed while in the IP(v6) stack,
920 the version should be 4 or 6. We can't use
921 skb->protocol because that isn't set on
922 PF_INET(6)/LOCAL_OUT. */
923 struct iphdr
*ip
= skb
->nh
.iph
;
925 if (ip
->version
== 4 && !brnf_call_iptables
)
927 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
929 else if (!brnf_deferred_hooks
)
932 if (hook
== NF_IP_POST_ROUTING
)
934 if (!nf_bridge_alloc(skb
))
938 nf_bridge
= skb
->nf_bridge
;
940 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
941 * will need the indev then. For a brouter, the real indev
942 * can be a bridge port, so we make sure br_nf_local_out()
943 * doesn't use the bridge parent of the indev by using
944 * the BRNF_DONT_TAKE_PARENT mask. */
945 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
946 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
947 nf_bridge
->physindev
= (struct net_device
*)in
;
949 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
950 /* the iptables outdev is br0.x, not br0 */
951 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
952 nf_bridge
->netoutdev
= (struct net_device
*)out
;
960 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
961 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
962 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
963 * ip_refrag() can return NF_STOLEN. */
964 static struct nf_hook_ops br_nf_ops
[] = {
965 { .hook
= br_nf_pre_routing
,
966 .owner
= THIS_MODULE
,
968 .hooknum
= NF_BR_PRE_ROUTING
,
969 .priority
= NF_BR_PRI_BRNF
, },
970 { .hook
= br_nf_local_in
,
971 .owner
= THIS_MODULE
,
973 .hooknum
= NF_BR_LOCAL_IN
,
974 .priority
= NF_BR_PRI_BRNF
, },
975 { .hook
= br_nf_forward_ip
,
976 .owner
= THIS_MODULE
,
978 .hooknum
= NF_BR_FORWARD
,
979 .priority
= NF_BR_PRI_BRNF
- 1, },
980 { .hook
= br_nf_forward_arp
,
981 .owner
= THIS_MODULE
,
983 .hooknum
= NF_BR_FORWARD
,
984 .priority
= NF_BR_PRI_BRNF
, },
985 { .hook
= br_nf_local_out
,
986 .owner
= THIS_MODULE
,
988 .hooknum
= NF_BR_LOCAL_OUT
,
989 .priority
= NF_BR_PRI_FIRST
, },
990 { .hook
= br_nf_post_routing
,
991 .owner
= THIS_MODULE
,
993 .hooknum
= NF_BR_POST_ROUTING
,
994 .priority
= NF_BR_PRI_LAST
, },
995 { .hook
= ip_sabotage_in
,
996 .owner
= THIS_MODULE
,
998 .hooknum
= NF_IP_PRE_ROUTING
,
999 .priority
= NF_IP_PRI_FIRST
, },
1000 { .hook
= ip_sabotage_in
,
1001 .owner
= THIS_MODULE
,
1003 .hooknum
= NF_IP6_PRE_ROUTING
,
1004 .priority
= NF_IP6_PRI_FIRST
, },
1005 { .hook
= ip_sabotage_out
,
1006 .owner
= THIS_MODULE
,
1008 .hooknum
= NF_IP_FORWARD
,
1009 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
1010 { .hook
= ip_sabotage_out
,
1011 .owner
= THIS_MODULE
,
1013 .hooknum
= NF_IP6_FORWARD
,
1014 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
1015 { .hook
= ip_sabotage_out
,
1016 .owner
= THIS_MODULE
,
1018 .hooknum
= NF_IP_LOCAL_OUT
,
1019 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
1020 { .hook
= ip_sabotage_out
,
1021 .owner
= THIS_MODULE
,
1023 .hooknum
= NF_IP6_LOCAL_OUT
,
1024 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
1025 { .hook
= ip_sabotage_out
,
1026 .owner
= THIS_MODULE
,
1028 .hooknum
= NF_IP_POST_ROUTING
,
1029 .priority
= NF_IP_PRI_FIRST
, },
1030 { .hook
= ip_sabotage_out
,
1031 .owner
= THIS_MODULE
,
1033 .hooknum
= NF_IP6_POST_ROUTING
,
1034 .priority
= NF_IP6_PRI_FIRST
, },
1037 #ifdef CONFIG_SYSCTL
1039 int brnf_sysctl_call_tables(ctl_table
* ctl
, int write
, struct file
*filp
,
1040 void __user
* buffer
, size_t * lenp
, loff_t
* ppos
)
1044 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
1046 if (write
&& *(int *)(ctl
->data
))
1047 *(int *)(ctl
->data
) = 1;
1051 static ctl_table brnf_table
[] = {
1053 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
1054 .procname
= "bridge-nf-call-arptables",
1055 .data
= &brnf_call_arptables
,
1056 .maxlen
= sizeof(int),
1058 .proc_handler
= &brnf_sysctl_call_tables
,
1061 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
1062 .procname
= "bridge-nf-call-iptables",
1063 .data
= &brnf_call_iptables
,
1064 .maxlen
= sizeof(int),
1066 .proc_handler
= &brnf_sysctl_call_tables
,
1069 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1070 .procname
= "bridge-nf-call-ip6tables",
1071 .data
= &brnf_call_ip6tables
,
1072 .maxlen
= sizeof(int),
1074 .proc_handler
= &brnf_sysctl_call_tables
,
1077 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1078 .procname
= "bridge-nf-filter-vlan-tagged",
1079 .data
= &brnf_filter_vlan_tagged
,
1080 .maxlen
= sizeof(int),
1082 .proc_handler
= &brnf_sysctl_call_tables
,
1087 static ctl_table brnf_bridge_table
[] = {
1089 .ctl_name
= NET_BRIDGE
,
1090 .procname
= "bridge",
1092 .child
= brnf_table
,
1097 static ctl_table brnf_net_table
[] = {
1099 .ctl_name
= CTL_NET
,
1102 .child
= brnf_bridge_table
,
1108 int br_netfilter_init(void)
1112 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1115 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1119 nf_unregister_hook(&br_nf_ops
[i
]);
1124 #ifdef CONFIG_SYSCTL
1125 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1126 if (brnf_sysctl_header
== NULL
) {
1128 "br_netfilter: can't register to sysctl.\n");
1129 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1130 nf_unregister_hook(&br_nf_ops
[i
]);
1135 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1140 void br_netfilter_fini(void)
1144 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1145 nf_unregister_hook(&br_nf_ops
[i
]);
1146 #ifdef CONFIG_SYSCTL
1147 unregister_sysctl_table(brnf_sysctl_header
);