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},
99 static inline struct net_device
*bridge_parent(const struct net_device
*dev
)
101 struct net_bridge_port
*port
= rcu_dereference(dev
->br_port
);
103 return port
? port
->br
->dev
: NULL
;
106 /* PF_BRIDGE/PRE_ROUTING *********************************************/
107 /* Undo the changes made for ip6tables PREROUTING and continue the
108 * bridge PRE_ROUTING hook. */
109 static int br_nf_pre_routing_finish_ipv6(struct sk_buff
*skb
)
111 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
113 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
114 skb
->pkt_type
= PACKET_OTHERHOST
;
115 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
117 nf_bridge
->mask
^= BRNF_NF_BRIDGE_PREROUTING
;
119 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
122 skb
->dev
= nf_bridge
->physindev
;
123 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
124 skb_push(skb
, VLAN_HLEN
);
125 skb
->nh
.raw
-= VLAN_HLEN
;
127 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
128 br_handle_frame_finish
, 1);
133 static void __br_dnat_complain(void)
135 static unsigned long last_complaint
;
137 if (jiffies
- last_complaint
>= 5 * HZ
) {
138 printk(KERN_WARNING
"Performing cross-bridge DNAT requires IP "
139 "forwarding to be enabled\n");
140 last_complaint
= jiffies
;
144 /* This requires some explaining. If DNAT has taken place,
145 * we will need to fix up the destination Ethernet address,
146 * and this is a tricky process.
148 * There are two cases to consider:
149 * 1. The packet was DNAT'ed to a device in the same bridge
150 * port group as it was received on. We can still bridge
152 * 2. The packet was DNAT'ed to a different device, either
153 * a non-bridged device or another bridge port group.
154 * The packet will need to be routed.
156 * The correct way of distinguishing between these two cases is to
157 * call ip_route_input() and to look at skb->dst->dev, which is
158 * changed to the destination device if ip_route_input() succeeds.
160 * Let us first consider the case that ip_route_input() succeeds:
162 * If skb->dst->dev equals the logical bridge device the packet
163 * came in on, we can consider this bridging. We then call
164 * skb->dst->output() which will make the packet enter br_nf_local_out()
165 * not much later. In that function it is assured that the iptables
166 * FORWARD chain is traversed for the packet.
168 * Otherwise, the packet is considered to be routed and we just
169 * change the destination MAC address so that the packet will
170 * later be passed up to the IP stack to be routed.
172 * Let us now consider the case that ip_route_input() fails:
174 * After a "echo '0' > /proc/sys/net/ipv4/ip_forward" ip_route_input()
175 * will fail, while __ip_route_output_key() will return success. The source
176 * address for __ip_route_output_key() is set to zero, so __ip_route_output_key
177 * thinks we're handling a locally generated packet and won't care
178 * if IP forwarding is allowed. We send a warning message to the users's
179 * log telling her to put IP forwarding on.
181 * ip_route_input() will also fail if there is no route available.
182 * In that case we just drop the packet.
184 * --Lennert, 20020411
185 * --Bart, 20020416 (updated)
186 * --Bart, 20021007 (updated) */
187 static int br_nf_pre_routing_finish_bridge(struct sk_buff
*skb
)
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
);
199 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
200 skb_pull(skb
, VLAN_HLEN
);
201 skb
->nh
.raw
+= VLAN_HLEN
;
203 skb
->dst
->output(skb
);
208 static int br_nf_pre_routing_finish(struct sk_buff
*skb
)
210 struct net_device
*dev
= skb
->dev
;
211 struct iphdr
*iph
= skb
->nh
.iph
;
212 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
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 * - Deal with redirected traffic. */
232 if (((struct dst_entry
*)rt
)->dev
== dev
||
233 rt
->rt_type
== RTN_LOCAL
) {
234 skb
->dst
= (struct dst_entry
*)rt
;
237 __br_dnat_complain();
238 dst_release((struct dst_entry
*)rt
);
243 if (skb
->dst
->dev
== dev
) {
245 /* Tell br_nf_local_out this is a
247 nf_bridge
->mask
|= BRNF_BRIDGED_DNAT
;
248 skb
->dev
= nf_bridge
->physindev
;
250 __constant_htons(ETH_P_8021Q
)) {
251 skb_push(skb
, VLAN_HLEN
);
252 skb
->nh
.raw
-= VLAN_HLEN
;
254 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
,
256 br_nf_pre_routing_finish_bridge
,
260 memcpy(eth_hdr(skb
)->h_dest
, dev
->dev_addr
,
262 skb
->pkt_type
= PACKET_HOST
;
265 skb
->dst
= (struct dst_entry
*)&__fake_rtable
;
269 skb
->dev
= nf_bridge
->physindev
;
270 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
271 skb_push(skb
, VLAN_HLEN
);
272 skb
->nh
.raw
-= VLAN_HLEN
;
274 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
275 br_handle_frame_finish
, 1);
280 /* Some common code for IPv4/IPv6 */
281 static struct net_device
*setup_pre_routing(struct sk_buff
*skb
)
283 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
285 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
286 skb
->pkt_type
= PACKET_HOST
;
287 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
290 nf_bridge
->mask
|= BRNF_NF_BRIDGE_PREROUTING
;
291 nf_bridge
->physindev
= skb
->dev
;
292 skb
->dev
= bridge_parent(skb
->dev
);
297 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff anyway */
298 static int check_hbh_len(struct sk_buff
*skb
)
300 unsigned char *raw
= (u8
*)(skb
->nh
.ipv6h
+1);
302 int off
= raw
- skb
->nh
.raw
;
303 int len
= (raw
[1]+1)<<3;
305 if ((raw
+ len
) - skb
->data
> skb_headlen(skb
))
312 int optlen
= skb
->nh
.raw
[off
+1]+2;
314 switch (skb
->nh
.raw
[off
]) {
323 if (skb
->nh
.raw
[off
+1] != 4 || (off
&3) != 2)
325 pkt_len
= ntohl(*(u32
*)(skb
->nh
.raw
+off
+2));
326 if (pkt_len
<= IPV6_MAXPLEN
||
327 skb
->nh
.ipv6h
->payload_len
)
329 if (pkt_len
> skb
->len
- sizeof(struct ipv6hdr
))
331 if (pskb_trim_rcsum(skb
,
332 pkt_len
+sizeof(struct ipv6hdr
)))
350 /* Replicate the checks that IPv6 does on packet reception and pass the packet
351 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
352 static unsigned int br_nf_pre_routing_ipv6(unsigned int hook
,
353 struct sk_buff
*skb
, const struct net_device
*in
,
354 const struct net_device
*out
, int (*okfn
)(struct sk_buff
*))
358 struct nf_bridge_info
*nf_bridge
;
360 if (skb
->len
< sizeof(struct ipv6hdr
))
363 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
368 if (hdr
->version
!= 6)
371 pkt_len
= ntohs(hdr
->payload_len
);
373 if (pkt_len
|| hdr
->nexthdr
!= NEXTHDR_HOP
) {
374 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
376 if (pkt_len
+ sizeof(struct ipv6hdr
) < skb
->len
) {
377 if (__pskb_trim(skb
, pkt_len
+ sizeof(struct ipv6hdr
)))
379 if (skb
->ip_summed
== CHECKSUM_HW
)
380 skb
->ip_summed
= CHECKSUM_NONE
;
383 if (hdr
->nexthdr
== NEXTHDR_HOP
&& check_hbh_len(skb
))
386 nf_bridge_put(skb
->nf_bridge
);
387 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
389 if (!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
,
409 const struct net_device
*out
,
410 int (*okfn
)(struct sk_buff
*))
414 struct sk_buff
*skb
= *pskb
;
415 struct nf_bridge_info
*nf_bridge
;
416 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(*pskb
);
418 if (skb
->protocol
== __constant_htons(ETH_P_IPV6
) || IS_VLAN_IPV6
) {
420 if (!brnf_call_ip6tables
)
423 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
426 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
427 u8
*vhdr
= skb
->data
;
428 skb_pull(skb
, VLAN_HLEN
);
429 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
430 skb
->nh
.raw
+= VLAN_HLEN
;
432 return br_nf_pre_routing_ipv6(hook
, skb
, in
, out
, okfn
);
435 if (!brnf_call_iptables
)
439 if (skb
->protocol
!= __constant_htons(ETH_P_IP
) && !IS_VLAN_IP
)
442 if ((skb
= skb_share_check(*pskb
, GFP_ATOMIC
)) == NULL
)
445 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
446 u8
*vhdr
= skb
->data
;
447 skb_pull(skb
, VLAN_HLEN
);
448 skb_postpull_rcsum(skb
, vhdr
, VLAN_HLEN
);
449 skb
->nh
.raw
+= VLAN_HLEN
;
452 if (!pskb_may_pull(skb
, sizeof(struct iphdr
)))
456 if (iph
->ihl
< 5 || iph
->version
!= 4)
459 if (!pskb_may_pull(skb
, 4*iph
->ihl
))
463 if (ip_fast_csum((__u8
*)iph
, iph
->ihl
) != 0)
466 len
= ntohs(iph
->tot_len
);
467 if (skb
->len
< len
|| len
< 4*iph
->ihl
)
470 if (skb
->len
> len
) {
471 __pskb_trim(skb
, len
);
472 if (skb
->ip_summed
== CHECKSUM_HW
)
473 skb
->ip_summed
= CHECKSUM_NONE
;
476 nf_bridge_put(skb
->nf_bridge
);
477 if ((nf_bridge
= nf_bridge_alloc(skb
)) == NULL
)
479 if (!setup_pre_routing(skb
))
481 store_orig_dstaddr(skb
);
483 NF_HOOK(PF_INET
, NF_IP_PRE_ROUTING
, skb
, skb
->dev
, NULL
,
484 br_nf_pre_routing_finish
);
489 // IP_INC_STATS_BH(IpInHdrErrors);
495 /* PF_BRIDGE/LOCAL_IN ************************************************/
496 /* The packet is locally destined, which requires a real
497 * dst_entry, so detach the fake one. On the way up, the
498 * packet would pass through PRE_ROUTING again (which already
499 * took place when the packet entered the bridge), but we
500 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
501 * prevent this from happening. */
502 static unsigned int br_nf_local_in(unsigned int hook
, struct sk_buff
**pskb
,
503 const struct net_device
*in
, const struct net_device
*out
,
504 int (*okfn
)(struct sk_buff
*))
506 struct sk_buff
*skb
= *pskb
;
508 if (skb
->dst
== (struct dst_entry
*)&__fake_rtable
) {
509 dst_release(skb
->dst
);
517 /* PF_BRIDGE/FORWARD *************************************************/
518 static int br_nf_forward_finish(struct sk_buff
*skb
)
520 struct nf_bridge_info
*nf_bridge
= skb
->nf_bridge
;
521 struct net_device
*in
;
522 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
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
);
554 struct net_device
*parent
;
560 parent
= bridge_parent(out
);
564 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
569 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
570 skb_pull(*pskb
, VLAN_HLEN
);
571 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
574 nf_bridge
= skb
->nf_bridge
;
575 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
576 skb
->pkt_type
= PACKET_HOST
;
577 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
580 /* The physdev module checks on this */
581 nf_bridge
->mask
|= BRNF_BRIDGED
;
582 nf_bridge
->physoutdev
= skb
->dev
;
584 NF_HOOK(pf
, NF_IP_FORWARD
, skb
, bridge_parent(in
), parent
,
585 br_nf_forward_finish
);
590 static unsigned int br_nf_forward_arp(unsigned int hook
, struct sk_buff
**pskb
,
591 const struct net_device
*in
, const struct net_device
*out
,
592 int (*okfn
)(struct sk_buff
*))
594 struct sk_buff
*skb
= *pskb
;
595 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
596 struct net_device
**d
= (struct net_device
**)(skb
->cb
);
599 if (!brnf_call_arptables
)
603 if (skb
->protocol
!= __constant_htons(ETH_P_ARP
)) {
606 skb_pull(*pskb
, VLAN_HLEN
);
607 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
610 if (skb
->nh
.arph
->ar_pln
!= 4) {
612 skb_push(*pskb
, VLAN_HLEN
);
613 (*pskb
)->nh
.raw
-= VLAN_HLEN
;
617 *d
= (struct net_device
*)in
;
618 NF_HOOK(NF_ARP
, NF_ARP_FORWARD
, skb
, (struct net_device
*)in
,
619 (struct net_device
*)out
, br_nf_forward_finish
);
625 /* PF_BRIDGE/LOCAL_OUT ***********************************************/
626 static int br_nf_local_out_finish(struct sk_buff
*skb
)
628 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
629 skb_push(skb
, VLAN_HLEN
);
630 skb
->nh
.raw
-= VLAN_HLEN
;
633 NF_HOOK_THRESH(PF_BRIDGE
, NF_BR_LOCAL_OUT
, skb
, NULL
, skb
->dev
,
634 br_forward_finish
, NF_BR_PRI_FIRST
+ 1);
639 /* This function sees both locally originated IP packets and forwarded
640 * IP packets (in both cases the destination device is a bridge
641 * device). It also sees bridged-and-DNAT'ed packets.
642 * To be able to filter on the physical bridge devices (with the physdev
643 * module), we steal packets destined to a bridge device away from the
644 * PF_INET/FORWARD and PF_INET/OUTPUT hook functions, and give them back later,
645 * when we have determined the real output device. This is done in here.
647 * If (nf_bridge->mask & BRNF_BRIDGED_DNAT) then the packet is bridged
648 * and we fake the PF_BRIDGE/FORWARD hook. The function br_nf_forward()
649 * will then fake the PF_INET/FORWARD hook. br_nf_local_out() has priority
650 * NF_BR_PRI_FIRST, so no relevant PF_BRIDGE/INPUT functions have been nor
652 * Otherwise, if nf_bridge->physindev is NULL, the bridge-nf code never touched
653 * this packet before, and so the packet was locally originated. We fake
654 * the PF_INET/LOCAL_OUT hook.
655 * Finally, if nf_bridge->physindev isn't NULL, then the packet was IP routed,
656 * so we fake the PF_INET/FORWARD hook. ip_sabotage_out() makes sure
657 * even routed packets that didn't arrive on a bridge interface have their
658 * nf_bridge->physindev set. */
659 static unsigned int br_nf_local_out(unsigned int hook
, struct sk_buff
**pskb
,
660 const struct net_device
*in
, const struct net_device
*out
,
661 int (*okfn
)(struct sk_buff
*))
663 struct net_device
*realindev
, *realoutdev
;
664 struct sk_buff
*skb
= *pskb
;
665 struct nf_bridge_info
*nf_bridge
;
666 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
672 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
677 #ifdef CONFIG_NETFILTER_DEBUG
678 /* Sometimes we get packets with NULL ->dst here (for example,
679 * running a dhcp client daemon triggers this). This should now
680 * be fixed, but let's keep the check around. */
681 if (skb
->dst
== NULL
) {
682 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
687 nf_bridge
= skb
->nf_bridge
;
688 nf_bridge
->physoutdev
= skb
->dev
;
689 realindev
= nf_bridge
->physindev
;
691 /* Bridged, take PF_BRIDGE/FORWARD.
692 * (see big note in front of br_nf_pre_routing_finish) */
693 if (nf_bridge
->mask
& BRNF_BRIDGED_DNAT
) {
694 if (nf_bridge
->mask
& BRNF_PKT_TYPE
) {
695 skb
->pkt_type
= PACKET_OTHERHOST
;
696 nf_bridge
->mask
^= BRNF_PKT_TYPE
;
698 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
699 skb_push(skb
, VLAN_HLEN
);
700 skb
->nh
.raw
-= VLAN_HLEN
;
703 NF_HOOK(PF_BRIDGE
, NF_BR_FORWARD
, skb
, realindev
,
704 skb
->dev
, br_forward_finish
);
707 realoutdev
= bridge_parent(skb
->dev
);
711 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
712 /* iptables should match -o br0.x */
713 if (nf_bridge
->netoutdev
)
714 realoutdev
= nf_bridge
->netoutdev
;
716 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
717 skb_pull(skb
, VLAN_HLEN
);
718 (*pskb
)->nh
.raw
+= VLAN_HLEN
;
720 /* IP forwarded traffic has a physindev, locally
721 * generated traffic hasn't. */
722 if (realindev
!= NULL
) {
723 if (!(nf_bridge
->mask
& BRNF_DONT_TAKE_PARENT
) ) {
724 struct net_device
*parent
= bridge_parent(realindev
);
729 NF_HOOK_THRESH(pf
, NF_IP_FORWARD
, skb
, realindev
,
730 realoutdev
, br_nf_local_out_finish
,
731 NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
+ 1);
733 NF_HOOK_THRESH(pf
, NF_IP_LOCAL_OUT
, skb
, realindev
,
734 realoutdev
, br_nf_local_out_finish
,
735 NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
+ 1);
743 /* PF_BRIDGE/POST_ROUTING ********************************************/
744 static unsigned int br_nf_post_routing(unsigned int hook
, struct sk_buff
**pskb
,
745 const struct net_device
*in
, const struct net_device
*out
,
746 int (*okfn
)(struct sk_buff
*))
748 struct sk_buff
*skb
= *pskb
;
749 struct nf_bridge_info
*nf_bridge
= (*pskb
)->nf_bridge
;
750 struct vlan_ethhdr
*hdr
= vlan_eth_hdr(skb
);
751 struct net_device
*realoutdev
= bridge_parent(skb
->dev
);
754 #ifdef CONFIG_NETFILTER_DEBUG
755 /* Be very paranoid. This probably won't happen anymore, but let's
756 * keep the check just to be sure... */
757 if (skb
->mac
.raw
< skb
->head
|| skb
->mac
.raw
+ ETH_HLEN
> skb
->data
) {
758 printk(KERN_CRIT
"br_netfilter: Argh!! br_nf_post_routing: "
759 "bad mac.raw pointer.");
770 if (skb
->protocol
== __constant_htons(ETH_P_IP
) || IS_VLAN_IP
)
775 #ifdef CONFIG_NETFILTER_DEBUG
776 if (skb
->dst
== NULL
) {
777 printk(KERN_CRIT
"br_netfilter: skb->dst == NULL.");
782 /* We assume any code from br_dev_queue_push_xmit onwards doesn't care
783 * about the value of skb->pkt_type. */
784 if (skb
->pkt_type
== PACKET_OTHERHOST
) {
785 skb
->pkt_type
= PACKET_HOST
;
786 nf_bridge
->mask
|= BRNF_PKT_TYPE
;
789 if (skb
->protocol
== __constant_htons(ETH_P_8021Q
)) {
790 skb_pull(skb
, VLAN_HLEN
);
791 skb
->nh
.raw
+= VLAN_HLEN
;
794 nf_bridge_save_header(skb
);
796 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
797 if (nf_bridge
->netoutdev
)
798 realoutdev
= nf_bridge
->netoutdev
;
800 NF_HOOK(pf
, NF_IP_POST_ROUTING
, skb
, NULL
, realoutdev
,
801 br_dev_queue_push_xmit
);
805 #ifdef CONFIG_NETFILTER_DEBUG
807 if (skb
->dev
!= NULL
) {
808 printk("[%s]", skb
->dev
->name
);
810 printk("[%s]", realoutdev
->name
);
812 printk(" head:%p, raw:%p, data:%p\n", skb
->head
, skb
->mac
.raw
,
819 /* IP/SABOTAGE *****************************************************/
820 /* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
821 * for the second time. */
822 static unsigned int ip_sabotage_in(unsigned int hook
, struct sk_buff
**pskb
,
823 const struct net_device
*in
, const struct net_device
*out
,
824 int (*okfn
)(struct sk_buff
*))
826 if ((*pskb
)->nf_bridge
&&
827 !((*pskb
)->nf_bridge
->mask
& BRNF_NF_BRIDGE_PREROUTING
)) {
834 /* Postpone execution of PF_INET(6)/FORWARD, PF_INET(6)/LOCAL_OUT
835 * and PF_INET(6)/POST_ROUTING until we have done the forwarding
836 * decision in the bridge code and have determined nf_bridge->physoutdev. */
837 static unsigned int ip_sabotage_out(unsigned int hook
, struct sk_buff
**pskb
,
838 const struct net_device
*in
, const struct net_device
*out
,
839 int (*okfn
)(struct sk_buff
*))
841 struct sk_buff
*skb
= *pskb
;
843 if ((out
->hard_start_xmit
== br_dev_xmit
&&
844 okfn
!= br_nf_forward_finish
&&
845 okfn
!= br_nf_local_out_finish
&&
846 okfn
!= br_dev_queue_push_xmit
)
847 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
848 || ((out
->priv_flags
& IFF_802_1Q_VLAN
) &&
849 VLAN_DEV_INFO(out
)->real_dev
->hard_start_xmit
== br_dev_xmit
)
852 struct nf_bridge_info
*nf_bridge
;
854 if (!skb
->nf_bridge
) {
856 /* This code is executed while in the IP(v6) stack,
857 the version should be 4 or 6. We can't use
858 skb->protocol because that isn't set on
859 PF_INET(6)/LOCAL_OUT. */
860 struct iphdr
*ip
= skb
->nh
.iph
;
862 if (ip
->version
== 4 && !brnf_call_iptables
)
864 else if (ip
->version
== 6 && !brnf_call_ip6tables
)
867 if (hook
== NF_IP_POST_ROUTING
)
869 if (!nf_bridge_alloc(skb
))
873 nf_bridge
= skb
->nf_bridge
;
875 /* This frame will arrive on PF_BRIDGE/LOCAL_OUT and we
876 * will need the indev then. For a brouter, the real indev
877 * can be a bridge port, so we make sure br_nf_local_out()
878 * doesn't use the bridge parent of the indev by using
879 * the BRNF_DONT_TAKE_PARENT mask. */
880 if (hook
== NF_IP_FORWARD
&& nf_bridge
->physindev
== NULL
) {
881 nf_bridge
->mask
|= BRNF_DONT_TAKE_PARENT
;
882 nf_bridge
->physindev
= (struct net_device
*)in
;
884 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
885 /* the iptables outdev is br0.x, not br0 */
886 if (out
->priv_flags
& IFF_802_1Q_VLAN
)
887 nf_bridge
->netoutdev
= (struct net_device
*)out
;
895 /* For br_nf_local_out we need (prio = NF_BR_PRI_FIRST), to insure that innocent
896 * PF_BRIDGE/NF_BR_LOCAL_OUT functions don't get bridged traffic as input.
897 * For br_nf_post_routing, we need (prio = NF_BR_PRI_LAST), because
898 * ip_refrag() can return NF_STOLEN. */
899 static struct nf_hook_ops br_nf_ops
[] = {
900 { .hook
= br_nf_pre_routing
,
901 .owner
= THIS_MODULE
,
903 .hooknum
= NF_BR_PRE_ROUTING
,
904 .priority
= NF_BR_PRI_BRNF
, },
905 { .hook
= br_nf_local_in
,
906 .owner
= THIS_MODULE
,
908 .hooknum
= NF_BR_LOCAL_IN
,
909 .priority
= NF_BR_PRI_BRNF
, },
910 { .hook
= br_nf_forward_ip
,
911 .owner
= THIS_MODULE
,
913 .hooknum
= NF_BR_FORWARD
,
914 .priority
= NF_BR_PRI_BRNF
- 1, },
915 { .hook
= br_nf_forward_arp
,
916 .owner
= THIS_MODULE
,
918 .hooknum
= NF_BR_FORWARD
,
919 .priority
= NF_BR_PRI_BRNF
, },
920 { .hook
= br_nf_local_out
,
921 .owner
= THIS_MODULE
,
923 .hooknum
= NF_BR_LOCAL_OUT
,
924 .priority
= NF_BR_PRI_FIRST
, },
925 { .hook
= br_nf_post_routing
,
926 .owner
= THIS_MODULE
,
928 .hooknum
= NF_BR_POST_ROUTING
,
929 .priority
= NF_BR_PRI_LAST
, },
930 { .hook
= ip_sabotage_in
,
931 .owner
= THIS_MODULE
,
933 .hooknum
= NF_IP_PRE_ROUTING
,
934 .priority
= NF_IP_PRI_FIRST
, },
935 { .hook
= ip_sabotage_in
,
936 .owner
= THIS_MODULE
,
938 .hooknum
= NF_IP6_PRE_ROUTING
,
939 .priority
= NF_IP6_PRI_FIRST
, },
940 { .hook
= ip_sabotage_out
,
941 .owner
= THIS_MODULE
,
943 .hooknum
= NF_IP_FORWARD
,
944 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_FORWARD
, },
945 { .hook
= ip_sabotage_out
,
946 .owner
= THIS_MODULE
,
948 .hooknum
= NF_IP6_FORWARD
,
949 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_FORWARD
, },
950 { .hook
= ip_sabotage_out
,
951 .owner
= THIS_MODULE
,
953 .hooknum
= NF_IP_LOCAL_OUT
,
954 .priority
= NF_IP_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
955 { .hook
= ip_sabotage_out
,
956 .owner
= THIS_MODULE
,
958 .hooknum
= NF_IP6_LOCAL_OUT
,
959 .priority
= NF_IP6_PRI_BRIDGE_SABOTAGE_LOCAL_OUT
, },
960 { .hook
= ip_sabotage_out
,
961 .owner
= THIS_MODULE
,
963 .hooknum
= NF_IP_POST_ROUTING
,
964 .priority
= NF_IP_PRI_FIRST
, },
965 { .hook
= ip_sabotage_out
,
966 .owner
= THIS_MODULE
,
968 .hooknum
= NF_IP6_POST_ROUTING
,
969 .priority
= NF_IP6_PRI_FIRST
, },
974 int brnf_sysctl_call_tables(ctl_table
*ctl
, int write
, struct file
* filp
,
975 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
)
979 ret
= proc_dointvec(ctl
, write
, filp
, buffer
, lenp
, ppos
);
981 if (write
&& *(int *)(ctl
->data
))
982 *(int *)(ctl
->data
) = 1;
986 static ctl_table brnf_table
[] = {
988 .ctl_name
= NET_BRIDGE_NF_CALL_ARPTABLES
,
989 .procname
= "bridge-nf-call-arptables",
990 .data
= &brnf_call_arptables
,
991 .maxlen
= sizeof(int),
993 .proc_handler
= &brnf_sysctl_call_tables
,
996 .ctl_name
= NET_BRIDGE_NF_CALL_IPTABLES
,
997 .procname
= "bridge-nf-call-iptables",
998 .data
= &brnf_call_iptables
,
999 .maxlen
= sizeof(int),
1001 .proc_handler
= &brnf_sysctl_call_tables
,
1004 .ctl_name
= NET_BRIDGE_NF_CALL_IP6TABLES
,
1005 .procname
= "bridge-nf-call-ip6tables",
1006 .data
= &brnf_call_ip6tables
,
1007 .maxlen
= sizeof(int),
1009 .proc_handler
= &brnf_sysctl_call_tables
,
1012 .ctl_name
= NET_BRIDGE_NF_FILTER_VLAN_TAGGED
,
1013 .procname
= "bridge-nf-filter-vlan-tagged",
1014 .data
= &brnf_filter_vlan_tagged
,
1015 .maxlen
= sizeof(int),
1017 .proc_handler
= &brnf_sysctl_call_tables
,
1022 static ctl_table brnf_bridge_table
[] = {
1024 .ctl_name
= NET_BRIDGE
,
1025 .procname
= "bridge",
1027 .child
= brnf_table
,
1032 static ctl_table brnf_net_table
[] = {
1034 .ctl_name
= CTL_NET
,
1037 .child
= brnf_bridge_table
,
1043 int br_netfilter_init(void)
1047 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++) {
1050 if ((ret
= nf_register_hook(&br_nf_ops
[i
])) >= 0)
1054 nf_unregister_hook(&br_nf_ops
[i
]);
1059 #ifdef CONFIG_SYSCTL
1060 brnf_sysctl_header
= register_sysctl_table(brnf_net_table
, 0);
1061 if (brnf_sysctl_header
== NULL
) {
1062 printk(KERN_WARNING
"br_netfilter: can't register to sysctl.\n");
1063 for (i
= 0; i
< ARRAY_SIZE(br_nf_ops
); i
++)
1064 nf_unregister_hook(&br_nf_ops
[i
]);
1069 printk(KERN_NOTICE
"Bridge firewalling registered\n");
1074 void br_netfilter_fini(void)
1078 for (i
= ARRAY_SIZE(br_nf_ops
) - 1; i
>= 0; i
--)
1079 nf_unregister_hook(&br_nf_ops
[i
]);
1080 #ifdef CONFIG_SYSCTL
1081 unregister_sysctl_table(brnf_sysctl_header
);