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_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/netfilter_bridge.h>
32 #include <linux/netfilter_ipv4.h>
33 #include <linux/netfilter_ipv6.h>
34 #include <linux/netfilter_arp.h>
35 #include <linux/in_route.h>
38 #include <asm/uaccess.h>
39 #include <asm/checksum.h>
40 #include "br_private.h"
42 #include <linux/sysctl.h>
45 #define skb_origaddr(skb) (((struct bridge_skb_cb *) \
46 (skb->nf_bridge->data))->daddr.ipv4)
47 #define store_orig_dstaddr(skb) (skb_origaddr(skb) = (skb)->nh.iph->daddr)
48 #define dnat_took_place(skb) (skb_origaddr(skb) != (skb)->nh.iph->daddr)
50 #define has_bridge_parent(device) ((device)->br_port != NULL)
51 #define bridge_parent(device) ((device)->br_port->br->dev)
54 static struct ctl_table_header
*brnf_sysctl_header
;
55 static int brnf_call_iptables
= 1;
56 static int brnf_call_ip6tables
= 1;
57 static int brnf_call_arptables
= 1;
58 static int brnf_filter_vlan_tagged
= 1;
60 #define brnf_filter_vlan_tagged 1
63 #define IS_VLAN_IP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
64 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IP) && \
65 brnf_filter_vlan_tagged)
66 #define IS_VLAN_IPV6 (skb->protocol == __constant_htons(ETH_P_8021Q) && \
67 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_IPV6) && \
68 brnf_filter_vlan_tagged)
69 #define IS_VLAN_ARP (skb->protocol == __constant_htons(ETH_P_8021Q) && \
70 hdr->h_vlan_encapsulated_proto == __constant_htons(ETH_P_ARP) && \
71 brnf_filter_vlan_tagged)
73 /* We need these fake structures to make netfilter happy --
74 * lots of places assume that skb->dst != NULL, which isn't
75 * all that unreasonable.
77 * Currently, we fill in the PMTU entry because netfilter
78 * refragmentation needs it, and the rt_flags entry because
79 * ipt_REJECT needs it. Future netfilter modules might
80 * require us to fill additional fields. */
81 static struct net_device __fake_net_device
= {
82 .hard_header_len
= ETH_HLEN
85 static struct rtable __fake_rtable
= {
88 .__refcnt
= ATOMIC_INIT(1),
89 .dev
= &__fake_net_device
,
90 .path
= &__fake_rtable
.u
.dst
,
91 .metrics
= {[RTAX_MTU
- 1] = 1500},
98 /* PF_BRIDGE/PRE_ROUTING *********************************************/
99 /* Undo the changes made for ip6tables PREROUTING and continue the
100 * bridge PRE_ROUTING hook. */
101 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
103 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
105 #ifdef CONFIG_NETFILTER_DEBUG
106 skb
->nf_debug
^= (1 << NF_BR_PRE_ROUTING
);
109 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
110 skb
->pkt_type
= PACKET_OTHERHOST
;
111 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
113 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
115 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
118 skb
->dev
= nf_bridge
->physindev
;
119 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
120 skb_push(skb
, VLAN_HLEN
);
121 skb
->nh
.raw
-= VLAN_HLEN
;
123 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
124 br_handle_frame_finish
, 1);
129 static void __br_dnat_complain(void)
131 static unsigned long last_complaint
;
133 if (jiffies
- last_complaint
>= 5 * HZ
) {
134 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
135 "forwarding to be enabled\n");
136 last_complaint
= jiffies
;
140 /* This requires some explaining. If DNAT has taken place,
141 * we will need to fix up the destination Ethernet address,
142 * and this is a tricky process.
144 * There are two cases to consider:
145 * 1. The packet was DNAT'ed to a device in the same bridge
146 * port group as it was received on. We can still bridge
148 * 2. The packet was DNAT'ed to a different device, either
149 * a non-bridged device or another bridge port group.
150 * The packet will need to be routed.
152 * The correct way of distinguishing between these two cases is to
153 * call ip_route_input() and to look at skb->dst->dev, which is
154 * changed to the destination device if ip_route_input() succeeds.
156 * Let us first consider the case that ip_route_input() succeeds:
158 * If skb->dst->dev equals the logical bridge device the packet
159 * came in on, we can consider this bridging. We then call
160 * skb->dst->output() which will make the packet enter br_nf_local_out()
161 * not much later. In that function it is assured that the iptables
162 * FORWARD chain is traversed for the packet.
164 * Otherwise, the packet is considered to be routed and we just
165 * change the destination MAC address so that the packet will
166 * later be passed up to the IP stack to be routed.
168 * Let us now consider the case that ip_route_input() fails:
170 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
171 * will fail, while __ip_route_output_key() will return success. The source
172 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
173 * thinks we're handling a locally generated packet and won't care
174 * if IP forwarding is allowed. We send a warning message to the users's
175 * log telling her to put IP forwarding on.
177 * ip_route_input() will also fail if there is no route available.
178 * In that case we just drop the packet.
180 * --Lennert, 20020411
181 * --Bart, 20020416 (updated)
182 * --Bart, 20021007 (updated) */
183 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
185 #ifdef CONFIG_NETFILTER_DEBUG
186 skb
->nf_debug
|= (1 << NF_BR_PRE_ROUTING
) | (1 << NF_BR_FORWARD
);
189 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
190 skb
->pkt_type
= PACKET_HOST
;
191 skb
->nf_bridge
->mask
|= BRNF_PKT_TYPE
;
193 skb
->nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
195 skb
->dev
= bridge_parent(skb
->dev
);
196 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
197 skb_pull(skb
, VLAN_HLEN
);
198 skb
->nh
.raw
+= VLAN_HLEN
;
200 skb
->dst
->output(skb
);
204 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
206 struct net_device
*dev
= skb
->dev
;
207 struct iphdr
*iph
= skb
->nh
.iph
;
208 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
210 #ifdef CONFIG_NETFILTER_DEBUG
211 skb
->nf_debug
^= (1 << NF_BR_PRE_ROUTING
);
214 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
215 skb
->pkt_type
= PACKET_OTHERHOST
;
216 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
218 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
220 if (dnat_took_place(skb
)) {
221 if (ip_route_input(skb
, iph
->daddr
, iph
->saddr
, iph
->tos
,
224 struct flowi fl
= { .nl_u
=
225 { .ip4_u
= { .daddr
= iph
->daddr
, .saddr
= 0 ,
226 .tos
= RT_TOS(iph
->tos
)} }, .proto
= 0};
228 if (!ip_route_output_key(&rt
, &fl
)) {
229 /* Bridged-and-DNAT'ed traffic doesn't
230 * require ip_forwarding. */
231 if (((struct dst_entry
*)rt
)->dev
== dev
) {
232 skb
->dst
= (struct dst_entry
*)rt
;
235 __br_dnat_complain();
236 dst_release((struct dst_entry
*)rt
);
241 if (skb
->dst
->dev
== dev
) {
243 /* Tell br_nf_local_out this is a
245 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
246 skb
->dev
= nf_bridge
->physindev
;
248 __constant_htons(ETH_P_8021Q
)) {
249 skb_push(skb
, VLAN_HLEN
);
250 skb
->nh
.raw
-= VLAN_HLEN
;
252 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
254 br_nf_pre_routing_finish_bridge
,
258 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
,
260 skb
->pkt_type
= PACKET_HOST
;
263 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
267 skb
->dev
= nf_bridge
->physindev
;
268 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
269 skb_push(skb
, VLAN_HLEN
);
270 skb
->nh
.raw
-= VLAN_HLEN
;
272 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
273 br_handle_frame_finish
, 1);
278 /* Some common code for IPv4/IPv6 */
279 static void setup_pre_routing(struct sk_buff
*skb
)
281 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
283 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
284 skb
->pkt_type
= PACKET_HOST
;
285 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
288 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
289 nf_bridge
->physindev
= skb
->dev
;
290 skb
->dev
= bridge_parent(skb
->dev
);
293 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
294 static int check_hbh_len(struct sk_buff
*skb
)
296 unsigned char *raw
= (u8
*)(skb
->nh
.ipv6h
+1);
298 int off
= raw
- skb
->nh
.raw
;
299 int len
= (raw
[1]+1)<<3;
301 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
308 int optlen
= raw
[off
+1]+2;
310 switch (skb
->nh
.raw
[off
]) {
319 if (skb
->nh
.raw
[off
+1] != 4 || (off
&3) != 2)
322 pkt_len
= ntohl(*(u32
*)(skb
->nh
.raw
+off
+2));
324 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
326 if (pkt_len
+ sizeof(struct ipv6hdr
) < skb
->len
) {
328 pkt_len
+ sizeof(struct ipv6hdr
)))
330 if (skb
->ip_summed
== CHECKSUM_HW
)
331 skb
->ip_summed
= CHECKSUM_NONE
;
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 #ifdef CONFIG_NETFILTER_DEBUG
386 skb
->nf_debug
^= (1 << NF_IP6_PRE_ROUTING
);
388 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
390 setup_pre_routing(skb
);
392 NF_HOOK(PF_INET6
, NF_IP6_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
393 br_nf_pre_routing_finish_ipv6
);
401 /* Direct IPv6 traffic to br_nf_pre_routing_ipv6.
402 * Replicate the checks that IPv4 does on packet reception.
403 * Set skb->dev to the bridge device (i.e. parent of the
404 * receiving device) to make netfilter happy, the REDIRECT
405 * target in particular. Save the original destination IP
406 * address to be able to detect DNAT afterwards. */
407 static unsigned int br_nf_pre_routing(unsigned int hook
, struct sk_buff
**pskb
,
408 const struct net_device
*in
, 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 skb_pull(skb
, VLAN_HLEN
);
427 (skb
)->nh
.raw
+= VLAN_HLEN
;
429 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
432 if (!brnf_call_iptables
)
436 if (skb
->protocol
!= __constant_htons(ETH_P_IP
) && !IS_VLAN_IP
)
439 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
442 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
443 skb_pull(skb
, VLAN_HLEN
);
444 (skb
)->nh
.raw
+= VLAN_HLEN
;
447 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
451 if (iph
->ihl
< 5 || iph
->version
!= 4)
454 if (!pskb_may_pull(skb
, 4*iph
->ihl
))
458 if (ip_fast_csum((__u8
*)iph
, iph
->ihl
) != 0)
461 len
= ntohs(iph
->tot_len
);
462 if (skb
->len
< len
|| len
< 4*iph
->ihl
)
465 if (skb
->len
> len
) {
466 __pskb_trim(skb
, len
);
467 if (skb
->ip_summed
== CHECKSUM_HW
)
468 skb
->ip_summed
= CHECKSUM_NONE
;
471 #ifdef CONFIG_NETFILTER_DEBUG
472 skb
->nf_debug
^= (1 << NF_IP_PRE_ROUTING
);
474 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
476 setup_pre_routing(skb
);
477 store_orig_dstaddr(skb
);
479 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
480 br_nf_pre_routing_finish
);
485 // IP_INC_STATS_BH(IpInHdrErrors);
491 /* PF_BRIDGE/LOCAL_IN ************************************************/
492 /* The packet is locally destined, which requires a real
493 * dst_entry, so detach the fake one. On the way up, the
494 * packet would pass through PRE_ROUTING again (which already
495 * took place when the packet entered the bridge), but we
496 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
497 * prevent this from happening. */
498 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
499 const struct net_device
*in
, const struct net_device
*out
,
500 int (*okfn
)(struct sk_buff
*))
502 struct sk_buff
*skb
= *pskb
;
504 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
505 dst_release(skb
->dst
);
513 /* PF_BRIDGE/FORWARD *************************************************/
514 static int br_nf_forward_finish(struct sk_buff
*skb
)
516 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
517 struct net_device
*in
;
518 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
520 #ifdef CONFIG_NETFILTER_DEBUG
521 skb
->nf_debug
^= (1 << NF_BR_FORWARD
);
524 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
) && !IS_VLAN_ARP
) {
525 in
= nf_bridge
->physindev
;
526 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
527 skb
->pkt_type
= PACKET_OTHERHOST
;
528 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
531 in
= *((struct net_device
**)(skb
->cb
));
533 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
534 skb_push(skb
, VLAN_HLEN
);
535 skb
->nh
.raw
-= VLAN_HLEN
;
537 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_FORWARD
, skb
, in
,
538 skb
->dev
, br_forward_finish
, 1);
542 /* This is the 'purely bridged' case. For IP, we pass the packet to
543 * netfilter with indev and outdev set to the bridge device,
544 * but we are still able to filter on the 'real' indev/outdev
545 * because of the physdev module. For ARP, indev and outdev are the
547 static unsigned int br_nf_forward_ip(unsigned int hook
, struct sk_buff
**pskb
,
548 const struct net_device
*in
, const struct net_device
*out
,
549 int (*okfn
)(struct sk_buff
*))
551 struct sk_buff
*skb
= *pskb
;
552 struct nf_bridge_info
*nf_bridge
;
553 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
559 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
564 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
565 skb_pull(*pskb
, VLAN_HLEN
);
566 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
569 #ifdef CONFIG_NETFILTER_DEBUG
570 skb
->nf_debug
^= (1 << NF_BR_FORWARD
);
572 nf_bridge
= skb
->nf_bridge
;
573 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
574 skb
->pkt_type
= PACKET_HOST
;
575 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
578 /* The physdev module checks on this */
579 nf_bridge
->mask
|= BRNF_BRIDGED
;
580 nf_bridge
->physoutdev
= skb
->dev
;
582 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
),
583 bridge_parent(out
), br_nf_forward_finish
);
588 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
589 const struct net_device
*in
, const struct net_device
*out
,
590 int (*okfn
)(struct sk_buff
*))
592 struct sk_buff
*skb
= *pskb
;
593 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
594 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
597 if (!brnf_call_arptables
)
601 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
)) {
604 skb_pull(*pskb
, VLAN_HLEN
);
605 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
608 #ifdef CONFIG_NETFILTER_DEBUG
609 skb
->nf_debug
^= (1 << NF_BR_FORWARD
);
612 if (skb
->nh
.arph
->ar_pln
!= 4) {
614 skb_push(*pskb
, VLAN_HLEN
);
615 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
619 *d
= (struct net_device
*)in
;
620 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
621 (struct net_device
*)out
, br_nf_forward_finish
);
627 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
628 static int br_nf_local_out_finish(struct sk_buff
*skb
)
630 #ifdef CONFIG_NETFILTER_DEBUG
631 skb
->nf_debug
&= ~(1 << NF_BR_LOCAL_OUT
);
633 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
634 skb_push(skb
, VLAN_HLEN
);
635 skb
->nh
.raw
-= VLAN_HLEN
;
638 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
639 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
644 /* This function sees both locally originated IP packets and forwarded
645 * IP packets (in both cases the destination device is a bridge
646 * device). It also sees bridged-and-DNAT'ed packets.
647 * To be able to filter on the physical bridge devices (with the physdev
648 * module), we steal packets destined to a bridge device away from the
649 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
650 * when we have determined the real output device. This is done in here.
652 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
653 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
654 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
655 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
657 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
658 * this packet before, and so the packet was locally originated. We fake
659 * the PF_INET/LOCAL_OUT hook.
660 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
661 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
662 * even routed packets that didn't arrive on a bridge interface have their
663 * nf_bridge->physindev set. */
664 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
665 const struct net_device
*in
, const struct net_device
*out
,
666 int (*okfn
)(struct sk_buff
*))
668 struct net_device
*realindev
, *realoutdev
;
669 struct sk_buff
*skb
= *pskb
;
670 struct nf_bridge_info
*nf_bridge
;
671 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
677 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
682 #ifdef CONFIG_NETFILTER_DEBUG
683 /* Sometimes we get packets with NULL ->dst here (for example,
684 * running a dhcp client daemon triggers this). This should now
685 * be fixed, but let's keep the check around. */
686 if (skb
->dst
== NULL
) {
687 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
692 nf_bridge
= skb
->nf_bridge
;
693 nf_bridge
->physoutdev
= skb
->dev
;
694 realindev
= nf_bridge
->physindev
;
696 /* Bridged, take PF_BRIDGE/FORWARD.
697 * (see big note in front of br_nf_pre_routing_finish) */
698 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
699 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
700 skb
->pkt_type
= PACKET_OTHERHOST
;
701 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
703 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
704 skb_push(skb
, VLAN_HLEN
);
705 skb
->nh
.raw
-= VLAN_HLEN
;
708 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
709 skb
->dev
, br_forward_finish
);
712 realoutdev
= bridge_parent(skb
->dev
);
714 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
715 /* iptables should match -o br0.x */
716 if (nf_bridge
->netoutdev
)
717 realoutdev
= nf_bridge
->netoutdev
;
719 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
720 skb_pull(skb
, VLAN_HLEN
);
721 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
723 /* IP forwarded traffic has a physindev, locally
724 * generated traffic hasn't. */
725 if (realindev
!= NULL
) {
726 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
) &&
727 has_bridge_parent(realindev
))
728 realindev
= bridge_parent(realindev
);
730 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
731 realoutdev
, br_nf_local_out_finish
,
732 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
734 #ifdef CONFIG_NETFILTER_DEBUG
735 skb
->nf_debug
^= (1 << NF_IP_LOCAL_OUT
);
738 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
739 realoutdev
, br_nf_local_out_finish
,
740 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
748 /* PF_BRIDGE/POST_ROUTING ********************************************/
749 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
750 const struct net_device
*in
, const struct net_device
*out
,
751 int (*okfn
)(struct sk_buff
*))
753 struct sk_buff
*skb
= *pskb
;
754 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
755 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
756 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
759 #ifdef CONFIG_NETFILTER_DEBUG
760 /* Be very paranoid. This probably won't happen anymore, but let's
761 * keep the check just to be sure... */
762 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
763 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
764 "bad mac.raw pointer.");
772 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
777 #ifdef CONFIG_NETFILTER_DEBUG
778 if (skb
->dst
== NULL
) {
779 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
783 skb
->nf_debug
^= (1 << NF_IP_POST_ROUTING
);
786 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
787 * about the value of skb->pkt_type. */
788 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
789 skb
->pkt_type
= PACKET_HOST
;
790 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
793 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
794 skb_pull(skb
, VLAN_HLEN
);
795 skb
->nh
.raw
+= VLAN_HLEN
;
798 nf_bridge_save_header(skb
);
800 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
801 if (nf_bridge
->netoutdev
)
802 realoutdev
= nf_bridge
->netoutdev
;
804 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
805 br_dev_queue_push_xmit
);
809 #ifdef CONFIG_NETFILTER_DEBUG
811 if (skb
->dev
!= NULL
) {
812 printk("[%s]", skb
->dev
->name
);
813 if (has_bridge_parent(skb
->dev
))
814 printk("[%s]", bridge_parent(skb
->dev
)->name
);
816 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
823 /* IP/SABOTAGE *****************************************************/
824 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
825 * for the second time. */
826 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
827 const struct net_device
*in
, const struct net_device
*out
,
828 int (*okfn
)(struct sk_buff
*))
830 if ((*pskb
)->nf_bridge
&&
831 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
838 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
839 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
840 * decision in the bridge code and have determined nf_bridge->physoutdev. */
841 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
842 const struct net_device
*in
, const struct net_device
*out
,
843 int (*okfn
)(struct sk_buff
*))
845 struct sk_buff
*skb
= *pskb
;
847 if ((out
->hard_start_xmit
== br_dev_xmit
&&
848 okfn
!= br_nf_forward_finish
&&
849 okfn
!= br_nf_local_out_finish
&&
850 okfn
!= br_dev_queue_push_xmit
)
851 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
852 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
853 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
856 struct nf_bridge_info
*nf_bridge
;
858 if (!skb
->nf_bridge
) {
860 /* This code is executed while in the IP(v6) stack,
861 the version should be 4 or 6. We can't use
862 skb->protocol because that isn't set on
863 PF_INET(6)/LOCAL_OUT. */
864 struct iphdr
*ip
= skb
->nh
.iph
;
866 if (ip
->version
== 4 && !brnf_call_iptables
)
868 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
871 if (hook
== NF_IP_POST_ROUTING
)
873 if (!nf_bridge_alloc(skb
))
877 nf_bridge
= skb
->nf_bridge
;
879 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
880 * will need the indev then. For a brouter, the real indev
881 * can be a bridge port, so we make sure br_nf_local_out()
882 * doesn't use the bridge parent of the indev by using
883 * the BRNF_DONT_TAKE_PARENT mask. */
884 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
885 nf_bridge
->mask
&= BRNF_DONT_TAKE_PARENT
;
886 nf_bridge
->physindev
= (struct net_device
*)in
;
888 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
889 /* the iptables outdev is br0.x, not br0 */
890 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
891 nf_bridge
->netoutdev
= (struct net_device
*)out
;
899 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
900 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
901 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
902 * ip_refrag() can return NF_STOLEN. */
903 static struct nf_hook_ops br_nf_ops
[] = {
904 { .hook
= br_nf_pre_routing
,
905 .owner
= THIS_MODULE
,
907 .hooknum
= NF_BR_PRE_ROUTING
,
908 .priority
= NF_BR_PRI_BRNF
, },
909 { .hook
= br_nf_local_in
,
910 .owner
= THIS_MODULE
,
912 .hooknum
= NF_BR_LOCAL_IN
,
913 .priority
= NF_BR_PRI_BRNF
, },
914 { .hook
= br_nf_forward_ip
,
915 .owner
= THIS_MODULE
,
917 .hooknum
= NF_BR_FORWARD
,
918 .priority
= NF_BR_PRI_BRNF
- 1, },
919 { .hook
= br_nf_forward_arp
,
920 .owner
= THIS_MODULE
,
922 .hooknum
= NF_BR_FORWARD
,
923 .priority
= NF_BR_PRI_BRNF
, },
924 { .hook
= br_nf_local_out
,
925 .owner
= THIS_MODULE
,
927 .hooknum
= NF_BR_LOCAL_OUT
,
928 .priority
= NF_BR_PRI_FIRST
, },
929 { .hook
= br_nf_post_routing
,
930 .owner
= THIS_MODULE
,
932 .hooknum
= NF_BR_POST_ROUTING
,
933 .priority
= NF_BR_PRI_LAST
, },
934 { .hook
= ip_sabotage_in
,
935 .owner
= THIS_MODULE
,
937 .hooknum
= NF_IP_PRE_ROUTING
,
938 .priority
= NF_IP_PRI_FIRST
, },
939 { .hook
= ip_sabotage_in
,
940 .owner
= THIS_MODULE
,
942 .hooknum
= NF_IP6_PRE_ROUTING
,
943 .priority
= NF_IP6_PRI_FIRST
, },
944 { .hook
= ip_sabotage_out
,
945 .owner
= THIS_MODULE
,
947 .hooknum
= NF_IP_FORWARD
,
948 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
949 { .hook
= ip_sabotage_out
,
950 .owner
= THIS_MODULE
,
952 .hooknum
= NF_IP6_FORWARD
,
953 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
954 { .hook
= ip_sabotage_out
,
955 .owner
= THIS_MODULE
,
957 .hooknum
= NF_IP_LOCAL_OUT
,
958 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
959 { .hook
= ip_sabotage_out
,
960 .owner
= THIS_MODULE
,
962 .hooknum
= NF_IP6_LOCAL_OUT
,
963 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
964 { .hook
= ip_sabotage_out
,
965 .owner
= THIS_MODULE
,
967 .hooknum
= NF_IP_POST_ROUTING
,
968 .priority
= NF_IP_PRI_FIRST
, },
969 { .hook
= ip_sabotage_out
,
970 .owner
= THIS_MODULE
,
972 .hooknum
= NF_IP6_POST_ROUTING
,
973 .priority
= NF_IP6_PRI_FIRST
, },
978 int brnf_sysctl_call_tables(ctl_table
*ctl
, int write
, struct file
* filp
,
979 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
983 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
985 if (write
&& *(int *)(ctl
->data
))
986 *(int *)(ctl
->data
) = 1;
990 static ctl_table brnf_table
[] = {
992 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
993 .procname
= "bridge-nf-call-arptables",
994 .data
= &brnf_call_arptables
,
995 .maxlen
= sizeof(int),
997 .proc_handler
= &brnf_sysctl_call_tables
,
1000 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
1001 .procname
= "bridge-nf-call-iptables",
1002 .data
= &brnf_call_iptables
,
1003 .maxlen
= sizeof(int),
1005 .proc_handler
= &brnf_sysctl_call_tables
,
1008 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1009 .procname
= "bridge-nf-call-ip6tables",
1010 .data
= &brnf_call_ip6tables
,
1011 .maxlen
= sizeof(int),
1013 .proc_handler
= &brnf_sysctl_call_tables
,
1016 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1017 .procname
= "bridge-nf-filter-vlan-tagged",
1018 .data
= &brnf_filter_vlan_tagged
,
1019 .maxlen
= sizeof(int),
1021 .proc_handler
= &brnf_sysctl_call_tables
,
1026 static ctl_table brnf_bridge_table
[] = {
1028 .ctl_name
= NET_BRIDGE
,
1029 .procname
= "bridge",
1031 .child
= brnf_table
,
1036 static ctl_table brnf_net_table
[] = {
1038 .ctl_name
= CTL_NET
,
1041 .child
= brnf_bridge_table
,
1047 int br_netfilter_init(void)
1051 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1054 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1058 nf_unregister_hook(&br_nf_ops
[i
]);
1063 #ifdef CONFIG_SYSCTL
1064 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1065 if (brnf_sysctl_header
== NULL
) {
1066 printk(KERN_WARNING
"br_netfilter: can't register to sysctl.\n");
1067 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1068 nf_unregister_hook(&br_nf_ops
[i
]);
1073 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1078 void br_netfilter_fini(void)
1082 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1083 nf_unregister_hook(&br_nf_ops
[i
]);
1084 #ifdef CONFIG_SYSCTL
1085 unregister_sysctl_table(brnf_sysctl_header
);