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 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
65 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
66 brnf_filter_vlan_tagged)
67 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
68 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
69 brnf_filter_vlan_tagged)
70 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
71 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
72 brnf_filter_vlan_tagged)
74 /* We need these fake structures to make netfilter happy --
75 * lots of places assume that skb->dst != NULL, which isn't
76 * all that unreasonable.
78 * Currently, we fill in the PMTU entry because netfilter
79 * refragmentation needs it, and the rt_flags entry because
80 * ipt_REJECT needs it. Future netfilter modules might
81 * require us to fill additional fields. */
82 static struct net_device __fake_net_device
= {
83 .hard_header_len
= ETH_HLEN
86 static struct rtable __fake_rtable
= {
89 .__refcnt
= ATOMIC_INIT(1),
90 .dev
= &__fake_net_device
,
91 .path
= &__fake_rtable
.u
.dst
,
92 .metrics
= {[RTAX_MTU
- 1] = 1500},
98 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
100 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
102 return port
? port
->br
->dev
: NULL
;
105 /* PF_BRIDGE/PRE_ROUTING *********************************************/
106 /* Undo the changes made for ip6tables PREROUTING and continue the
107 * bridge PRE_ROUTING hook. */
108 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
110 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
112 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
113 skb
->pkt_type
= PACKET_OTHERHOST
;
114 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
116 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
118 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
121 skb
->dev
= nf_bridge
->physindev
;
122 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
123 skb_push(skb
, VLAN_HLEN
);
124 skb
->nh
.raw
-= VLAN_HLEN
;
126 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
127 br_handle_frame_finish
, 1);
132 static void __br_dnat_complain(void)
134 static unsigned long last_complaint
;
136 if (jiffies
- last_complaint
>= 5 * HZ
) {
137 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
138 "forwarding to be enabled\n");
139 last_complaint
= jiffies
;
143 /* This requires some explaining. If DNAT has taken place,
144 * we will need to fix up the destination Ethernet address,
145 * and this is a tricky process.
147 * There are two cases to consider:
148 * 1. The packet was DNAT'ed to a device in the same bridge
149 * port group as it was received on. We can still bridge
151 * 2. The packet was DNAT'ed to a different device, either
152 * a non-bridged device or another bridge port group.
153 * The packet will need to be routed.
155 * The correct way of distinguishing between these two cases is to
156 * call ip_route_input() and to look at skb->dst->dev, which is
157 * changed to the destination device if ip_route_input() succeeds.
159 * Let us first consider the case that ip_route_input() succeeds:
161 * If skb->dst->dev equals the logical bridge device the packet
162 * came in on, we can consider this bridging. We then call
163 * skb->dst->output() which will make the packet enter br_nf_local_out()
164 * not much later. In that function it is assured that the iptables
165 * FORWARD chain is traversed for the packet.
167 * Otherwise, the packet is considered to be routed and we just
168 * change the destination MAC address so that the packet will
169 * later be passed up to the IP stack to be routed.
171 * Let us now consider the case that ip_route_input() fails:
173 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
174 * will fail, while __ip_route_output_key() will return success. The source
175 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
176 * thinks we're handling a locally generated packet and won't care
177 * if IP forwarding is allowed. We send a warning message to the users's
178 * log telling her to put IP forwarding on.
180 * ip_route_input() will also fail if there is no route available.
181 * In that case we just drop the packet.
183 * --Lennert, 20020411
184 * --Bart, 20020416 (updated)
185 * --Bart, 20021007 (updated) */
186 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
188 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
189 skb
->pkt_type
= PACKET_HOST
;
190 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
192 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
194 skb
->dev
= bridge_parent(skb
->dev
);
198 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
199 skb_pull(skb
, VLAN_HLEN
);
200 skb
->nh
.raw
+= VLAN_HLEN
;
202 skb
->dst
->output(skb
);
207 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
209 struct net_device
*dev
= skb
->dev
;
210 struct iphdr
*iph
= skb
->nh
.iph
;
211 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
213 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
214 skb
->pkt_type
= PACKET_OTHERHOST
;
215 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
217 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
219 if (dnat_took_place(skb
)) {
220 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
,
223 struct flowi fl
= { .nl_u
=
224 { .ip4_u
= { .daddr
= iph
->daddr
, .saddr
= 0 ,
225 .tos
= RT_TOS(iph
->tos
)} }, .proto
= 0};
227 if (!ip_route_output_key(&rt
, &fl
)) {
228 /* - Bridged-and-DNAT'ed traffic doesn't
229 * require ip_forwarding.
230 * - Deal with redirected traffic. */
231 if (((struct dst_entry
*)rt
)->dev
== dev
||
232 rt
->rt_type
== RTN_LOCAL
) {
233 skb
->dst
= (struct dst_entry
*)rt
;
236 __br_dnat_complain();
237 dst_release((struct dst_entry
*)rt
);
242 if (skb
->dst
->dev
== dev
) {
244 /* Tell br_nf_local_out this is a
246 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
247 skb
->dev
= nf_bridge
->physindev
;
249 __constant_htons(ETH_P_8021Q
)) {
250 skb_push(skb
, VLAN_HLEN
);
251 skb
->nh
.raw
-= VLAN_HLEN
;
253 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
255 br_nf_pre_routing_finish_bridge
,
259 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
,
261 skb
->pkt_type
= PACKET_HOST
;
264 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
268 skb
->dev
= nf_bridge
->physindev
;
269 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
270 skb_push(skb
, VLAN_HLEN
);
271 skb
->nh
.raw
-= VLAN_HLEN
;
273 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
274 br_handle_frame_finish
, 1);
279 /* Some common code for IPv4/IPv6 */
280 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
282 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
284 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
285 skb
->pkt_type
= PACKET_HOST
;
286 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
289 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
290 nf_bridge
->physindev
= skb
->dev
;
291 skb
->dev
= bridge_parent(skb
->dev
);
296 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
297 static int check_hbh_len(struct sk_buff
*skb
)
299 unsigned char *raw
= (u8
*)(skb
->nh
.ipv6h
+1);
301 int off
= raw
- skb
->nh
.raw
;
302 int len
= (raw
[1]+1)<<3;
304 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
311 int optlen
= skb
->nh
.raw
[off
+1]+2;
313 switch (skb
->nh
.raw
[off
]) {
322 if (skb
->nh
.raw
[off
+1] != 4 || (off
&3) != 2)
324 pkt_len
= ntohl(*(u32
*)(skb
->nh
.raw
+off
+2));
325 if (pkt_len
<= IPV6_MAXPLEN
||
326 skb
->nh
.ipv6h
->payload_len
)
328 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
330 if (pskb_trim_rcsum(skb
,
331 pkt_len
+sizeof(struct ipv6hdr
)))
349 /* Replicate the checks that IPv6 does on packet reception and pass the packet
350 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
351 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
352 struct sk_buff
*skb
, const struct net_device
*in
,
353 const struct net_device
*out
, int (*okfn
)(struct sk_buff
*))
357 struct nf_bridge_info
*nf_bridge
;
359 if (skb
->len
< sizeof(struct ipv6hdr
))
362 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
367 if (hdr
->version
!= 6)
370 pkt_len
= ntohs(hdr
->payload_len
);
372 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
373 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
375 if (pkt_len
+ sizeof(struct ipv6hdr
) < skb
->len
) {
376 if (__pskb_trim(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
378 if (skb
->ip_summed
== CHECKSUM_HW
)
379 skb
->ip_summed
= CHECKSUM_NONE
;
382 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
385 nf_bridge_put(skb
->nf_bridge
);
386 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
388 if (!setup_pre_routing(skb
))
391 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
392 br_nf_pre_routing_finish_ipv6
);
400 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
401 * Replicate the checks that IPv4 does on packet reception.
402 * Set skb->dev to the bridge device (i.e. parent of the
403 * receiving device) to make netfilter happy, the REDIRECT
404 * target in particular. Save the original destination IP
405 * address to be able to detect DNAT afterwards. */
406 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
407 const struct net_device
*in
,
408 const struct net_device
*out
,
409 int (*okfn
)(struct sk_buff
*))
413 struct sk_buff
*skb
= *pskb
;
414 struct nf_bridge_info
*nf_bridge
;
415 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(*pskb
);
417 if (skb
->protocol
== __constant_htons(ETH_P_IPV6
) || IS_VLAN_IPV6
) {
419 if (!brnf_call_ip6tables
)
422 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
425 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
426 u8
*vhdr
= skb
->data
;
427 skb_pull(skb
, VLAN_HLEN
);
428 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
429 skb
->nh
.raw
+= VLAN_HLEN
;
431 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
434 if (!brnf_call_iptables
)
438 if (skb
->protocol
!= __constant_htons(ETH_P_IP
) && !IS_VLAN_IP
)
441 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
444 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
445 u8
*vhdr
= skb
->data
;
446 skb_pull(skb
, VLAN_HLEN
);
447 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
448 skb
->nh
.raw
+= VLAN_HLEN
;
451 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
455 if (iph
->ihl
< 5 || iph
->version
!= 4)
458 if (!pskb_may_pull(skb
, 4*iph
->ihl
))
462 if (ip_fast_csum((__u8
*)iph
, iph
->ihl
) != 0)
465 len
= ntohs(iph
->tot_len
);
466 if (skb
->len
< len
|| len
< 4*iph
->ihl
)
469 if (skb
->len
> len
) {
470 __pskb_trim(skb
, len
);
471 if (skb
->ip_summed
== CHECKSUM_HW
)
472 skb
->ip_summed
= CHECKSUM_NONE
;
475 nf_bridge_put(skb
->nf_bridge
);
476 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
478 if (!setup_pre_routing(skb
))
480 store_orig_dstaddr(skb
);
482 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
483 br_nf_pre_routing_finish
);
488 // IP_INC_STATS_BH(IpInHdrErrors);
494 /* PF_BRIDGE/LOCAL_IN ************************************************/
495 /* The packet is locally destined, which requires a real
496 * dst_entry, so detach the fake one. On the way up, the
497 * packet would pass through PRE_ROUTING again (which already
498 * took place when the packet entered the bridge), but we
499 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
500 * prevent this from happening. */
501 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
502 const struct net_device
*in
, const struct net_device
*out
,
503 int (*okfn
)(struct sk_buff
*))
505 struct sk_buff
*skb
= *pskb
;
507 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
508 dst_release(skb
->dst
);
516 /* PF_BRIDGE/FORWARD *************************************************/
517 static int br_nf_forward_finish(struct sk_buff
*skb
)
519 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
520 struct net_device
*in
;
521 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
523 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
) && !IS_VLAN_ARP
) {
524 in
= nf_bridge
->physindev
;
525 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
526 skb
->pkt_type
= PACKET_OTHERHOST
;
527 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
530 in
= *((struct net_device
**)(skb
->cb
));
532 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
533 skb_push(skb
, VLAN_HLEN
);
534 skb
->nh
.raw
-= VLAN_HLEN
;
536 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
537 skb
->dev
, br_forward_finish
, 1);
541 /* This is the 'purely bridged' case. For IP, we pass the packet to
542 * netfilter with indev and outdev set to the bridge device,
543 * but we are still able to filter on the 'real' indev/outdev
544 * because of the physdev module. For ARP, indev and outdev are the
546 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
547 const struct net_device
*in
, const struct net_device
*out
,
548 int (*okfn
)(struct sk_buff
*))
550 struct sk_buff
*skb
= *pskb
;
551 struct nf_bridge_info
*nf_bridge
;
552 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
553 struct net_device
*parent
;
559 parent
= bridge_parent(out
);
563 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
568 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
569 skb_pull(*pskb
, VLAN_HLEN
);
570 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
573 nf_bridge
= skb
->nf_bridge
;
574 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
575 skb
->pkt_type
= PACKET_HOST
;
576 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
579 /* The physdev module checks on this */
580 nf_bridge
->mask
|= BRNF_BRIDGED
;
581 nf_bridge
->physoutdev
= skb
->dev
;
583 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
584 br_nf_forward_finish
);
589 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
590 const struct net_device
*in
, const struct net_device
*out
,
591 int (*okfn
)(struct sk_buff
*))
593 struct sk_buff
*skb
= *pskb
;
594 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
595 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
598 if (!brnf_call_arptables
)
602 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
)) {
605 skb_pull(*pskb
, VLAN_HLEN
);
606 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
609 if (skb
->nh
.arph
->ar_pln
!= 4) {
611 skb_push(*pskb
, VLAN_HLEN
);
612 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
616 *d
= (struct net_device
*)in
;
617 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
618 (struct net_device
*)out
, br_nf_forward_finish
);
624 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
625 static int br_nf_local_out_finish(struct sk_buff
*skb
)
627 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
628 skb_push(skb
, VLAN_HLEN
);
629 skb
->nh
.raw
-= VLAN_HLEN
;
632 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
633 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
638 /* This function sees both locally originated IP packets and forwarded
639 * IP packets (in both cases the destination device is a bridge
640 * device). It also sees bridged-and-DNAT'ed packets.
641 * To be able to filter on the physical bridge devices (with the physdev
642 * module), we steal packets destined to a bridge device away from the
643 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
644 * when we have determined the real output device. This is done in here.
646 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
647 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
648 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
649 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
651 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
652 * this packet before, and so the packet was locally originated. We fake
653 * the PF_INET/LOCAL_OUT hook.
654 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
655 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
656 * even routed packets that didn't arrive on a bridge interface have their
657 * nf_bridge->physindev set. */
658 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
659 const struct net_device
*in
, const struct net_device
*out
,
660 int (*okfn
)(struct sk_buff
*))
662 struct net_device
*realindev
, *realoutdev
;
663 struct sk_buff
*skb
= *pskb
;
664 struct nf_bridge_info
*nf_bridge
;
665 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
671 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
676 #ifdef CONFIG_NETFILTER_DEBUG
677 /* Sometimes we get packets with NULL ->dst here (for example,
678 * running a dhcp client daemon triggers this). This should now
679 * be fixed, but let's keep the check around. */
680 if (skb
->dst
== NULL
) {
681 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
686 nf_bridge
= skb
->nf_bridge
;
687 nf_bridge
->physoutdev
= skb
->dev
;
688 realindev
= nf_bridge
->physindev
;
690 /* Bridged, take PF_BRIDGE/FORWARD.
691 * (see big note in front of br_nf_pre_routing_finish) */
692 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
693 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
694 skb
->pkt_type
= PACKET_OTHERHOST
;
695 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
697 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
698 skb_push(skb
, VLAN_HLEN
);
699 skb
->nh
.raw
-= VLAN_HLEN
;
702 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
703 skb
->dev
, br_forward_finish
);
706 realoutdev
= bridge_parent(skb
->dev
);
710 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
711 /* iptables should match -o br0.x */
712 if (nf_bridge
->netoutdev
)
713 realoutdev
= nf_bridge
->netoutdev
;
715 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
716 skb_pull(skb
, VLAN_HLEN
);
717 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
719 /* IP forwarded traffic has a physindev, locally
720 * generated traffic hasn't. */
721 if (realindev
!= NULL
) {
722 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
) ) {
723 struct net_device
*parent
= bridge_parent(realindev
);
728 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
729 realoutdev
, br_nf_local_out_finish
,
730 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
732 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
733 realoutdev
, br_nf_local_out_finish
,
734 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
742 /* PF_BRIDGE/POST_ROUTING ********************************************/
743 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
744 const struct net_device
*in
, const struct net_device
*out
,
745 int (*okfn
)(struct sk_buff
*))
747 struct sk_buff
*skb
= *pskb
;
748 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
749 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
750 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
753 #ifdef CONFIG_NETFILTER_DEBUG
754 /* Be very paranoid. This probably won't happen anymore, but let's
755 * keep the check just to be sure... */
756 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
757 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
758 "bad mac.raw pointer.");
769 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
774 #ifdef CONFIG_NETFILTER_DEBUG
775 if (skb
->dst
== NULL
) {
776 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
781 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
782 * about the value of skb->pkt_type. */
783 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
784 skb
->pkt_type
= PACKET_HOST
;
785 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
788 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
789 skb_pull(skb
, VLAN_HLEN
);
790 skb
->nh
.raw
+= VLAN_HLEN
;
793 nf_bridge_save_header(skb
);
795 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
796 if (nf_bridge
->netoutdev
)
797 realoutdev
= nf_bridge
->netoutdev
;
799 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
800 br_dev_queue_push_xmit
);
804 #ifdef CONFIG_NETFILTER_DEBUG
806 if (skb
->dev
!= NULL
) {
807 printk("[%s]", skb
->dev
->name
);
809 printk("[%s]", realoutdev
->name
);
811 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
818 /* IP/SABOTAGE *****************************************************/
819 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
820 * for the second time. */
821 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
822 const struct net_device
*in
, const struct net_device
*out
,
823 int (*okfn
)(struct sk_buff
*))
825 if ((*pskb
)->nf_bridge
&&
826 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
833 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
834 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
835 * decision in the bridge code and have determined nf_bridge->physoutdev. */
836 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
837 const struct net_device
*in
, const struct net_device
*out
,
838 int (*okfn
)(struct sk_buff
*))
840 struct sk_buff
*skb
= *pskb
;
842 if ((out
->hard_start_xmit
== br_dev_xmit
&&
843 okfn
!= br_nf_forward_finish
&&
844 okfn
!= br_nf_local_out_finish
&&
845 okfn
!= br_dev_queue_push_xmit
)
846 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
847 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
848 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
851 struct nf_bridge_info
*nf_bridge
;
853 if (!skb
->nf_bridge
) {
855 /* This code is executed while in the IP(v6) stack,
856 the version should be 4 or 6. We can't use
857 skb->protocol because that isn't set on
858 PF_INET(6)/LOCAL_OUT. */
859 struct iphdr
*ip
= skb
->nh
.iph
;
861 if (ip
->version
== 4 && !brnf_call_iptables
)
863 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
866 if (hook
== NF_IP_POST_ROUTING
)
868 if (!nf_bridge_alloc(skb
))
872 nf_bridge
= skb
->nf_bridge
;
874 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
875 * will need the indev then. For a brouter, the real indev
876 * can be a bridge port, so we make sure br_nf_local_out()
877 * doesn't use the bridge parent of the indev by using
878 * the BRNF_DONT_TAKE_PARENT mask. */
879 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
880 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
881 nf_bridge
->physindev
= (struct net_device
*)in
;
883 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
884 /* the iptables outdev is br0.x, not br0 */
885 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
886 nf_bridge
->netoutdev
= (struct net_device
*)out
;
894 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
895 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
896 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
897 * ip_refrag() can return NF_STOLEN. */
898 static struct nf_hook_ops br_nf_ops
[] = {
899 { .hook
= br_nf_pre_routing
,
900 .owner
= THIS_MODULE
,
902 .hooknum
= NF_BR_PRE_ROUTING
,
903 .priority
= NF_BR_PRI_BRNF
, },
904 { .hook
= br_nf_local_in
,
905 .owner
= THIS_MODULE
,
907 .hooknum
= NF_BR_LOCAL_IN
,
908 .priority
= NF_BR_PRI_BRNF
, },
909 { .hook
= br_nf_forward_ip
,
910 .owner
= THIS_MODULE
,
912 .hooknum
= NF_BR_FORWARD
,
913 .priority
= NF_BR_PRI_BRNF
- 1, },
914 { .hook
= br_nf_forward_arp
,
915 .owner
= THIS_MODULE
,
917 .hooknum
= NF_BR_FORWARD
,
918 .priority
= NF_BR_PRI_BRNF
, },
919 { .hook
= br_nf_local_out
,
920 .owner
= THIS_MODULE
,
922 .hooknum
= NF_BR_LOCAL_OUT
,
923 .priority
= NF_BR_PRI_FIRST
, },
924 { .hook
= br_nf_post_routing
,
925 .owner
= THIS_MODULE
,
927 .hooknum
= NF_BR_POST_ROUTING
,
928 .priority
= NF_BR_PRI_LAST
, },
929 { .hook
= ip_sabotage_in
,
930 .owner
= THIS_MODULE
,
932 .hooknum
= NF_IP_PRE_ROUTING
,
933 .priority
= NF_IP_PRI_FIRST
, },
934 { .hook
= ip_sabotage_in
,
935 .owner
= THIS_MODULE
,
937 .hooknum
= NF_IP6_PRE_ROUTING
,
938 .priority
= NF_IP6_PRI_FIRST
, },
939 { .hook
= ip_sabotage_out
,
940 .owner
= THIS_MODULE
,
942 .hooknum
= NF_IP_FORWARD
,
943 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
944 { .hook
= ip_sabotage_out
,
945 .owner
= THIS_MODULE
,
947 .hooknum
= NF_IP6_FORWARD
,
948 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
949 { .hook
= ip_sabotage_out
,
950 .owner
= THIS_MODULE
,
952 .hooknum
= NF_IP_LOCAL_OUT
,
953 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
954 { .hook
= ip_sabotage_out
,
955 .owner
= THIS_MODULE
,
957 .hooknum
= NF_IP6_LOCAL_OUT
,
958 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
959 { .hook
= ip_sabotage_out
,
960 .owner
= THIS_MODULE
,
962 .hooknum
= NF_IP_POST_ROUTING
,
963 .priority
= NF_IP_PRI_FIRST
, },
964 { .hook
= ip_sabotage_out
,
965 .owner
= THIS_MODULE
,
967 .hooknum
= NF_IP6_POST_ROUTING
,
968 .priority
= NF_IP6_PRI_FIRST
, },
973 int brnf_sysctl_call_tables(ctl_table
*ctl
, int write
, struct file
* filp
,
974 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
978 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
980 if (write
&& *(int *)(ctl
->data
))
981 *(int *)(ctl
->data
) = 1;
985 static ctl_table brnf_table
[] = {
987 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
988 .procname
= "bridge-nf-call-arptables",
989 .data
= &brnf_call_arptables
,
990 .maxlen
= sizeof(int),
992 .proc_handler
= &brnf_sysctl_call_tables
,
995 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
996 .procname
= "bridge-nf-call-iptables",
997 .data
= &brnf_call_iptables
,
998 .maxlen
= sizeof(int),
1000 .proc_handler
= &brnf_sysctl_call_tables
,
1003 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1004 .procname
= "bridge-nf-call-ip6tables",
1005 .data
= &brnf_call_ip6tables
,
1006 .maxlen
= sizeof(int),
1008 .proc_handler
= &brnf_sysctl_call_tables
,
1011 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1012 .procname
= "bridge-nf-filter-vlan-tagged",
1013 .data
= &brnf_filter_vlan_tagged
,
1014 .maxlen
= sizeof(int),
1016 .proc_handler
= &brnf_sysctl_call_tables
,
1021 static ctl_table brnf_bridge_table
[] = {
1023 .ctl_name
= NET_BRIDGE
,
1024 .procname
= "bridge",
1026 .child
= brnf_table
,
1031 static ctl_table brnf_net_table
[] = {
1033 .ctl_name
= CTL_NET
,
1036 .child
= brnf_bridge_table
,
1042 int br_netfilter_init(void)
1046 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1049 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1053 nf_unregister_hook(&br_nf_ops
[i
]);
1058 #ifdef CONFIG_SYSCTL
1059 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1060 if (brnf_sysctl_header
== NULL
) {
1061 printk(KERN_WARNING
"br_netfilter: can't register to sysctl.\n");
1062 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1063 nf_unregister_hook(&br_nf_ops
[i
]);
1068 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1073 void br_netfilter_fini(void)
1077 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1078 nf_unregister_hook(&br_nf_ops
[i
]);
1079 #ifdef CONFIG_SYSCTL
1080 unregister_sysctl_table(brnf_sysctl_header
);